Embedded Functions in Makefile

Embedded Functions in Makefile

subst String Replacement Function

$(subst <from>, <to>, <text>) replaces the <from> string in <text> with <to> and returns the replaced string.

SRC:=/mnt/hgfs/share/123

DES:=$(subst /mnt,/root,$(SRC)) #ATTENTION: is, /root not, /root

all:
    @echo  $(SRC)       
    @echo  $(DES)
/mnt/hgfs/share/123
/root/hgfs/share/123

patsubst Pattern String Replacement Function

$(patsubst <pattern>, <replacement>, <text>) checks if the words in <text> (separated by spaces, tabs, or newlines) match the pattern <pattern>. If they do, they are replaced with <replacement>. The <pattern> can include the wildcard %, which represents a string of any length. If <replacement> also contains %, it will represent the string corresponding to <pattern>‘s %.

SRC:=/mnt/hgfs/share/123

DES:=$(patsubst %/123,%/456,$(SRC))
all:
    @echo  $(SRC)                           #Note: shell may not support @
    @echo  $(DES)
/mnt/hgfs/share/123
/mnt/hgfs/share/456

strip

$(strip a b c) removes leading and trailing spaces from the string, resulting in “a b c”.

DES:=$(strip       12,43  )         #The parameter string starts from 1, can remove the last space

DES:=$(strip "       12,43  ")      #The parameter string starts from space, can remove the last space

all:
    @echo  $(DES)
/mnt/hgfs/share/123
 12,43

findstring String Search Function

$(findstring <find>, <in>) searches for <find> in the string <in>. If found, it returns <find>; otherwise, it returns empty.

SRC:=/mnt/hgfs/share/123

DES:=$(findstring /mnt,$(SRC))
all:
    @echo  $(SRC)   
    @echo  $(DES)
/mnt/hgfs/share/123
/mnt

filter Filtering Function

$(filter <pattern…>, <text>) filters the words in <text> based on the <pattern>, retaining the words that match the <pattern. Multiple <pattern> can be used.

SRC:= 123.c 789.c 456.s eee.j
#DES:=$(filter %.c,%.s,$(SRC))          #WRONG!!!
DES:=$(filter %.c %.s,$(SRC))

all:
    @echo  $(SRC)                   
    @echo  $(DES)
123.c 789.c 456.s eee.j
123.c 789.c 456.s

filter-out Anti-Filtering Function

$(filter-out <pattern…> ,<text>) filters out the words in <text> that match the <pattern>, removing them. Multiple <pattern> can be used.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(filter-out %.c,$(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
456.s eee.j

sort Sorting Function

$(sort <list>) sorts the words in the string <list> in ascending order.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(sort $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
123.c 456.s 789.c eee.j

word Word Extraction Function

$(word <n>,<text>) extracts the <n>th word from the string <text>. If n is greater than the number of words in <text>, it returns an empty string.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(word 2,$(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
789.c

wordlist Word List Extraction Function

$(wordlist <s>, <e>, <text>) extracts the word list from <text> starting from <s> to <e>. If s is greater than <text>, it returns empty. If e is greater than <text>, it returns the substring from s to the end of <text>.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(wordlist 2,4,$(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
789.c 456.s eee.j

words Word Count Function

$(words <text>) returns the number of words.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(words $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
4

firstword First Word Function

$(firstword <text>) returns the first word of <text>.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(firstword $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
123.c

dir Directory Extraction Function

$(dir <names…>) extracts the directory part from each name and returns the directory where the file is located. If there is no /, it returns ./.

SRC:=/mnt/hgfs/share/123

DES:=$(dir $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
/mnt/hgfs/share/123
/mnt/hgfs/share/

notdir File Extraction Function

$(notdir <names…>) extracts the file part from each name and returns the filename. If there is no file, it returns empty. Here, the name must be a variable name, not the content of the variable; otherwise, nothing will be returned.

SRC:=/mnt/hgfs/share/123

DES:=$(notdir $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
/mnt/hgfs/share/123123

suffix Suffix Extraction Function

$(suffix <names…>) extracts the suffix from a sequence of filenames and returns the suffix. If there is no suffix, it returns empty.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(suffix $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
.c .c .s .j

basename Prefix Extraction Function

$(basename <names…>) extracts the prefix from a sequence of filenames and returns the prefix. If there is no prefix, it returns empty.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(basename $(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j123 789 456 eee

addsuffix

$(addsuffix <suffix>, <names…>) adds a suffix to each word in <names> and returns the sequence of filenames with the added suffix.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(addsuffix _suf,$(SRC))all:
    @echo  $(SRC)       
    @echo  $(DES)123.c 789.c 456.s eee.j
123.c_suf 789.c_suf 456.s_suf eee.j_suf

addprefix

$(addprefix <prefix>, <names…>) adds a prefix to each word in <names> and returns the sequence of filenames with the added prefix.

SRC:= 123.c 789.c 456.s eee.j
DES:=$(addprefix prf_,$(SRC))
all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
prf_123.c prf_789.c prf_456.s prf_eee.j

join

$(join <list1> , <list2>) connects the words in <list2> to the corresponding words in <list1>. If <list1> has more words, the extra words remain as they are. If <list2> has more words, the extra words are copied to <list2>.

SRC:= 123.c 789.c 456.s eee.j
SRCJOIN:= q e r t y u
DES:=$(join $(SRC),$(SRCJOIN))all:
    @echo  $(SRC)       
    @echo  $(DES)
123.c 789.c 456.s eee.j
123.cq 789.ce 456.sr eee.jt y u

foreach

$(foreach <var>, <list>, <text>) takes each word from <list> and assigns it to the variable <var>, then executes the expression contained in <text>. The strings returned by <text> will be separated by spaces. At the end of the loop, the entire string returned by <text> will be the return value of the foreach function.

if

$(if <condition>, <then-part>)$(if <condition>, <then-part>,<else-part>) where <condition> returns true if non-zero.

call Create Function Function

$(call <expression>, <param1>, <param2>, <param3>…)

origin Determine Variable Origin

$(origin <variable>) returns values such as “undefined”, “default”, “environment”, “file”, “command line”, “override”, “automatic”.

shell

$(shell <shell-command>) or $(<shell-command>) where the parameter is a shell command.

error

$(error <text…>) is an error generation function, where <text…> is the error message.

Embedded Linux Chinese Site

The most professional Chinese Embedded Linux website, 8 years of sharpening, with tens of thousands of registered users!

Sharing Embedded & Linux Technical dry goods, tutorials, information, and high-paying positions

SubscribeClick the “Embedded Linux Chinese Site” below the title

ShareClick the button in the upper right corner

Submit[email protected]

Embedded Functions in Makefile

Click below “Read the original text” for more
Embedded Functions in Makefile

Leave a Comment