Skip to content
Merged
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
10 changes: 9 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
os: ubuntu-26.04
preset: doc-gcc-conda
tmatrix: "ON"
pch: "ON"
pyarts: "yes"
check: "no"
doc: "yes"
Expand All @@ -26,6 +27,7 @@ jobs:
os: ubuntu-26.04
preset: default-gcc-conda
tmatrix: "ON"
pch: "ON"
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -37,6 +39,7 @@ jobs:
os: ubuntu-26.04-arm
preset: default-gcc-conda
tmatrix: "ON"
pch: "ON"
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -48,6 +51,7 @@ jobs:
os: ubuntu-26.04
preset: reldeb-gcc-conda
tmatrix: "ON"
pch: "OFF" # checks that every file includes what it uses
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -59,6 +63,7 @@ jobs:
os: ubuntu-26.04
preset: lgpl-gcc-conda
tmatrix: "OFF"
pch: "ON"
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -70,6 +75,7 @@ jobs:
os: macos-26
preset: default-clang-conda
tmatrix: "ON"
pch: "ON"
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -81,6 +87,7 @@ jobs:
os: macos-26
preset: reldeb-clang-conda
tmatrix: "ON"
pch: "OFF" # checks that every file includes what it uses
pyarts: "yes"
check: "yes"
doc: "no"
Expand All @@ -92,6 +99,7 @@ jobs:
os: windows-2025-vs2026
preset: default-msvc-conda
tmatrix: "OFF"
pch: "ON"
pyarts: "yes"
check: "yes"
doc: "no"
Expand Down Expand Up @@ -131,7 +139,7 @@ jobs:
- name: Configure
shell: bash -l {0}
run: |
cmake --preset=${{ matrix.preset }} -DENABLE_TMATRIX=${{ matrix.tmatrix }}
cmake --preset=${{ matrix.preset }} -DENABLE_TMATRIX=${{ matrix.tmatrix }} -DENABLE_PCH=${{ matrix.pch }}

