Skip to content

Commit

Permalink
Merge pull request #12947 from brave/rust_flag_cleanup
Browse files Browse the repository at this point in the history
clean up rustflags and clang toolchain paths
  • Loading branch information
bridiver authored Apr 8, 2022
2 parents 6042ab5 + 43b38d3 commit 775e329
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 45 deletions.
20 changes: 8 additions & 12 deletions build/cargo.gni
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,22 @@ template("cargo_build") {
group_target = target_name
cargo_build_target = target_name + "_cargo_build"

_rustflags = default_rustflags
if (defined(invoker.rustflags)) {
_rustflags += invoker.rustflags
}

action(cargo_build_target) {
script = "//brave/script/cargo.py"

inputs = [ "//brave/script/cargo.py" ]

forward_variables_from(invoker, "*", [ "inputs" ])

if (defined(invoker.inputs)) {
inputs += invoker.inputs
}

_rustflags = rustflags
if (defined(invoker.rustflags)) {
_rustflags += invoker.rustflags
}

forward_variables_from(invoker,
"*",
[
"rustflags",
"inputs",
])

outputs = [ rust_lib_output_path ]

if (!defined(target)) {
Expand All @@ -123,6 +118,7 @@ template("cargo_build") {
"--target=" + target,
"--toolchain=" +
rustc_toolchain, # defined by //brave/build/rust/config.gni,
"--clang_bin_path=" + rebase_path("$clang_base_path/bin"),
"--is_debug=$is_debug",
"--profile=$cargo_profile",
]
Expand Down
1 change: 1 addition & 0 deletions build/rust/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cargo_build("rust_lib") {
"//brave/components/skus/browser/rs:rust_lib",
"//brave/components/speedreader/rust/lib",
]
rustflags = []
if (enable_rust_bls) {
rustflags += [ "--cfg=feature=\"enable_rust_bls\"" ]
deps += [ "//brave/components/bls:rust_lib" ]
Expand Down
71 changes: 40 additions & 31 deletions build/rust/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/. */

import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/ios/config.gni")
import("//build/config/rust.gni")
Expand All @@ -28,42 +29,50 @@ rustc_toolchain = ""
cargo_lib_prefix = ""
cargo_lib_suffix = ""

# Rust compiler flags setup.
# ---------------------------
rustflags = [
# Overflow checks are optional in Rust, but even if switched
# off they do not cause undefined behavior (the overflowing
# behavior is defined). Because containers are bounds-checked
# in safe Rust, they also can't provoke buffer overflows.
# As such these checks may be less important in Rust than C++.
# But in (simplistic) testing they have negligible performance
# overhead, and this helps to provide consistent behavior
# between different configurations, so we'll keep them on until
# we discover a reason to turn them off.
"-Coverflow-checks=on",
]

if (use_lto_in_rustc_linking) {
rustflags += [ "-Clinker-plugin-lto" ]
default_rustflags = [ "-Coverflow-checks=on" ]

if (use_lld) {
default_rustflags += [
"-C",
"link-arg=-fuse-ld=lld",
]

if (!is_win) {
default_rustflags += [
"-C",
"link-arg=-B" + rebase_path("$clang_base_path/bin"),
]
}
}

if (use_lld && !is_component_build) {
rustflags += [ "-Cembed-bitcode=yes" ]
rustflags += [ "-Clto" ]
# https://bugzilla.mozilla.org/show_bug.cgi?id=1188030#c14
# Supposedly this bug was fixed, but it's still happening for us.
too_many_personality_profiles_workaround = is_apple && use_lld && !is_component_build

enable_rust_lto = too_many_personality_profiles_workaround

if (enable_rust_lto) {
default_rustflags += [ "-Cembed-bitcode=yes" ]
default_rustflags += [ "-Clto" ]
}

if (optimize_for_size) {
rustflags += [ "-Copt-level=s" ]
if (is_debug) {
if (too_many_personality_profiles_workaround) {
# must always be optimized for too_many_personality_profiles_workaround to work
default_rustflags += [ "-Copt-level=1" ]
} else {
default_rustflags += [ "-Copt-level=0" ]
}
} else if (optimize_for_size) {
default_rustflags += [ "-Copt-level=s" ]
} else {
rustflags += [ "-Copt-level=3" ]
default_rustflags += [ "-Copt-level=3" ]
}

if (symbol_level == 1) {
rustflags += [ "-Cdebuginfo=1" ]
}

if (symbol_level == 2) {
rustflags += [ "-g" ]
default_rustflags += [ "-Cdebuginfo=1" ]
} else if (symbol_level == 2) {
default_rustflags += [ "-g" ]
}

# get links flags for rust lib
Expand All @@ -72,7 +81,7 @@ if (symbol_level == 2) {
# See https://forge.rust-lang.org/release/platform-support.html for possible targets
if (is_win) {
if (current_cpu == "x86") {
rustflags += [ "--codegen target-feature=+crt-static" ]
default_rustflags += [ "--codegen target-feature=+crt-static" ]
rustc_target = "i686-pc-windows-msvc"
} else if (current_cpu == "x64") {
rustc_target = "x86_64-pc-windows-msvc"
Expand All @@ -87,8 +96,8 @@ if (is_win) {
if (current_cpu == "x64") {
rustc_target = "x86_64-unknown-linux-gnu"
if (use_sysroot) {
rustflags += [ "-C link-arg=--sysroot=$target_sysroot" ]
rustflags += [ "-C link-arg=-Wl,-rpath=\$ORIGIN" ]
default_rustflags += [ "-C link-arg=--sysroot=$target_sysroot" ]
default_rustflags += [ "-C link-arg=-Wl,-rpath=\$ORIGIN" ]
}
}
} else if (is_android) {
Expand Down
8 changes: 6 additions & 2 deletions script/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ def run_cargo(command, args):
toolchains_path = os.path.abspath(
os.path.join(rustup_path, 'toolchains', args.toolchain, "bin"))
env['PATH'] = toolchains_path + os.pathsep + env['PATH']
llvm_ar = os.path.join(toolchains_path, 'llvm-ar')
env['AR'] = llvm_ar

if args.clang_bin_path is not None and not sys.platform.startswith('win'):
env['AR'] = os.path.join(args.clang_bin_path, 'llvm-ar')
env['CC'] = os.path.join(args.clang_bin_path, 'clang')
env['CXX'] = os.path.join(args.clang_bin_path, 'clang++')

if args.mac_deployment_target is not None:
env['MACOSX_DEPLOYMENT_TARGET'] = args.mac_deployment_target
Expand Down Expand Up @@ -102,6 +105,7 @@ def parse_args():
parser.add_option('--toolchain')
parser.add_option('--is_debug')
parser.add_option('--profile')
parser.add_option('--clang_bin_path')
parser.add_option('--mac_deployment_target')
parser.add_option('--ios_deployment_target')
parser.add_option("--rust_flag", action="append",
Expand Down

0 comments on commit 775e329

Please sign in to comment.