Skip to content
/ rust Public
forked from rust-lang/rust

Commit d46cf6a

Browse files
authored
Rollup merge of rust-lang#134999 - Berrysoft:dev/new-cygwin-target, r=chenyukang,workingjubilee
Add cygwin target. This PR simply adds cygwin target together with msys2 target, based on ```@ookiineko``` 's (the account has been deleted) [work](https://github.com/ookiineko-cygport/rust) on cygwin target. My full work is here: rust-lang/rust@master...Berrysoft:rust:dev/cygwin I have succeeded in building a new rustc for cygwin target, and eventually distributed a new version of [fish-shell](https://github.com/Berrysoft/fish-shell/releases) (rewritten by Rust) for MSYS2. I will open a new PR to fix std if this PR is accepted.
2 parents 9fcc9cf + 8e5207e commit d46cf6a

File tree

10 files changed

+125
-5
lines changed

10 files changed

+125
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::borrow::Cow;
2+
3+
use crate::spec::{Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs};
4+
5+
pub(crate) fn opts() -> TargetOptions {
6+
let mut pre_link_args = TargetOptions::link_args(
7+
LinkerFlavor::Gnu(Cc::No, Lld::No),
8+
&["--disable-dynamicbase", "--enable-auto-image-base"],
9+
);
10+
crate::spec::add_link_args(
11+
&mut pre_link_args,
12+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
13+
&["-Wl,--disable-dynamicbase", "-Wl,--enable-auto-image-base"],
14+
);
15+
let cygwin_libs = &["-lcygwin", "-lgcc", "-lcygwin", "-luser32", "-lkernel32", "-lgcc_s"];
16+
let mut late_link_args =
17+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), cygwin_libs);
18+
crate::spec::add_link_args(
19+
&mut late_link_args,
20+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
21+
cygwin_libs,
22+
);
23+
TargetOptions {
24+
os: "cygwin".into(),
25+
vendor: "pc".into(),
26+
// FIXME(#13846) this should be enabled for cygwin
27+
function_sections: false,
28+
linker: Some("gcc".into()),
29+
dynamic_linking: true,
30+
dll_prefix: "".into(),
31+
dll_suffix: ".dll".into(),
32+
exe_suffix: ".exe".into(),
33+
families: cvs!["unix"],
34+
is_like_windows: true,
35+
allows_weak_linkage: false,
36+
pre_link_args,
37+
late_link_args,
38+
abi_return_struct_as_int: true,
39+
emit_debug_gdb_scripts: false,
40+
requires_uwtable: true,
41+
eh_frame_header: false,
42+
debuginfo_kind: DebuginfoKind::Dwarf,
43+
supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
44+
..Default::default()
45+
}
46+
}

compiler/rustc_target/src/spec/base/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(crate) mod android;
33
pub(crate) mod apple;
44
pub(crate) mod avr_gnu;
55
pub(crate) mod bpf;
6+
pub(crate) mod cygwin;
67
pub(crate) mod dragonfly;
78
pub(crate) mod freebsd;
89
pub(crate) mod fuchsia;

compiler/rustc_target/src/spec/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ supported_targets! {
20132013
("riscv64imac-unknown-nuttx-elf", riscv64imac_unknown_nuttx_elf),
20142014
("riscv64gc-unknown-nuttx-elf", riscv64gc_unknown_nuttx_elf),
20152015

2016+
("x86_64-pc-cygwin", x86_64_pc_cygwin),
20162017
}
20172018

20182019
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
@@ -2994,8 +2995,8 @@ impl Target {
29942995
);
29952996
check_eq!(
29962997
self.is_like_windows,
2997-
self.os == "windows" || self.os == "uefi",
2998-
"`is_like_windows` must be set if and only if `os` is `windows` or `uefi`"
2998+
self.os == "windows" || self.os == "uefi" || self.os == "cygwin",
2999+
"`is_like_windows` must be set if and only if `os` is `windows`, `uefi` or `cygwin`"
29993000
);
30003001
check_eq!(
30013002
self.is_like_wasm,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::spec::{Cc, LinkerFlavor, Lld, Target, base};
2+
3+
pub(crate) fn target() -> Target {
4+
let mut base = base::cygwin::opts();
5+
base.cpu = "x86-64".into();
6+
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), &["-m", "i386pep"]);
7+
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]);
8+
base.max_atomic_width = Some(64);
9+
base.linker = Some("x86_64-pc-cygwin-gcc".into());
10+
Target {
11+
llvm_target: "x86_64-pc-cygwin".into(),
12+
pointer_width: 64,
13+
data_layout:
14+
"e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
15+
arch: "x86_64".into(),
16+
options: base,
17+
metadata: crate::spec::TargetMetadata {
18+
description: Some("64-bit x86 Cygwin".into()),
19+
tier: Some(3),
20+
host_tools: Some(false),
21+
std: None,
22+
},
23+
}
24+
}

