build-linux.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. LIB_NAME=squish_ccr
  10. # Determine the host architecture
  11. CURRENT_HOST_ARCH=$(uname -m)
  12. # Use the host architecture if not supplied
  13. TARGET_ARCH=${1:-$(uname -m)}
  14. # If the host and target architecture does not match, make sure the necessary cross compilation packages are installed
  15. if [ "${CURRENT_HOST_ARCH}" != ${TARGET_ARCH} ]
  16. then
  17. echo "Checking cross compiling requirements."
  18. for package_check in docker-ce binfmt-support qemu-user-static
  19. do
  20. echo "Checking package $package_check"
  21. dpkg -s $package_check > /dev/null 2>&1
  22. if [ $? -ne 0 ]
  23. then
  24. echo ""
  25. echo "Missing package $package_check. Make sure to install it with your local package manager."
  26. echo ""
  27. exit 1
  28. fi
  29. done
  30. # Only cross compilation of an ARM64 image on an x86_64 host is supported
  31. if [ "${TARGET_ARCH}" = "aarch64" ]
  32. then
  33. # Make sure qemu-aarch64 is installed properly
  34. QEMU_AARCH_COUNT=$(update-binfmts --display | grep qemu-aarch64 | wc -l)
  35. if [ $QEMU_AARCH_COUNT -eq 0 ]
  36. then
  37. echo ""
  38. echo "QEMU aarch64 binary format not registered."
  39. echo "Run the following command to register"
  40. echo ""
  41. echo "sudo docker run --rm --privileged multiarch/qemu-user-static --reset -p yes"
  42. echo ""
  43. exit 1
  44. fi
  45. echo ""
  46. echo "Cross compiling aarch64 on an amd64 machine validated."
  47. echo ""
  48. fi
  49. else
  50. echo "Building ${TARGET_ARCH} natively."
  51. fi
  52. # Setup the docker arguments
  53. if [ "${TARGET_ARCH}" = "x86_64" ]
  54. then
  55. echo "Processing Docker for amd64"
  56. DOCKER_INPUT_ARCHITECTURE=amd64
  57. TARGET_DOCKER_PLATFORM_ARG=linux/amd64
  58. elif [ "${TARGET_ARCH}" = "aarch64" ]
  59. then
  60. echo "Processing Docker for aarch64"
  61. DOCKER_INPUT_ARCHITECTURE=arm64v8
  62. TARGET_DOCKER_PLATFORM_ARG=linux/arm64/v8
  63. else
  64. echo "Unsupported architecture ${TARGET_ARCH}"
  65. exit 1
  66. fi
  67. # Make sure docker is installed
  68. DOCKER_VERSION=$(docker --version)
  69. if [ $? -ne 0 ]
  70. then
  71. echo "Required package docker is not installed"
  72. echo "Follow instructions on https://docs.docker.com/engine/install/ubuntu/ to install docker properly"
  73. exit 1
  74. fi
  75. echo "Detected Docker Version $DOCKER_VERSION"
  76. DOCKER_BUILD_SCRIPT=docker_build_linux.sh
  77. if [ ! -f $DOCKER_BUILD_SCRIPT ]
  78. then
  79. echo "Invalid docker build script ${DOCKER_BUILD_SCRIPT}"
  80. exit 1
  81. fi
  82. # Prepare the docker file and use the temp folder as the context root
  83. cp -f ${DOCKER_BUILD_SCRIPT} temp/
  84. pushd temp
  85. # Build the Docker Image
  86. echo "Building the docker build script for ${DOCKER_IMAGE_NAME}"
  87. docker build --build-arg DOCKER_BUILD_SCRIPT=$DOCKER_BUILD_SCRIPT -f ../${TARGET_DOCKER_FILE} -t ${DOCKER_IMAGE_NAME}:latest .
  88. if [ $? -ne 0 ]
  89. then
  90. echo "Error occurred creating Docker image ${DOCKER_IMAGE_NAME}:latest."
  91. exit 1
  92. fi
  93. # Capture the Docker Image ID
  94. IMAGE_ID=$(docker images -q ${DOCKER_IMAGE_NAME}:latest)
  95. if [ -z $IMAGE_ID ]
  96. then
  97. echo "Error: Cannot find Image ID for ${DOCKER_IMAGE_NAME}"
  98. exit 1
  99. fi
  100. # Run the Docker Image
  101. echo "Running build script in the docker image"
  102. docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} --tty ${DOCKER_IMAGE_NAME}:latest /data/workspace/${DOCKER_BUILD_SCRIPT}
  103. if [ $? -ne 0 ]
  104. then
  105. echo Failed to build from docker image ${DOCKER_IMAGE_NAME}:latest
  106. echo "To log into and troubleshoot the docker container, run the following command:"
  107. echo ""
  108. echo "docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} -it --tty ${DOCKER_IMAGE_NAME}:latest"
  109. echo ""
  110. exit 1
  111. fi
  112. # Capture the container ID
  113. echo "Capturing the Container ID"
  114. CONTAINER_ID=$(docker container ls -l -q --filter "ancestor=${DOCKER_IMAGE_NAME}:latest")
  115. if [ -z $CONTAINER_ID ]
  116. then
  117. echo "Error: Cannot find Container ID for Image ${DOCKER_IMAGE_NAME}"
  118. exit 1
  119. fi
  120. # Copy the build artifacts from the Docker Container
  121. echo "Copying the built contents from the docker container for image ${DOCKER_IMAGE_NAME}"
  122. mkdir -p build
  123. docker cp $CONTAINER_ID:/data/workspace/package/. build
  124. if [ $? -ne 0 ]
  125. then
  126. echo "Error occurred copying build artifacts from Docker container ($CONTAINER_ID)"
  127. exit 1
  128. fi
  129. # Clean up the docker image and container
  130. echo "Cleaning up container"
  131. docker container rm $CONTAINER_ID || (echo "Warning: unable to clean up container for image ${DOCKER_IMAGE_NAME}")
  132. echo "Cleaning up image"
  133. docker rmi --force $IMAGE_ID || (echo "Warning: unable to clean up image ${DOCKER_IMAGE_NAME}")
  134. popd
  135. exit 0