Sync netCDF modules with WRF
[WPS.git] / CMakeLists.txt
blob9d9df3d340cb230791142e1cd4c50f9323939e76
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 ( NOT DEFINED WRF_DIR )
13   message( STATUS "No WRF_DIR provided, skipping targets that rely on WRF" )
14 endif()
16 list( APPEND CMAKE_MODULE_PATH 
17       ${PROJECT_SOURCE_DIR}/cmake/
18       ${PROJECT_SOURCE_DIR}/cmake/modules
19       )
21 # I'd love to be able to do something like this, but this would place a depedency
22 # on WRF which would be bad since who would expect the *WRF Preprocessing System*
23 # to have a strict dependency on WRF. Silly me
24 # if ( DEFINED WRF_DIR )
25 #   list( APPEND CMAKE_MODULE_PATH 
26 #       ${WRF_DIR}/share
27 #       )
28 # endif()
30 # Use link paths as rpaths 
31 set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
32 set( CMAKE_Fortran_PREPROCESS          ON )
34 include( CMakePackageConfigHelpers )
35 # include( confcheck   )
36 # include( gitinfo     )
37 include( wrf_get_version )
39 # Grab version info
40 wrf_get_version( ${PROJECT_SOURCE_DIR}/README )
42 ################################################################################
44 ## MPI & OpenMP
46 ################################################################################
47 if ( ${USE_MPI} )
48   # Through ***MUCH*** debugging, if utilizing MPI_<LANG>_COMPILER
49   # https://cmake.org/cmake/help/latest/module/FindMPI.html#variables-for-locating-mpi
50   # the find logic makes a mess of things by utilizing <mpi> -show[me]
51   # Which may or may not get polluted by the environment
52   # It still technically finds MPI but the output is nonintuitive 
53   # saying things like hdf5 or pthread
54   find_package( MPI REQUIRED COMPONENTS Fortran C )
55   add_compile_definitions(
56                           USE_MPI=1
57                           DM_PARALLEL
58                           )
60   if ( DEFINED WRF_MPI_Fortran_FLAGS AND NOT "${WRF_MPI_Fortran_FLAGS}" STREQUAL "" )
61     add_compile_options(
62                         $<$<COMPILE_LANGUAGE:Fortran>,${WRF_MPI_Fortran_FLAGS}>
63                         )
64   endif()
66   if ( DEFINED WRF_MPI_C_FLAGS AND NOT "${WRF_MPI_C_FLAGS}" STREQUAL "" )
67     add_compile_options(
68                         $<$<COMPILE_LANGUAGE:C>,${WRF_MPI_C_FLAGS}>
69                         )
70   endif()
71 endif()
73 if ( ${USE_OPENMP} )
74   find_package( OpenMP REQUIRED COMPONENTS Fortran C )
75   add_compile_definitions( USE_OPENMP=1 SM_PARALLEL )
76 endif()
79 # First do externals
80 if ( BUILD_EXTERNALS )
81   add_subdirectory( external )
82   # If we got here everything built, we are safe to add find paths for libs
83   set( ZLIB_ROOT   ${INTERNAL_GRIB2_PATH} ) # This may get overridden by HDF5 if zlib is packaged with that
84   set( PNG_ROOT    ${INTERNAL_GRIB2_PATH} )
85   set( Jasper_ROOT ${INTERNAL_GRIB2_PATH} )
86 endif()
89 # Now find required libraries, which may have been affected by externals
90 if ( DEFINED WRF_DIR )
91   find_package( WRF COMPONENTS WRF_Core io_netcdf io_grib1 io_int HINTS ${WRF_DIR}/lib )
92 endif()
94 # Find externals now -- this is a little different than normal since we want WRF to 
95 # take precedence if it is present on which libraries to use to avoid library splicing
96 # rather than finding our external thirdparty libraries first
97 find_package( ZLIB   REQUIRED )
98 find_package( PNG    REQUIRED )
99 find_package( Jasper REQUIRED )
101 # Add config definitions
102 message( STATUS "Adding configuration specific definitions : ${WPS_DEFINITIONS}" )
104 string( REPLACE " " ";" WPS_DEFINITIONS_LIST ${WPS_DEFINITIONS} )
106 # Make a copy, filter for anything not -D
107 set( WPS_UNDEFINITIONS_LIST ${WPS_DEFINITIONS_LIST} )
108 list( FILTER WPS_UNDEFINITIONS_LIST EXCLUDE REGEX "^-D(.*)" )
110 # Add this to the cpp processing
111 list( APPEND CMAKE_C_PREPROCESSOR_FLAGS ${WPS_UNDEFINITIONS_LIST} )
113 # Filter for anything that is -D
114 list( FILTER WPS_DEFINITIONS_LIST INCLUDE REGEX "^-D(.*)" )
116 # In CMake 2.26 this will not be necessary
117 list( TRANSFORM WPS_DEFINITIONS_LIST REPLACE "^-D(.*)" "\\1" )
119 add_compile_definitions( 
120                         ${WPS_DEFINITIONS_LIST}
121                         ${WPS_UNDEFINITIONS_LIST}
122                         $<$<COMPILE_LANGUAGE:Fortran>:USE_JPEG2000>
123                         $<$<COMPILE_LANGUAGE:Fortran>:USE_PNG>
124                         )
125 # Whole project flags
126 add_compile_options(
127                     $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fallow-argument-mismatch>
128                     )
131 # Always build whatever we can
132 add_subdirectory( ungrib )
134 if ( DEFINED WRF_DIR )
135   # WRF Specific things
136   add_subdirectory( metgrid )
137   add_subdirectory( geogrid )
138 endif()
140 add_subdirectory( util )
143 ################################################################################
145 ## Install and export
147 ################################################################################
149 # Install to namespace
150 install(
151         EXPORT      ${EXPORT_NAME}Targets
152         DESTINATION lib/cmake/
153         FILE        ${EXPORT_NAME}Targets.cmake
154         NAMESPACE   ${EXPORT_NAME}::
155         )
157 configure_package_config_file(
158                               ${PROJECT_SOURCE_DIR}/cmake/template/${EXPORT_NAME}Config.cmake.in
159                               ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
160                               INSTALL_DESTINATION lib/cmake
161                               )
163 write_basic_package_version_file(
164                                   ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
165                                   VERSION ${PROJECT_VERSION}
166                                   #!TODO Check if this is the type of versioning support we want to use
167                                   COMPATIBILITY SameMinorVersion
168                                   )
170 install(
171         FILES
172           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
173           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
174         DESTINATION lib/cmake
175         )
177 # Install some helper files for anyone using this build as part of their code
178 install(
179         DIRECTORY
180           # Trailing / is important
181           ${PROJECT_SOURCE_DIR}/cmake/modules/
182         COMPONENT       helpers
183         DESTINATION     share
184         FILES_MATCHING  
185           PATTERN       "*.cmake"
186         )