Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zigfetch): support fetch package from git #33

Merged
merged 9 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ targets=(
"aarch64-linux"
"x86_64-linux"
"x86-linux"
"aarch64-macos"
# This target is built on CI directly.
# "aarch64-macos"
"x86_64-macos"
"x86_64-windows"
)
Expand All @@ -39,7 +40,7 @@ for target in "${targets[@]}"; do
zig build -Doptimize=ReleaseSafe -p ${dst_dir} \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
else
zig build -Dskip_zigfetch=true -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
zig build -Dskip-zigfetch=true -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
fi

Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ jobs:
zig build test
zig build
find zig-out
- name: zigfetch compare
if: matrix.os == 'macos-latest'
run: |
bash .github/zigfetch.sh

cross-compile:
timeout-minutes: 10
Expand All @@ -72,4 +76,4 @@ jobs:
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
- name: Build
run: |
zig build -Dtarget=${{ matrix.targets }} -Dskip_zigfetch=true
zig build -Dtarget=${{ matrix.targets }} -Dskip-zigfetch=true
15 changes: 15 additions & 0 deletions .github/zigfetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)

pkg=https://github.com/jiacai2050/zig-curl/archive/c8e2f43f8f042f52373c86043ec16b0f2c3388a2.tar.gz

zig fetch --debug-hash "${pkg}"
"${script_dir}/../zig-out/bin/zigfetch" "${pkg}"

actual=$("${script_dir}/../zig-out/bin/zigfetch" "${pkg}" 2>&1 | tail -1)
expected="1220e9b279355ce92cd217684a2449bd8024274eb3fc09a576deb33ca1733b9f0a1f"
if [ "${actual}" != "${expected}" ];then
echo "Expected: ${expected}, actual:${actual}"
exit 1
fi
35 changes: 28 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const macos_private_framework = "/Applications/Xcode.app/Contents/Developer/Plat
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const skip_zigfetch = b.option(bool, "skip_zigfetch", "Skip zig fetch") orelse false;
const skip_zigfetch = b.option(bool, "skip-zigfetch", "Skip zig fetch") orelse false;
const vendor_libcurl = b.option(bool, "vendor-libcurl", "Static link libcurl") orelse false;
var all_tests = std.ArrayList(*Build.Step).init(b.allocator);

try addModules(b, target, &all_tests);
try buildBinaries(b, optimize, target, &all_tests, skip_zigfetch);
try buildBinaries(b, optimize, target, &all_tests, skip_zigfetch, vendor_libcurl);
try buildExamples(b, optimize, target, &all_tests);

const test_all_step = b.step("test", "Run all tests");
Expand Down Expand Up @@ -87,7 +88,7 @@ fn buildExamples(
"simargs-demo",
"pretty-table-demo",
}) |name| {
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests, false);
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests, false, false);
}
}

