Skip to content

Commit

Permalink
Fix static build with CMake (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi authored May 19, 2024
1 parent 629f774 commit 9a4bc48
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

#[=================================================================================================[
Expand All @@ -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 #
Expand Down
50 changes: 37 additions & 13 deletions cmake/FortunoHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<BUILD_LOCAL_INTERFACE:ThreadSafeBuild>)
endif ()

endfunction ()
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
2 changes: 1 addition & 1 deletion meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${moduledir}>
Expand Down

0 comments on commit 9a4bc48

Please sign in to comment.