Skip to content

Commit

Permalink
[build] Build the standard c++ library from source.
Browse files Browse the repository at this point in the history
This allows it to be instrumented by the sanitizers.

Enabled only for MSAN and for Android.

Don't pick up Flutter's including no_exceptions in the default config set.

TEST=ci
Bug: #44312
Bug: #44377
Bug: #43075
Bug: #50248
Bug: #50271
Bug: #52441
Change-Id: If01704ff29569fba8f8181ed31d52faba8d8370f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304824
Reviewed-by: Alexander Aprelev <[email protected]>
Commit-Queue: Ryan Macnak <[email protected]>
  • Loading branch information
rmacnak-google authored and Commit Queue committed May 22, 2023
1 parent c3e815b commit ddc11f8
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 16 deletions.
10 changes: 10 additions & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allowed_hosts = [
'dart.googlesource.com',
'dart-internal.googlesource.com',
'fuchsia.googlesource.com',
'llvm.googlesource.com',
]

vars = {
Expand All @@ -42,6 +43,7 @@ vars = {
# Chromium git
"chromium_git": "https://chromium.googlesource.com",
"fuchsia_git": "https://fuchsia.googlesource.com",
"llvm_git": "https://llvm.googlesource.com",

# Checked-in SDK version. The checked-in SDK is a Dart SDK distribution in a
# cipd package used to run Dart scripts in the build and test infrastructure,
Expand Down Expand Up @@ -95,6 +97,8 @@ vars = {
"devtools_rev": "026f0adf03725fbab24d601ac74c811808f258e5",
"icu_rev": "81d656878ec611cb0b42d52c82e9dae93920d9ba",
"jinja2_rev": "2222b31554f03e62600cd7e383376a7c187967a1",
"libcxx_rev": "44079a4cc04cdeffb9cfe8067bfb3c276fb2bab0",
"libcxxabi_rev": "2ce528fb5e0f92e57c97ec3ff53b75359d33af12",
"libprotobuf_rev": "24487dd1045c7f3d64a21f38a3f0c06cc4cf2edb",
"markupsafe_rev": "8f45f5cfa0009d2a70589bcda0349b8cb2b72783",
"perfetto_rev": "b8da07095979310818f0efde2ef3c69ea70d62c5",
Expand Down Expand Up @@ -275,6 +279,12 @@ deps = {
Var("chromium_git") + "/chromium/src/third_party/zlib.git" +
"@" + Var("zlib_rev"),

Var("dart_root") + "/third_party/libcxx":
Var("llvm_git") + "/llvm-project/libcxx" + "@" + Var("libcxx_rev"),

Var("dart_root") + "/third_party/libcxxabi":
Var("llvm_git") + "/llvm-project/libcxxabi" + "@" + Var("libcxxabi_rev"),

Var("dart_root") + "/third_party/boringssl":
Var("dart_git") + "boringssl_gen.git" + "@" + Var("boringssl_gen_rev"),
Var("dart_root") + "/third_party/boringssl/src":
Expand Down
36 changes: 36 additions & 0 deletions build/config/BUILDCONFIG.gn
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ if (!is_clang && (is_asan || is_lsan || is_msan || is_tsan || is_ubsan)) {
is_clang = true
}

use_flutter_cxx = is_msan || is_android

# =============================================================================
# TARGET DEFAULTS
# =============================================================================
Expand All @@ -235,6 +237,7 @@ if (!is_clang && (is_asan || is_lsan || is_msan || is_tsan || is_ubsan)) {
# duplication in each target below.
_native_compiler_configs = [
"//build/config/compiler:compiler",
"//build/config/compiler:cxx_version_default",
"//build/config/compiler:clang_stackrealign",
"//build/config/compiler:compiler_arm_fpu",
"//build/config/compiler:compiler_arm_thumb",
Expand All @@ -243,6 +246,14 @@ _native_compiler_configs = [
"//build/config/compiler:no_rtti",
"//build/config/compiler:runtime_library",
]

if (use_flutter_cxx) {
_native_compiler_configs += [
"//third_party/libcxxabi:libcxxabi_config",
"//third_party/libcxx:libcxx_config",
]
}

if (is_win) {
_native_compiler_configs += [
"//build/config/win:lean_and_mean",
Expand Down Expand Up @@ -419,6 +430,31 @@ if (is_win) {
}
}

# Sets default dependencies for executable and shared_library targets.
#
# Variables
# no_default_deps: If true, no standard dependencies will be added.
if (use_flutter_cxx) {
foreach(_target_type,
[
"executable",
"loadable_module",
"shared_library",
]) {
template(_target_type) {
target(_target_type, target_name) {
forward_variables_from(invoker, "*", [ "no_default_deps" ])
if (!defined(deps)) {
deps = []
}
if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
deps += [ "//third_party/libcxx" ]
}
}
}
}
}

# ==============================================================================
# COMPONENT SETUP
# ==============================================================================
Expand Down
9 changes: 9 additions & 0 deletions build/config/c++/c++.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

declare_args() {
# Use libc++ (buildtools/third_party/libc++ and
# buildtools/third_party/libc++abi) instead of stdlibc++ as standard library.
use_custom_libcxx = false
}
86 changes: 84 additions & 2 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (is_win) {
import("//build/config/win/visual_studio_version.gni")
}

import("//build/config/c++/c++.gni")
import("//build/toolchain/ccache.gni")

# default_include_dirs ---------------------------------------------------------
Expand Down Expand Up @@ -135,6 +136,14 @@ config("compiler") {
cflags += [ "-fsanitize=undefined" ]
ldflags += [ "-fsanitize=undefined" ]
}

if (use_custom_libcxx) {
cflags_cc += [ "-nostdinc++" ]
include_dirs = [
"//buildtools/third_party/libc++/trunk/include",
"//buildtools/third_party/libc++abi/trunk/include",
]
}
}

if (is_clang && is_debug) {
Expand Down Expand Up @@ -250,8 +259,19 @@ config("compiler") {
}

if (is_android || is_linux || is_mac || is_fuchsia) {
cflags += [ "-fPIE" ]
ldflags += [ "-fPIE" ]
if (use_flutter_cxx) {
# shared_library_config isn't transitive, so we don't automatically get
# another versions of libcxx with and without -fPIC. Properly setting this
# up so only shard libraries pay the extra cost fPIC over fPIE means
# something like separate GN toolchains like in the Fuchsia GN build. For
# now, globally enabling fPIC is okay because it is limited to test-only
# builds.
cflags += [ "-fPIC" ]
ldflags += [ "-fPIC" ]
} else {
cflags += [ "-fPIE" ]
ldflags += [ "-fPIE" ]
}
}

# Linux-specific compiler flags setup.
Expand Down Expand Up @@ -418,6 +438,56 @@ config("compiler") {
}
}

config("cxx_version_default") {
if (is_win) {
cc_std = [ "/std:c++17" ]
} else {
cc_std = [ "-std=c++17" ]
}
cflags_cc = cc_std
cflags_objcc = cc_std
}

config("cxx_version_11") {
if (is_win) {
cc_std = [ "/std:c++11" ]
} else {
cc_std = [ "-std=c++11" ]
}
cflags_cc = cc_std
cflags_objcc = cc_std
}

config("cxx_version_14") {
if (is_win) {
cc_std = [ "/std:c++14" ]
} else {
cc_std = [ "-std=c++14" ]
}
cflags_cc = cc_std
cflags_objcc = cc_std
}

config("cxx_version_17") {
if (is_win) {
cc_std = [ "/std:c++17" ]
} else {
cc_std = [ "-std=c++17" ]
}
cflags_cc = cc_std
cflags_objcc = cc_std
}

config("cxx_version_20") {
if (is_win) {
cc_std = [ "/std:c++20" ]
} else {
cc_std = [ "-std=c++20" ]
}
cflags_cc = cc_std
cflags_objcc = cc_std
}

# This is separate from :compiler_codegen (and not even a sub-config there)
# so that some targets can remove it from the list with:
# configs -= [ "//build/config/compiler:clang_stackrealign" ]
Expand Down Expand Up @@ -462,6 +532,8 @@ config("compiler_arm_thumb") {

config("runtime_library") {
cflags = []
cflags_cc = []
cflags_objcc = []
defines = []
ldflags = []
lib_dirs = []
Expand All @@ -483,6 +555,16 @@ config("runtime_library") {
]
}

if (use_flutter_cxx) {
cflags_cc += [ "-nostdinc++" ]
cflags_objcc += [ "-nostdinc++" ]
ldflags += [ "-nostdlib++" ]
include_dirs = [
"//third_party/libcxx/include",
"//third_party/libcxxabi/include",
]
}

if (is_linux) {
libs += [
"dl",
Expand Down
10 changes: 6 additions & 4 deletions build/config/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ config("sdk") {

# Explicitly use static linking for libstdc++ and libgcc to minimize
# dependencies.
ldflags += [
"-static-libgcc",
"-static-libstdc++",
]
if (!use_flutter_cxx) {
ldflags += [
"-static-libgcc",
"-static-libstdc++",
]
}

if (sysroot != "") {
cflags += [ "--sysroot=" + sysroot ]
Expand Down
10 changes: 6 additions & 4 deletions build/config/mac/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import("//build/config/sysroot.gni")
import("../clang/clang.gni")

config("sdk") {
ldflags = [
"-nostdlib++",
"${clang_prefix}/../lib/libc++.a",
]
if (!use_flutter_cxx) {
ldflags = [
"-nostdlib++",
"${clang_prefix}/../lib/libc++.a",
]
}
}

# On Mac, this is used for everything except static libraries.
Expand Down
6 changes: 0 additions & 6 deletions build/config/sanitizers/sanitizers.gni
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
# found in the LICENSE file.

declare_args() {
# Use libc++ (buildtools/third_party/libc++ and
# buildtools/third_party/libc++abi) instead of stdlibc++ as standard library.
# This is intended to be used for instrumented builds.
use_custom_libcxx =
(is_asan && is_linux) || is_lsan || is_msan || is_tsan || is_ubsan

# Track where uninitialized memory originates from. From fastest to slowest:
# 0 - no tracking, 1 - track only the initial allocation site, 2 - track the
# chain of stores leading from allocation site to use site.
Expand Down
Loading

0 comments on commit ddc11f8

Please sign in to comment.