Skip to content
Closed
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
1 change: 1 addition & 0 deletions test/build_examples.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pub fn addCases(cases: *tests.BuildExamplesContext) void {
cases.addBuildFile("test/standalone/pkg_import/build.zig");
cases.addBuildFile("test/standalone/use_alias/build.zig");
cases.addBuildFile("test/standalone/brace_expansion/build.zig");
cases.addBuildFile("test/standalone/cross_compile/build.zig");
}
3 changes: 3 additions & 0 deletions test/standalone/cross_compile/add.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn add(a: i32, b: i32) i32 {
return a + b;
}
18 changes: 18 additions & 0 deletions test/standalone/cross_compile/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const builtin = @import("builtin");
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
const exe = b.addExecutable("test", "test.zig");
exe.setBuildMode(b.standardReleaseOptions());

const lib = b.addStaticLibrary("lib", "lib.zig");
lib.setBuildMode(builtin.Mode.ReleaseSmall);
lib.setTarget(builtin.Arch.wasm32, builtin.Os.freestanding, builtin.Environ.unknown);

const run = b.addCommand(".", b.env_map, [][]const u8{exe.getOutputPath()});
run.step.dependOn(&exe.step);
run.step.dependOn(&lib.step);

const test_step = b.step("test", "Test it");
test_step.dependOn(&run.step);
}
1 change: 1 addition & 0 deletions test/standalone/cross_compile/lib.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use @import("add.zig");
15 changes: 15 additions & 0 deletions test/standalone/cross_compile/test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;

pub fn main() !void {
var direct = std.heap.DirectAllocator.init();
defer direct.deinit();

const bytes = try std.io.readFileAlloc(&direct.allocator, "zig-cache/liblib.a");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of how to do this without depending on zig-cache (which is supposed to be a black box):

const args = try std.os.argsAlloc(std.debug.global_allocator);
defer std.os.argsFree(std.debug.global_allocator, args);
const dynlib_name = args[1];
var lib = try std.DynLib.open(std.debug.global_allocator, dynlib_name);

const run = b.addCommand(".", b.env_map, [][]const u8{
main.getOutputPath(),
lib.getOutputPath(),
});

defer direct.allocator.free(bytes);

// Verify that it is a wasm file and has the symbol "add" somewhere in it.
assert(mem.indexOf(u8, bytes, "\x00asm").? == 0);
assert(mem.indexOf(u8, bytes, "\x03add") != null);
}