1 # Copyright 2020 The Pigweed Authors
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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
15 import("//build_overrides/pigweed.gni")
17 import("$dir_pw_build/python_action.gni")
19 # Takes a set of input sources and zips them up to a .zip output.
21 # Users can either pass in specific input files or entire directories.
22 # This target type also supports renaming files as well as specifing
23 # desired zip destination directories for each input source.
26 # deps: Dependencies for this target.
28 # inputs: List of input files following the custom input formatting
29 # convention. See below for syntax.
31 # dirs: List of directories to be completely zipped up following the same
32 # input formatting convention. See below for syntax.
34 # output: Filename of artifact .zip file produced by script's execution.
36 # Each input follows the following convention:
37 # /source_path > /zip_destination/
39 # All directories are expected to be end with a '/'. Inputs must always specify
40 # both a source and a destination. Destinations are expected to have a leading
41 # '/' which stands for the root of the archive.
44 # Let's say we have the following structure for a //source/ directory:
55 # And we create the following build target:
57 # import("$dir_pw_build/zip.gni")
59 # pw_zip("target_name") {
61 # "//source/file1.txt > /", # Copied to the zip root dir.
62 # "//source/file2.txt > /renamed.txt", # File renamed.
63 # "//source/file3.txt > /bar/", # File moved to the /bar/ dir.
67 # "//source/some_dir/ > /bar/some_dir/", # Whole /some_dir/ contents
68 # # copied as /bar/some_dir/.
71 # # Note on output: if the specific output directory isn't defined
72 # # (such as output = "zoo.zip") then the .zip will output to the
73 # # same directory as the BUILD.gn file that called the target.
74 # output = "//$target_out_dir/foo.zip", # Where the foo.zip will end up
77 # This will result in a .zip file called foo.zip stored in //$target_out_dir
78 # with the following structure:
85 # │ └── some_other_dir/
92 pw_python_action(target_name) {
93 forward_variables_from(invoker, [ "deps" ])
94 script = "$dir_pw_build/py/pw_build/zip.py"
96 args = [ "--out_filename" ]
97 args += [ rebase_path(invoker.output) ]
100 args += [ "--input_list" ]
101 if (defined(invoker.inputs)) {
102 foreach(input, invoker.inputs) {
103 # Adding spaces around our delimiter is great for readability,
104 # but not great for the string split: remove the spacing.
105 input = string_replace(input, " $_delimiter", _delimiter)
106 input = string_replace(input, "$_delimiter ", _delimiter)
109 input_list = string_split(input, _delimiter)
110 input_list[0] = rebase_path(input_list[0])
111 inputs += [ input_list[0] ]
113 # Pass rebased and delimited path to script.
114 args += [ string_join(_delimiter, input_list) ]
118 if (defined(invoker.dirs)) {
119 foreach(dir, invoker.dirs) {
120 # Adding spaces around our delimiter is great for readability,
121 # but not great for the string split: remove the spacing.
122 dir = string_replace(dir, " $_delimiter", _delimiter)
123 dir = string_replace(dir, "$_delimiter ", _delimiter)
125 args += [ rebase_path(dir) ]
129 outputs = [ invoker.output ]