build-linux.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. #
  3. # Copyright (c) Contributors to the Open 3D Engine Project.
  4. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR MIT
  7. #
  8. # This script will utilize Docker to build on either AMD64 or AARCH64 architectures. The script will
  9. # also build on both Ubuntu based docker images
  10. DOCKER_BUILD_SCRIPT=docker_build_aws_sdk.sh
  11. TARGET_BUILD_FOLDER=build
  12. #
  13. # Collect the required arguments for this ubuntu docker-base build script
  14. #
  15. # Get the base docker image name
  16. DOCKER_IMAGE_NAME_BASE=$1
  17. if [ "${DOCKER_IMAGE_NAME_BASE}" == "" ]
  18. then
  19. echo "Missing argument 1: Docker image name for this this process"
  20. exit 1
  21. fi
  22. # Get the ubuntu base version (16.04|18.04|20.04|22.04)
  23. UBUNTU_BASE=$2
  24. if [ "${UBUNTU_BASE}" == "" ]
  25. then
  26. echo "Missing argument 2: Ubuntu docker tag"
  27. exit 1
  28. fi
  29. # Get the dependent curl git tag to pull the dependent library source
  30. DEP_CURL_GIT_TAG=$3
  31. if [ "${DEP_CURL_GIT_TAG}" == "" ]
  32. then
  33. echo "Missing argument 3: dependent curl git tag"
  34. exit 1
  35. fi
  36. # Determine the host architecture
  37. CURRENT_HOST_ARCH=$(uname -m)
  38. # Use the host architecture if not supplied
  39. TARGET_ARCH=${4:-$(uname -m)}
  40. # Recompute the DOWNLOADED_PACKAGE_FOLDERS to apply to $WORKSPACE/temp inside the Docker script
  41. DEP_PACKAGES_FOLDERNAMES_ONLY=${DOWNLOADED_PACKAGE_FOLDERS//$TEMP_FOLDER\//}
  42. DEP_PACKAGES_DOCKER_FOLDERNAMES=${DOWNLOADED_PACKAGE_FOLDERS//$TEMP_FOLDER/"/data/workspace/temp"}
  43. echo "Executing docker-based build from the following arguments"
  44. echo " DOCKER_IMAGE_NAME_BASE = ${DOCKER_IMAGE_NAME_BASE}"
  45. echo " UBUNTU_BASE = ${UBUNTU_BASE}"
  46. echo " DOCKER_BUILD_SCRIPT = ${DOCKER_BUILD_SCRIPT}"
  47. echo " TARGET_BUILD_FOLDER = ${TARGET_BUILD_FOLDER}"
  48. echo " TARGET_ARCH = ${TARGET_ARCH}"
  49. echo " TEMP_FOLDER = ${TEMP_FOLDER}"
  50. echo " DOWNLOADED_PACKAGE_FOLDERS = ${DEP_PACKAGES_FOLDERNAMES_ONLY}"
  51. #
  52. # Make sure docker is installed
  53. #
  54. DOCKER_VERSION=$(docker --version)
  55. if [ $? -ne 0 ]
  56. then
  57. echo "Required package docker is not installed"
  58. echo "Follow instructions on https://docs.docker.com/engine/install/ubuntu/ to install docker properly"
  59. exit 1
  60. fi
  61. echo "Detected Docker Version $DOCKER_VERSION"
  62. #
  63. # Check the target architecture and determine if the necessary cross compilation requirements are met
  64. #
  65. # If the host and target architecture does not match, make sure the necessary cross compilation packages are installed
  66. if [ "${CURRENT_HOST_ARCH}" != ${TARGET_ARCH} ]
  67. then
  68. echo "Checking cross compiling requirements."
  69. for package_check in docker-ce qemu binfmt-support qemu-user-static
  70. do
  71. echo "Checking package $package_check"
  72. dpkg -s $package_check > /dev/null 2>&1
  73. if [ $? -ne 0 ]
  74. then
  75. echo ""
  76. echo "Missing package $package_check. Make sure to install it with your local package manager."
  77. echo ""
  78. exit 1
  79. fi
  80. done
  81. # Only cross compilation of an ARM64 image on an x86_64 host is supported
  82. if [ "${TARGET_ARCH}" = "aarch64" ]
  83. then
  84. # Make sure qemu-aarch64 is installed properly
  85. QEMU_AARCH_COUNT=$(update-binfmts --display | grep qemu-aarch64 | wc -l)
  86. if [ $QEMU_AARCH_COUNT -eq 0 ]
  87. then
  88. echo ""
  89. echo "QEMU aarch64 binary format not registered."
  90. echo "Run the following command to register"
  91. echo ""
  92. echo "sudo docker run --rm --privileged multiarch/qemu-user-static --reset -p yes"
  93. echo ""
  94. exit 1
  95. fi
  96. echo ""
  97. echo "Cross compiling aarch64 on an amd64 machine validated."
  98. echo ""
  99. fi
  100. else
  101. echo "Building ${TARGET_ARCH} natively."
  102. fi
  103. # Setup the docker arguments
  104. if [ "${TARGET_ARCH}" = "x86_64" ]
  105. then
  106. echo "Processing Docker for amd64"
  107. DOCKER_INPUT_ARCHITECTURE=amd64
  108. TARGET_DOCKER_PLATFORM_ARG=linux/amd64
  109. elif [ "${TARGET_ARCH}" = "aarch64" ]
  110. then
  111. echo "Processing Docker for aarch64"
  112. DOCKER_INPUT_ARCHITECTURE=arm64v8
  113. TARGET_DOCKER_PLATFORM_ARG=linux/arm64/v8
  114. else
  115. echo "Unsupported architecture ${TARGET_ARCH}"
  116. exit 1
  117. fi
  118. #
  119. # Prepare the docker base context based on ${TEMP_FOLDER}
  120. mkdir -p ${TEMP_FOLDER}
  121. cp -f ${DOCKER_BUILD_SCRIPT} ${TEMP_FOLDER}/
  122. echo "Building on ubuntu public.ecr.aws/ubuntu/ubuntu:${UBUNTU_BASE}"
  123. # Build the Docker Image
  124. DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME_BASE}_${DOCKER_INPUT_ARCHITECTURE}_3p
  125. echo DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME}
  126. echo "Building the docker build script for ${DOCKER_IMAGE_NAME_BASE} on ${DOCKER_INPUT_ARCHITECTURE} for Ubuntu $1"
  127. echo ""
  128. CMD_DOCKER_BUILD="docker build --build-arg INPUT_DOCKER_BUILD_SCRIPT=${DOCKER_BUILD_SCRIPT}\
  129. --build-arg INPUT_ARCHITECTURE=${DOCKER_INPUT_ARCHITECTURE} \
  130. --build-arg INPUT_IMAGE=ubuntu:${UBUNTU_BASE} \
  131. --build-arg INPUT_DEPENDENT_PACKAGE_FOLDERS=\"${DEP_PACKAGES_DOCKER_FOLDERNAMES}\" \
  132. --build-arg INPUT_DEP_CURL_GIT_TAG=\"${DEP_CURL_GIT_TAG}\" \
  133. -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest temp"
  134. echo ${CMD_DOCKER_BUILD}
  135. eval ${CMD_DOCKER_BUILD}
  136. if [ $? -ne 0 ]
  137. then
  138. echo "Error occurred creating Docker image ${DOCKER_IMAGE_NAME}:latest."
  139. exit 1
  140. fi
  141. # Prepare the target build folder to copy from the docker container on successful run of the docker script
  142. INSTALL_PACKAGE_PATH=${TEMP_FOLDER}/${TARGET_BUILD_FOLDER}/
  143. # Run the build script in the docker image
  144. echo "Running build script in the docker image ${DOCKER_IMAGE_NAME}"
  145. echo ""
  146. CMD_DOCKER_RUN="docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} \
  147. --tty \
  148. -v ${TEMP_FOLDER}:/data/workspace/temp:ro \
  149. ${DOCKER_IMAGE_NAME}:latest /data/workspace/${DOCKER_BUILD_SCRIPT}"
  150. echo ${CMD_DOCKER_RUN}
  151. eval ${CMD_DOCKER_RUN}
  152. if [ $? -ne 0 ]
  153. then
  154. echo Failed to build from docker image ${DOCKER_IMAGE_NAME}:latest
  155. echo "To log into and troubleshoot the docker container, run the following command:"
  156. echo ""
  157. echo "docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} -it --tty -v ${TEMP_FOLDER}:/data/workspace/temp:ro ${DOCKER_IMAGE_NAME}:latest"
  158. echo ""
  159. exit 1
  160. fi
  161. echo "Build Complete"
  162. # Copy the build artifacts from the docker image
  163. # Capture the Docker Image ID
  164. IMAGE_ID=$(docker images -q ${DOCKER_IMAGE_NAME}:latest)
  165. if [ -z $IMAGE_ID ]
  166. then
  167. echo "Error: Cannot find Image ID for ${DOCKER_IMAGE_NAME}"
  168. exit 1
  169. fi
  170. # Capture the container ID
  171. echo "Capturing the Container ID"
  172. CONTAINER_ID=$(docker container ls -l -q --filter "ancestor=${DOCKER_IMAGE_NAME}:latest")
  173. if [ -z $CONTAINER_ID ]
  174. then
  175. echo "Error: Cannot find Container ID for Image ${DOCKER_IMAGE_NAME}"
  176. exit 1
  177. fi
  178. DOCKER_BUILD_ROOT=/data/workspace/${TARGET_BUILD_FOLDER}
  179. rm -rf ${INSTALL_PACKAGE_PATH}
  180. mkdir -p ${INSTALL_PACKAGE_PATH}
  181. echo "Copying from $CONTAINER_ID:${DOCKER_BUILD_ROOT} to ${TEMP_FOLDER}/"
  182. docker cp $CONTAINER_ID:${DOCKER_BUILD_ROOT} ${TEMP_FOLDER}/
  183. if [ $? -ne 0 ]
  184. then
  185. echo "Error copying build from docker image ${DOCKER_IMAGE_NAME}:latest."
  186. exit 1
  187. fi
  188. echo "Built ${DOCKER_IMAGE_NAME_BASE} into ${INSTALL_PACKAGE_PATH} successfully"
  189. exit 0