Skip to content

Commit 3036b00

Browse files
committed
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]
1 parent ff50f24 commit 3036b00

File tree

20 files changed

+105
-41
lines changed

20 files changed

+105
-41
lines changed

mk/main.mk

+4-3
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ endif
148148
# libraries, so in the interest of space, prefer dynamic linking throughout the
149149
# compilation process.
150150
#
151-
# Note though that these flags are omitted for stage2+. This means that the
152-
# snapshot will be generated with a statically linked rustc so we only have to
153-
# worry about the distribution of one file (with its native dynamic
151+
# Note though that these flags are omitted for the *bins* in stage2+. This means
152+
# that the snapshot will be generated with a statically linked rustc so we only
153+
# have to worry about the distribution of one file (with its native dynamic
154154
# dependencies)
155155
RUSTFLAGS_STAGE0 += -C prefer-dynamic
156156
RUSTFLAGS_STAGE1 += -C prefer-dynamic
157+
RUST_LIB_FLAGS_ST2 += -C prefer-dynamic
157158

158159
# Landing pads require a lot of codegen. We can get through bootstrapping faster
159160
# by not emitting them.

mk/target.mk

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export CFG_COMPILER_HOST_TRIPLE
1717
# code, make sure that these common warnings are denied by default. These can
1818
# be overridden during development temporarily. For stage0, we allow warnings
1919
# which may be bugs in stage0 (should be fixed in stage1+)
20-
WFLAGS_ST0 = -W warnings
21-
WFLAGS_ST1 = -D warnings
22-
WFLAGS_ST2 = -D warnings
20+
RUST_LIB_FLAGS_ST0 += -W warnings
21+
RUST_LIB_FLAGS_ST1 += -D warnings
22+
RUST_LIB_FLAGS_ST2 += -D warnings
2323

2424
# Macro that generates the full list of dependencies for a crate at a particular
2525
# stage/target/host tuple.
@@ -80,7 +80,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
8080
$$(call REMOVE_ALL_OLD_GLOB_MATCHES, \
8181
$$(dir $$@)$$(call CFG_RLIB_GLOB,$(4)))
8282
$$(STAGE$(1)_T_$(2)_H_$(3)) \
83-
$$(WFLAGS_ST$(1)) \
83+
$$(RUST_LIB_FLAGS_ST$(1)) \
8484
-L "$$(RT_OUTPUT_DIR_$(2))" \
8585
-L "$$(LLVM_LIBDIR_$(2))" \
8686
-L "$$(dir $$(LLVM_STDCPP_LOCATION_$(2)))" \

src/librustc/middle/dependency_format.rs

+10
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ fn calculate_type(sess: &session::Session,
123123
return Vec::new();
124124
}
125125

126+
// Generating a dylib without `-C prefer-dynamic` means that we're going
127+
// to try to eagerly statically link all dependencies. This is normally
128+
// done for end-product dylibs, not intermediate products.
129+
config::CrateTypeDylib if !sess.opts.cg.prefer_dynamic => {
130+
match attempt_static(sess) {
131+
Some(v) => return v,
132+
None => {}
133+
}
134+
}
135+
126136
// Everything else falls through below
127137
config::CrateTypeExecutable | config::CrateTypeDylib => {},
128138
}

src/test/auxiliary/issue-12133-dylib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// no-prefer-dynamic
12-
1311
#![crate_type = "dylib"]

src/test/auxiliary/syntax-extension-with-dll-deps-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// no-prefer-dynamic
1211
// force-host
1312

1413
#![crate_type = "dylib"]

src/test/auxiliary/syntax-extension-with-dll-deps-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
// force-host
12-
// no-prefer-dynamic
1312

1413
#![crate_type = "dylib"]
1514
#![feature(plugin_registrar, quote, globs)]

src/test/run-make/c-dynamic-dylib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ all:
66
echo ignored
77
else
88
all: $(call DYLIB,cfoo)
9-
$(RUSTC) foo.rs
9+
$(RUSTC) foo.rs -C prefer-dynamic
1010
$(RUSTC) bar.rs
1111
$(call RUN,bar)
1212
$(call REMOVE_DYLIBS,cfoo)

