diff --git a/.bazelrc b/.bazelrc index 54d7309b..de3f1d13 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/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/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-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 # 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 409e9bab..3fe1734b 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -7,7 +7,7 @@ Public API re-exports ## zig_binary
-zig_binary(name, deps, main, srcs) +zig_binary(name, deps, linker_script, main, srcs)@@ -19,6 +19,7 @@ zig_binary(name, deps< | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | deps | Packages or libraries required to build the target. | List of labels | optional |
[] |
+| linker_script | Custom linker script for the target. | Label | optional | None |
| main | The main source file. | Label | required | |
| srcs | Other source files required to build the target. | List of labels | optional | [] |
@@ -91,7 +92,7 @@ zig_configure_test(name, name, deps, main, srcs)
+zig_library(name, deps, linker_script, main, srcs)
@@ -103,6 +104,7 @@ zig_library(name, de
| :------------- | :------------- | :------------- | :------------- | :------------- |
| name | A unique name for this target. | Name | required | |
| deps | Packages or libraries required to build the target. | List of labels | optional | [] |
+| linker_script | Custom linker script for the target. | Label | optional | None |
| main | The main source file. | Label | required | |
| srcs | Other source files required to build the target. | List of labels | optional | [] |
@@ -133,7 +135,7 @@ zig_package(name, de
## zig_test
-zig_test(name, deps, main, srcs) +zig_test(name, deps, linker_script, main, srcs)@@ -145,6 +147,7 @@ zig_test(name, deps, < | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | deps | Packages or libraries required to build the target. | List of labels | optional |
[] |
+| linker_script | Custom linker script for the target. | Label | optional | None |
| main | The main source file. | Label | required | |
| srcs | Other source files required to build the target. | List of labels | optional | [] |
diff --git a/e2e/workspace/linker-script/BUILD.bazel b/e2e/workspace/linker-script/BUILD.bazel
new file mode 100644
index 00000000..ff45d8aa
--- /dev/null
+++ b/e2e/workspace/linker-script/BUILD.bazel
@@ -0,0 +1,41 @@
+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_library", "zig_test")
+
+zig_binary(
+ name = "binary",
+ linker_script = "custom.ld",
+ main = "bin.zig",
+)
+
+genrule(
+ name = "bin_output",
+ outs = ["bin_output.actual"],
+ cmd = "$(execpath :binary) > $(OUTS)",
+ tools = [":binary"],
+)
+
+diff_test(
+ name = "bin_output_test",
+ file1 = ":bin_output.expected",
+ file2 = ":bin_output.actual",
+)
+
+zig_library(
+ name = "library",
+ linker_script = "custom.ld",
+ main = "lib.zig",
+)
+
+build_test(
+ name = "library_build_test",
+ targets = [
+ ":library",
+ ],
+)
+
+zig_test(
+ name = "test",
+ linker_script = "custom.ld",
+ main = "test.zig",
+)
diff --git a/e2e/workspace/linker-script/bin.zig b/e2e/workspace/linker-script/bin.zig
new file mode 100644
index 00000000..84f15daa
--- /dev/null
+++ b/e2e/workspace/linker-script/bin.zig
@@ -0,0 +1,7 @@
+const std = @import("std");
+
+extern const custom_global_symbol: u8;
+
+pub fn main() !void {
+ try std.io.getStdOut().writer().print("{d}\n", .{custom_global_symbol});
+}
diff --git a/e2e/workspace/linker-script/bin_output.expected b/e2e/workspace/linker-script/bin_output.expected
new file mode 100644
index 00000000..d81cc071
--- /dev/null
+++ b/e2e/workspace/linker-script/bin_output.expected
@@ -0,0 +1 @@
+42
diff --git a/e2e/workspace/linker-script/custom.ld b/e2e/workspace/linker-script/custom.ld
new file mode 100644
index 00000000..b300f88f
--- /dev/null
+++ b/e2e/workspace/linker-script/custom.ld
@@ -0,0 +1,21 @@
+SECTIONS
+{
+ . = 0x200000 + SIZEOF_HEADERS;
+ .rodata : { *(.rodata*) }
+ . += 0x10000;
+ .text : { *(.text*) }
+ . += 0x10000;
+ .got : { *(.got*) }
+ . += 0x10000;
+ .bss : { *(.bss*) }
+ . += 0x10000 - SIZEOF(.bss) % 0x10000;
+ .tbss : { *(.tbss*) }
+ . += 0x10000 - SIZEOF(.tbss) % 0x10000;
+ .data : { *(.data*) }
+ . += 0x10000;
+ .custom ALIGN(0x8) :
+ {
+ PROVIDE(custom_global_symbol = .);
+ BYTE(42)
+ }
+}
diff --git a/e2e/workspace/linker-script/lib.zig b/e2e/workspace/linker-script/lib.zig
new file mode 100644
index 00000000..00a02aa7
--- /dev/null
+++ b/e2e/workspace/linker-script/lib.zig
@@ -0,0 +1,7 @@
+const std = @import("std");
+
+extern const custom_global_symbol: u8;
+
+export fn getCustomGlobalSymbol() u8 {
+ return custom_global_symbol;
+}
diff --git a/e2e/workspace/linker-script/test.zig b/e2e/workspace/linker-script/test.zig
new file mode 100644
index 00000000..397d5317
--- /dev/null
+++ b/e2e/workspace/linker-script/test.zig
@@ -0,0 +1,7 @@
+const std = @import("std");
+
+extern const custom_global_symbol: u8;
+
+test "custom_global_symbol is 42" {
+ try std.testing.expectEqual(@as(u8, 42), custom_global_symbol);
+}
diff --git a/zig/private/BUILD.bazel b/zig/private/BUILD.bazel
index 82010cea..7f013dbe 100644
--- a/zig/private/BUILD.bazel
+++ b/zig/private/BUILD.bazel
@@ -30,6 +30,7 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
deps = [
"//zig/private/common:filetypes",
+ "//zig/private/common:linker_script",
"//zig/private/common:zig_cache",
"//zig/private/providers:zig_package_info",
"//zig/private/providers:zig_settings_info",
@@ -37,24 +38,25 @@ bzl_library(
)
bzl_library(
- name = "zig_library",
- srcs = ["zig_library.bzl"],
+ name = "zig_configure",
+ srcs = ["zig_configure.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
- "//zig/private/common:filetypes",
- "//zig/private/common:zig_cache",
- "//zig/private/providers:zig_package_info",
- "//zig/private/providers:zig_settings_info",
+ ":settings",
+ "@bazel_skylib//lib:paths",
],
)
bzl_library(
- name = "zig_configure",
- srcs = ["zig_configure.bzl"],
+ name = "zig_library",
+ srcs = ["zig_library.bzl"],
visibility = ["//zig:__subpackages__"],
deps = [
- ":settings",
- "@bazel_skylib//lib:paths",
+ "//zig/private/common:filetypes",
+ "//zig/private/common:linker_script",
+ "//zig/private/common:zig_cache",
+ "//zig/private/providers:zig_package_info",
+ "//zig/private/providers:zig_settings_info",
],
)
diff --git a/zig/private/common/BUILD.bazel b/zig/private/common/BUILD.bazel
index 15e6437d..74de4579 100644
--- a/zig/private/common/BUILD.bazel
+++ b/zig/private/common/BUILD.bazel
@@ -13,12 +13,19 @@ bzl_library(
visibility = ["//zig:__subpackages__"],
)
+bzl_library(
+ name = "linker_script",
+ srcs = ["linker_script.bzl"],
+ visibility = ["//zig:__subpackages__"],
+)
+
# Execute `bazel run //util:update_filegroups` to update this target.
filegroup(
name = "all_files",
srcs = [
":BUILD.bazel",
":filetypes.bzl",
+ ":linker_script.bzl",
":zig_cache.bzl",
],
visibility = ["//zig/private:__pkg__"],
diff --git a/zig/private/common/linker_script.bzl b/zig/private/common/linker_script.bzl
new file mode 100644
index 00000000..ccbf30e3
--- /dev/null
+++ b/zig/private/common/linker_script.bzl
@@ -0,0 +1,19 @@
+"""Handle custom linker scripts."""
+
+def zig_linker_script(*, linker_script, inputs, args):
+ """Handle custom linker scripts.
+
+ Sets the appropriate command-line flags for the Zig compiler to configure a
+ custom linker script, if provided.
+
+ Args:
+ linker_script: optional; Label, The linker script attribute.
+ inputs: List; mutable, Append the linker script inputs to this collection.
+ args: Args; mutable, Append the Zig command-line flags to this object.
+ """
+
+ if linker_script == None:
+ return
+
+ inputs.append(linker_script)
+ args.add_all(["-T", linker_script])
diff --git a/zig/private/zig_binary.bzl b/zig/private/zig_binary.bzl
index 2a590cfc..925aca07 100644
--- a/zig/private/zig_binary.bzl
+++ b/zig/private/zig_binary.bzl
@@ -1,6 +1,7 @@
"""Implementation of the zig_binary rule."""
load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
+load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
@@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
+ "linker_script": attr.label(
+ doc = "Custom linker script for the target.",
+ allow_single_file = True,
+ mandatory = False,
+ ),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
@@ -65,6 +71,12 @@ def _zig_binary_impl(ctx):
args = args,
)
+ zig_linker_script(
+ linker_script = ctx.file.linker_script,
+ inputs = direct_inputs,
+ args = args,
+ )
+
zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
diff --git a/zig/private/zig_library.bzl b/zig/private/zig_library.bzl
index 95d77277..4547fc7b 100644
--- a/zig/private/zig_library.bzl
+++ b/zig/private/zig_library.bzl
@@ -1,6 +1,7 @@
"""Implementation of the zig_library rule."""
load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
+load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
@@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
+ "linker_script": attr.label(
+ doc = "Custom linker script for the target.",
+ allow_single_file = True,
+ mandatory = False,
+ ),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
@@ -66,6 +72,12 @@ def _zig_library_impl(ctx):
args = args,
)
+ zig_linker_script(
+ linker_script = ctx.file.linker_script,
+ inputs = direct_inputs,
+ args = args,
+ )
+
zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,
diff --git a/zig/private/zig_test.bzl b/zig/private/zig_test.bzl
index a269db7a..9c3233aa 100644
--- a/zig/private/zig_test.bzl
+++ b/zig/private/zig_test.bzl
@@ -1,6 +1,7 @@
"""Implementation of the zig_test rule."""
load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
+load("//zig/private/common:linker_script.bzl", "zig_linker_script")
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
load(
"//zig/private/providers:zig_package_info.bzl",
@@ -32,6 +33,11 @@ ATTRS = {
mandatory = False,
providers = [ZigPackageInfo],
),
+ "linker_script": attr.label(
+ doc = "Custom linker script for the target.",
+ allow_single_file = True,
+ mandatory = False,
+ ),
"_settings": attr.label(
default = "//zig/settings",
doc = "Zig build settings.",
@@ -65,6 +71,12 @@ def _zig_test_impl(ctx):
args = args,
)
+ zig_linker_script(
+ linker_script = ctx.file.linker_script,
+ inputs = direct_inputs,
+ args = args,
+ )
+
zig_cache_output(
actions = ctx.actions,
name = ctx.label.name,