Tuesday, September 3, 2019

常用 Makefile 內建函式

 $(patsubst $(SRC)/%.c, $(BIN)/%, $(wildcard $(SRC)/*.c))
Name:     Text FunctionPattern Substitution
Rule:       output := $(patsubst pattern, replaced_with, input_text) 
Result:    From src/a.c src/b.c src/c.c to bin/a bin/b bin/c  .

$(subst ee,EE,feet on the street)
Name:     Text FunctionSubstitution
Rule:       output := $(subst from, to, text)
Result:    fEEt on the strEEt 

$(foreach OP, ProgA ProgB ProgC, good/$(OP))
Name:     Foreach Function
Rule:       output := $(foreach variable, list, text)
Result:    good/ProgA good/ProgB good/ProgC

$(filter-out main1.o main2.o, main1.o foo.o main2.o bar.o)
Name:     Text Function Filtering Out
Rule:       output := $(filter-out pattern, text)
Result:    foo.o bar.o

$(shell ls | wc -l )
Name:     Shell Function
Rule:       output := $(shell command)
Result:    Number of files

$(addprefix $(OBJDIR)/, foo.o bar.o baz.o)
Name:     File Name Function - Add Prefix
Rule:       output := $(addprefix prefix, names...)
Result:    objdir/foo.o objdir/bar.o objdir/baz.o 

$(findstring a, a b c)
Name:     Text Function - Find String
Rule:       output := $(findstring find, in). If it is found, output = find, else, output = empty
Result:     a

$(strip    a b c  )
Name:     Text Function - Strip (Remove leading and trailing whitespaces)
Rule:       output := $(strip string)
Result:    "a b c"

$(notdir src/foo.c bar.c)
Name:     File Name Function - Not Directory (basename)
Rule:       output := $(notdir names...)
Result:    foo.c bar.c

$(call reverse, a b)      #given that reverse = $(2) $(1)
Name:          Call Function
Behavior:     Call defined variable with parameters
Rule:            output := $(call variable, param, param...)
Result:         b a

$(eval $(call LINK_TARGET, $(BIN)/$(_bin)))     #given that LINK_TARGET is a variable defined earlier
Name:           Eval Function
Behavior:      Create a new makefile environment with the argument passed into eval, then parse it just like makefile syntax. 
Rule:             output(empty string) := $(eval argument)

No comments:

Post a Comment