build-linux.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. # Arg 1: The tiff package name
  10. TIFF_FOLDER_NAME=$1
  11. # Arg 2: The zlib package name
  12. ZLIB_FOLDER_NAME=$2
  13. # Arg 3: The openssl package name
  14. OPENSSL_FOLDER_NAME=$3
  15. # Determine the host architecture
  16. CURRENT_HOST_ARCH=$(uname -m)
  17. # Use the host architecture if not supplied
  18. TARGET_ARCH=${4:-$(uname -m)}
  19. #
  20. # Make sure docker is installed
  21. #
  22. DOCKER_VERSION=$(docker --version)
  23. if [ $? -ne 0 ]
  24. then
  25. echo "Required package docker is not installed"
  26. echo "Follow instructions on https://docs.docker.com/engine/install/ubuntu/ to install docker properly"
  27. exit 1
  28. fi
  29. echo "Detected Docker Version $DOCKER_VERSION"
  30. #
  31. # Check the target architecture and determine if the necessary cross compilation requirements are met
  32. #
  33. # If the host and target architecture does not match, make sure the necessary cross compilation packages are installed
  34. if [ "${CURRENT_HOST_ARCH}" != ${TARGET_ARCH} ]
  35. then
  36. echo "Checking cross compiling requirements."
  37. for package_check in docker-ce binfmt-support qemu-user-static
  38. do
  39. echo "Checking package $package_check"
  40. dpkg -s $package_check > /dev/null 2>&1
  41. if [ $? -ne 0 ]
  42. then
  43. echo ""
  44. echo "Missing package $package_check. Make sure to install it with your local package manager."
  45. echo ""
  46. exit 1
  47. fi
  48. done
  49. # Only cross compilation of an ARM64 image on an x86_64 host is supported
  50. if [ "${TARGET_ARCH}" = "aarch64" ]
  51. then
  52. # Make sure qemu-aarch64 is installed properly
  53. QEMU_AARCH_COUNT=$(update-binfmts --display | grep qemu-aarch64 | wc -l)
  54. if [ $QEMU_AARCH_COUNT -eq 0 ]
  55. then
  56. echo ""
  57. echo "QEMU aarch64 binary format not registered."
  58. echo "Run the following command to register"
  59. echo ""
  60. echo "sudo docker run --rm --privileged multiarch/qemu-user-static --reset -p yes"
  61. echo ""
  62. exit 1
  63. fi
  64. echo ""
  65. echo "Cross compiling aarch64 on an amd64 machine validated."
  66. echo ""
  67. fi
  68. else
  69. echo "Building ${TARGET_ARCH} natively."
  70. fi
  71. # Setup the docker arguments
  72. if [ "${TARGET_ARCH}" = "x86_64" ]
  73. then
  74. echo "Processing Docker for amd64"
  75. DOCKER_INPUT_ARCHITECTURE=amd64
  76. TARGET_DOCKER_PLATFORM_ARG=linux/amd64
  77. elif [ "${TARGET_ARCH}" = "aarch64" ]
  78. then
  79. echo "Processing Docker for aarch64"
  80. DOCKER_INPUT_ARCHITECTURE=arm64v8
  81. TARGET_DOCKER_PLATFORM_ARG=linux/arm64/v8
  82. else
  83. echo "Unsupported architecture ${TARGET_ARCH}"
  84. exit 1
  85. fi
  86. #
  87. # Prepare the docker file and use the temp folder as the context root
  88. cp docker_build_qt_linux.sh temp/
  89. DOCKERFILE=Dockerfile
  90. DOCKER_IMAGE_NAME_BASE=qt
  91. DOCKER_BUILD_SCRIPT=docker_build_qt_linux.sh
  92. DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME_BASE}_${DOCKER_INPUT_ARCHITECTURE}_3p
  93. UBUNTU_BASE=20.04
  94. echo "Executing docker-based build from the following arguments"
  95. echo " DOCKER_IMAGE_NAME_BASE=${DOCKER_IMAGE_NAME_BASE}"
  96. echo " UBUNTU_BASE=${UBUNTU_BASE}"
  97. echo " DOCKER_BUILD_SCRIPT=${DOCKER_BUILD_SCRIPT}"
  98. echo " TARGET_BUILD_FOLDER=${TARGET_BUILD_FOLDER}"
  99. echo " TARGET_ARCH=${TARGET_ARCH}"
  100. echo " DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME}"
  101. echo ""
  102. # Build the Docker Image
  103. echo "Building the docker build script for ${DOCKER_IMAGE_NAME_BASE} on ${DOCKER_INPUT_ARCHITECTURE} for Ubuntu $1"
  104. echo ""
  105. CMD_DOCKER_BUILD="\
  106. docker build --build-arg INPUT_DOCKER_BUILD_SCRIPT=${DOCKER_BUILD_SCRIPT}\
  107. --build-arg INPUT_ARCHITECTURE=${DOCKER_INPUT_ARCHITECTURE}\
  108. --build-arg INPUT_IMAGE=ubuntu:${UBUNTU_BASE}\
  109. --platform ${TARGET_DOCKER_PLATFORM_ARG}\
  110. -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest temp"
  111. echo ${CMD_DOCKER_BUILD}
  112. eval ${CMD_DOCKER_BUILD}
  113. if [ $? -ne 0 ]
  114. then
  115. echo "Error occurred creating Docker image ${DOCKER_IMAGE_NAME}:latest."
  116. exit 1
  117. fi
  118. # Capture the Docker Image ID
  119. IMAGE_ID=$(docker images -q ${DOCKER_IMAGE_NAME}:latest)
  120. if [ -z $IMAGE_ID ]
  121. then
  122. echo "Error: Cannot find Image ID for ${DOCKER_IMAGE_NAME}"
  123. exit 1
  124. fi
  125. # Run the Docker Image
  126. echo "Running docker build script"
  127. docker run -v $TEMP_FOLDER/src:/data/workspace/src -v $TEMP_FOLDER/$TIFF_FOLDER_NAME:/data/workspace/o3de_tiff -v $TEMP_FOLDER/$ZLIB_FOLDER_NAME:/data/workspace/o3de_zlib -v $TEMP_FOLDER/$OPENSSL_FOLDER_NAME:/data/workspace/o3de_openssl --tty ${DOCKER_IMAGE_NAME}:latest ./docker_build_qt_linux.sh
  128. if [ $? -ne 0 ]
  129. then
  130. echo "Error occurred running Docker image ${DOCKER_IMAGE_NAME}:latest."
  131. exit 1
  132. fi
  133. # Capture the container ID
  134. echo "Capturing the Container ID"
  135. CONTAINER_ID=$(docker container ls -l -q --filter "ancestor=${DOCKER_IMAGE_NAME}:latest")
  136. if [ -z $CONTAINER_ID ]
  137. then
  138. echo "Error: Cannot find Container ID for Image ${DOCKER_IMAGE_NAME}"
  139. exit 1
  140. fi
  141. # Copy the build artifacts from the Docker Container
  142. echo "Copying the built contents from the docker container for image ${DOCKER_IMAGE_NAME}"
  143. docker cp --follow-link $CONTAINER_ID:/data/workspace/qt/. $TARGET_INSTALL_ROOT
  144. if [ $? -ne 0 ]
  145. then
  146. echo "Error occurred copying build artifacts from Docker image ${DOCKER_IMAGE_NAME}:latest."
  147. exit 1
  148. fi
  149. # Clean up the docker image and container
  150. echo "Cleaning up container"
  151. docker container rm $CONTAINER_ID
  152. if [ $? -ne 0 ]
  153. then
  154. echo "Warning: Unable to clean up container for image ${DOCKER_IMAGE_NAME}"
  155. fi
  156. echo "Cleaning up image"
  157. docker rmi --force $IMAGE_ID
  158. if [ $? -ne 0 ]
  159. then
  160. echo "Warning: Unable to clean up image ${DOCKER_IMAGE_NAME}"
  161. fi
  162. popd
  163. exit 0