build-archlinux.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # Determine the host architecture
  10. CURRENT_HOST_ARCH=$(uname -m)
  11. # Use the host architecture if not supplied
  12. TARGET_ARCH=${1:-$(uname -m)}
  13. # If the host and target architecture does not match, make sure the necessary cross compilation packages are installed
  14. if [ "${CURRENT_HOST_ARCH}" != ${TARGET_ARCH} ]
  15. then
  16. pkg_list=(docker docker-buildx docker-compose qemu qemu-user-static-binfmt)
  17. echo "Checking cross compiling requirements."
  18. for package_check in "${pkg_list[@]}"
  19. do
  20. echo "Checking package $package_check"
  21. pacman -Qi $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-system-aarch64 is installed properly
  34. pacman -Qi qemu-system-aarch64 > /dev/null 2>&1
  35. if [ $? -ne 0 ]
  36. then
  37. echo ""
  38. echo "qemu-system-aarch64 package needs to be installed."
  39. echo "Use pacman to install it"
  40. exit 1
  41. fi
  42. echo ""
  43. echo "Cross compiling aarch64 on an amd64 machine validated."
  44. echo ""
  45. fi
  46. else
  47. echo "Building ${TARGET_ARCH} natively."
  48. fi
  49. # Set the library name prefix to lz4
  50. LIB_NAME="lz4"
  51. # Setup the docker arguments for the target architecture
  52. if [ "${TARGET_ARCH}" = "x86_64" ]
  53. then
  54. echo "Processing Docker for x86_64"
  55. TARGET_DOCKER_FILE=Dockerfile.x86_64
  56. TARGET_DOCKER_PLATFORM_ARG=linux/amd64
  57. DOCKER_IMAGE_NAME=${LIB_NAME}_linux_3p
  58. elif [ "${TARGET_ARCH}" = "aarch64" ]
  59. then
  60. echo "Processing Docker for aarch64"
  61. TARGET_DOCKER_FILE=Dockerfile.aarch64
  62. TARGET_DOCKER_PLATFORM_ARG=linux/arm64v8
  63. DOCKER_IMAGE_NAME=${LIB_NAME}_linux_aarch64_3p
  64. else
  65. echo "Unsupported architecture ${TARGET_ARCH}"
  66. exit 1
  67. fi
  68. # Make sure docker is installed
  69. DOCKER_VERSION=$(docker --version)
  70. if [ $? -ne 0 ]
  71. then
  72. echo "Required package docker is not installed"
  73. echo "docker can installed using the 'docker' package"
  74. exit 1
  75. fi
  76. echo "Detected Docker Version $DOCKER_VERSION"
  77. DOCKER_BUILD_SCRIPT=docker_build_${LIB_NAME}_linux.sh
  78. if [ ! -f $DOCKER_BUILD_SCRIPT ]
  79. then
  80. echo "Invalid docker build script ${DOCKER_BUILD_SCRIPT}"
  81. exit 1
  82. fi
  83. # Prepare the docker file and use the temp folder as the context root
  84. cp -f ${DOCKER_BUILD_SCRIPT} temp/
  85. pushd temp
  86. # Build the Docker Image
  87. echo "Building the docker build script for ${DOCKER_IMAGE_NAME}"
  88. docker buildx build --platform ${TARGET_DOCKER_PLATFORM_ARG} --build-arg DOCKER_BUILD_SCRIPT=$DOCKER_BUILD_SCRIPT -f ${TEMP_FOLDER}/../${TARGET_DOCKER_FILE} -t ${DOCKER_IMAGE_NAME}:latest ${TEMP_FOLDER}
  89. if [ $? -ne 0 ]
  90. then
  91. echo "Error occurred creating Docker image ${DOCKER_IMAGE_NAME}:latest."
  92. echo "The following command failed:"
  93. echo ""
  94. echo "docker buildx build --platform ${TARGET_DOCKER_PLATFORM_ARG} --build-arg DOCKER_BUILD_SCRIPT=$DOCKER_BUILD_SCRIPT -f ${TEMP_FOLDER}/../${TARGET_DOCKER_FILE} -t ${DOCKER_IMAGE_NAME}:latest ${TEMP_FOLDER}"
  95. echo ""
  96. exit 1
  97. fi
  98. # Capture the Docker Image ID
  99. IMAGE_ID=$(docker images -q ${DOCKER_IMAGE_NAME}:latest)
  100. if [ -z $IMAGE_ID ]
  101. then
  102. echo "Error: Cannot find Image ID for ${DOCKER_IMAGE_NAME}"
  103. exit 1
  104. fi
  105. # Run the Docker Image
  106. echo "Running build script in the docker image"
  107. docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} --tty ${DOCKER_IMAGE_NAME}:latest /data/workspace/${DOCKER_BUILD_SCRIPT}
  108. if [ $? -ne 0 ]
  109. then
  110. echo Failed to build from docker image ${DOCKER_IMAGE_NAME}:latest
  111. echo "To log into and troubleshoot the docker container, run the following command:"
  112. echo ""
  113. echo "docker run --platform ${TARGET_DOCKER_PLATFORM_ARG} -it --tty ${DOCKER_IMAGE_NAME}:latest"
  114. echo ""
  115. exit 1
  116. fi
  117. # Capture the container ID
  118. echo "Capturing the Container ID"
  119. CONTAINER_ID=$(docker container ls -l -q --filter "ancestor=${DOCKER_IMAGE_NAME}:latest")
  120. if [ -z $CONTAINER_ID ]
  121. then
  122. echo "Error: Cannot find Container ID for Image ${DOCKER_IMAGE_NAME}"
  123. exit 1
  124. fi
  125. # Copy the build artifacts from the Docker Container
  126. echo "Copying the built contents from the docker container for image ${DOCKER_IMAGE_NAME}"
  127. mkdir -p build
  128. docker cp $CONTAINER_ID:/data/workspace/package/. build
  129. if [ $? -ne 0 ]
  130. then
  131. echo "Error occurred copying build artifacts from Docker image ${DOCKER_IMAGE_NAME}:latest."
  132. exit 1
  133. fi
  134. # Clean up the docker image and container
  135. echo "Cleaning up container"
  136. docker container rm $CONTAINER_ID || (echo "Warning: unable to clean up container for image ${DOCKER_IMAGE_NAME}")
  137. echo "Cleaning up image"
  138. docker rmi --force $IMAGE_ID || (echo "Warning: unable to clean up image ${DOCKER_IMAGE_NAME}")
  139. popd
  140. exit 0