Last active
November 18, 2023 02:22
-
-
Save pauldraper/7bc811ffbef6d3f3d4a4bb01afa9808f to your computer and use it in GitHub Desktop.
Bazel pkg_runfiles and pkg_executable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@rules_pkg//pkg:tar.bzl", "pkg_tar") | |
load(":rules.bzl", "pkg_executable") | |
# | |
# Package executable :bin to tar | |
# | |
pkg_executable( | |
name = "pkg", | |
bin = ":bin", | |
path = "bin", | |
) | |
pkg_tar( | |
name = "tar", | |
srcs = [":pkg"], | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright Paul Draper 2022 | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@rules_pkg//:providers.bzl", "PackageFilesInfo", "PackageSymlinkInfo", "PackageFilegroupInfo") | |
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, | |
target = "%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], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @pauldraper, this works for me too. Would you mind specifying a license under which this can be used?