build-linux.sh 6.9 KB

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