build.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. # we are now ready to produke the firmware images, so let's "save" our state
  109. build_info_path="${OUTPUT_DIR}/${BRANCH}/${BUILD_INFO_FILENAME}"
  110. progress "Saving build information to: ${build_info_path}"
  111. [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path}
  112. mkdir -p $(dirname ${build_info_path})
  113. [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})"
  114. touch $(dirname ${build_info_path})
  115. [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}"
  116. echo "VERSION=${VERSION}" >> ${build_info_path}
  117. echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path}
  118. echo "BRANCH=${BRANCH}" >> ${build_info_path}
  119. echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path}
  120. echo "TS=${BUILD_TS}" >> ${build_info_path}
  121. ### BUILD FIRMWARE
  122. progress "Building the firmware - please stand by!"
  123. [ -n "${BROKEN}" ] || BROKEN=0
  124. [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo)
  125. [ -n "${TARGETS}" ] || TARGETS=$(get_all_supported_platforms "./targets")
  126. [ -n "${PRIORITY}" ] || PRIORITY=0
  127. MAKE_PARAM=""
  128. [ "_$VERBOSE" = "_1" ] && MAKE_PARAM="${MAKE_PARAM} V=s"
  129. for target in ${TARGETS} ; do
  130. # configure build environment for our current target
  131. export GLUON_TARGET="${target}"
  132. gluon_build_env_vars="GLUON_TARGET=\"${target}\" GLUON_BRANCH=\"${BRANCH}\" GLUON_RELEASE=\"${VERSION}\" BROKEN=\"${BROKEN}\""
  133. # prepare build environment for our current target
  134. progress "Preparing build environment for target ${target}."
  135. [ "${BRANCH}" == "experimental" ] || make clean
  136. make -j ${MAKEJOBS} prepare-target${MAKE_PARAM}
  137. # need to have a toolchain for the particular target
  138. progress "Building toolchain for target ${target}."
  139. make -j ${MAKEJOBS} toolchain/install${MAKE_PARAM}
  140. [ "$?" -eq "0" ] || abort "Unable to build toolchain for target. Aborting."
  141. # now we can start building the images for the target platform
  142. progress "Building FFPB-flavoured Gluon firmware for target ${target}. You'd better go and fetch some c0ffee!"
  143. make_targets="prepare"
  144. eval "${gluon_build_env_vars} faketime \"$BUILD_TS\" make -j ${MAKEJOBS} ${make_targets}"
  145. [ "$?" -eq "0" ] || abort "Failed to build firmware for target-platform ${target}."
  146. # finally compile the firmware binaries
  147. progress "Compiling binary firmware images."
  148. faketime "$BUILD_TS" make images
  149. [ "$?" -eq "0" ] || abort "Failed to assemble images for target-platform ${target}."
  150. done
  151. cd ..
  152. # compress all binaries into 7z archive
  153. progress "Assembling images.7z ..."
  154. [ -e "${OUTPUT_DIR}/${BRANCH}/images.7z" ] && rm "${OUTPUT_DIR}/${BRANCH}/images.7z"
  155. 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?)."
  156. # generate, franken-merge, and copy manifests
  157. progress "Generating and copying manifest ..."
  158. pushd $CODE_DIR
  159. for target in ${TARGETS} ; do
  160. GLUON_TARGET="${target}" GLUON_BRANCH=$BRANCH make manifest || abort "Failed to generate the manifest, try running 'make manifest' in '$CODE_DIR' directory manually."
  161. mv ./images/sysupgrade/${BRANCH}.manifest ./images/sysupgrade/${target}.${BRANCH}.manifest
  162. done
  163. frankenmerge_manifest_file="./images/sysupgrade/${BRANCH}.manifest"
  164. echo "BRANCH=${BRANCH}" > ${frankenmerge_manifest_file}
  165. echo "DATE=${BUILD_TS}$(date +%:z)" >> ${frankenmerge_manifest_file}
  166. echo "PRIORITY=${PRIORITY}" >> ${frankenmerge_manifest_file}
  167. echo "" >> ${frankenmerge_manifest_file}
  168. grep -hE "*.bin$" ./images/sysupgrade/*.${BRANCH}.manifest | sort >> ${frankenmerge_manifest_file}
  169. popd
  170. cp "${CODE_DIR}/images/sysupgrade/${BRANCH}.manifest" "${OUTPUT_DIR}/${BRANCH}/"
  171. # Saving a copy of the build info file as reference
  172. progress "Building a greater and brighter firmware finished successfully. Saving build information at: ${VERSIONS_INFO_DIR}/${VERSION}"
  173. cp -p "${build_info_path}" "${VERSIONS_INFO_DIR}/${VERSION}"
  174. # The end. Finally.
  175. success "We're done, go and enjoy your new firmware in ${OUTPUT_DIR}/${BRANCH}!"
  176. popd > /dev/null