From 15696813e4ce71060bcf09c3bdca617985cfa44a Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Mon, 19 Feb 2024 18:21:03 +0100 Subject: [PATCH 1/7] zig_module_info constructor function --- zig/private/providers/zig_module_info.bzl | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/zig/private/providers/zig_module_info.bzl b/zig/private/providers/zig_module_info.bzl index a90f0fe..83c2724 100644 --- a/zig/private/providers/zig_module_info.bzl +++ b/zig/private/providers/zig_module_info.bzl @@ -24,6 +24,51 @@ ZigModuleInfo = provider( doc = DOC, ) +def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps): + """Create `ZigModuleInfo` for a new Zig module. + + Args: + name: string, The import name of the module. + canonical_name: string, The canonical name may differ from the import name via remapping. + main: File, The main source file of the module. + srcs: list of File, Other Zig source files that belong to the module. + extra_srcs: list of File, Other files that belong to the module. + deps: list of ZigModuleInfo, Import dependencies of this module. + + Returns: + `ZigModuleInfo` + """ + imports = [] + mods_transitive = [] + srcs_transitive = [] + + for dep in deps: + if dep.canonical_name != dep.name: + imports.append("{}={}".format(dep.name, dep.canonical_name)) + else: + imports.append(dep.name) + + mods_transitive.append(dep.all_mods) + srcs_transitive.append(dep.all_srcs) + + mod_direct = "{name}:{imports}:{src}".format( + name = canonical_name, + imports = ",".join(imports), + src = main.path, + ) + all_mods = depset(direct = [mod_direct], transitive = mods_transitive) + all_srcs = depset(direct = [main] + srcs + extra_srcs, transitive = srcs_transitive) + module = ZigModuleInfo( + name = name, + canonical_name = canonical_name, + main = main, + srcs = srcs, + all_mods = all_mods, + all_srcs = all_srcs, + ) + + return module + def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): """Collect inputs and flags for Zig module dependencies. From 3c43256e164e9f165e9b459594290d96bfcde6b2 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 20 Feb 2024 07:55:26 +0100 Subject: [PATCH 2/7] Use zig_module_info --- zig/private/common/bazel_builtin.bzl | 14 +++++------ zig/private/zig_module.bzl | 35 +++++++--------------------- 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/zig/private/common/bazel_builtin.bzl b/zig/private/common/bazel_builtin.bzl index f2f3018..c3368ae 100644 --- a/zig/private/common/bazel_builtin.bzl +++ b/zig/private/common/bazel_builtin.bzl @@ -1,6 +1,9 @@ """Generate the `bazel_builtin` module.""" -load("//zig/private/providers:zig_module_info.bzl", "ZigModuleInfo") +load( + "//zig/private/providers:zig_module_info.bzl", + "zig_module_info", +) ATTRS = { "_bazel_builtin_template": attr.label( @@ -41,16 +44,13 @@ def bazel_builtin_module(ctx): is_executable = False, ) - module = ZigModuleInfo( + module = zig_module_info( name = "bazel_builtin", canonical_name = name, main = main, srcs = [], - all_mods = depset(direct = ["{name}::{src}".format( - name = name, - src = main.path, - )]), - all_srcs = depset(direct = [main]), + extra_srcs = [], + deps = [], ) return module diff --git a/zig/private/zig_module.bzl b/zig/private/zig_module.bzl index 576e593..7b40466 100644 --- a/zig/private/zig_module.bzl +++ b/zig/private/zig_module.bzl @@ -7,7 +7,11 @@ load( ) load("//zig/private/common:data.bzl", "zig_collect_data", "zig_create_runfiles") load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS") -load("//zig/private/providers:zig_module_info.bzl", "ZigModuleInfo") +load( + "//zig/private/providers:zig_module_info.bzl", + "ZigModuleInfo", + "zig_module_info", +) DOC = """\ Defines a Zig module. @@ -85,11 +89,6 @@ def _zig_module_impl(ctx): ), ) - srcs = [ctx.file.main] + ctx.files.srcs + ctx.files.extra_srcs - dep_names = [] - all_mods = [] - all_srcs = [] - bazel_builtin = bazel_builtin_module(ctx) modules = [ @@ -98,31 +97,13 @@ def _zig_module_impl(ctx): if ZigModuleInfo in dep ] + [bazel_builtin] - for module in modules: - if module.canonical_name != module.name: - dep_names.append("{}={}".format(module.name, module.canonical_name)) - else: - dep_names.append(module.name) - all_mods.append(module.all_mods) - all_srcs.append(module.all_srcs) - - module = ZigModuleInfo( + module = zig_module_info( name = ctx.label.name, canonical_name = ctx.label.name, main = ctx.file.main, srcs = ctx.files.srcs, - all_mods = depset( - direct = ["{name}:{deps}:{src}".format( - name = ctx.label.name, - deps = ",".join(dep_names), - src = ctx.file.main.path, - )], - transitive = all_mods, - ), - all_srcs = depset( - direct = srcs, - transitive = all_srcs, - ), + extra_srcs = ctx.files.extra_srcs, + deps = modules, ) return [default, module] From 381e56e5220098e6b3c2ddf0d73f6302dcecb20c Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 20 Feb 2024 08:50:57 +0100 Subject: [PATCH 3/7] Remove unused ZigModuleInfo fields --- zig/private/providers/zig_module_info.bzl | 4 -- zig/tests/module_info_test.bzl | 39 ++++++++++++++----- zig/tests/multiple-sources-module/BUILD.bazel | 9 +++++ zig/tests/nested-modules/BUILD.bazel | 12 ++++++ 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/zig/private/providers/zig_module_info.bzl b/zig/private/providers/zig_module_info.bzl index 83c2724..dc7c580 100644 --- a/zig/private/providers/zig_module_info.bzl +++ b/zig/private/providers/zig_module_info.bzl @@ -13,8 +13,6 @@ instead the Zig compiler performs whole program compilation. FIELDS = { "name": "string, The import name of the module.", "canonical_name": "string, The canonical name may differ from the import name via remapping.", - "main": "File, The main source file of the module.", - "srcs": "list of File, Other Zig source files that belong to the module.", "all_mods": "depset of string, All module CLI specifications required when depending on the module.", "all_srcs": "depset of File, All source files required when depending on the module.", } @@ -61,8 +59,6 @@ def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps): module = ZigModuleInfo( name = name, canonical_name = canonical_name, - main = main, - srcs = srcs, all_mods = all_mods, all_srcs = all_srcs, ) diff --git a/zig/tests/module_info_test.bzl b/zig/tests/module_info_test.bzl index 0fdb022..bd4d15d 100644 --- a/zig/tests/module_info_test.bzl +++ b/zig/tests/module_info_test.bzl @@ -81,7 +81,7 @@ def _single_module_test_impl(ctx): expected.extend(["--mod", "{name}:{deps}:{src}".format( name = module.name, deps = _bazel_builtin_dep(ctx.attr.pkg.label), - src = module.main.path, + src = ctx.file.pkg_main.path, )]) expected.extend(["--deps", module.name]) @@ -101,7 +101,7 @@ def _single_module_test_impl(ctx): inputs = depset(transitive = transitive_inputs) asserts.set_equals( env, - sets.make([module.main] + module.srcs + bazel_builtin_file), + sets.make([ctx.file.pkg_main] + ctx.files.pkg_srcs + bazel_builtin_file), sets.make(inputs.to_list()), "zig_module_dependencies should capture all module files.", ) @@ -112,6 +112,8 @@ _single_module_test = unittest.make( _single_module_test_impl, attrs = { "pkg": attr.label(providers = [ZigModuleInfo]), + "pkg_main": attr.label(allow_single_file = True), + "pkg_srcs": attr.label_list(allow_files = True), }, ) @@ -132,6 +134,11 @@ def _nested_modules_test_impl(ctx): for pkg in ctx.attr.pkgs } + pkg_mains = { + pkg.label.name: main + for (pkg, main) in zip(ctx.attr.pkgs, ctx.files.pkg_mains) + } + bazel_builtins = { pkg.label.name: struct( mod_flags = _bazel_builtin_mod_flags(ctx, pkg.label), @@ -147,17 +154,17 @@ def _nested_modules_test_impl(ctx): expected = [] expected.extend(bazel_builtins["e"].mod_flags) - expected.extend(["--mod", "e:{}:{}".format(bazel_builtins["e"].dep, pkgs["e"].main.path)]) + expected.extend(["--mod", "e:{}:{}".format(bazel_builtins["e"].dep, pkg_mains["e"].path)]) expected.extend(bazel_builtins["b"].mod_flags) - expected.extend(["--mod", "b:e,{}:{}".format(bazel_builtins["b"].dep, pkgs["b"].main.path)]) + expected.extend(["--mod", "b:e,{}:{}".format(bazel_builtins["b"].dep, pkg_mains["b"].path)]) expected.extend(bazel_builtins["c"].mod_flags) - expected.extend(["--mod", "c:e,{}:{}".format(bazel_builtins["c"].dep, pkgs["c"].main.path)]) + expected.extend(["--mod", "c:e,{}:{}".format(bazel_builtins["c"].dep, pkg_mains["c"].path)]) expected.extend(bazel_builtins["f"].mod_flags) - expected.extend(["--mod", "f:e,{}:{}".format(bazel_builtins["f"].dep, pkgs["f"].main.path)]) + expected.extend(["--mod", "f:e,{}:{}".format(bazel_builtins["f"].dep, pkg_mains["f"].path)]) expected.extend(bazel_builtins["d"].mod_flags) - expected.extend(["--mod", "d:f,{}:{}".format(bazel_builtins["d"].dep, pkgs["d"].main.path)]) + expected.extend(["--mod", "d:f,{}:{}".format(bazel_builtins["d"].dep, pkg_mains["d"].path)]) expected.extend(bazel_builtins["a"].mod_flags) - expected.extend(["--mod", "a:b,c,d,{}:{}".format(bazel_builtins["a"].dep, pkgs["a"].main.path)]) + expected.extend(["--mod", "a:b,c,d,{}:{}".format(bazel_builtins["a"].dep, pkg_mains["a"].path)]) expected.extend(["--deps", "a"]) asserts.equals( @@ -173,7 +180,7 @@ def _nested_modules_test_impl(ctx): sets.make([ src for pkg in pkgs.values() - for src in [pkg.main] + pkg.srcs + bazel_builtins[pkg.name].file + for src in [pkg_mains[pkg.name]] + bazel_builtins[pkg.name].file ]), sets.make(inputs.to_list()), "zig_module_dependencies should capture all module files.", @@ -185,6 +192,7 @@ _nested_modules_test = unittest.make( _nested_modules_test_impl, attrs = { "pkgs": attr.label_list(providers = [ZigModuleInfo]), + "pkg_mains": attr.label_list(allow_files = True), }, ) @@ -194,6 +202,11 @@ def module_info_test_suite(name): lambda name: _single_module_test( name = name, pkg = "//zig/tests/multiple-sources-module:data", + pkg_main = "//zig/tests/multiple-sources-module:data.zig", + pkg_srcs = [ + "//zig/tests/multiple-sources-module:data/hello.zig", + "//zig/tests/multiple-sources-module:data/world.zig", + ], size = "small", ), lambda name: _nested_modules_test( @@ -206,6 +219,14 @@ def module_info_test_suite(name): "//zig/tests/nested-modules:e", "//zig/tests/nested-modules:f", ], + pkg_mains = [ + "//zig/tests/nested-modules:a.zig", + "//zig/tests/nested-modules:b.zig", + "//zig/tests/nested-modules:c.zig", + "//zig/tests/nested-modules:d.zig", + "//zig/tests/nested-modules:e.zig", + "//zig/tests/nested-modules:f.zig", + ], size = "small", ), ) diff --git a/zig/tests/multiple-sources-module/BUILD.bazel b/zig/tests/multiple-sources-module/BUILD.bazel index c944d7c..2a535d7 100644 --- a/zig/tests/multiple-sources-module/BUILD.bazel +++ b/zig/tests/multiple-sources-module/BUILD.bazel @@ -1,5 +1,14 @@ load("@rules_zig//zig:defs.bzl", "zig_module") +exports_files( + [ + "data.zig", + "data/hello.zig", + "data/world.zig", + ], + visibility = ["//zig/tests:__pkg__"], +) + zig_module( name = "data", srcs = [ diff --git a/zig/tests/nested-modules/BUILD.bazel b/zig/tests/nested-modules/BUILD.bazel index 999dca8..0094df3 100644 --- a/zig/tests/nested-modules/BUILD.bazel +++ b/zig/tests/nested-modules/BUILD.bazel @@ -1,5 +1,17 @@ load("@rules_zig//zig:defs.bzl", "zig_module") +exports_files( + [ + "a.zig", + "b.zig", + "c.zig", + "d.zig", + "e.zig", + "f.zig", + ], + visibility = ["//zig/tests:__pkg__"], +) + zig_module( name = "a", main = "a.zig", From 25bf118f0906f770df296e2bdabc058888c260cb Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 20 Feb 2024 08:52:03 +0100 Subject: [PATCH 4/7] pkg --> mod in module info test --- zig/tests/module_info_test.bzl | 76 +++++++++++++++++----------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/zig/tests/module_info_test.bzl b/zig/tests/module_info_test.bzl index bd4d15d..da50d0a 100644 --- a/zig/tests/module_info_test.bzl +++ b/zig/tests/module_info_test.bzl @@ -69,19 +69,19 @@ def _single_module_test_impl(ctx): args = _mock_args() zig_module_dependencies( - deps = [ctx.attr.pkg], + deps = [ctx.attr.mod], inputs = transitive_inputs, args = args, ) - module = ctx.attr.pkg[ZigModuleInfo] + module = ctx.attr.mod[ZigModuleInfo] expected = [] - expected.extend(_bazel_builtin_mod_flags(ctx, ctx.attr.pkg.label)) + expected.extend(_bazel_builtin_mod_flags(ctx, ctx.attr.mod.label)) expected.extend(["--mod", "{name}:{deps}:{src}".format( name = module.name, - deps = _bazel_builtin_dep(ctx.attr.pkg.label), - src = ctx.file.pkg_main.path, + deps = _bazel_builtin_dep(ctx.attr.mod.label), + src = ctx.file.mod_main.path, )]) expected.extend(["--deps", module.name]) @@ -95,13 +95,13 @@ def _single_module_test_impl(ctx): bazel_builtin_file = [ file for file in module.all_srcs.to_list() - if file.path == _bazel_builtin_file_name(ctx, ctx.attr.pkg.label) + if file.path == _bazel_builtin_file_name(ctx, ctx.attr.mod.label) ] inputs = depset(transitive = transitive_inputs) asserts.set_equals( env, - sets.make([ctx.file.pkg_main] + ctx.files.pkg_srcs + bazel_builtin_file), + sets.make([ctx.file.mod_main] + ctx.files.mod_srcs + bazel_builtin_file), sets.make(inputs.to_list()), "zig_module_dependencies should capture all module files.", ) @@ -111,9 +111,9 @@ def _single_module_test_impl(ctx): _single_module_test = unittest.make( _single_module_test_impl, attrs = { - "pkg": attr.label(providers = [ZigModuleInfo]), - "pkg_main": attr.label(allow_single_file = True), - "pkg_srcs": attr.label_list(allow_files = True), + "mod": attr.label(providers = [ZigModuleInfo]), + "mod_main": attr.label(allow_single_file = True), + "mod_srcs": attr.label_list(allow_files = True), }, ) @@ -124,47 +124,47 @@ def _nested_modules_test_impl(ctx): args = _mock_args() zig_module_dependencies( - deps = [dep for dep in ctx.attr.pkgs if dep.label.name == "a"], + deps = [dep for dep in ctx.attr.mods if dep.label.name == "a"], inputs = transitive_inputs, args = args, ) - pkgs = { - pkg.label.name: pkg[ZigModuleInfo] - for pkg in ctx.attr.pkgs + mods = { + mod.label.name: mod[ZigModuleInfo] + for mod in ctx.attr.mods } - pkg_mains = { - pkg.label.name: main - for (pkg, main) in zip(ctx.attr.pkgs, ctx.files.pkg_mains) + mod_mains = { + mod.label.name: main + for (mod, main) in zip(ctx.attr.mods, ctx.files.mod_mains) } bazel_builtins = { - pkg.label.name: struct( - mod_flags = _bazel_builtin_mod_flags(ctx, pkg.label), - dep = _bazel_builtin_dep(pkg.label), + mod.label.name: struct( + mod_flags = _bazel_builtin_mod_flags(ctx, mod.label), + dep = _bazel_builtin_dep(mod.label), file = [ file - for file in pkgs[pkg.label.name].all_srcs.to_list() - if file.path == _bazel_builtin_file_name(ctx, pkg.label) + for file in mods[mod.label.name].all_srcs.to_list() + if file.path == _bazel_builtin_file_name(ctx, mod.label) ], ) - for pkg in ctx.attr.pkgs + for mod in ctx.attr.mods } expected = [] expected.extend(bazel_builtins["e"].mod_flags) - expected.extend(["--mod", "e:{}:{}".format(bazel_builtins["e"].dep, pkg_mains["e"].path)]) + expected.extend(["--mod", "e:{}:{}".format(bazel_builtins["e"].dep, mod_mains["e"].path)]) expected.extend(bazel_builtins["b"].mod_flags) - expected.extend(["--mod", "b:e,{}:{}".format(bazel_builtins["b"].dep, pkg_mains["b"].path)]) + expected.extend(["--mod", "b:e,{}:{}".format(bazel_builtins["b"].dep, mod_mains["b"].path)]) expected.extend(bazel_builtins["c"].mod_flags) - expected.extend(["--mod", "c:e,{}:{}".format(bazel_builtins["c"].dep, pkg_mains["c"].path)]) + expected.extend(["--mod", "c:e,{}:{}".format(bazel_builtins["c"].dep, mod_mains["c"].path)]) expected.extend(bazel_builtins["f"].mod_flags) - expected.extend(["--mod", "f:e,{}:{}".format(bazel_builtins["f"].dep, pkg_mains["f"].path)]) + expected.extend(["--mod", "f:e,{}:{}".format(bazel_builtins["f"].dep, mod_mains["f"].path)]) expected.extend(bazel_builtins["d"].mod_flags) - expected.extend(["--mod", "d:f,{}:{}".format(bazel_builtins["d"].dep, pkg_mains["d"].path)]) + expected.extend(["--mod", "d:f,{}:{}".format(bazel_builtins["d"].dep, mod_mains["d"].path)]) expected.extend(bazel_builtins["a"].mod_flags) - expected.extend(["--mod", "a:b,c,d,{}:{}".format(bazel_builtins["a"].dep, pkg_mains["a"].path)]) + expected.extend(["--mod", "a:b,c,d,{}:{}".format(bazel_builtins["a"].dep, mod_mains["a"].path)]) expected.extend(["--deps", "a"]) asserts.equals( @@ -179,8 +179,8 @@ def _nested_modules_test_impl(ctx): env, sets.make([ src - for pkg in pkgs.values() - for src in [pkg_mains[pkg.name]] + bazel_builtins[pkg.name].file + for mod in mods.values() + for src in [mod_mains[mod.name]] + bazel_builtins[mod.name].file ]), sets.make(inputs.to_list()), "zig_module_dependencies should capture all module files.", @@ -191,8 +191,8 @@ def _nested_modules_test_impl(ctx): _nested_modules_test = unittest.make( _nested_modules_test_impl, attrs = { - "pkgs": attr.label_list(providers = [ZigModuleInfo]), - "pkg_mains": attr.label_list(allow_files = True), + "mods": attr.label_list(providers = [ZigModuleInfo]), + "mod_mains": attr.label_list(allow_files = True), }, ) @@ -201,9 +201,9 @@ def module_info_test_suite(name): name, lambda name: _single_module_test( name = name, - pkg = "//zig/tests/multiple-sources-module:data", - pkg_main = "//zig/tests/multiple-sources-module:data.zig", - pkg_srcs = [ + mod = "//zig/tests/multiple-sources-module:data", + mod_main = "//zig/tests/multiple-sources-module:data.zig", + mod_srcs = [ "//zig/tests/multiple-sources-module:data/hello.zig", "//zig/tests/multiple-sources-module:data/world.zig", ], @@ -211,7 +211,7 @@ def module_info_test_suite(name): ), lambda name: _nested_modules_test( name = name, - pkgs = [ + mods = [ "//zig/tests/nested-modules:a", "//zig/tests/nested-modules:b", "//zig/tests/nested-modules:c", @@ -219,7 +219,7 @@ def module_info_test_suite(name): "//zig/tests/nested-modules:e", "//zig/tests/nested-modules:f", ], - pkg_mains = [ + mod_mains = [ "//zig/tests/nested-modules:a.zig", "//zig/tests/nested-modules:b.zig", "//zig/tests/nested-modules:c.zig", From de55a1d17db1777b9dbaf36942961b03ba0b06fa Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 21 Feb 2024 08:31:07 +0100 Subject: [PATCH 5/7] Store structured module arguments Zig's module system is changing drastically between releases. However, the direction seems to be a system where each module is represented on the command-line by a either multiple arguments in a specific order (0.12.0) or a single flag with a complex structure (0.11.0). Neither of those lends itself to tracking parts of the command-line separately in `depset` values to later on pass to `args.add_all`. Instead, we track modules in a `depset` of `struct` that carries all relevant information for each module, and later apply `args.add_all` using the `map_each` parameter to render the flags for each module. --- zig/private/providers/zig_module_info.bzl | 66 ++++++++++++++++------- zig/tests/module_info_test.bzl | 19 +++++-- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/zig/private/providers/zig_module_info.bzl b/zig/private/providers/zig_module_info.bzl index dc7c580..c1ead9a 100644 --- a/zig/private/providers/zig_module_info.bzl +++ b/zig/private/providers/zig_module_info.bzl @@ -13,7 +13,7 @@ instead the Zig compiler performs whole program compilation. FIELDS = { "name": "string, The import name of the module.", "canonical_name": "string, The canonical name may differ from the import name via remapping.", - "all_mods": "depset of string, All module CLI specifications required when depending on the module.", + "all_args": "depset of struct, All module CLI specifications required when depending on the module, to be rendered.", "all_srcs": "depset of File, All source files required when depending on the module.", } @@ -36,35 +36,62 @@ def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps): Returns: `ZigModuleInfo` """ - imports = [] - mods_transitive = [] + args_transitive = [] srcs_transitive = [] for dep in deps: - if dep.canonical_name != dep.name: - imports.append("{}={}".format(dep.name, dep.canonical_name)) - else: - imports.append(dep.name) - - mods_transitive.append(dep.all_mods) + args_transitive.append(dep.all_args) srcs_transitive.append(dep.all_srcs) - mod_direct = "{name}:{imports}:{src}".format( - name = canonical_name, - imports = ",".join(imports), - src = main.path, + arg_direct = _module_args( + canonical_name = canonical_name, + main = main, + deps = deps, ) - all_mods = depset(direct = [mod_direct], transitive = mods_transitive) - all_srcs = depset(direct = [main] + srcs + extra_srcs, transitive = srcs_transitive) + srcs_direct = [main] + srcs + extra_srcs + + all_args = depset(direct = [arg_direct], transitive = args_transitive) + all_srcs = depset(direct = srcs_direct, transitive = srcs_transitive) module = ZigModuleInfo( name = name, canonical_name = canonical_name, - all_mods = all_mods, + all_args = all_args, all_srcs = all_srcs, ) return module +def _dep_arg(dep): + if dep.canonical_name != dep.name: + return struct(name = dep.name, canonical_name = dep.canonical_name) + else: + return struct(name = dep.name) + +def _module_args(*, canonical_name, main, deps): + return struct( + name = canonical_name, + main = main.path, + deps = tuple([_dep_arg(dep) for dep in deps]), + ) + +def _render_args(args): + deps = [] + for dep in args.deps: + dep_spec = dep.name + + if hasattr(dep, "canonical_name"): + dep_spec += "=" + dep.canonical_name + + deps.append(dep_spec) + + spec = "{name}:{deps}:{main}".format( + name = args.name, + main = args.main, + deps = ",".join(deps), + ) + + return ["--mod", spec] + def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): """Collect inputs and flags for Zig module dependencies. @@ -74,7 +101,7 @@ def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): inputs: List of depset of File; mutable, Append the needed inputs to this list. args: Args; mutable, Append the needed Zig compiler flags to this object. """ - mods = [] + transitive_args = [] names = [] modules = [ @@ -88,8 +115,9 @@ def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): names.append("{}={}".format(module.name, module.canonical_name)) else: names.append(module.name) - mods.append(module.all_mods) + + transitive_args.append(module.all_args) inputs.append(module.all_srcs) - args.add_all(depset(transitive = mods), before_each = "--mod") + args.add_all(depset(transitive = transitive_args), map_each = _render_args) args.add_joined("--deps", names, join_with = ",") diff --git a/zig/tests/module_info_test.bzl b/zig/tests/module_info_test.bzl index da50d0a..ad35284 100644 --- a/zig/tests/module_info_test.bzl +++ b/zig/tests/module_info_test.bzl @@ -13,12 +13,25 @@ load( def _mock_args(): self_args = [] - def add_all(args, *, before_each = None): + def add_all(args, *, map_each = None): if type(args) == "depset": args = args.to_list() + + if map_each != None: + mapped = [] + + for arg in args: + result = map_each(arg) + if result == None or result == []: + continue + if type(result) == "list": + mapped.extend(result) + else: + mapped.append(result) + + args = mapped + for arg in args: - if before_each: - self_args.append(before_each) if type(arg) == "File": self_args.append(arg.path) else: From d4b53c79dc16127d7fcac4dcfd15acda16c8276a Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 21 Feb 2024 08:43:49 +0100 Subject: [PATCH 6/7] Factor out dependency name rendering --- zig/private/providers/zig_module_info.bzl | 25 ++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/zig/private/providers/zig_module_info.bzl b/zig/private/providers/zig_module_info.bzl index c1ead9a..246b3d5 100644 --- a/zig/private/providers/zig_module_info.bzl +++ b/zig/private/providers/zig_module_info.bzl @@ -74,15 +74,16 @@ def _module_args(*, canonical_name, main, deps): deps = tuple([_dep_arg(dep) for dep in deps]), ) -def _render_args(args): - deps = [] - for dep in args.deps: - dep_spec = dep.name +def _render_dep(dep): + dep_spec = dep.name + + if hasattr(dep, "canonical_name") and dep.canonical_name != dep.name: + dep_spec += "=" + dep.canonical_name - if hasattr(dep, "canonical_name"): - dep_spec += "=" + dep.canonical_name + return dep_spec - deps.append(dep_spec) +def _render_args(args): + deps = [_render_dep(dep) for dep in args.deps] spec = "{name}:{deps}:{main}".format( name = args.name, @@ -102,7 +103,7 @@ def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): args: Args; mutable, Append the needed Zig compiler flags to this object. """ transitive_args = [] - names = [] + deps_args = [] modules = [ dep[ZigModuleInfo] @@ -111,13 +112,9 @@ def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): ] + extra_deps for module in modules: - if module.canonical_name != module.name: - names.append("{}={}".format(module.name, module.canonical_name)) - else: - names.append(module.name) - + deps_args.append(_render_dep(module)) transitive_args.append(module.all_args) inputs.append(module.all_srcs) args.add_all(depset(transitive = transitive_args), map_each = _render_args) - args.add_joined("--deps", names, join_with = ",") + args.add_joined("--deps", deps_args, join_with = ",") From 2830f58631e21e4c9347e3796237005ac567fddd Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 21 Feb 2024 08:46:43 +0100 Subject: [PATCH 7/7] Change ZigModuleInfo field names --- zig/private/providers/zig_module_info.bzl | 20 ++++++++++---------- zig/tests/module_info_test.bzl | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/zig/private/providers/zig_module_info.bzl b/zig/private/providers/zig_module_info.bzl index 246b3d5..1543e01 100644 --- a/zig/private/providers/zig_module_info.bzl +++ b/zig/private/providers/zig_module_info.bzl @@ -13,8 +13,8 @@ instead the Zig compiler performs whole program compilation. FIELDS = { "name": "string, The import name of the module.", "canonical_name": "string, The canonical name may differ from the import name via remapping.", - "all_args": "depset of struct, All module CLI specifications required when depending on the module, to be rendered.", - "all_srcs": "depset of File, All source files required when depending on the module.", + "transitive_args": "depset of struct, All module CLI specifications required when depending on the module, including transitive dependencies, to be rendered.", + "transitive_inputs": "depset of File, All build inputs files required when depending on the module, including transitive dependencies.", } ZigModuleInfo = provider( @@ -40,8 +40,8 @@ def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps): srcs_transitive = [] for dep in deps: - args_transitive.append(dep.all_args) - srcs_transitive.append(dep.all_srcs) + args_transitive.append(dep.transitive_args) + srcs_transitive.append(dep.transitive_inputs) arg_direct = _module_args( canonical_name = canonical_name, @@ -50,13 +50,13 @@ def zig_module_info(*, name, canonical_name, main, srcs, extra_srcs, deps): ) srcs_direct = [main] + srcs + extra_srcs - all_args = depset(direct = [arg_direct], transitive = args_transitive) - all_srcs = depset(direct = srcs_direct, transitive = srcs_transitive) + transitive_args = depset(direct = [arg_direct], transitive = args_transitive) + transitive_inputs = depset(direct = srcs_direct, transitive = srcs_transitive) module = ZigModuleInfo( name = name, canonical_name = canonical_name, - all_args = all_args, - all_srcs = all_srcs, + transitive_args = transitive_args, + transitive_inputs = transitive_inputs, ) return module @@ -113,8 +113,8 @@ def zig_module_dependencies(*, deps, extra_deps = [], inputs, args): for module in modules: deps_args.append(_render_dep(module)) - transitive_args.append(module.all_args) - inputs.append(module.all_srcs) + transitive_args.append(module.transitive_args) + inputs.append(module.transitive_inputs) args.add_all(depset(transitive = transitive_args), map_each = _render_args) args.add_joined("--deps", deps_args, join_with = ",") diff --git a/zig/tests/module_info_test.bzl b/zig/tests/module_info_test.bzl index ad35284..06d0d4a 100644 --- a/zig/tests/module_info_test.bzl +++ b/zig/tests/module_info_test.bzl @@ -107,7 +107,7 @@ def _single_module_test_impl(ctx): bazel_builtin_file = [ file - for file in module.all_srcs.to_list() + for file in module.transitive_inputs.to_list() if file.path == _bazel_builtin_file_name(ctx, ctx.attr.mod.label) ] @@ -158,7 +158,7 @@ def _nested_modules_test_impl(ctx): dep = _bazel_builtin_dep(mod.label), file = [ file - for file in mods[mod.label.name].all_srcs.to_list() + for file in mods[mod.label.name].transitive_inputs.to_list() if file.path == _bazel_builtin_file_name(ctx, mod.label) ], )