-
Notifications
You must be signed in to change notification settings - Fork 30
/
gen_toolchain_tool.sh
executable file
·133 lines (119 loc) · 2.64 KB
/
gen_toolchain_tool.sh
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
#!/bin/sh
# Generate a wrapper for Solo5 (G)CC, ld, objcopy and any other binutil
# Expected argument: the tool to generate
# Expected environment variables:
# ARCH: the target architecture (x86_64 or aarch64)
# TOOL_CFLAGS and TOOL_LDFLAGS: extra flags
# SOLO5_TOOLCHAIN: the target for the wrapped Solo5 toolchain
# OTHERTOOLPREFIX: the prefix for tools not in the Solo5 toolchain
# TARGET_X: overrides the command for binutil X
gen_cc() {
# Note that -nostdlib is not required, as it is injected by Solo5' cc, ld
CFLAGS="$TOOL_CFLAGS"
LDFLAGS="$TOOL_LDFLAGS"
EXTRALIBS=""
case "$ARCH" in
aarch64)
EXTRALIBS="-lgcc"
;;
esac
# Add the -Wno-unused-command-line-argument option for clang, as we always
# give it compiling options, even if it will be only linking
# Reuse the test from Solo5 to detect clang
if "$SOLO5_TOOLCHAIN-cc" -dM -E - </dev/null | grep -Eq '^#define __clang__ 1$'
then CFLAGS="-Wno-unused-command-line-argument $CFLAGS"
fi
cat << EOF
#!/bin/sh
# Just like the Solo5 cc, we assume that we are linking, unless we find an
# argument suggesting we are compiling but we call Solo5' cc regardless
compiling=
for arg in "\$@"; do
case "\$arg" in
-[cSE])
compiling="\$arg"
break
;;
esac
done
set -- \\
$CFLAGS \\
-include _solo5/overrides.h \\
-D__BSD_VISIBLE=0 \\
-D__XSI_VISIBLE=0 \\
"\$@"
if [ -z "\$compiling" ]; then
# linking options
set -- \\
"\$@" \\
$LDFLAGS \\
-Wl,--start-group \\
-lnolibc \\
-lopenlibm \\
$EXTRALIBS \\
-Wl,--end-group
fi
[ -n "\${__V}" ] && set -x
exec "$SOLO5_TOOLCHAIN-cc" "\$@"
EOF
}
gen_tool() {
TOOL="$1"
case "$TOOL" in
ar)
TARGET_TOOL="$TARGET_AR"
;;
as)
TARGET_TOOL="$TARGET_AS"
;;
ld)
TARGET_TOOL="$TARGET_LD"
;;
nm)
TARGET_TOOL="$TARGET_NM"
;;
objcopy)
TARGET_TOOL="$TARGET_OBJCOPY"
;;
objdump)
TARGET_TOOL="$TARGET_OBJDUMP"
;;
ranlib)
TARGET_TOOL="$TARGET_RANLIB"
;;
readelf)
TARGET_TOOL="$TARGET_READELF"
;;
strip)
TARGET_TOOL="$TARGET_STRIP"
;;
esac
if test "$TARGET_TOOL" ; then
TOOL="$TARGET_TOOL"
elif command -v -- "$SOLO5_TOOLCHAIN-$TOOL" > /dev/null; then
TOOL="$SOLO5_TOOLCHAIN-$TOOL"
else
case "$TOOL" in
as)
TOOL="$SOLO5_TOOLCHAIN-cc -c"
;;
*)
if command -v -- "$OTHERTOOLPREFIX$TOOL" > /dev/null; then
TOOL="$OTHERTOOLPREFIX$TOOL"
fi
;;
esac
fi
cat << EOF
#!/bin/sh
exec $TOOL "\$@"
EOF
}
case "$1" in
cc|gcc)
gen_cc
;;
*)
gen_tool "$1"
;;
esac