Developing lightweight computation at the DSG edge

cooker 27.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

RESULT=0

[ -f options.conf.local ] && . options.conf.local || . options.conf
[ -f ${flavors_file}.local ] && . ${flavors_file}.local || . ${flavors_file}

[ ! -d "$tmp_dir" ] && mkdir -p "$tmp_dir"
[ ! -d "$downloads_dir" ] && mkdir -p "$downloads_dir"
[ ! -d "$bin_output" ] && mkdir -p "$bin_output"
[ ! -d "$files_dir" ] && mkdir -p "$files_dir"
J=${J:-$make_j}

14
15
16
17
18
19
20
21
22
23
24
IMAGEOPT=true
VERSIONOPT=true
VERSION_DIST="${dist_name:-qMp}"
VERSION_NICK="${release:-Kalimotxo}"
VERSION_NUMBER="${version:-trunk}"
VERSION_MANUFACTURER="${manufacturer:-Quick Mesh Project}"
VERSION_MANUFACTURER_URL="${manufacturer_url:-https://qmp.cat}"
VERSION_BUG_URL="${bug_url:-https://dev.qmp.cat/projects/qmp/issues}"
VERSION_SUPPORT_URL="${support_url:-https://qmp.cat/Documentation}"
QMP_CODENAME=$VERSION_NICK
QMP_RELEASE=$VERSION_NUMBER
25
QMP_DOWNLOADS_URL="${qmp_downloads_url:-http://fw.qmp.cat}"
26

27
usage() {
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
28
29
    echo "Usage: $0 [-f [--force]] [-d <target> [--sdk|ib|force]] [-i <target> [--sdk-file=<file>|ib-file=<file>]]
                [--download-all|build-all|update-feeds] [--targets|flavors|communities|update-communities|profiles=<target>]
30
                [-b <target> [-j<N>] [--no-update|no-link-ib|remote|clean|force-local|package=<pkg>]]
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
31
                [-c <target> [--profile=<profile>|no-update|remote|flavor=<flavor>|community=<path>|extra-pkg=<list>]]
32
33
34
35
36
37
38
39
40
41
                [--help]"
    echo ""
    echo "    --help                     : show full help with examples"
    echo "    --download-all             : download all SDK and ImageBuilders"
    echo "    --build-all	               : build SDK for all available tagets"
    echo "    --cook-all	               : cook firmwares for all available targets and profiles"
    echo "    --targets                  : list all officialy supported targets"
    echo "    --profiles=<target>        : list available hardware profiles for a specific target"
    echo "    --flavors                  : list available LibreMesh flavors for cooking"
    echo "    --communities              : list available community profiles"
p4u's avatar
p4u committed
42
    echo "    --update-communities       : update or download community profiles"
43
44
45
46
47
48
49
50
51
52
53
    echo "    --update-feeds             : update previously downloaded feeds (only works for Git feeds)"
    echo "    -f                         : download feeds based on feeds.conf.default file. Feeds will be shared among all targets"
    echo "       --force                 : force reinstall of feeds (remove old if exist)"
    echo "    -d <target>                : download SDK and ImageBuilder for specific target"
    echo "       --sdk                   : download only SDK"
    echo "       --ib                    : download only ImageBuilder"
    echo "       --force                 : force reinstall of SDK and/or ImageBuilder (remove old if exist)"
    echo "    -i <target>                : install local/custom SDK or ImageBuilder"
    echo "       --sdk-file=<file>       : specify SDK file to unpack"
    echo "       --ib-file=<file>        : specify ImageBuilder file to unpack"
    echo "    -b <target>                : build SDK for specific target and link it to the ImageBuilder"
54
    echo "       -j<N>                   : number of threads to use when building (recommended N=#cores+1)"
55
56
57
58
59
60
61
62
63
64
65
66
    echo "       --no-link-ib            : do not download and link ImageBuilder when building the SDK"
    echo "       --no-update             : do not update feeds when building SDK"
    echo "       --clean                 : clean sources before compiling"
    echo "       --package=<pkg_name>    : build only a package (and its dependencies)"
    echo "       --force-local           : force installation of all local SDK compiled packages when cooking firmware"
    echo "    -c <target>                : cook the firmware for specific target. Can be used with next options"
    echo "       --profile=<profile>     : use <profile> when cooking firmware (default is all available target profiles)"
    echo "       --flavor=<flavor>       : use <flavor> when cooking firmware (default $default_flavor)"
    echo "       --extra-pkg=<pkg_list>  : specify extra packages (separated by spaces) for including into the output firmware"
    echo "       --remote                : instead of building local SDK packages. Use only remote repositories for cooking"
    echo "       --community=<name/prof> : specify which network community and profile device to use (if any)"
    echo "       --no-update             : do not update package list when cooking (requires patch_ib_no_update.sh snippet)"
67
68
    echo "    -r                         : make a full qMp release"
    echo "       --refresh               : refresh a qMp release in this directory"
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
    echo ""
}