Expand All @@ -97,6 +98,7 @@ fn buildBinaries(
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
skip_zigfetch: bool,
vendor_libcurl: bool,
) !void {
inline for (.{
"zigfetch",
Expand All @@ -109,7 +111,15 @@ fn buildBinaries(
"repeat",
"tcp-proxy",
}) |name| {
try buildBinary(b, .{ .bin = name }, optimize, target, all_tests, skip_zigfetch);
try buildBinary(
b,
.{ .bin = name },
optimize,
target,
all_tests,
skip_zigfetch,
vendor_libcurl,
);
}

// TODO: move util out of `bin`
Expand All @@ -123,8 +133,16 @@ fn buildBinary(
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
skip_zigfetch: bool,
vendor_libcurl: bool,
) !void {
if (makeCompileStep(b, source, optimize, target, skip_zigfetch)) |exe| {
if (makeCompileStep(
b,
source,
optimize,
target,
skip_zigfetch,
vendor_libcurl,
)) |exe| {
var deps = b.modules.iterator();
while (deps.next()) |dep| {
exe.root_module.addImport(dep.key_ptr.*, dep.value_ptr.*);
Expand Down Expand Up @@ -168,6 +186,7 @@ fn makeCompileStep(
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
skip_zigfetch: bool,
vendor_libcurl: bool,
) ?*Build.Step.Compile {
const name = comptime source.name();
const path = comptime source.path();
Expand Down Expand Up @@ -206,9 +225,11 @@ fn makeCompileStep(
return null;
}
if (host_os == .linux or host_os == .macos) {
const dep_curl = b.dependency("curl", .{ .link_vendor = false });
const dep_curl = b.dependency("curl", .{ .link_vendor = vendor_libcurl });
if (!vendor_libcurl) {
exe.linkSystemLibrary("curl");
}
exe.root_module.addImport("curl", dep_curl.module("curl"));
exe.linkSystemLibrary("curl");
exe.linkLibC();
} else {
return null;
Expand Down
31 changes: 3 additions & 28 deletions docs/content/_index.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#+TITLE: Zigcli
#+TITLE: Introduction
#+DATE: 2023-10-21T12:09:48+0800
#+LASTMOD: 2025-01-01T19:29:01+0800
#+LASTMOD: 2025-01-02T23:22:49+0800
#+TYPE: docs
#+author: Jiacai Liu

Expand All @@ -18,32 +18,7 @@ Official website: https://zigcli.liujiacai.net/
It can be imported as [[https://zigcli.liujiacai.net/packages/][Zig packages]] or used directly as [[https://zigcli.liujiacai.net/programs/][command line programs]].

* Install
** Packages
#+begin_src bash
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
#+end_src

#+RESULTS:

Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:

#+begin_src zig
const zigcli = b.dependency("zigcli", .{});

// Currently zigcli provide two packages.
exe.root_module.addImport("simargs", zigcli.module("simargs"));
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
#+end_src
** CLI Programs
The latest pre-built binaries are available on the [[https://github.com/jiacai2050/zigcli/releases][release page]] or you can build it from source.

#+begin_src bash
git clone https://github.com/jiacai2050/zigcli.git
#+end_src
Then build with zig 0.13.0
#+begin_src bash
make build
#+end_src
See [[https://zigcli.liujiacai.net/install][INSTALL]] page.
* Who's Using
If you're using =zigcli=, and would like to be added here, welcome to [[https://github.com/jiacai2050/zigcli/pulls][open a PR]].

Expand Down
33 changes: 33 additions & 0 deletions docs/content/install.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#+TITLE: Install
#+DATE: 2025-01-02T23:20:23+0800
#+LASTMOD: 2025-01-02T23:21:04+0800
#+TYPE: docs
#+WEIGHT: 10
#+AUTHOR: Jiacai Liu

** Packages
#+begin_src bash
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
#+end_src

#+RESULTS:

Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:

#+begin_src zig
const zigcli = b.dependency("zigcli", .{});

// Currently zigcli provide two packages.
exe.root_module.addImport("simargs", zigcli.module("simargs"));
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
#+end_src
** CLI Programs
The latest pre-built binaries are available on the [[https://github.com/jiacai2050/zigcli/releases][release page]] or you can build it from source.

#+begin_src bash
git clone https://github.com/jiacai2050/zigcli.git
#+end_src
Then build with zig 0.13.0
#+begin_src bash
make build
#+end_src
4 changes: 2 additions & 2 deletions docs/content/programs/zigfetch.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#+TITLE: zigfetch
#+DATE: 2025-01-01T18:01:47+0800
#+LASTMOD: 2025-01-01T19:08:03+0800
#+LASTMOD: 2025-01-02T23:02:00+0800
#+TYPE: docs
#+DESCRIPTION: Fetch zig package, baked by libcurl.

Expand All @@ -12,7 +12,7 @@ Since the HTTP support within Zig's standard library isn't currently stable, thi

This poses a significant challenge for Chinese developers owing to [[https://en.wikipedia.org/wiki/Great_Firewall][the Great Firewall]].

=zigfetch= is baked by libcurl, so [[https://curl.se/libcurl/c/libcurl-env.html][http_proxy/https_proxy]] env vars work as expected.
So =zigfetch= is born, it's baked by libcurl, so [[https://curl.se/libcurl/c/libcurl-env.html][http_proxy/https_proxy]] env vars work as expected.

#+begin_src bash :results verbatim :exports result :dir ../../..
./zig-out/bin/zigfetch --help
Expand Down
10 changes: 8 additions & 2 deletions src/bin/pkg/Manifest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,14 @@ const Parse = struct {
buf.* = buf_managed.moveToUnmanaged();
switch (try result) {
.success => {},
.failure => |err| {
std.log.err("parse str lit failed, err:{any}, token:{any}, bytes:{s}, offset:{d}", .{ err, token, bytes, offset });
.failure => |e| {
std.log.err("parse str lit failed, err:{any}, token:{any}, bytes:{any}, offset:{any}", .{
e,
token,
bytes,
offset,
});
return error.ParseFailure;
},
}
}
Expand Down
Loading
Loading