build.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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="${SITE_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="ar71xx-generic ar71xx-nand mpc85xx-generic x86-generic x86-kvm_guest x86-64 x86-xen_domu"
  60. [ -n "${VERBOSE}" ] || VERBOSE=0
  61. if [ "${BRANCH}" == "experimental" -a -z "${VERSION}" ] ; then
  62. VERSION=$(make default-release)
  63. info "EXPERIMENTAL FIRMWARE: using version tag '$VERSION'"
  64. fi
  65. ### set some additional variables
  66. if [ "${SITE_REPO_FETCH_METHOD}" != "git" ]; then
  67. SITE_REPO_URL="https://${SITE_REPO_URL}"
  68. else
  69. SITE_REPO_URL="git@${SITE_REPO_URL}"
  70. fi
  71. MAKE_PARAM=""
  72. [ "$VERBOSE" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  73. ### INIT /src IF NECESSARY
  74. if [ ! -d "${CODE_DIR}" ]; then
  75. info "Code directory does not exist yet - fetching Gluon ..."
  76. git clone https://github.com/freifunk-gluon/gluon.git "${CODE_DIR}"
  77. [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository."
  78. fi
  79. ### INIT /src/site IF NECESSARY
  80. if [ ! -d "${SITE_DIR}" ]; then
  81. info "Site repository does not exist, fetching it ..."
  82. git clone "${SITE_REPO_URL}" "${SITE_DIR}"
  83. [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository."
  84. fi
  85. pushd ${CODE_DIR} > /dev/null
  86. ### CHECKOUT GLUON
  87. progress "Checking out GLUON '$BASE' ..."
  88. # TODO: check if gluon got modified and bail out if necessary
  89. git fetch
  90. git checkout -q ${BASE}
  91. [ "$?" -eq "0" ] || abort "Failed to checkout '$BASE' gluon base version, mimimi." >&2
  92. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  93. ### CHECKOUT SITE REPO
  94. progress "Checking out SITE REPO ..."
  95. pushd ${SITE_DIR} > /dev/null
  96. # TODO: check if site got modified locally and bail out if necessary
  97. if [ -z "${SITE_ID}" ]; then
  98. # no specific site given - get the most current one
  99. git checkout -q ${BRANCH} ; git pull
  100. [ "$?" -eq "0" ] || abort "Failed to get newest '${BRANCH}' in site repository, mimimi."
  101. else
  102. # fetch site repo updates
  103. git fetch || true
  104. # commit given - use this one
  105. git checkout -q ${SITE_ID}
  106. [ "$?" -eq "0" ] || abort "Failed to checkout requested site commit '${SITE_ID}', mimimi."
  107. fi
  108. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  109. popd > /dev/null #${SITE_DIR}
  110. ### APPLY PATCHES TO GLUON
  111. progress "Applying Patches ..."
  112. git checkout -B patching "${BASE}"
  113. if [ "$(echo "${PATCH_DIR}"/*.patch)" ]; then
  114. git am ${PATCH_DIR}/*.patch || (
  115. git am --abort
  116. git checkout patched
  117. git branch -D patching
  118. false
  119. )
  120. [ "$?" -eq "0" ] || abort "Failed to apply patches, mimimi."
  121. fi
  122. git branch -M patched
  123. ### DIRCLEAN
  124. if [ -d "./build/" -a "${DIRCLEAN}" -eq "1" ]; then
  125. progress "Cleaning your build environment ..."
  126. make dirclean
  127. fi
  128. ### PREPARE
  129. progress "Preparing the build environment (make update) ..."
  130. make update
  131. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  132. # we are now ready to produce the firmware images, so let's "save" our state
  133. build_info_path="${OUTPUT_DIR}/${BRANCH}/${BUILD_INFO_FILENAME}"
  134. progress "Saving build information to: ${build_info_path}"
  135. [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path}
  136. mkdir -p $(dirname ${build_info_path})
  137. [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})"
  138. touch $(dirname ${build_info_path})
  139. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}"
  140. echo "VERSION=${VERSION}" >> ${build_info_path}
  141. echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path}
  142. echo "BRANCH=${BRANCH}" >> ${build_info_path}
  143. echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path}
  144. echo "TARGETS=${TARGETS}" >> ${build_info_path}
  145. echo "TS=${BUILD_TS}" >> ${build_info_path}
  146. ### BUILD FIRMWARE
  147. progress "Building the firmware - please stand by!"
  148. for target in ${TARGETS} ; do
  149. # configure build environment for our current target
  150. export GLUON_TARGET="${target}"
  151. gluon_build_env_vars="GLUON_TARGET=\"${target}\" GLUON_RELEASE=\"${VERSION}\" BROKEN=\"${BROKEN}\""
  152. [ "${AUTOUPDATER}" != "off" ] && gluon_build_env_vars="${gluon_build_env_vars} GLUON_BRANCH=\"${AUTOUPDATER}\""
  153. # prepare build environment for our current target
  154. progress "Preparing build environment for target ${target}."
  155. [ "${DIRCLEAN}" -eq "0" ] && make clean
  156. make -j ${MAKEJOBS} prepare-target ${MAKE_PARAM}
  157. [ "$?" -eq "0" ] || abort "Unable to build environment for target-platform ${target}."
  158. # need to have a toolchain for the particular target
  159. progress "Building toolchain for target ${target}."
  160. make -j ${MAKEJOBS} toolchain/install ${MAKE_PARAM}
  161. [ "$?" -eq "0" ] || abort "Unable to build toolchain for target-platform ${target}."
  162. # now we can start building the images for the target platform
  163. progress "Building FFHO-flavoured Gluon firmware for target ${target}. You'd better go and fetch some c0ffee!"
  164. eval "${gluon_build_env_vars} make -j ${MAKEJOBS} prepare ${MAKE_PARAM}"
  165. [ "$?" -eq "0" ] || abort "Failed to build firmware for target-platform ${target}."
  166. # finally compile the firmware binaries
  167. progress "Compiling binary firmware images."
  168. make -j ${MAKEJOBS} images ${MAKE_PARAM}
  169. [ "$?" -eq "0" ] || abort "Failed to assemble images for target-platform ${target}."
  170. # compile the modules
  171. progress "Compiling modules."
  172. eval "${gluon_build_env_vars} make -j ${MAKEJOBS} modules ${MAKE_PARAM}"
  173. [ "$?" -eq "0" ] || abort "Failed to build modules for target-platform ${target}."
  174. done
  175. popd > /dev/null #${CODE_DIR}
  176. # compress all binaries into 7z archive
  177. progress "Assembling images.7z ..."
  178. pushd ${IMAGE_DIR} > /dev/null
  179. [ -e "${OUTPUT_DIR}/${BRANCH}/images.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/images.7z"
  180. 7z a -mmt=on -xr!*.manifest "${OUTPUT_DIR}/${BRANCH}/images.7z" ./sysupgrade/* ./factory/*
  181. [ "$?" -eq "0" ] || abort "Failed to assemble images (did you install p7zip-full?)."
  182. popd > /dev/null #${IMAGE_DIR}
  183. # compress modules into 7z archive
  184. progress "Assembling modules.7z ..."
  185. pushd ${MODULE_DIR} > /dev/null
  186. [ -e "${OUTPUT_DIR}/${BRANCH}/modules.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/modules.7z"
  187. 7z a -mmt=on "${OUTPUT_DIR}/${BRANCH}/modules.7z" ./gluon-ffho-${VERSION} > /dev/null
  188. [ "$?" -eq "0" ] || abort "Failed to assemble modules."
  189. popd > /dev/null #${MODULE_DIR}
  190. # generate and copy manifests
  191. progress "Generating and copying manifest ..."
  192. pushd ${CODE_DIR} > /dev/null
  193. GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest
  194. [ "$?" -eq "0" ] || abort "Failed to generate the manifest, try running 'make manifest' in '$CODE_DIR' directory manually."
  195. cp "${CODE_DIR}/output/images/sysupgrade/${BRANCH}.manifest" "${OUTPUT_DIR}/${BRANCH}/"
  196. popd > /dev/null #${CODE_DIR}
  197. # Saving a copy of the build info file as reference
  198. progress "Building a greater and brighter firmware finished successfully. Saving build information at: ${VERSIONS_INFO_DIR}/${VERSION}"
  199. cp -p "${build_info_path}" "${VERSIONS_INFO_DIR}/${VERSION}"
  200. # The end. Finally.
  201. success "We're done, go and enjoy your new firmware in ${OUTPUT_DIR}/${BRANCH}!"
  202. popd > /dev/null #${MY_DIR}