2012-09-29 18:36:38 +02:00
#!/bin/bash
2014-11-10 20:37:21 +01:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2012-09-29 18:36:38 +02:00
builddir=./build
2018-10-17 18:36:09 +02:00
OPENWRTREV="70255e3d624cd393612069aae0a859d1acbbeeae" # openwrt-18.06.1
2018-01-03 19:46:55 +01:00
OPENWRTURL="https://git.openwrt.org/openwrt/openwrt.git"
2018-10-17 18:36:09 +02:00
PACKAGEREV="35e0b737ab496f5b51e80079b0d8c9b442e223f5" # openwrt-18.06.1
2018-01-03 19:46:55 +01:00
PACKAGEURL="https://git.openwrt.org/feed/packages.git"
2013-12-01 14:15:14 +01:00
2015-10-11 20:37:08 +02:00
## Feed definition [0]: name aka directory, [1]: url, [2]: revision, [3..n]: patches
2014-11-28 19:10:55 +01:00
#official openwrt packages
OPENWRT=(openwrt
2015-06-28 14:35:15 +02:00
$PACKAGEURL
2017-11-14 01:14:50 +01:00
$PACKAGEREV
fastd/0020-fastd_generate_key_from_urandom.patch)
2015-12-13 22:16:09 +01:00
OPENWRT_PKGS="gpioctl-sysfs libugpio fastd haserl"
2014-11-28 19:10:55 +01:00
2017-10-05 15:37:41 +02:00
## Be careful: FFF uses COMPAT_VERSION 15 as default at the moment.
2015-10-13 21:57:34 +02:00
## See http://www.open-mesh.org/projects/batman-adv/wiki/Compatversion
2016-05-03 23:20:50 +02:00
GLUON=(gluon
2015-10-25 16:26:23 +01:00
https://github.com/freifunk-gluon/packages.git
2017-09-17 22:05:12 +02:00
8b65619f59c3bdce743c2f2fb2588fdd7079355a
"0001-simple-tc-Use-hotplug.d-iface-instead-of-hotplug.d-n.patch")
2016-05-03 23:20:50 +02:00
GLUON_PKGS="kmod-batman-adv-legacy micrond simple-tc uradvd"
2015-10-13 21:57:34 +02:00
#official openwrt routing packages
2014-11-28 19:10:55 +01:00
ROUTING=(routing
2018-01-03 19:46:55 +01:00
https://git.openwrt.org/feed/routing.git
2018-10-17 18:36:09 +02:00
1b9d1c419f0ecefda51922a7845ab2183d6acd76
"0002-Add-batman-adv-patch-to-remove-gw-mode-switch-messag.patch") # openwrt-18.06 from 2018-06-13
2019-01-28 11:07:24 +01:00
ROUTING_PKGS="kmod-batman-adv batctl alfred babeld"
2014-11-28 19:10:55 +01:00
2015-12-06 13:42:19 +01:00
FFF=(fff)
FFF_PKGS="-a"
2016-10-31 19:21:20 +01:00
FEEDS=(OPENWRT ROUTING GLUON FFF)
2013-12-01 14:15:14 +01:00
2017-01-16 21:50:55 +01:00
# Disable dnsmasq and other useless stuff
export DEVICE_TYPE=
2014-11-23 23:39:27 +01:00
checkout_git(){
2015-07-17 18:47:16 +02:00
local DIRECTORY=$1
local REPO_URL=$2
local COMMITID=$3
local MYGIT="git -C $DIRECTORY"
echo "checking out $REPO_URL to $DIRECTORY in version $COMMITID"
if [ -d "$DIRECTORY" ]; then
if $MYGIT remote -v | grep -q "$REPO_URL" ; then
echo "Right remote detected"
if ! $MYGIT checkout "$COMMITID" ; then
echo "commitid not found trying to fetch new commits"
$MYGIT pull && $MYGIT checkout "$COMMITID"
fi
else
echo "wrong remote or not an git repo at all -> deleting whole directory"
/bin/rm -rf "$DIRECTORY"
#needs to be without -C!!!
git clone "$REPO_URL" "$DIRECTORY"
$MYGIT checkout "$COMMITID"
fi
else
echo "We need to do a fresh checkout"
#needs to be without -C!!!
git clone "$REPO_URL" "$DIRECTORY"
$MYGIT checkout "$COMMITID"
fi
2014-11-23 23:39:27 +01:00
}
get_source() {
2015-07-17 18:47:16 +02:00
test -d src || mkdir src
cd src
#Get the OpenWrt Core Source for Firmware
checkout_git openwrt $OPENWRTURL $OPENWRTREV
test -d packages || mkdir packages
cd packages
#checkout needed package feeds
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
local URL=${FEED[1]}
local REV=${FEED[2]}
2015-12-06 13:42:17 +01:00
if [ -n "$URL" ] && [ -n "$REV" ]; then
checkout_git "$NAME" "$URL" "$REV"
# Patches for feeds could be stored in known directories like build_patches/$NAME/
# That way multiple patches for one feed could be supported
count=3
while [ "x${FEED[count]}" != "x" ]
do
2017-09-17 22:05:11 +02:00
local PATCH="../../../build_patches/${NAME}/${FEED[count]}"
2015-12-06 13:42:17 +01:00
if [ ! -z "$PATCH" ] ; then
echo "Patching $PATCH"
git -C "$NAME" am --whitespace=nowarn "$PATCH"
else
echo "Warning, $PATCH not found."
fi
count=$(( count + 1 ))
done
fi
2015-07-17 18:47:16 +02:00
done
cd .. # packages
cd .. # src
2013-12-01 14:15:14 +01:00
}
2016-03-19 11:56:40 +01:00
patch_target() {
2016-09-10 22:05:55 +02:00
if [ "$(find "$PWD"/build_patches/openwrt/*.patch 2> /dev/null | wc -l)" -ge 1 ]; then
for patch in "$PWD"/build_patches/openwrt/*.patch; do
2017-07-08 13:00:08 +02:00
patch --no-backup-if-mismatch -p1 -d "$target" -i "$patch"
2016-09-10 22:05:55 +02:00
done
fi
2016-03-19 11:56:40 +01:00
}
2012-09-29 18:36:38 +02:00
prepare() {
2015-07-17 18:47:16 +02:00
get_source
test -d $builddir || mkdir $builddir
/bin/rm -rf "$target"
cp -a src/openwrt "$target"
2017-07-08 13:00:07 +02:00
patch_target
2015-07-17 18:47:16 +02:00
## generate own feeds.conf
#this local variable should be globally configure variable used in get_source and here
local PACKAGEBASE=${PWD}/src/packages
rm -f "$target"/feeds.conf
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
echo "adding $NAME to package feeds"
echo src-link "$NAME" "$PACKAGEBASE"/"$NAME" >> "$target"/feeds.conf
done
echo "cleaning feeds"
/bin/rm -rf "$target"/feeds
"$target"/scripts/feeds update
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
local PACKAGESVAR="${FEEDVAR}_PKGS"
PACKAGESVAR="${PACKAGESVAR}[@]"
PACKAGESVAR=(${!PACKAGESVAR})
if [[ -n "${PACKAGESVAR[@]}" ]] ; then
echo "adding ${PACKAGESVAR[*]} from feed $NAME to available packages"
"$target"/scripts/feeds install -p "$NAME" "${PACKAGESVAR[@]}"
fi
done
#saves ~200MB for each build
test -d ./src/dl || mkdir ./src/dl
ln -s ../../src/dl "$target"/dl
2012-09-29 18:36:38 +02:00
}
prebuild() {
2015-07-17 18:47:16 +02:00
#create filesdir for our config
/bin/rm -rf "$target"/files
mkdir "$target"/files
cp -r ./bsp/default/root_file_system/* "$target"/files/
cp ./bsp/"$machine"/.config "$target"/.config
2016-01-04 16:36:59 +01:00
while IFS= read -r -d '' template
2015-07-17 18:47:16 +02:00
do
echo "Translating $template .."
$tpl_translate "$template" > "$(dirname "$template")"/"$(basename "$template" .tpl)"
/bin/rm "$template"
2016-01-04 16:36:59 +01:00
done < <(find "${target}/files" -name '*.tpl' -print0)
2015-07-17 18:47:16 +02:00
#insert actual firware version informations into release file
version=$(git describe --tags --dirty)
2016-09-24 15:28:27 +02:00
if [ 0 -ne $? ]; then
version=$(git log -1 --pretty=format:%h)
fi
2015-07-17 18:47:16 +02:00
{
echo "FIRMWARE_VERSION=\"$version\""
echo "BUILD_DATE=\"build date: $(date)\""
echo "OPENWRT_CORE_REVISION=\"${OPENWRTREV}\""
echo "OPENWRT_FEEDS_PACKAGES_REVISION=\"${PACKAGEREV}\""
} > "$target"/files/etc/firmware_release
2015-12-06 13:42:20 +01:00
opath=$(pwd)
cd "$target"
make defconfig
cd "$opath"
2012-09-29 18:36:38 +02:00
}
build() {
2015-07-17 18:47:16 +02:00
prebuild
opath=$(pwd)
cd "$target"
cpus=$(grep -c processor /proc/cpuinfo)
case "$1" in
"debug")
make V=99
;;
"fast")
ionice -c 2 -- nice -n 1 -- make -j $((cpus*2))
;;
*)
ionice -c 3 -- nice -n 10 -- make -j $((cpus+1))
;;
esac
cd "$opath"
if [ ! -d bin ]; then
mkdir bin
fi
2016-04-05 22:00:32 +02:00
cp_firmware
2012-09-29 18:36:38 +02:00
}
2013-12-01 14:15:16 +01:00
config() {
2015-07-17 18:47:16 +02:00
prebuild
opath=$(pwd)
cd "$target"
case "$1" in
"openwrt")
make menuconfig
;;
esac
save=""
until [ "$save" = "y" -o "$save" = "n" ]; do
echo "Do you want to save the generated config? (y/n)"
read save
done
if [ "$save" = "y" ]; then
case "$1" in
"openwrt")
2015-12-06 14:08:51 +01:00
echo "# Generated using \"./buildscript config openwrt\"." > "$opath"/bsp/"$machine"/.config
echo "# Do no edit manually" >> "$opath"/bsp/"$machine"/.config
echo "#" >> "$opath"/bsp/"$machine"/.config
./scripts/diffconfig.sh >> "$opath"/bsp/"$machine"/.config
2015-07-17 18:47:16 +02:00
;;
esac
fi
2015-12-06 14:08:51 +01:00
cd "$opath"
2013-12-01 14:15:16 +01:00
}
2016-04-05 22:00:32 +02:00
cp_firmware() {
2017-10-23 23:44:10 +02:00
[ -n "$subtarget" ] || subtarget="generic"
2016-04-05 22:00:32 +02:00
for image in ${images[@]}; do
2018-10-17 18:36:10 +02:00
filename_build=${image//openwrt/fff-${version}}
2016-07-18 19:27:34 +02:00
filename_build=${filename_build//generic/g}
2018-10-17 18:36:10 +02:00
filename_build=${filename_build//tiny/t}
2017-10-23 23:44:10 +02:00
cp "$target/bin/targets/${chipset}/${subtarget}/$image" "./bin/$filename_build"
2016-04-05 22:00:32 +02:00
2017-01-06 14:15:56 +01:00
for region in "" "-eu" "-us"; do
image_factory=${image/sysupgrade/factory$region}
2017-10-23 23:44:10 +02:00
if [[ -f "$target/bin/targets/${chipset}/${subtarget}/$image_factory" ]]; then
2017-01-06 14:15:56 +01:00
filename_build_factory=${filename_build/sysupgrade/factory$region}
if [ ${#image_factory} -lt ${#filename_build_factory} ]; then
2017-10-23 14:14:14 +02:00
echo "Warning: The factory image file name (${filename_build_factory}) is longer than the OpenWrt one, which might lead to incompatibility with the stock firmware."
2017-01-06 14:15:56 +01:00
fi
2017-10-23 23:44:10 +02:00
cp "$target/bin/targets/${chipset}/${subtarget}/$image_factory" "./bin/$filename_build_factory"
2017-01-06 14:15:56 +01:00
fi
done
2016-04-05 22:00:32 +02:00
done
2012-09-29 18:36:38 +02:00
}
2016-04-05 22:00:34 +02:00
buildrelease() {
if [ "$1" = "all" ];then
2016-05-14 16:36:28 +02:00
all=true
2016-04-05 22:00:34 +02:00
elif [ "$1" = "fast" ];then
2016-05-14 16:36:28 +02:00
fast=fast
fi
if [ "$2" = "all" ];then
all=true
elif [ "$2" = "fast" ];then
fast=fast
fi
if [ $all ]; then
buildall $fast
2016-04-05 22:00:34 +02:00
else
2016-05-14 16:36:28 +02:00
build $fast
2016-04-05 22:00:34 +02:00
fi
2016-05-14 16:36:28 +02:00
2016-04-05 22:00:34 +02:00
cd bin
for binary in *.bin; do
md5sum "$binary" > ./"$binary".md5
sha256sum "$binary" > ./"$binary".sha256
done
cd ..
echo -e "VERSION:$version" > ./bin/release.nfo
}
2012-09-29 18:36:38 +02:00
clean() {
2015-12-06 13:42:16 +01:00
/bin/rm -rf bin $builddir src/openwrt
# remove downloaded package feeds
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
2015-12-06 13:42:17 +01:00
local URL=${FEED[1]}
local REV=${FEED[2]}
2015-12-06 13:42:16 +01:00
2015-12-06 13:42:17 +01:00
if [ -n "$URL" ] && [ -n "$REV" ]; then
/bin/rm -rf src/packages/"$NAME"
fi
2015-12-06 13:42:16 +01:00
done
2012-09-29 18:36:38 +02:00
}
2016-05-14 16:36:26 +02:00
loadBSP()
{
echo "Working with $(/bin/ls -l selected_bsp | awk '{ print $11 }')"
. selected_bsp
}
setBSP()
{
/bin/rm -rf selected_bsp
/bin/ln -s "$1" selected_bsp
loadBSP
}
2013-09-19 20:40:28 +02:00
buildall() {
2015-07-17 18:47:16 +02:00
for bsp in $(/bin/ls bsp/*.bsp); do
2016-05-14 16:36:27 +02:00
setBSP "$bsp"
prepare
build "$1"
2015-07-17 18:47:16 +02:00
done
2013-09-19 20:40:28 +02:00
}
2017-08-19 16:07:26 +02:00
if [ "$(/usr/bin/id -u)" -eq 0 ]; then
echo "don't run buildscript as root"
exit 1
fi
2018-03-03 18:29:43 +01:00
if [ "$1" != "selectbsp" ]; then
2015-07-17 18:47:16 +02:00
if [ ! -h selected_bsp ]; then
echo "Please select a Board-Support-Package using:"
echo "$0 selectbsp"
exit
fi
2016-05-14 16:36:26 +02:00
loadBSP
2015-07-17 18:47:16 +02:00
echo
2012-09-29 18:36:38 +02:00
fi
case "$1" in
2015-07-17 18:47:16 +02:00
"selectbsp")
if [ "$2" = "help" ] || [ "$2" = "" ]; then
echo "Select a Board-Support-Package:"
echo
2016-01-28 00:30:56 +01:00
echo "Usage: $0 $1 <bsp-file>"
2015-07-17 18:47:16 +02:00
echo "available packages:"
/bin/ls bsp/*.bsp
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
else
if [ ! -f "$2" ]; then
echo "Could not find $2"
else
2016-05-14 16:36:26 +02:00
setBSP "$2"
2015-07-17 18:47:16 +02:00
fi
fi
;;
"prepare")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option fetches the sources for the images and configurates the build so that it can be compiled"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
echo "Usage: $0 $1"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
else
prepare
fi
;;
"build")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option compiles the firmware"
echo "Normaly the build uses lower IO and System priorities, "
echo "you can append \"fast\" option, to use normal user priorities"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
echo "Usage: $0 $1 [fast|debug]"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
else
build "$2"
fi
;;
"config")
case "$2" in
openwrt)
config openwrt
;;
*)
2016-08-11 22:02:03 +02:00
echo "This open the OpenWrt menuconfig dialog"
2015-07-17 18:47:16 +02:00
echo
2015-12-06 13:42:22 +01:00
echo "Usage: $0 $1 openwrt"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
;;
esac
;;
"clean")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option cleans all build files."
2016-01-28 00:30:56 +01:00
echo
echo "Usage: $0 $1"
echo
2015-07-17 18:47:16 +02:00
else
clean
fi
;;
"buildall")
2016-05-14 16:36:25 +02:00
if [ "$2" = "help" ]; then
2018-03-03 18:29:43 +01:00
echo "This option builds the firmware for all routers."
2016-01-28 00:30:56 +01:00
echo
2016-05-14 16:36:25 +02:00
echo "Usage: $0 $1 [fast]"
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
else
2016-05-14 16:36:25 +02:00
buildall "$2"
2015-07-17 18:47:16 +02:00
fi
;;
2016-04-05 22:00:34 +02:00
"release")
if [ "$2" = help ]; then
echo "This option builds the firmware for a given board. It also creates hash sums and a the file release.nfo"
echo
echo "Usage: $0 $1 [all] [fast]"
echo
else
2016-05-14 16:36:28 +02:00
buildrelease "$2" "$3"
2016-04-05 22:00:34 +02:00
fi
;;
2015-07-17 18:47:16 +02:00
*)
echo "This is the Build Environment Script of the Freifunk Community Franken."
2016-01-28 00:30:56 +01:00
echo
2015-07-17 18:47:16 +02:00
echo "Usage: $0 command"
echo "command:"
2016-01-28 00:30:56 +01:00
echo " selectbsp <bsp-file>"
2015-07-17 18:47:16 +02:00
echo " prepare"
2016-01-28 00:30:56 +01:00
echo " config openwrt"
echo " build [fast|debug]"
2016-05-14 16:36:25 +02:00
echo " buildall [fast]"
2016-04-05 22:00:34 +02:00
echo " release [all] [fast]"
2015-07-17 18:47:16 +02:00
echo " clean"
echo ""
2016-01-28 00:30:56 +01:00
echo "If you need help to one of these options just type: $0 <command> help"
echo
2015-07-17 18:47:16 +02:00
;;
2012-09-29 18:36:38 +02:00
esac