-
Notifications
You must be signed in to change notification settings - Fork 14
/
configure
executable file
·184 lines (184 loc) · 6.13 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
#!/bin/sh
#
# SPDX-License-Identifier: MIT
#
# This file is part of ruri, with ABSOLUTELY NO WARRANTY.
#
# MIT License
#
# Copyright (c) 2022-2024 Moe-hacker
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
error() {
printf "$@\n"
exit 1
}
init() {
printf "checking for make... "
if ! command -v make; then
error "not found"
fi
printf "checking for strip... "
if ! command -v strip; then
error "not found"
fi
export STRIP=$(realpath $(command -v strip))
printf "checking for compiler... "
if [ ! $CC ]; then
if [ ! $(command -v cc) ]; then
error "not found"
fi
CC=$(realpath $(command -v cc))
export CC=${CC##*/}
fi
printf "$CC\n"
printf "checking whether the compiler supports GNU C11... "
(echo "int main(){}" | $CC -x c -o /dev/null -std=gnu11 -) >/dev/null 2>&1 && printf "ok\n" || error "no"
if [ $STATIC_COMPILE ]; then
printf "checking whether the compiler supports -static compile... "
echo "int main(){}" | $CC -static -x c -o /dev/null -std=gnu11 - >/dev/null 2>&1 && printf "ok\n" || error "no"
fi
}
test_and_add_cflag() {
printf "checking whether the compiler supports $1... "
if echo "int main(void){}" | $CC $1 -Werror -x c -o /dev/null - >/dev/null 2>&1; then
printf "ok\n" && export CFLAGS="$CFLAGS $1"
else
printf "no\n"
fi
}
test_and_add_ldflag() {
printf "checking for $1... "
if [ $STATIC_COMPILE ]; then
if echo "int main(){}" | $CC -static -x c -o /dev/null - $1 >/dev/null 2>&1; then
printf "ok\n" && export LD_FLAGS="$LD_FLAGS $1"
else
printf "no\n"
fi
else
if echo "int main(){}" | $CC -x c -o /dev/null - $1 >/dev/null 2>&1; then
printf "ok\n" && export LD_FLAGS="$LD_FLAGS $1"
else
printf "no\n"
fi
fi
}
check_header() {
printf "checking for header $i... "
printf "#include <$1>\nint main(){}" | $CC -x c -o /dev/null - >/dev/null 2>&1 && printf "ok\n" || error "not found"
}
default_cflag() {
test_and_add_cflag "-ftrivial-auto-var-init=pattern"
test_and_add_cflag "-fcf-protection=full"
test_and_add_cflag "-flto=auto"
test_and_add_cflag "-fPIE"
test_and_add_cflag "-pie"
test_and_add_cflag "-Wl,-z,relro"
test_and_add_cflag "-Wl,-z,noexecstack"
test_and_add_cflag "-Wl,-z,now"
test_and_add_cflag "-fstack-protector-all"
test_and_add_cflag "-fstack-clash-protection"
test_and_add_cflag "-mshstk"
test_and_add_cflag "-Wno-unused-result"
test_and_add_cflag "-O2"
test_and_add_cflag "-Wl,--build-id=sha1"
test_and_add_cflag "-ffunction-sections"
test_and_add_cflag "-fdata-sections"
test_and_add_cflag "-Wl,--gc-sections"
if [ $STATIC_COMPILE ]; then
test_and_add_cflag "-static"
fi
test_and_add_cflag "-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3"
test_and_add_ldflag "-lcap"
test_and_add_ldflag "-lseccomp"
test_and_add_ldflag "-lpthread"
}
dev_cflag() {
test_and_add_cflag "-g"
test_and_add_cflag "-O0"
test_and_add_cflag "-fno-omit-frame-pointer"
test_and_add_cflag "-Wl,-z,norelro"
test_and_add_cflag "-Wl,-z,execstack"
test_and_add_cflag "-fno-stack-protector"
test_and_add_cflag "-Wall"
test_and_add_cflag "-Wextra"
test_and_add_cflag "-pedantic"
test_and_add_cflag "-Wconversion"
test_and_add_cflag "-Wno-newline-eof"
test_and_add_cflag "-Wno-gnu-zero-variadic-macro-arguments"
test_and_add_cflag "-fsanitize=address"
test_and_add_cflag "-Wl,--build-id=sha1"
test_and_add_cflag "-ffunction-sections"
test_and_add_cflag "-fdata-sections"
test_and_add_cflag "-Wl,--gc-sections"
if [ $DEBUG_BUILD ]; then
test_and_add_cflag "-DRURI_DEBUG"
fi
test_and_add_cflag "-DRURI_DEV"
if [ $STATIC_COMPILE ]; then
test_and_add_cflag "-static"
fi
test_and_add_ldflag "-lcap"
test_and_add_ldflag "-lseccomp"
test_and_add_ldflag "-lpthread"
export STRIP=true
}
check_headers_and_libs() {
# Check system headers.
for i in fcntl.h sys/ioctl.h sys/mount.h sys/socket.h unistd.h; do
check_header $i
done
# Check library headers.
for i in sys/capability.h seccomp.h pthread.h; do
check_header $i
done
}
show_help() {
echo "Usage: ./configure [OPTION]..."
echo " -h, --help show help"
echo " -s, --static compile static binary"
echo " -d, --dev compile dev version"
echo " -D, --debug compile with debug log"
}
while [ $1 ]; do
case $1 in
"-h" | "--help") show_help && exit ;;
"-s" | "--static") export STATIC_COMPILE=true ;;
"-d" | "--dev") export DEV_BUILD=true ;;
"-D" | "--debug") export DEBUG_BUILD=true ;;
esac
shift
done
init
check_headers_and_libs
if [ ! $DEV_BUILD ]; then
default_cflag
else
dev_cflag
fi
printf "create config.mk... "
if command -v git >/dev/null; then
CFLAGS="$CFLAGS -DRURI_COMMIT_ID=\\\"$(git rev-parse --short HEAD)\\\""
fi
echo "CFLAGS = $CFLAGS" >config.mk
echo "LD_FLAGS = $LD_FLAGS" >>config.mk
echo "CC = $CC" >>config.mk
echo "STRIP = $STRIP" >>config.mk
printf "ok\n"