Merge branch 'master' into blender2.8
[blender.git] / build_files / cmake / Modules / FindOpenEXR.cmake
1 # - Find OpenEXR library
2 # Find the native OpenEXR includes and library
3 # This module defines
4 #  OPENEXR_INCLUDE_DIRS, where to find ImfXdr.h, etc. Set when
5 #                        OPENEXR_INCLUDE_DIR is found.
6 #  OPENEXR_LIBRARIES, libraries to link against to use OpenEXR.
7 #  OPENEXR_ROOT_DIR, The base directory to search for OpenEXR.
8 #                    This can also be an environment variable.
9 #  OPENEXR_FOUND, If false, do not try to use OpenEXR.
10 #
11 # For individual library access these advanced settings are available
12 #  OPENEXR_HALF_LIBRARY, Path to Half library
13 #  OPENEXR_IEX_LIBRARY, Path to Half library
14 #  OPENEXR_ILMIMF_LIBRARY, Path to Ilmimf library
15 #  OPENEXR_ILMTHREAD_LIBRARY, Path to IlmThread library
16 #  OPENEXR_IMATH_LIBRARY, Path to Imath library
17 #
18 # also defined, but not for general use are
19 #  OPENEXR_LIBRARY, where to find the OpenEXR library.
20
21 #=============================================================================
22 # Copyright 2011 Blender Foundation.
23 #
24 # Distributed under the OSI-approved BSD License (the "License");
25 # see accompanying file Copyright.txt for details.
26 #
27 # This software is distributed WITHOUT ANY WARRANTY; without even the
28 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29 # See the License for more information.
30 #=============================================================================
31
32 # If OPENEXR_ROOT_DIR was defined in the environment, use it.
33 IF(NOT OPENEXR_ROOT_DIR AND NOT $ENV{OPENEXR_ROOT_DIR} STREQUAL "")
34   SET(OPENEXR_ROOT_DIR $ENV{OPENEXR_ROOT_DIR})
35 ENDIF()
36
37 # Old versions (before 2.0?) do not have any version string, just assuming this should be fine though.
38 SET(_openexr_libs_ver_init "2.0")
39
40 SET(_openexr_FIND_COMPONENTS
41   Half
42   Iex
43   IlmImf
44   IlmThread
45   Imath
46 )
47
48 SET(_openexr_SEARCH_DIRS
49   ${OPENEXR_ROOT_DIR}
50   /usr/local
51   /sw # Fink
52   /opt/local # DarwinPorts
53   /opt/lib/openexr
54 )
55
56 FIND_PATH(OPENEXR_INCLUDE_DIR
57   NAMES
58     OpenEXR/ImfXdr.h
59   HINTS
60     ${_openexr_SEARCH_DIRS}
61   PATH_SUFFIXES
62     include
63 )
64
65 # If the headers were found, get the version from config file, if not already set.
66 IF(OPENEXR_INCLUDE_DIR)
67   IF(NOT OPENEXR_VERSION)
68
69     FIND_FILE(_openexr_CONFIG
70       NAMES
71         OpenEXRConfig.h
72       PATHS
73         "${OPENEXR_INCLUDE_DIR}"
74         "${OPENEXR_INCLUDE_DIR}/OpenEXR"
75       NO_DEFAULT_PATH
76     )
77
78     IF(_openexr_CONFIG)
79       FILE(STRINGS "${_openexr_CONFIG}" OPENEXR_BUILD_SPECIFICATION
80            REGEX "^[ \t]*#define[ \t]+OPENEXR_VERSION_STRING[ \t]+\"[.0-9]+\".*$")
81     ELSE()
82       MESSAGE(WARNING "Could not find \"OpenEXRConfig.h\" in \"${OPENEXR_INCLUDE_DIR}\"")
83     ENDIF()
84
85     IF(OPENEXR_BUILD_SPECIFICATION)
86       MESSAGE(STATUS "${OPENEXR_BUILD_SPECIFICATION}")
87       STRING(REGEX REPLACE ".*#define[ \t]+OPENEXR_VERSION_STRING[ \t]+\"([.0-9]+)\".*"
88              "\\1" _openexr_libs_ver_init ${OPENEXR_BUILD_SPECIFICATION})
89     ELSE()
90       MESSAGE(WARNING "Could not determine ILMBase library version, assuming ${_openexr_libs_ver_init}.")
91     ENDIF()
92
93     UNSET(_openexr_CONFIG CACHE)
94
95   ENDIF()
96 ENDIF()
97
98 SET("OPENEXR_VERSION" ${_openexr_libs_ver_init} CACHE STRING "Version of OpenEXR lib")
99 UNSET(_openexr_libs_ver_init)
100
101 STRING(REGEX REPLACE "([0-9]+)[.]([0-9]+).*" "\\1_\\2" _openexr_libs_ver ${OPENEXR_VERSION})
102
103 SET(_openexr_LIBRARIES)
104 FOREACH(COMPONENT ${_openexr_FIND_COMPONENTS})
105   STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
106
107   FIND_LIBRARY(OPENEXR_${UPPERCOMPONENT}_LIBRARY
108     NAMES
109       ${COMPONENT}-${_openexr_libs_ver} ${COMPONENT}
110     HINTS
111       ${_openexr_SEARCH_DIRS}
112     PATH_SUFFIXES
113       lib64 lib
114     )
115   LIST(APPEND _openexr_LIBRARIES "${OPENEXR_${UPPERCOMPONENT}_LIBRARY}")
116 ENDFOREACH()
117
118 UNSET(_openexr_libs_ver)
119
120 # handle the QUIETLY and REQUIRED arguments and set OPENEXR_FOUND to TRUE if
121 # all listed variables are TRUE
122 INCLUDE(FindPackageHandleStandardArgs)
123 FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenEXR  DEFAULT_MSG
124     _openexr_LIBRARIES OPENEXR_INCLUDE_DIR)
125
126 IF(OPENEXR_FOUND)
127   SET(OPENEXR_LIBRARIES ${_openexr_LIBRARIES})
128   # Both include paths are needed because of dummy OSL headers mixing #include <OpenEXR/foo.h> and #include <foo.h> :(
129   SET(OPENEXR_INCLUDE_DIRS ${OPENEXR_INCLUDE_DIR} ${OPENEXR_INCLUDE_DIR}/OpenEXR)
130 ENDIF()
131
132 MARK_AS_ADVANCED(
133   OPENEXR_INCLUDE_DIR
134   OPENEXR_VERSION
135 )
136 FOREACH(COMPONENT ${_openexr_FIND_COMPONENTS})
137   STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
138   MARK_AS_ADVANCED(OPENEXR_${UPPERCOMPONENT}_LIBRARY)
139 ENDFOREACH()
140
141 UNSET(COMPONENT)
142 UNSET(UPPERCOMPONENT)
143 UNSET(_openexr_FIND_COMPONENTS)
144 UNSET(_openexr_LIBRARIES)
145 UNSET(_openexr_SEARCH_DIRS)