build.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. # DIRCLEAN = 1 perform "make dirclean" before build (BRANCH==stable/testing) or 0 perform "make clean" (BRANCH==experimental)
  16. # MAKEJOBS = number of compiler processes running in parallel (default: number of CPUs/Cores)
  17. # PRIORITY = determines the number of day a rollout phase should last at most
  18. # SITE_ID = specific site repository commit-id (leave blank to use HEAD)
  19. # SITE_REPO_FETCH_METHOD = http, everything except "git" will use the HTTP method for fetchting site repo
  20. # TARGETS = a space separated list of target platforms (if unset, all platforms will be build)
  21. # VERBOSE = 0 (default) or 1, call the make commands with 'V=s' to see actual errors better
  22. ### includes
  23. . functions.sh
  24. ### static variables
  25. MY_DIR=$(dirname $0)
  26. MY_DIR=$(readlink -f "${MY_DIR}")
  27. CODE_DIR="${MY_DIR}/src"
  28. SITE_DIR="${CODE_DIR}/site"
  29. PATCH_DIR="${SIZE_DIR}/patches"
  30. OUTPUT_DIR="${MY_DIR}/output"
  31. IMAGE_DIR="${CODE_DIR}/output/images"
  32. MODULE_DIR="${CODE_DIR}/output/modules"
  33. VERSIONS_INFO_DIR="${MY_DIR}/versions"
  34. BUILD_INFO_FILENAME="build-info.txt"
  35. SITE_REPO_URL="git.c3pb.de/freifunk-pb/site-ffho.git"
  36. LANG=C
  37. pushd ${MY_DIR} > /dev/null
  38. ### ERROR handling
  39. [ -n "${BASE}" ] || abort "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)."
  40. [ -n "${BRANCH}" ] || abort "Please specify BRANCH environment variable."
  41. [ "${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."
  42. [ -n "${VERSION}" -o "${BRANCH}" == "experimental" ] || abort "Please specify VERSION environment variable (not necessary for experimental branch)."
  43. [ "${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?"
  44. ### set reasonable defaults for unset environment variables
  45. [ -n "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH}
  46. # ToDo: [ "${BASE}" == "master" ] && BASE="origin/master"
  47. [ -n "${BROKEN}" ] || BROKEN=0
  48. [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  49. if [ -z "${DIRCLEAN}" ]; then
  50. if [ "${BRANCH}" == "experimental" ]; then
  51. DIRCLEAN=0
  52. else
  53. DIRCLEAN=1
  54. fi
  55. fi
  56. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  57. [ -n "${PRIORITY}" ] || PRIORITY=0
  58. [ -n "${SITE_REPO_FETCH_METHOD}" ] || SITE_REPO_FETCH_METHOD="http"
  59. [ -n "${TARGETS}" ] || TARGETS=$(BROKEN=${BROKEN} make list-targets | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  60. if [ "${BRANCH}" == "experimental" -a -z "${VERSION}" ] ; then
  61. default_release_pattern=$( awk -F" := " '/^DEFAULT_GLUON_RELEASE/ { gsub("shell ", "", $2); print $2; }' ${SITE_DIR}/site.mk )
  62. VERSION=$(eval echo ${default_release_pattern})
  63. info "EXPERIMENTAL FIRMWARE: using version tag '$VERSION'"
  64. fi
  65. if [ "${SITE_REPO_FETCH_METHOD}" != "git" ]; then
  66. SITE_REPO_URL="https://${SITE_REPO_URL}"
  67. else
  68. SITE_REPO_URL="git@${SITE_REPO_URL}"
  69. fi
  70. MAKE_PARAM=""
  71. [ "$VERBOSE" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  72. ### INIT /src IF NECESSARY
  73. if [ ! -d "${CODE_DIR}" ]; then
  74. info "Code directory does not exist yet - fetching Gluon ..."
  75. git clone https://github.com/freifunk-gluon/gluon.git "${CODE_DIR}"
  76. [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository."
  77. fi
  78. ### INIT /src/site IF NECESSARY
  79. if [ ! -d "${SITE_DIR}" ]; then
  80. info "Site repository does not exist, fetching it ..."
  81. git clone "${SITE_REPO_URL}" "${SITE_DIR}"
  82. [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository."
  83. fi
  84. pushd ${CODE_DIR}
  85. ### CHECKOUT GLUON
  86. progress "Checking out GLUON '$BASE' ..."
  87. # TODO: check if gluon got modified and bail out if necessary
  88. git fetch
  89. git checkout -q ${BASE}
  90. [ "$?" -eq "0" ] || abort "Failed to checkout '$BASE' gluon base version, mimimi." >&2
  91. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  92. ### CHECKOUT SITE REPO
  93. progress "Checking out SITE REPO ..."
  94. pushd ${SITE_DIR}
  95. # TODO: check if site got modified locally and bail out if necessary
  96. if [ -z "${SITE_ID}" ]; then
  97. # no specific site given - get the most current one
  98. git checkout -q ${BRANCH} ; git pull
  99. [ "$?" -eq "0" ] || abort "Failed to get newest '${BRANCH}' in site repository, mimimi."
  100. else
  101. # fetch site repo updates
  102. git fetch || true
  103. # commit given - use this one
  104. git checkout -q ${SITE_ID}
  105. [ "$?" -eq "0" ] || abort "Failed to checkout requested site commit '${SITE_ID}', mimimi."
  106. fi
  107. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  108. popd #${SITE_DIR}
  109. ### APPLY PATCHES TO GLUON
  110. progress "Applying Patches ..."
  111. git checkout -B patching base
  112. if [ "$(echo "${PATCH_DIR}"/*.patch)" ]; then
  113. git am ${PATCH_DIR}/*.patch || (
  114. git am --abort
  115. git checkout patched
  116. git branch -D patching
  117. false
  118. )
  119. [ "$?" -eq "0" ] || abort "Failed to apply patches, mimimi."
  120. fi
  121. git branch -M patched
  122. ### DIRCLEAN
  123. if [ -d "./build/" -a "${DIRCLEAN}" -eq "1" ]; then
  124. progress "Cleaning your build environment ..."
  125. make dirclean
  126. fi
  127. ### PREPARE
  128. progress "Preparing the build environment (make update) ..."
  129. make update
  130. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  131. # we are now ready to produce the firmware images, so let's "save" our state
  132. build_info_path="${OUTPUT_DIR}/${BRANCH}/${BUILD_INFO_FILENAME}"
  133. progress "Saving build information to: ${build_info_path}"
  134. [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path}
  135. mkdir -p $(dirname ${build_info_path})
  136. [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})"
  137. touch $(dirname ${build_info_path})
  138. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}"
  139. echo "VERSION=${VERSION}" >> ${build_info_path}
  140. echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path}
  141. echo "BRANCH=${BRANCH}" >> ${build_info_path}
  142. echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path}
  143. echo "TARGETS=${TARGETS}" >> ${build_info_path}
  144. echo "TS=${BUILD_TS}" >> ${build_info_path}
  145. ### BUILD FIRMWARE
  146. progress "Building the firmware - please stand by!"
  147. for target in ${TARGETS} ; do
  148. # configure build environment for our current target
  149. export GLUON_TARGET="${target}"
  150. gluon_build_env_vars="GLUON_TARGET=\"${target}\" GLUON_RELEASE=\"${VERSION}\" BROKEN=\"${BROKEN}\""
  151. [ "${AUTOUPDATER}" != "off" ] && gluon_build_env_vars="${gluon_build_env_vars} GLUON_BRANCH=\"${AUTOUPDATER}\""
  152. # prepare build environment for our current target
  153. progress "Preparing build environment for target ${target}."
  154. [ "${DIRCLEAN}" -eq "0" ] && make clean
  155. make -j ${MAKEJOBS} prepare-target${MAKE_PARAM}
  156. [ "$?" -eq "0" ] || abort "Unable to build environment for target-platform ${target}."
  157. # need to have a toolchain for the particular target
  158. progress "Building toolchain for target ${target}."
  159. make -j ${MAKEJOBS} toolchain/install${MAKE_PARAM}
  160. [ "$?" -eq "0" ] || abort "Unable to build toolchain for target-platform ${target}."
  161. # now we can start building the images for the target platform
  162. progress "Building FFHO-flavoured Gluon firmware for target ${target}. You'd better go and fetch some c0ffee!"
  163. eval "${gluon_build_env_vars} faketime \"${BUILD_TS}\" make -j ${MAKEJOBS} prepare${MAKE_PARAM}"
  164. [ "$?" -eq "0" ] || abort "Failed to build firmware for target-platform ${target}."
  165. # finally compile the firmware binaries
  166. progress "Compiling binary firmware images."
  167. faketime "${BUILD_TS}" make -j ${MAKEJOBS} images${MAKE_PARAM}
  168. [ "$?" -eq "0" ] || abort "Failed to assemble images for target-platform ${target}."
  169. # compile the modules
  170. process "Compiling modules."
  171. eval "${gluon_build_env_vars} faketime \"${BUILD_TS}\" make -j ${MAKEJOBS} modules${MAKE_PARAM}"
  172. [ "$?" -eq "0" ] || abort "Failed to build modules for target-platform ${target}."
  173. done
  174. popd #${CODE_DIR}
  175. # compress all binaries into 7z archive
  176. progress "Assembling images.7z ..."
  177. pushd ${IMAGE_DIR}
  178. [ -e "${OUTPUT_DIR}/${BRANCH}/images.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/images.7z"
  179. 7z a -xr!*.manifest "${OUTPUT_DIR}/${BRANCH}/images.7z" ./sysupgrade/* ./factory/*
  180. [ "$?" -eq "0" ] || abort "Failed to assemble images (did you install p7zip-full?)."
  181. popd #${IMAGE_DIR}
  182. # compress modules into 7z archive
  183. progress "Assembling modules.7z ..."
  184. pushd ${MODULE_DIR}
  185. [ -e "${OUTPUT_DIR}/${BRANCH}/modules.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/modules.7z"
  186. 7z a "${OUTPUT_DIR}/${BRANCH}/modules.7z" ./gluon-ffho-${VERSION}
  187. [ "$?" -eq "0" ] || abort "Failed to assemble modules."
  188. popd #${MODULE_DIR}
  189. # generate and copy manifests
  190. progress "Generating and copying manifest ..."
  191. pushd ${CODE_DIR}
  192. faketime "${BUILD_TS}" GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  193. [ "$?" -eq "0" ] || abort "Failed to generate the manifest, try running 'make manifest' in '$CODE_DIR' directory manually."
  194. cp "${CODE_DIR}/output/images/sysupgrade/${BRANCH}.manifest" "${OUTPUT_DIR}/${BRANCH}/"
  195. popd #${CODE_DIR}
  196. # Saving a copy of the build info file as reference
  197. progress "Building a greater and brighter firmware finished successfully. Saving build information at: ${VERSIONS_INFO_DIR}/${VERSION}"
  198. cp -p "${build_info_path}" "${VERSIONS_INFO_DIR}/${VERSION}"
  199. # The end. Finally.
  200. success "We're done, go and enjoy your new firmware in ${OUTPUT_DIR}/${BRANCH}!"
  201. popd > /dev/null #${MY_DIR}