examples() {
    echo "Examples:"
    echo ""
    echo " - Build packages using the SDK and cook the firmware for target tl-wdr3500-v1 and flavor generic (all in one command)"
    echo ""
    echo "    $0 -c ar71xx/generic --flavor=lime_default --profile=tl-wdr3500-v1"
    echo ""
    echo " - Cook the firmware without compiling the SDK but using only remote precompiled binaries"
    echo ""
    echo "    $0 -c ar71xx/generic --remote --flavor=lime_mini --profile=tl-wdr3500-v1"
    echo ""
    echo " - Build SDK and cook ar71xx target with all available profiles (step by step)"
    echo ""
    echo "    $0 -d ar71xx/generic                        # download SDK and IB "
    echo "    $0 -f                                       # download and prepare feeds"
    echo "    $0 -b ar71xx/generic                        # build the SDK and link it to IB"
    echo "    $0 -c ar71xx/generic --flavor=lime_default  # cook all firmwares for target ar71xx/generic"
    echo ""
    echo " - If you want to use an existing community network profile, specify it when cooking (in addition to the device profile)"
    echo ""
    echo "    $0 -c ar71xx/generic --flavor=lime_default --community=quintanalibre.org.ar/comun --profile=tl-wdr3500-v1"
    echo ""
    echo " - If not profile defined, cook all profiles of target. Also --extra-pkg option can be used to add extra packages when cooking"
    echo ""
    echo "    $0 -c ar71xx/generic --flavor=lime_zero --extra-pkg=\"luci-proto-3g iperf\""
    echo ""
    echo " - To see/debug build errors use J (number of threads) and V (verbose) system vars"
    echo ""
    echo "    J=1 V=s $0 -b ar71xx/generic --profile=tl-wdr3500-v1"
}

list_targets() {
    cat $targets_list
}

