diff --git a/BUILD.gn b/BUILD.gn index d4034a1023..f5d2f7e4ff 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -319,6 +319,17 @@ group("cpp14_compatibility") { ] } +# Build Pigweed with -std=c++20 to ensure compatibility. Compile with +# optimizations since the compiler tends to catch more errors with optimizations +# enabled than without. +group("cpp20_compatibility") { + _cpp20_tc = "$_internal_toolchains:pw_strict_host_clang_size_optimized_cpp20" + deps = [ + ":apps($_cpp20_tc)", + ":pw_module_tests.run($_cpp20_tc)", + ] +} + # The default toolchain is not used for compiling C/C++ code. if (current_toolchain != default_toolchain) { group("apps") { @@ -335,7 +346,7 @@ if (current_toolchain != default_toolchain) { } } - # All Pigweed modules that can be built using gn. This is not built by default. + # All Pigweed modules that can be built using gn. Not built by default. group("pw_modules") { deps = pw_modules } diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn index 1a74c2badd..025f17a28c 100644 --- a/pw_build/BUILD.gn +++ b/pw_build/BUILD.gn @@ -127,6 +127,13 @@ config("cpp17") { ] } +config("cpp20") { + cflags_cc = [ + "-std=c++20", + "-Wno-register", + ] +} + # Removes system-dependent prefixes from macros like __FILE__ and debug symbols. config("relative_paths") { _transformations = [ diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py index 5103ffd8f4..bb417c3fae 100755 --- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py +++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py @@ -110,9 +110,10 @@ def gn_full_build_check(ctx: PresubmitContext) -> None: 'pw_env_setup:build_pigweed_python_source_tree', ] - # TODO(b/234645359): Re-enable on Windows when cpp14_compatibility builds. + # TODO(b/234645359): Re-enable on Windows when compatibility tests build. if sys.platform != 'win32': build_targets.append('cpp14_compatibility') + build_targets.append('cpp20_compatibility') build.gn_gen(ctx.root, ctx.output_dir) build.ninja(ctx.output_dir, *build_targets) diff --git a/targets/host/target_docs.rst b/targets/host/target_docs.rst index f350e71dec..a9b86ca3ed 100644 --- a/targets/host/target_docs.rst +++ b/targets/host/target_docs.rst @@ -49,11 +49,15 @@ downstream projects. Toolchains for other C++ standards ================================== -Pigweed code requires C++17. A few modules, such as ``pw_tokenizer``, work with -C++14. To facilitate testing these modules with C++14, Pigweed uses the -``pw_strict_host_clang_debug_cpp14`` toolchain. This toolchain is only permitted -for use in upstream pigweed, but downstream users may create a similar toolchain -if needed. +Most Pigweed code requires C++17, but a few modules, such as ``pw_tokenizer``, +work with C++14. All Pigweed code is compatible with C++20. Pigweed defines +toolchains for testing with C++14 and C++20. + +* ``pw_strict_host_clang_debug_cpp14`` -- Builds with ``-std=c++14``. +* ``pw_strict_host_clang_size_optimized_cpp20`` -- Builds with ``-std=c++20``. + +These toolchains are only permitted for use in upstream pigweed, but downstream +users may create similar toolchains as needed. -------- Building diff --git a/targets/host/target_toolchains.gni b/targets/host/target_toolchains.gni index f7ead4ce32..4a4dd6b897 100644 --- a/targets/host/target_toolchains.gni +++ b/targets/host/target_toolchains.gni @@ -394,4 +394,19 @@ pw_internal_host_toolchains = [ pw_unit_test_MAIN = "$dir_pw_unit_test:printf_main" } }, + { + name = "pw_strict_host_clang_size_optimized_cpp20" + _toolchain_base = pw_toolchain_host_clang.size_optimized + forward_variables_from(_toolchain_base, "*", _excluded_members) + defaults = { + forward_variables_from(_toolchain_base.defaults, "*") + forward_variables_from(_host_common, "*") + forward_variables_from(_pigweed_internal, "*") + forward_variables_from(_os_specific_config, "*") + + remove_default_configs = [ "$dir_pw_build:cpp17" ] + default_configs += _clang_default_configs + default_configs += [ "$dir_pw_build:cpp20" ] + } + }, ]