buildx.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.1-4:1.14.5-25.3.2-2-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.1-4:1.14.5-25.3.2-2-debian11"
  23. }
  24. die() {
  25. msg="$1"
  26. echo "$msg" >&2
  27. help
  28. exit 1
  29. }
  30. while [ "$#" -gt 0 ]; do
  31. case $1 in
  32. -h|--help)
  33. help
  34. exit 0
  35. ;;
  36. --src_dir)
  37. SRC_DIR="$2"
  38. shift 2
  39. ;;
  40. --profile)
  41. PROFILE="$2"
  42. shift 2
  43. ;;
  44. --pkgtype)
  45. PKGTYPE="$2"
  46. shift 2
  47. ;;
  48. --builder)
  49. BUILDER="$2"
  50. shift 2
  51. ;;
  52. --arch)
  53. ARCH="$2"
  54. shift 2
  55. ;;
  56. --elixir)
  57. shift 1
  58. case ${1:-novalue} in
  59. -*)
  60. # another option
  61. IS_ELIXIR='yes'
  62. ;;
  63. yes|no)
  64. IS_ELIXIR="${1}"
  65. shift 1
  66. ;;
  67. novalue)
  68. IS_ELIXIR='yes'
  69. ;;
  70. *)
  71. echo "ERROR: unknown option: --elixir $2"
  72. exit 1
  73. ;;
  74. esac
  75. ;;
  76. *)
  77. echo "WARN: Unknown arg (ignored): $1"
  78. shift
  79. continue
  80. ;;
  81. esac
  82. done
  83. ## we have a different naming for them
  84. if [[ $(uname -m) == "x86_64" ]]; then
  85. NATIVE_ARCH='amd64'
  86. elif [[ $(uname -m) == "aarch64" ]]; then
  87. NATIVE_ARCH='arm64'
  88. elif [[ $(uname -m) == "arm64" ]]; then
  89. NATIVE_ARCH='arm64'
  90. elif [[ $(uname -m) == "armv7l" ]]; then
  91. # CHECKME: really ?
  92. NATIVE_ARCH='arm64'
  93. fi
  94. ARCH="${ARCH:-${NATIVE_ARCH:-}}"
  95. [ -z "${PROFILE:-}" ] && die "missing --prifile"
  96. [ -z "${PKGTYPE:-}" ] && die "missing --pkgtyp"
  97. [ -z "${BUILDER:-}" ] && die "missing --builder"
  98. [ -z "${ARCH:-}" ] && die "missing --arch"
  99. # ensure dir
  100. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
  101. set -x
  102. if [ -z "${IS_ELIXIR:-}" ]; then
  103. IS_ELIXIR=no
  104. fi
  105. case "$PKGTYPE" in
  106. tgz|pkg)
  107. true
  108. ;;
  109. *)
  110. echo "Bad --pkgtype option, should be tgz or pkg"
  111. exit 1
  112. ;;
  113. esac
  114. export CODE_PATH="${SRC_DIR:-$PWD}"
  115. cd "${CODE_PATH}"
  116. if [ "$IS_ELIXIR" = "yes" ]; then
  117. MAKE_TARGET="${PROFILE}-elixir-${PKGTYPE}"
  118. else
  119. MAKE_TARGET="${PROFILE}-${PKGTYPE}"
  120. fi
  121. HOST_SYSTEM="$(./scripts/get-distro.sh)"
  122. BUILDER_SYSTEM="$(echo "$BUILDER" | awk -F'-' '{print $NF}')"
  123. CMD_RUN="make ${MAKE_TARGET} && ./scripts/pkg-tests.sh ${MAKE_TARGET}"
  124. IS_NATIVE_SYSTEM='no'
  125. if [[ "$BUILDER_SYSTEM" == "force_host" ]] || [[ "$BUILDER_SYSTEM" == "$HOST_SYSTEM" ]]; then
  126. IS_NATIVE_SYSTEM='yes'
  127. fi
  128. IS_NATIVE_ARCH='no'
  129. if [[ "$NATIVE_ARCH" == "$ARCH" ]]; then
  130. IS_NATIVE_ARCH='yes'
  131. fi
  132. if [[ "${IS_NATIVE_SYSTEM}" == 'yes' && "${IS_NATIVE_ARCH}" == 'yes' ]]; then
  133. export ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal"
  134. eval "$CMD_RUN"
  135. elif docker info; then
  136. if [[ "${IS_NATIVE_ARCH}" == 'no' ]]; then
  137. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  138. fi
  139. docker run -i --rm \
  140. -v "$(pwd)":/emqx \
  141. --workdir /emqx \
  142. --platform="linux/$ARCH" \
  143. --env ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal" \
  144. "$BUILDER" \
  145. bash -euc "git config --global --add safe.directory /emqx && $CMD_RUN"
  146. else
  147. echo "Error: Docker not available on unsupported platform"
  148. exit 1;
  149. fi