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

Go port and windows support #57

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 26 additions & 7 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
# we require symlinks
startup --windows_enable_symlinks

common --lockfile_mode=off

build --remote_cache=grpcs://remote.buildbuddy.io
build --remote_timeout=3600
# Automatically apply --config=linux, --config=windows etc
common --enable_platform_specific_config

# Don’t want to push a rules author to update their deps if not needed.
# https://bazel.build/reference/command-line-reference#flag--check_direct_dependencies
# https://bazelbuild.slack.com/archives/C014RARENH0/p1691158021917459?thread_ts=1691156601.420349&cid=C014RARENH0
common --check_direct_dependencies=off

common --remote_cache=grpcs://remote.buildbuddy.io
common --remote_timeout=3600

# TODO: Remove once stardoc's protobuf doesn't have warnings
build --host_per_file_copt="external/.*protobuf.*@-Wno-everything"
build --per_file_copt="external/.*protobuf.*@-Wno-everything"
common:linux --host_per_file_copt="external/.*protobuf.*@-Wno-everything"
common:linux --per_file_copt="external/.*protobuf.*@-Wno-everything"

# TODO: Remove once stardoc's zlib doesn't have warnings
build --host_per_file_copt="external/.*zlib.*@-Wno-everything"
build --per_file_copt="external/.*zlib.*@-Wno-everything"
common:linux --host_per_file_copt="external/.*zlib.*@-Wno-everything"
common:linux --per_file_copt="external/.*zlib.*@-Wno-everything"

# rules_go's go sdk cgo compiler doesn't build with some compilers
# that may be configured by bazel, but we don't need it; disable
common --@rules_go//go/config:pure

common --test_output=errors

test --test_output=errors
# PATH varies when running vs testing, this makes it more like running to validate the actual behavior. Specifically '.' is included for tests but not runs
common:linux --test_env=PATH=/usr/bin:/bin
common:mac --test_env=PATH=/usr/bin:/bin
17 changes: 16 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "rules_python", version = "0.27.1")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.7")

bazel_dep(
name = "stardoc",
version = "0.6.2",
dev_dependency = True,
repo_name = "io_bazel_stardoc",
)

# Hermetic go toolchain
bazel_dep(name = "rules_go", version = "0.47.1", dev_dependency = True)
bazel_dep(name = "gazelle", version = "0.36.0", dev_dependency = True)
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(
version = "1.22.3",
# explicitly specify SDK names/checksums to avoid a query which fails in airgapped builds
# to update this, view checksums at https://go.dev/dl/?mode=json&include=all
sdks = {
"linux_amd64": ("go1.22.3.linux-amd64.tar.gz", "8920ea521bad8f6b7bc377b4824982e011c19af27df88a815e3586ea895f1b36"),
"windows_amd64": ("go1.22.3.windows-amd64.zip", "cab2af6951a6e2115824263f6df13ff069c47270f5788714fa1d776f7f60cb39"),
},
)
105 changes: 72 additions & 33 deletions command.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ load(
"//internal:constants.bzl",
"CommandInfo",
"RUNFILES_PREFIX",
"rlocation_path",
"update_attrs",
)
load("//internal/bazel-lib:windows_utils.bzl", "BATCH_RLOCATION_FUNCTION")
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")

_COMMAND_LAUNCHER_BAT_TMPL = """@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set RUNFILES_LIB_DEBUG=0
{BATCH_RLOCATION_FUNCTION}
{envs}

call :rlocation "{command}" command_path
::echo rlocation({command}) returns %command_path%
::echo command bat launcher
::echo RUNFILES_MANIFEST_FILE=!RUNFILES_MANIFEST_FILE!
::echo launching: {exec}%command_path% {args}
{exec}%command_path% {args}
"""

def _force_opt_impl(_settings, _attr):
return {"//command_line_option:compilation_mode": "opt"}
Expand All @@ -22,45 +38,65 @@ _force_opt = transition(
)

def _command_impl(ctx):
runfiles = ctx.runfiles().merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles)

for data_dep in ctx.attr.data:
default_runfiles = data_dep[DefaultInfo].default_runfiles
if default_runfiles != None:
runfiles = runfiles.merge(default_runfiles)

