Makefile通过覆盖构建变量来共享目标

时间:2018-12-02 06:18:29

标签: makefile gnu-make

我有一个makefile,其一部分如下所示:

.PHONY: all
all: master dispatcher

.PHONY: master
master: BUILDTARGET=master
master: buildbin    ## Builds master

.PHONY: dispatcher
dispatcher: BUILDTARGET=dispatcher
dispatcher: buildbin    ## Builds dispatcher

.PHONY: buildbin 
buildbin:
@cd ${BUILD_FOLDER}/${BUILDTARGET} && ${MAKE} build GOBIN=${GOBIN} 
     LDFLAGS="${LDFLAGS}" CMD_FOLDER=${CMD_FOLDER}
@cd ${PROJECT_ROOT}

请假定要设置BUILD_FOLDER,GOBIN,LDFLAGS,CMD_FOLDER,PROJECT_ROOT。

运行中的麻烦使make始终仅构建“ master”并退出。 更改“全部”的顺序,我发现它只是构建第一个。 上面的Makefile有什么问题。

1 个答案:

答案 0 :(得分:0)

您不能使用这样的依赖项,buildbin只是另一个目标,每次调用最多可以重做一次。您可以按以下步骤解决此问题:

.PHONY: all master dispatcher
all: master dispatcher

master: BUILDTARGET=master
dispatcher: BUILDTARGET=dispatcher

master dispatcher:
    @cd ${BUILD_FOLDER}/${BUILDTARGET} && ${MAKE} build GOBIN=${GOBIN} LDFLAGS="${LDFLAGS}" CMD_FOLDER=${CMD_FOLDER}
    @cd ${PROJECT_ROOT}

或更简单地

.PHONY: all master dispatcher
all: master dispatcher

master dispatcher:
    @cd ${BUILD_FOLDER}/$@ && ${MAKE} build GOBIN=${GOBIN} LDFLAGS="${LDFLAGS}" CMD_FOLDER=${CMD_FOLDER}
    @cd ${PROJECT_ROOT}