Skip to content

Conversation

@chrisirhc
Copy link

@chrisirhc chrisirhc commented Nov 21, 2024

Disclaimer: No idea what I'm doing.

@chrisirhc
Copy link
Author

chrisirhc commented Nov 22, 2024

Hit compilation errors which indicate that threads weren't turned on, when doing bazel test //test/c:test_libc_wasip1_wasm32 (when compiling on my Mac):

In file included from external/_main~toolchains~zig_sdk/lib/libcxx/src/barrier.cpp:9:
external/_main~toolchains~zig_sdk/lib/libcxx/include/barrier:51:4: error: "<barrier> is not supported since libc++ has been configured without support for threads."
   51 | #  error "<barrier> is not supported since libc++ has been configured without support for threads."
      |    ^
external/_main~toolchains~zig_sdk/lib/libcxx/src/condition_variable_destructor.cpp:26:3: error: unknown type name '__libcpp_condvar_t'
   26 |   __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
      |   ^
1 error generated.
Target //test/c:test_libc_wasip1_wasm32 failed to build
INFO: Elapsed time: 11.475s, Critical Path: 7.98s
INFO: 2 processes: 2 internal.

More similar errors around pthreads when using 0.14 nightlies (e.g. 0.14.0-dev.2271+f845fa04a) such as:

external/_main~toolchains~zig_sdk/lib/libcxx/include/__thread/support/pthread.h:18:10: error: 'pthread.h' file not found with <angled> include; use "quotes" instead
   18 | #include <pthread.h>
      |          ^
external/_main~toolchains~zig_sdk/lib/libcxx/include/__thread/support/pthread.h:47:9: error: unknown type name 'pthread_mutex_t'
   47 | typedef pthread_mutex_t __libcpp_mutex_t;
      |         ^

Maybe related to ziglang/zig#10989 (comment) ?

@ah-quant
Copy link

ah-quant commented Mar 13, 2025

I just want to drop by to say thank you, @chrisirhc! I adapted your existing PR and got a lot further in using Zig as a C/C++ toolchain on Windows (and only Windows - where getting a C/C++ environment set up is just ...blergh).

Please find my patch below - using it, I could continue until I hit ziglang/zig#23233 :-)
Disclaimer: not really a patch. A copy/paste/adapt from what github spilled out based on this PR. It worked for me in MODULE.bazel -> single_version_override but it's probably broken. Still sufficient to see the changes.

I needed 0.14.0 because pthreads.h was added to msys2 after 0.13.0 and I could not get through abseil without it.

If you want to use it, please do - I disclaim any copyright, this is just a minor change to what you did.

===================================================================
diff --git a/toolchain/private/zig_sdk.bzl b/toolchain/private/zig_sdk.bzl
index a328df5..c1b3cf5 100644
--- a/toolchain/private/zig_sdk.bzl
+++ b/toolchain/private/zig_sdk.bzl
@@ -1,12 +1,12 @@
-VERSION = "0.12.0"
+VERSION = "0.14.0"

 HOST_PLATFORM_SHA256 = {
-    "linux-aarch64": "754f1029484079b7e0ca3b913a0a2f2a6afd5a28990cb224fe8845e72f09de63",
-    "linux-x86_64": "c7ae866b8a76a568e2d5cfd31fe89cdb629bdd161fdd5018b29a4a0a17045cad",
-    "macos-aarch64": "294e224c14fd0822cfb15a35cf39aa14bd9967867999bf8bdfe3db7ddec2a27f",
-    "macos-x86_64": "4d411bf413e7667821324da248e8589278180dbc197f4f282b7dbb599a689311",
-    "windows-aarch64": "04c6b92689241ca7a8a59b5f12d2ca2820c09d5043c3c4808b7e93e41c7bf97b",
-    "windows-x86_64": "2199eb4c2000ddb1fba85ba78f1fcf9c1fb8b3e57658f6a627a8e513131893f5",
+    "windows-x86_64": "f53e5f9011ba20bbc3e0e6d0a9441b31eb227a97bac0e7d24172f1b8b27b4371",
 }

 # Official recommended version. Should use this when we have a usable release.
