diff --git a/Cargo.lock b/Cargo.lock index 994f98dd2216..fd9a795fbd71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6019,3 +6019,7 @@ dependencies = [ "podio", "time", ] + +[[patch.unused]] +name = "mozbuild" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 2d3e6f954f16..63a75b26d6a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,6 +86,9 @@ opt-level = 2 cmake = { path = "build/rust/cmake" } vcpkg = { path = "build/rust/vcpkg" } +# Helper crate for integration in the gecko build system. +mozbuild = { path = "build/rust/mozbuild" } + # Patch autocfg to hide rustc output. Workaround for https://github.com/cuviper/autocfg/issues/30 autocfg = { path = "third_party/rust/autocfg" } diff --git a/build/moz.build b/build/moz.build index 10d0e9aa6e0a..671e77e97b35 100644 --- a/build/moz.build +++ b/build/moz.build @@ -21,6 +21,9 @@ else: if CONFIG["MOZ_STDCXX_COMPAT"]: DIRS += ["unix/stdc++compat"] +if CONFIG["RUSTC"]: + DIRS += ["rust/mozbuild"] + CRAMTEST_MANIFESTS += [ "tests/cram/cram.ini", ] diff --git a/build/rust/mozbuild/Cargo.toml b/build/rust/mozbuild/Cargo.toml new file mode 100644 index 000000000000..e5958a05f866 --- /dev/null +++ b/build/rust/mozbuild/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "mozbuild" +version = "0.1.0" +edition = "2018" +license = "MPL-2.0" + +[lib] +path = "lib.rs" + +[dependencies] +once_cell = "1" diff --git a/build/rust/mozbuild/build.rs b/build/rust/mozbuild/build.rs new file mode 100644 index 000000000000..e9fb6b91b00e --- /dev/null +++ b/build/rust/mozbuild/build.rs @@ -0,0 +1,20 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +use std::path::PathBuf; + +fn main() { + let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + if let Some(topobjdir) = out_dir + .ancestors() + .find(|dir| dir.join("config.status").exists()) + { + println!( + "cargo:rustc-env=BUILDCONFIG_RS={}", + topobjdir + .join("build/rust/mozbuild/buildconfig.rs") + .display() + ); + } +} diff --git a/build/rust/mozbuild/generate_buildconfig.py b/build/rust/mozbuild/generate_buildconfig.py new file mode 100644 index 000000000000..8a814127d0dd --- /dev/null +++ b/build/rust/mozbuild/generate_buildconfig.py @@ -0,0 +1,20 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# 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 buildconfig + + +def generate_bool(name): + value = buildconfig.substs.get(name) + return f"pub const {name}: bool = {'true' if value else 'false'};\n" + + +def generate(output): + output.write(generate_bool("MOZ_FOLD_LIBS")) + output.write(generate_bool("NIGHTLY_BUILD")) + output.write(generate_bool("RELEASE_OR_BETA")) + output.write(generate_bool("EARLY_BETA_OR_EARLIER")) + output.write(generate_bool("MOZ_DEV_EDITION")) + output.write(generate_bool("MOZ_ESR")) + output.write(generate_bool("MOZ_DIAGNOSTIC_ASSERT_ENABLED")) diff --git a/build/rust/mozbuild/lib.rs b/build/rust/mozbuild/lib.rs new file mode 100644 index 000000000000..fcdd6f66c607 --- /dev/null +++ b/build/rust/mozbuild/lib.rs @@ -0,0 +1,41 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +use once_cell::sync::Lazy; +use std::env; +use std::path::PathBuf; + +pub static TOPOBJDIR: Lazy = Lazy::new(|| { + let path = PathBuf::from( + env::var_os("MOZ_TOPOBJDIR").expect("MOZ_TOPOBJDIR must be set in the environment"), + ); + assert!( + path.is_absolute() && path.is_dir(), + "MOZ_TOPOBJDIR must be an absolute directory, was: {}", + path.display() + ); + path +}); + +#[macro_export] +macro_rules! objdir_path { + ($path:literal) => { + concat!(env!("MOZ_TOPOBJDIR"), "/", $path) + }; +} + +pub static TOPSRCDIR: Lazy = Lazy::new(|| { + let path = + PathBuf::from(env::var_os("MOZ_SRC").expect("MOZ_SRC must be set in the environment")); + assert!( + path.is_absolute() && path.is_dir(), + "MOZ_SRC must be an absolute directory, was: {}", + path.display() + ); + path +}); + +pub mod config { + include!(env!("BUILDCONFIG_RS")); +} diff --git a/build/rust/mozbuild/moz.build b/build/rust/mozbuild/moz.build new file mode 100644 index 000000000000..3cc09a7c5a5e --- /dev/null +++ b/build/rust/mozbuild/moz.build @@ -0,0 +1,9 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# 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/. + +GeneratedFile( + "buildconfig.rs", script="generate_buildconfig.py", entry_point="generate" +)