1#
2# A simple cmake find module for OpenEXR
3#
4
5find_package(PkgConfig QUIET)
6if(PKG_CONFIG_FOUND)
7  pkg_check_modules(PC_OPENEXR QUIET OpenEXR)
8endif()
9
10if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
11  # Under Mac OS, if the user has installed using brew, the package config
12  # information is not entirely accurate in that it refers to the
13  # true install path but brew maintains links to /usr/local for you
14  # which they want you to use
15  if(PC_OPENEXR_FOUND AND "${PC_OPENEXR_INCLUDEDIR}" MATCHES ".*/Cellar/.*")
16    set(_OpenEXR_HINT_INCLUDE /usr/local/include)
17    set(_OpenEXR_HINT_LIB /usr/local/lib)
18  endif()
19endif()
20
21if(PC_OPENEXR_FOUND)
22  set(OpenEXR_CFLAGS "")
23  foreach(iter ${PC_OPENEXR_CFLAGS_OTHER})
24    set(OpenEXR_CFLAGS "${OpenEXR_CFLAGS} ${iter}")
25  endforeach()
26  set(OpenEXR_LIBRARY_DIRS ${PC_OPENEXR_LIBRARY_DIRS})
27  set(OpenEXR_LDFLAGS ${PC_OPENEXR_LDFLAGS_OTHER})
28  if("${_OpenEXR_HINT_INCLUDE}" STREQUAL "")
29    set(_OpenEXR_HINT_INCLUDE ${PC_OPENEXR_INCLUDEDIR} ${PC_OPENEXR_INCLUDE_DIRS})
30    set(_OpenEXR_HINT_LIB ${PC_OPENEXR_LIBDIR} ${PC_OPENEXR_LIBRARY_DIRS})
31  endif()
32else()
33  if(UNIX)
34    set(OpenEXR_CFLAGS "-pthread")
35    if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
36    else()
37      set(OpenEXR_LDFLAGS "-pthread")
38    endif()
39  endif()
40endif()
41
42find_path(OpenEXR_INCLUDE_DIR OpenEXRConfig.h
43          HINTS ${_OpenEXR_HINT_INCLUDE}
44          PATH_SUFFIXES OpenEXR )
45if(OpenEXR_INCLUDE_DIR AND EXISTS "${OpenEXR_INCLUDE_DIR}/OpenEXRConfig.h")
46    set(OpenEXR_VERSION ${PC_OPENEXR_VERSION})
47
48    if("${OpenEXR_VERSION}" STREQUAL "")
49      file(STRINGS "${OpenEXR_INCLUDE_DIR}/OpenEXRConfig.h" openexr_version_str
50           REGEX "^#define[\t ]+OPENEXR_VERSION_STRING[\t ]+\".*")
51
52      string(REGEX REPLACE "^#define[\t ]+OPENEXR_VERSION_STRING[\t ]+\"([^ \\n]*)\".*"
53             "\\1" OpenEXR_VERSION "${openexr_version_str}")
54      unset(openexr_version_str)
55    endif()
56endif()
57
58find_library(OpenEXR_LIBRARY NAMES OpenEXR libOpenEXR HINTS ${_OpenEXR_HINT_LIB})
59
60find_package(Imath QUIET)
61
62unset(_OpenEXR_HINT_INCLUDE)
63unset(_OpenEXR_HINT_LIB)
64
65set(OpenEXR_LIBRARIES ${OpenEXR_LIBRARY} ${Imath_LIBRARIES} )
66set(OpenEXR_INCLUDE_DIRS ${OpenEXR_INCLUDE_DIR} )
67
68if(NOT PC_OPENEXR_FOUND)
69get_filename_component(OpenEXR_LDFLAGS_OTHER ${OpenEXR_LIBRARY} PATH)
70set(OpenEXR_LDFLAGS_OTHER -L${OpenEXR_LDFLAGS_OTHER})
71endif()
72
73include(FindPackageHandleStandardArgs)
74# handle the QUIETLY and REQUIRED arguments and set OpenEXR_FOUND to TRUE
75# if all listed variables are TRUE
76find_package_handle_standard_args(OpenEXR
77                                  REQUIRED_VARS OpenEXR_LIBRARY OpenEXR_INCLUDE_DIR
78                                  VERSION_VAR OpenEXR_VERSION
79                                  FAIL_MESSAGE "Unable to find OpenEXR library" )
80
81# older versions of cmake don't support FOUND_VAR to find_package_handle
82# so just do it the hard way...
83if(OPENEXR_FOUND AND NOT OpenEXR_FOUND)
84  set(OpenEXR_FOUND 1)
85endif()
86
87mark_as_advanced(OpenEXR_INCLUDE_DIR OpenEXR_LIBRARY )
88