is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
command = ctx.attr.command if type(ctx.attr.command) == "Target" else ctx.attr.command[0]
default_info = command[DefaultInfo]
executable = default_info.files_to_run.executable

default_runfiles = default_info.default_runfiles
if default_runfiles != None:
runfiles = runfiles.merge(default_runfiles)
executable = command[DefaultInfo].files_to_run.executable

expansion_targets = ctx.attr.data
shell_type = "bash" if not is_windows or executable.extension in ["bash", "sh"] else "cmd"
if (shell_type == "bash"):
str_args = [
"%s" % shell.quote(ctx.expand_location(v, targets = expansion_targets))
for v in ctx.attr.arguments
]
else:
str_args = [
"%s" % shell.quote(ctx.expand_location(v, targets = expansion_targets))
for v in ctx.attr.arguments
]

if not is_windows:
str_env = [
"export %s=%s" % (k, shell.quote(ctx.expand_location(v, targets = expansion_targets)))
for k, v in ctx.attr.environment.items()
]
command_exec = " ".join(["exec $(rlocation %s)" % shell.quote(to_rlocation_path(ctx, executable))] + str_args + ['"$@"\n'])
#print(command_exec)
launcher = ctx.actions.declare_file(ctx.label.name + ".bash")
ctx.actions.write(
output = launcher,
content = "\n".join([RUNFILES_PREFIX] + str_env + [command_exec]),
is_executable = True,
)
else:
str_env = [
"set \"%s=%s\"" % (k, ctx.expand_location(v, targets = expansion_targets))
for k, v in ctx.attr.environment.items()
]
launcher = ctx.actions.declare_file(ctx.label.name + ".bat")
ctx.actions.write(
output = launcher,
content = _COMMAND_LAUNCHER_BAT_TMPL.format(
envs = "\n".join(str_env),
exec = "%BAZEL_SH% " if shell_type == "bash" else "",
command = to_rlocation_path(ctx, executable),
args = " ".join(str_args),
BATCH_RLOCATION_FUNCTION = BATCH_RLOCATION_FUNCTION,
),
is_executable = True,
)

str_env = [
"export %s=%s" % (k, shell.quote(ctx.expand_location(v, targets = expansion_targets)))
for k, v in ctx.attr.environment.items()
]
str_args = [
"%s" % shell.quote(ctx.expand_location(v, targets = expansion_targets))
for v in ctx.attr.arguments
]
command_exec = " ".join(["exec $(rlocation %s)" % shell.quote(rlocation_path(ctx, executable))] + str_args + ['"$@"\n'])

out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
ctx.actions.write(
output = out_file,
content = "\n".join([RUNFILES_PREFIX] + str_env + [command_exec]),
is_executable = True,
)
runfiles = ctx.runfiles(files = ctx.files.data + ctx.files._bash_runfiles + [executable])
runfiles = runfiles.merge_all([
d[DefaultInfo].default_runfiles
for d in ctx.attr.data + [command]
])

providers = [
DefaultInfo(
files = depset([out_file]),
runfiles = runfiles.merge(ctx.runfiles(files = ctx.files.data + [executable])),
executable = out_file,
files = depset([launcher]),
runfiles = runfiles,
executable = launcher,
),
]

Expand Down Expand Up @@ -109,6 +145,9 @@ def command_with_transition(cfg, allowlist = None, doc = None):
"_bash_runfiles": attr.label(
default = Label("@bazel_tools//tools/bash/runfiles"),
),
"_windows_constraint": attr.label(
default = "@platforms//os:windows"
),
}

return rule(
Expand Down
11 changes: 0 additions & 11 deletions internal/BUILD
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "multirun",
srcs = ["multirun.py"],
python_version = "PY3",
visibility = ["//visibility:public"],
deps = [
"@rules_python//python/runfiles",
],
)

bzl_library(
name = "constants",
Expand Down
11 changes: 11 additions & 0 deletions internal/bazel-lib/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Significant update to windows_utils.bzl from bazel-lib
# Now contains v3 runfiles logic
# Would like to contribute this back to bazel-lib

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "windows_utils",
srcs = ["windows_utils.bzl"],
visibility = ["//:__pkg__"],
)
Loading