Zig

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Zig and the translation is 48% complete.
Outdated translations are marked like this.
Other languages:

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software. Zig aims to replace C as the better language and can use or export C functions without FFI[1] and compile code in C and C++, using libclang.[2] Upstream also works on their own C compiler[3] so that these functions will be available without libclang.

Installation

Voraussetzungen

Warnung
Compiling Zig 0.10.1 requires up to 9.6 GiB of RAM.[4]

Zig supports at least Linux kernel 3.16+.[5] If building from source, it must be built with same compiler as LLVM/Clang, or the user will get in trouble (see relevant section in Troubleshooting below). Zig does not support custom environment variables like CFLAGS when building from source [6] and the ebuild ignores it.

USE-Flags

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

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

Hinweis
Replace "dev-lang/zig" with "dev-lang/zig-bin" if you want to use binary package (if available for your architecture).

Packages are currently not stabilized on any arch and keyworded as testing on a small subset of arches, so the user need to accept testing (~arch, replace with the system's arch) keyword for the latter:

root #echo "dev-lang/zig ~arch" >> /etc/portage/package.accept_keywords

Then emerge the package:

root #emerge --ask dev-lang/zig

Konfiguration

ZIG_MCPU und ZIG_TARGET

To adjust for which target and CPU dev-lang/zig will be built for, there are variables ZIG_MCPU and ZIG_TARGET (that will be passed as -target and -mcpu arguments to zig:

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

Setting a default slot

The eselect command can be used to manipulate a /usr/bin/zig symlink.

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

Environment variables

Hinweis
Unfortunately, there is no way to set other args (such as target or release/optimize mode) with environment variables, so the user needs to specify them in args.

Command args have higher precedence over the following environment variables:

  • 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

Verwendung

General usage

Warnung
Many programs and libraries written in Zig leverages comptime possibilities of the language. More info is available in language reference, in short: during compilation Zig can run arbitrary code in the source files (build.zig, standard library, project itself etc.), so please be cautious.
{{Invocation|zig --help|output=
Usage: zig [command] [options]
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.

Build-Modi

Zig has 4 "build modes" (or "optimize modes" in recent versions):
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)[7] -D_DEBUG -O0 -DNDEBUG -O2 -O2 -D_FORTIFY_SOURCE=2 -DNDEBUG -Os
Hinweis
Safety checks at runtime can be overridden per-scope/block by setting @setRuntimeSafety. Safety cheks at compile-time are always enabled.

LSP-Unterstützung

Unofficial language server is available as dev-zig/zls.
To install, do this guide. Then do this, but replace the example package with dev-zig/zls.

Texteditor-Syntax

Siehe diese Liste der Werkzeuge von Upstream.

Freestanding

Troubleshooting

"undefined reference to ..." during emerging

The most possible reason is that the LLVM and Clang packages were built with other C++ compiler than Zig. Ensure that the user set the same compiler and emerge again.

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>

Oder:

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.

Entfernung

Unmergen

root #emerge --ask --depclean --verbose dev-lang/zig

Siehe auch

* Clang — a C/C++/Objective-C compiler using LLVM as a backend and optimizer. * Rust — a general-purpose, multi-paradigm, compiled, programming language. * Go — an open source, statically typed, compiled programming language

Externe Ressourcen

* Offizielle Liste von Community-Spaces * Offizielle Projektliste von Community-Projekten und die awesome-zig Liste * Ziglings — Zig lernen, indem man kleine fehlerhafte Programme repariert, inspiriert von Rustlings * Unterschiede zwischen zig cc und clang

Verweise