build-linux.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 20.04 (focal) and Ubuntu 22.04 (jammy) systems because of the dependencies
  10. # on OpenSSL 1.1.1 and Open 3.0 respectively
  11. DOCKER_IMAGE_NAME_BASE=aws_gamelift_server_sdk
  12. DOCKER_BUILD_SCRIPT=docker_build_linux.sh
  13. # Determine the host architecture
  14. CURRENT_HOST_ARCH=$(uname -m)
  15. # Get the path within the source tarball to the root of the source folder for the build
  16. SDK_SRC_SUBPATH=${1:-.}
  17. # Use the host architecture if not supplied
  18. TARGET_ARCH=${2:-$(uname -m)}
  19. # Prepare the target install path
  20. INSTALL_PACKAGE_PATH=${TEMP_FOLDER}/install/
  21. # If the host and target architecture does not match, make sure the necessary cross compilation packages are installed
  22. if [ "${CURRENT_HOST_ARCH}" != ${TARGET_ARCH} ]
  23. then
  24. echo "Checking cross compiling requirements."
  25. for package_check in docker-ce qemu binfmt-support qemu-user-static
  26. do
  27. echo "Checking package $package_check"
  28. dpkg -s $package_check > /dev/null 2>&1
  29. if [ $? -ne 0 ]
  30. then
  31. echo ""
  32. echo "Missing package $package_check. Make sure to install it with your local package manager."
  33. echo ""
  34. exit 1
  35. fi
  36. done
  37. # Only cross compilation of an ARM64 image on an x86_64 host is supported
  38. if [ "${TARGET_ARCH}" = "aarch64" ]
  39. then
  40. # Make sure qemu-aarch64 is installed properly
  41. QEMU_AARCH_COUNT=$(update-binfmts --display | grep qemu-aarch64 | wc -l)
  42. if [ $QEMU_AARCH_COUNT -eq 0 ]
  43. then
  44. echo ""
  45. echo "QEMU aarch64 binary format not registered."
  46. echo "Run the following command to register"
  47. echo ""
  48. echo "sudo docker run --rm --privileged multiarch/qemu-user-static --reset -p yes"
  49. echo ""
  50. exit 1
  51. fi
  52. echo ""
  53. echo "Cross compiling aarch64 on an amd64 machine validated."
  54. echo ""
  55. fi
  56. else
  57. echo "Building ${TARGET_ARCH} natively."
  58. fi
  59. # Make sure docker is installed
  60. DOCKER_VERSION=$(docker --version)
  61. if [ $? -ne 0 ]
  62. then
  63. echo "Required package docker is not installed"
  64. echo "Follow instructions on https://docs.docker.com/engine/install/ubuntu/ to install docker properly"
  65. exit 1
  66. fi
  67. echo "Detected Docker Version $DOCKER_VERSION"
  68. # Setup the docker arguments
  69. if [ "${TARGET_ARCH}" = "x86_64" ]
  70. then
  71. echo "Processing Docker for amd64"
  72. DOCKER_INPUT_ARCHITECTURE=amd64
  73. TARGET_DOCKER_PLATFORM_ARG=linux/amd64
  74. DOCKER_BUILD_ARG=1
  75. elif [ "${TARGET_ARCH}" = "aarch64" ]
  76. then
  77. echo "Processing Docker for aarch64"
  78. DOCKER_INPUT_ARCHITECTURE=arm64v8
  79. TARGET_DOCKER_PLATFORM_ARG=linux/arm64/v8
  80. DOCKER_BUILD_ARG=3
  81. else
  82. echo "Unsupported architecture ${TARGET_ARCH}"
  83. exit 1
  84. fi
  85. # Prepare to build on both Ubuntu 20.04 and Ubuntu 22.04 based docker images
  86. mkdir -p ${TEMP_FOLDER}
  87. cp -f ${DOCKER_BUILD_SCRIPT} ${TEMP_FOLDER}/
  88. # Args
  89. # $1 : Ubuntu version
  90. # $2 : Include
  91. # $3 : Docker run platform
  92. function execute_docker() {
  93. # Determine the openssl version based on the ubuntu version (20.04/OpenSSL 1.1.1.x vs 22.04/OpenSSL 3.x)
  94. if [ $1 = "20.04" ]
  95. then
  96. echo "Preparing for OpenSSL 1.1.1.x version"
  97. BIN_SUBFOLDER_NAME=openssl-1
  98. elif [ $1 = "22.04" ]
  99. then
  100. echo "Preparing for OpenSSL 3.x version"
  101. BIN_SUBFOLDER_NAME=openssl-3
  102. else
  103. echo "Unsupported base build image ubuntu version ${1}"
  104. exit 1
  105. fi
  106. # Build the Docker Image
  107. DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME_BASE}_focal_${DOCKER_INPUT_ARCHITECTURE}_3p
  108. echo "Building the docker build script for ${DOCKER_IMAGE_NAME_BASE} on ${DOCKER_INPUT_ARCHITECTURE} for Ubuntu $1"
  109. echo ""
  110. echo docker build --build-arg INPUT_DOCKER_BUILD_SCRIPT=${DOCKER_BUILD_SCRIPT} \
  111. --build-arg INPUT_ARCHITECTURE=${DOCKER_INPUT_ARCHITECTURE} \
  112. --build-arg INPUT_IMAGE=ubuntu:${1} \
  113. -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest temp
  114. docker build --build-arg INPUT_DOCKER_BUILD_SCRIPT=${DOCKER_BUILD_SCRIPT} \
  115. --build-arg INPUT_ARCHITECTURE=${DOCKER_INPUT_ARCHITECTURE} \
  116. --build-arg INPUT_IMAGE=ubuntu:${1} \
  117. -f Dockerfile -t ${DOCKER_IMAGE_NAME}:latest temp
  118. if [ $? -ne 0 ]
  119. then
  120. echo "Error occurred creating Docker image ${DOCKER_IMAGE_NAME}:latest."
  121. exit 1
  122. fi
  123. # Run the build script in the docker image
  124. echo "Running build script in the docker image ${DOCKER_IMAGE_NAME}"
  125. echo ""
  126. echo docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} \
  127. -it --tty \
  128. ${DOCKER_IMAGE_NAME}:latest /data/workspace/${DOCKER_BUILD_SCRIPT} ${SDK_SRC_SUBPATH}
  129. docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} \
  130. --tty \
  131. ${DOCKER_IMAGE_NAME}:latest /data/workspace/${DOCKER_BUILD_SCRIPT} ${SDK_SRC_SUBPATH}
  132. if [ $? -ne 0 ]
  133. then
  134. echo Failed to build from docker image ${DOCKER_IMAGE_NAME}:latest
  135. echo "To log into and troubleshoot the docker container, run the following command:"
  136. echo ""
  137. echo "docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} -it --tty ${DOCKER_IMAGE_NAME}:latest"
  138. echo ""
  139. exit 1
  140. fi
  141. echo "Build Complete"
  142. # Copy the build artifacts from the docker image
  143. # Capture the Docker Image ID
  144. IMAGE_ID=$(docker images -q ${DOCKER_IMAGE_NAME}:latest)
  145. if [ -z $IMAGE_ID ]
  146. then
  147. echo "Error: Cannot find Image ID for ${DOCKER_IMAGE_NAME}"
  148. exit 1
  149. fi
  150. # Capture the container ID
  151. echo "Capturing the Container ID"
  152. CONTAINER_ID=$(docker container ls -l -q --filter "ancestor=${DOCKER_IMAGE_NAME}:latest")
  153. if [ -z $CONTAINER_ID ]
  154. then
  155. echo "Error: Cannot find Container ID for Image ${DOCKER_IMAGE_NAME}"
  156. exit 1
  157. fi
  158. DOCKER_BUILD_ROOT=/data/workspace/build/
  159. if [ ! -f ${INSTALL_PACKAGE_PATH}/include ]
  160. then
  161. docker cp $CONTAINER_ID:${DOCKER_BUILD_ROOT}/build_static/prefix/include ${INSTALL_PACKAGE_PATH}/
  162. fi
  163. if [ ! -f ${INSTALL_PACKAGE_PATH}/cmake ]
  164. then
  165. docker cp $CONTAINER_ID:${DOCKER_BUILD_ROOT}/build_static/prefix/cmake ${INSTALL_PACKAGE_PATH}/
  166. fi
  167. docker cp $CONTAINER_ID:${DOCKER_BUILD_ROOT}/build_static/prefix/lib ${INSTALL_PACKAGE_PATH}/lib/${BIN_SUBFOLDER_NAME}
  168. docker cp $CONTAINER_ID:${DOCKER_BUILD_ROOT}/build_shared/prefix/lib ${INSTALL_PACKAGE_PATH}/bin/${BIN_SUBFOLDER_NAME}
  169. }
  170. rm -rf ${INSTALL_PACKAGE_PATH}
  171. mkdir -p ${INSTALL_PACKAGE_PATH}
  172. mkdir -p ${INSTALL_PACKAGE_PATH}/lib
  173. mkdir -p ${INSTALL_PACKAGE_PATH}/bin
  174. # Build for Ubuntu 20.04
  175. execute_docker 20.04
  176. # Build for Ubuntu 22.04
  177. execute_docker 22.04
  178. echo "Build successful"
  179. exit 0