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 Language #955

Closed
kassane opened this issue Sep 16, 2020 · 33 comments
Closed

Zig Language #955

kassane opened this issue Sep 16, 2020 · 33 comments

Comments

@kassane
Copy link

kassane commented Sep 16, 2020

I can't create an automatic zig project that only works manually.

Command: xmake create -l zig test

Error output

create test ...
error: invalid template id: console!

Please provide compiling and running environment information:

  • xmake version: 2.3.7
  • os: Arch Linux
  • target platform: x86_64
@waruqi waruqi added this to the v2.3.8 milestone Sep 16, 2020
@waruqi
Copy link
Member

waruqi commented Sep 16, 2020

Because the support for zig is still in the experimental stage, there are still some problems, so I did not add the project template.

The new version of zig seems to have solved some problems, so this version I will add them

@waruqi
Copy link
Member

waruqi commented Sep 17, 2020

I have added console/static/shared project tempaltes for zig.

But there are some problems caused by zig itself, you can look at related issues.

ziglang/zig#5825
ziglang/zig#5828
ziglang/zig#2230 (This problem may have been fixed, but it has not been updated to the latest version)

It may be necessary to wait for the official release of a new version of zig before xmake will go back and try to further support and fix it.

@waruqi waruqi modified the milestones: v2.3.8, v2.3.9 Oct 12, 2020
@waruqi
Copy link
Member

waruqi commented Nov 10, 2020

Oh, new issues for zig 0.7.0 ziglang/zig#7045

and this issue ziglang/zig#5825 (comment) does not solved yet for zig 0.7.0

another issue ziglang/zig#7046

@waruqi
Copy link
Member

waruqi commented Nov 11, 2020

Oh, two new issues. I think the current version of zig is not very stable yet, and I plan to continue to support it after it becomes more stable.

ziglang/zig#7065
ziglang/zig#7066

@waruqi waruqi modified the milestones: v2.3.9, todo Nov 11, 2020
@waruqi
Copy link
Member

waruqi commented Dec 8, 2020

zig/master have fixed some of the problems I raised before, xmake is now able to compile zig projects on macOS/Linux well, although there are still many problems on windows (ziglang/zig#5825 ziglang/zig#7065 ziglang/zig#7066).

And we can use xmake create -l zig console to create console/static/shared empty projects fastly. for example

$ xmake create -l zig console
$ cd console
$ xmake -v
[ 40%]: ccache compiling.release src/main.zig
zig build-obj -O ReleaseFast --cache-dir build/.objs/test/macosx/x86_64/release/zig-cache -femit-bin=build/.objs/test/macosx/x86_64/release/src/main.zig.o src/main.zig
[ 60%]: ccache compiling.release src/test.zig
zig build-obj -O ReleaseFast --cache-dir build/.objs/test/macosx/x86_64/release/zig-cache -femit-bin=build/.objs/test/macosx/x86_64/release/src/test.zig.o src/test.zig
[ 80%]: linking.release test
zig build-exe --strip -lz -femit-bin=build/macosx/x86_64/release/test build/.objs/test/macosx/x86_64/release/src/main.zig.o build/.objs/test/macosx/x86_64/release/src/test.zig.o
[100%]: build ok!
$ xmake run
Hello, world!

And I also added an example to build zig project with c codes. see https://github.com/xmake-io/xmake/tree/dev/tests/projects/zig/console_c_call_zig

@kassane
Copy link
Author

kassane commented Dec 8, 2020

@waruqi, cool! But you could also try using rust with zig. Like this project for example:
https://github.com/DutchGhost/zigiffy

@waruqi
Copy link
Member

waruqi commented Dec 8, 2020

@waruqi, cool! But you could also try using rust with zig. Like this project for example:
https://github.com/DutchGhost/zigiffy

Ok, I will try it.

@kassane
Copy link
Author

kassane commented Jan 24, 2021

It was being discussed by the zig community, the use of the zig cc | zig c++ compiler for C/C++ projects with cmake, meson and other similar. And it would be interesting to see the zig usage in xmake C/C++ projects.
Currently in some systems they work as follows:

Unix-like

$> export CC="zig cc $@"
$> export CXX="zig c++ $@"
...
# your build system
$> cmake or meson - commands

Windows - (not tested yet)

PS C:\Users\My-PC\project> $Env:CC="zig cc %*"
PS C:\Users\My-PC\project> cmake or meson - commands

or Batch script:

set PATH=zig_PATH;%PATH%
set ZIG_LOCAL_CACHE="%TEMP%"" && zig cc %*

I tried the same using xmake and this happens:

error: @programdir/core/main.lua:290: @programdir/actions/build/main.lua:147:
...mdir/core/sandbox/modules/import/core/base/scheduler.lua:87:
...mdir/core/sandbox/modules/import/core/base/scheduler.lua:47:
@programdir/modules/private/async/runjobs.lua:217:
...amdir/core/sandbox/modules/import/core/tool/compiler.lua:37: cannot find known tool script for zig c++ 

stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:787]: in function 'raise'
    [...amdir/core/sandbox/modules/import/core/tool/compiler.lua:37]: in function 'load'
    [@programdir/modules/private/action/build/object.lua:39]: in function 'script'
    [@programdir/modules/private/action/build/object.lua:91]: in function '_build_object'
    [@programdir/modules/private/action/build/object.lua:116]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:193]:
    [C]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:121]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:186]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:317]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:787: in function 'raise'
        @programdir/modules/private/async/runjobs.lua:217: in function 'catch'
        @programdir/core/sandbox/modules/try.lua:127: in function 'try'
        @programdir/modules/private/async/runjobs.lua:186: in function 'cotask'
        @programdir/core/base/scheduler.lua:317: in function <@programdir/core/base/scheduler.lua:315>
stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:787: in function 'raise'
        ...mdir/core/sandbox/modules/import/core/base/scheduler.lua:47: in function 'co_start_named'
        @programdir/modules/private/async/runjobs.lua:184: in function <@programdir/modules/private/async/runjobs.lua:144>
        [C]: in function 'trycall'
        @programdir/core/base/scheduler.lua:423: in function 'co_group_begin'
        ...mdir/core/sandbox/modules/import/core/base/scheduler.lua:85: in function 'co_group_begin'
        @programdir/modules/private/async/runjobs.lua:144: in function 'runjobs'
        @programdir/actions/build/build.lua:223: in function 'build'
        @programdir/actions/build/main.lua:134: in function <@programdir/actions/build/main.lua:124>
        [C]: in function 'trycall'
        @programdir/core/sandbox/modules/try.lua:121: in function 'try'
        @programdir/actions/build/main.lua:123: in function <@programdir/actions/build/main.lua:97>
        [C]: in function 'load'
        @programdir/core/base/task.lua:519: in function 'run'
        @programdir/core/main.lua:288: in function 'cotask'
        @programdir/core/base/scheduler.lua:317: in function <@programdir/core/base/scheduler.lua:315>
stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:787: in function 'raise'
        @programdir/core/main.lua:290: in function 'cotask'
        @programdir/core/base/scheduler.lua:317: in function <@programdir/core/base/scheduler.lua:315>

@waruqi
Copy link
Member

waruqi commented Jan 25, 2021

Currently, it is not supported to switch to zig cc compiler via $CC, but you can enable it with the following command.

ruki:console ruki$ xmake f --cc="gcc@zig cc" --ld="g++@zig c++" -c
ruki:console ruki$ xmake -rv
[ 25%]: ccache compiling.release src/main.c
zig cc -c -arch x86_64 -mmacosx-version-min=10.15 -isysroot /Applications/Xcod
e.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -fvisibility=hidden
 -O3 -DNDEBUG -o build/.objs/test/macosx/x86_64/release/src/main.c.o src/main.c
