英文:
Implementing target-specific variable assignment in AIX's native make
问题
在AIX的本机make程序中,没有直接支持目标特定变量分配的功能。为了实现您的需求,您可以考虑以下两种方法:
- 将
A
的定义放在aixmakefile 中,并在需要的目标中覆盖它。这将允许您在AIX上的特定目标中设置不同的值。示例如下:
# aixmakefile
A=somenewvalue
target1:
# some recipe using $(B1)
target2:
# some recipe using $(B2)
- 如果您希望在不同的目标中设置不同的
A
值,您可以使用条件语句在主makefile中根据目标名称来分配不同的值。示例如下:
# main makefile
C=$(C1) $(A) $(C2)
B1=$(B11) $(C) $(B12)
B2=$(B21) $(C) $(B22)
ifeq ($(MAKECMDGOALS),target1)
A=somenewvalue
endif
ifeq ($(MAKECMDGOALS),target2)
A=somenewvalue
endif
target1:
# some recipe using $(B1)
target2:
# some recipe using $(B2)
include aixmakefile
这两种方法可以实现在AIX上的特定目标中覆盖A
的值。希望这有助于解决您的问题。
英文:
I have a main makefile (common for all platforms on which my software is deployed on) and a platform specific makefile containing AIX specific settings. The platform makefile is included at the end of the main makefile.
The main makefile defines a macro A=somevalueA
that I want to override with a new value somenewvalue
in the context of a few (not all) targets defined in the main makefile, for AIX. This makes it easier to visualize:
# main makefile
A=somevalueA
C=$(C1) $(A) $(C2)
B1=$(B11) $(C) $(B12)
B2=$(B21) $(C) $(B22)
B3=$(B31) $(C) $(B32)
B4=$(B41) $(C) $(B42)
target1:
# some recipe using $(B1)
target2:
# some recipe using $(B2)
target3:
# some recipe using $(B3)
target4:
# some recipe using $(B4)
include aixmakefile
Now suppose I want to override the value of A
on AIX for targets target1
, target2
only. With gmake, I can simply add the following target-specific variable assignments in aixmakefile:
# aixmakefile
...
target1: A=somenewvalue
target2: A=somenewvalue
But this is not supported by AIX's native make program. Alternatives to target-specific assignment that come to my mind are:
- Copying the definitions of
B1
,B2
to aixmakefile and substituting occurrences of$(A)
withsomenewvalue
($(A)
is guaranteed to be "at the end of a word" as described in this article on gmake substitution references, which is also true for AIX's native make):
# aixmakefile
...
B1=$(B11:$(A)=somenewvalue) $(C:$(A)=somenewvalue) $(B12:$(A)=somenewvalue)
B2=$(B21:$(A)=somenewvalue) $(C:$(A)=somenewvalue) $(B22:$(A)=somenewvalue)
- Copying the entire recipe for targets
target1
,target2
to aixmakefile and substituting occurrences of$(A)
withsomenewvalue
:
# aixmakefile
...
target1:
# some recipe using $(B1:$(A)=somenewvalue)
target2:
# some recipe using $(B2:$(A)=somenewvalue)
As you can see, these approaches are not very elegant. I was wondering if there is a neat hack to implement target-specific variable assignments in AIX's make or some other concise, elegant solution to my problem that eludes me.
Thanks for your time.
答案1
得分: 3
如果您在目标名称和值之间有明确定义的关系,并且不需要目标特定变量的“继承”功能,您可以使用构建的变量名称:
C = $(C1) $($@_A) $(C2)
...
target1_A = somevalueA
target2_A = somevalueA
target3_A = somevalueA
target4_A = somevalueA
现在在aixmakefile
中,您可以设置:
target1_A = somenewvalue
target2_A = somenewvalue
在$($@_A)
中的$@
将扩展为目标名称(target1
、target2
等),因此它将扩展为$(target1_A)
等。
您可以在这里阅读更多关于Make中的元编程信息:https://make.mad-scientist.net/category/metaprogramming/,但请注意,只有最早期的部分才是真正可移植的:更新/更强大的部分需要GNU Make。
英文:
If you have a well-defined relationship between the target names and the value, AND you don't need the "inheritance" feature of target-specific variables, you can just use constructed variable names:
C = $(C1) $($@_A) $(C2)
...
target1_A = somevalueA
target2_A = somevalueA
target3_A = somevalueA
target4_A = somevalueA
Now in aixmakefile
you can set:
target1_A = somenewvalue
target2_A = somenewvalue
The $@
in $($@_A)
will expand to the target name (target1
, target2
, etc.) and so it will expand to $(target1_A)
etc.
You can read more about metaprogramming in make here: https://make.mad-scientist.net/category/metaprogramming/ but note only the earliest parts of this are really portable: the newer/more powerful parts require GNU Make.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论