GNU制作 - 动态创建变量名 [英] GNU Make - Dynamically created variable names

查看:111
本文介绍了GNU制作 - 动态创建变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个makefile设置,其中它接受被在构建时解析,以确定要使用的编译器命令行参数, CPULIST

I have a makefile setup where it accepts a command-line argument that gets parsed at build time to determine which compiler to use, CPULIST.

所以,我打算通过下面的命令来构建:让所有CPULIST =胳膊的x86。然后,我创造了一些虚假的规则,使让所有循环和有效的作用:

So, I plan to build via the following command: make all CPULIST="arm x86". I then created some phony rules so that make all loops and effectively does:

make all CPULIST=x86
make all CPULIST=arm

的规则:

.PHONY: all
all:
    @echo "Detected CPULIST:${CPULIST_DETECTED}"
    @echo "CPULIST:${CPULIST}"
    @for cpu in $(CPULIST_DETECTED); do         \
        echo "CPU:$${cpu}";                      \
        variant_$${cpu}=abc;                        \
        echo "variant_$${cpu}: $${variant_x86}"; \
        $(MAKE) build CPULIST=$$cpu;             \
    done
    @true

.PHONY: build
build: sanity_check $(TARGET)
    @true

如果这是一个bash脚本,我会使用变种_ $ {CPU} ,例如,有它动态地创建变量名和任务,如:

If this were a bash script, I would use variant_${cpu} to, for example, have it dynamically create variables name and assignments like:

variant_x86=abc;
variant_arm=abc;

看来我没有收到转义序列正确的,至少在GNU使语法。

It seems I'm not getting the escape sequences correct, at least in GNU make syntax.

有关list_algorithms.o大厦依赖文件

Building dependencies file for list_algorithms.o

Detected CPULIST:x86 arm
CPULIST:test arm x86
CPU:x86
/bin/sh: 3: variant_x86=abc: not found
variant_x86:
make[1]: Entering directory `/home/svn/public/lib/libMYLib'
CPULIST:x86

我似乎有两个问题:

I seem to be having two problems:


  1. 我的字符串扩张似乎有什么东西在makefile正试图扩张后执行

  2. 我似乎没有被赋予任何 variant_x86 ,所以语法是关闭的。

  1. My string expansion seems to be something the makefile is trying to execute after expansion
  2. I don't seem to be assigning anything to variant_x86, so the syntax is off.

我如何解决这两个问题?

How do I resolve these two issues?

感谢您。

推荐答案

这无关与化妆。你正在试图做的是不合法的,因为你写它,在外壳。你不能在这样的外壳打造动态变量名

This has nothing to do with make. What you're trying to do is not legal, as you've written it, in the shell. You can't create "dynamic variable names" in the shell like this:

$ a=foo
$ xxx$a=baz
xxxfoo=baz: command not found

如果你想这样做,你必须使用eval壳:

If you want to do this in the shell you have to use eval:

$ a=foo
$ eval xxx$a=baz
$ echo $xxxfoo
baz

所以,重新编写你化妆的规则:

So rewrite your make rule:

all:
        @echo "Detected CPULIST:${CPULIST_DETECTED}"
        @echo "CPULIST:${CPULIST}"
        @for cpu in $(CPULIST_DETECTED); do          \
            echo "CPU:$${cpu}";                      \
            eval variant_$${cpu}=abc;                \
            echo "variant_$${cpu}: $${variant_x86}"; \
            $(MAKE) build CPULIST=$$cpu;             \
        done

另外,添加 @true 在这个配方年底已经没有用了。

Also, adding a @true at the end of this recipe has no use.

这篇关于GNU制作 - 动态创建变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