Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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 buildLlvmTools, 16 fixDarwinDylibNames, 17 enableManpages ? false, 18 devExtraCmakeFlags ? [ ], 19 replaceVars, 20 getVersionFile, 21 fetchpatch, 22}: 23stdenv.mkDerivation ( 24 finalAttrs: 25 { 26 pname = "clang"; 27 inherit version; 28 29 src = 30 if monorepoSrc != null then 31 runCommand "clang-src-${version}" { inherit (monorepoSrc) passthru; } ( 32 '' 33 mkdir -p "$out" 34 '' 35 + lib.optionalString (lib.versionAtLeast release_version "14") '' 36 cp -r ${monorepoSrc}/cmake "$out" 37 '' 38 + '' 39 cp -r ${monorepoSrc}/clang "$out" 40 cp -r ${monorepoSrc}/clang-tools-extra "$out" 41 '' 42 ) 43 else 44 src; 45 46 sourceRoot = "${finalAttrs.src.name}/clang"; 47 48 patches = [ 49 (getVersionFile "clang/purity.patch") 50 # Remove extraneous ".a" suffix from baremetal clang_rt.builtins when compiling for baremetal. 51 # https://reviews.llvm.org/D51899 52 (getVersionFile "clang/gnu-install-dirs.patch") 53 ] 54 ++ lib.optionals (lib.versionOlder release_version "20") [ 55 # https://github.com/llvm/llvm-project/pull/116476 56 # prevent clang ignoring warnings / errors for unsuppored 57 # options when building & linking a source file with trailing 58 # libraries. eg: `clang -munsupported hello.c -lc` 59 ./clang-unsupported-option.patch 60 ] 61 ++ 62 lib.optional (lib.versions.major release_version == "13") 63 # Revert of https://reviews.llvm.org/D100879 64 # The malloc alignment assumption is incorrect for jemalloc and causes 65 # mis-compilation in firefox. 66 # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 67 (getVersionFile "clang/revert-malloc-alignment-assumption.patch") 68 ++ lib.optional (lib.versionOlder release_version "17") ( 69 if lib.versionAtLeast release_version "14" then 70 fetchpatch { 71 name = "ignore-nostd-link.patch"; 72 url = "https://github.com/llvm/llvm-project/commit/5b77e752dcd073846b89559d6c0e1a7699e58615.patch"; 73 relative = "clang"; 74 hash = "sha256-qzSAmoGY+7POkDhcGgQRPaNQ3+7PIcIc9cZuiE/eLkc="; 75 } 76 else 77 ./ignore-nostd-link-13.diff 78 ) 79 # Pass the correct path to libllvm 80 ++ [ 81 (replaceVars 82 ( 83 if (lib.versionOlder release_version "16") then 84 ./clang-11-15-LLVMgold-path.patch 85 else 86 ./clang-at-least-16-LLVMgold-path.patch 87 ) 88 { 89 libllvmLibdir = "${libllvm.lib}/lib"; 90 } 91 ) 92 ] 93 # Backport version logic from Clang 16. This is needed by the following patch. 94 ++ lib.optional (lib.versions.major release_version == "15") (fetchpatch { 95 name = "clang-darwin-Use-consistent-version-define-stringifying-logic.patch"; 96 url = "https://github.com/llvm/llvm-project/commit/60a33ded751c86fff9ac1c4bdd2b341fbe4b0649.patch?full_index=1"; 97 includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; 98 stripLen = 1; 99 hash = "sha256-YVTSg5eZLz3po2AUczPNXCK26JA3CuTh6Iqp7hAAKIs="; 100 }) 101 # Backport `__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__` support from Clang 17. 102 # This is needed by newer SDKs (14+). 103 ++ 104 lib.optional 105 ( 106 lib.versionAtLeast (lib.versions.major release_version) "15" 107 && lib.versionOlder (lib.versions.major release_version) "17" 108 ) 109 (fetchpatch { 110 name = "clang-darwin-An-OS-version-preprocessor-define.patch"; 111 url = "https://github.com/llvm/llvm-project/commit/c8e2dd8c6f490b68e41fe663b44535a8a21dfeab.patch?full_index=1"; 112 includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; 113 stripLen = 1; 114 hash = "sha256-Vs32kql7N6qtLqc12FtZHURcbenA7+N3E/nRRX3jdig="; 115 }) 116 # Fixes a bunch of lambda-related crashes 117 # https://github.com/llvm/llvm-project/pull/93206 118 ++ lib.optional (lib.versions.major release_version == "18") (fetchpatch { 119 name = "tweak-tryCaptureVariable-for-unevaluated-lambdas.patch"; 120 url = "https://github.com/llvm/llvm-project/commit/3d361b225fe89ce1d8c93639f27d689082bd8dad.patch"; 121 # TreeTransform.h is not affected in LLVM 18. 122 excludes = [ 123 "docs/ReleaseNotes.rst" 124 "lib/Sema/TreeTransform.h" 125 ]; 126 stripLen = 1; 127 hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E="; 128 }) 129 ++ 130 lib.optional (stdenv.isAarch64 && lib.versions.major release_version == "17") 131 # Fixes llvm17 tblgen builds on aarch64. 132 # https://github.com/llvm/llvm-project/issues/106521#issuecomment-2337175680 133 (getVersionFile "clang/aarch64-tblgen.patch"); 134 135 nativeBuildInputs = [ 136 cmake 137 python3 138 ] 139 ++ (lib.optional (lib.versionAtLeast release_version "15") ninja) 140 ++ lib.optional (lib.versionAtLeast version "18" && enableManpages) python3.pkgs.myst-parser 141 ++ lib.optional enableManpages python3.pkgs.sphinx 142 ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames; 143 144 buildInputs = [ 145 libxml2 146 libllvm 147 ]; 148 149 cmakeFlags = 150 (lib.optionals (lib.versionAtLeast release_version "15") [ 151 (lib.cmakeFeature "CLANG_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/clang") 152 ]) 153 ++ [ 154 (lib.cmakeBool "CLANGD_BUILD_XPC" false) 155 (lib.cmakeBool "LLVM_ENABLE_RTTI" true) 156 (lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen") 157 (lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmTools.tblgen}/bin/clang-tblgen") 158 ] 159 ++ lib.optionals (lib.versionAtLeast release_version "21") [ 160 (lib.cmakeFeature "CLANG_RESOURCE_DIR" "${placeholder "lib"}/lib/clang/${lib.versions.major release_version}") 161 ] 162 ++ lib.optionals (lib.versionAtLeast release_version "17") [ 163 (lib.cmakeBool "LLVM_INCLUDE_TESTS" false) 164 ] 165 ++ lib.optionals enableManpages [ 166 (lib.cmakeBool "CLANG_INCLUDE_DOCS" true) 167 (lib.cmakeBool "LLVM_ENABLE_SPHINX" true) 168 (lib.cmakeBool "SPHINX_OUTPUT_MAN" true) 169 (lib.cmakeBool "SPHINX_OUTPUT_HTML" false) 170 (lib.cmakeBool "SPHINX_WARNINGS_AS_ERRORS" false) 171 ] 172 ++ lib.optionals (lib.versionAtLeast release_version "15") [ 173 # Added in LLVM15: 174 # `clang-tidy-confusable-chars-gen`: https://github.com/llvm/llvm-project/commit/c3574ef739fbfcc59d405985a3a4fa6f4619ecdb 175 # `clang-pseudo-gen`: https://github.com/llvm/llvm-project/commit/cd2292ef824591cc34cc299910a3098545c840c7 176 (lib.cmakeFeature "CLANG_TIDY_CONFUSABLE_CHARS_GEN" "${buildLlvmTools.tblgen}/bin/clang-tidy-confusable-chars-gen") 177 ] 178 ++ lib.optionals (lib.versionOlder release_version "20") [ 179 # clang-pseudo removed in LLVM20: https://github.com/llvm/llvm-project/commit/ed8f78827895050442f544edef2933a60d4a7935 180 (lib.cmakeFeature "CLANG_PSEUDO_GEN" "${buildLlvmTools.tblgen}/bin/clang-pseudo-gen") 181 ] 182 ++ lib.optional (lib.versionAtLeast release_version "20") ( 183 lib.cmakeFeature "LLVM_DIR" "${libllvm.dev}/lib/cmake/llvm" 184 ) 185 ++ devExtraCmakeFlags; 186 187 postPatch = '' 188 # Make sure clang passes the correct location of libLTO to ld64 189 substituteInPlace lib/Driver/ToolChains/Darwin.cpp \ 190 --replace-fail 'StringRef P = llvm::sys::path::parent_path(D.Dir);' 'StringRef P = "${lib.getLib libllvm}";' 191 (cd tools && ln -s ../../clang-tools-extra extra) 192 '' 193 + lib.optionalString stdenv.hostPlatform.isMusl '' 194 sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp 195 ''; 196 197 outputs = [ 198 "out" 199 "lib" 200 "dev" 201 "python" 202 ]; 203 204 separateDebugInfo = stdenv.buildPlatform.is64bit; # OOMs on 32 bit 205 206 postInstall = '' 207 ln -sv $out/bin/clang $out/bin/cpp 208 '' 209 + (lib.optionalString (lib.versions.major release_version == "17") '' 210 mkdir -p $lib/lib/clang 211 mv $lib/lib/17 $lib/lib/clang/17 212 '') 213 + (lib.optionalString 214 ((lib.versionAtLeast release_version "19") && !(lib.versionAtLeast release_version "21")) 215 '' 216 mv $out/lib/clang $lib/lib/clang 217 '' 218 ) 219 + '' 220 221 # Move libclang to 'lib' output 222 moveToOutput "lib/libclang.*" "$lib" 223 moveToOutput "lib/libclang-cpp.*" "$lib" 224 '' 225 + ( 226 if lib.versionOlder release_version "15" then 227 '' 228 mkdir -p $python/bin $python/share/{clang,scan-view} 229 '' 230 else 231 '' 232 mkdir -p $python/bin $python/share/clang/ 233 '' 234 ) 235 + '' 236 mv $out/bin/{git-clang-format,scan-view} $python/bin 237 if [ -e $out/bin/set-xcode-analyzer ]; then 238 mv $out/bin/set-xcode-analyzer $python/bin 239 fi 240 mv $out/share/clang/*.py $python/share/clang 241 '' 242 + (lib.optionalString (lib.versionOlder release_version "15") '' 243 mv $out/share/scan-view/*.py $python/share/scan-view 244 '') 245 + '' 246 rm $out/bin/c-index-test 247 patchShebangs $python/bin 248 249 mkdir -p $dev/bin 250 '' 251 + ( 252 if lib.versionOlder release_version "15" then 253 '' 254 cp bin/clang-tblgen $dev/bin 255 '' 256 else if lib.versionOlder release_version "20" then 257 '' 258 cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen,clang-pseudo-gen} $dev/bin 259 '' 260 else 261 '' 262 cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen} $dev/bin 263 '' 264 ); 265 266 env = 267 lib.optionalAttrs 268 ( 269 stdenv.buildPlatform != stdenv.hostPlatform 270 && !stdenv.hostPlatform.useLLVM 271 && lib.versionAtLeast release_version "15" 272 ) 273 { 274 # The following warning is triggered with (at least) gcc >= 275 # 12, but appears to occur only for cross compiles. 276 NIX_CFLAGS_COMPILE = "-Wno-maybe-uninitialized"; 277 }; 278 279 passthru = { 280 inherit libllvm; 281 isClang = true; 282 hardeningUnsupportedFlagsByTargetPlatform = 283 targetPlatform: 284 [ "fortify3" ] 285 ++ lib.optional ( 286 (lib.versionOlder release_version "7") || !targetPlatform.isLinux || !targetPlatform.isx86_64 287 ) "shadowstack" 288 ++ lib.optional ( 289 (lib.versionOlder release_version "8") || !targetPlatform.isAarch64 || !targetPlatform.isLinux 290 ) "pacret" 291 ++ lib.optional ( 292 (lib.versionOlder release_version "11") 293 || (targetPlatform.isAarch64 && (lib.versionOlder release_version "18.1")) 294 || (targetPlatform.isFreeBSD && (lib.versionOlder release_version "15")) 295 || !(targetPlatform.isLinux || targetPlatform.isFreeBSD) 296 || !( 297 targetPlatform.isx86 298 || targetPlatform.isPower64 299 || targetPlatform.isS390x 300 || targetPlatform.isAarch64 301 ) 302 ) "stackclashprotection" 303 ++ lib.optional ( 304 (lib.versionOlder release_version "15") || !(targetPlatform.isx86_64 || targetPlatform.isAarch64) 305 ) "zerocallusedregs" 306 ++ lib.optional (lib.versionOlder release_version "15") "strictflexarrays1" 307 ++ lib.optional (lib.versionOlder release_version "16") "strictflexarrays3" 308 ++ (finalAttrs.passthru.hardeningUnsupportedFlags or [ ]); 309 }; 310 311 requiredSystemFeatures = [ "big-parallel" ]; 312 meta = llvm_meta // { 313 homepage = "https://clang.llvm.org/"; 314 description = "C language family frontend for LLVM"; 315 longDescription = '' 316 The Clang project provides a language front-end and tooling 317 infrastructure for languages in the C language family (C, C++, Objective 318 C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project. 319 It aims to deliver amazingly fast compiles, extremely useful error and 320 warning messages and to provide a platform for building great source 321 level tools. The Clang Static Analyzer and clang-tidy are tools that 322 automatically find bugs in your code, and are great examples of the sort 323 of tools that can be built using the Clang frontend as a library to 324 parse C/C++ code. 325 ''; 326 mainProgram = "clang"; 327 }; 328 } 329 // lib.optionalAttrs enableManpages ( 330 { 331 pname = "clang-manpages"; 332 333 installPhase = '' 334 mkdir -p $out/share/man/man1 335 # Manually install clang manpage 336 cp docs/man/*.1 $out/share/man/man1/ 337 ''; 338 339 outputs = [ "out" ]; 340 341 doCheck = false; 342 343 meta = llvm_meta // { 344 description = "man page for Clang ${version}"; 345 }; 346 } 347 // ( 348 if lib.versionOlder release_version "15" then 349 { 350 buildPhase = '' 351 make docs-clang-man 352 ''; 353 } 354 else 355 { 356 ninjaFlags = [ "docs-clang-man" ]; 357 } 358 ) 359 ) 360)