Skip to content

Commit

Permalink
Add C++20 audit (#2688)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
niranjanyardi and andrewsavage1 committed Mar 26, 2024
1 parent aecd26d commit 3f45e5c
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 13 deletions.
1 change: 0 additions & 1 deletion starboard/android/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 48 additions & 4 deletions starboard/build/config/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down Expand Up @@ -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" ]
}
}
}
Expand Down Expand Up @@ -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" ]
}
}
}
3 changes: 3 additions & 0 deletions starboard/build/config/base_configuration.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions starboard/build/config/mac/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
5 changes: 1 addition & 4 deletions starboard/build/config/win/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
1 change: 0 additions & 1 deletion starboard/linux/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down
4 changes: 4 additions & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions starboard/nplb/compiler_compliance/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}
}
65 changes: 65 additions & 0 deletions starboard/nplb/compiler_compliance/cpp20_support.cc
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <memory>
#include <numeric>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

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<char> 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<std::uint32_t>::max();
std::uint32_t b = std::numeric_limits<std::uint32_t>::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
1 change: 0 additions & 1 deletion starboard/stub/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ config("platform_configuration") {
configs = [ "//starboard/build/config/sabi" ]
cflags = []

cflags_cc = [ "-std=gnu++17" ]
ldflags = [ "-static-libstdc++" ]

libs = [ "pthread" ]
Expand Down

0 comments on commit 3f45e5c

Please sign in to comment.