-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path90s.sh
executable file
·160 lines (138 loc) · 4.41 KB
/
90s.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
#!/usr/bin/env sh
DEFAULT_C_COMPILER="cc"
DEFAULT_COMPILER="c++"
EXE_EXT=""
SO_EXT="so"
if ! [ -x "$(command -v $DEFAULT_COMPILER)" ]; then
DEFAULT_COMPILER="clang++"
fi
if ! [ -x "$(command -v $DEFAULT_C_COMPILER)" ]; then
DEFAULT_COMPILER="clang"
fi
FLAGS="-s -O2"
OUT="${OUT:-bin/90s}"
CXX="${CXX:-$DEFAULT_COMPILER}"
CC="${CC:-$DEFAULT_C_COMPILER}"
mkdir -p bin
mkdir -p bin/obj
DEFINES="-DS90_SHARED_ORM"
LIBS="-lm -ldl -lpthread -lcrypto -lssl -lz -lresolv"
if [ "$(uname -o)" = "Msys" ]; then
SO_EXT="dll"
EXE_EXT=".exe"
LIBS=$(echo "$LIBS" | sed "s/-ldl//g")
LIBS="$LIBS -lws2_32 -lmswsock -lcrypt32 -liconv"
fi
if [ $(uname) = "FreeBSD" ]; then
LIBS=$(echo "$LIBS" | sed "s/-lresolv//g")
fi
if [ ! -z "$DEBUG_LEVEL" ]; then
DEFINES="$DEFINES -DS80_DEBUG_LEVEL=$DEBUG_LEVEL"
FLAGS="-O0 -ggdb"
else
FLAGS="$FLAGS -march=native"
fi
if [ "$INFO" = "true" ]; then
DEFINES="$DEFINES -S80_DEBUG_INFO=1"
fi
echo "Defines: $DEFINES"
echo "Libraries: $LIBS"
echo "Flags: $FLAGS"
arg="$1"
if [ "$1" = "clean" ]; then
rm -rf bin/obj
arg="$2"
fi
modtime() {
if [ $(uname) = "FreeBSD" ]; then
stat -f "%Sm" "$1"
else
stat -c %y "$1"
fi
}
xmake() {
cc="$1"
flags="$2"
libs="$3"
static_libs="$4"
outname="$5"
obj_files=""
if [ "$static_libs" = "-" ]; then
static_libs=""
fi
for i in `seq 6 $#`; do
file=$(eval echo \${$i})
echo "> Compiling $file"
dir=$(dirname "$file")
mkdir -p "bin/obj/$dir"
obj_file=$(echo "bin/obj/$file" | sed -e s/\\.c\$/.o/g | sed -e s/\\.cpp\$/.o/g)
do_compile="1"
# detect if the file exists and if it exists, check if .obj last modified date == .cpp last modified date
if [ -f "$obj_file" ]; then
script_time=$(modtime $file)
obj_time=$(modtime $obj_file)
if [ "$obj_time" = "$script_time" ]; then
do_compile="0"
fi
fi
# only recompile when .cpp was updated compared to .obj file
if [ $do_compile -eq "1" ]; then
$cc $flags -c $file -o "$obj_file"
if [ -f "$obj_file" ]; then
touch -r "$file" "$obj_file"
fi
fi
obj_files="$obj_files $obj_file"
done
# link it
if [ $(echo "$outname" | tail -c 3) = ".a" ]; then
# if output binary ends with .a, link it as a library
ar rcs "$outname" $obj_files
else
# otherwise link it as an executable or dynamic linked library
$cc -o "$outname" $flags $obj_files $static_libs $libs
fi
}
echo "Compiling lib80s"
xmake "$CC" "$FLAGS -fPIC $DEFINES" "$LIBS" "-" "bin/lib80s.a" \
src/80s/80s.c src/80s/80s_common.c src/80s/80s_common.windows.c src/80s/dynstr.c src/80s/algo.c src/80s/crypto.c \
src/80s/serve.epoll.c src/80s/serve.kqueue.c src/80s/serve.iocp.c
FLAGS="$FLAGS -std=c++23 -Isrc/ -fPIC -fcoroutines"
echo "Compiling template compiler"
xmake "$CXX" "$FLAGS" "$LIBS" "bin/lib80s.a" "bin/template$EXE_EXT" src/90s/httpd/template_compiler.cpp
if [ "$arg" = "pages" ]; then
# Detect all .htmls in webroot and compile them into separate .so/.dlls
echo "Compiling webpages"
WEB_ROOT="${WEB_ROOT:-src/90s/httpd/pages/}"
FLAGS="$FLAGS $DEFINES -shared"
to_translate=$(find "$WEB_ROOT" -name *.html -type f | grep -v .priv.html)
for file in $to_translate; do
echo "> Translating $file"
bin/template "$file" "$file" $(echo "$file" | sed s/\\.html$/.html.cpp/g)
touch -r "$file" "$(echo "$file" | sed s/\\.html$/.html.cpp/g)"
done
to_compile=$(find "$WEB_ROOT" -name "*.html.cpp" -type f)
for file in $to_compile; do
outname=$(echo "$file" | sed s/\\.cpp$/.$SO_EXT/g)
xmake "$CXX" "$FLAGS" "$LIBS" "bin/lib80s.a" "$outname" "$file"
done
# Detect all .cpps that aren't generated from .htmls and compile main lib
to_compile=$(find "$WEB_ROOT" -name "*.cpp" -type f | grep -v ".html.cpp")
if [ ! -z "$to_compile" ]; then
echo "Compiling main.$SO_EXT"
xmake "$CXX" "$FLAGS" "$LIBS" "bin/lib80s.a" "${WEB_ROOT}main.$SO_EXT" $to_compile
fi
else
echo "Compiling 90s web server"
FLAGS="$FLAGS $DEFINES"
xmake "$CXX" "$FLAGS" "$LIBS" "bin/lib80s.a" "$OUT" \
src/90s/90s.cpp src/90s/afd.cpp src/90s/context.cpp \
src/90s/httpd/environment.cpp src/90s/httpd/render_context.cpp src/90s/httpd/server.cpp \
src/90s/util/util.cpp \
src/90s/sql/mysql.cpp src/90s/sql/mysql_util.cpp \
src/90s/storage/disk_storage.cpp\
src/90s/dns/doh.cpp \
src/90s/dns/resolv.cpp \
src/90s/httpd/client.cpp \
src/90s/actors/actor.cpp
fi