buildx.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env bash
  2. ## This script helps to run docker buildx to build cross-arch/platform packages (linux only)
  3. ## It mounts (not copy) host directory to a cross-arch/platform builder container
  4. ## Make sure the source dir (specified by --src_dir option) is clean before running this script
  5. ## NOTE: it requires $USER in docker group
  6. ## i.e. will not work if docker command has to be executed with sudo
  7. ## example:
  8. ## ./scripts/buildx.sh --profile emqx --pkgtype tgz --arch arm64 \
  9. ## --builder ghcr.io/emqx/emqx-builder/5.0-26:1.13.4-24.3.4.2-1-debian11
  10. set -euo pipefail
  11. help() {
  12. echo
  13. echo "-h|--help: To display this usage information"
  14. echo "--profile <PROFILE>: EMQX profile to build (emqx|emqx-enterprise)"
  15. echo "--pkgtype tgz|pkg: Specify which package to build, tgz for .tar.gz,"
  16. echo " pkg for .rpm or .deb"
  17. echo "--elixir: Specify if the release should be built with Elixir, "
  18. echo " defaults to 'no'."
  19. echo "--arch amd64|arm64: Target arch to build the EMQX package for"
  20. echo "--src_dir <SRC_DIR>: EMQX source code in this dir, default to PWD"
  21. echo "--builder <BUILDER>: Builder image to pull"
  22. echo " E.g. ghcr.io/emqx/emqx-builder/5.0-26:1.13.4-24.3.4.2-1-debian11"
  23. }
  24. while [ "$#" -gt 0 ]; do
  25. case $1 in
  26. -h|--help)
  27. help
  28. exit 0
  29. ;;
  30. --src_dir)
  31. SRC_DIR="$2"
  32. shift 2
  33. ;;
  34. --profile)
  35. PROFILE="$2"
  36. shift 2
  37. ;;
  38. --pkgtype)
  39. PKGTYPE="$2"
  40. shift 2
  41. ;;
  42. --builder)
  43. BUILDER="$2"
  44. shift 2
  45. ;;
  46. --arch)
  47. ARCH="$2"
  48. shift 2
  49. ;;
  50. --elixir)
  51. shift 1
  52. case ${1:-novalue} in
  53. -*)
  54. # another option
  55. IS_ELIXIR='yes'
  56. ;;
  57. yes|no)
  58. IS_ELIXIR="${1}"
  59. shift 1
  60. ;;
  61. novalue)
  62. IS_ELIXIR='yes'
  63. ;;
  64. *)
  65. echo "ERROR: unknown option: --elixir $2"
  66. exit 1
  67. ;;
  68. esac
  69. ;;
  70. *)
  71. echo "WARN: Unknown arg (ignored): $1"
  72. shift
  73. continue
  74. ;;
  75. esac
  76. done
  77. if [ -z "${PROFILE:-}" ] ||
  78. [ -z "${PKGTYPE:-}" ] ||
  79. [ -z "${BUILDER:-}" ] ||
  80. [ -z "${ARCH:-}" ]; then
  81. help
  82. exit 1
  83. fi
  84. # ensure dir
  85. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
  86. set -x
  87. if [ -z "${IS_ELIXIR:-}" ]; then
  88. IS_ELIXIR=no
  89. fi
  90. case "$PKGTYPE" in
  91. tgz|pkg)
  92. true
  93. ;;
  94. *)
  95. echo "Bad --pkgtype option, should be tgz or pkg"
  96. exit 1
  97. ;;
  98. esac
  99. export CODE_PATH="${SRC_DIR:-$PWD}"
  100. cd "${CODE_PATH}"
  101. if [ "$IS_ELIXIR" = "yes" ]; then
  102. MAKE_TARGET="${PROFILE}-elixir-${PKGTYPE}"
  103. else
  104. MAKE_TARGET="${PROFILE}-${PKGTYPE}"
  105. fi
  106. HOST_SYSTEM="$(./scripts/get-distro.sh)"
  107. BUILDER_SYSTEM="$(echo "$BUILDER" | awk -F'-' '{print $NF}')"
  108. CMD_RUN="make ${MAKE_TARGET} && ./scripts/pkg-tests.sh ${MAKE_TARGET}"
  109. IS_NATIVE_SYSTEM='no'
  110. if [[ "$HOST_SYSTEM" = "$BUILDER_SYSTEM" ]]; then
  111. IS_NATIVE_SYSTEM='yes'
  112. fi
  113. IS_NATIVE_ARCH='no'
  114. if [[ $(uname -m) == "x86_64" && "$ARCH" == "amd64" ]]; then
  115. IS_NATIVE_ARCH='yes'
  116. elif [[ $(uname -m) == "aarch64" && "$ARCH" == "arm64" ]]; then
  117. IS_NATIVE_ARCH='yes'
  118. elif [[ $(uname -m) == "arm64" && "$ARCH" == "arm64" ]]; then
  119. IS_NATIVE_ARCH='yes'
  120. elif [[ $(uname -m) == "armv7l" && "$ARCH" == "arm64" ]]; then
  121. IS_NATIVE_ARCH='yes'
  122. fi
  123. if [[ "${IS_NATIVE_SYSTEM}" == 'yes' && "${IS_NATIVE_ARCH}" == 'yes' ]]; then
  124. export ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal"
  125. eval "$CMD_RUN"
  126. elif docker info; then
  127. if [[ "${IS_NATIVE_ARCH}" == 'no' ]]; then
  128. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  129. fi
  130. docker run -i --rm \
  131. -v "$(pwd)":/emqx \
  132. --workdir /emqx \
  133. --platform="linux/$ARCH" \
  134. --env ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal" \
  135. "$BUILDER" \
  136. bash -euc "$CMD_RUN"
  137. else
  138. echo "Error: Docker not available on unsupported platform"
  139. exit 1;
  140. fi