From e5996139045e27bc89e49c3c6bcc4bbaabc60005 Mon Sep 17 00:00:00 2001 From: Harshula Jayasuriya Date: Tue, 6 Aug 2024 10:30:12 +1000 Subject: [PATCH] CMakeLists.txt: modify to work with ACCESS-NRI's FMS fork --- CMakeLists.txt | 232 +++++++++++++++++++------------------------------ 1 file changed, 89 insertions(+), 143 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb2064270..ceeec46c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,21 +19,26 @@ # Copyright (c) GFDL, @underwoo -cmake_minimum_required(VERSION 3.12 FATAL_ERROR) +# https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html +# 3.11: add_library() doesn't require dummy source +# 3.12: target_link_libraries() supports OBJECT targets +# 3.20: cmake_path() +cmake_minimum_required(VERSION 3.20) # Define the CMake project project(FMS - VERSION 2024.02.0 - DESCRIPTION "GFDL FMS Library" - HOMEPAGE_URL "https://www.gfdl.noaa.gov/fms" + VERSION 2020.03.1 + # Forked in 2020 at: https://github.com/ACCESS-NRI/FMS/commit/e8940fe90d68c3dc7c0d6bf1b8f552a577251754 + DESCRIPTION "ACCESS-NRI fork of GFDL FMS Library" + HOMEPAGE_URL "https://github.com/ACCESS-NRI/FMS/" LANGUAGES C Fortran) include(GNUInstallDirs) if(NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release|RelWithDebInfo|MinSizeRel)$") - message(STATUS "Setting build type to 'Release' as none was specified.") + message(STATUS "Setting build type to 'Relwithdebinfo' as none was specified.") set(CMAKE_BUILD_TYPE - "Release" + "Relwithdebinfo" CACHE STRING "Choose the type of build." FORCE) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() @@ -55,20 +60,22 @@ endif() if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU") # Precision-based Fortran compiler flags set(r8_flags "-fdefault-real-8 -fdefault-double-8") # Fortran flags for 64BIT precision - set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fcray-pointer -fconvert=big-endian -ffree-line-length-none -fno-range-check -fbacktrace") - set(CMAKE_Fortran_FLAGS_RELEASE "-O3 -funroll-all-loops -finline-functions") - set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -fcheck=bounds -ffpe-trap=invalid,zero,overflow,underflow" ) - set(CMAKE_Fortran_LINK_FLAGS "" ) + + set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-pie -fcray-pointer -fdefault-real-8 -ffree-line-length-none -fno-range-check") + set(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -O2") + set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -g") + set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -O0 -g -Wuninitialized -fcheck=bounds -Werror -ffpe-trap=invalid,zero,overflow") elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel") # Precision-based Fortran compiler flags set(r4_flags "-real-size 32") # Fortran flags for 32BIT precision set(r8_flags "-real-size 64") # Fortran flags for 64BIT precision set(r8_flags "${r8_flags} -no-prec-div -no-prec-sqrt") - set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fpp -fno-alias -auto -safe-cray-ptr -ftz -assume byterecl -align array64byte -nowarn -sox -traceback") - set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -debug minimal -fp-model source -nowarn -qoverride-limits -qno-opt-dynamic-align -qopt-prefetch=3") - set(CMAKE_Fortran_FLAGS_DEBUG "-g -O0 -check -check noarg_temp_created -check nopointer -warn -warn noerrors -fpe0 -ftrapuv") - set(CMAKE_Fortran_LINK_FLAGS "") + + set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fno-alias -stack-temps -safe-cray-ptr -ftz -assume byterecl -i4 -r8 -nowarn -sox -traceback") + set(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -O2 -fp-model source") + set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -g") + set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -g -O0 -check -check noarg_temp_created -check nopointer -warn -warn noerrors -fpe0 -ftrapuv") elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "IntelLLVM") message(FATAL_ERROR "Fortran compiler ${CMAKE_Fortran_COMPILER_ID} is unsupported") @@ -84,16 +91,14 @@ endif() if(CMAKE_C_COMPILER_ID MATCHES "GNU") # GNU C - set(CMAKE_C_FLAGS_RELEASE "-O3") - set(CMAKE_C_FLAGS_DEBUG "-O0 -g") - set(CMAKE_C_LINK_FLAGS "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -frecord-gcc-switches") + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Werror -Wuninitialized -Wno-stringop-overflow") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel") - # Intel C - set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -sox -traceback") - set( CMAKE_C_FLAGS_RELEASE "-qno-opt-dynamic-align -O2 -debug minimal -fp-model source") - set( CMAKE_C_FLAGS_DEBUG "-O0 -g -ftrapuv") - set( CMAKE_C_LINK_FLAGS "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -sox -traceback") + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -debug") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -debug minimal") elseif(CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM") message(FATAL_ERROR "C compiler ${CMAKE_C_COMPILER_ID} is unsupported") @@ -109,17 +114,28 @@ endif() # 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) +# TODO: Should this be removed? +option(32BIT "Build 32-bit (r4) FMS library" OFF) + +# TODO: Should this be removed? +# ACCESS-NRI's MOM5 uses -r8: +# $ grep FFLAGS bin/mkmf.template.nci | grep r8 +# FFLAGS := -fno-alias -safe-cray-ptr -fpe0 -ftz -assume byterecl -i4 -r8 -traceback -nowarn -check noarg_temp_created -assume nobuffered_io -convert big_endian -grecord-gcc-switches -align all +option(64BIT "Build 64-bit (r8) FMS library" ON) + +# TODO: Remove: 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) +# TODO: Remove: option(ENABLE_QUAD_PRECISION "Enable compiler definition -DENABLE_QUAD_PRECISION" ON) option(GFS_PHYS "Enable compiler definition -DGFS_PHYS" OFF) option(LARGEFILE "Enable compiler definition -Duse_LARGEFILE" OFF) +# TODO: Remove: option(WITH_YAML "Enable compiler definition -Duse_yaml" OFF) +# TODO: Remove: option(USE_DEPRECATED_IO "Enable compiler definition -Duse_deprecated_io (compile with fms_io/mpp_io)" OFF) if(32BIT) @@ -136,7 +152,9 @@ endif() # Find dependencies find_package(MPI REQUIRED COMPONENTS C Fortran) -find_package(NetCDF REQUIRED COMPONENTS C Fortran) +find_package(PkgConfig REQUIRED) +pkg_check_modules(NETCDF_C REQUIRED IMPORTED_TARGET "netcdf") +pkg_check_modules(NETCDF_F REQUIRED IMPORTED_TARGET "netcdf-fortran") # Check for the OpenMP library and set the required compile flags if (OPENMP) @@ -155,97 +173,62 @@ endif () # Collect FMS Fortran source files list(APPEND fms_fortran_src_files - affinity/fms_affinity.F90 amip_interp/amip_interp.F90 astronomy/astronomy.F90 axis_utils/axis_utils.F90 - axis_utils/axis_utils2.F90 - block_control/block_control.F90 column_diagnostics/column_diagnostics.F90 constants/constants.F90 - constants/fmsconstants.F90 - constants4/constantsr4.F90 - constants4/fmsconstantsr4.F90 coupler/atmos_ocean_fluxes.F90 coupler/coupler_types.F90 coupler/ensemble_manager.F90 - data_override/get_grid_version.F90 data_override/data_override.F90 - diag_integral/diag_integral.F90 diag_manager/diag_axis.F90 diag_manager/diag_data.F90 diag_manager/diag_grid.F90 diag_manager/diag_manager.F90 + diag_manager/diag_manifest.F90 diag_manager/diag_output.F90 diag_manager/diag_table.F90 diag_manager/diag_util.F90 - diag_manager/fms_diag_time_utils.F90 - diag_manager/fms_diag_object.F90 - diag_manager/fms_diag_yaml.F90 - diag_manager/fms_diag_file_object.F90 - diag_manager/fms_diag_field_object.F90 - diag_manager/fms_diag_axis_object.F90 - diag_manager/fms_diag_output_buffer.F90 - diag_manager/fms_diag_input_buffer.F90 - diag_manager/fms_diag_time_reduction.F90 - diag_manager/fms_diag_outfield.F90 - diag_manager/fms_diag_elem_weight_procs.F90 - diag_manager/fms_diag_fieldbuff_update.F90 - diag_manager/fms_diag_bbox.F90 - diag_manager/fms_diag_reduction_methods.F90 - drifters/cloud_interpolator.F90 - drifters/drifters.F90 - drifters/drifters_comm.F90 - drifters/drifters_core.F90 - drifters/drifters_input.F90 - drifters/drifters_io.F90 - drifters/quicksort.F90 - exchange/stock_constants.F90 - exchange/xgrid.F90 + fft/fft99.F90 + fft/fft.F90 + fms/fms.F90 + fms/fms_io.F90 field_manager/field_manager.F90 field_manager/fm_util.F90 - field_manager/fm_yaml.F90 - fms/fms_io.F90 - fms/fms.F90 - fms2_io/blackboxio.F90 - fms2_io/fms_io_utils.F90 - fms2_io/fms_netcdf_domain_io.F90 - fms2_io/fms_netcdf_unstructured_domain_io.F90 - fms2_io/fms2_io.F90 - fms2_io/netcdf_io.F90 + field_manager/parse.inc horiz_interp/horiz_interp_bicubic.F90 horiz_interp/horiz_interp_bilinear.F90 horiz_interp/horiz_interp_conserve.F90 + horiz_interp/horiz_interp.F90 horiz_interp/horiz_interp_spherical.F90 horiz_interp/horiz_interp_type.F90 - horiz_interp/horiz_interp.F90 - interpolator/interpolator.F90 + horiz_interp/test_horiz_interp.F90 memutils/memutils.F90 - monin_obukhov/monin_obukhov_inter.F90 - monin_obukhov/monin_obukhov.F90 mosaic/gradient.F90 mosaic/grid.F90 mosaic/mosaic.F90 - mosaic2/grid2.F90 - mosaic2/mosaic2.F90 - mpp/mpp.F90 mpp/mpp_data.F90 mpp/mpp_domains.F90 mpp/mpp_efp.F90 + mpp/mpp.F90 mpp/mpp_io.F90 mpp/mpp_memutils.F90 mpp/mpp_parameter.F90 + mpp/mpp_pset.F90 mpp/mpp_utilities.F90 - parser/yaml_parser.F90 - parser/fms_yaml_output.F90 + oda_tools/oda_core_ecda.F90 + oda_tools/oda_core.F90 + oda_tools/oda_types.F90 + oda_tools/write_ocean_data.F90 + oda_tools/xbt_drop_rate_adjust.f90 platform/platform.F90 - random_numbers/mersennetwister.F90 + random_numbers/MersenneTwister.F90 random_numbers/random_numbers.F90 - sat_vapor_pres/sat_vapor_pres_k.F90 sat_vapor_pres/sat_vapor_pres.F90 - string_utils/fms_string_utils.F90 + sat_vapor_pres/sat_vapor_pres_k.F90 + station_data/station_data.F90 time_interp/time_interp_external.F90 - time_interp/time_interp_external2.F90 time_interp/time_interp.F90 time_manager/get_cal_time.F90 time_manager/time_manager.F90 @@ -253,22 +236,24 @@ list(APPEND fms_fortran_src_files topography/topography.F90 tracer_manager/tracer_manager.F90 tridiagonal/tridiagonal.F90 - libFMS.F90 ) # Collect FMS C source files +# mosaic/create_xgrid.h +# mosaic/gradient_c2l.h +# mosaic/interp.h +# mosaic/mosaic_util.h +# mosaic/read_mosaic.h list(APPEND fms_c_src_files - affinity/affinity.c - fms/fms_stacksize.c + memutils/memuse.c mosaic/create_xgrid.c mosaic/gradient_c2l.c mosaic/interp.c mosaic/mosaic_util.c mosaic/read_mosaic.c - mpp/mpp_memuse.c - parser/yaml_parser_binding.c - parser/yaml_output_functions.c - string_utils/fms_string_utils_binding.c + mpp/affinity.c + mpp/nsclock.c + mpp/threadloc.c ) # Collect FMS header files @@ -290,6 +275,7 @@ if(HAVE_GETTID) endif() # Additional (optional) compiler definitions +# TODO: Remove: if(NOT CONSTANTS) set(CONSTANTS GFDL) endif() @@ -346,10 +332,15 @@ foreach(kind ${kinds}) # C add_library(${libTgt}_c OBJECT ${fms_c_src_files}) - target_include_directories(${libTgt}_c PRIVATE include) + target_include_directories(${libTgt}_c PRIVATE include + mosaic + drifters + fms + mpp/include) + target_compile_definitions(${libTgt}_c PRIVATE "${fms_defs}") - target_link_libraries(${libTgt}_c PRIVATE NetCDF::NetCDF_C + target_link_libraries(${libTgt}_c PRIVATE PkgConfig::NETCDF_C MPI::MPI_C) if(OpenMP_C_FOUND) @@ -360,32 +351,10 @@ foreach(kind ${kinds}) add_library(${libTgt}_f OBJECT ${fms_fortran_src_files}) target_include_directories(${libTgt}_f PRIVATE include + mosaic + drifters fms - fms/include - fms2_io/include - string_utils/include - mpp/include - column_diagnostics/include - monin_obukhov/include - sat_vapor_pres/include - horiz_interp/include - diag_integral/include - random_numbers/include - diag_manager/include - constants4 - topography/include - axis_utils/include - mosaic2/include - constants - astronomy/include - field_manager/include - time_interp/include - tracer_manager/include - tridiagonal/include - interpolator/include - coupler/include - data_override/include - amip_interp/include) + mpp/include) target_compile_definitions(${libTgt}_f PRIVATE "${fms_defs}") target_compile_definitions(${libTgt}_f PRIVATE "${${kind}_defs}") @@ -395,7 +364,7 @@ foreach(kind ${kinds}) set_target_properties(${libTgt}_f PROPERTIES Fortran_MODULE_DIRECTORY ${moduleDir}) - target_link_libraries(${libTgt}_f PRIVATE NetCDF::NetCDF_Fortran + target_link_libraries(${libTgt}_f PRIVATE PkgConfig::NETCDF_F MPI::MPI_Fortran) if(OpenMP_Fortran_FOUND) @@ -426,30 +395,7 @@ foreach(kind ${kinds}) target_include_directories(${libTgt} PUBLIC $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $) + $) target_include_directories(${libTgt} INTERFACE $ @@ -458,8 +404,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_C - NetCDF::NetCDF_Fortran + target_link_libraries(${libTgt} PUBLIC PkgConfig::NETCDF_C + PkgConfig::NETCDF_F MPI::MPI_Fortran) if(OpenMP_Fortran_FOUND) @@ -477,13 +423,13 @@ endforeach() install( TARGETS ${LIB_TARGETS} EXPORT FMSExports - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) ### Package config include(CMakePackageConfigHelpers) -set(CONFIG_INSTALL_DESTINATION lib/cmake/fms) +set(CONFIG_INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/fms) export(EXPORT FMSExports NAMESPACE FMS::