Skip to content

Commit

Permalink
Clean up cmake build system. Work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
cianciosa committed Feb 5, 2024
1 parent 27122bc commit 43a011c
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 128 deletions.
234 changes: 161 additions & 73 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,181 @@ cmake_minimum_required (VERSION 3.5)

project (korc_proj C CXX Fortran)

# FIXME: Remove once link issues are resolved with the new macos linker.
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
SET (CMAKE_EXE_LINKER_FLAGS -ld_classic)
endif ()

#-------------------------------------------------------------------------------
# Setup build types.
#-------------------------------------------------------------------------------
set_property (CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
Debug
Release
Sanitized
)

# Make sure the prefix path appears as a ccmake variable.
set (CMAKE_PREFIX_PATH "" CACHE PATH "Search paths for dependency look up.")

#-------------------------------------------------------------------------------
# Build Options
#-------------------------------------------------------------------------------
option (USE_ACC "Use OpenACC" OFF)
option (USE_PSPLINE "Use PSPLINE interpolation" OFF)
option (USE_FIO "Use FIO interpolation support" OFF)
#FIXME: CORI is decomissioned. Is this necessary anymore?
option (CORI_DIR "Use Cori directories" OFF)

#-------------------------------------------------------------------------------
# Sanitizer options
#-------------------------------------------------------------------------------
add_library (sanitizer INTERFACE)
target_compile_options (sanitizer
INTERFACE
$<$<CONFIG:Sanitized>:-g>
)

macro (register_sanitizer_option name default)
string (TOUPPER ${name} upper_name)

option (SANITIZE_${upper_name} "Enable the ${name} sanitizer" ${default})

target_compile_options (sanitizer
INTERFACE
$<$<CONFIG:Sanitized>:$<$<BOOL:${SANITIZE_${upper_name}}>:-fsanitize=${name}>>
)
target_link_options (sanitizer
INTERFACE
$<$<CONFIG:Sanitized>:$<$<BOOL:${SANITIZE_${upper_name}}>:-fsanitize=${name}>>
)
endmacro ()

register_sanitizer_option (address ON)
register_sanitizer_option (leak OFF)
register_sanitizer_option (memory OFF)
register_sanitizer_option (thread OFF)
register_sanitizer_option (undefined ON)
register_sanitizer_option (float-divide-by-zero ON)

#-------------------------------------------------------------------------------
# Build Locations
#-------------------------------------------------------------------------------
# FIXME: Leave this as is for now but check if we want to use the cmake install
# functions better.

# Set directories to build all binary files.
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/build/bin)

# Fortran specific settings.
set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp")
set (CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/modules)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/lib)

# Set definitions.
add_definitions (-DDOUBLE_PRECISION)
add_definitions (-DPARALLEL_RANDOM)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# add_definitions (-DDBG_CHECK)
endif()

# Configure OpenMP
#-------------------------------------------------------------------------------
# Dependencies
#-------------------------------------------------------------------------------
find_package (OpenMP)
set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_Fortran_FLAGS}")
option (USE_OMP "Use OpenOMP" OFF)
if (${USE_OMP})
add_definitions (-DOMP)
endif()

option (USE_ACC "Use OpenACC" OFF)
if (${USE_ACC})
add_definitions (-DACC)
endif()

# Configure MPI
find_package (MPI REQUIRED)
set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${MPI_Fortran_COMPILE_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_Fortran_LINK_FLAGS}")
include_directories (${MPI_Fortran_INCLUDE_PATH})

# Configure HDF5
find_package (HDF5 REQUIRED COMPONENTS Fortran)
include_directories (${HDF5_INCLUDE_DIRS})

# Configure EZspline
option (USE_PSPLINE "Use PSPLINE interpolation" OFF)
if (${USE_PSPLINE})
#include_directories (${EZSpline_INCLUDE_PATH})
#set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EZSpline_LINK_FLAGS}")
add_definitions (-DPSPLINE)
endif ()

# Configure FIO interpolation
option (USE_FIO "Use FIO interpolation support" OFF)
if (${USE_FIO})
include_directories (${FIO_INCLUDE_PATH} ${NIMROD_INCLUDE_PATH} ${NIMFIO_INCLUDE_PATH} ${NIMCORE_INCLUDE_PATH} ${NIMLIB_INCLUDE_PATH} ${NIMEXT_INCLUDE_PATH})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FIO_LINK_FLAGS} ${NIMFIO_LINK_FLAGS} ${NIMCORE_LINK_FLAGS} ${NIMLIB_LINK_FLAGS} ${NIMEXT_LINK_FLAGS}")
add_definitions (-DFIO)
endif ()

