buildx.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. set -x
  11. help() {
  12. echo
  13. echo "-h|--help: To display this usage information"
  14. echo "--profile <PROFILE>: EMQ X profile to build, e.g. emqx, emqx-edge"
  15. echo "--pkgtype tgz|pkg: Specify which package to build, tgz for .tar.gz,"
  16. echo " pkg for .rpm or .deb"
  17. echo "--with-elixir: Specify if the release should be built with Elixir, "
  18. echo " defaults to false."
  19. echo "--arch amd64|arm64: Target arch to build the EMQ X package for"
  20. echo "--src_dir <SRC_DIR>: EMQ X 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/4.4-4:24.1.5-3-debian10"
  23. echo "--otp <OTP_VSN>: OTP version being used in the builder"
  24. echo "--elixir <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)
  58. ELIXIR_VSN="$2"
  59. shift 2
  60. ;;
  61. --with-elixir)
  62. WITH_ELIXIR=yes
  63. shift 1
  64. ;;
  65. --system)
  66. SYSTEM="$2"
  67. shift 2
  68. ;;
  69. *)
  70. echo "WARN: Unknown arg (ignored): $1"
  71. shift
  72. continue
  73. ;;
  74. esac
  75. done
  76. if [ -z "${PROFILE:-}" ] ||
  77. [ -z "${PKGTYPE:-}" ] ||
  78. [ -z "${BUILDER:-}" ] ||
  79. [ -z "${ARCH:-}" ] ||
  80. [ -z "${OTP_VSN:-}" ] ||
  81. [ -z "${ELIXIR_VSN:-}" ] ||
  82. [ -z "${SYSTEM:-}" ]; then
  83. help
  84. exit 1
  85. fi
  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