Functions commonly used in makefile

Time:2021-12-29

Makefile defines a series of rules to specify which source files in the project need to be compiled first and whichsourceFile post compilation, whichsourceFiles need to be recompiled or even more complex operations.Once written, only one make command is needed, and the whole project automatically compiles according to the rules written in Makefile, which greatly improves the efficiency of software development. The written makefile file is parsed by the make interpreter. In addition to parsing the Compilation Rules in the makefile file, the make parser also provides some functions with specific functions, which can be directly used in writing makefile. Mastering some of the commonly used functions will help us better write makefile to manage our project.

1、 Function call syntax

Before formally introducing the detailed functions of these functions, let’s take a look at the function call syntax in makefile. In fact, the function call is similar to the use of variables. It is also identified by the “$” symbol. Its syntax is as follows:

$( )

Or

${ }

amongfunctionRepresents the name of the function,argumentsRepresents a function parameter. Between the function name and the parameter is“Space”Separated, a function may have multiple parameters, and different parameters are separated by commas.

Let’s take an example to see how the function is called

DIR = D:\\MinGW\\mingw64\\bin
DIR_N = $(subst \\,/,$(DIR))

The second statement above $(subst \, /, $(DIR)) is a call to a function. Subst is the function name followed by ‘\’, ‘/’‘$(DIR)’Represents the three parameters passed to the function, and $(DIR) represents the use of the dir variable. Well, we have briefly introduced the function call format,As for the function of subst, we will explain it in detail later.

2、 Introduction to common functions

According to the function division, the functions in makefile can be divided into different types. Next, the common functions in makefile are introduced according to the function classification

1. String handler

  • Subst function

Subst is a string replacement function. The format of the function is as follows:

$(subst ,,)

Is the replaced string,Is a replacement string,Is the string of the replacement operation. The result returned by the function is the string after the replacement is completed.

Example:

DIR = D:\\MinGW\\mingw64\\bin
DIR_N = $(subst \\,/,$(DIR))

all:

  @echo $(DIR)

  @echo $(DIR_N)

Use the subst function to replace ‘\’ with ‘/’ in the string represented by the dir variable, and assign the replacement result to dir_ N variable. After execution, dir_ The content of n variable is “D: / MinGW / mingw64 / bin”

  • Patsubst function

Patsubst is also a string replacement function. Unlike the subst function, it is matched and replaced by a pattern. The format of the function is as follows:

$(patsubst ,,)

The patsubst function looks up whether the words separated by spaces in the string conform to the pattern part. If they match, it will replace them with the specified content.Is the replaced mode,Is an alternative form,Is the string of the replacement operation. The result returned by the function is the string after the replacement is completed.

Example:

SRC_FILE = main.c bsp_led.c bsp_uart.c
OBJ_FILE = $(patsubst %.c, %.o, $(SRC_FILE))

all:

  @echo $(SRC_FILE)

  @echo $(OBJ_FILE)

The wildcard “%” indicates a string of arbitrary length$ (patsubst%. C,%. O, $(src_file)) SRC_ All in file are in Replace the word with suffix C O is the suffix. After execution, obj_ The contents of the file variable are“main.o bsp_led.o bsp_uart.o

(Note:% is escaped by default. If you want to match the ‘%’ character in the string, you can use “\” to escape and “\%” to represent the real ‘% “character)

  • Strip function

Strip is a function to remove spaces. The format of the function is as follows:

$(strip )

The strip function removes the spaces at the beginning and end of the string, and combines multiple consecutive spaces into one space.Represents the string to be manipulated. The return value of the function is the string without spaces

Example:

obj_space = Hello     World      !
obj = $(strip $(obj_space))
all:

  @echo $(obj_space)

  @echo $(obj)

Obj_ Multiple consecutive spaces in the space variable are merged into one space, and the result is assigned to the obj variable, whose content is “Hello world!”

  • Findstring function

Findstring is a search string function. The format of the function is as follows:

$(findstring ,)

The findstring function has two parameters,Represents the target string,Represents the source string. If the target string is found in the source string, the target string is returned; otherwise, null is returned

Example:

src_obj = Hello Makefile
find_obj = $(findstring Makefile, $(src_obj))
all:
    @echo $(find_obj)

src_ There is a “makefile” string in the obj variable, so the returned result is “makefile”, find_ The content of obj variable is “makefile”

  • Filter function

The filter function is a filter function. The format of the function is as follows:

$(filter ,)

The filter function has two parameters,The filtering mode is passed in,The source string is passed in. The function filters out the string in text that conforms to the pattern (there can be multiple patterns). The return value of the function is the filtered string.

Example:

SRC_FILE = main.c led.c readme.txt start.S
OBJ_FILE = $(filter %.c %.S, $(SRC_FILE))
all:
    @echo $(OBJ_FILE)

From SRC_ Filter out all files in the file variable C documents and all S file, the filtered result is assigned to the variable obj_ FILE。 SRC_ In the file variable C file has main c 、led. c,. S file has start S. So use the filter function to filter the obj_ File variable content “main. C led. C start. S”

(Note: there is another function opposite to the filter function – filter out. The parameters of the two functions are the same, and filter out is filtered outMedium coincidenceAnd return the rest. For example, in the above example, filter is replaced by filter out, and the execution result is “readme. TXT”)

  • Words function

The words function is a function that passes through the number of words. The format of the function is as follows:

$(words )