find_package (HDF5 REQUIRED COMPONENTS C Fortran)

add_library (korc_depends INTERFACE)

target_link_libraries (korc_depends

INTERFACE

sanitizer
$<$<BOOL:${OpenMP_Fortran_FOUND}>:OpenMP::OpenMP_Fortran>
$<$<BOOL:${OpenMP_CXX_FOUND}>:OpenMP::OpenMP_CXX>
$<$<BOOL:${MPI_Fortran_FOUND}>:MPI::MPI_Fortran>
$<$<BOOL:${HDF5_Found}>:hdf5::hdf5_fortran>
$<$<BOOL:${USE_FIO}>:${FIO_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${M3DC1_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMFIO_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMBLK_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMBTY_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMLOC_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMITER_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMMAT_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMPAR_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMLIB_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMMPI_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMLAP_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMSLU_LIBRARIES}>
$<$<BOOL:${USE_FIO}>:${NIMLUD_LIBRARIES}>

)
target_compile_options (korc_depends

INTERFACE

$<$<COMPILE_LANGUAGE:Fortran>:-cpp>
)
# FIXME: Consider making FIO a cmake module and using find_package(FIO) to set
# the dependencies.
target_include_directories (korc_depends

INTERFACE

$<$<BOOL:${USE_FIO}>:${FIO_INCLUDE_PATH}>
$<$<BOOL:${USE_FIO}>:${NIMROD_INCLUDE_PATH}>
$<$<BOOL:${USE_FIO}>:${NIMFIO_INCLUDE_PATH}>
$<$<BOOL:${USE_FIO}>:${NIMCORE_INCLUDE_PATH}>
$<$<BOOL:${USE_FIO}>:${NIMLIB_INCLUDE_PATH}>
$<$<BOOL:${USE_FIO}>:${NIMEXT_INCLUDE_PATH}>
)
target_compile_definitions (korc_depends

INTERFACE

DOUBLE_PRECISION
PARALLEL_RANDOM
# $<$<CONFIG:Debug>:DBG_CHECK>
$<$<BOOL:${OpenMP_Fortran_FOUND}>:OMP>
$<$<BOOL:${USE_ACC}>:ACC>
$<$<BOOL:${USE_PSPLINE}>:PSPLINE>
$<$<BOOL:${USE_FIO}>:FIO>
$<$<NOT:$<BOOL:${CORI_DIR}>>:MAC>
)
target_link_options (korc_depends

INTERFACE

$<$<BOOL:USE_FIO>:${FIO_LINK_FLAGS}>
$<$<BOOL:USE_FIO>:${NIMFIO_LINK_FLAGS}>
$<$<BOOL:USE_FIO>:${NIMCORE_LINK_FLAGS}>
$<$<BOOL:USE_FIO>:${NIMLIB_LINK_FLAGS}>
$<$<BOOL:USE_FIO>:${NIMEXT_LINK_FLAGS}>
)

#-------------------------------------------------------------------------------
# Source files.
#-------------------------------------------------------------------------------
add_subdirectory (src)

# Add machine dependent definition for finding included scripts
option (CORI_DIR "Use Cori directories" OFF)
if (${CORI_DIR})
add_definitions (-DCORI)
else ()
add_definitions (-DMAC)
#-------------------------------------------------------------------------------
# Testing
#-------------------------------------------------------------------------------
if (${KORC_TEST})
enable_testing ()

if(${KORC_TEST})
# Configure FRUIT
include_directories (${FRUIT_INCLUDE_PATH})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FRUIT_LINK_FLAGS}")
add_library (korc_test INTERFACE)

# Configure KORCSRC
include_directories (${KORCSRC_INCLUDE_PATH})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${KORCSRC_LINK_FLAGS}")
target_include_directories (korc_test

add_subdirectory (test)
INTERFACE

enable_testing()
${FRUIT_INCLUDE_PATH}
${KORCSRC_INCLUDE_PATH}
)
target_link_options (korc_depends

add_test (NAME unit_testing
COMMAND xtest TEST_OUT
WORKING_DIRECTORY build/bin)
INTERFACE

add_test (NAME regression_test_1
COMMAND korc_regtest1.sh
WORKING_DIRECTORY test/reg_tests)
${FRUIT_LINK_FLAGS}
${KORCSRC_LINK_FLAGS}
)

