Wyatt Hepler | e59706b | 2021-03-02 09:00:25 -0800 | [diff] [blame] | 1 | # 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 | |
| 15 | import("//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 | # |
| 32 | template("pw_mirror_tree") { |
RJ Ascani | 1384514 | 2021-11-15 10:06:06 -0800 | [diff] [blame] | 33 | assert(defined(invoker.sources), "'sources' must be provided") |
Wyatt Hepler | e59706b | 2021-03-02 09:00:25 -0800 | [diff] [blame] | 34 | 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 Hepler | 2375d90 | 2021-03-09 19:51:48 -0800 | [diff] [blame] | 43 | _deps = [] |
| 44 | if (defined(invoker.deps)) { |
| 45 | _deps += invoker.deps |
| 46 | } |
| 47 | |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 48 | _public_deps = [] |
| 49 | if (defined(invoker.public_deps)) { |
| 50 | _public_deps += invoker.public_deps |
| 51 | } |
Wyatt Hepler | 2375d90 | 2021-03-09 19:51:48 -0800 | [diff] [blame] | 52 | |
RJ Ascani | 1384514 | 2021-11-15 10:06:06 -0800 | [diff] [blame] | 53 | _copy_deps = [] |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 54 | |
RJ Ascani | 1384514 | 2021-11-15 10:06:06 -0800 | [diff] [blame] | 55 | 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 Hepler | e59706b | 2021-03-02 09:00:25 -0800 | [diff] [blame] | 65 | } |
RJ Ascani | 1384514 | 2021-11-15 10:06:06 -0800 | [diff] [blame] | 66 | _copy_deps += [ ":${_subtarget_name}" ] |
Wyatt Hepler | 2375d90 | 2021-03-09 19:51:48 -0800 | [diff] [blame] | 67 | } |
Wyatt Hepler | e59706b | 2021-03-02 09:00:25 -0800 | [diff] [blame] | 68 | |
RJ Ascani | 1384514 | 2021-11-15 10:06:06 -0800 | [diff] [blame] | 69 | group(target_name) { |
| 70 | public_deps = _copy_deps |
Wyatt Hepler | e59706b | 2021-03-02 09:00:25 -0800 | [diff] [blame] | 71 | } |
| 72 | } |