From 72dbc9b4c7b773afad26ed98522758d8cea0f39a Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 19:31:49 +0200 Subject: ci: reenable windows --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index adc7db1..d0d1666 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,7 +29,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, macos-12] + os: [ubuntu-latest, windows-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 @@ -40,7 +40,6 @@ jobs: version: master - uses: actions/cache@v4 - if: ${{ matrix.os != 'windows-latest' }} with: path: | zig-cache -- cgit v1.2.3 From 9f0d2ed11dacbade3343cbe49e8f159f1d8b67f5 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 19:09:23 +0200 Subject: c: add a workaround for SQLITE_TRANSIENT being mistranslated See https://github.com/ziglang/zig/issues/15893 zig's translate-c creates an invalid type for SQLITE_TRANSIENT on some architectures (aarch64, riscv64 and others). We can work around this by making a C function that returns -1 cast to the proper destructor type and use that from zig (thanks https://github.com/Cloudef for mentioning this in this comment: https://github.com/ziglang/zig/issues/15893#issuecomment-1925092582) --- c.zig | 1 + c/loadable_extension.zig | 1 + c/workaround.c | 5 +++++ c/workaround.h | 3 +++ 4 files changed, 10 insertions(+) create mode 100644 c/workaround.c create mode 100644 c/workaround.h diff --git a/c.zig b/c.zig index 4589aef..3a43106 100644 --- a/c.zig +++ b/c.zig @@ -5,6 +5,7 @@ pub const c = if (@hasDecl(root, "loadable_extension")) else @cImport({ @cInclude("sqlite3.h"); + @cInclude("workaround.h"); }); // versionGreaterThanOrEqualTo returns true if the SQLite version is >= to the major.minor.patch provided. diff --git a/c/loadable_extension.zig b/c/loadable_extension.zig index 4b27534..fdfe15e 100644 --- a/c/loadable_extension.zig +++ b/c/loadable_extension.zig @@ -1,5 +1,6 @@ const c = @cImport({ @cInclude("loadable-ext-sqlite3ext.h"); + @cInclude("workaround.h"); }); pub usingnamespace c; diff --git a/c/workaround.c b/c/workaround.c new file mode 100644 index 0000000..592d33d --- /dev/null +++ b/c/workaround.c @@ -0,0 +1,5 @@ +#include "workaround.h" + +my_sqlite3_destructor_type sqliteTransientAsDestructor() { + return (my_sqlite3_destructor_type)-1; +} diff --git a/c/workaround.h b/c/workaround.h new file mode 100644 index 0000000..ae243b8 --- /dev/null +++ b/c/workaround.h @@ -0,0 +1,3 @@ +typedef void (*my_sqlite3_destructor_type)(void *); + +my_sqlite3_destructor_type sqliteTransientAsDestructor(); -- cgit v1.2.3 From ad4ded08b4b3d6d2ace6bb5e54e6ba699094ff3c Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 19:09:33 +0200 Subject: build: add the workaround C file --- build.zig | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/build.zig b/build.zig index cd617f7..9beb65d 100644 --- a/build.zig +++ b/build.zig @@ -267,6 +267,8 @@ pub fn build(b: *std.Build) !void { const target = b.resolveTargetQuery(query); const optimize = b.standardOptimizeOption(.{}); + const c_flags = &[_][]const u8{"-std=c99"}; + const sqlite_lib = b.addStaticLibrary(.{ .name = "sqlite", .target = target, @@ -274,9 +276,12 @@ pub fn build(b: *std.Build) !void { }); sqlite_lib.addIncludePath(.{ .path = "c/" }); - sqlite_lib.addCSourceFile(.{ - .file = .{ .path = "c/sqlite3.c" }, - .flags = &[_][]const u8{"-std=c99"}, + sqlite_lib.addCSourceFiles(.{ + .files = &[_][]const u8{ + "c/sqlite3.c", + "c/workaround.c", + }, + .flags = c_flags, }); sqlite_lib.linkLibC(); sqlite_lib.installHeader(.{ .path = "c/sqlite3.h" }, "sqlite3.h"); @@ -345,9 +350,12 @@ pub fn build(b: *std.Build) !void { .target = cross_target, .optimize = optimize, }); - lib.addCSourceFile(.{ - .file = .{ .path = "c/sqlite3.c" }, - .flags = &[_][]const u8{"-std=c99"}, + lib.addCSourceFiles(.{ + .files = &[_][]const u8{ + "c/sqlite3.c", + "c/workaround.c", + }, + .flags = c_flags, }); lib.linkLibC(); sqlite3 = lib; @@ -362,6 +370,7 @@ pub fn build(b: *std.Build) !void { .target = cross_target, .optimize = optimize, }); + lib.addCSourceFile(.{ .file = .{ .path = "c/workaround.c" }, .flags = c_flags }); if (bundled) lib.addIncludePath(.{ .path = "c" }); linkSqlite(lib); @@ -381,10 +390,7 @@ pub fn build(b: *std.Build) !void { .target = getTarget(target, true), .optimize = optimize, }); - lib.addCSourceFile(.{ - .file = .{ .path = "c/sqlite3.c" }, - .flags = &[_][]const u8{"-std=c99"}, - }); + lib.addCSourceFile(.{ .file = .{ .path = "c/sqlite3.c" }, .flags = c_flags }); lib.addIncludePath(.{ .path = "c" }); lib.linkLibC(); -- cgit v1.2.3 From a1c695f5be5178cefaa22aac22dff0480a29e61f Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 19:26:44 +0200 Subject: all: use our workaround function for SQLITE_TRANSIENT --- helpers.zig | 8 ++++---- sqlite.zig | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/helpers.zig b/helpers.zig index 7bb695e..b9a6131 100644 --- a/helpers.zig +++ b/helpers.zig @@ -13,8 +13,8 @@ pub fn setResult(ctx: ?*c.sqlite3_context, result: anytype) void { const ResultType = @TypeOf(result); switch (ResultType) { - Text => c.sqlite3_result_text(ctx, result.data.ptr, @intCast(result.data.len), c.SQLITE_TRANSIENT), - Blob => c.sqlite3_result_blob(ctx, result.data.ptr, @intCast(result.data.len), c.SQLITE_TRANSIENT), + Text => c.sqlite3_result_text(ctx, result.data.ptr, @intCast(result.data.len), c.sqliteTransientAsDestructor()), + Blob => c.sqlite3_result_blob(ctx, result.data.ptr, @intCast(result.data.len), c.sqliteTransientAsDestructor()), else => switch (@typeInfo(ResultType)) { .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) { c.sqlite3_result_int(ctx, result); @@ -26,12 +26,12 @@ pub fn setResult(ctx: ?*c.sqlite3_context, result: anytype) void { .Float => c.sqlite3_result_double(ctx, result), .Bool => c.sqlite3_result_int(ctx, if (result) 1 else 0), .Array => |arr| switch (arr.child) { - u8 => c.sqlite3_result_blob(ctx, &result, arr.len, c.SQLITE_TRANSIENT), + u8 => c.sqlite3_result_blob(ctx, &result, arr.len, c.sqliteTransientAsDestructor()), else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), }, .Pointer => |ptr| switch (ptr.size) { .Slice => switch (ptr.child) { - u8 => c.sqlite3_result_text(ctx, result.ptr, @intCast(result.len), c.SQLITE_TRANSIENT), + u8 => c.sqlite3_result_text(ctx, result.ptr, @intCast(result.len), c.sqliteTransientAsDestructor()), else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), }, else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), diff --git a/sqlite.zig b/sqlite.zig index be46dea..bd11dea 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -1676,7 +1676,7 @@ pub const DynamicStatement = struct { const data: []const u8 = field[0..field.len]; // NOTE(vincent): The array is temporary and must be copied, therefore we use SQLITE_TRANSIENT - const result = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(data.len), c.SQLITE_TRANSIENT); + const result = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(data.len), c.sqliteTransientAsDestructor()); return convertResultToError(result); }, else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)), -- cgit v1.2.3 From 947b4ade92e6a0919aa4a7b845ec674082e7dc51 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:10:41 +0200 Subject: build: rework and fix the CI tests * stop having a global sqlite3 compile step variable * remove the test_lib which doesn't do anything --- build.zig | 61 +++++++++++++++++++++---------------------------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/build.zig b/build.zig index 9beb65d..460df86 100644 --- a/build.zig +++ b/build.zig @@ -4,17 +4,6 @@ const Step = std.Build.Step; const ResolvedTarget = std.Build.ResolvedTarget; const Query = std.Target.Query; -var sqlite3: ?*Step.Compile = null; - -fn linkSqlite(b: *Step.Compile) void { - if (sqlite3) |lib| { - b.linkLibrary(lib); - } else { - b.linkLibC(); - b.linkSystemLibrary("sqlite3"); - } -} - fn getTarget(original_target: ResolvedTarget, bundled: bool) ResolvedTarget { if (bundled) { var tmp = original_target; @@ -335,6 +324,20 @@ pub fn build(b: *std.Build) !void { single_threaded_txt, }); + const test_sqlite_lib = b.addStaticLibrary(.{ + .name = "sqlite", + .target = cross_target, + .optimize = optimize, + }); + test_sqlite_lib.addCSourceFiles(.{ + .files = &[_][]const u8{ + "c/sqlite3.c", + "c/workaround.c", + }, + .flags = c_flags, + }); + test_sqlite_lib.linkLibC(); + const tests = b.addTest(.{ .name = test_name, .target = cross_target, @@ -342,44 +345,22 @@ pub fn build(b: *std.Build) !void { .root_source_file = .{ .path = "sqlite.zig" }, .single_threaded = test_target.single_threaded, }); - const run_tests = b.addRunArtifact(tests); - + tests.addIncludePath(.{ .path = "c" }); if (bundled) { - const lib = b.addStaticLibrary(.{ - .name = "sqlite", - .target = cross_target, - .optimize = optimize, - }); - lib.addCSourceFiles(.{ - .files = &[_][]const u8{ - "c/sqlite3.c", - "c/workaround.c", - }, - .flags = c_flags, - }); - lib.linkLibC(); - sqlite3 = lib; + tests.linkLibrary(test_sqlite_lib); + } else { + tests.linkLibC(); + tests.addCSourceFile(.{ .file = .{ .path = "c/workaround.c" }, .flags = c_flags }); + tests.linkSystemLibrary("sqlite3"); } - if (bundled) tests.addIncludePath(.{ .path = "c" }); - linkSqlite(tests); - - const lib = b.addStaticLibrary(.{ - .name = "zig-sqlite", - .root_source_file = .{ .path = "sqlite.zig" }, - .target = cross_target, - .optimize = optimize, - }); - lib.addCSourceFile(.{ .file = .{ .path = "c/workaround.c" }, .flags = c_flags }); - if (bundled) lib.addIncludePath(.{ .path = "c" }); - linkSqlite(lib); - const tests_options = b.addOptions(); tests.root_module.addImport("build_options", tests_options.createModule()); tests_options.addOption(bool, "in_memory", in_memory); tests_options.addOption(?[]const u8, "dbfile", dbfile); + const run_tests = b.addRunArtifact(tests); test_step.dependOn(&run_tests.step); } -- cgit v1.2.3 From 5890065d754ecaca9533ebfa46e72280576510b8 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:19:40 +0200 Subject: build: stop building with the system library in CI GitHub Actions workflows run on Ubuntu 22.04 which provides sqlite 3.37, we depend on at least 3.38 so we can't test this. --- build.zig | 4 ---- 1 file changed, 4 deletions(-) diff --git a/build.zig b/build.zig index 460df86..de81563 100644 --- a/build.zig +++ b/build.zig @@ -36,10 +36,6 @@ const ci_targets = switch (builtin.target.cpu.arch) { .x86_64 => switch (builtin.target.os.tag) { .linux => [_]TestTarget{ // Targets linux but other CPU archs. - TestTarget{ - .query = .{}, - .bundled = false, - }, TestTarget{ .query = .{ .cpu_arch = .x86_64, -- cgit v1.2.3 From e9ad0f005c7da2b2aa2f012e84733bf9aea2340b Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:23:17 +0200 Subject: build: revamp the CI targets --- build.zig | 63 +++++++++------------------------------------------------------ 1 file changed, 9 insertions(+), 54 deletions(-) diff --git a/build.zig b/build.zig index de81563..af6ece1 100644 --- a/build.zig +++ b/build.zig @@ -35,66 +35,21 @@ const TestTarget = struct { const ci_targets = switch (builtin.target.cpu.arch) { .x86_64 => switch (builtin.target.os.tag) { .linux => [_]TestTarget{ - // Targets linux but other CPU archs. - TestTarget{ - .query = .{ - .cpu_arch = .x86_64, - .abi = .musl, - }, - .bundled = true, - }, - TestTarget{ - .query = .{ - .cpu_arch = .x86, - .abi = .musl, - }, - .bundled = true, - }, + TestTarget{ .query = .{ .cpu_arch = .x86_64, .abi = .musl }, .bundled = true }, + TestTarget{ .query = .{ .cpu_arch = .x86, .abi = .musl }, .bundled = true }, + TestTarget{ .query = .{ .cpu_arch = .aarch64, .abi = .musl }, .bundled = true }, }, .windows => [_]TestTarget{ - TestTarget{ - .query = .{ - .cpu_arch = .x86_64, - .abi = .gnu, - }, - .bundled = true, - }, - TestTarget{ - .query = .{ - .cpu_arch = .x86, - .abi = .gnu, - }, - .bundled = true, - }, + TestTarget{ .query = .{ .cpu_arch = .x86_64, .abi = .gnu }, .bundled = true }, + TestTarget{ .query = .{ .cpu_arch = .x86, .abi = .gnu }, .bundled = true }, }, .macos => [_]TestTarget{ - TestTarget{ - .query = .{ - .cpu_arch = .x86_64, - }, - .bundled = true, - }, - // TODO(vincent): this fails for some reason - // TestTarget{ - // .query =.{ - // .cpu_arch = .aarch64, - // }, - // .bundled = true, - // }, - }, - else => [_]TestTarget{ - TestTarget{ - .query = .{}, - .bundled = false, - }, - }, - }, - else => [_]TestTarget{ - TestTarget{ - .query = .{}, - .bundled = false, + TestTarget{ .query = .{ .cpu_arch = .x86_64 }, .bundled = true }, + TestTarget{ .query = .{ .cpu_arch = .aarch64 }, .bundled = true }, }, + else => unreachable, }, + else => unreachable, }; const all_test_targets = switch (builtin.target.cpu.arch) { -- cgit v1.2.3 From b6b37394a392d1a47ca00e0c389879ee5ff859f6 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:25:43 +0200 Subject: ci: run with qemu and wine --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d0d1666..f6085ee 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -48,7 +48,7 @@ jobs: restore-keys: ${{ runner.os }}-${{ matrix.os }}-zig- - name: Run Tests in memory - run: zig build test -Dci=true -Din_memory=true --summary all + run: zig build test -Dci=true -Din_memory=true --summary all -fqemu -fwine - name: Build the example zigcrypto loadable extension run: zig build zigcrypto -- cgit v1.2.3 From a5b6dfa610208ababeaa19df711914c638d46315 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:29:53 +0200 Subject: ci: better output --- .github/workflows/main.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f6085ee..3f4ecf3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,14 +32,18 @@ jobs: os: [ubuntu-latest, windows-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 + - name: Checkout repository + uses: actions/checkout@v4 with: submodules: true - - uses: goto-bus-stop/setup-zig@v2 + + - name: Setup zig + uses: goto-bus-stop/setup-zig@v2 with: version: master - - uses: actions/cache@v4 + - name: Restore cache + uses: actions/cache@v4 with: path: | zig-cache -- cgit v1.2.3 From 2c751868628111b8ae555347d0f8115c609cdc72 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 14 Apr 2024 20:33:23 +0200 Subject: ci: run with -fqemu and -frosetta --- .github/workflows/main.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3f4ecf3..6a6093c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,6 +42,11 @@ jobs: with: version: master + - name: Install qemu + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + sudo apt-get update -y && sudo apt-get install -y qemu-user-binfmt + - name: Restore cache uses: actions/cache@v4 with: @@ -52,9 +57,17 @@ jobs: restore-keys: ${{ runner.os }}-${{ matrix.os }}-zig- - name: Run Tests in memory + if: ${{ matrix.os == 'ubuntu-latest' }} run: zig build test -Dci=true -Din_memory=true --summary all -fqemu -fwine + - name: Run Tests in memory + if: ${{ matrix.os == 'macos-latest' }} + run: zig build test -Dci=true -Din_memory=true --summary all -frosetta + - name: Run Tests in memory + if: ${{ matrix.os == 'windows-latest' }} + run: zig build test -Dci=true -Din_memory=true --summary all - name: Build the example zigcrypto loadable extension run: zig build zigcrypto - name: Test the zigcrypto loadable extension + if: ${{ matrix.os != 'windows-latest' }} run: ./zig-out/bin/zigcrypto-test -- cgit v1.2.3