Skip to content

Commit 7d27c90

Browse files
CMakeLists.txt: support building shared/dynamic libraries
* Use find_package for FMS, otherwise shared lib build fails. Co-authored-by: dougiesquire <[email protected]>
1 parent 5a88bb5 commit 7d27c90

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ endif()
2626

2727
cmake_path(APPEND CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR} "include")
2828

29+
option(BUILD_SHARED_LIBS "Build shared/dynamic libraries" OFF)
30+
2931
################################################################################
3032
# Fortran
3133
################################################################################
@@ -61,11 +63,11 @@ endif()
6163
################################################################################
6264

6365
find_package(MPI REQUIRED COMPONENTS Fortran)
66+
find_package(FMS REQUIRED COMPONENTS R8)
6467
find_package(PkgConfig REQUIRED)
65-
pkg_check_modules(FMS REQUIRED IMPORTED_TARGET "FMS")
6668
pkg_check_modules(MOCSY REQUIRED IMPORTED_TARGET "mocsy")
6769

68-
add_library(gtracers STATIC)
70+
add_library(gtracers)
6971

7072
target_sources(gtracers PRIVATE
7173
generic_tracers/FMS_coupler_util.F90
@@ -87,8 +89,19 @@ target_sources(gtracers PRIVATE
8789
generic_tracers/generic_WOMBATmid.F90
8890
)
8991

92+
set_target_properties(gtracers PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS})
93+
94+
# "Creating CMake Libraries - That others can find and use."
95+
# https://www.youtube.com/watch?v=08f5Dav72aE
96+
target_include_directories(gtracers
97+
PUBLIC
98+
# Generic Tracers has no header files but creates modules
99+
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/generic_tracers>"
100+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
101+
)
102+
90103
target_link_libraries(gtracers PUBLIC
91-
PkgConfig::FMS
104+
FMS::fms_r8
92105
PkgConfig::MOCSY)
93106

94107
install(TARGETS gtracers

0 commit comments

Comments
 (0)