build.sh 11 KB

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