Skip to content

Implement CMake build system for MOM6#25

Open
hctorres wants to merge 10 commits into
dev/turbofrom
192-feature-cmake-build-system-for-MOM6
Open

Implement CMake build system for MOM6#25
hctorres wants to merge 10 commits into
dev/turbofrom
192-feature-cmake-build-system-for-MOM6

Conversation

@hctorres
Copy link
Copy Markdown

@hctorres hctorres commented May 12, 2026

Part of issue-192 cross-repo CMake build system effort

This PR is one of four coordinated changes:

Repo PR Base
FMS TURBO-ESM/FMS#6 dev/turbo
TIM TURBO-ESM/TIM#16 main
MOM6 this PR dev/turbo
turbo-stack TURBO-ESM/turbo-stack#233 main

Recommended merge order: FMS → TIM → MOM6 → turbo-stack. turbo-stack consumes this via add_subdirectory(${MOM6_SOURCE_DIR} mom6_build).

Summary

  • Adds a parallel CMake build for MOM6 — purely additive; the existing mkmf flow is untouched.
  • cmake/MOM6Options.cmake is the canonical definition of TURBO_INFRA (FMS2 | TIM) and TURBO_MEMORY_MODE; turbo-stack includes this file so both repos stay in sync on option names and defaults.
  • cmake/MARBLTargets.cmake creates the MARBL::marbl target from source via MARBL_SOURCE_DIR.
  • Top-level CMakeLists.txt supports both standalone (cmake -S MOM6 -B build) and subdirectory (add_subdirectory) builds via if(NOT TARGET TURBO::infra_r8) guards for both the infra and MARBL targets.
  • Both FMS2 and TIM backends supported in standalone mode. For standalone builds, FindNetCDF.cmake is pre discovered from the backend's install prefix (FMS_ROOT / TIM_ROOT env or var, or via CMAKE_PREFIX_PATH) before find_package(FMS|TIM), so the transitive find_dependency(NetCDF) works on systems without a NetCDFConfig.cmake (e.g. NCAR Derecho's NetCDF module).
  • Layered library decomposition: MOM6::framework_baseMOM6::infraMOM6::frameworkMOM6::gridMOM6::ioMOM6::ocean, plus MOM6::CVMix, MOM6::GSW, and MOM6::marbl. Transitive include propagation correct via PUBLIC scope.
  • Configure-time guard on pkg/CVMix-src and pkg/GSW-Fortran submodules so an uninitialized clone fails with a clear
    FATAL_ERROR pointing at the right git command, instead of a cryptic "file not found" deep in the build.

Environment

turbo-stack has a script to set up the environment for you on derecho.

Test plan

  • Subdirectory build via turbo-stack: turbo-stack's script to "Build MOM6 with TIM and FMS".

hctorres and others added 6 commits May 6, 2026 16:45
- Fix LANGUAGES: remove unused C/CXX from project() declaration
- Add TURBO_INFRA and TURBO_MEMORY_MODE validation in MOM6Options.cmake
- Add if(NOT TARGET) guards so MOM6 can be built without turbo-stack:
  finds FMS2 or TIM via find_package, finds MPI/NetCDF directly
- Add cmake/MARBLTargets.cmake with MARBL source list; used by the
  standalone path when MARBL::marbl is not provided by a parent build
- Add comment blocks explaining subdirectory vs standalone build modes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Remove double-nesting bug: after find_package(TIM), TIM_DIR is already
  set to the directory containing TIMConfig.cmake, so appending
  /lib/cmake/tim created an invalid path.
- Change find_package(TIM REQUIRED) to find_package(TIM REQUIRED COMPONENTS R8)
  so CMake errors at configure time if TIM was built without 64BIT=ON.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…E_DIR guard.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@hctorres hctorres self-assigned this May 12, 2026
@hctorres hctorres added the enhancement New feature or request label May 12, 2026
@hctorres hctorres marked this pull request as draft May 12, 2026 17:56
@hctorres hctorres changed the title Add CMake build system for MOM6 Implement CMake build system for MOM6 May 15, 2026
hctorres and others added 3 commits May 22, 2026 17:39
…mes.

The recent dev/turbo merge added MOM_ANN.F90, MOM_diag_buffers.F90, and
Recon1d_PLM_WLS.F90; deleted FMS_coupler_util.F90, generic_tracer.F90,
and generic_tracer_utils.F90 (their functionality is folded into the
new MOM_generic_tracer.F90); and renamed MOM_load_love_numbers.F90 to
MOM_load_Love_numbers.F90 (case-only). The cmake lists weren't updated
in lockstep because dev/turbo's autotools build doesn't enumerate
sources by name. Reconcile here so the cmake build works again.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Two HIGH findings from the pre-PR review:

1. CMAKE_MODULE_PATH was appended AFTER find_package(FMS|TIM) — useless,
   because FMSConfig.cmake / TIMConfig.cmake call find_dependency(NetCDF)
   during the find_package, before the append runs. On NetCDF installs
   without a CMake config file (e.g. NCAR Derecho's spack module), that
   transitive resolution requires FindNetCDF.cmake on the module path.

   Pre-discover the bundled FindNetCDF.cmake from the backend's install
   prefix (FMS_ROOT / TIM_ROOT env or var, or via CMAKE_PREFIX_PATH) and
   prepend it to CMAKE_MODULE_PATH *before* find_package, so the
   transitive NetCDF lookup succeeds.

2. pkg/CVMix-src and pkg/GSW-Fortran are git submodules. If a downstream
   clone forgot `git submodule update --init`, the cmake build silently
   reached target_sources() with missing files and failed with a cryptic
   "file not found" error. Add an explicit existence check on a known
   source file in each submodule, with a FATAL_ERROR pointing the user
   at the right git command.
Comment thread config_src/infra/CMakeLists.txt Outdated
@@ -0,0 +1,121 @@
# ── Why multiple framework libraries? ─────────────────────────────────────────
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hctorres, if I am adding a new file to the src/framework directory. How do I figure out which tier to add my file? Is there an easy way to figure this out?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today it would be a manual process. You could grep the new file's use statements, map each used module to the tier it's defined in. In the future we might want to put each layer in its own directory to make what is used where more obvious but that would break the old build system if we did it now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hctorres, if you could add comments in the CMakeLists.txt file about using AI to identify which tier a new file should go into that would be great.

Comment thread CMakeLists.txt
Comment thread pkg/CMakeLists.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants