在AIX的本地make中实现针对特定目标的变量赋值。

huangapple go评论48阅读模式
英文:

Implementing target-specific variable assignment in AIX's native make

问题

在AIX的本机make程序中,没有直接支持目标特定变量分配的功能。为了实现您的需求,您可以考虑以下两种方法:

  1. A 的定义放在aixmakefile 中,并在需要的目标中覆盖它。这将允许您在AIX上的特定目标中设置不同的值。示例如下:
# aixmakefile
A=somenewvalue

target1:
  # some recipe using $(B1)

target2:
  # some recipe using $(B2)
  1. 如果您希望在不同的目标中设置不同的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:

  1. Copying the definitions of B1, B2 to aixmakefile and substituting occurrences of $(A) with somenewvalue ($(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)
  1. Copying the entire recipe for targets target1,target2 to aixmakefile and substituting occurrences of $(A) with somenewvalue:
# 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)中的$@将扩展为目标名称(target1target2等),因此它将扩展为$(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.

huangapple
  • 本文由 发表于 2023年6月1日 02:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定