Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: windows #45

Merged
merged 15 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ common --lockfile_mode=off

test --test_output=errors
test --test_summary=terse

# best practice to disable
common --nolegacy_external_runfiles

# off by default on windows, required for this module
startup --windows_enable_symlinks

# off by default on windows, required for this module
common --enable_runfiles
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.0
7.1.0
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ jobs:
{"bazelversion": "5.4.0"},
{"bazelversion": "6.4.0"},
]
# this ruleset only supports linux and macos
exclude_windows: true
integration-tests:
runs-on: ubuntu-latest
steps:
Expand Down
9 changes: 9 additions & 0 deletions examples/module/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
common --enable_bzlmod

common --lockfile_mode=off

# best practice to disable
common --nolegacy_external_runfiles

# off by default on windows, required for this module
startup --windows_enable_symlinks

# off by default on windows, required for this module
common --enable_runfiles
2 changes: 1 addition & 1 deletion examples/module/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.2
7.1.0
18 changes: 16 additions & 2 deletions examples/module/multitool.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"sha256": "c8a09143e9fe6eccc4b27a6be92c5929e5a78034a8d0b4c43dbed4ee539ec903",
"os": "linux",
"cpu": "x86_64"
},
{
"kind": "file",
"url": "https://github.com/bazel-contrib/target-determinator/releases/download/v0.25.0/target-determinator.windows.amd64.exe",
"sha256": "e14fd75e33d193f579505cf3e641e07025904fc027686e13e154ba8e10ac0f58",
"os": "windows",
"cpu": "x86_64"
}
]
},
Expand All @@ -47,9 +54,17 @@
"kind": "archive",
"url": "https://github.com/cli/cli/releases/download/v2.44.1/gh_2.44.1_linux_amd64.tar.gz",
"sha256": "f11eefb646768e3f53e2185f6d3b01b4cb02112c2c60e65a4b5875150287ff97",
"file": "gh_2.44.1_linux_amd64/bin",
"file": "gh_2.44.1_linux_amd64/bin/gh",
"os": "linux",
"cpu": "x86_64"
},
{
"kind": "archive",
"url": "https://github.com/cli/cli/releases/download/v2.44.1/gh_2.44.1_windows_amd64.zip",
"sha256": "5a19a334d5032cfcb01e71e418c5e355104f7afd9129daf8939b729ffa46aa08",
"file": "gh_2.44.1_windows_amd64/bin/gh.exe",
"os": "windows",
"cpu": "x86_64"
}
]
},
Expand Down Expand Up @@ -79,7 +94,6 @@
"os": "linux",
"cpu": "x86_64"
}

]
}
}
9 changes: 9 additions & 0 deletions examples/workspace/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
common --enable_bzlmod

common --lockfile_mode=off

# best practice to disable
common --nolegacy_external_runfiles

# off by default on windows, required for this module
startup --windows_enable_symlinks

# off by default on windows, required for this module
common --enable_runfiles
2 changes: 1 addition & 1 deletion examples/workspace/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.2
7.1.0
7 changes: 7 additions & 0 deletions examples/workspace/multitool.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"sha256": "c8a09143e9fe6eccc4b27a6be92c5929e5a78034a8d0b4c43dbed4ee539ec903",
"os": "linux",
"cpu": "x86_64"
},
{
"kind": "file",
"url": "https://github.com/bazel-contrib/target-determinator/releases/download/v0.25.0/target-determinator.windows.amd64.exe",
"sha256": "e14fd75e33d193f579505cf3e641e07025904fc027686e13e154ba8e10ac0f58",
"os": "windows",
"cpu": "x86_64"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion lockfile.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "object",
"definitions": {
"supported_os": {
"enum": ["macos", "linux"]
"enum": ["macos", "linux", "windows"]
},
"supported_cpu": {
"enum": ["x86_64", "arm64"]
Expand Down
1 change: 1 addition & 0 deletions multitool/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files([
"cwd.template.bat",
"cwd.template.sh",
])

Expand Down
17 changes: 13 additions & 4 deletions multitool/private/cwd.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
"cwd: a rule for executing an executable in the BUILD_WORKING_DIRECTORY"

def _cwd_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)
# This algorithm requires --enable_runfiles (enabled by default on non-windows)
template = ctx.file._template_sh
wrapper_name = ctx.label.name
tool_short_path = ctx.file.tool.short_path
if ctx.file.tool.extension == "exe":
template = ctx.file._template_bat
wrapper_name = wrapper_name + ".bat"
tool_short_path = tool_short_path.replace("/", "\\")
mark-thm marked this conversation as resolved.
Show resolved Hide resolved
output = ctx.actions.declare_file(wrapper_name)
ctx.actions.expand_template(
template = ctx.file._template,
template = template,
output = output,
substitutions = {
"{{tool}}": ctx.file.tool.short_path,
"{{tool}}": tool_short_path,
},
)
return [DefaultInfo(executable = output, runfiles = ctx.runfiles(files = [ctx.file.tool]))]
Expand All @@ -15,7 +23,8 @@ cwd = rule(
implementation = _cwd_impl,
attrs = {
"tool": attr.label(mandatory = True, allow_single_file = True, executable = True, cfg = "exec"),
"_template": attr.label(default = "//multitool/private:cwd.template.sh", allow_single_file = True),
"_template_sh": attr.label(default = "//multitool/private:cwd.template.sh", allow_single_file = True),
"_template_bat": attr.label(default = "//multitool/private:cwd.template.bat", allow_single_file = True),
},
executable = True,
)
3 changes: 3 additions & 0 deletions multitool/private/cwd.template.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@set tool=%cd%\{{tool}}
@cd %BUILD_WORKING_DIRECTORY%
@%tool% %*
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# generated by multitool

exports_files(glob(include=["*_executable"]))
exports_files(glob(include=["*_executable", "*_executable.exe"]))
13 changes: 11 additions & 2 deletions multitool/private/hub_repo_template/toolchain_info.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@ def _toolchain_info_impl(ctx):
executable = ctx.file.executable,
cpu = ctx.attr.cpu,
os = ctx.attr.os,
ext = ctx.attr.ext,
),
]

