Skip to content

Commit 6d4e2f2

Browse files
committed
Add tests
1 parent 674a7ad commit 6d4e2f2

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

Diff for: tests/run-make/embed-metadata/dep1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn func_dep1() {}

Diff for: tests/run-make/embed-metadata/foo.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate dep1;
2+
3+
fn main() {
4+
dep1::func_dep1();
5+
}

Diff for: tests/run-make/embed-metadata/rmake.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use run_make_support::rfs::{create_dir, remove_file, rename};
2+
use run_make_support::{Rustc, dynamic_lib_name, path, run_in_tmpdir, rust_lib_name, rustc};
3+
4+
#[derive(Debug, Copy, Clone)]
5+
enum LibraryKind {
6+
Rlib,
7+
Dylib,
8+
}
9+
10+
impl LibraryKind {
11+
fn crate_type(&self) -> &str {
12+
match self {
13+
LibraryKind::Rlib => "rlib",
14+
LibraryKind::Dylib => "dylib",
15+
}
16+
}
17+
18+
fn add_extern(&self, rustc: &mut Rustc, dep_name: &str, dep_path: &str) {
19+
let dep_path = match self {
20+
LibraryKind::Dylib => format!("{dep_path}/{}", dynamic_lib_name(dep_name)),
21+
LibraryKind::Rlib => format!("{dep_path}/{}", rust_lib_name(dep_name)),
22+
};
23+
rustc.extern_(dep_name, dep_path);
24+
}
25+
}
26+
27+
fn main() {
28+
// The compiler takes different paths based on if --extern is passed or not, so we test all
29+
// combinations (`rlib`/`dylib` x `--extern`/`no --extern`).
30+
for kind in [LibraryKind::Rlib, LibraryKind::Dylib] {
31+
eprintln!("Testing library kind {kind:?}");
32+
lookup_rmeta_in_lib_dir(kind);
33+
lookup_rmeta_through_extern(kind);
34+
lookup_rmeta_missing(kind);
35+
}
36+
}
37+
38+
// Lookup .rmeta file in the same directory as a rlib/dylib with stub metadata.
39+
fn lookup_rmeta_in_lib_dir(kind: LibraryKind) {
40+
run_in_tmpdir(|| {
41+
build_dep_rustc(kind).run();
42+
rustc().input("foo.rs").run();
43+
});
44+
}
45+
46+
// Lookup .rmeta file when specifying the dependency using --extern.
47+
fn lookup_rmeta_through_extern(kind: LibraryKind) {
48+
run_in_tmpdir(|| {
49+
// Generate libdep1.rlib and libdep1.rmeta in deps
50+
create_dir("deps");
51+
build_dep_rustc(kind).out_dir("deps").run();
52+
53+
let mut rustc = rustc();
54+
kind.add_extern(&mut rustc, "dep1", "deps");
55+
rustc.extern_("dep1", path("deps").join("libdep1.rmeta"));
56+
rustc.input("foo.rs").run();
57+
});
58+
}
59+
60+
// Check the error message when the .rmeta file is missing.
61+
fn lookup_rmeta_missing(kind: LibraryKind) {
62+
run_in_tmpdir(|| {
63+
create_dir("deps");
64+
build_dep_rustc(kind).out_dir("deps").run();
65+
66+
let mut rustc = rustc();
67+
kind.add_extern(&mut rustc, "dep1", "deps");
68+
rustc.input("foo.rs").run_fail().assert_stderr_contains("only metadata stub found");
69+
});
70+
}
71+
72+
fn build_dep_rustc(kind: LibraryKind) -> Rustc {
73+
let mut dep_rustc = rustc();
74+
dep_rustc
75+
.arg("-Zembed-metadata=no")
76+
.crate_type(kind.crate_type())
77+
.input("dep1.rs")
78+
.emit("metadata,link");
79+
if matches!(kind, LibraryKind::Dylib) {
80+
dep_rustc.arg("-Cprefer-dynamic");
81+
}
82+
dep_rustc
83+
}

0 commit comments

Comments
 (0)