-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_C.sh
executable file
·82 lines (79 loc) · 3.61 KB
/
compile_C.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
#!/usr/bin/env bash
set -u
export ANSI_RED=$'\e[41m' ANSI_MAGENTA=$'\e[45m' ANSI_GREEN=$'\e[42m' ANSI_BLUE=$'\e[44m' ANSI_YELLOW=$'\e[43m' ANSI_WHITE=$'\e[47m' ANSI_BLACK=$'\e[40m'
export ANSI_FG_GREEN=$'\e[32m' ANSI_FG_RED=$'\e[31m' ANSI_FG_MAGENTA=$'\e[35m' ANSI_FG_GRAY=$'\e[30;1m' ANSI_FG_BLUE=$'\e[34;1m' ANSI_FG_BLACK=$'\e[100;1m' ANSI_FG_YELLOW=$'\e[33m' ANSI_FG_WHITE=$'\e[37m'
export ANSI_INVERSE=$'\e[7m' ANSI_BOLD=$'\e[1m' ANSI_UNDERLINE=$'\e[4m' ANSI_RESET=$'\e[0m'
CCOMPILER=clang
! clang --version |head -n 1 && CCOMPILER=gcc
dirpaths_with_pfx(){
local pfx=$1 d
shift
for d in "$@"; do [[ -d $d ]] && echo -n "$pfx$d"; done
}
main(){
echo $ANSI_INVERSE"This is $0"$ANSI_RESET
local src=${1:-} is_bashbuiltin=0
[[ -z $src ]] && echo -e "Compile a file with gcc or clang.\nUsage: ${ANSI_BLACK}${ANSI_FG_GREEN}$0 my-source-file.c$ANSI_RESET" && return 1
local includes="$(grep '^#include ' $src)"
[[ /$src == */bashbuiltin_*.c ||
$includes == *\"loadables.h\"* ||
$includes == *\"bashtypes.h\"* ||
$includes == *\"bashgetopt.h\"* ]] && echo "Compiling a bash builtin" && is_bashbuiltin=1
local as="-O0 -fsanitize=address -fno-omit-frame-pointer"
SEARCH_INCLUDE_FILES='/usr/include/postgresql /usr/lib /opt/local/include /opt/local/lib'
((is_bashbuiltin)) && as='' && SEARCH_INCLUDE_FILES+=' /usr/lib/bash /usr/include/bash /usr/include/bash/include /usr/include/bash/builtins \
/opt/local/include/bash /opt/local/include/bash/include /opt/local/include/bash/builtins '
if [[ $includes == *\<libpq-fe.h\>* ]]; then
local inc=$(find /opt/local/include/postgresql* -name libpq-fe.h 2>/dev/null)
SEARCH_INCLUDE_FILES+=" ${inc%/*} "
fi
IPATHS=$(dirpaths_with_pfx ' -I' $SEARCH_INCLUDE_FILES)
export ASAN_OPTIONS=quarantine_size_mb=1
export PATH=/usr/lib/llvm-20/bin/:$PATH
export PATH=/usr/lib/llvm-14/bin/:$PATH
$CCOMPILER --version
# -pedantic-errors
local libs='' stack='-fno-stack-protector'
[[ ${OSTYPE,,} == linux* ]] && stack+=' -z execstack'
[[ $includes == *\<sqlite3.h\>* ]] && libs+=' -lsqlite3 '
[[ $includes == *\<pthread.h\>* ]] && libs+=' -lpthread '
[[ $includes == *\<libpq-fe.h\>* ]] && libs+=' -lpq '
local name=${src##*/}
name=${name%.c}
local out=~/compiled/$name mk_shared_object='' is_so=is_bashbuiltin
((is_bashbuiltin)) && out=~/compiled/bashbuiltin/$name
echo "out: $out"
mkdir -p ${out%/*}
[[ $name == lib* ]] && is_so=1
((is_so)) && out+=.so && mk_shared_object='-shared -undefined dynamic_lookup -fPIC'
local cmd="$CCOMPILER $mk_shared_object $stack -Wno-string-compare -Wno-strict-prototypes -g $as -rdynamic $IPATHS $src $libs -o $out"
echo $cmd
rm $out 2>/dev/null
$cmd
ls -l $out
if ((is_bashbuiltin)) && [[ $USER != cgille || $name != *sqlite ]]; then
local builtins="$(sed -n -e 's|^struct builtin \(.*\)_struct.*$|\1|p' -e 's|^MY_BASHBUILTIN(\(\w*\).*$|\1|p' $src)"
[[ -z $builtins ]] && builtins=$(sed -n -e 's|^#define NAME \(.*\)|\1|p' $src);
local bb="$(echo $builtins)"
local load=$PWD/load_this_builtin.sh
[[ $out == $HOME/* ]] && out='~'/${out#$HOME/}
echo oooooooooooooo $out
{
cat << EOF
# This file was generated by $0 $*"
# This script loads the plugins into the current Bash instance.
#
# Add the following line at the top of your script:
#
# source $load
#
enable -d $bb 2>/dev/null
enable -f $out $bb
EOF
}>$load
chmod a+x $load
echo "To install the plugin run $ANSI_FG_BLUE$load$ANSI_RESET"
ls -l $load
fi
}
main "$@"