Bug 1759555 - Part 1: Avoid using env! for includes in the mozbuild crate, r=glandium

This switches the code to instead use the generated source file, which is
discovered based on `OUT_DIR` so is easier for rust-analyzer to understand.

The configuration for rust-analyzer will be updated in the next part to
make sure that rust-analyzer is able to take advantage of these changes,
and produce better diagnostics.

Differential Revision: https://phabricator.services.mozilla.com/D153269
This commit is contained in:
Nika Layzell 2022-08-09 14:12:11 +00:00
Родитель 86ec2d5d74
Коммит c8e801b440
3 изменённых файлов: 53 добавлений и 31 удалений

Просмотреть файл

@ -3,6 +3,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import buildconfig
import textwrap
import string
def generate_bool(name):
@ -10,7 +12,55 @@ def generate_bool(name):
return f"pub const {name}: bool = {'true' if value else 'false'};\n"
def escape_rust_string(value):
"""escape the string into a Rust literal"""
# This could be more generous, but we're only escaping paths with it.
unescaped = string.ascii_letters + string.digits + "/$+-_~ "
result = ""
for ch in str(value):
if ch in unescaped:
result += ch
elif ch == "\r":
result += "\\r"
elif ch == "\n":
result += "\\n"
elif ch == "\\":
result += "\\\\"
elif ch == '"':
result += '\\"'
else:
result += "\\u{%x}" % ord(ch)
return '"%s"' % result
def generate(output):
# Write out a macro which can be used within `include!`-like methods to
# reference the topobjdir.
output.write(
textwrap.dedent(
f"""
/// Macro used to name a path in the objdir for use with macros like `include!`
#[macro_export]
macro_rules! objdir_path {{
($path:literal) => {{ concat!({escape_rust_string(buildconfig.topobjdir + "/")}, $path) }}
}}
/// Macro used to name a path in the srcdir for use with macros like `include!`
#[macro_export]
macro_rules! srcdir_path {{
($path:literal) => {{ concat!({escape_rust_string(buildconfig.topsrcdir + "/")}, $path) }}
}}
/// The objdir path for use in build scripts
pub const TOPOBJDIR: &str = {escape_rust_string(buildconfig.topobjdir)};
/// The srcdir path for use in build scripts
pub const TOPSRCDIR: &str = {escape_rust_string(buildconfig.topsrcdir)};
"""
)
)
# Finally, write out some useful booleans from the buildconfig.
output.write(generate_bool("MOZ_FOLD_LIBS"))
output.write(generate_bool("NIGHTLY_BUILD"))
output.write(generate_bool("RELEASE_OR_BETA"))

Просмотреть файл

@ -3,38 +3,11 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
use once_cell::sync::Lazy;
use std::env;
use std::path::PathBuf;
use std::path::Path;
pub static TOPOBJDIR: Lazy<PathBuf> = 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
});
pub static TOPOBJDIR: Lazy<&'static Path> = Lazy::new(|| Path::new(config::TOPOBJDIR));
#[macro_export]
macro_rules! objdir_path {
($path:literal) => {
concat!(env!("MOZ_TOPOBJDIR"), "/", $path)
};
}
pub static TOPSRCDIR: Lazy<PathBuf> = 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 static TOPSRCDIR: Lazy<&'static Path> = Lazy::new(|| Path::new(config::TOPSRCDIR));
pub mod config {
include!(env!("BUILDCONFIG_RS"));

Просмотреть файл

@ -219,7 +219,6 @@ export RUSTFLAGS
export RUSTC
export RUSTDOC
export RUSTFMT
export MOZ_SRC=$(topsrcdir)
export LIBCLANG_PATH=$(MOZ_LIBCLANG_PATH)
export CLANG_PATH=$(MOZ_CLANG_PATH)
export PKG_CONFIG