Skip to content

Commit

Permalink
use lazy dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed May 12, 2024
1 parent 2b12a41 commit 13aa092
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 22 deletions.
48 changes: 47 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
- '**.yml'

jobs:
test-linux:
test-vendor:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -56,3 +56,49 @@ jobs:
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${bin}
done
test-dynamic:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
zig-version: [master, 0.12.0]
steps:
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: ${{ matrix.zig-version }}
- uses: actions/setup-go@v5
with:
go-version: 'stable'
- name: Set Environment Variables
run: |
echo "ZIG_ARGS='-Dlink_vendor=false'" >> $GITHUB_ENV
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
- name: Run tests
run: |
make test
- name: Run examples
run: |
SERVER=/tmp/echo-server
go build -o ${SERVER} server/main.go
${SERVER} &
sleep 10
make run
- name: Install deps
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt update && sudo apt install -y valgrind
- name: Memory leak detect
if: matrix.os == 'ubuntu-latest'
run: |
zig build -Dcpu=baseline --verbose
BINS=("./zig-out/bin/basic" "./zig-out/bin/advanced")
for bin in ${BINS[@]}; do
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${bin}
done
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

ARGS = ${ZIG_ARGS}

prepare:
./libs/update.sh

Expand All @@ -9,12 +11,12 @@ serve:
cd server && go run main.go

run:
zig build run-basic -freference-trace
zig build run-advanced -freference-trace
zig build run-multi -freference-trace
zig build run-basic -freference-trace $(ARGS)
zig build run-advanced -freference-trace $(ARGS)
zig build run-multi -freference-trace $(ARGS)

test:
zig build test
zig build test $(ARGS)

docs:
if [ ! -d zig-out ]; then mkdir zig-out; fi
Expand Down
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#+TITLE: zig-curl
#+DATE: 2023-09-16T23:16:15+0800
#+LASTMOD: 2024-05-05T14:38:40+0800
#+LASTMOD: 2024-05-12T13:20:36+0800
#+OPTIONS: toc:nil num:nil
#+STARTUP: content

Expand All @@ -9,7 +9,7 @@
[[https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml/badge.svg]]
[[https://ci.codeberg.org/repos/13257][https://ci.codeberg.org/api/badges/13257/status.svg]]

Zig bindings to [[https://curl.haxx.se/libcurl/][libcurl]], a free and easy-to-use client-side URL transfer library.
Zig bindings for [[https://curl.haxx.se/libcurl/][libcurl]], a free and easy-to-use client-side URL transfer library.

#+begin_quote
This package is in early stage, although the core functionality works right now, the API is still subject to changes.
Expand Down
18 changes: 12 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ pub fn build(b: *Build) void {

var libcurl: ?*Step.Compile = null;
if (link_vendor) {
libcurl = buildLibcurl(b, target, optimize);
module.linkLibrary(libcurl.?);
if (buildLibcurl(b, target, optimize)) |v| {
libcurl = v;
module.linkLibrary(v);
} else {
return;
}
}

try addExample(b, "basic", module, libcurl, target, optimize);
Expand All @@ -31,6 +35,7 @@ pub fn build(b: *Build) void {
.root_source_file = .{ .path = "src/root.zig" },
.target = target,
.optimize = optimize,
.link_libc = true,
});

if (libcurl) |lib| {
Expand All @@ -44,10 +49,10 @@ pub fn build(b: *Build) void {
test_step.dependOn(&run_main_tests.step);
}

fn buildLibcurl(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Step.Compile {
const tls = @import("libs/mbedtls.zig").create(b, target, optimize);
const zlib = @import("libs/zlib.zig").create(b, target, optimize);
const curl = @import("libs/curl.zig").create(b, target, optimize);
fn buildLibcurl(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*Step.Compile {
const tls = @import("libs/mbedtls.zig").create(b, target, optimize) orelse return null;
const zlib = @import("libs/zlib.zig").create(b, target, optimize) orelse return null;
const curl = @import("libs/curl.zig").create(b, target, optimize) orelse return null;
curl.linkLibrary(tls);
curl.linkLibrary(zlib);
b.installArtifact(curl);
Expand All @@ -67,6 +72,7 @@ fn addExample(
.root_source_file = b.path("examples/" ++ name ++ ".zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});

b.installArtifact(exe);
Expand Down
3 changes: 3 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
// https://github.com/curl/curl/releases/tag/curl-8_5_0
.url = "https://github.com/curl/curl/releases/download/curl-8_5_0/curl-8.5.0.tar.gz",
.hash = "12206e97053bf43e6bd83f2d51eefbac497a05f2eee98868431e9d228a12971e716e",
.lazy = true,
},
// https://github.com/madler/zlib/releases/tag/v1.3
.zlib = .{
.url = "https://github.com/madler/zlib/releases/download/v1.3/zlib-1.3.tar.gz",
.hash = "1220c32f4f9dbcb4445c153b6a5bf1e1f697a76f6b690dda9c600357c6b7080af614",
.lazy = true,
},
.mbedtls = .{
.url = "https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/v3.5.1.tar.gz",
.hash = "1220700935566604edf3dd5491dab97c29571d1b07830e626ed31109811198fdc941",
.lazy = true,
},
},
}
6 changes: 3 additions & 3 deletions libs/curl.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const std = @import("std");

pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "curl",
.target = target,
.optimize = optimize,
.link_libc = true,
});
const curl_dep = b.dependency("curl", .{
const curl_dep = b.lazyDependency("curl", .{
.target = target,
.optimize = optimize,
});
}) orelse return null;

inline for (srcs) |s| {
lib.addCSourceFile(.{
Expand Down
6 changes: 3 additions & 3 deletions libs/mbedtls.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const std = @import("std");
const ResolvedTarget = std.Build.ResolvedTarget;

pub fn create(b: *std.Build, target: ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
pub fn create(b: *std.Build, target: ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "mbedtls",
.target = target,
.optimize = optimize,
.link_libc = true,
});

const mbedtls_dep = b.dependency("mbedtls", .{
const mbedtls_dep = b.lazyDependency("mbedtls", .{
.target = target,
.optimize = optimize,
});
}) orelse return null;
inline for (srcs) |s| {
lib.addCSourceFile(.{ .file = mbedtls_dep.path(s), .flags = &.{"-std=c99"} });
}
Expand Down
6 changes: 3 additions & 3 deletions libs/zlib.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const std = @import("std");

pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "z",
.target = target,
.optimize = optimize,
.link_libc = true,
});
const zlib_dep = b.dependency("zlib", .{
const zlib_dep = b.lazyDependency("zlib", .{
.target = target,
.optimize = optimize,
});
}) orelse return null;

inline for (srcs) |s| {
lib.addCSourceFile(.{
Expand Down

0 comments on commit 13aa092

Please sign in to comment.