build.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/bin/bash
  2. # (c) 2014-2018 Freifunk Hochstift <kontakt@hochstift.freifunk.net>
  3. #
  4. # This script builds the firmware by the environment variables given, the
  5. # first two being mandatory:
  6. #
  7. # BASE = Gluon Version (branch, tag or commit, i.e. v2014.4)
  8. # BRANCH = Firmware Branch (stable/testing/experimental)
  9. # VERSION = the version tag (can only be empty if BRANCH=experimental)
  10. #
  11. # optional:
  12. # AUTOUPDATER = force Autoupdater Branch (stable/testing/experimental/off)
  13. # BROKEN = 0 (default) or 1, build the untested hardware model firmwares, too
  14. # BUILD_TS = build timestamp (format: %Y-%m-%d %H:%M:%S)
  15. # CLEAN = 'dirclean' to perform "make dirclean" before build or 'clean' to perform "make clean" (default: 'none')
  16. # DEVICES = build some specific devices and not the entire target
  17. # KEY_DIR = specify directory for gluon-opkg-key
  18. # MAKEJOBS = number of compiler processes running in parallel (default: number of CPUs/Cores)
  19. # PRIORITY = determines the number of day a rollout phase should last at most
  20. # UPLOAD = 0 (default) or 1, upload firmware at the end
  21. # UPDATEGIT = 0 (default) or 1, update firmware website git at the end
  22. # SITE = site repository version (branch, tag or commit, default: '$BRANCH')
  23. # TARGETS = a space separated list of target platforms (if unset, all platforms will be build)
  24. # VERBOSE = 0 (default) or 1, call the make commands with 'V=s' to see actual errors better
  25. # VERSIONS_DIR = specify directory for version files
  26. #
  27. ### includes
  28. . functions.sh
  29. ### static variables
  30. DEFAULT_BASE="v2021.1.2"
  31. DEFAULT_BRANCH="experimental"
  32. MY_DIR=$(dirname $0)
  33. MY_DIR=$(readlink -f "${MY_DIR}")
  34. DEFAULT_KEY_DIR="${MY_DIR}/opkg-keys"
  35. DEFAULT_VERSIONS_DIR="${MY_DIR}/versions"
  36. CODE_DIR="${MY_DIR}/src"
  37. GLUON_BUILD_DIR="${CODE_DIR}/lede"
  38. SITE_DIR="${CODE_DIR}/site"
  39. PATCH_DIR="${SITE_DIR}/patches"
  40. OUTPUT_DIR="${MY_DIR}/output"
  41. IMAGE_DIR="${CODE_DIR}/output/images"
  42. PACKAGES_DIR="${CODE_DIR}/output/packages"
  43. MANIFEST_DIR="${CODE_DIR}/output/images/sysupgrade"
  44. SITE_GEN_SCRIPT="${SITE_DIR}/scripts/gensites.py"
  45. SRV_URL="firmware.in.ffho.net"
  46. SRV_USER="firmware"
  47. SRV_PATH="/srv/firmware"
  48. BUILD_INFO_FILENAME="build-info.txt"
  49. SITE_REPO_URL="https://git.ffho.net/freifunkhochstift/ffho-site.git"
  50. GLUON_REPO_URL="https://github.com/freifunk-gluon/gluon.git"
  51. LANG=C
  52. pushd ${MY_DIR} > /dev/null
  53. ### set reasonable defaults for unset environment variables
  54. [ -n "${BRANCH}" ] || BRANCH=${DEFAULT_BRANCH}
  55. [ -n "${BASE}" ] || BASE=${DEFAULT_BASE}
  56. [ -n "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH}
  57. [ "${BROKEN}" == "1" ] && export BROKEN || unset BROKEN
  58. [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  59. [ -n "${CLEAN}" ] || CLEAN="none"
  60. if [ -n "${KEY_DIR}" ]; then
  61. KEY_DIR=$(readlink -f "${KEY_DIR}")
  62. [ -e "${KEY_DIR}" ] || abort "Can not find specified key directory: ${KEY_DIR}"
  63. else
  64. KEY_DIR="${DEFAULT_KEY_DIR}"
  65. fi
  66. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  67. [ -n "${PRIORITY}" ] || PRIORITY=0
  68. [ -n "${UPLOAD}" ] || UPLOAD=0
  69. [ -n "${UPDATEGIT}" ] || UPDATEGIT=0
  70. [ -n "${SITE}" ] || SITE=${BRANCH}
  71. [ -n "${VERBOSE}" ] || VERBOSE=0
  72. if [ -n "${VERSIONS_DIR}" ]; then
  73. VERSIONS_DIR=$(readlink -f "${VERSIONS_DIR}")
  74. else
  75. VERSIONS_DIR="${DEFAULT_VERSIONS_DIR}"
  76. fi
  77. MAKE_PARAM=""
  78. [ "${VERBOSE}" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  79. [ -n "${DEVICE}" ] && MAKE_PARAM="${MAKE_PARAM} DEVICE=${DEVICE}"
  80. ### ERROR handling
  81. [ -n "${BASE}" ] || abort "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)."
  82. [ "${BASE}" == "HEAD" ] && abort "HEAD is not an allowed BASE-identifier. Either use a branch, a tagged commit or the commit-SHA itself."
  83. [ -n "${BRANCH}" ] || abort "Please specify BRANCH environment variable."
  84. [ -n "${VERSION}" -o "${BRANCH}" == "experimental" ] || abort "Please specify VERSION environment variable (not necessary for experimental branch)."
  85. [ "${BRANCH}" == "experimental" -o ! -r "${VERSIONS_DIR}/${VERSION}" ] || abort "There exists a version file for '${VERSION}' ... you are trying to do something really stupid, aren't you?"
  86. ### init VERSIONS_DIR if necessary
  87. if [ ! -d "${VERSIONS_DIR}" ]; then
  88. info "Versions directory does not exist yet - creating it ..."
  89. mkdir -p ${VERSIONS_DIR}
  90. [ "$?" -eq "0" ] || abort "Unable to create versions directory: ${VERSIONS_DIR}"
  91. fi
  92. ### init CODE_DIR if necessary
  93. if [ ! -d "${CODE_DIR}" ]; then
  94. info "Code directory does not exist yet - fetching Gluon ..."
  95. git clone "${GLUON_REPO_URL}" "${CODE_DIR}"
  96. [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository."
  97. fi
  98. ### init SITE_DIR if necessary
  99. if [ ! -d "${SITE_DIR}" ]; then
  100. info "Site repository does not exist, fetching it ..."
  101. git clone "${SITE_REPO_URL}" "${SITE_DIR}"
  102. [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository."
  103. fi
  104. ### CHECKOUT GLUON
  105. progress "Checking out GLUON '${BASE}' ..."
  106. pushd ${CODE_DIR} > /dev/null
  107. # check if gluon got modified and bail out if necessary
  108. [ "$(git status --porcelain)" ] && abort "Local changes to gluon directory. Cowardly refusing to update repository." >&2
  109. git fetch
  110. git show-ref --verify --quiet refs/remotes/origin/${BASE}
  111. if [ "$?" -eq "0" ]; then
  112. git checkout -B build origin/${BASE}
  113. [ "$?" -eq "0" ] || abort "Failed to checkout gluon origin/${BASE}."
  114. else
  115. git checkout -q ${BASE}
  116. [ "$?" -eq "0" ] || abort "Failed to checkout gluon ${BASE}." >&2
  117. fi
  118. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  119. popd > /dev/null #${CODE_DIR}
  120. ### CHECKOUT SITE REPO
  121. progress "Checking out SITE REPO ..."
  122. pushd ${SITE_DIR} > /dev/null
  123. # check if site repo got modified and bail out if necessary
  124. [ "$(git status --porcelain)" ] && abort "Local changes to site directory. Cowardly refusing to update repository." >&2
  125. git fetch
  126. git show-ref --verify --quiet refs/remotes/origin/${SITE}
  127. if [ "$?" -eq "0" ]; then
  128. git checkout -B build origin/${SITE}
  129. [ "$?" -eq "0" ] || abort "Failed to checkout site repo 'origin/${SITE}'."
  130. else
  131. git checkout -q ${SITE}
  132. [ "$?" -eq "0" ] || abort "Failed to checkout site repo '${SITE}'." >&2
  133. fi
  134. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  135. popd > /dev/null #${SITE_DIR}
  136. pushd ${CODE_DIR} > /dev/null
  137. ### APPLY PATCHES TO GLUON
  138. progress "Applying Patches ..."
  139. git checkout -B patching "${BASE}"
  140. if [ -d "${PATCH_DIR}" -a "$(echo ${PATCH_DIR}/*.patch)" ]; then
  141. git am --whitespace=nowarn ${PATCH_DIR}/*.patch || (
  142. git am --abort
  143. git checkout patched
  144. git branch -D patching
  145. false
  146. )
  147. [ "$?" -eq "0" ] || abort "Failed to apply patches, mimimi."
  148. fi
  149. git branch -M patched
  150. ### DIRCLEAN
  151. if [ -d "${GLUON_BUILD_DIR}/" -a "${CLEAN}" == "dirclean" ]; then
  152. progress "Cleaning your build environment (make dirclean) ..."
  153. make dirclean
  154. fi
  155. ### PREPARE
  156. progress "Preparing the build environment (make update) ..."
  157. make update
  158. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  159. ### set reasonable defaults for TARGETS and VERSION if unset
  160. if [ -z "${TARGETS}" ]; then
  161. TARGETS=$(make list-targets | sed ':a;N;$!ba;s/\n/ /g')
  162. info "building all targets: '${TARGETS}'"
  163. fi
  164. if [ -z "${VERSION}" ] ; then
  165. VERSION=$(make show-release)
  166. info "${BRANCH} firmware: using version tag '${VERSION}'"
  167. fi
  168. popd > /dev/null #${CODE_DIR}
  169. # we are now ready to produce the firmware images, so let's "save" our state
  170. BUILD_INFO="${VERSIONS_DIR}/${VERSION}"
  171. progress "Saving build information to: ${BUILD_INFO}"
  172. mkdir -p ${VERSIONS_DIR}
  173. [ "$?" -eq "0" ] || abort "Unable to create output directory: ${VERSIONS_DIR}"
  174. touch ${BUILD_INFO}
  175. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${BUILD_INFO}"
  176. echo "VERSION='${VERSION}'" > ${BUILD_INFO}
  177. echo "GLUON='${GLUON_COMMIT}' # ${BASE}" >> ${BUILD_INFO}
  178. echo "BRANCH='${BRANCH}'" >> ${BUILD_INFO}
  179. echo "SITE='${SITE_COMMIT}' # ${SITE}" >> ${BUILD_INFO}
  180. echo "TARGETS='${TARGETS}'" >> ${BUILD_INFO}
  181. echo "TS='${BUILD_TS}'" >> ${BUILD_INFO}
  182. ### restore opkg-keys
  183. if [ -e "${KEY_DIR}" ]; then
  184. info "build key already exists, restoring it."
  185. mkdir -p ${GLUON_BUILD_DIR}/
  186. [ "$?" -eq "0" ] || abort "Unable to create directory: ${GLUON_BUILD_DIR}/"
  187. cp -f ${KEY_DIR}/* ${GLUON_BUILD_DIR}/
  188. [ "$?" -eq "0" ] || abort "Unable to copy build key."
  189. fi
  190. ### create site.conf
  191. if [ -x "${SITE_GEN_SCRIPT}" ]; then
  192. progress "Generating defaut site.conf"
  193. eval GLUON_SITEDIR=${SITE_DIR} ${SITE_GEN_SCRIPT}
  194. fi
  195. ### BUILD FIRMWARE
  196. progress "Building the firmware - please stand by!"
  197. pushd ${CODE_DIR} > /dev/null
  198. export GLUON_RELEASE="${VERSION}"
  199. if [ "${AUTOUPDATER}" != "off" ] ; then
  200. export GLUON_AUTOUPDATER_BRANCH="${AUTOUPDATER}"
  201. export GLUON_AUTOUPDATER_ENABLED=1
  202. fi
  203. for target in ${TARGETS} ; do
  204. [[ "$EXCLUDE_TARGETS" =~ "$target" ]] && continue
  205. # configure build environment for our current target
  206. export GLUON_TARGET="${target}"
  207. # prepare build environment for our current target
  208. if [ "${CLEAN}" == "clean" ]; then
  209. progress "${target}: Preparing build environment. (make clean)"
  210. make clean
  211. [ "$?" -eq "0" ] || abort "${target}: Unable to clean environment."
  212. fi
  213. # now we can start building the images for the target platform
  214. progress "${target}: Building FFHO-flavoured Gluon firmware. You'd better go and fetch some c0ffee!"
  215. make -j ${MAKEJOBS} ${MAKE_PARAM}
  216. [ "$?" -eq "0" ] || abort "${target}: Unable to build firmware."
  217. done
  218. # generate manifest
  219. progress "Generating manifest ..."
  220. GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  221. [ "$?" -eq "0" ] || abort "Failed to generate the manifest."
  222. popd > /dev/null #${CODE_DIR}
  223. if [ "${UPLOAD}" -eq "1" ]; then
  224. # copying firmware to the server
  225. if [ -d "${IMAGE_DIR}" ]; then
  226. progress "Copying firmware images ..."
  227. rsync -rlutzc --filter="+ */" --filter="+ *-${VERSION}-*" --filter="- *" -e ssh ${IMAGE_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}
  228. [ "$?" -eq "0" ] || abort "Failed to copy firmware images."
  229. fi
  230. # copying packages to the server
  231. PKG_SUB_DIR=$(find ${PACKAGES_DIR}/ -maxdepth 1 -name *${VERSION})
  232. if [ -d "${PKG_SUB_DIR}" ]; then
  233. progress "Copying packages ..."
  234. rsync -rlutzc -e ssh ${PKG_SUB_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}/packages
  235. [ "$?" -eq "0" ] || abort "Failed to copy packages."
  236. fi
  237. fi
  238. if [ "${UPDATEGIT}" -eq "1" ]; then
  239. # copy manifest and the build info file
  240. progress "Copying manifest and build info file ..."
  241. pushd ${OUTPUT_DIR} > /dev/null
  242. git fetch
  243. git checkout -q signing
  244. git merge --ff-only origin/master
  245. mkdir -p "${OUTPUT_DIR}/${VERSION}/sysupgrade"
  246. cp -f "${MANIFEST_DIR}/${BRANCH}.manifest" "${OUTPUT_DIR}/${VERSION}/sysupgrade/"
  247. cp -f "${BUILD_INFO}" "${OUTPUT_DIR}/${VERSION}/${BUILD_INFO_FILENAME}"
  248. popd > /dev/null #${OUTPUT_DIR}
  249. # Update symlinks
  250. progress "Update symlink for ${BRANCH}"
  251. pushd ${OUTPUT_DIR} > /dev/null
  252. ln -fsn ${VERSION} ${BRANCH}
  253. popd > /dev/null #${OUTPUT_DIR}
  254. fi
  255. # The end. Finally.
  256. success "We're done, go and enjoy your new firmware (${VERSION})!"
  257. popd > /dev/null #${MY_DIR}