Subacme

Personal weblog. Nonpersonal topics.

Counting Build Number with Makefile

尚无评论

If you prefer writing code without an IDE, Makefile is something you probably can’t avoid.  With plain make, however, automatic build number counting can be tricky to implement.

Most of the existing solutions (to my knowledge) rely on ad hoc scripts that are neither elegant nor portable.  Here I’ll give my solution that relies solely on echo and grep (and, of course, GNU make), virtually available on all platforms.

The major obstacle to counting numbers with make is the absence of numerical operations.  While a number is usually represented with a numeral, it can also be encoded with repeated usage of a constant symbol—for example, two apples carry the information ‘2-ness’.

Therefore, the idea is to make make do the simplest it can do—adding a symbol to a counter file each time—and use grep to count the number of appearance of that symbol, i.e. the build number.

Below is a complete Makefile for you to try out.  It uses the file .BuildCounter to store the build number, and writes the string BuildNumber = N; to the file BuildCount.inc, where N is the build count.

__COUNTER_FILE__ = .BuildCounter
__COUNTER_INC_FILE__ = BuildCount.inc
ifeq ($(wildcard $(__COUNTER_FILE__)),)
$(shell echo > $(__COUNTER_FILE__))
endif
$(shell echo $$ >> $(__COUNTER_FILE__))
$(shell echo BuildNumber = $(shell grep -c -F $$ $(__COUNTER_FILE__))\; > $(__COUNTER_INC_FILE__))

default:
        @more $(__COUNTER_INC_FILE__)

Click here to download this file.

VN:F [1.9.13_1145]
Rating: 4.0/5 (1 vote cast)
Counting Build Number with Makefile, 4.0 out of 5 based on 1 rating

本文的作者为 Jetcheng Chu

发表於 2010 年 8 月 12 日 0:35

分类: 未分类

标签: ,

赞助商链接

您的支持让您喜欢的网站更好地发展。
页面右侧尚有更多赞助商链接。如果您感兴趣, 请别犹豫单击它们。

发表回复

*