[ 50%]: linking.release test
"zig c++" -o build/macosx/x86_64/release/test build/.objs/test/macosx/x86_64/release/src/main.c.o -a
rch x86_64 -mmacosx-version-min=10.15 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms
/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -stdlib=libc++ -Wl,-x -lz
[100%]: build ok!

@waruqi
Copy link
Member

waruqi commented Jan 25, 2021

I have supported to use $CC/$LD to switch to zig cc and zig c++ as c/c++ compiler on dev branch.

export CC="zig cc"
export LD="zig c++"
xmake

or

xmake f --cc="zig cc" --ld="zig c++" -c
xmake

@kassane
Copy link
Author

kassane commented Jan 25, 2021

I just tested this command but there is still an error.

xmake f --cc="zig cc" --ld="zig c++" -c
xmake

and also

xmake f --cc="gcc@zig cc" --ld="g++@zig c++" -c

Output:

checking for platform ... linux
checking for architecture ... x86_64
checking for gcc ... /usr/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/bin/gcc ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
> gcc "-fvisibility-inlines-hidden"
checking for flags (-O3) ... ok
> gcc "-O3"
checking for flags (-DNDEBUG) ... ok
> gcc "-DNDEBUG"
checking for zig ... /home/kassane/.local/bin/zig
checking for the zig linker (zcld) ... zig
[ 50%]: linking.release xmake_test
/home/kassane/.local/bin/zig build-exe -target x86_64-linux-gnu --strip -femit-bin=build/linux/x86_64/release/xmake_test build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o
error: @programdir/modules/private/async/runjobs.lua:217: @programdir/actions/build/kinds/binary.lua:73: @programdir/core/sandbox/modules/os.lua:258: ld.lld: error: undefined symbol: std::cout
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)
>>> referenced 1 more times

ld.lld: error: undefined symbol: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)

ld.lld: error: undefined symbol: std::ostream::put(char)
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)

ld.lld: error: undefined symbol: std::ostream::flush()
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)

ld.lld: error: undefined symbol: std::ctype<char>::_M_widen_init() const
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)

ld.lld: error: undefined symbol: std::__throw_bad_cast()
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(main)

ld.lld: error: undefined symbol: std::ios_base::Init::Init()
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(_GLOBAL__sub_I_main)

ld.lld: error: undefined symbol: std::ios_base::Init::~Init()
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(_GLOBAL__sub_I_main)

ld.lld: error: undefined symbol: __cxa_atexit
>>> referenced by main.cpp
>>>               build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o:(_GLOBAL__sub_I_main)
error: LLDReportedFailure

stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:787]: in function 'raise'
    [@programdir/core/sandbox/modules/os.lua:258]: in function 'runv'
    [@programdir/modules/core/tools/zig.lua:125]:
    [C]: in function 'link'
    [@programdir/actions/build/kinds/binary.lua:73]: in function 'callback'
    [@programdir/modules/core/project/depend.lua:186]: in function 'on_changed'
    [@programdir/actions/build/kinds/binary.lua:54]: in function '_do_link_target'
    [@programdir/actions/build/kinds/binary.lua:94]:
    [@programdir/actions/build/kinds/binary.lua:115]: in function '_link_target'
    [@programdir/actions/build/kinds/binary.lua:137]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:193]:
    [C]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:121]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:186]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:317]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:787: in function 'raise'
        @programdir/modules/private/async/runjobs.lua:217: in function 'catch'
        @programdir/core/sandbox/modules/try.lua:127: in function 'try'
        @programdir/modules/private/async/runjobs.lua:186: in function 'cotask'
        @programdir/core/base/scheduler.lua:317: in function <@programdir/core/base/scheduler.lua:315>

Works:

$> xmake f --cc="gcc@zig cc" --cxx="g++@zig c++" --ld="g++@zig c++" -c;xmake 

Output:

checking for platform ... linux
checking for architecture ... x86_64
checking for zig c++ ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
checking for ccache ... /usr/bin/ccache
[ 25%]: ccache compiling.release src/main.cpp
/usr/bin/ccache zig c++ -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test
"zig c++" -o build/linux/x86_64/release/xmake_test build/.objs/xmake_test/linux/x86_64/release/src/main.cpp.o -m64 -s
[100%]: build ok!

