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