User Tools

Site Tools


util:make

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

util:make [2009/06/07 14:01] (current)
Line 1: Line 1:
 +====== make ======
 +Things worth knowing about ''​make''​ and ''​Makefiles''​.
 +
 +====== Makefiles ======
 +
 +===== Hints =====
 +  * a literal '''​$'''​ has to be escaped by doubling it:
 +<code make>
 +bla:
 +        rm $$(echo "-rf /")
 +</​code>​
 +  * place a dash ('''​-'''​) in front of a command to have ''​make''​ ignore it's return value:
 +<code make>
 +bla:
 +        -rm $$(echo "-rf /")
 +</​code>​
 +
 +===== $(builtins ) =====
 +  * subst 
 +<code make>
 +$(OBJECTS) :
 +        javac $(subst .class,​.java,​$@)
 +</​code>​
 +  * filter
 +<code make>
 +FILES = Bla.java Blub.java Wrong.here
 +$(filter %.java,​$(FILES)) :
 +        javac $@
 +$(filter %.here,​$(FILES)) :
 +        echo "Wrong here?​!"​
 +</​code>​
 +
 +===== Static Patterns =====
 +<​file>​
 +targets ...: target-pattern:​ prereq-patterns ...
 +        commands
 +</​file>​
 +Sample usage:
 +<code make>
 +objects = foo.o bar.o
 +all: $(objects)
 +$(objects): %.o: %.c
 +        $(CC) -c $(CFLAGS) $< -o $@
 +</​code>​
 +
 +===== Misc Goodies =====
 +
 + * recursing paths:
 +<code make>
 +PATH=/​a/​b/​c/​d/​e/​f/​g/​h
 +
 +PLIST:=
 +define recurse
 +ifneq ($(strip $(shell dirname ${1})),/)
 +$$(eval $$(call recurse,​$(shell dirname ${1})))
 +PLIST+= $(shell dirname ${1})
 +endif
 +endef
 +
 +all: $(eval $(call recurse,​${PATH}))
 + @for i in ${PLIST}; do echo $$i; done
 +</​code>​
 +
 + * a more advanced version of the above:
 +<code make>
 +PATH=/​a/​b/​c/​d/​e/​f/​g/​h
 +ifneq (${REVERSE},​)
 +REVERSE:​=true
 +endif
 +REVERSE?​=false
 +
 +PLIST:=
 +define get_dirname
 +$(strip $(shell dirname ${1}))
 +endef
 +
 +define list_add
 +ifneq (${2},${3})
 +PLIST += $$(call get_dirname,​${1})
 +endif
 +endef
 +
 +define recurse
 +DNAME:​=$$(call get_dirname,​${1})
 +ifneq ($${DNAME},/​)
 +$$(eval $$(call list_add,​${1},​$${REVERSE},​true))
 +$$(eval $$(call recurse,​$${DNAME}))
 +$$(eval $$(call list_add,​${1},​$${REVERSE},​false))
 +endif
 +endef
 +
 +all: $(eval $(call recurse, ${PATH}))
 + @for i in ${PLIST}; do echo $$i; done
 +</​code>​
 +
 +====== Links ======
 +[[http://​www.gnu.org/​software/​make/​manual/​html_node/​index.html|GNU Make Manual]]
  
util/make.txt · Last modified: 2009/06/07 14:01 (external edit)