如何将发布目标添加到Makefile? [英] How to add release target to Makefile?
问题描述
我有以下Makefile,我想将其配置为默认生成调试版本,并通过指定相应的目标来释放版本.
I have following Makefile, and I would like to configure it to produce debug build by default and release build by specifying corresponding target.
我现在要解决的问题是-项目包含单元测试,我希望它们包含在默认版本中,但不包含在发行版中,因此我将发行版目标添加到了Makefile中:
The problem I am trying to solve right now is following, - project contains unit tests, and I want them to be included in default build, but excluded from release, so I am added release target to Makefile:
FC = ifort
FFLAGS = -c -free -module modules -g3 -warn all -warn nounused
LDFLAGS = -save-temps -dynamiclib
INTERFACES = src/Foundation.f units/UFoundation.f units/Asserts.f units/Report.f
EXCLUDES = $(patsubst %, ! -path './%', $(INTERFACES))
SOURCES = $(INTERFACES) \
$(shell find . -name '*.f' $(EXCLUDES) | sed 's/^\.\///' | sort)
OBJECTS = $(patsubst %.f, out/%.o, $(SOURCES))
EXECUTABLE = UFoundation
all: $(SOURCES) $(EXECUTABLE)
release: SOURCES := $(filter-out units/%.f, $(SOURCES))
release: OBJECTS := $(filter-out units/%.o, $(OBJECTS))
release: EXECUTABLE := 'Foundation.dlyb'
release: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
@echo 'Linking to $@...'
@$(FC) $(LDFLAGS) $(OBJECTS) -o out/$@
out/%.o: %.f
@echo 'Compiling $@...'
@mkdir -p modules
@mkdir -p $(dir $@)
@$(FC) $(FFLAGS) -c $< -o $@
clean:
@echo "Cleaning..."
@rm -rf modules out $(EXECUTABLE)
不幸的是,这无济于事-'make'和'make release'的结果相同(make release编译默认情况下的单元测试文件).我已经看过类似的问题,例如如何我为调试和发布版本配置了我的makefile吗?,但是解决该问题没有成功.
Unfortunate, it doesn't help - result of 'make' and 'make release' are the same (make release compiles unit test files as in default). I already looked to similar questions, like How can I configure my makefile for debug and release builds?, but without any success to solve the issue.
推荐答案
使用不同的宏(例如NDEBUG
)和不同的优化级别来构建调试和发布目标.通常,您不希望混合使用调试和发行目标文件,因此需要将它们编译到不同的目录中.
Debug and release targets get build with different macros (e.g. NDEBUG
) and different optimization levels. You normally do not want to mix debug and release object files, hence they need to be compiled into different directories.
我经常通过这种方式将不同的顶层构建目录用于不同的构建模式:
I often use a different top-level build directory for different build modes this way:
BUILD := debug
BUILD_DIR := build/${BUILD}
然后编译器以这种方式进行标记:
And compiler flags this way:
cppflags.debug :=
cppflags.release := -DNDEBUG
cppflags := ${cppflags.${BUILD}} ${CPPFLAGS}
cxxflags.debug := -Og
cxxflags.release := -O3 -march=native
cxxflags := -g -pthread ${cxxflags.${BUILD}} ${CXXFLAGS}
然后将您的对象构建到BUILD_DIR
.
And then build your objects into BUILD_DIR
.
当您以make BUILD=release
调用它时,它将覆盖在makefile中进行的BUILD := debug
分配.
When you invoke it as make BUILD=release
that overrides BUILD := debug
assignment made in the makefile.
这篇关于如何将发布目标添加到Makefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!