Skip to content

Commit

Permalink
Merge pull request #80 from COSIMA/cmake_lib_install
Browse files Browse the repository at this point in the history
Add option to install components as libraries
  • Loading branch information
micaeljtoliveira authored Nov 29, 2023
2 parents 8160455 + 5ee073b commit e7ebf6c
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 4 deletions.
41 changes: 41 additions & 0 deletions CDEPS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,44 @@ target_link_libraries(OM3_cdeps_drof
target_sources(OM3_cdeps_drof PRIVATE
CDEPS/drof/rof_comp_nuopc.F90
)

### Install and Export

## Library
if(OM3_LIB_INSTALL)
# cdeps_common
set_target_properties(OM3_cdeps_common PROPERTIES
OUTPUT_NAME access-cdeps-common
EXPORT_NAME cdeps-common
)
install(TARGETS OM3_cdeps_common
EXPORT AccessOM3cdeps_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# components
foreach(LIB atm ocn ice wav rof)
set_target_properties(OM3_cdeps_d${LIB} PROPERTIES
OUTPUT_NAME access-cdeps-d${LIB}
EXPORT_NAME cdeps-d${LIB}
)
install(TARGETS OM3_cdeps_d${LIB}
EXPORT AccessOM3cdeps_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_cdeps_d${LIB} PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-cdeps-d${LIB}>")
get_target_property(d${LIB}_moddir OM3_cdeps_d${LIB} Fortran_MODULE_DIRECTORY)
install(FILES ${d${LIB}_moddir}/${LIB}_comp_nuopc.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-cdeps-d${LIB}
COMPONENT AccessOM3_Development
)
endforeach()
install(EXPORT AccessOM3cdeps_Targets
FILE AccessOM3cdepsTargets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)
endif()
29 changes: 29 additions & 0 deletions CICE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,32 @@ elseif(OM3_CICE_IO MATCHES "Binary")
CICE/cicecore/cicedynB/infrastructure/io/io_binary/ice_restart.F90
)
endif()

### Install and Export

## Library
if(OM3_LIB_INSTALL)
set_target_properties(OM3_cice PROPERTIES
OUTPUT_NAME access-cice
EXPORT_NAME cice
)
install(TARGETS OM3_cice
EXPORT AccessOM3cice_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_cice PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-cice>")
get_target_property(cice_moddir OM3_cice Fortran_MODULE_DIRECTORY)
install(FILES ${cice_moddir}/ice_comp_nuopc.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-cice
COMPONENT AccessOM3_Development
)
install(EXPORT AccessOM3cice_Targets
FILE AccessOM3ciceTargets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)

endif()
52 changes: 52 additions & 0 deletions CMEPS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,55 @@ target_sources(OM3_nuopc_cap_share PRIVATE
CMEPS/cesm/nuopc_cap_share/glc_elevclass_mod.F90
CMEPS/cesm/nuopc_cap_share/nuopc_shr_methods.F90
)

### Install and Export

if(OM3_LIB_INSTALL)
## CMEPS library
set_target_properties(OM3_cmeps PROPERTIES
OUTPUT_NAME access-cmeps
EXPORT_NAME cmeps
)
install(TARGETS OM3_cmeps
EXPORT AccessOM3cmeps_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_cmeps PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-cmeps>")
get_target_property(cmeps_moddir OM3_cmeps Fortran_MODULE_DIRECTORY)
install(FILES ${cmeps_moddir}/med.mod ${cmeps_moddir}/med_time_mod.mod ${cmeps_moddir}/med_internalstate_mod.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-cmeps
COMPONENT AccessOM3_Development
)
install(EXPORT AccessOM3cmeps_Targets
FILE AccessOM3cmepsTargets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)

## NUOPC cap share
set_target_properties(OM3_nuopc_cap_share PROPERTIES
OUTPUT_NAME access-nuopc_cap_share
EXPORT_NAME nuopc_cap_share
)
install(TARGETS OM3_nuopc_cap_share
EXPORT AccessOM3nuopc_cap_share_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_nuopc_cap_share PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-nuopc_cap_share>")
get_target_property(nuopc_cap_share_moddir OM3_nuopc_cap_share Fortran_MODULE_DIRECTORY)
install(DIRECTORY ${nuopc_cap_share_moddir}/
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-nuopc_cap_share
COMPONENT AccessOM3_Development
)
install(EXPORT AccessOM3nuopc_cap_share_Targets
FILE AccessOM3nuopc_cap_shareTargets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)
endif()
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ message(STATUS " - OM3_CICE_IO ${OM3_CICE_IO}")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(GNUInstallDirs)
if(OM3_LIB_INSTALL)
include(CMakePackageConfigHelpers)
endif()

# Include some custom cmake modules
include(FortranLib)
Expand Down Expand Up @@ -116,6 +119,12 @@ add_compile_definitions(
CESMCOUPLED
)

## Fortran modules path; currently this is simply set to be the include dir
set(CMAKE_INSTALL_MODULEDIR ${CMAKE_INSTALL_INCLUDEDIR}
CACHE STRING
"Fortran module installation path (Not a cmake native variable)"
)

#[==============================================================================[
# External packages #
#]==============================================================================]
Expand Down Expand Up @@ -217,6 +226,10 @@ endforeach()
#]==============================================================================]

## Installs

# Note that the installation of some components is done in the corresponding subdirectory

# OM3 executables
if(OM3_BIN_INSTALL)
foreach(CONF IN LISTS KnownConfigurations)
if(NOT OM3_ENABLE_${CONF})
Expand All @@ -228,3 +241,16 @@ if(OM3_BIN_INSTALL)
)
endforeach()
endif()

# Libraries
if(OM3_LIB_INSTALL)
configure_package_config_file(
cmake/AccessOM3Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/AccessOM3Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)
install(FILES ${CMAKE_SOURCE_DIR}/cmake/FindFoX.cmake ${CMAKE_CURRENT_BINARY_DIR}/AccessOM3Config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
COMPONENT AccessOM3_Development
)
endif()
28 changes: 28 additions & 0 deletions MOM6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,31 @@ target_sources(OM3_mom6 PRIVATE
MOM6/config_src/drivers/nuopc_cap/time_utils.F90
)
add_patched_source(OM3_mom6 MOM6/config_src/drivers/nuopc_cap/mom_cap.F90)

### Install and Export

## Library
if(OM3_LIB_INSTALL)
set_target_properties(OM3_mom6 PROPERTIES
OUTPUT_NAME access-mom6
EXPORT_NAME mom6
)
install(TARGETS OM3_mom6
EXPORT AccessOM3mom6_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_mom6 PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-mom6>")
get_target_property(mom_moddir OM3_mom6 Fortran_MODULE_DIRECTORY)
install(FILES ${mom_moddir}/ocn_comp_nuopc.mod ${mom_moddir}/mom_cap_mod.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-mom6
COMPONENT AccessOM3_Development
)
install(EXPORT AccessOM3mom6_Targets
FILE AccessOM3mom6Targets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)
endif()
29 changes: 28 additions & 1 deletion WW3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,35 @@ set_target_properties(OM3_ww3_ounp PROPERTIES
)
target_link_libraries(OM3_ww3_ounp PRIVATE AccessOM3::ww3)

### Install and Export

## Installs
## Library
if(OM3_LIB_INSTALL)
set_target_properties(OM3_ww3 PROPERTIES
OUTPUT_NAME access-ww3
EXPORT_NAME ww3
)
install(TARGETS OM3_ww3
EXPORT AccessOM3ww3_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_ww3 PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-ww3>")
get_target_property(ww3_moddir OM3_ww3 Fortran_MODULE_DIRECTORY)
install(FILES ${ww3_moddir}/wav_comp_nuopc.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-ww3
COMPONENT AccessOM3_Development
)
install(EXPORT AccessOM3ww3_Targets
FILE AccessOM3ww3Targets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3
)
endif()

## Utilities
if(OM3_BIN_INSTALL)
install(TARGETS OM3_ww3_grid OM3_ww3_strt OM3_ww3_outf OM3_ww3_ounf OM3_ww3_ounp
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
82 changes: 82 additions & 0 deletions cmake/AccessOM3Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
@PACKAGE_INIT@

if(NOT AccessOM3_FIND_QUIETLY)
message(STATUS "Found AccessOM3: ${PACKAGE_PREFIX_DIR}")
endif()

# Available components

# The following components are always available (order is important!)
set(_supported_components timing share nuopc_cap_share cmeps cdeps mom6 cice ww3)

# Check validity of requested components
foreach(_comp ${AccessOM3_FIND_COMPONENTS})
if (NOT _comp IN_LIST _supported_components)
set(AccessOM3_FOUND False)
set(AccessOM3_NOT_FOUND_MESSAGE "Unsupported component: ${_comp}")
endif()
endforeach()

# Some components are only available if they were built
if ((mom6 IN_LIST ${AccessOM3_FIND_COMPONENTS}) AND (NOT @OM3_BUILD_MOM6@))
set(AccessOM3_FOUND False)
set(AccessOM3_NOT_FOUND_MESSAGE "The following component is not available: mom6")
endif()
if ((cice IN_LIST ${AccessOM3_FIND_COMPONENTS}) AND (NOT @OM3_BUILD_CICE6@))
set(AccessOM3_FOUND False)
set(AccessOM3_NOT_FOUND_MESSAGE "The following component is not available: cice")
endif()
if ((ww3 IN_LIST ${AccessOM3_FIND_COMPONENTS}) AND (NOT @OM3_BUILD_WW3@))
set(AccessOM3_FOUND False)
set(AccessOM3_NOT_FOUND_MESSAGE "The following component is not available: ww3")
endif()

# Build a list of all the required components, taking into account their dependencies
set(_required_components ${AccessOM3_FIND_COMPONENTS})
if(cice IN_LIST _required_components)
list(APPEND _required_components cdeps nuopc_cap_share share timing)
endif()
if(mom6 IN_LIST _required_components)
list(APPEND _required_components nuopc_cap_share share)
endif()
if(cdeps IN_LIST _required_components)
list(APPEND _required_components cmeps share nuopc_cap_share)
endif()
if(cmeps IN_LIST _required_components)
list(APPEND _required_components nuopc_cap_share share timing)
endif()
if(nuopc_cap_share IN_LIST _required_components)
list(APPEND _required_components share timing)
endif()
if(share IN_LIST _required_components)
list(APPEND _required_components timing)
endif()
list(REMOVE_DUPLICATES _required_components)

if(NOT AccessOM3_FIND_QUIETLY)
message(STATUS " - AccessOM3 Components: ${_required_components}")
endif()

# Include required targets. We do this by looping over the _supported_components
# list because the order in which targets are loaded matters.
foreach(_comp ${_supported_components})
if (_comp IN_LIST _required_components)
include("${CMAKE_CURRENT_LIST_DIR}/AccessOM3${_comp}Targets.cmake")
endif()
endforeach()

# Find dependencies of required components
include(CMakeFindDependencyMacro)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

if (cdeps IN_LIST _required_components)
find_dependency(FoX)
endif()
if (mom6 IN_LIST _required_components)
find_dependency(fms COMPONENTS R8 REQUIRED)
endif()

list(REMOVE_ITEM CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

check_required_components(_supported_components)
4 changes: 2 additions & 2 deletions cmake/FortranLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ function(add_fortran_library LIB MOD_DIR)
get_target_property(LIB_DIR ${LIB} BINARY_DIR)
set_target_properties(${LIB} PROPERTIES Fortran_MODULE_DIRECTORY ${LIB_DIR}/${MOD_DIR})

target_include_directories(${LIB} INTERFACE ${LIB_DIR}/${MOD_DIR})
endfunction(add_fortran_library)
target_include_directories(${LIB} INTERFACE "$<BUILD_INTERFACE:${LIB_DIR}/${MOD_DIR}>")
endfunction(add_fortran_library)
Loading

0 comments on commit e7ebf6c

Please sign in to comment.