Makefile - 循环遍历列表并选择特定值
我正在尝试做这样的事情(假设 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这应该是 Makefile 中的命令,则语法必须是这样的:
但是您没有告诉我们 是什么。和 <文件名>应该是,所以我假设它们是其他
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:
But you didn't tell us what <input> and <file_name> are supposed to be, so I assumed they are other
make
variables. Basically themake
rules look like one long shell line, with commands separated by semicolons, and lines continued with backslashes. $$ is replaced bymake
with a single $, which is why references to shell variables ($$prefix) need two dollars.Your
make
manual (typeman make
has the whole story and is fun to read and understand.) Become amake
guru today! Be sure to understand the difference between amake
variable and a shell variable.