@waruqi
Copy link
Member

waruqi commented Jan 25, 2021

yeah, you need pass --cxx="zig c++" if you compile c++

It works for me.

$ xmake f --cc="zig cc" --cxx="zig c++" --ld="zig c++" -c --ccache=n
checking for platform ... linux
checking for architecture ... x86_64
$ xmake -rv
checking for zig c++ ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.cpp
"zig c++" -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/test/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release test
"zig c++" -o build/linux/x86_64/release/test build/.objs/test/linux/x86_64/release/src/main.cpp.o -m64 -s
[100%]: build ok!

@kassane
Copy link
Author

kassane commented Jan 25, 2021

@waruqi

Windows - MinGW

Note: You will have to specify the target -target x86_64-windows-gnu, otherwise it will give an error asking for windows-sdk!

xmake f -p mingw --cc="gcc@zig cc -target x86_64-windows-gnu" --cxx="g++@zig c++ -target x86_64-windows-gnu" --ld="g++@zig c++ -target x86_64-windows-gnu" -c --ccache=n
xmake -rv

It does not work in v2.5.1 with zig c++ (works in latest dev version) directly without indicating the main compiler.

Output:

checking for architecture ... x86_64
checking for mingw directory ... C:\Users\Catarino\scoop\apps\msys2\2020-07-20\mingw64
checking for zig c++ -target x86_64-windows-gnu ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src\main.cpp
"zig c++ -target x86_64-windows-gnu" -c -Qunused-arguments -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build\.objs\test\mingw\x86_64\release\src\main.cpp.obj src\main.cpp
checking for flags (-MMD -MF) ... ok
[ 50%]: linking.release test.exe
"zig c++ -target x86_64-windows-gnu" -o build\mingw\x86_64\release\test.exe build\.objs\test\mingw\x86_64\release\src\main.cpp.obj -m64 -s
[100%]: build ok!

@waruqi
Copy link
Member

waruqi commented Jan 25, 2021

@waruqi

Windows - MinGW

Note: You will have to specify the target -target x86_64-windows-gnu, otherwise it will give an error asking for windows-sdk!

xmake f -p mingw --cc="gcc@zig cc -target x86_64-windows-gnu" --cxx="g++@zig c++ -target x86_64-windows-gnu" --ld="g++@zig c++ -target x86_64-windows-gnu" -c --ccache=n
xmake -rv

It does not work in v2.5.1 with zig c++ (works in latest dev version) directly without indicating the main compiler.

Output:

checking for architecture ... x86_64
checking for mingw directory ... C:\Users\Catarino\scoop\apps\msys2\2020-07-20\mingw64
checking for zig c++ -target x86_64-windows-gnu ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src\main.cpp
"zig c++ -target x86_64-windows-gnu" -c -Qunused-arguments -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build\.objs\test\mingw\x86_64\release\src\main.cpp.obj src\main.cpp
checking for flags (-MMD -MF) ... ok
[ 50%]: linking.release test.exe
"zig c++ -target x86_64-windows-gnu" -o build\mingw\x86_64\release\test.exe build\.objs\test\mingw\x86_64\release\src\main.cpp.obj -m64 -s
[100%]: build ok!

I have improved it, try it again on dev branch.

@kassane
Copy link
Author

kassane commented Jan 25, 2021

I have improved it, try it again on dev branch.

xmake f -p mingw --cc="zig cc" --cxx="zig c++" --ld="zig c++" -c --ccache=n
xmake -rv

Output:

