Skip to content

Commit 83f96e8

Browse files
committed
Add unstable feature flags
1 parent 39ba9da commit 83f96e8

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,22 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
9191
//
9292
// Treat cdylibs and staticlibs similarly. If `-C prefer-dynamic` is set,
9393
// the caller may be code-size conscious, but without it, it makes sense
94-
// to statically link a cdylib or staticlib.
95-
CrateType::Dylib | CrateType::Cdylib | CrateType::Staticlib => {
96-
if sess.opts.cg.prefer_dynamic { Linkage::Dynamic } else { Linkage::Static }
94+
// to statically link a cdylib or staticlib. For staticlibs we use
95+
// `-Z staticlib-prefer-dynamic` for now. This may be merged into
96+
// `-C prefer-dynamic` in the future.
97+
CrateType::Dylib | CrateType::Cdylib => {
98+
if sess.opts.cg.prefer_dynamic {
99+
Linkage::Dynamic
100+
} else {
101+
Linkage::Static
102+
}
103+
}
104+
CrateType::Staticlib => {
105+
if sess.opts.unstable_opts.staticlib_prefer_dynamic {
106+
Linkage::Dynamic
107+
} else {
108+
Linkage::Static
109+
}
97110
}
98111

99112
// If the global prefer_dynamic switch is turned off, or the final
@@ -123,9 +136,10 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
123136

124137
// Static executables must have all static dependencies.
125138
// If any are not found, generate some nice pretty errors.
126-
if ty == CrateType::Executable
127-
&& sess.crt_static(Some(ty))
128-
&& !sess.target.crt_static_allows_dylibs
139+
if (ty == CrateType::Staticlib && !sess.opts.unstable_opts.staticlib_allow_rdylib_deps)
140+
|| (ty == CrateType::Executable
141+
&& sess.crt_static(Some(ty))
142+
&& !sess.target.crt_static_allows_dylibs)
129143
{
130144
for &cnum in tcx.crates(()).iter() {
131145
if tcx.dep_kind(cnum).macros_only() {

compiler/rustc_session/src/options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,10 @@ options! {
17091709
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]
17101710
stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED],
17111711
"control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"),
1712+
staticlib_allow_rdylib_deps: bool = (false, parse_bool, [TRACKED],
1713+
"allow staticlibs to have rust dylib dependencies"),
1714+
staticlib_prefer_dynamic: bool = (false, parse_bool, [TRACKED],
1715+
"prefer dynamic linking to static linking for staticlibs (default: no)"),
17121716
strict_init_checks: bool = (false, parse_bool, [TRACKED],
17131717
"control if mem::uninitialized and mem::zeroed panic on more UB"),
17141718
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],

tests/run-make/staticlib-dylib-linkage/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ include ../tools.mk
22

33
all:
44
$(RUSTC) -C prefer-dynamic bar.rs
5-
$(RUSTC) foo.rs --crate-type staticlib --print native-static-libs 2>&1 | grep 'note: native-static-libs: ' | sed 's/note: native-static-libs: \(.*\)/\1/' > $(TMPDIR)/libs.txt
5+
$(RUSTC) foo.rs --crate-type staticlib --print native-static-libs \
6+
-Z staticlib-allow-rdylib-deps 2>&1 | grep 'note: native-static-libs: ' \
7+
| sed 's/note: native-static-libs: \(.*\)/\1/' > $(TMPDIR)/libs.txt
68
cat $(TMPDIR)/libs.txt
79

810
ifdef IS_MSVC

0 commit comments

Comments
 (0)