forked from PrincetonUniversity/SPEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
300 lines (264 loc) · 10.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# ATM the sources are added as it is. For some files, preprocessing is required. Use custom command to do preprocessing once it is understood.
set(MACROS ${CMAKE_CURRENT_SOURCE_DIR}/macros)
function(preprocess_m4 outvar)
message(STATUS "preprocess_fortran arguments: ${outvar}, followed by ${ARGN}")
set(srcs)
foreach(f ${ARGN})
message(STATUS "Got file: ${f}")
# construct output filename
if(NOT IS_ABSOLUTE "${f}")
get_filename_component(f "${f}" ABSOLUTE)
endif()
file(RELATIVE_PATH r "${CMAKE_CURRENT_SOURCE_DIR}" "${f}")
get_filename_component(e "${r}" EXT)
get_filename_component(n "${r}" NAME_WE)
get_filename_component(p "${r}" PATH)
string(TOUPPER "${e}" e)
set(of "${CMAKE_CURRENT_BINARY_DIR}/${n}_m${e}")
#set(of "${n}_m${e}")
message(STATUS "Output name: ${of}")
# preprocess the thing
add_custom_command(OUTPUT ${of}
COMMAND m4
ARGS -P ${MACROS} ${f} > ${of}
DEPENDS ${MACROS} ${f}
COMMENT "Preprocessing ${f}"
VERBATIM
)
list(APPEND srcs "${of}")
endforeach()
# return the (preprocessed) sources
set(${outvar} "${srcs}" PARENT_SCOPE)
endfunction()
set(unprocessed_src_files
manual.f90
rzaxis.f90
packxi.f90
volume.f90
coords.f90
basefn.f90
memory.f90
metrix.f90
ma00aa.f90
matrix.f90
spsmat.f90
spsint.f90
mp00ac.f90
ma02aa.f90
packab.f90
tr00ab.f90
curent.f90
df00ab.f90
lforce.f90
intghs.f90
mtrxhs.f90
lbpol.f90
brcast.f90
dfp100.f90
dfp200.f90
dforce.f90
newton.f90
casing.f90
bnorml.f90
jo00aa.f90
pp00aa.f90
pp00ab.f90
bfield.f90
stzxyz.f90
hesian.f90
ra00aa.f90
numrec.f90
preset.f90
#sphdf5.f90
global.f90
inputlist.f90
#${CMAKE_CURRENT_SOURCE_DIR}/xspech.f90
) # below assumes the .f files are double precision; the CFLAGS = -r8 option is not required;
set(f_src_files
${CMAKE_CURRENT_SOURCE_DIR}/dcuhre.f
${CMAKE_CURRENT_SOURCE_DIR}/minpack.f
${CMAKE_CURRENT_SOURCE_DIR}/iqpack.f
${CMAKE_CURRENT_SOURCE_DIR}/rksuite.f
${CMAKE_CURRENT_SOURCE_DIR}/i1mach.f
${CMAKE_CURRENT_SOURCE_DIR}/d1mach.f
${CMAKE_CURRENT_SOURCE_DIR}/ilut.f
${CMAKE_CURRENT_SOURCE_DIR}/iters.f
)
preprocess_m4(srcs ${unprocessed_src_files})
set(SPHDF5_FILE "${CMAKE_CURRENT_SOURCE_DIR}/sphdf5.f90")
set(SPHDF5_AWK_FILE "${CMAKE_CURRENT_SOURCE_DIR}/msphdf5.f90")
message(STATUS "sphdf5_FILE is ${SPHDF5_FILE}")
if(AWK MATCHES ".+-NOTFOUND")
message(FATAL_ERROR "FATAL: awk (and mawk and gawk) could not be found (${AWK}).")
else()
execute_process(
COMMAND /bin/sh -c "\"${AWK}\" -v file=sphdf5.f90 \
'{ gsub(\"__LINE__\", NR); gsub(\"__FILE__\",file); print }' \
\"${SPHDF5_FILE}\""
RESULT_VARIABLE AWK_EXITCODE
OUTPUT_FILE "${SPHDF5_AWK_FILE}"
)
message(STATUS "Exit code from awk: ${AWK_EXITCODE}")
endif()
preprocess_m4(sphdf5_src ${SPHDF5_AWK_FILE})
message(STATUS "sphdf5_src is ${sphdf5_src}")
#set(srcs)
#foreach(f ${unprocessed_src_files})
# # is it a Fortran file?
# if(f MATCHES "\\.[Ff](9[05])?")
# message(STATUS "Got fortran file: ${f}")
# # construct output filename
# if(NOT IS_ABSOLUTE "${f}")
# get_filename_component(f "${f}" ABSOLUTE)
# endif()
# file(RELATIVE_PATH r "${CMAKE_CURRENT_SOURCE_DIR}" "${f}")
# get_filename_component(e "${r}" EXT)
# get_filename_component(n "${r}" NAME_WE)
# get_filename_component(p "${r}" PATH)
# #set(of1 "${n}_m${e}")
# set(of "${CMAKE_CURRENT_BINARY_DIR}/${n}${e}")
# #set(of "${n}_m${e}")
# message(STATUS "Output name: ${of}")
# # preprocess the thing
# add_custom_command(OUTPUT ${of}
# COMMAND m4
# ARGS -P ${MACROS} ${f} > ${of}
# DEPENDS ${MACROS} ${f}
# COMMENT "Preprocessing ${f}"
# #VERBATIM
# )
# list(APPEND srcs "${of}")
# endif()
#endforeach()
#set_source_files_properties(${m4_output_files}
#set_source_files_properties(${src}
# PROPERTIES GENERATED true)
#add_custom_target(m4_process ALL
#DEPENDS ${m4_output_files}
# DEPENDS ${src}
#)
#add_dependencies(spec m4_process)
list(APPEND srcs "${sphdf5_src}")
add_library(spec3p OBJECT ${f_src_files})
target_compile_options(spec3p
PRIVATE
"-cpp"
$<$<Fortran_COMPILER_ID:GNU>:-ffree-line-length-none>
# $<$<Fortran_COMPILER_ID:GNU>:-fdefault-real-8>
$<$<Fortran_COMPILER_ID:GNU>:-fbounds-check>
# $<$<Fortran_COMPILER_ID:GNU>:-std=legacy>
$<$<Fortran_COMPILER_ID:GNU>:-fexternal-blas>
# $<$<Fortran_COMPILER_ID:Intel>:-r8>
)
set(ALLFILES manual rzaxis packxi volume coords basefn memory metrix ma00aa matrix spsmat spsint mp00ac ma02aa packab tr00ab curent df00ab lforce intghs mtrxhs lbpol brcast dfp100 dfp200 dforce newton casing bnorml jo00aa pp00aa pp00ab bfield stzxyz hesian ra00aa numrec dcuhre minpack iqpack rksuite i1mach d1mach ilut iters sphdf5 preset global xspech)
string(REPLACE ";" " " ALLFILES_STR "${ALLFILES}")
# Build spec executable
set(XSPEC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/xspech.f90")
set(XSPEC_AWK_FILE "${CMAKE_CURRENT_SOURCE_DIR}/mxspech.f90")
if(AWK MATCHES ".+-NOTFOUND")
message(FATAL_ERROR "FATAL: awk (and mawk and gawk) could not be found (${AWK}).")
else()
execute_process(
COMMAND bash -c "date"
OUTPUT_VARIABLE DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND /bin/sh -c "\"${AWK}\" -v date=\"${DATE}\" -v pwd=\"$ENV{PWD}\" -v macros=\"${MACROS}\" \
-v fc=\"${CMAKE_Fortran_COMPILER}\" -v flags=\"${COMP_DEFS}\" -v allfiles=\"${ALLFILES_STR}\" \
'BEGIN{nfiles=split(allfiles,files,\" \")} \
{if($2==\"COMPILATION\") { \
print \" write(ounit,*)\\\" : compiled : date = \"date\" ; \\\"\" ; \
print \" write(ounit,*)\\\" : : srcdir = \"pwd\" ; \\\"\" ; \
print \" write(ounit,*)\\\" : : macros = \"macros\" ; \\\"\" ; \
print \" write(ounit,*)\\\" : : fc = \"fc\" ; \\\"\" ; \
print \" write(ounit,*)\\\" : : flags = \"flags\" ; \\\"\" }} \
{if($2==\"SUMTIME\") {for (i=1;i<=nfiles;i++) print \" SUMTIME(\"files[i]\")\"}}\
{if($2==\"PRTTIME\") {for (i=1;i<=nfiles;i++) print \" PRTTIME(\"files[i]\")\"}}\
{print}' \"${XSPEC_FILE}\""
RESULT_VARIABLE AWK_EXITCODE
OUTPUT_FILE "${XSPEC_AWK_FILE}"
)
message(STATUS "Exit code from awk: ${AWK_EXITCODE}")
endif()
preprocess_m4(XSPEC_OUT_FILE ${XSPEC_AWK_FILE})
message(STATUS "XSPEC_OUT_FILE is ${XSPEC_OUT_FILE}")
list(APPEND srcs "${XSPEC_OUT_FILE}")
if(SKBUILD)
set(fortran_src_files "${srcs}" PARENT_SCOPE)
endif()
message(STATUS "srcs variable is ${srcs}")
add_library(spec
${srcs} "$<TARGET_OBJECTS:spec3p>"
)
#target_compile_options(spec PUBLIC "-cpp")
# For gfortran set the -ffree-line-length-none option
target_compile_options(spec
PUBLIC
"-cpp"
$<$<Fortran_COMPILER_ID:GNU>:-ffree-line-length-none>
$<$<Fortran_COMPILER_ID:GNU>:-fdefault-real-8>
$<$<Fortran_COMPILER_ID:GNU>:-fbounds-check>
#$<$<Fortran_COMPILER_ID:GNU>:-std=legacy>
$<$<Fortran_COMPILER_ID:GNU>:-fexternal-blas>
$<$<Fortran_COMPILER_ID:Intel>:-r8>
)
set_target_properties (spec PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY}/spec_modules)
target_include_directories(spec PUBLIC ${CMAKE_Fortran_MODULE_DIRECTORY}/spec_modules)
# If using MPI library, link against it
if(MPI_Fortran_FOUND AND NOT SKBUILD)
target_link_libraries(spec PUBLIC MPI::MPI_Fortran)
endif()
# Add OpenMP
if (OpenMP_Fortran_FOUND AND NOT SKBUILD)
target_link_libraries(spec PUBLIC OpenMP::OpenMP_Fortran)
target_compile_definitions(spec PUBLIC OPENMP)
target_link_libraries(spec3p PUBLIC OpenMP::OpenMP_Fortran)
target_compile_definitions(spec3p PUBLIC OPENMP)
endif()
# Add threads
#target_link_libraries(spec PUBLIC ${CMAKE_THREAD_LIBS_INIT})
# Alternatively
#target_link_libraries(spec PUBLIC Threads::Threads)
# Add libm
# target_link_libraries(spec PUBLIC ${POW_LIBS})
# Add HDF5
#target_link_libraries(spec PUBLIC ${HDF5_Fortran_HL_LIBRARIES} ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES} )
target_link_libraries(spec PUBLIC ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES} )
target_include_directories(spec PUBLIC ${HDF5_C_INCLUDE_DIRS} ${HDF5_Fortran_INCLUDE_DIRS})
#target_include_directories(spec PUBLIC ${HDF5_INCLUDE_DIRS})
#target_link_libraries(spec PUBLIC hdf5::hdf5 hdf5::hdf5_fortran hdf5::hdf5_hl_fortran)
# Add FFTW, LAPACK and BLAS libraries.
# MKL could be used for all the three
target_link_libraries(spec
PUBLIC
${FFTW_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES}
)
target_include_directories(spec
PUBLIC
${FFTW_INCLUDE_DIRS} ${LAPACK_INCLUDE_DIRS} ${BLAS_INCLUDE_DIRS}
)
#if(SKBUILD)
#set_target_properties(spec PROPERTIES POSITION_INDEPENDENT_CODE ON)
#endif()
get_property(COMP_DEFS
TARGET spec
PROPERTY COMPILE_OPTIONS
)
# Get the spec library linker properties and pass them to python wrapper
get_target_property(SPEC_LINK_LIB spec LINK_LIBRARIES)
message(STATUS "spec linked libraries are ${SPEC_LINK_LIB}")
get_target_property(SPEC_COMPILE_OPTIONS spec COMPILE_OPTIONS)
message(STATUS "spec compile options are ${SPEC_COMPILE_OPTIONS}")
get_target_property(SPEC_COMPILE_DEFS spec COMPILE_DEFINITIONS)
message(STATUS "spec compile definitions are ${SPEC_COMPILE_DEFS}")
# export linker flags for spec to parent scope
# for re-use when building python wrapper
if(SKBUILD)
# get_target_property(SPEC_LINK_LIB spec LINK_LIBRARIES) # redundant from debug out above
set(SPEC_LINK_LIB ${SPEC_LINK_LIB} PARENT_SCOPE)
endif()
add_executable(xspec ${XSPEC_OUT_FILE})
target_link_libraries(xspec PUBLIC spec)
set_target_properties(xspec PROPERTIES POSITION_INDEPENDENT_CODE ON)
install(TARGETS xspec spec)