What does a percent symbol do in a makefile?

后端 未结 1 559
终归单人心
终归单人心 2020-12-25 09:57

I have a makefile that looks like this :

include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))

%-test: 
       Something here

I unde

相关标签:
1条回答
  • 2020-12-25 10:25

    As you can read in the GNU make manual, the percent acts as a wildcard. The first argument of the patsubst function forms the pattern. Each item/word in the last argument is compared against this pattern, and if it matches, it is replaced with the second argument. If there is a wildcard symbol (%) in the pattern, this will match any number of characters, and these characters are copied into the replacement string at the place of the % in the second argument.

    In your example the pattern is just the wildcard symbol, so it will match any word in the last argument to the function, and this word will be copied into the replacement string (the second argument) at the place of the %.

    An example may make things more clear. Let's assume TEST_SUBDIRS contains two names.

    TEST_SUBDIRS := test1 test2
    include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))
    

    This is then equivalent to the following.

    include $(src)/test1/Make.tests $(src)/test2/Make.tests
    

    A makefile is processed sequentially, line by line. Variable assignments are "internalized", and include statements cause the contents of other files to be inserted literally at that location after which that content is processed as part of the makefile.
    A dependency graph is formed from the rules as they are being read in, and after the entire file is processed, the necessary recipes are executed to update the requested target.

    0 讨论(0)
提交回复
热议问题