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 ################################################################################
29 function _getgitmanifest() {
30 # this function takes the setup.manifest generated by setup script and uses DIST_METADIR
31 # to analyze git repos
33 local manifest=$1 mode=$2
34 [[ -f $manifest ]] && source $manifest || { echo "$BASH_SOURCE: Invalid setup manifest '$manifest'" >&2; return 1; }
35 [[ ! -d "$DIST_METADIR" ]] && {
36 echo "$BASH_SOURCE: Invalid meta directory. Check variable DIST_METADIR in manifest file '$manifest'." >&2
37 echo "$BASH_SOURCE: Also, check directory '$DIST_METADIR'." >&2
40 local GIT=$(which git) REALPATH=$(which realpath)
41 [[ ! -x $GIT ]] && { echo "$BASH_SOURCE: Unable to find git command in $PATH." >&2; return 3; }
42 [[ ! -x $REALPATH ]] && { echo "$BASH_SOURCE: Unable to find realpath command in $PATH." >&2; return 4; }
44 local gitrepo gitrev metagitdir sep=""
46 for metagitdir in $(ls -d $DIST_METADIR/*/.git); do
47 gitrepo=$($REALPATH -Ls $metagitdir/.. --relative-to=$DIST_METADIR)
48 pushd $DIST_METADIR/$gitrepo &>/dev/null && {
49 gitrev=$( { $GIT describe --long --dirty --always 2>/dev/null || echo "unknown_revision"; } | tr ' \t' '__' )
54 DIST_LAYERS="${DIST_LAYERS}${sep}${gitrepo}:${gitrev}"
59 DIST_LAYERS_MD5=$(echo $DIST_LAYERS|md5sum -|awk '{print $1;}')
62 DIST_BUILD_HASH="F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
63 DIST_BUILD_ID="${DIST_DISTRO_NAME}-${DIST_MACHINE}-F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
65 # compute setup manifest path and build TS
66 DIST_SETUP_MANIFEST="$($REALPATH $manifest)"
68 # Manifest build timestamp
69 DIST_BUILD_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
71 # what to retain from setup manifest?
72 # to generate the full list: cat setup.manifest | grep = | cut -f1 -d"=" | awk '{printf("%s ",$1);}'
74 SETUP_VARS[deploy]="DIST_MACHINE DIST_FEATURES DIST_FEATURES_MD5 DIST_BUILD_HOST DIST_BUILD_OS DIST_SETUP_TS"
75 SETUP_VARS[target]="DIST_MACHINE DIST_FEATURES"
76 SETUP_VARS[sdk]="DIST_MACHINE DIST_FEATURES"
78 # extra vars not coming from setup.manifest but generated here
80 EXTRA_VARS[deploy]="DIST_SETUP_MANIFEST DIST_BUILD_TS DIST_LAYERS DIST_LAYERS_MD5 DIST_BUILD_HASH DIST_BUILD_ID"
81 EXTRA_VARS[target]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
82 EXTRA_VARS[sdk]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
84 echo "# setup variables in $mode mode"
85 for x in ${SETUP_VARS[$mode]}; do
90 echo "# set by $BASH_SOURCE"
91 for x in ${EXTRA_VARS[$mode]}; do
96 function getmanifest() {
98 echo "# DISTRO BUILD MANIFEST"
101 # add layers manifest
102 echo "# ----- this fragment has been generated by $BASH_SOURCE"
103 _getgitmanifest $1 $2 || rc=$?
104 echo "# ------------ end of $BASH_SOURCE fragment --------"
112 if [[ "$1" =~ ^(-v|--verbose)$ ]]; then
117 if [[ -f "$1" ]]; then
124 deploy|target|sdk) mode=$mode;;
125 *) echo "Invalid mode specified. Allow modes are: deploy target sdk"; exit 42;;
128 getmanifest $manifest $mode | { [[ $verbose == 1 ]] && cat || sed -e 's/#.*$//g;/^\s*$/d'; }
129 exit ${PIPESTATUS[0]}
132 Usage: $0 [-v|--verbose] <setup_manifest_file> [mode]
134 -v|--verbose: generate comments in the output file
136 <setup_manifest_file> is generated from setup script in the specified build dir
138 [mode] specifies the destination for the generated manifest
140 'deploy' : for the tmp/deploy/images/* directories
141 'target' : for the manifest to be installed inside a target image
142 'sdk' : for the manifest to be installed inside the SDK