Makefile - 循环遍历列表并选择特定值

发布于 2024-11-29 16:19:48 字数 740 浏览 1 评论 0原文

我正在尝试做这样的事情(假设 $input 是用户提供的东西):

LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0

for prefix in $(LIST); do \
    if $(input) == $(prefix) then
        START = 1
    endif \
    if $(START) == 1 then \
        if [ -f  $(prefix)<file_name> ]; then <do_A>; else <do_B>; fi
    endif \
done

我的问题是上面提到的两个 if 。我不知道如何在迭代时从列表中选择特定的字符串值(如果 $(input) == $(prefix) 那么)并且我不知道如何检查值是 1 还是 0 (如果 $(START) == 1 那么)。

我使用此代码的目的是对具有相同文件名但具有不同前缀的不同目录使用相同的 makefile。有时,一个目录可能包含具有不同前缀的文件的多个版本,并且我想定义这些前缀的层次结构(在我的示例中由 LIST 定义)。当用户提供版本时,想法是从他提供的版本开始搜索最新版本(例如,如果用户提供 pre4,那么我需要首先搜索 pre4,如果它不存在 - i将继续搜索 pre5 等,但在本例中,我不会搜索 pre1,即使它确实存在于当前目录中)。

任何人都知道我该怎么做?

提前致谢。

I'm trying to do something like this (assuming $input is something provided by the user):

LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0

for prefix in $(LIST); do \
    if $(input) == $(prefix) then
        START = 1
    endif \
    if $(START) == 1 then \
        if [ -f  $(prefix)<file_name> ]; then <do_A>; else <do_B>; fi
    endif \
done

my problem is with the two if's mentioned above. i don't know how can i choose a specific string value from a list while iterating it (if $(input) == $(prefix) then) and i don't know how to check if a value is 1 or 0 (if $(START) == 1 then).

My intent with this code is to use the same makefile for different directories which have the same file name, but with a different prefix. sometimes, a directory might contain multiple versions of the file with a different prefix and i want to define a hierarchy of those prefixes (defined by LIST in my example). when the user provide a version, the idea is to start searching for the most up-to date version, starting from the version he provides (e.g. if the user provide pre4, then i need to search pre4 first and if it's not exist - i'll go on and search for pre5 and so on. but in this example, i won't search for pre1 even if it do exist in the current directory).

Anyone has an idea on how can i do that?

Thanks in advance.

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

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

发布评论

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

评论(1

半夏半凉 2024-12-06 16:19:48

如果这应该是 Makefile 中的命令,则语法必须是这样的:

LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
input = somename
file_name = whatever

some_target:
    for prefix in $(LIST); do \
        if test "$(input)" = $prefix; then \
            START=1; \
        fi; \
        if test "$(START)" = 1; then \
            if test -f  $prefix$(file_name); then \
                <do_A>; \
            else \
                <do_B>; \
            fi; \
        fi; \
    done

但是您没有告诉我们 是什么。和 <文件名>应该是,所以我假设它们是其他 make 变量。基本上,make 规则看起来像一长串 shell 行,命令之间用分号分隔,各行以反斜杠继续。 $$ 被 make 替换为单个 $,这就是为什么对 shell 变量($$前缀)的引用需要两美元。

您的 make 手册(输入 man make 包含整个故事,阅读和理解很有趣。)今天就成为 make 大师!请务必了解 make 变量和 shell 变量之间的区别。

If that is supposed to be a command in a Makefile, the syntax would have to be something like this:

LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
input = somename
file_name = whatever

some_target:
    for prefix in $(LIST); do \
        if test "$(input)" = $prefix; then \
            START=1; \
        fi; \
        if test "$(START)" = 1; then \
            if test -f  $prefix$(file_name); then \
                <do_A>; \
            else \
                <do_B>; \
            fi; \
        fi; \
    done

But you didn't tell us what <input> and <file_name> are supposed to be, so I assumed they are other make variables. Basically the make rules look like one long shell line, with commands separated by semicolons, and lines continued with backslashes. $$ is replaced by make with a single $, which is why references to shell variables ($$prefix) need two dollars.

Your make manual (type man make has the whole story and is fun to read and understand.) Become a make guru today! Be sure to understand the difference between a make variable and a shell variable.

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