build.sh 9.8 KB

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