-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc
82 lines (73 loc) · 2.03 KB
/
cc
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
#!/bin/bash
# Check if enough arguments are provided
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <source_file> [output_file]"
exit 1
fi
source_file="$1"
output_file="$2"
# Remove old generated directory if it exists
if [ -e "./generated" ]; then
rm -rf ./generated
fi
mkdir -p generated
# Compile with clang
clang -o ./generated/clang "$source_file" -Oz -w #-static
if [ -e "./generated/clang" ]; then
size=$(stat -c%s "./generated/clang")
size_kb=$(echo "scale=2; $size / 1024" | bc)
echo "clang: output $size_kb kilobytes"
else
echo "clang: file failed to compile"
fi
# Compile with tcc
tcc -o ./generated/tcc "$source_file" -w
if [ -e "./generated/tcc" ]; then
size=$(stat -c%s "./generated/tcc")
size_kb=$(echo "scale=2; $size / 1024" | bc)
echo "tcc: output $size_kb kilobytes"
else
echo "tcc: file failed to compile"
fi
# Compile with gcc
gcc -o ./generated/gcc "$source_file" -Oz -w #-static
if [ -e "./generated/gcc" ]; then
size=$(stat -c%s "./generated/gcc")
size_kb=$(echo "scale=2; $size / 1024" | bc)
echo "gcc: output $size_kb kilobytes"
else
echo "gcc: file failed to compile"
fi
# Determine output filename
if [ -z "$output_file" ]; then
# If no output filename specified, use source filename minus extension
base_name=$(basename "$source_file" .c)
output_file="./${base_name}"
else
# Use the specified output filename
output_file="${output_file}"
fi
# Remove old binary if one already exists
if [ -e "$output_file" ]; then
rm -rf $output_file
fi
# Copy one of the binaries over
if [ -e "./generated/clang" ]; then
cp ./generated/clang "$output_file"
else
echo "clang binary not found, trying tcc"
if [ -e "./generated/tcc" ]; then
cp ./generated/tcc "$output_file"
else
echo "tcc binary not found, trying gcc"
if [ -e "./generated/gcc"]; then
cp ./generated/gcc "$output_file"
else
echo "compilation failed"
fi
fi
fi
if [ -e $output_file ]; then
echo
$output_file
fi