buildx.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-8:1.13.3-24.2.1-1-debian10
  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-edge|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 ode 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-8:1.13.3-24.2.1-1-debian10"
  23. echo "--otp <OTP_VSN>: OTP version being used in the builder"
  24. echo "--elixir-vsn <ELIXIR_VSN>: Elixir version being used in the builder"
  25. echo "--system <SYSTEM>: OS used in the builder image"
  26. }
  27. while [ "$#" -gt 0 ]; do
  28. case $1 in
  29. -h|--help)
  30. help
  31. exit 0
  32. ;;
  33. --src_dir)
  34. SRC_DIR="$2"
  35. shift 2
  36. ;;
  37. --profile)
  38. PROFILE="$2"
  39. shift 2
  40. ;;
  41. --pkgtype)
  42. PKGTYPE="$2"
  43. shift 2
  44. ;;
  45. --builder)
  46. BUILDER="$2"
  47. shift 2
  48. ;;
  49. --arch)
  50. ARCH="$2"
  51. shift 2
  52. ;;
  53. --otp)
  54. OTP_VSN="$2"
  55. shift 2
  56. ;;
  57. --elixir-vsn)
  58. ELIXIR_VSN="$2"
  59. shift 2
  60. ;;
  61. --elixir)
  62. shift 1
  63. case ${1:-novalue} in
  64. -*)
  65. # another option
  66. IS_ELIXIR='yes'
  67. ;;
  68. yes|no)
  69. IS_ELIXIR="${1}"
  70. shift 1
  71. ;;
  72. novalue)
  73. IS_ELIXIR='yes'
  74. ;;
  75. *)
  76. echo "ERROR: unknown option: --elixir $2"
  77. exit 1
  78. ;;
  79. esac
  80. ;;
  81. --system)
  82. SYSTEM="$2"
  83. shift 2
  84. ;;
  85. *)
  86. echo "WARN: Unknown arg (ignored): $1"
  87. shift
  88. continue
  89. ;;
  90. esac
  91. done
  92. if [ -z "${PROFILE:-}" ] ||
  93. [ -z "${PKGTYPE:-}" ] ||
  94. [ -z "${BUILDER:-}" ] ||
  95. [ -z "${ARCH:-}" ] ||
  96. [ -z "${OTP_VSN:-}" ] ||
  97. [ -z "${ELIXIR_VSN:-}" ] ||
  98. [ -z "${SYSTEM:-}" ]; then
  99. help
  100. exit 1
  101. fi
  102. # ensure dir
  103. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
  104. set -x
  105. if [ -z "${IS_ELIXIR:-}" ]; then
  106. IS_ELIXIR=no
  107. fi
  108. case "$PKGTYPE" in
  109. tgz|pkg)
  110. true
  111. ;;
  112. *)
  113. echo "Bad --pkgtype option, should be tgz or pkg"
  114. exit 1
  115. ;;
  116. esac
  117. export CODE_PATH="${SRC_DIR:-$PWD}"
  118. cd "${CODE_PATH}"
  119. if [ "$IS_ELIXIR" = "yes" ]; then
  120. MAKE_TARGET="${PROFILE}-elixir-${PKGTYPE}"
  121. else
  122. MAKE_TARGET="${PROFILE}-${PKGTYPE}"
  123. fi
  124. CMD_RUN="make ${MAKE_TARGET} && ./scripts/pkg-tests.sh ${MAKE_TARGET}"
  125. if [[ $(uname -m) = "x86_64" && "$ARCH" = "amd64" ]]; then
  126. eval "$CMD_RUN"
  127. elif [[ $(uname -m) = "aarch64" && "$ARCH" = "arm64" ]]; then
  128. eval "$CMD_RUN"
  129. elif [[ $(uname -m) = "arm64" && "$ARCH" = "arm64" ]]; then
  130. eval "$CMD_RUN"
  131. elif [[ $(uname -m) = "armv7l" && "$ARCH" = "arm64" ]]; then
  132. eval "$CMD_RUN"
  133. elif docker info; then
  134. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  135. docker run -i --rm \
  136. -v "$(pwd)":/emqx \
  137. --workdir /emqx \
  138. --platform="linux/$ARCH" \
  139. "$BUILDER" \
  140. bash -euc "$CMD_RUN"
  141. else
  142. echo "Error: Docker not available on unsupported platform"
  143. exit 1;
  144. fi