From 5bf630519c1e8d3af10636db1a12af56fe88d932 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 09:54:06 +0200 Subject: [PATCH 1/6] Make the target platform configurable --- zig/private/zig_configure.bzl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/zig/private/zig_configure.bzl b/zig/private/zig_configure.bzl index 0501ccee..b282f55a 100644 --- a/zig/private/zig_configure.bzl +++ b/zig/private/zig_configure.bzl @@ -8,6 +8,8 @@ DOC = """\ def _zig_transition_impl(settings, attr): result = dict(settings) + if attr.target: + result["//command_line_option:platforms"] = str(attr.target) if attr.mode: result["//zig/settings:mode"] = attr.mode if attr.threaded: @@ -17,10 +19,12 @@ def _zig_transition_impl(settings, attr): _zig_transition = transition( implementation = _zig_transition_impl, inputs = [ + "//command_line_option:platforms", "//zig/settings:mode", "//zig/settings:threaded", ], outputs = [ + "//command_line_option:platforms", "//zig/settings:mode", "//zig/settings:threaded", ], @@ -34,6 +38,10 @@ def _make_attrs(*, executable, test): executable = executable, mandatory = True, ), + "target": attr.label( + doc = "The target platform", + mandatory = False, + ), "mode": attr.string( doc = "The build mode setting", mandatory = False, From df247df4712a6f3f282c024b5850fcc3125fa8f8 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 10:06:04 +0200 Subject: [PATCH 2/6] End to end test for target transition on binary --- e2e/workspace/configure-target/BUILD.bazel | 55 +++++++++++++++++++ .../binary_aarch64-linux_arch.expected | 1 + e2e/workspace/configure-target/main.zig | 5 ++ .../configure-target/read_elf_arch.zig | 23 ++++++++ 4 files changed, 84 insertions(+) create mode 100644 e2e/workspace/configure-target/BUILD.bazel create mode 100644 e2e/workspace/configure-target/binary_aarch64-linux_arch.expected create mode 100644 e2e/workspace/configure-target/main.zig create mode 100644 e2e/workspace/configure-target/read_elf_arch.zig diff --git a/e2e/workspace/configure-target/BUILD.bazel b/e2e/workspace/configure-target/BUILD.bazel new file mode 100644 index 00000000..434823d5 --- /dev/null +++ b/e2e/workspace/configure-target/BUILD.bazel @@ -0,0 +1,55 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load( + "@rules_zig//zig:defs.bzl", + "zig_binary", + "zig_configure_binary", +) + +platform( + name = "aarch64-linux", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], +) + +zig_binary( + name = "binary", + main = "main.zig", + tags = ["manual"], +) + +zig_configure_binary( + name = "binary_aarch64-linux", + actual = ":binary", + target = ":aarch64-linux", +) + +build_test( + name = "binary_aarch64-linux_build_test", + targets = [ + ":binary_aarch64-linux", + ], +) + +zig_binary( + name = "read_elf_arch", + main = "read_elf_arch.zig", +) + +genrule( + name = "binary_aarch64-linux_arch", + srcs = [":binary_aarch64-linux"], + outs = ["binary_aarch64-linux_arch.actual"], + cmd = "$(execpath :read_elf_arch) $(SRCS) > $(OUTS)", + tools = [":read_elf_arch"], +) + +diff_test( + name = "binary_aarch64-linux_arch_test", + file1 = ":binary_aarch64-linux_arch.expected", + file2 = ":binary_aarch64-linux_arch.actual", +) + +# TODO[AH] Test another operating system diff --git a/e2e/workspace/configure-target/binary_aarch64-linux_arch.expected b/e2e/workspace/configure-target/binary_aarch64-linux_arch.expected new file mode 100644 index 00000000..0a30acdc --- /dev/null +++ b/e2e/workspace/configure-target/binary_aarch64-linux_arch.expected @@ -0,0 +1 @@ +AARCH64 diff --git a/e2e/workspace/configure-target/main.zig b/e2e/workspace/configure-target/main.zig new file mode 100644 index 00000000..7421521b --- /dev/null +++ b/e2e/workspace/configure-target/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() !void { + try std.io.getStdOut().writer().print("Hello world!\n", .{}); +} diff --git a/e2e/workspace/configure-target/read_elf_arch.zig b/e2e/workspace/configure-target/read_elf_arch.zig new file mode 100644 index 00000000..53f702ad --- /dev/null +++ b/e2e/workspace/configure-target/read_elf_arch.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const elf = std.elf; + +pub fn main() !void { + var args = try std.process.argsAlloc(std.heap.page_allocator); + defer std.process.argsFree(std.heap.page_allocator, args); + + if (args.len < 2) { + try std.io.getStdErr().writer().print("Usage: {s} \n", .{args[0]}); + return; + } + + try printMachineType(args[1]); +} + +fn printMachineType(binary_path: []const u8) !void { + const file = try std.fs.cwd().openFile(binary_path, .{}); + defer file.close(); + + const elf_header = try elf.Header.read(file); + + try std.io.getStdOut().writer().print("{s}\n", .{@tagName(elf_header.machine)}); +} From 8b0364a80a9c003da8c9978307d04bc1271fc8bf Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 10:09:13 +0200 Subject: [PATCH 3/6] End to end test for transition on test --- e2e/workspace/configure-target/BUILD.bazel | 54 +++++++++++++++++-- .../test_aarch64-linux_arch.expected | 1 + 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 e2e/workspace/configure-target/test_aarch64-linux_arch.expected diff --git a/e2e/workspace/configure-target/BUILD.bazel b/e2e/workspace/configure-target/BUILD.bazel index 434823d5..c6190aad 100644 --- a/e2e/workspace/configure-target/BUILD.bazel +++ b/e2e/workspace/configure-target/BUILD.bazel @@ -4,6 +4,8 @@ load( "@rules_zig//zig:defs.bzl", "zig_binary", "zig_configure_binary", + "zig_configure_test", + "zig_test", ) platform( @@ -14,6 +16,14 @@ platform( ], ) +zig_binary( + name = "read_elf_arch", + main = "read_elf_arch.zig", +) + +# ---------------------------------------------------------- +# zig_configure_binary + zig_binary( name = "binary", main = "main.zig", @@ -33,11 +43,6 @@ build_test( ], ) -zig_binary( - name = "read_elf_arch", - main = "read_elf_arch.zig", -) - genrule( name = "binary_aarch64-linux_arch", srcs = [":binary_aarch64-linux"], @@ -53,3 +58,42 @@ diff_test( ) # TODO[AH] Test another operating system + +# ---------------------------------------------------------- +# zig_configure_test + +zig_test( + name = "test", + main = "main.zig", + tags = ["manual"], +) + +zig_configure_test( + name = "test_aarch64-linux", + actual = ":test", + target = ":aarch64-linux", +) + +build_test( + name = "test_aarch64-linux_build_test", + targets = [ + ":test_aarch64-linux", + ], +) + +genrule( + name = "test_aarch64-linux_arch", + testonly = True, + srcs = [":test_aarch64-linux"], + outs = ["test_aarch64-linux_arch.actual"], + cmd = "$(execpath :read_elf_arch) $(SRCS) > $(OUTS)", + tools = [":read_elf_arch"], +) + +diff_test( + name = "test_aarch64-linux_arch_test", + file1 = ":test_aarch64-linux_arch.expected", + file2 = ":test_aarch64-linux_arch.actual", +) + +# TODO[AH] Test another operating system diff --git a/e2e/workspace/configure-target/test_aarch64-linux_arch.expected b/e2e/workspace/configure-target/test_aarch64-linux_arch.expected new file mode 100644 index 00000000..0a30acdc --- /dev/null +++ b/e2e/workspace/configure-target/test_aarch64-linux_arch.expected @@ -0,0 +1 @@ +AARCH64 From 2f3893af4af25e1e1eff77b89acf7441aa1bde71 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 10:26:25 +0200 Subject: [PATCH 4/6] todo note: target transition zig_library --- e2e/workspace/configure-target/BUILD.bazel | 3 +++ 1 file changed, 3 insertions(+) diff --git a/e2e/workspace/configure-target/BUILD.bazel b/e2e/workspace/configure-target/BUILD.bazel index c6190aad..eace3c37 100644 --- a/e2e/workspace/configure-target/BUILD.bazel +++ b/e2e/workspace/configure-target/BUILD.bazel @@ -21,6 +21,9 @@ zig_binary( main = "read_elf_arch.zig", ) +# TODO[AH] Test target transition on zig_library. +# Open question: how to extract the target platform from .a file? + # ---------------------------------------------------------- # zig_configure_binary From cb02ee6249a58e8836ddba25dc8c4650f5ebaa29 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 10:27:05 +0200 Subject: [PATCH 5/6] update generated files --- .bazelrc | 4 ++-- docs/rules.md | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.bazelrc b/.bazelrc index de3f1d13..42be3d04 100644 --- a/.bazelrc +++ b/.bazelrc @@ -7,8 +7,8 @@ import %workspace%/.bazelrc.flags # Deleted packages for integration tests. # To update these lines, execute # `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages` -build --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,e2e/workspace/configure-threaded,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace -query --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,e2e/workspace/configure-threaded,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace +build --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace +query --deleted_packages=e2e/workspace,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/linker-script,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace # Load any settings specific to the current user. # .bazelrc.user should appear in .gitignore so that settings are not shared with team members diff --git a/docs/rules.md b/docs/rules.md index 3fe1734b..9744f5e1 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -29,7 +29,7 @@ zig_binary(name, deps< ## zig_configure
-zig_configure(name, actual, mode, threaded)
+zig_configure(name, actual, mode, target, threaded)
 
@@ -42,6 +42,7 @@ zig_configure(name, name | A unique name for this target. | Name | required | | | actual | The target to transition. | Label | required | | | mode | The build mode setting | String | optional | "" | +| target | The target platform | Label | optional | None | | threaded | The threaded setting | String | optional | "" | @@ -50,7 +51,7 @@ zig_configure(name, name, actual, mode, threaded) +zig_configure_binary(name, actual, mode, target, threaded) @@ -63,6 +64,7 @@ zig_configure_binary(name, name | A unique name for this target. | Name | required | | | actual | The target to transition. | Label | required | | | mode | The build mode setting | String | optional | "" | +| target | The target platform | Label | optional | None | | threaded | The threaded setting | String | optional | "" | @@ -71,7 +73,7 @@ zig_configure_binary(name, name, actual, mode, threaded) +zig_configure_test(name, actual, mode, target, threaded) @@ -84,6 +86,7 @@ zig_configure_test(name, name | A unique name for this target. | Name | required | | | actual | The target to transition. | Label | required | | | mode | The build mode setting | String | optional | "" | +| target | The target platform | Label | optional | None | | threaded | The threaded setting | String | optional | "" | From 86cbf18c6396453af4e7020dc3033c39ea7033df Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 19 May 2023 10:33:21 +0200 Subject: [PATCH 6/6] Mark target configured targets manual --- e2e/workspace/configure-target/BUILD.bazel | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/workspace/configure-target/BUILD.bazel b/e2e/workspace/configure-target/BUILD.bazel index eace3c37..282c897a 100644 --- a/e2e/workspace/configure-target/BUILD.bazel +++ b/e2e/workspace/configure-target/BUILD.bazel @@ -36,6 +36,7 @@ zig_binary( zig_configure_binary( name = "binary_aarch64-linux", actual = ":binary", + tags = ["manual"], target = ":aarch64-linux", ) @@ -74,6 +75,7 @@ zig_test( zig_configure_test( name = "test_aarch64-linux", actual = ":test", + tags = ["manual"], target = ":aarch64-linux", )