使功能和“命令在第一个目标之前开始”?

Édo*_*pez 1 bash makefile function gnu-make

这是我运行的命令

make -d -f dump.makefile
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

Reading makefile `dump.makefile'...
dump.makefile:31: *** commands commence before first target.  Stop.
Run Code Online (Sandbox Code Playgroud)

来源

ifneq (,)
This makefile requires GNU Make.
endif

# force use of Bash
SHELL := /bin/bash


# function
today=$(shell date '+%Y-%m:%b-%d')
update-latest=$(shell ln -nf {$(call today),latest}.cfdict-"$(1)".localhot.sql)

# variables
credentials="$$HOME/.my.cfdict.cnf"

default: data-only structure-only csv-only all

data-only: what=data
    argList=( --defaults-file="${credentials}" --no-create-db --no-create-info ) \
    mysqldump "$${argList[@]}" cfdict > $(call today).cfdict-"${what}".localhot.sql
    $(call update-latest,${what})
Run Code Online (Sandbox Code Playgroud)

触发错误的行是$(call update-latest,${what})调用update-latest函数。

完整的要点可在 github 上找到

我检查制表符/空格,这似乎是正确的。我是否滥用call或严重声明update-latest

Lou*_*uis 5

导致您报告错误的问题是您没有将特定于目标的变量定义与规则的定义分开。您目前有这种形式的规则:

data-only: what=data
        ... commands ...
Run Code Online (Sandbox Code Playgroud)

您可能期望该data-only: what=data行定义特定于目标的变量和规则,但事实并非如此。

您需要的是为变量声明添加一行,然后重复规则的目标名称。像这样:

data-only: what=data
data-only:
        ... commands ...
Run Code Online (Sandbox Code Playgroud)

因此data-only,仅举一个例子,将变成:

data-only: what=data
data-only:
    argList=( --defaults-file="${credentials}" --no-create-db --no-create-info ) \
    mysqldump "$${argList[@]}" cfdict > $(call today).cfdict-"${what}".localhot.sql
    $(call update-latest,${what})
Run Code Online (Sandbox Code Playgroud)

我看到您声明argList为 shell 变量,因此不需要更改。

您必须类似地更新所有在 Makefile 中具有特定于目标的变量的目标。