mirror of
https://github.com/servo/servo.git
synced 2025-10-04 02:29:12 +01:00
`#[cfg(target_os = "xxx")]` when used in build scripts checks which platform the **build script** is compiled for - i.e. the Host OS. Since ware interested in the actual target os, we need to read `CARGO_CFG_TARGET_OS`, a value that is set at **runtime of the build script**. Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
63 lines
2.2 KiB
Rust
63 lines
2.2 KiB
Rust
/* 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 https://mozilla.org/MPL/2.0/. */
|
|
|
|
use std::error::Error;
|
|
use std::path::Path;
|
|
|
|
use vergen::EmitBuilder;
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
// Cargo does not expose the profile name to crates or their build scripts,
|
|
// but we can extract it from OUT_DIR and set a custom cfg() ourselves.
|
|
let out = std::env::var("OUT_DIR")?;
|
|
let out = Path::new(&out);
|
|
let krate = out.parent().unwrap();
|
|
let build = krate.parent().unwrap();
|
|
let profile = build.parent().unwrap();
|
|
if profile.file_name().unwrap() == "production" {
|
|
println!("cargo:rustc-cfg=servo_production");
|
|
} else {
|
|
println!("cargo:rustc-cfg=servo_do_not_use_in_production");
|
|
}
|
|
|
|
// Note: We can't use `#[cfg(windows)]`, since that would check the host platform
|
|
// and not the target platform
|
|
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
|
|
|
|
if target_os == "windows" {
|
|
#[cfg(windows)]
|
|
{
|
|
let mut res = winres::WindowsResource::new();
|
|
res.set_icon("../../resources/servo.ico");
|
|
res.set_manifest_file("platform/windows/servo.exe.manifest");
|
|
res.compile().unwrap();
|
|
}
|
|
#[cfg(not(windows))]
|
|
panic!("Cross-compiling to windows is currently not supported");
|
|
} else if target_os == "macos" {
|
|
cc::Build::new()
|
|
.file("platform/macos/count_threads.c")
|
|
.compile("count_threads");
|
|
}
|
|
|
|
if let Err(error) = EmitBuilder::builder()
|
|
.fail_on_error()
|
|
.git_sha(true /* short */)
|
|
.emit()
|
|
{
|
|
println!(
|
|
"cargo:warning=Could not generate git version information: {:?}",
|
|
error
|
|
);
|
|
println!("cargo:rustc-env=VERGEN_GIT_SHA=nogit");
|
|
}
|
|
|
|
// On MacOS, all dylib dependencies are shipped along with the binary
|
|
// in the "/lib" directory. Setting the rpath here, allows the dynamic
|
|
// linker to locate them. See `man dyld` for more info.
|
|
if target_os == "macos" {
|
|
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/lib/");
|
|
}
|
|
Ok(())
|
|
}
|