xref: /spdk/scripts/detect_cc.sh (revision 56e12b00711b40d1e2ff45d4147193f45fdd9af0)
1#!/usr/bin/env bash
2
3set -e
4
5function err()
6{
7	echo "$@" >&2
8}
9
10function usage()
11{
12	err "Detect compiler and linker versions, generate mk/cc.mk"
13	err ""
14	err "Usage: ./detect_cc.sh [OPTION]..."
15	err ""
16	err "Defaults for the options are specified in brackets."
17	err ""
18	err "General:"
19	err " -h, --help                Display this help and exit"
20	err " --cc=path                 C compiler to use"
21	err " --cxx=path                C++ compiler to use"
22	err " --ld=path                 Linker to use"
23	err " --lto=[y|n]               Attempt to configure for LTO"
24}
25
26
27
28for i in "$@"; do
29	case "$i" in
30		-h|--help)
31			usage
32			exit 0
33			;;
34		--cc=*)
35			if [[ -n "${i#*=}" ]]; then
36				CC="${i#*=}"
37			fi
38			;;
39		--cxx=*)
40			if [[ -n "${i#*=}" ]]; then
41				CXX="${i#*=}"
42			fi
43			;;
44		--lto=*)
45			if [[ -n "${i#*=}" ]]; then
46				LTO="${i#*=}"
47			fi
48			;;
49		--ld=*)
50			if [[ -n "${i#*=}" ]]; then
51				LD="${i#*=}"
52			fi
53			;;
54		--)
55			break
56			;;
57		*)
58			err "Unrecognized option $i"
59			usage
60			exit 1
61	esac
62done
63
64: ${CC=cc}
65: ${CXX=c++}
66: ${LD=ld}
67: ${LTO=n}
68
69CC_TYPE=$($CC -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
70CXX_TYPE=$($CXX -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
71if [ "$CC_TYPE" != "$CXX_TYPE" ]; then
72	err "C compiler is $CC_TYPE but C++ compiler is $CXX_TYPE"
73	err "This may result in errors"
74fi
75
76LD_TYPE=$($LD --version 2>&1 | head -n1 | awk '{print $1, $2}')
77case "$LD_TYPE" in
78	"GNU ld"*)
79		LD_TYPE=bfd
80		;;
81	"GNU gold"*)
82		LD_TYPE=gold
83		;;
84	"LLD"*)
85		LD_TYPE=lld
86		;;
87	*)
88		err "Unsupported linker: $LD"
89		exit 1
90esac
91
92CCAR="ar"
93if [ "$LTO" = "y" ]; then
94	if [ "$CC_TYPE" = "clang" ]; then
95		if [ "$LD_TYPE" != "gold" ]; then
96			err "Using LTO with clang requires the gold linker."
97			exit 1
98		fi
99		CCAR="llvm-ar"
100	else
101		CCAR="gcc-ar"
102	fi
103fi
104
105function set_default() {
106	echo "ifeq (\$(origin $1),default)"
107	echo "$1 = $2"
108	echo "endif"
109	echo ""
110}
111
112set_default CC $CC
113set_default CXX $CXX
114set_default LD $LD
115
116echo "CCAR=$CCAR"
117echo "CC_TYPE=$CC_TYPE"
118echo "LD_TYPE=$LD_TYPE"
119