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

Add mingwW64-llvm cross-system. #171418

Merged
merged 18 commits into from
May 18, 2022
Merged
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
9 changes: 9 additions & 0 deletions lib/systems/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ rec {
libc = "msvcrt"; # This distinguishes the mingw (non posix) toolchain
};

# 64 bit mingw-w64 with a llvm-based toolchain targetting ucrt
#
# Inspired by mstorsjo/llvm-mingw
mingwW64-llvm = {
config = "x86_64-w64-mingw32";
libc = "ucrt";
useLLVM = true;
};

# BSDs

amd64-netbsd = lib.warn "The amd64-netbsd system example is deprecated. Use x86_64-netbsd instead." x86_64-netbsd;
Expand Down
6 changes: 6 additions & 0 deletions pkgs/build-support/bintools-wrapper/add-lld-ldflags-before.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ld.lld has two incompatible command-line drivers: One for the gnu-compatible COFF linker and one for
# the ELF linker. If no emulation is set (with -m), it will default to the ELF linker;
# unfortunately, some configure scripts use `ld --help` to check for certain Windows-specific flags,
# which don't show up in the help for the ELF linker. So we set a default -m here.

extraBefore+=("-m" "@mtype@")
32 changes: 32 additions & 0 deletions pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

# Darwin code signing support utilities
, postLinkSignHook ? null, signingUtils ? null

# Linker type
, isLld ? bintools.isLld or false
, isCctools ? bintools.isCctools or false
, isGNU ? bintools.isGNU or false
, isGold ? bintools.isGold or false
, isBfd ? bintools.isBfd or false
}:

