Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
423 lines (373 loc) · 16.2 KB
/
Copy pathCMakeLists.txt
File metadata and controls
423 lines (373 loc) · 16.2 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
cmake_minimum_required(VERSION 3.17.5...3.27.7)
# define build environments
set(PROJECT_NAME "HeatFlow")
set(PROJECT_LIBRARY_NAME "HeatFlow_library")
set( CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local/${PROJECT_NAME}"
CACHE STRING "Select where to install the program." )
set(CMAKE_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}
CACHE STRING "Select where to build the program." )
set(MODULE_DIR ${CMAKE_BUILD_DIR}/mod)
# Set compiler (auto-detected, MPI wrapper preferred; override with
# -DCMAKE_Fortran_COMPILER=...). Walk PATH in order and smoke-test every
# candidate so a broken wrapper (e.g. an incomplete conda MPI install) is
# skipped in favour of a later working wrapper or plain compiler.
file(TO_CMAKE_PATH "$ENV{PATH}" _hf_search_path)
if(WIN32)
set(_hf_path_sep ";")
else()
set(_hf_path_sep ":")
endif()
string(REPLACE "${_hf_path_sep}" ";" _hf_path_dirs "${_hf_search_path}")
list(APPEND _hf_path_dirs /opt/homebrew/bin /usr/local/bin /opt/local/bin /usr/bin)
if(NOT DEFINED CMAKE_Fortran_COMPILER)
foreach(_hf_fc_candidate mpifort mpif90 mpiifort mpiifx mpifc gfortran ifx ifort nagfor)
foreach(_hf_dir IN LISTS _hf_path_dirs)
if(_hf_dir STREQUAL "")
continue()
endif()
if(EXISTS "${_hf_dir}/${_hf_fc_candidate}"
AND NOT IS_DIRECTORY "${_hf_dir}/${_hf_fc_candidate}")
execute_process(COMMAND "${_hf_dir}/${_hf_fc_candidate}" --version
RESULT_VARIABLE _hf_fc_result OUTPUT_QUIET ERROR_QUIET)
if(NOT "${_hf_fc_result}" STREQUAL "0")
execute_process(COMMAND "${_hf_dir}/${_hf_fc_candidate}" -v
RESULT_VARIABLE _hf_fc_result OUTPUT_QUIET ERROR_QUIET)
endif()
if("${_hf_fc_result}" STREQUAL "0")
set(CMAKE_Fortran_COMPILER "${_hf_dir}/${_hf_fc_candidate}"
CACHE STRING "Select Fortran compiler." )
break()
endif()
endif()
endforeach()
if(CMAKE_Fortran_COMPILER)
break()
endif()
endforeach()
endif()
if(NOT DEFINED CMAKE_C_COMPILER)
foreach(_hf_cc_candidate mpicc mpiicc mpiicx gcc clang icx icc)
foreach(_hf_dir IN LISTS _hf_path_dirs)
if(_hf_dir STREQUAL "")
continue()
endif()
if(EXISTS "${_hf_dir}/${_hf_cc_candidate}"
AND NOT IS_DIRECTORY "${_hf_dir}/${_hf_cc_candidate}")
execute_process(COMMAND "${_hf_dir}/${_hf_cc_candidate}" --version
RESULT_VARIABLE _hf_cc_result OUTPUT_QUIET ERROR_QUIET)
if("${_hf_cc_result}" STREQUAL "0")
set(CMAKE_C_COMPILER "${_hf_dir}/${_hf_cc_candidate}"
CACHE STRING "Select C compiler." )
break()
endif()
endif()
endforeach()
if(CMAKE_C_COMPILER)
break()
endif()
endforeach()
endif()
set(CMAKE_Fortran_STANDARD 2018)
# set the project name
project(HeatFlow LANGUAGES C Fortran)
# Make installed binaries find non-system libraries (MKL/OpenBLAS/PETSc/HDF5).
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# set options for building tests and examples
option(BUILD_TESTS "Build the unit tests" On)
option(HEATFLOW_PETSC "Build with PETSc solver support (auto-detected; legacy-solver-only build if not found)" On)
option(HEATFLOW_HDF5 "Build with HDF5 output support" Off)
option(HEATFLOW_BLAS "Link BLAS/LAPACK when available (optional; the current sources do not call them directly)" On)
set(HEATFLOW_BLAS_BACKEND "auto" CACHE STRING "BLAS/LAPACK backend: auto, accelerate, openblas, mkl, reference, download or none")
set_property(CACHE HEATFLOW_BLAS_BACKEND PROPERTY STRINGS auto accelerate openblas mkl reference download none)
set(HEATFLOW_BLAS_LIBRARIES "" CACHE STRING "Explicit BLAS/LAPACK linker flags/libraries (overrides detection)")
option(HEATFLOW_BLAS_DOWNLOAD "Download and build OpenBLAS locally (opt-in; requires make and network)" Off)
if (HEATFLOW_HDF5)
# Prefer pkg-config (Debian/Ubuntu hdf5-openmpi), fall back to CMake's FindHDF5
find_package(PkgConfig QUIET)
if (PkgConfig_FOUND)
pkg_search_module(HDF5_PC QUIET hdf5-openmpi hdf5-mpi hdf5)
endif()
if (HDF5_PC_FOUND)
set(HDF5_Fortran_INCLUDE_DIRS "${HDF5_PC_INCLUDE_DIRS}")
find_library(HDF5_FORTRAN_LIB NAMES hdf5_fortran HINTS ${HDF5_PC_LIBRARY_DIRS})
if (NOT HDF5_FORTRAN_LIB)
message(FATAL_ERROR "HDF5 Fortran library (libhdf5_fortran) not found")
endif()
set(HDF5_Fortran_LIBRARIES "${HDF5_FORTRAN_LIB};${HDF5_PC_LIBRARIES}")
else()
find_package(HDF5 REQUIRED COMPONENTS Fortran)
endif()
endif()
# MPI is required (mod_inputs/mod_setup use mpi)
find_package(MPI REQUIRED)
# PETSc is optional: without it the code builds a legacy-solver-only version
if (HEATFLOW_PETSC)
find_package(PkgConfig QUIET)
if (PkgConfig_FOUND)
pkg_search_module(PETSC QUIET petsc PETSc)
endif()
# Fall back to PETSC_DIR/PETSC_ARCH (e.g. bespoke or module-built PETSc)
if (NOT PETSC_FOUND)
if (NOT DEFINED PETSC_DIR)
set(PETSC_DIR $ENV{PETSC_DIR})
endif()
if (NOT DEFINED PETSC_ARCH)
set(PETSC_ARCH $ENV{PETSC_ARCH})
endif()
if (PETSC_DIR)
set(PETSC_INCLUDE_DIRS "${PETSC_DIR}/include")
set(HF_PETSC_LIB_HINTS "${PETSC_DIR}/lib")
if (PETSC_ARCH)
list(APPEND PETSC_INCLUDE_DIRS "${PETSC_DIR}/${PETSC_ARCH}/include")
list(APPEND HF_PETSC_LIB_HINTS "${PETSC_DIR}/${PETSC_ARCH}/lib")
endif()
find_library(PETSC_LIBRARY NAMES petsc HINTS ${HF_PETSC_LIB_HINTS})
if (PETSC_LIBRARY)
set(PETSC_FOUND TRUE)
set(PETSC_LIBRARIES "${PETSC_LIBRARY}")
endif()
endif()
endif()
endif()
# BLAS/LAPACK (optional; needed for static PETSc and future BLAS calls)
if (HEATFLOW_BLAS_BACKEND STREQUAL "download")
set(HEATFLOW_BLAS_DOWNLOAD ON)
set(HEATFLOW_BLAS_BACKEND "openblas")
endif()
if (HEATFLOW_BLAS AND HEATFLOW_BLAS_DOWNLOAD AND NOT HEATFLOW_BLAS_LIBRARIES)
if (NOT (HEATFLOW_BLAS_BACKEND STREQUAL "auto" OR HEATFLOW_BLAS_BACKEND STREQUAL "openblas"))
message(FATAL_ERROR
"HEATFLOW_BLAS_DOWNLOAD=ON requires HEATFLOW_BLAS_BACKEND=auto or openblas")
endif()
find_program(HF_MAKE_EXECUTABLE NAMES gmake make)
if (NOT HF_MAKE_EXECUTABLE)
message(FATAL_ERROR
"HEATFLOW_BLAS_DOWNLOAD=ON requires GNU make; install make or set "
"HEATFLOW_BLAS_LIBRARIES explicitly.")
endif()
message(STATUS "BLAS/LAPACK: building OpenBLAS via 'make deps-blas' (opt-in)")
execute_process(
COMMAND ${HF_MAKE_EXECUTABLE} -C ${CMAKE_SOURCE_DIR} deps-blas
RESULT_VARIABLE HF_OPENBLAS_RESULT
OUTPUT_VARIABLE HF_OPENBLAS_OUTPUT
ERROR_VARIABLE HF_OPENBLAS_ERROR
)
if (NOT HF_OPENBLAS_RESULT EQUAL 0)
message(FATAL_ERROR "OpenBLAS build failed:\n${HF_OPENBLAS_OUTPUT}\n${HF_OPENBLAS_ERROR}")
endif()
execute_process(
COMMAND ${HF_MAKE_EXECUTABLE} -s -C ${CMAKE_SOURCE_DIR} print-openblas-prefix
RESULT_VARIABLE HF_OPENBLAS_PREFIX_RESULT
OUTPUT_VARIABLE HF_OPENBLAS_PREFIX
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT HF_OPENBLAS_PREFIX_RESULT EQUAL 0 OR NOT HF_OPENBLAS_PREFIX)
message(FATAL_ERROR "Could not determine the local OpenBLAS installation prefix")
endif()
set(HEATFLOW_BLAS_BACKEND "openblas")
endif()
if (HEATFLOW_BLAS)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/HeatFlowBLAS.cmake)
message(STATUS "BLAS/LAPACK: ${HEATFLOW_BLAS_NOTE}")
else()
set(HEATFLOW_BLAS_FOUND FALSE)
set(HEATFLOW_BLAS_NOTE "disabled")
message(STATUS "BLAS/LAPACK: disabled")
endif()
# set coverage compiler flags
if (CMAKE_BUILD_TYPE MATCHES "Coverage")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
include(CodeCoverage)
setup_target_for_coverage_gcovr_html(
NAME coverage
EXECUTABLE ctest
EXCLUDE "${CMAKE_SOURCE_DIR}/test/*")
endif()
endif()
# enable testing
enable_testing()
set( PROJECT_DESCRIPTION
"Fortran heat flow" )
set( PROJECT_URL "https://github.com/ExeQuantCode/HeatFlow" )
set( CMAKE_CONFIGURATION_TYPES "Release" "Parallel" "Serial" "Dev" "Debug" "Coverage" "Bigmem"
CACHE STRING "List of configurations types." )
set( CMAKE_BUILD_TYPE "Release"
CACHE STRING "Select which configuration to build." )
# Define the sources
set(SRC_DIR src)
set(LIB_DIR ${SRC_DIR}/heatflow)
set(LIB_FILES
mod_constants.f90
mod_constructions.f90
mod_SPtype.f90
mod_global.f90
mod_Sparse.f90
mod_inputs.f90
mod_output_hdf5.f90
mod_material.f90
mod_hmatrix.f90
mod_init_evolve.f90
mod_petsc_solver.f90
mod_setup.f90
mod_boundary.f90
mod_heating.f90
mod_cattaneo.f90
mod_tempdep.f90
mod_evolve.f90
mod_output.f90
)
set(SRC_FILES
heatflow.f90
)
foreach(lib ${LIB_FILES})
list(APPEND PREPENDED_LIB_FILES ${CMAKE_CURRENT_LIST_DIR}/${LIB_DIR}/${lib})
endforeach()
foreach(src ${SRC_FILES})
list(APPEND PREPENDED_SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/${SRC_DIR}/${src})
endforeach()
# initialise flags
set(CPPFLAGS "")
set(CFLAGS "")
set(MODULEFLAGS "")
set(MPFLAGS "")
set(WARNFLAGS "")
set(DEVFLAGS "")
set(DEBUGFLAGS "")
set(MEMFLAGS "")
set(OPTIMFLAGS "")
set(FASTFLAGS "")
# set flags based on compiler ID (robust across wrappers and platforms)
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
message(STATUS "Using GNU compiler: ${CMAKE_Fortran_COMPILER}")
set(PPFLAGS -cpp)
set(MPFLAGS -fopenmp)
set(WARNFLAGS -Wall)
set(DEVFLAGS -g -fbacktrace -fcheck=all -fbounds-check -Og)
set(DEBUGFLAGS -fbounds-check)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3 -march=native)
set(FASTFLAGS -Ofast -march=native)
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG")
message(STATUS "Using nag compiler")
set(PPFLAGS -f2018 -fpp)
set(MPFLAGS -openmp)
set(WARNFLAGS -Wall)
set(DEVFLAGS -g -mtrace -C=all -colour -O0)
set(DEBUGFLAGS -C=array)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3)
set(FASTFLAGS -Ofast)
elseif (CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
message(STATUS "Using intel compiler")
set(PPFLAGS -fpp)
set(MPFLAGS -qopenmp)
set(WARNFLAGS -warn all)
set(DEVFLAGS -check all -warn)
set(DEBUGFLAGS -check all -fpe0 -warn -tracekback -debug extended)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3)
set(FASTFLAGS -Ofast)
else()
# Code for other Fortran compilers
message(STATUS "Using a different Fortran compiler: ${CMAKE_Fortran_COMPILER_ID}")
endif()
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${PPFLAGS}")
message(STATUS "library files: ${PREPENDED_LIB_FILES}")
message(STATUS "source files: ${PREPENDED_SRC_FILES}")
message(STATUS "project name: ${PROJECT_NAME}")
# create the library
add_library(${PROJECT_LIBRARY_NAME} STATIC ${PREPENDED_LIB_FILES})
set_target_properties(${PROJECT_LIBRARY_NAME} PROPERTIES Fortran_MODULE_DIRECTORY ${MODULE_DIR})
target_link_libraries(${PROJECT_LIBRARY_NAME} PUBLIC MPI::MPI_Fortran)
if (PETSC_FOUND)
message(STATUS "PETSc found: building with PETSc solver support")
target_compile_definitions(${PROJECT_LIBRARY_NAME} PUBLIC HAVE_PETSC)
target_include_directories(${PROJECT_LIBRARY_NAME} PUBLIC ${PETSC_INCLUDE_DIRS})
target_link_directories(${PROJECT_LIBRARY_NAME} PUBLIC ${PETSC_LIBRARY_DIRS})
target_link_libraries(${PROJECT_LIBRARY_NAME} PUBLIC ${PETSC_LIBRARIES})
else()
message(STATUS "PETSc not found: building legacy-solver-only version")
endif()
if (HEATFLOW_BLAS AND HEATFLOW_BLAS_FOUND)
message(STATUS "Linking BLAS/LAPACK: ${HEATFLOW_BLAS_NOTE}")
target_link_libraries(${PROJECT_LIBRARY_NAME} PUBLIC ${HEATFLOW_BLAS_LIBRARIES})
endif()
if (HEATFLOW_HDF5)
message(STATUS "Building with HDF5 output support")
target_compile_definitions(${PROJECT_LIBRARY_NAME} PUBLIC USE_HDF5)
target_include_directories(${PROJECT_LIBRARY_NAME} PUBLIC ${HDF5_Fortran_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS})
target_link_directories(${PROJECT_LIBRARY_NAME} PUBLIC ${HDF5_PC_LIBRARY_DIRS})
target_link_libraries(${PROJECT_LIBRARY_NAME} PUBLIC ${HDF5_Fortran_LIBRARIES} ${HDF5_LIBRARIES})
endif()
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Release>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Bigmem>:${MEMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Parallel>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Parallel>:${MPFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Dev>:${DEVFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Debug>:${DEBUGFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Debug>:${WARNFLAGS}>")
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Release>:${MEMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Parallel>:${MEMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Dev>:${MEMFLAGS}>")
target_compile_options(${PROJECT_LIBRARY_NAME} PUBLIC "$<$<CONFIG:Debug>:${MEMFLAGS}>")
endif()
add_executable(${PROJECT_NAME} ${PREPENDED_SRC_FILES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_LIBRARY_NAME})
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
set_target_properties(${PROJECT_NAME} PROPERTIES Fortran_MODULE_DIRECTORY ${MODULE_DIR})
# set compile options based on different build configurations
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Release>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Bigmem>:${MEMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel>:${MPFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Dev>:${DEVFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Debug>:${DEBUGFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Debug>:${WARNFLAGS}>")
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Release>:${MEMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel>:${MEMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Dev>:${MEMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Debug>:${MEMFLAGS}>")
endif()
# include the build test directory
if(BUILD_TESTS)
add_subdirectory(test)
endif()
if ( ( CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" ) AND
( CMAKE_BUILD_TYPE MATCHES "Coverage" ) )
append_coverage_compiler_flags()
endif()
if(BUILD_TESTS)
# Create a directory for test data
add_custom_command(
OUTPUT ${CMAKE_BUILD_DIR}/test/test_dir_created # Temporary marker file to indicate the directory was created
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BUILD_DIR}/test/test
COMMENT "Creating test directory"
)
# Copy test data files
add_custom_command(
OUTPUT ${CMAKE_BUILD_DIR}/test/test_data_copied # Temporary marker file to indicate the data was copied
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/test/data ${CMAKE_BUILD_DIR}/test/test/data
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/test/data ${CMAKE_BUILD_DIR}/test/test_dir_created # Ensure the source data exists and the directory was created
COMMENT "Copying test data files"
)
# Create a custom target to ensure that the copy commands are executed
add_custom_target(copy_test_data ALL
DEPENDS ${CMAKE_BUILD_DIR}/test/test_data_copied # Ensure it depends on the data copying command
)
# Add dependency to your main project target
add_dependencies(${PROJECT_NAME} copy_test_data)
# Ensure that ctest waits for copy_test_data
add_custom_command(TARGET copy_test_data POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Test data files copied successfully"
)
# Get all registered tests
get_property(all_tests GLOBAL PROPERTY TESTS)
# Loop through all tests and add the dependency
foreach(test_name IN LISTS all_tests)
add_dependencies(${test_name} copy_test_data)
endforeach()
endif()