Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

makefile - Search and Replace CFLAGS in a target

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?

question from:https://stackoverflow.com/questions/65942288/search-and-replace-cflags-in-a-target

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...