Skip to content

Rollup of 4 pull requests #125872

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 12 commits 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: 5 additions & 2 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,9 +1148,12 @@ macro_rules! uint_impl {
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
if self <= 0 || base <= 1 {
None
} else if self < base {
Some(0)
} else {
let mut n = 0;
let mut r = 1;
// Since base >= self, n >= 1
let mut n = 1;
let mut r = base;

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
Expand Down
7 changes: 7 additions & 0 deletions src/tools/run-make-support/src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ impl Cc {
self
}

/// Specify path of the output binary.
pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.cmd.arg("-o");
self.cmd.arg(path.as_ref());
self
}

/// Get the [`Output`][::std::process::Output] of the finished process.
pub fn command_output(&mut self) -> ::std::process::Output {
self.cmd.output().expect("failed to get output of finished process")
Expand Down
38 changes: 26 additions & 12 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,23 @@ pub fn dynamic_lib_name(name: &str) -> String {
// ```
assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");

let extension = dynamic_lib_extension();
if is_darwin() {
format!("lib{name}.dylib")
format!("lib{name}.{extension}")
} else if is_windows() {
format!("{name}.dll")
format!("{name}.{extension}")
} else {
format!("lib{name}.so")
format!("lib{name}.{extension}")
}
}

pub fn dynamic_lib_extension() -> &'static str {
if is_darwin() {
"dylib"
} else if is_windows() {
"dll"
} else {
"so"
}
}

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

let dir2 = dir2.as_ref();
for entry in fs::read_dir(dir1).unwrap() {
let entry = entry.unwrap();
let entry_name = entry.file_name();
let path = entry.path();

if path.is_dir() {
recursive_diff(&path, &dir2.join(entry_name));
read_dir(dir1, |entry_path| {
let entry_name = entry_path.file_name().unwrap();
if entry_path.is_dir() {
recursive_diff(&entry_path, &dir2.join(entry_name));
} else {
let path2 = dir2.join(entry_name);
let file1 = read_file(&path);
let file1 = read_file(&entry_path);
let file2 = read_file(&path2);

// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
Expand All @@ -267,10 +275,16 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
assert!(
file1 == file2,
"`{}` and `{}` have different content",
path.display(),
entry_path.display(),
path2.display(),
);
}
});
}

pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) {
for entry in fs::read_dir(dir).unwrap() {
callback(&entry.unwrap().path());
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ run-make/bare-outfile/Makefile
run-make/branch-protection-check-IBT/Makefile
run-make/c-dynamic-dylib/Makefile
run-make/c-dynamic-rlib/Makefile
run-make/c-link-to-rust-dylib/Makefile
run-make/c-static-dylib/Makefile
run-make/c-static-rlib/Makefile
run-make/c-unwind-abi-catch-lib-panic/Makefile
run-make/c-unwind-abi-catch-panic/Makefile
run-make/cat-and-grep-sanity-check/Makefile
run-make/cdylib-dylib-linkage/Makefile
run-make/cdylib-fewer-symbols/Makefile
run-make/cdylib/Makefile
run-make/codegen-options-parsing/Makefile
run-make/comment-section/Makefile
run-make/compiler-lookup-paths-2/Makefile
Expand Down
20 changes: 20 additions & 0 deletions tests/codegen/checked_ilog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ compile-flags: -O

#![crate_type = "lib"]

// Ensure that when val < base, we do not divide or multiply.

// CHECK-LABEL: @checked_ilog
// CHECK-SAME: (i16 noundef %val, i16 noundef %base)
#[no_mangle]
pub fn checked_ilog(val: u16, base: u16) -> Option<u32> {
// CHECK-NOT: udiv
// CHECK-NOT: mul
// CHECK: %[[IS_LESS:.+]] = icmp ult i16 %val, %base
// CHECK-NEXT: br i1 %[[IS_LESS]], label %[[TRUE:.+]], label %[[FALSE:.+]]
// CHECK: [[TRUE]]:
// CHECK-NOT: udiv
// CHECK-NOT: mul
// CHECK: ret { i32, i32 }
val.checked_ilog(base)
}
21 changes: 0 additions & 21 deletions tests/run-make/c-link-to-rust-dylib/Makefile

This file was deleted.

41 changes: 41 additions & 0 deletions tests/run-make/c-link-to-rust-dylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This test checks that C linking with Rust does not encounter any errors, with dynamic libraries.
// See <https://github.com./rust-lang/rust/issues/10434>.

//@ ignore-cross-compile

use std::fs::remove_file;

use run_make_support::{
dynamic_lib_extension, is_msvc, read_dir, run, run_fail, rustc, tmp_dir, Cc,
};

fn main() {
rustc().input("foo.rs").run();

if is_msvc() {
Cc::new().input("bar.c").arg(tmp_dir().join("foo.dll.lib")).out_exe("bar").run();
} else {
Cc::new()
.input("bar.c")
.arg("-lfoo")
.output(tmp_dir().join("bar"))
.library_search_path(tmp_dir())
.run();
}

run("bar");

let expected_extension = dynamic_lib_extension();
read_dir(tmp_dir(), |path| {
if path.is_file()
&& path.extension().is_some_and(|ext| ext == expected_extension)
&& path
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("lib"))
{
remove_file(path).unwrap();
}
});
run_fail("bar");
}
23 changes: 0 additions & 23 deletions tests/run-make/cdylib/Makefile

This file was deleted.

36 changes: 36 additions & 0 deletions tests/run-make/cdylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This test tries to check that basic cdylib libraries can be compiled and linked successfully
// with C code, that the cdylib itself can depend on another rlib, and that the library can be built
// with LTO.
//
// - `bar.rs` is a rlib
// - `foo.rs` is a cdylib that relies on an extern crate `bar` and defines two `extern "C"`
// functions:
// - `foo()` which calls `bar::bar()`.
// - `bar()` which implements basic addition.

//@ ignore-cross-compile

use std::fs::remove_file;

use run_make_support::{cc, dynamic_lib, is_msvc, run, rustc, tmp_dir};

fn main() {
rustc().input("bar.rs").run();
rustc().input("foo.rs").run();

if is_msvc() {
cc().input("foo.c").arg(tmp_dir().join("foo.dll.lib")).out_exe("foo").run();
} else {
cc().input("foo.c")
.arg("-lfoo")
.output(tmp_dir().join("foo"))
.library_search_path(tmp_dir())
.run();
}

run("foo");
remove_file(dynamic_lib("foo")).unwrap();

rustc().input("foo.rs").arg("-Clto").run();
run("foo");
}
Loading
Loading