Skip to content
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

Fix cross-compiling with dlltool for raw-dylib #108355

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

dpaoliello
Copy link
Contributor

@dpaoliello dpaoliello commented Feb 22, 2023

Fix for #103939

Issue Details:
When attempting to cross-compile using the raw-dylib feature and the GNU toolchain, rustc would attempt to find a cross-compiling version of dlltool (e.g., i686-w64-mingw32-dlltool). The has two issues 1) on Windows dlltool is always dlltool (no cross-compiling named versions exist) and 2) it only supported compiling to i686 and x86_64 resulting in ARM 32 and 64 compiling as x86_64.

Fix Details:

(This is the first of two PRs to fix the remaining issues for the raw-dylib feature (#58713) that is blocking stabilization (#104218))

@rustbot
Copy link
Collaborator

rustbot commented Feb 22, 2023

r? @nagisa

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 22, 2023
@dpaoliello
Copy link
Contributor Author

r? @michaelwoerister

@rustbot rustbot assigned michaelwoerister and unassigned nagisa Feb 23, 2023
@mati865
Copy link
Contributor

mati865 commented Feb 23, 2023

This will not work when cross-compiling because on Linux there is no dlltool (since this tool is meant only for Windows targets), only {triple}-dlltool are available:
https://archlinux.org/packages/community/x86_64/mingw-w64-binutils/files/
https://packages.ubuntu.com/kinetic/amd64/binutils-mingw-w64-x86-64/filelist

@dpaoliello
Copy link
Contributor Author

This will not work when cross-compiling because on Linux there is no dlltool (since this tool is meant only for Windows targets), only {triple}-dlltool are available: https://archlinux.org/packages/community/x86_64/mingw-w64-binutils/files/ https://packages.ubuntu.com/kinetic/amd64/binutils-mingw-w64-x86-64/filelist

Well, that's fun. Also means that the original code was broken if cross-compiling from non-Windows for Windows with the same architecture:

> rustc +nightly test.rs --target x86_64-pc-windows-gnu
error: Error calling dlltool: No such file or directory (os error 2)

error: aborting due to previous error

@dpaoliello dpaoliello changed the title Use -m option instead of looking for a cross-compiling version of dlltool Fix cross-compiling with dlltool Feb 24, 2023
@dpaoliello dpaoliello changed the title Fix cross-compiling with dlltool Fix cross-compiling with dlltool for raw-dylib Feb 24, 2023
@michaelwoerister
Copy link
Member

Thanks a lot for taking this on, @dpaoliello!

@michaelwoerister
Copy link
Member

michaelwoerister commented Feb 24, 2023

I'd really like us to have proper tests for this before we merge the PR, at least for the cases:

  • x86_64-pc-windows-gnu -> x86_64-pc-windows-gnu
  • x86_64-pc-windows-gnu -> i686-pc-windows-gnu
  • x86_64-unknown-linux-gnu -> x86_64-pc-windows-gnu
  • x86_64-unknown-linux-gnu -> i686-pc-windows-gnu (?)

It seems to be straightforward to cross compiling simple libraries as part of a test, as long as the library does not depend on libcore or libstd (which we don't need here). Here's an example for a UI test:

// build-pass
// only-linux

// Run the test for both i686 and x86_64
// revisions: i686 x86_64
// [i686]compile-flags:--target=i686-pc-windows-gnu
// [x86_64]compile-flags:--target=x86_64-pc-windows-gnu

#![feature(raw_dylib)]
#![feature(no_core, lang_items)]
#![no_std]
#![no_core]
#![crate_type = "lib"]

// This is needed because of #![no_core]:
#[lang = "sized"]
trait Sized {}

// The DLL does not actually exist anywhere, but we don't care.
// We just want rustc to successfully build an import library
// from the extern block.
#[link(name = "extern_1", kind = "raw-dylib")]
extern "C" {
    fn extern_fn_2();
    fn print_extern_variable();
    static mut extern_variable: i32;
    #[link_name = "extern_fn_4"]
    fn extern_fn_4_renamed();
}

pub unsafe fn foo() {
    extern_fn_2();
    print_extern_variable();
    extern_variable = 123;
    extern_fn_4_renamed();
}

This test will fail if it cannot find and invoke the expected dlltool. However, the downside is that we then would require any system running tests to have binutils-mingw-w64-x86-64 installed.

The x86_64-pc-windows-gnu -> *-pc-windows-gnu cases should be easier, since we can make such tests only-x86_64-pc-windows-gnu.

Another thing I noticed is that there might be an additional set of flags needed, as described here:
microsoft/windows-rs@cb8d6a9#diff-82947385e42042ffba4e2ba53c683427146ce3bf9e7c15ef45ad38e624a66444R100-R106

That PR seems to apply them unconditionally (at least on Windows?). Maybe we should add a (run-make) test that does a check as described in the corresponding bug report.

@mati865
Copy link
Contributor

mati865 commented Feb 24, 2023

The x86_64-pc-windows-gnu -> *-pc-windows-gnu cases should be easier, since we can make such tests only-x86_64-pc-windows-gnu.

This test would fail on both CI and on user systems because typically only one toolchain is available in PATH (either i686 or x86_64 or AArch64).

@michaelwoerister
Copy link
Member

Even if the host only ever is x86_64-pc-windows-gnu?

@mati865
Copy link
Contributor

mati865 commented Feb 24, 2023

I mean you could only test x86_64-pc-windows-gnu -> x86_64-pc-windows-gnu and not x86_64-pc-windows-gnu -> i686-pc-windows-gnu.

@dpaoliello
Copy link
Contributor Author

I'd really like us to have proper tests for this before we merge the PR, at least for the cases:

  • x86_64-pc-windows-gnu -> x86_64-pc-windows-gnu
  • x86_64-pc-windows-gnu -> i686-pc-windows-gnu
  • x86_64-unknown-linux-gnu -> x86_64-pc-windows-gnu
  • x86_64-unknown-linux-gnu -> i686-pc-windows-gnu (?)

Done.

This test would fail on both CI and on user systems because typically only one toolchain is available in PATH (either i686 or x86_64 or AArch64).

Ony my local testing it seemed to work - let's see what the CI does...

Another thing I noticed is that there might be an additional set of flags needed, as described here: microsoft/windows-rs@cb8d6a9#diff-82947385e42042ffba4e2ba53c683427146ce3bf9e7c15ef45ad38e624a66444R100-R106

That PR seems to apply them unconditionally (at least on Windows?). Maybe we should add a (run-make) test that does a check as described in the corresponding bug report.

Good catch - added.

@rust-log-analyzer

This comment has been minimized.

@michaelwoerister
Copy link
Member

I'm not sure what's going on here. rustc tries to call dlltool (without .exe) which seems to indicate that this branch is hit: https://github.com/rust-lang/rust/pull/108355/files#diff-0629e2ee816d38d7591985254f2e34523289cf29188ff24c5d4fba05ae839de5R449 -- but I don't understand why, since rustc is being called with --target i686-pc-windows-gnu as expected. The test failure doesn't reproduce for me locally either.

Copy link
Member

@michaelwoerister michaelwoerister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the CI failure, but there seem to be a few small errors in the Makefile that lead to the test passing without actually testing anything.

tests/run-make/raw-dylib-cross-compilation/Makefile Outdated Show resolved Hide resolved
tests/run-make/raw-dylib-cross-compilation/Makefile Outdated Show resolved Hide resolved
tests/run-make/raw-dylib-cross-compilation/Makefile Outdated Show resolved Hide resolved
@mati865
Copy link
Contributor

mati865 commented Feb 27, 2023

I'm not sure what's going on here. rustc tries to call dlltool (without .exe) which seems to indicate that this branch is hit: #108355 (files) -- but I don't understand why, since rustc is being called with --target i686-pc-windows-gnu as expected. The test failure doesn't reproduce for me locally either.

Pretty sure the error message doesn't tell the actual executable and just uses hardcoded dlltool but haven't bothered to check.
If I'm right this error would be expected because there is no i686-w64-mingw32-dlltool installed on that runner.

@michaelwoerister
Copy link
Member

Yes, indeed -- thanks, @mati865!

That should be fixable by installing the binutils-mingw-w64 package similar to the mingw-check Dockerfile.

But this highlights the problem already mentioned before: If we add the test in its current form, everyone who wants to run tests locally will have to install binutils-mingw-w64. I'm not sure that's acceptable.

There's precedent for ignoring tests when the environment doesn't fulfill certain requirements (e.g. xLTO tests need Clang). We may have to go down that route if we can't find a more elegant way.

@dpaoliello
Copy link
Contributor Author

Ok, here's my workaround: we try to detect if i686-w64-mingw32-dlltool is present (and use it if it is), otherwise we use the -Zdlltool arg to redirect to x86_64-w64-mingw32-dlltool instead.

@rust-log-analyzer

This comment has been minimized.

@bors bors added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 11, 2023
@rust-log-analyzer

This comment has been minimized.

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2023
…nk-jobs, r=Mark-Simulacrum

Limit the number of parallel link jobs during LLVM build for mingw.

This PR is an attempt to unblock rust-lang#108355, which keeps failing while trying to link various LLVM artifacts on mingw runners. It looks like doing too many linking jobs might put too much load on the system? (Although I don't understand why the jobs are only failing for rust-lang#108355 while they seem to pass for others)

r? infra-ci
@michaelwoerister
Copy link
Member

#109073 has been merged, maybe that helps.
@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 22, 2023
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Mar 22, 2023
…r=Mark-Simulacrum

Limit the number of parallel link jobs during LLVM build for mingw.

This PR is an attempt to unblock rust-lang/rust#108355, which keeps failing while trying to link various LLVM artifacts on mingw runners. It looks like doing too many linking jobs might put too much load on the system? (Although I don't understand why the jobs are only failing for #108355 while they seem to pass for others)

r? infra-ci
@bors
Copy link
Contributor

bors commented Mar 22, 2023

⌛ Testing commit 94aaac6c6715291c994643dc59f616ca77a913b6 with merge 2eb6699bd74c7f5b3d1b98907bf4b8dada17997d...

@bors
Copy link
Contributor

bors commented Mar 22, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 22, 2023
@rust-log-analyzer

This comment has been minimized.

@dpaoliello
Copy link
Contributor Author

Looks like i686 dlltool.exe can't produce x64 binaries, so I'm ignoring that target triple in the test for now.

@michaelwoerister
Copy link
Member

Thanks, @dpaoliello!
@bors r+

@bors
Copy link
Contributor

bors commented Mar 23, 2023

📌 Commit a90f342 has been approved by michaelwoerister

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2023
@bors
Copy link
Contributor

bors commented Mar 23, 2023

⌛ Testing commit a90f342 with merge 9a6b0c3...

@bors
Copy link
Contributor

bors commented Mar 23, 2023

☀️ Test successful - checks-actions
Approved by: michaelwoerister
Pushing 9a6b0c3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 23, 2023
@bors bors merged commit 9a6b0c3 into rust-lang:master Mar 23, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 23, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9a6b0c3): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.4% [3.4%, 3.4%] 1
Regressions ❌
(secondary)
2.0% [0.9%, 3.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.7% [-1.1%, -0.4%] 5
All ❌✅ (primary) 3.4% [3.4%, 3.4%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.6% [-0.7%, -0.5%] 4
All ❌✅ (primary) - - 0

@dpaoliello dpaoliello deleted the dlltoolm branch March 23, 2023 17:03
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 20, 2024
…r=Mark-Simulacrum

Limit the number of parallel link jobs during LLVM build for mingw.

This PR is an attempt to unblock rust-lang/rust#108355, which keeps failing while trying to link various LLVM artifacts on mingw runners. It looks like doing too many linking jobs might put too much load on the system? (Although I don't understand why the jobs are only failing for #108355 while they seem to pass for others)

r? infra-ci
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
…r=Mark-Simulacrum

Limit the number of parallel link jobs during LLVM build for mingw.

This PR is an attempt to unblock rust-lang/rust#108355, which keeps failing while trying to link various LLVM artifacts on mingw runners. It looks like doing too many linking jobs might put too much load on the system? (Although I don't understand why the jobs are only failing for #108355 while they seem to pass for others)

r? infra-ci
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants