How to Convert a Quoted String to a Normal One in Makefile?

流过昼夜 提交于 2021-02-07 05:49:18

问题


I am not sure whether I have described the question properly, but currently I am solving this problem in the following way

QUOTEDSTR := "hello world"
NORMALSTR := $(shell echo $(QUOTEDSTR))

Is there a more built-in way that 'make' can do this without calling shell? Thanks


回答1:


Another option:

NORMALSTR := $(patsubst "%",%,$(QUOTEDSTR))

Beta's answer will remove every quote in the string. The above solution will ONLY remove quotes that appear at the beginning and end. For example:

QUOTEDSTR := -DTITLE=\"Title\"

Beta's answer will result in a value of -DTITLE=\Title\ while using the patsubst solution this value will not be changed.

It depends on what you want.

EDIT

If you want to handle whitespace and still only match quotes at the beginning/end of the variable as per @stefanct's comment, you'll have to play some tricks. First you need to find a non-whitespace character which you know will never appear in your string. Let's choose ^ but you can choose something else if you want.

The algorithm is: convert all spaces to this character, then remove the quotes from the resulting single "word", then convert all instances of that character back to spaces, like this:

# Get a variable S that contains a single space
E :=
S := $E $E

NORMALSTR := $(subst ^,$S,$(patsubst "%",%,$(subst $S,^,$(QUOTEDSTR))))

Of course there are still complications; this handles only spaces for example, not other whitespace characters like TAB.




回答2:


This should do it:

NORMALSTR := $(subst $\",,$(QUOTEDSTR))



回答3:


All answers yet have issues. patsubst "[f]inds whitespace-separated words" so the simple solution by @MadScientist does not work for strings like "hello world". The one by @Beta on the other hand removes all quote characters no matter where they are.

The code below shows how to deal with strings with spaces in them as well. However, it will also remove other quote characters on the edge of words, for example "hello "world"3" will be transformed to hello world"3. If that's any better... I don't know, probably not.

Instead of the other solutions this one creates a user function named unquote instead of directly replacing the strings.

quoted="hello world"

unquote = $(patsubst "%,%,$(patsubst %",%,$(1)))
#unquote = $(subst $\",,$(1))
#unquote = $(patsubst "%",%,$(1))
#unquote = $(shell echo $(1))

unquoted = $(call unquote,$(quoted))

$(info Variable quoted is $(quoted))
$(info Variable unquoted is $(unquoted))

This simply looks for all quote characters at the start and end of every (white-space separated) word and removes it.




回答4:


NORMALSTR := $(subst $\",,$(QUOTEDSTR))

equal to

NORMALSTR := $(subst ",,$(QUOTEDSTR))

because you didn't define $\ variable, so it is null string

$\" avoid to syntax highlight issue in editor.

",,$(QUOTEDSTR)) will be recognized as a string by Editor until it find another ".



来源:https://stackoverflow.com/questions/10424645/how-to-convert-a-quoted-string-to-a-normal-one-in-makefile

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