简体   繁体   中英

c++ makefile append prefeix to list of double-quoted items with spaces

Lets say I have this in my makefile:

FOLDERS = "C:\Program Files\some3rdpartytool\inc\thing1.h" \
          "C:\Program Files\some3rdpartytool\lib\libthing.lib" \
          "C:\data\data1.h" \

Now I want INCLUDE to contain:

INCLUDE = -I"C:\Program Files\some3rdpartytool\inc" \
          -I"C:\Program Files\some3rdpartytool\lib" \
          -I"C:\data" \

Normally this is easy - you just to something like:

INCLUDE = $(addprefix -I, $(FOLDERS))
# you can also use the standard makefile function to take the folder path
# only - I just forget what it is off the top of my head...

But no matter what I do, this always only works on white space separated lists (as it is designed to) so I get something like this:

INCLUDE = -I"C:\Program \
          -IFiles\some3rdpartytool\inc" \
          -I"C:\Program \
          -IFiles\some3rdpartytool\lib" \
          -I"C:\data" \

Is there a standard makefile way to do this? (standard == portable), I want this to work on windows and Linux ideally.

Note I did think about replacing any ' "' --> ' -I"' but I can't seem to use a white space in the subst command... I am struggling on a way forward with this...

All right, here we go.

FOLDERS = "C:\Program Files\blah\thing1.h" \
          "C:\Program Files\blah\libthing.lib" \
          "C:\data\data1.h"

Now we use I trick I learned from @MadScientist, defining a variable that contains a space, and using a placeholder that you're confident does not occur in your paths, such as "SPACE".

E :=
S := $E $E

X1 := $(subst $(S),SPACE,$(FOLDERS))
# "C:\ProgramSPACEFiles\blah\thing1.h"SPACE"C:\ProgramSPACEFiles\...

This seems to replace all instances of ' ', including the ones between paths, so we'll change those back:

X2 := $(subst "SPACE","$(S)",$(X1))
# "C:\ProgramSPACEFiles\blah\thing1.h" "C:\ProgramSPACEFiles\...

Then add the "-I":

X3 := $(addprefix -I,$(X2))
# -I"C:\ProgramSPACEFiles\blah\thing1.h" -I"C:\ProgramSPACEFiles\...

Then change "SPACE" back to ' ':

X4 := $(subst SPACE,$(S),$(X3))
# -I"C:\Program Files\blah\thing1.h" -I"C:\Program Files...

@Beta's solution is nice and general. Another option that is more specific but maybe simpler is to use something like:

FOLDERS = "C:\Program Files\some3rdpartytool\inc" \
          "C:\Program Files\some3rdpartytool\lib" \
          "C:\data"

INCLUDES := $(patsubst "C:%,-I"C:%,$(FOLDERS))

but of course this fails if you have folders outside of the C: drive.

A final option which is more general but still has a few issues (it reduces whitespace is the main one, but I've never heard of paths with multiple consecutive spaces so maybe that's OK) would be something like:

INCLUDES := -I$(subst " "," -I",$(strip $(FOLDERS)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM