de4c0aba48c8ed7126b6217bb268a6d9fd677183
[blender.git] / build_files / build_environment / patches / cmake / modules / FindIlmBase.cmake
1 # Module to find IlmBase
2 #
3 # This module will first look into the directories defined by the variables:
4 #   - ILMBASE_HOME, ILMBASE_VERSION, ILMBASE_LIB_AREA
5 #
6 # It also supports non-standard names for the library components.
7 #
8 # To use a custom IlmBase:
9 #   - Set the variable ILMBASE_CUSTOM to True
10 #   - Set the variable ILMBASE_CUSTOM_LIBRARIES to a list of the libraries to
11 #     use, e.g. "SpiImath SpiHalf SpiIlmThread SpiIex"
12 #   - Optionally set the variable ILMBASE_CUSTOM_INCLUDE_DIR to any
13 #     particularly weird place that the OpenEXR/*.h files may be found
14 #   - Optionally set the variable ILMBASE_CUSTOM_LIB_DIR to any
15 #     particularly weird place that the libraries files may be found
16 #
17 # This module defines the following variables:
18 #
19 # ILMBASE_INCLUDE_DIR - where to find half.h, IlmBaseConfig.h, etc.
20 # ILMBASE_LIBRARIES   - list of libraries to link against when using IlmBase.
21 # ILMBASE_FOUND       - True if IlmBase was found.
22
23 # Other standarnd issue macros
24 include(FindPackageHandleStandardArgs)
25 include(FindPackageMessage)
26 include(SelectLibraryConfigurations)
27
28
29 if( ILMBASE_USE_STATIC_LIBS )
30   set( _ilmbase_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
31   if(WIN32)
32     set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
33   else()
34     set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
35   endif()
36 endif()
37
38 # Macro to assemble a helper state variable
39 macro(SET_STATE_VAR varname)
40   set(tmp_ilmbaselibs ${ILMBASE_CUSTOM_LIBRARIES})
41   separate_arguments(tmp_ilmbaselibs)
42   set(tmp_lst
43     ${ILMBASE_CUSTOM} | ${tmp_ilmbaselibs} |
44     ${ILMBASE_HOME} | ${ILMBASE_VERSION} | ${ILMBASE_LIB_AREA}
45   )
46   set(${varname} "${tmp_lst}")
47   unset(tmp_ilmbaselibs)
48   unset(tmp_lst)
49 endmacro()
50
51 # To enforce that find_* functions do not use inadvertently existing versions
52 if(ILMBASE_CUSTOM)
53   set(ILMBASE_FIND_OPTIONS "NO_DEFAULT_PATH")
54 endif()
55
56 # Macro to search for an include directory
57 macro(PREFIX_FIND_INCLUDE_DIR prefix includefile libpath_var)
58   string(TOUPPER ${prefix}_INCLUDE_DIR tmp_varname)
59   find_path(${tmp_varname} ${includefile}
60     HINTS ${${libpath_var}}
61     PATH_SUFFIXES include
62     ${ILMBASE_FIND_OPTIONS}
63   )
64   if(${tmp_varname})
65     mark_as_advanced(${tmp_varname})
66   endif()
67   unset(tmp_varname)
68 endmacro()
69
70
71 # Macro to search for the given library and adds the cached
72 # variable names to the specified list
73 macro(PREFIX_FIND_LIB prefix libname libpath_var liblist_var cachelist_var)
74   string(TOUPPER ${prefix}_${libname} tmp_prefix)
75   # Handle new library names for OpenEXR 2.1 build via cmake
76   string(REPLACE "." "_" _ILMBASE_VERSION ${ILMBASE_VERSION})
77   string(SUBSTRING ${_ILMBASE_VERSION} 0 3 _ILMBASE_VERSION )
78   
79   find_library(${tmp_prefix}_LIBRARY_RELEASE
80     NAMES ${libname} ${libname}-${_ILMBASE_VERSION}
81     HINTS ${${libpath_var}}
82     PATH_SUFFIXES lib
83     ${ILMBASE_FIND_OPTIONS}
84   )
85   find_library(${tmp_prefix}_LIBRARY_DEBUG
86     NAMES ${libname}d ${libname}_d ${libname}debug ${libname}_debug
87     HINTS ${${libpath_var}}
88     PATH_SUFFIXES lib
89     ${ILMBASE_FIND_OPTIONS}
90   )
91   # Properly define ${tmp_prefix}_LIBRARY (cached) and ${tmp_prefix}_LIBRARIES
92   select_library_configurations(${tmp_prefix})
93   list(APPEND ${liblist_var} ${tmp_prefix}_LIBRARIES)
94
95   # Add to the list of variables which should be reset
96   list(APPEND ${cachelist_var}
97     ${tmp_prefix}_LIBRARY
98     ${tmp_prefix}_LIBRARY_RELEASE
99     ${tmp_prefix}_LIBRARY_DEBUG)
100   mark_as_advanced(
101     ${tmp_prefix}_LIBRARY
102     ${tmp_prefix}_LIBRARY_RELEASE
103     ${tmp_prefix}_LIBRARY_DEBUG)
104   unset(tmp_prefix)
105 endmacro()
106
107
108 # Encode the current state of the external variables into a string
109 SET_STATE_VAR(ILMBASE_CURRENT_STATE)
110
111 # If the state has changed, clear the cached variables
112 if(ILMBASE_CACHED_STATE AND
113     NOT ILMBASE_CACHED_STATE STREQUAL ILMBASE_CURRENT_STATE)
114
115   foreach(libvar ${ILMBASE_CACHED_VARS})
116     unset(${libvar} CACHE)
117   endforeach()
118 endif()
119
120
121 # Generic search paths
122 set(IlmBase_generic_include_paths
123   ${ILMBASE_CUSTOM_INCLUDE_DIR}
124   /usr/include
125   /usr/include/${CMAKE_LIBRARY_ARCHITECTURE}
126   /usr/local/include
127   /sw/include
128   /opt/local/include
129 )
130 set(IlmBase_generic_library_paths
131   ${ILMBASE_CUSTOM_LIB_DIR}
132   /usr/lib
133   /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}
134   /usr/local/lib
135   /usr/local/lib/${CMAKE_LIBRARY_ARCHITECTURE}
136   /sw/lib
137   /opt/local/lib
138 )
139
140 # Search paths for the IlmBase files
141 if(ILMBASE_HOME)
142   if(ILMBASE_VERSION)
143     set(IlmBase_include_paths
144       ${ILMBASE_HOME}/ilmbase-${ILMBASE_VERSION}/include
145       ${ILMBASE_HOME}/include/ilmbase-${ILMBASE_VERSION}
146     )
147     set(IlmBase_library_paths
148       ${ILMBASE_HOME}/ilmbase-${ILMBASE_VERSION}/lib
149       ${ILMBASE_HOME}/lib/ilmbase-${ILMBASE_VERSION}
150     )
151   endif()
152   list(APPEND IlmBase_include_paths ${ILMBASE_HOME}/include)
153   set(IlmBase_library_paths
154     ${ILMBASE_HOME}/lib
155     ${ILMBASE_HOME}/lib64
156     ${ILMBASE_LIB_AREA}
157     ${IlmBase_library_paths})
158 endif()
159 list(APPEND IlmBase_include_paths ${IlmBase_generic_include_paths})
160 list(APPEND IlmBase_library_paths ${IlmBase_generic_library_paths})
161
162 # Locate the header files
163 PREFIX_FIND_INCLUDE_DIR(IlmBase
164   OpenEXR/IlmBaseConfig.h IlmBase_include_paths)
165
166 if(ILMBASE_INCLUDE_DIR)
167   # Get the version from config file, if not already set.
168   if(NOT ILMBASE_VERSION)
169     FILE(STRINGS "${ILMBASE_INCLUDE_DIR}/OpenEXR/IlmBaseConfig.h" ILMBASE_BUILD_SPECIFICATION
170          REGEX "^[ \t]*#define[ \t]+ILMBASE_VERSION_STRING[ \t]+\"[.0-9]+\".*$")
171
172     if(ILMBASE_BUILD_SPECIFICATION)
173       if(NOT IlmBase_FIND_QUIETLY)
174         message(STATUS "${ILMBASE_BUILD_SPECIFICATION}")
175       endif()
176       string(REGEX REPLACE ".*#define[ \t]+ILMBASE_VERSION_STRING[ \t]+\"([.0-9]+)\".*"
177              "\\1" XYZ ${ILMBASE_BUILD_SPECIFICATION})
178       set("ILMBASE_VERSION" ${XYZ} CACHE STRING "Version of ILMBase lib")
179     else()
180       # Old versions (before 2.0?) do not have any version string, just assuming 2.0 should be fine though. 
181       message(WARNING "Could not determine ILMBase library version, assuming 2.0.")
182       set("ILMBASE_VERSION" "2.0" CACHE STRING "Version of ILMBase lib")
183     endif()
184   endif()
185 endif()
186
187
188 if(ILMBASE_CUSTOM)
189   if(NOT ILMBASE_CUSTOM_LIBRARIES)
190     message(FATAL_ERROR "Custom IlmBase libraries requested but ILMBASE_CUSTOM_LIBRARIES is not set.")
191   endif()
192   set(IlmBase_Libraries ${ILMBASE_CUSTOM_LIBRARIES})
193   separate_arguments(IlmBase_Libraries)
194 else()
195 #elseif(${ILMBASE_VERSION} VERSION_LESS "2.1")
196   set(IlmBase_Libraries Half Iex Imath IlmThread)
197 #else()
198 #  string(REGEX REPLACE "([0-9]+)[.]([0-9]+).*" "\\1_\\2" _ilmbase_libs_ver ${ILMBASE_VERSION})
199 #  set(IlmBase_Libraries Half Iex-${_ilmbase_libs_ver} Imath-${_ilmbase_libs_ver} IlmThread-${_ilmbase_libs_ver})
200 endif()
201
202
203 # Locate the IlmBase libraries
204 set(IlmBase_libvars "")
205 set(IlmBase_cachevars "")
206 foreach(ilmbase_lib ${IlmBase_Libraries})
207   PREFIX_FIND_LIB(IlmBase ${ilmbase_lib}
208     IlmBase_library_paths IlmBase_libvars IlmBase_cachevars)
209 endforeach()
210 # Create the list of variables that might need to be cleared
211 set(ILMBASE_CACHED_VARS
212   ILMBASE_INCLUDE_DIR ${IlmBase_cachevars}
213   CACHE INTERNAL "Variables set by FindIlmBase.cmake" FORCE)
214
215 # Store the current state so that variables might be cleared if required
216 set(ILMBASE_CACHED_STATE ${ILMBASE_CURRENT_STATE}
217   CACHE INTERNAL "State last seen by FindIlmBase.cmake" FORCE)
218
219 # Link with pthreads if required
220 if(NOT WIN32 AND EXISTS ${ILMBASE_INCLUDE_DIR}/OpenEXR/IlmBaseConfig.h)
221   file(STRINGS ${ILMBASE_INCLUDE_DIR}/OpenEXR/IlmBaseConfig.h
222     ILMBASE_HAVE_PTHREAD
223     REGEX "^[ \\t]*#define[ \\t]+HAVE_PTHREAD[ \\t]1[ \\t]*\$"
224   )
225   if(ILMBASE_HAVE_PTHREAD)
226     find_package(Threads)
227     if(CMAKE_USE_PTHREADS_INIT)
228       set(ILMBASE_PTHREADS ${CMAKE_THREAD_LIBS_INIT})
229     endif()
230   endif()
231 endif()
232
233 # Use the standard function to handle ILMBASE_FOUND
234 FIND_PACKAGE_HANDLE_STANDARD_ARGS(IlmBase DEFAULT_MSG
235   ILMBASE_INCLUDE_DIR ${IlmBase_libvars})
236
237 if(ILMBASE_FOUND)
238   set(ILMBASE_LIBRARIES "")
239   foreach(tmplib ${IlmBase_libvars})
240     list(APPEND ILMBASE_LIBRARIES ${${tmplib}})
241   endforeach()
242   list(APPEND ILMBASE_LIBRARIES ${ILMBASE_PTHREADS})
243   if(NOT IlmBase_FIND_QUIETLY)
244     FIND_PACKAGE_MESSAGE(ILMBASE
245       "Found IlmBase: ${ILMBASE_LIBRARIES}"
246       "[${ILMBASE_INCLUDE_DIR}][${ILMBASE_LIBRARIES}][${ILMBASE_CURRENT_STATE}]"
247       )
248   endif()
249 endif()
250
251 # Restore the original find library ordering
252 if( ILMBASE_USE_STATIC_LIBS )
253   set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ilmbase_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
254 endif()
255
256 # Unset the helper variables to avoid pollution
257 unset(ILMBASE_CURRENT_STATE)
258 unset(IlmBase_include_paths)
259 unset(IlmBase_library_paths)
260 unset(IlmBase_generic_include_paths)
261 unset(IlmBase_generic_library_paths)
262 unset(IlmBase_libvars)
263 unset(IlmBase_cachevars)
264 unset(ILMBASE_PTHREADS)