checking for architecture ... x86_64
checking for mingw directory ... C:\Users\Catarino\scoop\apps\msys2\2020-07-20\mingw64
checking for zig c++ ... no
[ 25%]: compiling.release src\main.cpp
"zig c++" -c -target x86_64-windows-gnu -m64 -fvisibility=hidden -o build\.objs\test\mingw\x86_64\release\src\main.cpp.obj src\main.cpp
[ 50%]: linking.release test.exe
"zig c++" -o build\mingw\x86_64\release\test.exe build\.objs\test\mingw\x86_64\release\src\main.cpp.obj -target x86_64-windows-gnu -m64 -s
[100%]: build ok!
warning: add_cxflags("-DNDEBUG") is ignored, please pass `{force = true}` or call `set_policy("check.auto_ignore_flags", false)` if you want to set it.

@waruqi
Copy link
Member

waruqi commented Jan 26, 2021

I have improved it, try it again on dev branch.

xmake f -p mingw --cc="zig cc" --cxx="zig c++" --ld="zig c++" -c --ccache=n
xmake -rv

Output:

checking for architecture ... x86_64
checking for mingw directory ... C:\Users\Catarino\scoop\apps\msys2\2020-07-20\mingw64
checking for zig c++ ... no
[ 25%]: compiling.release src\main.cpp
"zig c++" -c -target x86_64-windows-gnu -m64 -fvisibility=hidden -o build\.objs\test\mingw\x86_64\release\src\main.cpp.obj src\main.cpp
[ 50%]: linking.release test.exe
"zig c++" -o build\mingw\x86_64\release\test.exe build\.objs\test\mingw\x86_64\release\src\main.cpp.obj -target x86_64-windows-gnu -m64 -s
[100%]: build ok!
warning: add_cxflags("-DNDEBUG") is ignored, please pass `{force = true}` or call `set_policy("check.auto_ignore_flags", false)` if you want to set it.

You can update to dev branch and try xmake f -p mingw --toolchain=zig -c --ccache=n

@kassane
Copy link
Author

kassane commented Jan 26, 2021

You can update to dev branch and try xmake f -p mingw --toolchain=zig -c --ccache=n

Well done! I tested it in both Windows and Linux environments.You have tried to compile on your machine, you no longer need to test on Windows exclusively!

Output:

checking for zig c++ --target=x86_64-windows-gnu ... ok
checking for the linker (ld) ... zig c++ --target=x86_64-windows-gnu
checking for the c++ compiler (cxx) ... zig c++ --target=x86_64-windows-gnu
checking for zig c++ --target=x86_64-windows-gnu ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.cpp
"zig c++ --target=x86_64-windows-gnu" -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/xmake_test/mingw/x86_64/release/src/main.cpp.obj src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test.exe
"zig c++ --target=x86_64-windows-gnu" -o build/mingw/x86_64/release/xmake_test.exe build/.objs/xmake_test/mingw/x86_64/release/src/main.cpp.obj -m64 -s
[100%]: build ok!

I believe that with zig, we won't need to use other external kits like mingw or mxe to compile from any platform to another.
Could you add in toolchain zig the possibility to compile on other architectures, including aarch64-macos-gnu?

@waruqi
Copy link
Member

waruqi commented Jan 26, 2021

Could you add in toolchain zig the possibility to compile on other architectures, including aarch64-macos-gnu?

error: error: unknown target CPU 'generic'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont,
goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i,
haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake,
icelake-client, icelake-server, tigerlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, ath
lon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znve
r1, znver2, x86-64

@kassane
Copy link
Author

kassane commented Jan 26, 2021

@waruqi ,

My suggestion:

-- init flags
local march
if is_plat("macosx") then
-- FIXME
--march = is_arch("x86") and "i386-macosx-gnu" or "x86_64-macosx-gnu"
elseif is_plat("linux") then
march = is_arch("x86") and "i386-linux-gnu" or "x86_64-linux-gnu"
elseif is_plat("windows") then
march = is_arch("x86") and "i386-windows-msvc" or "x86_64-windows-msvc"
elseif is_plat("mingw") then
march = is_arch("x86") and "i386-windows-gnu" or "x86_64-windows-gnu"
end

Change to:

if not self:program():find("target", 1, true) then
        local march
