build.sh 6.8 KB

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