-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·135 lines (119 loc) · 3.3 KB
/
build.sh
File metadata and controls
executable file
·135 lines (119 loc) · 3.3 KB
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
#!/bin/bash
function print_help() {
echo "csvdiff build"
echo ""
echo "usage ./build.sh [OPTIONs]"
echo ""
echo "options:"
echo " --help print this help"
echo " --all build for all platforms (output in bin with platform suffix)"
echo " --upx compress binary with upx"
echo " --clean delete .zig-cache, zig-out and files in bin"
echo " --test run tests (before build)"
echo " --release prepare release (all flags set)"
echo ""
}
function exit_on_error() {
if [ $? -ne 0 ]; then
>&2 echo ""
>&2 echo "an error occured, stopping build"
>&2 echo ""
exit 1
fi
}
function exit_argument_error() {
error=$1
print_help
>&2 echo "error: $error"
>&2 echo ""
exit 1
}
ALL="false"
UPX="false"
TEST="false"
CLEAN="false"
TESTS="false"
RELEASE="false"
while (( "$#" )); do
case "$1" in
-h | --help ) print_help; exit 0;;
--all ) ALL="true"; shift;;
--upx ) UPX="true"; shift;;
--clean ) CLEAN="true"; shift;;
--test ) TESTS="true"; shift;;
--release ) ALL="true"; UPX="true"; TESTS="true"; RELEASE="true"; shift;;
* ) exit_argument_error "unknown command $1";;
esac
done
case "$(uname -s)" in
Linux*) HOST_OS='linux';;
Darwin*) HOST_OS='mac';;
MINGW*) HOST_OS='windows';;
MSYS_NT*) HOST_OS='windows';;
*) HOST_OS="UNKNOWN:${unameOut}"
esac
if [ "$CLEAN" == "true" ]; then
echo cleaning
rm -fr zig-out .zig-cache >/dev/null 2>&1
rm bin/* >/dev/null 2>&1
fi
NATIVE_SUFFIX=""
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
NATIVE_SUFFIX=".exe"
fi
function build_platform() {
PLAT=$1
SUFFIX=$2
COMPILED_BIN=zig-out/bin/csvcut${SUFFIX}
EXE_NAME=bin/csvcut-${PLAT}${SUFFIX}
if [ "$PLAT" == "native" ]; then
EXE_NAME=bin/csvcut${SUFFIX}
fi
#compile
echo building $PLAT
rm -f $EXE_NAME
zig build -Doptimize=ReleaseFast -Dtarget=${PLAT}
#compress with UPX if requested, else copy
if [ "$UPX" = "true" ]; then
echo compressing $PLAT
if [[ "$RELEASE" == "true" ]] && [[ "$PLAT" != "native" ]]; then
upx -qq --lzma $COMPILED_BIN
else
upx -qq --lzma -o $EXE_NAME $COMPILED_BIN
fi
else
cp $COMPILED_BIN $EXE_NAME
fi
#create release packages
if [[ "$RELEASE" == "true" ]] && [[ "$PLAT" != "native" ]]; then
echo packaging $PLAT
if [ "$SUFFIX" == ".exe" ]; then
cd zig-out/bin/
if [ "$HOST_OS" == "windows" ]; then
zip a ../../bin/csvcut-${PLAT}.zip csvcut.exe > /dev/null
else
zip ../../bin/csvcut-${PLAT}.zip csvcut.exe > /dev/null
fi
cd ../../
else
gzip -c zig-out/bin/csvcut > ./bin/csvcut-${PLAT}.gz
fi
fi
}
# build native
echo
build_platform "native" $NATIVE_SUFFIX
if [ "$TESTS" == "true" ]; then
echo testing
zig build test || exit_on_error
fi
# build all
if [ "$ALL" = "true" ]; then
echo
build_platform "x86_64-windows" ".exe"
echo
build_platform "x86_64-linux"
echo
build_platform "aarch64-linux"
echo
fi