tFindUDUNITS2.cmake - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
(HTM) git clone git://src.adamsgaard.dk/pism
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tFindUDUNITS2.cmake (2838B)
---
1 # - Find UDUNITS2
2 # Find the native UDUNITS2 includes and library
3 #
4 # UDUNITS2_INCLUDES - where to find udunits2.h
5 # UDUNITS2_LIBRARIES - libraries to link with
6 # UDUNITS2_FOUND - True if UDUNITS2 was found.
7
8 if (UDUNITS2_INCLUDES)
9 # Already in cache, be silent
10 set (UDUNITS2_FIND_QUIETLY TRUE)
11 endif (UDUNITS2_INCLUDES)
12
13 find_path (UDUNITS2_INCLUDES udunits2.h
14 HINTS "${UDUNITS2_ROOT}/include" "$ENV{UDUNITS2_ROOT}/include"
15 PATH_SUFFIXES "udunits2"
16 DOC "Path to udunits2.h")
17
18 # UDUNITS2 headers might be in .../include or .../include/udunits2.
19 # We try both.
20 if (${UDUNITS2_INCLUDES} MATCHES "udunits2/?$")
21 string(REGEX REPLACE "/include/udunits2/?$" "/lib"
22 UDUNITS2_LIB_HINT ${UDUNITS2_INCLUDES})
23 else()
24 string(REGEX REPLACE "/include/?$" "/lib"
25 UDUNITS2_LIB_HINT ${UDUNITS2_INCLUDES})
26 endif()
27
28 find_library (UDUNITS2_LIBRARIES
29 NAMES udunits2
30 HINTS ${UDUNITS2_LIB_HINT})
31
32 set(UDUNITS2_TEST_SRC "
33 #include <udunits2.h>
34
35 int main(int argc, char **argv) {
36 ut_system *s = ut_read_xml(NULL);
37 ut_free_system(s);
38 return 0;
39 }
40 ")
41
42 if ((NOT UDUNITS2_LIBRARIES) OR (NOT UDUNITS2_INCLUDES))
43 message(STATUS "Trying to find UDUNITS-2 using LD_LIBRARY_PATH (we're desperate)...")
44
45 file(TO_CMAKE_PATH "$ENV{LD_LIBRARY_PATH}" LD_LIBRARY_PATH)
46
47 find_library(UDUNITS2_LIBRARIES
48 NAMES udunits2
49 HINTS ${LD_LIBRARY_PATH})
50
51 if (UDUNITS2_LIBRARIES)
52 get_filename_component(UDUNITS2_LIB_DIR ${UDUNITS2_LIBRARIES} PATH)
53 string(REGEX REPLACE "/lib/?$" "/include"
54 UDUNITS2_H_HINT ${UDUNITS2_LIB_DIR})
55
56 find_path (UDUNITS2_INCLUDES udunits2.h
57 HINTS ${UDUNITS2_H_HINT}
58 PATH_SUFFIXES "udunits2"
59 DOC "Path to udunits2.h")
60 endif()
61 endif()
62
63 include (CheckCSourceRuns)
64
65 set(CMAKE_REQUIRED_INCLUDES ${UDUNITS2_INCLUDES})
66 set(CMAKE_REQUIRED_LIBRARIES ${UDUNITS2_LIBRARIES})
67 check_c_source_runs("${UDUNITS2_TEST_SRC}" UDUNITS2_WORKS_WITHOUT_EXPAT)
68
69 if(${UDUNITS2_WORKS_WITHOUT_EXPAT})
70 message(STATUS "UDUNITS-2 does not require expat")
71 else()
72 find_package(EXPAT REQUIRED)
73
74 set(CMAKE_REQUIRED_INCLUDES ${UDUNITS2_INCLUDES} ${EXPAT_INCLUDE_DIRS})
75 set(CMAKE_REQUIRED_LIBRARIES ${UDUNITS2_LIBRARIES} ${EXPAT_LIBRARIES})
76 check_c_source_runs("${UDUNITS2_TEST_SRC}" UDUNITS2_WORKS_WITH_EXPAT)
77
78 if(NOT ${UDUNITS2_WORKS_WITH_EXPAT})
79 message(FATAL_ERROR "UDUNITS-2 does not seem to work with or without expat")
80 endif()
81
82 message(STATUS "UDUNITS-2 requires EXPAT")
83 set (UDUNITS2_LIBRARIES "${UDUNITS2_LIBRARIES};${EXPAT_LIBRARIES}" CACHE STRING "" FORCE)
84 endif()
85
86 # handle the QUIETLY and REQUIRED arguments and set UDUNITS2_FOUND to TRUE if
87 # all listed variables are TRUE
88 include (FindPackageHandleStandardArgs)
89 find_package_handle_standard_args (UDUNITS2 DEFAULT_MSG UDUNITS2_LIBRARIES UDUNITS2_INCLUDES)
90
91 mark_as_advanced (UDUNITS2_LIBRARIES UDUNITS2_INCLUDES)