blob: 6114bf4fc1c339186850bb869a77058fcf486392 [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
17# Mirrors a directory structure to the output directory.
18#
19# This is similar to a GN copy target, with some differences:
20#
21# - The outputs list is generated by the template based on the source_root and
22# directory arguments, rather than using source expansion.
23# - The source_root argument can be used to trim prefixes from source files.
24# - pw_mirror_tree uses hard links instead of copies for efficiency.
25#
26# Args:
27#
28# directory: Output directory for the files.
29# sources: List of files to mirror to the output directory.
30# source_root: Root path for sources; defaults to ".".
31#
32template("pw_mirror_tree") {
RJ Ascani13845142021-11-15 10:06:06 -080033 assert(defined(invoker.sources), "'sources' must be provided")
Wyatt Heplere59706b2021-03-02 09:00:25 -080034 assert(defined(invoker.directory) && invoker.directory != "",
35 "The output path must be specified as 'directory'")
36
37 if (defined(invoker.source_root)) {
38 _root = invoker.source_root
39 } else {
40 _root = "."
41 }
42
Wyatt Hepler2375d902021-03-09 19:51:48 -080043 _deps = []
44 if (defined(invoker.deps)) {
45 _deps += invoker.deps
46 }
47
Wyatt Hepler446f24c2021-03-24 09:29:35 -070048 _public_deps = []
49 if (defined(invoker.public_deps)) {
50 _public_deps += invoker.public_deps
51 }
Wyatt Hepler2375d902021-03-09 19:51:48 -080052
RJ Ascani13845142021-11-15 10:06:06 -080053 _copy_deps = []
Wyatt Hepler446f24c2021-03-24 09:29:35 -070054
RJ Ascani13845142021-11-15 10:06:06 -080055 foreach(source, invoker.sources) {
56 _stripped_source = rebase_path(source, _root)
57 _subtarget_name =
58 string_replace("${target_name}_${_stripped_source}", "/", ".")
59
60 copy(_subtarget_name) {
61 sources = [ source ]
62 outputs = [ "${invoker.directory}/${_stripped_source}" ]
63 deps = _deps
64 public_deps = _public_deps
Wyatt Heplere59706b2021-03-02 09:00:25 -080065 }
RJ Ascani13845142021-11-15 10:06:06 -080066 _copy_deps += [ ":${_subtarget_name}" ]
Wyatt Hepler2375d902021-03-09 19:51:48 -080067 }
Wyatt Heplere59706b2021-03-02 09:00:25 -080068
RJ Ascani13845142021-11-15 10:06:06 -080069 group(target_name) {
70 public_deps = _copy_deps
Wyatt Heplere59706b2021-03-02 09:00:25 -080071 }
72}