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