build.sh 8.3 KB

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