-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompilerCheck.sh
More file actions
executable file
·100 lines (89 loc) · 2.79 KB
/
compilerCheck.sh
File metadata and controls
executable file
·100 lines (89 loc) · 2.79 KB
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
#!/bin/sh
# Minimum required versions
MIN_GCC_MAJOR=11
MIN_CLANG_MAJOR=14
# Fallback clang versions to try
CLANG_CANDIDATES="clang++-20 clang++-19 clang++-18 clang++-17 clang++-16 clang++-15"
# Find c++ path and real target
CXX_PATH=$(command -v c++)
[ -z "$CXX_PATH" ] && {
echo "No c++ compiler found in PATH."
exit 1
}
CXX_REAL=$(readlink -f "$CXX_PATH")
echo "Default 'c++' path: $CXX_PATH"
echo "'c++' resolves to: $CXX_REAL"
# Extract version string
CXX_VERSION=$($CXX_PATH --version | head -n 1)
echo "Version string: $CXX_VERSION"
# Functions to extract versions
get_gcc_major_version() {
echo "$1" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 | cut -d. -f1
}
get_clang_major_version() {
echo "$1" | sed -n 's/.*clang.*\s\([0-9]\+\)\..*/\1/p'
}
# Function to try known clang++ versions
try_clang_versions() {
for candidate in $CLANG_CANDIDATES; do
if command -v "$candidate" >/dev/null 2>&1; then
VERSION=$($candidate --version | head -n 1)
MAJOR=$(get_clang_major_version "$VERSION")
if [ "$MAJOR" -ge "$MIN_CLANG_MAJOR" ]; then
echo "Using $candidate (version $MAJOR)"
export CXX=$candidate
return 0
else
echo "Found $candidate but version $MAJOR is too old"
fi
fi
done
return 1
}
# Determine compiler type and act accordingly
if echo "$CXX_VERSION" | grep -qi "gcc\|g++" || echo "$CXX_REAL" | grep -qi "gcc\|g++"; then
COMPILER="GCC"
MAJOR=$(get_gcc_major_version "$CXX_VERSION")
echo "Detected compiler: $COMPILER $MAJOR"
if [ "$MAJOR" -lt "$MIN_GCC_MAJOR" ]; then
echo "$COMPILER version too old (<$MIN_GCC_MAJOR). Trying newer clang++..."
if command -v clang++ >/dev/null 2>&1; then
CLANG_VERSION=$(clang++ --version | head -n 1)
CLANG_MAJOR=$(get_clang_major_version "$CLANG_VERSION")
if [ "$CLANG_MAJOR" -ge "$MIN_CLANG_MAJOR" ]; then
echo "Using clang++ (version $CLANG_MAJOR)"
export CXX=clang++
else
echo "System clang++ version $CLANG_MAJOR is too old. Trying specific versions..."
try_clang_versions || {
echo "No suitable clang++ found."
exit 1
}
fi
else
try_clang_versions || {
echo "No clang++ available."
exit 1
}
fi
else
export CXX="$CXX_PATH"
fi
elif echo "$CXX_VERSION" | grep -qi "clang"; then
COMPILER="Clang"
MAJOR=$(get_clang_major_version "$CXX_VERSION")
echo "Detected compiler: $COMPILER $MAJOR"
if [ "$MAJOR" -lt "$MIN_CLANG_MAJOR" ]; then
echo "$COMPILER version too old (<$MIN_CLANG_MAJOR). Trying specific versions..."
try_clang_versions || {
echo "No suitable clang++ found."
exit 1
}
else
export CXX="$CXX_PATH"
fi
else
echo "Unknown compiler: $CXX_VERSION"
export CXX="$CXX_PATH"
fi
echo "Final compiler selected: $CXX"