3 ################################################################################
5 # The MIT License (MIT)
7 # Copyright (c) 2018 Stéphane Desneux <sdx@iot.bzh>
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:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
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
27 ################################################################################
35 function info() { echo "$@" >&2; }
36 function error() { echo "$BASH_SOURCE: $@" >&2; }
37 function out() { echo -n "$@"; }
38 function out_object() {
39 # expected stdin stream is:
52 [[ -z "$k" ]] && { k="$x"; continue; }
63 [[ -z "$k" ]] && { k="$x"; continue; }
74 function out_array() {
75 # expected stdin stream is:
102 function out_value() {
114 if [[ "$x" =~ ^(true|false|null)$ ]]; then
117 elif [[ "$x" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]]; then
120 elif [[ "$x" =~ ^\{.*\}$ ]]; then
123 elif [[ "$x" =~ ^\[.*\]$ ]]; then
127 out "\"$(sed 's/\("\)/\\\1/g' <<<$x)\""
131 function out_comment() {
134 [[ "$verbose" == 1 ]] && echo "# $@" || true
141 function _getgitmanifest() {
142 # this function takes the setup.manifest generated by setup script and uses DIST_METADIR
143 # to analyze git repos
145 local manifest=$1 mode=$2
146 [[ -f $manifest ]] && source $manifest || { error "Invalid setup manifest '$manifest'"; return 1; }
147 [[ ! -d "$DIST_METADIR" ]] && {
148 error "Invalid meta directory. Check variable DIST_METADIR in manifest file '$manifest'."
149 error "$BASH_SOURCE: Also, check directory '$DIST_METADIR'."
152 local GIT=$(which git) REALPATH=$(which realpath)
153 [[ ! -x $GIT ]] && { error "$BASH_SOURCE: Unable to find git command in $PATH."; return 3; }
154 [[ ! -x $REALPATH ]] && { error "$BASH_SOURCE: Unable to find realpath command in $PATH."; return 4; }
156 local gitrepo gitrev metagitdir sep=""
158 for metagitdir in $(ls -d $DIST_METADIR/*/.git); do
159 gitrepo=$($REALPATH -Ls $metagitdir/.. --relative-to=$DIST_METADIR)
160 pushd $DIST_METADIR/$gitrepo &>/dev/null && {
161 gitrev=$( { $GIT describe --long --dirty --always 2>/dev/null || echo "unknown_revision"; } | tr ' \t' '__' )
166 DIST_LAYERS="${DIST_LAYERS}${sep}${gitrepo}:${gitrev}"
171 DIST_LAYERS_MD5=$(echo $DIST_LAYERS|md5sum -|awk '{print $1;}')
173 # in json, transform layers in an object, features in array
174 [[ "$format" == "json" ]] && {
175 DIST_FEATURES=$(for x in $DIST_FEATURES; do
178 DIST_LAYERS=$(for x in $DIST_LAYERS; do
186 DIST_BUILD_HASH="F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
187 DIST_BUILD_ID="${DIST_DISTRO_NAME}-${DIST_MACHINE}-F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
189 # compute setup manifest path and build TS
190 DIST_SETUP_MANIFEST="$($REALPATH $manifest)"
192 # Manifest build timestamp
193 DIST_BUILD_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
195 # what to retain from setup manifest?
196 # to generate the full list: cat setup.manifest | grep = | cut -f1 -d"=" | awk '{printf("%s ",$1);}'
197 declare -A SETUP_VARS
198 SETUP_VARS[deploy]="DIST_MACHINE DIST_FEATURES DIST_FEATURES_MD5 DIST_BUILD_HOST DIST_BUILD_OS DIST_SETUP_TS"
199 SETUP_VARS[target]="DIST_MACHINE DIST_FEATURES"
200 SETUP_VARS[sdk]="DIST_MACHINE DIST_FEATURES"
202 # extra vars not coming from setup.manifest but generated here
203 declare -A EXTRA_VARS
204 EXTRA_VARS[deploy]="DIST_SETUP_MANIFEST DIST_BUILD_TS DIST_LAYERS DIST_LAYERS_MD5 DIST_BUILD_HASH DIST_BUILD_ID"
205 EXTRA_VARS[target]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
206 EXTRA_VARS[sdk]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
208 # BITBAKE_VARS may be defined from external file to source (--source arg)
209 # this is used to dump extra vars from inside bitbake recipe
211 { for x in ${SETUP_VARS[$mode]} ${EXTRA_VARS[$mode]} ${BITBAKE_VARS[$mode]}; do
213 [[ "$format" == "json" ]] && {
214 k=${k#DIST_} # remove prefix
215 k=${k,,*} # to lower case
222 function getmanifest() {
224 out_comment "DISTRO BUILD MANIFEST"
227 # add layers manifest
228 out_comment "----- this fragment has been generated by $BASH_SOURCE"
229 _getgitmanifest $1 $2 || rc=$?
230 out_comment "------------ end of $BASH_SOURCE fragment --------"
237 Usage: $BASH_SOURCE [-v|--verbose] [-f|--format <fmt>] [-m|--mode <mode>] [-s|--source <file>] <setup_manifest_file>
239 -v|--verbose: generate comments in the output file
240 -s|--source: extra file to source (get extra variables generated from bitbake recipe)
241 -f|--format: specify output format: 'bash' or 'json'
242 -m|--mode: specify the destination for the generated manifest
243 'deploy' : for the tmp/deploy/images/* directories
244 'target' : for the manifest to be installed inside a target image
245 'sdk' : for the manifest to be installed inside the SDK
247 <setup_manifest_file> is the input manifest generated from setup script
253 tmp=$(getopt -o h,v,m:,f:,s: --long help,verbose,mode:,format:,source: -n "$BASH_SOURCE" -- "$@") || {
254 error "Invalid arguments."
262 -h|--help) __usage; exit 0;;
263 -v|--verbose) verbose=1; shift ;;
264 -f|--format) format=$2; shift 2;;
265 -m|--mode) mode=$2; shift 2;;
266 -s|--source) sourcefile=$2; shift 2;;
268 *) fatal "Internal error";;
274 [[ ! -f "$manifest" ]] && { __usage; exit 1; }
277 deploy|target|sdk) ;;
278 *) error "Invalid mode specified. Allowed modes are: 'deploy', 'target', 'sdk'"; __usage; exit 42;;
283 *) error "Invalid format specified. Allowed formats are 'json' or 'bash'"; __usage; exit 43;;
286 info "Generating manifest: mode=$mode format=$format manifest=$manifest"
287 [[ -f "$sourcefile" ]] && {
288 info "Sourcing file $sourcefile"
290 # this may define extra vars: to be taken into account BITBAKE_VARS must be defined
293 [[ "$format" == "json" ]] && {
294 # if jq is present, use it to format json output
295 jq=$(which jq || true)
297 getmanifest $manifest $mode | $jq ""
298 exit ${PIPESTATUS[0]}
302 getmanifest $manifest $mode