From 4e3d494ff8f8b2746a344a685ef44fb9867a2178 Mon Sep 17 00:00:00 2001
From: Augustin Bussy <augustin.bussy@cscs.ch>
Date: Tue, 8 Oct 2024 15:31:07 +0200
Subject: [PATCH 1/4] [Libxc] update to version 7.0.0

---
 L/Libxc/Libxc/build_tarballs.jl               |  4 +-
 L/Libxc/Libxc_GPU/build_tarballs.jl           | 34 ++++-----
 .../bundled/patches/cmake-cuda.patch          | 72 -------------------
 .../bundled/patches/source-fixes.patch        | 21 ------
 L/Libxc/sources.jl                            |  2 +-
 5 files changed, 16 insertions(+), 117 deletions(-)
 delete mode 100644 L/Libxc/Libxc_GPU/bundled/patches/cmake-cuda.patch
 delete mode 100644 L/Libxc/Libxc_GPU/bundled/patches/source-fixes.patch

diff --git a/L/Libxc/Libxc/build_tarballs.jl b/L/Libxc/Libxc/build_tarballs.jl
index 099a35b4439..0877ea191db 100644
--- a/L/Libxc/Libxc/build_tarballs.jl
+++ b/L/Libxc/Libxc/build_tarballs.jl
@@ -1,7 +1,7 @@
 using BinaryBuilder, Pkg
 
 name = "Libxc"
-version = v"6.1.0"
+version = v"7.0.0"
 include("../sources.jl")
 
 
@@ -37,4 +37,4 @@ dependencies = [
 
 # Build the tarballs, and possibly a `build.jl` as well.
 build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies;
-               preferred_gcc_version=v"5", julia_compat="1.6")
+               preferred_gcc_version=v"8", julia_compat="1.8")
diff --git a/L/Libxc/Libxc_GPU/build_tarballs.jl b/L/Libxc/Libxc_GPU/build_tarballs.jl
index 30d3634348a..53d12c9411e 100644
--- a/L/Libxc/Libxc_GPU/build_tarballs.jl
+++ b/L/Libxc/Libxc_GPU/build_tarballs.jl
@@ -5,43 +5,35 @@ include(joinpath(YGGDRASIL_DIR, "fancy_toys.jl"))
 include(joinpath(YGGDRASIL_DIR, "platforms", "cuda.jl"))
 
 name = "Libxc_GPU"
-version = v"6.1.0"
+version = v"7.0.0"
 include("../sources.jl")
 
-sources = [
-    sources;
-    DirectorySource("./bundled")
-]
-
 # Bash recipe for building GPU version
 # Notes:
 #   - 3rd and 4th derivatives (KXC, LXC) not built since gives a binary size of ~200MB
 script = raw"""
-cd $WORKSPACE/srcdir/libxc-*/
 
-# Needed for Libxc 6.1.0 as these backport some fixes on libxc master
-# On Libxc > 6.1.0 we can also remove the -DBUILD_TESTING=OFF
-atomic_patch -p1 ${WORKSPACE}/srcdir/patches/cmake-cuda.patch
-atomic_patch -p1 ${WORKSPACE}/srcdir/patches/source-fixes.patch
+cd $WORKSPACE/srcdir/libxc-*/
 
 mkdir libxc_build
 cd libxc_build
-cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
+
+mv ${WORKSPACE}/destdir/cuda/lib ${WORKSPACE}/destdir/cuda/lib64
+
+cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN}\
     -DCMAKE_BUILD_TYPE=Release -DENABLE_XHOST=OFF -DBUILD_SHARED_LIBS=ON \
-    -DENABLE_CUDA=ON -DCMAKE_CUDA_COMPILER=$prefix/cuda/bin/nvcc -DBUILD_TESTING=OFF \
-    -DENABLE_FORTRAN=OFF -DDISABLE_KXC=ON ..
+    -DENABLE_CUDA=ON -DCMAKE_CUDA_COMPILER=$prefix/cuda/bin/nvcc \
+    -DBUILD_TESTING=OFF -DENABLE_FORTRAN=OFF \
+    -DDISABLE_KXC=ON ..
 
 make -j${nproc}
 make install
 """
 
-augment_platform_block = CUDA.augment
-
 # Override the default platforms
-platforms = CUDA.supported_platforms()
+platforms = CUDA.supported_platforms(; min_version=v"11.4")
 filter!(p -> arch(p) == "x86_64", platforms)
 
