Use abs paths
[WPS.git] / CMakeLists.txt
blobcf7578de4eb8f058aa7f062f38aa7caecf92e350
1 cmake_minimum_required( VERSION 3.20 )
2 cmake_policy( SET CMP0118 NEW )
4 enable_language( C )
5 enable_language( CXX )
6 enable_language( Fortran )
8 project( WPS )
9 set( EXPORT_NAME          ${PROJECT_NAME} )
10 set( INTERNAL_GRIB2_PATH  ${CMAKE_INSTALL_PREFIX}/grib2 )
12 if ( DEFINED CMAKE_TOOLCHAIN_FILE )
13   set( WPS_CONFIG ${CMAKE_TOOLCHAIN_FILE} )
14 endif()
18 list( APPEND CMAKE_MODULE_PATH 
19       ${PROJECT_SOURCE_DIR}/cmake/
20       ${PROJECT_SOURCE_DIR}/cmake/modules
21       )
23 # I'd love to be able to do something like this, but this would place a depedency
24 # on WRF which would be bad since who would expect the *WRF Preprocessing System*
25 # to have a strict dependency on WRF. Silly me
26 # if ( DEFINED WRF_DIR )
27 #   list( APPEND CMAKE_MODULE_PATH 
28 #       ${WRF_DIR}/share
29 #       )
30 # endif()
32 # Use link paths as rpaths 
33 set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
34 set( CMAKE_Fortran_PREPROCESS          ON )
36 include( CMakePackageConfigHelpers )
37 # include( confcheck   )
38 # include( gitinfo     )
39 include( wrf_get_version )
40 include( wrf_case_setup )
43 # Grab version info
44 wrf_get_version( ${PROJECT_SOURCE_DIR}/README )
46 # Disable WRF-specifics entirely
47 set( USE_WRF      ON CACHE BOOL "USE_WRF"     )
49 ################################################################################
51 ## MPI & OpenMP
53 ################################################################################
54 if ( ${USE_MPI} )
55   # Through ***MUCH*** debugging, if utilizing MPI_<LANG>_COMPILER
56   # https://cmake.org/cmake/help/latest/module/FindMPI.html#variables-for-locating-mpi
57   # the find logic makes a mess of things by utilizing <mpi> -show[me]
58   # Which may or may not get polluted by the environment
59   # It still technically finds MPI but the output is nonintuitive 
60   # saying things like hdf5 or pthread
61   find_package( MPI REQUIRED COMPONENTS Fortran C )
62   add_compile_definitions(
63                           USE_MPI=1
64                           DM_PARALLEL
65                           )
67   if ( DEFINED WRF_MPI_Fortran_FLAGS AND NOT "${WRF_MPI_Fortran_FLAGS}" STREQUAL "" )
68     add_compile_options(
69                         $<$<COMPILE_LANGUAGE:Fortran>,${WRF_MPI_Fortran_FLAGS}>
70                         )
71   endif()
73   if ( DEFINED WRF_MPI_C_FLAGS AND NOT "${WRF_MPI_C_FLAGS}" STREQUAL "" )
74     add_compile_options(
75                         $<$<COMPILE_LANGUAGE:C>,${WRF_MPI_C_FLAGS}>
76                         )
77   endif()
78 endif()
80 if ( ${USE_OPENMP} )
81   find_package( OpenMP REQUIRED COMPONENTS Fortran C )
82   add_compile_definitions( USE_OPENMP=1 SM_PARALLEL )
83 endif()
86 # First do externals
87 if ( ${BUILD_EXTERNALS} )
88   add_subdirectory( external )
89   # If we got here everything built, we are safe to add find paths for libs
90   set( ZLIB_ROOT   ${INTERNAL_GRIB2_PATH} ) # This may get overridden by HDF5 if zlib is packaged with that
91   set( PNG_ROOT    ${INTERNAL_GRIB2_PATH} )
92   set( Jasper_ROOT ${INTERNAL_GRIB2_PATH} )
93 endif()
96 # Now find required libraries, which may have been affected by externals
97 if ( 
98     DEFINED WRF_DIR OR 
99     DEFINED WRF_ROOT OR 
100     DEFINED ENV{WRF_DIR} OR 
101     DEFINED ENV{WRF_ROOT} OR
102     EXISTS "${PROJECT_SOURCE_DIR}/../WRF/" OR EXISTS "${PROJECT_SOURCE_DIR}/../wrf/" )
103   # PATHS will be checked last
104   find_package( 
105                 WRF 
106                 COMPONENTS 
107                   WRF_Core io_netcdf io_grib1 io_int 
108                 REQUIRED 
109                 PATHS
110                   ${PROJECT_SOURCE_DIR}/../WRF/install
111                   ${PROJECT_SOURCE_DIR}/../wrf/install
112                 )
113   if ( ${WRF_FOUND} )
114     message( STATUS "Found WRF : ${WRF_CONFIG} (found version \"${WRF_VERSION}\")" )
115   endif()
116 elseif ( ${USE_WRF} )
117   message( STATUS "No WRF_DIR or WRF_ROOT provided, skipping targets that rely on WRF" )
118 endif()
120 # Find externals now -- this is a little different than normal since we want WRF to 
121 # take precedence if it is present on which libraries to use to avoid library splicing
122 # rather than finding our external thirdparty libraries first
123 find_package( ZLIB   REQUIRED )
124 find_package( PNG    REQUIRED )
125 find_package( Jasper 1.900.1...1.900.29 REQUIRED )
127 # Because WRF might use a different version of zlib, check to see if we are using a different one
128 # when externals (really internals) are used
129 if ( ${BUILD_EXTERNALS} AND "${WRF_FOUND}" )
130   if ( NOT ( ${ZLIB_INCLUDE_DIRS} STREQUAL ${INTERNAL_GRIB2_PATH}/include ) )
131     message( STATUS "Note: Using WRF-specified zlib instead of ${INTERNAL_GRIB2_PATH}" )
132   endif()
133 endif()
135 # Add config definitions
136 message( STATUS "Adding configuration specific definitions : ${WPS_DEFINITIONS}" )
138 string( REPLACE " " ";" WPS_DEFINITIONS_LIST ${WPS_DEFINITIONS} )
140 # Make a copy, filter for anything not -D
141 set( WPS_UNDEFINITIONS_LIST ${WPS_DEFINITIONS_LIST} )
142 list( FILTER WPS_UNDEFINITIONS_LIST EXCLUDE REGEX "^-D(.*)" )
144 # Add this to the cpp processing
145 list( APPEND CMAKE_C_PREPROCESSOR_FLAGS ${WPS_UNDEFINITIONS_LIST} -P -nostdinc -traditional )
147 # Filter for anything that is -D
148 list( FILTER WPS_DEFINITIONS_LIST INCLUDE REGEX "^-D(.*)" )
150 # In CMake 2.26 this will not be necessary
151 list( TRANSFORM WPS_DEFINITIONS_LIST REPLACE "^-D(.*)" "\\1" )
153 add_compile_definitions( 
154                         ${WPS_DEFINITIONS_LIST}
155                         ${WPS_UNDEFINITIONS_LIST}
156                         $<$<COMPILE_LANGUAGE:Fortran>:USE_JPEG2000>
157                         $<$<COMPILE_LANGUAGE:Fortran>:USE_PNG>
158                         )
159 # Whole project flags
160 add_compile_options(
161                     $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fallow-argument-mismatch>
162                     )
165 # Always build whatever we can
166 add_subdirectory( ungrib )
168 if ( DEFINED WRF_DIR )
169   # WRF Specific things
170   add_subdirectory( metgrid )
171   add_subdirectory( geogrid )
172 endif()
174 add_subdirectory( util )
177 ################################################################################
179 ## Install and export
181 ################################################################################
183 if ( "${WRF_FOUND}" )
185   wrf_setup_targets(
186                     TARGETS
187                       geogrid
188                       metgrid
189                     DEST_PATH       ${CMAKE_INSTALL_PREFIX}/bin
190                     )
191 endif()
193 wrf_setup_targets(
194                   TARGETS
195                     ungrib
196                   DEST_PATH       ${CMAKE_INSTALL_PREFIX}/bin
197                   )
200 wrf_setup_files( 
201                 FILES
202                   ${PROJECT_SOURCE_DIR}/link_grib.csh
203                 DEST_PATH
204                   ${CMAKE_INSTALL_PREFIX}/bin
205                 USE_SYMLINKS
206                 )
209 # Install to namespace
210 install(
211         EXPORT      ${EXPORT_NAME}Targets
212         DESTINATION lib/cmake/
213         FILE        ${EXPORT_NAME}Targets.cmake
214         NAMESPACE   ${EXPORT_NAME}::
215         )
217 configure_package_config_file(
218                               ${PROJECT_SOURCE_DIR}/cmake/template/${EXPORT_NAME}Config.cmake.in
219                               ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
220                               INSTALL_DESTINATION lib/cmake
221                               )
223 write_basic_package_version_file(
224                                   ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
225                                   VERSION ${PROJECT_VERSION}
226                                   #!TODO Check if this is the type of versioning support we want to use
227                                   COMPATIBILITY SameMinorVersion
228                                   )
230 install(
231         FILES
232           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
233           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
234         DESTINATION lib/cmake
235         )
237 # Install some helper files for anyone using this build as part of their code
238 install(
239         DIRECTORY
240           # Trailing / is important
241           ${PROJECT_SOURCE_DIR}/cmake/modules/
242         COMPONENT       helpers
243         DESTINATION     share
244         FILES_MATCHING  
245           PATTERN       "*.cmake"
246         )