if is_plat("macosx") then
            if is_arch("x86" or "x86_64") then
                march = is_arch("x86") and "i386-macos-gnu" or "x86_64-macos-gnu"
            else
                march = is_arch("arm64") and "aarch64-macos-gnu"
            end
        elseif is_plat("linux") then
            if is_arch("x86" or "x86_64") then
                march = is_arch("x86") and "i386-linux-gnu" or "x86_64-linux-gnu"
            elseif is_arch("arm" or "arm64") then
                march = is_arch("arm64") and "aarch64-linux-gnu" or "arm-linux-gnu"
            end
        elseif is_plat("windows") then
            march = is_arch("x86") and "i386-windows-msvc" or "x86_64-windows-msvc"
        elseif is_plat("mingw") then
            march = is_arch("x86") and "i386-windows-gnu" or "x86_64-windows-gnu"
        end

Test

MacOSX

Command:

xmake f -p macosx --toolchain=zig -c --ccache=n
xmake -rv

Output:

checking for architecture ... x86_64
checking for Xcode directory ... %s
checking for Codesign Identity of Xcode ... no
checking for SDK version of Xcode for macosx (x86_64) ... nil
checking for Minimal target version of Xcode for macosx (x86_64) ... nil
checking for zig c++ ... ok
checking for the linker (ld) ... zig c++
checking for zig cc ... ok
checking for the c compiler (cc) ... zig cc
checking for zig cc ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.c
"zig cc" -c -arch x86_64 -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/xmake_test/macosx/x86_64/release/src/main.c.o src/main.c
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test
"zig c++" -o build/macosx/x86_64/release/xmake_test build/.objs/xmake_test/macosx/x86_64/release/src/main.c.o -arch x86_64 -stdlib=libc++ -Wl,-x -lz
[100%]: build ok!

MacOSX - ARM64

Command:

xmake f -p macosx -a arm64 --toolchain=zig -c --ccache=n
xmake -rv

Output:

checking for Xcode directory ... %s
checking for Codesign Identity of Xcode ... no
checking for SDK version of Xcode for macosx (arm64) ... nil
checking for Minimal target version of Xcode for macosx (arm64) ... nil
checking for zig c++ ... ok
checking for the linker (ld) ... zig c++
checking for zig cc ... ok
checking for the c compiler (cc) ... zig cc
checking for zig cc ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.c
"zig cc" -c -target aarch64-macos-gnu -arch arm64 -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/xmake_test/macosx/arm64/release/src/main.c.o src/main.c
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test
"zig c++" -o build/macosx/arm64/release/xmake_test build/.objs/xmake_test/macosx/arm64/release/src/main.c.o -target aarch64-macos-gnu -arch arm64 -stdlib=libc++ -Wl,-x -lz
[100%]: build ok!

@waruqi
Copy link
Member

waruqi commented Jan 26, 2021

        march = is_arch("x86") and "i386-macos-gnu" or "x86_64-macos-gnu"

I have added it.

        march = is_arch("arm64") and "aarch64-macos-gnu"

error: error: unknown target CPU 'generic'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont,
goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i,
haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake,
icelake-client, icelake-server, tigerlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, ath
lon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znve
r1, znver2, x86-64

@kassane
Copy link
Author

kassane commented Jan 26, 2021

error: error: unknown target CPU 'generic'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont,
goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i,
haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake,
icelake-client, icelake-server, tigerlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, ath
lon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znve
r1, znver2, x86-64

Which version of the zig?
I use zig-master.

You have already tried it manually?

zig cc src/main.c -target aarch64-macos-gnu
# or
zig cc src/main.c -target x86_64-macos-gnu

I think the problem involves lld in the macosx exclusively.

Project under development:
https://github.com/kubkon/zld - Zig's lld drop-in replacement

@waruqi
Copy link
Member

waruqi commented Jan 27, 2021

I have improved it, you can try it again

xmake f --toolchain=zig -a arm64 -c
xmake

@kassane
Copy link
Author

kassane commented Jan 27, 2021

