use bindgen::Builder; use serde_derive::Deserialize; use std::collections::HashMap; use std::collections::HashSet; use std::env; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command;
// This is the format of a single section of the configuration file. #[derive(Deserialize)] struct Bindings { /// types that are explicitly included #[serde(default)]
types: Vec<String>, /// functions that are explicitly included #[serde(default)]
functions: Vec<String>, /// variables (and `#define`s) that are explicitly included #[serde(default)]
variables: Vec<String>, /// types that should be explicitly marked as opaque #[serde(default)]
opaque: Vec<String>, /// enumerations that are turned into a module (without this, the enum is /// mapped using the default, which means that the individual values are /// formed with an underscore as <enum_type>_<enum_value_name>). #[serde(default)]
enums: Vec<String>,
/// Any item that is specifically excluded; if none of the types, functions, /// or variables fields are specified, everything defined will be mapped, /// so this can be used to limit that. #[serde(default)]
exclude: Vec<String>,
/// Whether the file is to be interpreted as C++ #[serde(default)]
cplusplus: bool,
}
// bindgen needs access to libclang. // On windows, this doesn't just work, you have to set LIBCLANG_PATH. // Rather than download the 400Mb+ files, like gecko does, let's just reuse their work. fn setup_clang() { if env::consts::OS != "windows" { return;
}
println!("rerun-if-env-changed=LIBCLANG_PATH");
println!("rerun-if-env-changed=MOZBUILD_STATE_PATH"); if env::var("LIBCLANG_PATH").is_ok() { return;
} let mozbuild_root = iflet Ok(dir) = env::var("MOZBUILD_STATE_PATH") {
PathBuf::from(dir.trim())
} else {
eprintln!("warning: Building without a gecko setup is not likely to work.");
eprintln!(" A working libclang is needed to build nss-sys.");
eprintln!(" Either LIBCLANG_PATH or MOZBUILD_STATE_PATH needs to be set.");
eprintln!();
eprintln!(" We recommend checking out https://github.com/mozilla/gecko-dev");
eprintln!(" Then run `./mach bootstrap` which will retrieve clang.");
eprintln!(" Make sure to export MOZBUILD_STATE_PATH when building."); return;
}; let libclang_dir = mozbuild_root.join("clang").join("lib"); if libclang_dir.is_dir() {
env::set_var("LIBCLANG_PATH", libclang_dir.to_str().unwrap());
println!("rustc-env:LIBCLANG_PATH={}", libclang_dir.to_str().unwrap());
} else {
println!("warning: LIBCLANG_PATH isn't set; maybe run ./mach bootstrap with gecko");
}
}
fn nss_dir() -> PathBuf { let dir = iflet Ok(dir) = env::var("NSS_DIR") { let path = PathBuf::from(dir.trim());
assert!(
!path.is_relative(), "The NSS_DIR environment variable is expected to be an absolute path."
);
path
} else { let out_dir = env::var("OUT_DIR").unwrap(); let dir = Path::new(&out_dir).join("nss"); if !dir.exists() {
Command::new("hg")
.args(&[ "clone", "https://hg.mozilla.org/projects/nss",
dir.to_str().unwrap(),
])
.status()
.expect("can't clone nss");
} let nspr_dir = Path::new(&out_dir).join("nspr"); if !nspr_dir.exists() {
Command::new("hg")
.args(&[ "clone", "https://hg.mozilla.org/projects/nspr",
nspr_dir.to_str().unwrap(),
])
.status()
.expect("can't clone nspr");
}
dir
};
assert!(dir.is_dir(), "NSS_DIR {:?} doesn't exist", dir); // Note that this returns a relative path because UNC // paths on windows cause certain tools to explode.
dir
}
fn get_bash() -> PathBuf { // When running under MOZILLABUILD, we need to make sure not to invoke // another instance of bash that might be sitting around (like WSL). match env::var("MOZILLABUILD") {
Ok(d) => PathBuf::from(d).join("msys").join("bin").join("bash.exe"),
Err(_) => PathBuf::from("bash"),
}
}
// Dynamic libs that aren't transitively included by NSS libs. letmut other_libs = Vec::new(); if env::consts::OS != "windows" {
other_libs.extend_from_slice(&["pthread", "dl", "c", "z"]);
} if env::consts::OS == "macos" {
other_libs.push("sqlite3");
}
dynamic_link_both(&other_libs);
}
fn get_includes(nsstarget: &Path, nssdist: &Path) -> Vec<PathBuf> { let nsprinclude = nsstarget.join("include").join("nspr"); let nssinclude = nssdist.join("public").join("nss"); let includes = vec![nsprinclude, nssinclude]; for i in &includes {
println!("cargo:include={}", i.to_str().unwrap());
}
includes
}
fn build_bindings(base: &str, bindings: &Bindings, flags: &[String], gecko: bool) { let suffix = if bindings.cplusplus { ".hpp" } else { ".h" }; let header_path = PathBuf::from(BINDINGS_DIR).join(String::from(base) + suffix); let header = header_path.to_str().unwrap(); let out = PathBuf::from(env::var("OUT_DIR").unwrap()).join(String::from(base) + ".rs");
// Apply the configuration. for v in &bindings.types {
builder = builder.allowlist_type(v);
} for v in &bindings.functions {
builder = builder.allowlist_function(v);
} for v in &bindings.variables {
builder = builder.allowlist_var(v);
} for v in &bindings.exclude {
builder = builder.blocklist_item(v);
} for v in &bindings.opaque {
builder = builder.opaque_type(v);
} for v in &bindings.enums {
builder = builder.constified_enum_module(v);
}
let bindings = builder.generate().expect("unable to generate bindings");
bindings
.write_to_file(out)
.expect("couldn't write bindings");
}
let nss = nss_dir();
println!("cargo:rerun-if-env-changed={}", nss.display());
build_nss(nss.clone());
// $NSS_DIR/../dist/ let nssdist = nss.parent().unwrap().join("dist");
println!("cargo:rerun-if-env-changed={}", nssdist.display());
let nsstarget = env::var("NSS_TARGET")
.unwrap_or_else(|_| fs::read_to_string(nssdist.join("latest")).unwrap()); let nsstarget = nssdist.join(nsstarget.trim());
let includes = get_includes(&nsstarget, &nssdist);
let nsslibdir = nsstarget.join("lib");
println!( "cargo:rustc-link-search=native={}",
nsslibdir.to_str().unwrap()
); if is_debug() {
static_link();
} else {
dynamic_link();
}
letmut flags: Vec<String> = Vec::new(); for i in includes {
flags.push(String::from("-I") + i.to_str().unwrap());
}
flags
}
#[cfg(feature = "gecko")] fn setup_for_gecko() -> Vec<String> { use mozbuild::TOPOBJDIR;
let fold_libs = mozbuild::config::MOZ_FOLD_LIBS; let libs = if fold_libs {
vec!["nss3"]
} else {
vec!["nssutil3", "nss3", "ssl3", "plds4", "plc4", "nspr4"]
};
for lib in &libs {
println!("cargo:rustc-link-lib=dylib={}", lib);
}
fn process_config(config: &mut HashMap<String, Bindings>) { letmut excludes = HashMap::new(); for header in config.keys().cloned() { // Collect the list of types, functions, and variables configured // for generation in any other configured header, and add it to the list // of items excluded from generation in this header. This ensures that // each item only appears in one bindings module, which prevents some // type conflicts. (However, it does mean that appropriate `use` // declarations must be added for the generated modules.)
excludes.insert(
header.clone(),
config.iter().flat_map(|(h, b)| { if *h != header {
vec![
&b.types,
&b.functions,
&b.variables,
]
} else {
vec![]
}.into_iter().flat_map(|v| v.iter()).cloned()
}).collect::<HashSet<String>>()
);
}
for (header, excludes) in excludes.into_iter() {
config.get_mut(&header).expect("key disappeared from config?").exclude.extend(excludes.into_iter());
}
}
fn main() { let flags = if cfg!(feature = "gecko") {
setup_for_gecko()
} else {
setup_standalone()
};
let config_file = PathBuf::from(BINDINGS_DIR).join(BINDINGS_CONFIG);
println!("cargo:rerun-if-changed={}", config_file.to_str().unwrap()); let config = fs::read_to_string(config_file).expect("unable to read binding configuration"); letmut config: HashMap<String, Bindings> = ::toml::from_str(&config).unwrap();
process_config(&mut config);
for (k, v) in &config {
build_bindings(k, v, &flags[..], cfg!(feature = "gecko"));
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.