Zig
Zig は、堅牢で、最適化された、再利用可能なソフトウェアを保守するための、汎用プログラミング言語およびツールチェーンです。Zig は C より良い言語として C を置き換えることを目的としており、C の関数を FFI 無しで利用したりエクスポートすることができ[1]、C および C++ で書かれたコードを libclang を使用してコンパイルすることもできます。[2]上流は、libclang 無しでこれらの関数を利用できるように、独自の C コンパイラにも取り組んでいます[3]。
インストール
前提条件
Zig 0.10.1 をコンパイルするためには 9.6 GiB の RAM が必要です。[4]
Zig は少なくともカーネル 3.16+ をサポートしています。[5]ソースからビルドする場合は、LLVM/Clang のコンパイルに使ったものと同じコンパイラを使ってビルドする必要があり、そうしないと問題が発生するでしょう (下のトラブルシューティングの関連する節を参照してください)。
USE フラグ
USE flags for dev-lang/zig A robust, optimal, and maintainable programming language
USE flags for dev-lang/zig-bin A robust, optimal, and maintainable programming language
doc
|
Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally |
Emerge
バイナリパッケージを使用したい場合は、"dev-lang/zig" を "dev-lang/zig-bin" で置き換えてください (現在のアーキテクチャで利用可能な場合)。
パッケージは現在どのアーキテクチャ上でも安定化されておらず、一部のアーキテクチャ上で testing としてキーワード指定されています。そのため、後者のアーキテクチャで使用するには、testing (~arch
、arch はシステムのアーキテクチャで置き換えてください) キーワードを許可する必要があります:
root #
echo "dev-lang/zig ~arch" >> /etc/portage/package.accept_keywords
そして、パッケージを emerge してください:
root #
emerge --ask dev-lang/zig
設定
ZIG_MCPU と ZIG_TARGET
どのターゲットと CPU 向けに dev-lang/zig をビルドするかを調節するために、ZIG_MCPU および ZIG_TARGET 変数があります (これらは -target
および -mcpu
引数として zig に渡されます:
/etc/portage/make.conf/zig
Examples of ZIG_MCPU and ZIG_TARGET# By default ZIG_MCPU and ZIG_TARGET equal to "native"
# In case of cross-compilation ZIG_MCPU equals to "baseline"
# and ZIG_TARGET is converted from CTARGET or CHOST
# In most cases user don't have to set custom values, except when "native" or convertion doesn't work
#
# ZIG_TARGET has this required sections: <arch>-<os>-<abi>
ZIG_TARGET="x86_64-linux-gnu" # For computer running AMD64 on Linux kernel with glibc
# And optional ones: min_ver...max_version of OS and ABI/libc version
ZIG_TARGET="x86_64-linux.6.1.12...6.1.12-gnu.2.36" # For Linux kernel 6.1.12 and glibc 2.36
#
# ZIG_MCPU has required section of architecture's name
ZIG_MCPU="znver2" # For processors from Zen 2 family
# And optional section for features on this architecture
# ("+" means it has and "-" means it hasn't this feature)
ZIG_MCPU="x86_64+x87-sse" # For processors on AMD64 architecture, with added support for X87 but without SSE2 instructions
デフォルトスロットを設定する
eselect コマンドを利用して /usr/bin/zig シンボリックリンクを操作することができます。
user $
eselect zig help
Manage Zig versions Usage: eselect zig <action> <options> Standard actions: help Display help text usage Display usage information version Display version information Extra actions: list List available Zig versions set <target> Set active Zig version target Target name or number (from 'list' action) show Show current Zig version update Automatically update the zig symlink ifunset Do not override currently set version
user $
eselect zig list
Available Zig versions: [1] zig-0.10.1 * [2] zig-9999 [3] zig-bin-0.10.1
環境変数
残念ながら、以下以外の引数 (ターゲットや、リリース/最適化モードなど) を環境変数で設定する方法は無いので、ユーザはそれらを引数で指定する必要があります。
以下の環境変数よりも、コマンドライン引数のほうが優先されます:
- ZIG_GLOBAL_CACHE_DIR - path to the directory where things that are shared between different compilations are cached
- ZIG_LOCAL_CACHE_DIR - path to the directory where the current compilation is cached, by default next to build.zig
- ZIG_LIB_DIR - path to Zig standard library
使い方
一般的な使い方
Zig で書かれた多くのプログラムやライブラリが、comptime 言語機能を活用しています。詳しい情報は language reference で読めますが、一言で言うと、Zig はコンパイル時にソースファイル (build.zig、標準ライブラリ、プロジェクト自身のファイルなど) の中の任意のコードを実行することができます。注意してください。
user $
zig --help
Most commands grouped together, like ast-check to translate-c, have the same command options.
In the second group, it is highly recommended to use -fstrip to reduce the executable size except for debugging purposes.
ビルドモード
Zig には 4 種類の「ビルドモード」 (最近のバージョンでは「最適化モード」とも) があります:
Feature | Debug (default) | ReleaseFast | ReleaseSafe | ReleaseSmall |
---|---|---|---|---|
Compilation speed | Fast | Slow | Slow | Slow |
Safety checks at runtime | Enabled | Disabled | Enabled | Disabled |
Optimizations | Disabled | Optimized for speed | Optimized for speed | Optimized for size |
Reproducible | Not guaranteed | Yes | Yes | Yes |
Equivalent in C
(interop. or compiling)[6] |
-D_DEBUG -O0
|
-DNDEBUG -O2
|
-O2 -D_FORTIFY_SOURCE=2
|
-DNDEBUG -Os
|
Safety checks at runtime can be overridden per-scope/block by setting @setRuntimeSafety. Safety cheks at compile-time are always enabled.
LSP サポート
Unofficial language server is available as dev-zig/zls
in GURU repository.
For installing follow this guide to enable GURU repository, accept testing branch for this package and run:
root #
emerge --ask dev-zig/zls::guru
テキストエディタサポート
上流によるツールの一覧を参照してください。現時点では、これらはどれもパッケージ化されていません。
フリースタンディング
User might want to take a look at posts Using Zig to build a bare metal RISCV32 binary and Build an IoT App with Zig and LoRaWAN.
トラブルシューティング
emerge 中の "undefined reference to ..."
もっとも可能性の高い原因は、LLVM と Clang パッケージが Zig と異なる C++ コンパイラでビルドされていることです。同じコンパイラが設定されていることを確認して、再度 emerge してください。
warning: Encountered error: UnexpectedEndOfFile, falling back to default ABI and dynamic linker.
Zig compiler relies on the presence of /usr/bin/env for choosing libc when targeting host machine. It checks whether /usr/bin/env contains actual ELF file or is a symlink/shebang to another executable, and retries this operation recursively until found. Then, it tries to extract information about libc from this ELF file. If this message occurs, it means that Zig failed to detect it properly, and will choose default libc and ABI instead (e.g. musl on Linux). The workaround is by using the following args to the command:
user $
zig build -Dtarget=<target>
または:
user $
zig build-exe -target <target>
Where target
is the desired target, such as native-native-gnu
or native-native-musl
. For more information about target format see ZIG_MCPU and ZIG_TARGET section.
削除
unmerge
root #
emerge --ask --depclean --verbose dev-lang/zig
関連項目
- Clang
- Rust — a general-purpose, multi-paradigm, compiled, programming language.
- Go — an open source, statically typed, compiled programming language
外部資料
- Official list of the community spaces
- Official list of community projects and awesome-zig list
- Ziglings — learn Zig by fixing tiny broken programs, inspired by Rustlings
- Differences between zig cc and clang
参照
- ↑ Integration with C libraries without FFI/bindings、Zig's Overview。2023 年 2 月 19 日取得。
- ↑ Andrew Kelley.
zig cc
: Powerful Drop-In Replacement for GCC/Clang、Andrew Kelley's blog。2020 年 3 月 24 日。2023 年 2 月 19 日取得。 - ↑ Aro — a C compiler written in Zig。2023 年 2 月 19 日取得。
- ↑ 0.10.0 Release Notes、2023 年 2 月 26 日取得。
- ↑ Linux Kernel Version Support、2023 年 1 月 26 日取得。
- ↑ https://github.com/ziglang/zig/blob/b57081f039bd3f8f82210e8896e336e3c3a6869b/src/Compilation.zig#L4416-L4444