-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add build logic for zig cc
#19535
Comments
If For the benefits, the biggest concern is the C++ standard library (libc++ and libstdc++), it seems |
Yeah, I'm wondering if we can use Bazel's automatic toolchain discoevry with the right env vars to make this work. Then it's just a question of whether we want a CI job, which would be a lower bar. In general, I think we would need some owners and contribution to help make this a reality if there is non-trivial work. |
If I understand correctly you are concerned with how to provide libc++. In case it is not clear, zig provides libc++ in addition to libc. It will be compiled lazily from source according to the target and command line flags and then cached for reuse. If you decide to go this route in envoy and run into trouble, don't hesitate to reach out! I'd be happy to help. |
Played around with this a bit. Command:
Got up to this point:
Fails with:
If I remove
I assume the first issue is bazel not recognizing zig's libc+ somehow and the latter is not using libc++ at all...? |
I also played around with https://sr.ht/~motiejus/bazel-zig-cc/ in https://github.com/envoyproxy/envoy/compare/main...howardjohn:zig2?expand=1, but struggled to actually get the toolchain used. It was registered but zig never actually executed. |
You seem to be missing diff --git a/.bazelrc b/.bazelrc
index 89110fc7cd..d2f6940c3a 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -15,6 +15,7 @@ run --color=yes
build --color=yes
build --workspace_status_command="bash bazel/get_workspace_status"
build --incompatible_strict_action_env
+build --incompatible_enable_cc_toolchain_resolution
build --host_force_python=PY3
build --host_javabase=@bazel_tools//tools/jdk:remote_jdk11
build --javabase=@bazel_tools//tools/jdk:remote_jdk11 I added that, and it seemed to be doing something (not an expert in envoy, not sure if the target is the right way to go at all): I've added a note about it to the top-level README. Thanks for trying. |
Nice, thanks @motiejus . That got me to a complete build. Initial I had a full build and had issues on v8 failing. I noticed while The binary is failing with |
Illegal instruction is most likely UBSAN: You may confirm the hypothesis by adding |
Here's an issue that would ease discovery of the fact that "illegal hardware instruction" is in fact UBSAN tripping: ziglang/zig#5163 May I see the full list of linker warnings? |
Awesome, we have a running binary now! Currently failing on envoy/source/common/signal/signal_action.cc Line 109 in 450631b
|
Thanks, I'll look into these linker warnings. |
We do run all tests with Clang UBSAN so I think it is unlikely unless zig enables something extra. |
Not sure if the same but others finding UBSAN error's when using |
Thank you @andrewrk for ziglang/zig@50905d8. Once the CI passes, I will cut a new bazel-zig-cc with this new version. |
No problem. I was planning on solving those other two as well - will make a note here when those land. |
May I see the contents of |
The usage of ^ can be disabled in the envoy build with: diff --git a/source/exe/BUILD b/source/exe/BUILD
index 0eb4078c16..905d1f9afc 100644
--- a/source/exe/BUILD
+++ b/source/exe/BUILD
@@ -26,7 +26,7 @@ envoy_cc_binary(
"//bazel:windows_opt_build": ["generate_pdb_file"],
"//conditions:default": [],
}),
- stamped = True,
+ stamped = False,
deps = [":envoy_main_entry_lib"],
) already though |
Thanks. The issue to track for that one is ziglang/zig#3047, however it's not a high priority at the moment. Do I understand correctly that this use case is unblocked? |
I haven't had a chance to try with ziglang/zig@40c9ce2 yet. I did, however, disable the --hash-style flag from being set in the Envoy build config. This makes the build succeed without any errors. However, running it immediately fails on envoy/source/common/signal/signal_action.cc Line 109 in 450631b
If I remove that assertion,
If I remove that one as well, I can run I also had to disable
I am guessing this is somehow implicitly included in the clang bazel logic |
Neither |
Certainly - was mostly removing them temporarily to see if it would unblock the rest of the process.
|
this address is not properly aligned: from the mmap call with Not sure why that would happen but that's clearly the wrong value for altstack_size_ to have. |
When Bazel is configured to use a C/C++ toolchain, it defaults to a "more hermetic" build (for the lack of better word, or lack of my understanding how it actually works). I.e. it will refuse to include anything that's not explicitly allowlisted, and As a consequence of a hermetic build, you will be able to cross-compile envoy on anything that bazel and zig run. I.e. compile linux_arm64 envoy binaries on osx or bsd or similar. |
bazel-zig-cc 0.4.2 is now released, which includes this change. |
Thanks for the pointer @andrewrk ! It turns out
evaluates to
For now, I have just replaced it with
|
There are a few remaining issues when cross-compiling, mostly around Bazel rules.
|
Perhaps this can be provided as a linker flag instead?
From a quick grep I can see we (my company) are cross-compiling at least libjpeg-turbo, libpng, libwebp and zlib with |
The issue is not "how to strip the binary", but "how to prevent LuaJIT's Makefile from running
There can be a many reason why they work, e.g.:
For most projects, it doesn't matter, since if you're using the same sources for all platforms, then Having said that, I know that some of those libraries (definitely zlib) use CPU-specific files for optimizations, so it would be worth verifying that the correct files are picked up when cross-compiling. |
Do you mean I should if it works at all? Can you elaborate? I'd need to setup a main.c and link it to |
No, run |
OK, I have V8 working and executing Proxy-Wasm plugins, but it might take a while to fix its Bazel rules. |
i can't get it to print more verbose logs than this:
|
@motiejus CMake logs should be in However, it looks that vanilla zlib doesn't automatically detect platform anyway, and you need to manually select the build option to use the assembly optimizations (see: madler/zlib's CMakeLists.txt#L9). |
...but yeah, I verified that
Filed on issue with upstream: bazel-contrib/rules_foreign_cc#869. |
Fixes envoyproxy#19535. Signed-off-by: Piotr Sikora <piotrsikora@google.com>
I opened #19885, which adds The binaries work fine, but there are a few test failures that might be worth investigating (and some more when cross-compiling to Linux/aarch64). The biggest holdbacks are the conflicts between
which prevent building Proxy-Wasm plugins in tests. |
Thank you @howardjohn, that buys a lot of coffee. A nice surprise. |
See envoyproxy#19535 (comment) This allows using othe compiler toolchains that are not literally in /usr/include but are still present. I don't think this should impact standard clang builds at all. Signed-off-by: John Howard <howardjohn@google.com> Signed-off-by: Josh Perry <josh.perry@mx.com>
This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions. |
Hi all, looks like there's been some great steps on the zig front on this issue and we've been having a look at zig to see if it can build Envoy for CentOS 7. Caveat we're still learning about C++ build chains and the tooling around it so we're beginners in this space. We've installed zig on the latest Envoy ubuntu build container 18.04 and installed @PiotrSikora pull request #19885. The only configuration we've changed is to remove the aarch toolchain We've also disabled the tcp_stats extension to remove the header compatibility issues on CentOS 7 (https://github.com/envoyproxy/envoy-build-tools/blob/main/build_container/CENTOS7_BUILD_STATUS.md). We are aware that there is a third party ELRepo kernel version that could work https://centos.pkgs.org/7/elrepo-kernel-x86_64/kernel-ml-headers-5.16.15-1.el7.elrepo.x86_64.rpm.html, but most CentOS 7 installs we have do not use it. Running
1 warning generated.
ERROR: /envoy/source/exe/BUILD:23:16: Linking source/exe/envoy-static failed: (Exit 1): c++ failed: error executing command /root/.cache/bazel/_bazel_root/37e3aec351bcd85a6ea8b58e3592ef6e/external/zig_sdk/tools/c++ -o bazel-out/k8-opt/bin/source/exe/envoy-static ... (remaining 3991 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox
warning: unsupported linker arg: @bazel-out/k8-opt/bin/bazel/gnu_build_id.ldscript
ld.lld: error: undefined symbol: __cxa_thread_atexit_impl
>>> referenced by cxa_thread_atexit.cpp
>>> /tmp/bazel-zig-cc/o/a3f0d4f3c69cdfff9abe6bb2e87b4c32/cxa_thread_atexit.o:(__cxa_thread_atexit) in archive /tmp/bazel-zig-cc/o/4692f8c4f45428a48864c01ec35e7820/libc++abi.a
Target //source/exe:envoy-static failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 2643.189s, Critical Path: 117.61s
INFO: 10716 processes: 2124 internal, 1 local, 8590 processwrapper-sandbox, 1 worker.
FAILED: Build did NOT complete successfully
root@917c7a6f20bf:/envoy# Is this a problem linking against a later version of glibc (https://groups.google.com/g/envoy-users/c/XK9M-RhWrxg/m/XEGMU6hVBwAJ) or something else as cxa_thread_atexit_impl.c is not in glibc 2.17 https://elixir.bootlin.com/glibc/glibc-2.17/source/stdlib/cxa_thread_atexit_impl.c and only glibc 2.18 onwards? Is there an easy way to explain what the problem is and anything we can configure on a zig or bazel front to make it compile for |
The symbol doesn't seem to exist in glibc 2.17:
May I ask why you are linking against glibc that is almost 10 years old? Edit: looks like this is what CentOS uses. Unfortunately, I don't have a better answer than "upgrade your OS" or "build statically instead". |
Also, keep in mind that the glibc headers in zig are always latest (of glibc), but the symbol definitions are pointing to the right glibc version. So if you are compiling for
However, linking phase will fail, like it does now. The fact that the function prototype is included, but linking fails, is a known discrepancy (similar issue in ziglang/zig#9485). If envoy can compile on glibc 2.17 at all, maybe it detects that the symbol does not exist, and uses a different one instead. So inconsistent headers may or may not be a problem and a solution. :) cc @marler8997 who has done some work on tackling this. |
Thanks @motiejus for continuing to look into this. Yes glibc 2.17 is the CentOS 7 default and why we're trying to compile using it until the maintenance window expires (probably a bit before). Atm we can get Envoy to build using glibc 2.17 just on CentOS 7 disabling the tcp_stats extension (see comment above). We use the Envoy CentOS 7 build container script located here (https://github.com/envoyproxy/envoy-build-tools/tree/main/build_container/Dockerfile-centos) and per your note above we presume that's the reason why it works currently. |
This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions. |
This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions. |
Stumbled upon this by accident. Quick update: @andrewrk started working on universal headers for zig, which, when implemented, will this issue. |
Title: Add build logic for
zig cc
Description:
https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html can act as a drop in replacement for clang. This offers a few benefits over Envoy's current build:
There is some third-party bazel integration: https://git.sr.ht/~motiejus/bazel-zig-cc
Overall as a project, this seems like this may be problematic - maintaining multiple build systems can cause a lot of burden and I assume we would not fully migrate everything to zig. However, I thought I would float the idea out.
I did experiment with this a few months back and was able to get a binary compiled. However, it crashed immediately for unclear reasons. I am not an expert on C++, zig, or bazel though so likely someone else could get much further
The text was updated successfully, but these errors were encountered: