diff --git a/pkgs/development/compilers/llvm/common/clang/default.nix b/pkgs/development/compilers/llvm/common/clang/default.nix index a9ca2afbaeed..c5565ca405b8 100644 --- a/pkgs/development/compilers/llvm/common/clang/default.nix +++ b/pkgs/development/compilers/llvm/common/clang/default.nix @@ -1,37 +1,47 @@ -{ lib -, stdenv -, llvm_meta -, src ? null -, monorepoSrc ? null -, runCommand -, cmake -, ninja -, libxml2 -, libllvm -, release_version -, version -, python3 -, buildLlvmTools -, fixDarwinDylibNames -, enableManpages ? false -, devExtraCmakeFlags ? [] -, replaceVars -, getVersionFile -, fetchpatch +{ + lib, + stdenv, + llvm_meta, + src ? null, + monorepoSrc ? null, + runCommand, + cmake, + ninja, + libxml2, + libllvm, + release_version, + version, + python3, + buildLlvmTools, + fixDarwinDylibNames, + enableManpages ? false, + devExtraCmakeFlags ? [ ], + replaceVars, + getVersionFile, + fetchpatch, }: -stdenv.mkDerivation (finalAttrs: { +stdenv.mkDerivation ( + finalAttrs: + { pname = "clang"; inherit version; - src = if monorepoSrc != null then - runCommand "clang-src-${version}" { inherit (monorepoSrc) passthru; } ('' - mkdir -p "$out" - '' + lib.optionalString (lib.versionAtLeast release_version "14") '' - cp -r ${monorepoSrc}/cmake "$out" - '' + '' - cp -r ${monorepoSrc}/clang "$out" - cp -r ${monorepoSrc}/clang-tools-extra "$out" - '') else src; + src = + if monorepoSrc != null then + runCommand "clang-src-${version}" { inherit (monorepoSrc) passthru; } ( + '' + mkdir -p "$out" + '' + + lib.optionalString (lib.versionAtLeast release_version "14") '' + cp -r ${monorepoSrc}/cmake "$out" + '' + + '' + cp -r ${monorepoSrc}/clang "$out" + cp -r ${monorepoSrc}/clang-tools-extra "$out" + '' + ) + else + src; sourceRoot = "${finalAttrs.src.name}/clang"; @@ -48,179 +58,227 @@ stdenv.mkDerivation (finalAttrs: { # libraries. eg: `clang -munsupported hello.c -lc` ./clang-unsupported-option.patch ] - ++ - lib.optional (lib.versions.major release_version == "13") - # Revert of https://reviews.llvm.org/D100879 - # The malloc alignment assumption is incorrect for jemalloc and causes - # mis-compilation in firefox. - # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 - (getVersionFile "clang/revert-malloc-alignment-assumption.patch") - ++ lib.optional (lib.versionOlder release_version "17") ( - if lib.versionAtLeast release_version "14" then - fetchpatch { - name = "ignore-nostd-link.patch"; - url = "https://github.com/llvm/llvm-project/commit/5b77e752dcd073846b89559d6c0e1a7699e58615.patch"; - relative = "clang"; - hash = "sha256-qzSAmoGY+7POkDhcGgQRPaNQ3+7PIcIc9cZuiE/eLkc="; - } - else - ./ignore-nostd-link-13.diff - ) - ++ [ - (replaceVars - ( - if (lib.versionOlder release_version "16") then - ./clang-11-15-LLVMgold-path.patch - else - ./clang-at-least-16-LLVMgold-path.patch - ) - { - libllvmLibdir = "${libllvm.lib}/lib"; - } + ++ + lib.optional (lib.versions.major release_version == "13") + # Revert of https://reviews.llvm.org/D100879 + # The malloc alignment assumption is incorrect for jemalloc and causes + # mis-compilation in firefox. + # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 + (getVersionFile "clang/revert-malloc-alignment-assumption.patch") + ++ lib.optional (lib.versionOlder release_version "17") ( + if lib.versionAtLeast release_version "14" then + fetchpatch { + name = "ignore-nostd-link.patch"; + url = "https://github.com/llvm/llvm-project/commit/5b77e752dcd073846b89559d6c0e1a7699e58615.patch"; + relative = "clang"; + hash = "sha256-qzSAmoGY+7POkDhcGgQRPaNQ3+7PIcIc9cZuiE/eLkc="; + } + else + ./ignore-nostd-link-13.diff + ) + ++ [ + (replaceVars + ( + if (lib.versionOlder release_version "16") then + ./clang-11-15-LLVMgold-path.patch + else + ./clang-at-least-16-LLVMgold-path.patch ) - ] - # Backport version logic from Clang 16. This is needed by the following patch. - ++ lib.optional (lib.versions.major release_version == "15") (fetchpatch { - name = "clang-darwin-Use-consistent-version-define-stringifying-logic.patch"; - url = "https://github.com/llvm/llvm-project/commit/60a33ded751c86fff9ac1c4bdd2b341fbe4b0649.patch?full_index=1"; - includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; - stripLen = 1; - hash = "sha256-YVTSg5eZLz3po2AUczPNXCK26JA3CuTh6Iqp7hAAKIs="; - }) - # Backport `__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__` support from Clang 17. - # This is needed by newer SDKs (14+). - ++ - lib.optional - ( - lib.versionAtLeast (lib.versions.major release_version) "15" - && lib.versionOlder (lib.versions.major release_version) "17" - ) - (fetchpatch { - name = "clang-darwin-An-OS-version-preprocessor-define.patch"; - url = "https://github.com/llvm/llvm-project/commit/c8e2dd8c6f490b68e41fe663b44535a8a21dfeab.patch?full_index=1"; - includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; - stripLen = 1; - hash = "sha256-Vs32kql7N6qtLqc12FtZHURcbenA7+N3E/nRRX3jdig="; - }) - ++ lib.optional (lib.versions.major release_version == "18") (fetchpatch { - name = "tweak-tryCaptureVariable-for-unevaluated-lambdas.patch"; - url = "https://github.com/llvm/llvm-project/commit/3d361b225fe89ce1d8c93639f27d689082bd8dad.patch"; - # TreeTransform.h is not affected in LLVM 18. - excludes = [ - "docs/ReleaseNotes.rst" - "lib/Sema/TreeTransform.h" - ]; - stripLen = 1; - hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E="; - }) - ++ lib.optional (stdenv.isAarch64 && lib.versions.major release_version == "17") + { + libllvmLibdir = "${libllvm.lib}/lib"; + } + ) + ] + # Backport version logic from Clang 16. This is needed by the following patch. + ++ lib.optional (lib.versions.major release_version == "15") (fetchpatch { + name = "clang-darwin-Use-consistent-version-define-stringifying-logic.patch"; + url = "https://github.com/llvm/llvm-project/commit/60a33ded751c86fff9ac1c4bdd2b341fbe4b0649.patch?full_index=1"; + includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; + stripLen = 1; + hash = "sha256-YVTSg5eZLz3po2AUczPNXCK26JA3CuTh6Iqp7hAAKIs="; + }) + # Backport `__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__` support from Clang 17. + # This is needed by newer SDKs (14+). + ++ + lib.optional + ( + lib.versionAtLeast (lib.versions.major release_version) "15" + && lib.versionOlder (lib.versions.major release_version) "17" + ) + (fetchpatch { + name = "clang-darwin-An-OS-version-preprocessor-define.patch"; + url = "https://github.com/llvm/llvm-project/commit/c8e2dd8c6f490b68e41fe663b44535a8a21dfeab.patch?full_index=1"; + includes = [ "lib/Basic/Targets/OSTargets.cpp" ]; + stripLen = 1; + hash = "sha256-Vs32kql7N6qtLqc12FtZHURcbenA7+N3E/nRRX3jdig="; + }) + ++ lib.optional (lib.versions.major release_version == "18") (fetchpatch { + name = "tweak-tryCaptureVariable-for-unevaluated-lambdas.patch"; + url = "https://github.com/llvm/llvm-project/commit/3d361b225fe89ce1d8c93639f27d689082bd8dad.patch"; + # TreeTransform.h is not affected in LLVM 18. + excludes = [ + "docs/ReleaseNotes.rst" + "lib/Sema/TreeTransform.h" + ]; + stripLen = 1; + hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E="; + }) + ++ + lib.optional (stdenv.isAarch64 && lib.versions.major release_version == "17") # Fixes llvm17 tblgen builds on aarch64. # https://github.com/llvm/llvm-project/issues/106521#issuecomment-2337175680 (getVersionFile "clang/aarch64-tblgen.patch"); - nativeBuildInputs = [ cmake python3 ] + nativeBuildInputs = + [ + cmake + python3 + ] ++ (lib.optional (lib.versionAtLeast release_version "15") ninja) ++ lib.optional (lib.versionAtLeast version "18" && enableManpages) python3.pkgs.myst-parser ++ lib.optional enableManpages python3.pkgs.sphinx ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames; - buildInputs = [ libxml2 libllvm ]; + buildInputs = [ + libxml2 + libllvm + ]; - cmakeFlags = (lib.optionals (lib.versionAtLeast release_version "15") [ - (lib.cmakeFeature "CLANG_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/clang") - ]) ++ [ - (lib.cmakeBool "CLANGD_BUILD_XPC" false) - (lib.cmakeBool "LLVM_ENABLE_RTTI" true) - (lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen") - (lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmTools.tblgen}/bin/clang-tblgen") - ] ++ lib.optionals (lib.versionAtLeast release_version "17") [ - (lib.cmakeBool "LLVM_INCLUDE_TESTS" false) - ] ++ lib.optionals enableManpages [ - (lib.cmakeBool "CLANG_INCLUDE_DOCS" true) - (lib.cmakeBool "LLVM_ENABLE_SPHINX" true) - (lib.cmakeBool "SPHINX_OUTPUT_MAN" true) - (lib.cmakeBool "SPHINX_OUTPUT_HTML" false) - (lib.cmakeBool "SPHINX_WARNINGS_AS_ERRORS" false) - ] ++ lib.optionals (lib.versionAtLeast release_version "15") [ - # 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 - (lib.cmakeFeature "CLANG_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 - (lib.cmakeFeature "CLANG_PSEUDO_GEN" "${buildLlvmTools.tblgen}/bin/clang-pseudo-gen") - ] ++ lib.optional (lib.versionAtLeast release_version "20") (lib.cmakeFeature "LLVM_DIR" "${libllvm.dev}/lib/cmake/llvm") + cmakeFlags = + (lib.optionals (lib.versionAtLeast release_version "15") [ + (lib.cmakeFeature "CLANG_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/clang") + ]) + ++ [ + (lib.cmakeBool "CLANGD_BUILD_XPC" false) + (lib.cmakeBool "LLVM_ENABLE_RTTI" true) + (lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen") + (lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmTools.tblgen}/bin/clang-tblgen") + ] + ++ lib.optionals (lib.versionAtLeast release_version "17") [ + (lib.cmakeBool "LLVM_INCLUDE_TESTS" false) + ] + ++ lib.optionals enableManpages [ + (lib.cmakeBool "CLANG_INCLUDE_DOCS" true) + (lib.cmakeBool "LLVM_ENABLE_SPHINX" true) + (lib.cmakeBool "SPHINX_OUTPUT_MAN" true) + (lib.cmakeBool "SPHINX_OUTPUT_HTML" false) + (lib.cmakeBool "SPHINX_WARNINGS_AS_ERRORS" false) + ] + ++ lib.optionals (lib.versionAtLeast release_version "15") [ + # 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 + (lib.cmakeFeature "CLANG_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 + (lib.cmakeFeature "CLANG_PSEUDO_GEN" "${buildLlvmTools.tblgen}/bin/clang-pseudo-gen") + ] + ++ lib.optional (lib.versionAtLeast release_version "20") ( + lib.cmakeFeature "LLVM_DIR" "${libllvm.dev}/lib/cmake/llvm" + ) ++ devExtraCmakeFlags; - 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}";' - (cd tools && ln -s ../../clang-tools-extra extra) - '' - + lib.optionalString stdenv.hostPlatform.isMusl '' - sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp - ''; + 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}";' + (cd tools && ln -s ../../clang-tools-extra extra) + '' + + lib.optionalString stdenv.hostPlatform.isMusl '' + sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp + ''; - outputs = [ "out" "lib" "dev" "python" ]; + outputs = [ + "out" + "lib" + "dev" + "python" + ]; - postInstall = '' - ln -sv $out/bin/clang $out/bin/cpp - '' + (lib.optionalString (lib.versions.major release_version == "17") '' - mkdir -p $lib/lib/clang - mv $lib/lib/17 $lib/lib/clang/17 - '') + (lib.optionalString (lib.versionAtLeast release_version "19") '' - mv $out/lib/clang $lib/lib/clang - '') + '' + postInstall = + '' + ln -sv $out/bin/clang $out/bin/cpp + '' + + (lib.optionalString (lib.versions.major release_version == "17") '' + mkdir -p $lib/lib/clang + mv $lib/lib/17 $lib/lib/clang/17 + '') + + (lib.optionalString (lib.versionAtLeast release_version "19") '' + mv $out/lib/clang $lib/lib/clang + '') + + '' - # Move libclang to 'lib' output - moveToOutput "lib/libclang.*" "$lib" - moveToOutput "lib/libclang-cpp.*" "$lib" - '' + (if lib.versionOlder release_version "15" then '' - mkdir -p $python/bin $python/share/{clang,scan-view} - '' else '' - mkdir -p $python/bin $python/share/clang/ - '') + '' - 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 - '' + (lib.optionalString (lib.versionOlder release_version "15") '' - mv $out/share/scan-view/*.py $python/share/scan-view - '') + '' - rm $out/bin/c-index-test - patchShebangs $python/bin + # Move libclang to 'lib' output + moveToOutput "lib/libclang.*" "$lib" + moveToOutput "lib/libclang-cpp.*" "$lib" + '' + + ( + if lib.versionOlder release_version "15" then + '' + mkdir -p $python/bin $python/share/{clang,scan-view} + '' + else + '' + mkdir -p $python/bin $python/share/clang/ + '' + ) + + '' + 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 + '' + + (lib.optionalString (lib.versionOlder release_version "15") '' + mv $out/share/scan-view/*.py $python/share/scan-view + '') + + '' + rm $out/bin/c-index-test + patchShebangs $python/bin - mkdir -p $dev/bin - '' + (if lib.versionOlder release_version "15" then '' - cp bin/clang-tblgen $dev/bin - '' else if lib.versionOlder release_version "20" then '' - cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen,clang-pseudo-gen} $dev/bin - '' else '' - cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen} $dev/bin - ''); + mkdir -p $dev/bin + '' + + ( + if lib.versionOlder release_version "15" then + '' + cp bin/clang-tblgen $dev/bin + '' + else if lib.versionOlder release_version "20" then + '' + cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen,clang-pseudo-gen} $dev/bin + '' + else + '' + cp bin/{clang-tblgen,clang-tidy-confusable-chars-gen} $dev/bin + '' + ); - env = lib.optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform && !stdenv.hostPlatform.useLLVM && lib.versionAtLeast release_version "15") { - # The following warning is triggered with (at least) gcc >= - # 12, but appears to occur only for cross compiles. - NIX_CFLAGS_COMPILE = "-Wno-maybe-uninitialized"; - }; + env = + lib.optionalAttrs + ( + stdenv.buildPlatform != stdenv.hostPlatform + && !stdenv.hostPlatform.useLLVM + && lib.versionAtLeast release_version "15" + ) + { + # The following warning is triggered with (at least) gcc >= + # 12, but appears to occur only for cross compiles. + NIX_CFLAGS_COMPILE = "-Wno-maybe-uninitialized"; + }; passthru = { inherit libllvm; isClang = true; - hardeningUnsupportedFlagsByTargetPlatform = targetPlatform: + hardeningUnsupportedFlagsByTargetPlatform = + targetPlatform: [ "fortify3" ] ++ lib.optional ( - (lib.versionOlder release_version "7") - || !targetPlatform.isLinux - || !targetPlatform.isx86_64 + (lib.versionOlder release_version "7") || !targetPlatform.isLinux || !targetPlatform.isx86_64 ) "shadowstack" ++ lib.optional ( - (lib.versionOlder release_version "8") - || !targetPlatform.isAarch64 - || !targetPlatform.isLinux + (lib.versionOlder release_version "8") || !targetPlatform.isAarch64 || !targetPlatform.isLinux ) "pacret" ++ lib.optional ( (lib.versionOlder release_version "11") @@ -235,10 +293,9 @@ stdenv.mkDerivation (finalAttrs: { ) ) "stackclashprotection" ++ lib.optional ( - (lib.versionOlder release_version "15") - || !(targetPlatform.isx86_64 || targetPlatform.isAarch64) + (lib.versionOlder release_version "15") || !(targetPlatform.isx86_64 || targetPlatform.isAarch64) ) "zerocallusedregs" - ++ (finalAttrs.passthru.hardeningUnsupportedFlags or []); + ++ (finalAttrs.passthru.hardeningUnsupportedFlags or [ ]); }; meta = llvm_meta // { @@ -257,26 +314,36 @@ stdenv.mkDerivation (finalAttrs: { ''; mainProgram = "clang"; }; - } // lib.optionalAttrs enableManpages ({ - pname = "clang-manpages"; + } + // lib.optionalAttrs enableManpages ( + { + pname = "clang-manpages"; - installPhase = '' - mkdir -p $out/share/man/man1 - # Manually install clang manpage - cp docs/man/*.1 $out/share/man/man1/ - ''; + installPhase = '' + mkdir -p $out/share/man/man1 + # Manually install clang manpage + cp docs/man/*.1 $out/share/man/man1/ + ''; - outputs = [ "out" ]; + outputs = [ "out" ]; - doCheck = false; + doCheck = false; - meta = llvm_meta // { - description = "man page for Clang ${version}"; - }; - } // (if lib.versionOlder release_version "15" then { - buildPhase = '' - make docs-clang-man - ''; - } else { - ninjaFlags = [ "docs-clang-man" ]; - }))) + meta = llvm_meta // { + description = "man page for Clang ${version}"; + }; + } + // ( + if lib.versionOlder release_version "15" then + { + buildPhase = '' + make docs-clang-man + ''; + } + else + { + ninjaFlags = [ "docs-clang-man" ]; + } + ) + ) +)