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

需要改进对 zig 编译器的支持 #5915

Open
Redbeanw44602 opened this issue Dec 1, 2024 · 10 comments
Open

需要改进对 zig 编译器的支持 #5915

Redbeanw44602 opened this issue Dec 1, 2024 · 10 comments
Labels

Comments

@Redbeanw44602
Copy link
Contributor

Redbeanw44602 commented Dec 1, 2024

Xmake 版本

v2.9.6+20241030

操作系统版本和架构

Manjaro Linux

描述问题

一般有需要使用 zig 编译 C/C++ 项目的需求,xmake 对此已经有了初步支持,但测试发现仍然有些方面需要改进。

  1. 对于系统包的处理
    zig 默认不导入系统包,而 xmake 的默认行为恰好与之相反,这将导致头文件查找问题。

  2. 依赖库编译的工具链
    zig 基于 llvm 工具链构建,与 linux 下常用的 gcc 工具链(以及对应包)兼容性往往存在问题。

  3. glibc 版本选择
    zig 支持在 target 中传入 glibc 版本(doc),xmake 尚不支持。

  4. Compiling a cmake project with zig c++ doesn't resolve to and absolute path #5610

期待的结果

  1. 考虑默认不搜索系统包,等价 xmake 配置:
local opt = {
    system = false
}

add_requireconfs('**|cmake|pkg-config', opt)
  1. 考虑在文档中写明兼容问题,或者添加警告?

  2. 添加一个 glibc_version 参数

工程配置

N/A

附加信息和错误日志

N/A

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: Need to improve zig compiler support

Xmake version

v2.9.6+20241030

Operating system version and architecture

Manjaro Linux

Describe the problem

Generally, there is a need to use zig to compile C/C++ projects. xmake already has preliminary support for this, but testing found that there are still some aspects that need improvement.

  1. Processing of system packages
    zig does not import system packages by default, and xmake's default behavior is exactly the opposite, which will cause header file lookup problems.

  2. Tool chain that relies on library compilation
    zig is built based on the llvm tool chain, and there are often compatibility issues with the gcc tool chain (and corresponding packages) commonly used under Linux.

  3. glibc version selection
    zig supports passing the glibc version in the target (doc), but xmake does not yet support it.

Expected results

  1. Consider not searching for system packages by default, equivalent to xmake configuration:
local opt = {
    system = false
}

add_requireconfs('**|cmake|pkg-config', opt)
  1. Consider writing compatibility issues in the documentation, or adding warnings?

  2. Add a glibc_version parameter

Project configuration

N/A

Additional information and error logs

N/A

@waruqi
Copy link
Member

waruqi commented Dec 2, 2024

考虑默认不搜索系统包,等价 xmake 配置:

这个在语言层面是没法自动处理的,你只能自己配置 system = false 不走系统,

或者 set_policy("package.install_only", true) 全局配置下,不走系统。

考虑在文档中写明兼容问题,或者添加警告?

没时间,可以来个 pr 过来。

添加一个 glibc_version 参数

这个可以参考 gcc/clang/msvc ,在 toolchain 中增加支持的 runtimes ,比如 glibc 等

set_runtimes("stdc++_static", "stdc++_shared")

然后在 zig rules 中取 runtimes 处理下,可以来个 pr 过来

@star-hengxing
Copy link
Contributor

添加一个 glibc_version 参数

这个可以参考 gcc/clang/msvc ,在 toolchain 中增加支持的 runtimes ,比如 glibc 等

set_runtimes("stdc++_static", "stdc++_shared")

然后在 zig rules 中取 runtimes 处理下,可以来个 pr 过来

这个好像不好加,因为 glibc version 是这样用的 x86_64-linux-gnu.2.29 ,而 target 是在 toolchain load 配置的。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Add a glibc_version parameter

You can refer to gcc/clang/msvc for this, and add supported runtimes in the toolchain, such as glibc, etc.

set_runtimes("stdc++_static", "stdc++_shared")

Then take the runtimes in zig rules and process it, and you can do a PR.

This seems difficult to add, because the glibc version is x86_64-linux-gnu.2.29, and the target is configured in toolchain load.

@waruqi
Copy link
Member

waruqi commented Jan 13, 2025

添加一个 glibc_version 参数

这个可以参考 gcc/clang/msvc ,在 toolchain 中增加支持的 runtimes ,比如 glibc 等

set_runtimes("stdc++_static", "stdc++_shared")

然后在 zig rules 中取 runtimes 处理下,可以来个 pr 过来

这个好像不好加,因为 glibc version 是这样用的 x86_64-linux-gnu.2.29 ,而 target 是在 toolchain load 配置的。

那啥也不用改,走 cross 平台就行了,--cross 对标 --target ,可以随意传。

$ xmake f -p cross --toolchain=zig -c --cross=x86_64-linux-gnu.2.29
$ xmake -rv
[ 50%]: cache compiling.release src/main.c
/tmp/.xmake999/250113/zigcc/cc -c -target x86_64-linux-gnu.2.29 -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/test/cross/x86_64/release/src/main.c.o src/main.c
[ 75%]: linking.release test
/tmp/.xmake999/250113/zigcc/c++ -o build/cross/x86_64/release/test build/.objs/test/cross/x86_64/release/src/main.c.o -target x86_64-linux-gnu.2.29 -s
[100%]: build ok, spent 0.67s

@waruqi
Copy link
Member

waruqi commented Jan 13, 2025

另外,我放开了 zig 下对 cross 设置,任何平台,只要设置了 cross 优先使用

#6069

@star-hengxing
Copy link
Contributor

  1. 对于系统包的处理
    zig 默认不导入系统包,而 xmake 的默认行为恰好与之相反,这将导致头文件查找问题。
  2. 依赖库编译的工具链
    zig 基于 llvm 工具链构建,与 linux 下常用的 gcc 工具链(以及对应包)兼容性往往存在问题。

看起来 zig rule 默认 runtimes 应该设置为 libc++。
不过适配 libstdc++ 貌似有点麻烦。
https://gist.github.com/kassane/7e9a2da137e13eb6e1dbab726693bdb7?permalink_comment_id=4475986#gistcomment-4475986

可以试一下 sysroot 之类的行不行

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


In addition, I have released the cross settings under zig. Any platform, as long as cross is set, will be used first.

@star-hengxing
Copy link
Contributor

msys2 + mingw64 + zig + libstdc++

  • linkpath = true
[ 50%]: cache compiling.release src\main.cpp
C:\msys64\tmp\.xmake\250113\zigcc\c++.cmd -c -target x86_64-windows-gnu -IC:\msys64\mingw64\include -IC:\msys64\mingw64\include\c++\14.2.0 -IC:\msys64\mingw64\include\c++\14.2.0\x86_64-w64-mingw32 -IC:\Users\star\AppData\Local\.xmake\packages\a\ada\v2.9.2\1e516e9da60946fb8b0a5ee751eefead\include -nostdinc++ -nostdlib++ -o build\.objs\test\mingw\x86_64\release\src\main.cpp.obj src\main.cpp
[ 75%]: linking.release test.exe
C:\msys64\tmp\.xmake\250113\zigcc\c++.cmd -o build\mingw\x86_64\release\test.exe build\.objs\test\mingw\x86_64\release\src\main.cpp.obj -target x86_64-windows-gnu -LC:\msys64\mingw64\lib C:\Users\star\AppData\Local\.xmake\packages\a\ada\v2.9.2\1e516e9da60946fb8b0a5ee751eefead\lib\libada.dll.a C:/msys64/mingw64/lib/libstdc++.dll.a
[100%]: build ok, spent 1.375s
  • linkpath = false
:/msys64/mingw64/lib/libstdc++.dll.a
error: unable to find dynamic system library 'ada' using strategy 'paths_first'. searched paths:
  C:\msys64\mingw64\lib\ada.dll
  C:\msys64\mingw64\lib\ada.lib
  C:\msys64\mingw64\lib\libada.a
  C:\Users\star\AppData\Local\.xmake\packages\a\ada\v2.9.2\1e516e9da60946fb8b0a5ee751eefead\lib\ada.dll
  C:\Users\star\AppData\Local\.xmake\packages\a\ada\v2.9.2\1e516e9da60946fb8b0a5ee751eefead\lib\ada.lib
  C:\Users\star\AppData\Local\.xmake\packages\a\ada\v2.9.2\1e516e9da60946fb8b0a5ee751eefead\lib\libada.a
add_requires("ada[shared]")
add_packages("ada", {linkpath = true})

set_runtimes("stdc++_shared")

target("test")
    set_kind("binary")
    add_files("src/main.cpp")
    set_toolchains("zig")

    on_config(function (target)
        if target:has_runtime("stdc++_shared", "stdc++_static") then
            if is_subhost("msys") then
                local mingw_prefix = os.getenv("MINGW_PREFIX")
                if mingw_prefix and os.isdir(mingw_prefix) then
                    target:add("includedirs", path.join(mingw_prefix, "include"))
                    target:add("includedirs", path.join(mingw_prefix, "include/c++/14.2.0"))
                    target:add("includedirs", path.join(mingw_prefix, "include/c++/14.2.0/x86_64-w64-mingw32"))
                    target:add("cxflags", "-nostdinc++", "-nostdlib++")

                    target:add("linkdirs", path.join(mingw_prefix, "lib"))
                    target:add("syslinks", "C:/msys64/mingw64/lib/libstdc++.dll.a")
                end
            end
        end
    end)

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


  1. Processing of system packages
    zig does not import system packages by default, and xmake's default behavior is exactly the opposite, which will cause header file lookup problems.
  2. Tool chain that relies on library compilation
    zig is built based on the llvm tool chain, and there are often compatibility issues with the gcc tool chain (and corresponding packages) commonly used under Linux.

It looks like the zig rule default runtimes should be set to libc++.
However, adapting libstdc++ seems to be a bit troublesome.
https://gist.github.com/kassane/7e9a2da137e13eb6e1dbab726693bdb7?permalink_comment_id=4475986#gistcomment-4475986

Can you try something like sysroot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants