nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 llvm_meta,
5 src ? null,
6 monorepoSrc ? null,
7 runCommand,
8 cmake,
9 ninja,
10 libxml2,
11 libllvm,
12 release_version,
13 version,
14 python3,
15 buildLlvmPackages,
16 fixDarwinDylibNames,
17 enableManpages ? false,
18 enableClangToolsExtra ? true,
19 devExtraCmakeFlags ? [ ],
20 replaceVars,
21 getVersionFile,
22 fetchpatch,
23 # for tests
24 libclang,
25}:
26stdenv.mkDerivation (
27 finalAttrs:
28 {
29 pname = "clang";
30 inherit version;
31
32 src =
33 if monorepoSrc != null then
34 runCommand "clang-src-${version}" { inherit (monorepoSrc) passthru; } ''
35 mkdir -p "$out"
36 cp -r ${monorepoSrc}/cmake "$out"
37 cp -r ${monorepoSrc}/clang "$out"
38 ${lib.optionalString enableClangToolsExtra "cp -r ${monorepoSrc}/clang-tools-extra \"$out\""}
39 ''
40 else
41 src;
42
43 sourceRoot = "${finalAttrs.src.name}/clang";
44
45 patches = [
46 (getVersionFile "clang/purity.patch")
47 # Remove extraneous ".a" suffix from baremetal clang_rt.builtins when compiling for baremetal.
48 # https://reviews.llvm.org/D51899
49 (getVersionFile "clang/gnu-install-dirs.patch")
50 ]
51 ++ lib.optionals (lib.versionOlder release_version "20") [
52 # https://github.com/llvm/llvm-project/pull/116476
53 # prevent clang ignoring warnings / errors for unsuppored
54 # options when building & linking a source file with trailing
55 # libraries. eg: `clang -munsupported hello.c -lc`
56 ./clang-unsupported-option.patch
57 ]
58 # Pass the correct path to libllvm
59 ++ [
60 (replaceVars ./clang-at-least-16-LLVMgold-path.patch {
61 libllvmLibdir = "${libllvm.lib}/lib";
62 })
63 ]
64 # Fixes a bunch of lambda-related crashes
65 # https://github.com/llvm/llvm-project/pull/93206
66 ++ lib.optional (lib.versions.major release_version == "18") (fetchpatch {
67 name = "tweak-tryCaptureVariable-for-unevaluated-lambdas.patch";
68 url = "https://github.com/llvm/llvm-project/commit/3d361b225fe89ce1d8c93639f27d689082bd8dad.patch";
69 # TreeTransform.h is not affected in LLVM 18.
70 excludes = [
71 "docs/ReleaseNotes.rst"
72 "lib/Sema/TreeTransform.h"
73 ];
74 stripLen = 1;
75 hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E=";
76 });
77
78 nativeBuildInputs = [
79 cmake
80 python3
81 ninja
82 ]
83 ++ lib.optionals enableManpages [
84 python3.pkgs.myst-parser
85 python3.pkgs.sphinx
86 ]
87 ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
88
89 buildInputs = [
90 libxml2
91 libllvm
92 ];
93
94 cmakeFlags = [
95 (lib.cmakeFeature "CLANG_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/clang")
96 (lib.cmakeBool "CLANGD_BUILD_XPC" false)
97 (lib.cmakeBool "LLVM_ENABLE_RTTI" true)
98 (lib.cmakeBool "LLVM_INCLUDE_TESTS" false)
99 (lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmPackages.tblgen}/bin/llvm-tblgen")
100 (lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmPackages.tblgen}/bin/clang-tblgen")
101 (lib.cmakeFeature "CLANG_TIDY_CONFUSABLE_CHARS_GEN" "${buildLlvmPackages.tblgen}/bin/clang-tidy-confusable-chars-gen")
102 ]
103 ++ lib.optional (lib.versionAtLeast release_version "20") (
104 lib.cmakeFeature "LLVM_DIR" "${libllvm.dev}/lib/cmake/llvm"
105 )
106 ++ lib.optionals (lib.versionAtLeast release_version "21") [
107 (lib.cmakeFeature "CLANG_RESOURCE_DIR" "${placeholder "lib"}/lib/clang/${lib.versions.major release_version}")
108 ]
109 ++ lib.optionals enableManpages [
110 (lib.cmakeBool "CLANG_INCLUDE_DOCS" true)
111 (lib.cmakeBool "LLVM_ENABLE_SPHINX" true)
112 (lib.cmakeBool "SPHINX_OUTPUT_MAN" true)
113 (lib.cmakeBool "SPHINX_OUTPUT_HTML" false)
114 (lib.cmakeBool "SPHINX_WARNINGS_AS_ERRORS" false)
115 ]
116 ++ lib.optionals (lib.versionOlder release_version "20") [
117 # clang-pseudo removed in LLVM20: https://github.com/llvm/llvm-project/commit/ed8f78827895050442f544edef2933a60d4a7935
118 (lib.cmakeFeature "CLANG_PSEUDO_GEN" "${buildLlvmPackages.tblgen}/bin/clang-pseudo-gen")
119 ]
120 ++ devExtraCmakeFlags;
121
122 postPatch = ''
123 # Make sure clang passes the correct location of libLTO to ld64
124 substituteInPlace lib/Driver/ToolChains/Darwin.cpp \
125 --replace-fail 'StringRef P = llvm::sys::path::parent_path(D.Dir);' 'StringRef P = "${lib.getLib libllvm}";'
126 (cd tools && ln -s ../../clang-tools-extra extra)
127 ''
128 + lib.optionalString stdenv.hostPlatform.isMusl ''
129 sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp
130 '';
131
132 outputs = [
133 "out"
134 "lib"
135 "dev"
136 "python"
137 ];
138
139 separateDebugInfo = stdenv.buildPlatform.is64bit; # OOMs on 32 bit
140
141 postInstall = ''
142 ln -sv $out/bin/clang $out/bin/cpp
143 ''
144 + (lib.optionalString
145 ((lib.versionAtLeast release_version "19") && !(lib.versionAtLeast release_version "21"))
146 ''
147 mv $out/lib/clang $lib/lib/clang
148 ''
149 )
150 + ''
151
152 # Move libclang to 'lib' output
153 moveToOutput "lib/libclang.*" "$lib"
154 moveToOutput "lib/libclang-cpp.*" "$lib"
155 mkdir -p $python/bin $python/share/clang/
156 ''
157 + ''
158 mv $out/bin/{git-clang-format,scan-view} $python/bin
159 if [ -e $out/bin/set-xcode-analyzer ]; then
160 mv $out/bin/set-xcode-analyzer $python/bin
161 fi
162 mv $out/share/clang/*.py $python/share/clang
163 ''
164 + lib.optionalString (lib.versionOlder release_version "22") ''
165 rm $out/bin/c-index-test
166 ''
167 + ''
168 patchShebangs $python/bin
169
170 mkdir -p $dev/bin
171 cp bin/clang-tblgen $dev/bin
172 ''
173 + lib.optionalString enableClangToolsExtra ''
174 cp bin/clang-tidy-confusable-chars-gen $dev/bin
175 ''
176 + lib.optionalString (enableClangToolsExtra && lib.versionOlder release_version "20") ''
177 cp bin/clang-pseudo-gen $dev/bin
178 '';
179
180 env =
181 lib.optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform && !stdenv.hostPlatform.useLLVM)
182 {
183 # The following warning is triggered with (at least) gcc >=
184 # 12, but appears to occur only for cross compiles.
185 NIX_CFLAGS_COMPILE = "-Wno-maybe-uninitialized";
186 };
187
188 passthru = {
189 inherit libllvm;
190 isClang = true;
191 hardeningUnsupportedFlagsByTargetPlatform =
192 targetPlatform:
193 [ "fortify3" ]
194 ++ lib.optional (!targetPlatform.isLinux || !targetPlatform.isx86_64) "shadowstack"
195 ++ lib.optional (!targetPlatform.isAarch64 || !targetPlatform.isLinux) "pacret"
196 ++ lib.optional (
197 !(targetPlatform.isLinux || targetPlatform.isFreeBSD)
198 || !(
199 targetPlatform.isx86
200 || targetPlatform.isPower64
201 || targetPlatform.isS390x
202 || targetPlatform.isAarch64
203 )
204 ) "stackclashprotection"
205 ++ lib.optional (!(targetPlatform.isx86_64 || targetPlatform.isAarch64)) "zerocallusedregs"
206 ++ (finalAttrs.passthru.hardeningUnsupportedFlags or [ ]);
207 tests.withoutOptionalFeatures = libclang.override {
208 enableClangToolsExtra = false;
209 };
210 };
211
212 requiredSystemFeatures = [ "big-parallel" ];
213 meta = llvm_meta // {
214 homepage = "https://clang.llvm.org/";
215 description = "C language family frontend for LLVM";
216 longDescription = ''
217 The Clang project provides a language front-end and tooling
218 infrastructure for languages in the C language family (C, C++, Objective
219 C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.
220 It aims to deliver amazingly fast compiles, extremely useful error and
221 warning messages and to provide a platform for building great source
222 level tools. The Clang Static Analyzer and clang-tidy are tools that
223 automatically find bugs in your code, and are great examples of the sort
224 of tools that can be built using the Clang frontend as a library to
225 parse C/C++ code.
226 '';
227 mainProgram = "clang";
228 };
229 }
230 // lib.optionalAttrs enableManpages {
231 pname = "clang-manpages";
232
233 ninjaFlags = [ "docs-clang-man" ];
234
235 installPhase = ''
236 mkdir -p $out/share/man/man1
237 # Manually install clang manpage
238 cp docs/man/*.1 $out/share/man/man1/
239 '';
240
241 outputs = [ "out" ];
242
243 doCheck = false;
244
245 meta = llvm_meta // {
246 description = "man page for Clang ${version}";
247 };
248 }
249)