summaryrefslogtreecommitdiff
path: root/utils/sb2-config-gcc-toolchain
blob: 32bcba03e0766b778837ef50088732a30bb490da (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
#!/bin/bash
# sb2-config-gcc-toolchain - configure a cross-gcc compiler for SB2
#
# Copyright (c) 2008 Nokia Corporation.
# Copyright (C) 2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

my_path=$_
if [ $(basename $my_path) != $(basename $0) ]; then
	my_path=$0
	if [ $(basename $my_path) = $my_path ]; then
		my_path=$(which $my_path)
	fi
fi

function log_config_action()
{
	tstamp=`/bin/date '+%Y-%m-%d %H:%M:%S'`
	echo "$tstamp	$*" >>$SBOX_CONFIG_DIR/CONFIG-LOG
}

# Get the original arguments that were specified to sb2-init from
function usage()
{
	cat <<EOF
sb2-config-gcc-toolchain - Configure a cross-gcc compiler for SB2
Usage:
	sb2-config-gcc-toolchain [OPTION]... [COMPILER]

COMPILER is of the form $HOME/arm-2006q3/bin/arm-linux-gcc

Options:
	################
    -t target         set target name
    -C "options"      add extra options for the compiler, for example:
                      "-fgnu89-inline"
    -A arch           manually override target architecture
    -h                print this help
    -m mapping_mode   target uses mapping_mode as default mode
    -S SBOX_DIR       define value for SBOX_DIR
    -R TARGET_ROOT    define value for SBOX_TARGET_ROOT
    -V                this toolchain is a secondary toolchain,
                      tools require version numbers in pathnames
    -v                verbose operation

EOF
	exit 2
}

function write_gcc_config()
{
	gcc_version=$1

	if [ -n "$gcc_version" ] ; then
		gcc_version_id=`echo $gcc_version | sed -e 's/[^0-9a-zA-Z]/_/g'`
	fi

	SBOX_TARGET_TOOLCHAIN_DIR=$(dirname "$SBOX_CROSS_GCC_DIR")
	SBOX_CROSS_GCC_PREFIX_LIST=$GCC_TARGET-:$SB2INIT_CROSS_GCC_PREFIX_LIST:$GCC_PREFIX

	# Note: "sb2" script will replace "@SBOX_TARGET_ROOT@" by the real 
	# value when the session is created.
	SBOX_EXTRA_CROSS_LD_ARGS="-rpath-link @SBOX_TARGET_ROOT@/usr/lib:@SBOX_TARGET_ROOT@/lib"
	SBOX_CROSS_GCC_SUBST_PREFIX=$GCC_PREFIX

	cat - > $HOME/.scratchbox2/$TARGET/sb2.config.d/gcc$gcc_version_id.config.sh <<EOF
# gcc configuration file generated by sb2-config-gcc-toolchain.
SB2_GCC_INIT_ORIG_ARGS="$SB2_GCC_INIT_ORIG_ARGS"
SB2_GCC_INIT_TIME=$SB2_GCC_INIT_TIME
SB2_GCC_INIT_ID="$SB2_GCC_INIT_ID"
SB2_GCC_INIT_ARCH="$ARCH"

SBOX_GCC_TARGET=$GCC_TARGET
SBOX_DEFAULT_GCC_PREFIX=$GCC_PREFIX
SBOX_CROSS_GCC_NAME=cross-gcc
SBOX_CROSS_GCC_VERSION="$SBOX_CROSS_GCC_VERSION"
SBOX_CROSS_GCC_SHORTVERSION="$SBOX_CROSS_GCC_SHORTVERSION"
SBOX_CROSS_GCC_PREFIX_LIST=$SBOX_CROSS_GCC_PREFIX_LIST
SBOX_CROSS_GCC_SUBST_PREFIX=$SBOX_CROSS_GCC_SUBST_PREFIX
SBOX_CROSS_GCC_SPECS_FILE="$SBOX_CROSS_GCC_SPECS_FILE"
SBOX_CROSS_GCC_DIR=$SBOX_CROSS_GCC_DIR
SBOX_CROSS_GCC_LD_ARGS=

SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS"
SBOX_EXTRA_CROSS_COMPILER_STDINC="$SBOX_EXTRA_CROSS_COMPILER_STDINC"
SBOX_EXTRA_CROSS_LD_ARGS="$SBOX_CROSS_GCC_PREFIX_LIST"
EOF

	cat - > $HOME/.scratchbox2/$TARGET/sb2.config.d/gcc.config$gcc_version_id.lua <<EOF
-- Automatically created gcc config for SB2's lua scripts. Do not edit.

gcc_config$gcc_version_id = {
	target_toolchain_dir="$SBOX_TARGET_TOOLCHAIN_DIR",

	cross_gcc_prefix_list="$SBOX_CROSS_GCC_PREFIX_LIST",
	cross_gcc_dir="$SBOX_CROSS_GCC_DIR",
	cross_gcc_version="$SBOX_CROSS_GCC_VERSION",
	cross_gcc_shortversion="$SBOX_CROSS_GCC_SHORTVERSION",

	extra_cross_compiler_args="$SBOX_EXTRA_CROSS_COMPILER_ARGS",

	cross_gcc_subst_prefix="$SBOX_CROSS_GCC_SUBST_PREFIX",
	cross_gcc_specs_file="$SBOX_CROSS_GCC_SPECS_FILE",
	extra_cross_compiler_stdinc="$SBOX_EXTRA_CROSS_COMPILER_STDINC",
	block_cross_compiler_args="$SBOX_BLOCK_CROSS_COMPILER_ARGS",

	extra_cross_ld_args="$SBOX_EXTRA_CROSS_LD_ARGS",
	block_cross_ld_args="$SBOX_BLOCK_CROSS_LD_ARGS",
}

add_cross_compiler(gcc_config$gcc_version_id, "$gcc_version")

EOF

	if [ -n "$verbose" ] ; then
		echo "Finished writing sb2.gcc.config"
	fi
}

# SB2_GCC_INIT_* variables are used to record who & when this
# toolchain was initialized
# It is possible to set SB2_GCC_INIT_ID externally (for example, if
# another program is used to initialize the system)
SB2_GCC_INIT_ORIG_ARGS="$*"
SB2_GCC_INIT_TIME=`date +%Y-%m-%d_%H:%M:%S`
if [ -z "$SB2_GCC_INIT_ID" ]; then
	SB2_GCC_INIT_ID="user '$USER'"
fi

SBOX_EXTRA_CROSS_COMPILER_ARGS=""

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

verbose=""

while getopts t:m:C:A:hS:R:Vv option
do
	case $option in
	(A) ARCH=$OPTARG ;;
	(h) usage ;;
	(t) TARGET=$OPTARG ;;
	(m) MAPPING_MODE=$OPTARG ;;
	(C) SBOX_EXTRA_CROSS_COMPILER_ARGS="$SBOX_EXTRA_CROSS_COMPILER_ARGS $OPTARG " ;;
	(S) SBOX_DIR=$OPTARG ;;
	(R) SBOX_TARGET_ROOT=$OPTARG ;;
	(V) SECONDARY_COMPILER=yes ;;
	(v) verbose=yes ;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

GCC=$1

# ---------- Check parameters

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

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

SBOX_CONFIG_DIR=~/.scratchbox2/$TARGET/sb2.config.d 

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

if [ ! -d $SBOX_TARGET_ROOT ]; then
        echo "Error: Target root directory does not exist"
        exit 1
fi

if [ -z "$MAPPING_MODE" ]; then
        echo "Error: no mapping mode given"
        exit 1
fi

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

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

GCC_FULLPATH=$(which $GCC)
# test that gcc exists and can be executed
if [ $? != 0 ]; then
	echo "$GCC doesn't exist"
	exit 1
fi

SBOX_CROSS_GCC_DIR=$(dirname $(which $GCC))
if [ $GCC -v > /dev/null 2>&1 != 0 ]; then
	echo "Invalid compiler specified: $GCC"
	exit 1
fi

# ---------- end of parameter checks

case "$(basename $GCC)" in
(*-*)	# $GCC contains a prefix
	GCC_PREFIX=$(basename $GCC | sed 's/-gcc$/-/')
	;;
(*)	# No embedded prefix
	GCC_PREFIX=""
	;;
esac

SBOX_CROSS_GCC_VERSION=`$GCC -dumpversion`

# Create a version string with two digits
SBOX_CROSS_GCC_SHORTVERSION=`echo $SBOX_CROSS_GCC_VERSION |
	sed -e 's/^\([0-9][0-9]*\.[0-9][0-9]*\)\..*/\1/'`

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

# default for the cross-gcc prefix list:
# these may be changed by sb2rc.$MAPPING_MODE

SB2INIT_CROSS_GCC_PREFIX_LIST=$ARCH-linux-

# $ARCH has been set, get mode-specific settings..
if [ -f $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$MAPPING_MODE ]; then
	if [ -n "$verbose" ] ; then
		echo "Reading mode-specific settings.."
	fi
	. $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$MAPPING_MODE "initializing"
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-$SBOX_CROSS_GCC_VERSION-specs-$ARCH.$MAPPING_MODE ]; then
	SBOX_CROSS_GCC_SPECS_FILE="$SBOX_DIR/share/scratchbox2/modeconf/gcc-$SBOX_CROSS_GCC_VERSION-specs-$ARCH.$MAPPING_MODE"
