bakefile
bakefile copied to clipboard
GNU Makefile generation should honor CXX and CC environment variables
Currently, the gnu
toolset generates every GNUmakefile
with a definition of default compilers, namely:
CC := cc
CXX := c++
The program should check if CC and/or CXX are already defined by the environment. Travis CI, for example, switches compiler by switching the environment variables. If you want to use Travis CI with bakefile, you currently always compile via cc
and c++
which is the same compiler every time, even if you change your build environment between gcc
and clang
compiler settings.
Or you can just run make CC=whatever CXX=whatever
...
Like I said, Continuous Integration tools don't work with it and if your build matrix contains many entries (complex build matrices like mine usually have 10 or more entries), it's not very convenient to add an environment variable for every entry and to manually call make CC=$OWN_CC CXX=$OWN_CXX
Your solution is a great workaround for the current situation, but bakefile should support environment variables for compilers, maybe via a command line option like -e
and --environment
if it's not intended to be default behavior.
The program should check if CC and/or CXX are already defined by the environment.
"The program” being what? Bakefile, which runs before use and so can’t check environment variables because it doesn’t know the environment of the unknown number of unknown computers the generated makefile will be used on at some unknown point in the future? Or GNU make, which I have no control over and which behaves as it always did in this regard?
Like I said, Continuous Integration tools don't work with it
There is no inherent property of all “CI tools” that would prevent them from using environment variables…
but bakefile should support environment variables for compilers, maybe via a command line option like -e and --environment
Bakefile does support specifying the compiler, of course, in at least two ways:
- What @kirbyfan64 says is the standard, widely-used way of doing it, fully supported by the generated makefiles. You seem to think that Travis is somehow incapable of launching make with the
make CC=$CC CXX=$CXX
command; let me assure you that’s not the case. - GNU make does have exactly the
-e
flag you’re asking for. It’s documented (and not recommended).
Just run make CC=$CC CXX=$CXX
.
We could use ?=
for CC
and CXX
to always inherit them from the environment if they're defined, I think this is often the right thing to do by default...
@vadz That would be a great choice in my opinion. It would greatly improve bakefile
@vslavik I'm fairly new into generating or writing Makefiles and CI tools, so I didn't know about the exact problems that such a change could cause.
And I didn't know that make CC=$CC CXX=$CXX
was even possible (I should read the GNU make documentation, I think).
I would agree with vadz's recommendation, though. Using ?=
instead of :=
could be a great idea.
Right. Apparently we discussed ?=
before, but never got around to implementing it (PR anyone?) The whole story is a bit more complicated, though, because other special-meaning variables like CPPFLAGS
should get some special treatment as well, but not ?=
, because they may contain makefile-specific flags, so the two alternatives discussed were:
- Use
CPPFLAGS += …
instead of:=
- Use something like this instead:
ifneq "$(origin CPPFLAGS)" "environment"
CPPFLAGS :=
endif
Using +=
is simpler and usually works just fine, but, unfortunately, it doesn't work for all cases and sometimes you should really be allowed to override the default flag values instead of just appending to them, e.g.if bakefile uses c-compiler-options = -O2
, then we'd have CFLAGS := -O2
in the generated makefile and we need to provide the user with some way to override this with -O0
or -O3
.
OTOH ideally we'd also have some way to append an extra option to CFLAGS
. So it looks like we ought to really do
ifeq "$(origin CFLAGS)" "environment"
CFLAGS += -O2
else
CFLAGS := -O2
endif
This looks ugly, but does seem to do what we want, I think.