views:

32

answers:

2

Hi all,

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?

Just curious (again!) Thanks in advance.

A: 

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.

Franci Penov
Then how are we supposed to override compiler switches for software built by make? I mean in a environmentally friendly way? :-) Surely one would want to control optimizations? You can't have make decide on that. If anything, a makefile that hardcodes CFLAGS/CXXFLAGS/CPPFLAGS and DOES NOT let one change these, is not well designed?
amn
You either change the makefile or you overwrite them by passing the proper flags as command line parameters when you call make. overwriting these in that case is an _explicit_ action from the user. getting whatever happens to be in the environment might lead in unpredictable result, as the user might not even be aware what modifications to the environment the previous program he used did
Franci Penov
Well that is my entire argument - that the environment knows best. Or at least should, with a properly designed Makefile. Hence the implication that if the Makefile breaks on an environment set variable, it is no good. If an author really really needs to have his '-g' compiler switch, and is afraid that the makefile user might shed it for something else, they should perhaps use "override" syntax, or the second-stage assignment ":=" ? Passing command line flags to make is not a good option when you are building a whole set of packages....
amn
But the environment does not know better. The environment is not an active entity, it's a passive storage for any junk that any other process or the user might dump in it.
Franci Penov
I have accepted your answer, because it makes sense. But I definitely don't agree with everything you say. For example, I don't see how environmental "junk" should be blamed for make faults. First, if environment is not clean, then it is not clean - compiling packages in such an environment is a hazardous matter anyway. If it is clean, and make faults, this means makefile is badly written.
amn
+1  A: 

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.

Zan Lynx