generate.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. set -e
  3. shopt -s globstar
  4. cd "$(dirname "$0")"
  5. source util/vars.sh
  6. export LC_ALL=C.UTF-8
  7. rm -f Dockerfile Dockerfile.{dl,final,dl.final}
  8. layername() {
  9. printf "layer-"
  10. basename "$1" | sed 's/.sh$//'
  11. }
  12. to_df() {
  13. _of="${TODF:-Dockerfile}"
  14. printf "$@" >> "$_of"
  15. echo >> "$_of"
  16. }
  17. ###
  18. ### Generate main Dockerfile
  19. ###
  20. exec_dockerstage() {
  21. SCRIPT="$1"
  22. (
  23. SELF="$SCRIPT"
  24. STAGENAME="$(basename "$SCRIPT" | sed 's/.sh$//')"
  25. source util/dl_functions.sh
  26. source "$SCRIPT"
  27. ffbuild_enabled || exit 0
  28. to_df "ENV SELF=\"$SELF\" STAGENAME=\"$STAGENAME\""
  29. STG="$(ffbuild_dockerdl)"
  30. if [[ -n "$STG" ]]; then
  31. HASH="$(sha256sum <<<"$STG" | cut -d" " -f1)"
  32. export SELFCACHE=".cache/downloads/${STAGENAME}_${HASH}.tar.xz"
  33. fi
  34. ffbuild_dockerstage || exit $?
  35. )
  36. }
  37. export TODF="Dockerfile"
  38. to_df "FROM ${REGISTRY}/${REPO}/base-${TARGET}:latest AS base"
  39. to_df "ENV TARGET=$TARGET VARIANT=$VARIANT REPO=$REPO ADDINS_STR=$ADDINS_STR"
  40. to_df "COPY --link util/run_stage.sh /usr/bin/run_stage"
  41. for addin in "${ADDINS[@]}"; do
  42. (
  43. source addins/"${addin}.sh"
  44. type ffbuild_dockeraddin &>/dev/null && ffbuild_dockeraddin || true
  45. )
  46. done
  47. PREVLAYER="base"
  48. for ID in $(ls -1d scripts.d/??-* | sed -s 's|^.*/\(..\).*|\1|' | sort -u); do
  49. LAYER="layer-$ID"
  50. for STAGE in scripts.d/$ID-*; do
  51. to_df "FROM $PREVLAYER AS $(layername "$STAGE")"
  52. if [[ -f "$STAGE" ]]; then
  53. exec_dockerstage "$STAGE"
  54. else
  55. for STAGE in "${STAGE}"/??-*; do
  56. exec_dockerstage "$STAGE"
  57. done
  58. fi
  59. done
  60. to_df "FROM $PREVLAYER AS $LAYER"
  61. for STAGE in scripts.d/$ID-*; do
  62. if [[ -f "$STAGE" ]]; then
  63. SCRIPT="$STAGE"
  64. else
  65. SCRIPTS=( "$STAGE"/??-* )
  66. SCRIPT="${SCRIPTS[-1]}"
  67. fi
  68. (
  69. SELF="$SCRIPT"
  70. SELFLAYER="$(layername "$STAGE")"
  71. source "$SCRIPT"
  72. ffbuild_enabled || exit 0
  73. ffbuild_dockerlayer || exit $?
  74. TODF="Dockerfile.final" PREVLAYER="__PREVLAYER__" \
  75. ffbuild_dockerfinal || exit $?
  76. )
  77. done
  78. PREVLAYER="$LAYER"
  79. done
  80. to_df "FROM base"
  81. sed "s/__PREVLAYER__/$PREVLAYER/g" Dockerfile.final | sort -u >> Dockerfile
  82. rm Dockerfile.final
  83. ###
  84. ### Compile list of configure arguments and add them to the final Dockerfile
  85. ###
  86. get_output() {
  87. (
  88. SELF="$1"
  89. source $1
  90. if ffbuild_enabled; then
  91. ffbuild_$2 || exit 0
  92. else
  93. ffbuild_un$2 || exit 0
  94. fi
  95. )
  96. }
  97. source "variants/${TARGET}-${VARIANT}.sh"
  98. for addin in ${ADDINS[*]}; do
  99. source "addins/${addin}.sh"
  100. done
  101. for script in scripts.d/**/*.sh; do
  102. FF_CONFIGURE+=" $(get_output $script configure)"
  103. FF_CFLAGS+=" $(get_output $script cflags)"
  104. FF_CXXFLAGS+=" $(get_output $script cxxflags)"
  105. FF_LDFLAGS+=" $(get_output $script ldflags)"
  106. FF_LDEXEFLAGS+=" $(get_output $script ldexeflags)"
  107. FF_LIBS+=" $(get_output $script libs)"
  108. done
  109. FF_CONFIGURE="$(xargs <<< "$FF_CONFIGURE")"
  110. FF_CFLAGS="$(xargs <<< "$FF_CFLAGS")"
  111. FF_CXXFLAGS="$(xargs <<< "$FF_CXXFLAGS")"
  112. FF_LDFLAGS="$(xargs <<< "$FF_LDFLAGS")"
  113. FF_LDEXEFLAGS="$(xargs <<< "$FF_LDEXEFLAGS")"
  114. FF_LIBS="$(xargs <<< "$FF_LIBS")"
  115. to_df "ENV \\"
  116. to_df " FF_CONFIGURE=\"$FF_CONFIGURE\" \\"
  117. to_df " FF_CFLAGS=\"$FF_CFLAGS\" \\"
  118. to_df " FF_CXXFLAGS=\"$FF_CXXFLAGS\" \\"
  119. to_df " FF_LDFLAGS=\"$FF_LDFLAGS\" \\"
  120. to_df " FF_LDEXEFLAGS=\"$FF_LDEXEFLAGS\" \\"
  121. to_df " FF_LIBS=\"$FF_LIBS\""