diff --git a/build/cargo.gni b/build/cargo.gni index add32df19e88..0ef653b2cb1c 100644 --- a/build/cargo.gni +++ b/build/cargo.gni @@ -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)) { @@ -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", ] diff --git a/build/rust/BUILD.gn b/build/rust/BUILD.gn index d750da0fcd4f..6dc882c40494 100644 --- a/build/rust/BUILD.gn +++ b/build/rust/BUILD.gn @@ -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" ] diff --git a/build/rust/config.gni b/build/rust/config.gni index 3e8f5b9b5684..ce8b5ab02aad 100644 --- a/build/rust/config.gni +++ b/build/rust/config.gni @@ -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") @@ -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 @@ -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" @@ -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) { diff --git a/script/cargo.py b/script/cargo.py index 48f15544fbbc..d7d872ef5b39 100755 --- a/script/cargo.py +++ b/script/cargo.py @@ -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 @@ -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",