summaryrefslogtreecommitdiff
path: root/utils/sb2-init
blob: 7016020011573dd7fa9e6a4c6711732365f32140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/bash
# sb2-init - Copyright (C) 2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

my_path=$_

function show_existing_config_info()
{
	has_targets="no"
	echo "Already initialized targets:"
	echo
	for f in $HOME/.scratchbox2/*/sb2.config
	do
		if [ -f $f ]
		then
			SBOX_CONFIG_VERSION=0
			. $f
			has_targets="yes"
			targetdir=`dirname $f`
			targetname=`basename $targetdir`
			echo "Target $targetname:"
			if [ "$SBOX_CONFIG_VERSION" -lt 5 ]
			then
				echo "(configuration file version too old)"
			else
				echo "configured at $SBOX_INIT_TIME by $SBOX_INIT_ID, with command"
				echo "( cd $SBOX_TARGET_ROOT;"
				echo "sb2-init $SBOX_INIT_ORIG_ARGS )"
			fi
			echo
		fi
	done
	if [ has_targets = "no" ]
	then
		echo "none."
	fi
}

function usage()
{
	cat <<EOF
sb2-init - initialize a target root for scratchbox2
Usage:
	sb2-init [OPTION]... [TARGETNAME] [COMPILER]

sb2-init is expected to be run in the directory you want
to use as scratchbox 2 target root.

TARGETNAME is what you want to call this target
COMPILER is of the form $HOME/arm-2006q3/bin/arm-linux-gcc


Options:
    -c "command"      specify cpu transparency command, for example:
                      "qemu-arm", "sbrsh" or "qemu-arm -m 512"
    -r [hostname]     generate sbrsh config using remote device address
    -l [hostname]     NFS server/localhost address seen by remote device
    -d                set target as default scratchbox2 target
    -m [mapping_mode] use mapping_mode as default
    -h                print this help
    -n                don't build libtool for the target
    -s                skip checks for target root's /usr/include etc.
    -t [tools_dir]    set directory containing the build tools distribution
    -C "options"      add extra options for the compiler, for example:
                      "-fgnu89-inline"
    -A arch           manually override target architecture
    -v                display version

Examples:
    sb2-init ARM arm-linux-gcc
    sb2-init -m maemo ARM arm-linux-gcc
    sb2-init -sn -c sbrsh armel-debian /path/to/arm-linux-gcc

EOF
	show_existing_config_info
	exit 2
}

function version()
{
	cat $SBOX_DIR/share/scratchbox2/version
	exit 0
}


function write_target_config()
{
	cat - > $HOME/.scratchbox2/$TARGET/sb2.config <<EOF
# Scratchbox2 configuration file generated by sb2-init.

SBOX_INIT_ORIG_ARGS="$SBOX_INIT_ORIG_ARGS"
SBOX_INIT_TIME=$SBOX_INIT_TIME
SBOX_INIT_ID="$SBOX_INIT_ID"

export SBOX_CONFIG_VERSION=5
export SBOX_TARGET_ROOT=$SBOX_TARGET_ROOT
export SBOX_CPU=$ARCH
export SBOX_GCC_TARGET=$GCC_TARGET
export SBOX_CPUTRANSPARENCY_METHOD="$CPUTRANSP"
export SBOX_UNAME_MACHINE=$ARCH

export SBOX_DEFAULT_GCC_PREFIX=$GCC_PREFIX

export SBOX_CROSS_GCC_NAME=cross-gcc
export SBOX_CROSS_GCC_PREFIX_LIST=$GCC_TARGET-:$ARCH-linux-:$GCC_PREFIX
export SBOX_CROSS_GCC_SUBST_PREFIX=$GCC_PREFIX
export SBOX_CROSS_GCC_SPECS_FILE="$SBOX_CROSS_GCC_SPECS_FILE"
export SBOX_CROSS_GCC_DIR=$GCC_PATH
export SBOX_CROSS_GCC_LD_ARGS=
export SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS"
export SBOX_EXTRA_CROSS_COMPILER_STDINC="$SBOX_EXTRA_CROSS_COMPILER_STDINC"
export SBOX_EXTRA_CROSS_LD_ARGS="-rpath-link $SBOX_TARGET_ROOT/usr/lib:$SBOX_TARGET_ROOT/lib"

export DEB_BUILD_ARCH_CPU=$DEBIAN_CPU
export DEB_BUILD_ARCH=$DEBIAN_CPU
export DEB_BUILD_GNU_CPU=$ARCH
export DEB_BUILD_GNU_TYPE=$SB2INIT_DEB_BUILD_GNU_TYPE

export DEB_HOST_ARCH_CPU=$DEBIAN_CPU
export DEB_HOST_ARCH=$DEBIAN_CPU
export DEB_HOST_GNU_CPU=$ARCH
export DEB_HOST_GNU_TYPE=$SB2INIT_DEB_HOST_GNU_TYPE

export SBOX_HOST_GCC_NAME=host-gcc
export SBOX_HOST_GCC_PREFIX_LIST=host-
export SBOX_HOST_GCC_SUBST_PREFIX=
export SBOX_HOST_GCC_SPECS_FILE=
export SBOX_HOST_GCC_DIR=/usr/bin
export SBOX_HOST_GCC_LD_ARGS=
export SBOX_EXTRA_HOST_COMPILER_ARGS="$HOST_GCC_INC"

export SBRSH_CONFIG=$HOME/.scratchbox2/$TARGET/sbrsh.config

if [ -z "\$SBOX_MAPMODE" ]; then
	export SBOX_MAPMODE=$MAPPING_MODE
fi

export SBOX_TOOLS_ROOT=$TOOLS_ROOT
export SBOX_REDIR_LD_SO=$REDIR_LD_SO
export SBOX_REDIR_LD_LIBRARY_PATH=$REDIR_LD_LIBRARY_PATH
if [ -n "\$SBOX_TOOLS_ROOT" ]; then
	export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/:/usr/lib64:/lib:/lib64:\$SBOX_TOOLS_ROOT/usr/lib/libfakeroot:\$SBOX_TOOLS_ROOT/usr/lib64/libfakeroot:\$SBOX_TOOLS_ROOT/usr/lib32/libfakeroot:\$SBOX_REDIR_LD_LIBRARY_PATH:\$LD_LIBRARY_PATH
else
	# SBOX_TOOLS_ROOT not set, include the fakeroot lib path in any case.
	export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/lib/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot
fi
EOF
	echo "Finished writing sb2.config"
}

function write_sbrsh_config()
{
	cat - > $HOME/.scratchbox2/$TARGET/sbrsh.config <<EOF
# sbrsh configuration file generated by sb2-init.

$TARGET  $REMOTEHOST
  nfs   $LOCALHOST:$SBOX_TARGET_ROOT  /  rw,nolock,noac,tcp
  nfs   $LOCALHOST:$HOME  $HOME  rw,nolock,noac,tcp
  bind  /tmp      /tmp
  bind  /proc     /proc
  bind  /sys      /sys
  bind  /dev      /dev
  bind  /dev/pts  /dev/pts
EOF
	echo "Finished writing sbrsh.config"
}

function check_buildroot_sanity()
{
	a_ok=1

	if [ ! -e usr/include/stdio.h ]; then
		echo "no usr/include/stdio.h"
		a_ok=0
	fi

	if [ ! -e lib/libc.so.6 ] && [ ! -e lib/libc.so.0 ]; then
		echo "no lib/libc.so.6 or lib/libc.so.0"
		a_ok=0
	fi

	if [ ! -e usr/lib/libc.so ]; then
		echo "no usr/lib/libc.so"
		a_ok=0
	fi
	
	if [ $a_ok == 1 ]; then
		true
	else
		echo "Your buildroot seems to lack basic essentials like headers 
or c-library. You should probably get either a ready rootfs tarball or
copy the necessary files from your toolchain into place. After doing that
you can re-run this script."
		exit 1
	fi
}

if [ -z "$SBOX_DIR" ]
then
	export SBOX_DIR=$(readlink -f $(dirname $my_path)/..)
fi

# SBOX_INIT_* variables are used to record who initialized, and when and 
# how sb2 was initialized.
# It is possible to set SBOX_INIT_ID externally (for example, if
# another program is used to initialize the system)
SBOX_INIT_ORIG_ARGS="$*"
SBOX_INIT_TIME=`date +%Y-%m-%d_%H:%M:%S`
if [ -z "$SBOX_INIT_ID" ]
then
	SBOX_INIT_ID="user '$USER'"
fi

REMOTEHOST=
LOCALHOST=

set_as_default=0
with_libtool=1
skip_checks=false

SBOX_EXTRA_CROSS_COMPILER_ARGS=""

if [ -z "$*" ]; then
	usage
fi

while getopts A:c:C:r:l:m:dhnst:v foo
do
	case $foo in
	(A) ARCH=$OPTARG ;;
	(c) CPUTRANSP=$OPTARG ;;
	(r) REMOTEHOST=$OPTARG ;;
	(l) LOCALHOST=$OPTARG ;;
	(d) set_as_default=1 ;;
	(h) usage ;;
	(m) MAPPING_MODE=$OPTARG ;;
	(n) with_libtool=0 ;;
	(s) skip_checks=true ;;
	(t) TOOLS_ROOT=$OPTARG ;;
	(v) version ;;
	(C) SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS $OPTARG " ;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

if [ "$LOCALHOST" ] && [ -z "$REMOTEHOST" ]; then
	echo "Warning: Local host specified without remote host."
fi

TARGET=$1
SBOX_TARGET_ROOT=$PWD
GCC=$2

if [ -z "$TARGET" ]; then
	echo "Error: no target given"
	exit 1
fi

if [ -z "$GCC" ]; then
	echo "Warning: no compiler given"
else
	GCC_FULLPATH=$(which $GCC)
	# test that gcc exists and can be executed
	if [ $? != 0 ]; then
		echo "$GCC doesn't exist"
		exit 1
	fi
	GCC_PATH=$(dirname $(which $GCC))
	if [ $GCC -v > /dev/null 2>&1 != 0 ]; then
		echo "Invalid compiler specified: $GCC"
		exit 1
	fi

	GCC_PREFIX=$(basename $GCC | sed 's/-gcc$/-/')

	if [ -z "$ARCH" ]
	then
		echo "Using $GCC to detect target architecture:"
		ARCH=$($GCC -dumpmachine | awk -F- '{ print $1 }')
		GCC_TARGET=$($GCC -dumpmachine)
	else
		echo "Target architecture set to $ARCH"
	fi
fi

# defaults for SB2_INIT_DEB_BUILD_GNU_TYPE and SB2_INIT_DEB_HOST_GNU_TYPE, 
# these may be changed by sb2rc.$MAPPING_MODE
SB2INIT_DEB_BUILD_GNU_TYPE=$ARCH-linux-gnu
SB2INIT_DEB_HOST_GNU_TYPE=$ARCH-linux-gnu

# $ARCH has been set, get mode-specific settings..
if [ -f $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$MAPPING_MODE ]
then
	echo "Reading mode-specific settings.."
	. $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$MAPPING_MODE
fi


DEBIAN_CPU=$ARCH
if [ -z "$CPUTRANSP" ]; then
	CPUTRANSP="$(which qemu-$ARCH)"
fi

case "$ARCH" in
	arm*) 
		if [ -z "$GCC_TARGET" ]
		then
			DEBIAN_CPU=$ARCH
		else
			echo $GCC_TARGET | grep -q -i eabi
			if [ $? == 0 ]; then
				DEBIAN_CPU=armel
			fi
		fi
		;;
	ppc*) ;;
	mips*) ;;
	sh*) ;;
	i386*) ;;
	i486*) ;;
	i586*) ;;
	i686*) ;;
	*)
		echo "Unsupported target architecture: $ARCH"
		echo "You must add support for it into preload/sb_exec.c"
		echo "and utils/sb2-init"
		exit 1
		;;

esac

if [ -z "$MAPPING_MODE" ]; then
	MAPPING_MODE="simple"
fi

if [ ! -d $SBOX_DIR/share/scratchbox2/lua_scripts/pathmaps/$MAPPING_MODE ]; then
	echo "Invalid mapping mode: $MAPPING_MODE"
	exit 1
fi


if [ ! $skip_checks ]; then
	check_buildroot_sanity
fi


mkdir -p $HOME/.scratchbox2

if [ -z "$(sb2-config -l)" ]; then
# force this as default anyway as there are no
# other existing targets
	set_as_default=1
fi

if [ -n "$TOOLS_ROOT" ]; then
	if [ -e "$TOOLS_ROOT/etc/scratchbox-version" ]; then
		# this is a scratchbox 1.x directory, tread carefully
		# sb1 is not FHS, *sigh*
		REDIR_LD_SO=\$SBOX_TOOLS_ROOT/host_shared/lib/ld-linux.so.2
		REDIR_LD_LIBRARY_PATH=\$SBOX_TOOLS_ROOT/host_shared/lib:$TOOLS_ROOT/tools/lib:\$LD_LIBRARY_PATH
		TOOLS_ROOT=$TOOLS_ROOT/tools
	else
		# assume standard FHS system
		REDIR_LD_SO=
		REDIR_LD_LIBRARY_PATH=\$SBOX_TOOLS_ROOT/usr/local/lib:\$SBOX_TOOLS_ROOT/usr/lib:\$SBOX_TOOLS_ROOT/lib:\$LD_LIBRARY_PATH
	fi
fi

# Use "specs" file for gcc if it exists, otherwise add -I/usr/include to params
SBOX_CROSS_GCC_SPECS_FILE=""
SBOX_EXTRA_CROSS_COMPILER_STDINC=""
if [ -f $SBOX_DIR/share/scratchbox2/modeconf/gcc-specs.$MAPPING_MODE ]
then
	SBOX_CROSS_GCC_SPECS_FILE="$SBOX_DIR/share/scratchbox2/modeconf/gcc-specs.$MAPPING_MODE"
else
	SBOX_EXTRA_CROSS_COMPILER_STDINC="-I/usr/include"
fi

SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS -L$SBOX_TARGET_ROOT/usr/lib -L$SBOX_TARGET_ROOT/lib"

# test if the cross compiler needs to be silenced about /usr/include
# usage
echo "" | $GCC_FULLPATH -E - -Wno-poison-system-directories > /dev/null 2>&1
if [ $? = 0 ] ; then
	SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS -Wno-poison-system-directories"
fi

HOST_GCC_INC=$(echo "#include <stdio.h>" | gcc -M -E - | perl -e 'while(<STDIN>) { $foo{$1} = 1 if m/\/usr([^[:space:]]*\/include)/;}; foreach my $k (keys %foo) {print " -isystem $ENV{SBOX_DIR}/share/scratchbox2/host_usr$k"};')

SBOX_COMPILER_ROOT=$(readlink -f $SBOX_CROSS_GCC_DIR/..)

mkdir -p $HOME/.scratchbox2/$TARGET/bin
write_target_config

if [ -n "$REMOTEHOST" ]; then
	if [ -z "$LOCALHOST" ]; then
		LOCALHOST=$(ip -f inet -o addr \
			| awk -- '/: eth[0-9]/ { print $4; exit }' \
			| cut -d/ -f1)
		echo "Using NFS server address: $LOCALHOST"
	fi
	write_sbrsh_config
fi

if [ $set_as_default == 1 ]; then
	sb2-config -d $TARGET
fi

if [ $with_libtool == 1 ]; then
	$SBOX_DIR/bin/sb2 -t $TARGET $SBOX_DIR/bin/sb2-build-libtool
	printf "\n\n"
	if [ $? == 0 ]; then
		echo "sb2-init completed successfully, have fun!"
	else
		echo "Running $SBOX_DIR/bin/sb2-build-libtool failed"
		echo "You can run this manually later, otherwise your"
		echo "sb2 environment is correctly setup and ready to use"
	fi
fi