Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building zig with clang-cl on Windows #20331

Merged
merged 12 commits into from
Jan 29, 2025

Conversation

vahur
Copy link
Contributor

@vahur vahur commented Jun 17, 2024

When building zig with clang-cl 18 on Windows, there were couple of issues:

  1. zig_import macro had too few arguments:
D:\Projects\zig\stage1\zig.h(3377,1): error: too few arguments provided to function-like macro invocation
 3377 | zig_common_float_builtins(32)
      | ^
D:\Projects\zig\stage1\zig.h(3362,5): note: expanded from macro 'zig_common_float_builtins'
 3362 |     zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fmax)))(zig_f##w, zig_max_f##w, zig_libc_name_f##w(fmax), (zig_f##w x, zig_f##w y), (x, y)) \
      |     ^
D:\Projects\zig\stage1\zig.h(29,37): note: expanded from macro 'zig_expand_concat'
   29 | #define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
      |                                     ^
D:\Projects\zig\stage1\zig.h(28,30): note: expanded from macro 'zig_concat'
   28 | #define zig_concat(lhs, rhs) lhs##rhs
      |                              ^
<scratch space>(15,1): note: expanded from here
   15 | zig_expand_import_1
      | ^
D:\Projects\zig\stage1\zig.h(236,76): note: expanded from macro 'zig_expand_import_1'
  236 | #define zig_expand_import_1(Type, fn_name, libc_name, sig_args, call_args) zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args)
      |                                                                            ^
D:\Projects\zig\stage1\zig.h(228,120): note: expanded from macro 'zig_import_builtin'
  228 | #define zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args) zig_import(Type, fn_name, sig_args, call_args)
      |
       ^
D:\Projects\zig\stage1\zig.h(226,9): note: macro 'zig_import' defined here
  226 | #define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;
  1. zig_export failed to export symbols with clang-cl.
  2. zig2.c compilation failed with an error
In file included from D:\Projects\Misc\zig\build\zig2.c:3:
D:\Projects\zig\stage1\zig.h(3844,11): error: address argument to atomic operation must be a pointer to _Atomic type ('volatile zig_u128 *' (aka 'volatile unsigned __int128 *') invalid)
 3844 |     (void)zig_cmpxchg_strong(obj, expected, expected, zig_memory_order_seq_cst, zig_memory_order_seq_cst, u128, zig_u128);
      |           ^                  ~~~
D:\Projects\zig\stage1\zig.h(3576,85): note: expanded from macro 'zig_cmpxchg_strong'
 3576 | #define zig_cmpxchg_strong(     obj, expected, desired, succ, fail, Type, ReprType) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
      |                                                                                     ^                                       ~~~
D:\Frameworks\llvm-18\lib\clang\18\include\stdatomic.h(145,49): note: expanded from macro 'atomic_compare_exchange_strong_explicit'
  145 | #define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong

With this patch, it is possible to compile zig on Windows with clang as MSVC replacement. Target triple is x86_64-windows-msvc.

Sample build script - build-zig-stage3.cmd:

@echo off
setlocal
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
SET LLVM_DIR=D:/Frameworks/llvm-18-zig
SET CLANG_DIR=D:/Frameworks/llvm-18
SET CFLAGS=/Oy /GS- /clang:-march=native /clang:-mtune=native /D_CRT_SECURE_NO_WARNINGS -Wno-deprecated-declarations

cd /d D:\Projects\zig
rmdir /s /q build
mkdir build
cd build

cmake ..  ^
  -DCMAKE_BUILD_TYPE=Release ^
  -DCMAKE_C_COMPILER="%CLANG_DIR%/bin/clang-cl.exe" ^
  -DCMAKE_CXX_COMPILER="%CLANG_DIR%/bin/clang-cl.exe" ^
  -DCMAKE_C_FLAGS="%CFLAGS%" ^
  -DCMAKE_CXX_FLAGS="%CFLAGS%" ^
  -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ^
  -DCMAKE_PREFIX_PATH="%LLVM_DIR%" ^
  -DCMAKE_RC_COMPILER="%CLANG_DIR%/bin/llvm-rc.exe" ^
  -DZIG_USE_LLVM_CONFIG=OFF ^
  -DZIG_STATIC_LLVM=ON ^
  -DZIG_TARGET_TRIPLE=x86_64-windows-msvc ^
  -G Ninja

ninja

@vahur vahur changed the title Fix building zig with clang-cl in Windows Fix building zig with clang-cl on Windows Jun 17, 2024
@alexrp
Copy link
Member

alexrp commented Oct 16, 2024

Note that changes to zig.h should be done in lib/zig.h first. They will then eventually flow to stage1/zig.h on the next zig1.wasm update.

cc @kcbanner

@vahur
Copy link
Contributor Author

vahur commented Dec 30, 2024

@alexrp I updated lib/zig.h with changes from stage1/zig.h. It now compiles with clang-cl even if stage1/zig.h is replaced with the one from lib.

@alexrp
Copy link
Member

alexrp commented Jan 26, 2025

@alexrp I updated lib/zig.h with changes from stage1/zig.h. It now compiles with clang-cl even if stage1/zig.h is replaced with the one from lib.

You still need to revert the changes to stage1/zig.h. To be clear, that file should never really be touched manually; it is updated when a Zig core team member does zig build update-zig1 to update stage1/zig1.wasm.

(Also, this has merge conflicts that need to be resolved.)

lib/zig.h Outdated Show resolved Hide resolved
lib/zig.h Outdated Show resolved Hide resolved
lib/zig.h Outdated Show resolved Hide resolved
@vahur
Copy link
Contributor Author

vahur commented Jan 28, 2025

@alexrp I reverted stage1/zig.h and made the suggested changes to lib/zig.h

lib/zig.h Outdated Show resolved Hide resolved
@alexrp alexrp enabled auto-merge (squash) January 29, 2025 08:34
@alexrp alexrp merged commit c4445bc into ziglang:master Jan 29, 2025
10 checks passed
@vahur vahur deleted the fix-build-with-clang-cl branch January 31, 2025 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants