diff --git a/.bazelrc b/.bazelrc index f791d2fee6322..2d8b8911c45dd 100644 --- a/.bazelrc +++ b/.bazelrc @@ -170,6 +170,7 @@ build:release --compilation_mode=opt build:release-lto --copt=-flto=thin build:release-lto --linkopt=-flto=thin build:release-lto --@rules_rust//rust/settings:lto=thin +build:release-lto --@//misc/bazel/platforms:xlang_lto=True # Builds from `main` or tagged builds. # diff --git a/misc/bazel/cargo-gazelle/BUILD.bazel b/misc/bazel/cargo-gazelle/BUILD.bazel index df79fac97baca..6ccf8714ce5b3 100644 --- a/misc/bazel/cargo-gazelle/BUILD.bazel +++ b/misc/bazel/cargo-gazelle/BUILD.bazel @@ -90,7 +90,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = ["-Copt-level=3"], + rustc_flags = ["-Copt-level=3"] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [":cargo_gazelle"] + all_crate_deps(normal = True), ) diff --git a/misc/bazel/cargo-gazelle/src/config.rs b/misc/bazel/cargo-gazelle/src/config.rs index dd1a02d794334..246829d1ff54b 100644 --- a/misc/bazel/cargo-gazelle/src/config.rs +++ b/misc/bazel/cargo-gazelle/src/config.rs @@ -13,6 +13,7 @@ use std::collections::BTreeMap; use guppy::graph::PackageMetadata; use std::sync::LazyLock; +use crate::ToBazelDefinition; use crate::targets::{AdditiveContent, RustTestSize}; const KEY_NAME: &str = "cargo-gazelle"; @@ -315,3 +316,21 @@ impl CommonConfig { &self.rustc_env } } + +/// Values that are mirrored in `/misc/bazel/platforms/BUILD.bazel`. +#[derive(Debug, Copy, Clone)] +pub enum ConfigSettingGroup { + XlangLtoEnabled, +} + +impl ToBazelDefinition for ConfigSettingGroup { + fn format(&self, w: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> { + match self { + ConfigSettingGroup::XlangLtoEnabled => { + write!(w, "\"@//misc/bazel/platforms:xlang_lto_enabled\"")? + } + } + + Ok(()) + } +} diff --git a/misc/bazel/cargo-gazelle/src/lib.rs b/misc/bazel/cargo-gazelle/src/lib.rs index 0ac5ce200839c..f957e339c0239 100644 --- a/misc/bazel/cargo-gazelle/src/lib.rs +++ b/misc/bazel/cargo-gazelle/src/lib.rs @@ -11,7 +11,6 @@ use std::collections::{BTreeMap, VecDeque}; use std::fmt::{self, Debug, Write}; use std::rc::Rc; -use crate::platforms::PlatformVariant; use crate::targets::RustTarget; pub mod args; @@ -510,20 +509,21 @@ impl ToBazelDefinition for Glob { /// ``` #[derive(Debug)] pub struct Select { - entries: BTreeMap, + entries: BTreeMap, default: T, } impl Select { - pub fn new(entires: I, default: E) -> Select + pub fn new(entires: I, default: E) -> Select where E: Into, - I: IntoIterator, + J: ToBazelDefinition, + I: IntoIterator, { Select { entries: entires .into_iter() - .map(|(variant, entry)| (variant, entry.into())) + .map(|(variant, entry)| (variant.to_bazel_definition(), entry.into())) .collect(), default: default.into(), } @@ -538,7 +538,7 @@ impl ToBazelDefinition for Select { { let mut w = w.indent(); for (variant, entry) in &self.entries { - variant.format(&mut w)?; + write!(w, "{variant}")?; write!(w, ": ")?; entry.format(&mut w)?; writeln!(w, ",")?; diff --git a/misc/bazel/cargo-gazelle/src/targets.rs b/misc/bazel/cargo-gazelle/src/targets.rs index ac50cae55068f..37b576413267f 100644 --- a/misc/bazel/cargo-gazelle/src/targets.rs +++ b/misc/bazel/cargo-gazelle/src/targets.rs @@ -21,7 +21,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fmt::{self, Write}; use std::str::FromStr; -use crate::config::{CrateConfig, GlobalConfig}; +use crate::config::{ConfigSettingGroup, CrateConfig, GlobalConfig}; use crate::context::CrateContext; use crate::platforms::PlatformVariant; use crate::rules::Rule; @@ -382,8 +382,17 @@ impl RustBinary { let (paths, globs) = bin_config.common().compile_data(); let compile_data = List::new(paths).concat_other(globs.map(Glob::new)); + let xlang_lto_select: Select> = Select::new( + [( + ConfigSettingGroup::XlangLtoEnabled, + vec![QuotedString::new("-Clinker-plugin-lto")], + )], + vec![], + ); + let env = Dict::new(bin_config.env()); - let rustc_flags = List::new(bin_config.common().rustc_flags()); + let rustc_flags = + List::new(bin_config.common().rustc_flags()).concat_other(xlang_lto_select); let rustc_env = Dict::new(bin_config.common().rustc_env()); Ok(Some(RustBinary { diff --git a/misc/bazel/platforms/BUILD.bazel b/misc/bazel/platforms/BUILD.bazel index ab9f496160f2c..a36e79d836d05 100644 --- a/misc/bazel/platforms/BUILD.bazel +++ b/misc/bazel/platforms/BUILD.bazel @@ -16,7 +16,7 @@ support building Materialize for. """ load("@bazel_skylib//lib:selects.bzl", "selects") -load("@bazel_skylib//rules:common_settings.bzl", "string_flag") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") # A flag that we can specify on the command line to configure whether or not we # build with a sanitizer. @@ -45,6 +45,28 @@ config_setting( flag_values = {":sanitizer": "hwaddress"}, ) +bool_flag( + name = "xlang_lto", + build_setting_default = False, +) + +config_setting( + name = "use_xlang_lto", + flag_values = {":xlang_lto": "True"}, +) + +# With our current toolchain setup, cross language LTO is only supported when building for Linux. +# +# See for macOS support. +selects.config_setting_group( + name = "xlang_lto_enabled", + match_all = [ + "@platforms//os:linux", + ":use_xlang_lto", + ], + visibility = ["//visibility:public"], +) + # We only want to use jemalloc if we're building for Linux and we're not using sanitizers. selects.config_setting_group( name = "use_jemalloc", diff --git a/src/balancerd/BUILD.bazel b/src/balancerd/BUILD.bazel index c0f1589f7aba8..ba100e1a6885d 100644 --- a/src/balancerd/BUILD.bazel +++ b/src/balancerd/BUILD.bazel @@ -179,7 +179,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ ":mz_balancerd", diff --git a/src/catalog-debug/BUILD.bazel b/src/catalog-debug/BUILD.bazel index e48f1037e0d27..83fb36f8d05ba 100644 --- a/src/catalog-debug/BUILD.bazel +++ b/src/catalog-debug/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ "//src/adapter:mz_adapter", diff --git a/src/clusterd/BUILD.bazel b/src/clusterd/BUILD.bazel index 80d12fda92627..d8acd456aea6b 100644 --- a/src/clusterd/BUILD.bazel +++ b/src/clusterd/BUILD.bazel @@ -156,7 +156,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ ":mz_clusterd", diff --git a/src/environmentd/BUILD.bazel b/src/environmentd/BUILD.bazel index f4ce2bacdc89a..85e0f5a2d819b 100644 --- a/src/environmentd/BUILD.bazel +++ b/src/environmentd/BUILD.bazel @@ -793,7 +793,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ ":mz_environmentd", diff --git a/src/fivetran-destination/BUILD.bazel b/src/fivetran-destination/BUILD.bazel index cb9d31bb0de13..4f289df286a18 100644 --- a/src/fivetran-destination/BUILD.bazel +++ b/src/fivetran-destination/BUILD.bazel @@ -116,7 +116,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ ":mz_fivetran_destination", diff --git a/src/frontegg-mock/BUILD.bazel b/src/frontegg-mock/BUILD.bazel index 2010c60205b22..05b1b4b6f3e88 100644 --- a/src/frontegg-mock/BUILD.bazel +++ b/src/frontegg-mock/BUILD.bazel @@ -132,7 +132,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ ":mz_frontegg_mock", diff --git a/src/interchange/BUILD.bazel b/src/interchange/BUILD.bazel index 85f87278dbafa..62a5ca0987a13 100644 --- a/src/interchange/BUILD.bazel +++ b/src/interchange/BUILD.bazel @@ -126,7 +126,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ ":mz_interchange", diff --git a/src/kafka-util/BUILD.bazel b/src/kafka-util/BUILD.bazel index 596ba9de8e646..8f516f43b9d19 100644 --- a/src/kafka-util/BUILD.bazel +++ b/src/kafka-util/BUILD.bazel @@ -105,7 +105,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ ":mz_kafka_util", diff --git a/src/lsp-server/BUILD.bazel b/src/lsp-server/BUILD.bazel index 42e8c6441cda0..92ed03aa2efa1 100644 --- a/src/lsp-server/BUILD.bazel +++ b/src/lsp-server/BUILD.bazel @@ -144,7 +144,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.3.0", deps = [ ":mz_lsp_server", diff --git a/src/materialized/BUILD.bazel b/src/materialized/BUILD.bazel index e7a832759be51..91ef68bc50469 100644 --- a/src/materialized/BUILD.bazel +++ b/src/materialized/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ "//src/clusterd:mz_clusterd", diff --git a/src/mz-debug/BUILD.bazel b/src/mz-debug/BUILD.bazel index 9369a13247649..8c9a7b23b8dbf 100644 --- a/src/mz-debug/BUILD.bazel +++ b/src/mz-debug/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.1.0", deps = [ "//src/build-info:mz_build_info", diff --git a/src/mz/BUILD.bazel b/src/mz/BUILD.bazel index 94c6bbc96e83e..d1cf31776bc87 100644 --- a/src/mz/BUILD.bazel +++ b/src/mz/BUILD.bazel @@ -139,7 +139,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.3.0", deps = [ ":mz", diff --git a/src/orchestratord/BUILD.bazel b/src/orchestratord/BUILD.bazel index 6766763d9e1ff..a1f513d98bd3f 100644 --- a/src/orchestratord/BUILD.bazel +++ b/src/orchestratord/BUILD.bazel @@ -123,7 +123,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ ":mz_orchestratord", diff --git a/src/persist-cli/BUILD.bazel b/src/persist-cli/BUILD.bazel index bd2ebca042256..461b08592925e 100644 --- a/src/persist-cli/BUILD.bazel +++ b/src/persist-cli/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ "//src/http-util:mz_http_util", diff --git a/src/pgtest/BUILD.bazel b/src/pgtest/BUILD.bazel index bb1ba4c9c23ee..579767c23bdf6 100644 --- a/src/pgtest/BUILD.bazel +++ b/src/pgtest/BUILD.bazel @@ -90,7 +90,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ ":mz_pgtest", diff --git a/src/s3-datagen/BUILD.bazel b/src/s3-datagen/BUILD.bazel index 1a3311f67f466..3fa5e5c79d80e 100644 --- a/src/s3-datagen/BUILD.bazel +++ b/src/s3-datagen/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ "//src/aws-util:mz_aws_util", diff --git a/src/sqllogictest/BUILD.bazel b/src/sqllogictest/BUILD.bazel index 4bc23413a33d5..859e4bc71c729 100644 --- a/src/sqllogictest/BUILD.bazel +++ b/src/sqllogictest/BUILD.bazel @@ -203,7 +203,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.1", deps = [ ":mz_sqllogictest", diff --git a/src/testdrive/BUILD.bazel b/src/testdrive/BUILD.bazel index 1c56c294f3d29..288c2fd6712b9 100644 --- a/src/testdrive/BUILD.bazel +++ b/src/testdrive/BUILD.bazel @@ -165,7 +165,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.144.0-dev.0", deps = [ ":mz_testdrive", diff --git a/test/metabase/smoketest/BUILD.bazel b/test/metabase/smoketest/BUILD.bazel index 44b01db9c00e4..04da01e3c741c 100644 --- a/test/metabase/smoketest/BUILD.bazel +++ b/test/metabase/smoketest/BUILD.bazel @@ -30,7 +30,10 @@ rust_binary( lint_config = ":lints", proc_macro_deps = [] + all_crate_deps(proc_macro = True), rustc_env = {}, - rustc_flags = [], + rustc_flags = [] + select({ + "@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"], + "//conditions:default": [], + }), version = "0.0.0", deps = [ "//src/metabase:mz_metabase",