-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
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
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") |
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
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
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], | ||
) |
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
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
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