Skip to content

Commit

Permalink
refactor(bazel): cleanup and simplify the toolchain config
Browse files Browse the repository at this point in the history
  • Loading branch information
Globegitter committed Jun 6, 2019
1 parent ecea166 commit 10b0a6f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 167 deletions.
37 changes: 4 additions & 33 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,9 @@ def _nodejs_binary_impl(ctx):
)
else:
node_tool = node_tool_info.target_tool_path
print(node_tool)
if node_tool_info.target_tool:
node_tool = _short_path_to_manifest_path(ctx, node_tool_info.target_tool.files.to_list()[0].short_path)

# print(node.short_path)
# print(_short_path_to_manifest_path(ctx, node.short_path))
# print(node_tool.path)
# print(node_tool.short_path)
# print(_short_path_to_manifest_path(ctx, node_tool.short_path))
# node_tool = _runfiles(ctx, node_tool_info.tool_target.files.to_list()[0])
node_tool_files += node_tool_info.target_tool.files.to_list()

print("------------")

# print(node_tool)
# print(node_tool.path)
# print(_short_path_to_manifest_path(ctx, node_tool))
# print(node.short_path)
# print(_short_path_to_manifest_path(ctx, node.short_path))
# print("------------")
print("node_tool_files", node_tool_files)

target_tool_args = node_tool_info.target_tool_args.files.to_list()[0]
node_tool = _short_path_to_manifest_path(ctx, node_tool_info.target_tool.file.short_path)
node_tool_files += node_tool_info.target_tool.file

