Repository: zcash/zcash (language: rust)

Issue:
Clang 18's `intrin.h` is causing build errors on Windows
_Originally posted by @daira in https://github.com/zcash/zcash/issues/6910#issuecomment-2240103198_

> Clang 18's `intrin.h` is causing build errors on Windows. Seems related to https://github.com/llvm/llvm-project/issues/95133 (it's not that specific function, but it's the same error in the same header file).
>
> [...] It might be possible to work around it by a patch to the dependency / dependencies that use `intrin.h` (looks like just `libsodium`).


            


Discussion / hints:
Same problem, is there a solution?
In file included from randombytes/internal/randombytes_internal_random.c:53:
In file included from /opt/zcash-win/depends/x86_64-w64-mingw32/native/lib/clang/18/include/immintrin.h:36:
/opt/zcash-win/depends/x86_64-w64-mingw32/native/lib/clang/18/include/pmmintrin.h:58:1: error: attribute 'target' multiversioning cannot be combined with attribute 'always_inline'
   58 | _mm_addsub_ps(__m128 __a, __m128 __b)
      | ^
I confirmed that this issue is reproducible with `libsodium`'s current `master` branch, and opened https://github.com/jedisct1/libsodium/discussions/1413.

Fix patch:
diff --git a/depends/packages/libcxx.mk b/depends/packages/libcxx.mk
index c1a09712948..39532126d24 100644
--- a/depends/packages/libcxx.mk
+++ b/depends/packages/libcxx.mk
@@ -1,6 +1,6 @@
 package=libcxx
 $(package)_version=$(if $(native_clang_version_$(host_arch)_$(host_os)),$(native_clang_version_$(host_arch)_$(host_os)),$(if $(native_clang_version_$(host_os)),$(native_clang_version_$(host_os)),$(native_clang_default_version)))
-$(package)_msys2_version=18.1.8-1
+$(package)_msys2_version=18.1.6-1
 
 ifneq ($(canonical_host),$(build))
 ifneq ($(host_os),mingw32)
@@ -30,7 +30,7 @@ else
 $(package)_download_path=https://repo.msys2.org/mingw/x86_64
 $(package)_download_file=mingw-w64-x86_64-libc++-$($(package)_msys2_version)-any.pkg.tar.zst
 $(package)_file_name=mingw-w64-x86_64-libcxx-$($(package)_msys2_version)-any.pkg.tar.zst
-$(package)_sha256_hash=4673dba0e8c4bac3c4d645a4a64b947855228b21c82f6366b201e7636a78bc1a
+$(package)_sha256_hash=6857cc24cbe326c0cd571e52872bffe95f64ab67a8c5032c04875a9778fdece7
 
 define $(package)_stage_cmds
   mkdir -p $($(package)_staging_prefix_dir)/lib && \
diff --git a/depends/packages/libsodium.mk b/depends/packages/libsodium.mk
index cb13dd4634f..c0b9fbc4472 100644
--- a/depends/packages/libsodium.mk
+++ b/depends/packages/libsodium.mk
@@ -4,12 +4,13 @@ $(package)_download_path=https://download.libsodium.org/libsodium/releases/
 $(package)_file_name=$(package)-$($(package)_version).tar.gz
 $(package)_sha256_hash=ebb65ef6ca439333c2bb41a0c1990587288da07f6c7fd07cb3a18cc18d30ce19
 $(package)_dependencies=
-$(package)_patches=1.0.15-pubkey-validation.diff 1.0.15-signature-validation.diff
+$(package)_patches=1.0.15-pubkey-validation.diff 1.0.15-signature-validation.diff 1.0.20-immintrin-conflict.diff
 $(package)_config_opts=
 
 define $(package)_preprocess_cmds
   patch -p1 < $($(package)_patch_dir)/1.0.15-pubkey-validation.diff && \
   patch -p1 < $($(package)_patch_dir)/1.0.15-signature-validation.diff && \
+  patch -p1 < $($(package)_patch_dir)/1.0.20-immintrin-conflict.diff && \
   cd $($(package)_build_subdir); DO_NOT_UPDATE_CONFIG_SCRIPTS=1 ./autogen.sh
 endef
 
diff --git a/depends/patches/libsodium/1.0.20-immintrin-conflict.diff b/depends/patches/libsodium/1.0.20-immintrin-conflict.diff
new file mode 100644
index 00000000000..e58a0904694
--- /dev/null
+++ b/depends/patches/libsodium/1.0.20-immintrin-conflict.diff
@@ -0,0 +1,34 @@
+diff -ur libsodium-1.0.20-orig/src/libsodium/randombytes/internal/randombytes_internal_random.c libsodium-1.0.20/src/libsodium/randombytes/internal/randombytes_internal_random.c
+--- libsodium-1.0.20-orig/src/libsodium/randombytes/internal/randombytes_internal_random.c	2024-05-25 12:15:18.000000000 +0000
++++ libsodium-1.0.20/src/libsodium/randombytes/internal/randombytes_internal_random.c	2024-09-26 18:11:50.173273070 +0000
+@@ -44,14 +44,6 @@
+ #ifdef BLOCK_ON_DEV_RANDOM
+ # include <poll.h>
+ #endif
+-#ifdef HAVE_RDRAND
+-# ifdef __clang__
+-#  pragma clang attribute push(__attribute__((target("rdrnd"))), apply_to = function)
+-# elif defined(__GNUC__)
+-#  pragma GCC target("rdrnd")
+-# endif
+-# include <immintrin.h>
+-#endif
+ 
+ #include "core.h"
+ #include "crypto_core_hchacha20.h"
+@@ -109,6 +101,15 @@
+ # endif
+ #endif
+ 
++#ifdef HAVE_RDRAND
++# ifdef __clang__
++#  pragma clang attribute push(__attribute__((target("rdrnd"))), apply_to = function)
++# elif defined(__GNUC__)
++#  pragma GCC target("rdrnd")
++# endif
++# include <immintrin.h>
++#endif
++
+ typedef struct InternalRandomGlobal_ {
+     int           initialized;
+     int           random_data_source_fd;


Test patch: