1 This is make.info, produced by makeinfo version 4.2 from make.texi.
3 INFO-DIR-SECTION GNU Packages
5 * Make: (make). Remake files automatically.
8 This file documents the GNU Make utility, which determines
9 automatically which pieces of a large program need to be recompiled,
10 and issues the commands to recompile them.
12 This is Edition 0.60, last updated 08 July 2002, of `The GNU Make
13 Manual', for `make', Version 3.80.
15 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
16 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.1 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
22 Texts. A copy of the license is included in the section entitled "GNU
23 Free Documentation License".
26 File: make.info, Node: Syntax of Functions, Next: Text Functions, Prev: Functions, Up: Functions
31 A function call resembles a variable reference. It looks like this:
39 Here FUNCTION is a function name; one of a short list of names that
40 are part of `make'. You can also essentially create your own functions
41 by using the `call' builtin function.
43 The ARGUMENTS are the arguments of the function. They are separated
44 from the function name by one or more spaces or tabs, and if there is
45 more than one argument, then they are separated by commas. Such
46 whitespace and commas are not part of an argument's value. The
47 delimiters which you use to surround the function call, whether
48 parentheses or braces, can appear in an argument only in matching pairs;
49 the other kind of delimiters may appear singly. If the arguments
50 themselves contain other function calls or variable references, it is
51 wisest to use the same kind of delimiters for all the references; write
52 `$(subst a,b,$(x))', not `$(subst a,b,${x})'. This is because it is
53 clearer, and because only one type of delimiter is matched to find the
56 The text written for each argument is processed by substitution of
57 variables and function calls to produce the argument value, which is
58 the text on which the function acts. The substitution is done in the
59 order in which the arguments appear.
61 Commas and unmatched parentheses or braces cannot appear in the text
62 of an argument as written; leading spaces cannot appear in the text of
63 the first argument as written. These characters can be put into the
64 argument value by variable substitution. First define variables
65 `comma' and `space' whose values are isolated comma and space
66 characters, then substitute these variables where such characters are
71 space:= $(empty) $(empty)
73 bar:= $(subst $(space),$(comma),$(foo))
76 Here the `subst' function replaces each space with a comma, through the
77 value of `foo', and substitutes the result.
80 File: make.info, Node: Text Functions, Next: File Name Functions, Prev: Syntax of Functions, Up: Functions
82 Functions for String Substitution and Analysis
83 ==============================================
85 Here are some functions that operate on strings:
87 `$(subst FROM,TO,TEXT)'
88 Performs a textual replacement on the text TEXT: each occurrence
89 of FROM is replaced by TO. The result is substituted for the
90 function call. For example,
92 $(subst ee,EE,feet on the street)
94 substitutes the string `fEEt on the strEEt'.
96 `$(patsubst PATTERN,REPLACEMENT,TEXT)'
97 Finds whitespace-separated words in TEXT that match PATTERN and
98 replaces them with REPLACEMENT. Here PATTERN may contain a `%'
99 which acts as a wildcard, matching any number of any characters
100 within a word. If REPLACEMENT also contains a `%', the `%' is
101 replaced by the text that matched the `%' in PATTERN. Only the
102 first `%' in the PATTERN and REPLACEMENT is treated this way; any
103 subsequent `%' is unchanged.
105 `%' characters in `patsubst' function invocations can be quoted
106 with preceding backslashes (`\'). Backslashes that would
107 otherwise quote `%' characters can be quoted with more backslashes.
108 Backslashes that quote `%' characters or other backslashes are
109 removed from the pattern before it is compared file names or has a
110 stem substituted into it. Backslashes that are not in danger of
111 quoting `%' characters go unmolested. For example, the pattern
112 `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
113 `%' character, and `pattern\\' following it. The final two
114 backslashes are left alone because they cannot affect any `%'
117 Whitespace between words is folded into single space characters;
118 leading and trailing whitespace is discarded.
122 $(patsubst %.c,%.o,x.c.c bar.c)
124 produces the value `x.c.o bar.o'.
126 Substitution references (*note Substitution References:
127 Substitution Refs.) are a simpler way to get the effect of the
130 $(VAR:PATTERN=REPLACEMENT)
134 $(patsubst PATTERN,REPLACEMENT,$(VAR))
136 The second shorthand simplifies one of the most common uses of
137 `patsubst': replacing the suffix at the end of file names.
139 $(VAR:SUFFIX=REPLACEMENT)
143 $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))
145 For example, you might have a list of object files:
147 objects = foo.o bar.o baz.o
149 To get the list of corresponding source files, you could simply
154 instead of using the general form:
156 $(patsubst %.o,%.c,$(objects))
159 Removes leading and trailing whitespace from STRING and replaces
160 each internal sequence of one or more whitespace characters with a
161 single space. Thus, `$(strip a b c )' results in `a b c'.
163 The function `strip' can be very useful when used in conjunction
164 with conditionals. When comparing something with the empty string
165 `' using `ifeq' or `ifneq', you usually want a string of just
166 whitespace to match the empty string (*note Conditionals::).
168 Thus, the following may fail to have the desired results:
171 ifneq "$(needs_made)" ""
174 all:;@echo 'Nothing to make!'
177 Replacing the variable reference `$(needs_made)' with the function
178 call `$(strip $(needs_made))' in the `ifneq' directive would make
181 `$(findstring FIND,IN)'
182 Searches IN for an occurrence of FIND. If it occurs, the value is
183 FIND; otherwise, the value is empty. You can use this function in
184 a conditional to test for the presence of a specific substring in
185 a given string. Thus, the two examples,
187 $(findstring a,a b c)
190 produce the values `a' and `' (the empty string), respectively.
191 *Note Testing Flags::, for a practical application of `findstring'.
193 `$(filter PATTERN...,TEXT)'
194 Returns all whitespace-separated words in TEXT that _do_ match any
195 of the PATTERN words, removing any words that _do not_ match. The
196 patterns are written using `%', just like the patterns used in the
197 `patsubst' function above.
199 The `filter' function can be used to separate out different types
200 of strings (such as file names) in a variable. For example:
202 sources := foo.c bar.c baz.s ugh.h
204 cc $(filter %.c %.s,$(sources)) -o foo
206 says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
207 but only `foo.c', `bar.c' and `baz.s' should be specified in the
208 command to the compiler.
210 `$(filter-out PATTERN...,TEXT)'
211 Returns all whitespace-separated words in TEXT that _do not_ match
212 any of the PATTERN words, removing the words that _do_ match one
213 or more. This is the exact opposite of the `filter' function.
217 objects=main1.o foo.o main2.o bar.o
218 mains=main1.o main2.o
220 the following generates a list which contains all the object files
223 $(filter-out $(mains),$(objects))
226 Sorts the words of LIST in lexical order, removing duplicate
227 words. The output is a list of words separated by single spaces.
232 returns the value `bar foo lose'.
234 Incidentally, since `sort' removes duplicate words, you can use it
235 for this purpose even if you don't care about the sort order.
238 Returns the Nth word of TEXT. The legitimate values of N start
239 from 1. If N is bigger than the number of words in TEXT, the
240 value is empty. For example,
242 $(word 2, foo bar baz)
246 `$(wordlist S,E,TEXT)'
247 Returns the list of words in TEXT starting with word S and ending
248 with word E (inclusive). The legitimate values of S and E start
249 from 1. If S is bigger than the number of words in TEXT, the
250 value is empty. If E is bigger than the number of words in TEXT,
251 words up to the end of TEXT are returned. If S is greater than E,
252 nothing is returned. For example,
254 $(wordlist 2, 3, foo bar baz)
259 Returns the number of words in TEXT. Thus, the last word of TEXT
260 is `$(word $(words TEXT),TEXT)'.
262 `$(firstword NAMES...)'
263 The argument NAMES is regarded as a series of names, separated by
264 whitespace. The value is the first name in the series. The rest
265 of the names are ignored.
271 produces the result `foo'. Although `$(firstword TEXT)' is the
272 same as `$(word 1,TEXT)', the `firstword' function is retained for
275 Here is a realistic example of the use of `subst' and `patsubst'.
276 Suppose that a makefile uses the `VPATH' variable to specify a list of
277 directories that `make' should search for prerequisite files (*note
278 `VPATH' Search Path for All Prerequisites: General Search.). This
279 example shows how to tell the C compiler to search for header files in
280 the same list of directories.
282 The value of `VPATH' is a list of directories separated by colons,
283 such as `src:../headers'. First, the `subst' function is used to
284 change the colons to spaces:
286 $(subst :, ,$(VPATH))
288 This produces `src ../headers'. Then `patsubst' is used to turn each
289 directory name into a `-I' flag. These can be added to the value of
290 the variable `CFLAGS', which is passed automatically to the C compiler,
293 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
295 The effect is to append the text `-Isrc -I../headers' to the previously
296 given value of `CFLAGS'. The `override' directive is used so that the
297 new value is assigned even if the previous value of `CFLAGS' was
298 specified with a command argument (*note The `override' Directive:
299 Override Directive.).
302 File: make.info, Node: File Name Functions, Next: Foreach Function, Prev: Text Functions, Up: Functions
304 Functions for File Names
305 ========================
307 Several of the built-in expansion functions relate specifically to
308 taking apart file names or lists of file names.
310 Each of the following functions performs a specific transformation
311 on a file name. The argument of the function is regarded as a series
312 of file names, separated by whitespace. (Leading and trailing
313 whitespace is ignored.) Each file name in the series is transformed in
314 the same way and the results are concatenated with single spaces
318 Extracts the directory-part of each file name in NAMES. The
319 directory-part of the file name is everything up through (and
320 including) the last slash in it. If the file name contains no
321 slash, the directory part is the string `./'. For example,
323 $(dir src/foo.c hacks)
325 produces the result `src/ ./'.
328 Extracts all but the directory-part of each file name in NAMES.
329 If the file name contains no slash, it is left unchanged.
330 Otherwise, everything through the last slash is removed from it.
332 A file name that ends with a slash becomes an empty string. This
333 is unfortunate, because it means that the result does not always
334 have the same number of whitespace-separated file names as the
335 argument had; but we do not see any other valid alternative.
339 $(notdir src/foo.c hacks)
341 produces the result `foo.c hacks'.
344 Extracts the suffix of each file name in NAMES. If the file name
345 contains a period, the suffix is everything starting with the last
346 period. Otherwise, the suffix is the empty string. This
347 frequently means that the result will be empty when NAMES is not,
348 and if NAMES contains multiple file names, the result may contain
353 $(suffix src/foo.c src-1.0/bar.c hacks)
355 produces the result `.c .c'.
357 `$(basename NAMES...)'
358 Extracts all but the suffix of each file name in NAMES. If the
359 file name contains a period, the basename is everything starting
360 up to (and not including) the last period. Periods in the
361 directory part are ignored. If there is no period, the basename
362 is the entire file name. For example,
364 $(basename src/foo.c src-1.0/bar hacks)
366 produces the result `src/foo src-1.0/bar hacks'.
368 `$(addsuffix SUFFIX,NAMES...)'
369 The argument NAMES is regarded as a series of names, separated by
370 whitespace; SUFFIX is used as a unit. The value of SUFFIX is
371 appended to the end of each individual name and the resulting
372 larger names are concatenated with single spaces between them.
375 $(addsuffix .c,foo bar)
377 produces the result `foo.c bar.c'.
379 `$(addprefix PREFIX,NAMES...)'
380 The argument NAMES is regarded as a series of names, separated by
381 whitespace; PREFIX is used as a unit. The value of PREFIX is
382 prepended to the front of each individual name and the resulting
383 larger names are concatenated with single spaces between them.
386 $(addprefix src/,foo bar)
388 produces the result `src/foo src/bar'.
390 `$(join LIST1,LIST2)'
391 Concatenates the two arguments word by word: the two first words
392 (one from each argument) concatenated form the first word of the
393 result, the two second words form the second word of the result,
394 and so on. So the Nth word of the result comes from the Nth word
395 of each argument. If one argument has more words that the other,
396 the extra words are copied unchanged into the result.
398 For example, `$(join a b,.c .o)' produces `a.c b.o'.
400 Whitespace between the words in the lists is not preserved; it is
401 replaced with a single space.
403 This function can merge the results of the `dir' and `notdir'
404 functions, to produce the original list of files which was given
405 to those two functions.
407 `$(wildcard PATTERN)'
408 The argument PATTERN is a file name pattern, typically containing
409 wildcard characters (as in shell file name patterns). The result
410 of `wildcard' is a space-separated list of the names of existing
411 files that match the pattern. *Note Using Wildcard Characters in
412 File Names: Wildcards.
415 File: make.info, Node: Foreach Function, Next: If Function, Prev: File Name Functions, Up: Functions
417 The `foreach' Function
418 ======================
420 The `foreach' function is very different from other functions. It
421 causes one piece of text to be used repeatedly, each time with a
422 different substitution performed on it. It resembles the `for' command
423 in the shell `sh' and the `foreach' command in the C-shell `csh'.
425 The syntax of the `foreach' function is:
427 $(foreach VAR,LIST,TEXT)
429 The first two arguments, VAR and LIST, are expanded before anything
430 else is done; note that the last argument, TEXT, is *not* expanded at
431 the same time. Then for each word of the expanded value of LIST, the
432 variable named by the expanded value of VAR is set to that word, and
433 TEXT is expanded. Presumably TEXT contains references to that
434 variable, so its expansion will be different each time.
436 The result is that TEXT is expanded as many times as there are
437 whitespace-separated words in LIST. The multiple expansions of TEXT
438 are concatenated, with spaces between them, to make the result of
441 This simple example sets the variable `files' to the list of all
442 files in the directories in the list `dirs':
445 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
447 Here TEXT is `$(wildcard $(dir)/*)'. The first repetition finds the
448 value `a' for `dir', so it produces the same result as `$(wildcard
449 a/*)'; the second repetition produces the result of `$(wildcard b/*)';
450 and the third, that of `$(wildcard c/*)'.
452 This example has the same result (except for setting `dirs') as the
455 files := $(wildcard a/* b/* c/* d/*)
457 When TEXT is complicated, you can improve readability by giving it a
458 name, with an additional variable:
460 find_files = $(wildcard $(dir)/*)
462 files := $(foreach dir,$(dirs),$(find_files))
464 Here we use the variable `find_files' this way. We use plain `=' to
465 define a recursively-expanding variable, so that its value contains an
466 actual function call to be reexpanded under the control of `foreach'; a
467 simply-expanded variable would not do, since `wildcard' would be called
468 only once at the time of defining `find_files'.
470 The `foreach' function has no permanent effect on the variable VAR;
471 its value and flavor after the `foreach' function call are the same as
472 they were beforehand. The other values which are taken from LIST are
473 in effect only temporarily, during the execution of `foreach'. The
474 variable VAR is a simply-expanded variable during the execution of
475 `foreach'. If VAR was undefined before the `foreach' function call, it
476 is undefined after the call. *Note The Two Flavors of Variables:
479 You must take care when using complex variable expressions that
480 result in variable names because many strange things are valid variable
481 names, but are probably not what you intended. For example,
483 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
485 might be useful if the value of `find_files' references the variable
486 whose name is `Esta escrito en espanol!' (es un nombre bastante largo,
487 no?), but it is more likely to be a mistake.
490 File: make.info, Node: If Function, Next: Call Function, Prev: Foreach Function, Up: Functions
495 The `if' function provides support for conditional expansion in a
496 functional context (as opposed to the GNU `make' makefile conditionals
497 such as `ifeq' (*note Syntax of Conditionals: Conditional Syntax.).
499 An `if' function call can contain either two or three arguments:
501 $(if CONDITION,THEN-PART[,ELSE-PART])
503 The first argument, CONDITION, first has all preceding and trailing
504 whitespace stripped, then is expanded. If it expands to any non-empty
505 string, then the condition is considered to be true. If it expands to
506 an empty string, the condition is considered to be false.
508 If the condition is true then the second argument, THEN-PART, is
509 evaluated and this is used as the result of the evaluation of the entire
512 If the condition is false then the third argument, ELSE-PART, is
513 evaluated and this is the result of the `if' function. If there is no
514 third argument, the `if' function evaluates to nothing (the empty
517 Note that only one of the THEN-PART or the ELSE-PART will be
518 evaluated, never both. Thus, either can contain side-effects (such as
519 `shell' function calls, etc.)
522 File: make.info, Node: Call Function, Next: Value Function, Prev: If Function, Up: Functions
527 The `call' function is unique in that it can be used to create new
528 parameterized functions. You can write a complex expression as the
529 value of a variable, then use `call' to expand it with different values.
531 The syntax of the `call' function is:
533 $(call VARIABLE,PARAM,PARAM,...)
535 When `make' expands this function, it assigns each PARAM to
536 temporary variables `$(1)', `$(2)', etc. The variable `$(0)' will
537 contain VARIABLE. There is no maximum number of parameter arguments.
538 There is no minimum, either, but it doesn't make sense to use `call'
541 Then VARIABLE is expanded as a `make' variable in the context of
542 these temporary assignments. Thus, any reference to `$(1)' in the
543 value of VARIABLE will resolve to the first PARAM in the invocation of
546 Note that VARIABLE is the _name_ of a variable, not a _reference_ to
547 that variable. Therefore you would not normally use a `$' or
548 parentheses when writing it. (You can, however, use a variable
549 reference in the name if you want the name not to be a constant.)
551 If VARIABLE is the name of a builtin function, the builtin function
552 is always invoked (even if a `make' variable by that name also exists).
554 The `call' function expands the PARAM arguments before assigning
555 them to temporary variables. This means that VARIABLE values
556 containing references to builtin functions that have special expansion
557 rules, like `foreach' or `if', may not work as you expect.
559 Some examples may make this clearer.
561 This macro simply reverses its arguments:
565 foo = $(call reverse,a,b)
567 Here FOO will contain `b a'.
569 This one is slightly more interesting: it defines a macro to search
570 for the first instance of a program in `PATH':
572 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
574 LS := $(call pathsearch,ls)
576 Now the variable LS contains `/bin/ls' or similar.
578 The `call' function can be nested. Each recursive invocation gets
579 its own local values for `$(1)', etc. that mask the values of
580 higher-level `call'. For example, here is an implementation of a "map"
583 map = $(foreach a,$(2),$(call $(1),$(a)))
585 Now you can MAP a function that normally takes only one argument,
586 such as `origin', to multiple values in one step:
588 o = $(call map,origin,o map MAKE)
590 and end up with O containing something like `file file default'.
592 A final caution: be careful when adding whitespace to the arguments
593 to `call'. As with other functions, any whitespace contained in the
594 second and subsequent arguments is kept; this can cause strange
595 effects. It's generally safest to remove all extraneous whitespace when
596 providing parameters to `call'.
599 File: make.info, Node: Value Function, Next: Eval Function, Prev: Call Function, Up: Functions
604 The `value' function provides a way for you to use the value of a
605 variable _without_ having it expanded. Please note that this does not
606 undo expansions which have already occurred; for example if you create
607 a simply expanded variable its value is expanded during the definition;
608 in that case the `value' function will return the same result as using
609 the variable directly.
611 The syntax of the `value' function is:
615 Note that VARIABLE is the _name_ of a variable; not a _reference_ to
616 that variable. Therefore you would not normally use a `$' or
617 parentheses when writing it. (You can, however, use a variable
618 reference in the name if you want the name not to be a constant.)
620 The result of this function is a string containing the value of
621 VARIABLE, without any expansion occurring. For example, in this
630 The first output line would be `ATH', since the "$P" would be expanded
631 as a `make' variable, while the second output line would be the current
632 value of your `$PATH' environment variable, since the `value' function
633 avoided the expansion.
635 The `value' function is most often used in conjunction with the
636 `eval' function (*note Eval Function::).
639 File: make.info, Node: Eval Function, Next: Origin Function, Prev: Value Function, Up: Functions
644 The `eval' function is very special: it allows you to define new
645 makefile constructs that are not constant; which are the result of
646 evaluating other variables and functions. The argument to the `eval'
647 function is expanded, then the results of that expansion are parsed as
648 makefile syntax. The expanded results can define new `make' variables,
649 targets, implicit or explicit rules, etc.
651 The result of the `eval' function is always the empty string; thus,
652 it can be placed virtually anywhere in a makefile without causing
655 It's important to realize that the `eval' argument is expanded
656 _twice_; first by the `eval' function, then the results of that
657 expansion are expanded again when they are parsed as makefile syntax.
658 This means you may need to provide extra levels of escaping for "$"
659 characters when using `eval'. The `value' function (*note Value
660 Function::) can sometimes be useful in these situations, to circumvent
663 Here is an example of how `eval' can be used; this example combines
664 a number of concepts and other functions. Although it might seem
665 overly complex to use `eval' in this example, rather than just writing
666 out the rules, consider two things: first, the template definition (in
667 `PROGRAM_template') could need to be much more complex than it is here;
668 and second, you might put the complex, "generic" part of this example
669 into another makefile, then include it in all the individual makefiles.
670 Now your individual makefiles are quite straightforward.
672 PROGRAMS = server client
674 server_OBJS = server.o server_priv.o server_access.o
675 server_LIBS = priv protocol
677 client_OBJS = client.o client_api.o client_mem.o
678 client_LIBS = protocol
680 # Everything after this is generic
685 define PROGRAM_template
686 $(1): $$($(1)_OBJ) $$($(1)_LIBS:%=-l%)
687 ALL_OBJS += $$($(1)_OBJS)
690 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
693 $(LINK.o) $^ $(LDLIBS) -o $@
696 rm -f $(ALL_OBJS) $(PROGRAMS)
699 File: make.info, Node: Origin Function, Next: Shell Function, Prev: Eval Function, Up: Functions
701 The `origin' Function
702 =====================
704 The `origin' function is unlike most other functions in that it does
705 not operate on the values of variables; it tells you something _about_
706 a variable. Specifically, it tells you where it came from.
708 The syntax of the `origin' function is:
712 Note that VARIABLE is the _name_ of a variable to inquire about; not
713 a _reference_ to that variable. Therefore you would not normally use a
714 `$' or parentheses when writing it. (You can, however, use a variable
715 reference in the name if you want the name not to be a constant.)
717 The result of this function is a string telling you how the variable
718 VARIABLE was defined:
721 if VARIABLE was never defined.
724 if VARIABLE has a default definition, as is usual with `CC' and so
725 on. *Note Variables Used by Implicit Rules: Implicit Variables.
726 Note that if you have redefined a default variable, the `origin'
727 function will return the origin of the later definition.
730 if VARIABLE was defined as an environment variable and the `-e'
731 option is _not_ turned on (*note Summary of Options: Options
734 `environment override'
735 if VARIABLE was defined as an environment variable and the `-e'
736 option _is_ turned on (*note Summary of Options: Options Summary.).
739 if VARIABLE was defined in a makefile.
742 if VARIABLE was defined on the command line.
745 if VARIABLE was defined with an `override' directive in a makefile
746 (*note The `override' Directive: Override Directive.).
749 if VARIABLE is an automatic variable defined for the execution of
750 the commands for each rule (*note Automatic Variables: Automatic.).
752 This information is primarily useful (other than for your curiosity)
753 to determine if you want to believe the value of a variable. For
754 example, suppose you have a makefile `foo' that includes another
755 makefile `bar'. You want a variable `bletch' to be defined in `bar' if
756 you run the command `make -f bar', even if the environment contains a
757 definition of `bletch'. However, if `foo' defined `bletch' before
758 including `bar', you do not want to override that definition. This
759 could be done by using an `override' directive in `foo', giving that
760 definition precedence over the later definition in `bar';
761 unfortunately, the `override' directive would also override any command
762 line definitions. So, `bar' could include:
765 ifeq "$(origin bletch)" "environment"
766 bletch = barf, gag, etc.
770 If `bletch' has been defined from the environment, this will redefine
773 If you want to override a previous definition of `bletch' if it came
774 from the environment, even under `-e', you could instead write:
776 ifneq "$(findstring environment,$(origin bletch))" ""
777 bletch = barf, gag, etc.
780 Here the redefinition takes place if `$(origin bletch)' returns
781 either `environment' or `environment override'. *Note Functions for
782 String Substitution and Analysis: Text Functions.
785 File: make.info, Node: Shell Function, Next: Make Control Functions, Prev: Origin Function, Up: Functions
790 The `shell' function is unlike any other function except the
791 `wildcard' function (*note The Function `wildcard': Wildcard Function.)
792 in that it communicates with the world outside of `make'.
794 The `shell' function performs the same function that backquotes
795 (``') perform in most shells: it does "command expansion". This means
796 that it takes an argument that is a shell command and returns the
797 output of the command. The only processing `make' does on the result,
798 before substituting it into the surrounding text, is to convert each
799 newline or carriage-return / newline pair to a single space. It also
800 removes the trailing (carriage-return and) newline, if it's the last
803 The commands run by calls to the `shell' function are run when the
804 function calls are expanded (*note How `make' Reads a Makefile: Reading
805 Makefiles.). Because this function involves spawning a new shell, you
806 should carefully consider the performance implications of using the
807 `shell' function within recursively expanded variables vs. simply
808 expanded variables (*note The Two Flavors of Variables: Flavors.).
810 Here are some examples of the use of the `shell' function:
812 contents := $(shell cat foo)
814 sets `contents' to the contents of the file `foo', with a space (rather
815 than a newline) separating each line.
817 files := $(shell echo *.c)
819 sets `files' to the expansion of `*.c'. Unless `make' is using a very
820 strange shell, this has the same result as `$(wildcard *.c)'.
823 File: make.info, Node: Make Control Functions, Prev: Shell Function, Up: Functions
825 Functions That Control Make
826 ===========================
828 These functions control the way make runs. Generally, they are used
829 to provide information to the user of the makefile or to cause make to
830 stop if some sort of environmental error is detected.
833 Generates a fatal error where the message is TEXT. Note that the
834 error is generated whenever this function is evaluated. So, if
835 you put it inside a command script or on the right side of a
836 recursive variable assignment, it won't be evaluated until later.
837 The TEXT will be expanded before the error is generated.
842 $(error error is $(ERROR1))
845 will generate a fatal error during the read of the makefile if the
846 `make' variable `ERROR1' is defined. Or,
848 ERR = $(error found an error!)
853 will generate a fatal error while `make' is running, if the `err'
857 This function works similarly to the `error' function, above,
858 except that `make' doesn't exit. Instead, TEXT is expanded and
859 the resulting message is displayed, but processing of the makefile
862 The result of the expansion of this function is the empty string.
865 File: make.info, Node: Running, Next: Implicit Rules, Prev: Functions, Up: Top
870 A makefile that says how to recompile a program can be used in more
871 than one way. The simplest use is to recompile every file that is out
872 of date. Usually, makefiles are written so that if you run `make' with
873 no arguments, it does just that.
875 But you might want to update only some of the files; you might want
876 to use a different compiler or different compiler options; you might
877 want just to find out which files are out of date without changing them.
879 By giving arguments when you run `make', you can do any of these
880 things and many others.
882 The exit status of `make' is always one of three values:
884 The exit status is zero if `make' is successful.
887 The exit status is two if `make' encounters any errors. It will
888 print messages describing the particular errors.
891 The exit status is one if you use the `-q' flag and `make'
892 determines that some target is not already up to date. *Note
893 Instead of Executing the Commands: Instead of Execution.
897 * Makefile Arguments:: How to specify which makefile to use.
898 * Goals:: How to use goal arguments to specify which
899 parts of the makefile to use.
900 * Instead of Execution:: How to use mode flags to specify what
901 kind of thing to do with the commands
902 in the makefile other than simply
904 * Avoiding Compilation:: How to avoid recompiling certain files.
905 * Overriding:: How to override a variable to specify
906 an alternate compiler and other things.
907 * Testing:: How to proceed past some errors, to
909 * Options Summary:: Summary of Options
912 File: make.info, Node: Makefile Arguments, Next: Goals, Prev: Running, Up: Running
914 Arguments to Specify the Makefile
915 =================================
917 The way to specify the name of the makefile is with the `-f' or
918 `--file' option (`--makefile' also works). For example, `-f altmake'
919 says to use the file `altmake' as the makefile.
921 If you use the `-f' flag several times and follow each `-f' with an
922 argument, all the specified files are used jointly as makefiles.
924 If you do not use the `-f' or `--file' flag, the default is to try
925 `GNUmakefile', `makefile', and `Makefile', in that order, and use the
926 first of these three which exists or can be made (*note Writing
927 Makefiles: Makefiles.).
930 File: make.info, Node: Goals, Next: Instead of Execution, Prev: Makefile Arguments, Up: Running
932 Arguments to Specify the Goals
933 ==============================
935 The "goals" are the targets that `make' should strive ultimately to
936 update. Other targets are updated as well if they appear as
937 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
939 By default, the goal is the first target in the makefile (not
940 counting targets that start with a period). Therefore, makefiles are
941 usually written so that the first target is for compiling the entire
942 program or programs they describe. If the first rule in the makefile
943 has several targets, only the first target in the rule becomes the
944 default goal, not the whole list.
946 You can specify a different goal or goals with arguments to `make'.
947 Use the name of the goal as an argument. If you specify several goals,
948 `make' processes each of them in turn, in the order you name them.
950 Any target in the makefile may be specified as a goal (unless it
951 starts with `-' or contains an `=', in which case it will be parsed as
952 a switch or variable definition, respectively). Even targets not in
953 the makefile may be specified, if `make' can find implicit rules that
954 say how to make them.
956 `Make' will set the special variable `MAKECMDGOALS' to the list of
957 goals you specified on the command line. If no goals were given on the
958 command line, this variable is empty. Note that this variable should
959 be used only in special circumstances.
961 An example of appropriate use is to avoid including `.d' files
962 during `clean' rules (*note Automatic Prerequisites::), so `make' won't
963 create them only to immediately remove them again:
965 sources = foo.c bar.c
967 ifneq ($(MAKECMDGOALS),clean)
968 include $(sources:.c=.d)
971 One use of specifying a goal is if you want to compile only a part of
972 the program, or only one of several programs. Specify as a goal each
973 file that you wish to remake. For example, consider a directory
974 containing several programs, with a makefile that starts like this:
977 all: size nm ld ar as
979 If you are working on the program `size', you might want to say
980 `make size' so that only the files of that program are recompiled.
982 Another use of specifying a goal is to make files that are not
983 normally made. For example, there may be a file of debugging output,
984 or a version of the program that is compiled specially for testing,
985 which has a rule in the makefile but is not a prerequisite of the
988 Another use of specifying a goal is to run the commands associated
989 with a phony target (*note Phony Targets::) or empty target (*note
990 Empty Target Files to Record Events: Empty Targets.). Many makefiles
991 contain a phony target named `clean' which deletes everything except
992 source files. Naturally, this is done only if you request it
993 explicitly with `make clean'. Following is a list of typical phony and
994 empty target names. *Note Standard Targets::, for a detailed list of
995 all the standard target names which GNU software packages use.
998 Make all the top-level targets the makefile knows about.
1001 Delete all files that are normally created by running `make'.
1004 Like `clean', but may refrain from deleting a few files that people
1005 normally don't want to recompile. For example, the `mostlyclean'
1006 target for GCC does not delete `libgcc.a', because recompiling it
1007 is rarely necessary and takes a lot of time.
1012 Any of these targets might be defined to delete _more_ files than
1013 `clean' does. For example, this would delete configuration files
1014 or links that you would normally create as preparation for
1015 compilation, even if the makefile itself cannot create these files.
1018 Copy the executable file into a directory that users typically
1019 search for commands; copy any auxiliary files that the executable
1020 uses into the directories where it will look for them.
1023 Print listings of the source files that have changed.
1026 Create a tar file of the source files.
1029 Create a shell archive (shar file) of the source files.
1032 Create a distribution file of the source files. This might be a
1033 tar file, or a shar file, or a compressed version of one of the
1034 above, or even more than one of the above.
1037 Update a tags table for this program.
1041 Perform self tests on the program this makefile builds.
1044 File: make.info, Node: Instead of Execution, Next: Avoiding Compilation, Prev: Goals, Up: Running
1046 Instead of Executing the Commands
1047 =================================
1049 The makefile tells `make' how to tell whether a target is up to date,
1050 and how to update each target. But updating the targets is not always
1051 what you want. Certain options specify other activities for `make'.
1057 "No-op". The activity is to print what commands would be used to
1058 make the targets up to date, but not actually execute them.
1062 "Touch". The activity is to mark the targets as up to date without
1063 actually changing them. In other words, `make' pretends to compile
1064 the targets but does not really change their contents.
1068 "Question". The activity is to find out silently whether the
1069 targets are up to date already; but execute no commands in either
1070 case. In other words, neither compilation nor output will occur.
1076 "What if". Each `-W' flag is followed by a file name. The given
1077 files' modification times are recorded by `make' as being the
1078 present time, although the actual modification times remain the
1079 same. You can use the `-W' flag in conjunction with the `-n' flag
1080 to see what would happen if you were to modify specific files.
1082 With the `-n' flag, `make' prints the commands that it would
1083 normally execute but does not execute them.
1085 With the `-t' flag, `make' ignores the commands in the rules and
1086 uses (in effect) the command `touch' for each target that needs to be
1087 remade. The `touch' command is also printed, unless `-s' or `.SILENT'
1088 is used. For speed, `make' does not actually invoke the program
1089 `touch'. It does the work directly.
1091 With the `-q' flag, `make' prints nothing and executes no commands,
1092 but the exit status code it returns is zero if and only if the targets
1093 to be considered are already up to date. If the exit status is one,
1094 then some updating needs to be done. If `make' encounters an error,
1095 the exit status is two, so you can distinguish an error from a target
1096 that is not up to date.
1098 It is an error to use more than one of these three flags in the same
1099 invocation of `make'.
1101 The `-n', `-t', and `-q' options do not affect command lines that
1102 begin with `+' characters or contain the strings `$(MAKE)' or
1103 `${MAKE}'. Note that only the line containing the `+' character or the
1104 strings `$(MAKE)' or `${MAKE}' is run regardless of these options.
1105 Other lines in the same rule are not run unless they too begin with `+'
1106 or contain `$(MAKE)' or `${MAKE}' (*Note How the `MAKE' Variable Works:
1109 The `-W' flag provides two features:
1111 * If you also use the `-n' or `-q' flag, you can see what `make'
1112 would do if you were to modify some files.
1114 * Without the `-n' or `-q' flag, when `make' is actually executing
1115 commands, the `-W' flag can direct `make' to act as if some files
1116 had been modified, without actually modifying the files.
1118 Note that the options `-p' and `-v' allow you to obtain other
1119 information about `make' or about the makefiles in use (*note Summary
1120 of Options: Options Summary.).
1123 File: make.info, Node: Avoiding Compilation, Next: Overriding, Prev: Instead of Execution, Up: Running
1125 Avoiding Recompilation of Some Files
1126 ====================================
1128 Sometimes you may have changed a source file but you do not want to
1129 recompile all the files that depend on it. For example, suppose you add
1130 a macro or a declaration to a header file that many other files depend
1131 on. Being conservative, `make' assumes that any change in the header
1132 file requires recompilation of all dependent files, but you know that
1133 they do not need to be recompiled and you would rather not waste the
1134 time waiting for them to compile.
1136 If you anticipate the problem before changing the header file, you
1137 can use the `-t' flag. This flag tells `make' not to run the commands
1138 in the rules, but rather to mark the target up to date by changing its
1139 last-modification date. You would follow this procedure:
1141 1. Use the command `make' to recompile the source files that really
1142 need recompilation, ensuring that the object files are up-to-date
1145 2. Make the changes in the header files.
1147 3. Use the command `make -t' to mark all the object files as up to
1148 date. The next time you run `make', the changes in the header
1149 files will not cause any recompilation.
1151 If you have already changed the header file at a time when some files
1152 do need recompilation, it is too late to do this. Instead, you can use
1153 the `-o FILE' flag, which marks a specified file as "old" (*note
1154 Summary of Options: Options Summary.). This means that the file itself
1155 will not be remade, and nothing else will be remade on its account.
1156 Follow this procedure:
1158 1. Recompile the source files that need compilation for reasons
1159 independent of the particular header file, with `make -o
1160 HEADERFILE'. If several header files are involved, use a separate
1161 `-o' option for each header file.
1163 2. Touch all the object files with `make -t'.
1166 File: make.info, Node: Overriding, Next: Testing, Prev: Avoiding Compilation, Up: Running
1168 Overriding Variables
1169 ====================
1171 An argument that contains `=' specifies the value of a variable:
1172 `V=X' sets the value of the variable V to X. If you specify a value in
1173 this way, all ordinary assignments of the same variable in the makefile
1174 are ignored; we say they have been "overridden" by the command line
1177 The most common way to use this facility is to pass extra flags to
1178 compilers. For example, in a properly written makefile, the variable
1179 `CFLAGS' is included in each command that runs the C compiler, so a
1180 file `foo.c' would be compiled something like this:
1182 cc -c $(CFLAGS) foo.c
1184 Thus, whatever value you set for `CFLAGS' affects each compilation
1185 that occurs. The makefile probably specifies the usual value for
1186 `CFLAGS', like this:
1190 Each time you run `make', you can override this value if you wish.
1191 For example, if you say `make CFLAGS='-g -O'', each C compilation will
1192 be done with `cc -c -g -O'. (This also illustrates how you can use
1193 quoting in the shell to enclose spaces and other special characters in
1194 the value of a variable when you override it.)
1196 The variable `CFLAGS' is only one of many standard variables that
1197 exist just so that you can change them this way. *Note Variables Used
1198 by Implicit Rules: Implicit Variables, for a complete list.
1200 You can also program the makefile to look at additional variables of
1201 your own, giving the user the ability to control other aspects of how
1202 the makefile works by changing the variables.
1204 When you override a variable with a command argument, you can define
1205 either a recursively-expanded variable or a simply-expanded variable.
1206 The examples shown above make a recursively-expanded variable; to make a
1207 simply-expanded variable, write `:=' instead of `='. But, unless you
1208 want to include a variable reference or function call in the _value_
1209 that you specify, it makes no difference which kind of variable you
1212 There is one way that the makefile can change a variable that you
1213 have overridden. This is to use the `override' directive, which is a
1214 line that looks like this: `override VARIABLE = VALUE' (*note The
1215 `override' Directive: Override Directive.).
1218 File: make.info, Node: Testing, Next: Options Summary, Prev: Overriding, Up: Running
1220 Testing the Compilation of a Program
1221 ====================================
1223 Normally, when an error happens in executing a shell command, `make'
1224 gives up immediately, returning a nonzero status. No further commands
1225 are executed for any target. The error implies that the goal cannot be
1226 correctly remade, and `make' reports this as soon as it knows.
1228 When you are compiling a program that you have just changed, this is
1229 not what you want. Instead, you would rather that `make' try compiling
1230 every file that can be tried, to show you as many compilation errors as
1233 On these occasions, you should use the `-k' or `--keep-going' flag.
1234 This tells `make' to continue to consider the other prerequisites of
1235 the pending targets, remaking them if necessary, before it gives up and
1236 returns nonzero status. For example, after an error in compiling one
1237 object file, `make -k' will continue compiling other object files even
1238 though it already knows that linking them will be impossible. In
1239 addition to continuing after failed shell commands, `make -k' will
1240 continue as much as possible after discovering that it does not know
1241 how to make a target or prerequisite file. This will always cause an
1242 error message, but without `-k', it is a fatal error (*note Summary of
1243 Options: Options Summary.).
1245 The usual behavior of `make' assumes that your purpose is to get the
1246 goals up to date; once `make' learns that this is impossible, it might
1247 as well report the failure immediately. The `-k' flag says that the
1248 real purpose is to test as much as possible of the changes made in the
1249 program, perhaps to find several independent problems so that you can
1250 correct them all before the next attempt to compile. This is why Emacs'
1251 `M-x compile' command passes the `-k' flag by default.