src/test/run-make/c-static-dylib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-include ../tools.mk
22

33
all: $(call STATICLIB,cfoo)
4-
$(RUSTC) foo.rs
4+
$(RUSTC) foo.rs -C prefer-dynamic
55
$(RUSTC) bar.rs
66
rm $(TMPDIR)/$(call STATICLIB_GLOB,cfoo)
77
$(call RUN,bar)

src/test/run-make/dylib-chain/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) m1.rs
5-
$(RUSTC) m2.rs
6-
$(RUSTC) m3.rs
4+
$(RUSTC) m1.rs -C prefer-dynamic
5+
$(RUSTC) m2.rs -C prefer-dynamic
6+
$(RUSTC) m3.rs -C prefer-dynamic
77
$(RUSTC) m4.rs
88
$(call RUN,m4)
99
$(call REMOVE_DYLIBS,m1)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-include ../tools.mk
22

33
all: $(TMPDIR)/libfoo.a
4-
$(RUSTC) foo.rs -C extra-filename=-383hf8
4+
$(RUSTC) foo.rs -C extra-filename=-383hf8 -C prefer-dynamic
55
$(RUSTC) bar.rs
66
$(call RUN,bar)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) both.rs
4+
$(RUSTC) both.rs -C prefer-dynamic
55
$(RUSTC) dylib.rs -C prefer-dynamic
66
$(RUSTC) prog.rs
77
$(call RUN,prog)

src/test/run-make/mixing-formats/Makefile

