Total Pageviews

Saturday, 12 February 2022

zig, A general-purpose programming language

 

A general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

https://ziglang.org/

Documentation

If you are looking at this README file in a source tree, please refer to the Release Notes, Language Reference, or Standard Library Documentation corresponding to the version of Zig that you are using by following the appropriate link on the download page.

Otherwise, you're looking at a release of Zig, and you can find documentation here:

  • doc/langref.html
  • doc/std/index.html

Installation

A Zig installation is composed of two things:

  1. The Zig executable
  2. The lib/ directory

At runtime, the executable searches up the file system for the lib/ directory, relative to itself:

  • lib/
  • lib/zig/
  • ../lib/
  • ../lib/zig/
  • (and so on)

In other words, you can unpack a release of Zig anywhere, and then begin using it immediately. There is no need to install it globally, although this mechanism supports that use case too (i.e. /usr/bin/zig and /usr/lib/zig/).

Building from Source

Ensure you have the required dependencies:

  • CMake >= 3.5
  • System C/C++ Toolchain
  • LLVM, Clang, LLD development libraries == 17.x

Then it is the standard CMake build process:

mkdir build
cd build
cmake ..
make install

For more options, tips, and troubleshooting, please see the Building Zig From Source page on the wiki.

Building from Source without LLVM

In this case, the only system dependency is a C compiler.

cc -o bootstrap bootstrap.c
./bootstrap

This produces a zig2 executable in the current working directory. This is a "stage2" build of the compiler, without LLVM extensions, and is therefore lacking these features:

However, a compiler built this way does provide a C backend, which may be useful for creating system packages of Zig projects using the system C toolchain. In such case, LLVM is not needed!

Contributing

Donate monthly.

Zig is Free and Open Source Software. We welcome bug reports and patches from everyone. However, keep in mind that Zig governance is BDFN (Benevolent Dictator For Now) which means that Andrew Kelley has final say on the design and implementation of everything.

One of the best ways you can contribute to Zig is to start using it for an open-source personal project.

This leads to discovering bugs and helps flesh out use cases, which lead to further design iterations of Zig. Importantly, each issue found this way comes with real world motivations, making it straightforward to explain the reasoning behind proposals and feature requests.

You will be taken much more seriously on the issue tracker if you have a personal project that uses Zig.

The issue label Contributor Friendly exists to help you find issues that are limited in scope and/or knowledge of Zig internals.

Please note that issues labeled Proposal but do not also have the Accepted label are still under consideration, and efforts to implement such a proposal have a high risk of being wasted. If you are interested in a proposal which is still under consideration, please express your interest in the issue tracker, providing extra insights and considerations that others have not yet expressed. The most highly regarded argument in such a discussion is a real world use case.

For more tips, please see the Contributing page on the wiki.

Community

The Zig community is decentralized. Anyone is free to start and maintain their own space for Zig users to gather. There is no concept of "official" or "unofficial". Each gathering place has its own moderators and rules. Users are encouraged to be aware of the social structures of the spaces they inhabit, and work purposefully to facilitate spaces that align with their values.

Please see the Community wiki page for a public listing of social spaces.

from https://github.com/ziglang/zig

 ------------------------------------------------

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

https://ziglang.org/

A general-purpose programming language and toolchain for maintaining robustoptimal, and reusable software.

Resources

Installation

from https://github.com/ziglang/zig
----------------------------------------------
 
https://github.com/ziglang/zig/wiki/Building-Zig-From-Source
----------------------------------------------------------------------------
 

A toolkit for building command lines programs in Zig.

zigcli.liujiacai.net/

Zigcli

https://img.shields.io/github/stars/jiacai2050/zigcli.svg https://img.shields.io/github/license/jiacai2050/zigcli.svg https://github.com/jiacai2050/loc/actions/workflows/CI.yml/badge.svg https://github.com/jiacai2050/loc/actions/workflows/binary.yml/badge.svg https://img.shields.io/badge/zig%20version-0.12.0-blue.svg

Zigcli is a toolkit for building command lines programs in Zig.

This package provides:

Modules
Zig modules to help developers build their awesome cli programs. Currently there are:
  • simargs, which is used to parse arguments
  • pretty-table, which is used to print aligned and formatted tables
Programs
Practical command line programs which can be used directly. Currently there are:
  • loc, lines of code.
  • tree, list contents of directories in a tree-like format.
  • yes, output a string repeatedly until killed.
  • repeat, repeat a command until it succeeds.
  • pidof, like pidof, but for macOS.
  • night-shift, control Night Shift in macOS.
  • dark-mode, control dark mode in macOS.

Install

Programs

Prebuilt binaries can be found in CI’s artifacts, or you can build from source:

git clone https://github.com/jiacai2050/zigcli

