build.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 perform "make dirclean" before build (BRANCH==stable/testing) or CLEAN perform "make clean" or NONE (BRANCH==experimental)
  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. # SITE_REPO_FETCH_METHOD = http, everything except "git" will use the HTTP method for fetchting site repo
  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. #
  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. CODE_DIR="${MY_DIR}/src"
  32. GLUON_BUILD_DIR="${CODE_DIR}/build"
  33. SITE_DIR="${CODE_DIR}/site"
  34. PATCH_DIR="${SITE_DIR}/patches"
  35. OUTPUT_DIR="${MY_DIR}/output"
  36. IMAGE_DIR="${CODE_DIR}/output/images"
  37. MODULE_DIR="${CODE_DIR}/output/modules"
  38. MANIFEST_DIR="${CODE_DIR}/output/images/sysupgrade"
  39. VERSIONS_INFO_DIR="${MY_DIR}/versions"
  40. SRV_URL="firmware.in.ffho.net"
  41. SRV_USER="firmware"
  42. SRV_PATH="/srv/firmware"
  43. BUILD_INFO_FILENAME="build-info.txt"
  44. SITE_REPO_URL="git.c3pb.de/freifunk-pb/site-ffho.git"
  45. LANG=C
  46. pushd ${MY_DIR} > /dev/null
  47. ### ERROR handling
  48. [ -n "${BASE}" ] || abort "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)."
  49. [ -n "${BRANCH}" ] || abort "Please specify BRANCH environment variable."
  50. [ "${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."
  51. [ -n "${VERSION}" -o "${BRANCH}" == "experimental" ] || abort "Please specify VERSION environment variable (not necessary for experimental branch)."
  52. [ "${BRANCH}" == "experimental" -o ! -r "${VERSIONS_INFO_DIR}/${VERSION}" ] || abort "There exists a version file for '${VERSION}' ... you are trying to do something really stupid, aren't you?"
  53. ### set reasonable defaults for unset environment variables
  54. [ -n "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH}
  55. if [ -n "${BROKEN}" ]; then
  56. if [ "${BROKEN}" -eq "1" ]; then
  57. export BROKEN
  58. else
  59. unset BROKEN
  60. fi
  61. fi
  62. [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  63. if [ -z "${CLEAN}" ]; then
  64. if [ "${BRANCH}" == "experimental" ]; then
  65. CLEAN="none"
  66. else
  67. CLEAN="dirclean"
  68. fi
  69. fi
  70. if [ -n "${KEY_DIR}" ]; then
  71. KEY_DIR=$(readlink -f "${KEY_DIR}")
  72. else
  73. KEY_DIR="${DEFAULT_KEY_DIR}"
  74. fi
  75. [ -e "${KEY_DIR}" ] || mkdir -p ${KEY_DIR}
  76. [ "$?" -eq "0" ] || abort "Unable to create key directory: ${KEY_DIR}"
  77. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  78. [ -n "${PRIORITY}" ] || PRIORITY=0
  79. [ -n "${PUBLISH}" ] || PUBLISH=0
  80. [ -n "${SITE_REPO_FETCH_METHOD}" ] || SITE_REPO_FETCH_METHOD="http"
  81. [ -n "${VERBOSE}" ] || VERBOSE=0
  82. if [ "${SITE_REPO_FETCH_METHOD}" != "git" ]; then
  83. SITE_REPO_URL="https://${SITE_REPO_URL}"
  84. else
  85. SITE_REPO_URL="git@${SITE_REPO_URL}"
  86. fi
  87. MAKE_PARAM=""
  88. [ "${VERBOSE}" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  89. ### INIT /src IF NECESSARY
  90. if [ ! -d "${CODE_DIR}" ]; then
  91. info "Code directory does not exist yet - fetching Gluon ..."
  92. git clone https://github.com/freifunk-gluon/gluon.git "${CODE_DIR}"
  93. [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository."
  94. fi
  95. ### INIT /src/site IF NECESSARY
  96. if [ ! -d "${SITE_DIR}" ]; then
  97. info "Site repository does not exist, fetching it ..."
  98. git clone "${SITE_REPO_URL}" "${SITE_DIR}"
  99. [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository."
  100. fi
  101. pushd ${CODE_DIR} > /dev/null
  102. ### CHECKOUT GLUON
  103. progress "Checking out GLUON '${BASE}' ..."
  104. # check if gluon got modified and bail out if necessary
  105. [ "$(git status --porcelain)" ] && abort "Local changes to peers directory. Cowardly refusing to update gluon repository." >&2
  106. git fetch
  107. git checkout -q ${BASE}
  108. [ "$?" -eq "0" ] || abort "Failed to checkout '${BASE}' gluon base version, mimimi." >&2
  109. git show-ref --verify --quiet refs/remotes/origin/${BASE}
  110. if [ "$?" -eq "0" ]; then
  111. git pull
  112. [ "$?" -eq "0" ] || abort "Failed to get newest '${BASE}' in gluon repository, mimimi."
  113. fi
  114. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  115. ### CHECKOUT SITE REPO
  116. progress "Checking out SITE REPO ..."
  117. pushd ${SITE_DIR} > /dev/null
  118. if [ $(git remote | wc -l) -ge "1" ]; then
  119. git fetch
  120. # TODO: check if site got modified locally and bail out if necessary
  121. if [ -z "${SITE_ID}" ]; then
  122. # no specific site given - get the most current one
  123. git checkout -q ${BRANCH}
  124. git branch -r | grep ${BRANCH} > /dev/null
  125. if [ "$?" -eq "0" ]; then
  126. git rebase
  127. [ "$?" -eq "0" ] || abort "Failed to get newest '${BRANCH}' in site repository, mimimi."
  128. fi
  129. else
  130. # fetch site repo updates
  131. git fetch || true
  132. # commit given - use this one
  133. git checkout -q ${SITE_ID}
  134. [ "$?" -eq "0" ] || abort "Failed to checkout requested site commit '${SITE_ID}', mimimi."
  135. fi
  136. fi
  137. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  138. popd > /dev/null #${SITE_DIR}
  139. ### APPLY PATCHES TO GLUON
  140. progress "Applying Patches ..."
  141. git checkout -B patching "${BASE}"
  142. if [ -d "${PATCH_DIR}" -a "$(echo ${PATCH_DIR}/*.patch)" ]; then
  143. git am --whitespace=nowarn ${PATCH_DIR}/*.patch || (
  144. git am --abort
  145. git checkout patched
  146. git branch -D patching
  147. false
  148. )
  149. [ "$?" -eq "0" ] || abort "Failed to apply patches, mimimi."
  150. fi
  151. git branch -M patched
  152. ### DIRCLEAN
  153. if [ -d "${GLUON_BUILD_DIR}/" -a "${CLEAN}" == "dirclean" ]; then
  154. progress "Cleaning your build environment (make dirclean) ..."
  155. make dirclean
  156. fi
  157. ### PREPARE
  158. progress "Preparing the build environment (make update) ..."
  159. make update
  160. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  161. popd > /dev/null #${CODE_DIR}
  162. ### set reasonable defaults for ${TARGETS} and ${BRANCH} if unset
  163. if [ -z "${TARGETS}" ]; then
  164. TARGETS=$(make list-targets | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  165. info "building all targets: '${TARGETS}'"
  166. fi
  167. if [ "${BRANCH}" == "experimental" -a -z "${VERSION}" ] ; then
  168. VERSION=$(make default-release)
  169. info "EXPERIMENTAL FIRMWARE: using version tag '${VERSION}'"
  170. fi
  171. # we are now ready to produce the firmware images, so let's "save" our state
  172. build_info_path="${VERSIONS_INFO_DIR}/${VERSION}"
  173. progress "Saving build information to: ${build_info_path}"
  174. [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path}
  175. mkdir -p $(dirname ${build_info_path})
  176. [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})"
  177. touch $(dirname ${build_info_path})
  178. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}"
  179. echo "VERSION=${VERSION}" >> ${build_info_path}
  180. echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path}
  181. echo "BRANCH=${BRANCH}" >> ${build_info_path}
  182. echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path}
  183. echo "TARGETS=${TARGETS}" >> ${build_info_path}
  184. echo "TS=${BUILD_TS}" >> ${build_info_path}
  185. ### restore gluon-opkg-key, if already exists
  186. if [ -e "${KEY_DIR}/gluon-opkg-key" -a -e "${KEY_DIR}/gluon-opkg-key.pub" ]; then
  187. info "gluon-opkg-key already exists, restoring it."
  188. mkdir -p ${GLUON_BUILD_DIR}/
  189. [ "$?" -eq "0" ] || abort "Unable to create directory: ${GLUON_BUILD_DIR}/"
  190. cp -f ${KEY_DIR}/gluon-opkg-key* ${GLUON_BUILD_DIR}/
  191. [ "$?" -eq "0" ] || abort "Unable to copy gluon-opkg-key."
  192. fi
  193. ### BUILD FIRMWARE
  194. progress "Building the firmware - please stand by!"
  195. pushd ${CODE_DIR} > /dev/null
  196. for target in ${TARGETS} ; do
  197. # configure build environment for our current target
  198. export GLUON_TARGET="${target}"
  199. export GLUON_RELEASE="${VERSION}"
  200. [ "${AUTOUPDATER}" != "off" ] && export GLUON_BRANCH="${AUTOUPDATER}"
  201. # prepare build environment for our current target
  202. progress "${target}: Preparing build environment."
  203. if [ "${CLEAN}" == "clean" ]; then
  204. make clean
  205. [ "$?" -eq "0" ] || abort "${target}: Unable to clean environment."
  206. fi
  207. make -j ${MAKEJOBS} prepare-target ${MAKE_PARAM}
  208. [ "$?" -eq "0" ] || abort "${target}: Unable to build environment."
  209. # need to have a toolchain for the particular target
  210. progress "${target}: Building toolchain."
  211. make -j ${MAKEJOBS} toolchain/install ${MAKE_PARAM}
  212. [ "$?" -eq "0" ] || abort "${target}: Unable to build toolchain."
  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} prepare ${MAKE_PARAM}
  216. [ "$?" -eq "0" ] || abort "${target}: Unable to build firmware."
  217. # finally compile the firmware binaries
  218. progress "${target}: Compiling binary firmware images."
  219. make -j ${MAKEJOBS} images ${MAKE_PARAM}
  220. [ "$?" -eq "0" ] || abort "${target}: Unable to assemble images."
  221. # compile the modules
  222. progress "${target}: Compiling modules."
  223. make -j ${MAKEJOBS} modules ${MAKE_PARAM}
  224. [ "$?" -eq "0" ] || abort "${target}: Unable to build modules."
  225. done
  226. # generate manifest
  227. progress "Generating manifest ..."
  228. GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  229. [ "$?" -eq "0" ] || abort "Failed to generate the manifest."
  230. popd > /dev/null #${CODE_DIR}
  231. if [ "${PUBLISH}" -eq "1" ]; then
  232. # copying firmware to the server
  233. if [ -d "${IMAGE_DIR}" ]; then
  234. progress "Copying the firmware ..."
  235. rsync -rlutzc --exclude=*manifest -e ssh ${IMAGE_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}
  236. [ "$?" -eq "0" ] || abort "Failed to copy the firmware."
  237. fi
  238. # copying modules to the server
  239. if [ -d "${MODULE_DIR}" ]; then
  240. progress "Copying the modules ..."
  241. rsync -rlutzc -e ssh ${MODULE_DIR}/gluon-ffho-${VERSION}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}/modules
  242. [ "$?" -eq "0" ] || abort "Failed to copy the modules."
  243. fi
  244. # copy manifest and the build info file
  245. progress "Copying manifest and build info file ..."
  246. pushd ${OUTPUT_DIR} > /dev/null
  247. git fetch
  248. git checkout -q signing
  249. git merge --ff-only origin/master
  250. mkdir -p "${OUTPUT_DIR}/${VERSION}/sysupgrade"
  251. cp -f "${MANIFEST_DIR}/${BRANCH}.manifest" "${OUTPUT_DIR}/${VERSION}/sysupgrade/"
  252. cp -f "${build_info_path}" "${OUTPUT_DIR}/${VERSION}/${BUILD_INFO_FILENAME}"
  253. popd > /dev/null #${OUTPUT_DIR}
  254. # Update symlinks
  255. progress "Update symlink for ${BRANCH}"
  256. pushd ${OUTPUT_DIR} > /dev/null
  257. ln -fsn ${VERSION} ${BRANCH}
  258. popd > /dev/null #${OUTPUT_DIR}
  259. fi
  260. # Saving a copy of gluon-opkg-key
  261. [ -e "${KEY_DIR}/gluon-opkg-key" -a -e "${KEY_DIR}/gluon-opkg-key.pub" ] || cp ${GLUON_BUILD_DIR}/gluon-opkg-key* ${KEY_DIR}/
  262. [ "$?" -eq "0" ] || abort "Failed to save gluon-opkg-key, try to execute 'cp ${GLUON_BUILD_DIR}/gluon-opkg-key* ${KEY_DIR}/' manually"
  263. # The end. Finally.
  264. success "We're done, go and enjoy your new firmware (${VERSION})!"
  265. popd > /dev/null #${MY_DIR}