Bringing in changes from respective wrf module
[WPS.git] / CMakeLists.txt
blobb8b04ff2a85b82b22dc36964c7d4f9d557cb46f2
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 # if ( DEFINED CMAKE_TOOLCHAIN_FILE )
17 #   set( WPS_CONFIG ${CMAKE_TOOLCHAIN_FILE} )
18 #   message( STATUS "Loading configuration file... : ${WPS_CONFIG}" )
19 #   include( ${WPS_CONFIG} )
20 # endif()
22 # list( APPEND CMAKE_MODULE_PATH         )
23 list( APPEND CMAKE_MODULE_PATH 
24       ${PROJECT_SOURCE_DIR}/cmake/
25       ${PROJECT_SOURCE_DIR}/cmake/modules
26       )
28 # I'd love to be able to do something like this, but this would place a depedency
29 # on WRF which would be bad since who would expect the *WRF Preprocessing System*
30 # to have a strict dependency on WRF. Silly me
31 # if ( DEFINED WRF_DIR )
32 #   list( APPEND CMAKE_MODULE_PATH 
33 #       ${WRF_DIR}/share
34 #       )
35 # endif()
37 # Use link paths as rpaths 
38 set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
39 set( CMAKE_Fortran_PREPROCESS          ON )
41 include( CMakePackageConfigHelpers )
42 # include( confcheck   )
43 # include( gitinfo     )
44 include( wrf_get_version )
46 # Grab version info
47 wrf_get_version( ${PROJECT_SOURCE_DIR}/README )
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 ( DEFINED WRF_DIR )
98   find_package( WRF COMPONENTS WRF_Core io_netcdf io_grib1 io_int HINTS ${WRF_DIR}/lib )
99 endif()
101 # Find externals now -- this is a little different than normal since we want WRF to 
102 # take precedence if it is present on which libraries to use to avoid library splicing
103 # rather than finding our external thirdparty libraries first
104 find_package( ZLIB   REQUIRED )
105 find_package( PNG    REQUIRED )
106 find_package( Jasper REQUIRED )
108 # Add config definitions
109 message( STATUS "Adding configuration specific definitions : ${WPS_DEFINITIONS}" )
110 # In CMake 2.26 this will not be necessary
111 string( REPLACE " " ";" WPS_DEFINITIONS_LIST ${WPS_DEFINITIONS} )
112 list( TRANSFORM WPS_DEFINITIONS_LIST REPLACE "^-D(.*)" "\\1" )
113 add_compile_definitions( 
114                         ${WPS_DEFINITIONS_LIST}
115                         $<$<COMPILE_LANGUAGE:Fortran>:USE_JPEG2000>
116                         $<$<COMPILE_LANGUAGE:Fortran>:USE_PNG>
117                         )
118 # Whole project flags
119 add_compile_options(
120                     $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fallow-argument-mismatch>
121                     )
124 # Always build whatever we can
125 add_subdirectory( ungrib )
127 if ( DEFINED WRF_DIR )
128   # WRF Specific things
129   add_subdirectory( metgrid )
130   add_subdirectory( geogrid )
131 endif()
134 ################################################################################
136 ## Install and export
138 ################################################################################
140 # Install to namespace
141 install(
142         EXPORT      ${EXPORT_NAME}Targets
143         DESTINATION lib/cmake/
144         FILE        ${EXPORT_NAME}Targets.cmake
145         NAMESPACE   ${EXPORT_NAME}::
146         )
148 configure_package_config_file(
149                               ${PROJECT_SOURCE_DIR}/cmake/template/${EXPORT_NAME}Config.cmake.in
150                               ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
151                               INSTALL_DESTINATION lib/cmake
152                               )
154 write_basic_package_version_file(
155                                   ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
156                                   VERSION ${PROJECT_VERSION}
157                                   #!TODO Check if this is the type of versioning support we want to use
158                                   COMPATIBILITY SameMinorVersion
159                                   )
161 install(
162         FILES
163           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
164           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
165         DESTINATION lib/cmake
166         )
168 # Install some helper files for anyone using this build as part of their code
169 install(
170         DIRECTORY
171           # Trailing / is important
172           ${PROJECT_SOURCE_DIR}/cmake/modules/
173         COMPONENT       helpers
174         DESTINATION     share
175         FILES_MATCHING  
176           PATTERN       "*.cmake"
177         )
178 # install(
179 #         FILES
180 #           ${PROJECT_SOURCE_DIR}/cmake/confcheck.cmake
181 #           ${PROJECT_SOURCE_DIR}/cmake/gitinfo.cmake
182 #           ${PROJECT_SOURCE_DIR}/cmake/printOption.cmake
183 #           ${PROJECT_SOURCE_DIR}/cmake/wrf_get_version.cmake
184 #         COMPONENT     helpers
185 #         DESTINATION   share
186 #         )