You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.8KB

  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2019 HardenedBSD
  4. # Author: Shawn Webb <shawn.webb@hardenedbsd.org>
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. is_locked() {
  27. if [ -e ${HBSD_LOCKFILE} ]; then
  28. return 0
  29. fi
  30. return 1
  31. }
  32. lock_build() {
  33. touch ${HBSD_LOCKFILE}
  34. return ${?}
  35. }
  36. unlock_build() {
  37. rm -f ${HBSD_LOCKFILE}
  38. return ${?}
  39. }
  40. assert_unlocked() {
  41. if is_locked; then
  42. echo "Build locked. Remove ${HBSD_LOCKFILE}"
  43. exit 1
  44. fi
  45. return 0
  46. }
  47. # The build_number function must be called EXACTLY ONCE.
  48. build_number() {
  49. local n
  50. if [ -e ${HBSD_INDEX_FILE} ]; then
  51. n=$(cat ${HBSD_INDEX_FILE})
  52. n=$((${n} + 1))
  53. else
  54. n=1
  55. fi
  56. echo ${n} | tee ${HBSD_INDEX_FILE}
  57. return 0
  58. }
  59. codebase_hashish() {
  60. (
  61. cd ${HBSD_SRC}
  62. git rev-parse HEAD
  63. exit ${?}
  64. )
  65. return ${?}
  66. }
  67. cache_codebase_hashish() {
  68. codebase_hashish > ${HBSD_CACHEDIR}/last_build.txt
  69. return ${?}
  70. }
  71. update_codebase() {
  72. (
  73. set -ex
  74. cd ${HBSD_SRC}
  75. git pull
  76. )
  77. return ${?}
  78. }
  79. should_build() {
  80. local lastbuild
  81. local currenthashish
  82. local forcebuild
  83. forcebuild=${1}
  84. [ ${forcebuild} -gt 0 ] && return 0
  85. if [ ! -f ${HBSD_CACHEDIR}/last_build.txt ]; then
  86. return 0
  87. fi
  88. lastbuild=$(cat ${HBSD_CACHEDIR}/last_build.txt)
  89. currenthashish=$(codebase_hashish)
  90. if [ "${lastbuild}" = "${currenthashish}" ]; then
  91. return 1
  92. fi
  93. return 0
  94. }
  95. prune_old_builds() {
  96. [ ${HBSD_BUILDNUMBER} -lt ${HBSD_KEEP_NBUILDS} ] && return 0
  97. floor=$((${HBSD_BUILDNUMBER} - ${HBSD_KEEP_NBUILDS}))
  98. for d in $(find ${HBSD_PUBDIR} -maxdepth 1 -type d | sed 1d); do
  99. n=$(basename ${d})
  100. n=${n##*-}
  101. if [ ${n} -lt ${floor} ]; then
  102. rm -rf ${d}
  103. fi
  104. done
  105. return 0
  106. }