blob: 5e78de8230dbd072fe91f2f05e214a93ec9eeb02 [file] [log] [blame]
Wyatt Heplere59706b2021-03-02 09:00:25 -08001# Copyright 2021 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("//build_overrides/pigweed.gni")
16
Wyatt Hepler2375d902021-03-09 19:51:48 -080017import("$dir_pw_build/python_action.gni")
18
Wyatt Heplere59706b2021-03-02 09:00:25 -080019# Mirrors a directory structure to the output directory.
20#
21# This is similar to a GN copy target, with some differences:
22#
23# - The outputs list is generated by the template based on the source_root and
24# directory arguments, rather than using source expansion.
25# - The source_root argument can be used to trim prefixes from source files.
26# - pw_mirror_tree uses hard links instead of copies for efficiency.
27#
28# Args:
29#
30# directory: Output directory for the files.
31# sources: List of files to mirror to the output directory.
32# source_root: Root path for sources; defaults to ".".
Wyatt Hepler2375d902021-03-09 19:51:48 -080033# path_data_keys: GN metadata data_keys from which to extract file or
34# directory paths to add to the list of sources.
Wyatt Heplere59706b2021-03-02 09:00:25 -080035#
36template("pw_mirror_tree") {
Wyatt Hepler2375d902021-03-09 19:51:48 -080037 assert(defined(invoker.sources) || defined(invoker.path_data_keys),
38 "At least one of 'sources' or 'path_data_keys' must be provided")
Wyatt Heplere59706b2021-03-02 09:00:25 -080039 assert(defined(invoker.directory) && invoker.directory != "",
40 "The output path must be specified as 'directory'")
41
42 if (defined(invoker.source_root)) {
43 _root = invoker.source_root
44 } else {
45 _root = "."
46 }
47
Wyatt Hepler2375d902021-03-09 19:51:48 -080048 _args = [
49 "--source-root",
50 rebase_path(_root),
51 "--directory",
52 rebase_path(invoker.directory),
53 ]
Wyatt Heplere59706b2021-03-02 09:00:25 -080054
Wyatt Hepler2375d902021-03-09 19:51:48 -080055 _deps = []
56 if (defined(invoker.deps)) {
57 _deps += invoker.deps
58 }
59
Wyatt Hepler446f24c2021-03-24 09:29:35 -070060 _public_deps = []
61 if (defined(invoker.public_deps)) {
62 _public_deps += invoker.public_deps
63 }
Wyatt Hepler2375d902021-03-09 19:51:48 -080064
Wyatt Hepler446f24c2021-03-24 09:29:35 -070065 if (defined(invoker.path_data_keys)) {
Wyatt Hepler2375d902021-03-09 19:51:48 -080066 generated_file("$target_name._path_list") {
67 data_keys = invoker.path_data_keys
68 rebase = root_build_dir
Wyatt Hepler2375d902021-03-09 19:51:48 -080069 outputs = [ "$target_gen_dir/$target_name.txt" ]
Wyatt Hepler446f24c2021-03-24 09:29:35 -070070 deps = _deps + _public_deps
71
72 assert(deps != [],
73 "'path_data_keys' requires at least one dependency in 'deps'")
Wyatt Heplere59706b2021-03-02 09:00:25 -080074 }
75
Wyatt Hepler2375d902021-03-09 19:51:48 -080076 _deps += [ ":$target_name._path_list" ]
77 _args += [ "--path-file" ] +
78 rebase_path(get_target_outputs(":$target_name._path_list"))
79 }
Wyatt Heplere59706b2021-03-02 09:00:25 -080080
Wyatt Hepler2375d902021-03-09 19:51:48 -080081 pw_python_action(target_name) {
82 script = "$dir_pw_build/py/pw_build/mirror_tree.py"
83 args = _args
84
85 outputs = []
86
87 if (defined(invoker.sources)) {
88 args += rebase_path(invoker.sources)
89
90 foreach(path, rebase_path(invoker.sources, _root)) {
91 outputs += [ "${invoker.directory}/$path" ]
92 }
93 }
94
95 # If path_data_keys is used, the outputs may be unknown.
96 if (outputs == []) {
97 stamp = true
98 }
99
100 deps = _deps
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700101 public_deps = _public_deps
Wyatt Hepler2375d902021-03-09 19:51:48 -0800102
103 _ignore_args = [
104 "script",
105 "args",
106 "outputs",
Wyatt Hepler2375d902021-03-09 19:51:48 -0800107 "directory",
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700108 "deps",
109 "public_deps",
Wyatt Hepler2375d902021-03-09 19:51:48 -0800110 ]
111 forward_variables_from(invoker, "*", _ignore_args)
Wyatt Heplere59706b2021-03-02 09:00:25 -0800112 }
113}