Skip to content

Commit

Permalink
improved inline documentation; cleaned up files; removed GC-GetRunDir…
Browse files Browse the repository at this point in the history
… and moved that code to the root CMakeLists
  • Loading branch information
Liam Bindle committed May 30, 2019
1 parent 9d27642 commit 477046c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 113 deletions.
65 changes: 51 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()

# Add our module path and include our helpers
# Add CMakeScripts/ to the module path and import helper functions
list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/CMakeScripts)
include(GC-Helpers)

# Define the base target (which will store the build configuration properties)
# Declare the BaseTarget. All GEOS-Chem targets depend on BaseTarget.
# This is used to control the compiler options and definitions for GEOS-Chem
# targets (via inheritance).
add_library(BaseTarget INTERFACE)

# Set default CMAKE_BUILD_TYPE to Release
message(STATUS "Selected compilers:")
message(" C: ${CMAKE_C_COMPILER}")
message(" C++: ${CMAKE_CXX_COMPILER}")
message(" Fortran: ${CMAKE_Fortran_COMPILER}")
message(STATUS "Common CMake variables:")
message(" + CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")

# Set CMAKE_BUILD_TYPE to Release by default
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release"
CACHE STRING
Expand All @@ -24,35 +33,64 @@ if(NOT CMAKE_BUILD_TYPE)
)
endif()

message(STATUS "Selected compilers:")
message(" C: ${CMAKE_C_COMPILER}")
message(" C++: ${CMAKE_CXX_COMPILER}")
message(" Fortran: ${CMAKE_Fortran_COMPILER}")
message(STATUS "Common CMake variables:")
message(" + CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
# Make CMAKE_BUILD_TYPE an option. This restricts the possible values
# to Release and Debug.
set_dynamic_option(CMAKE_BUILD_TYPE
DEFAULT ${CMAKE_BUILD_TYPE}
OPTIONS "Release" "Debug"
LOG CMAKE_VAR_LOG
)
dump_log(CMAKE_VAR_LOG)

# Put all mod files in mod subdirectory
# Put all mod files in a subdirectory of the build directory called mod
set(CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/mod)
target_include_directories(BaseTarget
INTERFACE ${PROJECT_BINARY_DIR}/mod
)

# Get the run directory and implementation type
include(GC-GetRunDir) # sets RUNDIR and IMPL
# Set the default value of RUNDIR to the parent directory of the build directory
message(STATUS "Run directory setup:")
set(RUNDIR_DEFAULT "..")
set_dynamic_default(RUNDIR DEFAULT "${RUNDIR_DEFAULT}"
LOG RUNDIR_LOG
IS_DIRECTORY
)
dump_log(RUNDIR_LOG)
message(STATUS "Bootstrapping ${RUNDIR}")
get_filename_component(RUNDIR "${RUNDIR}" ABSOLUTE)

# Inspect RUNDIR's name to guess the implementation (GCC or GCHP)
get_filename_component(RUNDIR_NAME "${RUNDIR}" NAME)
if("${RUNDIR_NAME}" MATCHES "gchp.*")
set(IMPL_GUESS "GCHP")
else()
set(IMPL_GUESS "Classic")
endif()

# Set the default value of the implementation to that guess
message(STATUS "GEOS-Chem implementation type:")
set_dynamic_option(IMPL
DEFAULT "${IMPL_GUESS}"
LOG IMPL_LOG
SELECT_EXACTLY 1
OPTIONS "Classic" "GCHP"
)
dump_log(IMPL_LOG)

# Print a description of the source code repo's version
get_repo_version(GC_REPO_VERSION ${CMAKE_SOURCE_DIR})
message(STATUS "GEOS-Chem version: ${GC_REPO_VERSION}")

# The implementation controls who configures BaseTarget
if("${IMPL}" STREQUAL "Classic")
# Configure BaseTarget for a GC-Classic build
include(GC-ConfigureClassicBuild)
elseif("${IMPL}" STREQUAL "GCHP")
# Add GCHP as a subdirectory. The CMake scripts in GCHP setup BaseTarget
add_subdirectory(GCHP)
endif()

# Add subdirectories
# Add all the subdirectories. Each subdirectory specifies how it should be built.
add_subdirectory(KPP)
add_subdirectory(Headers)
add_subdirectory(GeosUtil)
Expand All @@ -64,4 +102,3 @@ add_subdirectory(ISORROPIA)
add_subdirectory(GeosRad)
add_subdirectory(GTMM)
add_subdirectory(GeosCore)

108 changes: 38 additions & 70 deletions CMakeScripts/GC-ConfigureClassicBuild.cmake
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
#[[ GC-ConfigureClassicBuild.cmake
This file configures BaseTarget for a GEOS-Chem Classic build. This file does
three things:
1) Finds dependencies using find_package. For GEOS-Chem Classic these
dependencies are:
a) NetCDF-C and NetCDF-Fortran
b) OpenMP
2) Sets the appropriate preprocessor definitions for the run directory.
3) Sets the default compiler flags.
]]