toolchain_info = rule(
attrs = dict(
executable = attr.label(allow_single_file = True),
cpu = attr.string(mandatory = True, values = ["arm64", "x86_64"]),
os = attr.string(mandatory = True, values = ["linux", "macos"]),
os = attr.string(mandatory = True, values = ["linux", "macos", "windows"]),
ext = attr.string(mandatory = True, values = ["", ".exe"]),
),
implementation = _toolchain_info_impl,
)

def _extension(os):
if os == "windows":
return ".exe"
return ""

def declare_toolchain(name, os, cpu, toolchain_type):
ext = _extension(os)
toolchain_info(
name = "{name}_{os}_{cpu}_toolchain_info".format(name=name, os=os, cpu=cpu),
executable = "@@{hub_name}.{os}_{cpu}//tools/{name}:{os}_{cpu}_executable".format(name=name, os=os, cpu=cpu),
executable = "@@{hub_name}.{os}_{cpu}//tools/{name}:{os}_{cpu}_executable{ext}".format(name=name, os=os, cpu=cpu, ext=ext),
os = os,
cpu = cpu,
ext = ext,
)

native.toolchain(
Expand Down
2 changes: 1 addition & 1 deletion multitool/private/hub_repo_tool_template/tool.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _TOOLCHAIN_TYPE = "//tools/{name}:toolchain_type"

def _tool_impl(ctx):
toolchain = ctx.toolchains[_TOOLCHAIN_TYPE]
output = ctx.actions.declare_file(ctx.label.name)
output = ctx.actions.declare_file(ctx.label.name + toolchain.ext)
ctx.actions.symlink(output = output, target_file = toolchain.executable)
return [DefaultInfo(executable = output)]

Expand Down
22 changes: 20 additions & 2 deletions multitool/private/multitool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,24 @@ _SUPPORTED_ENVS = [
("linux", "x86_64"),
("macos", "arm64"),
("macos", "x86_64"),
("windows", "arm64"),
("windows", "x86_64"),
peakschris marked this conversation as resolved.
Show resolved Hide resolved
]

def _check(condition, message):
"fails iff condition is False and emits message"
if not condition:
fail(message)

def _check_version(os):
# skip version check on windows if we don't have a release version. We can't tell from a hash what features we have.
if os == "windows" and native.bazel_version:
version = native.bazel_version.split(".")
if int(version[0]) > 7 or (int(version[0]) == 7 and int(version[1]) >= 1):
pass
else:
fail("rules_multitool: windows artifacts require bazel 7.1+; current bazel is " + native.bazel_version)

def _load_tools(rctx):
tools = {}
for lockfile in rctx.attr.lockfiles:
Expand All @@ -74,7 +85,7 @@ def _load_tools(rctx):
for tool_name, tool in tools.items():
for binary in tool["binaries"]:
_check(
binary["os"] in ["linux", "macos"],
binary["os"] in ["linux", "macos", "windows"],
"{tool_name}: Unknown os '{os}'".format(
tool_name = tool_name,
os = binary["os"],
Expand All @@ -87,6 +98,7 @@ def _load_tools(rctx):
cpu = binary["cpu"],
),
)
_check_version(binary["os"])

return tools

Expand All @@ -97,6 +109,11 @@ def _feature_sensitive_args(binary):

return args

def _extension(os):
if os == "windows":
return ".exe"
return ""

def _env_specific_tools_impl(rctx):
tools = _load_tools(rctx)

Expand All @@ -105,10 +122,11 @@ def _env_specific_tools_impl(rctx):
if binary["os"] != rctx.attr.os or binary["cpu"] != rctx.attr.cpu:
continue

target_executable = "tools/{tool_name}/{os}_{cpu}_executable".format(
target_executable = "tools/{tool_name}/{os}_{cpu}_executable{ext}".format(
tool_name = tool_name,
cpu = binary["cpu"],
os = binary["os"],
ext = _extension(binary["os"]),
)

if binary["kind"] == "file":
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Define a [lockfile](lockfile.schema.json) that references the tools to load:
"kind": "file",
"url": "https://...",
"sha256": "sha256 of the file",
"os": "linux|macos",
"os": "linux|macos|windows",
"cpu": "x86_64|arm64"
}
]
Expand Down
Loading