From 9b3db9e7d620780562e6113618ea14aaf94945ce Mon Sep 17 00:00:00 2001 From: geonik-code <132887728+geonik-code@users.noreply.github.com> Date: Thu, 7 Mar 2024 12:14:28 +0100 Subject: [PATCH] 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, ) ` --- lib/private/write_source_file.bzl | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index 8b72681..4c23387 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -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( name, in_file = None, @@ -21,6 +34,7 @@ def write_source_file( suggested_update_target = None, diff_test = True, check_that_out_file_exists = True, + out_file_is_select = False, **kwargs): """Write a file or directory to the source tree. @@ -65,22 +79,19 @@ def write_source_file( if in_file: if not out_file: fail("out_file must be specified if in_file is set") - - if out_file: - out_file = utils.to_label(out_file) - - if utils.is_external_label(out_file): - msg = "out file {} must be in the user workspace".format(out_file) - fail(msg) - - 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) + + if out_file: + if out_file_is_select: + for key, value in out_file.items(): + out_file[key] = _prepare_out_file(value, check_that_out_file_exists) + out_file = select(out_file) + else: + out_file = _prepare_out_file(out_file, check_that_out_file_exists) _write_source_file( name = name, 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, additional_update_targets = additional_update_targets, **kwargs @@ -89,7 +100,7 @@ def write_source_file( if not in_file or not out_file or not diff_test: 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 if out_file_missing: