build.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. # PUBLISH = 0 (default) or 1, publish firmware at the end
  21. # SITE = site repository version (branch, tag or commit, default: '$BRANCH')
  22. # TARGETS = a space separated list of target platforms (if unset, all platforms will be build)
  23. # VERBOSE = 0 (default) or 1, call the make commands with 'V=s' to see actual errors better
  24. # VERSIONS_DIR = specify directory for version files
  25. #
  26. ### includes
  27. . functions.sh
  28. ### static variables
  29. MY_DIR=$(dirname $0)
  30. MY_DIR=$(readlink -f "${MY_DIR}")
  31. DEFAULT_KEY_DIR="${MY_DIR}/opkg-keys"
  32. DEFAULT_VERSIONS_DIR="${MY_DIR}/versions"
  33. CODE_DIR="${MY_DIR}/src"
  34. GLUON_BUILD_DIR="${CODE_DIR}/lede"
  35. SITE_DIR="${CODE_DIR}/site"
  36. PATCH_DIR="${SITE_DIR}/patches"
  37. OUTPUT_DIR="${MY_DIR}/output"
  38. IMAGE_DIR="${CODE_DIR}/output/images"
  39. PACKAGES_DIR="${CODE_DIR}/output/packages"
  40. MANIFEST_DIR="${CODE_DIR}/output/images/sysupgrade"
  41. SITE_GEN_SCRIPT="${CODE_DIR}/packages/ffho/ffho/ffho-site-generate/scripts/gen-site-conf.lua"
  42. SRV_URL="firmware.in.ffho.net"
  43. SRV_USER="firmware"
  44. SRV_PATH="/srv/firmware"
  45. BUILD_INFO_FILENAME="build-info.txt"
  46. SITE_REPO_URL="https://git.ffho.net/freifunkhochstift/ffho-site.git"
  47. GLUON_REPO_URL="https://git.ffho.net/freifunkhochstift/gluon.git"
  48. LANG=C
  49. pushd ${MY_DIR} > /dev/null
  50. ### set reasonable defaults for unset environment variables
  51. [ -n "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH}
  52. [ "${BROKEN}" -eq "1" ] && export BROKEN || unset BROKEN
  53. [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  54. [ -n "${CLEAN}" ] || CLEAN="none"
  55. if [ -n "${KEY_DIR}" ]; then
  56. KEY_DIR=$(readlink -f "${KEY_DIR}")
  57. [ -e "${KEY_DIR}" ] || abort "Can not find specified key directory: ${KEY_DIR}"
  58. else
  59. KEY_DIR="${DEFAULT_KEY_DIR}"
  60. fi
  61. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  62. [ -n "${PRIORITY}" ] || PRIORITY=0
  63. [ -n "${PUBLISH}" ] || PUBLISH=0
  64. [ -n "${SITE}" ] || SITE=${BRANCH}
  65. [ -n "${VERBOSE}" ] || VERBOSE=0
  66. if [ -n "${VERSIONS_DIR}" ]; then
  67. VERSIONS_DIR=$(readlink -f "${VERSIONS_DIR}")
  68. else
  69. VERSIONS_DIR="${DEFAULT_VERSIONS_DIR}"
  70. fi
  71. MAKE_PARAM=""
  72. [ "${VERBOSE}" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  73. [ -n "${DEVICE}" ] && MAKE_PARAM="${MAKE_PARAM} DEVICE=${DEVICE}"
  74. ### ERROR handling
  75. [ -n "${BASE}" ] || abort "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)."
  76. [ "${BASE}" == "HEAD" ] && abort "HEAD is not an allowed BASE-identifier. Either use a branch, a tagged commit or the commit-SHA itself."
  77. [ -n "${BRANCH}" ] || abort "Please specify BRANCH environment variable."
  78. [ -n "${VERSION}" -o "${BRANCH}" == "experimental" ] || abort "Please specify VERSION environment variable (not necessary for experimental branch)."
  79. [ "${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?"
  80. ### init VERSIONS_DIR if necessary
  81. if [ ! -d "${VERSIONS_DIR}" ]; then
  82. info "Versions directory does not exist yet - creating it ..."
  83. mkdir -p ${VERSIONS_DIR}
  84. [ "$?" -eq "0" ] || abort "Unable to create versions directory: ${VERSIONS_DIR}"
  85. fi
  86. ### init CODE_DIR if necessary
  87. if [ ! -d "${CODE_DIR}" ]; then
  88. info "Code directory does not exist yet - fetching Gluon ..."
  89. git clone "${GLUON_REPO_URL}" "${CODE_DIR}"
  90. [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository."
  91. fi
  92. ### init SITE_DIR if necessary
  93. if [ ! -d "${SITE_DIR}" ]; then
  94. info "Site repository does not exist, fetching it ..."
  95. git clone "${SITE_REPO_URL}" "${SITE_DIR}"
  96. [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository."
  97. fi
  98. ### CHECKOUT GLUON
  99. progress "Checking out GLUON '${BASE}' ..."
  100. pushd ${CODE_DIR} > /dev/null
  101. # check if gluon got modified and bail out if necessary
  102. [ "$(git status --porcelain)" ] && abort "Local changes to gluon directory. Cowardly refusing to update repository." >&2
  103. git fetch
  104. git show-ref --verify --quiet refs/remotes/origin/${BASE}
  105. if [ "$?" -eq "0" ]; then
  106. git checkout -B build origin/${BASE}
  107. [ "$?" -eq "0" ] || abort "Failed to checkout gluon origin/${BASE}."
  108. else
  109. git checkout -q ${BASE}
  110. [ "$?" -eq "0" ] || abort "Failed to checkout gluon ${BASE}." >&2
  111. fi
  112. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  113. popd > /dev/null #${CODE_DIR}
  114. ### CHECKOUT SITE REPO
  115. progress "Checking out SITE REPO ..."
  116. pushd ${SITE_DIR} > /dev/null
  117. # check if site repo got modified and bail out if necessary
  118. [ "$(git status --porcelain)" ] && abort "Local changes to site directory. Cowardly refusing to update repository." >&2
  119. git fetch
  120. git show-ref --verify --quiet refs/remotes/origin/${SITE}
  121. if [ "$?" -eq "0" ]; then
  122. git checkout -B build origin/${SITE}
  123. [ "$?" -eq "0" ] || abort "Failed to checkout site repo 'origin/${SITE}'."
  124. else
  125. git checkout -q ${SITE}
  126. [ "$?" -eq "0" ] || abort "Failed to checkout site repo '${SITE}'." >&2
  127. fi
  128. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  129. popd > /dev/null #${SITE_DIR}
  130. pushd ${CODE_DIR} > /dev/null
  131. ### DIRCLEAN
  132. if [ -d "${GLUON_BUILD_DIR}/" -a "${CLEAN}" == "dirclean" ]; then
  133. progress "Cleaning your build environment (make dirclean) ..."
  134. make dirclean
  135. fi
  136. ### PREPARE
  137. progress "Preparing the build environment (make update) ..."
  138. make update
  139. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  140. ### set reasonable defaults for TARGETS and VERSION if unset
  141. if [ -z "${TARGETS}" ]; then
  142. TARGETS=$(make list-targets | sed ':a;N;$!ba;s/\n/ /g')
  143. info "building all targets: '${TARGETS}'"
  144. fi
  145. if [ -z "${VERSION}" ] ; then
  146. VERSION=$(make show-release)
  147. info "${BRANCH} firmware: using version tag '${VERSION}'"
  148. fi
  149. popd > /dev/null #${CODE_DIR}
  150. # we are now ready to produce the firmware images, so let's "save" our state
  151. BUILD_INFO="${VERSIONS_DIR}/${VERSION}"
  152. progress "Saving build information to: ${BUILD_INFO}"
  153. mkdir -p ${VERSIONS_DIR}
  154. [ "$?" -eq "0" ] || abort "Unable to create output directory: ${VERSIONS_DIR}"
  155. touch ${BUILD_INFO}
  156. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${BUILD_INFO}"
  157. echo "VERSION='${VERSION}'" > ${BUILD_INFO}
  158. echo "GLUON='${GLUON_COMMIT}' # ${BASE}" >> ${BUILD_INFO}
  159. echo "BRANCH='${BRANCH}'" >> ${BUILD_INFO}
  160. echo "SITE='${SITE_COMMIT}' # ${SITE}" >> ${BUILD_INFO}
  161. echo "TARGETS='${TARGETS}'" >> ${BUILD_INFO}
  162. echo "TS='${BUILD_TS}'" >> ${BUILD_INFO}
  163. ### restore opkg-keys
  164. if [ -e "${KEY_DIR}" ]; then
  165. info "build key already exists, restoring it."
  166. mkdir -p ${GLUON_BUILD_DIR}/
  167. [ "$?" -eq "0" ] || abort "Unable to create directory: ${GLUON_BUILD_DIR}/"
  168. cp -f ${KEY_DIR}/* ${GLUON_BUILD_DIR}/
  169. [ "$?" -eq "0" ] || abort "Unable to copy build key."
  170. fi
  171. ### create site.conf
  172. if [ -x "${SITE_GEN_SCRIPT}" ]; then
  173. progress "Generating defaut site.conf"
  174. eval GLUON_SITEDIR=${SITE_DIR} ${SITE_GEN_SCRIPT}
  175. fi
  176. ### BUILD FIRMWARE
  177. progress "Building the firmware - please stand by!"
  178. pushd ${CODE_DIR} > /dev/null
  179. export GLUON_RELEASE="${VERSION}"
  180. [ "${AUTOUPDATER}" != "off" ] && export GLUON_BRANCH="${AUTOUPDATER}"
  181. for target in ${TARGETS} ; do
  182. # configure build environment for our current target
  183. export GLUON_TARGET="${target}"
  184. # prepare build environment for our current target
  185. if [ "${CLEAN}" == "clean" ]; then
  186. progress "${target}: Preparing build environment. (make clean)"
  187. make clean
  188. [ "$?" -eq "0" ] || abort "${target}: Unable to clean environment."
  189. fi
  190. # now we can start building the images for the target platform
  191. progress "${target}: Building FFHO-flavoured Gluon firmware. You'd better go and fetch some c0ffee!"
  192. make -j ${MAKEJOBS} ${MAKE_PARAM}
  193. [ "$?" -eq "0" ] || abort "${target}: Unable to build firmware."
  194. done
  195. # generate manifest
  196. progress "Generating manifest ..."
  197. GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  198. [ "$?" -eq "0" ] || abort "Failed to generate the manifest."
  199. popd > /dev/null #${CODE_DIR}
  200. if [ "${PUBLISH}" -eq "1" ]; then
  201. # copying firmware to the server
  202. if [ -d "${IMAGE_DIR}" ]; then
  203. progress "Copying firmware images ..."
  204. rsync -rlutzc --filter="+ */" --filter="+ *-${VERSION}-*" --filter="- *" -e ssh ${IMAGE_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}
  205. [ "$?" -eq "0" ] || abort "Failed to copy firmware images."
  206. fi
  207. # copying packages to the server
  208. PKG_SUB_DIR=$(find ${PACKAGES_DIR}/ -maxdepth 1 -name *${VERSION})
  209. if [ -d "${PKG_SUB_DIR}" ]; then
  210. progress "Copying packages ..."
  211. rsync -rlutzc -e ssh ${PKG_SUB_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}/packages
  212. [ "$?" -eq "0" ] || abort "Failed to copy packages."
  213. fi
  214. # copy manifest and the build info file
  215. progress "Copying manifest and build info file ..."
  216. pushd ${OUTPUT_DIR} > /dev/null
  217. git fetch
  218. git checkout -q signing
  219. git merge --ff-only origin/master
  220. mkdir -p "${OUTPUT_DIR}/${VERSION}/sysupgrade"
  221. cp -f "${MANIFEST_DIR}/${BRANCH}.manifest" "${OUTPUT_DIR}/${VERSION}/sysupgrade/"
  222. cp -f "${BUILD_INFO}" "${OUTPUT_DIR}/${VERSION}/${BUILD_INFO_FILENAME}"
  223. popd > /dev/null #${OUTPUT_DIR}
  224. # Update symlinks
  225. progress "Update symlink for ${BRANCH}"
  226. pushd ${OUTPUT_DIR} > /dev/null
  227. ln -fsn ${VERSION} ${BRANCH}
  228. popd > /dev/null #${OUTPUT_DIR}
  229. fi
  230. # The end. Finally.
  231. success "We're done, go and enjoy your new firmware (${VERSION})!"
  232. popd > /dev/null #${MY_DIR}