Skip to content

Commit 274a7ac

Browse files
authored
Merge pull request #1145 from bjorn3/mingw_support
Support cross-compiling to Windows using MinGW
2 parents 9a0c329 + 00f1cdd commit 274a7ac

File tree

10 files changed

+156
-56
lines changed

10 files changed

+156
-56
lines changed

.github/workflows/main.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
os: [ubuntu-latest, macos-latest]
14+
include:
15+
- os: ubuntu-latest
16+
- os: macos-latest
17+
# cross-compile from Linux to Windows using mingw
18+
- os: ubuntu-latest
19+
env:
20+
TARGET_TRIPLE: x86_64-pc-windows-gnu
1521

1622
steps:
1723
- uses: actions/checkout@v2
@@ -36,13 +42,21 @@ jobs:
3642
path: target
3743
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
3844

45+
- name: Install MinGW toolchain and wine
46+
if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
47+
run: |
48+
sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable
49+
rustup target add x86_64-pc-windows-gnu
50+
3951
- name: Prepare dependencies
4052
run: |
4153
git config --global user.email "[email protected]"
4254
git config --global user.name "User"
4355
./prepare.sh
4456
4557
- name: Test
58+
env:
59+
TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }}
4660
run: |
4761
# Enable backtraces for easier debugging
4862
export RUST_BACKTRACE=1
@@ -57,6 +71,7 @@ jobs:
5771
run: tar cvfJ cg_clif.tar.xz build
5872

5973
- name: Upload prebuilt cg_clif
74+
if: matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu'
6075
uses: actions/upload-artifact@v2
6176
with:
6277
name: cg_clif-${{ runner.os }}

Cargo.lock

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.sh

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ln target/$CHANNEL/*rustc_codegen_cranelift* "$target_dir"/lib
5555
ln rust-toolchain scripts/config.sh scripts/cargo.sh "$target_dir"
5656

5757
mkdir -p "$target_dir/lib/rustlib/$TARGET_TRIPLE/lib/"
58+
mkdir -p "$target_dir/lib/rustlib/$HOST_TRIPLE/lib/"
5859
if [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then
5960
cp $(rustc --print sysroot)/lib/rustlib/$TARGET_TRIPLE/lib/*.o "$target_dir/lib/rustlib/$TARGET_TRIPLE/lib/"
6061
fi
@@ -64,12 +65,18 @@ case "$build_sysroot" in
6465
;;
6566
"llvm")
6667
cp -r $(rustc --print sysroot)/lib/rustlib/$TARGET_TRIPLE/lib "$target_dir/lib/rustlib/$TARGET_TRIPLE/"
68+
if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
69+
cp -r $(rustc --print sysroot)/lib/rustlib/$HOST_TRIPLE/lib "$target_dir/lib/rustlib/$HOST_TRIPLE/"
70+
fi
6771
;;
6872
"clif")
6973
echo "[BUILD] sysroot"
7074
dir=$(pwd)
7175
cd "$target_dir"
7276
time "$dir/build_sysroot/build_sysroot.sh"
77+
if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
78+
time TARGET_TRIPLE="$HOST_TRIPLE" "$dir/build_sysroot/build_sysroot.sh"
79+
fi
7380
cp lib/rustlib/*/lib/libstd-* lib/
7481
;;
7582
*)

example/mini_core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ struct PanicLocation {
621621
}
622622

623623
#[no_mangle]
624+
#[cfg(not(windows))]
624625
pub fn get_tls() -> u8 {
625626
#[thread_local]
626627
static A: u8 = 42;

example/mini_core_hello_world.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn main() {
239239

240240
assert_eq!(((|()| 42u8) as fn(()) -> u8)(()), 42);
241241

242-
#[cfg(not(jit))]
242+
#[cfg(not(any(jit, windows)))]
243243
{
244244
extern {
245245
#[linkage = "extern_weak"]
@@ -292,7 +292,7 @@ fn main() {
292292

293293
from_decimal_string();
294294

295-
#[cfg(not(jit))]
295+
#[cfg(not(any(jit, windows)))]
296296
test_tls();
297297

298298
#[cfg(all(not(jit), target_os = "linux"))]

example/std_example.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ fn main() {
1616
let mut stderr = stderr.lock();
1717

1818
// FIXME support lazy jit when multi threading
19-
#[cfg(not(lazy_jit))]
19+
// FIXME support TLS on windows
20+
#[cfg(not(any(lazy_jit, windows)))]
2021
std::thread::spawn(move || {
2122
println!("Hello from another thread!");
2223
});

scripts/tests.sh

+32-13
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@ function base_sysroot_tests() {
7171
echo "[AOT] mod_bench"
7272
$MY_RUSTC example/mod_bench.rs --crate-type bin --target "$TARGET_TRIPLE"
7373
$RUN_WRAPPER ./target/out/mod_bench
74+
}
7475

76+
function extended_sysroot_tests() {
7577
pushd rand
76-
rm -r ./target || true
77-
../build/cargo.sh test --workspace
78+
cargo clean
79+
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
80+
echo "[TEST] rust-random/rand"
81+
../build/cargo.sh test --workspace
82+
else
83+
echo "[AOT] rust-random/rand"
84+
../build/cargo.sh build --workspace --target $TARGET_TRIPLE --tests
85+
fi
7886
popd
79-
}
8087

81-
function extended_sysroot_tests() {
8288
pushd simple-raytracer
8389
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
8490
echo "[BENCH COMPILE] ebobby/simple-raytracer"
@@ -92,27 +98,40 @@ function extended_sysroot_tests() {
9298
else
9399
echo "[BENCH COMPILE] ebobby/simple-raytracer (skipped)"
94100
echo "[COMPILE] ebobby/simple-raytracer"
95-
../cargo.sh build
101+
../build/cargo.sh build --target $TARGET_TRIPLE
96102
echo "[BENCH RUN] ebobby/simple-raytracer (skipped)"
97103
fi
98104
popd
99105

100106
pushd build_sysroot/sysroot_src/library/core/tests
101107
echo "[TEST] libcore"
102-
rm -r ./target || true
103-
../../../../../build/cargo.sh test
108+
cargo clean
109+
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
110+
../../../../../build/cargo.sh test
111+
else
112+
../../../../../build/cargo.sh build --target $TARGET_TRIPLE --tests
113+
fi
104114
popd
105115

106116
pushd regex
107117
echo "[TEST] rust-lang/regex example shootout-regex-dna"
108-
../build/cargo.sh clean
118+
cargo clean
109119
# Make sure `[codegen mono items] start` doesn't poison the diff
110-
../build/cargo.sh build --example shootout-regex-dna
111-
cat examples/regexdna-input.txt | ../build/cargo.sh run --example shootout-regex-dna | grep -v "Spawned thread" > res.txt
112-
diff -u res.txt examples/regexdna-output.txt
120+
../build/cargo.sh build --example shootout-regex-dna --target $TARGET_TRIPLE
121+
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
122+
cat examples/regexdna-input.txt \
123+
| ../build/cargo.sh run --example shootout-regex-dna --target $TARGET_TRIPLE \
124+
| grep -v "Spawned thread" > res.txt
125+
diff -u res.txt examples/regexdna-output.txt
126+
fi
113127

114-
echo "[TEST] rust-lang/regex tests"
115-
../build/cargo.sh test --tests -- --exclude-should-panic --test-threads 1 -Zunstable-options -q
128+
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
129+
echo "[TEST] rust-lang/regex tests"
130+
../build/cargo.sh test --tests -- --exclude-should-panic --test-threads 1 -Zunstable-options -q
131+
else
132+
echo "[AOT] rust-lang/regex tests"
133+
../build/cargo.sh build --tests --target $TARGET_TRIPLE
134+
fi
116135
popd
117136
}
118137

src/codegen_i128.rs

+70-19
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,56 @@ pub(crate) fn maybe_codegen<'tcx>(
3232
BinOp::Add | BinOp::Sub if !checked => None,
3333
BinOp::Mul if !checked => {
3434
let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
35-
Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
35+
if fx.tcx.sess.target.is_like_windows {
36+
let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
37+
let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
38+
let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
39+
assert!(lhs_extra.is_none());
40+
assert!(rhs_extra.is_none());
41+
let args =
42+
[ret_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
43+
fx.lib_call(
44+
"__multi3",
45+
vec![
46+
AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
47+
AbiParam::new(pointer_ty(fx.tcx)),
48+
AbiParam::new(pointer_ty(fx.tcx)),
49+
],
50+
vec![],
51+
&args,
52+
);
53+
Some(ret_place.to_cvalue(fx))
54+
} else {
55+
Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
56+
}
3657
}
3758
BinOp::Add | BinOp::Sub | BinOp::Mul => {
3859
assert!(checked);
3960
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
4061
let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
41-
let param_types = vec![
42-
AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
43-
AbiParam::new(types::I128),
44-
AbiParam::new(types::I128),
45-
];
46-
let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)];
62+
let (param_types, args) = if fx.tcx.sess.target.is_like_windows {
63+
let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
64+
let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
65+
assert!(lhs_extra.is_none());
66+
assert!(rhs_extra.is_none());
67+
(
68+
vec![
69+
AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
70+
AbiParam::new(pointer_ty(fx.tcx)),
71+
AbiParam::new(pointer_ty(fx.tcx)),
72+
],
73+
[out_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)],
74+
)
75+
} else {
76+
(
77+
vec![
78+
AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
79+
AbiParam::new(types::I128),
80+
AbiParam::new(types::I128),
81+
],
82+
[out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)],
83+
)
84+
};
4785
let name = match (bin_op, is_signed) {
4886
(BinOp::Add, false) => "__rust_u128_addo",
4987
(BinOp::Add, true) => "__rust_i128_addo",
@@ -57,20 +95,33 @@ pub(crate) fn maybe_codegen<'tcx>(
5795
Some(out_place.to_cvalue(fx))
5896
}
5997
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
60-
BinOp::Div => {
98+
BinOp::Div | BinOp::Rem => {
6199
assert!(!checked);
62-
if is_signed {
63-
Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
64-
} else {
65-
Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
66-
}
67-
}
68-
BinOp::Rem => {
69-
assert!(!checked);
70-
if is_signed {
71-
Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
100+
let name = match (bin_op, is_signed) {
101+
(BinOp::Div, false) => "__udivti3",
102+
(BinOp::Div, true) => "__divti3",
103+
(BinOp::Rem, false) => "__umodti3",
104+
(BinOp::Rem, true) => "__modti3",
105+
_ => unreachable!(),
106+
};
107+
if fx.tcx.sess.target.is_like_windows {
108+
let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
109+
let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
110+
assert!(lhs_extra.is_none());
111+
assert!(rhs_extra.is_none());
112+
let args = [lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
113+
let ret = fx.lib_call(
114+
name,
115+
vec![AbiParam::new(pointer_ty(fx.tcx)), AbiParam::new(pointer_ty(fx.tcx))],
116+
vec![AbiParam::new(types::I64X2)],
117+
&args,
118+
)[0];
119+
// FIXME use bitcast instead of store to get from i64x2 to i128
120+
let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
121+
ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
122+
Some(ret_place.to_cvalue(fx))
72123
} else {
73-
Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
124+
Some(fx.easy_call(name, &[lhs, rhs], lhs.layout().ty))
74125
}
75126
}
76127
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {

0 commit comments

Comments
 (0)