Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # SPDX-License-Identifier: MIT
  4. #
  5. # Copyright 2023 Michael Rodriguez
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the “Software”), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24.  
  25. # This script is to be used to build a toolchain suitable for developing and
  26. # deploying samething. It is to be used on host Unix(-like) systems.
  27.  
  28. # When pushing a commit to CI, it will check if any of these variables have
  29. # changed since the last commit. If they have, the toolchain is rebuilt.
  30.  
  31. LLVM_VER="16.0.3"
  32. GCC_VER="13.0.1"
  33. CMAKE_VER="3.26.4"
  34. QT_VER="6.5.0"
  35.  
  36. # The architectures which are supported by the toolchain.
  37. SUPPORTED_ARCHS="AArch64 X86"
  38.  
  39. PROJECT_NAME="samething"
  40.  
  41. # Directory where the build takes place.
  42. STAGING_DIR=""
  43.  
  44. # Directory where the toolchain is stored.
  45. TARGET_DIR=""
  46.  
  47. # Accept all prompts, useful for CI.
  48. UNATTENDED=false
  49.  
  50. # Do not suppress console output of external programs.
  51. VERBOSE=false
  52.  
  53. # Immediately exit if any command fails.
  54. set -e
  55.  
  56. directory_create() {
  57. if [ "$VERBOSE" = true ]; then
  58. mkdir -v "$1"
  59. else
  60. mkdir "$1"
  61. fi
  62. }
  63.  
  64. usage() {
  65. echo "Usage: ./build_toolchain.sh [OPTIONS]"
  66. echo
  67. echo "Builds a toolchain suitable for development and deployment of "
  68. echo "$PROJECT_NAME."
  69. echo
  70. echo "Required arguments: "
  71. echo
  72. echo " --staging-dir=STAGINGDIR Directory where the build takes place."
  73. echo " --target-dir=TARGETDIR Directory where the toolchain is stored."
  74. echo
  75. echo "Optional arguments:"
  76. echo " -u, --unattended Accept all prompts, useful for CI. Default is off."
  77. echo " --use-lto Compile the toolchain with link-time optimization"
  78. echo " enabled. The toolchain will be faster, but the "
  79. echo " build time will be much slower and much more"
  80. echo " intensive on the host. Default is off."
  81. echo " -v, --verbose Do not suppress console output of external "
  82. echo " programs; default is off."
  83. }
  84.  
  85. options=$(getopt -l "staging-dir:,target-dir:,use-lto,unattended,help,verbose" -o "s:t:luhv" -a -- "$@")
  86.  
  87. eval set -- "$options"
  88.  
  89. while true
  90. do
  91. case "$1" in
  92. -s|--staging-dir)
  93. shift
  94. STAGING_DIR="$1"
  95. ;;
  96.  
  97. -t|--target-dir)
  98. shift
  99. TARGET_DIR="$1"
  100. ;;
  101.  
  102. -l|--use-lto)
  103. USE_LTO=true
  104. ;;
  105.  
  106. -u|--unattended)
  107. UNATTENDED=true
  108. ;;
  109.  
  110. -h|--help)
  111. usage
  112. exit 0
  113. ;;
  114.  
  115. -v|--verbose)
  116. VERBOSE=true
  117. ;;
  118. --)
  119. shift
  120. break;;
  121. esac
  122. shift
  123. done
  124.  
  125. if [ -z "$STAGING_DIR" ]; then
  126. >&2 echo "Staging directory not specified."
  127. usage
  128. exit 1
  129. fi
  130.  
  131. if [ -z "$TARGET_DIR" ]; then
  132. >&2 echo "Target directory not specified."
  133. usage
  134. exit 1
  135. fi
  136.  
  137. install_software_display() {
  138. echo "WARNING: This script requires the existence of certain software on your"
  139. echo "system to bootstrap the toolchain. It is highly recommended that you "
  140. echo "consult your distribution's package manager to install the required "
  141. echo "software. This script will not install the software for you as there"
  142. echo "are too many package managers to account for to reasonably automate"
  143. echo "this process. If you are unable to install the required software, your "
  144. echo "only alternative is to download a prebuilt toolchain."
  145. echo
  146. echo "Required software:"
  147. echo
  148. echo "* CMake - https://cmake.org/"
  149. echo "* GCC - https://gcc.gnu.org/"
  150. echo "* LLVM - https://llvm.org/"
  151. echo "* Ninja - https://ninja-build.org/"
  152. echo
  153. }
  154.  
  155. if [ "$UNATTENDED" = false ]; then
  156. install_software_display
  157.  
  158. while true
  159. do
  160. printf "Have you installed this software? [Y/N] "
  161. read -r REPLY
  162.  
  163. if expr "$REPLY" : '^[Yy]' 1>/dev/null; then
  164. echo
  165. break
  166. elif expr "$REPLY" : '^[Nn]' 1>/dev/null; then
  167. >&2 echo "Aborting due to required software not being installed."
  168. exit 1
  169. else
  170. >&2 echo "Invalid input; please press Y or N, or press Ctrl+C to exit."
  171. fi
  172. done
  173. fi
  174.  
  175. # Resolve relative to absolute paths.
  176. STAGING_DIR=$(realpath "${STAGING_DIR}")
  177. TARGET_DIR=$(realpath "${TARGET_DIR}")
  178.  
  179. echo "Creating staging directory ${STAGING_DIR}..."
  180. directory_create "${STAGING_DIR}"
  181.  
  182. echo "Creating target directory ${TARGET_DIR}..."
  183. directory_create "${TARGET_DIR}"
  184.  
  185. cd "${STAGING_DIR}" || return
  186.  
  187. echo "Downloading CMake v${CMAKE_VER}..."
  188.  
  189. curl -sLO "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}.tar.gz"
  190.  
  191. echo "Extracting CMake v${CMAKE_VER}..."
  192.  
  193. if [ "$VERBOSE" = true ]; then
  194. tar xvf "cmake-${CMAKE_VER}.tar.gz"
  195. else
  196. tar xf "cmake-${CMAKE_VER}.tar.gz"
  197. fi
  198.  
  199. cd cmake-${CMAKE_VER} || return
  200. echo "Configuring CMake v${CMAKE_VER}, please wait..."
  201.  
  202. CMAKE_BUILD_FLAGS="-DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:STRING=""${TARGET_DIR}""/cmake"
  203.  
  204. if [ "$USE_LTO" = true ]; then
  205. CMAKE_BUILD_FLAGS="$CMAKE_BUILD_FLAGS -DCMake_BUILD_LTO:BOOL=ON"
  206. fi
  207.  
  208. if [ "$VERBOSE" = true ]; then
  209. cmake -S . -B build -G Ninja "${CMAKE_BUILD_FLAGS}"
  210. else
  211. cmake -S . -B build -G Ninja "${CMAKE_BUILD_FLAGS}" 2>/dev/null
  212. fi
  213.  
  214. echo "Building and installing CMake v${CMAKE_VER}, this may take a while."
  215. cd build || return
  216.  
  217. if [ "$VERBOSE" = true ]; then
  218. ninja install
  219. else
  220. ninja install 2>/dev/null
  221. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement