Skip to content

Commit

Permalink
working packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewyx committed Nov 10, 2024
1 parent 1c99c3b commit ec9d6f2
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_pkg", version = "1.0.1")
13 changes: 13 additions & 0 deletions src/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ go_rules_dependencies()

go_register_toolchains()

http_archive(
name = "rules_pkg",
strip_prefix = "rules_pkg-1.0.1/pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz",
],
sha256 = "d20c951960ed77cb7b341c2a59488534e494d5ad1d30c4818c736d57772a9fef",
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()

# Needed for bazel buildifier
http_archive(
name = "bazel_gazelle",
Expand Down
97 changes: 97 additions & 0 deletions src/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
load("@rules_pkg//:providers.bzl", "PackageFilesInfo", "PackageSymlinkInfo", "PackageFilegroupInfo")

# No idea how this works, refer to:
# https://gist.github.com/pauldraper/7bc811ffbef6d3f3d4a4bb01afa9808f

# For maintaining Bazel runfile tree structure during packaging
def _runfile_path(workspace_name, file):
path = file.short_path
return path[len("../"):] if path.startswith("../") else "%s/%s" % (workspace_name, path)

def _runfiles_pkg_files(workspace_name, runfiles):
files = {}
for file in runfiles.files.to_list():
files[_runfile_path(workspace_name, file)] = file
for file in runfiles.symlinks.to_list():
files[file.path] = "%s/%s" % (workspace_name, files.target_file)
for file in runfiles.root_symlinks.to_list():
files[file.path] = files.target_file

return PackageFilesInfo(
dest_src_map = files,
attributes = { "mode": "0755" },
)

def _pkg_runfiles_impl(ctx):
runfiles = ctx.attr.runfiles[DefaultInfo]
label = ctx.label
workspace_name = ctx.workspace_name

runfiles_files = _runfiles_pkg_files(workspace_name, runfiles.default_runfiles)

pkg_filegroup_info = PackageFilegroupInfo(
pkg_dirs = [],
pkg_files = [(runfiles_files, label)],
pkg_symlinks = [],
)

default_info = DefaultInfo(files = depset(runfiles_files.dest_src_map.values()))

return [default_info, pkg_filegroup_info]

pkg_runfiles = rule(
implementation = _pkg_runfiles_impl,
attrs = {
"runfiles": attr.label(
doc = "Runfiles.",
mandatory = True,
),
},
provides = [PackageFilegroupInfo],
)

def _pkg_executable_impl(ctx):
bin = ctx.attr.bin[DefaultInfo]
bin_executable = ctx.executable.bin
path = ctx.attr.path
label = ctx.label
workspace_name = ctx.workspace_name

runfiles_files = _runfiles_pkg_files(workspace_name, bin.default_runfiles)
runfiles_files = PackageFilesInfo(
dest_src_map = { "%s.runfiles/%s" % (path, p): file for p, file in runfiles_files.dest_src_map.items() },
attributes = runfiles_files.attributes,
)

executable_symlink = PackageSymlinkInfo(
attributes = { "mode": "0755" },
destination = path,
source = "%s.runfiles/%s" % (path, _runfile_path(workspace_name, bin_executable)),
)

pkg_filegroup_info = PackageFilegroupInfo(
pkg_dirs = [],
pkg_files = [(runfiles_files, label)],
pkg_symlinks = [(executable_symlink, label)],
)

default_info = DefaultInfo(files = depset(runfiles_files.dest_src_map.values()))

return [default_info, pkg_filegroup_info]

pkg_executable = rule(
implementation = _pkg_executable_impl,
attrs = {
"bin": attr.label(
doc = "Executable.",
executable = True,
cfg = "target",
mandatory = True,
),
"path": attr.string(
doc = "Packaged path of executable. (Runfiles tree will be at <path>.runfiles.)",
mandatory = True,
),
},
provides = [PackageFilegroupInfo],
)
4 changes: 1 addition & 3 deletions src/software/embedded/ansible/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package(default_visibility = ["//visibility:public"])
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@ansible_deps//:requirements.bzl", "requirement")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

compile_pip_requirements(
name = "requirements",
Expand All @@ -28,8 +27,7 @@ py_binary(
"//software/embedded/redis",
"//software/embedded/services:robot_auto_test",
"//software/power:powerloop_tar",
"//software/embedded/robot_diagnostics_cli:robot_diagnostics_cli_zip",
# INSERT SCRIPT HERE
"//software/embedded/robot_diagnostics_cli:robot_diagnostics_cli_tar",
],
deps = [
requirement("ansible"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
binary_paths:
thunderloop: ../../thunderloop_main
zip_paths:
diagnostics: ../../robot_diagnostics_cli/robot_diagnostics_cli_zip.zip
diagnostics: ../../robot_diagnostics_cli/robot_diagnostics_cli_tar.tar
# // ADD SOMETHINGS HERE

tasks:
Expand Down Expand Up @@ -80,7 +80,7 @@
tags: always

- name: Unzipping Binaries
command: 'unzip ~/thunderbots_binaries/*.zip'
command: 'tar xopf ~/thunderbots_binaries/*.tar'
args:
chdir: ~/thunderbots_binaries

Expand Down
19 changes: 13 additions & 6 deletions src/software/embedded/robot_diagnostics_cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ load("@rules_python//python:defs.bzl", "py_binary")
load("@robot_diagnostics_cli_deps//:requirements.bzl", "requirement")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//:defs.bzl", "pkg_executable")

compile_pip_requirements(
name = "requirements",
Expand All @@ -27,12 +29,17 @@ py_binary(
requirement("protobuf"),
requirement("redis"),
],
python_version = "PY3",
)

genrule(
name = "robot_diagnostics_cli_zip",
outs = ["robot_diagnostics_cli_zip.zip"],
srcs = [":robot_diagnostics_cli"],
cmd = "zip -r $@ $(location :robot_diagnostics_cli)*",
pkg_tar(
name = "robot_diagnostics_cli_tar",
srcs = [
":robot_diagnostics_cli_pkg",
],
)

pkg_executable(
name = "robot_diagnostics_cli_pkg",
bin = ":robot_diagnostics_cli",
path = "robot_diagnostics_cli",
)

0 comments on commit ec9d6f2

Please sign in to comment.