Saturday, May 23, 2009 / Make, Ubuntu

GNU Makeでの if 関数による 実行制御

Makeでは、条件に応じて処理を変えられることが判明。 以下の例では、カレントディレクトリに index.html が 存在している場合は、proc-a マクロを実行、そうでない場合は proc-b マクロを実行します。

実行制御する Makefile

indexhtml := $(shell ls | egrep "^index.html$$")

define proc-a
	@echo "index.html found."
endef 

define proc-b
	@echo "index.html not found."
endef 


test :
	$(if $(indexhtml) ,$(proc-a),$(proc-b))

index.html がない場合

$ make
index.html not found.

index.html がある場合

$ make
index.html found.