cook() {
    local target="$1"
    local profile="$2"
    local ib="$release/$target/ib"
    local output_dir="$PWD/$bin_output/$target/$profile/$flavor"

    [ ! -d "$ib" ] && {
        echo "-> ImageBuilder for target $target not ready"
        download_ib $target
    }

118
119
120
    echo "-> Copy public qMp.cat usign keys from keys"
    cp -v keys/* $ib/keys

121
122
123
124
125
126
127
128
129
130
131
132
133
    [ -n "$use_remote_pkgs" ] && {
        local arch=`get_arch_from_ib $target`
        echo "-> Using remote repository for ImageBuilder, architecture: $arch"
        cat $ib/repositories.original.conf $remote_pkg_repos | sed s/"{ARCH}"/$arch/g > $ib/repositories.conf
    }

    [ -z "$use_remote_pkgs" ] && {
        [ ! -f $ib/repositories.sdk.conf ] && build_sdk $target
        cp -f $ib/repositories.sdk.conf $ib/repositories.conf
    }

    echo "-> Cooking $target/$profile"
    [ ! -d "$output_dir" ] && mkdir -p $output_dir
134
135
136
137
138
139
140
141

    # handle community packages and files, and local files
    local ib_files="${PWD}/$tmp_dir/files"
    local myfiles="${PWD}/$files_dir"
    rm -rf ${ib_files}
    mkdir -p ${ib_files}
    [ -n "$community_files" ] && {
      [ -f "${community_files}/PACKAGES" ] && {
p4u's avatar
p4u committed
142
143
        echo "--> Adding community packages: "
        join_packages $(cat "${community_files}/PACKAGES")
144
145
      }
      echo "--> Copying community files"
p4u's avatar
p4u committed
146
      cp -rf ${community_files}/* $ib_files/ 2>/dev/null
147
148
      [ -f ${ib_files}/PACKAGES ] && rm -f ${ib_files}/PACKAGES
    }
p4u's avatar
p4u committed
149
    cp -rf ${myfiles}/* ${ib_files}/ 2>/dev/null
150

151
152
    [ "$qmp_release" == "1" ] && generate_qmp_feeds_file $target

153
    # make binary using imagebuilder
154
155
156
157
158
159
160
    local fw_extra_name="$flavor"
    [ -n "$community" ] && fw_extra_name="$community-$community_profile"
    find $myfiles/* 2>/dev/null && ib_files="FILES=$myfiles" # if files directory is empty, IB crashes
    [ "$profile" == "Default" ] && profile="" # if profile is Default better leave it blank. some targets do not have any profile
    [ -n "$profile" ] && profile="PROFILE=$profile"
    [ -n "$no_update" ] && force_no_update="NO_UPDATE=1"
    [ -f "files_remove" ] && files_remove="FILES_REMOVE=$(pwd)/files_remove"
161
    echo "-> Cooking firmware image"
p4u's avatar
p4u committed
162
    join_packages ${!flavor}
p4u's avatar
p4u committed
163
    echo "--> Selected extra packages:$PKG"
164
165
    # Fix for #483
    make -C $ib image $profile PACKAGES="$PKG" EXTRA_IMAGE_NAME="$fw_extra_name" BIN_DIR="$output_dir" $ib_files $force_no_update $files_remove CONFIG_SELINUX=0
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
    ERROR=$?
    if (($ERROR != 0)); then
      echo -e "\n-> Firmware not cooked, something wrong happened on the ImageBuilder compilation process"
      ERROR_PROFILES="$ERROR_PROFILES $target/$profile/$flavor"
    else
      echo -e "\n-> Firmware for target $target, profile $profile and flavor $flavor cooked!\nFind the binaries in $output_dir directory"
    fi
    return $ERROR
}

cook_all_profiles() {
    local target=$1
    echo -n -e "\n-> Cooking ALL profiles for target $target"
    for p in `get_profile_names`; do
      local log="$tmp_dir/$(echo $target.$p | sed s/"\/"/-/g).log"
      echo -n -e "\n--> Cooking $target $p"
      (cook $target $p &> $log) && { echo -n ": SUCCESS!"; mv $log $log.ok; } || {
          mv $log $log.fail; echo -n ": FAIL! (log at $log.fail)"; }
    done
    [ -n "$ERROR_PROFILES" ] && echo "-> Profiles with errors: $ERROR_PROFILES"
}

cook_all_targets() {
    for t in $(list_targets); do
      cook_all_profiles $t
    done
    [ -n "$ERROR_PROFILES" ] && echo "-> Profiles with errors: $ERROR_PROFILES"
}

clean_sdk() {
    local target="$1"
    local sdk="$release/$target/sdk"
    echo "--> Cleaning SDK before compiling"
    make -j$J V=$V -C $sdk package/clean
}

build_sdk() {
    local target="$1"
    local sdk="$release/$target/sdk"
    local ib="$release/$target/ib"

    [ ! -d "$sdk" ] && {
        echo "-> SDK for target $target not found"
        download_sdk $target
    }
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
211

Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
212
    [ -d "$sdk" ] && [ -n $refresh_release ] && {
213
214
215
216
        echo "-> SDK for target $target found, uncompressing on top"
        download_sdk $target
    }

217
    echo "-> Building $sdk"
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
218
219

    [ -f "$feeds_file" ] || {
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        echo "-> Local feeds file not found"
        download_feeds
    }
    cp -f "$feeds_file" "$sdk/feeds.conf"
    [ -z "$no_update" ] && (cd $sdk && scripts/feeds update -a) || (cd $sdk && scripts/feeds update -i)

    [ -n "$sdk_clean" ] && clean_sdk $target

    for r in $sdk_install_repos; do
        (cd $sdk && scripts/feeds install -p $r -a)
    done
    for p in $sdk_install_packages; do
        (cd $sdk && scripts/feeds install $p)
    done

    [ -n "$package" ] && {
        build_pkg $sdk $package
        return
    }

    cp ${sdk_config}.local $sdk/.config 2>/dev/null || cp ${sdk_config} $sdk/.config
    make -C $sdk defconfig
242

243
    echo "-> Copy private and public qMp.cat usign keys from ../keys"
244
245
    cp -v ../keys/key-build* $sdk

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
    make -j$J -C $sdk V=$V && [ -z "$no_link_ib" ] && {
        [ -f "$ib/repositories.original.conf" ] || download_ib $target
        cat $ib/repositories.original.conf > $tmp_dir/repositories.sdk.conf
        echo "-> Linking IB with SDK"
        [ -n "$force_local" ] && echo "-> Force all local packages is on"
        echo "" >> $tmp_dir/repositories.sdk.conf
        echo "##SDK_repos" >> $tmp_dir/repositories.sdk.conf
        for r in $PWD/$sdk/bin/packages/*/*; do
          [ -z "$force_local" ] && {
            case $r in
            # we build the master branch of the routing feed, hence
            # don't include it in the filter
            *base | *luci | *packages | *telephony)
               continue
              ;;
            esac
          }
          echo "src sdk_$(basename $r) file:$r" >> $tmp_dir/repositories.sdk.conf
        done
        # Add target packages (kmods) if force-local enabled
        [ -n "$force_local" ] && {
            local target_pkg_dir="$PWD/$sdk/bin/targets/$target/packages"
            [ -d "$target_pkg_dir" ] && \
            echo "src sdk_target file:$target_pkg_dir" >> $tmp_dir/repositories.sdk.conf
        }
        echo "" >> $tmp_dir/repositories.sdk.conf
        cp -f $tmp_dir/repositories.sdk.conf $ib/repositories.sdk.conf
        echo "-> Using custom $tmp_dir/repositories.sdk.conf"
    } || echo "-> Error compiling SDK"
}

