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

Check for std::format support in CMake #891

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stephenswat
Copy link
Member

Today I tried building traccc with g++12 which broke as #845 introduced a dependency on std::format which is not universally available. In order to provide earlier and more readable errors, this commit adds a configuration-time check to ensure that the C++ compiler supports std::format.

Today I tried building traccc with g++12 which broke as acts-project#845 introduced
a dependency on `std::format` which is not universally available. In
order to provide earlier and more readable errors, this commit adds a
configuration-time check to ensure that the C++ compiler supports
`std::format`.
@stephenswat stephenswat added the build This relates to the build system label Feb 25, 2025
@stephenswat stephenswat requested a review from krasznaa February 25, 2025 10:16
Copy link
Member

@krasznaa krasznaa left a comment

Choose a reason for hiding this comment

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

Let's indeed add such code. Just in a slightly higher level way. 😉

Comment on lines +73 to +87
message(STATUS "Checking for `std::format` support")
file( WRITE
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/compile_test_format.cpp"
"#include <format>\n"
"int main() {\n"
"std::format(\"Hello {}\\n\", \"world\");\n"
"return 0; }\n" )
try_compile( TRACCCC_INTERNAL_STD_FORMAT_WORKS SOURCES
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/compile_test_format.cpp")

if (NOT TRACCCC_INTERNAL_STD_FORMAT_WORKS)
message(FATAL_ERROR "Examples are enabled, but `std::format` is not supported by the C++ compiler; upgrade to gcc 13 or clang 14 or higher.")
endif()

unset(TRACCCC_INTERNAL_STD_FORMAT_WORKS)
Copy link
Member

Choose a reason for hiding this comment

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

No need to write such low level code. We have check_cxx_source_compiles(...) for this. Which is used extensively in vecmem for instance. (https://github.com/acts-project/vecmem/blob/main/core/CMakeLists.txt#L208)

I'd propose:

Suggested change
message(STATUS "Checking for `std::format` support")
file( WRITE
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/compile_test_format.cpp"
"#include <format>\n"
"int main() {\n"
"std::format(\"Hello {}\\n\", \"world\");\n"
"return 0; }\n" )
try_compile( TRACCCC_INTERNAL_STD_FORMAT_WORKS SOURCES
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/compile_test_format.cpp")
if (NOT TRACCCC_INTERNAL_STD_FORMAT_WORKS)
message(FATAL_ERROR "Examples are enabled, but `std::format` is not supported by the C++ compiler; upgrade to gcc 13 or clang 14 or higher.")
endif()
unset(TRACCCC_INTERNAL_STD_FORMAT_WORKS)
include(CheckCxxSourceCompiles)
check_cxx_source_compiles( "
#include <format>
int main() {
std::format(\"Hello {}\\n\", \"world\");
}
" TRACCC_STD_FORMAT_WORKS )
if( NOT TRACCC_STD_FORMAT_WORKS )
message(FATAL_ERROR "Examples are enabled, but `std::format` is not supported by the C++ compiler; upgrade to gcc 13 or clang 14 or higher.")
endif()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build This relates to the build system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants