Skip to content

Commit 12bc843

Browse files
committed
Introduce RUSTC_STATIC_CLANG_RT_PATH and RUSTC_STATIC_UNWIND_PATH envs
The goal of this envirements variable is easy statically link `libclang_rt` or `libunwind` into rust. It also introduces a way to easy extend rust build in a way which allows to inject more static builds for some rare system if it required. It haven't been documented because it seems as very deep hack and the user who needs it will discover this hack by reading sources ;)
1 parent 6d6d089 commit 12bc843

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4995,6 +4995,7 @@ version = "0.0.0"
49954995
dependencies = [
49964996
"addr2line",
49974997
"alloc",
4998+
"build_helper",
49984999
"cfg-if 0.1.10",
49995000
"compiler_builtins",
50005001
"core",
@@ -5662,6 +5663,7 @@ dependencies = [
56625663
name = "unwind"
56635664
version = "0.0.0"
56645665
dependencies = [
5666+
"build_helper",
56655667
"cc",
56665668
"cfg-if 0.1.10",
56675669
"compiler_builtins",

compiler/rustc_llvm/build.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::Command;
44

5-
use build_helper::{output, tracked_env_var_os};
5+
use build_helper::{maybe_static_library, output, tracked_env_var_os};
66

77
fn detect_llvm_link() -> (&'static str, &'static str) {
88
// Force the link mode we want, preferring static by default, but
@@ -307,4 +307,6 @@ fn main() {
307307
if target.contains("windows-gnu") {
308308
println!("cargo:rustc-link-lib=static:-bundle=pthread");
309309
}
310+
311+
maybe_static_library("RUSTC_STATIC_CLANG_RT_PATH", "clang_rt");
310312
}

library/std/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@ heap_size = 0x8000000
8484
name = "stdbenches"
8585
path = "benches/lib.rs"
8686
test = true
87+
88+
[build-dependencies]
89+
build_helper = { path = "../../src/build_helper" }

library/std/build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::env;
22

3+
use build_helper::maybe_static_library;
4+
35
fn main() {
46
println!("cargo:rerun-if-changed=build.rs");
57
let target = env::var("TARGET").expect("TARGET was not set");
@@ -47,4 +49,6 @@ fn main() {
4749
}
4850
println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
4951
println!("cargo:rustc-cfg=backtrace_in_libstd");
52+
53+
maybe_static_library("RUSTC_STATIC_CLANG_RT_PATH", "clang_rt");
5054
}

library/unwind/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ compiler_builtins = "0.1.0"
2020
cfg-if = "0.1.8"
2121

2222
[build-dependencies]
23+
build_helper = { path = "../../src/build_helper" }
2324
cc = "1.0.69"
2425

2526
[features]

library/unwind/build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use std::env;
22

3+
use build_helper::maybe_static_library;
4+
35
fn main() {
46
println!("cargo:rerun-if-changed=build.rs");
57
let target = env::var("TARGET").expect("TARGET was not set");
68

9+
if maybe_static_library("RUSTC_STATIC_UNWIND_PATH", "unwind") {
10+
return;
11+
}
12+
713
if target.contains("android") {
814
let build = cc::Build::new();
915

src/build_helper/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,25 @@ fn fail(s: &str) -> ! {
203203
println!("\n\n{}\n\n", s);
204204
std::process::exit(1);
205205
}
206+
207+
/// if you need for some reason to statically inject some library, like clang_rt
208+
/// here a good place. Anyway, you should use only thin .a file on macOS.
209+
/// You may extract it like:
210+
/// lipo -thin x86_64 -output libclang_rt.a /path/to/llvm/lib/../libclang_rt.osx.a
211+
///
212+
/// It returns true, when it had injected static library.
213+
pub fn maybe_static_library(env_path_name: &str, library_name: &str) -> bool {
214+
println!("cargo:rerun-if-env-changed={}", env_path_name);
215+
216+
if let Ok(path) = env::var(env_path_name) {
217+
let target = env::var("TARGET").expect("TARGET was not set");
218+
println!("cargo:rerun-if-env-changed=TARGET");
219+
220+
println!("cargo:rustc-link-search=native={}", path);
221+
println!("cargo:rustc-link-search=native={}/{}", path, target);
222+
println!("cargo:rustc-link-lib=static={}", library_name);
223+
return true;
224+
}
225+
226+
return false;
227+
}

0 commit comments

Comments
 (0)