build_pkg() {
  local sdk=$1
  local pkg=$2
  local output_dir=$PWD/$bin_output/packages
  [ ! -d $output_dir ] && mkdir -p $output_dir
  echo "-> Building package $pkg on sdk $sdk"
283
  make -j$J V=$V -C $sdk IMAGEOPT="$IMAGEOPT" VERSIONOPT="$VERSIONOPT" VERSION_DIST="$VERSION_DIST" VERSION_NICK="$VERSION_NICK" VERSION_NUMBER="$VERSION_NUMBER" VERSION_MANUFACTURER="$VERSION_MANUFACTURER" VERSION_MANUFACTURER_URL="$VERSION_MANUFACTURER_URL" VERSION_BUG_URL="$VERSION_BUG_URL" VERSION_SUPPORT_URL="$VERSION_SUPPORT_URL" BRANCH_GIT="$QMP_GIT_BRANCH" REV_GIT="$REV_GIT" QMP_CODENAME="$VERSION_NICK" QMP_RELEASE="$VERSION_NUMBER" package/$pkg/{clean,compile,install} && {
284
  echo "-> Build of $pkg successful"
285
286
287
  local pkg_path=$(find $sdk/bin/packages -type f -name "${pkg}*.ipk")
  local pkg_file=$(echo $pkg_path | awk -F/ '{print $NF}')
  cp -f $pkg_path $output_dir/
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  echo "-> You can find it on $output_dir/$pkg_file"
  } || echo "-> Build error, see full output using V=s J=1 ./cooker <options>"
}

download_feeds() {
    local feeds_template="$feeds_default_file"
    [ -f "${feeds_template}.local" ] && feeds_template=${feeds_template}.local

    [ -f "$feeds_file" ] && [ -z "$force_install" ] && {
        echo "-> Feeds already downloaded, doing nothing. Use --force to reinstall"
        return
    }

    [ -f $feeds_file ] && rm -f $feeds_file
    [ ! -d $feeds_dir ] && mkdir -p "$feeds_dir"
    echo "-> Using feeds from file $feeds_template"
    echo "-> Downloading feeds into $feeds_dir/"
305
    set -e
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    while read feed; do
        echo "$feed" | grep -q ^src-git || continue
        name="$(echo $feed | awk '{print $2}')"
        full_url="$(echo $feed | awk '{print $3}')"
        [ -d $feeds_dir/$name ] && rm -rf $feeds_dir/$name
        if echo "$full_url" | grep \;; then
            url="$(echo $full_url | awk -F\; '{print $1}')"
            branch="$(echo $full_url | awk -F\; '{print $2}')"
            git clone $url -b $branch $feeds_dir/$name
        elif echo "$full_url" | grep \^; then
            url="$(echo $full_url | awk -F\^ '{print $1}')"
            commit="$(echo $full_url | awk -F\^ '{print $2}')"
            git clone $url $feeds_dir/$name
            ( cd $feeds_dir/$name && git checkout $commit )
        fi
        echo "src-link $name $PWD/$feeds_dir/$name" | tee -a $feeds_file
    done < $feeds_template
323
    set +e
324
325
326
327
328
329
330
331
332
}

