toolchain-shar-extract.sh 9.2 KB

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