Makefile:使用多行字符串作为命令的输入

时间:2014-12-31 00:40:21

标签: makefile typescript

我想使用多行,json编码的字符串作为输入 makefile中的命令。现在,似乎没有任何东西传递给 命令watchman-typescriptwatchman-debug

生成文件

.PHONY: typescript
typescript:
    tsc --target ES5 ts/main.ts

.PHONY: watch
watch:
    watchman watch .

define WATCHMAN_TYPESCRIPT_TRIGGER =
["trigger", "./ts", {
  "name": "tsTrigger",
  "expression": ["suffix", "ts"],
  "command": ["make", "typescript"]
}]
endef

.PHONY: watchman-typescript
watchman-typescript:
    watchman -j '$(WATCHMAN_TYPESCRIPT_TRIGGER)'

.PHONY: watchman-debug
watchman-debug:
    echo $(WATCHMAN_TYPESCRIPT_TRIGGER)

2 个答案:

答案 0 :(得分:3)

虽然这在make本身似乎不可能 - 最后将变量的每一行解释为配方中的命令 - 有一个解决方法:您可以将变量导出为shell变量并引用该变量在命令中。那就是:

define WATCHMAN_TYPESCRIPT_TRIGGER =
["trigger", "./ts", {
  "name": "tsTrigger",
  "expression": ["suffix", "ts"],
  "command": ["make" "typescript"]
}]
endef

# export as shell variable
export WATCHMAN_TYPESCRIPT_TRIGGER

# ...

# use shell variable in the command. $$ in make means $ in the shell command.
watchman-typescript:
    watchman -j "$$WATCHMAN_TYPESCRIPT_TRIGGER"

答案 1 :(得分:1)

在makefile中,您可以使用line continuation character \分割多行:

define WATCHMAN_TYPESCRIPT_TRIGGER = \
["trigger", "./ts", { \
  "name": "tsTrigger", \
  "expression": ["suffix", "ts"], \
  "command": ["make" "typescript"] \
}] \
endef