substitutions = {
"TEMPLATED_args": " ".join([
Expand All @@ -196,8 +176,7 @@ def _nodejs_binary_impl(ctx):
"TEMPLATED_expected_exit_code": str(expected_exit_code),
# "TEMPLATED_node": _short_path_to_manifest_path(ctx, node.short_path),
"TEMPLATED_node": node_tool,
"TEMPLATED_repository_args": _short_path_to_manifest_path(ctx, target_tool_args.short_path),
# "TEMPLATED_repository_args": "",
"TEMPLATED_repository_args": _short_path_to_manifest_path(ctx, ctx.file._repository_args.short_path),
"TEMPLATED_script_path": script_path,
}
ctx.actions.expand_template(
Expand All @@ -207,10 +186,7 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset(node_tool_files + [ctx.outputs.loader, target_tool_args], transitive = [node_tool_info.target_tool_runfiles.files, sources, node_modules])
# runfiles = depset(node_tool_files + [ctx.outputs.loader] + node_modules, transitive = [sources])
# runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args] + ctx.files._node_runfiles, transitive = [sources, node_modules])
# runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args], transitive = [sources, node_modules])
runfiles = depset(node_tool_files + [ctx.outputs.loader, ctx.file._repository_args], transitive = [sources, node_modules])

return [DefaultInfo(
executable = ctx.outputs.script,
Expand Down Expand Up @@ -262,11 +238,6 @@ _NODEJS_EXECUTABLE_ATTRS = {
in TypeScript.""",
default = True,
),
# "node": attr.label(
# doc = """The node entry point target.""",
# default = Label("@nodejs//:node_bin"),
# allow_single_file = True,
# ),
"node_modules": attr.label(
doc = """The npm packages which should be available to `require()` during
execution.
Expand Down
6 changes: 3 additions & 3 deletions toolchains/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ toolchain(
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
toolchain = "@nodejs_config_linux_amd64//:toolchain",
toolchain = "@nodejs_linux_amd64_config//:toolchain",
toolchain_type = ":toolchain_type",
)

Expand All @@ -80,7 +80,7 @@ toolchain(
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
toolchain = "@nodejs_config_darwin_amd64//:toolchain",
toolchain = "@nodejs_darwin_amd64_config//:toolchain",
toolchain_type = ":toolchain_type",
)

Expand All @@ -90,6 +90,6 @@ toolchain(
"@bazel_tools//platforms:windows",
"@bazel_tools//platforms:x86_64",
],
toolchain = "@nodejs_config_windows_amd64//:toolchain",
toolchain = "@nodejs_windows_amd64_config//:toolchain",
toolchain_type = ":toolchain_type",
)
25 changes: 0 additions & 25 deletions toolchains/node/BUILD.path.tpl

This file was deleted.

30 changes: 0 additions & 30 deletions toolchains/node/BUILD.target.tpl

This file was deleted.

82 changes: 25 additions & 57 deletions toolchains/node/node_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,23 @@ Defines a repository rule for configuring the node binary.
load("//internal/common:os_name.bzl", "OS_ARCH_NAMES")

def _impl(repository_ctx):
substitutions = None
host_os = repository_ctx.os.name.lower()
if repository_ctx.attr.node_repository_names:
target_tool = ""
host_tool = ""
target_repo_name = ""
for repo_name in repository_ctx.attr.node_repository_names:
if repository_ctx.attr.os in repo_name:
target_tool = "@%s//:node" % repo_name
target_repo_name = repo_name

if host_os == "mac os x":
host_os = "darwin"

if "windows" in host_os:
host_os = "windows"

if host_os in repo_name:
host_tool = "@%s//:node" % repo_name
if repository_ctx.attr.target_tool and repository_ctx.attr.target_tool_path:
fail("Can only set one of target_tool or target_tool_path but both where set.")

if not target_tool or not host_tool:
fail(("No host_tool for Host OS '%s' and/or no target_tool for provided OS '%s' found with given nodejs" +
" repository names: %s.") % (host_os, repository_ctx.attr.os, repository_ctx.attr.node_repository_names))

substitutions = {
"%{ARCH}": "%s" % repository_ctx.attr.arch,
"%{NODE_HOST_TOOL}": "%s" % host_tool,
"%{NODE_TARGET_ARGS}": "@%s//:bin/node_args.sh" % target_repo_name,
"%{NODE_TARGET_RUNFILES}": "@%s//:node_runfiles" % target_repo_name,
"%{NODE_TARGET_TOOL}": "%s" % target_tool,
"%{OS}": "%s" % repository_ctx.attr.os,
}
template = Label("@build_bazel_rules_nodejs//toolchains/node:BUILD.target.tpl")

else:
node_tool_path = repository_ctx.attr.local_path or repository_ctx.which("node") or ""
substitutions = {"%{NODE_PATH}": "%s" % node_tool_path}
template = Label("@build_bazel_rules_nodejs//toolchains/node:BUILD.path.tpl")
host_os = repository_ctx.os.name.lower()
substitutions = {
"%{ARCH}": repository_ctx.attr.arch,
"%{OS}": repository_ctx.attr.os,
}
default_tool_path = repository_ctx.attr.target_tool_path or repository_ctx.which("node") or ""
substitutions["%{TOOL_ATTRS}"] = " target_tool_path = \"%s\"\n" % default_tool_path

if repository_ctx.attr.target_tool:
substitutions["%{TOOL_ATTRS}"] = " target_tool = \"%s\"\n" % ctx.attr.target_tool

repository_ctx.template(
"BUILD",
template,
Label("@build_bazel_rules_nodejs//toolchains/node:BUILD.tpl"),
substitutions,
False,
)
Expand All @@ -71,18 +46,6 @@ _node_configure = repository_rule(
mandatory = True,
doc = "Default target architecture",
),
"host_tool": attr.label(
doc = "Target for a downloaded nodejs binary for the host os.",
mandatory = False,
allow_single_file = True,
),
"host_tool_path": attr.string(
doc = "Absolute path to a pre-installed nodejs binary for the host os.",
mandatory = False,
),
# "node_repository_names": attr.string_list(
# mandatory = False,
# ),
"os": attr.string(
mandatory = True,
doc = "Default target OS",
Expand All @@ -99,11 +62,12 @@ _node_configure = repository_rule(
},
)

def node_configure(node_repository_name):
def node_configure(node_repository_names = None, **kwargs):
"""Creates an external repository with a node_toolchain target properly configured.
Args:
node_repository_name: list of strings
node_repository_names: list of strings
**kwargs: all the args of the _node_configure rules
"""

native.register_toolchains(
Expand All @@ -112,12 +76,16 @@ def node_configure(node_repository_name):
"@build_bazel_rules_nodejs//toolchains/node:node_windows_toolchain",
)

if node_repositories:
for os, arch in OS_ARCH_NAMES:
if node_repository_names:
for name in node_repository_names:
_node_configure(
name = "nodejs_config_%s_%s" % (os, arch),
name = "%s_config" % name,
os = os,
arch = arch,
host_tool = "@nodejs//:node"
node_repository_names = node_repositories
host_tool = "@nodejs//:node_bin",
target_tool = "@%s//:node_bin" % name,
)
else:
_node_configure(
**kwargs
)
19 changes: 0 additions & 19 deletions toolchains/node/node_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ node_toolchain = rule(
mandatory = True,
doc = "Default target architecture",
),
"host_tool": attr.label(
doc = "Target for a downloaded nodejs binary for the host os.",
mandatory = False,
allow_single_file = True,
),
"host_tool_path": attr.string(
doc = "Absolute path to a pre-installed nodejs binary for the host os.",
mandatory = False,
),
"os": attr.string(
mandatory = True,
doc = "Default target OS",
Expand All @@ -67,19 +58,9 @@ node_toolchain = rule(
mandatory = False,
allow_single_file = True,
),
"target_tool_args": attr.label(
doc = "Target for a downloaded nodejs binary for the target os.",
mandatory = False,
allow_single_file = True,
),
"target_tool_path": attr.string(
doc = "Absolute path to a pre-installed nodejs binary for the target os.",
mandatory = False,
),
"target_tool_runfiles": attr.label(
doc = "Target for a downloaded nodejs binary for the target os.",
mandatory = False,
allow_files = False,
),
},
)

0 comments on commit 10b0a6f

Please sign in to comment.