xref: /aosp_15_r20/external/pigweed/pw_build/copy_and_patch.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Workerimport("python_action.gni")
16*61c4878aSAndroid Build Coastguard Workerimport("target_types.gni")
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker# Applies a patch to a copy of file.  The source file will not be patched in place,
19*61c4878aSAndroid Build Coastguard Worker# but instead copied before patching.
20*61c4878aSAndroid Build Coastguard Worker# The output of this target will be the patched file.
21*61c4878aSAndroid Build Coastguard Worker#
22*61c4878aSAndroid Build Coastguard Worker# Args:
23*61c4878aSAndroid Build Coastguard Worker#   source: (required) The source file to be patched.
24*61c4878aSAndroid Build Coastguard Worker#   out: (required) The output file containing the patched contents. This value
25*61c4878aSAndroid Build Coastguard Worker#                   can not be the same as the source value.
26*61c4878aSAndroid Build Coastguard Worker#   patch_file: (required) The patch file.
27*61c4878aSAndroid Build Coastguard Worker#   root: (optional) The root directory for applying the path.
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Workertemplate("pw_copy_and_patch_file") {
30*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.source), "'source' must be provided")
31*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.out), "'out' must be provided")
32*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.patch_file), "'patch_file' must be provided")
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker  _src_file = rebase_path(invoker.source, root_build_dir)
35*61c4878aSAndroid Build Coastguard Worker  _out_file = "${target_gen_dir}/${invoker.out}"
36*61c4878aSAndroid Build Coastguard Worker  _out_relative = rebase_path(_out_file, root_build_dir)
37*61c4878aSAndroid Build Coastguard Worker  _patch_file = rebase_path(invoker.patch_file, root_build_dir)
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker  _args = [
40*61c4878aSAndroid Build Coastguard Worker    "--patch-file=${_patch_file}",
41*61c4878aSAndroid Build Coastguard Worker    "--src=${_src_file}",
42*61c4878aSAndroid Build Coastguard Worker    "--dst=${_out_relative}",
43*61c4878aSAndroid Build Coastguard Worker  ]
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.root)) {
46*61c4878aSAndroid Build Coastguard Worker    _args += [
47*61c4878aSAndroid Build Coastguard Worker      "--root",
48*61c4878aSAndroid Build Coastguard Worker      rebase_path(invoker.root, root_build_dir),
49*61c4878aSAndroid Build Coastguard Worker    ]
50*61c4878aSAndroid Build Coastguard Worker  }
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker  pw_python_action(target_name) {
53*61c4878aSAndroid Build Coastguard Worker    module = "pw_build.copy_and_patch"
54*61c4878aSAndroid Build Coastguard Worker    inputs = [
55*61c4878aSAndroid Build Coastguard Worker      invoker.patch_file,
56*61c4878aSAndroid Build Coastguard Worker      invoker.source,
57*61c4878aSAndroid Build Coastguard Worker    ]
58*61c4878aSAndroid Build Coastguard Worker    args = _args
59*61c4878aSAndroid Build Coastguard Worker    outputs = [ _out_file ]
60*61c4878aSAndroid Build Coastguard Worker    python_deps = [ "$dir_pw_build/py" ]
61*61c4878aSAndroid Build Coastguard Worker  }
62*61c4878aSAndroid Build Coastguard Worker}
63