src/bootstrap/src/core/builder/cargo.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ impl Cargo {
245245
// flesh out rpath support more fully in the future.
246246
self.rustflags.arg("-Zosx-rpath-install-name");
247247
Some(format!("-Wl,-rpath,@loader_path/../{libdir}"))
248-
} else if !target.is_windows() && !target.contains("aix") && !target.contains("xous") {
248+
} else if !target.is_windows()
249+
&& !target.contains("cygwin")
250+
&& !target.contains("aix")
251+
&& !target.contains("xous")
252+
{
249253
self.rustflags.arg("-Clink-args=-Wl,-z,origin");
250254
Some(format!("-Wl,-rpath,$ORIGIN/../{libdir}"))
251255
} else {

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
- [\*-win7-windows-gnu](platform-support/win7-windows-gnu.md)
103103
- [\*-win7-windows-msvc](platform-support/win7-windows-msvc.md)
104104
- [x86_64-fortanix-unknown-sgx](platform-support/x86_64-fortanix-unknown-sgx.md)
105+
- [x86_64-pc-cygwin](platform-support/x86_64-pc-cygwin.md)
105106
- [x86_64-pc-solaris](platform-support/solaris.md)
106107
- [x86_64-unknown-linux-none.md](platform-support/x86_64-unknown-linux-none.md)
107108
- [x86_64-unknown-none](platform-support/x86_64-unknown-none.md)

src/doc/rustc/src/platform-support.md

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ target | std | host | notes
407407
[`wasm64-unknown-unknown`](platform-support/wasm64-unknown-unknown.md) | ? | | WebAssembly
408408
[`x86_64-apple-tvos`](platform-support/apple-tvos.md) | ✓ | | x86 64-bit tvOS
409409
[`x86_64-apple-watchos-sim`](platform-support/apple-watchos.md) | ✓ | | x86 64-bit Apple WatchOS simulator
410+
[`x86_64-pc-cygwin`](platform-support/x86_64-pc-cygwin.md) | ? | | 64-bit x86 Cygwin |
410411
[`x86_64-pc-nto-qnx710`](platform-support/nto-qnx.md) | ✓ | | x86 64-bit QNX Neutrino 7.1 RTOS with default network stack (io-pkt) |
411412
[`x86_64-pc-nto-qnx710_iosock`](platform-support/nto-qnx.md) | ✓ | | x86 64-bit QNX Neutrino 7.1 RTOS with new network stack (io-sock) |
412413
[`x86_64-pc-nto-qnx800`](platform-support/nto-qnx.md) | ✓ | | x86 64-bit QNX Neutrino 8.0 RTOS |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# `x86_64-pc-cygwin`
2+
3+
**Tier: 3**
4+
5+
Windows targets supporting Cygwin.
6+
The `*-cygwin` targets are **not** intended as native target for applications,
7+
a developer writing Windows applications should use the `*-pc-windows-*` targets instead, which are *native* Windows.
8+
9+
Cygwin is only intended as an emulation layer for Unix-only programs which do not support the native Windows targets.
10+
11+
## Target maintainers
12+
13+
- [Berrysoft](https://github.com/Berrysoft)
14+
15+
## Requirements
16+
17+
This target is cross compiled. It needs `x86_64-pc-cygwin-gcc` as linker.
18+
19+
The `target_os` of the target is `cygwin`, and it is `unix`.
20+
21+
## Building the target
22+
23+
For cross-compilation you want LLVM with [llvm/llvm-project#121439 (merged)](https://github.com/llvm/llvm-project/pull/121439) applied to fix the LLVM codegen on importing external global variables from DLLs.
24+
No native builds on Cygwin now. It should be possible theoretically though, but might need a lot of patches.
25+
26+
## Building Rust programs
27+
28+
Rust does not yet ship pre-compiled artifacts for this target. To compile for
29+
this target, you will either need to build Rust with the target enabled (see
30+
"Building the target" above), or build your own copy of `core` by using
31+
`build-std` or similar.
32+
33+
## Testing
34+
35+
Created binaries work fine on Windows with Cygwin.
36+
37+
## Cross-compilation toolchains and C code
38+
39+
Compatible C code can be built with GCC shipped with Cygwin. Clang is untested.

tests/assembly/targets/targets-pe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
//@ revisions: x86_64_win7_windows_msvc
8585
//@ [x86_64_win7_windows_msvc] compile-flags: --target x86_64-win7-windows-msvc
8686
//@ [x86_64_win7_windows_msvc] needs-llvm-components: x86
87+
//@ revisions: x86_64_pc_cygwin
88+
//@ [x86_64_pc_cygwin] compile-flags: --target x86_64-pc-cygwin
89+
//@ [x86_64_pc_cygwin] needs-llvm-components: x86
8790

8891
// Sanity-check that each target can produce assembly code.
8992

tests/ui/check-cfg/well-known-values.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
201201
LL | target_os = "_UNEXPECTED_VALUE",
202202
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203203
|
204-
= note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm`
204+
= note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm`
205205
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
206206

207207
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
@@ -274,7 +274,7 @@ LL | #[cfg(target_os = "linuz")] // testing that we suggest `linux`
274274
| |
275275
| help: there is a expected value with a similar name: `"linux"`
276276
|
277-
= note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm`
277+
= note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm`
278278
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
279279

280280
warning: 28 warnings emitted

0 commit comments

Comments
 (0)