toolchain-shar-extract.sh 8.5 KB

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