aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/sdks/agl/get-sdk-info
blob: 12892ac7ea7675b3256368f398b632f5ede92920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
 ###########################################################################
# Copyright 2018 IoT.bzh
#
# author: Sebastien Douheret <sebastien@iot.bzh>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

. $(dirname "$0")/_env-init.sh

usage() {
    echo "Usage: $(basename $0) [-h|--help] [-f|--file <sdk-filename>] [-u|--url <https_url>] [--md5 <string>]"
	exit 1
}

SDK_FILE=""
MD5VAL=""
URL=""
UUID=""

while [ $# -ne 0 ]; do
    case $1 in
        -f|--file)
            shift
            SDK_FILE=$1
            ;;
        -h|--help)
            usage
            ;;
        --md5)
            shift
            MD5VAL=$1
            ;;
        -u|--url)
            shift
            URL=$1
            ;;
        --uuid)
            shift
            UUID=$1
            ;;
        *)
            echo "Invalid argument: $1"
            usage
            ;;
    esac
    shift
done

if [ "${SDK_FILE}" != "" ]; then

    if [ ! -f "${SDK_FILE}" ]; then
        echo "sdk file doesn't exist"
        exit 1
    fi

    # Criteria to consider that SDK is a valid AGL sdk:
    #  - shell and binary file type
    #  - "$MARKER:$" string found
    #  - md5sum match MD5VAL if MD5VAL is set

    if ! file "${SDK_FILE}" |grep shell |grep -q binary ; then
        echo "Not expected file type"
        exit 2
    fi

    if ! grep -aq -m1 "^MARKER:$" "${SDK_FILE}"; then
        echo "marker not found"
        exit 4
    fi

    if [ "${MD5VAL}" != "" ]; then
        if ! echo "${MD5VAL} ${SDK_FILE}" | md5sum --status -c -; then
            echo "md5sum dit not match"
            exit 5
        fi
    fi

    filename=$(basename "${SDK_FILE}")
    sdkUrl=file://${SDK_FILE}
    imageUrl=""
    sdkDate=$(stat -c %y "${SDK_FILE}")
    sdkSize=$(/bin/ls -sh "${SDK_FILE}" |cut -d' ' -f1)

elif [ "${URL}" != "" ]; then

    filename=$(basename "${URL}")
    sdkUrl=${URL}
    imageUrl=""
    sdkDate=""
    sdkSize=""

else
    echo "--file or --url option must be set"
    exit 1
fi

# assume that sdk name follow this format :
#  _PROFILE_-_COMPILER_ARCH_-_TARGET_-crosssdk-_ARCH_-toolchain-_VERSION_.sh
# for example:
#  poky-agl-glibc-x86_64-agl-demo-platform-crosssdk-corei7-64-toolchain-4.0.1.sh


if [[ "${filename}" != *"crosssdk"* ]]; then
    echo "malformed sdk file name"
    exit 6
fi

profile=$(echo "${filename}" | sed -r 's/(.*)-glibc.*/\1/')
version=$(echo "${filename}" | sed -r 's/.*toolchain-(.*).sh/\1/')
arch=$(echo "${filename}" | sed -r 's/.*crosssdk-(.*)-toolchain.*/\1/')
installPath=${SDK_ROOT_DIR}/${profile}/${version}/${arch}

[ "${profile}" = "" ] && { echo "profile not set"; exit 7; }
[ "${version}" = "" ] && { echo "version not set"; exit 8; }
[ "${arch}" = "" ] && { echo " arch not set"; exit 9; }

sdkName=${profile}_${arch}_${version}

# Define a unique ID to be able to distinguish for example corei7-64 from qemux86-64
if [ "${UUID}" = "" ]; then
    curInstDir=$(ls -d "${installPath}/*" 2> /dev/null)
    if [ -d "${curInstDir}" ]; then
        UUID="basename ${curInstDir}"
    elif [ "$URL" != "" ]; then
        UUID=$(echo "$URL" | md5sum |cut -d' ' -f1)
    elif [ "$filename" != "" ]; then
        UUID=$(echo $(basename $filename) | md5sum |cut -d' ' -f1)
    else
        echo "UUID value must be specify using --uuid option."
        exit 1
    fi
fi
installPath="${installPath}/${UUID}"

status="Not Installed"
if [ -d "${installPath}" ]; then
    envFile=$(find "${installPath}" -maxdepth 1 -name "${SDK_ENV_SETUP_FILENAME}")
    [ "${envFile}" != "" ] && status="Installed"
fi

sdkDef=""
if [ "${UUID}" != "" ]; then
    sdkDef=$($(dirname "$0")/db-dump | jq '.[] | select(.uuid=="'${UUID}'")')
elif [ "${URL}" != "" ]; then
    sdkDef=$($(dirname "$0")/db-dump | jq '.[] | select(.url=="'${URL}'")')
fi

if [ "${sdkDef}" != "" ]; then
    sdkName=$(echo "$sdkDef" | jq -r .name)
    [ "$UUID" = "" ] && UUID=$(echo "$sdkDef" | jq -r .uuid)
    [ "$sdkUrl" = "" ] && sdkUrl=$(echo "$sdkDef" | jq -r .url)
    [ "$imageUrl" = "" ] && imageUrl=$(echo "$sdkDef" | jq -r .image_url)
    [ "$sdkDate" = "" ] && sdkDate=$(echo "$sdkDef" | jq -r .date)
    [ "$sdkSize" = "" ] && sdkSize=$(echo "$sdkDef" | jq -r .size)
    [ "$MD5VAL" = "" ] && MD5VAL=$(echo "$sdkDef" | jq -r .md5sum)
fi

read -r -d '' res <<- EndOfMessage
{
    "name":         "${sdkName}",
    "uuid":         "${UUID}",
    "description":  "AGL SDK ${arch} (version ${version})",
    "profile":      "${profile}",
    "version":      "${version}",
    "arch":         "${arch}",
    "path":         "${installPath}",
    "url":          "${sdkUrl}",
    "image_url":    "${imageUrl}",
    "status":       "${status}",
    "date":         "${sdkDate}",
    "size":         "${sdkSize}",
    "md5sum":       "${MD5VAL}",
    "setupFile":    "${envFile}"
}
EndOfMessage

echo "$res"
exit 0