-
 # The products that we will ensure are always built
 products = [
     LibraryProduct("libxc", :libxc)
@@ -49,17 +41,17 @@ products = [
 
 # Dependencies that must be installed before this package can be built
 dependencies = [
-    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae")),
+    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae"))
 ]
 
 # Build Libxc for all supported CUDA toolkits
 for platform in platforms
     should_build_platform(triplet(platform)) || continue
 
-    cuda_deps = CUDA.required_dependencies(platform)
+    cuda_deps = CUDA.required_dependencies(platform; static_sdk=true)
 
     build_tarballs(ARGS, name, version, sources, script, [platform],
                    products, [dependencies; cuda_deps]; lazy_artifacts=true,
-                   julia_compat="1.7", augment_platform_block,
+                   julia_compat="1.8", augment_platform_block=CUDA.augment, preferred_gcc_version=v"8",
                    skip_audit=true, dont_dlopen=true)
 end
diff --git a/L/Libxc/Libxc_GPU/bundled/patches/cmake-cuda.patch b/L/Libxc/Libxc_GPU/bundled/patches/cmake-cuda.patch
deleted file mode 100644
index 37b4dedc249..00000000000
--- a/L/Libxc/Libxc_GPU/bundled/patches/cmake-cuda.patch
+++ /dev/null
@@ -1,72 +0,0 @@
-diff --git a/CMakeLists.txt b/CMakeLists.txt
-index a45f2db9e..2ee468621 100644
---- a/CMakeLists.txt
-+++ b/CMakeLists.txt
-@@ -11,6 +11,16 @@ cmake_minimum_required(VERSION 3.1)
- set(CMAKE_C_STANDARD 99)
- list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
- 
-+if(ENABLE_CUDA)
-+  cmake_minimum_required(VERSION 3.8)
-+  set(CMAKE_VERBOSE_MAKEFILE ON)
-+  enable_language(CUDA)
-+  set(CMAKE_CUDA_STANDARD 98)
-+  if(POLICY CMP0104)
-+    cmake_policy(SET CMP0104 OLD)
-+  endif()
-+endif()
-+
- ################################### Options ####################################
- include(psi4OptionsTools)
- option_with_default(CMAKE_BUILD_TYPE "Build type" Release)
-@@ -443,6 +453,11 @@ set(raw_sources_list
- set(src_prefix "src/")
- string(REGEX REPLACE "([^;]+)" "${src_prefix}\\1" sources_list "${raw_sources_list}")
- 
-+
-+if(ENABLE_CUDA)
-+  set_source_files_properties(${sources_list} PROPERTIES LANGUAGE CUDA)
-+endif()
-+
- set(raw_sources_list_f03
-   src/libxc_master.F90
-   )
-@@ -468,6 +483,7 @@ set(CMAKE_INSTALL_RPATH ${base} ${base}/${relDir})
- 
- # STATIC/SHARED on below governed by BUILD_SHARED_LIBS
- add_library(xc ${sources_list})
-+set_target_properties(xc PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
- target_link_libraries(xc INTERFACE ${STANDARD_MATH_LIBRARY})
- set_target_properties(xc PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_FPIC}
-   SOVERSION ${${PROJECT_NAME}_SOMAJOR})
-@@ -493,9 +509,15 @@ if(ENABLE_FORTRAN)
- endif()
- 
- add_executable(xc-info "${src_prefix}/xc-info.c")
-+if(ENABLE_CUDA)
-+  set_source_files_properties("${src_prefix}/xc-info.c" PROPERTIES LANGUAGE CUDA)
-+endif()
- target_link_libraries(xc-info xc)
- 
- add_executable(xc-threshold "${src_prefix}/xc-threshold.c")
-+if(ENABLE_CUDA)
-+  set_source_files_properties("${src_prefix}/xc-threshold.c" PROPERTIES LANGUAGE CUDA)
-+endif()
- target_link_libraries(xc-threshold xc)
- 
- 
-diff --git a/config.h.cmake.in b/config.h.cmake.in
-index 7b151e5bf..816fd600e 100644
---- a/config.h.cmake.in
-+++ b/config.h.cmake.in
-@@ -7,5 +7,10 @@
- #define FC_FUNC_ FC_FUNC_GLOBAL_
- #endif
- 
-+#cmakedefine ENABLE_CUDA @ENABLE_CUDA@
-+#ifdef ENABLE_CUDA
-+#define HAVE_CUDA 1
-+#endif
-+
- #include <stdio.h>
- 
diff --git a/L/Libxc/Libxc_GPU/bundled/patches/source-fixes.patch b/L/Libxc/Libxc_GPU/bundled/patches/source-fixes.patch
deleted file mode 100644
index ebac855517e..00000000000
--- a/L/Libxc/Libxc_GPU/bundled/patches/source-fixes.patch
+++ /dev/null
@@ -1,21 +0,0 @@
-diff --git a/src/util.h b/src/util.h
-index ce26160bd..1f1c3b820 100644
---- a/src/util.h
-+++ b/src/util.h
-@@ -331,14 +331,14 @@ void libxc_free(void *ptr);
- #else
- 
- template <class int_type>
--auto libxc_malloc(const int_type size){
-+void* libxc_malloc(const int_type size){
-   void * mem;
-   cudaMallocManaged(&mem, size);
-   return mem;
- }
- 
- template <class int_type1, class int_type2>
--auto libxc_calloc(const int_type1 size1, const int_type2 size2){
-+void* libxc_calloc(const int_type1 size1, const int_type2 size2){
-   void * mem;
-   cudaMallocManaged(&mem, size1*size2);
-   cudaMemset(mem, 0, size1*size2);
diff --git a/L/Libxc/sources.jl b/L/Libxc/sources.jl
index c146ef991d5..ee9bb0d3834 100644
--- a/L/Libxc/sources.jl
+++ b/L/Libxc/sources.jl
@@ -1,5 +1,5 @@
 # Sources required for all builds
 sources = [
     ArchiveSource("https://gitlab.com/libxc/libxc/-/archive/$(version)/libxc-$(version).tar.gz",
-                  "f593745fa47ebfb9ddc467aaafdc2fa1275f0d7250c692ce9761389a90dd8eaf"),
+                  "8d4e343041c9cd869833822f57744872076ae709a613c118d70605539fb13a77"),
 ]

From 6d2d7ef0743a6b1a3d9d77853faf1cddb61f6a12 Mon Sep 17 00:00:00 2001
From: Augustin Bussy <augustin.bussy@cscs.ch>
Date: Tue, 3 Dec 2024 17:54:38 +0100
Subject: [PATCH 2/4] Clean-up

---
 L/Libxc/Libxc_GPU/build_tarballs.jl | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/L/Libxc/Libxc_GPU/build_tarballs.jl b/L/Libxc/Libxc_GPU/build_tarballs.jl
index 53d12c9411e..44579977b28 100644
--- a/L/Libxc/Libxc_GPU/build_tarballs.jl
+++ b/L/Libxc/Libxc_GPU/build_tarballs.jl
@@ -12,22 +12,23 @@ include("../sources.jl")
 # Notes:
 #   - 3rd and 4th derivatives (KXC, LXC) not built since gives a binary size of ~200MB
 script = raw"""
-
 cd $WORKSPACE/srcdir/libxc-*/
 
+ln -s $prefix/cuda/lib $prefix/cuda/lib64
+
 mkdir libxc_build
 cd libxc_build
 
-mv ${WORKSPACE}/destdir/cuda/lib ${WORKSPACE}/destdir/cuda/lib64
-
 cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN}\
     -DCMAKE_BUILD_TYPE=Release -DENABLE_XHOST=OFF -DBUILD_SHARED_LIBS=ON \
     -DENABLE_CUDA=ON -DCMAKE_CUDA_COMPILER=$prefix/cuda/bin/nvcc \
     -DBUILD_TESTING=OFF -DENABLE_FORTRAN=OFF \
     -DDISABLE_KXC=ON ..
 
-make -j${nproc}
-make install
+cmake --build . --parallel $nproc
+cmake --install .
+
+unlink $prefix/cuda/lib64
 """
 
 # Override the default platforms
@@ -52,6 +53,5 @@ for platform in platforms
 
     build_tarballs(ARGS, name, version, sources, script, [platform],
                    products, [dependencies; cuda_deps]; lazy_artifacts=true,
-                   julia_compat="1.8", augment_platform_block=CUDA.augment, preferred_gcc_version=v"8",
-                   skip_audit=true, dont_dlopen=true)
+                   julia_compat="1.8", augment_platform_block=CUDA.augment, preferred_gcc_version=v"8")
 end

From 33f77f72021051682952f73e1083bbd7c2d45195 Mon Sep 17 00:00:00 2001
From: Augustin Bussy <augustin.bussy@cscs.ch>
Date: Wed, 4 Dec 2024 10:30:18 +0100
Subject: [PATCH 3/4] cosmetics

---
 L/Libxc/Libxc/build_tarballs.jl     | 13 +++++++++----
 L/Libxc/Libxc_GPU/build_tarballs.jl | 22 +++++++++++++++-------
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/L/Libxc/Libxc/build_tarballs.jl b/L/Libxc/Libxc/build_tarballs.jl
index 0877ea191db..d6d041a26f3 100644
--- a/L/Libxc/Libxc/build_tarballs.jl
+++ b/L/Libxc/Libxc/build_tarballs.jl
@@ -13,9 +13,13 @@ cd $WORKSPACE/srcdir/libxc-*/
 
 mkdir libxc_build
 cd libxc_build
-cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
-    -DCMAKE_BUILD_TYPE=Release -DENABLE_XHOST=OFF -DBUILD_SHARED_LIBS=ON \
-    -DENABLE_FORTRAN=OFF -DDISABLE_KXC=ON ..
+cmake -DCMAKE_INSTALL_PREFIX=$prefix \
+      -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
+      -DCMAKE_BUILD_TYPE=Release \
+      -DBUILD_SHARED_LIBS=ON \
+      -DENABLE_XHOST=OFF \
+      -DENABLE_FORTRAN=OFF \
+      -DDISABLE_KXC=ON ..
 
 make -j${nproc}
 make install
@@ -37,4 +41,5 @@ dependencies = [
 
 # Build the tarballs, and possibly a `build.jl` as well.
 build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies;
-               preferred_gcc_version=v"8", julia_compat="1.8")
+               preferred_gcc_version=v"8",
+               julia_compat="1.8")
diff --git a/L/Libxc/Libxc_GPU/build_tarballs.jl b/L/Libxc/Libxc_GPU/build_tarballs.jl
index 44579977b28..2f7126939ab 100644
--- a/L/Libxc/Libxc_GPU/build_tarballs.jl
+++ b/L/Libxc/Libxc_GPU/build_tarballs.jl
@@ -19,11 +19,16 @@ ln -s $prefix/cuda/lib $prefix/cuda/lib64
 mkdir libxc_build
 cd libxc_build
 
-cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN}\
-    -DCMAKE_BUILD_TYPE=Release -DENABLE_XHOST=OFF -DBUILD_SHARED_LIBS=ON \
-    -DENABLE_CUDA=ON -DCMAKE_CUDA_COMPILER=$prefix/cuda/bin/nvcc \
-    -DBUILD_TESTING=OFF -DENABLE_FORTRAN=OFF \
-    -DDISABLE_KXC=ON ..
+cmake -DCMAKE_INSTALL_PREFIX=$prefix \
+      -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
+      -DCMAKE_BUILD_TYPE=Release \
+      -DCMAKE_CUDA_COMPILER=$prefix/cuda/bin/nvcc \
+      -DBUILD_SHARED_LIBS=ON \
+      -DBUILD_TESTING=OFF \
+      -DENABLE_CUDA=ON \
+      -DENABLE_XHOST=OFF\
+      -DENABLE_FORTRAN=OFF \
+      -DDISABLE_KXC=ON ..
 
 cmake --build . --parallel $nproc
 cmake --install .
@@ -52,6 +57,9 @@ for platform in platforms
     cuda_deps = CUDA.required_dependencies(platform; static_sdk=true)
 
     build_tarballs(ARGS, name, version, sources, script, [platform],
-                   products, [dependencies; cuda_deps]; lazy_artifacts=true,
-                   julia_compat="1.8", augment_platform_block=CUDA.augment, preferred_gcc_version=v"8")
+                   products, [dependencies; cuda_deps];
+                   lazy_artifacts=true,
+                   julia_compat="1.8",
+                   augment_platform_block=CUDA.augment,
+                   preferred_gcc_version=v"8")
 end

From 040da1f2b5ac1629f47605fc7fb7c441a6d0053d Mon Sep 17 00:00:00 2001
From: Augustin Bussy <augustin.bussy@cscs.ch>
Date: Mon, 9 Dec 2024 15:10:27 +0100
Subject: [PATCH 4/4] add trailing coma back in

---
 L/Libxc/Libxc_GPU/build_tarballs.jl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/L/Libxc/Libxc_GPU/build_tarballs.jl b/L/Libxc/Libxc_GPU/build_tarballs.jl
index 2f7126939ab..91a624f55ac 100644
--- a/L/Libxc/Libxc_GPU/build_tarballs.jl
+++ b/L/Libxc/Libxc_GPU/build_tarballs.jl
@@ -47,7 +47,7 @@ products = [
 
 # Dependencies that must be installed before this package can be built
 dependencies = [
-    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae"))
+    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae")),
 ]
 
 # Build Libxc for all supported CUDA toolkits