toolchain-shar-extract.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/sh
  2. export LC_ALL=en_US.UTF-8
  3. # The pipefail option is now part of POSIX (POSIX.1-2024) and available in more
  4. # and more shells. Enable it if available to make the SDK installer more robust.
  5. (set -o pipefail 2> /dev/null) && set -o pipefail
  6. #Make sure at least one python is installed
  7. INIT_PYTHON=$(command -v python3 2>/dev/null )
  8. [ -z "$INIT_PYTHON" ] && INIT_PYTHON=$(command -v python2 2>/dev/null)
  9. [ -z "$INIT_PYTHON" ] && echo "Error: The SDK needs a python installed" && exit 1
  10. # Remove invalid PATH elements first (maybe from a previously setup toolchain now deleted
  11. PATH=`$INIT_PYTHON -c 'import os; print(":".join(e for e in os.environ["PATH"].split(":") if os.path.exists(e)))'`
  12. tweakpath () {
  13. case ":${PATH}:" in
  14. *:"$1":*)
  15. ;;
  16. *)
  17. PATH=$PATH:$1
  18. esac
  19. }
  20. # Some systems don't have /usr/sbin or /sbin in the cleaned environment PATH but we make need it
  21. # for the system's host tooling checks
  22. tweakpath /usr/sbin
  23. tweakpath /sbin
  24. INST_ARCH=$(uname -m | sed -e "s/i[3-6]86/ix86/" -e "s/x86[-_]64/x86_64/")
  25. SDK_ARCH=$(echo @SDK_ARCH@ | sed -e "s/i[3-6]86/ix86/" -e "s/x86[-_]64/x86_64/")
  26. INST_GCC_VER=$(gcc --version 2>/dev/null | sed -ne 's/.* \([0-9]\+\.[0-9]\+\)\.[0-9]\+.*/\1/p')
  27. SDK_GCC_VER='@SDK_GCC_VER@'
  28. verlte () {
  29. [ "$1" = "`printf "$1\n$2" | sort -V | head -n1`" ]
  30. }
  31. verlt() {
  32. [ "$1" = "$2" ] && return 1 || verlte $1 $2
  33. }
  34. verlt `uname -r` @OLDEST_KERNEL@
  35. if [ $? = 0 ]; then
  36. echo "Error: The SDK needs a kernel > @OLDEST_KERNEL@"
  37. exit 1
  38. fi
  39. if [ "$INST_ARCH" != "$SDK_ARCH" ]; then
  40. # Allow for installation of ix86 SDK on x86_64 host
  41. if [ "$INST_ARCH" != x86_64 -o "$SDK_ARCH" != ix86 ]; then
  42. echo "Error: Incompatible SDK installer! Your host is $INST_ARCH and this SDK was built for $SDK_ARCH hosts."
  43. exit 1
  44. fi
  45. fi
  46. if ! xz -V > /dev/null 2>&1; then
  47. echo "Error: xz is required for installation of this SDK, please install it first"
  48. exit 1
  49. fi
  50. SDK_BUILD_PATH="@SDKPATH@"
  51. DEFAULT_INSTALL_DIR="@SDKPATHINSTALL@"
  52. SUDO_EXEC=""
  53. EXTRA_TAR_OPTIONS=""
  54. target_sdk_dir=""
  55. answer=""
  56. relocate=1
  57. savescripts=0
  58. verbose=0
  59. publish=0
  60. listcontents=0
  61. while getopts ":yd:npDRSl" OPT; do
  62. case $OPT in
  63. y)
  64. answer="Y"
  65. ;;
  66. d)
  67. target_sdk_dir=$OPTARG
  68. ;;
  69. n)
  70. prepare_buildsystem="no"
  71. ;;
  72. p)
  73. prepare_buildsystem="no"
  74. publish=1
  75. ;;
  76. D)
  77. verbose=1
  78. ;;
  79. R)
  80. relocate=0
  81. savescripts=1
  82. ;;
  83. S)
  84. savescripts=1
  85. ;;
  86. l)
  87. listcontents=1
  88. ;;
  89. *)
  90. echo "Usage: $(basename "$0") [-y] [-d <dir>]"
  91. echo " -y Automatic yes to all prompts"
  92. echo " -d <dir> Install the SDK to <dir>"
  93. echo "======== Extensible SDK only options ============"
  94. echo " -n Do not prepare the build system"
  95. echo " -p Publish mode (implies -n)"
  96. echo "======== Advanced DEBUGGING ONLY OPTIONS ========"
  97. echo " -S Save relocation scripts"
  98. echo " -R Do not relocate executables"
  99. echo " -D use set -x to see what is going on"
  100. echo " -l list files that will be extracted"
  101. exit 1
  102. ;;
  103. esac
  104. done
  105. payload_offset=$(($(grep -na -m1 "^MARKER:$" "$0"|cut -d':' -f1) + 1))
  106. if [ "$listcontents" = "1" ] ; then
  107. if [ @SDK_ARCHIVE_TYPE@ = "zip" ]; then
  108. tail -n +$payload_offset "$0" > sdk.zip
  109. if unzip -l sdk.zip;then
  110. rm sdk.zip
  111. else
  112. rm sdk.zip && exit 1
  113. fi
  114. else
  115. tail -n +$payload_offset "$0"| tar tvJ || exit 1
  116. fi
  117. exit
  118. fi
  119. titlestr="@SDK_TITLE@ installer version @SDK_VERSION@"
  120. printf "%s\n" "$titlestr"
  121. printf "%${#titlestr}s\n" | tr " " "="
  122. if [ $verbose = 1 ] ; then
  123. set -x
  124. fi
  125. @SDK_PRE_INSTALL_COMMAND@
  126. # SDK_EXTENSIBLE is exposed from the SDK_PRE_INSTALL_COMMAND above
  127. if [ "$SDK_EXTENSIBLE" = "1" ]; then
  128. DEFAULT_INSTALL_DIR="@SDKEXTPATH@"
  129. if [ "$INST_GCC_VER" = '4.8' -a "$SDK_GCC_VER" = '4.9' ] || [ "$INST_GCC_VER" = '4.8' -a "$SDK_GCC_VER" = '' ] || \
  130. [ "$INST_GCC_VER" = '4.9' -a "$SDK_GCC_VER" = '' ]; then
  131. echo "Error: Incompatible SDK installer! Your host gcc version is $INST_GCC_VER and this SDK was built by gcc higher version."
  132. exit 1
  133. fi
  134. fi
  135. if [ "$target_sdk_dir" = "" ]; then
  136. if [ "$answer" = "Y" ]; then
  137. target_sdk_dir="$DEFAULT_INSTALL_DIR"
  138. else
  139. read -p "Enter target directory for SDK (default: $DEFAULT_INSTALL_DIR): " target_sdk_dir
  140. [ "$target_sdk_dir" = "" ] && target_sdk_dir=$DEFAULT_INSTALL_DIR
  141. fi
  142. fi
  143. eval target_sdk_dir=$(echo "$target_sdk_dir"|sed 's/ /\\ /g')
  144. if [ -d "$target_sdk_dir" ]; then
  145. target_sdk_dir=$(cd "$target_sdk_dir"; pwd)
  146. else
  147. target_sdk_dir=$(readlink -m "$target_sdk_dir")
  148. fi
  149. # limit the length for target_sdk_dir, ensure the relocation behaviour in relocate_sdk.py has right result.
  150. # This is due to ELF interpreter being set to 'a'*1024 in
  151. # meta/recipes-core/meta/uninative-tarball.bb
  152. if [ ${#target_sdk_dir} -gt 1024 ]; then
  153. echo "Error: The target directory path is too long!!!"
  154. exit 1
  155. fi
  156. if [ "$SDK_EXTENSIBLE" = "1" ]; then
  157. # We're going to be running the build system, additional restrictions apply
  158. if echo "$target_sdk_dir" | grep -q '[+\ @$]'; then
  159. echo "The target directory path ($target_sdk_dir) contains illegal" \
  160. "characters such as spaces, @, \$ or +. Abort!"
  161. exit 1
  162. fi
  163. # The build system doesn't work well with /tmp on NFS
  164. fs_dev_path="$target_sdk_dir"
  165. while [ ! -d "$fs_dev_path" ] ; do
  166. fs_dev_path=`dirname $fs_dev_path`
  167. done
  168. fs_dev_type=`stat -f -c '%t' "$fs_dev_path"`
  169. if [ "$fsdevtype" = "6969" ] ; then
  170. echo "The target directory path $target_sdk_dir is on NFS, this is not possible. Abort!"
  171. exit 1
  172. fi
  173. else
  174. if [ -n "$(echo $target_sdk_dir|grep ' ')" ]; then
  175. echo "The target directory path ($target_sdk_dir) contains spaces. Abort!"
  176. exit 1
  177. fi
  178. fi
  179. if [ -e "$target_sdk_dir/environment-setup-@REAL_MULTIMACH_TARGET_SYS@" ]; then
  180. echo "The directory \"$target_sdk_dir\" already contains a SDK for this architecture."
  181. printf "If you continue, existing files will be overwritten! Proceed [y/N]? "
  182. default_answer="n"
  183. else
  184. printf "You are about to install the SDK to \"$target_sdk_dir\". Proceed [Y/n]? "
  185. default_answer="y"
  186. fi
  187. if [ "$answer" = "" ]; then
  188. read answer
  189. [ "$answer" = "" ] && answer="$default_answer"
  190. else
  191. echo $answer
  192. fi
  193. if [ "$answer" != "Y" -a "$answer" != "y" ]; then
  194. echo "Installation aborted!"
  195. exit 1
  196. fi
  197. # Try to create the directory (this will not succeed if user doesn't have rights)
  198. mkdir -p $target_sdk_dir >/dev/null 2>&1
  199. # if don't have the right to access dir, gain by sudo
  200. if [ ! -x $target_sdk_dir -o ! -w $target_sdk_dir -o ! -r $target_sdk_dir ]; then
  201. if [ "$SDK_EXTENSIBLE" = "1" ]; then
  202. echo "Unable to access \"$target_sdk_dir\", will not attempt to use" \
  203. "sudo as as extensible SDK cannot be used as root."
  204. exit 1
  205. fi
  206. SUDO_EXEC=$(command -v "sudo")
  207. if [ -z $SUDO_EXEC ]; then
  208. echo "No command 'sudo' found, please install sudo first. Abort!"
  209. exit 1
  210. fi
  211. # test sudo could gain root right
  212. $SUDO_EXEC pwd >/dev/null 2>&1
  213. [ $? -ne 0 ] && echo "Sorry, you are not allowed to execute as root." && exit 1
  214. # now that we have sudo rights, create the directory
  215. $SUDO_EXEC mkdir -p $target_sdk_dir >/dev/null 2>&1
  216. fi
  217. printf "Extracting SDK..."
  218. if [ @SDK_ARCHIVE_TYPE@ = "zip" ]; then
  219. if [ -z "$(command -v unzip)" ]; then
  220. echo "Aborted, unzip is required to extract the SDK archive, please make sure it's installed on your system!"
  221. exit 1
  222. fi
  223. tail -n +$payload_offset "$0" > sdk.zip
  224. if $SUDO_EXEC unzip $EXTRA_TAR_OPTIONS sdk.zip -d $target_sdk_dir;then
  225. rm sdk.zip
  226. else
  227. rm sdk.zip && exit 1
  228. fi
  229. elif [ @SDK_ARCHIVE_TYPE@ = "tar.zst" ]; then
  230. if [ -z "$(command -v zstd)" ]; then
  231. echo "Aborted, zstd is required to extract the SDK archive, please make sure it's installed on your system!"
  232. exit 1
  233. fi
  234. tail -n +$payload_offset "$0"| zstd -T0 -dc | $SUDO_EXEC tar mx -C $target_sdk_dir --checkpoint=.2500 $EXTRA_TAR_OPTIONS || exit 1
  235. else
  236. if [ -z "$(command -v xz)" ]; then
  237. echo "Aborted, xz is required to extract the SDK archive, please make sure it's installed on your system!"
  238. exit 1
  239. fi
  240. tail -n +$payload_offset "$0"| $SUDO_EXEC tar mxJ -C $target_sdk_dir --checkpoint=.2500 $EXTRA_TAR_OPTIONS || exit 1
  241. fi
  242. echo "done"
  243. printf "Setting it up..."
  244. # fix environment paths
  245. real_env_setup_script=""
  246. for env_setup_script in `ls $target_sdk_dir/environment-setup-*`; do
  247. if grep -q 'OECORE_NATIVE_SYSROOT=' $env_setup_script; then
  248. # Handle custom env setup scripts that are only named
  249. # environment-setup-* so that they have relocation
  250. # applied - what we want beyond here is the main one
  251. # rather than the one that simply sorts last
  252. real_env_setup_script="$env_setup_script"
  253. fi
  254. $SUDO_EXEC sed -e "s:@SDKPATH@:$target_sdk_dir:g" -i $env_setup_script
  255. done
  256. if [ -n "$real_env_setup_script" ] ; then
  257. env_setup_script="$real_env_setup_script"
  258. fi
  259. @SDK_POST_INSTALL_COMMAND@
  260. # delete the relocating script, so that user is forced to re-run the installer
  261. # if he/she wants another location for the sdk
  262. if [ $savescripts = 0 ] ; then
  263. $SUDO_EXEC rm -f ${env_setup_script%/*}/relocate_sdk.py ${env_setup_script%/*}/relocate_sdk.sh
  264. fi
  265. # Execute post-relocation script
  266. post_relocate="$target_sdk_dir/post-relocate-setup.sh"
  267. if [ -e "$post_relocate" ]; then
  268. $SUDO_EXEC sed -e "s:@SDKPATH@:$target_sdk_dir:g" -i $post_relocate
  269. $SUDO_EXEC /bin/sh $post_relocate "$target_sdk_dir" "@SDKPATH@"
  270. if [ $? -ne 0 ]; then
  271. echo "Executing $post_relocate failed"
  272. exit 1
  273. fi
  274. $SUDO_EXEC rm -f $post_relocate
  275. fi
  276. echo "SDK has been successfully set up and is ready to be used."
  277. echo "Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g."
  278. for env_setup_script in `ls $target_sdk_dir/environment-setup-*`; do
  279. echo " \$ . $env_setup_script"
  280. done
  281. exit 0
  282. MARKER: