Install main bins with .exe links similar to WRF cmake
[WPS.git] / CMakeLists.txt
blobbd6ce9b8457784b12982e7fe7c3b0453797511e3
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 AND NOT DEFINED WRF_ROOT )
13   message( STATUS "No WRF_DIR or WRF_ROOT 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 )
38 include( wrf_case_setup )
41 # Grab version info
42 wrf_get_version( ${PROJECT_SOURCE_DIR}/README )
44 ################################################################################
46 ## MPI & OpenMP
48 ################################################################################
49 if ( ${USE_MPI} )
50   # Through ***MUCH*** debugging, if utilizing MPI_<LANG>_COMPILER
51   # https://cmake.org/cmake/help/latest/module/FindMPI.html#variables-for-locating-mpi
52   # the find logic makes a mess of things by utilizing <mpi> -show[me]
53   # Which may or may not get polluted by the environment
54   # It still technically finds MPI but the output is nonintuitive 
55   # saying things like hdf5 or pthread
56   find_package( MPI REQUIRED COMPONENTS Fortran C )
57   add_compile_definitions(
58                           USE_MPI=1
59                           DM_PARALLEL
60                           )
62   if ( DEFINED WRF_MPI_Fortran_FLAGS AND NOT "${WRF_MPI_Fortran_FLAGS}" STREQUAL "" )
63     add_compile_options(
64                         $<$<COMPILE_LANGUAGE:Fortran>,${WRF_MPI_Fortran_FLAGS}>
65                         )
66   endif()
68   if ( DEFINED WRF_MPI_C_FLAGS AND NOT "${WRF_MPI_C_FLAGS}" STREQUAL "" )
69     add_compile_options(
70                         $<$<COMPILE_LANGUAGE:C>,${WRF_MPI_C_FLAGS}>
71                         )
72   endif()
73 endif()
75 if ( ${USE_OPENMP} )
76   find_package( OpenMP REQUIRED COMPONENTS Fortran C )
77   add_compile_definitions( USE_OPENMP=1 SM_PARALLEL )
78 endif()
81 # First do externals
82 if ( ${BUILD_EXTERNALS} )
83   add_subdirectory( external )
84   # If we got here everything built, we are safe to add find paths for libs
85   set( ZLIB_ROOT   ${INTERNAL_GRIB2_PATH} ) # This may get overridden by HDF5 if zlib is packaged with that
86   set( PNG_ROOT    ${INTERNAL_GRIB2_PATH} )
87   set( Jasper_ROOT ${INTERNAL_GRIB2_PATH} )
88 endif()
91 # Now find required libraries, which may have been affected by externals
92 if ( DEFINED WRF_DIR OR DEFINED WRF_ROOT )
93   find_package( WRF COMPONENTS WRF_Core io_netcdf io_grib1 io_int REQUIRED )
94   if ( ${WRF_FOUND} )
95     message( STATUS "Found WRF : ${WRF_CONFIG} (found version \"${WRF_VERSION}\")" )
96   endif()
97 endif()
99 # Find externals now -- this is a little different than normal since we want WRF to 
100 # take precedence if it is present on which libraries to use to avoid library splicing
101 # rather than finding our external thirdparty libraries first
102 find_package( ZLIB   REQUIRED )
103 find_package( PNG    REQUIRED )
104 find_package( Jasper 1.900.1...1.900.29 REQUIRED )
106 # Because WRF might use a different version of zlib, check to see if we are using a different one
107 # when externals (really internals) are used
108 if ( ${BUILD_EXTERNALS} AND ${WRF_FOUND} )
109   if ( NOT ( ${ZLIB_INCLUDE_DIRS} STREQUAL ${INTERNAL_GRIB2_PATH}/include ) )
110     message( STATUS "Note: Using WRF-specified zlib instead of ${INTERNAL_GRIB2_PATH}" )
111   endif()
112 endif()
114 # Add config definitions
115 message( STATUS "Adding configuration specific definitions : ${WPS_DEFINITIONS}" )
117 string( REPLACE " " ";" WPS_DEFINITIONS_LIST ${WPS_DEFINITIONS} )
119 # Make a copy, filter for anything not -D
120 set( WPS_UNDEFINITIONS_LIST ${WPS_DEFINITIONS_LIST} )
121 list( FILTER WPS_UNDEFINITIONS_LIST EXCLUDE REGEX "^-D(.*)" )
123 # Add this to the cpp processing
124 list( APPEND CMAKE_C_PREPROCESSOR_FLAGS ${WPS_UNDEFINITIONS_LIST} -P -nostdinc -traditional )
126 # Filter for anything that is -D
127 list( FILTER WPS_DEFINITIONS_LIST INCLUDE REGEX "^-D(.*)" )
129 # In CMake 2.26 this will not be necessary
130 list( TRANSFORM WPS_DEFINITIONS_LIST REPLACE "^-D(.*)" "\\1" )
132 add_compile_definitions( 
133                         ${WPS_DEFINITIONS_LIST}
134                         ${WPS_UNDEFINITIONS_LIST}
135                         $<$<COMPILE_LANGUAGE:Fortran>:USE_JPEG2000>
136                         $<$<COMPILE_LANGUAGE:Fortran>:USE_PNG>
137                         )
138 # Whole project flags
139 add_compile_options(
140                     $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fallow-argument-mismatch>
141                     )
144 # Always build whatever we can
145 add_subdirectory( ungrib )
147 if ( DEFINED WRF_DIR )
148   # WRF Specific things
149   add_subdirectory( metgrid )
150   add_subdirectory( geogrid )
151 endif()
153 add_subdirectory( util )
156 ################################################################################
158 ## Install and export
160 ################################################################################
161 wrf_setup_targets(
162                   TARGETS
163                     $<TARGET_NAME_IF_EXISTS:geogrid>
164                     $<TARGET_NAME_IF_EXISTS:metgrid>
165                     $<TARGET_NAME_IF_EXISTS:ungrib>
166                   DEST_PATH       ${CMAKE_INSTALL_PREFIX}/bin
167                   )
170 wrf_setup_files( 
171                 FILES
172                   ${PROJECT_SOURCE_DIR}/link_grib.csh
173                 DEST_PATH
174                   ${CMAKE_INSTALL_PREFIX}/bin
175                 USE_SYMLINKS
176                 )
179 # Install to namespace
180 install(
181         EXPORT      ${EXPORT_NAME}Targets
182         DESTINATION lib/cmake/
183         FILE        ${EXPORT_NAME}Targets.cmake
184         NAMESPACE   ${EXPORT_NAME}::
185         )
187 configure_package_config_file(
188                               ${PROJECT_SOURCE_DIR}/cmake/template/${EXPORT_NAME}Config.cmake.in
189                               ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
190                               INSTALL_DESTINATION lib/cmake
191                               )
193 write_basic_package_version_file(
194                                   ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
195                                   VERSION ${PROJECT_VERSION}
196                                   #!TODO Check if this is the type of versioning support we want to use
197                                   COMPATIBILITY SameMinorVersion
198                                   )
200 install(
201         FILES
202           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}Config.cmake
203           ${CMAKE_BINARY_DIR}/${EXPORT_NAME}ConfigVersion.cmake
204         DESTINATION lib/cmake
205         )
207 # Install some helper files for anyone using this build as part of their code
208 install(
209         DIRECTORY
210           # Trailing / is important
211           ${PROJECT_SOURCE_DIR}/cmake/modules/
212         COMPONENT       helpers
213         DESTINATION     share
214         FILES_MATCHING  
215           PATTERN       "*.cmake"
216         )