buildx.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  9. set -euo pipefail
  10. help() {
  11. echo
  12. echo "-h|--help: To display this usage information"
  13. echo "--profile <PROFILE>: EMQX profile to build (emqx|emqx-enterprise)"
  14. echo "--pkgtype tgz|pkg|rel: Specify which package to build, tgz for .tar.gz,"
  15. echo " pkg for .rpm or .deb, rel for release only"
  16. echo "--elixir: Specify if the release should be built with Elixir, "
  17. echo " defaults to 'no'."
  18. echo "--arch amd64|arm64: Target arch to build the EMQX package for"
  19. echo "--src_dir <SRC_DIR>: EMQX source code in this dir, default to PWD"
  20. echo "--builder <BUILDER>: Docker image to use for building"
  21. echo " E.g. ghcr.io/emqx/emqx-builder/5.3-8:1.15.7-26.2.5-2-debian12"
  22. }
  23. die() {
  24. msg="$1"
  25. echo "$msg" >&2
  26. help
  27. exit 1
  28. }
  29. # ensure dir
  30. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
  31. # shellcheck disable=SC1091
  32. source ./env.sh
  33. while [ "$#" -gt 0 ]; do
  34. case $1 in
  35. -h|--help)
  36. help
  37. exit 0
  38. ;;
  39. --src_dir)
  40. SRC_DIR="$2"
  41. shift 2
  42. ;;
  43. --profile)
  44. PROFILE="$2"
  45. shift 2
  46. ;;
  47. --pkgtype)
  48. PKGTYPE="$2"
  49. shift 2
  50. ;;
  51. --builder)
  52. EMQX_BUILDER="$2"
  53. shift 2
  54. ;;
  55. --arch)
  56. ARCH="$2"
  57. shift 2
  58. ;;
  59. --elixir)
  60. shift 1
  61. case ${1:-novalue} in
  62. -*)
  63. # another option
  64. IS_ELIXIR='yes'
  65. ;;
  66. yes|no)
  67. IS_ELIXIR="${1}"
  68. shift 1
  69. ;;
  70. novalue)
  71. IS_ELIXIR='yes'
  72. ;;
  73. *)
  74. echo "ERROR: unknown option: --elixir $2"
  75. exit 1
  76. ;;
  77. esac
  78. ;;
  79. *)
  80. echo "WARN: Unknown arg (ignored): $1"
  81. shift
  82. continue
  83. ;;
  84. esac
  85. done
  86. ## we have a different naming for them
  87. if [[ $(uname -m) == "x86_64" ]]; then
  88. NATIVE_ARCH='amd64'
  89. elif [[ $(uname -m) == "aarch64" ]]; then
  90. NATIVE_ARCH='arm64'
  91. elif [[ $(uname -m) == "arm64" ]]; then
  92. NATIVE_ARCH='arm64'
  93. elif [[ $(uname -m) == "armv7l" ]]; then
  94. # CHECKME: really ?
  95. NATIVE_ARCH='arm64'
  96. fi
  97. ARCH="${ARCH:-${NATIVE_ARCH:-}}"
  98. [ -z "${PROFILE:-}" ] && die "missing --profile"
  99. [ -z "${PKGTYPE:-}" ] && die "missing --pkgtype"
  100. [ -z "${EMQX_BUILDER:-}" ] && die "missing --builder"
  101. [ -z "${ARCH:-}" ] && die "missing --arch"
  102. set -x
  103. if [ -z "${IS_ELIXIR:-}" ]; then
  104. IS_ELIXIR=no
  105. fi
  106. case "${PKGTYPE:-}" in
  107. tgz|pkg|rel)
  108. true
  109. ;;
  110. *)
  111. echo "Bad --pkgtype option, should be tgz, pkg or rel"
  112. exit 1
  113. ;;
  114. esac
  115. export CODE_PATH="${SRC_DIR:-$PWD}"
  116. cd "${CODE_PATH}"
  117. if [ "$IS_ELIXIR" = "yes" ]; then
  118. MAKE_TARGET="${PROFILE}-elixir-${PKGTYPE}"
  119. else
  120. MAKE_TARGET="${PROFILE}-${PKGTYPE}"
  121. fi
  122. HOST_SYSTEM="$(./scripts/get-distro.sh)"
  123. BUILDER_SYSTEM="${BUILDER_SYSTEM:-$(echo "$EMQX_BUILDER" | awk -F'-' '{print $NF}')}"
  124. if [ "${PKGTYPE}" != 'rel' ]; then
  125. CMD_RUN="make ${MAKE_TARGET} && ./scripts/pkg-tests.sh ${MAKE_TARGET}"
  126. else
  127. CMD_RUN="make ${MAKE_TARGET}"
  128. fi
  129. IS_NATIVE_SYSTEM='no'
  130. if [[ "$BUILDER_SYSTEM" != "force_docker" ]]; then
  131. if [[ "$BUILDER_SYSTEM" == "force_host" ]] || [[ "$BUILDER_SYSTEM" == "$HOST_SYSTEM" ]]; then
  132. IS_NATIVE_SYSTEM='yes'
  133. fi
  134. fi
  135. IS_NATIVE_ARCH='no'
  136. if [[ "$NATIVE_ARCH" == "$ARCH" ]]; then
  137. IS_NATIVE_ARCH='yes'
  138. fi
  139. if [[ "${IS_NATIVE_SYSTEM}" == 'yes' && "${IS_NATIVE_ARCH}" == 'yes' ]]; then
  140. export ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal"
  141. eval "$CMD_RUN"
  142. elif docker info; then
  143. if [[ "${IS_NATIVE_ARCH}" == 'no' ]]; then
  144. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  145. fi
  146. docker run -i --rm \
  147. -v "$(pwd)":/emqx \
  148. --workdir /emqx \
  149. --platform="linux/$ARCH" \
  150. --env ACLOCAL_PATH="/usr/share/aclocal:/usr/local/share/aclocal" \
  151. "$EMQX_BUILDER" \
  152. bash -euc "git config --global --add safe.directory /emqx && $CMD_RUN"
  153. else
  154. echo "Error: Docker not available on unsupported platform"
  155. exit 1;
  156. fi