Skip to content

Commit 78d8293

Browse files
committed
Make configure respect (and save) values for CC, CXX, CFLAGS, etc.
I mostly tried to remain backwards compatible with old invocations of the `configure` script; if you do not want to use `CC` et al., you should not have to; you can keep using `--enable-clang` and/or `--enable-ccache`. The overall intention is to capture the following precedences for guessing the C compiler: 1. Value of `CC` at make invocation time. 2. Value of `CC` at configure invocation time. 3. Compiler inferred at configure invocation time (`gcc` or `clang`). The strategy is to check (at `configure` time) if each of the environment variables is set, and if so, save its value in a corresponding `CFG_` variable (e.g. `CFG_CC`). Then, in the makefiles, if `CC` is not set but `CFG_CC` is, then we use the `CFG_CC` setting as `CC`. Fix rust-lang#13805. ---- Note that if you try to set the compiler to clang via the `CC` and `CXX` environment variables, you will probably need to also set `CXXFLAGS` to `--enable-libcpp` so that LLVM will be configured properly. ---- The `configure` script does not infer the compiler setting if `CC` is set; but if `--enable-clang` was passed, then it *does* still attempt to validate that the clang version is compatible. Supporting this required revising `CLANG_VERSION` check to be robust in face of user-provided `CC` value. In particular, on Travis, the `CC` is set to `gcc` and so the natural thing to do is to attempt to use `gcc` as the compiler, but Travis is also passing `--enable-clang` to configure. So, what is the right answer in the face of these contradictory requests? One approach would be to have `--enable-clang` supersede the setting for `CC` (and instead just call whatever we inferred for `CFG_CLANG`). That sounds maximally inflexible to me (pnkfelix): a developer requesting a `CC` value probably wants it respected, and should be able to set it to something else; it is harder for that developer to hack our configure script to change its inferred path to clang. A second approach would be to blindly use the `CC` value but keep going through the clang version check when `--enable-clang` is turned on. But on Travis (a Linux host), the `gcc` invocation won't print a clang version, so we would not get past the CLANG_VERSION check in that context. A third approach would be to never run the CLANG_VERSION check if `CC` is explicitly set. That is not a terrible idea; but if the user uses `CC` to pass in a path to some other version of clang that they want to test, probably should still send that through the `CLANG_VERSION` check. So in the end I (pnkfelix) took a fourth approach: do the CLANG_VERSION check if `CC` is unset *or* if `CC` is set to a string ending with `clang`. This way setting `CC` to things like `path/to/clang` or `ccache clang` will still go through the CLANG_VERSION check, while setting `CC` to `gcc` or some unknown compiler will skip the CLANG_VERSION check (regardless of whether the user passed --enable-clang to `configure`). ---- As a drive-by fix, the call that sets `CFG_CLANG_VERSION` was quoting `"$CFG_CC"` in its invocation, but that does not play nicely with someone who sets `$CFG_CC` to e.g. `ccache clang`, since you do not want to intepret that whole string as a command. (On the other hand, a path with spaces might need the quoted invocation. Not sure which one of these corner use-cases is more important to support.)
1 parent 5d2eddd commit 78d8293

File tree

2 files changed

+107
-76
lines changed

2 files changed

+107
-76
lines changed

configure

+88-27
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ opt() {
223223
fi
224224
}
225225

226+
envopt() {
227+
local NAME=$1
228+
local V="CFG_${NAME}"
229+
eval VV=\$$V
230+
231+
# If configure didn't set a value already, then check environment.
232+
#
233+
# (It is recommended that the configure script always check the
234+
# environment before setting any values to envopt variables; see
235+
# e.g. how CFG_CC is handled, where it first checks `-z "$CC"`,
236+
# and issues msg if it ends up employing that provided value.)
237+
if [ -z "$VV" ]
238+
then
239+
eval $V=\$$NAME
240+
eval VV=\$$V
241+
fi
242+
243+
# If script or environment provided a value, save it.
244+
if [ ! -z "$VV" ]
245+
then
246+
putvar $V
247+
fi
248+
}
249+
226250
msg "looking for configure programs"
227251
need_cmd cmp
228252
need_cmd mkdir
@@ -646,41 +670,69 @@ then
646670
esac
647671
fi
648672