I have improved it, you can try it again

xmake f --toolchain=zig -a arm64 -c
xmake

Well done!

Linux ARM64

checking for platform ... linux
checking for zig c++ ... ok
checking for the linker (ld) ... zig c++
checking for zig cc ... ok
checking for the c compiler (cc) ... zig cc
checking for zig cc ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.c
"zig cc" -c -target aarch64-linux-gnu -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/xmake_test/linux/arm64/release/src/main.c.o src/main.c
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test
"zig c++" -o build/linux/arm64/release/xmake_test build/.objs/xmake_test/linux/arm64/release/src/main.c.o -target aarch64-linux-gnu -s
[100%]: build ok!

MacOSX - ARM64

checking for zig c++ ... ok
checking for the linker (ld) ... zig c++
checking for zig cc ... ok
checking for the c compiler (cc) ... zig cc
checking for zig cc ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: compiling.release src/main.c
"zig cc" -c -target aarch64-macos-gnu -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/xmake_test/macosx/arm64/release/src/main.c.o src/main.c
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
[ 50%]: linking.release xmake_test
"zig c++" -o build/macosx/arm64/release/xmake_test build/.objs/xmake_test/macosx/arm64/release/src/main.c.o -target aarch64-macos-gnu -Wl,-x
[100%]: build ok!

Can you add more platforms for testing?

zig targets | jq .libc

Output:

[
  "aarch64_be-linux-gnu",
  "aarch64_be-linux-musl",
  "aarch64_be-windows-gnu",
  "aarch64-linux-gnu",
  "aarch64-linux-musl",
  "aarch64-windows-gnu",
  "aarch64-macos-gnu",
  "armeb-linux-gnueabi",
  "armeb-linux-gnueabihf",
  "armeb-linux-musleabi",
  "armeb-linux-musleabihf",
  "armeb-windows-gnu",
  "arm-linux-gnueabi",
  "arm-linux-gnueabihf",
  "arm-linux-musleabi",
  "arm-linux-musleabihf",
  "arm-windows-gnu",
  "i386-linux-gnu",
  "i386-linux-musl",
  "i386-windows-gnu",
  "mips64el-linux-gnuabi64",
  "mips64el-linux-gnuabin32",
  "mips64el-linux-musl",
  "mips64-linux-gnuabi64",
  "mips64-linux-gnuabin32",
  "mips64-linux-musl",
  "mipsel-linux-gnu",
  "mipsel-linux-musl",
  "mips-linux-gnu",
  "mips-linux-musl",
  "powerpc64le-linux-gnu",
  "powerpc64le-linux-musl",
  "powerpc64-linux-gnu",
  "powerpc64-linux-musl",
  "powerpc-linux-gnu",
  "powerpc-linux-musl",
  "riscv64-linux-gnu",
  "riscv64-linux-musl",
  "s390x-linux-gnu",
  "s390x-linux-musl",
  "sparc-linux-gnu",
  "sparcv9-linux-gnu",
  "wasm32-freestanding-musl",
  "x86_64-linux-gnu",
  "x86_64-linux-gnux32",
  "x86_64-linux-musl",
  "x86_64-windows-gnu",
  "x86_64-macos-gnu"
]

Is it plausible to add QEMU support to run other architecture/platform applications?

@waruqi
Copy link
Member

waruqi commented Jan 27, 2021

xmake f -p linux -a [mips|mips64| riscv64|ppc|ppc64|s390x ...] --toolchain=zig --ccache=n
xmake

or

xmake f -p cross --cross=riscv64-linux-musl --toolchain=zig --ccache=n
xmake

on dev

@kassane
Copy link
Author

kassane commented Jan 27, 2021

xmake f -p linux -a [mips|mips64| riscv64|ppc|ppc64|s390x ...] --toolchain=zig --ccache=n
xmake

or

xmake f -p cross --cross=riscv64-linux-musl --toolchain=zig --ccache=n
xmake

I did some tests on several platforms/architectures both of the mentioned commands work perfectly.

