【问题标题】:Makefile set variable in targetMakefile 在目标中设置变量
【发布时间】:2016-03-23 09:17:59
【问题描述】:

我有一个 makefile,其中变量 $DEBUG 决定是为部署还是调试构建。有一个主要的 Makefile,以及包含多个特定于平台的 Makefile。我希望在构建目标test 时自动将变量$DEBUG 设置为1

主makefile:

DEBUG := 0

test : override DEBUG := 1

DIST_DIR := dist/
ifeq ($(DEBUG), 1)
    BUILD_DIR := build/debug
else
    BUILD_DIR := build/deploy
endif


# detect operating system
ifeq ($(OS), Windows_NT)
    UNAME := Windows
else
    UNAME := $(shell uname -s)
endif

# include platform-specific Makefile
export
ifeq ($(UNAME), Darwin)
    include Makefile.darwin
endif
ifeq ($(UNAME), Linux)
    include Makefile.linux
endif

以及特定于平台的Makefile.linux

... 
CXX := clang++-3.8
CXXFLAGS := -std=c++14 -fPIC -I./external/include -fcolor-diagnostics
LDFLAGS := 
LDLIBS := -lpthread -lm


ifeq ($(DEBUG), 1)
    CXXFLAGS += -g
else
    CXXFLAGS += -O3 -DNDEBUG
endif

... 

all : $(TARGET)


test : $(TEST_TARGET)
    $(TEST_TARGET)

所以test 有两条规则:主makefile 中的一条设置目标特定变量$DEBUGMakefile.linux 中的一条构建测试。用于构建的变量($CXXFLAGS$BUILDDIR 等)设置在规则之前,并使用条件。

makefile 中的所有内容都正常工作,但是调用 make test 不会更改变量 $DEBUG$CXXFLAGS$BUILDDIR 仍然设置为构建部署时所具有的值。

有没有办法在目标为test时先在Makefile中设置$DEBUG := 1,然后正常进行? 或者是否有包含目标名称的特殊变量,例如

ifeq ($(MAKEFILE_TARGET), test)
    ...
endif

【问题讨论】:

标签: makefile


【解决方案1】:

test : override DEBUG := 1 不起作用,因为它声明了一个仅在该配方中可见的 target-specific variable

确实存在一个变量,其中包含在命令行中指定的目标名称:MAKECMDGOALS。请注意,如果未在命令行中明确指定默认目标,则它不包含默认目标。

示例生成文件:

DEBUG := $(filter test,$(MAKECMDGOALS))

all:
    @echo all : $(MAKECMDGOALS) : $(DEBUG) : $(if $(DEBUG),1,0)

test:
    @echo test : $(MAKECMDGOALS) : $(DEBUG) : $(if $(DEBUG),1,0)

用法:

$ make
all : : : 0
$ make all
all : all : : 0
$ make test
test : test : test : 1
$ make all test
all : all test : test : 1
test : all test : test : 1

【讨论】:

    【解决方案2】:

    一个简单的选择是让您的test 目标递归调用makeDEBUG=1。这也适用于包含的 makefile。示例:

    DEBUG := 0
    
    test:
      make DEBUG=1
    
    DIST_DIR := dist/
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2012-07-22
      • 2015-02-01
      • 2013-10-28
      • 1970-01-01
      相关资源
      最近更新 更多