649-
if [ ! -z "$CFG_ENABLE_CLANG" ]
673+
if [ ! -z "$CC" ]
650674
then
651-
if [ -z "$CFG_CLANG" ]
675+
msg "skipping compiler inference steps; using provided CC=$CC"
676+
CFG_CC="$CC"
677+
else
678+
if [ ! -z "$CFG_ENABLE_CLANG" ]
652679
then
653-
err "clang requested but not found"
680+
if [ -z "$CFG_CLANG" ]
681+
then
682+
err "clang requested but not found"
683+
fi
684+
CFG_CC="$CFG_CLANG"
685+
else
686+
CFG_CC="gcc"
654687
fi
655-
CFG_CLANG_VERSION=$("$CFG_CLANG" \
656-
--version \
657-
| grep version \
658-
| sed 's/.*\(version .*\)/\1/; s/.*based on \(LLVM .*\))/\1/' \
659-
| cut -d ' ' -f 2)
660-
661-
case $CFG_CLANG_VERSION in
662-
(3.0svn | 3.0 | 3.1* | 3.2* | 3.3* | 3.4* | 3.5* )
663-
step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
664-
CFG_C_COMPILER="clang"
665-
;;
666-
(*)
667-
err "bad CLANG version: $CFG_CLANG_VERSION, need >=3.0svn"
668-
;;
669-
esac
670-
else
671-
CFG_C_COMPILER="gcc"
688+
689+
if [ ! -z "$CFG_ENABLE_CCACHE" ]
690+
then
691+
if [ -z "$CFG_CCACHE" ]
692+
then
693+
err "ccache requested but not found"
694+
fi
695+
696+
CFG_CC="ccache $CFG_CC"
697+
fi
698+
699+
msg "inferred compiler: $CFG_CC"
672700
fi
673701

674-
if [ ! -z "$CFG_ENABLE_CCACHE" ]
702+
if [ ! -z "$CFG_ENABLE_CLANG" ]
675703
then
676-
if [ -z "$CFG_CCACHE" ]
704+
if [ -z "$CC" ] || [[ $CC == *clang ]]
677705
then
678-
err "ccache requested but not found"
706+
CFG_CLANG_VERSION=$($CFG_CC \
707+
--version \
708+
| grep version \
709+
| sed 's/.*\(version .*\)/\1/; s/.*based on \(LLVM .*\))/\1/' \
710+
| cut -d ' ' -f 2)
711+
712+
case $CFG_CLANG_VERSION in
713+
(3.0svn | 3.0 | 3.1* | 3.2* | 3.3* | 3.4* | 3.5* )
714+
step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
715+
if [ -z "$CC" ]
716+
then
717+
CFG_CC="clang"
718+
fi
719+
;;
720+
(*)
721+
err "bad CLANG version: $CFG_CLANG_VERSION, need >=3.0svn"
722+
;;
723+
esac
724+
else
725+
msg "skipping CFG_ENABLE_CLANG version check; provided CC=$CC"
679726
fi
680-
681-
CFG_C_COMPILER="ccache $CFG_C_COMPILER"
682727
fi
683728

729+
# Set CFG_{CC,CXX,CPP,CFLAGS,CXXFLAGS}
730+
envopt CC
731+
envopt CXX
732+
envopt CPP
733+
envopt CFLAGS
734+
envopt CXXFLAGS
735+
684736
# a little post-processing of various config values
685737
CFG_PREFIX=${CFG_PREFIX%/}
686738
CFG_MANDIR=${CFG_MANDIR%/}
@@ -961,7 +1013,7 @@ do
9611013
;;
9621014
esac
9631015

964-
case "$CFG_C_COMPILER" in
1016+
case "$CFG_CC" in
9651017
("ccache clang")
9661018
LLVM_CXX_32="ccache clang++ -m32 -Qunused-arguments"
9671019
LLVM_CC_32="ccache clang -m32 -Qunused-arguments"
@@ -991,6 +1043,16 @@ do
9911043

