Skip to content

Commit eac0a8b

Browse files
committed
bootstrap: Add directives to not double-link libs
Have all Cargo-built crates pass `--cfg cargobuild` and then add appropriate `#[cfg]` definitions to all crates to avoid linking anything if this is passed. This should help allow libstd to compile with both the makefiles and with Cargo.
1 parent 4da4970 commit eac0a8b

File tree

15 files changed

+38
-5
lines changed

15 files changed

+38
-5
lines changed

src/liballoc_jemalloc/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::process::Command;
1717
use build_helper::run;
1818

1919
fn main() {
20+
println!("cargo:rustc-cfg=cargobuild");
21+
2022
let target = env::var("TARGET").unwrap();
2123
let host = env::var("HOST").unwrap();
2224
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

src/liballoc_jemalloc/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ use libc::{c_int, c_void, size_t};
3838
not(target_os = "android"),
3939
not(target_env = "musl")),
4040
link(name = "pthread"))]
41-
extern "C" {
41+
#[cfg(not(cargobuild))]
42+
extern {}
43+
44+
extern {
4245
fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
4346
fn je_rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
4447
fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;

src/libflate/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
extern crate gcc;
1212

1313
fn main() {
14+
println!("cargo:rustc-cfg=cargobuild");
1415
gcc::Config::new()
1516
.file("../rt/miniz.c")
1617
.compile("libminiz.a");

src/libflate/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl Drop for Bytes {
7979
}
8080

8181
#[link(name = "miniz", kind = "static")]
82-
extern "C" {
82+
#[cfg(not(cargobuild))]
83+
extern {}
84+
85+
extern {
8386
/// Raw miniz compression function.
8487
fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
8588
src_buf_len: size_t,

src/librustc_llvm/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use std::path::PathBuf;
1818
use build_helper::output;
1919

2020
fn main() {
21+
println!("cargo:rustc-cfg=cargobuild");
22+
2123
let target = env::var("TARGET").unwrap();
2224
let llvm_config = env::var_os("LLVM_CONFIG").map(PathBuf::from)
2325
.unwrap_or_else(|| {

src/librustc_llvm/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ pub mod debuginfo {
609609
// automatically updated whenever LLVM is updated to include an up-to-date
610610
// set of the libraries we need to link to LLVM for.
611611
#[link(name = "rustllvm", kind = "static")]
612+
#[cfg(not(cargobuild))]
613+
extern {}
614+
612615
#[linked_from = "rustllvm"] // not quite true but good enough
613616
extern {
614617
/* Create and destroy contexts. */
@@ -2486,6 +2489,7 @@ impl Drop for OperandBundleDef {
24862489
// parts of LLVM that rustllvm depends on aren't thrown away by the linker.
24872490
// Works to the above fix for #15460 to ensure LLVM dependencies that
24882491
// are only used by rustllvm don't get stripped by the linker.
2492+
#[cfg(not(cargobuild))]
24892493
mod llvmdeps {
24902494
include! { env!("CFG_LLVM_LINKAGE_FILE") }
24912495
}

src/librustdoc/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
extern crate gcc;
1212

1313
fn main() {
14+
println!("cargo:rustc-cfg=cargobuild");
1415
let mut cfg = gcc::Config::new();
1516
cfg.file("../rt/hoedown/src/autolink.c")
1617
.file("../rt/hoedown/src/buffer.c")

src/librustdoc/html/markdown.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ struct hoedown_buffer {
157157

158158
// hoedown FFI
159159
#[link(name = "hoedown", kind = "static")]
160+
#[cfg(not(cargobuild))]
161+
extern {}
162+
160163
extern {
161164
fn hoedown_html_renderer_new(render_flags: libc::c_uint,
162165
nesting_level: libc::c_int)

src/libstd/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::process::Command;
1919
use build_helper::run;
2020

2121
fn main() {
22+
println!("cargo:rustc-cfg=cargobuild");
23+
2224
let target = env::var("TARGET").unwrap();
2325
let host = env::var("HOST").unwrap();
2426
if !target.contains("apple") && !target.contains("msvc") {

src/libstd/rand/os.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ mod imp {
269269
const kSecRandomDefault: *const SecRandom = ptr::null();
270270

271271
#[link(name = "Security", kind = "framework")]
272-
extern "C" {
272+
#[cfg(not(cargobuild))]
273+
extern {}
274+
275+
extern {
273276
fn SecRandomCopyBytes(rnd: *const SecRandom,
274277
count: size_t, bytes: *mut u8) -> c_int;
275278
}

src/libstd/rtdeps.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! the standard library This varies per-platform, but these libraries are
1313
//! necessary for running libstd.
1414
15+
#![cfg(not(cargobuild))]
16+
1517
// LLVM implements the `frem` instruction as a call to `fmod`, which lives in
1618
// libm. Hence, we must explicitly link to it.
1719
//

src/libstd/sys/common/gnu/libbacktrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn print(w: &mut Write, idx: isize, addr: *mut libc::c_void,
4040
errnum: libc::c_int);
4141
enum backtrace_state {}
4242
#[link(name = "backtrace", kind = "static")]
43-
#[cfg(not(test))]
43+
#[cfg(all(not(test), not(cargobuild)))]
4444
extern {}
4545

4646
extern {

src/libstd/sys/common/unwind/gcc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ pub mod eh_frame_registry {
252252
// See also: rtbegin.rs, `unwind` module.
253253

254254
#[link(name = "gcc_eh")]
255+
#[cfg(not(cargobuild))]
256+
extern {}
257+
255258
extern {
256259
fn __register_frame_info(eh_frame_begin: *const u8, object: *mut u8);
257260
fn __deregister_frame_info(eh_frame_begin: *const u8, object: *mut u8);

src/libstd/sys/unix/os.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ pub fn args() -> Args {
339339
pub fn args() -> Args {
340340
use mem;
341341

342-
#[link(name = "objc")]
343342
extern {
344343
fn sel_registerName(name: *const libc::c_uchar) -> Sel;
345344
fn objc_msgSend(obj: NsId, sel: Sel, ...) -> NsId;
346345
fn objc_getClass(class_name: *const libc::c_uchar) -> NsId;
347346
}
348347

349348
#[link(name = "Foundation", kind = "framework")]
349+
#[link(name = "objc")]
350+
#[cfg(not(cargobuild))]
350351
extern {}
351352

352353
type Sel = *const libc::c_void;

src/libstd/sys/windows/c.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,9 @@ pub enum EXCEPTION_DISPOSITION {
966966
#[link(name = "userenv")]
967967
#[link(name = "shell32")]
968968
#[link(name = "advapi32")]
969+
#[cfg(not(cargobuild))]
970+
extern {}
971+
969972
extern "system" {
970973
pub fn WSAStartup(wVersionRequested: WORD,
971974
lpWSAData: LPWSADATA) -> c_int;

0 commit comments

Comments
 (0)