-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
200 lines (171 loc) · 5.02 KB
/
build.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
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
#!/bin/bash
# Copyright (c) 2014, 2016 Robert Clipsham <robert@octarineparrot.com>
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
# Build script, since cargo doesn't currently work on FreeBSD
[[ "$VERBOSE" == "1" ]] && set -x
RUSTC="$(which rustc)"
RUSTDOC="$(which rustdoc)"
CARGO="$(which cargo)"
SUDO="$(which sudo)"
SYSTEM="$(uname -s)"
TESTER="$CARGO test"
CC="$(which clang || which gcc)"
MACROS_WITH_SYNTEX=0
if [[ -n "$PNET_FEATURES" ]]; then
PNET_CARGO_FLAGS="--no-default-features --features \"$PNET_FEATURES\""
else
PNET_CARGO_FLAGS=""
fi
if [[ -n "$PNET_MACROS_FEATURES" ]]; then
PNET_MACROS_CARGO_FLAGS="--no-default-features --features \"$PNET_MACROS_FEATURES\""
else
PNET_MACROS_CARGO_FLAGS=""
fi
# FIXME Need to get interface differently on Windows
IFCONFIG=$(which ifconfig)
IPROUTE2=$(which ip)
echo $PNET_MACROS_FEATURES | grep -q with-syntex && MACROS_WITH_SYNTEX=1
PNET_TEST_IFACE=""
if [[ -x "$IFCONFIG" ]]; then
PNET_TEST_IFACE=$($IFCONFIG | egrep 'UP| active' | \
perl -pe '/^[A-z0-9]+:([^\n]|\n\t)*status: active/' | \
grep active -B1 | head -n1 | cut -f1 -d:)
fi
if [[ -z "$PNET_TEST_IFACE" && -x "$IPROUTE2" ]]; then
PNET_TEST_IFACE=$($IPROUTE2 link show | grep 'UP' | head -n1 | \
cut -f2 -d: | xargs)
fi
# https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
if [[ -z "$PNET_TEST_IFACE" && "$SYSTEM" = "Linux" ]]; then
for file in /sys/class/net/*/carrier; do
tmp=$(cat "$file")
if [[ "$tmp" -eq 1 ]]; then
PNET_TEST_IFACE=$(echo "$file" | cut -d '/' -f 5)
break
fi
done
fi
set -euo pipefail
# FIXME Need to link libraries properly on Windows
build() {
if [[ -x "$CARGO" ]]; then
"$CARGO" build $PNET_CARGO_FLAGS --release
else
"$RUSTC" src/lib.rs
fi
}
build_doc() {
if [[ -x "$CARGO" ]]; then
"$CARGO" doc $PNET_CARGO_FLAGS
else
"$RUSTDOC" src/lib.rs -o target/doc --crate-name pnet
fi
}
build_test() {
if [[ -x "$CARGO" ]]; then
"$CARGO" test --no-run $PNET_CARGO_FLAGS
"$CARGO" bench --no-run $PNET_CARGO_FLAGS
else
"$RUSTC" src/lib.rs --test --out-dir ./target/ -C extra-filename=-no-cargo
fi
}
run_macro_tests() {
cd pnet_macros &&
sh -c "$CARGO test $PNET_MACROS_CARGO_FLAGS" &&
cd ..
}
run_packet_tests() {
cd pnet_packet &&
sh -c "$CARGO test" &&
cd ..
}
run_test() {
run_macro_tests &&
run_packet_tests &&
export RUST_TEST_THREADS=1 &&
case "$SYSTEM" in
Linux)
"$SUDO" -E sh -c "$CARGO build $PNET_CARGO_FLAGS --release && \
$CARGO test $PNET_CARGO_FLAGS && \
$CARGO bench --no-run $PNET_CARGO_FLAGS && \
$CARGO doc $PNET_CARGO_FLAGS"
;;
FreeBSD|Darwin)
export PNET_TEST_IFACE
"$SUDO" -E bash -c "$CARGO build $PNET_CARGO_FLAGS && \
$CARGO test $PNET_CARGO_FLAGS && \
$CARGO bench --no-run $PNET_CARGO_FLAGS && \
$CARGO doc $PNET_CARGO_FLAGS"
;;
MINGW*|MSYS*)
PNET_TEST_IFACE=$PNET_TEST_IFACE RUST_TEST_THREADS=1 $TESTER
;;
FreeBSD|*)
echo "Unsupported testing platform"
;;
esac
}
clean() {
if [[ -x "$CARGO" ]]; then
"$CARGO" clean $PNET_CARGO_FLAGS
else
rm -fr target
fi
}
benchmarks() {
[[ "$SYSTEM" != "Darwin" ]] && echo warning: C benchmarks only work on OS X
"$CC" -W -Wall -O2 benches/c_receiver.c -o target/benches/c_receiver
"$CC" -W -Wall -O2 benches/c_sender.c -o target/benches/c_sender
"$RUSTC" -O benches/rs_receiver.rs --out-dir target/benches -L target/release
"$RUSTC" -O benches/rs_sender.rs --out-dir target/benches -L target/release
}
travis_script() {
case "$SYSTEM" in
Linux)
"$SUDO" sed -i 's/secure_path="/secure_path="\/home\/travis\/.cargo\/bin:/' /etc/sudoers
;;
Darwin)
echo Defaults secure_path = \"$PATH\" | $SUDO tee -a /etc/sudoers
;;
*)
echo "Unsupported travis platform"
exit 1
;;
esac
run_test
}
mkdir -p target/doc
mkdir -p target/benches
if [[ "$VERBOSE" == "1" ]]; then
PNET_CARGO_FLAGS="$PNET_CARGO_FLAGS --verbose"
PNET_MACROS_CARGO_FLAGS="$PNET_MACROS_CARGO_FLAGS --verbose"
TESTER="$TESTER $PNET_CARGO_FLAGS"
fi
if [[ ! -x "$CARGO" ]]; then
TESTER='./target/pnet-*'
fi
case "$1" in
test)
run_test
;;
doc)
build_doc
;;
clean)
clean
;;
benchmarks)
benchmarks
;;
travis_script)
travis_script
;;
*)
build
;;
esac