Function in Makefile

4138 단어 Makefilemakefile

Function in Makefile


Call function

$( )  or ${ }
example1:
  $(subst a, b, $(x))
example2:
  comma := ,
  empty := 
  space := $(empty)$(empty)
  foo := a b c
  bar := $(subst $(space), $(comma), $(foo))

In example2, the program will change foo from “a b c” to “a,b,c”.

String Function


function name
functon
prototype
subst
Replace with in
$(subst ,,)
patsubst
Replace with
$(patst ,,)
strip
Delete space character at the begin or end of
$(strip )
findstring
Find in
$(findstring ,)
filter
Return words followed the in
$(filter ,)
filter-out
Return words not followed the\pattern> in
$(filter-out ,>)
sort
Sort the words in according to the ascending
$(sort )
word
Get the word from by . is ordinal number.
$(word ,)
wordlist
Take the multiple words out of from to
$(wordlist ,,)
words
Return the quantity of word in
$(word )
firstword
Return the firt word in
$(firtword )
example :
  override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
  # VPATH := src:../headers
  ret_val : -Isrc -I../headers

File Name Manipulation Function


function name
function
prototype
dir
Return the directory string. If no directory, return ./
$(dir )
notdir
Return the file name without the directory string
$(notdir )
suffix
Return the suffix in
$(suffix )
basename
Return the prefix in
$(basename )
addsuffix
Append the postfix to
$(addsuffix , )
addprefix
Add a prefix before every word in $(addprefix , ) join Concatenated strings in and according to the ordinal number $(join ,)

Others Function

foreach

foreach function is similar to for in Unix.The prototype is

$(foreach ,,) 

Each time, var take a parameter from , then var will be processed by the regulation defined in . The return string will be split by . For example:
names := a b c d
files := $(foreach n,$(names),$(n).o)
# $(files) := a.o b.o c.o d.o

if


The prototype of function if :
$(if ,,)

If is true, return value is . If not, return .

call

$(call ,,,...,)

example :
# Makefile file
reverse = $(1) $(2)
foo = $(call reverse,a,b)
tryCall:
  @echo $(foo)

origin

$(origin )

is the name of variable. Using the ‘$’ is not suitable here. Origin will return different value for different type of variale.
return value
means
undefine
has never be defined!
default
is a default variable.
environment
is a environment varible.
file
was defined in a Makefile file.
command file
was defined by command line.
override
was redefined by overide command.
automatic
is a autimatic variable.

shell


Using the shell command and return what the shell command return.
contents := $(shell cat foo)
files := $(shell echo *.c)

error

$(error )

Print the error info and exit.
$(warning )

Print the warning info and comtinues execution.

Reference


How to write makefile Chen Hao

좋은 웹페이지 즐겨찾기