with lib;
Expand Down Expand Up @@ -113,6 +120,8 @@ stdenv.mkDerivation {
passthru = {
inherit bintools libc nativeTools nativeLibc nativePrefix;

inherit isLld isCctools isGNU isGold isBfd;

emacsBufferSetup = pkgs: ''
; We should handle propagation here too
(mapc
Expand Down Expand Up @@ -326,6 +335,11 @@ stdenv.mkDerivation {
echo "-arch ${targetPlatform.darwinArch}" >> $out/nix-support/libc-ldflags
''

# lld's MinGW driver (e.g. `ld.lld -m i386pep`) does not support the `-z` flag.
+ optionalString (targetPlatform.isWindows && isLld) ''
hardening_unsupported_flags+=" relro bindnow"
''

##
## GNU specific extra strip flags
##
Expand Down Expand Up @@ -370,6 +384,24 @@ stdenv.mkDerivation {
''
)

##
## Set the default machine type so that $prefix-ld.lld uses the COFF driver for --help
##
## Needed because autotools parses --help for linker features...
##
+ optionalString (isLld && stdenv.targetPlatform.isWindows) (let
mtype =
/**/ if targetPlatform.isx86_32 then "i386pe"
else if targetPlatform.isx86_64 then "i386pep"
else if targetPlatform.isAarch32 then "thumb2pe"
else if targetPlatform.isAarch64 then "arm64pe"
else throw "unsupported target arch for lld";
in ''
export mtype=${mtype}
substituteAll ${./add-lld-ldflags-before.sh} add-local-ldflags-before.sh
cat add-local-ldflags-before.sh >> $out/nix-support/add-local-ldflags-before.sh
'')

##
## Code signing on Apple Silicon
##
Expand Down
5 changes: 4 additions & 1 deletion pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
, isGNU ? false, isClang ? cc.isClang or false, gnugrep ? null
, buildPackages ? {}
, libcxx ? null
, isCompilerRT ? false
Copy link
Member

Choose a reason for hiding this comment

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

Isn't useLLVM the only way to get a compiler-rt stdenv.cc at the moment? stdenv.hostPlatform.useLLVM or false should also be a feasible replacement.

This disambiguator is probably useful regardless, but I'm wondering if we should be conservative about what we tack on here and if there's possibly a better system for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, you can use this with llvmPackages.tools.clangUseLLVM etc.

Probably there is a better system. e.g. something like compiler-rt is perhaps plausibly an attribute of the platform (if it's required to be consistent across libraries and executables being linked together) whereas something like the linker really isn't (at least on platforms where there are multiple interchangeable linkers). But I'd prefer to separate out these improvements to how these are handled from the functional addition in this PR, unless there's a better current alternative.

Copy link
Member

Choose a reason for hiding this comment

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

@sternenseemann yeah I think this is good too. useLLVM is kind of a sledge hammer when we ought to support more mix and match. I would not be opposed to specifying the default like we do for the linker in the platform description, but non-default compilers can do different things so we can track in the wrapper too.

}:

with lib;
Expand Down Expand Up @@ -145,7 +146,7 @@ stdenv.mkDerivation {
# Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
# unused middle-ground name that evokes both.
inherit bintools;
inherit libc nativeTools nativeLibc nativePrefix isGNU isClang;
inherit libc nativeTools nativeLibc nativePrefix isGNU isClang isCompilerRT;

emacsBufferSetup = pkgs: ''
; We should handle propagation here too
Expand Down Expand Up @@ -476,6 +477,8 @@ stdenv.mkDerivation {
hardening_unsupported_flags+=" pic"
'' + optionalString targetPlatform.isMinGW ''
hardening_unsupported_flags+=" stackprotector fortify"
'' + optionalString (targetPlatform.isWindows && isClang) ''
hardening_unsupported_flags+=" pic"
'' + optionalString targetPlatform.isAvr ''
hardening_unsupported_flags+=" stackprotector pic"
'' + optionalString (targetPlatform.libc == "newlib") ''
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/10/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ let majorVersion = "10";
});

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -292,7 +292,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/11/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let majorVersion = "11";
++ optional (!crossStageStatic && targetPlatform.isMinGW) ./Added-mcf-thread-model-support-from-mcfgthread.patch;

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -296,7 +296,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/4.8/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ let majorVersion = "4";
javaAwtGtk = langJava && x11Support;

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -316,7 +316,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/4.9/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ let majorVersion = "4";
javaAwtGtk = langJava && x11Support;

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -332,7 +332,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/6/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ let majorVersion = "6";
javaAwtGtk = langJava && x11Support;

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -345,7 +345,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/7/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let majorVersion = "7";
++ [ ../libsanitizer-no-cyclades-9.patch ];

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -301,7 +301,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/8/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let majorVersion = "8";
++ [ ../libsanitizer-no-cyclades-9.patch ];

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -280,7 +280,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/gcc/9/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ let majorVersion = "9";
++ [ ../libsanitizer-no-cyclades-9.patch ];

/* Cross-gcc settings (build == host != target) */
crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";

Expand Down Expand Up @@ -311,7 +311,7 @@ stdenv.mkDerivation ({
};
}

// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.isMinGW && crossStageStatic) {
makeFlags = [ "all-gcc" "all-target-libgcc" ];
installTargets = "install-gcc install-target-libgcc";
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/gcc/common/configure-flags.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let
inherit (stdenv)
buildPlatform hostPlatform targetPlatform;

crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
crossDarwin = targetPlatform != hostPlatform && targetPlatform.libc == "libSystem";

targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/llvm/10/bintools/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
if stdenv.hostPlatform != stdenv.targetPlatform
then "${stdenv.targetPlatform.config}-"
else "";
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; } ''
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; passthru.isLld = true; } ''
mkdir -p $out/bin
for prog in ${lld}/bin/*; do
ln -s $prog $out/bin/${prefix}$(basename $prog)
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/llvm/11/bintools/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
if stdenv.hostPlatform != stdenv.targetPlatform
then "${stdenv.targetPlatform.config}-"
else "";
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; } ''
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; passthru.isLld = true; } ''
mkdir -p $out/bin
for prog in ${lld}/bin/*; do
ln -s $prog $out/bin/${prefix}$(basename $prog)
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/llvm/12/bintools/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
if stdenv.hostPlatform != stdenv.targetPlatform
then "${stdenv.targetPlatform.config}-"
else "";
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; } ''
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; passthru.isLld = true; } ''
mkdir -p $out/bin
for prog in ${lld}/bin/*; do
ln -s $prog $out/bin/${prefix}$(basename $prog)
Expand Down
15 changes: 12 additions & 3 deletions pkgs/development/compilers/llvm/13/bintools/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{ runCommand, stdenv, llvm, lld, version }:
{ runCommand, stdenv, llvm, lld, version, lib }:

let
prefix =
if stdenv.hostPlatform != stdenv.targetPlatform
then "${stdenv.targetPlatform.config}-"
else "";
in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; } ''
in runCommand "llvm-binutils-${version}" {
preferLocalBuild = true;
passthru = {
isLld = true;
targetPrefix = prefix;
};
} (''
mkdir -p $out/bin
for prog in ${lld}/bin/*; do
ln -s $prog $out/bin/${prefix}$(basename $prog)
Expand All @@ -26,4 +32,7 @@ in runCommand "llvm-binutils-${version}" { preferLocalBuild = true; } ''
ln -s ${llvm}/bin/llvm-strip $out/bin/${prefix}strip

ln -s ${lld}/bin/lld $out/bin/${prefix}ld
''
'' + lib.optionalString stdenv.targetPlatform.isWindows ''
ln -s ${llvm}/bin/llvm-windres $out/bin/${prefix}windres
ln -s ${llvm}/bin/llvm-dlltool $out/bin/${prefix}dlltool
'')
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/13/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ let
'' + lib.optionalString stdenv.targetPlatform.isWasm ''
echo "-fno-exceptions" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands cc;
isCompilerRT = true;
};

clangNoLibcxx = wrapCCWith rec {
Expand All @@ -182,6 +183,7 @@ let
echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
echo "-nostdlib++" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands cc;
isCompilerRT = true;
};

clangNoLibc = wrapCCWith rec {
Expand All @@ -195,6 +197,7 @@ let
echo "-rtlib=compiler-rt" >> $out/nix-support/cc-cflags
echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands cc;
isCompilerRT = true;
};

clangNoCompilerRt = wrapCCWith rec {
Expand Down
12 changes: 9 additions & 3 deletions pkgs/development/compilers/llvm/13/libcxx/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ lib, stdenv, llvm_meta, src, cmake, python3, fixDarwinDylibNames, version
, libcxxabi
, libcxxabi, libunwind
, enableShared ? !stdenv.hostPlatform.isStatic

# If headersOnly is true, the resulting package would only include the headers.
Expand Down Expand Up @@ -32,7 +32,7 @@ stdenv.mkDerivation rec {
nativeBuildInputs = [ cmake python3 ]
++ lib.optional stdenv.isDarwin fixDarwinDylibNames;

buildInputs = lib.optionals (!headersOnly) [ libcxxabi ];
buildInputs = lib.optionals (!headersOnly) ([ libcxxabi ] ++ lib.optional libcxxabi.useLLVMUnwinder libunwind);

cmakeFlags = [ "-DLIBCXX_CXX_ABI=libcxxabi" ]
++ lib.optional (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) "-DLIBCXX_HAS_MUSL_LIBC=1"
Expand All @@ -41,7 +41,13 @@ stdenv.mkDerivation rec {
"-DLIBCXX_ENABLE_THREADS=OFF"
"-DLIBCXX_ENABLE_FILESYSTEM=OFF"
"-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
] ++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF";
]
++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF"
++ lib.optionals (!headersOnly && libcxxabi.semi-static) [
"-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=TRUE"
"-DLIBCXX_CXX_ABI_LIBRARY_PATH=${libcxxabi}/lib"
] ++ lib.optional (!headersOnly && libcxxabi.useLLVMUnwinder)
"-DLIBCXXABI_USE_LLVM_UNWINDER=ON";
Copy link
Member

Choose a reason for hiding this comment

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

Curious about this; libcxx/CMakeLists.txt claims this is only about linking the test cases which we don't run.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got link errors without this, I wouldn't be surprised if it's only needed when LIBCXX_ENABLE_STATIC_ABI_LIBRARY (which is narrowly tested).

Copy link
Member

@Ericson2314 Ericson2314 May 7, 2022

Choose a reason for hiding this comment

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

In which versions, @sternenseemann? I don't see that in LLVM 13's libcxx/CMakeLists.txt.

Copy link
Member

Choose a reason for hiding this comment

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


buildFlags = lib.optional headersOnly "generate-cxx-headers";
installTargets = lib.optional headersOnly "install-cxx-headers";
Expand Down
Loading