forked from hlibc/hlibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure
executable file
·214 lines (176 loc) · 3.98 KB
/
configure
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
#!/bin/sh
# hlibc configure script
print() {
printf -- "%s\n" "$*"
}
menu()
{
print "hlibc 2016-2018, CM Graff et al.
Usage:
--prefix=PREFIX main installation prefix [/usr/local/hlibc]
Command line variables are added last to the config.mak and passed
to the makefile. For instance:
./configure AS='cc -x assembler'
"
exit 0
}
fatal()
{
(>&2 print "fatal" "$*")
exit 1
}
detectcommand()
{
printf "trying $i .. "
if command -v "$1" >/dev/null 2>&1
then print "yes"
return 0
else print "no"
return 1
fi
}
inspect_compiler()
{
printf "trying %s .. " "$2"
print "long y = 1;" > "$temporary_file"
print "double z = 0;" >> "$temporary_file"
if "$CC" "$2" -c -o .temporary "$temporary_file" 2> .temporary
then print "yes"
eval "$1=\"\${$1} \$2\""
eval "$1=\${$1# }"
return 0
else print "no"
return 1
fi
}
OPERATING_SYSTEM="linux"
FREESTANDING=
CFLAGS_SUPPLEMENT=
prefix="/usr/local/hlibc"
architecture=
print "# Automatically generated" > config.mak
for i in "$@"
do case "$i" in
-h|--help)
menu
;;
--prefix=*)
prefix=${i#*=}
;;
*=*)
# Let these be added at the end
;;
*)
fatal "$i is not a valid option !!"
;;
esac
done
# Detect the operating system
OS_DIRTY_STR=$(uname -a)
case $OS_DIRTY_STR in
*linux*|*Linux*)
OPERATING_SYSTEM="linux"
;;
*aix*)
OPERATING_SYSTEM="aix"
;;
*FreeBSD*|*freebsd*|*Freebsd*)
print "Note that the FreeBSD 'c99' command has a bug and won't accept '-dumpmachine'"
OPERATING_SYSTEM="freebsd"
;;
*NetBSD*|*netbsd*|*Netbsd*)
print "NetBSD is not fully supported."
print "It will build, and likely works, however NetBSD needs a note in the ELF headers"
print "which hlibc has no functionality to provide at this time."
OPERATING_SYSTEM="netbsd"
;;
*Darwin*|*darwin*)
OPERATING_SYSTEM="macos"
;;
*OpenBSD*|*openbsd*|*Openbsd*)
OPERATING_SYSTEM="openbsd"
;;
esac
# Obtain a temporary file
temporary_file="./temporary_work_file"
[ -f "${temporary_file}" ] && rm "$temporary_file"
print "Attempting to detect a C compiler ..."
if [ -z "${CC}" ]
then for i in unlikely-cc gcc cc clang c99 pcc tcc
do if detectcommand "$i"
then CC="$i"
break
fi
done
if [ -z "${CC}" ]
then fatal "No compiler was able to be found. Giving up."
fi
else detectcommand "${CC}"
fi
# Find target architecture
printf "Automatically detecting the computer architecture: "
architecture=$("$CC" -dumpmachine 2>/dev/null)
if [ -z "$architecture" ]
then print
print "Warning: $CC does not appear to support -dumpmachine"
print "Falling back to uname -m which is not compiler specific"
print "This will mess up cross compiling unless your host and target are the same"
architecture=$(uname -m 2>/dev/null)
fi
print "$architecture"
case "$architecture" in
*i*86*)
ARCH="i386"
;;
*arm64*|*aarch64*)
ARCH="aarch64"
;;
*amd64*|x86_64*)
ARCH="x86_64"
;;
*)
fatal "Unknown architecture !!"
;;
esac
# one offs
AS="as"
if [ $OPERATING_SYSTEM = "openbsd" -a $ARCH = "aarch64" ]
then export AS="$CC -x assembler"
fi
# Obtain a freestanding C99 env
inspect_compiler FREESTANDING -ffreestanding
inspect_compiler FREESTANDING -std=c99
inspect_compiler FREESTANDING -nostdinc
inspect_compiler CFLAGS_SUPPLEMENT -pipe
inspect_compiler CFLAGS_SUPPLEMENT -Os
# Correct broken compiler installations
inspect_compiler CFLAGS_SUPPLEMENT -fno-stack-protector
inspect_compiler DISABLE_PIE -fno-pie
inspect_compiler DISABLE_PIE -no-pie
# Create the config.mak
print "Generating a config.mak for the main Makefile"
print "
ARCH = $ARCH
prefix = $prefix
CC = $CC
CFLAGS += $CFLAGS_SUPPLEMENT $CFLAGS
FREESTANDING += $FREESTANDING
DISABLE_PIE += $DISABLE_PIE
OPERATING_SYSTEM += $OPERATING_SYSTEM
AS = $AS
" > config.mak
# Adjust the compiler wrapper if clang is being used
case "$CC" in
*clang*)
print "CC_IS_CLANG = yes" >> config.mak
print "" >> config.mak
;;
esac
for i in "$@"
do case "$i" in
*=*) print "${i%=*} = ${i#*=}" >> config.mak ;;
esac
done
# Clean up and report
rm "$temporary_file" .temporary
print "done"