在 GNU Make 中,如何将变量转换为小写?

发布于 2024-07-16 07:43:59 字数 242 浏览 5 评论 0原文

这是一个愚蠢的问题,但是...使用 GNU Make:

VAR = MixedCaseText
LOWER_VAR = $(VAR,lc)

default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

在上面的示例中,将 VAR 的内容转换为小写的正确语法是什么? 显示的语法(以及我遇到的其他所有内容)导致 LOWER_VAR 成为空字符串。

This is a silly question, but.... with GNU Make:

VAR = MixedCaseText
LOWER_VAR = $(VAR,lc)

default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

In the above example, what's the correct syntax for converting VAR's contents to lower case? The syntax shown (and everything else I've run across) result in LOWER_VAR being an empty string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

記憶穿過時間隧道 2024-07-23 07:43:59

您始终可以生成 tr

LOWER_VAR = `echo $(VAR) | tr A-Z a-z`

LOWER_VAR  = $(shell echo $(VAR) | tr A-Z a-z)

您尝试调用的“lc”函数来自 GNU Make Standard Library

假设安装后,正确的语法是

LOWER_VAR  = $(call lc,$(VAR))

you can always spawn off tr

LOWER_VAR = `echo $(VAR) | tr A-Z a-z`

or

LOWER_VAR  = $(shell echo $(VAR) | tr A-Z a-z)

The 'lc' functions you trying to call is from GNU Make Standard Library

Assuming that is installed , the proper syntax would be

LOWER_VAR  = $(call lc,$(VAR))
小…楫夜泊 2024-07-23 07:43:59

您可以直接在 gmake 中执行此操作,无需使用 GNU Make 标准库:

lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))

VAR = MixedCaseText
LOWER_VAR = $(call lc,$(VAR))

all:
        @echo $(VAR)
        @echo $(LOWER_VAR)

它看起来有点笨重,但它可以完成工作。

如果您确实使用 $(shell) 变量,请使用 := 而不是仅使用 =,如 LOWER_VAR := $(shell echo $VAR | tr AZ az)。 这样,您只需在声明变量时调用 shell 一次,而不是每次引用该变量时!

You can do this directly in gmake, without using the GNU Make Standard Library:

lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))

VAR = MixedCaseText
LOWER_VAR = $(call lc,$(VAR))

all:
        @echo $(VAR)
        @echo $(LOWER_VAR)

It looks a little clunky, but it gets the job done.

If you do go with the $(shell) variety, please do use := instead of just =, as in LOWER_VAR := $(shell echo $VAR | tr A-Z a-z). That way, you only invoke the shell one time, when the variable is declared, instead of every time the variable is referenced!

醉态萌生 2024-07-23 07:43:59

处理带有重音符号的大写字母:

LOWER_VAR  = $(shell echo $VAR | tr '[:upper:]' '[:lower:]')

结果:

$ VAR="Éclipse"
$ echo $VAR | tr A-Z a-z
Éclipse
$ echo $VAR | tr '[:upper:]' '[:lower:]'
éclipse

To handle capital letters with accents:

LOWER_VAR  = $(shell echo $VAR | tr '[:upper:]' '[:lower:]')

Results:

$ VAR="Éclipse"
$ echo $VAR | tr A-Z a-z
Éclipse
$ echo $VAR | tr '[:upper:]' '[:lower:]'
éclipse
擦肩而过的背影 2024-07-23 07:43:59

我觉得这个稍微干净一点...

$(shell tr '[:upper:]' '[:lower:]' <<< $(VAR))

I find this slightly cleaner...

$(shell tr '[:upper:]' '[:lower:]' <<< $(VAR))
ま昔日黯然 2024-07-23 07:43:59

如果安装了 Python,它甚至可以在 Windows 上运行:

$(shell python -c "print('$(VAR)'.lower())")

If Python is installed this runs even on Windows:

$(shell python -c "print('$(VAR)'.lower())")
夜夜流光相皎洁 2024-07-23 07:43:59

GNU make 不包含用于大小写转换的字符串函数。 因此,默认情况下没有定义 lc 函数。

但 GNU Make 通常会启用 GNU Guile 支持(例如 Fedora 33 上就是这种情况)。

因此,您可以调用 Guile 函数来转换大小写:

VAR = MixedCaseText
LOWER_VAR = $(guile (string-downcase "$(VAR)"))

default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

或者如果您想封装 Guile 调用:

VAR = MixedCaseText
LOWER_VAR = $(call to_lower,$(VAR))


define to_lower
$(guile (string-downcase "$(1)"))
endef


default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

GNU make doesn't include string functions for case conversion. Thus, there is no lc function defined, by default.

But GNU Make usually comes with GNU Guile support enabled (e.g. this is the case on Fedora 33).

Thus, you can just call a Guile function for converting the case:

VAR = MixedCaseText
LOWER_VAR = $(guile (string-downcase "$(VAR)"))

default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

Or if you want to encapsulate the Guile call:

VAR = MixedCaseText
LOWER_VAR = $(call to_lower,$(VAR))


