|
2 | 2 | # SPDX-License-Identifier: CC0-1.0 |
3 | 3 | # SPDX-FileContributor: Carsten Lemmen <carsten.lemmen@hereon.de> |
4 | 4 |
|
5 | | -# Placeholder for src/utilities CMake definitions |
6 | | -# This directory will contain utility code for the project. |
7 | | -# Example: |
8 | | -# add_library(MyUtility MyUtility.cpp) |
9 | | -# target_include_directories(MyUtility PUBLIC ../include) |
| 5 | +# Ensure the directory for generated mossco_sha.F90 exists |
| 6 | +file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/src/include) |
| 7 | + |
| 8 | +# Custom command to generate mossco_sha.F90 |
| 9 | +set(MOSSCO_SHA_SCRIPT ${PROJECT_SOURCE_DIR}/scripts/git-sha.sh) |
| 10 | +set(MOSSCO_SHA_OUTPUT ${PROJECT_BINARY_DIR}/src/include/mossco_sha.F90) |
| 11 | + |
| 12 | +add_custom_command( |
| 13 | + OUTPUT ${MOSSCO_SHA_OUTPUT} |
| 14 | + COMMAND bash ${MOSSCO_SHA_SCRIPT} ${MOSSCO_SHA_OUTPUT} |
| 15 | + DEPENDS ${MOSSCO_SHA_SCRIPT} # If the script itself changes, regenerate |
| 16 | + COMMENT "Generating ${MOSSCO_SHA_OUTPUT} from git revision" |
| 17 | + VERBATIM |
| 18 | +) |
| 19 | + |
| 20 | +# Define the library target |
| 21 | +add_library(mossco_utilities "") |
| 22 | + |
| 23 | +# List of source files |
| 24 | +set(UTILITIES_SOURCES |
| 25 | + ESMF_GridCreate3D.F90 |
| 26 | + mossco.F90 |
| 27 | + mossco_attribute.F90 |
| 28 | + mossco_component.F90 |
| 29 | + mossco_config.F90 |
| 30 | + mossco_db.F90 |
| 31 | + mossco_dictionary.F90 |
| 32 | + mossco_field.F90 |
| 33 | + mossco_geom.F90 |
| 34 | + mossco_grid.F90 |
| 35 | + mossco_gridspec.F90 |
| 36 | + mossco_info.F90 |
| 37 | + mossco_loc.F90 |
| 38 | + mossco_locstream.F90 |
| 39 | + mossco_logging.F90 |
| 40 | + mossco_memory.F90 |
| 41 | + mossco_mesh.F90 |
| 42 | + mossco_netcdf.F90 |
| 43 | + mossco_parameter.F90 |
| 44 | + mossco_routehandle.F90 |
| 45 | + mossco_state.F90 |
| 46 | + mossco_strings.F90 |
| 47 | + mossco_time.F90 |
| 48 | + mossco_variable_types.F90 |
| 49 | + nc_extract_variable.c |
| 50 | + nc_extract_variable_lasttime.c |
| 51 | + solver_library.F90 |
| 52 | + ${MOSSCO_SHA_OUTPUT} # Add generated file to sources |
| 53 | +) |
| 54 | + |
| 55 | +target_sources(mossco_utilities PRIVATE ${UTILITIES_SOURCES}) |
| 56 | + |
| 57 | +# Set Fortran module output directory |
| 58 | +set(CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/modules) |
| 59 | +file(MAKE_DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY}) # Ensure the directory exists |
| 60 | + |
| 61 | +target_include_directories(mossco_utilities |
| 62 | + PUBLIC |
| 63 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> # For sources in this dir |
| 64 | + $<INSTALL_INTERFACE:include/mossco/utilities> # For installed headers of this lib |
| 65 | + $<INSTALL_INTERFACE:include> # For project-wide installed headers (e.g. from src/include) |
| 66 | + PRIVATE |
| 67 | + ${PROJECT_SOURCE_DIR}/src/include # Project-wide source includes |
| 68 | + ${PROJECT_BINARY_DIR}/src/include # For generated headers like mossco_sha.F90 |
| 69 | + ${CMAKE_Fortran_MODULE_DIRECTORY} # For Fortran modules produced by this target |
| 70 | + INTERFACE |
| 71 | + ${CMAKE_Fortran_MODULE_DIRECTORY} # Consumers need access to .mod files |
| 72 | + ${PROJECT_BINARY_DIR}/src/include # Consumers might need generated headers |
| 73 | +) |
| 74 | + |
| 75 | +# Attempt to find ESMF |
| 76 | +find_package(ESMF QUIET) |
| 77 | + |
| 78 | +if(ESMF_FOUND) |
| 79 | + message(STATUS "ESMF found by find_package(ESMF). Linking with ESMF::ESMF.") |
| 80 | + target_link_libraries(mossco_utilities PRIVATE ESMF::ESMF) |
| 81 | + # Modern ESMF::ESMF target should propagate its include directories. |
| 82 | + # If not, explicitly add: |
| 83 | + # target_include_directories(mossco_utilities PRIVATE $<TARGET_PROPERTY:ESMF::ESMF,INTERFACE_INCLUDE_DIRECTORIES>) |
| 84 | +else() |
| 85 | + message(WARNING "ESMF not found by find_package(ESMF). Placeholder for ESMFMKFILE logic or manual setup if needed.") |
| 86 | + # Placeholder for ESMFMKFILE logic: |
| 87 | + # if(DEFINED ENV{ESMFMKFILE} AND EXISTS "$ENV{ESMFMKFILE}") |
| 88 | + # message(STATUS "ESMFMKFILE found at $ENV{ESMFMKFILE}. Parsing for flags (not implemented).") |
| 89 | + # # Add commands to parse $ENV{ESMFMKFILE} for: |
| 90 | + # # ESMF_F90COMPILEPATHS, ESMF_CXXCOMPILEPATHS, ESMF_F90ESMFLINKLIBS, etc. |
| 91 | + # # And apply them using target_include_directories and target_link_libraries. |
| 92 | + # # E.g., target_include_directories(mossco_utilities PRIVATE <parsed_esmf_include_paths>) |
| 93 | + # # E.g., target_link_libraries(mossco_utilities PRIVATE <parsed_esmf_libraries>) |
| 94 | + # else() |
| 95 | + # message(WARNING "ESMFMKFILE environment variable not set or file does not exist.") |
| 96 | + # endif() |
| 97 | +endif() |
| 98 | + |
| 99 | +# Find and link NetCDF |
| 100 | +find_package(NetCDF REQUIRED COMPONENTS Fortran C) |
| 101 | + |
| 102 | +if(NetCDF_FOUND) |
| 103 | + message(STATUS "NetCDF found. Linking with NetCDF::NetCDF_Fortran and NetCDF::NetCDF_C.") |
| 104 | + target_link_libraries(mossco_utilities PRIVATE NetCDF::NetCDF_Fortran NetCDF::NetCDF_C) |
| 105 | + # Modern NetCDF targets should propagate their include directories. |
| 106 | + # If not, explicitly add: |
| 107 | + # target_include_directories(mossco_utilities PRIVATE ${NetCDF_INCLUDE_DIRS}) |
| 108 | +else() |
| 109 | + message(FATAL_ERROR "NetCDF (Fortran and C) was REQUIRED but not found. Check NetCDF installation and CMAKE_PREFIX_PATH.") |
| 110 | +endif() |
| 111 | + |
| 112 | +# Compiler flags based on src/Rules.make |
| 113 | +if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") |
| 114 | + target_compile_options(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-O3>) |
| 115 | + if(CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 10 OR CMAKE_Fortran_COMPILER_VERSION VERSION_EQUAL 10) |
| 116 | + target_compile_options(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-fallow-argument-mismatch>) |
| 117 | + endif() |
| 118 | +elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel") |
| 119 | + target_compile_options(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-O3 -module ${CMAKE_Fortran_MODULE_DIRECTORY}>) |
| 120 | + # Potentially add -DNO_ISO_FORTRAN_ENV if needed, based on Rules.make EXTRA_CPP for IFORT |
| 121 | + # target_compile_definitions(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:NO_ISO_FORTRAN_ENV>) |
| 122 | +elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "PGI") # For NVIDIA HPC SDK (nvfortran) |
| 123 | + target_compile_options(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-O3 -module ${CMAKE_Fortran_MODULE_DIRECTORY}>) |
| 124 | +elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "XL" OR CMAKE_Fortran_COMPILER_ID STREQUAL "IBM") |
| 125 | + target_compile_options(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-O3 -qmoddir=${CMAKE_Fortran_MODULE_DIRECTORY} -qstrict>) |
| 126 | + target_compile_definitions(mossco_utilities PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:NO_ISO_FORTRAN_ENV>) |
| 127 | +endif() |
| 128 | + |
| 129 | +# Add a dependency to ensure mossco_sha.F90 is generated before mossco_utilities is compiled |
| 130 | +add_dependencies(mossco_utilities ${MOSSCO_SHA_OUTPUT}) # This is not the right way for source files. |
| 131 | +# The custom command output being part of UTILITIES_SOURCES handles this dependency. |
| 132 | + |
| 133 | +message(STATUS "Configured mossco_utilities library.") |
0 commit comments