build.sh 12 KB

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