From da97764e94d2d17c34837c42850e7bbcb50f03c4 Mon Sep 17 00:00:00 2001 From: Jae B Date: Thu, 30 Apr 2026 13:21:28 +1000 Subject: [PATCH 1/2] add tests to ensure recursion works with translate-c modules --- test/translate_c_dep/build.zig | 48 +++++++++++++++++-- .../translate_c_dep_double_depth_module.zig | 6 +++ .../src/translate_c_dep_main.zig | 1 + .../translate_c_dep_single_depth_module.zig | 3 ++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test/translate_c_dep/src/translate_c_dep_double_depth_module.zig create mode 100644 test/translate_c_dep/src/translate_c_dep_single_depth_module.zig diff --git a/test/translate_c_dep/build.zig b/test/translate_c_dep/build.zig index 3f896a1..c55cc63 100644 --- a/test/translate_c_dep/build.zig +++ b/test/translate_c_dep/build.zig @@ -45,22 +45,60 @@ pub fn build(b: *std.Build) void { break :blk apk; }; + // Load translate_c module + const translate_c_dep_name = "translate_c"; + const translate_c_import = b.lazyImport(@This(), translate_c_dep_name) orelse return; + const translate_c = b.lazyDependency(translate_c_dep_name, .{}) orelse return; + const Translator = translate_c_import.Translator; + for (targets) |target| { if (!target.result.abi.isAndroid()) { std.debug.panic("For testing Android builds only. Target(s) should be Android not: {t}", .{target.result.abi}); } const app_module = b.createModule(.{ + .root_source_file = b.path("src/translate_c_dep_main.zig"), .target = target, .optimize = optimize, - .root_source_file = b.path("src/translate_c_dep_main.zig"), }); - // Must be stable release of Zig *and* 0.16.X or higher + // testTranslateCExternal + { + const translator: Translator = .init(translate_c, .{ + .c_source_file = b.addWriteFiles().add("android_c.h", + \\#include /* For main module */ + ), + .target = target, + .optimize = optimize, + }); + app_module.addImport("translate_c_external", translator.mod); + log.info("testTranslateCExternal({t}): add import 'translate_c_external'", .{target.result.cpu.arch}); + } + + // testTranslateCExternal for sub-sub-module, this test should ideally catch recursion with imported modules { - const translate_c_external_mod = testTranslateCExternal(b, target, optimize) orelse return; - app_module.addImport("translate_c_external", translate_c_external_mod); - log.info("testTranslateCExternal: add import 'translate_c_external' to {t}", .{target.result.cpu.arch}); + const single_depth_mod = b.createModule(.{ + .root_source_file = b.path("src/translate_c_dep_single_depth_module.zig"), + .target = target, + .optimize = optimize, + }); + const double_depth_mod = b.createModule(.{ + .root_source_file = b.path("src/translate_c_dep_double_depth_module.zig"), + .target = target, + .optimize = optimize, + }); + single_depth_mod.addImport("double_depth", double_depth_mod); + + const translator: Translator = .init(translate_c, .{ + .c_source_file = b.addWriteFiles().add("android_sub_c.h", + \\#include /* For sub-module (two-depth from main) */ + ), + .target = target, + .optimize = optimize, + }); + double_depth_mod.addImport("translate_c_external_recursive", translator.mod); + app_module.addImport("single_depth", single_depth_mod); + log.info("testTranslateCExternalSubModule({t}): add import 'translate_c_external_recursive' a sub-sub-module of main", .{target.result.cpu.arch}); } const libmain = b.addLibrary(.{ diff --git a/test/translate_c_dep/src/translate_c_dep_double_depth_module.zig b/test/translate_c_dep/src/translate_c_dep_double_depth_module.zig new file mode 100644 index 0000000..4949e2f --- /dev/null +++ b/test/translate_c_dep/src/translate_c_dep_double_depth_module.zig @@ -0,0 +1,6 @@ +//! Test that a translate-c module will work recursively + +comptime { + // For external translate-c usage, validate at compile-time that this symbol exists + _ = @import("translate_c_external_recursive").__android_log_write; +} diff --git a/test/translate_c_dep/src/translate_c_dep_main.zig b/test/translate_c_dep/src/translate_c_dep_main.zig index 1eb7bb4..4acb8a3 100644 --- a/test/translate_c_dep/src/translate_c_dep_main.zig +++ b/test/translate_c_dep/src/translate_c_dep_main.zig @@ -1,6 +1,7 @@ const builtin = @import("builtin"); const android = @import("android"); +const _ = @import("single_depth"); comptime { // For external translate-c usage, validate at compile-time that this symbol exists diff --git a/test/translate_c_dep/src/translate_c_dep_single_depth_module.zig b/test/translate_c_dep/src/translate_c_dep_single_depth_module.zig new file mode 100644 index 0000000..87b890b --- /dev/null +++ b/test/translate_c_dep/src/translate_c_dep_single_depth_module.zig @@ -0,0 +1,3 @@ +//! Test that a module added to "main" works + +const _ = @import("double_depth"); From e7f1a2d724575616ab0ea8a05f2e23b1e4f5825e Mon Sep 17 00:00:00 2001 From: Jae B Date: Thu, 30 Apr 2026 13:23:41 +1000 Subject: [PATCH 2/2] add different scope for log --- test/translate_c_dep/build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/translate_c_dep/build.zig b/test/translate_c_dep/build.zig index c55cc63..6b24bbf 100644 --- a/test/translate_c_dep/build.zig +++ b/test/translate_c_dep/build.zig @@ -2,7 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); -const log = std.log.scoped(.build); +const log = std.log.scoped(.translate_c_dep); const android = @import("android");