Allowing users to override CFLAGS, CXXFLAGS and friends

家住魔仙堡 提交于 2019-12-01 19:15:33

The approach I prefer is to provide sensible default values to these common variables, but let users provide their own - overriding the default values.

include $(wildcard makefile.in Makefile.in)

BUILD ?= build
CFLAGS ?= -O2 -fPIC -pedantic -Wall -Wextra -Wconversion

This can be done by either environment variables, command line parameters like make CFLAGS=-g or persistently in a makefile.in.

I am aware that this doesn't exactly pick up the issue you described in the questions, but I found use cases in which users want to compile a project with non-default flags should be able to

  1. Define these variables to their needs
  2. Check their defaults, preferably at the top of the makefile
  3. Maybe adjust the definitions in accordance to the defaults

If someone wants to build with some special flags and is incapable of these steps, there will be some more serious problems anyhow.

This approach will not scale well when the build becomes more involved and the defaults are set across a larger makefile and dependent on other conditions.

I am faced with the same problem. For the time being my solution is to provide "non-standard" flags such as OPTIMS, WARNINGS, MODENV which will be appended to the "standard" CXXFLAGS internally.

If the user defines CXXFLAGS from the command-line it is assumed that he wants to override it, and if that's what he wants, that's what he should get: an override. Ironically this means I'm not using override CXXFLAGS += ... in the Makefile.

I don't want advanced users to pull their hairs out because I insist on appending/prepending my stuff to their flags, so in my opinion the final situation is like this:

  • GOOD: require advanced users to pass intricate custom flags
  • BAD: require advanced users to patch the Makefile

Just stumbled upon the same question while building an RPM with debuginfo package.

The requirement for debuginfo generation is to pass -g in CFLAGS while preserving whatever CFLAGS the software has in its Makefile.

So if you want to add some extra bits to CFLAGS, without overwriting the ones present in Makefile, you can simply use CFLAGS as an environment variable. But only as long as the Makefile in question uses CFLAGS += ... notation.

Example, suppose that you have software with Makefile having:

CFLAGS += $(ARCH) -O3 -std=gnu11 -Wall ...

To have it build with all those flags and -g, you will do:

CFLAGS='-g' make 

Note that passing it as an argument to make won't work, as in: make CFLAGS='-g' is wrong, because it will overwrite internal CFLAGS.

More on the solution to pass -g for building debuginfo packages properly

Here's reference on make: appending to variables.

The override directive may be what you are looking for:

$ cat Makefile
override CFLAGS += -foobar

all:
    $(info CFLAGS = $(CFLAGS))

$ make
CFLAGS = -foobar
make: 'all' is up to date.

$ make CFLAGS=-g
CFLAGS = -g -foobar
make: 'all' is up to date.

Note that you can also use:

$ make CFLAGS+=-g

on the command line but it behaves just like:

$ make CFLAGS=-g
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!