- name: Build ARTS Workspace
shell: bash -l {0}
Expand Down
38 changes: 32 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ option (ENABLE_CXX26 "Turn on C++26 (use is risky, there are known issues)" OFF)
option (ENABLE_IPO "Turn on IPO (experimental)" OFF)
option (ENABLE_MPI "Turn on MPI" OFF)
option (ENABLE_ARTS_PROFILING "Turn on the profiling macro" OFF)
option (ENABLE_PCH "Turn on precomnpiled headers" OFF)
option (ENABLE_PCH "Turn on precompiled headers" ON)
option (ENABLE_CDISORT "Turn on cdisort" ON)
option (ENABLE_TMATRIX "Build the T-matrix solver (licensed for not-for-profit research)" OFF)
option (ENABLE_PYARTS_STUBS "Turn on python stub generation for pyarts library" ON)
Expand Down Expand Up @@ -190,7 +190,33 @@ endif (NO_ASSERT)
if (ENABLE_CCACHE)
find_program(CCACHE_FOUND ccache)
if (CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set (ARTS_CCACHE ${CCACHE_FOUND})
if (ENABLE_PCH)
# ccache refuses every compilation that uses a precompiled header unless
# its sloppiness includes pch_defines and time_macros. They are added to
# the sloppiness that ccache is configured with when CMake runs.
execute_process (COMMAND ${CCACHE_FOUND} --get-config sloppiness
OUTPUT_VARIABLE ARTS_CCACHE_SLOPPINESS
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
string (REPLACE " " "" ARTS_CCACHE_SLOPPINESS "${ARTS_CCACHE_SLOPPINESS}")
string (REPLACE "," ";" ARTS_CCACHE_SLOPPINESS "${ARTS_CCACHE_SLOPPINESS}")
list (APPEND ARTS_CCACHE_SLOPPINESS pch_defines time_macros)
list (REMOVE_DUPLICATES ARTS_CCACHE_SLOPPINESS)
list (JOIN ARTS_CCACHE_SLOPPINESS "," ARTS_CCACHE_SLOPPINESS)
set (ARTS_CCACHE ${CMAKE_COMMAND} -E env CCACHE_SLOPPINESS=${ARTS_CCACHE_SLOPPINESS} ${CCACHE_FOUND})
message (STATUS "ccache sloppiness for precompiled headers: ${ARTS_CCACHE_SLOPPINESS}")

# Clang writes the time stamps of the headers into a precompiled header,
# and then ccache cannot reuse it once a checkout has touched the headers
add_compile_options ("$<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:SHELL:-Xclang -fno-pch-timestamp>")
endif ()
# A launcher that the user has chosen is kept
if (NOT CMAKE_C_COMPILER_LAUNCHER)
set (CMAKE_C_COMPILER_LAUNCHER ${ARTS_CCACHE})
endif ()
if (NOT CMAKE_CXX_COMPILER_LAUNCHER)
set (CMAKE_CXX_COMPILER_LAUNCHER ${ARTS_CCACHE})
endif ()
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif (CCACHE_FOUND)
endif ()
Expand Down Expand Up @@ -612,10 +638,10 @@ endif()

if (ENABLE_PCH)
message (STATUS "Precompiled headers enabled")
if (CCACHE_FOUND)
message (STATUS "Make sure you set CCACHE_SLOPPINES properly for PCH:
export CCACHE_SLOPPINESS=pch_defines,time_macros")
endif()
# GCC compiles a file without a precompiled header that it cannot use, and
# only warns. As an error, it catches every target that the reuse set up by
# ArtsPrecompiledHeaders.cmake does not fit
add_compile_options ("$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Werror=invalid-pch>")
else()
message (STATUS "Precompiled headers disabled")
endif()
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"CMAKE_INSTALL_PREFIX": "$env{HOME}/arts_install",
"CMAKE_PREFIX_PATH": "$env{CONDA_PREFIX}",
"ENABLE_ARTS_LGPL": "0",
"ENABLE_PCH": "0",
"ENABLE_PCH": "1",
"FASTWIGNER": "0",
"NO_OPENMP": "0",
"SPHINX_JOBS": "auto",
Expand Down
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,6 @@ fast-but-accurate computations, some IEEE rules will be ignored. For now only
complex computations are IEEE incompatible running this mode of build.


Optional features
-----------------

Precompiled headers: PCH can speed up builds significantly. However, it hampers
the ability for ccache to properly skip unnecessary compilations, potentially
increasing rebuild times. Tests have shown that it only speeds up the build
considerably for Clang, but not for GCC.

```
cmake --preset=default-clang-conda -DENABLE_PCH=1 ..
```

If you enable PCH and also use ccache, you need to set the `CCACHE_SLOPPINESS`
environment variable properly:

```
export CCACHE_SLOPPINESS=pch_defines,time_macros
```


Disabling features
------------------

Expand All @@ -245,13 +225,27 @@ Disable OpenMP: `-DNO_OPENMP=1`

Disable the built-in documentation server: `-DNO_DOCSERVER=1`

Disable precompiled headers: `-DENABLE_PCH=0`. Most targets reuse one
precompiled `matpack.h`. A build without it checks that every file includes
what it uses.


ccache support
--------------

The build utilizes ccache automatically when available, it can be
turned off with the option `-DENABLE_CCACHE=0`

With precompiled headers, the build gives ccache the settings it needs for
them: `pch_defines,time_macros` is added to the sloppiness that ccache is
configured with when CMake runs, and Clang compiles with
`-Xclang -fno-pch-timestamp`. With GCC, ccache cannot help once the
precompiled header is rebuilt in an existing build directory, for instance
after a branch switch that touches a header that `matpack.h` includes: every
file that uses the precompiled header is then compiled again. If you often
switch between such branches in one build directory, `-DENABLE_PCH=0` may
rebuild faster.

For details see https://ccache.samba.org/


Expand Down
72 changes: 61 additions & 11 deletions cmake/modules/ArtsPrecompiledHeaders.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Precompiled headers, see the ENABLE_PCH option
#
# The matpack target precompiles matpack.h, which brings the standard library,
# the XML IO and the format helpers that nearly every ARTS translation unit
# needs. Every other target that links matpack, and has no precompiled header
# of its own, reuses that one instead of building its own copy.
# matpack.h brings the standard library, the XML IO and the format helpers that
# nearly every ARTS translation unit needs. The matpack target precompiles it,
# and every other target that links matpack reuses a precompiled copy instead
# of building its own.
#
# A precompiled header can only be used with the compiler options it was made
# with. Libraries are compiled as position-independent code (-fPIC) and
# executables as position-independent executables (-fPIE), so a library's copy
# is of no use to an executable: Clang refuses it and GCC cannot use it.
# Executables therefore reuse a second copy, made by the empty executable
# matpack_pch_executable.
#
# A target is left alone if it
# - has a precompiled header of its own,
# - has other compile options than the target that makes its copy, or
# - sets DISABLE_PRECOMPILE_HEADERS, because forcing matpack.h on its sources
# costs more than it saves.
#
# GCC reports a reused header that it cannot use as an error (-Werror=invalid-pch,
# see CMakeLists.txt). That also catches a compile definition of a target that
# changes what matpack.h means, which Clang and MSVC do not check.

#! Whether target links matpack, directly or through the targets it links
function(arts_links_matpack target result)
Expand Down Expand Up @@ -43,22 +60,55 @@ function(arts_targets_in dir out)
set(${out} ${targets} PARENT_SCOPE)
endfunction()

#! Let every target below dir that links matpack reuse its precompiled matpack.h
#! The compile options of target, empty if it has none
function(arts_compile_options target out)
get_target_property(options ${target} COMPILE_OPTIONS)
if(NOT options)
set(options "")
endif()
set(${out} "${options}" PARENT_SCOPE)
endfunction()

#! Let every target below dir that links matpack reuse a precompiled matpack.h
function(arts_reuse_matpack_pch dir)
set(main ${CMAKE_CURRENT_BINARY_DIR}/matpack_pch_executable.cc)
file(CONFIGURE OUTPUT ${main} CONTENT "int main() { return 0; }\n")
add_executable(matpack_pch_executable EXCLUDE_FROM_ALL ${main})
target_link_libraries(matpack_pch_executable PRIVATE matpack)
target_precompile_headers(matpack_pch_executable PRIVATE <matpack.h>)

arts_compile_options(matpack library_options)
arts_compile_options(matpack_pch_executable executable_options)

set(library_count 0)
set(executable_count 0)
arts_targets_in(${dir} targets)
set(users)
foreach(t IN LISTS targets)
get_target_property(type ${t} TYPE)
if(type STREQUAL "EXECUTABLE")
set(kind executable)
set(source matpack_pch_executable)
elseif(type MATCHES "^(STATIC|OBJECT|SHARED|MODULE)_LIBRARY$")
set(kind library)
set(source matpack)
else()
continue()
endif()

get_target_property(own ${t} PRECOMPILE_HEADERS)
if(t STREQUAL "matpack" OR own OR NOT type MATCHES "^(STATIC_LIBRARY|OBJECT_LIBRARY|SHARED_LIBRARY|MODULE_LIBRARY|EXECUTABLE)$")
get_target_property(reused ${t} PRECOMPILE_HEADERS_REUSE_FROM)
get_target_property(disabled ${t} DISABLE_PRECOMPILE_HEADERS)
arts_compile_options(${t} options)
if(own OR reused OR disabled OR NOT "${options}" STREQUAL "${${kind}_options}")
continue()
endif()

arts_links_matpack(${t} links)
if(links)
target_precompile_headers(${t} REUSE_FROM matpack)
list(APPEND users ${t})
target_precompile_headers(${t} REUSE_FROM ${source})
math(EXPR ${kind}_count "${${kind}_count} + 1")
endif()
endforeach()
list(LENGTH users n)
message(STATUS "Targets reusing the precompiled matpack.h: ${n}")
message(STATUS "Targets reusing the precompiled matpack.h: "
"${library_count} libraries, ${executable_count} executables")
endfunction()
39 changes: 35 additions & 4 deletions doc/arts/dev.classes.formatter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ This has some main uses:
more consistent. And it allows us to specialize the formatting for each
object using the python ``__format__`` interface to control some aspects of
how the output looks.
2. Calls to ``std::format`` inside the C++ code can be used to format strings
2. Calls to ``arts::format`` inside the C++ code can be used to format strings
consistently. This is especially useful for logging and debugging,
and our experience using it in XML IO code with ``std::print`` shows
that it is significantly faster than the old stream-based solution to
and our experience using it in XML IO code shows that it is
significantly faster than the old stream-based solution to
text manipulation.

How is it implemented?
----------------------

Most ARTS classes should provide a ``std::formatter<T>`` specialization to allow
formatting of the class using ``std::format`` and its ilks.
formatting of the class using ``arts::format``, ``std::format`` and their ilks.
The only exceptions are those groups that are marked as ``value_type`` in the
workspace groups definition.

Expand Down Expand Up @@ -105,6 +105,37 @@ If your class's ``MetaData`` is ``format_tags tags;`` then the following is the
The above ensures that there exists at most one ``tags`` object for each class, and that
the ``tags`` object is the only object that needs to be passed around to ensure compatibility.

Formatting in the C++ code
--------------------------

Use ``arts::format`` instead of ``std::format`` to create strings in the C++
code, and ``arts::print`` and ``arts::println`` instead of ``std::print`` and
``std::println`` to write them to ``stdout``, a ``std::FILE*`` or a
``std::ostream``. They take the same arguments and check the format string at
compile time in the same way:

.. code-block:: cpp
:caption: Formatting in the C++ code

const std::string s = arts::format("{} has {} elements", name, v.size());
arts::println(os, "{:IO}", v);

These format with ``arts::vformat`` and ``arts::vprint``, and
``arts::vformat_to`` writes to a ``std::format_context``. All are compiled
once, in ``format_tags.cpp``. ``std::format``, ``std::vformat``,
``std::format_to``, ``std::print`` and ``std::println`` instead compile the
whole formatting machinery of the standard library in every file that calls
them, which makes the build considerably slower.

Write formatted text to a stream with ``arts::print(os, ...)`` or
``arts::println(os, ...)``, not with ``os << arts::format(...)``.

Inside a formatter, ``tags.format`` writes builtin values, such as ``Numeric``
and ``Index``, with ``format_tags::single_format``. These have specializations
for ``std::format_context`` in ``format_tags.h`` that are also compiled once.
A formatter that writes another builtin type, such as ``float``, should add a
specialization for it next to the others.

What formatter options are available?
-------------------------------------

Expand Down
18 changes: 10 additions & 8 deletions doc/arts/dev.error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ for the message outside of the `if`.
A message without arguments is used as it is, so braces in it are printed
as they are written.

Each call with arguments formats its message with `std::format`. Prefer
making these calls in source files rather than in header files, as they
increase compilation times.
Each call with arguments formats its message with `arts::vformat` (see
:doc:`dev.classes.formatter`). Prefer making these calls in source files
rather than in header files, as a call in a header is compiled again in every
file that includes it.

ARTS_METHOD_ERROR_CATCH
-----------------------
Expand All @@ -79,15 +80,16 @@ functions that failed, followed by the error:
Guide to a valid use of the formatting string
---------------------------------------------

The formatting string should be a valid format string for the `std::format`
function. This means that you need to use `{}` for each of the arguments that
you want to insert. You may add extra formatting options to the `{}`, these
depend on the type you are formatting.
The formatting string should be a valid format string for the `arts::format`
function, which takes the same format strings as `std::format`. This means
that you need to use `{}` for each of the arguments that you want to insert.
You may add extra formatting options to the `{}`, these depend on the type you
are formatting.

The formatting string of `arts::user_error` must use every argument. A call
with an argument that the string does not use does not compile.

.. code-block:: c++

std::format("I have {} apple(s), {} orange(s), and {} argument(s)", 1, 2, 3);
arts::format("I have {} apple(s), {} orange(s), and {} argument(s)", 1, 2, 3);
// Should give the string: "I have 1 apple(s), 2 orange(s), and 3 argument(s)"
2 changes: 1 addition & 1 deletion src/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void CallbackOperator::operator()(Workspace& ws_in) const try {
// so its value is assigned to the output of the calling workspace
for (auto& n : outputs) ws_in.share(n).assign(ws.share(n));
} catch (std::exception& e) {
throw std::runtime_error(std::format("Error in callback operator:\n{}\n{}", *this, e.what()));
throw std::runtime_error(arts::format("Error in callback operator:\n{}\n{}", *this, e.what()));
}

void xml_io_stream<CallbackOperator>::write(std::ostream& os,
Expand Down
Loading
Loading