blob: 6091738924cfafe4386e7e1c8f7bf93c2bfbdeed [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",
Michael Spangc8b93902021-05-30 15:53:56 -040050 rebase_path(_root, root_build_dir),
Wyatt Hepler2375d902021-03-09 19:51:48 -080051 "--directory",
Michael Spangc8b93902021-05-30 15:53:56 -040052 rebase_path(invoker.directory, root_build_dir),
Wyatt Hepler2375d902021-03-09 19:51:48 -080053 ]
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" ] +
Michael Spangc8b93902021-05-30 15:53:56 -040078 rebase_path(get_target_outputs(":$target_name._path_list"),
79 root_build_dir)
Wyatt Hepler2375d902021-03-09 19:51:48 -080080 }
Wyatt Heplere59706b2021-03-02 09:00:25 -080081
Wyatt Hepler2375d902021-03-09 19:51:48 -080082 pw_python_action(target_name) {
83 script = "$dir_pw_build/py/pw_build/mirror_tree.py"
84 args = _args
85
86 outputs = []
87
88 if (defined(invoker.sources)) {
Michael Spangc8b93902021-05-30 15:53:56 -040089 args += rebase_path(invoker.sources, root_build_dir)
Wyatt Hepler2375d902021-03-09 19:51:48 -080090
91 foreach(path, rebase_path(invoker.sources, _root)) {
92 outputs += [ "${invoker.directory}/$path" ]
93 }
94 }
95
96 # If path_data_keys is used, the outputs may be unknown.
97 if (outputs == []) {
98 stamp = true
99 }
100
101 deps = _deps
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700102 public_deps = _public_deps
Wyatt Hepler2375d902021-03-09 19:51:48 -0800103
104 _ignore_args = [
105 "script",
106 "args",
107 "outputs",
Wyatt Hepler2375d902021-03-09 19:51:48 -0800108 "directory",
Wyatt Hepler446f24c2021-03-24 09:29:35 -0700109 "deps",
110 "public_deps",
Wyatt Hepler2375d902021-03-09 19:51:48 -0800111 ]
112 forward_variables_from(invoker, "*", _ignore_args)
Wyatt Heplere59706b2021-03-02 09:00:25 -0800113 }
114}