Skip to content

rustc: Default to static linking dylibs #18592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mk/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions mk/target.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)))" \
Expand Down
10 changes: 10 additions & 0 deletions src/librustc/middle/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {},
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/auxiliary/issue-12133-dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 0 additions & 1 deletion src/test/auxiliary/syntax-extension-with-dll-deps-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 0 additions & 1 deletion src/test/auxiliary/syntax-extension-with-dll-deps-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

// force-host
// no-prefer-dynamic

#![crate_type = "dylib"]
#![feature(plugin_registrar, quote, globs)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/c-dynamic-dylib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/c-static-dylib/Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-make/dylib-chain/Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/issue-15460/Makefile
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/test/run-make/mixing-deps/Makefile
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 19 additions & 19 deletions src/test/run-make/mixing-formats/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/test/run-make/prefer-dylib/Makefile
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/simple-dylib/Makefile
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions src/test/run-make/static-dylib-by-default/Makefile
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions src/test/run-make/static-dylib-by-default/bar.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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();
}
14 changes: 14 additions & 0 deletions src/test/run-make/static-dylib-by-default/foo.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}
16 changes: 16 additions & 0 deletions src/test/run-make/static-dylib-by-default/main.c
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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;
}
2 changes: 1 addition & 1 deletion src/test/run-make/suspicious-library/Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/test/run-make/symlinked-libraries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down