download_communities() {
    [ ! -d "$communities_dir" ] && {
        echo "-> Downloading communities repository"
        git clone $communities_git $communities_dir || {
            echo "-> Cannot fetch communities repository"
            exit 1
        }
333
334
335
336
337
    }
}

update_communities() {
    [ ! -d "$communities_dir" ] && download_communities || {
338
        (cd $communities_dir && git pull) || {
p4u's avatar
p4u committed
339
340
341
            echo "-> Cannot update communities repository"
            exit 1
        }
342
      }
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
}

list_communities() {
    download_communities
    find $communities_dir/ -maxdepth 1 -type d | grep -v .git | \
      while read c; do echo ${c#*/}; done | grep -v ^$
}

update_feeds() {
    for f in $feeds_dir/*; do
        [ -d $f/.git ] && (cd $f && git pull)
    done
}

build_all_sdk() {
    for t in $(cat $targets_list); do build_sdk $t; done
}

download_all() {
    cat $targets_list | while read t; do download_sdk $t; download_ib $t; done
}

download_sdk() {
    local target="$1"
    [ -z "$target" ] && {
        echo "-> You must specify target to download, check $targets_list file"
        usage
        exit 1
    }
    local url="$base_url/$target"
    local output="$release/$target"
    [ ! -d "$output" ] && mkdir -p "$output"
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
375

376
    local sdk_file="$(wget -q -O- $url | grep openwrt-sdk | grep -Eoi '<a [^>]+>' |  grep -Eo 'href="[^\"]+"' | cut -d '=' -f 2 | sed 's/\"//g')"
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    echo "-> Downloading $url/$sdk_file"
    wget -c "$url/$sdk_file" -O "$tmp_dir/$sdk_file" || {
        echo "-> Error, cannot download SDK $url/$sdk_file"
        exit 1
    }
    unpack_sdk $tmp_dir/$sdk_file $output
}

unpack_sdk() {
    local file="$1"
    local output="$2"
    [ -d $output/sdk ] && [ -z "$force_install" ] && {
        echo "-> SDK for $target already exist, doing nothing. Use --force to reinstall"
    } || {
        echo "-> Unpacking SDK to $output"
        tar xf $file -C $output/ && {
            [ -d $output/sdk ] && rm -rf $output/sdk
394
            mv $output/openwrt-sdk* $output/sdk
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
            rm -rf $output/sdk/dl
            local dl=$downloads_dir
            echo $dl | grep -q / || dl="$PWD/$dl"
            ln -s $dl $output/sdk/dl
            #rm -f $TMP/$SDK_FILE
        } || echo "-> Error installing SDK"
    }
}

download_ib() {
    local target="$1"
    [ -z "$target" ] && {
        echo "-> You must specify target to download, check $targets_list file"
        usage
        exit 1
    }
    local url="$base_url/$target"
    local output="$release/$target"
    [ ! -d "$output" ] && mkdir -p "$output"
414
    local ib_file="$(wget -q -O- $url | grep openwrt-imagebuilder | grep -Eoi '<a [^>]+>' |  grep -Eo 'href="[^\"]+"' | cut -d '=' -f 2 | sed 's/\"//g')"
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    echo "-> Downloading $url/$ib_file"
    wget -c "$url/$ib_file" -O "$tmp_dir/$ib_file" || {
        echo "-> Error, cannot download ImageBuilder $url/$ib_file"
        exit 1
    }
    unpack_ib $tmp_dir/$ib_file $output
}

unpack_ib() {
     local file="$1"
     local output="$2"
     [ -d $output/ib ] && [ -z "$force_install" ] && {
        echo "-> ImageBuilder for $target already exist, doing nothing. Use --force to reinstall"
     } || {
        echo "-> Unpacking ImageBuilder to $output"
        tar xf $file -C $output/ && {
           [ -d $output/ib ] && rm -rf $output/ib
432
           mv $output/openwrt-imagebuilder* $output/ib
433
434
435
436
437
438
439
           cp -f $output/ib/repositories.conf $output/ib/repositories.original.conf
           sed -i 's%^src/gz .*_routing .*$%# &%' $output/ib/repositories.original.conf
       } || echo "-> Error installing ImageBuilder"
    }
}

list_flavors() {
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
440
    cat qmp.flavors.conf | egrep -v "^_|^#|^$" | awk -F= '{print $1}'
441
442
}

443
444
445
446
447
448
449
450
451
452
453
generate_qmp_feeds_file() {
  local target=$1
  local qmp_feeds_dir="${PWD}/files/etc/opkg"

  [ ! -d "$qmp_feeds_dir" ] && mkdir -p "$qmp_feeds_dir"
  [ -f $qmp_feeds_dir/qmpfeeds.conf ] && rm -f $qmp_feeds_dir/qmpfeeds.conf

  for r in $sdk_install_repos; do
    this_qmp_feed_url="$QMP_DOWNLOADS_URL/Releases/$VERSION_NUMBER/packages/$(get_arch_from_ib $target)/$r"
    echo "src/gz qmp_$r $this_qmp_feed_url" >> $qmp_feeds_dir/qmpfeeds.conf
  done
454
455
456
457
458

  for r in $sdk_install_repos; do
    this_qmp_feed_repo="openwrt_${r} "
    sed -i "/${this_qmp_feed_repo}/d" ${qmp_feeds_dir}/distfeeds.conf
  done
459
460
}

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
get_profile_names() {
    local profiles="$(cat $release/$target/ib/.profiles.mk 2>/dev/null | grep ^PROFILE_NAMES | \
      sed -e s/"PROFILE_NAMES = Default"//g -e s/DEVICE_//g)"
    profiles="${profiles:-Default}"
    echo "$profiles"
}

get_arch_from_ib() {
    local target=$1
    [ ! -d $release/$target/ib ] && download_ib $target
    grep CONFIG_TARGET_ARCH_PACKAGES $release/$target/ib/.config | cut -d= -f2 | tr -d \"
}

list_profiles() {
    local target="$1"
    [ ! -d $release/$target/ib ] && download_ib $target
    make -C $release/$target/ib info
}

p4u's avatar
p4u committed
480
481
482
483
# join a list of new packages into PKG var, older has preference
join_packages() {
  local new="$@"
  for p in $new; do
p4u's avatar
p4u committed
484
    echo "$PKG" | egrep -q "^(|-)${p#-*}($|)" || PKG="$PKG $p"
p4u's avatar
p4u committed
485
486
487
  done
}

488
make_qmp_release() {
489
490
491
492
493
494
495

    # Download the feeds (./cooker -f)
    download_feeds

    # Update feeds if refresh_release is on
    [ -n "$refresh_release" ] && update_feeds

496
    # Remove downloaded IB and SDK files, if using trunk
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
497
    [ -n "$refresh_release" ] && [ "$version" == "trunk" ] && rm -rf tmp/openwrt-imagebuilder-*.tar.xz tmp/openwrt-sdk-*.tar.xz
498
499

    # Remove decompressed IB and SDK directories, if using trunk
500
    [ -n "$refresh_release" ] && [ "$version" == "trunk" ] && rm -rf $release/*/*/sdk $release/*/*/ib
501

502
503
504
505
    # Build the SDK and the packages locally for each architecture
    force_local=1
    build_all_sdk

506
507
508
509
510
511
512
513
514
515
    DISTCL="openwrt"
    DISTLEGACY="openwrt"
    COMBINEDEXT4IMG="combined-ext4.img.gz"
    COMBINEDEXT4VDI="combined-ext4.vdi"
    COMBINEDEXT4VMDK="combined-ext4.vmdk"
    COMBINEDSQUASH="combined-squashfs.bin"
    COMBINEDSQUASHIMG="combined-squashfs.img.gz"
    COMBINEDSQUASHVDI="combined-squashfs.vdi"
    COMBINEDSQUASHVMDK="combined-squashfs.vmdk"
    EXT4SDCARD="ext4-sdcard.img.gz"
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
516
517
    EXT4COMBINEDIMGGZ="ext4-combined.img.gz"
    SQUASHCOMBINEDIMGGZ="squashfs-combined.img.gz"
518
519
520
521
522
523
524
    SDCARDVFATEXT4="sdcard-vfat-ext4.img"
    SQUASHFACTORY="squashfs-factory.bin"
    SQUASHSYSUPGRADE="squashfs-sysupgrade.bin"
    SQUASHFSSDCARD="squashfs-sdcard.img.gz"

    [ -f qmp.profiles.conf ] && . qmp.profiles.conf

525
526
527
    mkdir -p $bin_output
    cp DEVICES $bin_output/

528
529
530
531
532
    for i in $SUPPORTED_PROFILES; do
      TPROFILE="$i" && . qmp.profiles.conf

      flavor=$FLAVOR

533
      pre_PKG=$PKG
534
535
      join_packages $EXTRAPKGS

536
537
538
539
540
541
      cook "$ARCH/$SUBARCH" "$PROFILE"

      local output_dir="$PWD/$bin_output/$ARCH/$SUBARCH/$PROFILE/$FLAVOR"

      [ "$FACTORY" ] && {
        echo "$NAME - FACTORY"
542
        fw_file="$output_dir/$(echo $FACTORY)"
543
        output_file=$bin_output"/qMp_"$VERSION_NUMBER"-"$VERSION_NICK"_"$NAME"_factory_"$(date +%Y%m%d-%H%M)".bin"
544
545
        echo cp "$fw_file" "$output_file"
        cp "$fw_file" "$output_file"
546
547
548
      }
      [ "$SYSUPGRADE" ] && {
        echo "$NAME - SYSUPGRADE"
549
        fw_file="$output_dir/$(echo $SYSUPGRADE)"
550
        output_file=$bin_output"/qMp_"$VERSION_NUMBER"-"$VERSION_NICK"_"$NAME"_sysupgrade_"$(date +%Y%m%d-%H%M)".bin"
551
552
        echo cp "$fw_file" "$output_file"
        cp "$fw_file" "$output_file"
553
554
555
      }
      [ "$SQUASHIMAGE" ] && {
        echo "$NAME - SQUASHIMAGE"
556
        fw_file="$output_dir/$(echo $SQUASHIMAGE)"
557
        output_file=$bin_output"/qMp_"$VERSION_NUMBER"-"$VERSION_NICK"_"$NAME"_combined-squashfs_"$(date +%Y%m%d-%H%M)"."$BINEXT
558
559
        echo cp "$fw_file" "$output_file"
        cp "$fw_file" "$output_file"
560
561
562
      }
      [ "$EXT4IMAGE" ] && {
        echo "$NAME - EXT4IMAGE"
563
        fw_file="$output_dir/$(echo $EXT4IMAGE)"
564
        output_file=$bin_output"/qMp_"$VERSION_NUMBER"-"$VERSION_NICK"_"$NAME"_combined-ext4_"$(date +%Y%m%d-%H%M)"."$BINEXT
565
566
        echo cp "$fw_file" "$output_file"
        cp "$fw_file" "$output_file"
567
568
      }

569
570
      PKG=$pre_PKG

571
572
    done

573
    cd "$bin_output"
574
575
576
    md5sum *.* >> IMAGES
    cd ..

577
578
579
    # Run repository snippet
    ./snippets/create_repository.sh

580
581
}

582
[ -z "$1" ] && usage
583
OPTS=$(getopt -o hd:fb:c:d:i:j:rb: -l targets,build-all,download-all,profiles:,flavors,profile:,flavor:,\
584
update-feeds,no-update,no-link-ib,only-sdk,only-ib,remote,ib,sdk,sdk-file:,ib-file:,help,force,clean,\
585
communities,update-communities,community:,cook-all,package:,extra-pkg:,refresh,force-local -n $0 -- "$@")
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
eval set -- "$OPTS"

while true; do
    case $1 in
        --help)
            usage
            examples
            break
        ;;
        --download-all)
            download_all
            break
        ;;
        --build-all)
            build_all=1
            shift
        ;;
        --cook-all)
            cook_all=1
            shift
        ;;
        --targets)
            list_targets
            break
        ;;
        --profiles)
            list_profiles $2
            break
        ;;
        --flavors)
            list_flavors
            break
        ;;
        --communities)
            list_communities
            break
        ;;
        --update-feeds)
            update_feeds
            break
        ;;
        --no-update)
            no_update=1
            shift
        ;;
        --no-link-ib)
            no_link_ib=1
            shift
        ;;
        -d)
            download=1
            target=$2
            shift;shift
        ;;
        --sdk)
            only_sdk=1
            shift
        ;;
        --ib)
            only_ib=1
            shift
        ;;
        --force)
            force_install=1
            shift
        ;;
        -i)
            local_install=1
            target=$2
            shift;shift
        ;;
        --sdk-file)
            sdk_path=$2
            shift;shift
        ;;
        --ib-file)
            ib_path=$2
            shift;shift
        ;;
        -f)
            download_feeds=1
            shift
        ;;
        -b)
            build_sdk=1
            target=$2
            shift;shift
        ;;