define to_lower
$(guile (string-downcase "$(1)"))
endef


default:
        @echo $(VAR)
        @echo $(LOWER_VAR)
甜尕妞 2024-07-23 07:43:59

我在寻找解决方案时写了这篇文章。
它有点冗长,但相信它解释了步骤并在 Makefile 中保留了很长的行。

您可以轻松修改它以执行您可能想要的任何替换。

希望它能帮助某人。

# set the separator for the *_TABLE variables, needed as otherwise `$(addprefix ...)` fails
luc_JOIN ::= ,

# define the upper and lower cased characters
lc_CHARS ::= a b c d e f g h i j k l m n o p q r s t u v w x y z
uc_CHARS ::= A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

# join the above to create the *_TABLE variables (i.e `a,A b,B ...`, `A,a B,b ...`)
lc_TABLE ::= $(join $(uc_CHARS),$(addprefix $(luc_JOIN),$(lc_CHARS)))
uc_TABLE ::= $(join $(lc_CHARS),$(addprefix $(luc_JOIN),$(uc_CHARS)))

# an internal macro to recursively create `$(subst ...)` from provided *_TABLE and string, (e.g. `$(subst a,A,$(subst b,B,...))`)
luc_internal = $(if $1,$(subst $(firstword $1),$(call luc_internal,$(wordlist 2,$(words $1),$1),$2)),$2)

# the actual macros to $(call ...), which calls the luc_internal with the correct *_TABLE
lc = $(eval lc_RESULT ::= $(call luc_internal,$(lc_TABLE),$1))$(lc_RESULT)
uc = $(eval uc_RESULT ::= $(call luc_internal,$(uc_TABLE),$1))$(uc_RESULT)

# a mixed case value
VAR = SOME text

default:
    @echo $(call lc,$(VAR))
    @echo $(call uc,$(VAR))

I wrote this while looking for a solution.
It is a bit verbose but believe it explains the steps and keeps really long lines out on the Makefile.

You can easily be modify it to perform any substitution you may want.

Hope it helps someone.

# set the separator for the *_TABLE variables, needed as otherwise `$(addprefix ...)` fails
luc_JOIN ::= ,

# define the upper and lower cased characters
lc_CHARS ::= a b c d e f g h i j k l m n o p q r s t u v w x y z
uc_CHARS ::= A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

# join the above to create the *_TABLE variables (i.e `a,A b,B ...`, `A,a B,b ...`)
lc_TABLE ::= $(join $(uc_CHARS),$(addprefix $(luc_JOIN),$(lc_CHARS)))
uc_TABLE ::= $(join $(lc_CHARS),$(addprefix $(luc_JOIN),$(uc_CHARS)))

# an internal macro to recursively create `$(subst ...)` from provided *_TABLE and string, (e.g. `$(subst a,A,$(subst b,B,...))`)
luc_internal = $(if $1,$(subst $(firstword $1),$(call luc_internal,$(wordlist 2,$(words $1),$1),$2)),$2)

# the actual macros to $(call ...), which calls the luc_internal with the correct *_TABLE
lc = $(eval lc_RESULT ::= $(call luc_internal,$(lc_TABLE),$1))$(lc_RESULT)
uc = $(eval uc_RESULT ::= $(call luc_internal,$(uc_TABLE),$1))$(uc_RESULT)

# a mixed case value
VAR = SOME text

default:
    @echo $(call lc,$(VAR))
    @echo $(call uc,$(VAR))
星星的轨迹 2024-07-23 07:43:59

Eric Melski 的回答给我留下了深刻的印象,我很好奇 make 如何处理递归(我正在看你的 C 预处理器)。 比原来的答案稍微复杂一些,但一个 50 年前的工具可以做的事情很有趣。 并不是说你应该使用这段代码,但我想你可以。

pop2 = $(wordlist 3,$(words $(1)),$(1))
sub1 = $(subst $(word 1,$(1)),$(word 2,$(1)),$(2))
map = $(if $(1),$(call sub1,$(1),$(call map,$(call pop2,$(1)),$(2))),$(2))

upperMap := a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z
upper = $(call map,$(upperMap),$(1))

lowerMap := A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z
lower = $(call map,$(lowerMap),$(1))

#Usage:
x := $(call upper,AaBbCcDdEe)

Being impressed by the Eric Melski answer, I was curious how make handles recursion (I'm looking at you C preprocessor). Somewhat more involved, than original answer, but it's fascinating what a 50 years old tool can do. Not saying you should use this code, but I guess you could.

pop2 = $(wordlist 3,$(words $(1)),$(1))
sub1 = $(subst $(word 1,$(1)),$(word 2,$(1)),$(2))
map = $(if $(1),$(call sub1,$(1),$(call map,$(call pop2,$(1)),$(2))),$(2))

upperMap := a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z
upper = $(call map,$(upperMap),$(1))

lowerMap := A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z
lower = $(call map,$(lowerMap),$(1))

#Usage:
x := $(call upper,AaBbCcDdEe)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文