build-linux.sh 6.3 KB

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