blob: dc27b66e56c0d114d29f6d55269732ea30289176 [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
60 if (defined(invoker.path_data_keys)) {
61 assert(_deps != [],
62 "'path_data_keys' requires at least one dependency in 'deps'")
63
64 generated_file("$target_name._path_list") {
65 data_keys = invoker.path_data_keys
66 rebase = root_build_dir
67 deps = _deps
68 outputs = [ "$target_gen_dir/$target_name.txt" ]
Wyatt Heplere59706b2021-03-02 09:00:25 -080069 }
70
Wyatt Hepler2375d902021-03-09 19:51:48 -080071 _deps += [ ":$target_name._path_list" ]
72 _args += [ "--path-file" ] +
73 rebase_path(get_target_outputs(":$target_name._path_list"))
74 }
Wyatt Heplere59706b2021-03-02 09:00:25 -080075
Wyatt Hepler2375d902021-03-09 19:51:48 -080076 pw_python_action(target_name) {
77 script = "$dir_pw_build/py/pw_build/mirror_tree.py"
78 args = _args
79
80 outputs = []
81
82 if (defined(invoker.sources)) {
83 args += rebase_path(invoker.sources)
84
85 foreach(path, rebase_path(invoker.sources, _root)) {
86 outputs += [ "${invoker.directory}/$path" ]
87 }
88 }
89
90 # If path_data_keys is used, the outputs may be unknown.
91 if (outputs == []) {
92 stamp = true
93 }
94
95 deps = _deps
96
97 _ignore_args = [
98 "script",
99 "args",
100 "outputs",
101 "deps",
102 "directory",
103 ]
104 forward_variables_from(invoker, "*", _ignore_args)
Wyatt Heplere59706b2021-03-02 09:00:25 -0800105 }
106}