xref: /aosp_15_r20/external/pigweed/pw_build/copy_and_patch.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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
15import("python_action.gni")
16import("target_types.gni")
17
18# Applies a patch to a copy of file.  The source file will not be patched in place,
19# but instead copied before patching.
20# The output of this target will be the patched file.
21#
22# Args:
23#   source: (required) The source file to be patched.
24#   out: (required) The output file containing the patched contents. This value
25#                   can not be the same as the source value.
26#   patch_file: (required) The patch file.
27#   root: (optional) The root directory for applying the path.
28
29template("pw_copy_and_patch_file") {
30  assert(defined(invoker.source), "'source' must be provided")
31  assert(defined(invoker.out), "'out' must be provided")
32  assert(defined(invoker.patch_file), "'patch_file' must be provided")
33
34  _src_file = rebase_path(invoker.source, root_build_dir)
35  _out_file = "${target_gen_dir}/${invoker.out}"
36  _out_relative = rebase_path(_out_file, root_build_dir)
37  _patch_file = rebase_path(invoker.patch_file, root_build_dir)
38
39  _args = [
40    "--patch-file=${_patch_file}",
41    "--src=${_src_file}",
42    "--dst=${_out_relative}",
43  ]
44
45  if (defined(invoker.root)) {
46    _args += [
47      "--root",
48      rebase_path(invoker.root, root_build_dir),
49    ]
50  }
51
52  pw_python_action(target_name) {
53    module = "pw_build.copy_and_patch"
54    inputs = [
55      invoker.patch_file,
56      invoker.source,
57    ]
58    args = _args
59    outputs = [ _out_file ]
60    python_deps = [ "$dir_pw_build/py" ]
61  }
62}
63