9921044
LLVM_CXX_64="g++"
9931045
LLVM_CC_64="gcc"
1046+
;;
1047+
1048+
(*)
1049+
msg "inferring LLVM_CXX/CC from CXX/CC = $CXX/$CC"
1050+
LLVM_CXX_32="$CXX -m32"
1051+
LLVM_CC_32="$CC -m32"
1052+
1053+
LLVM_CXX_64="$CXX"
1054+
LLVM_CC_64="$CC"
1055+
;;
9941056
esac
9951057

9961058
LLVM_CFLAGS_32="-m32"
@@ -1073,7 +1135,6 @@ putvar CFG_PREFIX
10731135
putvar CFG_BUILD
10741136
putvar CFG_HOST
10751137
putvar CFG_TARGET
1076-
putvar CFG_C_COMPILER
10771138
putvar CFG_LIBDIR
10781139
putvar CFG_LIBDIR_RELATIVE
10791140
putvar CFG_DISABLE_MANAGE_SUBMODULES

mk/platform.mk

+19-49
Original file line numberDiff line numberDiff line change
@@ -81,57 +81,27 @@ CFG_DEPEND_FLAGS = -MMD -MP -MT $(1) -MF $(1:%.o=%.d)
8181

8282
AR := ar
8383

84-
CFG_INFO := $(info cfg: using $(CFG_C_COMPILER))
85-
ifeq ($(CFG_C_COMPILER),clang)
86-
# The -Qunused-arguments sidesteps spurious warnings from clang
87-
ifeq ($(origin CC),default)
88-
CC=clang -Qunused-arguments
89-
endif
90-
ifeq ($(origin CXX),default)
91-
CXX=clang++ -Qunused-arguments
92-
endif
93-
ifeq ($(origin CPP),default)
94-
CPP=clang -Qunused-arguments
95-
endif
96-
else
97-
ifeq ($(CFG_C_COMPILER),gcc)
98-
ifeq ($(origin CC),default)
99-
CC=gcc
100-
endif
101-
ifeq ($(origin CXX),default)
102-
CXX=g++
103-
endif
104-
ifeq ($(origin CPP),default)
105-
CPP=gcc
84+
define SET_FROM_CFG
85+
ifdef CFG_$(1)
86+
ifeq ($(origin $(1)),undefined)
87+
$$(info cfg: using $(1)=$(CFG_$(1)) (CFG_$(1)))
88+
$(1)=$(CFG_$(1))
89+
endif
90+
ifeq ($(origin $(1)),default)
91+
$$(info cfg: using $(1)=$(CFG_$(1)) (CFG_$(1)))
92+
$(1)=$(CFG_$(1))
93+
endif
10694
endif
107-
else
108-
ifeq ($(CFG_C_COMPILER),ccache clang)
95+
endef
96+
97+
$(foreach cvar,CC CXX CPP CFLAGS CXXFLAGS CPPFLAGS,\
98+
$(eval $(call SET_FROM_CFG,$(cvar))))
99+
100+
ifeq ($(CFG_ENABLE_CLANG),1)
109101
# The -Qunused-arguments sidesteps spurious warnings from clang
110-
ifeq ($(origin CC),default)
111-
CC=ccache clang -Qunused-arguments
112-
endif
113-
ifeq ($(origin CXX),default)
114-
CXX=ccache clang++ -Qunused-arguments
115-
endif
116-
ifeq ($(origin CPP),default)
117-
CPP=ccache clang -Qunused-arguments
118-
endif
119-
else
120-
ifeq ($(CFG_C_COMPILER),ccache gcc)
121-
ifeq ($(origin CC),default)
122-
CC=ccache gcc
123-
endif
124-
ifeq ($(origin CXX),default)
125-
CXX=ccache g++
126-
endif
127-
ifeq ($(origin CPP),default)
128-
CPP=ccache gcc
129-
endif
130-
else
131-
CFG_ERR := $(error please try on a system with gcc or clang)
132-
endif
133-
endif
134-
endif
102+
CFLAGS += -Qunused-arguments
103+
CXXFLAGS += -Qunused-arguments
104+
CPPFLAGS += -Qunused-arguments
135105
endif
136106

137107
CFG_RLIB_GLOB=lib$(1)-*.rlib

0 commit comments

Comments
 (0)