Skip to content

Commit

Permalink
[CImgPlugin] Add fetchable CImg and remove embedded cimg (#4357)
Browse files Browse the repository at this point in the history
* support interface-type for auto_target_properties install macro

* try updating install macro

* try updating install macro again

* use (only) find_package for CImg

* fix image compilation for newer version of cimg

* remove cimg src

* export mutex symbol with a .def file for windows

* add comment

* add fetchable cimg

* fix packaging

* fix image_gui

* fix image_type comparison

---------

Co-authored-by: erik pernod <[email protected]>
  • Loading branch information
fredroy and epernod authored Dec 21, 2023
1 parent 0e3766a commit 7140f1d
Show file tree
Hide file tree
Showing 87 changed files with 128 additions and 206,463 deletions.
1 change: 1 addition & 0 deletions Sofa/framework/Config/cmake/SofaMacrosInstall.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ macro(sofa_auto_set_target_version)
endif()
set_target_properties(${target} PROPERTIES VERSION "${version}")
endif()

set(${sofa_target_name_upper}_VERSION "${version}")
set(PROJECT_VERSION "${version}") # warning: dangerous to touch this variable?
endforeach()
Expand Down
2 changes: 2 additions & 0 deletions applications/plugins/CImgPlugin/CImgPluginConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ set(CIMGPLUGIN_HAVE_ZLIB @CIMGPLUGIN_HAVE_ZLIB@)

find_package(Sofa.Helper QUIET REQUIRED)
find_package(Sofa.DefaultType QUIET REQUIRED)

set(CImg_INCLUDE_DIRS "@CImg_INCLUDE_DIRS@")
find_package(CImg QUIET REQUIRED)

if(CIMGPLUGIN_HAVE_JPEG)
Expand Down
45 changes: 40 additions & 5 deletions applications/plugins/CImgPlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,35 @@ set(SOURCE_FILES
src/CImgPlugin/initCImgPlugin.cpp
)

add_subdirectory(extlibs/CImg)

sofa_find_package(Sofa.Helper REQUIRED)
sofa_find_package(Sofa.DefaultType REQUIRED)
sofa_find_package(CImg REQUIRED)

# add FindCImg.cmake
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindCImg.cmake ${CMAKE_BINARY_DIR}/lib/cmake/FindCImg.cmake COPYONLY)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindCImg.cmake DESTINATION lib/cmake/${PROJECT_NAME} COMPONENT headers)

sofa_find_package(CImg QUIET)

if(NOT CImg_FOUND AND SOFA_ALLOW_FETCH_DEPENDENCIES)
message("CImgPlugin: DEPENDENCY CImg NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is ON, fetching CImg...")

include(FetchContent)
FetchContent_Declare(CImg
GIT_REPOSITORY https://github.com/GreycLab/CImg
GIT_TAG v.3.3.2
)

FetchContent_GetProperties(CImg)
if(NOT CImg_POPULATED)
FetchContent_Populate(CImg)

set(CIMG_H_DIR ${cimg_SOURCE_DIR})
sofa_find_package(CImg REQUIRED)
endif()
elseif (NOT CImg_FOUND)
message(FATAL_ERROR "CImgPlugin: DEPENDENCY CImg NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install cimg-dev, or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.")
endif()

# OS X only: if the user installed its own JPEG/PNG lib (typically with homebrew/port),
# it will allow to use those instead of those present in XCode's frameworks
Expand Down Expand Up @@ -67,23 +91,34 @@ add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})

target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC "$<BUILD_INTERFACE:${CImg_INCLUDE_DIRS}>")
target_compile_options(${PROJECT_NAME} PUBLIC ${CIMG_CFLAGS})
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Helper Sofa.DefaultType)
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Helper Sofa.DefaultType )
target_link_libraries(${PROJECT_NAME} INTERFACE CImg)
target_link_libraries(${PROJECT_NAME} PUBLIC ${DEP_TARGETS})

if(SOFA_BUILD_RELEASE_PACKAGE OR CMAKE_SYSTEM_NAME STREQUAL Windows)
sofa_install_libraries(TARGETS ${DEP_TARGETS})
endif()

if(MSVC)
target_link_options(${PROJECT_NAME} PRIVATE "/DEF:${CMAKE_CURRENT_SOURCE_DIR}/exports.def")
endif()

## Install rules for the library; CMake package configurations files
sofa_create_package_with_targets(
PACKAGE_NAME ${PROJECT_NAME}
PACKAGE_VERSION ${PROJECT_VERSION}
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
TARGETS ${PROJECT_NAME} CImg AUTO_SET_TARGET_PROPERTIES
INCLUDE_SOURCE_DIR "src"
INCLUDE_INSTALL_DIR "CImgPlugin"
RELOCATABLE "plugins"
)

install(FILES
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindCImg.cmake"
DESTINATION lib/cmake/Modules
COMPONENT headers
)

# Tests
# If SOFA_BUILD_TESTS exists and is OFF, then these tests will be auto-disabled
cmake_dependent_option(CIMGPLUGIN_BUILD_TESTS "Compile the automatic tests" ON "SOFA_BUILD_TESTS OR NOT DEFINED SOFA_BUILD_TESTS" OFF)
Expand Down
69 changes: 69 additions & 0 deletions applications/plugins/CImgPlugin/cmake/FindCImg.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#[=======================================================================[.rst:
FindCImg
-------
Finds the CImg library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``CImg``
The CImg library
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``CImg_FOUND``
True if the system has the CImg library.
``CImg_VERSION``
The version of the CImg library which was found.
``CImg_INCLUDE_DIRS``
Include directories needed to use CImg.
Cache Variables
^^^^^^^^^^^^^^^
The following cache variables may also be set:
``CImg_INCLUDE_DIR``
The directory containing ``CImg.h``.
#]=======================================================================]

cmake_minimum_required(VERSION 3.9)

find_path(CIMG_H_DIR
NAMES CImg.h
HINTS ${CMAKE_INSTALL_PREFIX} ${CIMG_DIR}
PATH_SUFFIXES include include/linux
)

if(NOT CIMG_H_DIR)
set(CImg_FOUND FALSE)
else()
set(CImg_FOUND TRUE)

set(CImg_INCLUDE_DIR ${CIMG_H_DIR} CACHE STRING "")
set(CImg_INCLUDE_DIRS ${CImg_INCLUDE_DIR})

file(READ "${CIMG_H_DIR}/CImg.h" header)
string(REGEX MATCH "#define cimg_version ([0-9a-zA-Z\.]+)" _ "${header}")
set(CImg_VERSION "${CMAKE_MATCH_1}")

if(NOT TARGET CImg)
add_library(CImg INTERFACE)
endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CImg
FOUND_VAR CImg_FOUND
REQUIRED_VARS
CImg_INCLUDE_DIRS
VERSION_VAR CImg_VERSION
)
3 changes: 3 additions & 0 deletions applications/plugins/CImgPlugin/exports.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LIBRARY CIMGPLUGIN
EXPORTS
Mutex_attr @1
Loading

0 comments on commit 7140f1d

Please sign in to comment.