|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright Contributors to the OpenColorIO Project. |
| 3 | +# |
| 4 | +# Locate or install Imath |
| 5 | +# |
| 6 | +# Variables defined by this module: |
| 7 | +# Imath_FOUND - If FALSE, do not try to link to ilmbase |
| 8 | +# Imath_LIBRARY - Imath library to link to |
| 9 | +# Imath_INCLUDE_DIR - Where to find ImathConfig.h |
| 10 | +# Imath_VERSION - The version of the library |
| 11 | +# |
| 12 | +# Targets defined by this module: |
| 13 | +# Imath::Imath - IMPORTED target, if found |
| 14 | +# |
| 15 | +# By default, the dynamic libraries of Imath will be found. To find the |
| 16 | +# static ones instead, you must set the Imath_STATIC_LIBRARY variable to |
| 17 | +# TRUE before calling find_package(Imath ...). |
| 18 | +# |
| 19 | +# If Imath is not installed in a standard path, you can use the |
| 20 | +# Imath_ROOT variable to tell CMake where to find it. If it is not found |
| 21 | +# and OCIO_INSTALL_EXT_PACKAGES is set to MISSING or ALL, Imath will be |
| 22 | +# downloaded, built, and statically-linked into libOpenColorIO at build time. |
| 23 | +# |
| 24 | + |
| 25 | +############################################################################### |
| 26 | +### Try to find package ### |
| 27 | + |
| 28 | +#if(NOT OCIO_INSTALL_EXT_PACKAGES STREQUAL ALL) |
| 29 | + set(_Imath_REQUIRED_VARS Imath_LIBRARY) |
| 30 | + set(_Imath_LIB_VER "${Imath_FIND_VERSION_MAJOR}_${Imath_FIND_VERSION_MINOR}") |
| 31 | + |
| 32 | + if(NOT DEFINED Imath_ROOT) |
| 33 | + # Search for ImathConfig.cmake |
| 34 | + find_package(Imath ${Imath_FIND_VERSION} CONFIG QUIET) |
| 35 | + endif() |
| 36 | + |
| 37 | + if(Imath_FOUND) |
| 38 | + get_target_property(Imath_LIBRARY Imath::Imath LOCATION) |
| 39 | + else() |
| 40 | + list(APPEND _Imath_REQUIRED_VARS Imath_INCLUDE_DIR) |
| 41 | + |
| 42 | + # Search for Imath.pc |
| 43 | + find_package(PkgConfig QUIET) |
| 44 | + pkg_check_modules(PC_Imath QUIET "Imath>=${Imath_FIND_VERSION}") |
| 45 | + |
| 46 | + # Find include directory |
| 47 | + find_path(Imath_INCLUDE_DIR |
| 48 | + NAMES |
| 49 | + Imath/ImathConfig.h |
| 50 | + HINTS |
| 51 | + ${Imath_ROOT} |
| 52 | + ${PC_Imath_INCLUDE_DIRS} |
| 53 | + PATH_SUFFIXES |
| 54 | + include |
| 55 | + ) |
| 56 | + |
| 57 | + # Lib names to search for |
| 58 | + set(_Imath_LIB_NAMES "Imath-${_Imath_LIB_VER}" Imath) |
| 59 | + if(BUILD_TYPE_DEBUG) |
| 60 | + # Prefer Debug lib names |
| 61 | + list(INSERT _Imath_LIB_NAMES 0 "Imath-${_Imath_LIB_VER}_d") |
| 62 | + endif() |
| 63 | + |
| 64 | + if(Imath_STATIC_LIBRARY) |
| 65 | + # Prefer static lib names |
| 66 | + set(_Imath_STATIC_LIB_NAMES |
| 67 | + "${CMAKE_STATIC_LIBRARY_PREFIX}Imath-${_Imath_LIB_VER}${CMAKE_STATIC_LIBRARY_SUFFIX}" |
| 68 | + "${CMAKE_STATIC_LIBRARY_PREFIX}Imath${CMAKE_STATIC_LIBRARY_SUFFIX}" |
| 69 | + ) |
| 70 | + if(BUILD_TYPE_DEBUG) |
| 71 | + # Prefer static Debug lib names |
| 72 | + list(INSERT _Imath_STATIC_LIB_NAMES 0 |
| 73 | + "${CMAKE_STATIC_LIBRARY_PREFIX}Imath-${_Imath_LIB_VER}_d${CMAKE_STATIC_LIBRARY_SUFFIX}") |
| 74 | + endif() |
| 75 | + endif() |
| 76 | + |
| 77 | + # Find library |
| 78 | + find_library(Imath_LIBRARY |
| 79 | + NAMES |
| 80 | + ${_Imath_STATIC_LIB_NAMES} |
| 81 | + ${_Imath_LIB_NAMES} |
| 82 | + HINTS |
| 83 | + ${Imath_ROOT} |
| 84 | + ${PC_Imath_LIBRARY_DIRS} |
| 85 | + PATH_SUFFIXES |
| 86 | + lib64 lib |
| 87 | + ) |
| 88 | + |
| 89 | + # Get version from config header file |
| 90 | + if(Imath_INCLUDE_DIR) |
| 91 | + if(EXISTS "${Imath_INCLUDE_DIR}/Imath/ImathConfig.h") |
| 92 | + set(_Imath_CONFIG "${Imath_INCLUDE_DIR}/Imath/ImathConfig.h") |
| 93 | + endif() |
| 94 | + endif() |
| 95 | + |
| 96 | + if(_Imath_CONFIG) |
| 97 | + file(STRINGS "${_Imath_CONFIG}" _Imath_VER_SEARCH |
| 98 | + REGEX "^[ \t]*#define[ \t]+IMATH_VERSION_STRING[ \t]+\"[.0-9]+\".*$") |
| 99 | + if(_Imath_VER_SEARCH) |
| 100 | + string(REGEX REPLACE ".*#define[ \t]+IMATH_VERSION_STRING[ \t]+\"([.0-9]+)\".*" |
| 101 | + "\\1" Imath_VERSION "${_Imath_VER_SEARCH}") |
| 102 | + endif() |
| 103 | + elseif(PC_Imath_FOUND) |
| 104 | + set(Imath_VERSION "${PC_Imath_VERSION}") |
| 105 | + endif() |
| 106 | + endif() |
| 107 | + |
| 108 | + # Override REQUIRED if package can be installed |
| 109 | + #if(OCIO_INSTALL_EXT_PACKAGES STREQUAL MISSING) |
| 110 | + set(Imath_FIND_REQUIRED FALSE) |
| 111 | + # endif() |
| 112 | + |
| 113 | + include(FindPackageHandleStandardArgs) |
| 114 | + find_package_handle_standard_args(Imath |
| 115 | + REQUIRED_VARS |
| 116 | + ${_Imath_REQUIRED_VARS} |
| 117 | + VERSION_VAR |
| 118 | + Imath_VERSION |
| 119 | + ) |
| 120 | +#endif() |
| 121 | + |
| 122 | +############################################################################### |
| 123 | +### Create target |
| 124 | + |
| 125 | +if (NOT TARGET Imath::Imath) |
| 126 | + add_library(Imath::Imath UNKNOWN IMPORTED GLOBAL) |
| 127 | + add_library(Imath::ImathConfig INTERFACE IMPORTED GLOBAL) |
| 128 | + set(_Imath_TARGET_CREATE TRUE) |
| 129 | +endif() |
| 130 | + |
| 131 | +############################################################################### |
| 132 | +### Install package from source ### |
| 133 | + |
| 134 | +if(NOT Imath_FOUND AND NOT OCIO_INSTALL_EXT_PACKAGES STREQUAL NONE) |
| 135 | + include(ExternalProject) |
| 136 | + include(GNUInstallDirs) |
| 137 | + |
| 138 | + set(_EXT_DIST_ROOT "${CMAKE_BINARY_DIR}/ext/dist") |
| 139 | + set(_EXT_BUILD_ROOT "${CMAKE_BINARY_DIR}/ext/build") |
| 140 | + |
| 141 | + # Set find_package standard args |
| 142 | + set(Imath_FOUND TRUE) |
| 143 | + if(_Imath_ExternalProject_VERSION) |
| 144 | + set(Imath_VERSION ${_Imath_ExternalProject_VERSION}) |
| 145 | + else() |
| 146 | + set(Imath_VERSION ${Imath_FIND_VERSION}) |
| 147 | + endif() |
| 148 | + set(Imath_INCLUDE_DIR "${_EXT_DIST_ROOT}/${CMAKE_INSTALL_INCLUDEDIR}") |
| 149 | + |
| 150 | + # Set the expected library name |
| 151 | + if(BUILD_TYPE_DEBUG) |
| 152 | + set(_Imath_LIB_SUFFIX "_d") |
| 153 | + endif() |
| 154 | + |
| 155 | + include(VersionUtils) |
| 156 | + split_version_string(${Imath_VERSION} _Imath_ExternalProject) |
| 157 | + |
| 158 | + set(_Imath_LIB_VER "${_Imath_ExternalProject_VERSION_MAJOR}_${_Imath_ExternalProject_VERSION_MINOR}") |
| 159 | + |
| 160 | + set(Imath_LIBRARY |
| 161 | + "${_EXT_DIST_ROOT}/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}Imath-${_Imath_LIB_VER}${_Imath_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}") |
| 162 | + |
| 163 | + if(_Imath_TARGET_CREATE) |
| 164 | + if(MSVC) |
| 165 | + set(Imath_CXX_FLAGS "${Imath_CXX_FLAGS} /EHsc") |
| 166 | + endif() |
| 167 | + |
| 168 | + string(STRIP "${Imath_CXX_FLAGS}" Imath_CXX_FLAGS) |
| 169 | + |
| 170 | + set(Imath_CMAKE_ARGS |
| 171 | + ${Imath_CMAKE_ARGS} |
| 172 | + -DCMAKE_CXX_VISIBILITY_PRESET=${CMAKE_CXX_VISIBILITY_PRESET} |
| 173 | + -DCMAKE_VISIBILITY_INLINES_HIDDEN=${CMAKE_VISIBILITY_INLINES_HIDDEN} |
| 174 | + -DCMAKE_POSITION_INDEPENDENT_CODE=ON |
| 175 | + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} |
| 176 | + -DCMAKE_CXX_FLAGS=${Imath_CXX_FLAGS} |
| 177 | + -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} |
| 178 | + -DCMAKE_INSTALL_MESSAGE=${CMAKE_INSTALL_MESSAGE} |
| 179 | + -DCMAKE_INSTALL_PREFIX=${_EXT_DIST_ROOT} |
| 180 | + -DCMAKE_INSTALL_BINDIR=${CMAKE_INSTALL_BINDIR} |
| 181 | + -DCMAKE_INSTALL_DATADIR=${CMAKE_INSTALL_DATADIR} |
| 182 | + -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} |
| 183 | + -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} |
| 184 | + -DCMAKE_OBJECT_PATH_MAX=${CMAKE_OBJECT_PATH_MAX} |
| 185 | + -DBUILD_SHARED_LIBS=OFF |
| 186 | + -DBUILD_TESTING=OFF |
| 187 | + -DPYTHON=OFF |
| 188 | + -DDOCS=OFF |
| 189 | + -DIMATH_HALF_USE_LOOKUP_TABLE=OFF |
| 190 | + ) |
| 191 | + |
| 192 | + if(CMAKE_TOOLCHAIN_FILE) |
| 193 | + set(Imath_CMAKE_ARGS |
| 194 | + ${Imath_CMAKE_ARGS} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}) |
| 195 | + endif() |
| 196 | + |
| 197 | + if(APPLE) |
| 198 | + string(REPLACE ";" "$<SEMICOLON>" ESCAPED_CMAKE_OSX_ARCHITECTURES "${CMAKE_OSX_ARCHITECTURES}") |
| 199 | + |
| 200 | + set(Imath_CMAKE_ARGS |
| 201 | + ${Imath_CMAKE_ARGS} |
| 202 | + -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET} |
| 203 | + -DCMAKE_OSX_ARCHITECTURES=${ESCAPED_CMAKE_OSX_ARCHITECTURES} |
| 204 | + ) |
| 205 | + endif() |
| 206 | + |
| 207 | + if (ANDROID) |
| 208 | + set(Imath_CMAKE_ARGS |
| 209 | + ${Imath_CMAKE_ARGS} |
| 210 | + -DANDROID_PLATFORM=${ANDROID_PLATFORM} |
| 211 | + -DANDROID_ABI=${ANDROID_ABI} |
| 212 | + -DANDROID_STL=${ANDROID_STL}) |
| 213 | + endif() |
| 214 | + |
| 215 | + # Hack to let imported target be built from ExternalProject_Add |
| 216 | + file(MAKE_DIRECTORY ${Imath_INCLUDE_DIR}) |
| 217 | + |
| 218 | + ExternalProject_Add(imath_install |
| 219 | + GIT_REPOSITORY "https://github.com/AcademySoftwareFoundation/Imath.git" |
| 220 | + GIT_TAG "v${Imath_VERSION}" |
| 221 | + GIT_CONFIG advice.detachedHead=false |
| 222 | + GIT_SHALLOW TRUE |
| 223 | + PREFIX "${_EXT_BUILD_ROOT}/Imath" |
| 224 | + BUILD_BYPRODUCTS ${Imath_LIBRARY} |
| 225 | + CMAKE_ARGS ${Imath_CMAKE_ARGS} |
| 226 | + EXCLUDE_FROM_ALL TRUE |
| 227 | + BUILD_COMMAND "" |
| 228 | + INSTALL_COMMAND |
| 229 | + ${CMAKE_COMMAND} --build . |
| 230 | + --config ${CMAKE_BUILD_TYPE} |
| 231 | + --target install |
| 232 | + --parallel |
| 233 | + ) |
| 234 | + |
| 235 | + add_dependencies(Imath::Imath imath_install) |
| 236 | + |
| 237 | + message(STATUS "Installing Imath: ${Imath_LIBRARY} (version \"${Imath_VERSION}\")") |
| 238 | + endif() |
| 239 | +endif() |
| 240 | + |
| 241 | +############################################################################### |
| 242 | +### Configure target ### |
| 243 | + |
| 244 | +if(_Imath_TARGET_CREATE) |
| 245 | + file(MAKE_DIRECTORY ${Imath_INCLUDE_DIR}/Imath) |
| 246 | + |
| 247 | + set_target_properties(Imath::Imath PROPERTIES |
| 248 | + IMPORTED_LOCATION ${Imath_LIBRARY} |
| 249 | + INTERFACE_INCLUDE_DIRECTORIES "${Imath_INCLUDE_DIR};${Imath_INCLUDE_DIR}/Imath" |
| 250 | + ) |
| 251 | + set_target_properties(Imath::ImathConfig PROPERTIES |
| 252 | + INTERFACE_INCLUDE_DIRECTORIES "${Imath_INCLUDE_DIR};${Imath_INCLUDE_DIR}/Imath" |
| 253 | + ) |
| 254 | + |
| 255 | + mark_as_advanced(Imath_INCLUDE_DIR Imath_LIBRARY Imath_VERSION) |
| 256 | +endif() |
0 commit comments