File c added (mode: 100755) (index 0000000..d027162) |
|
1 |
|
#!/bin/sh |
|
2 |
|
|
|
3 |
|
################################################################### |
|
4 |
|
# Wrapper around c compilers |
|
5 |
|
# Makes compiling and writing c easier |
|
6 |
|
# for people who write small programs (unfortunately not everyone). |
|
7 |
|
# Mostly made for personal workflow. |
|
8 |
|
# Inspired by the Go compiler. |
|
9 |
|
################################################################### |
|
10 |
|
|
|
11 |
|
if [ "$1" = "" ] |
|
12 |
|
then |
|
13 |
|
echo "$0 [command] (file|foldername)" | sed "s|/.*/||g" |
|
14 |
|
echo |
|
15 |
|
echo "commands:" |
|
16 |
|
echo |
|
17 |
|
echo " build compiles all c files in a directory" |
|
18 |
|
echo " mk compiles a single file using tcc" |
|
19 |
|
echo " run compiles and runs the c file pointed to" |
|
20 |
|
echo " clean removes the binary named after the directory" |
|
21 |
|
echo |
|
22 |
|
|
|
23 |
|
exit |
|
24 |
|
fi |
|
25 |
|
|
|
26 |
|
whatdo="$1" |
|
27 |
|
file="$2" # optional |
|
28 |
|
|
|
29 |
|
CC="clang" |
|
30 |
|
while getopts ":c:" c; do |
|
31 |
|
case "$c" in |
|
32 |
|
c) CC="$OPTARG"; whatdo="$3"; file="$4" |
|
33 |
|
;; |
|
34 |
|
esac |
|
35 |
|
done |
|
36 |
|
# add all the link options you can think of. |
|
37 |
|
translate="s|math.h|-lm|; s|xcb/xcb.h|-lxcb|; s|xcb/xcb_util.h|-lxcb-util|;" |
|
38 |
|
|
|
39 |
|
stdgone="s/assert.h|ctype.h|dirent.h|errno.h|fcntl.h|inttypes.h|limits.h|locale.h|setjmp.h|signal.h|stdarg.h|stdio.h|string.h|stdlib.h|time.h|unistd.h|sys\/types.h|sys\/wait.h|iostream//g;" |
|
40 |
|
|
|
41 |
|
cfiletext="^//cfiles: " # uses grep regex |
|
42 |
|
|
|
43 |
|
|
|
44 |
|
flags() { |
|
45 |
|
incflangs="" |
|
46 |
|
cflags="" |
|
47 |
|
cfiles="$(grep -h "$cfiletext" *.c | cut -b 11-)" |
|
48 |
|
include="$(grep -h "^#include <" *.c | uniq | sed "s/#include <//g;s/>//g;$translate;$stdgone")" |
|
49 |
|
|
|
50 |
|
echo $cfiles $cflags $include $incflags |
|
51 |
|
} |
|
52 |
|
|
|
53 |
|
singleflags() { |
|
54 |
|
incflangs="" |
|
55 |
|
cflags="" |
|
56 |
|
cfiles="$(grep "$cfiletext" "$file" | cut -c 11-)" |
|
57 |
|
include="$(grep -h "^#include <" $file $cfiles | uniq | sed -E "s/#include <//g;s/>//g;$translate;$stdgone")" |
|
58 |
|
|
|
59 |
|
echo $cfiles $cflags $include $incflags |
|
60 |
|
} |
|
61 |
|
|
|
62 |
|
build() { |
|
63 |
|
[ "$file" != "" ] && [ -d "$file" ] && cd "$file" |
|
64 |
|
arecfiles="$(ls -l | grep "\.c")" |
|
65 |
|
[ "$arecfiles" = "" ] && echo "not a buildable directory" && exit |
|
66 |
|
binname="$(pwd | sed "s|/.*/||g")" # The name of the working directory |
|
67 |
|
allflags="$(flags)" |
|
68 |
|
$CC $allflags -o "$binname" *.c |
|
69 |
|
} |
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
mak() { |
|
74 |
|
[ ! -e "$file" ] || [ -d "$file" ] && echo "give one existing file name" && exit |
|
75 |
|
filebase="${file%.*}" |
|
76 |
|
allflags="$(singleflags)" |
|
77 |
|
$CC -o "$filebase" $allflags "$file" -lfl |
|
78 |
|
} |
|
79 |
|
|
|
80 |
|
|
|
81 |
|
clean() { |
|
82 |
|
[ "$file" != "" ] && [ -d "$file" ] && cd "$file" |
|
83 |
|
binname="$(pwd | sed "s|/.*/||g")" |
|
84 |
|
[ -f "$binname" ] && rm "$binname" |
|
85 |
|
} |
|
86 |
|
|
|
87 |
|
case "$whatdo" in |
|
88 |
|
"build") build ;; |
|
89 |
|
"debug") debugbuild ;; |
|
90 |
|
"mk") mak ;; |
|
91 |
|
"run") mak && "$(readlink -f $filebase)" ;; |
|
92 |
|
"drun") debugonone ;; |
|
93 |
|
"clean") clean ;; |
|
94 |
|
esac |