build.sh 12 KB

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