From f3cf2aa34324f35a0caa2cb0906b054e17d159d9 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Sat, 24 Jun 2023 12:33:03 +0200 Subject: [PATCH 1/2] Analysis test for progress message with package --- zig/tests/package-binary/BUILD.bazel | 19 +++++++++++++ zig/tests/package-binary/data.zig | 1 + zig/tests/package-binary/main.zig | 8 ++++++ zig/tests/rules_test.bzl | 41 ++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 zig/tests/package-binary/BUILD.bazel create mode 100644 zig/tests/package-binary/data.zig create mode 100644 zig/tests/package-binary/main.zig diff --git a/zig/tests/package-binary/BUILD.bazel b/zig/tests/package-binary/BUILD.bazel new file mode 100644 index 00000000..11054356 --- /dev/null +++ b/zig/tests/package-binary/BUILD.bazel @@ -0,0 +1,19 @@ +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_package") + +exports_files( + ["main.zig"], + visibility = ["//zig/tests:__pkg__"], +) + +zig_package( + name = "data", + main = "data.zig", + visibility = ["//zig/tests:__pkg__"], +) + +zig_binary( + name = "binary", + main = "main.zig", + visibility = ["//zig/tests:__pkg__"], + deps = [":data"], +) diff --git a/zig/tests/package-binary/data.zig b/zig/tests/package-binary/data.zig new file mode 100644 index 00000000..91cf36c8 --- /dev/null +++ b/zig/tests/package-binary/data.zig @@ -0,0 +1 @@ +pub const hello_world = "Hello world!\n"; diff --git a/zig/tests/package-binary/main.zig b/zig/tests/package-binary/main.zig new file mode 100644 index 00000000..286e7027 --- /dev/null +++ b/zig/tests/package-binary/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const data = @import("data"); + +pub fn main() void { + std.io.getStdOut().writeAll( + data.hello_world, + ) catch unreachable; +} diff --git a/zig/tests/rules_test.bzl b/zig/tests/rules_test.bzl index d87f73a5..072aeb7d 100644 --- a/zig/tests/rules_test.bzl +++ b/zig/tests/rules_test.bzl @@ -80,6 +80,46 @@ def _test_multiple_sources_binary(name): ) return [":" + name] +def _package_binary_test_impl(ctx): + env = analysistest.begin(ctx) + target = analysistest.target_under_test(env) + default = target[DefaultInfo] + + executable = default.files_to_run.executable + main = ctx.file.main + + build = [ + action + for action in analysistest.target_actions(env) + if action.mnemonic == "ZigBuildExe" + ] + asserts.equals(env, 1, len(build), "zig_binary should generate one ZigBuildExe action.") + build = build[0] + + # The position in the action input and output matters for the progress_message. + asserts.equals(env, main, build.inputs.to_list()[0], "the main source should be the first input.") + asserts.equals(env, executable, build.outputs.to_list()[0], "the binary should be the first output.") + + return analysistest.end(env) + +_package_binary_test = analysistest.make( + _package_binary_test_impl, + attrs = { + "main": attr.label( + allow_single_file = True, + mandatory = True, + ), + }, +) + +def _test_package_binary(name): + _package_binary_test( + name = name, + target_under_test = "//zig/tests/package-binary:binary", + main = "//zig/tests/package-binary:main.zig", + ) + return [":" + name] + def _c_sources_binary_test_impl(ctx): env = analysistest.begin(ctx) @@ -152,6 +192,7 @@ def rules_test_suite(name): tests = [] tests += _test_simple_binary(name = "simple_binary_test") tests += _test_multiple_sources_binary(name = "multiple_sources_binary_test") + tests += _test_package_binary(name = "package_binary_test") tests += _test_c_sources_binary(name = "c_sources_binary_test") native.test_suite( name = name, From bae2e71e0cbc75de081e02de25b221be2e58b00c Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Sat, 24 Jun 2023 12:35:28 +0200 Subject: [PATCH 2/2] fix: set inputs order to preorder This ensures that the main source file is the first input and that the progress message refers to the correct root module. --- zig/private/zig_binary.bzl | 2 +- zig/private/zig_library.bzl | 2 +- zig/private/zig_test.bzl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zig/private/zig_binary.bzl b/zig/private/zig_binary.bzl index dfeca4d0..b2559bb2 100644 --- a/zig/private/zig_binary.bzl +++ b/zig/private/zig_binary.bzl @@ -154,7 +154,7 @@ def _zig_binary_impl(ctx): ctx.actions.run( outputs = outputs, - inputs = depset(direct = direct_inputs, transitive = transitive_inputs), + inputs = depset(direct = direct_inputs, transitive = transitive_inputs, order = "preorder"), executable = zigtoolchaininfo.zig_exe_path, tools = zigtoolchaininfo.zig_files, arguments = ["build-exe", args], diff --git a/zig/private/zig_library.bzl b/zig/private/zig_library.bzl index eb40c7e3..be4adcca 100644 --- a/zig/private/zig_library.bzl +++ b/zig/private/zig_library.bzl @@ -154,7 +154,7 @@ def _zig_library_impl(ctx): ctx.actions.run( outputs = outputs, - inputs = depset(direct = direct_inputs, transitive = transitive_inputs), + inputs = depset(direct = direct_inputs, transitive = transitive_inputs, order = "preorder"), executable = zigtoolchaininfo.zig_exe_path, tools = zigtoolchaininfo.zig_files, arguments = ["build-lib", args], diff --git a/zig/private/zig_test.bzl b/zig/private/zig_test.bzl index 20b32622..2fd62533 100644 --- a/zig/private/zig_test.bzl +++ b/zig/private/zig_test.bzl @@ -153,7 +153,7 @@ def _zig_test_impl(ctx): ctx.actions.run( outputs = outputs, - inputs = depset(direct = direct_inputs, transitive = transitive_inputs), + inputs = depset(direct = direct_inputs, transitive = transitive_inputs, order = "preorder"), executable = zigtoolchaininfo.zig_exe_path, tools = zigtoolchaininfo.zig_files, arguments = ["test", "--test-no-exec", args],