Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't environment variables override variables set in makefiles by default?

I am compiling packages and I see that oftentimes Makefile authors write set CFLAGS in the makefile, with such and such options. I, on the other hand, would like to try out some compiler optimizations and would like to propagate the compiler switches to make with as little trouble as possible. Not always is this doable though. For instance when a makefile specifies CFLAGS and I want all C compiler invocations to use -fomit-frame-pointer, short of having to explicitly write something like CFLAGS=-fomit-frame-pointer make, what are my options that are not hackish. From what I can see there is the above, then there is the same but different make "CFLAGS=-fomit-frame-pointer" and I can also do what I consider to be the best solution and the reason for this question:

export CFLAGS=-fomit-frame-pointer
make -e

I consider this the best one, because frankly even thought potentially dangerous flag, I don't debug software that much, and when I need to I can recompile a particular piece on demand, with debugging info and all. Otherwise I like to work with release software without debugging bells and whistles at all, especially if the package is not authored by me. So I guess what I ask here specifically is: why make does not automatically prefer environment variables to makefile's own? After all environment knows best what is what, and in case make author really needs to have something their way there is the 'override' syntax, right?

like image 784
amn Avatar asked Jul 02 '10 21:07

amn


2 Answers

Make will override the variables if you put them on the make command line.

Try this: make CFLAGS=-fomit-frame-pointer

One word of warning. Most Makefiles don't expect their variables to be overridden. That means that if the Makefile uses CFLAGS to specify -I for include files, -O2 for optimization, or other flags, you must add them to your CFLAGS override or the make will probably fail.

like image 183
Zan Lynx Avatar answered Oct 24 '22 12:10

Zan Lynx


That is arguable: "After all environment knows best what is what"

It's like saying "mother knows best" to an all-grown up child and stopping them from going to study law because you wanted to see what kind of plumber they'd be. It's their life. Let go and let them make their choice for themselves.

Oh wait, you were asking about environment and make. Right.

Well, make is a new process that starts after you've modified your environment and in the general case, the environment does not know what the make goals are and how it works. So, whatever make explicitly sets should trump whatever might happen to be in the environment.

Update: There's an argument I missed originally in my answer - predictability. The only requirement should be the presence of standardized set of tools (same version of GCC, same libs), and given that a properly written make file should always produce the same result, no matter what the environment is, or what other tools are present.

like image 5
Franci Penov Avatar answered Oct 24 '22 12:10

Franci Penov