1eb4ea3f5e7be2a1df8d6cdb5c32e41caad0ed57
[blender.git] / build_files / build_environment / patches / cmake / modules / FindOpenEXR.cmake
1 # Module to find OpenEXR.
2 #
3 # This module will first look into the directories defined by the variables:
4 #   - OPENEXR_HOME, OPENEXR_VERSION, OPENEXR_LIB_AREA
5 #
6 # It also supports non-standard names for the library components.
7 #
8 # To use a custom OpenEXR
9 #   - Set the variable OPENEXR_CUSTOM to True
10 #   - Set the variable OPENEXR_CUSTOM_LIBRARY to the name of the library to
11 #     use, e.g. "SpiIlmImf"
12 #   - Optionally set the variable OPENEXR_CUSTOM_INCLUDE_DIR to any
13 #     particularly weird place that the OpenEXR/*.h files may be found
14 #   - Optionally set the variable OPENEXR_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 # OPENEXR_INCLUDE_DIR - where to find ImfRgbaFile.h, OpenEXRConfig, etc.
20 # OPENEXR_LIBRARIES   - list of libraries to link against when using OpenEXR.
21 #                       This list does NOT include the IlmBase libraries.
22 #                       These are defined by the FindIlmBase module.
23 # OPENEXR_FOUND       - True if OpenEXR was found.
24
25 # Other standarnd issue macros
26 include(SelectLibraryConfigurations)
27 include(FindPackageHandleStandardArgs)
28 include(FindPackageMessage)
29
30 if(OPENEXR_USE_STATIC_LIBS)
31   set(_openexr_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
32   if(WIN32)
33     set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
34   else()
35     set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
36   endif()
37 endif()
38
39 # Macro to assemble a helper state variable
40 macro(SET_STATE_VAR varname)
41   set(tmp_lst
42     ${OPENEXR_CUSTOM} | ${OPENEXR_CUSTOM_LIBRARY} |
43     ${OPENEXR_HOME} | ${OPENEXR_VERSION} | ${OPENEXR_LIB_AREA}
44   )
45   set(${varname} "${tmp_lst}")
46   unset(tmp_lst)
47 endmacro()
48
49 # To enforce that find_* functions do not use inadvertently existing versions
50 if(OPENEXR_CUSTOM)
51   set(OPENEXR_FIND_OPTIONS "NO_DEFAULT_PATH")
52 endif()
53
54 # Macro to search for an include directory
55 macro(PREFIX_FIND_INCLUDE_DIR prefix includefile libpath_var)
56   string(TOUPPER ${prefix}_INCLUDE_DIR tmp_varname)
57   find_path(${tmp_varname} ${includefile}
58     HINTS ${${libpath_var}}
59     PATH_SUFFIXES include
60     ${OPENEXR_FIND_OPTIONS}
61   )
62   if(${tmp_varname})
63     mark_as_advanced(${tmp_varname})
64   endif()
65   unset(tmp_varname)
66 endmacro()
67
68
69 # Macro to search for the given library and adds the cached
70 # variable names to the specified list
71 macro(PREFIX_FIND_LIB prefix libname libpath_var liblist_var cachelist_var)
72   string(TOUPPER ${prefix}_${libname} tmp_prefix)
73   # Handle new library names for OpenEXR 2.1 build via cmake
74   string(REPLACE "." "_" _ILMBASE_VERSION ${ILMBASE_VERSION})
75   string(SUBSTRING ${_ILMBASE_VERSION} 0 3 _ILMBASE_VERSION )
76   find_library(${tmp_prefix}_LIBRARY_RELEASE
77     NAMES ${libname} ${libname}-${_ILMBASE_VERSION}
78     HINTS ${${libpath_var}}
79     PATH_SUFFIXES lib
80     ${OPENEXR_FIND_OPTIONS}
81   )
82   find_library(${tmp_prefix}_LIBRARY_DEBUG
83     NAMES ${libname}d ${libname}_d ${libname}debug ${libname}_debug
84     HINTS ${${libpath_var}}
85     PATH_SUFFIXES lib
86     ${OPENEXR_FIND_OPTIONS}
87   )
88   # Properly define ${tmp_prefix}_LIBRARY (cached) and ${tmp_prefix}_LIBRARIES
89   select_library_configurations(${tmp_prefix})
90   list(APPEND ${liblist_var} ${tmp_prefix}_LIBRARIES)
91
92   # Add to the list of variables which should be reset
93   list(APPEND ${cachelist_var}
94     ${tmp_prefix}_LIBRARY
95     ${tmp_prefix}_LIBRARY_RELEASE
96     ${tmp_prefix}_LIBRARY_DEBUG)
97   mark_as_advanced(
98     ${tmp_prefix}_LIBRARY
99     ${tmp_prefix}_LIBRARY_RELEASE
100     ${tmp_prefix}_LIBRARY_DEBUG)
101   unset(tmp_prefix)
102 endmacro()
103
104
105 # Encode the current state of the external variables into a string
106 SET_STATE_VAR(OPENEXR_CURRENT_STATE)
107
108 # If the state has changed, clear the cached variables
109 if(OPENEXR_CACHED_STATE AND
110     NOT OPENEXR_CACHED_STATE STREQUAL OPENEXR_CURRENT_STATE)
111   foreach(libvar ${OPENEXR_CACHED_VARS})
112     unset(${libvar} CACHE)
113   endforeach()
114 endif()
115
116 # Generic search paths
117 set(OpenEXR_generic_include_paths
118   ${OPENEXR_CUSTOM_INCLUDE_DIR}
119   /usr/include
120   /usr/include/${CMAKE_LIBRARY_ARCHITECTURE}
121   /usr/local/include
122   /sw/include
123   /opt/local/include
124 )
125 set(OpenEXR_generic_library_paths
126   ${OPENEXR_CUSTOM_LIB_DIR}
127   /usr/lib
128   /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}
129   /usr/local/lib
130   /usr/local/lib/${CMAKE_LIBRARY_ARCHITECTURE}
131   /sw/lib
132   /opt/local/lib
133 )
134
135 # Search paths for the OpenEXR files
136 if(OPENEXR_HOME)
137   set(OpenEXR_library_paths
138     ${OPENEXR_HOME}/lib
139     ${OPENEXR_HOME}/lib64)
140   if(OPENEXR_VERSION)
141     set(OpenEXR_include_paths
142       ${OPENEXR_HOME}/openexr-${OPENEXR_VERSION}/include
143       ${OPENEXR_HOME}/include/openexr-${OPENEXR_VERSION})
144     list(APPEND OpenEXR_library_paths
145       ${OPENEXR_HOME}/openexr-${OPENEXR_VERSION}/lib
146       ${OPENEXR_HOME}/lib/openexr-${OPENEXR_VERSION})
147   endif()
148   list(APPEND OpenEXR_include_paths ${OPENEXR_HOME}/include)
149   if(OPENEXR_LIB_AREA)
150     list(INSERT OpenEXR_library_paths 2 ${OPENEXR_LIB_AREA})
151   endif()
152 endif()
153 if(ILMBASE_HOME AND OPENEXR_VERSION)
154   list(APPEND OpenEXR_include_paths
155     ${ILMBASE_HOME}/include/openexr-${OPENEXR_VERSION})
156 endif()
157 list(APPEND OpenEXR_include_paths ${OpenEXR_generic_include_paths})
158 list(APPEND OpenEXR_library_paths ${OpenEXR_generic_library_paths})
159
160 # Locate the header files
161 PREFIX_FIND_INCLUDE_DIR(OpenEXR
162   OpenEXR/ImfArray.h OpenEXR_include_paths)
163
164 if(OPENEXR_INCLUDE_DIR)
165   # Get the version from config file, if not already set.
166   if(NOT OPENEXR_VERSION)
167     FILE(STRINGS "${OPENEXR_INCLUDE_DIR}/OpenEXR/OpenEXRConfig.h" OPENEXR_BUILD_SPECIFICATION
168          REGEX "^[ \t]*#define[ \t]+OPENEXR_VERSION_STRING[ \t]+\"[.0-9]+\".*$")
169
170     if(OPENEXR_BUILD_SPECIFICATION)
171       if(NOT OpenEXR_FIND_QUIETLY)
172         message(STATUS "${OPENEXR_BUILD_SPECIFICATION}")
173       endif()
174       string(REGEX REPLACE ".*#define[ \t]+OPENEXR_VERSION_STRING[ \t]+\"([.0-9]+)\".*"
175              "\\1" XYZ ${OPENEXR_BUILD_SPECIFICATION})
176       set("OPENEXR_VERSION" ${XYZ} CACHE STRING "Version of OpenEXR lib")
177     else()
178       # Old versions (before 2.0?) do not have any version string, just assuming 2.0 should be fine though. 
179       message(WARNING "Could not determine ILMBase library version, assuming 2.0.")
180       set("OPENEXR_VERSION" "2.0" CACHE STRING "Version of OpenEXR lib")
181     endif()
182   endif()
183 endif()
184
185 if(OPENEXR_CUSTOM)
186   if(NOT OPENEXR_CUSTOM_LIBRARY)
187     message(FATAL_ERROR "Custom OpenEXR library requested but OPENEXR_CUSTOM_LIBRARY is not set.")
188   endif()
189   set(OpenEXR_Library ${OPENEXR_CUSTOM_LIBRARY})
190 else()
191 #elseif(${OPENEXR_VERSION} VERSION_LESS "2.1")
192   set(OpenEXR_Library IlmImf)
193 #else()
194 #  string(REGEX REPLACE "([0-9]+)[.]([0-9]+).*" "\\1_\\2" _openexr_libs_ver ${OPENEXR_VERSION})
195 #  set(OpenEXR_Library IlmImf-${_openexr_libs_ver})
196 endif()
197
198 # Locate the OpenEXR library
199 set(OpenEXR_libvars "")
200 set(OpenEXR_cachevars "")
201 PREFIX_FIND_LIB(OpenEXR ${OpenEXR_Library}
202   OpenEXR_library_paths OpenEXR_libvars OpenEXR_cachevars)
203
204 # Create the list of variables that might need to be cleared
205 set(OPENEXR_CACHED_VARS
206   OPENEXR_INCLUDE_DIR ${OpenEXR_cachevars}
207   CACHE INTERNAL "Variables set by FindOpenEXR.cmake" FORCE)
208
209 # Store the current state so that variables might be cleared if required
210 set(OPENEXR_CACHED_STATE ${OPENEXR_CURRENT_STATE}
211   CACHE INTERNAL "State last seen by FindOpenEXR.cmake" FORCE)
212
213 # Always link explicitly with zlib
214 set(OPENEXR_ZLIB ${ZLIB_LIBRARIES})
215
216 # Use the standard function to handle OPENEXR_FOUND
217 FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenEXR DEFAULT_MSG
218   OPENEXR_INCLUDE_DIR ${OpenEXR_libvars})
219
220 if(OPENEXR_FOUND)
221   set(OPENEXR_LIBRARIES "")
222   foreach(tmplib ${OpenEXR_libvars})
223     list(APPEND OPENEXR_LIBRARIES ${${tmplib}})
224   endforeach()
225   list(APPEND OPENEXR_LIBRARIES ${ZLIB_LIBRARIES})
226   if(NOT OpenEXR_FIND_QUIETLY)
227     FIND_PACKAGE_MESSAGE(OPENEXR
228       "Found OpenEXR: ${OPENEXR_LIBRARIES}"
229       "[${OPENEXR_INCLUDE_DIR}][${OPENEXR_LIBRARIES}][${OPENEXR_CURRENT_STATE}]"
230       )
231   endif()
232 endif()
233
234 # Restore the original find library ordering
235 if( OPENEXR_USE_STATIC_LIBS )
236   set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openexr_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
237 endif()
238
239 # Unset the helper variables to avoid pollution
240 unset(OPENEXR_CURRENT_STATE)
241 unset(OpenEXR_include_paths)
242 unset(OpenEXR_library_paths)
243 unset(OpenEXR_generic_include_paths)
244 unset(OpenEXR_generic_library_paths)
245 unset(OpenEXR_libvars)
246 unset(OpenEXR_cachevars)