2008年2月9日 星期六

gmake remove intermediate file

通常在 windows 寫簡單的程式時, 我習慣用 nmake, 雖然功能少, 但還堪用, 省得還得另外張羅或設定.
這幾天想用 suffixes 讓 make 自動推論來避免寫重複的 rule, 譬如像這樣(這是簡化過的例子)

$ cat makefile
.SUFFIXES : .c .obj .exe
all: file.exe
.c.obj:
touch $*.obj
.obj.exe:
touch $*.exe

當新增加 file2.c 時, 我只要在 makefile 多加 file2.exe, 剩下的事 make 會自動搞定.

然而事情沒我想像得那麼順利: 如果 file.obj 不存在, nmake 就會叫不知道怎麼生 file.exe. 也就是說, nmake 只能自動推論實際存在檔案的規則. 實在是相當差啊. 之後我原本想一些招數 workaround, 後來受不了乾脆換用 gmake.

雖然 gmake 如預期完成工作. 但它竟然在工作完成後把 file.obj 砍了. 以下是在 FreeBSD 做的實驗:

$ gmake
touch file.obj
touch file.exe
rm file.obj
$ make # bsdmake
touch file.obj
touch file.exe

可以看到 bsdmake 跟 gmake 行為不同. 查 gmake 文件才知道這是 feature, 可以用 .SECONDARY 語法避免砍檔.
intermediate files ...... if make does create b in order to update something else, it deletes b later on after it is no longer needed.
...... You can prevent automatic deletion of an intermediate file by marking it as a secondary file. To do this, list it as a prerequisite of the special target .SECONDARY.

2007年12月17日 星期一

初試 waf

最近在看 build system, 目前比較喜歡的是 waf.

原本偏好的是 scons. 但 scons 只能在 top level build, 不能只在子目錄中 build subtree, 除非在 top level 中用 name 來指定 build 那個 component, 感覺不好用, 我已經習慣現在 recursive make 這種 style 的用法了.
加上當時許多文件都說 scons performance 不好(不知道新版修好沒).

waf 的設計及語法我滿喜歡的. 唯一的問題是 depend on python. 雖然現在這應該不是大問題了.

在 google 及實際使用 waf 之後, 簡短的結論是
  • waf 的開發還很 active, 我發的幾個 bug report 當天就修好了, 從 svn log 來看也算 active
  • 由於很少 project 用, 因此可能有些問題要有人遇到才知道.
  • waf 對各 language/tool 的支援不一定夠成熟/portable, 尤其是非 c/c++ 系列 tool 在 windows 平台可能會是個問題.
  • waf 的 tool 實作採用類似 shell variable expansion 的作法, 因此目錄或檔名有空白會有問題. 短期內看來不會改 (README 說的).
  • 我的建議是可以在以 unix 為主的 c/c++ 小 project 試用看看. 若是複雜 build rule 的 project, 又想超級 portable, 現在用還太早.