build.sh 10 KB

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