elif [ -f $SBOX_DIR/share/scratchbox2/modeconf/gcc-$SBOX_CROSS_GCC_VERSION-specs.$MAPPING_MODE ]; then
	SBOX_CROSS_GCC_SPECS_FILE="$SBOX_DIR/share/scratchbox2/modeconf/gcc-$SBOX_CROSS_GCC_VERSION-specs.$MAPPING_MODE"
elif [ -f $SBOX_DIR/share/scratchbox2/modeconf/gcc-$SBOX_CROSS_GCC_SHORTVERSION-specs.$MAPPING_MODE ]; then
	SBOX_CROSS_GCC_SPECS_FILE="$SBOX_DIR/share/scratchbox2/modeconf/gcc-$SBOX_CROSS_GCC_SHORTVERSION-specs.$MAPPING_MODE"
elif [ -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

# Note: "sb2" script will replace "@SBOX_TARGET_ROOT@" by the real 
# value when the session is created.
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 - | SBOX_DIR=$SBOX_DIR 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"};')

if [ -z "$SECONDARY_COMPILER" ] ; then
	write_gcc_config
else
	write_gcc_config $SBOX_CROSS_GCC_VERSION
fi

if [ -n "$verbose" ] ; then
	echo "gcc configured."
fi

log_config_action "gcc configured ($SBOX_CROSS_GCC_VERSION)"
if [ -n "$SBOX_CROSS_GCC_SPECS_FILE" ]; then
	log_config_action "(gcc specs from $SBOX_CROSS_GCC_SPECS_FILE)"
fi

exit 0