1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Implementation for pw_copy_and_patch_file""" 15 16load("@bazel_skylib//rules:run_binary.bzl", "run_binary") 17 18def pw_copy_and_patch_file(*, name, src, out, patch_file, **kwargs): 19 """Applies a patch to a copy of the file. 20 21 The source file will not be patched in place, but instead copied before 22 patching. The output of this target will be the patched file. 23 24 Args: 25 name: The name of the target. 26 src: The source file to be patched. 27 out: The output file containing the patched contents. This value 28 can not be the same as the src value. 29 patch_file: The patch file. 30 **kwargs: Passed to run_binary. 31 """ 32 _args = [ 33 "--patch-file", 34 "$(execpath " + patch_file + ")", 35 "--src", 36 "$(execpath " + src + ")", 37 "--dst", 38 "$(execpath " + out + ")", 39 ] 40 41 # If src is an external repo, set the root 42 # of the external repo path. 43 if Label(src).workspace_root: 44 root = Label(src).workspace_root 45 _args.append("--root") 46 _args.append(root) 47 48 run_binary( 49 name = name, 50 srcs = [src, patch_file], 51 outs = [out], 52 args = _args, 53 tool = str(Label("//pw_build/py:copy_and_patch")), 54 **kwargs 55 ) 56