diff --git a/toolchain/zig-wrapper.zig b/toolchain/zig-wrapper.zig
index d1d59f9..99be373 100644
--- a/toolchain/zig-wrapper.zig
+++ b/toolchain/zig-wrapper.zig
@@ -93,7 +93,7 @@ const ParseResults = union(Action) {
 };

 // sub-commands in the same folder as `zig-wrapper`
-const sub_commands_target = std.ComptimeStringMap(void, .{
+const sub_commands_target = std.StaticStringMap(void).initComptime(.{
     .{"ar"},
     .{"ld.lld"},
     .{"lld-link"},


diff --git a/toolchain/zig-wrapper.zig b/toolchain/zig-wrapper.zig
index 99be373..70ee41e 100644
--- a/toolchain/zig-wrapper.zig
+++ b/toolchain/zig-wrapper.zig
@@ -55,7 +55,7 @@ const std = @import("std");
 const fs = std.fs;
 const mem = std.mem;
 const process = std.process;
-const ChildProcess = std.ChildProcess;
+const ChildProcess = std.process.Child;
 const ArrayListUnmanaged = std.ArrayListUnmanaged;
 const sep = fs.path.sep_str;


diff --git a/toolchain/zig-wrapper.zig b/toolchain/zig-wrapper.zig
index 99be373..70ee41e 100644
--- a/toolchain/zig-wrapper.zig
+++ b/toolchain/zig-wrapper.zig
@@ -279,7 +279,7 @@ const std = @import("std");
     // strings `is.it.x86_64?-stallinux,macos-`; we are trying to aid users
     // that run things from the wrong directory, not trying to punish the ones
     // having fun.
-    var it = mem.split(u8, triple, "-");
+    var it = mem.splitSequence(u8, triple, "-");

     const arch = it.next() orelse return error.BadParent;
     if (mem.indexOf(u8, "aarch64,x86_64,wasm32", arch) == null)

@chrisirhc
Copy link
Author

chrisirhc commented Mar 13, 2025

@ah-quant Thanks for sharing your patch!

Maybe time to test this again now that zig 0.14 has some partial fixes to ziglang/zig#21721

@chrisirhc chrisirhc force-pushed the chua/11-21-upgrade-zig branch from b4ab950 to 6fd5738 Compare March 16, 2025 12:12
@chrisirhc chrisirhc changed the title upgrade to zig 0.13.0 upgrade to zig 0.14.0 Mar 16, 2025
Comment on lines 214 to 225
"libc/include/generic-musl",
"libc/wasi",
] + _INCLUDE_TAIL,
linkopts = [],
dynamic_library_linkopts = [],
supports_dynamic_linker = False,
copts = [],
copts = [
"-D_LIBCPP_HAS_MUSL_LIBC",
"-D_LIBCPP_HAS_NO_THREADS",
"-D_LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS",
"-D_LIBCPP_HAS_NO_THREAD_API_PTHREAD",
],
Copy link
Author

@chrisirhc chrisirhc Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempt to resolve issue:

external/_main~toolchains~zig_config/lib/libc/include/wasm-wasi-musl/stdio.h:8:10: fatal error: 'features.h' file not found

Output from claude 3.5:

I've made the following changes to fix the WASI target configuration:
Added additional include paths:
libc/include/generic-musl - For generic musl libc headers
libc/include/any-wasi-any - For WASI-specific headers
Added compiler flags:
-D_LIBCPP_HAS_MUSL_LIBC - To indicate we're using musl libc
-D_LIBCPP_HAS_NO_THREADS - WASI doesn't support threading
-D_LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS - Disable thread-unsafe functions
-D_LIBCPP_HAS_NO_THREAD_API_PTHREAD - Disable pthread API since WASI doesn't support it
These changes should:
Ensure the compiler can find the necessary libc headers
Configure libc++ to work in a single-threaded WASI environment
Prevent pthread-related errors since WASI doesn't support threading
Try running the test again with these changes. The compiler should now be able to find the necessary headers and properly handle the WASI target configuration.

I removed libc/include/any-wasi-any since that doesn't exist, but the other changes seem to fix the test.

Copy link
Author

@chrisirhc chrisirhc Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing all the additional copts led to the tests passing, so I removed them but kept the additional generic-musl includes.

Comment on lines 164 to 168
copts = [
"-D_GNU_SOURCE",
"-D_POSIX_C_SOURCE=200809L",
"-D_XOPEN_SOURCE=600",
],
Copy link
Author

@chrisirhc chrisirhc Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempt to resolve issue:

bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:24:17: could not determine kind of name for C.grantpt
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:20:12: could not determine kind of name for C.posix_openpt
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:32:26: could not determine kind of name for C.ptsname
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:28:17: could not determine kind of name for C.unlockpt

Output from Claude 3.5:

I've added three important feature test macros to the Linux GNU target configuration:
-D_GNU_SOURCE - Enables all GNU extensions
-D_POSIX_C_SOURCE=200809L - Enables POSIX.1-2008 features
-D_XOPEN_SOURCE=600 - Enables X/Open System Interface (XSI) features
These macros should make the necessary POSIX terminal functions (grantpt, posix_openpt, ptsname, unlockpt) available to CGo when building the Go standard library.
The error occurred because these functions are part of the POSIX terminal interface, and they need to be explicitly enabled through feature test macros when building with certain C library implementations.

It does seem to fix the issue above, but introduces new warnings:

INFO: From GoStdlib external/rules_go~0.45.1/stdlib_/pkg:
# internal/testpty
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:10:9: warning: '_XOPEN_SOURCE' macro redefined [-Wmacro-redefined]
   10 | #define _XOPEN_SOURCE 600
      |         ^
./external/_main~toolchains~zig_sdk-macos-amd64/tools/x86_64-linux-gnu.2.28/../../lib/libc/include/generic-glibc/features.h:236:10: note: previous definition is here
  236 | # define _XOPEN_SOURCE  700
      |          ^
1 warning generated.
# net
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/net/cgo_unix_cgo.go:10:9: warning: '_GNU_SOURCE' macro redefined [-Wmacro-redefined]
   10 | #define _GNU_SOURCE
      |         ^
<command line>:13:9: note: previous definition is here
   13 | #define _GNU_SOURCE 1
      |         ^
1 warning generated.
INFO: From GoStdlib external/rules_go~0.45.1/stdlib_/pkg:
# internal/testpty
bazel-out/darwin_x86_64-fastbuild-ST-1a3e3e752a28/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:10:9: warning: '_XOPEN_SOURCE' macro redefined [-Wmacro-redefined]
   10 | #define _XOPEN_SOURCE 600
      |         ^
./external/_main~toolchains~zig_sdk-macos-amd64/tools/x86_64-linux-gnu.2.28/../../lib/libc/include/generic-glibc/features.h:236:10: note: previous definition is here
  236 | # define _XOPEN_SOURCE  700
      |          ^
1 warning generated.
# net
bazel-out/darwin_x86_64-fastbuild-ST-1a3e3e752a28/bin/external/rules_go~0.45.1/stdlib_/src/net/cgo_unix_cgo.go:10:9: warning: '_GNU_SOURCE' macro redefined [-Wmacro-redefined]
   10 | #define _GNU_SOURCE
      |         ^
<command line>:13:9: note: previous definition is here
   13 | #define _GNU_SOURCE 1
      |         ^
1 warning generated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all except for -D_GNU_SOURCE led to the tests passing, so I removed them.

@chrisirhc
Copy link
Author

Failure is due to missing ziglang mirror, see bazelbuild/bazel#25575 . Will retry after that has landed.

@chrisirhc chrisirhc force-pushed the chua/11-21-upgrade-zig branch from fe9b182 to 5681445 Compare March 17, 2025 03:56
chrisirhc and others added 10 commits March 17, 2025 04:12
>  The error is coming from Go's CGo trying to build the standard library with the Zig C++ compiler, and it's failing to find certain POSIX terminal functions (grantpt, posix_openpt, ptsname, unlockpt).

```
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:24:17: could not determine kind of name for C.grantpt
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:20:12: could not determine kind of name for C.posix_openpt
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:32:26: could not determine kind of name for C.ptsname
bazel-out/darwin_x86_64-fastbuild-ST-1dd79e0b6a44/bin/external/rules_go~0.45.1/stdlib_/src/internal/testpty/pty_cgo.go:28:17: could not determine kind of name for C.unlockpt
```
-D_GNU_SOURCE - Enables all GNU extensions
-D_POSIX_C_SOURCE=200809L - Enables POSIX.1-2008 features
-D_XOPEN_SOURCE=600 - Enables X/Open System Interface (XSI) features

These macros should make the necessary POSIX terminal functions (grantpt, posix_openpt, ptsname, unlockpt) available to CGo when building the Go standard library.
The error occurred because these functions are part of the POSIX terminal interface, and they need to be explicitly enabled through feature test macros when building with certain C library implementations.
@chrisirhc chrisirhc force-pushed the chua/11-21-upgrade-zig branch from 5681445 to 81dfbf9 Compare March 17, 2025 04:15
@chrisirhc chrisirhc marked this pull request as ready for review March 17, 2025 04:31
Comment on lines +145 to +147
"error spawning {s}: {s}\n",
.{ params.args.items[0], @errorName(err) },
);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From new version of zig's zig fmt.

@chrisirhc
Copy link
Author

chrisirhc commented Mar 18, 2025

Do not land. Blocked on ziglang/zig#23287 based on internal testing.

@code-weirdo
Copy link

I ran into this with trying to set up something hermetic for Windows just to get rules_java working, and VS just isn't an option for us.
Applying @ah-quant's patch got me to the same place where abseil-cpp won't compile because of the missing winrt windows.*.h headers that were removed in zig 0.14. Seems, though, that some fixes have landed in later versions of abseil-cpp and protobuf than the ones that rules_java use. Just updating those to something more recent got then to compile successfully. Maybe this helps someone...

# Windows, Windows, Windows...
# Patch hermetic_cc_toolchain to use zig 0.14.0
git_override(
  module_name = "hermetic_cc_toolchain",
  remote = "https://github.com/uber/hermetic_cc_toolchain.git",
  commit = "9b854287380e630372234ad63d9cf092702dd68d", # 4.0.0
  patches = ["//internal/patches:hermetic_cc_toolchain/zig-sdk.patch"],
  patch_args = ["-p1"],
)
# A fix for windows builds landed in 31.0
bazel_dep(name = "protobuf", version = "31.0", repo_name = "com_google_protobuf")
# The older version of abseil-cpp is incompatilbe with the more recent version of protobuf
bazel_dep(name = "abseil-cpp", version = "20250512.1", repo_name = "com_google_absl")

@skeggse
Copy link

skeggse commented Oct 23, 2025

I'm running into some integration issues between Zig 0.14 and rules_go when I try to pull in this patch. I'm suspecting it's due to Zig 0.14 enabling UBSAN by default, which is causing linker errors like

net(.text): unknown symbol __ubsan_handle_type_mismatch_v1 in callarm64
net(.text): unknown symbol __ubsan_handle_pointer_overflow in callarm64

on arm64.

I wonder a bit if the afore-linked ziglang/zig#21721 will eventually catch this, or if it's something specific to my setup.

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.

4 participants