Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cmake #110

Draft
wants to merge 5 commits into
base: noah_om_grid
Choose a base branch
from
Draft

Cmake #110

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Usage: from noah-owp-modular directory type:
# mkdir ../build
# cd ../build
# cmake ../noah-owp-modular
# cmake --build . -- VERBOSE=1
# cd ../noah-owp-modular/run
# ../../build/noah-owp-modular.exe namelist.input
# cd ../test
# ../../build/noahowp_driver_test.exe ../run/namelist.input
#
# Tested on:
# 1) MacOS Sonoma v14.4.1 (Darwin 23.4.0 arm64)
# 2) Ubuntu Linux 6.5.0-35-generic running in Lima terminal on Mac
############################################################
# Define the project and the dependencies that it has
############################################################

cmake_minimum_required(VERSION 3.28)

project(noah-owp-modular VERSION 0.1
DESCRIPTION "Noah OWP Modular"
LANGUAGES Fortran)

# Add NetCDF Libraries
message(PROJECT_SOURCE_DIR:"${PROJECT_SOURCE_DIR}")

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
include("FindNetCDF.cmake")
cmake_path(GET NETCDF_LIBRARIES_F90 PARENT_PATH NETCDF_LIB_DIR)
set(NETCDF_INC_DIR ${NETCDF_INCLUDES})
message(NETCDF_INC_DIR:"${NETCDF_INC_DIR}")
message(NETCDF_LIB_DIR:"${NETCDF_LIB_DIR}")
message(CMAKE_SOURCE_DIR:"${CMAKE_SOURCE_DIR}")
message(CMAKE_BINARY_DIR:"${CMAKE_BINARY_DIR}")

# Require that Fortran 90 is supported
if(NOT CMAKE_Fortran_COMPILER_SUPPORTS_F90)
message(FATAL_ERROR "Fortran compiler does not support F90")
endif(NOT CMAKE_Fortran_COMPILER_SUPPORTS_F90)