However, the fact that the zig compiler is unstable, currently in some occur some errors on the part of the compiler. Like for example: riscv64-linux-gnu (not work), but the riscv64-linux-musl version works.

@waruqi
Copy link
Member

waruqi commented Jan 27, 2021

However, the fact that the zig compiler is unstable, currently in some occur some errors on the part of the compiler. Like for example: riscv64-linux-gnu (not work), but the riscv64-linux-musl version works.

This just needs to wait for zig to improve them, xmake does not need to care about these

@kassane
Copy link
Author

kassane commented Jan 27, 2021

@waruqi , nice work again!

@waruqi
Copy link
Member

waruqi commented Jan 27, 2021

If there are no other problem about zig, I am going to close this issue. It should work for zig.

@kassane kassane closed this as completed Jan 27, 2021
@DrSensor
Copy link

DrSensor commented Feb 16, 2021

Hi, does --toolchain=zig work with go, or should it be set explicitly?

https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho

@waruqi
Copy link
Member

waruqi commented Feb 16, 2021

Hi, does --toolchain=zig work with go, or should it be set explicitly?

https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho

Not yet supported

@kassane
Copy link
Author

kassane commented Feb 16, 2021

Hi @DrSensor, as @waruqi already informed xmake there is no cgo support. But already in the next release version or currently in development version it already allows us to interoperate with rust projects.

See example

set_xmakever("2.5.2") -- (current: latest dev branch) Zig toolchain support

add_rules("mode.debug", "mode.release")

target("math_rust")
    set_kind("static")
    add_files("math_ffi/rust/src/lib.rs")
    after_build(function (target) -- rename rlib to ...
        local prefix
        if is_plat("windows") then prefix = ".lib"
        else prefix=".a"
        end
        os.mv(target:targetfile(), path.join(path.directory(target:targetfile()), path.basename(target:targetfile()) .. prefix))
    end)
    set_toolchains("rust")
    
target("math_zig")
    set_kind("static")
    add_files("math_ffi/zig/src/main.zig")
    add_zcflags("-fPIC", "--single-threaded", "--strip", {force = true})
    set_toolchains("zig")

    
target("rust_app")
    set_kind("binary")
    add_files("app_rs/src/*.rs")
    add_deps("math_zig")
    add_linkdirs("$(buildir)")
    set_toolchains("rust")

target("zig_app")
    set_kind("binary")
    add_files("app_zig/src/main.zig")
    add_deps("math_rust")
    set_toolchains("zig")

If you are interested in this, you can see some examples here.
https://kassane.github.io/rust-ffi-omnibus

@DrSensor
Copy link

Does that mean it will leverage zig cc caching system when compiling rust code and/or crates?

https://kristoff.it/blog/zig-new-relationship-llvm/#in-place-binary-patching

@kassane
Copy link
Author

kassane commented Feb 16, 2021

Does that mean it will leverage zig cc caching system when compiling rust code and/or crates?

Any questions regarding the zig should be handled by their community.
zig cc is also accepted by rustc. I have done some testing by modifying the ~/.cargo/config file to improve cross-compilation support.

[target.x86_64-unknown-linux-musl]
linker="zig cc --target x86_64-linux-musl"
ar="zig cc --target x86_64-linux-musl"

[target.x86_64-pc-windows-gnu]
linker="zig cc --target x86_64-windows-gnu"
ar="zig cc --target x86_64-windows-gnu"

XMake does not promise to be a replacement for cargo and does not offer crates support. Although the focus of the project is C/C++. But the Zig toolchain support will allow not only access to projects written in ziglang but also C/C++ projects as an alternative to traditional compilers, although the zig compiler itself is a clang compiler fork.

In the example shown by me earlier, it demonstrates that we will be able to develop different toolchain projects simultaneously through interoperability between languages. I know that many Rust programmers think this kind of project is ridiculous, but they forget that both have the same backend called LLVM and precisely because of this they are binary compatible.

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

No branches or pull requests

3 participants