2024-03-28 11:11:16 -07:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
|
|
|
, llvm_meta
|
|
|
|
, patches ? []
|
|
|
|
, src ? null
|
|
|
|
, monorepoSrc ? null
|
|
|
|
, runCommand
|
|
|
|
, cmake
|
|
|
|
, ninja
|
|
|
|
, libxml2
|
|
|
|
, libllvm
|
|
|
|
, release_version
|
|
|
|
, version
|
|
|
|
, python3
|
2022-10-04 11:25:28 -05:00
|
|
|
, buildLlvmTools
|
|
|
|
, fixDarwinDylibNames
|
|
|
|
, enableManpages ? false
|
llvmPackages.*: Add devExtraCmakeFlags parameter
cmake flags have a 'last flag wins' logic, so by appending to the end of
the flags it is possible to override any cmake flag.
It also ignores (and warns) if a flag is unused, so passing flags across
all packages should be safe if you want to target one package.
In combination with #320261, this PR allows consistently overriding all
packages within LLVM with additional cmake arguments. Consistency here
means for example 'if you override LLVM, then all dependencies on it are
also see the overridden LLVM in their input'. Consistency is hard to
achieve with the other obvious way of implementing this as a user: if
you use overrideAttrs then you have to write a big mess of override code
in order to override all dependents, and this can be very difficult in a
cross-compilation scenario using crossSystem and useLLVM, for example.
With this PR it is possible to write an overlay which overlays
`llvmPackages` with `llvmPackage.override { devExtraCmakeFlags = [ ... ]; }`,
and then the toolchain used with useLLVM in effect should respect
these flags.
This is useful in development for experimenting with the effect of
various flags, hence the chosen name `devCmakeFlags`.
This won't work out of the box without #341855 applied, which fixes
override passthrough.
See-Also: #320261, #341855
Signed-off-by: Peter Waller <p@pwaller.net>
2024-09-15 11:37:03 +01:00
|
|
|
, devExtraCmakeFlags ? []
|
2022-10-04 11:25:28 -05:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-03-28 11:11:16 -07:00
|
|
|
pname = "clang";
|
2022-10-04 11:25:28 -05:00
|
|
|
|
2024-03-28 11:11:16 -07:00
|
|
|
src' = if monorepoSrc != null then
|
2024-11-08 12:48:25 +00:00
|
|
|
runCommand "${pname}-src-${version}" { inherit (monorepoSrc) passthru; } (''
|
2022-10-04 11:25:28 -05:00
|
|
|
mkdir -p "$out"
|
2024-10-11 11:41:49 +02:00
|
|
|
'' + lib.optionalString (lib.versionAtLeast release_version "14") ''
|
2022-10-04 11:25:28 -05:00
|
|
|
cp -r ${monorepoSrc}/cmake "$out"
|
2024-10-11 11:41:49 +02:00
|
|
|
'' + ''
|
2022-10-04 11:25:28 -05:00
|
|
|
cp -r ${monorepoSrc}/${pname} "$out"
|
|
|
|
cp -r ${monorepoSrc}/clang-tools-extra "$out"
|
2024-10-11 11:41:49 +02:00
|
|
|
'') else src;
|
2024-03-28 11:11:16 -07:00
|
|
|
|
|
|
|
self = stdenv.mkDerivation (finalAttrs: rec {
|
|
|
|
inherit pname version patches;
|
2022-10-04 11:25:28 -05:00
|
|
|
|
2024-03-28 11:11:16 -07:00
|
|
|
src = src';
|
2022-10-04 11:25:28 -05:00
|
|
|
|
2024-10-06 00:32:56 +02:00
|
|
|
sourceRoot = "${src.name}/${pname}";
|
2024-03-28 11:11:16 -07:00
|
|
|
|
|
|
|
nativeBuildInputs = [ cmake ]
|
|
|
|
++ (lib.optional (lib.versionAtLeast release_version "15") ninja)
|
|
|
|
++ [ python3 ]
|
|
|
|
++ lib.optional (lib.versionAtLeast version "18" && enableManpages) python3.pkgs.myst-parser
|
2022-10-04 11:25:28 -05:00
|
|
|
++ lib.optional enableManpages python3.pkgs.sphinx
|
|
|
|
++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
|
|
|
|
|
|
|
|
buildInputs = [ libxml2 libllvm ];
|
|
|
|
|
2024-03-28 11:11:16 -07:00
|
|
|
cmakeFlags = (lib.optionals (lib.versionAtLeast release_version "15") [
|
2022-10-04 11:25:28 -05:00
|
|
|
"-DCLANG_INSTALL_PACKAGE_DIR=${placeholder "dev"}/lib/cmake/clang"
|
2024-03-28 11:11:16 -07:00
|
|
|
]) ++ [
|
2022-10-04 11:25:28 -05:00
|
|
|
"-DCLANGD_BUILD_XPC=OFF"
|
|
|
|
"-DLLVM_ENABLE_RTTI=ON"
|
2024-03-28 11:11:16 -07:00
|
|
|
] ++ lib.optionals (lib.versionAtLeast release_version "17") [
|
|
|
|
"-DLLVM_INCLUDE_TESTS=OFF"
|
2022-10-04 11:25:28 -05:00
|
|
|
] ++ lib.optionals enableManpages [
|
|
|
|
"-DCLANG_INCLUDE_DOCS=ON"
|
|
|
|
"-DLLVM_ENABLE_SPHINX=ON"
|
|
|
|
"-DSPHINX_OUTPUT_MAN=ON"
|
|
|
|
"-DSPHINX_OUTPUT_HTML=OFF"
|
|
|
|
"-DSPHINX_WARNINGS_AS_ERRORS=OFF"
|
2024-12-06 11:26:22 +00:00
|
|
|
] ++ [
|
|
|
|
"-DLLVM_TABLEGEN_EXE=${buildLlvmTools.tblgen}/bin/llvm-tblgen"
|
|
|
|
"-DCLANG_TABLEGEN=${buildLlvmTools.tblgen}/bin/clang-tblgen"
|
2024-03-28 11:11:16 -07:00
|
|
|
] ++ lib.optionals (lib.versionAtLeast release_version "15") [
|
2023-04-16 16:59:18 -07:00
|
|
|
# Added in LLVM15:
|
|
|
|
# `clang-tidy-confusable-chars-gen`: https://github.com/llvm/llvm-project/commit/c3574ef739fbfcc59d405985a3a4fa6f4619ecdb
|
|
|
|
# `clang-pseudo-gen`: https://github.com/llvm/llvm-project/commit/cd2292ef824591cc34cc299910a3098545c840c7
|
2024-12-06 11:26:22 +00:00
|
|
|
"-DCLANG_TIDY_CONFUSABLE_CHARS_GEN=${buildLlvmTools.tblgen}/bin/clang-tidy-confusable-chars-gen"
|
|
|
|
] ++ lib.optionals (lib.versionOlder release_version "20") [
|
|
|
|
# clang-pseudo removed in LLVM20: https://github.com/llvm/llvm-project/commit/ed8f78827895050442f544edef2933a60d4a7935
|
|
|
|
"-DCLANG_PSEUDO_GEN=${buildLlvmTools.tblgen}/bin/clang-pseudo-gen"
|
|
|
|
] ++ lib.optional (lib.versionAtLeast release_version "20") "-DLLVM_DIR=${libllvm.dev}/lib/cmake/llvm"
|
2024-09-22 21:07:34 -07:00
|
|
|
++ devExtraCmakeFlags;
|
2024-03-28 11:11:16 -07:00
|
|
|
|
2024-04-07 22:36:45 -04:00
|
|
|
postPatch = ''
|
|
|
|
# Make sure clang passes the correct location of libLTO to ld64
|
|
|
|
substituteInPlace lib/Driver/ToolChains/Darwin.cpp \
|
|
|
|
--replace-fail 'StringRef P = llvm::sys::path::parent_path(D.Dir);' 'StringRef P = "${lib.getLib libllvm}";'
|
2024-10-06 00:32:56 +02:00
|
|
|
(cd tools && ln -s ../../clang-tools-extra extra)
|
|
|
|
''
|
|
|
|
+ lib.optionalString stdenv.hostPlatform.isMusl ''
|
2022-10-04 11:25:28 -05:00
|
|
|
sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp
|
|
|
|
'';
|
|
|
|
|
|
|
|
outputs = [ "out" "lib" "dev" "python" ];
|
|
|
|
|
|
|
|
postInstall = ''
|
|
|
|
ln -sv $out/bin/clang $out/bin/cpp
|
2024-03-28 11:11:16 -07:00
|
|
|
'' + (lib.optionalString (lib.versions.major release_version == "17") ''
|
|
|
|
|
|
|
|
mkdir -p $lib/lib/clang
|
|
|
|
mv $lib/lib/17 $lib/lib/clang/17
|
2024-04-14 18:10:34 -07:00
|
|
|
'') + (lib.optionalString (lib.versionAtLeast release_version "19") ''
|
|
|
|
mv $out/lib/clang $lib/lib/clang
|
2024-03-28 11:11:16 -07:00
|
|
|
'') + ''
|
2022-10-04 11:25:28 -05:00
|
|
|
|
|
|
|
# Move libclang to 'lib' output
|
|
|
|
moveToOutput "lib/libclang.*" "$lib"
|
|
|
|
moveToOutput "lib/libclang-cpp.*" "$lib"
|
2024-03-28 11:11:16 -07:00
|
|
|
'' + (if lib.versionOlder release_version "15" then ''
|
|
|
|
mkdir -p $python/bin $python/share/{clang,scan-view}
|
|
|
|
'' else ''
|
2022-10-04 11:25:28 -05:00
|
|
|
mkdir -p $python/bin $python/share/clang/
|
2024-03-28 11:11:16 -07:00
|
|
|
'') + ''
|
2022-10-04 11:25:28 -05:00
|
|
|
mv $out/bin/{git-clang-format,scan-view} $python/bin
|
|
|
|
if [ -e $out/bin/set-xcode-analyzer ]; then
|
|
|
|
mv $out/bin/set-xcode-analyzer $python/bin
|
|
|
|
fi
|
|
|
|
mv $out/share/clang/*.py $python/share/clang
|
2024-03-28 11:11:16 -07:00
|
|
|
'' + (lib.optionalString (lib.versionOlder release_version "15") ''
|
|
|
|
mv $out/share/scan-view/*.py $python/share/scan-view
|
|
|
|
'') + ''
|
2022-10-04 11:25:28 -05:00
|
|
|
rm $out/bin/c-index-test
|
2023-01-08 06:12:31 -08:00
|
|
|
patchShebangs $python/bin
|
2022-10-04 11:25:28 -05:00
|
|
|
|
|
|
|
mkdir -p $dev/bin
|
2024-03-28 11:11:16 -07:00
|
|
|
'' + (if lib.versionOlder release_version "15" then ''
|
|
|
|
cp bin/clang-tblgen $dev/bin
|
2024-09-22 21:07:34 -07:00
|
|
|
'' else if lib.versionOlder release_version "20" then ''
|
2023-04-16 16:59:18 -07:00
|
|
|
cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen,clang-pseudo-gen} $dev/bin
|
2024-09-22 21:07:34 -07:00
|
|
|
'' else ''
|
|
|
|
cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen} $dev/bin
|
2024-03-28 11:11:16 -07:00
|
|
|
'');
|
2022-10-04 11:25:28 -05:00
|
|
|
|
|
|
|
passthru = {
|
|
|
|
inherit libllvm;
|
2023-02-27 11:37:40 +00:00
|
|
|
isClang = true;
|
2023-12-17 14:04:44 +00:00
|
|
|
hardeningUnsupportedFlagsByTargetPlatform = targetPlatform:
|
2024-02-14 09:30:45 +01:00
|
|
|
[ "fortify3" ]
|
|
|
|
++ lib.optional (
|
2024-07-11 23:11:13 +01:00
|
|
|
(lib.versionOlder release_version "7")
|
|
|
|
|| !targetPlatform.isLinux
|
|
|
|
|| !targetPlatform.isx86_64
|
|
|
|
) "shadowstack"
|
|
|
|
++ lib.optional (
|
2024-06-08 15:58:54 +01:00
|
|
|
(lib.versionOlder release_version "8")
|
|
|
|
|| !targetPlatform.isAarch64
|
|
|
|
|| !targetPlatform.isLinux
|
|
|
|
) "pacret"
|
|
|
|
++ lib.optional (
|
2024-02-14 09:30:45 +01:00
|
|
|
(lib.versionOlder release_version "11")
|
|
|
|
|| (targetPlatform.isAarch64 && (lib.versionOlder release_version "18.1"))
|
|
|
|
|| (targetPlatform.isFreeBSD && (lib.versionOlder release_version "15"))
|
|
|
|
|| !(targetPlatform.isLinux || targetPlatform.isFreeBSD)
|
|
|
|
|| !(
|
|
|
|
targetPlatform.isx86
|
|
|
|
|| targetPlatform.isPower64
|
|
|
|
|| targetPlatform.isS390x
|
|
|
|
|| targetPlatform.isAarch64
|
|
|
|
)
|
|
|
|
) "stackclashprotection"
|
|
|
|
++ lib.optional (
|
|
|
|
(lib.versionOlder release_version "15")
|
|
|
|
|| !(targetPlatform.isx86_64 || targetPlatform.isAarch64)
|
|
|
|
) "zerocallusedregs"
|
2023-12-17 14:04:44 +00:00
|
|
|
++ (finalAttrs.passthru.hardeningUnsupportedFlags or []);
|
2024-02-14 09:30:45 +01:00
|
|
|
};
|
2022-10-04 11:25:28 -05:00
|
|
|
|
|
|
|
meta = llvm_meta // {
|
|
|
|
homepage = "https://clang.llvm.org/";
|
|
|
|
description = "C language family frontend for LLVM";
|
|
|
|
longDescription = ''
|
|
|
|
The Clang project provides a language front-end and tooling
|
|
|
|
infrastructure for languages in the C language family (C, C++, Objective
|
|
|
|
C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.
|
|
|
|
It aims to deliver amazingly fast compiles, extremely useful error and
|
|
|
|
warning messages and to provide a platform for building great source
|
|
|
|
level tools. The Clang Static Analyzer and clang-tidy are tools that
|
|
|
|
automatically find bugs in your code, and are great examples of the sort
|
|
|
|
of tools that can be built using the Clang frontend as a library to
|
|
|
|
parse C/C++ code.
|
|
|
|
'';
|
2023-01-08 06:12:31 -08:00
|
|
|
mainProgram = "clang";
|
2022-10-04 11:25:28 -05:00
|
|
|
};
|
2024-03-28 11:11:16 -07:00
|
|
|
} // lib.optionalAttrs enableManpages ({
|
2022-10-04 11:25:28 -05:00
|
|
|
pname = "clang-manpages";
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/share/man/man1
|
|
|
|
# Manually install clang manpage
|
|
|
|
cp docs/man/*.1 $out/share/man/man1/
|
|
|
|
'';
|
|
|
|
|
|
|
|
outputs = [ "out" ];
|
|
|
|
|
|
|
|
doCheck = false;
|
|
|
|
|
|
|
|
meta = llvm_meta // {
|
|
|
|
description = "man page for Clang ${version}";
|
|
|
|
};
|
2024-03-28 11:11:16 -07:00
|
|
|
} // (if lib.versionOlder release_version "15" then {
|
|
|
|
buildPhase = ''
|
|
|
|
make docs-clang-man
|
|
|
|
'';
|
|
|
|
} else {
|
|
|
|
ninjaFlags = [ "docs-clang-man" ];
|
2024-10-06 00:32:56 +02:00
|
|
|
}))
|
2024-03-28 11:11:16 -07:00
|
|
|
// (lib.optionalAttrs (lib.versionAtLeast release_version "15") {
|
2024-06-16 15:58:46 +01:00
|
|
|
env = lib.optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform && !stdenv.hostPlatform.useLLVM) {
|
2024-03-28 11:11:16 -07:00
|
|
|
# The following warning is triggered with (at least) gcc >=
|
|
|
|
# 12, but appears to occur only for cross compiles.
|
|
|
|
NIX_CFLAGS_COMPILE = "-Wno-maybe-uninitialized";
|
|
|
|
};
|
|
|
|
}));
|
2022-10-04 11:25:28 -05:00
|
|
|
in self
|