CMake Build Guidelines#
This guide documents the architecture, conventions, and automated checks for ABACUS’s CMake build system. It complements docs/developers_guide/agent_governance.md and AGENTS.md.
Responsibility Boundaries#
ABACUS separates build configuration from target construction across four layers:
Root
CMakeLists.txt— Project declaration, user-facingoption()/cmake_dependent_option()declarations, C++ standard selection, andfind_package()calls. Do not construct or link ABACUS targets here.cmake/modules/Find<Pkg>.cmakeandSetup<Pkg>.cmake— Package and backend discovery. Each module should export standardIMPORTEDtargets (e.g.,Pkg::TargetcarryingINTERFACE_INCLUDE_DIRECTORIESandINTERFACE_LINK_LIBRARIES) instead of leaking raw library paths or global macros intosource/.source/CMakeLists.txt— The single place that assembles:shared compile usage requirements (
abacus::compile_requirements, configured viacmake/CompilerConfiguration.cmake);global feature macro definitions (
_abacus_feature_definitions, applied per target byabacus_apply_build_options());ordered external link closures (
abacus::feature_libs,abacus::linalg_libs,abacus::link_libs);the main executable and its ordered internal target linkage.
Module
source/<module>/CMakeLists.txtandunittests/CMakeLists.txt— Own only their local targets (OBJECTlibraries ortarget_sources()additions) and unit tests registered viaAddTest(). They must not modify global compiler flags, global include paths, or discover external packages.
Remember that ABACUS still maintains two parallel build surfaces alongside the main CMake build. When adding, moving, splitting, or deleting C++ source files, keep all three in sync:
the relevant C++
CMakeLists.txt;source/Makefile.Objects;python/pyabacus/src/*/CMakeLists.txt(when shared files are affected).
Formatting#
All CMake files (CMakeLists.txt and *.cmake) follow cmake-format with the repository configuration in cmake-format.yaml (2-space indentation, markup disabled, AddTest keyword metadata configured).
Run locally before committing:
pre-commit run cmake-format --files <changed-cmake-files>
The Precommit workflow enforces cmake-format as a blocking check on the PR diff.
Forbidden And Deprecated Patterns#
The governance checker (tools/03_code_analysis/agent_governance_check.py) blocks newly added lines that reintroduce legacy patterns:
Removed variables and macros (main build tree: root
CMakeLists.txt,cmake/,source/,tests/):${math_libs}— removed;AddTest()linksabacus::linalg_libsautomatically andsource/CMakeLists.txtowns the executable link closure.USE_LIBXC— renamed to__LIBXC.ENABLE_RAPIDJSON/USE_RAPIDJSON— removed; useENABLE_JSON(__JSON, backed bynlohmann_json).ENABLE_LIBCOMM— removed; LibComm is discovered withENABLE_LIBRI.remove_definitions(...)— ineffective against target-scoped compile definitions; if a legacy test target genuinely must drop a feature macro, useabacus_disable_feature_definitions(...)(discouraged for new tests).
Directory-scoped and package-discovery commands under
source/:add_definitions(),add_compile_definitions(),add_compile_options(),include_directories(),link_directories(),link_libraries()— usetarget_*()commands,cmake/CompilerConfiguration.cmake, or_abacus_feature_definitionsinsource/CMakeLists.txt.find_package(),find_library()— place dependency discovery in rootCMakeLists.txtandcmake/modules/Find<Pkg>.cmake(orcmake/Testing.cmakefor test-only packages).
Legacy test staging in unit-test CMake files under
source/:install(...)andfile(COPY ...)— useabacus_copy_test_files(...)fromcmake/Testing.cmake, which prepares runtime scripts and data at configure time viaconfigure_file(... COPYONLY)and tracks directory changes withCONFIGURE_DEPENDS.
Standard Templates#
1. Adding an Optional External Dependency#
Follow this checklist so options, macros, linkage, and --version / build summary stay consistent:
Declare the option in root
CMakeLists.txt:Use
ENABLE_<FEATURE>for optional features (orUSE_<BACKEND>for hardware/math backends).If the option depends on another option (e.g.
ENABLE_MPIorENABLE_LCAO), usecmake_dependent_option()rather than manualif()overrides.
Discover the package in root
CMakeLists.txt+cmake/modules/:Prefer upstream
*Config.cmakepackages when available.If a custom module is needed, add
cmake/modules/Find<Pkg>.cmakeand export anIMPORTEDtargetPkg::Target.
Wire compile requirements and linkage in
source/CMakeLists.txt:Add
$<$<BOOL:${ENABLE_<FEATURE>}>:__<FEATURE>>to_abacus_feature_definitions(use the__<FEATURE>macro convention, not subdirectory-localUSE_<FEATURE>definitions).Add
$<$<BOOL:${ENABLE_<FEATURE>}>:Pkg::Target>to_abacus_feature_libs(or_abacus_linalg_libsfor numerical backends).Add
Pkg::Targetto theabacus_add_target_compile_requirements(...)loop so object libraries and unit tests inherit its headers and compile flags.
Update build summary:
Record the option status and detected version/path in
cmake/CollectBuildInfoVars.cmake(andcmake/BuildInfo.cmakeif a new placeholder is printed).
2. Adding a Module or Submodule#
Choose between two patterns:
Submodule that extends an existing library (tightly coupled to parent, no separate link boundary needed):
target_sources(parent_target PRIVATE foo.cpp bar.cpp)
if(BUILD_TESTING)
add_subdirectory(unittests)
endif()
Standalone
OBJECTlibrary (distinct link boundary, optional feature, or isolated so lightweight unit tests can link it cleanly):
add_library(my_module OBJECT foo.cpp bar.cpp)
if(ENABLE_COVERAGE)
add_coverage(my_module)
endif()
if(BUILD_TESTING)
add_subdirectory(unittests)
endif()
When adding a new OBJECT library:
Link it in
source/CMakeLists.txtin consumer-before-provider order (static linkers resolve symbols left-to-right).If callers span both PW and LCAO, keep the library unconditional rather than gating it inside
if(ENABLE_LCAO).
3. Adding Unit Tests#
Per AGENTS.md rule 11, unit tests under source/ live in a flat unittests/ directory with source files named test_<file_under_test>.cpp:
abacus_copy_test_files(support)
AddTest(
TARGET MODULE_FOO_bar
LIBS my_module base parameter
SOURCES test_bar.cpp)
Guidelines for unit-test CMakeLists.txt files:
AddTest()automatically linksGTest::gtest_main,GTest::gmock_main, andabacus::linalg_libs— do not pass${math_libs}or duplicateGTesttargets.Prefer linking module
OBJECTtargets inLIBSrather than recompiling../foo.cppor../../bar.cppdirectly inSOURCES; direct cross-directory source lists break downstream tests whenever production files are split or moved.Avoid
abacus_disable_feature_definitions(...)in new unit tests (discouraged). It only strips macros from targets defined in that test directory; linked productionOBJECTlibraries keep their original MPI/GPU compile definitions, so reusing them still requires compatible feature assumptions and initialization. Any test that genuinely needs those features disabled must link or compile objects built with matching settings. In legacy directories that still callabacus_disable_feature_definitions(...), an individualAddTest(...)call may passKEEP_FEATURE_DEFINITIONS __MPIto opt back in.For tests that require MPI, guard
add_subdirectory(unittests)or the specificAddTest()target withif(ENABLE_MPI)so serial builds (-DENABLE_MPI=OFF -DBUILD_TESTING=ON) stay green.
Governance Enforcement Summary#
Check | Executor | Severity | Action |
|---|---|---|---|
| CI ( | medium | block |
New source / heterogeneous file CMake linkage |
| medium | block |
Deprecated CMake variables, macros, and |
| high | block |
Directory-scoped commands and |
| high | block |
|
| medium | block |
New root options or |
| medium | warn |
Unit test |
| high | block |