2010-02-06 30 views
0

我想要關於下面的Makefile的一些建議。它工作正常,但它過於冗餘,並沒有利用任何大部分的魔力可以幫助C項目。這個Makefile如何變得更好/更容易/更少冗餘?

其目的是測試一個小的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 [email protected] 

time64.o : time64_config.h time64.h time64.c Makefile 

t/bench : t/bench.c time64.o 
    $(LINK) time64.o t/bench.c -o [email protected] 

bench : t/bench 
    time t/bench 

t/localtime_test : t/localtime_test.c time64.o 
    $(LINK) time64.o t/localtime_test.c -o [email protected] 

t/gmtime_test : t/gmtime_test.c time64.o 
    $(LINK) time64.o t/gmtime_test.c -o [email protected] 

t/year_limit.t : t/tap.c t/year_limit.t.c time64.o 
    $(LINK) time64.o t/year_limit.t.c -o [email protected] 

t/negative.t : t/tap.c t/negative.t.c time64.o 
    $(LINK) time64.o t/negative.t.c -o [email protected] 

t/overflow.t : t/tap.c t/overflow.t.c time64.o 
    $(LINK) time64.o t/overflow.t.c -o [email protected] 

t/timegm.t : t/tap.c t/timegm.t.c time64.o 
    $(LINK) time64.o t/timegm.t.c -o [email protected] 

t/safe_year.t : t/tap.c t/safe_year.t.c time64.c 
    $(LINK) t/safe_year.t.c -o [email protected] 

t/gmtime64.t : t/tap.c t/gmtime64.t.c time64.o 
    $(LINK) time64.o t/gmtime64.t.c -o [email protected] 

t/mktime64.t : t/tap.c t/mktime64.t.c time64.o 
    $(LINK) time64.o t/mktime64.t.c -o [email protected] 

t/asctime64.t : t/tap.c t/asctime64.t.c time64.o 
    $(LINK) time64.o t/asctime64.t.c -o [email protected] 

t/ctime64.t : t/tap.c t/ctime64.t.c time64.o 
    $(LINK) time64.o t/ctime64.t.c -o [email protected] 

t/seconds_between_years.t : t/tap.c t/seconds_between_years.t.c time64.c 
    $(LINK) t/seconds_between_years.t.c -o [email protected] 

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 

You can see it in situ here

回答

3

使用隱式規則,不要重新聲明事情使自動找出。還簡化了頂部的變量,但更偏好(幾個僅僅重置默認值)。這可能稍微不便攜,但不是那麼重要,恕我直言。它在某些方面也更具可移植性,例如在'gcc'不是默認編譯器的系統上。

使localtime_tests和tap_tests進入shell腳本(但不會改變它們的作用),將該邏輯移動到其他地方並且是使makefile每天可讀的主要事件。

 
CFLAGS = -g -Wall -ansi -pedantic -Wno-long-long -Wextra \ 
-Wdeclaration-after-statement -Wendif-labels -Wconversion 

all : bin/check_max 

bin/check_max : time64.o time64_config.h 
time64.o : time64_config.h time64.h Makefile 

bench : t/bench 
    time t/bench 

t/bench : t/bench.c time64.o 
t/localtime_test : time64.o 
t/gmtime_test : time64.o 

t/year_limit.t: t/tap.c time64.o 
t/negative.t : t/tap.c time64.o 
t/overflow.t : t/tap.c time64.o 
t/timegm.t : t/tap.c time64.o 
t/safe_year.t : t/tap.c time64.c 
t/gmtime64.t : t/tap.c time64.o 
t/mktime64.t : t/tap.c time64.o 
t/asctime64.t : t/tap.c time64.o 
t/ctime64.t : t/tap.c time64.o 
t/seconds_between_years.t: t/tap.c time64.c 

test : tap_tests localtime_tests 
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 
    ./tap_tests 
localtime_tests: t/localtime_test t/gmtime_test 
    ./localtime_tests 
.PHONY : test tap_tests localtime_tests 

clean: 
    -rm t/*.t t/localtime_test t/gmtime_test t/*_test.out.bz2 t/bench 
    -rm *.o 
.PHONY : clean 

您可以使用特定gnumake的,功能simplify甚至more,如果便攜性的損失是可以接受的。

+0

謝謝!我採納了大部分建議,這也迫使我在測試代碼中清除一些難看的位。我離開了CFLAGS,因爲我想更改生產的優化標誌,但只留下警告標誌。稍後我會將localtime_tests規範爲一個Perl腳本。 – Schwern 2010-02-07 09:33:02