+19-19
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,60 @@
1515
all:
1616
# Building just baz
1717
$(RUSTC) --crate-type=rlib foo.rs
18-
$(RUSTC) --crate-type=dylib bar1.rs
19-
$(RUSTC) --crate-type=dylib,rlib baz.rs
18+
$(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic
19+
$(RUSTC) --crate-type=dylib,rlib baz.rs -C prefer-dynamic
2020
$(RUSTC) --crate-type=bin baz.rs
2121
rm $(TMPDIR)/*
22-
$(RUSTC) --crate-type=dylib foo.rs
22+
$(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic
2323
$(RUSTC) --crate-type=rlib bar1.rs
24-
$(RUSTC) --crate-type=dylib,rlib baz.rs
24+
$(RUSTC) --crate-type=dylib,rlib baz.rs -C prefer-dynamic
2525
$(RUSTC) --crate-type=bin baz.rs
2626
rm $(TMPDIR)/*
2727
# Building baz2
2828
$(RUSTC) --crate-type=rlib foo.rs
29-
$(RUSTC) --crate-type=dylib bar1.rs
30-
$(RUSTC) --crate-type=dylib bar2.rs
29+
$(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic
30+
$(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic
3131
$(RUSTC) --crate-type=dylib baz2.rs && exit 1 || exit 0
3232
$(RUSTC) --crate-type=bin baz2.rs && exit 1 || exit 0
3333
rm $(TMPDIR)/*
3434
$(RUSTC) --crate-type=rlib foo.rs
3535
$(RUSTC) --crate-type=rlib bar1.rs
36-
$(RUSTC) --crate-type=dylib bar2.rs
36+
$(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic
3737
$(RUSTC) --crate-type=dylib,rlib baz2.rs
3838
$(RUSTC) --crate-type=bin baz2.rs
3939
rm $(TMPDIR)/*
4040
$(RUSTC) --crate-type=rlib foo.rs
41-
$(RUSTC) --crate-type=dylib bar1.rs
41+
$(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic
4242
$(RUSTC) --crate-type=rlib bar2.rs
43-
$(RUSTC) --crate-type=dylib,rlib baz2.rs
43+
$(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic
4444
$(RUSTC) --crate-type=bin baz2.rs
4545
rm $(TMPDIR)/*
4646
$(RUSTC) --crate-type=rlib foo.rs
4747
$(RUSTC) --crate-type=rlib bar1.rs
4848
$(RUSTC) --crate-type=rlib bar2.rs
49-
$(RUSTC) --crate-type=dylib,rlib baz2.rs
49+
$(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic
5050
$(RUSTC) --crate-type=bin baz2.rs
5151
rm $(TMPDIR)/*
52-
$(RUSTC) --crate-type=dylib foo.rs
52+
$(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic
5353
$(RUSTC) --crate-type=rlib bar1.rs
5454
$(RUSTC) --crate-type=rlib bar2.rs
55-
$(RUSTC) --crate-type=dylib,rlib baz2.rs
55+
$(RUSTC) --crate-type=dylib,rlib baz2.rs -C prefer-dynamic
5656
$(RUSTC) --crate-type=bin baz2.rs
5757
rm $(TMPDIR)/*
58-
$(RUSTC) --crate-type=dylib foo.rs
59-
$(RUSTC) --crate-type=dylib bar1.rs
58+
$(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic
59+
$(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic
6060
$(RUSTC) --crate-type=rlib bar2.rs
6161
$(RUSTC) --crate-type=dylib,rlib baz2.rs
6262
$(RUSTC) --crate-type=bin baz2.rs
6363
rm $(TMPDIR)/*
64-
$(RUSTC) --crate-type=dylib foo.rs
64+
$(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic
6565
$(RUSTC) --crate-type=rlib bar1.rs
66-
$(RUSTC) --crate-type=dylib bar2.rs
66+
$(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic
6767
$(RUSTC) --crate-type=dylib,rlib baz2.rs
6868
$(RUSTC) --crate-type=bin baz2.rs
6969
rm $(TMPDIR)/*
70-
$(RUSTC) --crate-type=dylib foo.rs
71-
$(RUSTC) --crate-type=dylib bar1.rs
72-
$(RUSTC) --crate-type=dylib bar2.rs
70+
$(RUSTC) --crate-type=dylib foo.rs -C prefer-dynamic
71+
$(RUSTC) --crate-type=dylib bar1.rs -C prefer-dynamic
72+
$(RUSTC) --crate-type=dylib bar2.rs -C prefer-dynamic
7373
$(RUSTC) --crate-type=dylib,rlib baz2.rs
7474
$(RUSTC) --crate-type=bin baz2.rs

src/test/run-make/prefer-dylib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) bar.rs --crate-type=dylib --crate-type=rlib
4+
$(RUSTC) bar.rs --crate-type=dylib --crate-type=rlib -C prefer-dynamic
55
$(RUSTC) foo.rs -C prefer-dynamic
66
$(call RUN,foo)
77
rm $(TMPDIR)/*bar*
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-include ../tools.mk
22
all:
3-
$(RUSTC) bar.rs --crate-type=dylib
3+
$(RUSTC) bar.rs --crate-type=dylib -C prefer-dynamic
44
$(RUSTC) foo.rs
55
$(call RUN,foo)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) foo.rs
5+
$(RUSTC) bar.rs
6+
$(CC) main.c -o $(call RUN_BINFILE,main) -lbar
7+
rm $(TMPDIR)/*.rlib
8+
rm $(call DYLIB,foo)
9+
$(call RUN,main)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "dylib"]
12+
13+
extern crate foo;
14+
15+
#[no_mangle]
16+
pub extern fn bar() {
17+
foo::foo();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
#![crate_type = "dylib"]
13+
14+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern void bar();
12+
13+
int main() {
14+
bar();
15+
return 0;
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) foo.rs
4+
$(RUSTC) foo.rs -C prefer-dynamic
55
touch $(call DYLIB,foo-something-special)
66
touch $(call DYLIB,foo-something-special2)
77
$(RUSTC) bar.rs

src/test/run-make/symlinked-libraries/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ifndef IS_WINDOWS
55

66
all:
7-
$(RUSTC) foo.rs
7+
$(RUSTC) foo.rs -C prefer-dynamic
88
mkdir -p $(TMPDIR)/other
99
ln -nsf $(TMPDIR)/$(call DYLIB_GLOB,foo) $(TMPDIR)/other
1010
$(RUSTC) bar.rs -L $(TMPDIR)/other

0 commit comments

Comments
 (0)