Skip to content

Commit

Permalink
build: Add CMake config to support building shared libraries (#1559)
Browse files Browse the repository at this point in the history
  • Loading branch information
srherbener authored Jul 29, 2024
1 parent bc62069 commit 361352e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
11 changes: 6 additions & 5 deletions CMAKE_INSTRUCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ make install
```

### User configurable options:
By default, FMS is built without `OpenMP` and in `single precision (r4)`
By default, FMS is built without `OpenMP`, in `single precision (r4)` and delivered in static library files.

The 64BIT and 32BIT precision options will build distinct libraries when enabled with the given default
real size, libfms_r4 or libfms_r8.

The following build options are available:
```
-DOPENMP "Build FMS with OpenMP support" DEFAULT: OFF
-D32BIT "Build 32-bit (r4) FMS library" DEFAULT: ON
-D64BIT "Build 64-bit (r8) FMS library" DEFAULT: OFF
-DFPIC "Build with position independent code" DEFAULT: OFF
-DOPENMP "Build FMS with OpenMP support" DEFAULT: OFF
-D32BIT "Build 32-bit (r4) FMS library" DEFAULT: ON
-D64BIT "Build 64-bit (r8) FMS library" DEFAULT: OFF
-DFPIC "Build with position independent code" DEFAULT: OFF
-DSHARED_LIBS "Build shared/dynamic libraries" DEFAULT: OFF
-DCONSTANTS "Build with <X> constants parameter definitions" DEFAULT:GFDL OPTIONS:GFS|GEOS|GFDL
-DINTERNAL_FILE_NML "Enable compiler definition -DINTERNAL_FILE_NML" DEFAULT: ON
Expand Down
23 changes: 16 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ endif()
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# Build options
option(OPENMP "Build FMS with OpenMP support" OFF)
option(32BIT "Build 32-bit (r4) FMS library" ON)
option(64BIT "Build 64-bit (r8) FMS library" OFF)
option(FPIC "Build with position independent code" OFF)
option(OPENMP "Build FMS with OpenMP support" OFF)
option(32BIT "Build 32-bit (r4) FMS library" ON)
option(64BIT "Build 64-bit (r8) FMS library" OFF)
option(FPIC "Build with position independent code" OFF)
option(SHARED_LIBS "Build shared/dynamic libraries" OFF)

# Options for compiler definitions
option(INTERNAL_FILE_NML "Enable compiler definition -DINTERNAL_FILE_NML" ON)
Expand Down Expand Up @@ -360,8 +361,15 @@ foreach(kind ${kinds})
endif()

# FMS (C + Fortran)
add_library(${libTgt} STATIC $<TARGET_OBJECTS:${libTgt}_c>
$<TARGET_OBJECTS:${libTgt}_f>)
if (SHARED_LIBS)
message(STATUS "Shared library target: ${libTgt}")
add_library(${libTgt} SHARED $<TARGET_OBJECTS:${libTgt}_c>
$<TARGET_OBJECTS:${libTgt}_f>)
else ()
message(STATUS "Static library target: ${libTgt}")
add_library(${libTgt} STATIC $<TARGET_OBJECTS:${libTgt}_c>
$<TARGET_OBJECTS:${libTgt}_f>)
endif ()

target_include_directories(${libTgt} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down Expand Up @@ -397,7 +405,8 @@ foreach(kind ${kinds})
target_compile_definitions(${libTgt} PRIVATE "${fms_defs}")
target_compile_definitions(${libTgt} PRIVATE "${${kind}_defs}")

target_link_libraries(${libTgt} PUBLIC NetCDF::NetCDF_Fortran
target_link_libraries(${libTgt} PUBLIC NetCDF::NetCDF_C
NetCDF::NetCDF_Fortran
MPI::MPI_Fortran)

if(OpenMP_Fortran_FOUND)
Expand Down

0 comments on commit 361352e

Please sign in to comment.