具有不应硬编码但应在规则调用时作为参数传递的参数

时间:2019-07-18 09:16:15

标签: bash makefile

具有一个包含规则dev-create-empty-migration的makefile,当前,此规则具有硬编码的参数accounts_table,不应对该参数进行硬编码,而应在调用规则时将其作为参数传递。 e。 G。制作dev-create-empty-migration accounts_table

有什么想法怎么做?

.PHONY: dev-create-empty-migration
dev-create-empty-migration:
    migrate create -ext sql -dir
./pkg/acc/repo/postgres/migrations accounts_table

1 个答案:

答案 0 :(得分:1)

您应该使用变量并将其存储在其中。通过使用?=,首先搜索一个参数。如果找到,它将使用参数,否则使用默认内容。

$ cat Makefile 
VAR     ?= derp
test:
        @echo $(VAR)
$ make
derp
$ make VAR=lala
lala

仅仅因为它很有趣,您还可以执行以下操作。这省略了附加参数的使用,但是您当然可以进行一些混合。

$ cat Makefile 
VAR     := None

dev-create-empty-migration-%_table:
        $(eval VAR=$(patsubst dev-create-empty-migration-%,%,$@))
        @echo $(VAR)
$ make dev-create-empty-migration-derp_table
derp_table