aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/release/mel-checkout
blob: 7f7e807a4ad121abaf0d92ff8e9808a54f8d346f (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/sh

# ---------------------------------------------------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# ---------------------------------------------------------------------------------------------------------------------

set -eu

usage () {
    cat >&2 <<END
usage: ${0##*/} [options] BUILD_DIR [MANIFEST_FILE [EXTRA_MANIFEST_FILES..]]

Create the BUILD_DIR, populate it with bitbake, layers, etc, from this MEL
installation, and configure it via the usual setup-environment script from
meta-mentor.

If no manifest file is specified, and -n is not specified, a prompt will be
shown to interactively select one of the manifests in the same mel
installation as this script. If no extra manifest files are specified, and
neither -n nor -X are specified, a prompt will be shown for each type.

Options:
  -n      Non-interactive mode. Do not prompt
  -X      Disable inclusion of extra manifests
END
    exit 1
}

evalf () {
    fmt="$1"
    shift
    # printf >&2 "evalf: $fmt\n" "$@"
    # eval echo >&2 "evalf: $(printf "$fmt" "$@")"
    eval "$(printf "$fmt" "$@")"
}

verify_machine () {
    workspace="$1"
    manifest_name=$(basename "$2")
    manifest_machine=$(echo "$manifest_name" | cut -d'-' -f3- | cut -d'.' -f1)
    workspace_machine=$(cat "$workspace/.machine")
    if [ "$workspace_machine" != "$manifest_machine" ]; then
        echo >&2 "Error: workspace in $workspace is already setup for $workspace_machine."
        echo >&2 "Please specify a different location if you want to setup for $manifest_machine"
        echo >&2 "or clean up $workspace before continuing."
        exit 1
    fi
}

set_machine () {
    workspace="$1"
    manifest_name=$(basename "$2")
    manifest_machine=$(echo "$manifest_name" | cut -d'-' -f3- | cut -d'.' -f1)
    echo "$manifest_machine" > "$workspace/.machine"
}

prompt_choice () {
    if [ "$1" = "-n" ]; then
        choice_non_interactive=1
        shift
    else
        choice_non_interactive=0
    fi

    prompt_message="$1"
    prompt_default="${2:-}"

    prompt_options="$(mktemp prompt-choice.XXXXXX)"
    cat >>"$prompt_options"

    prompt_count="$(wc -l <"$prompt_options")"
    case $prompt_count in
        0)
            # No choices provided
            rm -f "$prompt_options"
            return 1
            ;;
        1)
            # Only one choice
            cat "$prompt_options"
            rm -f "$prompt_options"
            return 0
            ;;
    esac

    if [ $choice_non_interactive -eq 1 ]; then
        printf >&2 'Error: unable to prompt for `%s` interactively\n' "$prompt_message"
        return 1
    fi

    if [ -n "$prompt_default" ] && \
            ! echo "$prompt_default" | grep -q '^[0-9]*'; then
        printf >&2 'Error: prompt_default value '%s' is invalid, default must be a selection number.\n' "$prompt_default"
        prompt_default=
    fi

    while [ -z "$prompt_value" ]; do
        prompt_num=0
        while read choice; do
            prompt_num=$((prompt_num + 1))
            evalf 'prompt_value_%s="%s"' "$prompt_num" "$choice"
            printf >&2 "%s) %s\n" "$prompt_num" "$choice"
        done <"$prompt_options"

        if [ -n "$prompt_default" ]; then
            printf >&2 "%s [%s]: " "$prompt_message" "$prompt_default"
        else
            printf >&2 "%s: " "$prompt_message"
        fi
        read selection </dev/tty
        if [ -n "$prompt_default" ] && [ -z "$selection" ]; then
            selection="$prompt_default"
        fi
        prompt_value="$(evalf 'echo "${prompt_value_%s}"' "$selection")"
    done
    printf "%s\n" "$prompt_value"
    rm -f "$prompt_options"
}

remove_old_versions () {
    while IFS=- read -r product version machine; do
        printf '%s\t%s\t%s\n' "$product" "$version" "$machine"
    done \
    | uniq -f2 \
    | tr "$(printf '\t')" -
}

sort_manifests () {
    "$scriptdir/version-sort" \
    | tac \
    | sort -s -t- -k3 \
    | if [ $all_versions -eq 0 ]; then remove_old_versions; else cat; fi \
    | sed '/qemu/!s/^/1 /; /qemu/s/^/0 /;' | sort -srn | sed 's/^[01] //'
}

prompt_manifest () {
    find "$1" -maxdepth 1 -type f -name \*.manifest | \
        sed 's,^.*/,,' | sort_manifests | \
        ( set +u; prompt_choice $prompt_non_interactive "$2" 1; ) | sed "s,^,$1/,"
}

process_arguments () {
    no_extra_manifests=0
    non_interactive=0
    all_versions=0
    while getopts Xnah opt; do
        case "$opt" in
            X)
                no_extra_manifests=1
                ;;
            n)
                non_interactive=1
                ;;
            a)
                all_versions=1
                ;;
            \?|h)
                usage
                return 1
                ;;
        esac
    done

    if ! [ -t 1 ] || ! [ -t 0 ]; then
        non_interactive=1
    fi

    if [ $non_interactive -eq 1 ]; then
        prompt_non_interactive=-n
    else
        prompt_non_interactive=
    fi
}

process_arguments "$@" || exit $?
shift $((OPTIND - 1))

