build.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/bash
  2. # (c) 2014-2015 Freifunk Paderborn <maschinenraum@paderborn.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. # SITE = site repository to use
  10. # SITE_ID = specific site repository commit-id (leave blank to use HEAD)
  11. # VERSION = the version tag (can only be empty if BRANCH=experimental)
  12. # BUILD_TS = build timestamp (format: %Y-%m-%d %H:%M:%S)
  13. # BROKEN = 0 (default) or 1, build the untested hardware model firmwares, too
  14. # MAKEJOBS = number of compiler processes running in parallel (default: number of CPUs/Cores)
  15. # TARGETS = a space separated list of target platforms (if unset, all platforms will be build)
  16. # PRIORITY = determines the number of day a rollout phase should last at most
  17. # VERBOSE = 0 (default) or 1, call the make commands with 'V=s' to see actual errors better
  18. # SITE_REPO_FETCH_METHOD = http, everything except "git" will use the HTTP method for fetchting site repo
  19. #
  20. function get_all_supported_platforms()
  21. {
  22. local buffer;
  23. for val in $(ls ${1}) ; do
  24. [ -d "./${1}/${val}" ] || continue
  25. buffer="${buffer} ${val}"
  26. done
  27. echo ${buffer}
  28. }
  29. if [ "_$BRANCH" == "_" ]; then
  30. echo "Please specify BRANCH environment variable." >&2
  31. exit 1
  32. fi
  33. if [ "_$BASE" == "_" ]; then
  34. echo "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)." >&2
  35. exit 1
  36. fi
  37. if [ "${BRANCH}" != "experimental" -a "${BASE}" == "HEAD" ] ; then
  38. echo "HEAD is not an allowed BASE-identifier for non-experimental builds." >&2
  39. echo "Either use a tagged commit or the commit-SHA itself." >&2
  40. exit 1
  41. fi
  42. if [ "_$VERSION" == "_" -a "$BRANCH" != "experimental" ]; then
  43. echo "Please specify VERSION environment variable (not necessary for experimental branch)." >&2
  44. exit 1
  45. fi
  46. MY_DIR=$(dirname $0)
  47. MY_DIR=$(readlink -f "$MY_DIR")
  48. CODE_DIR="src"
  49. PATCH_DIR="site/patches"
  50. OUTPUT_DIR="${MY_DIR}/output"
  51. BUILD_INFO_FILENAME="build-info.txt"
  52. VERSIONS_INFO_DIR="versions"
  53. LANG=C
  54. pushd $MY_DIR > /dev/null
  55. [ "_$BUILD_TS" == "_" ] && export BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S")
  56. . functions.sh
  57. ### CHECK THAT VERSION DOES NOT YET EXISTS
  58. [ -n "$VERSION" -a -x "${VERSIONS_INFO_DIR}/${VERSION}" ] && abort "There exists a version file for '$VERSION' ... you are trying to do something really stupid, aren't you?"
  59. ### INIT /src IF NECESSARY
  60. if [ ! -d "$CODE_DIR" ]; then
  61. info "Code directory does not exist yet - fetching Gluon ..."
  62. git clone https://github.com/freifunk-gluon/gluon.git "$CODE_DIR"
  63. fi
  64. if [ "_${SITE_REPO_FETCH_METHOD}" != "_git" ]; then
  65. SITE_REPO_URL="https://git.c3pb.de/freifunk-pb/site-${SITE}.git"
  66. else
  67. SITE_REPO_URL="git@git.c3pb.de:freifunk-pb/site-${SITE}.git"
  68. fi
  69. ### INIT /src/site IF NECESSARY
  70. if [ -d "$CODE_DIR/site" ]; then
  71. # verify the site-repo is the correct one ($SITE), otherwise delete the repo
  72. pushd "$CODE_DIR/site"
  73. url=$(git remote show origin | awk '/Fetch URL/ { print $3; }')
  74. if [ "$SITE_REPO_URL" != "$url" ]; then
  75. info "The site repository is not the correct one."
  76. if ! git diff-index --quiet HEAD --; then
  77. popd > /dev/null
  78. abort "The site repo is the wrong one but has local modifications, please fix this manually."
  79. fi
  80. # check on the actual branch, not the target one given as parameter
  81. local_branch=$(git branch --list --no-color | awk '/^*/ { print $2; }')
  82. commits=$(git log origin/${local_branch}..HEAD)
  83. if [ -n "$commits" ]; then
  84. popd > /dev/null
  85. abort "The site repo is the wrong one but has unpushed commits, please fix this manually."
  86. fi
  87. # remove the directory without asking further questions
  88. popd > /dev/null
  89. rm -Rf "$CODE_DIR/site" || abort "Failed to remove wrong site-repository."
  90. success "Removed old site directory in order to be able to clone the correct one."
  91. else
  92. popd > /dev/null
  93. fi
  94. fi
  95. if [ ! -d "$CODE_DIR/site" ]; then
  96. info "Site repository does not exist, fetching it ..."
  97. git clone "$SITE_REPO_URL" "$CODE_DIR/site" || abort "Failed to fetch SITE repository."
  98. fi
  99. ### CHECKOUT GLUON
  100. progress "Checking out GLUON '$BASE' ..."
  101. cd $CODE_DIR
  102. # TODO: check if gluon got modified and bail out if necessary
  103. git fetch
  104. if [ "$BASE" = "master" ]; then
  105. git checkout -q origin/master
  106. else
  107. git checkout -q $BASE
  108. fi
  109. [ "$?" -eq "0" ] || abort "Failed to checkout '$BASE' gluon base version, mimimi." >&2
  110. GLUON_COMMIT=$(git rev-list --max-count=1 HEAD)
  111. ### CHECKOUT SITE REPO
  112. progress "Checking out SITE REPO ..."
  113. cd site
  114. # TODO: check if site got modified locally and bail out if necessary
  115. if [ "_${SITE_ID}" == "_" ]; then
  116. # no specific site given - get the most current one
  117. git checkout -q $BRANCH ; git pull
  118. [ "$?" -eq "0" ] || abort "Failed to get newest '$BRANCH' in site repository, mimimi."
  119. else
  120. # fetch site repo updates
  121. git fetch || true
  122. # commit given - use this one
  123. git checkout -q ${SITE_ID} || abort "Failed to checkout requested site commit, mimimi."
  124. fi
  125. SITE_COMMIT=$(git rev-list --max-count=1 HEAD)
  126. cd ..
  127. ### APPLY PATCHES TO GLUON
  128. if [ -d "$PATCH_DIR" ]; then
  129. progress "Applying Patches ..."
  130. git am $PATCH_DIR/*.patch
  131. [ "$?" -eq "0" ] || abort "Failed to apply patches, mimimi."
  132. fi
  133. ### CLEAN
  134. if [ -d "./build/" -a "$BRANCH" != "experimental" ]; then
  135. progress "Cleaning your build environment ..."
  136. make dirclean
  137. fi
  138. ### PREPARE
  139. progress "Preparing the build environment (make update) ..."
  140. make update
  141. [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi."
  142. # determine VERSION for BRANCH=experimental if it is not set
  143. if [ "${BRANCH}" == "experimental" -a -z "${VERSION}" ] ; then
  144. default_release_pattern=$( awk -F" := " '/^DEFAULT_GLUON_RELEASE/ { gsub("shell ", "", $2); print $2; }' ./site/site.mk )
  145. VERSION=$(eval echo ${default_release_pattern})
  146. info "EXPERIMENTAL FIRMWARE: using version tag '$VERSION'"
  147. fi
  148. # set reasonable defaults for unset environment variables
  149. [ -n "${BROKEN}" ] || BROKEN=0
  150. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  151. [ -n "${TARGETS}" ] || TARGETS=$(get_all_supported_platforms "./targets")
  152. [ -n "${PRIORITY}" ] || PRIORITY=0
  153. MAKE_PARAM=""
  154. [ "_$VERBOSE" = "_1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  155. # we are now ready to produke the firmware images, so let's "save" our state
  156. build_info_path="${OUTPUT_DIR}/${BRANCH}/${BUILD_INFO_FILENAME}"
  157. progress "Saving build information to: ${build_info_path}"
  158. [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path}
  159. mkdir -p $(dirname ${build_info_path})
  160. [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})"
  161. touch $(dirname ${build_info_path})
  162. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}"
  163. echo "VERSION=${VERSION}" >> ${build_info_path}
  164. echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path}
  165. echo "BRANCH=${BRANCH}" >> ${build_info_path}
  166. echo "SITE_REPO=${SITE}" >> ${build_info_path}
  167. echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path}
  168. echo "TARGETS=${TARGETS}" >> ${build_info_path}
  169. echo "TS=${BUILD_TS}" >> ${build_info_path}
  170. ### BUILD FIRMWARE
  171. progress "Building the firmware - please stand by!"
  172. for target in ${TARGETS} ; do
  173. # configure build environment for our current target
  174. export GLUON_TARGET="${target}"
  175. gluon_build_env_vars="GLUON_TARGET=\"${target}\" GLUON_BRANCH=\"${BRANCH}\" GLUON_RELEASE=\"${VERSION}\" BROKEN=\"${BROKEN}\""
  176. # prepare build environment for our current target
  177. progress "Preparing build environment for target ${target}."
  178. [ "${BRANCH}" == "experimental" ] || make clean
  179. make -j ${MAKEJOBS} prepare-target${MAKE_PARAM}
  180. # need to have a toolchain for the particular target
  181. progress "Building toolchain for target ${target}."
  182. make -j ${MAKEJOBS} toolchain/install${MAKE_PARAM}
  183. [ "$?" -eq "0" ] || abort "Unable to build toolchain for target. Aborting."
  184. # now we can start building the images for the target platform
  185. progress "Building FFPB-flavoured Gluon firmware for target ${target}. You'd better go and fetch some c0ffee!"
  186. make_targets="prepare"
  187. eval "${gluon_build_env_vars} faketime \"$BUILD_TS\" make -j ${MAKEJOBS} ${make_targets}${MAKE_PARAM}"
  188. [ "$?" -eq "0" ] || abort "Failed to build firmware for target-platform ${target}."
  189. # finally compile the firmware binaries
  190. progress "Compiling binary firmware images."
  191. faketime "$BUILD_TS" make images${MAKE_PARAM}
  192. [ "$?" -eq "0" ] || abort "Failed to assemble images for target-platform ${target}."
  193. done
  194. cd ..
  195. # compress all binaries into 7z archive
  196. progress "Assembling images.7z ..."
  197. [ -e "${OUTPUT_DIR}/${BRANCH}/images.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/images.7z"
  198. 7z a -xr!*.manifest "${OUTPUT_DIR}/${BRANCH}/images.7z" ${CODE_DIR}/images/sysupgrade/* ${CODE_DIR}/images/factory/* || abort "Failed to assemble images (did you install p7zip-full?)."
  199. # generate, franken-merge, and copy manifests
  200. progress "Generating and copying manifest ..."
  201. pushd $CODE_DIR
  202. GLUON_TARGET="ar71xx-generic" GLUON_BRANCH=$BRANCH make manifest || abort "Failed to generate the manifest, try running 'make manifest' in '$CODE_DIR' directory manually."
  203. popd
  204. cp "${CODE_DIR}/images/sysupgrade/${BRANCH}.manifest" "${OUTPUT_DIR}/${BRANCH}/"
  205. # Saving a copy of the build info file as reference
  206. progress "Building a greater and brighter firmware finished successfully. Saving build information at: ${VERSIONS_INFO_DIR}/${VERSION}"
  207. cp -p "${build_info_path}" "${VERSIONS_INFO_DIR}/${VERSION}"
  208. # The end. Finally.
  209. success "We're done, go and enjoy your new firmware in ${OUTPUT_DIR}/${BRANCH}!"
  210. popd > /dev/null