-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·137 lines (102 loc) · 3.72 KB
/
install.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
#!/bin/sh
# This is a standalone - not used by other scripts
# needs lots of logic improvements this is a vEpsilon
# TODO: what should it do if `jcc` folder already exists - unhandled
# TODO: should it delete folder at end? probably yes
BOLD="\033[1m"
BOLDRED="\033[1;31m"
BOLDYELLOW="\033[1;33m"
RESET="\033[0m"
has_tool() {
command -v "$1" > /dev/null 2>&1
return $?
}
try_root() {
has_tool sudo && sudo "$@" || "$@"
}
script_dir="$(dirname "$0")"
ispipe="1"
if [ -f "$script_dir/../jcc.sh" ]; then
printf "%b\n\n" "${BOLD}Assuming run is from inside JCC directory; will not download${RESET}"
# use presence of `jcc.sh` as marker for being in repo already (rather than being a piped script)
ispipe=""
# hmm, we don't update if in a git repo. maybe this is not optimal
cd "$script_dir/.."
fi
if [ -n "$ispipe" ]; then
mkdir -p jcc
if has_tool git; then
printf "%b\n" "${BOLD}Downloading with 'git clone'...${RESET}"
git clone --quiet https://github.com/john-h-k/jcc jcc
else
if has_tool curl; then
printf "%b\n" "${BOLD}Downloading tarball with 'curl'...${RESET}"
curl -sSL https://github.com/john-h-k/jcc/archive/refs/heads/main.tar.gz | tar xz --strip-components=1 -C jcc
elif has_tool wget; then
printf "%b\n" "${BOLD}Downloading tarball with 'wget'...${RESET}"
wget -q -O - https://github.com/john-h-k/jcc/archive/refs/heads/main.tar.gz | tar xz --strip-components=1 -C jcc
else
# could be that the script has been copied over file system or similar
printf "%b\n" "${BOLDRED}'curl' or 'wget' required to install JCC${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
fi
cd jcc
fi
printf "%b\n\n" "${BOLD}Downloading done!${RESET}"
output=""
if has_tool bash && has_tool cmake && [ -z "$JCC_FORCE_SIMPLE_BUILD" ]; then
output="build/jcc"
./jcc.sh build --mode release
else
printf "%b\n" "${BOLD}Bash/CMake is not installed; reverting to a simple build...$RESET"
# if bash isn't installed, manually do a simple build (literally feeding all files directly to the C compiler)
mkdir -p build
output="build/jcc"
# first, find a C compiler we can use
CC=""
has_tool cc && CC=cc
has_tool jcc && CC=jcc
has_tool gcc && CC=gcc
has_tool clang && CC=clang
if [ -z "$CC" ]; then
printf "%b\n" "${BOLDRED}Could not find a C compiler! (tried 'cc', 'jcc', 'gcc', 'clang')${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
flags=""
flags="$flags -O3" # full opts
flags="$flags -DJCC_ALL" # define JCC_ALL for a fat build
if ! "$CC" $flags -DJCC_ALL -o "$output" $(find src -type f -name '*.c') -lm; then
printf "%b\n" "${BOLDRED}Build failed!${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
fi
full="$(pwd)/${output}"
printf "%b\n" "${BOLD}JCC built to '$full'!${RESET}"
os="$(uname)"
if [ "$os" = "Darwin" ]; then
target="$HOME/usr/local/bin"
if ! try_root cp "$output" "$target"; then
printf "%b\n" "${BOLDYELLOW}JCC built, but installing to '$target' failed${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
printf "%b\n" "${BOLD}JCC installed to '$target'${RESET}"
elif [ "$os" = "Linux" ]; then
target="/usr/local/bin"
if ! try_root cp "$output" "$target"; then
printf "%b\n" "${BOLDYELLOW}JCC built, but installing to '$target' failed${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
printf "%b\n" "${BOLD}JCC installed to '$target'${RESET}"
else
printf "%b\n" "${BOLDYELLOW}JCC built, but unsure how to install for OS '$os'${RESET}"
cd - > /dev/null 2>&1
exit 1
fi
# TODO: show if on path
cd - > /dev/null 2>&1