Closed kassane closed 11 months ago
Hi @lupyuen,
I remember you made a pinephone guide using unicorn-rs. Do you have any suggestions for suitable zig support based on your experiment?
@kassane Sorry I haven't thought about how we might support Zig in Unicorn. (I used the standard Rust Bindings for Unicorn)
But I think Zig support would be really useful.
@wtdcode Test outupt (Zig CI test): https://github.com/kassane/unicorn/actions/runs/4596902849/jobs/8118942852
--edit
Sorry for late because I was trying to use zig to try your samples and stuck on some dumb questions. I will review it asap.
Sorry for late because I was trying to use zig to try your samples and stuck on some dumb questions. I will review it asap.
No worries! If you need help, I'm available.
Post commit https://github.com/unicorn-engine/unicorn/pull/1815/commits/765c84dd943901a2dca1b1750eef31d2c9a51f72 was retested on windows mingw and no segfault occurred. Currently zig toolchain/mingw doesn't provide winpthreads support. However, in this build the libunicorn already includes winpthread.
Edit Sadly still haven't finalized the readaptation from build.zig ( like standalone zig) to QEMU, because it needs to have the configs.h (host and target) predefined already in some build steps. The winpthread issue would probably get fixed with that: https://github.com/kassane/winpthreads-zigbuild
New output: (Build all samples)
Help
$> zig build -h
Usage: /home/kassane/zig/0.11.0-dev.2545+311d50f9d/files/zig build [steps] [options]
Steps:
install (default) Copy build artifacts to prefix path
uninstall Remove build artifacts from prefix path
sample_riscv_zig Run the sample_riscv_zig sample
sample_arm Run the sample_arm sample
sample_arm64 Run the sample_arm64 sample
sample_ctl Run the sample_ctl sample
sample_batch_reg Run the sample_batch_reg sample
sample_m68k Run the sample_m68k sample
sample_riscv Run the sample_riscv sample
sample_sparc Run the sample_sparc sample
sample_s390x Run the sample_s390x sample
shellcode Run the shellcode sample
sample_tricore Run the sample_tricore sample
sample_x86 Run the sample_x86 sample
sample_x86_32_gdt_and_seg_regs Run the sample_x86_32_gdt_and_seg_regs sample
cmake Run cmake build
Build
Build Summary: 29/29 steps succeeded
sample_x86_32_gdt_and_seg_regs success
└─ run sample_x86_32_gdt_and_seg_regs success 1ms MaxRSS:4M
├─ zig build-exe sample_x86_32_gdt_and_seg_regs Debug native success 355ms MaxRSS:353M
└─ install success
├─ install sample_riscv_zig success
│ └─ zig build-exe sample_riscv_zig Debug native success 2s MaxRSS:356M
├─ install sample_arm success
│ └─ zig build-exe sample_arm Debug native success 344ms MaxRSS:352M
├─ install sample_arm64 success
│ └─ zig build-exe sample_arm64 Debug native success 345ms MaxRSS:352M
├─ install sample_ctl success
│ └─ zig build-exe sample_ctl Debug native success 368ms MaxRSS:352M
├─ install sample_batch_reg success
│ └─ zig build-exe sample_batch_reg Debug native success 386ms MaxRSS:352M
├─ install sample_m68k success
│ └─ zig build-exe sample_m68k Debug native success 415ms MaxRSS:353M
├─ install sample_riscv success
│ └─ zig build-exe sample_riscv Debug native success 343ms MaxRSS:352M
├─ install sample_sparc success
│ └─ zig build-exe sample_sparc Debug native success 527ms MaxRSS:353M
├─ install sample_s390x success
│ └─ zig build-exe sample_s390x Debug native success 381ms MaxRSS:352M
├─ install shellcode success
│ └─ zig build-exe shellcode Debug native success 1s MaxRSS:352M
├─ install sample_tricore success
│ └─ zig build-exe sample_tricore Debug native success 241ms MaxRSS:351M
├─ install sample_x86 success
│ └─ zig build-exe sample_x86 Debug native success 386ms MaxRSS:352M
└─ install sample_x86_32_gdt_and_seg_regs success
└─ zig build-exe sample_x86_32_gdt_and_seg_regs Debug native (reused)
cc: @wtdcode @aquynh
Following the theme proposed in the title of this contribution is enough. It only remains to improve the necessary later after the review.
The next step (another PR) will be to make zig a fully standalone build alternative (change build.zig
).
zig is not well tolerated by cmake or any trivial build system because there is 'space' in the zig cc/zig c++
command causing certain conflicts.
By using only the zig toolchain, it would allow, in theory, to unify the configuration between the library and the built examples (cross compilation).
cc: @wtdcode @aquynh
Following the theme proposed in the title of this contribution is enough. It only remains to improve the necessary later after the review.
The next step (another PR) will be to make zig a fully standalone build alternative (change
build.zig
).Why? Would CMake not solve it?
zig is not well tolerated by cmake or any trivial build system because there is 'space' in the
zig cc/zig c++
command causing certain conflicts. By using only the zig toolchain, it would allow, in theory, to unify the configuration between the library and the built examples (cross compilation).
Just curious about whether is there any other workaround for this? One straightforward workaround I think is something like ln -s "zig cc" "zigc"
.
Just curious about whether is there any other workaround for this? One straightforward workaround I think is something like
ln -s "zig cc" "zigc"
.
Usually it has been tried to create a false clang (zigcc) in this way below:
Unix-like
#!/usr/bin/env bash
`which zig` cc $@
Windows - cmd
@echo off
zig cc %*
@mrexodia - zig-cmake https://github.com/mrexodia/zig-cross
The real issue with cmake is that tools like ranlib and friends cannot have arguments in the compiler path. As you can see in zig-cross this is worked around by pointing cmake to a script file that wraps it as mentioned above.
Latest change - CI: https://github.com/kassane/unicorn/actions/runs/5026896166
The only change I propose is to move the shell scripts to bindings/zig
. Other looks fine to me. Sorry for the delay because I was sick for a few weeks.
zig-mingw
build CI was disabled.
It found a compiler failure (on zig cc w/ cmake
), described here:
x86_64: https://github.com/kassane/unicorn/actions/runs/5032387218/jobs/9025992784#step:6:21
ERROR: Your compiler does not support the __thread specifier for
Thread-Local Storage (TLS). Please upgrade to a version that does.
aarch64-windows: https://github.com/kassane/unicorn/actions/runs/5032387218/jobs/9025992877#step:6:13
run cmake: error: unable to spawn cmake: InvalidExe # x86_64 executable?
Zig 0.11.0 released: https://ziglang.org/download/0.11.0/release-notes.html
@kassane Is it the final version? I'm going to merge it if it's done.
@kassane Is it the final version? I'm going to merge it if it's done.
Yes! Merge - works in 0.11.0 stable
@kassane Is it the final version? I'm going to merge it if it's done.
Yes! Merge - works in 0.11.0 stable
Here you go. Thanks!
The sample riscv ( random choose) was chosen for reference and was used as basis for developing the wrapper/binding. In addition to a basic build-system that could trigger cmake and its necessary commands to build.
How to use
Need
libunicorn
:When building, it will create the build folder containing the required library.
build
sample_riscv.zig
Note: It is recommended that you try to link the libunicorn-shared or build libunicorn with the same compiler (
zig cc
). Unfortunately there are conflicts with gcc/mingw.Ref.: #1809