我使用Makefiles。

我有一个名为run的目标,它运行构建目标。简化后,如下图所示:

prog: ....
  ...

run: prog
  ./prog

有传递参数的方法吗?这

make run asdf --> ./prog asdf
make run the dog kicked the cat --> ./prog the dog kicked the cat

当前回答

建立在理想答案的基础上。 你可以创建一个通用的“函数”来获取参数 使用以下方法:

define fetch_parameter
    $(eval target_name:= $(firstword $(MAKECMDGOALS)))
    $(eval varname := $(target_name)_value)
    $(eval $(varname) := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)))
    $(eval $($(varname))::;@:)
endef

然后你可以像这样在你的目标中使用它:

my-target: res := $(call fetch_parameter)
my-target: ## Example target. Usage: make my-target <value>
    echo The value: $($@_value)

这样你就可以在任何你想检索值的目标上使用res:= $(调用fetch_parameter)。

注意:我在这段代码中添加了额外的:$(eval $($(varname))::;@:),因为如果你有多个调用fetch_parameter的目标,它也会被触发。 如果你有:

my-target: res := $(call fetch_parameter)
my-target: ## Example target. Usage: make my-target <value>
    echo The value: $($@_value)

my-target2: res := $(call fetch_parameter)
my-target2: ## Example target. Usage: make my-target2 <value>
    echo The value: $($@_value)

并且你调用make my-target2 hello, make的工作方式,$(call fetch_parameter)都将被触发,导致创建2个伪hello目标,但额外的:(hello::) make不会抱怨你覆盖了一个目标。

其他回答

我使用的另一个技巧是-n标志,它告诉make做一个演练。例如,

$ make install -n 
# Outputs the string: helm install stable/airflow --name airflow -f values.yaml
$ eval $(make install -n) --dry-run --debug
# Runs: helm install stable/airflow --name airflow -f values.yaml --dry-run --debug

Run: ./prog看起来有点奇怪,因为正确的部分应该是一个先决条件,所以Run: prog看起来更好。

我的建议很简单:

.PHONY: run

run:
    prog $(arg1)

我想补充一点,可以传递参数:

作为参数:使arg1="asdf"运行 或者定义为environment: arg1="asdf" make run

建立在理想答案的基础上。 你可以创建一个通用的“函数”来获取参数 使用以下方法:

define fetch_parameter
    $(eval target_name:= $(firstword $(MAKECMDGOALS)))
    $(eval varname := $(target_name)_value)
    $(eval $(varname) := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)))
    $(eval $($(varname))::;@:)
endef

然后你可以像这样在你的目标中使用它:

my-target: res := $(call fetch_parameter)
my-target: ## Example target. Usage: make my-target <value>
    echo The value: $($@_value)

这样你就可以在任何你想检索值的目标上使用res:= $(调用fetch_parameter)。

注意:我在这段代码中添加了额外的:$(eval $($(varname))::;@:),因为如果你有多个调用fetch_parameter的目标,它也会被触发。 如果你有:

my-target: res := $(call fetch_parameter)
my-target: ## Example target. Usage: make my-target <value>
    echo The value: $($@_value)

my-target2: res := $(call fetch_parameter)
my-target2: ## Example target. Usage: make my-target2 <value>
    echo The value: $($@_value)

并且你调用make my-target2 hello, make的工作方式,$(call fetch_parameter)都将被触发,导致创建2个伪hello目标,但额外的:(hello::) make不会抱怨你覆盖了一个目标。

您可以显式地提取命令行中的第n个参数。要做到这一点,你可以使用变量MAKECMDGOALS,它保存给'make'的命令行参数列表,它将其解释为目标列表。如果你想提取第n个参数,你可以使用该变量与"word"函数结合使用,例如,如果你想要第二个参数,你可以将它存储在一个变量中,如下所示:

second_argument := $(word 2, $(MAKECMDGOALS) )

你可以像下面这样将变量传递给Makefile:

run:
    @echo ./prog $$FOO

用法:

$ make run FOO="the dog kicked the cat"
./prog the dog kicked the cat

or:

$ FOO="the dog kicked the cat" make run
./prog the dog kicked the cat

或者使用Beta提供的解决方案:

run:
    @echo ./prog $(filter-out $@,$(MAKECMDGOALS))
%:
    @:

%: -匹配任何任务名称的规则; @: -空食谱=什么都不做

用法:

$ make run the dog kicked the cat
./prog the dog kicked the cat