Skip to content

Commit 17a3152

Browse files
committed
Auto merge of #125872 - fmease:rollup-ei6igof, r=fmease
Rollup of 4 pull requests Successful merges: - #124294 (Unroll first iteration of checked_ilog loop) - #125773 (Migrate run make cdylib) - #125808 (Migrate `run-make/c-link-to-rust-dylib` to `rmake.rs`) - #125822 (Refactor `--print=check-cfg` test) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f67a1ac + 86db13b commit 17a3152

File tree

10 files changed

+221
-124
lines changed

10 files changed

+221
-124
lines changed

library/core/src/num/uint_macros.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,12 @@ macro_rules! uint_impl {
11481148
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
11491149
if self <= 0 || base <= 1 {
11501150
None
1151+
} else if self < base {
1152+
Some(0)
11511153
} else {
1152-
let mut n = 0;
1153-
let mut r = 1;
1154+
// Since base >= self, n >= 1
1155+
let mut n = 1;
1156+
let mut r = base;
11541157

11551158
// Optimization for 128 bit wide integers.
11561159
if Self::BITS == 128 {

src/tools/run-make-support/src/cc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ impl Cc {
8080
self
8181
}
8282

83+
/// Specify path of the output binary.
84+
pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
85+
self.cmd.arg("-o");
86+
self.cmd.arg(path.as_ref());
87+
self
88+
}
89+
8390
/// Get the [`Output`][::std::process::Output] of the finished process.
8491
pub fn command_output(&mut self) -> ::std::process::Output {
8592
self.cmd.output().expect("failed to get output of finished process")

src/tools/run-make-support/src/lib.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,23 @@ pub fn dynamic_lib_name(name: &str) -> String {
123123
// ```
124124
assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");
125125

126+
let extension = dynamic_lib_extension();
126127
if is_darwin() {
127-
format!("lib{name}.dylib")
128+
format!("lib{name}.{extension}")
128129
} else if is_windows() {
129-
format!("{name}.dll")
130+
format!("{name}.{extension}")
130131
} else {
131-
format!("lib{name}.so")
132+
format!("lib{name}.{extension}")
133+
}
134+
}
135+
136+
pub fn dynamic_lib_extension() -> &'static str {
137+
if is_darwin() {
138+
"dylib"
139+
} else if is_windows() {
140+
"dll"
141+
} else {
142+
"so"
132143
}
133144
}
134145

@@ -249,16 +260,13 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
249260
}
250261

251262
let dir2 = dir2.as_ref();
252-
for entry in fs::read_dir(dir1).unwrap() {
253-
let entry = entry.unwrap();
254-
let entry_name = entry.file_name();
255-
let path = entry.path();
256-
257-
if path.is_dir() {
258-
recursive_diff(&path, &dir2.join(entry_name));
263+
read_dir(dir1, |entry_path| {
264+
let entry_name = entry_path.file_name().unwrap();
265+
if entry_path.is_dir() {
266+
recursive_diff(&entry_path, &dir2.join(entry_name));
259267
} else {
260268
let path2 = dir2.join(entry_name);
261-
let file1 = read_file(&path);
269+
let file1 = read_file(&entry_path);
262270
let file2 = read_file(&path2);
263271

264272
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
@@ -267,10 +275,16 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
267275
assert!(
268276
file1 == file2,
269277
"`{}` and `{}` have different content",
270-
path.display(),
278+
entry_path.display(),
271279
path2.display(),
272280
);
273281
}
282+
});
283+
}
284+
285+
pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) {
286+
for entry in fs::read_dir(dir).unwrap() {
287+
callback(&entry.unwrap().path());
274288
}
275289
}
276290

src/tools/tidy/src/allowed_run_make_makefiles.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ run-make/bare-outfile/Makefile
66
run-make/branch-protection-check-IBT/Makefile
77
run-make/c-dynamic-dylib/Makefile
88
run-make/c-dynamic-rlib/Makefile
9-
run-make/c-link-to-rust-dylib/Makefile
109
run-make/c-static-dylib/Makefile
1110
run-make/c-static-rlib/Makefile
1211
run-make/c-unwind-abi-catch-lib-panic/Makefile
1312
run-make/c-unwind-abi-catch-panic/Makefile
1413
run-make/cat-and-grep-sanity-check/Makefile
1514
run-make/cdylib-dylib-linkage/Makefile
1615
run-make/cdylib-fewer-symbols/Makefile
17-
run-make/cdylib/Makefile
1816
run-make/codegen-options-parsing/Makefile
1917
run-make/comment-section/Makefile
2018
run-make/compiler-lookup-paths-2/Makefile

tests/codegen/checked_ilog.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// Ensure that when val < base, we do not divide or multiply.
6+
7+
// CHECK-LABEL: @checked_ilog
8+
// CHECK-SAME: (i16 noundef %val, i16 noundef %base)
9+
#[no_mangle]
10+
pub fn checked_ilog(val: u16, base: u16) -> Option<u32> {
11+
// CHECK-NOT: udiv
12+
// CHECK-NOT: mul
13+
// CHECK: %[[IS_LESS:.+]] = icmp ult i16 %val, %base
14+
// CHECK-NEXT: br i1 %[[IS_LESS]], label %[[TRUE:.+]], label %[[FALSE:.+]]
15+
// CHECK: [[TRUE]]:
16+
// CHECK-NOT: udiv
17+
// CHECK-NOT: mul
18+
// CHECK: ret { i32, i32 }
19+
val.checked_ilog(base)
20+
}

tests/run-make/c-link-to-rust-dylib/Makefile

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This test checks that C linking with Rust does not encounter any errors, with dynamic libraries.
2+
// See <https://github.com/rust-lang/rust/issues/10434>.
3+
4+
//@ ignore-cross-compile
5+
6+
use std::fs::remove_file;
7+
8+
use run_make_support::{
9+
dynamic_lib_extension, is_msvc, read_dir, run, run_fail, rustc, tmp_dir, Cc,
10+
};
11+
12+
fn main() {
13+
rustc().input("foo.rs").run();
14+
15+
if is_msvc() {
16+
Cc::new().input("bar.c").arg(tmp_dir().join("foo.dll.lib")).out_exe("bar").run();
17+
} else {
18+
Cc::new()
19+
.input("bar.c")
20+
.arg("-lfoo")
21+
.output(tmp_dir().join("bar"))
22+
.library_search_path(tmp_dir())
23+
.run();
24+
}
25+
26+
run("bar");
27+
28+
let expected_extension = dynamic_lib_extension();
29+
read_dir(tmp_dir(), |path| {
30+
if path.is_file()
31+
&& path.extension().is_some_and(|ext| ext == expected_extension)
32+
&& path
33+
.file_name()
34+
.and_then(|name| name.to_str())
35+
.is_some_and(|name| name.starts_with("lib"))
36+
{
37+
remove_file(path).unwrap();
38+
}
39+
});
40+
run_fail("bar");
41+
}

tests/run-make/cdylib/Makefile

-23
This file was deleted.

tests/run-make/cdylib/rmake.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This test tries to check that basic cdylib libraries can be compiled and linked successfully
2+
// with C code, that the cdylib itself can depend on another rlib, and that the library can be built
3+
// with LTO.
4+
//
5+
// - `bar.rs` is a rlib
6+
// - `foo.rs` is a cdylib that relies on an extern crate `bar` and defines two `extern "C"`
7+
// functions:
8+
// - `foo()` which calls `bar::bar()`.
9+
// - `bar()` which implements basic addition.
10+
11+
//@ ignore-cross-compile
12+
13+
use std::fs::remove_file;
14+
15+
use run_make_support::{cc, dynamic_lib, is_msvc, run, rustc, tmp_dir};
16+
17+
fn main() {
18+
rustc().input("bar.rs").run();
19+
rustc().input("foo.rs").run();
20+
21+
if is_msvc() {
22+
cc().input("foo.c").arg(tmp_dir().join("foo.dll.lib")).out_exe("foo").run();
23+
} else {
24+
cc().input("foo.c")
25+
.arg("-lfoo")
26+
.output(tmp_dir().join("foo"))
27+
.library_search_path(tmp_dir())
28+
.run();
29+
}
30+
31+
run("foo");
32+
remove_file(dynamic_lib("foo")).unwrap();
33+
34+
rustc().input("foo.rs").arg("-Clto").run();
35+
run("foo");
36+
}

0 commit comments

Comments
 (0)