#[[--------------------------------------------------------------------------]]
#[[ Finding dependencies. ]]
#[[--------------------------------------------------------------------------]]
# Find NetCDF on the local machine. Make NetCDF-F a dependency of BaseTarget.
find_package(NetCDF REQUIRED)

# Set BaseTarget properties
target_link_libraries(BaseTarget
INTERFACE NetCDF-F
)

# Print message with the repo's last commit
get_repo_version(GC_REPO_VERSION ${CMAKE_SOURCE_DIR})
message(STATUS "GEOS-Chem version: ${GC_REPO_VERSION}")

#[[--------------------------------------------------------------------------]]
#[[ Setting preprocessor definitions. ]]
#[[--------------------------------------------------------------------------]]


#[[ Get defaults for settings by inspecting the run directory. ]]

# Define a macro to call getRunInfo in the run directory
# Define a macro for inspecting the run directory. Inspecting the run
# directory is how we determine which compiler definitions need to be set.
macro(inspect_rundir VAR ID)
execute_process(COMMAND perl ${RUNDIR}/getRunInfo ${RUNDIR} ${ID}
OUTPUT_VARIABLE ${VAR}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endmacro()

# Inspect MET
# Inspect the run directory to get the met field type and grid resolution
inspect_rundir(RUNDIR_MET 0)
if("${RUNDIR_MET}" STREQUAL "geosfp")
set(RUNDIR_MET "GEOS_FP")
elseif("${RUNDIR_MET}" STREQUAL "merra2")
set(RUNDIR_MET "MERRA2")
endif()

# Inspect GRID
inspect_rundir(RUNDIR_GRID 1)
if("${RUNDIR_GRID}" STREQUAL "2x25")
set(RUNDIR_GRID "2x2.5")
Expand All @@ -61,7 +29,10 @@ elseif("${RUNDIR_GRID}" STREQUAL "025x03125")
set(RUNDIR_GRID "0.25x0.3125")
endif()

# Inspect SIM and select KPP mech
# Inspect the run directory to get simulation type
inspect_rundir(RUNDIR_SIM 2)

# Determine the appropriate chemistry mechanism base on the simulation
set(STANDARD_MECHS
"standard"
"benchmark"
Expand All @@ -78,6 +49,7 @@ set(STANDARD_MECHS
"CO2"
"aerosol"
"Hg"
"HEMCO" # doesn't matter for the HEMCO standalone
)
set(TROPCHEM_MECHS
"tropchem"
Expand All @@ -92,9 +64,6 @@ set(SOA_SVPOA_MECHS
set(CUSTOM_MECHS
"custom"
)

inspect_rundir(RUNDIR_SIM 2)

if("${RUNDIR_SIM}" IN_LIST STANDARD_MECHS)
set(RUNDIR_MECH "Standard")
elseif("${RUNDIR_SIM}" IN_LIST TROPCHEM_MECHS)
Expand All @@ -107,7 +76,7 @@ else()
message(FATAL_ERROR "Unknown simulation type \"${RUNDIR_SIM}\". Cannot determine MECH.")
endif()

# Misc
# Definitions for specific run directories
if("${RUNDIR_SIM}" STREQUAL "masscons")
set_dynamic_default(GC_DEFINES DEFAULT MASSCONS)
elseif("${RUNDIR_SIM}" MATCHES "TOMAS15")
Expand All @@ -117,7 +86,7 @@ elseif("${RUNDIR_SIM}" MATCHES "TOMAS40")
set_dynamic_default(GC_DEFINES DEFAULT TOMAS TOMAS40)
endif()

# Inspect NESTED
# Inspect the run directory to determine if it's a nested simulation
inspect_rundir(RUNDIR_REGION 3)
if("${RUNDIR_REGION}" STREQUAL "n")
set(RUNDIR_NESTED "FALSE")
Expand All @@ -127,10 +96,7 @@ else()
string(TOUPPER "${RUNDIR_REGION}" RUNDIR_REGION)
endif()


#[[ Settings TUI with defaults from the run directory inspection. ]]

# MET field
# Make MET an option and set the appropriate definition
set_dynamic_option(MET
DEFAULT ${RUNDIR_MET}
LOG GENERAL_OPTIONS_LOG
Expand All @@ -139,7 +105,7 @@ set_dynamic_option(MET
)
set_dynamic_default(GC_DEFINES DEFAULT ${MET})

# Check for nested grid
# Make NESTED an option and set the appropriate definitions
set_dynamic_option(NESTED
DEFAULT "${RUNDIR_NESTED}"
LOG GENERAL_OPTIONS_LOG
Expand All @@ -157,7 +123,8 @@ if(${NESTED})
set_dynamic_default(GC_DEFINES DEFAULT NESTED_${REGION})
endif()

# Horizontal grid
# Make GRID an option with different options based on MET and NESTED, and
# set the appropriate definition
if(${NESTED})
if("${MET}" STREQUAL "MERRA2") # Nested w/ MERRA2
set_dynamic_option(GRID
Expand Down Expand Up @@ -185,22 +152,24 @@ endif()
string(REPLACE "." "" TEMP "GRID${GRID}")
set_dynamic_default(GC_DEFINES DEFAULT ${TEMP})

# Chemistry mechanism
# Make MECH an option. This controls which KPP directory is used.
set_dynamic_option(MECH
DEFAULT "${RUNDIR_MECH}"
LOG GENERAL_OPTIONS_LOG
SELECT_EXACTLY 1
OPTIONS "Standard" "Tropchem" "SOA_SVPOA"
)

# Set reduced grid
# Make LAYERS an option and set the appropriate definitions. Determine the
# default value based on RUNDIR_SIM.
set(LAYERS_72_SIMS
"standard"
"benchmark"
"aciduptake"
"marinePOA"
"TransportTracers"
"custom"
"HEMCO" # doesn't matter for the HEMCO standalone
)
set(LAYERS_47_SIMS
"masscons"
Expand Down Expand Up @@ -265,19 +234,24 @@ if(${GTMM})
endif()

# Build hemco_standalone?
if("${RUNDIR_SIM}" STREQUAL "HEMCO")
set(HCOSA_DEFAULT "TRUE")
else()
set(HCOSA_DEFAULT "FALSE")
endif()
set_dynamic_option(HCOSA
DEFAULT "FALSE"
DEFAULT "${HCOSA_DEFAULT}"
LOG GENERAL_OPTIONS_LOG
SELECT_EXACTLY 1
OPTIONS "TRUE" "FALSE"
)

# Build with timers?
if("${RUNDIR_SIM}" STREQUAL "benchmark")
set(TIMERS_DEFAULT "TRUE")
else()
set(TIMERS_DEFAULT "FALSE")
endif()
# Build with timers?
set_dynamic_option(TIMERS
DEFAULT ${TIMERS_DEFAULT}
LOG GENERAL_OPTIONS_LOG
Expand All @@ -289,7 +263,7 @@ if(${TIMERS})
endif()


# Get diagnostics
# Build with BPCH diagnostics?
set_dynamic_option(BPCH_DIAG
DEFAULT "TRUE"
OPTIONS "TRUE" "FALSE"
Expand All @@ -299,15 +273,16 @@ if(${BPCH_DIAG})
set_dynamic_default(GC_DEFINES DEFAULT "BPCH_DIAG" "BPCH_TIMESER" "BPCH_TPBC")
endif()

# Read netcdf.inc and search for nf_def_var_deflate
# Use the NC_HAS_COMPRESSION definition if nf_def_var_deflate is in netcdf.inc
if(EXISTS ${NETCDF_F77_INCLUDE_DIR}/netcdf.inc)
file(READ ${NETCDF_F77_INCLUDE_DIR}/netcdf.inc NCINC)
if("${NCINC}" MATCHES ".*nf_def_var_deflate.*")
set_dynamic_default(GC_DEFINES DEFAULT "NC_HAS_COMPRESSION")
endif()
endif()

# Get flexible precision setting
# Make an option for controlling the flexible precision. Set the appropriate
# definition
set_dynamic_option(PREC
DEFAULT "REAL8"
SELECT_EXACTLY 1
Expand All @@ -318,7 +293,7 @@ if("${PREC}" STREQUAL "REAL8")
set_dynamic_default(GC_DEFINES DEFAULT "USE_REAL8")
endif()

# Single-threaded or multi-threaded
# Build a single-threaded or multi-threaded executable?
set_dynamic_option(OMP
DEFAULT "TRUE"
SELECT_EXACTLY 1
Expand All @@ -337,20 +312,16 @@ endif()
message(STATUS "General settings:")
dump_log(GENERAL_OPTIONS_LOG)

# Get resulting GC_DEFINES (overridable)
# By using set_dynamic_default, the resulting GC_DEFINES is overwritable
string(REPLACE " " ";" GC_DEFINES "${GC_DEFINES}")
set_dynamic_default(GC_DEFINES LOG RESULTING_DEFINES_LOG)


#[[ Set resulting defintions on BaseTarget. ]]
# Set the definitions for the BaseTarget
target_compile_definitions(BaseTarget INTERFACE ${GC_DEFINES})
unset(GC_DEFINES)


#[[--------------------------------------------------------------------------]]
#[[ Setting default compiler options. ]]
#[[--------------------------------------------------------------------------]]

# Set the Fortran compiler options based on the compiler's family
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Intel")
set_dynamic_default(FC_OPTIONS
DEFAULT
Expand Down Expand Up @@ -390,15 +361,12 @@ elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_Fortran_FLAGS_RELEASE "-O3 -funroll-loops")
set(CMAKE_Fortran_FLAGS_DEBUG "-g -gdwarf-2 -gstrict-dwarf -O0 -Wall -Wextra -Wconversion -Warray-temporaries -fcheck-array-temporaries")
else()
message(FATAL_ERROR "${CMAKE_Fortran_COMPILER_ID} Fortran compiler is not currently supported!")
message(FATAL_ERROR "${CMAKE_Fortran_COMPILER_ID} Fortran compiler is currently not supported!")
endif()

message(STATUS "Resulting definitions/options:")
dump_log(RESULTING_DEFINES_LOG)

# Set compiler definitions and options in BaseTarget
target_compile_options(BaseTarget
INTERFACE
${FC_OPTIONS}
)
unset(FC_OPTIONS)
# Set compiler options for the BaseTarget
target_compile_options(BaseTarget INTERFACE ${FC_OPTIONS})
unset(FC_OPTIONS)
Loading

0 comments on commit 477046c

Please sign in to comment.