summaryrefslogtreecommitdiffstats
path: root/src/spdk/scripts/detect_cc.sh
blob: a05ff9adf226993c2fb79cb736311a5a253a8956 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash

set -e

function err() {
	echo "$@" >&2
}

function usage() {
	err "Detect compiler and linker versions, generate mk/cc.mk"
	err ""
	err "Usage: ./detect_cc.sh [OPTION]..."
	err ""
	err "Defaults for the options are specified in brackets."
	err ""
	err "General:"
	err " -h, --help                Display this help and exit"
	err " --cc=path                 C compiler to use"
	err " --cxx=path                C++ compiler to use"
	err " --ld=path                 Linker to use"
	err " --lto=[y|n]               Attempt to configure for LTO"
	err " --cross-prefix=prefix     Use the given prefix for the cross compiler toolchain"
}

for i in "$@"; do
	case "$i" in
		-h | --help)
			usage
			exit 0
			;;
		--cc=*)
			if [[ -n "${i#*=}" ]]; then
				CC="${i#*=}"
			fi
			;;
		--cxx=*)
			if [[ -n "${i#*=}" ]]; then
				CXX="${i#*=}"
			fi
			;;
		--lto=*)
			if [[ -n "${i#*=}" ]]; then
				LTO="${i#*=}"
			fi
			;;
		--ld=*)
			if [[ -n "${i#*=}" ]]; then
				LD="${i#*=}"
			fi
			;;
		--cross-prefix=*)
			if [[ -n "${i#*=}" ]]; then
				CROSS_PREFIX="${i#*=}"
			fi
			;;
		--)
			break
			;;
		*)
			err "Unrecognized option $i"
			usage
			exit 1
			;;
	esac
done

OS=$(uname)

: ${CC=cc}
: ${CXX=c++}
: ${LD=}
: ${LTO=n}
: ${CROSS_PREFIX=}

if [ -z "$LD" ]; then
	if [ "$OS" = "Linux" ]; then
		LD=ld
	fi
	if [ "$OS" = "FreeBSD" ]; then
		LD=ld.lld
	fi
fi

CC_TYPE=$($CC -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
CXX_TYPE=$($CXX -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
if [ "$CC_TYPE" != "$CXX_TYPE" ]; then
	err "C compiler is $CC_TYPE but C++ compiler is $CXX_TYPE"
	err "This may result in errors"
fi

LD_TYPE=$($LD --version 2>&1 | head -n1 | awk '{print $1, $2}')
case "$LD_TYPE" in
	"GNU ld"*)
		LD_TYPE=bfd
		;;
	"GNU gold"*)
		LD_TYPE=gold
		;;
	"LLD"*)
		LD_TYPE=lld
		;;
	*)
		err "Unsupported linker: $LD"
		exit 1
		;;
esac

CCAR="ar"
if [ "$LTO" = "y" ]; then
	if [ "$CC_TYPE" = "clang" ]; then
		if [ "$LD_TYPE" != "gold" ]; then
			err "Using LTO with clang requires the gold linker."
			exit 1
		fi
		CCAR="llvm-ar"
	else
		CCAR="gcc-ar"
	fi
fi

if [ -n "$CROSS_PREFIX" ]; then
	expected_prefix=$($CC -dumpmachine)

	if [ ! "$expected_prefix" = "$CROSS_PREFIX" ]; then
		err "Cross prefix specified ($CROSS_PREFIX) does not match prefix of $CC ($expected_prefix)."

		# Try to fix this automatically. Maybe the user set CROSS_PREFIX but not CC.
		CC=$CROSS_PREFIX-$CC
		if hash $CC 2> /dev/null; then
			expected_prefix=$($CC -dumpmachine)

			if [ "$expected_prefix" = "$CROSS_PREFIX" ]; then
				err "Automatically changed CC to $CC"
			else
				err "Set CC to the appropriate compiler."
				exit 1
			fi
		else
			err "Set CC to the appropriate compiler."
			exit 1
		fi

	fi

	expected_prefix=$($CXX -dumpmachine)

	if [ ! "$expected_prefix" = "$CROSS_PREFIX" ]; then
		err "Cross prefix specified ($CROSS_PREFIX) does not match prefix of $CXX ($expected_prefix)."

		# Try to fix this automatically. Maybe the user set CROSS_PREFIX but not CXX.
		CXX=$CROSS_PREFIX-$CXX
		if hash $CXX 2> /dev/null; then
			expected_prefix=$($CXX -dumpmachine)

			if [ "$expected_prefix" = "$CROSS_PREFIX" ]; then
				err "Automatically changed CXX to $CXX"
			else
				err "Set CXX to the appropriate compiler."
				exit 1
			fi
		else
			err "Set CXX to the appropriate compiler."
			exit 1
		fi
	fi
fi

function set_default() {
	echo "DEFAULT_$1=$2"
	echo "ifeq (\$(origin $1),default)"
	echo "$1=$2"
	echo "endif"
	echo ""
}

set_default CC "$CC"
set_default CXX "$CXX"
set_default LD "$LD"

echo "CCAR=$CCAR"
echo "CC_TYPE=$CC_TYPE"
echo "LD_TYPE=$LD_TYPE"

if [ -n "$CROSS_PREFIX" ]; then
	echo "CROSS_PREFIX=$CROSS_PREFIX"
fi