endif ()
add_subdirectory (test)

add_test (NAME unit_testing COMMAND xtest TEST_OUT WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/bin)
add_test (NAME regression_test_1 COMMAND korc_regtest1.sh WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/reg_tests)
endif ()


94 changes: 45 additions & 49 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
# Append all source files to variable korc_sources. As new files are added,
# this must be updated.
set (korc_sources "")
list (APPEND korc_sources
korc_collisions.f90
korc_fields.f90
korc_interp.f90
korc_spatial_distribution.f90
korc_constants.f90
korc_finalize.f90
korc_ppusher.f90
korc_HDF5.f90
korc_coords.f90
korc_hammersley_generator.f90
korc_profiles.f90
korc_types.f90
korc_avalanche.f90
korc_hpc.f90
korc_rnd_numbers.f90
korc_units.f90
korc_experimental_pdf.f90
korc_initialize.f90
korc_velocity_distribution.f90
korc_c_random.cpp
korc_random.f90
korc_fio_interface.f90
korc_input.f90
korc_pspline.f90
main.f90
add_library(korc STATIC)

target_sources(korc

PRIVATE

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_collisions.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_fields.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_interp.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_spatial_distribution.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_constants.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_finalize.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_ppusher.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_HDF5.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_coords.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_hammersley_generator.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_profiles.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_types.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_avalanche.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_hpc.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_rnd_numbers.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_units.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_experimental_pdf.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_initialize.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_velocity_distribution.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_c_random.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_random.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_fio_interface.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_input.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_pspline.f90>
)
target_link_libraries (korc

add_executable (xkorc ${korc_sources})
set_property(TARGET xkorc PROPERTY LINKER_LANGUAGE Fortran)
PUBLIC

option (CORI_DIR "Use Cori directories" OFF)
if (${CORI_DIR})
add_definitions (-DCORI)
else ()
add_definitions (-DMAC)
if (${KORC_TEST})
add_library (korcsrc STATIC ${korc_sources})
endif ()
endif ()
korc_depends
)

add_executable (xkorc)
target_sources (xkorc

target_link_libraries (xkorc ${MPI_Fortran_LIBRARIES})
target_link_libraries (xkorc ${HDF5_LIBRARIES})
PRIVATE

if (${USE_PSPLINE})
#target_link_libraries (xkorc ${EZSpline_LIBRARIES})
endif()
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/main.f90>
)
target_link_libraries (xkorc

PUBLIC

if(${USE_FIO})
target_link_libraries (xkorc ${FIO_LIBRARIES} ${M3DC1_LIBRARIES})
target_link_libraries (xkorc ${NIMFIO_LIBRARIES} ${NIMBLK_LIBRARIES} ${NIMBTY_LIBRARIES} ${NIMLOC_LIBRARIES} ${NIMITER_LIBRARIES} ${NIMMAT_LIBRARIES} ${NIMPAR_LIBRARIES} ${NIMLIB_LIBRARIES} ${NIMMPI_LIBRARIES} ${NIMLAP_LIBRARIES} ${NIMSLU_LIBRARIES} ${NIMLUD_LIBRARIES})
endif()
korc
)
set_property (TARGET xkorc PROPERTY LINKER_LANGUAGE Fortran)
21 changes: 15 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ list (APPEND korc_tests
test_hpc.f90
)

add_executable (xtest ${korc_tests})
set_property(TARGET xtest PROPERTY LINKER_LANGUAGE Fortran)
add_executable (xtest)
target_sources(v

PRIVATE

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/korc_test.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/test_io.f90>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/test_hpc.f90>
)
target_link_libraries (xtest

target_link_libraries (xtest ${FRUIT_LIBRARIES})
target_link_libraries (xtest ${MPI_Fortran_LIBRARIES})
target_link_libraries (xtest ${HDF5_LIBRARIES})
target_link_libraries (xtest ${KORCSRC_LIBRARIES})
PUBLIC

korc
)
set_property(TARGET xtest PROPERTY LINKER_LANGUAGE Fortran)

0 comments on commit 43a011c

Please sign in to comment.