Skip to content

Commit 98daf39

Browse files
authored
Merge pull request #240 from tweag/cross-cc-toolchain
Add support for cross cc compilers
2 parents db8ad39 + 20ad84f commit 98daf39

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

toolchains/cc/cc.nix

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
let
2-
pkgs = import <nixpkgs> { config = { }; overlays = [ ]; };
3-
in
4-
51
{ ccType
62
, ccAttrPath ? null
73
, ccAttrSet ? null
84
, ccExpr ? null
5+
, ccPkgs ? import <nixpkgs> { config = { }; overlays = [ ]; }
96
, ccLang ? "c++"
107
}:
118

129
let
10+
pkgs = ccPkgs.buildPackages;
11+
stdenv = ccPkgs.stdenv;
1312
# The original `postLinkSignHook` from nixpkgs assumes `codesign_allocate` is
1413
# in the PATH which is not the case when using our cc_wrapper. Set
1514
# `CODESIGN_ALLOCATE` to an absolute path here and override the hook for
@@ -28,8 +27,8 @@ let
2827
# Work around https://github.com/NixOS/nixpkgs/issues/42059.
2928
# See also https://github.com/NixOS/nixpkgs/pull/41589.
3029
pkgs.wrapCCWith rec {
31-
cc = pkgs.stdenv.cc.cc;
32-
bintools = pkgs.stdenv.cc.bintools.override { inherit postLinkSignHook; };
30+
cc = stdenv.cc.cc;
31+
bintools = stdenv.cc.bintools.override { inherit postLinkSignHook; };
3332
extraBuildCommands = with pkgs.darwin.apple_sdk.frameworks; ''
3433
echo "-Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags
3534
echo "-Wno-elaborated-enum-base" >> $out/nix-support/cc-cflags
@@ -54,22 +53,23 @@ let
5453
else
5554
pkgs.buildEnv (
5655
let
57-
cc = if pkgs.stdenv.isDarwin then darwinCC else pkgs.stdenv.cc;
56+
cc = if stdenv.isDarwin then darwinCC else stdenv.cc;
5857
in
5958
{
60-
name = "bazel-nixpkgs-cc";
59+
name = "bazel-${cc.name}-wrapper";
6160
# XXX: `gcov` is missing in `/bin`.
6261
# It exists in `stdenv.cc.cc` but that collides with `stdenv.cc`.
6362
paths = [ cc cc.bintools ] ++ pkgs.lib.optional pkgs.stdenv.isDarwin pkgs.darwin.cctools;
6463
pathsToLink = [ "/bin" ];
6564
passthru = {
66-
isClang = cc.isClang;
65+
inherit (cc) isClang targetPrefix;
66+
orignalName = cc.name;
6767
};
6868
}
6969
)
7070
;
7171
in
72-
pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
72+
pkgs.runCommand "bazel-${cc.orignalName or cc.name}-toolchain"
7373
{
7474
executable = false;
7575
# Pointless to do this on a remote machine.
@@ -89,10 +89,14 @@ pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
8989
TOOL_NAMES=(''${!TOOLS[@]})
9090
declare -A TOOL_PATHS=()
9191
for tool_name in ''${!TOOLS[@]}; do
92-
tool_path=${cc}/bin/''${TOOLS[$tool_name]}
92+
tool_path=${cc}/bin/${cc.targetPrefix}''${TOOLS[$tool_name]}
9393
if [[ -x $tool_path ]]; then
9494
TOOL_PATHS[$tool_name]=$tool_path
9595
else
96+
if [[ $tool_name == gcc ]]; then
97+
echo "Failed to find ${cc.targetPrefix}''${TOOLS[gcc]} in ${cc}/bin" >&2
98+
exit 1
99+
fi
96100
TOOL_PATHS[$tool_name]=${pkgs.coreutils}/bin/false
97101
fi
98102
done
@@ -188,7 +192,7 @@ pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
188192
add_linker_option_if_supported -Wl,-z,relro,-z,now -z
189193
)
190194
${
191-
if pkgs.stdenv.isDarwin
195+
if stdenv.isDarwin
192196
then "-undefined dynamic_lookup -headerpad_max_install_names"
193197
else "-B${cc}/bin"
194198
}
@@ -199,11 +203,11 @@ pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
199203
)
200204
LINK_LIBS=(
201205
${
202-
# Use pkgs.stdenv.isDarwin as a marker instead of cc.isClang because
206+
# Use stdenv.isDarwin as a marker instead of cc.isClang because
203207
# we might have usecases with stdenv with clang and libstdc++.
204208
# On Darwin libstdc++ is not available, so it's safe to assume that
205209
# everybody use libc++ from LLVM.
206-
if pkgs.stdenv.isDarwin then "-lc++" else "-lstdc++"
210+
if stdenv.isDarwin then "-lc++" else "-lstdc++"
207211
}
208212
-lm
209213
)
@@ -233,7 +237,7 @@ pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
233237
)
234238
OPT_LINK_FLAGS=(
235239
${
236-
if pkgs.stdenv.isDarwin
240+
if stdenv.isDarwin
237241
then ""
238242
else "$(add_linker_option_if_supported -Wl,--gc-sections -gc-sections)"
239243
}
@@ -256,15 +260,15 @@ pkgs.runCommand "bazel-nixpkgs-cc-toolchain"
256260
DBG_COMPILE_FLAGS=(-g)
257261
COVERAGE_COMPILE_FLAGS=(
258262
${
259-
if pkgs.stdenv.isDarwin then
263+
if stdenv.isDarwin then
260264
"-fprofile-instr-generate -fcoverage-mapping"
261265
else
262266
"--coverage"
263267
}
264268
)
265269
COVERAGE_LINK_FLAGS=(
266270
${
267-
if pkgs.stdenv.isDarwin then
271+
if stdenv.isDarwin then
268272
"-fprofile-instr-generate"
269273
else
270274
"--coverage"

0 commit comments

Comments
 (0)