From 9a4bc48dbda4cb4611a1fdb719450a41d501692c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Aradi?= Date: Sun, 19 May 2024 18:12:56 +0200 Subject: [PATCH] Fix static build with CMake (#14) --- CMakeLists.txt | 5 ++-- cmake/FortunoHelpers.cmake | 50 ++++++++++++++++++++++++++++---------- meson.build | 4 +-- meson.options | 2 +- src/CMakeLists.txt | 2 +- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40402f4..f0e59cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,13 +53,13 @@ set( ) set( - FORTUNO_THREAD_SAFE_FLAGS "" CACHE STRING + FORTUNO_THREAD_SAFE_COMPILE_FLAGS "" CACHE STRING "Fortuno: Flags needed to enforce thread-safe build during compilation" ) set( FORTUNO_THREAD_SAFE_LINK_FLAGS "" CACHE STRING - "Fortuno: Flags neeeded to enforce thread-safe build durink linkage" + "Fortuno: Flags neeeded to enforce thread-safe build during linking" ) #[=================================================================================================[ @@ -76,7 +76,6 @@ endif () set(BUILD_SHARED_LIBS ${FORTUNO_BUILD_SHARED_LIBS}) fortuno_setup_build_type("RelWithDebInfo") -fortuno_def_thread_safe_build_target() #[=================================================================================================[ # Main definition # diff --git a/cmake/FortunoHelpers.cmake b/cmake/FortunoHelpers.cmake index f8e18c8..7d75fba 100644 --- a/cmake/FortunoHelpers.cmake +++ b/cmake/FortunoHelpers.cmake @@ -25,22 +25,46 @@ endfunction() # Defines the ThreadSafeBuild target for the thread-safe parts -function (fortuno_def_thread_safe_build_target) +function (fortuno_create_thread_safe_build_target) - if (FORTUNO_THREAD_SAFE_FLAGS) - set(_compiler_flags "${FORTUNO_THREAD_SAFE_FLAGS}") - elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG") - set(_compiler_flags "-thread_safe") - endif () + if (NOT TARGET ThreadSafeBuild) + if (FORTUNO_THREAD_SAFE_COMPILE_FLAGS) + set(_compiler_flags "${FORTUNO_THREAD_SAFE_COMPILE_FLAGS}") + elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG") + set(_compiler_flags "-thread_safe") + endif () + + if (FORTUNO_THREAD_SAFE_LINK_FLAGS) + set(_linker_flags "${FORTUNO_THREAD_SAFE_LINK_FLAGS}") + elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG") + set(_linker_flags "-thread_safe") + endif () - if (FORTUNO_THREAD_SAFE_LINK_FLAGS) - set(_linker_flags "${FORTUNO_THREAD_SAFE_LINK_FLAGS}") - elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG") - set(_linker_flags "-thread_safe") + add_library(ThreadSafeBuild INTERFACE) + target_compile_options(ThreadSafeBuild INTERFACE ${_compiler_flags}) + target_link_options(ThreadSafeBuild INTERFACE ${_linker_flags}) endif () - add_library(ThreadSafeBuild INTERFACE) - target_compile_options(ThreadSafeBuild INTERFACE ${_compiler_flags}) - target_link_options(ThreadSafeBuild INTERFACE ${_linker_flags}) +endfunction () + + +# Applies thread safe build flags to a target +function (fortuno_add_thread_safe_build_flags target) + + fortuno_create_thread_safe_build_target() + + # TODO: Delete first branch once cmake minimum version is 3.26 or above + if (CMAKE_VERSION VERSION_LESS 3.26) + get_target_property(_compile_flags ThreadSafeBuild INTERFACE_COMPILE_OPTIONS) + if (_compile_flags) + target_compile_options(${target} PRIVATE ${_compile_flags}) + endif () + get_target_property(_link_flags ThreadSafeBuild INTERFACE_LINK_OPTIONS) + if (_link_flags) + target_link_options(${target} PRIVATE ${_link_flags}) + endif () + else () + target_link_libraries(${target} PRIVATE $) + endif () endfunction () \ No newline at end of file diff --git a/meson.build b/meson.build index e5792e7..9e9eca3 100644 --- a/meson.build +++ b/meson.build @@ -15,14 +15,14 @@ fortuno_sources = [] fortuno_serial_sources = [] subdir('src') -thread_safe_flags = get_option('thread_safe_flags') +thread_safe_compile_flags = get_option('thread_safe_compile_flags') thread_safe_link_flags = get_option('thread_safe_link_flags') fortuno_lib = library( 'fortuno', version: meson.project_version(), sources: fortuno_sources, - fortran_args: thread_safe_flags, + fortran_args: thread_safe_compile_flags, link_args: thread_safe_link_flags, ) diff --git a/meson.options b/meson.options index 65204b7..a34812c 100644 --- a/meson.options +++ b/meson.options @@ -7,7 +7,7 @@ option('build_serial_interface', type: 'boolean', value: true, description: 'Bui option('build_examples', type: 'boolean', value: false, description: 'Build examples') option( - 'thread_safe_flags', type: 'array', value: [], + 'thread_safe_compile_flags', type: 'array', value: [], description: 'Fortran compiler arguments to use when compiling thread-safe sources' ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 692f048..387dc8b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,7 @@ set_target_properties( OUTPUT_NAME fortuno Fortran_MODULE_DIRECTORY "${moduledir}" ) -target_link_libraries(fortuno PRIVATE ThreadSafeBuild) +fortuno_add_thread_safe_build_flags(fortuno) target_include_directories( fortuno PUBLIC $