Skip to content

Commit

Permalink
GitHub Issue NOAA-EMC/GSI#112. A refactor of CMake build framework. (…
Browse files Browse the repository at this point in the history
…#327)
  • Loading branch information
aerorahul authored and MichaelLueken committed May 18, 2022
1 parent 28cbc48 commit f27aee9
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 58 deletions.
137 changes: 112 additions & 25 deletions src/ncdiag/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,113 @@
cmake_minimum_required(VERSION 3.16.1)
if(BUILD_NCDIAG)
if(BUILD_NCDIAG_SERIAL)
add_subdirectory(serial)
endif(BUILD_NCDIAG_SERIAL)
set(Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/include")

# NetCDF-4 library
include_directories( ${NETCDF_INCLUDE_DIRS} ${NCDIAG_INCS} )

# 32-bit reals, for now
add_definitions(-D_REAL4_ -DUSE_MPI)
FILE(GLOB NCDIAG_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*90 )
set_source_files_properties( ${NCDIAG_SRC} PROPERTIES COMPILE_FLAGS ${NCDIAG_Fortran_FLAGS} )
LIST(REMOVE_ITEM NCDIAG_SRC ${CMAKE_CURRENT_SOURCE_DIR}/test_nc_unlimdims.F90 )
LIST(REMOVE_ITEM NCDIAG_SRC ${CMAKE_CURRENT_SOURCE_DIR}/nc_diag_cat.F90 )
add_library(ncdiag STATIC ${NCDIAG_SRC})
add_executable(test_nc_unlimdims.x ${CMAKE_CURRENT_SOURCE_DIR}/test_nc_unlimdims.F90 )
add_executable(ncdiag_cat_mpi.x ${CMAKE_CURRENT_SOURCE_DIR}/nc_diag_cat.F90 )
target_link_libraries(ncdiag_cat_mpi.x ncdiag ${NETCDF_Fortran_LIBRARIES} ${NETCDF_C_LIBRARIES} ${MPI_Fortran_LIBRARIES})
target_link_libraries(test_nc_unlimdims.x ncdiag ${NETCDF_Fortran_LIBRARIES} ${NETCDF_C_LIBRARIES} ${MPI_Fortran_LIBRARIES})
# set_target_properties(test_nc_unlimdims.x PROPERTIES Fortran_MODULE_DIRECTORY ${NCDIAG_INCS} )
# set_target_properties(ncdiag_cat_mpi.x PROPERTIES Fortran_MODULE_DIRECTORY ${NCDIAG_INCS} ${MPI_Fortran_INCLUDE_PATH})

endif(BUILD_NCDIAG)
cmake_minimum_required(VERSION 3.19)

project(ncdiag
VERSION 1.0.0
LANGUAGES Fortran)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(${PROJECT_NAME}_compiler_flags)
include(GNUInstallDirs)

if(NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release|RelWithDebInfo|MinSizeRel)$")
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|Intel)$")
message(WARNING "${CMAKE_Fortran_COMPILER_ID} is not supported.")
endif()

option(ENABLE_NCDIAG_SERIAL "Enable Serial NetCDF diagnostics" ON)

# Dependencies
find_package(MPI REQUIRED)
find_package(NetCDF REQUIRED COMPONENTS Fortran)

# Echo user options
message(STATUS "ENABLE_NCDIAG_SERIAL ... ${ENABLE_NCDIAG_SERIAL}")

# Get list of files
include(ncdiag_files.cmake)

set(module_dir "${CMAKE_CURRENT_BINARY_DIR}/include/ncdiag")
add_library(ncdiag STATIC ${NCDIAG_SRC_lib})
add_library(${PROJECT_NAME}::ncdiag ALIAS ncdiag)
set_target_properties(ncdiag PROPERTIES Fortran_MODULE_DIRECTORY "${module_dir}")
target_compile_definitions(ncdiag PRIVATE "_REAL4_")
target_compile_definitions(ncdiag PRIVATE "USE_MPI")
target_link_libraries(ncdiag PUBLIC NetCDF::NetCDF_Fortran)
target_link_libraries(ncdiag PUBLIC MPI::MPI_Fortran)
target_include_directories(ncdiag PUBLIC $<BUILD_INTERFACE:${module_dir}>
$<INSTALL_INTERFACE:include/ncdiag>)
list(APPEND NCDIAG_Targets ncdiag)
install(DIRECTORY ${module_dir} DESTINATION ${CMAKE_INSTALL_PREFIX}/include)

# Build applications
foreach(app ${NCDIAG_SRC_app})
get_filename_component(app_name ${app} NAME_WLE)
add_executable(${app_name}.x ${app})
add_dependencies(${app_name}.x ncdiag)
target_compile_definitions(${app_name}.x PRIVATE "USE_MPI")
target_link_libraries(${app_name}.x PUBLIC ncdiag)
target_link_libraries(${app_name}.x PUBLIC NetCDF::NetCDF_Fortran)
target_link_libraries(${app_name}.x PUBLIC MPI::MPI_Fortran)
list(APPEND NCDIAG_Targets ${app_name}.x)
endforeach()

# Build serial library and applications
if(ENABLE_NCDIAG_SERIAL)
set(module_dir "${CMAKE_CURRENT_BINARY_DIR}/include/ncdiag_serial")
add_library(ncdiag_serial STATIC ${NCDIAG_SRC_lib})
add_library(${PROJECT_NAME}::ncdiag_serial ALIAS ncdiag_serial)
set_target_properties(ncdiag_serial PROPERTIES Fortran_MODULE_DIRECTORY "${module_dir}")
target_compile_definitions(ncdiag_serial PRIVATE "_REAL4_")
target_link_libraries(ncdiag_serial PUBLIC NetCDF::NetCDF_Fortran)
target_include_directories(ncdiag_serial PUBLIC $<BUILD_INTERFACE:${module_dir}>
$<INSTALL_INTERFACE:include/ncdiag_serial>)
list(APPEND NCDIAG_Targets ncdiag_serial)

set(app_name "ncdiag_cat_serial")
add_executable(${app_name}.x nc_diag_cat.F90)
add_dependencies(${app_name}.x ncdiag_serial)
target_link_libraries(${app_name}.x PUBLIC ncdiag_serial)
target_link_libraries(${app_name}.x PUBLIC NetCDF::NetCDF_Fortran)
list(APPEND NCDIAG_Targets ${app_name}.x)
install(DIRECTORY ${module_dir} DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
endif()

install(
TARGETS ${NCDIAG_Targets}
EXPORT ${PROJECT_NAME}Exports
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

# Package config
include(CMakePackageConfigHelpers)
set(CONFIG_INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

export(EXPORT ${PROJECT_NAME}Exports
NAMESPACE ${PROJECT_NAME}::
FILE ${PROJECT_NAME}-targets.cmake)

configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/PackageConfig.cmake.in ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config.cmake
INSTALL_DESTINATION ${CONFIG_INSTALL_DESTINATION})
install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config.cmake
DESTINATION ${CONFIG_INSTALL_DESTINATION})

write_basic_package_version_file(
${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)
install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
DESTINATION ${CONFIG_INSTALL_DESTINATION})

install(EXPORT ${PROJECT_NAME}Exports
NAMESPACE ${PROJECT_NAME}::
FILE ${PROJECT_NAME}-targets.cmake
DESTINATION ${CONFIG_INSTALL_DESTINATION})
Loading

0 comments on commit f27aee9

Please sign in to comment.