# Set compiler options. You may need to uncomment/comment appropriate lines.
if(CMAKE_Fortran_COMPILER_ID STREQUAL "Absoft")
message(FATAL_ERROR "Absoft Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "CCur")
message(FATAL_ERROR "Concurrent Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Flang")
message(FATAL_ERROR "Classic Flang Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "G95")
message(FATAL_ERROR "G95 Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
# Mac OS Gfortran F90FLAGS and FREESOURCE flags
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-cpp;-fbacktrace;-fcheck=all>")
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-ffree-form;-ffree-line-length-none;-fbounds-check;-ffpe-trap=overflow,invalid,zero>")
# Linux Gfortran F90FLAGS and FREESOURCE flags
#add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-cpp;-fbacktrace;-fcheck=all>")
#add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-ffree-form;-ffree-line-length-none>")
# Big Sur Gfortran F90FLAGS and FREESOURCE flags
#add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-cpp;-fbacktrace;-fcheck=all>")
#add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-ffree-form;-ffree-line-length-none>")
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-check;-warn;-traceback>")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "LCC")
message(FATAL_ERROR "MCST Elbrus C/C++/Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
message(FATAL_ERROR "LLVM Flang Fortran compiler not configured.")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "PGI")
# Linux pgf90 F90FLAGS and FREESOURCE flags
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-cpp;-Kieee>")
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-Mfree>")
# Cheyenne pgf90 F90FLAGS and FREESOURCE flags
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-cpp;-Kieee;-byteswapio;-Mbackslash>")
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-Mfree>")
endif()

# Set some options the user may choose
# Uncomment the below if you want the user to choose a parallelization library
#option(USE_MPI "Use the MPI library for parallelization" OFF)
#option(USE_OPENMP "Use OpenMP for parallelization" OFF)

# This INCLUDE statement executes code that sets the compile flags for DEBUG,
# RELEASE, and TESTING. You should review this file and make sure the flags
# are to your liking.
#include(${CMAKE_MODULE_PATH}/SetFortranFlags.cmake)

# Add NGEN_ACTIVE option
#option(NGEN_ACTIVE "Enter mode" OFF)
#if (NGEN_ACTIVE)
# add_definitions(-DNGEN_ACTIVE)
#endif()

# Locate and set parallelization libraries. There are some CMake peculiarities
# taken care of here, such as the fact that the FindOpenMP routine doesn't know
# about Fortran.
#include(${CMAKE_MODULE_PATH}/SetParallelizationLibrary.cmake)

# There is an error in CMAKE with this flag for pgf90. Unset it
#get_filename_component(FCNAME ${CMAKE_Fortran_COMPILER} NAME)
#if(FCNAME STREQUAL "pgf90")
# unset(CMAKE_SHARED_LIBRARY_LINK_Fortran_FLAGS)
#endif(FCNAME STREQUAL "pgf90")


############################################################
# Define the actual files and folders that make up the build
############################################################

# Define some directories
set(src ${CMAKE_SOURCE_DIR}/src)
set(lib ${CMAKE_BINARY_DIR}/lib)
set(bin ${CMAKE_BINARY_DIR}/bin)

############################################################
# Build the targets
############################################################

add_executable(noah-owp-modular.exe "${PROJECT_SOURCE_DIR}/driver/NoahModularDriver.f90")
add_executable(noahowp_driver_test.exe "${PROJECT_SOURCE_DIR}/test/noahowp_driver_test.f90")
include_directories(bmi src ${lib})
target_link_libraries(noah-owp-modular.exe src bmi ${NETCDF_LIBRARIES})
target_link_libraries(noahowp_driver_test.exe src bmi ${NETCDF_LIBRARIES})
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-I ${NETCDF_INC_DIR};-L ${NETCDF_LIB_DIR} -lnetcdff>")

# Have the .mod files placed in the lib folder
set(CMAKE_Fortran_MODULE_DIRECTORY ${lib})

# Add source have compiled files placed in the bin folder
add_subdirectory(${src} ${bin})
add_subdirectory(bmi)
# Add a distclean target to the Makefile
#add_custom_target(distclean
# COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/distclean.cmake
68 changes: 68 additions & 0 deletions FindNetCDF.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# - Find NetCDF
# Find the native NetCDF includes and library
# From: https://github.com/jedbrown/cmake-modules/blob/master/FindNetCDF.cmake
#
# NETCDF_INCLUDES - where to find netcdf.h, etc
# NETCDF_LIBRARIES - Link these libraries when using NetCDF
# NETCDF_FOUND - True if NetCDF found including required interfaces (see below)
#
# Your package can require certain interfaces to be FOUND by setting these
#
# NETCDF_CXX - require the C++ interface and link the C++ library
# NETCDF_F77 - require the F77 interface and link the fortran library
# NETCDF_F90 - require the F90 interface and link the fortran library
#
# The following are not for general use and are included in
# NETCDF_LIBRARIES if the corresponding option above is set.
#
# NETCDF_LIBRARIES_C - Just the C interface
# NETCDF_LIBRARIES_CXX - C++ interface, if available
# NETCDF_LIBRARIES_F77 - Fortran 77 interface, if available
# NETCDF_LIBRARIES_F90 - Fortran 90 interface, if available
#
set (NETCDF_F90 "YES")

if (NETCDF_INCLUDES AND NETCDF_LIBRARIES)
# Already in cache, be silent
set (NETCDF_FIND_QUIETLY TRUE)
endif (NETCDF_INCLUDES AND NETCDF_LIBRARIES)

find_path (NETCDF_INCLUDES netcdf.h
HINTS NETCDF_DIR ENV NETCDF_DIR)

find_library (NETCDF_LIBRARIES_C NAMES netcdf)
mark_as_advanced(NETCDF_LIBRARIES_C)

set (NetCDF_has_interfaces "YES") # will be set to NO if we're missing any interfaces
set (NetCDF_libs "${NETCDF_LIBRARIES_C}")

get_filename_component (NetCDF_lib_dirs "${NETCDF_LIBRARIES_C}" PATH)

macro (NetCDF_check_interface lang header libs)
if (NETCDF_${lang})
find_path (NETCDF_INCLUDES_${lang} NAMES ${header}
HINTS "${NETCDF_INCLUDES}" NO_DEFAULT_PATH)
find_library (NETCDF_LIBRARIES_${lang} NAMES ${libs}
HINTS "${NetCDF_lib_dirs}" NO_DEFAULT_PATH)
mark_as_advanced (NETCDF_INCLUDES_${lang} NETCDF_LIBRARIES_${lang})
if (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
list (INSERT NetCDF_libs 0 ${NETCDF_LIBRARIES_${lang}}) # prepend so that -lnetcdf is last
else (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
set (NetCDF_has_interfaces "NO")
message (STATUS "Failed to find NetCDF interface for ${lang}")
endif (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
endif (NETCDF_${lang})
endmacro (NetCDF_check_interface)

NetCDF_check_interface (CXX netcdfcpp.h netcdf_c++)
NetCDF_check_interface (F77 netcdf.inc netcdff)
NetCDF_check_interface (F90 netcdf.mod netcdff)

set (NETCDF_LIBRARIES "${NetCDF_libs}" CACHE STRING "All NetCDF libraries required for interface level")

# handle the QUIETLY and REQUIRED arguments and set NETCDF_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (NetCDF DEFAULT_MSG NETCDF_LIBRARIES NETCDF_INCLUDES NetCDF_has_interfaces)

mark_as_advanced (NETCDF_LIBRARIES NETCDF_INCLUDES)
4 changes: 4 additions & 0 deletions bmi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(BMI_MODS bmi.f90
bmi_grid.f90
CACHE INTERNAL "")
add_library(bmi "${BMI_MODS}")
File renamed without changes.
54 changes: 54 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
set(SRC_MODS AsciiReadModule.f90
AlbedoModule.f90
AtmProcessing.f90
AttributesType.f90
bmi_noahowp.f90
CMakeLists.txt
CanopyWaterModule.f90
ConstantsModule.f90
DateTimeUtilsModule.f90
DevNotes.txt
DomainType.f90
DomainGridType.f90
EnergyGridType.f90
ForcingType.f90
UtilitiesModule.f90
WaterType.f90
EnergyModule.f90
EnergyGridType.f90
EnergyType.f90
ErrorCheckModule.f90
EtFluxModule.f90
ForcingType.f90
ForcingGridType.f90
ForcingModule.f90
InterceptionModule.f90
LevelsGridType.f90
LevelsType.f90
NamelistRead.f90
OptionsGridType.f90
OptionsType.f90
OutputModule.f90
ParametersGridType.f90
ParametersRead.f90
ParametersType.f90
PrecipHeatModule.f90
RunModule.f90
ShortwaveRadiationModule.f90
SnowLayerChange.f90
SnowSoilTempModule.f90
SnowWaterModule.f90
SnowWaterRenew.f90
SoilWaterModule.f90
SoilWaterMovement.f90
SoilWaterRetentionCoeff.f90
SubsurfaceRunoffModule.f90
SurfaceRunoffInfiltration.f90
SurfaceRunoffModule.f90
ThermalPropertiesModule.f90
UtilitiesModule.f90
WaterGridType.f90
WaterModule.f90
WaterType.f90
CACHE INTERNAL "")
add_library(src "${SRC_MODS}")
File renamed without changes.
File renamed without changes.