tPISM_CMake_macros.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
---
tPISM_CMake_macros.cmake (11542B)
---
1 # This file contains CMake macros used in the root CMakeLists.txt
2
3 # Set CMake variables to enable rpath
4 macro(pism_use_rpath)
5 ## Use full RPATH, with this setting Pism libraries cannot be moved after installation
6 ## but the correct libraries will always be found regardless of LD_LIBRARY_PATH
7 ## in use, i.e. don't skip the full RPATH for the build tree
8 set (CMAKE_SKIP_BUILD_RPATH FALSE)
9 # when building, don't use the install RPATH already
10 # (but later on when installing)
11 set (CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
12 # the RPATH to be used when installing
13 set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${Pism_LIB_DIR}")
14 # add the automatically determined parts of the RPATH
15 # which point to directories outside the build tree to the install RPATH
16 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
17
18 # Mac OS X install_name fix:
19 set (CMAKE_MACOSX_RPATH 1)
20 set (CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${Pism_LIB_DIR}")
21 endmacro(pism_use_rpath)
22
23 # Set CMake variables to disable rpath
24 macro(pism_dont_use_rpath)
25 set (CMAKE_SKIP_BUILD_RPATH TRUE)
26 set (CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
27 set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${Pism_LIB_DIR}")
28 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
29 endmacro(pism_dont_use_rpath)
30
31 # Set CMake variables to ensure that everything is static
32 macro(pism_strictly_static)
33
34 if (BUILD_SHARED_LIBS)
35 message(FATAL_ERROR "Please set BUILD_SHARED_LIBS to OFF.")
36 endif()
37
38 set (CMAKE_SKIP_RPATH ON CACHE BOOL "Disable RPATH completely")
39 set (CMAKE_FIND_LIBRARY_SUFFIXES .a)
40
41 set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # get rid of -rdynamic
42 set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") # ditto
43
44 set_property(GLOBAL PROPERTY LINK_SEARCH_END_STATIC 1)
45 set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
46 set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
47
48 pism_dont_use_rpath()
49 endmacro(pism_strictly_static)
50
51 # Set the revision tag if PISM was checked out using Git.
52 macro(pism_set_revision_tag_git)
53 if (NOT Pism_VERSION)
54 if (EXISTS ${Pism_SOURCE_DIR}/.git)
55 find_program (GIT_EXECUTABLE git DOC "Git executable")
56 mark_as_advanced(GIT_EXECUTABLE)
57 execute_process (COMMAND ${GIT_EXECUTABLE} describe --always --match v?.?*
58 WORKING_DIRECTORY ${Pism_SOURCE_DIR}
59 OUTPUT_VARIABLE Pism_VERSION
60 OUTPUT_STRIP_TRAILING_WHITESPACE)
61 execute_process (COMMAND ${GIT_EXECUTABLE} --no-pager log -1 "--pretty=format:committed by %an on %ci"
62 WORKING_DIRECTORY ${Pism_SOURCE_DIR}
63 OUTPUT_VARIABLE Pism_COMMIT_INFO
64 OUTPUT_STRIP_TRAILING_WHITESPACE)
65 set(Pism_VERSION "${Pism_VERSION} ${Pism_COMMIT_INFO}")
66 endif (EXISTS ${Pism_SOURCE_DIR}/.git)
67 endif(NOT Pism_VERSION)
68 endmacro(pism_set_revision_tag_git)
69
70 # Set the PISM revision tag
71 macro(pism_set_revision_tag)
72 # Git
73 pism_set_revision_tag_git()
74
75 # Otherwise...
76 if (NOT Pism_VERSION)
77 set (Pism_VERSION "v1.2.2")
78 endif (NOT Pism_VERSION)
79
80 set (Pism_REVISION_TAG "${Pism_BRANCH} ${Pism_VERSION}")
81
82 message(STATUS "Configuring PISM version '${Pism_REVISION_TAG}'")
83 endmacro(pism_set_revision_tag)
84
85 macro(pism_set_install_prefix)
86 # Allow setting a custom install prefix using the PISM_INSTALL_PREFIX environment variable.
87 string (LENGTH "$ENV{PISM_INSTALL_PREFIX}" INSTALL_PREFIX_LENGTH)
88 if (INSTALL_PREFIX_LENGTH)
89 set (CMAKE_INSTALL_PREFIX $ENV{PISM_INSTALL_PREFIX} CACHE PATH "PISM install prefix" FORCE)
90 message (STATUS "Setting PISM install prefix to ${CMAKE_INSTALL_PREFIX}.")
91 endif()
92
93 # Define the directory structure.
94 set (Pism_BIN_DIR "bin")
95 set (Pism_LIB_DIR "lib")
96 set (Pism_SHARE_DIR "share/pism")
97 set (Pism_DOC_DIR "share/doc/pism")
98 endmacro()
99
100 # Set pedantic compiler flags
101 macro(pism_set_pedantic_flags)
102 set (DEFAULT_PEDANTIC_FLAGS "-pedantic -Wall -Wextra -Wno-cast-qual -Wundef -Wshadow -Wpointer-arith -Wno-cast-align -Wwrite-strings -Wno-conversion -Wsign-compare -Wno-redundant-decls -Wno-inline -Wno-long-long -Wmissing-format-attribute -Wpacked -Wdisabled-optimization -Wmultichar -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wendif-labels -Winvalid-pch -Wmissing-field-initializers -Wvariadic-macros -Wstrict-aliasing -funit-at-a-time -Wno-unknown-pragmas")
103 set (DEFAULT_PEDANTIC_CFLAGS "${DEFAULT_PEDANTIC_FLAGS} -std=c99")
104 set (DEFAULT_PEDANTIC_CXXFLAGS "${DEFAULT_PEDANTIC_FLAGS} -Woverloaded-virtual")
105 set (PEDANTIC_CFLAGS ${DEFAULT_PEDANTIC_CFLAGS} CACHE STRING "Compiler flags to enable pedantic warnings")
106 set (PEDANTIC_CXXFLAGS ${DEFAULT_PEDANTIC_CXXFLAGS} CACHE STRING "Compiler flags to enable pedantic warnings for C++")
107 mark_as_advanced (PEDANTIC_CFLAGS PEDANTIC_CXXFLAGS)
108 set (CMAKE_C_FLAGS_DEBUG "-g ${PEDANTIC_CFLAGS}")
109 set (CMAKE_CXX_FLAGS_DEBUG "-g ${PEDANTIC_CXXFLAGS}")
110 endmacro(pism_set_pedantic_flags)
111
112 # Make sure that we don't create .petscrc in $HOME, because this would affect
113 # all PISM runs by the current user.
114 macro(pism_check_build_dir_location)
115 if (DEFINED ENV{HOME})
116 # Don't assume that HOME env var is set.
117 file (TO_CMAKE_PATH $ENV{HOME} home_dir)
118 file (TO_CMAKE_PATH ${PROJECT_BINARY_DIR} build_dir)
119
120 if (${home_dir} STREQUAL ${build_dir})
121 message (FATAL_ERROR
122 "\n"
123 "The build directory is the same as your $HOME!\n"
124 "Buiding PISM here would result in a big mess. "
125 "Please create a special build directory and run cmake from there.\n")
126 endif()
127 endif()
128 endmacro()
129
130 macro(pism_find_prerequisites)
131 # PETSc
132 find_package (PETSc REQUIRED)
133 if (DEFINED PETSC_VERSION)
134
135 # FindPETSc.cmake does not put PETSC_VERSION into the CMake cache,
136 # so we save it here.
137 set(Pism_PETSC_VERSION ${PETSC_VERSION} CACHE STRING "PETSc version")
138 mark_as_advanced(Pism_PETSC_VERSION)
139
140 if (PETSC_VERSION VERSION_LESS 3.5)
141 # Force PISM to look for PETSc again if the version we just found
142 # is too old:
143 set(PETSC_CURRENT "OFF" CACHE BOOL "" FORCE)
144 # Stop with an error message.
145 message(FATAL_ERROR "PISM requires PETSc version 3.5 or newer (found ${PETSC_VERSION}).")
146 endif()
147
148 if (PETSC_VERSION VERSION_EQUAL 3.6.0)
149 # Force PISM to look for PETSc again if the version we just found
150 # is not supported
151 set(PETSC_CURRENT "OFF" CACHE BOOL "" FORCE)
152 # Stop with an error message.
153 message(FATAL_ERROR "PISM does not support PETSc ${PETSC_VERSION}. Please install PETSc <= 3.5.4 or PETSc > 3.6.0.")
154 endif()
155
156 endif (DEFINED PETSC_VERSION)
157
158 # MPI
159 find_package (MPI REQUIRED COMPONENTS C)
160
161 # Other required libraries
162 find_package (UDUNITS2 REQUIRED)
163 find_package (GSL REQUIRED)
164 find_package (NetCDF REQUIRED)
165 find_package (FFTW REQUIRED)
166 find_package (HDF5 COMPONENTS C HL)
167
168 # Optional libraries
169 if (Pism_USE_PNETCDF)
170 find_package (PNetCDF REQUIRED)
171 endif()
172
173 if (Pism_USE_PROJ)
174 find_package (PROJ REQUIRED)
175 endif()
176
177 if (Pism_USE_PIO)
178 find_package (ParallelIO REQUIRED)
179 endif()
180
181 if (Pism_USE_PARALLEL_NETCDF4)
182 # Try to find netcdf_par.h. We assume that NetCDF was compiled with
183 # parallel I/O if this header is present.
184 find_file(NETCDF_PAR_H netcdf_par.h HINTS ${NETCDF_INCLUDES} NO_DEFAULT_PATH)
185
186 # Set default values for build options
187 if (NOT NETCDF_PAR_H)
188 message(FATAL_ERROR
189 "Selected NetCDF library (include: ${NETCDF_INCLUDES}, lib: ${NETCDF_LIBRARIES}) does not support parallel I/O.")
190 endif()
191 endif()
192
193 if (Pism_USE_JANSSON)
194 find_package(Jansson REQUIRED)
195
196 if (NOT JANSSON_FOUND)
197 set(pism_jansson_dir ${Pism_BINARY_DIR}/jansson)
198 include(ExternalProject)
199 ExternalProject_Add(pism_jansson
200 GIT_REPOSITORY https://github.com/akheron/jansson.git
201 GIT_TAG 2.7
202 TIMEOUT 10
203 PREFIX ${Pism_BINARY_DIR} # install with PISM
204 INSTALL_DIR ${pism_jansson_dir}
205 CMAKE_ARGS -DJANSSON_BUILD_DOCS=OFF -DCMAKE_INSTALL_PREFIX=${pism_jansson_dir}
206 LOG_DOWNLOAD ON
207 LOG_BUILD ON
208 LOG_CONFIGURE ON
209 )
210 set(JANSSON_INCLUDE_DIRS ${pism_jansson_dir}/include CACHE STRING "Jansson include directory" OFRCE)
211 set(JANSSON_LIBRARIES "-L${pism_jansson_dir}/lib -ljansson" CACHE STRING "Jansson library" FORCE)
212 set(Pism_BUILD_JANSSON ON CACHE BOOL "ON if we are using our own Jansson build." FORCE)
213 message(WARNING "
214 Jansson was not found.
215 We will try to download and build it automatically. If it does not work, please install it manually and try again.
216 ")
217 endif()
218 endif()
219
220 endmacro()
221
222 macro(pism_set_dependencies)
223
224 # Set include and library directories for *required* libraries.
225 include_directories (BEFORE
226 ${PETSC_INCLUDES}
227 ${FFTW_INCLUDES}
228 ${GSL_INCLUDES}
229 ${UDUNITS2_INCLUDES}
230 ${NETCDF_INCLUDES}
231 ${MPI_C_INCLUDE_PATH})
232
233 # Use option values to set compiler and linker flags
234 set (Pism_EXTERNAL_LIBS "")
235
236 # required libraries
237 list (APPEND Pism_EXTERNAL_LIBS
238 ${PETSC_LIBRARIES}
239 ${UDUNITS2_LIBRARIES}
240 ${FFTW_LIBRARIES}
241 ${GSL_LIBRARIES}
242 ${NETCDF_LIBRARIES}
243 ${MPI_C_LIBRARIES}
244 ${HDF5_LIBRARIES}
245 ${HDF5_HL_LIBRARIES})
246
247 # optional libraries
248 if (Pism_USE_JANSSON)
249 include_directories (${JANSSON_INCLUDE_DIRS})
250 list (APPEND Pism_EXTERNAL_LIBS ${JANSSON_LIBRARIES})
251 endif()
252
253 if (Pism_USE_PROJ)
254 include_directories (${PROJ_INCLUDES})
255 list (APPEND Pism_EXTERNAL_LIBS ${PROJ_LIBRARIES})
256 endif()
257
258 if (Pism_USE_PIO)
259 include_directories (${ParallelIO_INCLUDES})
260 list (APPEND Pism_EXTERNAL_LIBS ${ParallelIO_LIBRARIES})
261 endif()
262
263 if (Pism_USE_PNETCDF)
264 include_directories (${PNETCDF_INCLUDES})
265 list (APPEND Pism_EXTERNAL_LIBS ${PNETCDF_LIBRARIES})
266 endif()
267
268 # Hide distracting CMake variables
269 mark_as_advanced(file_cmd MPI_LIBRARY MPI_EXTRA_LIBRARY
270 HDF5_C_LIBRARY_dl HDF5_C_LIBRARY_hdf5 HDF5_C_LIBRARY_hdf5_hl HDF5_C_LIBRARY_m HDF5_C_LIBRARY_z
271 CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT
272 MAKE_EXECUTABLE HDF5_DIR NETCDF_PAR_H)
273
274 endmacro()
275
276 macro (pism_petsc_get_variable name var)
277 set (pism_petsc_config_makefile "${PROJECT_BINARY_DIR}/Makefile.pism_petsc")
278 file (WRITE "${pism_petsc_config_makefile}"
279 "## This file was autogenerated by FindPETSc.cmake
280 # PETSC_DIR = ${PETSC_DIR}
281 # PETSC_ARCH = ${PETSC_ARCH}
282 include ${petsc_conf_rules}
283 include ${petsc_conf_variables}
284 show :
285 \t-@echo -n \${\${VARIABLE}}
286 ")
287 set (${var} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
288 execute_process (COMMAND ${MAKE_EXECUTABLE} --no-print-directory -f ${pism_petsc_config_makefile} show VARIABLE=${name}
289 OUTPUT_VARIABLE ${var}
290 RESULT_VARIABLE petsc_return)
291 string(CONFIGURE "\${${var}}" ${var} ESCAPE_QUOTES)
292 file (REMOVE ${pism_petsc_config_makefile})
293 endmacro (pism_petsc_get_variable)
294
295 # Make sure that PetscScalar is double and not complex.
296 macro(pism_check_petsc_scalar_type)
297 pism_petsc_get_variable("PETSC_SCALAR" PISM_PETSC_SCALAR)
298
299 if (${PISM_PETSC_SCALAR} MATCHES "complex")
300 message(FATAL_ERROR
301 "PETSc configured with --with-scalar-type=complex cannot be used to build PISM.")
302 endif()
303 endmacro()
304
305 # Create a list of subdirectories.
306 # See https://stackoverflow.com/questions/7787823/cmake-how-to-get-the-name-of-all-subdirectories-of-a-directory
307 MACRO(SUBDIRLIST result curdir)
308 FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
309 SET(dirlist "")
310 FOREACH(child ${children})
311 IF(IS_DIRECTORY ${curdir}/${child})
312 LIST(APPEND dirlist ${child})
313 ENDIF()
314 ENDFOREACH()
315 SET(${result} ${dirlist})
316 ENDMACRO()