forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#106560 - bjorn3:support_staticlib_dylib_linki…
…ng, r=pnkfelix Support linking to rust dylib with --crate-type staticlib This allows for example dynamically linking libstd, while statically linking the user crate into an executable or C dynamic library. For this two unstable flags (`-Z staticlib-allow-rdylib-deps` and `-Z staticlib-prefer-dynamic`) are introduced. Without the former you get an error. The latter is the equivalent to `-C prefer-dynamic` for the staticlib crate type to indicate that dynamically linking is preferred when both options are available, like for libstd. Care must be taken to ensure that no crate ends up being merged into two distinct staticlibs that are linked together. Doing so will cause a linker error at best and undefined behavior at worst. In addition two distinct staticlibs compiled by different rustc may not be combined under any circumstances due to some rustc private symbols not being mangled. To successfully link a staticlib, `--print native-static-libs` can be used while compiling to ask rustc for the linker flags necessary when linking the staticlib. This is an existing flag which previously only listed native libraries. It has been extended to list rust dylibs too. Trying to locate libstd yourself to link against it is not supported and may break if for example the libstd of multiple rustc versions are put in the same directory. For an example on how to use this see the `src/test/run-make-fulldeps/staticlib-dylib-linkage/` test.
- Loading branch information
Showing
7 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
include ../tools.mk | ||
|
||
# ignore-cross-compile | ||
# ignore-msvc FIXME(bjorn3) can't figure out how to link with the MSVC toolchain | ||
# ignore-wasm wasm doesn't support dynamic libraries | ||
|
||
all: | ||
$(RUSTC) -C prefer-dynamic bar.rs | ||
$(RUSTC) foo.rs --crate-type staticlib --print native-static-libs \ | ||
-Z staticlib-allow-rdylib-deps 2>&1 | grep 'note: native-static-libs: ' \ | ||
| sed 's/note: native-static-libs: \(.*\)/\1/' > $(TMPDIR)/libs.txt | ||
cat $(TMPDIR)/libs.txt | ||
|
||
ifdef IS_MSVC | ||
$(CC) $(CFLAGS) /c foo.c /Fo:$(TMPDIR)/foo.o | ||
$(RUSTC_LINKER) $(TMPDIR)/foo.o $(TMPDIR)/foo.lib $$(cat $(TMPDIR)/libs.txt) $(call OUT_EXE,foo) | ||
else | ||
$(CC) $(CFLAGS) foo.c -L $(TMPDIR) -lfoo $$(cat $(TMPDIR)/libs.txt) -o $(call RUN_BINFILE,foo) | ||
endif | ||
|
||
$(call RUN,foo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![crate_type = "dylib"] | ||
|
||
pub fn bar() { | ||
println!("hello!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <assert.h> | ||
|
||
extern void foo(); | ||
extern unsigned bar(unsigned a, unsigned b); | ||
|
||
int main() { | ||
foo(); | ||
assert(bar(1, 2) == 3); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![crate_type = "staticlib"] | ||
|
||
extern crate bar; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn foo() { | ||
bar::bar(); | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn bar(a: u32, b: u32) -> u32 { | ||
a + b | ||
} |