674
675
676
677
        -j)
            J=$2
            shift;shift
        ;;
678
        --clean)
p4u's avatar
p4u committed
679
680
            sdk_clean=1
            shift
681
682
        ;;
        --package)
p4u's avatar
p4u committed
683
684
            package=$2
            shift;shift
685
686
687
688
689
        ;;
        -c)
            cook=1
            target="$2"
            shift;shift
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
690
        ;;
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
        --profile)
            profile="$2"
            shift;shift
        ;;
        --flavor)
            flavor="$2"
            shift;shift
        ;;
        --remote)
            use_remote_pkgs="1"
            shift
        ;;
        --force-local)
            force_local="1"
            shift
        ;;
p4u's avatar
p4u committed
707
708
709
710
        --update-communities)
            update_communities="1"
            shift
        ;;
711
712
713
714
715
716
        --community)
            community="${2%/*}"
            community_profile="${2#*/}"
            shift;shift
        ;;
        --extra-pkg)
p4u's avatar
p4u committed
717
            system_pkg="$PKG"
718
            PKG="$2"
p4u's avatar
p4u committed
719
            join_packages $system_pkg
720
721
            shift;shift
        ;;
722
723
724
725
        -r)
            qmp_release=1
            shift
        ;;
726
727
728
729
        --refresh)
            refresh_release=1
            shift
        ;;
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
        -h)
            usage
            break
        ;;
        --)
            shift
            break
        ;;
        *)
        ;;
    esac
