build.sh 10 KB

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