From 3f45e5c7b116b592a6fb195a1f37db34d3eb5f04 Mon Sep 17 00:00:00 2001 From: Niranjan Yardi Date: Mon, 25 Mar 2024 17:13:30 -0700 Subject: [PATCH] Add C++20 audit (#2688) b/309847996 b/329683364 Refactor c++17 flags from all platforms used while building cobalt into a central place "default_cpp_standard" in starboard/build/config/BUILD.gn Remove default config while compiling c++20 audit, add "cpp20_supported_config" in it's place. Co-author: andrewsavage1 --------- Co-authored-by: Andrew Savage --- .../shared/platform_configuration/BUILD.gn | 1 - starboard/build/config/BUILD.gn | 52 +++++++++++++-- starboard/build/config/base_configuration.gni | 3 + starboard/build/config/mac/BUILD.gn | 2 - starboard/build/config/win/BUILD.gn | 5 +- .../shared/platform_configuration/BUILD.gn | 1 - starboard/nplb/BUILD.gn | 4 ++ starboard/nplb/compiler_compliance/BUILD.gn | 17 +++++ .../nplb/compiler_compliance/cpp20_support.cc | 65 +++++++++++++++++++ .../stub/platform_configuration/BUILD.gn | 1 - 10 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 starboard/nplb/compiler_compliance/cpp20_support.cc diff --git a/starboard/android/shared/platform_configuration/BUILD.gn b/starboard/android/shared/platform_configuration/BUILD.gn index 6178e4ad7bc3..1c626909f9f6 100644 --- a/starboard/android/shared/platform_configuration/BUILD.gn +++ b/starboard/android/shared/platform_configuration/BUILD.gn @@ -70,7 +70,6 @@ config("platform_configuration") { defines += [ "THREAD_SANITIZER" ] } - cflags_cc = [ "-std=c++17" ] cflags += [ # libwebp uses the cpufeatures library to detect ARM NEON support "-I${android_ndk_path}/sources/android/cpufeatures", diff --git a/starboard/build/config/BUILD.gn b/starboard/build/config/BUILD.gn index 67ce8e9ad17d..79a31096b6ca 100644 --- a/starboard/build/config/BUILD.gn +++ b/starboard/build/config/BUILD.gn @@ -13,6 +13,7 @@ # limitations under the License. import("//build/config/compiler/compiler.gni") +import("//starboard/build/config/os_definitions.gni") config("base") { defines = [ "USE_COBALT_CUSTOMIZATIONS" ] @@ -84,7 +85,11 @@ config("host") { cflags = [ "/wd4267", # Possible loss of precision from size_t to a smaller type. "/wd4715", # Not all control paths return value. + "/std:c++17", ] + } else if (is_apple) { + cflags_cc = [ "-std=gnu++17" ] + cflags_objcc = [ "-std=gnu++17" ] } } } @@ -233,10 +238,49 @@ config("default_compiler_flags") { } } -# TODO: b/329683364 - Move other flags setting the c++ standard from each -# platform's platform_configuration to this config. +# This config is intended to encapsulate all platform-specific logic for selecting the C++ standard +# version in shared code while building with the default toolchain. +# Starboard platform-specific build code should not attempt to overload this. config("default_cpp_standard") { - if (sb_is_modular && current_toolchain == cobalt_toolchain) { - cflags_cc = [ "-std=c++17" ] + if (current_toolchain == default_toolchain) { + if (sb_is_modular || is_android) { + cflags_cc = [ "-std=c++17" ] + } + + # These flags apply to non-modular windows platforms which includes win32, xb1. + if (is_host_win && !sb_is_modular) { + cflags = [ "/std:c++17" ] + } + + if (is_linux && !sb_is_modular) { + cflags_cc = [ "-std=gnu++17" ] + } + + if (is_apple) { + cflags_cc = [ "-std=gnu++17" ] + cflags_objcc = [ "-std=gnu++17" ] + } + } +} + +config("cpp20_supported_config") { + if (current_toolchain == default_toolchain) { + if (sb_is_modular || is_android) { + cflags_cc = [ "-std=c++20" ] + } + + # These flags apply to non-modular windows platforms which includes win32, xb1. + if (is_host_win && !sb_is_modular) { + cflags = [ "/std:c++20" ] + } + + if (is_linux && !sb_is_modular) { + cflags_cc = [ "-std=gnu++20" ] + } + + if (is_apple) { + cflags_cc = [ "-std=gnu++20" ] + cflags_objcc = [ "-std=gnu++20" ] + } } } diff --git a/starboard/build/config/base_configuration.gni b/starboard/build/config/base_configuration.gni index 29761b5993d8..8f71a1a45d1d 100644 --- a/starboard/build/config/base_configuration.gni +++ b/starboard/build/config/base_configuration.gni @@ -158,6 +158,9 @@ declare_args() { # Enables an NPLB audit of C++17 support. sb_enable_cpp17_audit = true + # Enables an NPLB audit of C++20 support. + sb_enable_cpp20_audit = true + # Enable when using clang 16. is_clang_16 = false diff --git a/starboard/build/config/mac/BUILD.gn b/starboard/build/config/mac/BUILD.gn index c69eaf2a32f4..6bf4ccfcf191 100644 --- a/starboard/build/config/mac/BUILD.gn +++ b/starboard/build/config/mac/BUILD.gn @@ -43,8 +43,6 @@ config("host") { config("common") { arflags = [ "-no_warning_for_no_symbols" ] - cflags_cc = [ "-std=gnu++17" ] - cflags_objcc = [ "-std=gnu++17" ] cflags = [ "-fno-common" ] asmflags = [ "-fno-common" ] ldflags = [ "-fno-common" ] diff --git a/starboard/build/config/win/BUILD.gn b/starboard/build/config/win/BUILD.gn index 98356fc5e0c9..58ebf14bb234 100644 --- a/starboard/build/config/win/BUILD.gn +++ b/starboard/build/config/win/BUILD.gn @@ -45,10 +45,7 @@ config("common") { "$wdk_include_path/winrt", "$msvc_path/include", ] - cflags += [ - "/EHsc", - "/std:c++17", - ] + cflags += [ "/EHsc" ] # msvs_debug/_devel/etc ldflags += [ "/INCREMENTAL:NO" ] diff --git a/starboard/linux/shared/platform_configuration/BUILD.gn b/starboard/linux/shared/platform_configuration/BUILD.gn index 00ba19ec68dc..08ebbe868ad8 100644 --- a/starboard/linux/shared/platform_configuration/BUILD.gn +++ b/starboard/linux/shared/platform_configuration/BUILD.gn @@ -98,7 +98,6 @@ config("compiler_flags") { "MESA_EGL_NO_X11_HEADERS", ] cflags_c += [ "-std=c11" ] - cflags_cc = [ "-std=gnu++17" ] if (use_asan) { cflags += [ diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 868e8b6c66d8..a01b3d47c31c 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -291,6 +291,10 @@ target(gtest_target_type, "nplb") { deps += [ "//starboard/nplb/compiler_compliance:cpp17_supported" ] } + if (sb_enable_cpp20_audit) { + deps += [ "//starboard/nplb/compiler_compliance:cpp20_supported" ] + } + data_deps = [ "//cobalt/network:copy_ssl_certificates", "//starboard/nplb/testdata/file_tests:nplb_file_tests_data", diff --git a/starboard/nplb/compiler_compliance/BUILD.gn b/starboard/nplb/compiler_compliance/BUILD.gn index e3638c1436b9..38dcd9fea97a 100644 --- a/starboard/nplb/compiler_compliance/BUILD.gn +++ b/starboard/nplb/compiler_compliance/BUILD.gn @@ -40,3 +40,20 @@ if (sb_enable_cpp17_audit) { cflags_cc = [ "-std=c++17" ] } } + +if (sb_enable_cpp20_audit) { + static_library("cpp20_supported") { + sources = [ "cpp20_support.cc" ] + deps = [ + ":cpp20_supported_config_shim", + "//starboard:starboard_group", + ] + configs -= [ "//starboard/build/config:default_cpp_standard" ] + } + + # We do this to ensure the -std=c++20 flag is added after any other -std flag + # so it overrides any other one. + group("cpp20_supported_config_shim") { + public_configs = [ "//starboard/build/config:cpp20_supported_config" ] + } +} diff --git a/starboard/nplb/compiler_compliance/cpp20_support.cc b/starboard/nplb/compiler_compliance/cpp20_support.cc new file mode 100644 index 000000000000..e3c1cd9d80c6 --- /dev/null +++ b/starboard/nplb/compiler_compliance/cpp20_support.cc @@ -0,0 +1,65 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the aLicense is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include +#include +#include + +namespace starboard { +namespace nplb { +namespace compiler_compliance { +namespace { + +// These examples are taken after referring to +// 1) C++20 allowlist from chromium m114 milestone branch : +// https://chromium.googlesource.com/chromium/src/+/refs/branch-heads/5735/styleguide/c++/c++-features.md +// 2) cpp reference : https://en.cppreference.com/w/cpp + +// Test std::string ends_with support +void test_string_ends_with() { + bool result = std::string("foobar").ends_with("bar"); +} + +// Test std::erase_if support +void test_erase_if() { + std::vector cnt(10); + std::iota(cnt.begin(), cnt.end(), '0'); + std::erase(cnt, '3'); + std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; }); +} + +// Test std::midpoint support +void test_midpoint() { + std::uint32_t a = std::numeric_limits::max(); + std::uint32_t b = std::numeric_limits::max() - 2; + std::midpoint(a, b); +} + +// Test designated initializers +void test_designated_initializer() { + struct S { + int x = 1; + int y = 2; + }; + S s{.y = 3}; // OK, s.x == 1, s.y == 3 +} + +} // namespace +} // namespace compiler_compliance +} // namespace nplb +} // namespace starboard diff --git a/starboard/stub/platform_configuration/BUILD.gn b/starboard/stub/platform_configuration/BUILD.gn index d3804a6280e1..7d5b53f44ecc 100644 --- a/starboard/stub/platform_configuration/BUILD.gn +++ b/starboard/stub/platform_configuration/BUILD.gn @@ -16,7 +16,6 @@ config("platform_configuration") { configs = [ "//starboard/build/config/sabi" ] cflags = [] - cflags_cc = [ "-std=gnu++17" ] ldflags = [ "-static-libstdc++" ] libs = [ "pthread" ]