Skip to content

Commit

Permalink
Merge pull request #233 from aherrmann/zig-mod
Browse files Browse the repository at this point in the history
chore: refactor ZigModuleInfo
  • Loading branch information
aherrmann authored Feb 21, 2024
2 parents 6068fea + 2830f58 commit 1fe88a2
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 82 deletions.
14 changes: 7 additions & 7 deletions zig/private/common/bazel_builtin.bzl
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
96 changes: 81 additions & 15 deletions zig/private/providers/zig_module_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,86 @@ 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.",
"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(
fields = FIELDS,
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`
"""
args_transitive = []
srcs_transitive = []

for dep in deps:
args_transitive.append(dep.transitive_args)
srcs_transitive.append(dep.transitive_inputs)

arg_direct = _module_args(
canonical_name = canonical_name,
main = main,
deps = deps,
)
srcs_direct = [main] + srcs + extra_srcs

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,
transitive_args = transitive_args,
transitive_inputs = transitive_inputs,
)

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_dep(dep):
dep_spec = dep.name

if hasattr(dep, "canonical_name") and dep.canonical_name != dep.name:
dep_spec += "=" + dep.canonical_name

return dep_spec

def _render_args(args):
deps = [_render_dep(dep) for dep in args.deps]

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.
Expand All @@ -33,8 +102,8 @@ 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 = []
names = []
transitive_args = []
deps_args = []

modules = [
dep[ZigModuleInfo]
Expand All @@ -43,12 +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)
mods.append(module.all_mods)
inputs.append(module.all_srcs)

args.add_all(depset(transitive = mods), before_each = "--mod")
args.add_joined("--deps", names, join_with = ",")
deps_args.append(_render_dep(module))
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 = ",")
35 changes: 8 additions & 27 deletions zig/private/zig_module.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand All @@ -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]
Expand Down
Loading

0 comments on commit 1fe88a2

Please sign in to comment.