summaryrefslogtreecommitdiff
path: root/utils/sb2
blob: c6e1b0e41e908de373274f5a949651f652e22ef4 (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
#!/bin/bash
# Copyright (C) 2006,2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

function usage()
{
	cat <<EOF
sb2 - crosscompiling environment
Usage:
    sb2 [OPTION]... [COMMAND] [PARAMETERS]

If no COMMAND is given, a bash shell in scratchbox2 environment is started.

Options:
    -v           verbose mode: log all redirections
    -h           print this help
    -t DIRECTORY target directory, else search up in directory
                 structure for a .sbrc
    -e           emulation mode
    -m DIRECTORY load preload scripts from alternative location

Examples:
    sb2 ./configure
	sb2 make
	sb2 -e make install
EOF
	exit 2
}

function exit_error()
{
		echo "$@"
		exit 1
}

function sanity_check()
{
	if [ `id -u` = 0 ] ; then
		exit_error "Do not use sbox2 as root!"
	fi
	# check that most important host and target files exist
}

find_target_root()
{
(
	while [ "$PWD" != "/" ]; do
		if [ -r sb2.config -o -d .sb2cache ]; then
			echo "$(readlink -f -n $PWD)"
			return 0
		else
			cd ..
		fi
	done
	return 1
)
}

function sboxify_environment()
{

	if [ -r ~/.sb2rc ]; then
		. ~/.sb2rc
	fi

	if [ -z "$SBOX_TARGET_ROOT" ]; then
		SBOX_TARGET_ROOT=`find_target_root` || exit_error "Cannot find target root!"
	fi

	SBOX_MAPPING_CACHE=$SBOX_TARGET_ROOT/.sb2cache
	if [ ! -d $SBOX_MAPPING_CACHE ]; then
		mkdir -p $SBOX_MAPPING_CACHE
	fi


	if [ -n "$SBOX_DIR" ]; then
		SBOX_LIBSB2="$SBOX_DIR/lib/libsb2.so"
		if [ -z "$SBOX_REDIR_SCRIPTS" ]; then
			SBOX_REDIR_SCRIPTS="$SBOX_DIR/share/scratchbox2/redir_scripts"
		fi
		SBOX_GCCWRAPPER="$SBOX_DIR/bin/sb_gcc_wrapper"
	else
		exit_error "Something foul happeninged..."
	fi

	if [ -n "$SBOX_TOOLS_ROOT" ]; then
		REDIR_LD_SO="$SBOX_TOOLS_DIR/lib/ld-linux.so.2"
		REDIR_LD_LIBRARY_PATH="$SBOX_TOOLS_DIR/lib:$SBOX_TOOLS_DIR/usr/lib:$SBOX_TOOLS_DIR/usr/local/lib"

		export REDIR_LD_SO REDIR_LD_LIBRARY_PATH SBOX_TOOLS_ROOT
	fi

	export SBOX_LIBSB2 SBOX_REDIR_SCRIPTS SBOX_TARGET_ROOT SBOX_MAPPING_CACHE SBOX_GCCWRAPPER SBOX_TARGET_COMPILER

#	cd $SBOX_TARGET_ROOT

	# -Riku: I don't think we want to run this on every sb2 invocation..
	HOST_GCC_INC_DIR=$(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"};')

	export PATH=$SBOX_TARGET_ROOT/sb_tools/bin:$SBOX_DIR/bin:$PATH:$SBOX_TARGET_ROOT/bin:$SBOX_TARGET_ROOT/usr/bin:$SBOX_TARGET_ROOT/usr/local/bin:/sbin:/usr/sbin

	export LD_PRELOAD=$SBOX_LIBSB2
	export SBOX_EXTRA_HOST_COMPILER_ARGS="$HOST_GCC_INC_DIR"
	export SBOX_EXTRA_CROSS_COMPILER_ARGS="--sysroot=$SBOX_TARGET_ROOT -isystem $SBOX_TARGET_ROOT/usr/local/include -isystem $SBOX_TARGET_ROOT/usr/include -I$SBOX_TARGET_ROOT/usr/local/include -I$SBOX_TARGET_ROOT/usr/include -L$SBOX_TARGET_ROOT/usr/local/lib -L$SBOX_TARGET_ROOT/usr/lib -L$SBOX_TARGET_ROOT/lib"
	export SBOX_EXTRA_CROSS_LD_ARGS="-rpath-link $SBOX_TARGET_ROOT/usr/local/lib:$SBOX_TARGET_ROOT/usr/lib:$SBOX_TARGET_ROOT/lib"
	export SBOX_SCRATCHBOX_CONFIG=$SBOX_TARGET_ROOT/sb2.config
	export PS1="[SB2] \u@\h \w \$ "

	. $SBOX_SCRATCHBOX_CONFIG

	# Debian exports REVISIT on dpkg 1.14
	if [ $SBOX_CPU = "arm" ]; then
		export DEB_BUILD_ARCH=armel
	else
		export DEB_BUILD_ARCH=$SBOX_CPU
	fi
	export DEB_BUILD_ARCH_CPU=$SBOX_CPU
	export DEB_BUILD_GNU_CPU=$SBOX_CPU
	export DEB_BUILD_GNU_TYPE=$SBOX_CPU-linux-gnu

	export SBOX_COMPILER_ROOT=$(readlink -f $SBOX_CROSS_GCC_DIR/..)
	export SBOX_CPUTRANSPARENCY_METHOD
	export SBOX_CROSS_GCC_PREFIX_LIST
	export SBOX_HOST_GCC_PREFIX_LIST
	export SBOX_UNAME_MACHINE=$SBOX_CPU

	if [ "$SBOX_MAPPING_VERBOSE" == "1" ]; then
		export SBOX_DISABLE_MAPPING_CACHE=1 # disable the cache to force use of lua engine
		export SBOX_MAPPING_LOGFILE=$PWD/sb2_mapping.log
		echo "Running scratchbox with these settings:"
		echo "SBOX_LIBSB2 = $SBOX_LIBSB2"
		echo "SBOX_REDIR_SCRIPTS = $SBOX_REDIR_SCRIPTS"
		echo "SBOX_GCCWRAPPER = $SBOX_GCCWRAPPER"
		echo "REDIR_LD_SO = $REDIR_LD_SO"
		echo "REDIR_LD_LIBRARY_PATH = $REDIR_LD_LIBRARY_PATH"
		echo "SBOX_TARGET_ROOT = $SBOX_TARGET_ROOT"
	fi
}

export SBOX_DIR=$(readlink -f $(dirname $_)/..)

while getopts vht:em: foo
do
	case $foo in
	(v) export SBOX_MAPPING_VERBOSE=1 ;;
	(h) usage ;;
	(t) export SBOX_TARGET_ROOT=$OPTARG ;;
	(e) export SBOX_EMULATE=1 ;;
	(m) export SBOX_REDIR_SCRIPTS=$OPTARG;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

# read commands to execute from stdin - not yet implemented
if [ "$1" = "-" ] ; then
	STDIN=true
fi

sboxify_environment

# set the initial binary name for the mapping engine
export __SB2_BINARYNAME="bash"

if [ $# -gt 0 -o "$STDIN" = true ] ; then
	binary="$1"
	shift 1
	args="$@"
	exec /bin/sh -c "$binary $args"
else
	exec /bin/bash --noprofile --norc
fi