User Tools

Site Tools


util:make

make

Things worth knowing about make and Makefiles.

Makefiles

Hints

  • a literal '$' has to be escaped by doubling it:
bla:
        rm $$(echo "-rf /")
  • place a dash ('-') in front of a command to have make ignore it's return value:
bla:
        -rm $$(echo "-rf /")

$(builtins )

  • subst
$(OBJECTS) :
        javac $(subst .class,.java,$@)
  • filter
FILES = Bla.java Blub.java Wrong.here
$(filter %.java,$(FILES)) :
        javac $@
$(filter %.here,$(FILES)) :
        echo "Wrong here?!"

Static Patterns

targets ...: target-pattern: prereq-patterns ...
        commands

Sample usage:

objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
        $(CC) -c $(CFLAGS) $< -o $@

Misc Goodies

* recursing paths:

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

* a more advanced version of the above:

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

Links

util/make.txt · Last modified: 2009/06/07 14:01 by 127.0.0.1