buildx.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 --builder ghcr.io/emqx/emqx-builder/4.4-4:24.1.5-3-debian10
  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, e.g. emqx, emqx-edge"
  14. echo "--pkgtype tgz|pkg: Specify which package to build, tgz for .tar.gz,"
  15. echo " pkg for .rpm or .deb"
  16. echo "--with-elixir: Specify if the release should be built with Elixir, "
  17. echo " defaults to false."
  18. echo "--arch amd64|arm64: Target arch to build the EMQX package for"
  19. echo "--src_dir <SRC_DIR>: EMQX source ode in this dir, default to PWD"
  20. echo "--builder <BUILDER>: Builder image to pull"
  21. echo " E.g. ghcr.io/emqx/emqx-builder/4.4-4:24.1.5-3-debian10"
  22. echo "--otp <OTP_VSN>: OTP version being used in the builder"
  23. echo "--elixir <ELIXIR_VSN>: Elixir version being used in the builder"
  24. echo "--system <SYSTEM>: OS used in the builder image"
  25. }
  26. while [ "$#" -gt 0 ]; do
  27. case $1 in
  28. -h|--help)
  29. help
  30. exit 0
  31. ;;
  32. --src_dir)
  33. SRC_DIR="$2"
  34. shift 2
  35. ;;
  36. --profile)
  37. PROFILE="$2"
  38. shift 2
  39. ;;
  40. --pkgtype)
  41. PKGTYPE="$2"
  42. shift 2
  43. ;;
  44. --builder)
  45. BUILDER="$2"
  46. shift 2
  47. ;;
  48. --arch)
  49. ARCH="$2"
  50. shift 2
  51. ;;
  52. --otp)
  53. OTP_VSN="$2"
  54. shift 2
  55. ;;
  56. --elixir)
  57. ELIXIR_VSN="$2"
  58. shift 2
  59. ;;
  60. --with-elixir)
  61. WITH_ELIXIR=yes
  62. shift 1
  63. ;;
  64. --system)
  65. SYSTEM="$2"
  66. shift 2
  67. ;;
  68. *)
  69. echo "WARN: Unknown arg (ignored): $1"
  70. shift
  71. continue
  72. ;;
  73. esac
  74. done
  75. if [ -z "${PROFILE:-}" ] ||
  76. [ -z "${PKGTYPE:-}" ] ||
  77. [ -z "${BUILDER:-}" ] ||
  78. [ -z "${ARCH:-}" ] ||
  79. [ -z "${OTP_VSN:-}" ] ||
  80. [ -z "${ELIXIR_VSN:-}" ] ||
  81. [ -z "${SYSTEM:-}" ]; then
  82. help
  83. exit 1
  84. fi
  85. set -x
  86. if [ -z "${WITH_ELIXIR:-}" ]; then
  87. WITH_ELIXIR=no
  88. fi
  89. case "$PKGTYPE" in
  90. tgz|pkg)
  91. true
  92. ;;
  93. *)
  94. echo "Bad --pkgtype option, should be tgz or pkg"
  95. exit 1
  96. ;;
  97. esac
  98. export CODE_PATH="${SRC_DIR:-$PWD}"
  99. cd "${CODE_PATH}"
  100. PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh "$PROFILE")}"
  101. if [ "$WITH_ELIXIR" = "yes" ]
  102. then
  103. PKG_NAME="${PROFILE}-${PKG_VSN}-elixir${ELIXIR_VSN}-otp${OTP_VSN}-${SYSTEM}-${ARCH}"
  104. MAKE_TARGET="${PROFILE}-elixir-${PKGTYPE}"
  105. else
  106. PKG_NAME="${PROFILE}-${PKG_VSN}-otp${OTP_VSN}-${SYSTEM}-${ARCH}"
  107. MAKE_TARGET="${PROFILE}-${PKGTYPE}"
  108. fi
  109. CMD_RUN="export EMQX_NAME=\"$PROFILE\"; make ${MAKE_TARGET} && .ci/build_packages/tests.sh $PKG_NAME $PKGTYPE $ARCH"
  110. if docker info; then
  111. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  112. docker run -i --rm \
  113. -v "$(pwd)":/emqx \
  114. --workdir /emqx \
  115. --platform="linux/$ARCH" \
  116. "$BUILDER" \
  117. bash -euc "$CMD_RUN"
  118. elif [[ $(uname -m) = "x86_64" && "$ARCH" = "amd64" ]]; then
  119. eval "$CMD_RUN"
  120. elif [[ $(uname -m) = "aarch64" && "$ARCH" = "arm64" ]]; then
  121. eval "$CMD_RUN"
  122. elif [[ $(uname -m) = "arm64" && "$ARCH" = "arm64" ]]; then
  123. eval "$CMD_RUN"
  124. elif [[ $(uname -m) = "armv7l" && "$ARCH" = "arm64" ]]; then
  125. eval "$CMD_RUN"
  126. else
  127. echo "Error: Docker not available on unsupported platform"
  128. exit 1;
  129. fi