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.

The Counting Build Number with Makefile by Subacme, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
赞助商链接
您的支持让您喜欢的网站更好地发展。
页面右侧尚有更多赞助商链接。如果您感兴趣, 请别犹豫单击它们。
在 Twitter 上跟踪 Subacme





