c7e8cc301bbe2636f43188a8537408522e787a5d
[AGL/meta-agl.git] / scripts / distro-manifest-generator.sh
1 #!/usr/bin/env bash
2
3 ################################################################################
4 #
5 # The MIT License (MIT)
6 #
7 # Copyright (c) 2018 Stéphane Desneux <sdx@iot.bzh>
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # SOFTWARE.
26 #
27 ################################################################################
28
29 mode=deploy
30 manifest=
31 verbose=0
32 format=bash
33 sourcefile=
34 timestamp="$(date -u +%Y%m%d_%H%M%S_%Z)"
35
36 function info() { echo "$@" >&2; }
37 function error() { echo "$BASH_SOURCE: $@" >&2; }
38 function out() { echo -n "$@"; }
39 function out_object() {
40         # expected stdin stream is:
41         # --------------
42         # key
43         # value
44         # key
45         # value
46         # ...
47         # --------------
48         local sep=""
49         local k
50         case $format in
51                 bash)
52                         while read x; do
53                                 [[ -z "$k" ]] && { k="$x"; continue; }
54                                 out "$sep${k}="
55                                 out_value "$x"
56                                 sep=$'\n'
57                                 k=
58                         done
59                         out "$sep"
60                         ;;
61                 json)
62                         out "{"
63                         while read x; do
64                                 [[ -z "$k" ]] && { k="$x"; continue; }
65                                 out "$sep\"${k}\":"
66                                 out_value "$x"
67                                 sep=","
68                                 k=
69                         done
70                         out "}"
71                         ;;
72         esac
73 }
74
75 function out_array() {
76         # expected stdin stream is:
77         # --------------
78         # value
79         # value
80         # ...
81         # --------------
82         local sep=""
83         case $format in
84                 bash)
85                         while read x; do
86                                 out "$sep"
87                                 out_value "$x"
88                                 sep=" "
89                         done
90                         ;;
91                 json)
92                         out "["
93                         while read x; do
94                                 out $sep
95                                 out_value "$x"
96                                 sep=","
97                         done
98                         out "]"
99                         ;;
100         esac
101 }
102
103 function out_value() {
104         # string
105         # number
106         # object
107         # array
108         # 'true'
109         # 'false'
110         # 'null'
111
112         x=$1
113
114         # litterals
115         if [[ "$x" =~ ^(true|false|null)$ ]]; then
116                 out "$x"
117         # number
118         elif [[ "$x" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]]; then
119                 out "$x"
120         # object
121         elif [[ "$x" =~ ^\{.*\}$ ]]; then
122                 out "$x"
123         # array
124         elif [[ "$x" =~ ^\[.*\]$ ]]; then
125                 out "$x"
126         # string
127         else
128                 out "\"$(sed 's/\("\)/\\\1/g' <<<$x)\""
129         fi
130 }
131
132 function out_comment() {
133         case $format in
134                 bash)
135                         [[ "$verbose" == 1 ]] && echo "# $@" || true
136                         ;;
137                 json)
138                         ;;
139         esac
140 }
141
142 function _getgitmanifest() {
143         # this function takes the setup.manifest generated by setup script and uses DIST_METADIR
144         # to analyze git repos
145
146         local manifest=$1 mode=$2
147         [[ -f $manifest ]] && source $manifest || { error "Invalid setup manifest '$manifest'"; return 1; }
148         [[ ! -d "$DIST_METADIR" ]] && {
149                 error "Invalid meta directory. Check variable DIST_METADIR in manifest file '$manifest'."
150                 error "$BASH_SOURCE: Also, check directory '$DIST_METADIR'."
151                 return 2
152         }
153         local GIT=$(which git) REALPATH=$(which realpath)
154         [[ ! -x $GIT ]] && { error "$BASH_SOURCE: Unable to find git command in $PATH."; return 3; }
155         [[ ! -x $REALPATH ]] && { error "$BASH_SOURCE: Unable to find realpath command in $PATH."; return 4; }
156
157         local gitrepo gitrev metagitdir sep=""
158         DIST_LAYERS=""
159         for metagitdir in $(find $DIST_METADIR -maxdepth 2 -type d \( -not -path '*/.*' \) -exec test -d "{}/.git" \; -print -prune); do
160                 gitrepo=$($REALPATH -Ls $metagitdir --relative-to=$DIST_METADIR)
161                 pushd $DIST_METADIR/$gitrepo &>/dev/null && {
162                         gitrev=$( { $GIT describe --long --dirty --always 2>/dev/null || echo "unknown_revision"; } | tr ' \t' '__' )
163                         popd &>/dev/null
164                 } || {
165                         gitrev=unknown
166                 }
167                 DIST_LAYERS="${DIST_LAYERS}${sep}${gitrepo}:${gitrev}"
168                 sep=" "
169         done
170
171         # layers checksum
172         DIST_LAYERS_MD5=$(echo $DIST_LAYERS|md5sum -|awk '{print $1;}')
173
174         # in json, transform layers in an object, features in array
175         [[ "$format" == "json" ]] && {
176                 DIST_FEATURES=$(for x in $DIST_FEATURES; do
177                         echo $x
178                 done | out_array)
179                 DIST_LAYERS=$(for x in $DIST_LAYERS; do
180                         echo ${x%%:*}
181                         echo ${x#*:}
182                 done | out_object)
183         }
184
185
186         # compute build hash
187         DIST_BUILD_HASH="F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
188         DIST_BUILD_ID="${DIST_DISTRO_NAME}-${DIST_MACHINE}-F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
189
190
191         # compute setup manifest path and build TS
192         DIST_SETUP_MANIFEST="$($REALPATH $manifest)"
193
194         # Manifest build timestamp
195         DIST_BUILD_TS="$timestamp"
196
197         # what to retain from setup manifest?
198         # to generate the full list: cat setup.manifest  | grep = | cut -f1 -d"=" | awk '{printf("%s ",$1);}'
199         declare -A SETUP_VARS
200         SETUP_VARS[deploy]="DIST_MACHINE DIST_FEATURES DIST_FEATURES_MD5 DIST_BUILD_HOST DIST_BUILD_OS DIST_SETUP_TS"
201         SETUP_VARS[target]="DIST_MACHINE DIST_FEATURES"
202         SETUP_VARS[sdk]="DIST_MACHINE DIST_FEATURES"
203
204         # extra vars not coming from setup.manifest but generated here
205         declare -A EXTRA_VARS
206         EXTRA_VARS[deploy]="DIST_SETUP_MANIFEST DIST_BUILD_TS DIST_LAYERS DIST_LAYERS_MD5 DIST_BUILD_HASH DIST_BUILD_ID"
207         EXTRA_VARS[target]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID DIST_BUILD_TS"
208         EXTRA_VARS[sdk]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID DIST_BUILD_TS"
209
210         # BITBAKE_VARS may be defined from external file to source (--source arg)
211         # this is used to dump extra vars from inside bitbake recipe
212
213         { for x in ${SETUP_VARS[$mode]} ${EXTRA_VARS[$mode]} ${BITBAKE_VARS[$mode]}; do
214                 k=$x
215                 [[ "$format" == "json" ]] && {
216                         k=${k#DIST_} # remove prefix
217                         k=${k,,*} # to lower case
218                 }
219                 echo $k
220                 echo ${!x}
221         done } | out_object
222 }
223
224 function getmanifest() {
225         local rc=0
226         out_comment "DISTRO BUILD MANIFEST"
227         out_comment
228
229         # add layers manifest
230         out_comment "----- this fragment has been generated by $BASH_SOURCE"
231         _getgitmanifest $1 $2 || rc=$?
232         out_comment "------------ end of $BASH_SOURCE fragment --------"
233
234         return $rc
235 }
236
237 function __usage() {
238         cat <<EOF >&2
239 Usage: $BASH_SOURCE [-v|--verbose] [-f|--format <fmt>] [-t|--timestamp <value>] [-m|--mode <mode>] [-s|--source <file>] <setup_manifest_file>
240    Options:
241       -v|--verbose: generate comments in the output file
242       -s|--source: extra file to source (get extra variables generated from bitbake recipe)
243       -t|--timestamp: set build timestamp (default: current date - may not be the same ts as bitbake)
244       -f|--format: specify output format: 'bash' or 'json'
245       -m|--mode: specify the destination for the generated manifest
246          'deploy' : for the tmp/deploy/images/* directories
247          'target' : for the manifest to be installed inside a target image
248          'sdk'    : for the manifest to be installed inside the SDK
249
250    <setup_manifest_file> is the input manifest generated from setup script
251 EOF
252 }
253
254 set -e
255
256 tmp=$(getopt -o h,v,m:,f:,t:,s: --long help,verbose,mode:,format:,timestamp:,source: -n "$BASH_SOURCE" -- "$@") || {
257         error "Invalid arguments."
258         __usage
259         exit 1
260 }
261 eval set -- $tmp
262
263 while true; do
264         case "$1" in
265                 -h|--help) __usage; exit 0;;
266                 -v|--verbose) verbose=1; shift ;;
267                 -f|--format) format=$2; shift 2;;
268                 -m|--mode) mode=$2; shift 2;;
269                 -t|--timestamp) timestamp=$2; shift 2;;
270                 -s|--source) sourcefile=$2; shift 2;;
271                 --) shift; break;;
272                 *) fatal "Internal error";;
273         esac
274 done
275
276 manifest=$1
277 shift
278 [[ ! -f "$manifest" ]] && { __usage; exit 1; }
279
280 case $mode in
281         deploy|target|sdk) ;;
282         *) error "Invalid mode specified. Allowed modes are: 'deploy', 'target', 'sdk'"; __usage; exit 42;;
283 esac
284
285 case $format in
286         bash|json) ;;
287         *) error "Invalid format specified. Allowed formats are 'json' or 'bash'"; __usage; exit 43;;
288 esac
289
290 info "Generating manifest: mode=$mode format=$format manifest=$manifest"
291 [[ -f "$sourcefile" ]] && {
292         info "Sourcing file $sourcefile"
293         . $sourcefile
294         # this may define extra vars: to be taken into account BITBAKE_VARS must be defined
295 }
296
297 [[ "$format" == "json" ]] && {
298         # if jq is present, use it to format json output
299         jq=$(which jq || true)
300         [[ -n "$jq" ]] && {
301                 getmanifest $manifest $mode | $jq ""
302                 exit ${PIPESTATUS[0]}
303         }
304 }
305
306 getmanifest $manifest $mode
307