done

flavor=${flavor:-$default_flavor}
profile=${profile:-Default}

[ "$local_install" == "1" ] && {
  [ -n "$sdk_path" ] && unpack_sdk "$sdk_path" $release/$target
  [ -n "$ib_path" ] && unpack_ib "$ib_path" $release/$target
  [ -z "$sdk_path$ib_path" ] && {
    echo "-> Either --sdk-file or --ib-file must be specified (or both)"
    exit 1
  }
}

[ "$download" == "1" ] && {
    [ "$only_sdk" == "1" ] && download_sdk $target
    [ "$only_ib" == "1" ] && download_ib $target
    [ -z "$only_sdk$only_ib" ] && download_sdk $target && download_ib $target
}

[ "$download_feeds" == "1" ] && {
    download_feeds
}

765
[ "$update_communities" == "1" ] && update_communities
p4u's avatar
p4u committed
766

767
768
769
770
771
[ "$build_sdk" == "1" ] && build_sdk $target

[ "$cook" == "1" ] && {
    [ -n "$community" ] && [ -z "$community_profile" ] && {
        echo "-> If community is defined you must also specify its profile: --community=name/profile"
772
        exit 1
773
774
775
776
777
    } || [ -n "$community" ] && {
        download_communities
        community_files="$PWD/$communities_dir/$community/$community_profile"
        [ ! -d "$community_files" ] && {
            echo "-> The community and profile specified does not exist ($community_files)"
778
779
            exit 1
        } || echo "-> Using community files from $community_files"
780
781
782
783
784
785
786
787
788
789
790
    }
    if [ "$profile" == "Default" ]; then
      cook_all_profiles $target
    else
      cook $target $profile
      RESULT=$?
    fi
}

[ "$build_all" == "1" ] && build_all_sdk
[ "$cook_all" == "1" ] && cook_all_targets
791
[ "$qmp_release" == "1" ] && make_qmp_release
792
793

exit $RESULT