2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-27 17:43:27 +00:00

Add support for select in write_source_file.bzl

Add additional argument to write_source_file macro: `out_file_is_select = False`, that enables support for defining select dictionary as out_file. For example:
`
write_source_file(
    name = "write_foo_c",
    in_file = ":foo.c",
    out_file = {
        "//conditions:default": "//workspace/subdir:foo.c",
    },
    executable = False,
    additional_update_targets = [],
    suggested_update_target = ":write_foo_h",
    diff_test = True,
    check_that_out_file_exists = False,
    out_file_is_select = True,
)
`
This commit is contained in:
geonik-code 2024-03-07 12:14:28 +01:00 committed by GitHub
parent 7f3829dbf3
commit 9b3db9e7d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,19 @@ WriteSourceFileInfo = provider(
}, },
) )
def _prepare_out_file(out_file, check_that_out_file_exists):
out_file_label = utils.to_label(out_file)
if utils.is_external_label(out_file_label):
msg = "out file {} must be in the user workspace".format(out_file_label)
fail(msg)
if check_that_out_file_exists and out_file_label.package != native.package_name():
msg = "out file {} (in package '{}') must be a source file within the target's package: '{}'; set check_that_out_file_exists to False to work-around this requirement".format(out_file_label, out_file_label.package, native.package_name())
fail(msg)
return str(out_file)
def write_source_file( def write_source_file(
name, name,
in_file = None, in_file = None,
@ -21,6 +34,7 @@ def write_source_file(
suggested_update_target = None, suggested_update_target = None,
diff_test = True, diff_test = True,
check_that_out_file_exists = True, check_that_out_file_exists = True,
out_file_is_select = False,
**kwargs): **kwargs):
"""Write a file or directory to the source tree. """Write a file or directory to the source tree.
@ -67,20 +81,17 @@ def write_source_file(
fail("out_file must be specified if in_file is set") fail("out_file must be specified if in_file is set")
if out_file: if out_file:
out_file = utils.to_label(out_file) if out_file_is_select:
for key, value in out_file.items():
if utils.is_external_label(out_file): out_file[key] = _prepare_out_file(value, check_that_out_file_exists)
msg = "out file {} must be in the user workspace".format(out_file) out_file = select(out_file)
fail(msg) else:
out_file = _prepare_out_file(out_file, check_that_out_file_exists)
if check_that_out_file_exists and out_file.package != native.package_name():
msg = "out file {} (in package '{}') must be a source file within the target's package: '{}'; set check_that_out_file_exists to False to work-around this requirement".format(out_file, out_file.package, native.package_name())
fail(msg)
_write_source_file( _write_source_file(
name = name, name = name,
in_file = in_file, in_file = in_file,
out_file = str(out_file) if out_file else None, out_file = out_file if out_file else None,
executable = executable, executable = executable,
additional_update_targets = additional_update_targets, additional_update_targets = additional_update_targets,
**kwargs **kwargs
@ -89,7 +100,7 @@ def write_source_file(
if not in_file or not out_file or not diff_test: if not in_file or not out_file or not diff_test:
return None return None
out_file_missing = check_that_out_file_exists and _is_file_missing(out_file) out_file_missing = check_that_out_file_exists and not out_file_is_select and _is_file_missing(out_file)
test_target_name = "%s_test" % name test_target_name = "%s_test" % name
if out_file_missing: if out_file_missing: