build.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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="v2018.1.4"
  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="${CODE_DIR}/packages/ffho/ffho-site-generate/scripts/gen-site-conf.lua"
  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 "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH}
  55. [ "${BROKEN}" == "1" ] && export BROKEN || unset BROKEN
  56. [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  57. [ -n "${CLEAN}" ] || CLEAN="none"
  58. [ -n "${BASE}" ] || BASE=${DEFAULT_BASE}
  59. [ -n "${BRANCH}" ] || BRANCH=${DEFAULT_BRANCH}
  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. ### DIRCLEAN
  138. if [ -d "${GLUON_BUILD_DIR}/" -a "${CLEAN}" == "dirclean" ]; then
  139. progress "Cleaning your build environment (make dirclean) ..."
  140. make dirclean
  141. fi
  142. ### PREPARE
  143. progress "Preparing the build environment (make update) ..."
  144. make update
  145. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  146. ### set reasonable defaults for TARGETS and VERSION if unset
  147. if [ -z "${TARGETS}" ]; then
  148. TARGETS=$(make list-targets | sed ':a;N;$!ba;s/\n/ /g')
  149. info "building all targets: '${TARGETS}'"
  150. fi
  151. if [ -z "${VERSION}" ] ; then
  152. VERSION=$(make show-release)
  153. info "${BRANCH} firmware: using version tag '${VERSION}'"
  154. fi
  155. popd > /dev/null #${CODE_DIR}
  156. # we are now ready to produce the firmware images, so let's "save" our state
  157. BUILD_INFO="${VERSIONS_DIR}/${VERSION}"
  158. progress "Saving build information to: ${BUILD_INFO}"
  159. mkdir -p ${VERSIONS_DIR}
  160. [ "$?" -eq "0" ] || abort "Unable to create output directory: ${VERSIONS_DIR}"
  161. touch ${BUILD_INFO}
  162. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${BUILD_INFO}"
  163. echo "VERSION='${VERSION}'" > ${BUILD_INFO}
  164. echo "GLUON='${GLUON_COMMIT}' # ${BASE}" >> ${BUILD_INFO}
  165. echo "BRANCH='${BRANCH}'" >> ${BUILD_INFO}
  166. echo "SITE='${SITE_COMMIT}' # ${SITE}" >> ${BUILD_INFO}
  167. echo "TARGETS='${TARGETS}'" >> ${BUILD_INFO}
  168. echo "TS='${BUILD_TS}'" >> ${BUILD_INFO}
  169. ### restore opkg-keys
  170. if [ -e "${KEY_DIR}" ]; then
  171. info "build key already exists, restoring it."
  172. mkdir -p ${GLUON_BUILD_DIR}/
  173. [ "$?" -eq "0" ] || abort "Unable to create directory: ${GLUON_BUILD_DIR}/"
  174. cp -f ${KEY_DIR}/* ${GLUON_BUILD_DIR}/
  175. [ "$?" -eq "0" ] || abort "Unable to copy build key."
  176. fi
  177. ### create site.conf
  178. if [ -x "${SITE_GEN_SCRIPT}" ]; then
  179. progress "Generating defaut site.conf"
  180. eval GLUON_SITEDIR=${SITE_DIR} ${SITE_GEN_SCRIPT}
  181. fi
  182. ### BUILD FIRMWARE
  183. progress "Building the firmware - please stand by!"
  184. pushd ${CODE_DIR} > /dev/null
  185. export GLUON_RELEASE="${VERSION}"
  186. [ "${AUTOUPDATER}" != "off" ] && export GLUON_BRANCH="${AUTOUPDATER}"
  187. for target in ${TARGETS} ; do
  188. # configure build environment for our current target
  189. export GLUON_TARGET="${target}"
  190. # prepare build environment for our current target
  191. if [ "${CLEAN}" == "clean" ]; then
  192. progress "${target}: Preparing build environment. (make clean)"
  193. make clean
  194. [ "$?" -eq "0" ] || abort "${target}: Unable to clean environment."
  195. fi
  196. # now we can start building the images for the target platform
  197. progress "${target}: Building FFHO-flavoured Gluon firmware. You'd better go and fetch some c0ffee!"
  198. make -j ${MAKEJOBS} ${MAKE_PARAM}
  199. [ "$?" -eq "0" ] || abort "${target}: Unable to build firmware."
  200. done
  201. # generate manifest
  202. progress "Generating manifest ..."
  203. GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  204. [ "$?" -eq "0" ] || abort "Failed to generate the manifest."
  205. popd > /dev/null #${CODE_DIR}
  206. if [ "${UPLOAD}" -eq "1" ]; then
  207. # copying firmware to the server
  208. if [ -d "${IMAGE_DIR}" ]; then
  209. progress "Copying firmware images ..."
  210. rsync -rlutzc --filter="+ */" --filter="+ *-${VERSION}-*" --filter="- *" -e ssh ${IMAGE_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}
  211. [ "$?" -eq "0" ] || abort "Failed to copy firmware images."
  212. fi
  213. # copying packages to the server
  214. PKG_SUB_DIR=$(find ${PACKAGES_DIR}/ -maxdepth 1 -name *${VERSION})
  215. if [ -d "${PKG_SUB_DIR}" ]; then
  216. progress "Copying packages ..."
  217. rsync -rlutzc -e ssh ${PKG_SUB_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}/packages
  218. [ "$?" -eq "0" ] || abort "Failed to copy packages."
  219. fi
  220. fi
  221. if ["${UPDATEGIT}" -eq "1"]; then
  222. # copy manifest and the build info file
  223. progress "Copying manifest and build info file ..."
  224. pushd ${OUTPUT_DIR} > /dev/null
  225. git fetch
  226. git checkout -q signing
  227. git merge --ff-only origin/master
  228. mkdir -p "${OUTPUT_DIR}/${VERSION}/sysupgrade"
  229. cp -f "${MANIFEST_DIR}/${BRANCH}.manifest" "${OUTPUT_DIR}/${VERSION}/sysupgrade/"
  230. cp -f "${BUILD_INFO}" "${OUTPUT_DIR}/${VERSION}/${BUILD_INFO_FILENAME}"
  231. popd > /dev/null #${OUTPUT_DIR}
  232. # Update symlinks
  233. progress "Update symlink for ${BRANCH}"
  234. pushd ${OUTPUT_DIR} > /dev/null
  235. ln -fsn ${VERSION} ${BRANCH}
  236. popd > /dev/null #${OUTPUT_DIR}
  237. fi
  238. # The end. Finally.
  239. success "We're done, go and enjoy your new firmware (${VERSION})!"
  240. popd > /dev/null #${MY_DIR}