Search and Replace CFLAGS in a target

471 views Asked by At

I need to add -Werror to the already existing (Exported?) CFLAGS for a build. Right now I am just trying to extract the data CFLAGS holds. I am super new to Make and Makefiles but have to add some pre-existing build files.

Say I have a target in a makefile like this

   .PHONY: add_errors
   add_errors:
       @flags=$(CFLAGS);\
       echo $$flags;\

But the issue is, CFLAGS is a really large string that has many options set. When the makefile is executed I get the following error

/bin/sh: 1: -marm: not found
make[2]: *** [add_errors] Error 127

Which looks like something is taking the first space as the string and then discarding the rest of it.

Inside CFLAGS, a snippet of the text is

-march=armv5te -marm -mthumb-interwork -mtune=arm926ej-s --sysroot=/opt/dspg/v2.11-rc2/sysroots/cortexa9-neon-dspg-linux-gnueabi -Wno-psabi -ggdb -I/opt/dspg/v2.11-rc2/sysroots/cortexa9-neon-dspg-linux-gnueabi/usr/include/libxml2

What can I do?

1

There are 1 answers

0
MadScientist On

You should ask a question which actually has some relation to what you really want to do, including relevant parts of the code. This example you gave is not useful for anything so the answer we give probably won't actually help you, but:

The first advice I have for you is NEVER use the @ prefix on your recipes. Or at the very least never use them until AFTER your makefile is already working 100% correctly. Suppressing make's output like that is like trying to debug while blindfolded.

The problem is not related to make at all, really: it's just shell quoting rules.

If you remove the @ and look at what make prints you'll see it's running this command:

flags=-march=armv5te -marm -mthumb-interwork -mtune=arm926ej-s ...; echo $flags;

If you cut and paste that to your shell, you'll get exactly the same error.

That's because the shell command foo=bar biz baz means, set the environment variable foo to the value bar then run the command biz with the argument baz.

You need to add quoting so that the shell puts all the arguments into the flags variable:

.PHONY: add_errors
add_errors:
        @flags='$(CFLAGS)';\
        echo $$flags;\

will cause make to run this:

flags='-march=armv5te -marm -mthumb-interwork -mtune=arm926ej-s ...'; echo $flags;