1*61c4878aSAndroid Build Coastguard Worker# Copyright 2020 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard Workerimport("//build_overrides/pigweed.gni") 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/python_action.gni") 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker# Takes a set of input sources and zips them up to a .zip output. 20*61c4878aSAndroid Build Coastguard Worker# 21*61c4878aSAndroid Build Coastguard Worker# Users can either pass in specific input files or entire directories. 22*61c4878aSAndroid Build Coastguard Worker# This target type also supports renaming files as well as specifing 23*61c4878aSAndroid Build Coastguard Worker# desired zip destination directories for each input source. 24*61c4878aSAndroid Build Coastguard Worker# 25*61c4878aSAndroid Build Coastguard Worker# Args: 26*61c4878aSAndroid Build Coastguard Worker# deps: Dependencies for this target. 27*61c4878aSAndroid Build Coastguard Worker# 28*61c4878aSAndroid Build Coastguard Worker# inputs: List of input files following the custom input formatting 29*61c4878aSAndroid Build Coastguard Worker# convention. See below for syntax. 30*61c4878aSAndroid Build Coastguard Worker# 31*61c4878aSAndroid Build Coastguard Worker# dirs: List of directories to be completely zipped up following the same 32*61c4878aSAndroid Build Coastguard Worker# input formatting convention. See below for syntax. 33*61c4878aSAndroid Build Coastguard Worker# 34*61c4878aSAndroid Build Coastguard Worker# output: Filename of artifact .zip file produced by script's execution. 35*61c4878aSAndroid Build Coastguard Worker# 36*61c4878aSAndroid Build Coastguard Worker# Each input follows the following convention: 37*61c4878aSAndroid Build Coastguard Worker# /source_path > /zip_destination/ 38*61c4878aSAndroid Build Coastguard Worker# 39*61c4878aSAndroid Build Coastguard Worker# All directories are expected to be end with a '/'. Inputs must always specify 40*61c4878aSAndroid Build Coastguard Worker# both a source and a destination. Destinations are expected to have a leading 41*61c4878aSAndroid Build Coastguard Worker# '/' which stands for the root of the archive. 42*61c4878aSAndroid Build Coastguard Worker# 43*61c4878aSAndroid Build Coastguard Worker# Example: 44*61c4878aSAndroid Build Coastguard Worker# Let's say we have the following structure for a //source/ directory: 45*61c4878aSAndroid Build Coastguard Worker# 46*61c4878aSAndroid Build Coastguard Worker# source/ 47*61c4878aSAndroid Build Coastguard Worker# ├── file1.txt 48*61c4878aSAndroid Build Coastguard Worker# ├── file2.txt 49*61c4878aSAndroid Build Coastguard Worker# ├── file3.txt 50*61c4878aSAndroid Build Coastguard Worker# └── some_dir/ 51*61c4878aSAndroid Build Coastguard Worker# ├── file4.txt 52*61c4878aSAndroid Build Coastguard Worker# └── some_other_dir/ 53*61c4878aSAndroid Build Coastguard Worker# └── file5.txt 54*61c4878aSAndroid Build Coastguard Worker# 55*61c4878aSAndroid Build Coastguard Worker# And we create the following build target: 56*61c4878aSAndroid Build Coastguard Worker# 57*61c4878aSAndroid Build Coastguard Worker# import("$dir_pw_build/zip.gni") 58*61c4878aSAndroid Build Coastguard Worker# 59*61c4878aSAndroid Build Coastguard Worker# pw_zip("target_name") { 60*61c4878aSAndroid Build Coastguard Worker# inputs = [ 61*61c4878aSAndroid Build Coastguard Worker# "//source/file1.txt > /", # Copied to the zip root dir. 62*61c4878aSAndroid Build Coastguard Worker# "//source/file2.txt > /renamed.txt", # File renamed. 63*61c4878aSAndroid Build Coastguard Worker# "//source/file3.txt > /bar/", # File moved to the /bar/ dir. 64*61c4878aSAndroid Build Coastguard Worker# ] 65*61c4878aSAndroid Build Coastguard Worker# 66*61c4878aSAndroid Build Coastguard Worker# dirs = [ 67*61c4878aSAndroid Build Coastguard Worker# "//source/some_dir/ > /bar/some_dir/", # Whole /some_dir/ contents 68*61c4878aSAndroid Build Coastguard Worker# # copied as /bar/some_dir/. 69*61c4878aSAndroid Build Coastguard Worker# ] 70*61c4878aSAndroid Build Coastguard Worker# 71*61c4878aSAndroid Build Coastguard Worker# # Note on output: if the specific output directory isn't defined 72*61c4878aSAndroid Build Coastguard Worker# # (such as output = "zoo.zip") then the .zip will output to the 73*61c4878aSAndroid Build Coastguard Worker# # same directory as the BUILD.gn file that called the target. 74*61c4878aSAndroid Build Coastguard Worker# output = "//$target_out_dir/foo.zip", # Where the foo.zip will end up 75*61c4878aSAndroid Build Coastguard Worker# } 76*61c4878aSAndroid Build Coastguard Worker# 77*61c4878aSAndroid Build Coastguard Worker# This will result in a .zip file called foo.zip stored in //$target_out_dir 78*61c4878aSAndroid Build Coastguard Worker# with the following structure: 79*61c4878aSAndroid Build Coastguard Worker# 80*61c4878aSAndroid Build Coastguard Worker# foo.zip 81*61c4878aSAndroid Build Coastguard Worker# ├── bar/ 82*61c4878aSAndroid Build Coastguard Worker# │ ├── file3.txt 83*61c4878aSAndroid Build Coastguard Worker# │ └── some_dir/ 84*61c4878aSAndroid Build Coastguard Worker# │ ├── file4.txt 85*61c4878aSAndroid Build Coastguard Worker# │ └── some_other_dir/ 86*61c4878aSAndroid Build Coastguard Worker# │ └── file5.txt 87*61c4878aSAndroid Build Coastguard Worker# ├── file1.txt 88*61c4878aSAndroid Build Coastguard Worker# └── renamed.txt 89*61c4878aSAndroid Build Coastguard Worker# 90*61c4878aSAndroid Build Coastguard Workertemplate("pw_zip") { 91*61c4878aSAndroid Build Coastguard Worker _delimiter = ">" 92*61c4878aSAndroid Build Coastguard Worker pw_python_action(target_name) { 93*61c4878aSAndroid Build Coastguard Worker forward_variables_from(invoker, 94*61c4878aSAndroid Build Coastguard Worker [ 95*61c4878aSAndroid Build Coastguard Worker "deps", 96*61c4878aSAndroid Build Coastguard Worker "public_deps", 97*61c4878aSAndroid Build Coastguard Worker ]) 98*61c4878aSAndroid Build Coastguard Worker script = "$dir_pw_build/py/pw_build/zip.py" 99*61c4878aSAndroid Build Coastguard Worker 100*61c4878aSAndroid Build Coastguard Worker args = [ "--out_filename" ] 101*61c4878aSAndroid Build Coastguard Worker args += [ rebase_path(invoker.output, root_build_dir) ] 102*61c4878aSAndroid Build Coastguard Worker 103*61c4878aSAndroid Build Coastguard Worker inputs = [] 104*61c4878aSAndroid Build Coastguard Worker args += [ "--input_list" ] 105*61c4878aSAndroid Build Coastguard Worker if (defined(invoker.inputs)) { 106*61c4878aSAndroid Build Coastguard Worker foreach(input, invoker.inputs) { 107*61c4878aSAndroid Build Coastguard Worker # Adding spaces around our delimiter is great for readability, 108*61c4878aSAndroid Build Coastguard Worker # but not great for the string split: remove the spacing. 109*61c4878aSAndroid Build Coastguard Worker input = string_replace(input, " $_delimiter", _delimiter) 110*61c4878aSAndroid Build Coastguard Worker input = string_replace(input, "$_delimiter ", _delimiter) 111*61c4878aSAndroid Build Coastguard Worker 112*61c4878aSAndroid Build Coastguard Worker input_list = [] 113*61c4878aSAndroid Build Coastguard Worker input_list = string_split(input, _delimiter) 114*61c4878aSAndroid Build Coastguard Worker inputs += [ input_list[0] ] 115*61c4878aSAndroid Build Coastguard Worker input_list[0] = rebase_path(input_list[0], root_build_dir) 116*61c4878aSAndroid Build Coastguard Worker 117*61c4878aSAndroid Build Coastguard Worker # Pass rebased and delimited path to script. 118*61c4878aSAndroid Build Coastguard Worker args += [ string_join(_delimiter, input_list) ] 119*61c4878aSAndroid Build Coastguard Worker } 120*61c4878aSAndroid Build Coastguard Worker } 121*61c4878aSAndroid Build Coastguard Worker 122*61c4878aSAndroid Build Coastguard Worker if (defined(invoker.dirs)) { 123*61c4878aSAndroid Build Coastguard Worker foreach(dir, invoker.dirs) { 124*61c4878aSAndroid Build Coastguard Worker # Adding spaces around our delimiter is great for readability, 125*61c4878aSAndroid Build Coastguard Worker # but not great for the string split: remove the spacing. 126*61c4878aSAndroid Build Coastguard Worker dir = string_replace(dir, " $_delimiter", _delimiter) 127*61c4878aSAndroid Build Coastguard Worker dir = string_replace(dir, "$_delimiter ", _delimiter) 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard Worker args += [ rebase_path(dir, root_build_dir) ] 130*61c4878aSAndroid Build Coastguard Worker } 131*61c4878aSAndroid Build Coastguard Worker } 132*61c4878aSAndroid Build Coastguard Worker 133*61c4878aSAndroid Build Coastguard Worker outputs = [ invoker.output ] 134*61c4878aSAndroid Build Coastguard Worker } 135*61c4878aSAndroid Build Coastguard Worker} 136