Subject: [PATCH 09/14] gnu: Add llvm-17.

From: Hilton Chain <[email-masked]>
Date: Mon, 23 Oct 2023 20:43:53 +0800

* gnu/packages/llvm.scm (llvm-17): New variable.
* gnu/packages/patches/clang-17.0-libc-search-path.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it.

Change-Id: I8cdda0737b1137cf86887fb5b854e71977a88b6b
---
 gnu/local.mk                                  |  1 +
 gnu/packages/llvm.scm                         | 12 ++-
 .../patches/clang-17.0-libc-search-path.patch | 90 +++++++++++++++++++
 3 files changed, 101 insertions(+), 2 deletions(-)
 create mode 100644 gnu/packages/patches/clang-17.0-libc-search-path.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index d606cffd1f..6057456c2b 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1017,6 +1017,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/clang-14.0-libc-search-path.patch	\
   %D%/packages/patches/clang-15.0-libc-search-path.patch	\
   %D%/packages/patches/clang-16.0-libc-search-path.patch	\
+  %D%/packages/patches/clang-17.0-libc-search-path.patch	\
   %D%/packages/patches/clang-runtime-asan-build-fixes.patch	\
   %D%/packages/patches/clang-runtime-esan-build-fixes.patch	\
   %D%/packages/patches/clang-runtime-9-libsanitizer-mode-field.patch	\
diff --git a/gnu/packages/llvm.scm b/gnu/packages/llvm.scm
index ee2131b0dd..eb806b9cf0 100644
--- a/gnu/packages/llvm.scm
+++ b/gnu/packages/llvm.scm
@@ -591,12 +591,14 @@ (define-public (make-clang-toolchain clang libomp)
 (define %llvm-monorepo-hashes
   '(("14.0.6" . "14f8nlvnmdkp9a9a79wv67jbmafvabczhah8rwnqrgd5g3hfxxxx")
     ("15.0.7" . "12sggw15sxq1krh1mfk3c1f07h895jlxbcifpwk3pznh4m1rjfy2")
-    ("16.0.6" . "0jxmapg7shwkl88m4mqgfjv4ziqdmnppxhjz6vz51ycp2x4nmjky")))
+    ("16.0.6" . "0jxmapg7shwkl88m4mqgfjv4ziqdmnppxhjz6vz51ycp2x4nmjky")
+    ("17.0.3" . "1fhrnsv87if7kbqmrsxy2r7ykx3gnr9lmbmvkhvycc91ii4ihybx")))
 
 (define %llvm-patches
   '(("14.0.6" . ("clang-14.0-libc-search-path.patch"))
     ("15.0.7" . ("clang-15.0-libc-search-path.patch"))
-    ("16.0.6" . ("clang-16.0-libc-search-path.patch"))))
+    ("16.0.6" . ("clang-16.0-libc-search-path.patch"))
+    ("17.0.3" . ("clang-17.0-libc-search-path.patch"))))
 
 (define (llvm-monorepo version)
   (origin
@@ -1486,6 +1488,12 @@ (define-public libomp-16
 (define-public clang-toolchain-16
   (make-clang-toolchain clang-16 libomp-16))
 
+(define-public llvm-17
+  (package
+    (inherit llvm-15)
+    (version "17.0.3")
+    (source (llvm-monorepo version))))
+
 ;; Default LLVM and Clang version.
 (define-public libomp libomp-13)
 (define-public llvm llvm-13)
diff --git a/gnu/packages/patches/clang-17.0-libc-search-path.patch b/gnu/packages/patches/clang-17.0-libc-search-path.patch
new file mode 100644
index 0000000000..dd4d8cb998
--- /dev/null
+++ b/gnu/packages/patches/clang-17.0-libc-search-path.patch
@@ -0,0 +1,90 @@
+Clang attempts to guess file names based on the OS and distro (yes!),
+but unfortunately, that doesn't work for us.
+
+This patch makes it easy to insert libc's $libdir so that Clang passes the
+correct absolute file name of crt1.o etc. to 'ld'.  It also disables all
+the distro-specific stuff and removes the hard-coded FHS directory names
+to make sure Clang also works on foreign distros.
+
+diff --git a/clang/lib/Driver/Distro.cpp b/clang/lib/Driver/Distro.cpp
+index 6e0087565941..c0a3ba7d7702 100644
+--- a/clang/lib/Driver/Distro.cpp
++++ b/clang/lib/Driver/Distro.cpp
+@@ -99,6 +99,10 @@ static Distro::DistroType DetectLsbRelease(llvm::vfs::FileSystem &VFS) {
+ }
+ 
+ static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
++  // The compiler should always behave the same, even when used via Guix on a
++  // foreign distro.
++  return Distro::UnknownDistro;
++
+   Distro::DistroType Version = Distro::UnknownDistro;
+ 
+   // Newer freedesktop.org's compilant systemd-based systems
+diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
+index 3a577650eb08..ca3373edc127 100644
+--- a/clang/lib/Driver/ToolChains/Cuda.cpp
++++ b/clang/lib/Driver/ToolChains/Cuda.cpp
+@@ -128,6 +128,9 @@ CudaInstallationDetector::CudaInstallationDetector(
+     const Driver &D, const llvm::Triple &HostTriple,
+     const llvm::opt::ArgList &Args)
+     : D(D) {
++  // Don't look for CUDA in /usr.
++  return;
++
+   struct Candidate {
+     std::string Path;
+     bool StrictChecking;
+diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
+index 1ba222bf83b1..28766c5ab792 100644
+--- a/clang/lib/Driver/ToolChains/Linux.cpp
++++ b/clang/lib/Driver/ToolChains/Linux.cpp
+@@ -220,6 +220,10 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
+ 
+   Generic_GCC::PushPPaths(PPaths);
+ 
++  // Comment out the distro-specific tweaks so that they don't bite when
++  // using Guix on a foreign distro.
++#if 0
++
+   Distro Distro(D.getVFS(), Triple);
+ 
+   if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
+@@ -280,6 +284,7 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
+ 
+ #ifdef ENABLE_LINKER_BUILD_ID
+   ExtraOpts.push_back("--build-id");
++#endif
+ #endif
+ 
+   // The selection of paths to try here is designed to match the patterns which
+@@ -301,6 +306,7 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
+   }
+   Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
+ 
++#if 0
+   addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths);
+   addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths);
+ 
+@@ -329,11 +335,21 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
+     addPathIfExists(D, concat(SysRoot, "/", OSLibDir, ABIName), Paths);
+     addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir, ABIName), Paths);
+   }
++#endif
+ 
+   Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
+ 
++#if 0
+   addPathIfExists(D, concat(SysRoot, "/lib"), Paths);
+   addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths);
++#endif
++
++  // Add libc's lib/ directory to the search path, so that crt1.o, crti.o,
++  // and friends can be found.
++  addPathIfExists(D, "@GLIBC_LIBDIR@", Paths);
++
++  // Add GCC's lib/ directory so libstdc++.so can be found.
++  addPathIfExists(D, GCCInstallation.getParentLibPath(), Paths);
+ }
+ 
+ ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const {
-- 
2.41.0