-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·149 lines (133 loc) · 3.06 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
#!/bin/sh
# Basic Config, edit these variabl
PROJECT_NAME="z_"
CC=gcc
CCFLAGS="-Wall -Wextra -O2"
LIBFLAGS=""
TESTFLAGS="-ggdb -g"
AR=ar
ARFLAGS="-q"
OUT_BIN="z_"
OUT_LIB="libz_.a"
OUT_HEAD="z_"
BIN_DIR="/usr/local/bin/"
LIB_DIR="/usr/local/lib/"
INC_DIR="/usr/local/include/"
BIN_DIR="$HOME/../usr/bin/"
LIB_DIR="$HOME/../usr/lib/"
INC_DIR="$HOME/../usr/include/"
check_mkdir() {
if [ -d "$1" ]; then
continue
else
$(mkdir "$1")
fi
}
check_build() {
check_mkdir "./build"
check_mkdir "./build/lib"
check_mkdir "./build/include"
check_mkdir "./build/bin"
}
make_clean() {
clean_what="./build"
casevar=$1
case "$casevar" in
"lib") clean_what="$clean_what/lib"
;;
"headers") clean_what="$clean_what/include"
;;
"test") clean_what="$clean_what/test"
;;
"bin") clean_what="$clean_what/bin"
;;
"ofiles") clean_what="*.o"
;;
"all") clean_what="./build/* *.o"
;;
*) echo "Unknown option '$*'"
return
;;
esac
$(rm -rf $clean_what)
}
make_lib_header() {
name=$OUT_HEAD
$(find "./src/lib" -name '*.h' | cpio -pdm ./build/include)
$(rm -rf ./build/$name)
$(mv ./build/include/src/lib ./build/include/$name)
$(rm -rf ./build/include/src)
echo "Made headers as $name in ./build/include"
}
make_lib(){
extraflags="$1"
FILES=$(find "./src/lib" -name '*.c')
$($CC $CCFLAGS $extraflags -c $FILES)
$($AR $ARFLAGS $OUT_LIB *.o)
make_clean "all"
check_build
$(mv $OUT_LIB ./build/lib)
}
make_bin(){
make_lib
FILES=$(find "./src/bin" -name '*.c')
$(mkdir ./build/bin)
$($CC $CCFLAGS $LIBFLAGS $FILES ./build/lib/$OUT_LIB -o ./build/bin/$OUT_BIN)
}
make_install_lib() {
make_lib
$(mv -ni ./build/lib/$OUT_LIB "$LIB_DIR")
make_lib_header
$(cp -Rf ./build/include/$OUT_HEAD "$INC_DIR")
$(rm -rf ./build/include/$OUT_HEAD)
}
make_install_bin() {
make_bin
$(mv -ni ./build/bin/$OUT_BIN "$BIN_DIR")
}
make_install() {
case "$1" in
"bin") make_install_bin
;;
"lib") make_install_lib
;;
*) echo "Unknown option '$1', either use 'bin' or 'lib'"
;;
esac
}
make_test_lib(){
make_lib $TESTFLAGS
FILES=$(find "./src/testlib" -name '*.c')
$($CC $CCFLAGS $TESTFLAGS $FILES ./build/lib/$OUT_LIB -o "./build/testlib")
case "$1" in
"run") $(./build/testlib)
;;
*) echo "Use './build.sh testlib run' to build & run the test"
;;
esac
}
make() {
check_build
case "$1" in
"lib") make_lib
;;
"headers") make_lib_header
;;
"bin") make_bin
;;
"install") make_install $2
;;
"testlib") make_test_lib $2
;;
"testbin") make_test_bin $2
;;
"clean")
make_clean $2
;;
*)
echo "Unknown option '$1'"
echo "Usage: $0 [ lib | headers | bin | testlib | testbin | install[lib|bin]]"
;;
esac
}
make $*