if [ $# -eq 0 ]; then
    usage
fi

scriptdir="$(cd "$(dirname "$0")" && pwd -P)"
installdir="$(dirname "$scriptdir")"

project="$1"
shift

mkdir -p "$project"
cd "$project"
echo "$installdir" >"$project/.installpath"

for install_subdir in ../../toolchains ../../tools; do
    if [ -d "$installdir/$install_subdir" ]; then
        ln -sf "$installdir/$install_subdir" .
    fi
done

manifest="$1"
if [ -z "$1" ]; then
    if ! [ -e "$project/.manifest" ]; then
        prompt_manifest "$installdir/manifests" "Select manifest" >"$project/.manifest"
        if ! [ -s "$project/.manifest" ]; then
            echo >&2 "Error: no manifest found, please specify"
            rm -f "$project/.manifest"
            exit 1
        fi
    fi
    if [ -e "$project/.machine" ]; then
        verify_machine "$project" "$(cat "$project/.manifest" | head -n1)"
    fi
    set -- $(cat "$project/.manifest")
    manifest="$1"
else
    if [ -e "$project/.machine" ]; then
        verify_machine "$project" "$manifest"
    fi
    echo "$manifest" >"$project/.manifest"
fi
set_machine "$project" "$manifest"

if [ $no_extra_manifests -eq 1 ]; then
    set -- "$1"
elif [ $# -lt 2 ]; then
    extra_base="$installdir/manifests/extra"
    for extra_dir in "$extra_base"/*/; do
        if [ -d "$extra_dir" ]; then
            extra_dir="${extra_dir%/}"
            name="${extra_dir#$extra_base/}"
            extra_manifest="$(prompt_manifest "$extra_dir" "Select manifest for $name")"
            if [ -z "$extra_manifest" ]; then
                echo >&2 "Error: no extra manifest for $name?"
                exit 1
            else
                set -- "$@" "$extra_manifest"
                echo "$extra_manifest" >>"$project/.manifest"
            fi
        fi
    done
fi

infofn="${manifest%.manifest}.info"
if [ -e "$infofn" ]; then
    bspfiles_path="$(sed -n -e 's/^bspfiles_path = //p' "$infofn")"
    if [ -n "$bspfiles_path" ] && [ -e "$installdir/$bspfiles_path" ]; then
        rm -f "${bspfiles_path%/*}"
        ln -s "$installdir/$bspfiles_path" "${bspfiles_path%/*}"
        for i in xlayers.conf customer.conf; do
            if [ -e "$installdir/$bspfiles_path/$i" ]; then
                ln -s "$installdir/$bspfiles_path/$i" .
            fi
        done
    fi
fi

mkdir -p downloads

for manifest;  do
    printf '%s\t%s\n' "${manifest##*/}" "$manifest"
done | (
    read -r base_manifest
    printf '%s\n' "$base_manifest"
    "$scriptdir/version-sort"
) | cut -d"$(printf '\t')" -f2 | \
    while read -r manifest; do
        cat "$manifest" | while read -r checkout_path commit remotes; do
            if [ ! -d "$checkout_path/.git" ]; then
                git init "$checkout_path"
            fi

            cd "$checkout_path"
            if [ -d "$installdir/objects" ]; then
                echo "$installdir/objects" >.git/objects/info/alternates
            fi
            if [ -e "$installdir/git-bundles/$commit.bundle" ]; then
                git bundle unbundle "$installdir/git-bundles/$commit.bundle" >/dev/null
            fi

            echo "$remotes" | tr '\t' '\n' | while IFS== read -r name url; do
                if [ -n "$name" ]; then
                    if git remote | grep -qxF "$name"; then
                        git remote set-url "$name" "$url"
                    else
                        git remote add "$name" "$url"
                    fi
                fi
            done

            if [ -z "$remotes" ]; then
                echo "$commit" >.git/shallow
            fi
            # Create branches for all manifests, for reference
            manifestdir="$(dirname "$manifest")"
            for otherfn in "$manifestdir/"*.manifest; do
                while read -r other_checkout_path other_commit _; do
                    if [ "$other_checkout_path" = "$checkout_path" ]; then
                        branch="$(basename "${otherfn%.manifest}")"
                        if ! git rev-parse -q --verify "refs/heads/$branch" >/dev/null; then
                            if [ -e "$installdir/git-bundles/$other_commit.bundle" ]; then
                                git bundle unbundle "$installdir/git-bundles/$other_commit.bundle" >/dev/null
                            fi
                            git update-ref "refs/heads/$branch" "$other_commit"
                        fi
                        if [ -z "$remotes" ]; then
                            echo "$other_commit" >>.git/shallow
                        fi
                    fi
                done <"$otherfn"
            done

            branch="$(basename "${manifest%.manifest}")"
            if ! git rev-parse -q --verify "refs/heads/$branch" >/dev/null; then
                git update-ref "refs/heads/$branch" "$commit"
            fi
            if [ "$(git rev-parse -q --verify --symbolic-full-name HEAD 2>/dev/null)" != "refs/heads/$branch" ]; then
                git checkout "$branch"
            fi

            cd - >/dev/null
        done

        downloads="${manifest%.manifest}.downloads"
        if [ -e "$downloads" ]; then
            cat "$downloads" | while read -r path checksum; do
                destdir="$(dirname "$path")"
                if [ "$destdir" != "." ]; then
                    mkdir -p "downloads/$destdir"
                    ln -sf "$path" downloads/
                    ln -sf "${path}.done" downloads/
                fi
                ln -sf "$installdir/downloads/$checksum" "downloads/$path"
                touch "downloads/${path}.done"
            done
        fi
    done