From 1721031322b7e51137796f9816b82353b3222286 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Wed, 15 Oct 2025 12:03:03 -0600 Subject: [PATCH] :art: Provide better control over freestanding Problem: - The `SIMULATE_FREESTANDING` macro applies to multiple repos. Sometimes we might want finer-grained control over which elements of which repos are freestanding-compatible, because toolchains differ in what they support. Solution: - Introduce `STDX_FREESTANDING` for this repo control. - Defining `SIMULATE_FREESTANDING` will also control `STDX_FREESTANDING`. --- CMakeLists.txt | 1 + include/stdx/detail/fmt.hpp | 8 +++++++- include/stdx/detail/freestanding.hpp | 5 +++++ test/CMakeLists.txt | 2 +- test/ct_format.cpp | 2 +- usage_test/CMakeLists.txt | 2 +- 6 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 include/stdx/detail/freestanding.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 174be64..c91fa2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ target_sources( include/stdx/cx_vector.hpp include/stdx/detail/bitset_common.hpp include/stdx/detail/fmt.hpp + include/stdx/detail/freestanding.hpp include/stdx/detail/list_common.hpp include/stdx/env.hpp include/stdx/for_each_n_args.hpp diff --git a/include/stdx/detail/fmt.hpp b/include/stdx/detail/fmt.hpp index 3d2abe4..fd6172a 100644 --- a/include/stdx/detail/fmt.hpp +++ b/include/stdx/detail/fmt.hpp @@ -1,6 +1,10 @@ #pragma once -#if not STDX_FMT_FREESTANDING +#include + +// NOLINTBEGIN(cppcoreguidelines-macro-usage) + +#ifndef STDX_FREESTANDING #include #include @@ -172,3 +176,5 @@ CONSTEVAL auto format_to(It dest, auto fmtstr, auto v) -> void { } // namespace stdx #endif + +// NOLINTEND(cppcoreguidelines-macro-usage) diff --git a/include/stdx/detail/freestanding.hpp b/include/stdx/detail/freestanding.hpp new file mode 100644 index 0000000..d113323 --- /dev/null +++ b/include/stdx/detail/freestanding.hpp @@ -0,0 +1,5 @@ +#pragma once + +#ifdef SIMULATE_FREESTANDING +#define STDX_FREESTANDING +#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2bef2bf..1d888ca 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -95,7 +95,7 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20) warnings stdx) target_compile_definitions(ct_format_freestanding_test - PRIVATE -DSTDX_FMT_FREESTANDING=1) + PRIVATE STDX_FREESTANDING) endif() add_subdirectory(fail) diff --git a/test/ct_format.cpp b/test/ct_format.cpp index af34730..5876da8 100644 --- a/test/ct_format.cpp +++ b/test/ct_format.cpp @@ -106,7 +106,7 @@ TEST_CASE("format a compile-time argument with different base", "[ct_format]") { "Hello 0x2a"_fmt_res); } -#if not STDX_FMT_FREESTANDING +#ifndef STDX_FREESTANDING TEST_CASE("format a compile-time argument with fmt spec", "[ct_format]") { STATIC_REQUIRE(stdx::ct_format<"Hello {:*>#6x}">(CX_VALUE(42)) == "Hello **0x2a"_fmt_res); diff --git a/usage_test/CMakeLists.txt b/usage_test/CMakeLists.txt index d490908..0ff84f4 100644 --- a/usage_test/CMakeLists.txt +++ b/usage_test/CMakeLists.txt @@ -12,6 +12,6 @@ cpmaddpackage(NAME stdx SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/.." GIT_TAG HEAD) add_executable(app main.cpp) target_link_libraries(app PRIVATE stdx) if(CPP_IMPLEMENTATION STREQUAL "FREESTANDING") - target_compile_definitions(app PRIVATE STDX_FMT_FREESTANDING) + target_compile_definitions(app PRIVATE SIMULATE_FREESTANDING) target_compile_options(app PRIVATE -ffreestanding) endif()