build.sh 7.7 KB

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