The words function has an argument,The source string is passed in, and the function returnsThe number of words in the incoming source string

Example:

OBJS = apple orange banana

all: @echo $(words $(OBJS))

Count the number of words in objs variable. There are three words in objs, and the return result is 3

2. File name operation function

  • Wildcard function

The wildcard function obtains the file name satisfying the matching pattern. The format of the function is as follows:

$(wildcard PATTERN)

The wildcard function lists all file names in the current directory that conform to the pattern format (there can be multiple patterns). The return value is all file names in the current folder that conform to the pattern pattern. The file names in the returned string are separated by spaces.

Example:

OBJS = $(wildcard %.c)
all:
    @echo $(OBJS)
  • Dir function

Dir function is a function to obtain directory. The format is as follows:

$(dir )

The dir function retrieves a sequence from a file nameTake out the directory section, ifThere is no ‘/’ in the. The retrieved value is’. / (current directory). The return value is the directory part, which refers to the part before the last backslash. If there is no backslash, a ‘. /’ will be returned.

Example:

OBJS = driver/led.c mmt

all:

  @echo $(dir $(OBJS))

Take out the directory part of objs variable content, and the execution result is “driver /. /”

  • Notdir function

Notdir function and dir function have the opposite functions. The format of the function is as follows:

$(notdir )

The function of notdir is to start a sequence from a file nameTake out the non directory part of the. The non directory part is the part after the last backslash. The return value is the non directory part of the file.

Example:

OBJS = driver/led.c mmt
all:
  @echo $(notdir $(OBJS))

Take out the non directory part of the objs variable content, and the execution result is “led. C NMT”

  • Suffix function

Get the suffix function. The format of the function is as follows:

$(suffix )

The suffix function retrieves a sequence from a file nameTake out the suffix name of each file. The return value is the file name sequenceIf the file does not have a suffix name, an empty string is returned.

Example:

OBJS = driver/led.c mmt
all:
  @echo $(suffix $(OBJS))

Take out the file suffixes in the objs variable content, and the execution result is “. C”

  • Basename function

Get the prefix function. The format of the function is as follows: 

$(basename )

The basename function is a sequence from file namesTake out the prefix part of each file name. The return value is the prefix name of the extracted file. If the file has no prefix name, an empty string is returned.

Example:

OBJS = driver/led.c mmt
all:
  @echo $(basename $(OBJS))

Take out the file prefix names in the objs variable content, and the execution result is “driver / LED MMT”

  • Addsuffix function

Add a suffix function. The format of the function is as follows:

$(addsuffix ,)

The addsuffix function is the suffixAdd toAfter each word in. The return value is the file name sequence with the suffix added.

Example:

OBJS = driver/led mmt
all:
  @echo $(addsuffix .c, $(OBJS))

Add a suffix to each word in the objs variable content, and the execution result is “driver / LED. C MMT. C”

  • addprefix

Add a prefix function. The format of the function is as follows:

$(addperfix ,)

The addprefix function adds the prefix prefix to each word in names. The return value is the sequence of file names prefixed with.

Example:

OBJS = led.c uart
all:
  @echo $(addprefix driver/, $(OBJS))

Add the prefix driver / to each word in the objs variable content, and the execution result is “driver / LED. C, driver / UART”

3. Loop, select function

  • Foreach function

Loop function. The format of the function is as follows:

$(foreach ,,)

Put parametersTake out the words in one by one and put them into the parameterIn the specified variable, and then in executionThe contained expression. every timeA string will be returned. During the loop,Each returned string is separated by spaces. Finally, when the whole loop ends,The entire string (separated by spaces) of each returned string will be the return value of the foreach function.

Example:

src = $(wildcard *.c)

objs:=$(foreach var, $(basename $(src)), $(var).o)
all:
    @echo $(objs)

First, use the wildcard function to get all the files in the current folder C file name, and then call the foreach function to retain each string in the existing file name. C replace the word with o

  • If function

Select a structure function. The format of the function is as follows:

$(if ,)Or (if),,)

The if function may or may not contain the else part. That is, the parameters of the if function can be two or three. IfIf part of the returned string is a non empty string, the if condition holds,Partially executed, otherwiseExecuted.

Example:

OBJ = app.c
OBJ = $(if $(OBJ),$(OBJ),main.c)
all:
      @echo $(OBJ)

The obj variable is a non empty string. If the condition is true, the content of the obj variable is returned. The obj variable is returned, and the obj variable is still app c

4. Other

  • Origin function

The origin function is different from other functions. It does not operate on the value of the variable, but tells us the information of the variable, especially where the variable comes from. The format of the function is as follows:

$(origin )

Return results

1) If the variable is undefined, the origin function returns “undefined”

2) If the variable is an environment variable, the origin function returns “environment”

3) If the variable is defined in the makefile file, the origin function returns “file”

4) If the variable is passed in from the command line, the origin function returns “command line”

5) If the variable is redefined by override, then “override” is returned

6) If the variable is an automation variable, “automatic” is returned

7) If the variable is the default variable, “default” is returned

Example:

In the makefile of uboot, use origin to judge whether the V variable is passed in through the command line. For example, execute make v = XXX. At this time, ifeq is established, and the following statement is executed

ifeq ("$(origin V)", "command line")
  KBUILD_VERBOSE = $(V)
endif

3、 Summary

Here are some functions commonly used in makefile. Examples show how these functions are used. There are some other functions in makefile for us to use. We will supplement the content of this article when we contact later!