From 3036b001276a6e43409b08b7f2334ce72aeeb036 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 2 Nov 2014 23:02:53 -0800 Subject: [PATCH] rustc: Default to static linking dylibs If a dylib is being produced, the compiler will now first check to see if it can be created entirely statically before falling back to dynamic dependencies. This behavior can be overridden with `-C prefer-dynamic`. Due to the alteration in behavior, this is a breaking change. Any previous users relying on dylibs implicitly maximizing dynamic dependencies should start passing `-C prefer-dynamic` to compilations. Closes #18499 [breaking-change] --- mk/main.mk | 7 ++-- mk/target.mk | 8 ++-- src/librustc/middle/dependency_format.rs | 10 +++++ src/test/auxiliary/issue-12133-dylib.rs | 2 - .../syntax-extension-with-dll-deps-1.rs | 1 - .../syntax-extension-with-dll-deps-2.rs | 1 - src/test/run-make/c-dynamic-dylib/Makefile | 2 +- src/test/run-make/c-static-dylib/Makefile | 2 +- src/test/run-make/dylib-chain/Makefile | 6 +-- src/test/run-make/issue-15460/Makefile | 2 +- src/test/run-make/mixing-deps/Makefile | 2 +- src/test/run-make/mixing-formats/Makefile | 38 +++++++++---------- src/test/run-make/prefer-dylib/Makefile | 2 +- src/test/run-make/simple-dylib/Makefile | 2 +- .../run-make/static-dylib-by-default/Makefile | 9 +++++ .../run-make/static-dylib-by-default/bar.rs | 18 +++++++++ .../run-make/static-dylib-by-default/foo.rs | 14 +++++++ .../run-make/static-dylib-by-default/main.c | 16 ++++++++ src/test/run-make/suspicious-library/Makefile | 2 +- .../run-make/symlinked-libraries/Makefile | 2 +- 20 files changed, 105 insertions(+), 41 deletions(-) create mode 100644 src/test/run-make/static-dylib-by-default/Makefile create mode 100644 src/test/run-make/static-dylib-by-default/bar.rs create mode 100644 src/test/run-make/static-dylib-by-default/foo.rs create mode 100644 src/test/run-make/static-dylib-by-default/main.c diff --git a/mk/main.mk b/mk/main.mk index 181bca69462bd..adc39c39f32b9 100644 --- a/mk/main.mk +++ b/mk/main.mk @@ -148,12 +148,13 @@ endif # libraries, so in the interest of space, prefer dynamic linking throughout the # compilation process. # -# Note though that these flags are omitted for stage2+. This means that the -# snapshot will be generated with a statically linked rustc so we only have to -# worry about the distribution of one file (with its native dynamic +# Note though that these flags are omitted for the *bins* in stage2+. This means +# that the snapshot will be generated with a statically linked rustc so we only +# have to worry about the distribution of one file (with its native dynamic # dependencies) RUSTFLAGS_STAGE0 += -C prefer-dynamic RUSTFLAGS_STAGE1 += -C prefer-dynamic +RUST_LIB_FLAGS_ST2 += -C prefer-dynamic # Landing pads require a lot of codegen. We can get through bootstrapping faster # by not emitting them. diff --git a/mk/target.mk b/mk/target.mk index 10fed6ad98d93..ed7d8bb497d28 100644 --- a/mk/target.mk +++ b/mk/target.mk @@ -17,9 +17,9 @@ export CFG_COMPILER_HOST_TRIPLE # code, make sure that these common warnings are denied by default. These can # be overridden during development temporarily. For stage0, we allow warnings # which may be bugs in stage0 (should be fixed in stage1+) -WFLAGS_ST0 = -W warnings -WFLAGS_ST1 = -D warnings -WFLAGS_ST2 = -D warnings +RUST_LIB_FLAGS_ST0 += -W warnings +RUST_LIB_FLAGS_ST1 += -D warnings +RUST_LIB_FLAGS_ST2 += -D warnings # Macro that generates the full list of dependencies for a crate at a particular # stage/target/host tuple. @@ -80,7 +80,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \ $$(call REMOVE_ALL_OLD_GLOB_MATCHES, \ $$(dir $$@)$$(call CFG_RLIB_GLOB,$(4))) $$(STAGE$(1)_T_$(2)_H_$(3)) \ - $$(WFLAGS_ST$(1)) \ + $$(RUST_LIB_FLAGS_ST$(1)) \ -L "$$(RT_OUTPUT_DIR_$(2))" \ -L "$$(LLVM_LIBDIR_$(2))" \ -L "$$(dir $$(LLVM_STDCPP_LOCATION_$(2)))" \ diff --git a/src/librustc/middle/dependency_format.rs b/src/librustc/middle/dependency_format.rs index 3baa8eb0cc040..0930347b20cbf 100644 --- a/src/librustc/middle/dependency_format.rs +++ b/src/librustc/middle/dependency_format.rs @@ -123,6 +123,16 @@ fn calculate_type(sess: &session::Session, return Vec::new(); } + // Generating a dylib without `-C prefer-dynamic` means that we're going + // to try to eagerly statically link all dependencies. This is normally + // done for end-product dylibs, not intermediate products. + config::CrateTypeDylib if !sess.opts.cg.prefer_dynamic => { + match attempt_static(sess) { + Some(v) => return v, + None => {} + } + } + // Everything else falls through below config::CrateTypeExecutable | config::CrateTypeDylib => {}, } diff --git a/src/test/auxiliary/issue-12133-dylib.rs b/src/test/auxiliary/issue-12133-dylib.rs index 858d7269cd874..ea22258f67d6b 100644 --- a/src/test/auxiliary/issue-12133-dylib.rs +++ b/src/test/auxiliary/issue-12133-dylib.rs @@ -8,6 +8,4 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// no-prefer-dynamic - #![crate_type = "dylib"] diff --git a/src/test/auxiliary/syntax-extension-with-dll-deps-1.rs b/src/test/auxiliary/syntax-extension-with-dll-deps-1.rs index a6e17e73322ec..338e04fbb0746 100644 --- a/src/test/auxiliary/syntax-extension-with-dll-deps-1.rs +++ b/src/test/auxiliary/syntax-extension-with-dll-deps-1.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// no-prefer-dynamic // force-host #![crate_type = "dylib"] diff --git a/src/test/auxiliary/syntax-extension-with-dll-deps-2.rs b/src/test/auxiliary/syntax-extension-with-dll-deps-2.rs index 7a4339aa9f042..88548bb5410e6 100644 --- a/src/test/auxiliary/syntax-extension-with-dll-deps-2.rs +++ b/src/test/auxiliary/syntax-extension-with-dll-deps-2.rs @@ -9,7 +9,6 @@ // except according to those terms. // force-host -// no-prefer-dynamic #![crate_type = "dylib"] #![feature(plugin_registrar, quote, globs)] diff --git a/src/test/run-make/c-dynamic-dylib/Makefile b/src/test/run-make/c-dynamic-dylib/Makefile index c4720c418a77a..83bddd4c73c3f 100644 --- a/src/test/run-make/c-dynamic-dylib/Makefile +++ b/src/test/run-make/c-dynamic-dylib/Makefile @@ -6,7 +6,7 @@ all: echo ignored else all: $(call DYLIB,cfoo) - $(RUSTC) foo.rs + $(RUSTC) foo.rs -C prefer-dynamic $(RUSTC) bar.rs $(call RUN,bar) $(call REMOVE_DYLIBS,cfoo) diff --git a/src/test/run-make/c-static-dylib/Makefile b/src/test/run-make/c-static-dylib/Makefile index 6b047846cfdeb..9914e12d5696e 100644 --- a/src/test/run-make/c-static-dylib/Makefile +++ b/src/test/run-make/c-static-dylib/Makefile @@ -1,7 +1,7 @@ -include ../tools.mk all: $(call STATICLIB,cfoo) - $(RUSTC) foo.rs + $(RUSTC) foo.rs -C prefer-dynamic $(RUSTC) bar.rs rm $(TMPDIR)/$(call STATICLIB_GLOB,cfoo) $(call RUN,bar) diff --git a/src/test/run-make/dylib-chain/Makefile b/src/test/run-make/dylib-chain/Makefile index 2149f2451470e..a33177197b119 100644 --- a/src/test/run-make/dylib-chain/Makefile +++ b/src/test/run-make/dylib-chain/Makefile @@ -1,9 +1,9 @@ -include ../tools.mk all: - $(RUSTC) m1.rs - $(RUSTC) m2.rs - $(RUSTC) m3.rs + $(RUSTC) m1.rs -C prefer-dynamic + $(RUSTC) m2.rs -C prefer-dynamic + $(RUSTC) m3.rs -C prefer-dynamic $(RUSTC) m4.rs $(call RUN,m4) $(call REMOVE_DYLIBS,m1) diff --git a/src/test/run-make/issue-15460/Makefile b/src/test/run-make/issue-15460/Makefile index e6dd5c4c1af55..bc5e9b728694f 100644 --- a/src/test/run-make/issue-15460/Makefile +++ b/src/test/run-make/issue-15460/Makefile @@ -1,6 +1,6 @@ -include ../tools.mk all: $(TMPDIR)/libfoo.a - $(RUSTC) foo.rs -C extra-filename=-383hf8 + $(RUSTC) foo.rs -C extra-filename=-383hf8 -C prefer-dynamic $(RUSTC) bar.rs $(call RUN,bar) diff --git a/src/test/run-make/mixing-deps/Makefile b/src/test/run-make/mixing-deps/Makefile index 76106ada14487..0e52d4a8befba 100644 --- a/src/test/run-make/mixing-deps/Makefile +++ b/src/test/run-make/mixing-deps/Makefile @@ -1,7 +1,7 @@ -include ../tools.mk all: - $(RUSTC) both.rs + $(RUSTC) both.rs -C prefer-dynamic $(RUSTC) dylib.rs -C prefer-dynamic $(RUSTC) prog.rs $(call RUN,prog) diff --git a/src/test/run-make/mixing-formats/Makefile b/src/test/run-make/mixing-formats/Makefile index e665cb2e5350d..48257669baf94 100644 --- a/src/test/run-make/mixing-formats/Makefile +++ b/src/test/run-make/mixing-formats/Makefile @@ -15,60 +15,60 @@ all: # Building just baz $(RUSTC) --crate-type=rlib foo.rs - $(RUSTC) --crate-type=dylib bar1.rs - $(RUSTC) --crate-type=dylib,rlib baz.rs + $(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic + $(RUSTC) --crate-type=dylib,rlib baz.rs -C prefer-dynamic $(RUSTC) --crate-type=bin baz.rs rm $(TMPDIR)/* - $(RUSTC) --crate-type=dylib foo.rs + $(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic $(RUSTC) --crate-type=rlib bar1.rs - $(RUSTC) --crate-type=dylib,rlib baz.rs + $(RUSTC) --crate-type=dylib,rlib baz.rs -C prefer-dynamic $(RUSTC) --crate-type=bin baz.rs rm $(TMPDIR)/* # Building baz2 $(RUSTC) --crate-type=rlib foo.rs - $(RUSTC) --crate-type=dylib bar1.rs - $(RUSTC) --crate-type=dylib bar2.rs + $(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic + $(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic $(RUSTC) --crate-type=dylib baz2.rs && exit 1 || exit 0 $(RUSTC) --crate-type=bin baz2.rs && exit 1 || exit 0 rm $(TMPDIR)/* $(RUSTC) --crate-type=rlib foo.rs $(RUSTC) --crate-type=rlib bar1.rs - $(RUSTC) --crate-type=dylib bar2.rs + $(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic $(RUSTC) --crate-type=dylib,rlib baz2.rs $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* $(RUSTC) --crate-type=rlib foo.rs - $(RUSTC) --crate-type=dylib bar1.rs + $(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic $(RUSTC) --crate-type=rlib bar2.rs - $(RUSTC) --crate-type=dylib,rlib baz2.rs + $(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* $(RUSTC) --crate-type=rlib foo.rs $(RUSTC) --crate-type=rlib bar1.rs $(RUSTC) --crate-type=rlib bar2.rs - $(RUSTC) --crate-type=dylib,rlib baz2.rs + $(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* - $(RUSTC) --crate-type=dylib foo.rs + $(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic $(RUSTC) --crate-type=rlib bar1.rs $(RUSTC) --crate-type=rlib bar2.rs - $(RUSTC) --crate-type=dylib,rlib baz2.rs + $(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* - $(RUSTC) --crate-type=dylib foo.rs - $(RUSTC) --crate-type=dylib bar1.rs + $(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic + $(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic $(RUSTC) --crate-type=rlib bar2.rs $(RUSTC) --crate-type=dylib,rlib baz2.rs $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* - $(RUSTC) --crate-type=dylib foo.rs + $(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic $(RUSTC) --crate-type=rlib bar1.rs - $(RUSTC) --crate-type=dylib bar2.rs + $(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic $(RUSTC) --crate-type=dylib,rlib baz2.rs $(RUSTC) --crate-type=bin baz2.rs rm $(TMPDIR)/* - $(RUSTC) --crate-type=dylib foo.rs - $(RUSTC) --crate-type=dylib bar1.rs - $(RUSTC) --crate-type=dylib bar2.rs + $(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic + $(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic + $(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic $(RUSTC) --crate-type=dylib,rlib baz2.rs $(RUSTC) --crate-type=bin baz2.rs diff --git a/src/test/run-make/prefer-dylib/Makefile b/src/test/run-make/prefer-dylib/Makefile index fe9bbb95095ba..bd44feecf2a5d 100644 --- a/src/test/run-make/prefer-dylib/Makefile +++ b/src/test/run-make/prefer-dylib/Makefile @@ -1,7 +1,7 @@ -include ../tools.mk all: - $(RUSTC) bar.rs --crate-type=dylib --crate-type=rlib + $(RUSTC) bar.rs --crate-type=dylib --crate-type=rlib -C prefer-dynamic $(RUSTC) foo.rs -C prefer-dynamic $(call RUN,foo) rm $(TMPDIR)/*bar* diff --git a/src/test/run-make/simple-dylib/Makefile b/src/test/run-make/simple-dylib/Makefile index 84e6e079e6f06..26730820fea2b 100644 --- a/src/test/run-make/simple-dylib/Makefile +++ b/src/test/run-make/simple-dylib/Makefile @@ -1,5 +1,5 @@ -include ../tools.mk all: - $(RUSTC) bar.rs --crate-type=dylib + $(RUSTC) bar.rs --crate-type=dylib -C prefer-dynamic $(RUSTC) foo.rs $(call RUN,foo) diff --git a/src/test/run-make/static-dylib-by-default/Makefile b/src/test/run-make/static-dylib-by-default/Makefile new file mode 100644 index 0000000000000..20bed0abbfd37 --- /dev/null +++ b/src/test/run-make/static-dylib-by-default/Makefile @@ -0,0 +1,9 @@ +-include ../tools.mk + +all: + $(RUSTC) foo.rs + $(RUSTC) bar.rs + $(CC) main.c -o $(call RUN_BINFILE,main) -lbar + rm $(TMPDIR)/*.rlib + rm $(call DYLIB,foo) + $(call RUN,main) diff --git a/src/test/run-make/static-dylib-by-default/bar.rs b/src/test/run-make/static-dylib-by-default/bar.rs new file mode 100644 index 0000000000000..63da277dece47 --- /dev/null +++ b/src/test/run-make/static-dylib-by-default/bar.rs @@ -0,0 +1,18 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "dylib"] + +extern crate foo; + +#[no_mangle] +pub extern fn bar() { + foo::foo(); +} diff --git a/src/test/run-make/static-dylib-by-default/foo.rs b/src/test/run-make/static-dylib-by-default/foo.rs new file mode 100644 index 0000000000000..341040e653c5c --- /dev/null +++ b/src/test/run-make/static-dylib-by-default/foo.rs @@ -0,0 +1,14 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "rlib"] +#![crate_type = "dylib"] + +pub fn foo() {} diff --git a/src/test/run-make/static-dylib-by-default/main.c b/src/test/run-make/static-dylib-by-default/main.c new file mode 100644 index 0000000000000..30bb0783edfb1 --- /dev/null +++ b/src/test/run-make/static-dylib-by-default/main.c @@ -0,0 +1,16 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern void bar(); + +int main() { + bar(); + return 0; +} diff --git a/src/test/run-make/suspicious-library/Makefile b/src/test/run-make/suspicious-library/Makefile index 621f3064b5ced..12f437075fbd4 100644 --- a/src/test/run-make/suspicious-library/Makefile +++ b/src/test/run-make/suspicious-library/Makefile @@ -1,7 +1,7 @@ -include ../tools.mk all: - $(RUSTC) foo.rs + $(RUSTC) foo.rs -C prefer-dynamic touch $(call DYLIB,foo-something-special) touch $(call DYLIB,foo-something-special2) $(RUSTC) bar.rs diff --git a/src/test/run-make/symlinked-libraries/Makefile b/src/test/run-make/symlinked-libraries/Makefile index 9eb2c13523006..ac595546aa7c7 100644 --- a/src/test/run-make/symlinked-libraries/Makefile +++ b/src/test/run-make/symlinked-libraries/Makefile @@ -4,7 +4,7 @@ ifndef IS_WINDOWS all: - $(RUSTC) foo.rs + $(RUSTC) foo.rs -C prefer-dynamic mkdir -p $(TMPDIR)/other ln -nsf $(TMPDIR)/$(call DYLIB_GLOB,foo) $(TMPDIR)/other $(RUSTC) bar.rs -L $(TMPDIR)/other