如何使这个 Makefile 变得更好/更简单/更少冗余?
我想要一些关于以下 Makefile 的建议。它工作得很好,但它过于多余,并且没有利用 make 的大部分魔力来帮助 C 项目。
其目的是测试一个小型 ANSI C 库。便携性很重要。
.PHONY : test
OPTIMIZE = -g
INCLUDE = -I.
CC = gcc
WARNINGS = -Wall -ansi -pedantic -Wno-long-long -Wextra -Wdeclaration-after-statement -Wendif-labels -Wconversion
CFLAGS = $(WARNINGS) $(OPTIMIZE) $(INCLUDE)
COMPILE = $(CC) $(CFLAGS)
LINK = $(COMPILE)
all : time64.o bin/check_max
bin/check_max : time64.o time64_config.h bin/check_max.c
$(LINK) time64.o bin/check_max.c -o $@
time64.o : time64_config.h time64.h time64.c Makefile
t/bench : t/bench.c time64.o
$(LINK) time64.o t/bench.c -o $@
bench : t/bench
time t/bench
t/localtime_test : t/localtime_test.c time64.o
$(LINK) time64.o t/localtime_test.c -o $@
t/gmtime_test : t/gmtime_test.c time64.o
$(LINK) time64.o t/gmtime_test.c -o $@
t/year_limit.t : t/tap.c t/year_limit.t.c time64.o
$(LINK) time64.o t/year_limit.t.c -o $@
t/negative.t : t/tap.c t/negative.t.c time64.o
$(LINK) time64.o t/negative.t.c -o $@
t/overflow.t : t/tap.c t/overflow.t.c time64.o
$(LINK) time64.o t/overflow.t.c -o $@
t/timegm.t : t/tap.c t/timegm.t.c time64.o
$(LINK) time64.o t/timegm.t.c -o $@
t/safe_year.t : t/tap.c t/safe_year.t.c time64.c
$(LINK) t/safe_year.t.c -o $@
t/gmtime64.t : t/tap.c t/gmtime64.t.c time64.o
$(LINK) time64.o t/gmtime64.t.c -o $@
t/mktime64.t : t/tap.c t/mktime64.t.c time64.o
$(LINK) time64.o t/mktime64.t.c -o $@
t/asctime64.t : t/tap.c t/asctime64.t.c time64.o
$(LINK) time64.o t/asctime64.t.c -o $@
t/ctime64.t : t/tap.c t/ctime64.t.c time64.o
$(LINK) time64.o t/ctime64.t.c -o $@
t/seconds_between_years.t : t/tap.c t/seconds_between_years.t.c time64.c
$(LINK) t/seconds_between_years.t.c -o $@
test : tap_tests localtime_tests
localtime_tests: t/localtime_test t/gmtime_test
@which bzdiff > /dev/null || (echo 'You need bzdiff to run these tests'; exit 1)
@which less > /dev/null || (echo 'You need less to run these tests'; exit 1)
@echo "On failure, these tests will produce a diff between the failed and expected results. If they pass they'll be quiet."
TZ=Canada/Eastern t/gmtime_test | bzip2 -9 > t/gmtime_test.out.bz2
bzdiff -u t/gmtime_test.out.bz2 t/gmtime.out.bz2 | less -F
TZ=Canada/Eastern t/localtime_test | bzip2 -9 > t/eastern_test.out.bz2
bzdiff -u t/eastern_test.out.bz2 t/eastern.out.bz2 | less -F
TZ=Australia/West t/localtime_test | bzip2 -9 > t/oz_test.out.bz2
bzdiff -u t/oz_test.out.bz2 t/oztime.out.bz2 | less -F
tap_tests: t/year_limit.t t/negative.t t/overflow.t t/timegm.t t/safe_year.t t/gmtime64.t t/asctime64.t t/ctime64.t
@which prove > /dev/null || (echo 'You need prove (from the Test::Harness perl module) to run these tests'; exit 1)
@prove --exec '' t/*.t
clean:
-rm t/*.t \
t/localtime_test \
t/gmtime_test \
t/*_test.out.bz2 \
t/bench \
*.o
I would like some advice about the following Makefile. It works fine, but it is overly redundant and not harnessing any most of the magic make can do to help a C project.
Its purpose is to test a small ANSI C library. Portability is important.
.PHONY : test
OPTIMIZE = -g
INCLUDE = -I.
CC = gcc
WARNINGS = -Wall -ansi -pedantic -Wno-long-long -Wextra -Wdeclaration-after-statement -Wendif-labels -Wconversion
CFLAGS = $(WARNINGS) $(OPTIMIZE) $(INCLUDE)
COMPILE = $(CC) $(CFLAGS)
LINK = $(COMPILE)
all : time64.o bin/check_max
bin/check_max : time64.o time64_config.h bin/check_max.c
$(LINK) time64.o bin/check_max.c -o $@
time64.o : time64_config.h time64.h time64.c Makefile
t/bench : t/bench.c time64.o
$(LINK) time64.o t/bench.c -o $@
bench : t/bench
time t/bench
t/localtime_test : t/localtime_test.c time64.o
$(LINK) time64.o t/localtime_test.c -o $@
t/gmtime_test : t/gmtime_test.c time64.o
$(LINK) time64.o t/gmtime_test.c -o $@
t/year_limit.t : t/tap.c t/year_limit.t.c time64.o
$(LINK) time64.o t/year_limit.t.c -o $@
t/negative.t : t/tap.c t/negative.t.c time64.o
$(LINK) time64.o t/negative.t.c -o $@
t/overflow.t : t/tap.c t/overflow.t.c time64.o
$(LINK) time64.o t/overflow.t.c -o $@
t/timegm.t : t/tap.c t/timegm.t.c time64.o
$(LINK) time64.o t/timegm.t.c -o $@
t/safe_year.t : t/tap.c t/safe_year.t.c time64.c
$(LINK) t/safe_year.t.c -o $@
t/gmtime64.t : t/tap.c t/gmtime64.t.c time64.o
$(LINK) time64.o t/gmtime64.t.c -o $@
t/mktime64.t : t/tap.c t/mktime64.t.c time64.o
$(LINK) time64.o t/mktime64.t.c -o $@
t/asctime64.t : t/tap.c t/asctime64.t.c time64.o
$(LINK) time64.o t/asctime64.t.c -o $@
t/ctime64.t : t/tap.c t/ctime64.t.c time64.o
$(LINK) time64.o t/ctime64.t.c -o $@
t/seconds_between_years.t : t/tap.c t/seconds_between_years.t.c time64.c
$(LINK) t/seconds_between_years.t.c -o $@
test : tap_tests localtime_tests
localtime_tests: t/localtime_test t/gmtime_test
@which bzdiff > /dev/null || (echo 'You need bzdiff to run these tests'; exit 1)
@which less > /dev/null || (echo 'You need less to run these tests'; exit 1)
@echo "On failure, these tests will produce a diff between the failed and expected results. If they pass they'll be quiet."
TZ=Canada/Eastern t/gmtime_test | bzip2 -9 > t/gmtime_test.out.bz2
bzdiff -u t/gmtime_test.out.bz2 t/gmtime.out.bz2 | less -F
TZ=Canada/Eastern t/localtime_test | bzip2 -9 > t/eastern_test.out.bz2
bzdiff -u t/eastern_test.out.bz2 t/eastern.out.bz2 | less -F
TZ=Australia/West t/localtime_test | bzip2 -9 > t/oz_test.out.bz2
bzdiff -u t/oz_test.out.bz2 t/oztime.out.bz2 | less -F
tap_tests: t/year_limit.t t/negative.t t/overflow.t t/timegm.t t/safe_year.t t/gmtime64.t t/asctime64.t t/ctime64.t
@which prove > /dev/null || (echo 'You need prove (from the Test::Harness perl module) to run these tests'; exit 1)
@prove --exec '' t/*.t
clean:
-rm t/*.t \
t/localtime_test \
t/gmtime_test \
t/*_test.out.bz2 \
t/bench \
*.o
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用隐式规则并且不要重新声明 make 可以自动找到的东西。还简化了顶部的变量,但这是更多的偏好(有几个只是重置默认值)。恕我直言,这可能稍微不太便携,但也不是很明显。它在某些方面也更具可移植性,例如在“gcc”不是默认编译器的系统上。
将 localtime_tests 和 tap_tests 制作成 shell 脚本(但不改变它们的作用),将逻辑移到其他地方,并且是使 makefile 每天可读的主要内容。
您可以利用 GNUmake 特定的功能来简化 更多,如果可移植性的损失是可以接受的。
Use implicit rules and don't redeclare things make can find out automatically. Also simplified the variables at the top, but that's more preference (several merely reset default values). This is possibly slightly less portable, but not significantly so, IMHO. it's also more portable in some ways, e.g. on systems where 'gcc' isn't the default compiler.
Making localtime_tests and tap_tests into shell scripts (but not changing what they do), moves that logic elsewhere and is the main thing that makes the makefile readable daily.
You can make use of GNUmake-specific features to simplify even more, if that loss of portability is acceptable.