Skip to content

Commit 3dffe47

Browse files
authored
Add pkg_install.destdir_flag. (#990)
Add pkg_install.destdir_flag. This allows `pkg_install(destdir)` to be set from a string_flag. The use case is to push destdir to a flag you can set in .bazelrc instead of pushing an environment variable through the build.
1 parent 66521ac commit 3dffe47

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

pkg/install.bzl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This module provides an interface (`pkg_install`) for creating a `bazel
1717
run`-able installation script.
1818
"""
1919

20+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
2021
load("@rules_python//python:defs.bzl", "py_binary")
2122
load("//pkg:providers.bzl", "PackageDirsInfo", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo")
2223
load("//pkg/private:pkg_files.bzl", "create_mapping_context_from_ctx", "process_src", "write_manifest")
@@ -34,6 +35,11 @@ def _pkg_install_script_impl(ctx):
3435

3536
manifest_file = ctx.actions.declare_file(ctx.attr.name + "-install-manifest.json")
3637

38+
destdir = ctx.attr.destdir
39+
if ctx.attr.destdir_flag:
40+
if BuildSettingInfo in ctx.attr.destdir_flag:
41+
destdir = ctx.attr.destdir_flag[BuildSettingInfo].value
42+
3743
# Write out the manifest in terms of "short" paths, which are those expected
3844
# when you make `bazel run`nable binaries).
3945
#
@@ -62,7 +68,7 @@ def _pkg_install_script_impl(ctx):
6268
"{WORKSPACE_NAME}": ctx.workspace_name,
6369
# Used to annotate --help with "bazel run //path/to/your:installer"
6470
"{TARGET_LABEL}": label_str,
65-
"{DEFAULT_DESTDIR}": ctx.attr.destdir,
71+
"{DEFAULT_DESTDIR}": destdir,
6672
},
6773
is_executable = True,
6874
)
@@ -100,6 +106,7 @@ _pkg_install_script = rule(
100106
doc = "Source mapping/grouping targets",
101107
),
102108
"destdir": attr.string(),
109+
"destdir_flag": attr.label(doc = "string flag to obtain destdir from"),
103110
# This is private for now -- one could perhaps imagine making this
104111
# public, but that would require more documentation of the underlying
105112
# scripts and expected interfaces.
@@ -111,7 +118,7 @@ _pkg_install_script = rule(
111118
executable = True,
112119
)
113120

114-
def pkg_install(name, srcs, destdir = None, **kwargs):
121+
def pkg_install(name, srcs, destdir = None, destdir_flag = None, **kwargs):
115122
"""Create an installer script from pkg_filegroups and friends.
116123
117124
This macro allows users to create `bazel run`nable installation scripts
@@ -170,14 +177,18 @@ def pkg_install(name, srcs, destdir = None, **kwargs):
170177
171178
If this is an absolute path, it is used as-is. If this is a relative
172179
path, it is interpreted against `BUILD_WORKSPACE_DIRECTORY`.
180+
destdir_flag: A string_flag target used to obtain the value of destdir.
173181
**kwargs: common rule attributes
174182
175183
"""
184+
if destdir and destdir_flag:
185+
fail("You may only set on of destdir or destdir_flag")
176186

177187
_pkg_install_script(
178188
name = name + "_install_script",
179189
srcs = srcs,
180190
destdir = destdir,
191+
destdir_flag = destdir_flag,
181192
**kwargs
182193
)
183194

tests/install/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
1516
load("@rules_python//python:defs.bzl", "py_test")
1617
load("//pkg:install.bzl", "pkg_install")
1718
load("//pkg:mappings.bzl", "pkg_attributes", "pkg_files", "pkg_mkdirs", "pkg_mklink")
@@ -25,6 +26,7 @@ py_test(
2526
args = ["-v"],
2627
data = [
2728
":test_installer",
29+
":test_installer_flag",
2830
],
2931
imports = ["../.."],
3032
main = "test.py",
@@ -115,3 +117,16 @@ pkg_mklink(
115117
link_name = "/lib/fake.so.1",
116118
target = "fake.so.1.2.3",
117119
)
120+
121+
string_flag(
122+
name = "install_dir",
123+
build_setting_default = "FromFlag",
124+
)
125+
126+
pkg_install(
127+
name = "test_installer_flag",
128+
srcs = [
129+
":artifact-in-owned-dir",
130+
],
131+
destdir_flag = ":install_dir",
132+
)

tests/install/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,25 @@ def test_manifest_matches(self):
211211
self.assertEqual(num_missing, 0)
212212

213213

214+
class DestdirFlagTest(unittest.TestCase):
215+
216+
@classmethod
217+
def setUpClass(cls):
218+
super().setUpClass()
219+
r = runfiles.Create()
220+
cls.script_path = r.Rlocation(
221+
f"rules_pkg/tests/install/test_installer_flag_install_script.py"
222+
)
223+
224+
def test_installer_build_from_flag(self):
225+
# This is about as good as we can do without a lot of scaffolding.
226+
# To test the flag, we would have to invoke bazel from bazel, which is messy.
227+
with open(self.script_path) as installer:
228+
script = installer.read()
229+
# Default value from BUILD file.
230+
self.assertIn("FromFlag", script)
231+
232+
214233
class WipeTest(PkgInstallTestBase):
215234
def test_wipe(self):
216235
self.installdir.mkdir(exist_ok=True)

0 commit comments

Comments
 (0)