Allowing users to override CFLAGS, CXXFLAGS and friends

后端 未结 4 2149
面向向阳花
面向向阳花 2021-01-19 10:40

Typical makefiles often use the built-in variables CFLAGS, CXXFLAGS, CPPFLAGS and so on1 to set the flags passed to the C,

相关标签:
4条回答
  • 2021-01-19 10:41

    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.

    0 讨论(0)
  • 2021-01-19 10:45

    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.

    0 讨论(0)
  • 2021-01-19 10:59

    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
    0 讨论(0)
  • 2021-01-19 10:59

    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
    
    0 讨论(0)
提交回复
热议问题