Zig

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Zig and the translation is 80% complete.
Other languages:

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

doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
llvm Build with LLVM backend and extensions enabled.

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/zigExamples 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

テキストエディタサポート

上流によるツールの一覧を参照してください。現時点では、これらはどれもパッケージ化されていません。

フリースタンディング

トラブルシューティング

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 — a C/C++/Objective-C/C++, CUDA, and RenderScript language front-end for the LLVM project
  • 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

参照