Then build with

make build

To build zigcli locally, Zig master is required, which can be downloaded here.

from  https://github.com/jiacai2050/zigcli

(https://codeberg.org/jiacai2050/zigcli)

------------------------------------------------------

zigcc 

A util script that aims to ease use zig cc to compile C/C++/Rust/Go programs.

Why

In most cases, we can use following command to use Zig for compile

CC='zig cc' CXX='zig c++' ...

However in real world, there are many issues this way, such as:

So this project was born, it will

  • Convert target between Zig and Rust/Go
  • Ignore link args when zig cc throw errors, hopefully this will make compile successfully, WIP.

Install

Download zigcc file, give it executable permission, and create two soft links zigcxx, zigcargo to it.

curl -Lo zigcc https://github.com/jiacai2050/zig-cc/raw/main/zigcc
chmod +x zigcc
ln -s zigcc zigcxx
ln -s zigcc zigcargo

Then use like this

CC=zigcc CXX=zigcxx ...

zigcargo can used to replace cargo, it will automatically set

  • CC CARGO_TARGET_<triple>_LINKER to zigcc
  • CXX to zigcxx

Use in GitHub Action

Adding a step to your workflow like this:

- uses: jiacai2050/zigcc@v1
  with:
    zig-version: master

Then you can invoke zigcc zigcxx zigcargo in following steps.

Config

There some are env variable to config zigcc:

  • ZIGCC_FLAGS, space separated flags, pass to zig cc. An example is set this to -fno-sanitize=undefined to disable sanitize since they may break your programs. See Catching undefined behavior with zig cc
  • ZIGCC_BLACKLIST_FLAGS, space separated flags, used to filter flags zig cc don’t support, such as -Wl,-dylib otherwise you could see errors below
    = note: error: unsupported linker arg: -dylib
        
ZIGCC_LOG Set to 1 enable verbose logs.

from https://github.com/jiacai2050/zigcc 

(https://codeberg.org/jiacai2050/zigcc)

---------------------------------------------------

https://github.com/ziglibs/repository

--------------------------------------------------

zig学习资料

由于 Zig 目前还处于快速迭代,因此最权威的资料无疑是官方的 Zig Language Reference,遇到语言的细节问题,基本都可以在这里找到答案。其次是社区的一些高质量教程,例如:

学习 Zig
该系列教程最初由 Karl Seguin 编写,该教程行文流畅,讲述的脉络由浅入深,深入浅出,是入门 Zig 非常不错的选择
Zig 语言圣经
一套完整的 Zig 教程
ziglings/exercises
Learn the Zig programming language by fixing tiny broken programs.
Zig Cookbook
A collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks
zigcc/zig-idioms
Common idioms used in Zig
zigcc/awesome-zig
A collection of some awesome public Zig programming language projects.
 
from https://ziglang.cc/ 
(https://github.com/zigcc,    Zig Chinese Community, Zig 语言中文社区
---------------------------------------------------------------------------------

How build systems run

Different with Rust, there is no rustc-like zigc to compile build.zig, then how does build.zig works?

The answer is sub-process. When you build a fresh project, zig build will first compile build.zig to a binary, then run this binary to execute specific chosen steps.

The build binary sub-process to other Zig sub-commands to finish their jobs, such as zig build-exe for CompileStep.

build_runner.zig contains the main entry for the build binary, build.zig is imported using const root = @import("@build"), it requires four arguments when start:

  • Zig executable
  • directory where build.zig locates
  • Local cache directory(optional)
  • Global cache directory(optional)

We can manually invoke the build binary like this:


$ find . -name build
./zig-cache/o/0c7d271c608c27670403b31835132c7d/build
$ ./zig-cache/o/0c7d271c608c27670403b31835132c7d/build $(which zig) $(pwd) .cache .glocal-cache
$ zig-out/bin/demo
All your codebase are belong to us.
Run `zig build test` to run the tests.

So when you have bugs in build.zig, you first need to find where the build binary is, then run it in gdb/lldb with args above.

Conclusion

A fully featured build system is necessary to keep developers productive, but flexibility come at a cost of misuse, so design a good build system is hard.

Zig's build system is based on the language itself, developers don't need to learn a new language to write their build(CMake, I'm looking at you!). And Zig provides a declarative API to construct steps' DAG, lots of pre-defined steps to finish common task, powerful as will as practical.

Besides build system, package manager in Zig is in rapid development. With those, developer can build old/complex C/C++ projects in just one command: zig build. Some early adopters:

  • pcre2, Perl-Compatible Regular Expressions
  • llama.cpp, Port of Facebook's LLaMA model in C/C++
  • raylib, A simple and easy-to-use library to enjoy videogames programming

Discussions on Lobsters and Reddit

 

 
 

1 comment: