|
| 1 | +### setup project ### |
| 2 | +# https://numpy.org/doc/stable/f2py/buildtools/skbuild.html |
| 3 | +cmake_minimum_required(VERSION 3.18) |
| 4 | + |
| 5 | +project(${SKBUILD_PROJECT_NAME} |
| 6 | + VERSION ${SKBUILD_PROJECT_VERSION} |
| 7 | + DESCRIPTION "Utilities for reading WRF output" |
| 8 | + LANGUAGES C Fortran |
| 9 | + ) |
| 10 | + |
| 11 | +# Safety net |
| 12 | +if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) |
| 13 | + message( |
| 14 | + FATAL_ERROR |
| 15 | + "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\n" |
| 16 | + ) |
| 17 | +endif() |
| 18 | + |
| 19 | +find_package(Python COMPONENTS Interpreter Development.Module NumPy REQUIRED) |
| 20 | + |
| 21 | +# Ensure scikit-build modules |
| 22 | +if (NOT SKBUILD) |
| 23 | + # Kanged --> https://github.com/Kitware/torch_liberator/blob/master/CMakeLists.txt |
| 24 | + # If skbuild is not the driver; include its utilities in CMAKE_MODULE_PATH |
| 25 | + execute_process( |
| 26 | + COMMAND "${Python_EXECUTABLE}" |
| 27 | + -c "import os, skbuild; print(os.path.dirname(skbuild.__file__))" |
| 28 | + OUTPUT_VARIABLE SKBLD_DIR |
| 29 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 30 | + ) |
| 31 | + list(APPEND CMAKE_MODULE_PATH "${SKBLD_DIR}/resources/cmake") |
| 32 | + message(STATUS "Looking in ${SKBLD_DIR}/resources/cmake for CMake modules") |
| 33 | +endif() |
| 34 | + |
| 35 | +# Grab the variables from a local Python installation |
| 36 | +# NumPy headers |
| 37 | +# F2PY headers |
| 38 | +execute_process( |
| 39 | + COMMAND "${Python_EXECUTABLE}" |
| 40 | + -c "import numpy.f2py; print(numpy.f2py.get_include())" |
| 41 | + OUTPUT_VARIABLE F2PY_INCLUDE_DIR |
| 42 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 43 | +) |
| 44 | + |
| 45 | +find_package(OpenMP COMPONENTS Fortran) |
| 46 | +set_source_files_properties(fortran/ompgen.F90 PROPERTIES Fortran_PREPROCESS ON) |
| 47 | +# TODO: Figure out the conditionals for running the C Preprocessor on Fortran files |
| 48 | +# I think the main thing to be changed is -E -cpp |
| 49 | +# Intel is -fpp -save-temps or /fpp on Windows |
| 50 | +# or call fpp instead of the fortran compiler to get it to stop after preprocessing |
| 51 | +if (${OpenMP_Fortran_FOUND}) |
| 52 | + # This would probably be cleaner if I shoved it in the subdirectory |
| 53 | + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fortran") |
| 54 | + add_executable(sizes "${CMAKE_SOURCE_DIR}/fortran/build_help/omp_sizes.f90") |
| 55 | + target_link_libraries(sizes PUBLIC OpenMP::OpenMP_Fortran) |
| 56 | + add_custom_command( |
| 57 | + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/fortran/ompgen.F90" |
| 58 | + DEPENDS "${CMAKE_SOURCE_DIR}/fortran/ompgen.F90.template" |
| 59 | + ${CMAKE_SOURCE_DIR}/fortran/build_help/sub_sizes.py |
| 60 | + sizes |
| 61 | + COMMAND |
| 62 | + ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/fortran/build_help/sub_sizes.py |
| 63 | + ${CMAKE_SOURCE_DIR}/fortran/ompgen.F90.template |
| 64 | + ${CMAKE_CURRENT_BINARY_DIR}/fortran/ompgen.F90 |
| 65 | + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" |
| 66 | + ) |
| 67 | + add_custom_command( |
| 68 | + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/fortran/omp.f90" |
| 69 | + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/fortran/ompgen.F90" |
| 70 | + COMMAND ${CMAKE_Fortran_COMPILER} -E "${CMAKE_CURRENT_BINARY_DIR}/fortran/ompgen.F90" |
| 71 | + -o "${CMAKE_CURRENT_BINARY_DIR}/fortran/omp.f90" ${OpenMP_Fortran_FLAGS} -cpp |
| 72 | + ) |
| 73 | +else() |
| 74 | + add_custom_command( |
| 75 | + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/fortran/omp.f90" |
| 76 | + DEPENDS "${CMAKE_SOURCE_DIR}/fortran/ompgen.F90" |
| 77 | + COMMAND ${CMAKE_Fortran_COMPILER} -E fortran/ompgen.F90 -o fortran/omp.f90 -cpp |
| 78 | + ) |
| 79 | +endif() |
| 80 | + |
| 81 | +# Prepping the module |
| 82 | +set(f2py_module_name "_wrffortran") |
| 83 | +set(fortran_src_files |
| 84 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_constants.f90" |
| 85 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_testfunc.f90" |
| 86 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_user.f90" |
| 87 | + "${CMAKE_SOURCE_DIR}/fortran/rip_cape.f90" |
| 88 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_cloud_fracf.f90" |
| 89 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_fctt.f90" |
| 90 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_user_dbz.f90" |
| 91 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_relhl.f90" |
| 92 | + "${CMAKE_SOURCE_DIR}/fortran/calc_uh.f90" |
| 93 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_user_latlon_routines.f90" |
| 94 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_pvo.f90" |
| 95 | + "${CMAKE_SOURCE_DIR}/fortran/eqthecalc.f90" |
| 96 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_rip_phys_routines.f90" |
| 97 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_pw.f90" |
| 98 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_vinterp.f90" |
| 99 | + "${CMAKE_SOURCE_DIR}/fortran/wrf_wind.f90" |
| 100 | + "${CMAKE_CURRENT_BINARY_DIR}/fortran/omp.f90") |
| 101 | +set(python_src_files |
| 102 | + "${CMAKE_SOURCE_DIR}/src/wrf/__init__.py" |
| 103 | + "${CMAKE_SOURCE_DIR}/src/wrf/api.py" |
| 104 | + "${CMAKE_SOURCE_DIR}/src/wrf/cache.py" |
| 105 | + "${CMAKE_SOURCE_DIR}/src/wrf/computation.py" |
| 106 | + "${CMAKE_SOURCE_DIR}/src/wrf/config.py" |
| 107 | + "${CMAKE_SOURCE_DIR}/src/wrf/constants.py" |
| 108 | + "${CMAKE_SOURCE_DIR}/src/wrf/contrib.py" |
| 109 | + "${CMAKE_SOURCE_DIR}/src/wrf/coordpair.py" |
| 110 | + "${CMAKE_SOURCE_DIR}/src/wrf/decorators.py" |
| 111 | + "${CMAKE_SOURCE_DIR}/src/wrf/destag.py" |
| 112 | + "${CMAKE_SOURCE_DIR}/src/wrf/extension.py" |
| 113 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_cape.py" |
| 114 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_cloudfrac.py" |
| 115 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_ctt.py" |
| 116 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_dbz.py" |
| 117 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_dewpoint.py" |
| 118 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_geoht.py" |
| 119 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_helicity.py" |
| 120 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_latlon.py" |
| 121 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_omega.py" |
| 122 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_precip.py" |
| 123 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_pressure.py" |
| 124 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_pw.py" |
| 125 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_rh.py" |
| 126 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_slp.py" |
| 127 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_temp.py" |
| 128 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_terrain.py" |
| 129 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_times.py" |
| 130 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_uvmet.py" |
| 131 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_vorticity.py" |
| 132 | + "${CMAKE_SOURCE_DIR}/src/wrf/g_wind.py" |
| 133 | + "${CMAKE_SOURCE_DIR}/src/wrf/geobnds.py" |
| 134 | + "${CMAKE_SOURCE_DIR}/src/wrf/interp.py" |
| 135 | + "${CMAKE_SOURCE_DIR}/src/wrf/interputils.py" |
| 136 | + "${CMAKE_SOURCE_DIR}/src/wrf/latlonutils.py" |
| 137 | + "${CMAKE_SOURCE_DIR}/src/wrf/metadecorators.py" |
| 138 | + "${CMAKE_SOURCE_DIR}/src/wrf/projection.py" |
| 139 | + "${CMAKE_SOURCE_DIR}/src/wrf/projutils.py" |
| 140 | + "${CMAKE_SOURCE_DIR}/src/wrf/py3compat.py" |
| 141 | + "${CMAKE_SOURCE_DIR}/src/wrf/routines.py" |
| 142 | + "${CMAKE_SOURCE_DIR}/src/wrf/specialdec.py" |
| 143 | + "${CMAKE_SOURCE_DIR}/src/wrf/units.py" |
| 144 | + "${CMAKE_SOURCE_DIR}/src/wrf/util.py" |
| 145 | + "${CMAKE_SOURCE_DIR}/src/wrf/version.py" |
| 146 | +) |
| 147 | +set(f2py_module_c "${f2py_module_name}module.c") |
| 148 | + |
| 149 | +# Target for enforcing dependencies |
| 150 | +add_custom_target(genpyf |
| 151 | + DEPENDS "${fortran_src_files}" |
| 152 | +) |
| 153 | +add_custom_command( |
| 154 | + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_c}" |
| 155 | + "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}-f2pywrappers.f" |
| 156 | + "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}-f2pywrappers2.f90" |
| 157 | + COMMAND ${Python_EXECUTABLE} -m "numpy.f2py" |
| 158 | + -m "${f2py_module_name}" |
| 159 | + --lower # Important |
| 160 | + ${fortran_src_files} |
| 161 | + DEPENDS "${fortran_src_files}" # Fortran source |
| 162 | +) |
| 163 | + |
| 164 | +Python_add_library(${f2py_module_name} MODULE |
| 165 | + "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_c}" |
| 166 | + "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}-f2pywrappers.f" |
| 167 | + "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}-f2pywrappers2.f90" |
| 168 | + "${F2PY_INCLUDE_DIR}/fortranobject.c" |
| 169 | + "${fortran_src_files}") |
| 170 | + |
| 171 | +target_include_directories(${f2py_module_name} PUBLIC |
| 172 | + ${F2PY_INCLUDE_DIR} |
| 173 | + ${Python_NumPy_INCLUDE_DIRS} |
| 174 | + ${Python_INCLUDE_DIRS}) |
| 175 | +set_target_properties(${f2py_module_name} PROPERTIES SUFFIX ".${Python_SOABI}${CMAKE_SHARED_MODULE_SUFFIX}") |
| 176 | +set_target_properties(${f2py_module_name} PROPERTIES PREFIX "") |
| 177 | + |
| 178 | +# https://scikit-build-core.readthedocs.io/en/latest/getting_started.html |
| 179 | +target_link_libraries(${f2py_module_name} PRIVATE Python::NumPy) |
| 180 | +if (${OpenMP_Fortran_FOUND}) |
| 181 | + target_link_libraries(${f2py_module_name} PUBLIC OpenMP::OpenMP_Fortran) |
| 182 | +endif() |
| 183 | + |
| 184 | +# Linker fixes |
| 185 | +if (UNIX) |
| 186 | + if (APPLE) |
| 187 | + set_target_properties(${f2py_module_name} PROPERTIES |
| 188 | + LINK_FLAGS '-Wl,-dylib,-undefined,dynamic_lookup') |
| 189 | + else() |
| 190 | + set_target_properties(${f2py_module_name} PROPERTIES |
| 191 | + LINK_FLAGS '-Wl,--allow-shlib-undefined') |
| 192 | + endif() |
| 193 | +endif() |
| 194 | + |
| 195 | +add_dependencies(${f2py_module_name} genpyf) |
| 196 | + |
| 197 | +if (NOT SKBUILD) |
| 198 | + string(REGEX REPLACE "^/(usr/(local/)?)?" "" Python_SITEARCH_INSTALL ${Python_SITEARCH}) |
| 199 | + string(REGEX REPLACE "^/(usr/(local/)?)?" "" Python_SITELIB_INSTALL ${Python_SITELIB}) |
| 200 | + # string(SUBSTRING ${Python_SITEARCH} 1 -1 Python_SITEARCH_INSTALL) |
| 201 | + # string(SUBSTRING ${Python_SITELIB} 1 -1 Python_SITELIB_INSTALL) |
| 202 | + |
| 203 | + install(TARGETS ${f2py_module_name} DESTINATION "${Python_SITEARCH_INSTALL}/wrf/") |
| 204 | + install(FILES ${python_src_files} DESTINATION "${Python_SITELIB_INSTALL}/wrf/") |
| 205 | + install(FILES src/wrf/data/psadilookup.dat DESTINATION "${Python_SITELIB_INSTALL}/wrf") |
| 206 | +else() |
| 207 | + # https://scikit-build-core.readthedocs.io/en/latest/cmakelists.html#install-directories |
| 208 | + install(TARGETS ${f2py_module_name} DESTINATION "${SKBUILD_PLATLIB_DIR}/wrf/") |
| 209 | +endif() |
0 commit comments