构建后,为什么我的makefile在依赖项上调用rm? [英] Why does my makefile call rm on dependencies after building?

查看:72
本文介绍了构建后,为什么我的makefile在依赖项上调用rm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行make -j tests时,它可以使测试正常进行,但随后会删除依赖项.为什么要这样做,我该如何解决?对于复杂的Makefile,我深表歉意. makefile的相关部分是测试"部分.

When I run make -j tests, it makes the tests just fine but removes the dependencies afterwards. Why is it doing this and how can I solve it? I apologize for the complicated makefile. The relevant portions of the makefile are the Test sections.

Makefile:

# Build tools
CC = clang++ -g --std=gnu++11 -O3
LEX = flex
YACC = bison -d

# Includes
CC_HEADERS   = `llvm-config-3.5 --cppflags`
CC_LIBRARIES = -lboost_program_options `llvm-config-3.5 --libs all --ldflags` -ltinfo -lpthread -lffi -ldl -lm -lz

# Created files
GENERATED_SOURCES = parser.cpp tokens.cpp
GENERATED_FILES   = $(GENERATED_SOURCES)
EXEC = brainfuck.out

# Test cases
TESTS            = $(wildcard ./tests/*.bf)
TESTS_IN         = $(TESTS:.bf=.in)
TESTS_BUILD      = $(TESTS:.bf=.build)
TESTS_EXPECTED   = $(TESTS:.bf=.expected)
TESTS_ACTUAL     = $(TESTS:.bf=.actual)
TESTS_DIFF       = $(TESTS:.bf=.diff)
GENERATED_FILES += $(TESTS_BUILD) $(TESTS_EXPECTED) $(TESTS_ACTUAL) $(TESTS_DIFF)

# Generic config
SOURCES  = $(filter-out $(GENERATED_SOURCES), $(wildcard *.cpp))
SOURCES += $(GENERATED_SOURCES)
OBJECTS  = $(SOURCES:.cpp=.o)

# Main targets
target: $(EXEC)

all: target tests

# Generated source targets
tokens.cpp: tokens.l parser.hpp
    $(LEX) -o $@ $<

parser.hpp: parser.cpp

parser.cpp: parser.y
    $(YACC) -o $@ $<

# Test targets
tests: $(TESTS_DIFF)
    @echo ""
    @echo "#####################"
    @echo "# Begin test output #"
    @echo "#####################"
    @$(foreach f,$^, echo "Test:" $(f:.diff=.bf); cat $(f); echo "";)
    @echo "#####################"
    @echo "#  End test output  #"
    @echo "#####################"
    @echo ""

tests/%.build: tests/%.bf $(EXEC)
    ./brainfuck.out $< -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o $@

tests/%.expected: tests/%.bf tests/%.in
    -bf -c65535 $< < $(word 2,$^) > $@

tests/%.actual: tests/%.build tests/%.in
    -$< < $(word 2,$^) > $@

tests/%.diff: tests/%.expected tests/%.actual
    -diff $< $(word 2,$^) > $@

# Generic targets
clean:
    rm -rf $(EXEC) $(OBJECTS) $(GENERATED_FILES) $(GENERATED_SOURCES:.cpp=.hpp)

$(EXEC): $(OBJECTS)
    $(CC) -o $@ ${OBJECTS} $(CC_LIBRARIES)

%.o: %.cpp parser.hpp
    $(CC) $(CC_HEADERS) -c $< -o $@

进行输出:

bf -c65535 tests/awib-0.4.bf < tests/awib-0.4.in > tests/awib-0.4.expected
./brainfuck.out tests/awib-0.4.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/awib-0.4.build
bf -c65535 tests/dbfi.bf < tests/dbfi.in > tests/dbfi.expected
./brainfuck.out tests/dbfi.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/dbfi.build
bf -c65535 tests/factor.bf < tests/factor.in > tests/factor.expected
./brainfuck.out tests/factor.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/factor.build
bf -c65535 tests/hanoi.bf < tests/hanoi.in > tests/hanoi.expected
./brainfuck.out tests/hanoi.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/hanoi.build
bf -c65535 tests/long.bf < tests/long.in > tests/long.expected
./brainfuck.out tests/long.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/long.build
bf -c65535 tests/mandelbrot.bf < tests/mandelbrot.in > tests/mandelbrot.expected
./brainfuck.out tests/mandelbrot.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/mandelbrot.build
bf -c65535 tests/prime.bf < tests/prime.in > tests/prime.expected
./brainfuck.out tests/prime.bf -p | llc-3.5 - -o - | gcc -O0 -x assembler - -o tests/prime.build
tests/dbfi.build < tests/dbfi.in > tests/dbfi.actual
tests/long.build < tests/long.in > tests/long.actual
tests/factor.build < tests/factor.in > tests/factor.actual
tests/prime.build < tests/prime.in > tests/prime.actual
tests/mandelbrot.build < tests/mandelbrot.in > tests/mandelbrot.actual
tests/hanoi.build < tests/hanoi.in > tests/hanoi.actual
tests/awib-0.4.build < tests/awib-0.4.in > tests/awib-0.4.actual
diff tests/factor.expected tests/factor.actual > tests/factor.diff
diff tests/awib-0.4.expected tests/awib-0.4.actual > tests/awib-0.4.diff
diff tests/mandelbrot.expected tests/mandelbrot.actual > tests/mandelbrot.diff
diff tests/hanoi.expected tests/hanoi.actual > tests/hanoi.diff
diff tests/long.expected tests/long.actual > tests/long.diff
diff tests/prime.expected tests/prime.actual > tests/prime.diff
diff tests/dbfi.expected tests/dbfi.actual > tests/dbfi.diff

#####################
# Begin test output #
#####################
Test: tests/awib-0.4.bf

Test: tests/dbfi.bf

Test: tests/factor.bf

Test: tests/hanoi.bf

Test: tests/long.bf

Test: tests/mandelbrot.bf

Test: tests/prime.bf

#####################
#  End test output  #
#####################

rm tests/mandelbrot.actual tests/hanoi.build tests/long.actual tests/mandelbrot.build tests/factor.actual tests/awib-0.4.actual tests/long.build tests/prime.actual tests/hanoi.expected tests/factor.build tests/awib-0.4.build tests/dbfi.expected tests/prime.build tests/mandelbrot.expected tests/dbfi.actual tests/long.expected tests/dbfi.build tests/hanoi.actual tests/factor.expected tests/prime.expected tests/awib-0.4.expected

注意最后一行rm ....这是哪里来的?

Notice the last line rm .... Where is this coming from?

推荐答案

解决方案是将.SECONDARY:放在makefile的顶部.此处的更多信息:

The solution is to put .SECONDARY: at the top of the makefile. More info here: http://www.thinkplexx.com/learn/howto/build-chain/make-based/prevent-gnu-make-from-always-removing-files-it-says-things-like-rm-or-removing-intermediate-files

节选:

GNU make会跟踪一些在构建过程中创建的文件, 并且它将在构建后删除此类文件.这些文件称为 中间文件".它们应该是由make的连锁店"创建的 隐式规则".因为它们是为促进某些事情而创建的 否则,请在构建后将其视为无用并将其删除.

GNU make does track some files, which are created during the build, and it will remove such files after the build. The files are called "intermediate files". They are supposed to be created by make’s "chain of Implicit Rules". Because they were created to facilitate something else, make considers them useless after the build and removes them.

...

.SECONDARY会将所有目标视为 次要的(即未删除任何目标,因为它已被视为 中级

.SECONDARY with no prerequisites causes all targets to be treated as secondary (i.e., no target is removed because it is considered intermediate

这篇关于构建后,为什么我的makefile在依赖项上调用rm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