Skip to content

Commit

Permalink
llvmPackages_*.clang: bind linker on compiling clang
Browse files Browse the repository at this point in the history
When using clang for cross compilation, clang cannot find the linker.
Because, for consistency with gcc, given a linker name, clang finds the
linker in the following order:

1. in prefix dirs, which contains toolchain specific paths and paths
   specified by `-B`
2. in ProgramPaths
3. in PATH environment

(refer to `Driver::GetProgramPath` in `clang/lib/Driver/Driver.cpp`)
(also refer to https://gcc.gnu.org/onlinedocs/gccint/Collect2.html)

The step 2 and 3 considers all possible target triple prefixes, but
the step 1 does not. In nixpkgs, all binaries in cross toolchains are
prefixed by the target triple, thus clang cannot find them.

For gcc in nixpkgs, the linker path is hardcoded in configuration
phase. This commit copies this behavior to clang.

But the added cmakeFlags also introduce circular dependency. The
circle comes from the fact that clangNoLibc and clangNoCompilerRT
introduce dependency of libc unexpectedly via cmakeFlags of their
underlying clang. We need to override bintools for them. Refer to

#220595 (comment)

for details.
  • Loading branch information
SharzyL committed Nov 7, 2023
1 parent 62370ea commit b89ce8b
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 26 deletions.
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/10/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -37,6 +38,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/10/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -208,7 +210,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/11/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
, fixDarwinDylibNames
, enableManpages ? false
, enablePolly ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -42,6 +43,8 @@ let
] ++ lib.optionals enablePolly [
"-DWITH_POLLY=ON"
"-DLINK_POLLY_INTO_TOOLS=ON"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];


Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/11/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -223,7 +225,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/12/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -40,6 +41,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/12/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -223,7 +225,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/13/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -30,6 +31,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/13/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -236,7 +238,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/14/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -38,6 +39,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/14/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -240,7 +242,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/15/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -44,6 +45,8 @@ let
# `clang-pseudo-gen`: https://github.com/llvm/llvm-project/commit/cd2292ef824591cc34cc299910a3098545c840c7
"-DCLANG_TIDY_CONFUSABLE_CHARS_GEN=${buildLlvmTools.libclang.dev}/bin/clang-tidy-confusable-chars-gen"
"-DCLANG_PSEUDO_GEN=${buildLlvmTools.libclang.dev}/bin/clang-pseudo-gen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/15/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ in let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -288,7 +290,9 @@ in let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/16/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -44,6 +45,8 @@ let
# `clang-pseudo-gen`: https://github.com/llvm/llvm-project/commit/cd2292ef824591cc34cc299910a3098545c840c7
"-DCLANG_TIDY_CONFUSABLE_CHARS_GEN=${buildLlvmTools.libclang.dev}/bin/clang-tidy-confusable-chars-gen"
"-DCLANG_PSEUDO_GEN=${buildLlvmTools.libclang.dev}/bin/clang-pseudo-gen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/16/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ in let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -293,7 +295,9 @@ in let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/5/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -38,6 +39,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/6/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, buildLlvmTools
, fixDarwinDylibNames
, enableManpages ? false
, targetPackages
}:

let
Expand Down Expand Up @@ -38,6 +39,8 @@ let
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.llvm}/bin/llvm-tblgen"
"-DCLANG_TABLEGEN=${buildLlvmTools.libclang.dev}/bin/clang-tblgen"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/7/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
, fixDarwinDylibNames
, enableManpages ? false
, enablePolly ? false # TODO: get this info from llvm (passthru?)
, targetPackages
}:

let
Expand Down Expand Up @@ -42,6 +43,8 @@ let
] ++ lib.optionals enablePolly [
"-DWITH_POLLY=ON"
"-DLINK_POLLY_INTO_TOOLS=ON"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/7/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -210,7 +212,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/compilers/llvm/8/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
, fixDarwinDylibNames
, enableManpages ? false
, enablePolly ? false # TODO: get this info from llvm (passthru?)
, targetPackages
}:

let
Expand Down Expand Up @@ -42,6 +43,8 @@ let
] ++ lib.optionals enablePolly [
"-DWITH_POLLY=ON"
"-DLINK_POLLY_INTO_TOOLS=ON"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
"-DCLANG_DEFAULT_LINKER=${targetPackages.stdenv.cc.bintools}/bin/${stdenv.targetPlatform.config}-ld"
];

patches = [
Expand Down
8 changes: 6 additions & 2 deletions pkgs/development/compilers/llvm/8/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ let
};

clangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [
Expand All @@ -211,7 +213,9 @@ let
};

clangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
cc = tools.clang-unwrapped.override {
targetPackages.stdenv.cc.bintools = bintoolsNoLibc';
};
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
Expand Down
Loading

0 comments on commit b89ce8b

Please sign in to comment.