-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Proposal: standard wrappers for zig cc
and zig c++
#8716
Comments
Yes. This is an issue for Such wrappers are one-liners, though ( |
This would have helped quite a bit while trying to use zig within build scripts with guix. Instead of just
The wrappers might be easy to write but within build scripts you'd either repackage zig to export them or add them to every script it's needed in which quickly becomes error prone. |
@andrewrk might correct me here but I believe the idea is to ultimately drop in a |
Such wrappers remain useful to compile unmodified, existing code. The ability to effortlessly cross-compile existing C/C++ code is a strong selling point. Now, should these wrappers be part of the Zig distribution? Maybe not. They can reside in a dedicated repository, that the documentation can point to. |
Such wrappers are convenient, however they are a detail of the "glue" code between a Zig distribution and the system it is run on. Such wrappers will need system-specific code. Out of scope for this repository. |
Do you mind sharing your solution using the "some other undocumented cmake flags"?? I am also finding it very difficult using |
Not easy, but not so difficult. look here: https://github.com/zhaozg/luajit-cmake/blob/master/Utils/zig.toolchain.cmake |
FWIW I write a handy script that can work:
#!/usr/bin/env bash
zig cc $@
#!/usr/bin/env bash
zig c++ $@ For Kvrocks project, I run: git clone https://github.com/apache/incubator-kvrocks
./x.py build --ninja -DCMAKE_C_COMPILER="$(pwd)/zig-cc" -DCMAKE_CXX_COMPILER="$(pwd)/zig-cxx" Although some macOS specific issues occur, the original proposal here is resolved. Full output for who is interested in:$ /opt/homebrew/bin/cmake /Users/chenzili/Brittani/kvrocks -DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja -DCMAKE_CXX_COMPILER=/Users/chenzili/Brittani/kvrocks/zig-cxx -- The CXX compiler identification is Clang 15.0.3 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /Users/chenzili/Brittani/kvrocks/zig-cxx - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- The C compiler identification is AppleClang 14.0.0.14000029 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Found Python: /opt/homebrew/Frameworks/Python.framework/Versions/3.11/bin/python3.11 (found version "3.11.0") found components: Interpreter -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success -- Found Threads: TRUE -- Could NOT find Unwind (missing: Unwind_LIBRARY) -- Looking for C++ include unwind.h -- Looking for C++ include unwind.h - found -- Looking for _Unwind_Backtrace -- Looking for _Unwind_Backtrace - found -- Looking for C++ include dlfcn.h -- Looking for C++ include dlfcn.h - found -- Looking for C++ include execinfo.h -- Looking for C++ include execinfo.h - found -- Looking for C++ include glob.h -- Looking for C++ include glob.h - found -- Looking for C++ include inttypes.h -- Looking for C++ include inttypes.h - found -- Looking for C++ include memory.h -- Looking for C++ include memory.h - found -- Looking for C++ include pwd.h -- Looking for C++ include pwd.h - found -- Looking for C++ include stdint.h -- Looking for C++ include stdint.h - found -- Looking for C++ include strings.h -- Looking for C++ include strings.h - found -- Looking for C++ include sys/stat.h -- Looking for C++ include sys/stat.h - found -- Looking for C++ include sys/syscall.h -- Looking for C++ include sys/syscall.h - found -- Looking for C++ include sys/time.h -- Looking for C++ include sys/time.h - found -- Looking for C++ include sys/types.h -- Looking for C++ include sys/types.h - found -- Looking for C++ include sys/utsname.h -- Looking for C++ include sys/utsname.h - found -- Looking for C++ include sys/wait.h -- Looking for C++ include sys/wait.h - found -- Looking for C++ include syscall.h -- Looking for C++ include syscall.h - not found -- Looking for C++ include syslog.h -- Looking for C++ include syslog.h - found -- Looking for C++ include ucontext.h -- Looking for C++ include ucontext.h - not found -- Looking for C++ include unistd.h -- Looking for C++ include unistd.h - found -- Looking for C++ include ext/hash_map -- Looking for C++ include ext/hash_map - found -- Looking for C++ include ext/hash_set -- Looking for C++ include ext/hash_set - found -- Looking for C++ include ext/slist -- Looking for C++ include ext/slist - not found -- Looking for C++ include tr1/unordered_map -- Looking for C++ include tr1/unordered_map - not found -- Looking for C++ include tr1/unordered_set -- Looking for C++ include tr1/unordered_set - not found -- Looking for C++ include unordered_map -- Looking for C++ include unordered_map - found -- Looking for C++ include unordered_set -- Looking for C++ include unordered_set - found -- Looking for C++ include stddef.h -- Looking for C++ include stddef.h - found -- Check size of unsigned __int16 -- Check size of unsigned __int16 - failed -- Check size of u_int16_t -- Check size of u_int16_t - done -- Check size of uint16_t -- Check size of uint16_t - done -- Looking for dladdr -- Looking for dladdr - found -- Looking for fcntl -- Looking for fcntl - found -- Looking for pread -- Looking for pread - found -- Looking for pwrite -- Looking for pwrite - found -- Looking for sigaction -- Looking for sigaction - found -- Looking for sigaltstack -- Looking for sigaltstack - found -- Performing Test HAVE_NO_DEPRECATED -- Performing Test HAVE_NO_DEPRECATED - Success -- Performing Test HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS -- Performing Test HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS - Success -- Looking for pthread_threadid_np -- Looking for pthread_threadid_np - found -- Looking for snprintf -- Looking for snprintf - found -- Looking for UnDecorateSymbolName in dbghelp -- Looking for UnDecorateSymbolName in dbghelp - not found -- Performing Test HAVE___ATTRIBUTE__ -- Performing Test HAVE___ATTRIBUTE__ - Success -- Performing Test HAVE___ATTRIBUTE__VISIBILITY_DEFAULT -- Performing Test HAVE___ATTRIBUTE__VISIBILITY_DEFAULT - Success -- Performing Test HAVE___ATTRIBUTE__VISIBILITY_HIDDEN -- Performing Test HAVE___ATTRIBUTE__VISIBILITY_HIDDEN - Success -- Performing Test HAVE___BUILTIN_EXPECT -- Performing Test HAVE___BUILTIN_EXPECT - Success -- Performing Test HAVE___SYNC_VAL_COMPARE_AND_SWAP -- Performing Test HAVE___SYNC_VAL_COMPARE_AND_SWAP - Success -- Performing Test HAVE_RWLOCK -- Performing Test HAVE_RWLOCK - Success -- Performing Test HAVE___DECLSPEC -- Performing Test HAVE___DECLSPEC - Failed -- Performing Test STL_NO_NAMESPACE -- Performing Test STL_NO_NAMESPACE - Failed -- Performing Test STL_STD_NAMESPACE -- Performing Test STL_STD_NAMESPACE - Success -- Performing Test HAVE_USING_OPERATOR -- Performing Test HAVE_USING_OPERATOR - Success -- Performing Test HAVE_NAMESPACES -- Performing Test HAVE_NAMESPACES - Success -- Performing Test HAVE_GCC_TLS -- Performing Test HAVE_GCC_TLS - Success -- Performing Test HAVE_MSVC_TLS -- Performing Test HAVE_MSVC_TLS - Failed -- Performing Test HAVE_CXX11_TLS -- Performing Test HAVE_CXX11_TLS - Success -- Performing Test HAVE_ALIGNED_STORAGE -- Performing Test HAVE_ALIGNED_STORAGE - Success -- Performing Test HAVE_CXX11_ATOMIC -- Performing Test HAVE_CXX11_ATOMIC - Success -- Performing Test HAVE_CXX11_CONSTEXPR -- Performing Test HAVE_CXX11_CONSTEXPR - Success -- Performing Test HAVE_CXX11_CHRONO -- Performing Test HAVE_CXX11_CHRONO - Success -- Performing Test HAVE_CXX11_NULLPTR_T -- Performing Test HAVE_CXX11_NULLPTR_T - Success -- Performing Test HAVE_LOCALTIME_R -- Performing Test HAVE_LOCALTIME_R - Success -- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY -- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success -- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY -- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success -- Performing Test COMPILER_HAS_DEPRECATED_ATTR -- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success -- Looking for byteswap.h -- Looking for byteswap.h - not found -- Looking for sys/endian.h -- Looking for sys/endian.h - not found -- Looking for sys/mman.h -- Looking for sys/mman.h - found -- Looking for sys/resource.h -- Looking for sys/resource.h - found -- Looking for sys/uio.h -- Looking for sys/uio.h - found -- Looking for windows.h -- Looking for windows.h - not found -- Looking for zlibVersion in z -- Looking for zlibVersion in z - found -- Looking for lzo1x_1_15_compress in lzo2 -- Looking for lzo1x_1_15_compress in lzo2 - not found -- Performing Test HAVE_BUILTIN_EXPECT -- Performing Test HAVE_BUILTIN_EXPECT - Success -- Performing Test HAVE_BUILTIN_CTZ -- Performing Test HAVE_BUILTIN_CTZ - Success -- Looking for mmap -- Looking for mmap - found -- Looking for sysconf -- Looking for sysconf - found CMake Deprecation Warning at build/_deps/zlib-src/CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake. -- Check size of struct addrinfo -- Check size of struct addrinfo - done -- Check size of struct in6_addr -- Check size of struct in6_addr - done -- Performing Test EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16 -- Performing Test EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16 - Failed -- Performing Test EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32 -- Performing Test EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32 - Failed -- Check size of sa_family_t -- Check size of sa_family_t - done -- Check size of struct sockaddr_in6 -- Check size of struct sockaddr_in6 - done -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN - Success -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN - Failed -- Check size of struct sockaddr_storage -- Check size of struct sockaddr_storage - done -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY - Success -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY -- Performing Test EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY - Failed -- Check size of struct linger -- Check size of struct linger - done -- -- ---( Libevent 2.1.12-stable )--- -- -- Available event backends: -- CMAKE_BINARY_DIR: /Users/chenzili/Brittani/kvrocks/build -- CMAKE_CURRENT_BINARY_DIR: /Users/chenzili/Brittani/kvrocks/build/_deps/libevent-build -- CMAKE_SOURCE_DIR: /Users/chenzili/Brittani/kvrocks -- CMAKE_CURRENT_SOURCE_DIR: /Users/chenzili/Brittani/kvrocks/build/_deps/libevent-src -- PROJECT_BINARY_DIR: /Users/chenzili/Brittani/kvrocks/build/_deps/libevent-build -- PROJECT_SOURCE_DIR: /Users/chenzili/Brittani/kvrocks/build/_deps/libevent-src -- CMAKE_MODULE_PATH: /Users/chenzili/Brittani/kvrocks/build/_deps/libevent-src/cmake/ -- CMAKE_COMMAND: /opt/homebrew/Cellar/cmake/3.25.1/bin/cmake -- CMAKE_ROOT: /opt/homebrew/Cellar/cmake/3.25.1/share/cmake -- CMAKE_SYSTEM: Darwin-21.6.0 -- CMAKE_SYSTEM_NAME: Darwin -- CMAKE_SYSTEM_VERSION: 21.6.0 -- CMAKE_SYSTEM_PROCESSOR: arm64 -- CMAKE_SKIP_RPATH: NO -- CMAKE_VERBOSE_MAKEFILE: FALSE -- CMAKE_C_FLAGS: -fdiagnostics-color=always -Wall -Wextra -Wno-unused-parameter -Wstrict-aliasing -Wstrict-prototypes -fno-strict-aliasing -Wmissing-prototypes -Winit-self -Wmissing-field-initializers -Wdeclaration-after-statement -Waddress -Woverride-init -Wwrite-strings -Wno-unused-function -Wno-error=deprecated-declarations -Qunused-arguments -- CMAKE_BUILD_TYPE: RelWithDebInfo -- CMAKE_C_COMPILER: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc (id AppleClang, clang 1, GNUC 1) -- CMAKE_AR: /usr/bin/ar -- CMAKE_RANLIB: /usr/bin/ranlib -- -- Module support is disabled. -- Version: 9.1.0 -- Build type: RelWithDebInfo -- CXX_STANDARD: 17 -- Performing Test has_std_17_flag -- Performing Test has_std_17_flag - Success -- Performing Test has_std_1z_flag -- Performing Test has_std_1z_flag - Success -- Required features: cxx_variadic_templates -- Performing Test HAS_NULLPTR_WARNING -- Performing Test HAS_NULLPTR_WARNING - Success CMake Warning at CMakeLists.txt:202 (message): IPO is not supported: Change Dir: /Users/chenzili/Brittani/kvrocks/build/CMakeFiles/_CMakeLTOTest-CXX/bin |
Since I just ran into this: you can set |
Won't work on macOS. Fortunately @tisonkun answer above does. |
The answer by @tisonkun breaks when an argument has a space in it. For example, when you build clang, you have a define like if(CLANG_VENDOR)
set_source_files_properties(Version.cpp
PROPERTIES COMPILE_DEFINITIONS "CLANG_VENDOR=\"${CLANG_VENDOR} \"")
endif() This gets broken due to some problem with passing the args:
|
If you add quotes around the
|
Notes from further experimentation: |
Hi @andrewrk (and others), I know the reasons to maintain the zig binary clean. And I know (and I use it) that with CMake we can use (i.e.): But, today I found this error:
So the problem is this: sometimes the Therefore to solve this problem I suggest to support the most straightforward solution: support links like Please, comment. |
Under what circumstances does this happen? |
Running CMake to try to compile Canonical Multipass with ZIG CC. |
This is exactly the issue wrappers would solve. |
Hi to all, I'm experimenting with something like this:
With this if you call to |
Hi @andrewrk , Please, could you check this patch?
If you agree with this approach, then I'll prepare a PR. NOTE: This patch doesn't work! |
Are you sure that wasn't the sort of auto-detection that I mentioned you can get around with |
Hi @widlarizer ,
I'll need to check it. However, I feel it will wont work because this VAR is not created/used for this pourpose.
Perhaps. But I want to know the opinion from @andrewrk before writing a complete patch (this one is only a functional example). |
I believe that is what draft PRs are exactly for |
Hi to all! I've created a draft patch that solves the problem from the root: #22211 The solution is quite simple: if you create a symlink (or rename) the binary with the name "zig-cc" then calling to it will be the same as "zig cc". Therefore any other paramters will be parsed after the "cc". So that's a definitive solution. The PR is a draft because I need the help from @andrewrk to clean the code, add the resting cases (now only for "cc" and "c++"), and add support for Windows (perhaps adding "zig-cc.exe" checks). What you think? |
Hi @andrewrk , Please can you explain why it is rejected? |
There are multiple existing tools which require a direct filename or filepath for C/C++ compiler(s), which causes issues when trying to use Zig's executable+argument (
zig
+cc
) sub-command. People currently work around this by creating bash / batch wrappers which forward the arguments tozig cc
orzig c++
.I personally ran into this issue today while trying to use cmake's
-DCMAKE_C_COMPILER
and-DCMAKE_CXX_COMPILER
flags, as it doesn't support spaces/multi-part commands. Luckily I was able to work around the issue without changes to cmake itself by using some other undocumented cmake flags.There have been multiple other cases where this zig command could not be used and required workarounds or code patches:
cmake
- zig cc: unsupported darwin linker args #4841cgo
(before downstream patch) - "zig cc" doesn't work with cgo (golang) #7342graal native-image
- https://discord.com/channels/605571803288698900/606246764651085829/804764962101788722Given the above: Should we consider distributing two standard wrappers with Zig (e.g.
zigcc
andzigcxx
) which would redirect arguments tozig cc
andzig c++
?The text was updated successfully, but these errors were encountered: