1 cmake_minimum_required( VERSION 3.20 )
2 cmake_policy( SET CMP0118 NEW )
6 enable_language( Fortran )
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} )
18 list( APPEND CMAKE_MODULE_PATH
19 ${PROJECT_SOURCE_DIR}/cmake/
20 ${PROJECT_SOURCE_DIR}/cmake/modules
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
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 )
39 include( wrf_get_version )
40 include( wrf_case_setup )
44 wrf_get_version( ${PROJECT_SOURCE_DIR}/README )
46 # Disable WRF-specifics entirely
47 set( USE_WRF ON CACHE BOOL "USE_WRF" )
49 ################################################################################
53 ################################################################################
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(
67 if ( DEFINED WRF_MPI_Fortran_FLAGS AND NOT "${WRF_MPI_Fortran_FLAGS}" STREQUAL "" )
69 $<$<COMPILE_LANGUAGE:Fortran>,${WRF_MPI_Fortran_FLAGS}>
73 if ( DEFINED WRF_MPI_C_FLAGS AND NOT "${WRF_MPI_C_FLAGS}" STREQUAL "" )
75 $<$<COMPILE_LANGUAGE:C>,${WRF_MPI_C_FLAGS}>
81 find_package( OpenMP REQUIRED COMPONENTS Fortran C )
82 add_compile_definitions( USE_OPENMP=1 SM_PARALLEL )
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} )
96 # Now find required libraries, which may have been affected by externals
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
107 WRF_Core io_netcdf io_grib1 io_int
110 ${PROJECT_SOURCE_DIR}/../WRF/install
111 ${PROJECT_SOURCE_DIR}/../wrf/install
114 message( STATUS "Found WRF : ${WRF_CONFIG} (found version \"${WRF_VERSION}\")" )
116 elseif ( ${USE_WRF} )
117 message( STATUS "No WRF_DIR or WRF_ROOT provided, skipping targets that rely on WRF" )
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}" )
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>
159 # Whole project flags
161 $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fallow-argument-mismatch>
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 )
174 add_subdirectory( util )
177 ################################################################################
179 ## Install and export
181 ################################################################################
183 if ( "${WRF_FOUND}" )
189 DEST_PATH ${CMAKE_INSTALL_PREFIX}/bin
196 DEST_PATH ${CMAKE_INSTALL_PREFIX}/bin
202 ${PROJECT_SOURCE_DIR}/link_grib.csh
204 ${CMAKE_INSTALL_PREFIX}/bin
209 # Install to namespace
211 EXPORT ${EXPORT_NAME}Targets
212 DESTINATION lib/cmake/
213 FILE ${EXPORT_NAME}Targets.cmake
214 NAMESPACE ${EXPORT_NAME}::
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
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
232 ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
233 ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
234 DESTINATION lib/cmake
237 # Install some helper files for anyone using this build as part of their code
240 # Trailing / is important
241 ${PROJECT_SOURCE_DIR}/cmake/modules/