Skip to content

Commit

Permalink
reorder tests
Browse files Browse the repository at this point in the history
add cross-build on CI
  • Loading branch information
kassane committed Sep 25, 2024
1 parent 9796b25 commit 1be8ff8
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
echo "$PWD/zig-mos-aarch64-macos-apple_m1" >> $GITHUB_PATH
- name: Build
run: zig build -Dtests --summary all -freference-trace
run: zig build --build-file $PWD/tests/build.zig -Dtests --summary all -freference-trace

- name: Run
run: zig build rtsan -Dtests -freference-trace
run: zig build rtsan --build-file $PWD/tests/build.zig -Dtests -freference-trace
43 changes: 43 additions & 0 deletions .github/workflows/cross.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Zig

on: [push, pull_request]

jobs:
build:
strategy:
fail-fast: false
matrix:
targets:
- x86_64-linux-gnu
- x86_64-linux-musl
- x86-linux-gnu
- x86-linux-musl
- aarch64-linux-gnu
- aarch64-linux-musl
- riscv64-linux-musl
- powerpc64-linux-musl
- x86_64-macos
- aarch64-macos
# - x86-windows
# - x86_64-windows
# - aarch64-windows

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- name: Download & Extract packages
run: |
curl -LO https://github.com/kassane/zig-mos-bootstrap/releases/download/0.2/zig-mos-x86_64-linux-musl-baseline.tar.xz
tar -xf zig-mos-x86_64-linux-musl-baseline.tar.xz
echo "$PWD/zig-mos-x86_64-linux-musl-baseline" >> $GITHUB_PATH
sudo apt update && sudo apt install -y qemu-user-binfmt
- name: Build Summary ${{ matrix.targets }}
run: zig build --build-file $PWD/tests/build.zig --summary all -freference-trace -Dtarget=${{ matrix.targets }}

- name: Run ${{ matrix.targets }}
run: zig build rtsan --build-file $PWD/tests/build.zig --summary all -freference-trace -Dtarget=${{ matrix.targets }}
58 changes: 3 additions & 55 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,60 +1,8 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const tests = b.option(bool, "tests", "build tests") orelse false;

// FIXME
// {
// const exe = b.addExecutable(.{
// .name = "test-asan",
// .target = target,
// .optimize = optimize,
// });
// exe.addCSourceFile(.{
// .file = b.path("tests/vec_test.cc"),
// .flags = &.{"-fsanitize=address"},
// });
// buildASan(b, exe);

// if (tests) {
// b.installArtifact(exe);
// const run = b.addRunArtifact(exe);
// const run_step = b.step("asan", "Run the test-asan test");
// run_step.dependOn(&run.step);
// }
// }
// LLVM 20
{
const libzig = b.addStaticLibrary(.{
.name = "zig",
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/array.zig"),
});
libzig.linkLibC();

const exe = b.addExecutable(.{
.name = "test-rtsan",
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libzig);
exe.addCSourceFile(.{
.file = b.path("tests/vec_test.cc"),
.flags = &.{"-fsanitize=realtime"},
});
buildRTSan(b, exe);

if (tests) {
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
const run_step = b.step("rtsan", "Run the test-rtsan test");
run_step.dependOn(&run.step);
}
}
_ = b; // autofix
// TODO
}

pub fn buildASan(b: *std.Build, lib: *std.Build.Step.Compile) void {
Expand Down Expand Up @@ -337,7 +285,7 @@ fn buildSanCommon(lib: *std.Build.Step.Compile, dependency: ?*std.Build.Dependen
"sanitizer_tls_get_addr.cpp",
"sanitizer_type_traits.cpp",
// "symbolizer/sanitizer_symbolize.cpp", // need LLVM headers
"symbolizer/sanitizer_wrappers.cpp",
// "symbolizer/sanitizer_wrappers.cpp",
},
.flags = cxxflags,
});
Expand Down
16 changes: 16 additions & 0 deletions tests/app/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Based on vec_test.cc

extern "C" {
void ffi_zigarray();
void ffi_zig_openfile();
}

void zig_main() [[clang::nonblocking]] {
ffi_zigarray();
ffi_zig_openfile();
}

int main() {
zig_main();
return 0;
}
11 changes: 11 additions & 0 deletions tests/app/vec_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Reference:
/// https://github.com/CppCon/CppCon2024/blob/main/Presentations/LLVMs_Realtime_Safety_Revolution.pdf

#include <vector>

int process() [[clang::nonblocking]] {
std::vector<int> v(16);
return v[16];
}

int main() { return process(); }
51 changes: 51 additions & 0 deletions tests/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const std = @import("std");
const sanitizers = @import("sanitizers");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// FIXME
// {
// const exe = b.addExecutable(.{
// .name = "test-asan",
// .target = target,
// .optimize = optimize,
// });
// exe.addCSourceFile(.{
// .file = b.path("app/test.cc"),
// .flags = &.{"-fsanitize=address"},
// });
// sanitizers.buildASan(b, exe);
// b.installArtifact(exe);
// const run = b.addRunArtifact(exe);
// const run_step = b.step("asan", "Run the test-asan test");
// run_step.dependOn(&run.step);
// }
// LLVM 20
{
const libzig = b.addStaticLibrary(.{
.name = "zig",
.target = target,
.optimize = optimize,
.root_source_file = b.path("lib/array.zig"),
});
libzig.linkLibC();

const exe = b.addExecutable(.{
.name = "test-rtsan",
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libzig);
exe.addCSourceFile(.{
.file = b.path("app/test.cc"),
.flags = &.{"-fsanitize=realtime"},
});
sanitizers.buildRTSan(b, exe);
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
const run_step = b.step("rtsan", "Run the test-rtsan test");
run_step.dependOn(&run.step);
}
}
14 changes: 14 additions & 0 deletions tests/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.{
.name = "tests",
.version = "0.0.0",
.dependencies = .{
.sanitizers = .{
.path = "..",
},
.@"compiler-rt" = .{
.url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.0/compiler-rt-19.1.0.src.tar.xz",
.hash = "1220c8a83cf97bac4d7c1515c95cf9108dfb2fe8f0633da29085146fccb510d76097",
},
},
.paths = .{""},
}
8 changes: 6 additions & 2 deletions tests/array.zig → tests/lib/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ fn array() callconv(.C) void {
// `posix_memalign` in real-time context!
// page_allocator (no-libc/syscalls): None

var arr = std.ArrayList([]const u8).init(std.heap.raw_c_allocator);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var arr = std.ArrayList([]const u8).init(allocator);
defer arr.deinit();
arr.append("foo") catch unreachable;
arr.append("bar") catch unreachable;
arr.append("baz") catch unreachable;
}

fn openfile() callconv(.C) void {
const file = std.fs.cwd().openFile("tests/malloc.d", .{}) catch unreachable;
const file = std.fs.cwd().openFile("build.zig.zon", .{}) catch unreachable;
defer file.close();
}

Expand Down
24 changes: 0 additions & 24 deletions tests/vec_test.cc

This file was deleted.

0 comments on commit 1be8ff8

Please sign in to comment.