Changeset 503 in kBuild for trunk/src/gmake/doc/make.info-1
- Timestamp:
- Sep 15, 2006 5:09:38 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/doc/make.info-1
r200 r503 1 This is ../../doc/make.info, produced by makeinfo version 4.6 from 2 ../../doc/make.texi. 1 This is make.info, produced by makeinfo version 4.8 from make.texi. 2 3 This file documents the GNU `make' utility, which determines 4 automatically which pieces of a large program need to be recompiled, 5 and issues the commands to recompile them. 6 7 This is Edition 0.70, last updated 1 April 2006, of `The GNU Make 8 Manual', for GNU `make' version 3.81. 9 10 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 11 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 Free Software 12 Foundation, Inc. 13 14 Permission is granted to copy, distribute and/or modify this 15 document under the terms of the GNU Free Documentation License, 16 Version 1.2 or any later version published by the Free Software 17 Foundation; with no Invariant Sections, with the Front-Cover Texts 18 being "A GNU Manual," and with the Back-Cover Texts as in (a) 19 below. A copy of the license is included in the section entitled 20 "GNU Free Documentation License." 21 22 (a) The FSF's Back-Cover Text is: "You have freedom to copy and 23 modify this GNU Manual, like GNU software. Copies published by 24 the Free Software Foundation raise funds for GNU development." 3 25 4 26 INFO-DIR-SECTION GNU Packages … … 7 29 END-INFO-DIR-ENTRY 8 30 9 This file documents the GNU Make utility, which determines 31 32 File: make.info, Node: Top, Next: Overview, Prev: (dir), Up: (dir) 33 34 GNU `make' 35 ********** 36 37 This file documents the GNU `make' utility, which determines 10 38 automatically which pieces of a large program need to be recompiled, 11 39 and issues the commands to recompile them. 12 40 13 This is Edition 0.61, last updated 02 May 2003, of `The GNU Make 14 Manual', for `make', Version 3.81. 15 16 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 17 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. 18 19 Permission is granted to copy, distribute and/or modify this document 20 under the terms of the GNU Free Documentation License, Version 1.1 or 21 any later version published by the Free Software Foundation; with no 22 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover 23 Texts. A copy of the license is included in the section entitled "GNU 24 Free Documentation License". 25 26 27 File: make.info, Node: Top, Next: Overview, Prev: (dir), Up: (dir) 28 29 Make 30 **** 31 32 The GNU `make' utility automatically determines which pieces of a large 33 program need to be recompiled, and issues the commands to recompile 34 them. 35 36 This edition of the `GNU Make Manual', last updated 02 May 2003, 37 documents GNU `make' Version 3.81. 38 39 This manual describes `make' and contains the following chapters: 41 This is Edition 0.70, last updated 1 April 2006, of `The GNU Make 42 Manual', for GNU `make' version 3.81. 43 44 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 45 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 Free Software 46 Foundation, Inc. 47 48 Permission is granted to copy, distribute and/or modify this 49 document under the terms of the GNU Free Documentation License, 50 Version 1.2 or any later version published by the Free Software 51 Foundation; with no Invariant Sections, with the Front-Cover Texts 52 being "A GNU Manual," and with the Back-Cover Texts as in (a) 53 below. A copy of the license is included in the section entitled 54 "GNU Free Documentation License." 55 56 (a) The FSF's Back-Cover Text is: "You have freedom to copy and 57 modify this GNU Manual, like GNU software. Copies published by 58 the Free Software Foundation raise funds for GNU development." 40 59 41 60 * Menu: … … 97 116 with another makefile. 98 117 * Reading Makefiles:: How makefiles are parsed. 118 * Secondary Expansion:: How and when secondary expansion is performed. 99 119 100 120 Writing Rules … … 107 127 * Phony Targets:: Using a target that is not a real file's name. 108 128 * Force Targets:: You can use a target without commands 109 or prerequisites to mark other 110 targetsas phony.129 or prerequisites to mark other targets 130 as phony. 111 131 * Empty Targets:: When only the date matters and the 112 132 files are empty. … … 148 168 Writing the Commands in Rules 149 169 170 * Command Syntax:: Command syntax features and pitfalls. 150 171 * Echoing:: How to control when commands are echoed. 151 172 * Execution:: How commands are executed. … … 156 177 * Sequences:: Defining canned sequences of commands. 157 178 * Empty Commands:: Defining useful, do-nothing commands. 179 180 Command Syntax 181 182 * Splitting Lines:: Breaking long command lines for readability. 183 * Variables in Commands:: Using `make' variables in commands. 184 185 Command Execution 186 187 * Choosing the Shell:: How `make' chooses the shell used 188 to run commands. 158 189 159 190 Recursive Use of `make' … … 201 232 * Text Functions:: General-purpose text manipulation functions. 202 233 * File Name Functions:: Functions for manipulating file names. 234 * Conditional Functions:: Functions that implement conditions. 203 235 * Foreach Function:: Repeat some text with controlled variation. 204 * If Function:: Conditionally expand a value.205 236 * Call Function:: Expand a user-defined function. 206 237 * Value Function:: Return the un-expanded value of a variable. 207 238 * Eval Function:: Evaluate the arguments as makefile syntax. 208 239 * Origin Function:: Find where a variable got its value. 240 * Flavor Function:: Find out the flavor of a variable. 209 241 * Shell Function:: Substitute the output of a shell command. 210 242 * Make Control Functions:: Functions that control how make runs. … … 234 266 * Chained Rules:: How to use a chain of implicit rules. 235 267 * Pattern Rules:: How to define new implicit rules. 236 * Last Resort:: How to defin ing commands for rules237 whichcannot find any.268 * Last Resort:: How to define commands for rules which 269 cannot find any. 238 270 * Suffix Rules:: The old-fashioned style of implicit rule. 239 271 * Implicit Rule Search:: The precise algorithm for applying … … 264 296 * Archive Symbols:: How to update archive symbol directories. 265 297 266 Makefile Conventions267 268 * Makefile Basics:: General Conventions for Makefiles269 * Utilities in Makefiles:: Utilities in Makefiles270 * Command Variables:: Variables for Specifying Commands271 * Directory Variables:: Variables for Installation Directories272 * Standard Targets:: Standard Targets for Users273 * Install Command Categories:: Three categories of commands in the `install'274 275 Copying This Manual276 277 298 278 299 File: make.info, Node: Overview, Next: Introduction, Prev: Top, Up: Top 279 300 280 Overview of `make'281 ****************** 301 1 Overview of `make' 302 ******************** 282 303 283 304 The `make' utility automatically determines which pieces of a large … … 288 309 289 310 GNU `make' conforms to section 6.2 of `IEEE Standard 1003.2-1992' 290 (POSIX.2). 311 (POSIX.2). 291 312 292 313 Our examples show C programs, since they are most common, but you … … 330 351 File: make.info, Node: Reading, Next: Bugs, Prev: Preparing, Up: Overview 331 352 332 How to Read This Manual333 ======================= 353 1.1 How to Read This Manual 354 =========================== 334 355 335 356 If you are new to `make', or are looking for a general introduction, … … 351 372 File: make.info, Node: Bugs, Prev: Reading, Up: Overview 352 373 353 Problems and Bugs354 ================= 374 1.2 Problems and Bugs 375 ===================== 355 376 356 377 If you have problems with GNU `make' or think you've found a bug, … … 394 415 File: make.info, Node: Introduction, Next: Makefiles, Prev: Overview, Up: Top 395 416 396 An Introduction to Makefiles397 **************************** 417 2 An Introduction to Makefiles 418 ****************************** 398 419 399 420 You need a file called a "makefile" to tell `make' what to do. Most 400 often, the makefile tells `make' how to compile and link a program. 421 often, the makefile tells `make' how to compile and link a program. 401 422 402 423 In this chapter, we will discuss a simple makefile that describes … … 413 434 Finally, if any source file has been recompiled, all the object files, 414 435 whether newly made or saved from previous compilations, must be linked 415 together to produce the new executable editor. 436 together to produce the new executable editor. 416 437 417 438 * Menu: … … 428 449 File: make.info, Node: Rule Introduction, Next: Simple Makefile, Prev: Introduction, Up: Introduction 429 450 430 What a Rule Looks Like431 ====================== 451 2.1 What a Rule Looks Like 452 ========================== 432 453 433 454 A simple makefile consists of "rules" with the following shape: … … 471 492 File: make.info, Node: Simple Makefile, Next: How Make Works, Prev: Rule Introduction, Up: Introduction 472 493 473 A Simple Makefile474 ================= 494 2.2 A Simple Makefile 495 ===================== 475 496 476 497 Here is a straightforward makefile that describes the way an executable … … 486 507 cc -o edit main.o kbd.o command.o display.o \ 487 508 insert.o search.o files.o utils.o 488 509 489 510 main.o : main.c defs.h 490 511 cc -c main.c … … 508 529 509 530 We split each long line into two lines using backslash-newline; this is 510 like using one long line, but is easier to read. 531 like using one long line, but is easier to read. 511 532 512 533 To use this makefile to create the executable file called `edit', … … 536 557 prerequisites. These shell commands say how to update the target file. 537 558 A tab character must come at the beginning of every command line to 538 distinguish command slines from other lines in the makefile. (Bear in559 distinguish command lines from other lines in the makefile. (Bear in 539 560 mind that `make' does not know anything about how the commands work. 540 561 It is up to you to supply commands that will update the target file 541 562 properly. All `make' does is execute the commands in the rule you have 542 specified when the target file needs to be updated.) 563 specified when the target file needs to be updated.) 543 564 544 565 The target `clean' is not a file, but merely the name of an action. … … 551 572 called "phony targets". *Note Phony Targets::, for information about 552 573 this kind of target. *Note Errors in Commands: Errors, to see how to 553 cause `make' to ignore errors from `rm' or any other command. 574 cause `make' to ignore errors from `rm' or any other command. 554 575 555 576 556 577 File: make.info, Node: How Make Works, Next: Variables Simplify, Prev: Simple Makefile, Up: Introduction 557 578 558 How `make' Processes a Makefile559 =============================== 579 2.3 How `make' Processes a Makefile 580 =================================== 560 581 561 582 By default, `make' starts with the first target (not targets whose 562 583 names start with `.'). This is called the "default goal". ("Goals" 563 are the targets that `make' strives ultimately to update. *Note 564 Arguments to Specify the Goals: Goals.) 584 are the targets that `make' strives ultimately to update. You can 585 override this behavior using the command line (*note Arguments to 586 Specify the Goals: Goals.) or with the `.DEFAULT_GOAL' special variable 587 (*note Other Special Variables: Special Variables.). 565 588 566 589 In the simple example of the previous section, the default goal is to … … 597 620 not exist, or if any of the object files are newer than it. If an 598 621 object file was just recompiled, it is now newer than `edit', so `edit' 599 is relinked. 622 is relinked. 600 623 601 624 Thus, if we change the file `insert.c' and run `make', `make' will … … 608 631 File: make.info, Node: Variables Simplify, Next: make Deduces, Prev: How Make Works, Up: Introduction 609 632 610 Variables Make Makefiles Simpler611 ================================ 633 2.4 Variables Make Makefiles Simpler 634 ==================================== 612 635 613 636 In our example, we had to list all the object files twice in the rule … … 642 665 objects = main.o kbd.o command.o display.o \ 643 666 insert.o search.o files.o utils.o 644 667 645 668 edit : $(objects) 646 669 cc -o edit $(objects) … … 667 690 File: make.info, Node: make Deduces, Next: Combine By Prerequisite, Prev: Variables Simplify, Up: Introduction 668 691 669 Letting `make' Deduce the Commands670 ================================== 692 2.5 Letting `make' Deduce the Commands 693 ====================================== 671 694 672 695 It is not necessary to spell out the commands for compiling the … … 687 710 objects = main.o kbd.o command.o display.o \ 688 711 insert.o search.o files.o utils.o 689 712 690 713 edit : $(objects) 691 714 cc -o edit $(objects) 692 715 693 716 main.o : defs.h 694 717 kbd.o : defs.h command.h … … 699 722 files.o : defs.h buffer.h command.h 700 723 utils.o : defs.h 701 724 702 725 .PHONY : clean 703 726 clean : … … 714 737 File: make.info, Node: Combine By Prerequisite, Next: Cleanup, Prev: make Deduces, Up: Introduction 715 738 716 Another Style of Makefile717 ========================= 739 2.6 Another Style of Makefile 740 ============================= 718 741 719 742 When the objects of a makefile are created only by implicit rules, an … … 724 747 objects = main.o kbd.o command.o display.o \ 725 748 insert.o search.o files.o utils.o 726 749 727 750 edit : $(objects) 728 751 cc -o edit $(objects) 729 752 730 753 $(objects) : defs.h 731 754 kbd.o command.o files.o : command.h … … 743 766 File: make.info, Node: Cleanup, Prev: Combine By Prerequisite, Up: Introduction 744 767 745 Rules for Cleaning the Directory746 ================================ 768 2.7 Rules for Cleaning the Directory 769 ==================================== 747 770 748 771 Compiling a program is not the only thing you might want to write rules … … 782 805 File: make.info, Node: Makefiles, Next: Rules, Prev: Introduction, Up: Top 783 806 784 Writing Makefiles785 ***************** 807 3 Writing Makefiles 808 ******************* 786 809 787 810 The information that tells `make' how to recompile a system comes from … … 800 823 with another makefile. 801 824 * Reading Makefiles:: How makefiles are parsed. 825 * Secondary Expansion:: How and when secondary expansion is performed. 802 826 803 827 804 828 File: make.info, Node: Makefile Contents, Next: Makefile Names, Prev: Makefiles, Up: Makefiles 805 829 806 What Makefiles Contain807 ====================== 830 3.1 What Makefiles Contain 831 ========================== 808 832 809 833 Makefiles contain five kinds of things: "explicit rules", "implicit … … 865 889 File: make.info, Node: Makefile Names, Next: Include, Prev: Makefile Contents, Up: Makefiles 866 890 867 What Name to Give Your Makefile868 =============================== 891 3.2 What Name to Give Your Makefile 892 =================================== 869 893 870 894 By default, when `make' looks for the makefile, it tries the following 871 names, in order: `GNUmakefile', `makefile' and `Makefile'. 895 names, in order: `GNUmakefile', `makefile' and `Makefile'. 872 896 873 897 Normally you should call your makefile either `makefile' or … … 892 916 concatenated in the order specified. The default makefile names 893 917 `GNUmakefile', `makefile' and `Makefile' are not checked automatically 894 if you specify `-f' or `--file'. 918 if you specify `-f' or `--file'. 895 919 896 920 897 921 File: make.info, Node: Include, Next: MAKEFILES Variable, Prev: Makefile Names, Up: Makefiles 898 922 899 Including Other Makefiles900 ========================= 923 3.3 Including Other Makefiles 924 ============================= 901 925 902 926 The `include' directive tells `make' to suspend reading the current … … 906 930 include FILENAMES... 907 931 908 FILENAMES can contain shell file name patterns. 932 FILENAMES can contain shell file name patterns. If FILENAMES is empty, 933 nothing is included and no error is printed. 909 934 910 935 Extra spaces are allowed and ignored at the beginning of the line, … … 943 968 than that of somehow appending the prerequisites to the end of the main 944 969 makefile as has been traditionally done with other versions of `make'. 945 *Note Automatic Prerequisites::. 970 *Note Automatic Prerequisites::. 946 971 947 972 If the specified name does not start with a slash, and the file is … … 981 1006 File: make.info, Node: MAKEFILES Variable, Next: MAKEFILE_LIST Variable, Prev: Include, Up: Makefiles 982 1007 983 The Variable `MAKEFILES'984 ======================== 1008 3.4 The Variable `MAKEFILES' 1009 ============================ 985 1010 986 1011 If the environment variable `MAKEFILES' is defined, `make' considers … … 1010 1035 File: make.info, Node: MAKEFILE_LIST Variable, Next: Special Variables, Prev: MAKEFILES Variable, Up: Makefiles 1011 1036 1012 The Variable `MAKEFILE_LIST'1013 ============================ 1037 3.5 The Variable `MAKEFILE_LIST' 1038 ================================ 1014 1039 1015 1040 As `make' reads various makefiles, including any obtained from the … … 1026 1051 If a makefile named `Makefile' has this content: 1027 1052 1028 name1 := $( word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))1029 1053 name1 := $(lastword $(MAKEFILE_LIST)) 1054 1030 1055 include inc.mk 1031 1032 name2 := $( word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))1033 1056 1057 name2 := $(lastword $(MAKEFILE_LIST)) 1058 1034 1059 all: 1035 1060 @echo name1 = $(name1) … … 1049 1074 File: make.info, Node: Special Variables, Next: Remaking Makefiles, Prev: MAKEFILE_LIST Variable, Up: Makefiles 1050 1075 1051 Other Special Variables 1052 ======================= 1053 1054 GNU `make' also supports a special variable. Note that any value you 1055 assign to this variable will be ignored; it will always return its 1056 special value. 1057 1058 The first special variable is `.VARIABLES'. When expanded, the 1059 value consists of a list of the _names_ of all global variables defined 1060 in all makefiles read up until that point. This includes variables 1061 which have empty values, as well as built-in variables (*note Variables 1062 Used by Implicit Rules: Implicit Variables.), but does not include any 1063 variables which are only defined in a target-specific context. 1076 3.6 Other Special Variables 1077 =========================== 1078 1079 GNU `make' also supports other special variables. Unless otherwise 1080 documented here, these values lose their special properties if they are 1081 set by a makefile or on the command line. 1082 1083 `.DEFAULT_GOAL' 1084 Sets the default goal to be used if no targets were specified on 1085 the command line (*note Arguments to Specify the Goals: Goals.). 1086 The `.DEFAULT_GOAL' variable allows you to discover the current 1087 default goal, restart the default goal selection algorithm by 1088 clearing its value, or to explicitly set the default goal. The 1089 following example illustrates these cases: 1090 1091 # Query the default goal. 1092 ifeq ($(.DEFAULT_GOAL),) 1093 $(warning no default goal is set) 1094 endif 1095 1096 .PHONY: foo 1097 foo: ; @echo $@ 1098 1099 $(warning default goal is $(.DEFAULT_GOAL)) 1100 1101 # Reset the default goal. 1102 .DEFAULT_GOAL := 1103 1104 .PHONY: bar 1105 bar: ; @echo $@ 1106 1107 $(warning default goal is $(.DEFAULT_GOAL)) 1108 1109 # Set our own. 1110 .DEFAULT_GOAL := foo 1111 1112 This makefile prints: 1113 1114 no default goal is set 1115 default goal is foo 1116 default goal is bar 1117 foo 1118 1119 Note that assigning more than one target name to `.DEFAULT_GOAL' is 1120 illegal and will result in an error. 1121 1122 `MAKE_RESTARTS' 1123 This variable is set only if this instance of `make' has restarted 1124 (*note How Makefiles Are Remade: Remaking Makefiles.): it will 1125 contain the number of times this instance has restarted. Note 1126 this is not the same as recursion (counted by the `MAKELEVEL' 1127 variable). You should not set, modify, or export this variable. 1128 1129 `.VARIABLES' 1130 Expands to a list of the _names_ of all global variables defined 1131 so far. This includes variables which have empty values, as well 1132 as built-in variables (*note Variables Used by Implicit Rules: 1133 Implicit Variables.), but does not include any variables which are 1134 only defined in a target-specific context. Note that any value 1135 you assign to this variable will be ignored; it will always return 1136 its special value. 1137 1138 `.FEATURES' 1139 Expands to a list of special features supported by this version of 1140 `make'. Possible values include: 1141 1142 `archives' 1143 Supports `ar' (archive) files using special filename syntax. 1144 *Note Using `make' to Update Archive Files: Archives. 1145 1146 `check-symlink' 1147 Supports the `-L' (`--check-symlink-times') flag. *Note 1148 Summary of Options: Options Summary. 1149 1150 `else-if' 1151 Supports "else if" non-nested conditionals. *Note Syntax of 1152 Conditionals: Conditional Syntax. 1153 1154 `jobserver' 1155 Supports "job server" enhanced parallel builds. *Note 1156 Parallel Execution: Parallel. 1157 1158 `second-expansion' 1159 Supports secondary expansion of prerequisite lists. 1160 1161 `order-only' 1162 Supports order-only prerequisites. *Note Types of 1163 Prerequisites: Prerequisite Types. 1164 1165 `target-specific' 1166 Supports target-specific and pattern-specific variable 1167 assignments. *Note Target-specific Variable Values: 1168 Target-specific. 1169 1170 1171 `.INCLUDE_DIRS' 1172 Expands to a list of directories that `make' searches for included 1173 makefiles (*note Including Other Makefiles: Include.). 1174 1064 1175 1065 1176 1066 1177 File: make.info, Node: Remaking Makefiles, Next: Overriding Makefiles, Prev: Special Variables, Up: Makefiles 1067 1178 1068 How Makefiles Are Remade1069 ======================== 1179 3.7 How Makefiles Are Remade 1180 ============================ 1070 1181 1071 1182 Sometimes makefiles can be remade from other files, such as RCS or SCCS … … 1104 1215 If you do not specify any makefiles to be read with `-f' or `--file' 1105 1216 options, `make' will try the default makefile names; *note What Name to 1106 Give Your Makefile: Makefile Names. .Unlike makefiles explicitly1217 Give Your Makefile: Makefile Names. Unlike makefiles explicitly 1107 1218 requested with `-f' or `--file' options, `make' is not certain that 1108 1219 these makefiles should exist. However, if a default makefile does not … … 1144 1255 File: make.info, Node: Overriding Makefiles, Next: Reading Makefiles, Prev: Remaking Makefiles, Up: Makefiles 1145 1256 1146 Overriding Part of Another Makefile1147 =================================== 1257 3.8 Overriding Part of Another Makefile 1258 ======================================= 1148 1259 1149 1260 Sometimes it is useful to have a makefile that is mostly just like … … 1165 1276 foo: 1166 1277 frobnicate > foo 1167 1278 1168 1279 %: force 1169 1280 @$(MAKE) -f Makefile $@ … … 1187 1298 1188 1299 1189 File: make.info, Node: Reading Makefiles, Prev: Overriding Makefiles, Up: Makefiles1190 1191 How `make' Reads a Makefile1192 =========================== 1300 File: make.info, Node: Reading Makefiles, Next: Secondary Expansion, Prev: Overriding Makefiles, Up: Makefiles 1301 1302 3.9 How `make' Reads a Makefile 1303 =============================== 1193 1304 1194 1305 GNU `make' does its work in two distinct phases. During the first … … 1225 1336 IMMEDIATE := IMMEDIATE 1226 1337 IMMEDIATE += DEFERRED or IMMEDIATE 1227 1338 1228 1339 define IMMEDIATE 1229 1340 DEFERRED … … 1239 1350 All instances of conditional syntax are parsed immediately, in their 1240 1351 entirety; this includes the `ifdef', `ifeq', `ifndef', and `ifneq' 1241 forms. 1352 forms. Of course this means that automatic variables cannot be used in 1353 conditional statements, as automatic variables are not set until the 1354 command script for that rule is invoked. If you need to use automatic 1355 variables in a conditional you _must_ use shell conditional syntax, in 1356 your command script proper, for these tests, not `make' conditionals. 1242 1357 1243 1358 Rule Definition … … 1255 1370 1256 1371 1372 File: make.info, Node: Secondary Expansion, Prev: Reading Makefiles, Up: Makefiles 1373 1374 3.10 Secondary Expansion 1375 ======================== 1376 1377 In the previous section we learned that GNU `make' works in two 1378 distinct phases: a read-in phase and a target-update phase (*note How 1379 `make' Reads a Makefile: Reading Makefiles.). GNU make also has the 1380 ability to enable a _second expansion_ of the prerequisites (only) for 1381 some or all targets defined in the makefile. In order for this second 1382 expansion to occur, the special target `.SECONDEXPANSION' must be 1383 defined before the first prerequisite list that makes use of this 1384 feature. 1385 1386 If that special target is defined then in between the two phases 1387 mentioned above, right at the end of the read-in phase, all the 1388 prerequisites of the targets defined after the special target are 1389 expanded a _second time_. In most circumstances this secondary 1390 expansion will have no effect, since all variable and function 1391 references will have been expanded during the initial parsing of the 1392 makefiles. In order to take advantage of the secondary expansion phase 1393 of the parser, then, it's necessary to _escape_ the variable or 1394 function reference in the makefile. In this case the first expansion 1395 merely un-escapes the reference but doesn't expand it, and expansion is 1396 left to the secondary expansion phase. For example, consider this 1397 makefile: 1398 1399 .SECONDEXPANSION: 1400 ONEVAR = onefile 1401 TWOVAR = twofile 1402 myfile: $(ONEVAR) $$(TWOVAR) 1403 1404 After the first expansion phase the prerequisites list of the 1405 `myfile' target will be `onefile' and `$(TWOVAR)'; the first 1406 (unescaped) variable reference to ONEVAR is expanded, while the second 1407 (escaped) variable reference is simply unescaped, without being 1408 recognized as a variable reference. Now during the secondary expansion 1409 the first word is expanded again but since it contains no variable or 1410 function references it remains the static value `onefile', while the 1411 second word is now a normal reference to the variable TWOVAR, which is 1412 expanded to the value `twofile'. The final result is that there are 1413 two prerequisites, `onefile' and `twofile'. 1414 1415 Obviously, this is not a very interesting case since the same result 1416 could more easily have been achieved simply by having both variables 1417 appear, unescaped, in the prerequisites list. One difference becomes 1418 apparent if the variables are reset; consider this example: 1419 1420 .SECONDEXPANSION: 1421 AVAR = top 1422 onefile: $(AVAR) 1423 twofile: $$(AVAR) 1424 AVAR = bottom 1425 1426 Here the prerequisite of `onefile' will be expanded immediately, and 1427 resolve to the value `top', while the prerequisite of `twofile' will 1428 not be full expanded until the secondary expansion and yield a value of 1429 `bottom'. 1430 1431 This is marginally more exciting, but the true power of this feature 1432 only becomes apparent when you discover that secondary expansions 1433 always take place within the scope of the automatic variables for that 1434 target. This means that you can use variables such as `$@', `$*', etc. 1435 during the second expansion and they will have their expected values, 1436 just as in the command script. All you have to do is defer the 1437 expansion by escaping the `$'. Also, secondary expansion occurs for 1438 both explicit and implicit (pattern) rules. Knowing this, the possible 1439 uses for this feature increase dramatically. For example: 1440 1441 .SECONDEXPANSION: 1442 main_OBJS := main.o try.o test.o 1443 lib_OBJS := lib.o api.o 1444 1445 main lib: $$($$@_OBJS) 1446 1447 Here, after the initial expansion the prerequisites of both the 1448 `main' and `lib' targets will be `$($@_OBJS)'. During the secondary 1449 expansion, the `$@' variable is set to the name of the target and so 1450 the expansion for the `main' target will yield `$(main_OBJS)', or 1451 `main.o try.o test.o', while the secondary expansion for the `lib' 1452 target will yield `$(lib_OBJS)', or `lib.o api.o'. 1453 1454 You can also mix functions here, as long as they are properly 1455 escaped: 1456 1457 main_SRCS := main.c try.c test.c 1458 lib_SRCS := lib.c api.c 1459 1460 .SECONDEXPANSION: 1461 main lib: $$(patsubst %.c,%.o,$$($$@_SRCS)) 1462 1463 This version allows users to specify source files rather than object 1464 files, but gives the same resulting prerequisites list as the previous 1465 example. 1466 1467 Evaluation of automatic variables during the secondary expansion 1468 phase, especially of the target name variable `$$@', behaves similarly 1469 to evaluation within command scripts. However, there are some subtle 1470 differences and "corner cases" which come into play for the different 1471 types of rule definitions that `make' understands. The subtleties of 1472 using the different automatic variables are described below. 1473 1474 Secondary Expansion of Explicit Rules 1475 ------------------------------------- 1476 1477 During the secondary expansion of explicit rules, `$$@' and `$$%' 1478 evaluate, respectively, to the file name of the target and, when the 1479 target is an archive member, the target member name. The `$$<' 1480 variable evaluates to the first prerequisite in the first rule for this 1481 target. `$$^' and `$$+' evaluate to the list of all prerequisites of 1482 rules _that have already appeared_ for the same target (`$$+' with 1483 repetitions and `$$^' without). The following example will help 1484 illustrate these behaviors: 1485 1486 .SECONDEXPANSION: 1487 1488 foo: foo.1 bar.1 $$< $$^ $$+ # line #1 1489 1490 foo: foo.2 bar.2 $$< $$^ $$+ # line #2 1491 1492 foo: foo.3 bar.3 $$< $$^ $$+ # line #3 1493 1494 In the first prerequisite list, all three variables (`$$<', `$$^', 1495 and `$$+') expand to the empty string. In the second, they will have 1496 values `foo.1', `foo.1 bar.1', and `foo.1 bar.1' respectively. In the 1497 third they will have values `foo.1', `foo.1 bar.1 foo.2 bar.2', and 1498 `foo.1 bar.1 foo.2 bar.2' respectively. 1499 1500 Rules undergo secondary expansion in makefile order, except that the 1501 rule with the command script is always evaluated last. 1502 1503 The variables `$$?' and `$$*' are not available and expand to the 1504 empty string. 1505 1506 Secondary Expansion of Static Pattern Rules 1507 ------------------------------------------- 1508 1509 Rules for secondary expansion of static pattern rules are identical to 1510 those for explicit rules, above, with one exception: for static pattern 1511 rules the `$$*' variable is set to the pattern stem. As with explicit 1512 rules, `$$?' is not available and expands to the empty string. 1513 1514 Secondary Expansion of Implicit Rules 1515 ------------------------------------- 1516 1517 As `make' searches for an implicit rule, it substitutes the stem and 1518 then performs secondary expansion for every rule with a matching target 1519 pattern. The value of the automatic variables is derived in the same 1520 fashion as for static pattern rules. As an example: 1521 1522 .SECONDEXPANSION: 1523 1524 foo: bar 1525 1526 foo foz: fo%: bo% 1527 1528 %oo: $$< $$^ $$+ $$* 1529 1530 When the implicit rule is tried for target `foo', `$$<' expands to 1531 `bar', `$$^' expands to `bar boo', `$$+' also expands to `bar boo', and 1532 `$$*' expands to `f'. 1533 1534 Note that the directory prefix (D), as described in *Note Implicit 1535 Rule Search Algorithm: Implicit Rule Search, is appended (after 1536 expansion) to all the patterns in the prerequisites list. As an 1537 example: 1538 1539 .SECONDEXPANSION: 1540 1541 /tmp/foo.o: 1542 1543 %.o: $$(addsuffix /%.c,foo bar) foo.h 1544 1545 The prerequisite list after the secondary expansion and directory 1546 prefix reconstruction will be `/tmp/foo/foo.c /tmp/var/bar/foo.c 1547 foo.h'. If you are not interested in this reconstruction, you can use 1548 `$$*' instead of `%' in the prerequisites list. 1549 1550 1257 1551 File: make.info, Node: Rules, Next: Commands, Prev: Makefiles, Up: Top 1258 1552 1259 Writing Rules1260 ************* 1553 4 Writing Rules 1554 *************** 1261 1555 1262 1556 A "rule" appears in the makefile and says when and how to remake … … 1289 1583 * Phony Targets:: Using a target that is not a real file's name. 1290 1584 * Force Targets:: You can use a target without commands 1291 or prerequisites to mark other 1292 targetsas phony.1585 or prerequisites to mark other targets 1586 as phony. 1293 1587 * Empty Targets:: When only the date matters and the 1294 1588 files are empty. … … 1307 1601 File: make.info, Node: Rule Example, Next: Rule Syntax, Prev: Rules, Up: Rules 1308 1602 1309 Rule Example1310 ============ 1603 4.1 Rule Example 1604 ================ 1311 1605 1312 1606 Here is an example of a rule: … … 1333 1627 File: make.info, Node: Rule Syntax, Next: Prerequisite Types, Prev: Rule Example, Up: Rules 1334 1628 1335 Rule Syntax1336 =========== 1629 4.2 Rule Syntax 1630 =============== 1337 1631 1338 1632 In general, a rule looks like this: … … 1359 1653 appear on the line after the prerequisites, with a tab character, or may 1360 1654 appear on the same line, with a semicolon. Either way, the effect is 1361 the same. *Note Writing the Commands in Rules: Commands. 1362 1363 Because dollar signs are used to start variable references, if you 1364 really want a dollar sign in a rule you must write two of them, `$$' 1365 (*note How to Use Variables: Using Variables.). You may split a long 1366 line by inserting a backslash followed by a newline, but this is not 1367 required, as `make' places no limit on the length of a line in a 1368 makefile. 1655 the same. There are other differences in the syntax of command lines. 1656 *Note Writing the Commands in Rules: Commands. 1657 1658 Because dollar signs are used to start `make' variable references, 1659 if you really want a dollar sign in a target or prerequisite you must 1660 write two of them, `$$' (*note How to Use Variables: Using Variables.). 1661 If you have enabled secondary expansion (*note Secondary Expansion::) 1662 and you want a literal dollar sign in the prerequisites lise, you must 1663 actually write _four_ dollar signs (`$$$$'). 1664 1665 You may split a long line by inserting a backslash followed by a 1666 newline, but this is not required, as `make' places no limit on the 1667 length of a line in a makefile. 1369 1668 1370 1669 A rule tells `make' two things: when the targets are out of date, … … 1388 1687 File: make.info, Node: Prerequisite Types, Next: Wildcards, Prev: Rule Syntax, Up: Rules 1389 1688 1390 Types of Prerequisites1391 ====================== 1689 4.3 Types of Prerequisites 1690 ========================== 1392 1691 1393 1692 There are actually two different types of prerequisites understood by … … 1424 1723 File: make.info, Node: Wildcards, Next: Directory Search, Prev: Prerequisite Types, Up: Rules 1425 1724 1426 Using Wildcard Characters in File Names1427 ======================================= 1725 4.4 Using Wildcard Characters in File Names 1726 =========================================== 1428 1727 1429 1728 A single file name can specify many files using "wildcard characters". … … 1441 1740 simulated by setting the environment variable HOME. 1442 1741 1443 Wildcard expansion happens automatically in targets, in1444 prerequisites, and in commands (where the shell does the expansion). 1445 In other contexts, wildcard expansion happens only if you request it 1446 explicitly with the `wildcard' function.1742 Wildcard expansion is performed by `make' automatically in targets 1743 and in prerequisites. In commands the shell is responsible for 1744 wildcard expansion. In other contexts, wildcard expansion happens only 1745 if you request it explicitly with the `wildcard' function. 1447 1746 1448 1747 The special significance of a wildcard character can be turned off by … … 1460 1759 File: make.info, Node: Wildcard Examples, Next: Wildcard Pitfall, Prev: Wildcards, Up: Wildcards 1461 1760 1462 Wildcard Examples1463 ----------------- 1761 4.4.1 Wildcard Examples 1762 ----------------------- 1464 1763 1465 1764 Wildcards can be used in the commands of a rule, where they are expanded … … 1500 1799 File: make.info, Node: Wildcard Pitfall, Next: Wildcard Function, Prev: Wildcard Examples, Up: Wildcards 1501 1800 1502 Pitfalls of Using Wildcards1503 --------------------------- 1801 4.4.2 Pitfalls of Using Wildcards 1802 --------------------------------- 1504 1803 1505 1804 Now here is an example of a naive way of using wildcard expansion, that … … 1509 1808 1510 1809 objects = *.o 1511 1810 1512 1811 foo : $(objects) 1513 1812 cc -o foo $(CFLAGS) $(objects) … … 1543 1842 File: make.info, Node: Wildcard Function, Prev: Wildcard Pitfall, Up: Wildcards 1544 1843 1545 The Function `wildcard'1546 ----------------------- 1844 4.4.3 The Function `wildcard' 1845 ----------------------------- 1547 1846 1548 1847 Wildcard expansion happens automatically in rules. But wildcard … … 1579 1878 1580 1879 objects := $(patsubst %.c,%.o,$(wildcard *.c)) 1581 1880 1582 1881 foo : $(objects) 1583 1882 cc -o foo $(objects) … … 1591 1890 File: make.info, Node: Directory Search, Next: Phony Targets, Prev: Wildcards, Up: Rules 1592 1891 1593 Searching Directories for Prerequisites1594 ======================================= 1892 4.5 Searching Directories for Prerequisites 1893 =========================================== 1595 1894 1596 1895 For large systems, it is often desirable to put sources in a separate … … 1615 1914 File: make.info, Node: General Search, Next: Selective Search, Prev: Directory Search, Up: Directory Search 1616 1915 1617 `VPATH': Search Path for All Prerequisites1618 ------------------------------------------ 1916 4.5.1 `VPATH': Search Path for All Prerequisites 1917 ------------------------------------------------ 1619 1918 1620 1919 The value of the `make' variable `VPATH' specifies a list of … … 1659 1958 File: make.info, Node: Selective Search, Next: Search Algorithm, Prev: General Search, Up: Directory Search 1660 1959 1661 The `vpath' Directive1662 --------------------- 1960 4.5.2 The `vpath' Directive 1961 --------------------------- 1663 1962 1664 1963 Similar to the `VPATH' variable, but more selective, is the `vpath' … … 1738 2037 File: make.info, Node: Search Algorithm, Next: Commands/Search, Prev: Selective Search, Up: Directory Search 1739 2038 1740 How Directory Searches are Performed1741 ------------------------------------ 2039 4.5.3 How Directory Searches are Performed 2040 ------------------------------------------ 1742 2041 1743 2042 When a prerequisite is found through directory search, regardless of … … 1796 2095 File: make.info, Node: Commands/Search, Next: Implicit/Search, Prev: Search Algorithm, Up: Directory Search 1797 2096 1798 Writing Shell Commands with Directory Search1799 -------------------------------------------- 2097 4.5.4 Writing Shell Commands with Directory Search 2098 -------------------------------------------------- 1800 2099 1801 2100 When a prerequisite is found in another directory through directory … … 1817 2116 compilation by implicit rules; we use it here for consistency so it will 1818 2117 affect all C compilations uniformly; *note Variables Used by Implicit 1819 Rules: Implicit Variables. .)2118 Rules: Implicit Variables.) 1820 2119 1821 2120 Often the prerequisites include header files as well, which you do … … 1830 2129 File: make.info, Node: Implicit/Search, Next: Libraries/Search, Prev: Commands/Search, Up: Directory Search 1831 2130 1832 Directory Search and Implicit Rules1833 ----------------------------------- 2131 4.5.5 Directory Search and Implicit Rules 2132 ----------------------------------------- 1834 2133 1835 2134 The search through the directories specified in `VPATH' or with `vpath' … … 1851 2150 File: make.info, Node: Libraries/Search, Prev: Implicit/Search, Up: Directory Search 1852 2151 1853 Directory Search for Link Libraries1854 ----------------------------------- 2152 4.5.6 Directory Search for Link Libraries 2153 ----------------------------------------- 1855 2154 1856 2155 Directory search applies in a special way to libraries used with the … … 1890 2189 word in the list will be used. 1891 2190 1892 The default value for `.LIBPATTERNS' is "`lib%.so lib%.a'", which2191 The default value for `.LIBPATTERNS' is `lib%.so lib%.a', which 1893 2192 provides the default behavior described above. 1894 2193 … … 1899 2198 File: make.info, Node: Phony Targets, Next: Force Targets, Prev: Directory Search, Up: Rules 1900 2199 1901 Phony Targets1902 ============= 2200 4.6 Phony Targets 2201 ================= 1903 2202 1904 2203 A phony target is one that is not really the name of a file. It is … … 1916 2215 Because the `rm' command does not create a file named `clean', probably 1917 2216 no such file will ever exist. Therefore, the `rm' command will be 1918 executed every time you say `make clean'. 2217 executed every time you say `make clean'. 1919 2218 1920 2219 The phony target will cease to work if anything ever does create a … … 1946 2245 Another example of the usefulness of phony targets is in conjunction 1947 2246 with recursive invocations of `make' (for more information, see *Note 1948 Recursive Use of `make': Recursion ). In this case the makefile will2247 Recursive Use of `make': Recursion.). In this case the makefile will 1949 2248 often contain a variable which lists a number of subdirectories to be 1950 2249 built. One way to handle this is with one rule whose command is a … … 1952 2251 1953 2252 SUBDIRS = foo bar baz 1954 2253 1955 2254 subdirs: 1956 2255 for dir in $(SUBDIRS); do \ … … 1964 2263 it will do so even if `make' is invoked with the `-k' option, which is 1965 2264 unfortunate. Second, and perhaps more importantly, you cannot take 1966 advantage of the parallel build capabilities of make using this method,1967 since there is only one rule.2265 advantage of `make''s ability to build targets in parallel (*note 2266 Parallel Execution: Parallel.), since there is only one rule. 1968 2267 1969 2268 By declaring the subdirectories as phony targets (you must do this as … … 1972 2271 1973 2272 SUBDIRS = foo bar baz 1974 2273 1975 2274 .PHONY: subdirs $(SUBDIRS) 1976 2275 1977 2276 subdirs: $(SUBDIRS) 1978 2277 1979 2278 $(SUBDIRS): 1980 2279 $(MAKE) -C $@ 1981 2280 1982 2281 foo: baz 1983 2282 … … 2003 2302 all : prog1 prog2 prog3 2004 2303 .PHONY : all 2005 2304 2006 2305 prog1 : prog1.o utils.o 2007 2306 cc -o prog1 prog1.o utils.o 2008 2307 2009 2308 prog2 : prog2.o 2010 2309 cc -o prog2 prog2.o 2011 2310 2012 2311 prog3 : prog3.o sort.o utils.o 2013 2312 cc -o prog3 prog3.o sort.o utils.o 2014 2313 2015 2314 Now you can say just `make' to remake all three programs, or specify as 2016 arguments the ones to remake (as in `make prog1 prog3'). 2315 arguments the ones to remake (as in `make prog1 prog3'). Phoniness is 2316 not inherited: the prerequisites of a phony target are not themselves 2317 phony, unless explicitly declared to be so. 2017 2318 2018 2319 When one phony target is a prerequisite of another, it serves as a … … 2021 2322 2022 2323 .PHONY: cleanall cleanobj cleandiff 2023 2324 2024 2325 cleanall : cleanobj cleandiff 2025 2326 rm program 2026 2327 2027 2328 cleanobj : 2028 2329 rm *.o 2029 2330 2030 2331 cleandiff : 2031 2332 rm *.diff … … 2034 2335 File: make.info, Node: Force Targets, Next: Empty Targets, Prev: Phony Targets, Up: Rules 2035 2336 2036 Rules without Commands or Prerequisites2037 ======================================= 2337 4.7 Rules without Commands or Prerequisites 2338 =========================================== 2038 2339 2039 2340 If a rule has no prerequisites or commands, and the target of the rule … … 2063 2364 File: make.info, Node: Empty Targets, Next: Special Targets, Prev: Force Targets, Up: Rules 2064 2365 2065 Empty Target Files to Record Events2066 =================================== 2366 4.8 Empty Target Files to Record Events 2367 ======================================= 2067 2368 2068 2369 The "empty target" is a variant of the phony target; it is used to hold … … 2085 2386 lpr -p $? 2086 2387 touch print 2087 2388 2088 2389 With this rule, `make print' will execute the `lpr' command if either 2089 2390 source file has changed since the last `make print'. The automatic … … 2094 2395 File: make.info, Node: Special Targets, Next: Multiple Targets, Prev: Empty Targets, Up: Rules 2095 2396 2096 Special Built-in Target Names2097 ============================= 2397 4.9 Special Built-in Target Names 2398 ================================= 2098 2399 2099 2400 Certain names have special meanings if they appear as targets. … … 2148 2449 intermediate). 2149 2450 2451 `.SECONDEXPANSION' 2452 If `.SECONDEXPANSION' is mentioned as a target anywhere in the 2453 makefile, then all prerequisite lists defined _after_ it appears 2454 will be expanded a second time after all makefiles have been read 2455 in. *Note Secondary Expansion: Secondary Expansion. 2456 2457 The prerequisites of the special target `.SUFFIXES' are the list 2458 of suffixes to be used in checking for suffix rules. *Note 2459 Old-Fashioned Suffix Rules: Suffix Rules. 2460 2150 2461 `.DELETE_ON_ERROR' 2151 2462 If `.DELETE_ON_ERROR' is mentioned as a target anywhere in the … … 2234 2545 File: make.info, Node: Multiple Targets, Next: Multiple Rules, Prev: Special Targets, Up: Rules 2235 2546 2236 Multiple Targets in a Rule2237 ========================== 2547 4.10 Multiple Targets in a Rule 2548 =============================== 2238 2549 2239 2550 A rule with multiple targets is equivalent to writing many rules, each … … 2259 2570 bigoutput littleoutput : text.g 2260 2571 generate text.g -$(subst output,,$@) > $@ 2261 2572 2262 2573 is equivalent to 2263 2574 … … 2281 2592 File: make.info, Node: Multiple Rules, Next: Static Pattern, Prev: Multiple Targets, Up: Rules 2282 2593 2283 Multiple Rules for One Target2284 ============================= 2594 4.11 Multiple Rules for One Target 2595 ================================== 2285 2596 2286 2597 One file can be the target of several rules. All the prerequisites … … 2332 2643 File: make.info, Node: Static Pattern, Next: Double-Colon, Prev: Multiple Rules, Up: Rules 2333 2644 2334 Static Pattern Rules2335 ==================== 2645 4.12 Static Pattern Rules 2646 ========================= 2336 2647 2337 2648 "Static pattern rules" are rules which specify multiple targets and … … 2349 2660 File: make.info, Node: Static Usage, Next: Static versus Implicit, Prev: Static Pattern, Up: Static Pattern 2350 2661 2351 Syntax of Static Pattern Rules2352 ------------------------------ 2662 4.12.1 Syntax of Static Pattern Rules 2663 ------------------------------------- 2353 2664 2354 2665 Here is the syntax of a static pattern rule: … … 2398 2709 2399 2710 objects = foo.o bar.o 2400 2711 2401 2712 all: $(objects) 2402 2713 2403 2714 $(objects): %.o: %.c 2404 2715 $(CC) -c $(CFLAGS) $< -o $@ … … 2415 2726 2416 2727 files = foo.elc bar.o lose.o 2417 2728 2418 2729 $(filter %.o,$(files)): %.o: %.c 2419 2730 $(CC) -c $(CFLAGS) $< -o $@ … … 2427 2738 from `foo.el'. 2428 2739 2429 Another example shows how to use `$*' in static pattern rules: 2740 Another example shows how to use `$*' in static pattern rules: 2430 2741 2431 2742 bigoutput littleoutput : %output : text.g … … 2438 2749 File: make.info, Node: Static versus Implicit, Prev: Static Usage, Up: Static Pattern 2439 2750 2440 Static Pattern Rules versus Implicit Rules2441 ------------------------------------------ 2751 4.12.2 Static Pattern Rules versus Implicit Rules 2752 ------------------------------------------------- 2442 2753 2443 2754 A static pattern rule has much in common with an implicit rule defined … … 2475 2786 File: make.info, Node: Double-Colon, Next: Automatic Prerequisites, Prev: Static Pattern, Up: Rules 2476 2787 2477 Double-Colon Rules2478 ================== 2788 4.13 Double-Colon Rules 2789 ======================= 2479 2790 2480 2791 "Double-colon" rules are rules written with `::' instead of `:' after … … 2512 2823 File: make.info, Node: Automatic Prerequisites, Prev: Double-Colon, Up: Rules 2513 2824 2514 Generating Prerequisites Automatically2515 ====================================== 2825 4.14 Generating Prerequisites Automatically 2826 =========================================== 2516 2827 2517 2828 In the makefile for a program, many of the rules you need to write often … … 2525 2836 would have to write dozens of such rules in your makefile. And, you 2526 2837 must always be very careful to update the makefile every time you add 2527 or remove an `#include'. 2838 or remove an `#include'. 2528 2839 2529 2840 To avoid this hassle, most modern C compilers can write these rules … … 2544 2855 makefile, so it can never be considered an intermediate file by implicit 2545 2856 rule search. This means that `make' won't ever remove the file after 2546 using it; *note Chains of Implicit Rules: Chained Rules. .2857 using it; *note Chains of Implicit Rules: Chained Rules. 2547 2858 2548 2859 With old `make' programs, it was traditional practice to use this … … 2568 2879 2569 2880 %.d: %.c 2570 2881 @set -e; rm -f $@; \ 2571 2882 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ 2572 2883 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ … … 2575 2886 *Note Pattern Rules::, for information on defining pattern rules. The 2576 2887 `-e' flag to the shell causes it to exit immediately if the `$(CC)' 2577 command (or any other command) fails (exits with a nonzero status). 2888 command (or any other command) fails (exits with a nonzero status). 2578 2889 2579 2890 With the GNU C compiler, you may wish to use the `-MM' flag instead … … 2600 2911 2601 2912 sources = foo.c bar.c 2602 2913 2603 2914 include $(sources:.c=.d) 2604 2915 … … 2611 2922 2612 2923 Note that the `.d' files contain target definitions; you should be 2613 sure to place the `include' directive _after_ the first, default target2924 sure to place the `include' directive _after_ the first, default goal 2614 2925 in your makefiles or run the risk of having a random object file become 2615 the default target. *Note How Make Works::.2926 the default goal. *Note How Make Works::. 2616 2927 2617 2928 2618 2929 File: make.info, Node: Commands, Next: Using Variables, Prev: Rules, Up: Top 2619 2930 2620 Writing the Commands in Rules 2621 ***************************** 2622 2623 The commands of a rule consist of shell command lines to be executed one 2624 by one. Each command line must start with a tab, except that the first 2625 command line may be attached to the target-and-prerequisites line with a 2626 semicolon in between. Blank lines and lines of just comments may appear 2627 among the command lines; they are ignored. (But beware, an apparently 2628 "blank" line that begins with a tab is _not_ blank! It is an empty 2629 command; *note Empty Commands::.) 2931 5 Writing the Commands in Rules 2932 ******************************* 2933 2934 The commands of a rule consist of one or more shell command lines to be 2935 executed, one at a time, in the order they appear. Typically, the 2936 result of executing these commands is that the target of the rule is 2937 brought up to date. 2630 2938 2631 2939 Users use many different shell programs, but commands in makefiles … … 2633 2941 otherwise. *Note Command Execution: Execution. 2634 2942 2635 The shell that is in use determines whether comments can be written2636 on command lines, and what syntax they use. When the shell is2637 `/bin/sh', a `#' starts a comment that extends to the end of the line.2638 The `#' does not have to be at the beginning of a line. Text on a line2639 before a `#' is not part of the comment.2640 2641 2943 * Menu: 2642 2944 2945 * Command Syntax:: Command syntax features and pitfalls. 2643 2946 * Echoing:: How to control when commands are echoed. 2644 2947 * Execution:: How commands are executed. … … 2651 2954 2652 2955 2653 File: make.info, Node: Echoing, Next: Execution, Prev: Commands, Up: Commands 2654 2655 Command Echoing 2656 =============== 2956 File: make.info, Node: Command Syntax, Next: Echoing, Prev: Commands, Up: Commands 2957 2958 5.1 Command Syntax 2959 ================== 2960 2961 Makefiles have the unusual property that there are really two distinct 2962 syntaxes in one file. Most of the makefile uses `make' syntax (*note 2963 Writing Makefiles: Makefiles.). However, commands are meant to be 2964 interpreted by the shell and so they are written using shell syntax. 2965 The `make' program does not try to understand shell syntax: it performs 2966 only a very few specific translations on the content of the command 2967 before handing it to the shell. 2968 2969 Each command line must start with a tab, except that the first 2970 command line may be attached to the target-and-prerequisites line with a 2971 semicolon in between. _Any_ line in the makefile that begins with a 2972 tab and appears in a "rule context" (that is, after a rule has been 2973 started until another rule or variable definition) will be considered a 2974 command line for that rule. Blank lines and lines of just comments may 2975 appear among the command lines; they are ignored. 2976 2977 Some consequences of these rules include: 2978 2979 * A blank line that begins with a tab is not blank: it's an empty 2980 command (*note Empty Commands::). 2981 2982 * A comment in a command line is not a `make' comment; it will be 2983 passed to the shell as-is. Whether the shell treats it as a 2984 comment or not depends on your shell. 2985 2986 * A variable definition in a "rule context" which is indented by a 2987 tab as the first character on the line, will be considered a 2988 command line, not a `make' variable definition, and passed to the 2989 shell. 2990 2991 * A conditional expression (`ifdef', `ifeq', etc. *note Syntax of 2992 Conditionals: Conditional Syntax.) in a "rule context" which is 2993 indented by a tab as the first character on the line, will be 2994 considered a command line and be passed to the shell. 2995 2996 2997 * Menu: 2998 2999 * Splitting Lines:: Breaking long command lines for readability. 3000 * Variables in Commands:: Using `make' variables in commands. 3001 3002 3003 File: make.info, Node: Splitting Lines, Next: Variables in Commands, Prev: Command Syntax, Up: Command Syntax 3004 3005 5.1.1 Splitting Command Lines 3006 ----------------------------- 3007 3008 One of the few ways in which `make' does interpret command lines is 3009 checking for a backslash just before the newline. As in normal 3010 makefile syntax, a single command can be split into multiple lines in 3011 the makefile by placing a backslash before each newline. A sequence of 3012 lines like this is considered a single command, and one instance of the 3013 shell will be invoked to run it. 3014 3015 However, in contrast to how they are treated in other places in a 3016 makefile, backslash-newline pairs are _not_ removed from the command. 3017 Both the backslash and the newline characters are preserved and passed 3018 to the shell. How the backslash-newline is interpreted depends on your 3019 shell. If the first character of the next line after the 3020 backslash-newline is a tab, then that tab (and only that tab) is 3021 removed. Whitespace is never added to the command. 3022 3023 For example, this makefile: 3024 3025 all : 3026 @echo no\ 3027 space 3028 @echo no\ 3029 space 3030 @echo one \ 3031 space 3032 @echo one\ 3033 space 3034 3035 consists of four separate shell commands where the output is: 3036 3037 nospace 3038 nospace 3039 one space 3040 one space 3041 3042 As a more complex example, this makefile: 3043 3044 all : ; @echo 'hello \ 3045 world' ; echo "hello \ 3046 world" 3047 3048 will run one shell with a command script of: 3049 3050 echo 'hello \ 3051 world' ; echo "hello \ 3052 world" 3053 3054 which, according to shell quoting rules, will yield the following 3055 output: 3056 3057 hello \ 3058 world 3059 hello world 3060 3061 Notice how the backslash/newline pair was removed inside the string 3062 quoted with double quotes (`"..."'), but not from the string quoted 3063 with single quotes (`'...''). This is the way the default shell 3064 (`/bin/sh') handles backslash/newline pairs. If you specify a 3065 different shell in your makefiles it may treat them differently. 3066 3067 Sometimes you want to split a long line inside of single quotes, but 3068 you don't want the backslash-newline to appear in the quoted content. 3069 This is often the case when passing scripts to languages such as Perl, 3070 where extraneous backslashes inside the script can change its meaning 3071 or even be a syntax error. One simple way of handling this is to place 3072 the quoted string, or even the entire command, into a `make' variable 3073 then use the variable in the command. In this situation the newline 3074 quoting rules for makefiles will be used, and the backslash-newline 3075 will be removed. If we rewrite our example above using this method: 3076 3077 HELLO = 'hello \ 3078 world' 3079 3080 all : ; @echo $(HELLO) 3081 3082 we will get output like this: 3083 3084 hello world 3085 3086 If you like, you can also use target-specific variables (*note 3087 Target-specific Variable Values: Target-specific.) to obtain a tighter 3088 correspondence between the variable and the command that uses it. 3089 3090 3091 File: make.info, Node: Variables in Commands, Prev: Splitting Lines, Up: Command Syntax 3092 3093 5.1.2 Using Variables in Commands 3094 --------------------------------- 3095 3096 The other way in which `make' processes commands is by expanding any 3097 variable references in them (*note Basics of Variable References: 3098 Reference.). This occurs after make has finished reading all the 3099 makefiles and the target is determined to be out of date; so, the 3100 commands for targets which are not rebuilt are never expanded. 3101 3102 Variable and function references in commands have identical syntax 3103 and semantics to references elsewhere in the makefile. They also have 3104 the same quoting rules: if you want a dollar sign to appear in your 3105 command, you must double it (`$$'). For shells like the default shell, 3106 that use dollar signs to introduce variables, it's important to keep 3107 clear in your mind whether the variable you want to reference is a 3108 `make' variable (use a single dollar sign) or a shell variable (use two 3109 dollar signs). For example: 3110 3111 LIST = one two three 3112 all: 3113 for i in $(LIST); do \ 3114 echo $$i; \ 3115 done 3116 3117 results in the following command being passed to the shell: 3118 3119 for i in one two three; do \ 3120 echo $i; \ 3121 done 3122 3123 which generates the expected result: 3124 3125 one 3126 two 3127 three 3128 3129 3130 File: make.info, Node: Echoing, Next: Execution, Prev: Command Syntax, Up: Commands 3131 3132 5.2 Command Echoing 3133 =================== 2657 3134 2658 3135 Normally `make' prints each command line before it is executed. We … … 2683 3160 File: make.info, Node: Execution, Next: Parallel, Prev: Echoing, Up: Commands 2684 3161 2685 Command Execution2686 ================= 3162 5.3 Command Execution 3163 ===================== 2687 3164 2688 3165 When it is time to execute commands to update a target, they are 2689 executed by making a new subshell for each line. (In practice, `make' 2690 may take shortcuts that do not affect the results.) 2691 2692 *Please note:* this implies that shell commands such as `cd' that 2693 set variables local to each process will not affect the following 2694 command lines. (1) If you want to use `cd' to affect the next command, 2695 put the two on a single line with a semicolon between them. Then 2696 `make' will consider them a single command and pass them, together, to 2697 a shell which will execute them in sequence. For example: 3166 executed by invoking a new subshell for each command line. (In 3167 practice, `make' may take shortcuts that do not affect the results.) 3168 3169 *Please note:* this implies that setting shell variables and 3170 invoking shell commands such as `cd' that set a context local to each 3171 process will not affect the following command lines.(1) If you want to 3172 use `cd' to affect the next statement, put both statements in a single 3173 command line. Then `make' will invoke one shell to run the entire 3174 line, and the shell will execute the statements in sequence. For 3175 example: 2698 3176 2699 3177 foo : bar/lose 2700 cd bar; gobble lose > ../foo 2701 2702 If you would like to split a single shell command into multiple 2703 lines of text, you must use a backslash at the end of all but the last 2704 subline. Such a sequence of lines is combined into a single line, by 2705 deleting the backslash-newline sequences, before passing it to the 2706 shell. Thus, the following is equivalent to the preceding example: 2707 2708 foo : bar/lose 2709 cd bar; \ 2710 gobble lose > ../foo 2711 2712 The program used as the shell is taken from the variable `SHELL'. 2713 By default, the program `/bin/sh' is used. 3178 cd $(@D) && gobble $(@F) > ../$@ 3179 3180 Here we use the shell AND operator (`&&') so that if the `cd' command 3181 fails, the script will fail without trying to invoke the `gobble' 3182 command in the wrong directory, which could cause problems (in this 3183 case it would certainly cause `../foo' to be truncated, at least). 3184 3185 * Menu: 3186 3187 * Choosing the Shell:: How `make' chooses the shell used 3188 to run commands. 3189 3190 ---------- Footnotes ---------- 3191 3192 (1) On MS-DOS, the value of current working directory is *global*, so 3193 changing it _will_ affect the following command lines on those systems. 3194 3195 3196 File: make.info, Node: Choosing the Shell, Prev: Execution, Up: Execution 3197 3198 5.3.1 Choosing the Shell 3199 ------------------------ 3200 3201 The program used as the shell is taken from the variable `SHELL'. If 3202 this variable is not set in your makefile, the program `/bin/sh' is 3203 used as the shell. 3204 3205 Unlike most variables, the variable `SHELL' is never set from the 3206 environment. This is because the `SHELL' environment variable is used 3207 to specify your personal choice of shell program for interactive use. 3208 It would be very bad for personal choices like this to affect the 3209 functioning of makefiles. *Note Variables from the Environment: 3210 Environment. 3211 3212 Furthermore, when you do set `SHELL' in your makefile that value is 3213 _not_ exported in the environment to commands that `make' invokes. 3214 Instead, the value inherited from the user's environment, if any, is 3215 exported. You can override this behavior by explicitly exporting 3216 `SHELL' (*note Communicating Variables to a Sub-`make': 3217 Variables/Recursion.), forcing it to be passed in the environment to 3218 commands. 3219 3220 However, on MS-DOS and MS-Windows the value of `SHELL' in the 3221 environment *is* used, since on those systems most users do not set 3222 this variable, and therefore it is most likely set specifically to be 3223 used by `make'. On MS-DOS, if the setting of `SHELL' is not suitable 3224 for `make', you can set the variable `MAKESHELL' to the shell that 3225 `make' should use; if set it will be used as the shell instead of the 3226 value of `SHELL'. 3227 3228 Choosing a Shell in DOS and Windows 3229 ................................... 3230 3231 Choosing a shell in MS-DOS and MS-Windows is much more complex than on 3232 other systems. 2714 3233 2715 3234 On MS-DOS, if `SHELL' is not set, the value of the variable … … 2757 3276 2758 3277 The effect of the above DOS-specific processing is that a Makefile 2759 that says `SHELL = /bin/sh' (as many Unix makefiles do), will work on 2760 MS-DOS unaltered if you have e.g. `sh.exe' installed in some directory 2761 along your `PATH'. 2762 2763 Unlike most variables, the variable `SHELL' is never set from the 2764 environment. This is because the `SHELL' environment variable is used 2765 to specify your personal choice of shell program for interactive use. 2766 It would be very bad for personal choices like this to affect the 2767 functioning of makefiles. *Note Variables from the Environment: 2768 Environment. However, on MS-DOS and MS-Windows the value of `SHELL' in 2769 the environment *is* used, since on those systems most users do not set 2770 this variable, and therefore it is most likely set specifically to be 2771 used by `make'. On MS-DOS, if the setting of `SHELL' is not suitable 2772 for `make', you can set the variable `MAKESHELL' to the shell that 2773 `make' should use; this will override the value of `SHELL'. 2774 2775 ---------- Footnotes ---------- 2776 2777 (1) On MS-DOS, the value of current working directory is *global*, 2778 so changing it _will_ affect the following command lines on those 2779 systems. 3278 that contains `SHELL = /bin/sh' (as many Unix makefiles do), will work 3279 on MS-DOS unaltered if you have e.g. `sh.exe' installed in some 3280 directory along your `PATH'. 2780 3281 2781 3282 2782 3283 File: make.info, Node: Parallel, Next: Errors, Prev: Execution, Up: Commands 2783 3284 2784 Parallel Execution2785 ================== 3285 5.4 Parallel Execution 3286 ====================== 2786 3287 2787 3288 GNU `make' knows how to execute several commands at once. Normally, … … 2809 3310 streams of all but one running command. This means that attempting to 2810 3311 read from standard input will usually be a fatal error (a `Broken pipe' 2811 signal) for most child processes if there are several. 3312 signal) for most child processes if there are several. 2812 3313 2813 3314 It is unpredictable which command will have a valid standard input … … 2858 3359 File: make.info, Node: Errors, Next: Interrupts, Prev: Parallel, Up: Commands 2859 3360 2860 Errors in Commands2861 ================== 3361 5.5 Errors in Commands 3362 ====================== 2862 3363 2863 3364 After each shell command returns, `make' looks at its exit status. If … … 2917 3418 program as possible, perhaps to find several independent problems so 2918 3419 that you can correct them all before the next attempt to compile. This 2919 is why Emacs' `compile' command passes the `-k' flag by default. 3420 is why Emacs' `compile' command passes the `-k' flag by default. 2920 3421 2921 3422 Usually when a command fails, if it has changed the target file at … … 2934 3435 File: make.info, Node: Interrupts, Next: Recursion, Prev: Errors, Up: Commands 2935 3436 2936 Interrupting or Killing `make'2937 ============================== 3437 5.6 Interrupting or Killing `make' 3438 ================================== 2938 3439 2939 3440 If `make' gets a fatal signal while a command is executing, it may … … 2965 3466 File: make.info, Node: Recursion, Next: Sequences, Prev: Interrupts, Up: Commands 2966 3467 2967 Recursive Use of `make'2968 ======================= 3468 5.7 Recursive Use of `make' 3469 =========================== 2969 3470 2970 3471 Recursive use of `make' means using `make' as a command in a makefile. … … 2982 3483 subsystem: 2983 3484 $(MAKE) -C subdir 2984 3485 2985 3486 You can write recursive `make' commands just by copying this example, 2986 3487 but there are many things to know about how they work and why, and about … … 3012 3513 File: make.info, Node: MAKE Variable, Next: Variables/Recursion, Prev: Recursion, Up: Recursion 3013 3514 3014 How the `MAKE' Variable Works3015 ----------------------------- 3515 5.7.1 How the `MAKE' Variable Works 3516 ----------------------------------- 3016 3517 3017 3518 Recursive `make' commands should always use the variable `MAKE', not … … 3025 3526 is `cd subdir && /bin/make'. If you use a special version of `make' to 3026 3527 run the top-level makefile, the same special version will be executed 3027 for recursive invocations. 3528 for recursive invocations. 3028 3529 3029 3530 As a special feature, using the variable `MAKE' in the commands of a … … 3043 3544 file named `subsystem' and do nothing else. What you really want it to 3044 3545 do is run `cd subdir && make -t'; but that would require executing the 3045 command, and `-t' says not to execute commands. 3546 command, and `-t' says not to execute commands. 3046 3547 3047 3548 The special feature makes this do what you want: whenever a command … … 3057 3558 File: make.info, Node: Variables/Recursion, Next: Options/Recursion, Prev: MAKE Variable, Up: Recursion 3058 3559 3059 Communicating Variables to a Sub-`make'3060 --------------------------------------- 3560 5.7.2 Communicating Variables to a Sub-`make' 3561 --------------------------------------------- 3061 3562 3062 3563 Variable values of the top-level `make' can be passed to the sub-`make' … … 3077 3578 characters other than letters, numbers, and underscores. 3078 3579 3079 The special variables `SHELL' and `MAKEFLAGS' are always exported 3080 (unless you unexport them). `MAKEFILES' is exported if you set it to 3081 anything. 3580 The value of the `make' variable `SHELL' is not exported. Instead, 3581 the value of the `SHELL' variable from the invoking environment is 3582 passed to the sub-`make'. You can force `make' to export its value for 3583 `SHELL' by using the `export' directive, described below. *Note 3584 Choosing the Shell::. 3585 3586 The special variable `MAKEFLAGS' is always exported (unless you 3587 unexport it). `MAKEFILES' is exported if you set it to anything. 3082 3588 3083 3589 `make' automatically passes down variable values that were defined … … 3154 3660 `.EXPORT_ALL_VARIABLES' instead of using the `export' directive. This 3155 3661 will be ignored by old `make's, while the `export' directive will cause 3156 a syntax error. 3662 a syntax error. 3157 3663 3158 3664 Likewise, you can use `unexport' by itself to tell `make' _not_ to … … 3187 3693 File: make.info, Node: Options/Recursion, Next: -w Option, Prev: Variables/Recursion, Up: Recursion 3188 3694 3189 Communicating Options to a Sub-`make'3190 ------------------------------------- 3695 5.7.3 Communicating Options to a Sub-`make' 3696 ------------------------------------------- 3191 3697 3192 3698 Flags such as `-s' and `-k' are passed automatically to the sub-`make' … … 3292 3798 File: make.info, Node: -w Option, Prev: Options/Recursion, Up: Recursion 3293 3799 3294 The `--print-directory' Option3295 ------------------------------ 3800 5.7.4 The `--print-directory' Option 3801 ------------------------------------ 3296 3802 3297 3803 If you use several levels of recursive `make' invocations, the `-w' or … … 3318 3824 File: make.info, Node: Sequences, Next: Empty Commands, Prev: Recursion, Up: Commands 3319 3825 3320 Defining Canned Command Sequences3321 ================================= 3826 5.8 Defining Canned Command Sequences 3827 ===================================== 3322 3828 3323 3829 When the same sequence of commands is useful in making various targets, … … 3333 3839 mv y.tab.c $@ 3334 3840 endef 3335 3841 3336 3842 Here `run-yacc' is the name of the variable being defined; `endef' 3337 3843 marks the end of the definition; the lines in between are the commands. … … 3393 3899 File: make.info, Node: Empty Commands, Prev: Sequences, Up: Commands 3394 3900 3395 Using Empty Commands3396 ==================== 3901 5.9 Using Empty Commands 3902 ======================== 3397 3903 3398 3904 It is sometimes useful to define commands which do nothing. This is … … 3422 3928 File: make.info, Node: Using Variables, Next: Conditionals, Prev: Commands, Up: Top 3423 3929 3424 How to Use Variables3425 ******************** 3930 6 How to Use Variables 3931 ********************** 3426 3932 3427 3933 A "variable" is a name defined in a makefile to represent a string of … … 3429 3935 explicit request into targets, prerequisites, commands, and other parts 3430 3936 of the makefile. (In some other versions of `make', variables are 3431 called "macros".) 3937 called "macros".) 3432 3938 3433 3939 Variables and functions in all parts of a makefile are expanded when … … 3484 3990 File: make.info, Node: Reference, Next: Flavors, Prev: Using Variables, Up: Using Variables 3485 3991 3486 Basics of Variable References3487 ============================= 3992 6.1 Basics of Variable References 3993 ================================= 3488 3994 3489 3995 To substitute a variable's value, write a dollar sign followed by the … … 3501 4007 program : $(objects) 3502 4008 cc -o program $(objects) 3503 4009 3504 4010 $(objects) : defs.h 3505 4011 … … 3524 4030 File: make.info, Node: Flavors, Next: Advanced, Prev: Reference, Up: Using Variables 3525 4031 3526 The Two Flavors of Variables3527 ============================ 4032 6.2 The Two Flavors of Variables 4033 ================================ 3528 4034 3529 4035 There are two ways that a variable in GNU `make' can have a value; we … … 3545 4051 bar = $(ugh) 3546 4052 ugh = Huh? 3547 4053 3548 4054 all:;echo $(foo) 3549 4055 … … 3565 4071 3566 4072 because it will cause an infinite loop in the variable expansion. 3567 (Actually `make' detects the infinite loop and reports an error.) 4073 (Actually `make' detects the infinite loop and reports an error.) 3568 4074 3569 4075 Another disadvantage is that any functions (*note Functions for … … 3606 4112 3607 4113 ifeq (0,${MAKELEVEL}) 3608 cur-dir := $(shell pwd)3609 4114 whoami := $(shell whoami) 3610 4115 host-type := $(shell arch) … … 3616 4121 3617 4122 ${subdirs}: 3618 ${MAKE} cur-dir=${cur-dir}/$@-C $@ all4123 ${MAKE} -C $@ all 3619 4124 3620 4125 Simply expanded variables generally make complicated makefile … … 3670 4175 File: make.info, Node: Advanced, Next: Values, Prev: Flavors, Up: Using Variables 3671 4176 3672 Advanced Features for Reference to Variables3673 ============================================ 4177 6.3 Advanced Features for Reference to Variables 4178 ================================================ 3674 4179 3675 4180 This section describes some advanced features you can use to reference … … 3685 4190 File: make.info, Node: Substitution Refs, Next: Computed Names, Prev: Advanced, Up: Advanced 3686 4191 3687 Substitution References3688 ----------------------- 4192 6.3.1 Substitution References 4193 ----------------------------- 3689 4194 3690 4195 A "substitution reference" substitutes the value of a variable with … … 3718 4223 3719 4224 For example: 3720 4225 3721 4226 foo := a.o b.o c.o 3722 4227 bar := $(foo:%.o=%.c) … … 3727 4232 File: make.info, Node: Computed Names, Prev: Substitution Refs, Up: Advanced 3728 4233 3729 Computed Variable Names3730 ----------------------- 4234 6.3.2 Computed Variable Names 4235 ----------------------------- 3731 4236 3732 4237 Computed variable names are a complicated concept needed only for … … 3799 4304 a_dirs := dira dirb 3800 4305 1_dirs := dir1 dir2 3801 4306 3802 4307 a_files := filea fileb 3803 4308 1_files := file1 file2 3804 4309 3805 4310 ifeq "$(use_a)" "yes" 3806 4311 a1 := a … … 3808 4313 a1 := 1 3809 4314 endif 3810 4315 3811 4316 ifeq "$(use_dirs)" "yes" 3812 4317 df := dirs … … 3814 4319 df := files 3815 4320 endif 3816 4321 3817 4322 dirs := $($(a1)_$(df)) 3818 4323 … … 3824 4329 a_objects := a.o b.o c.o 3825 4330 1_objects := 1.o 2.o 3.o 3826 4331 3827 4332 sources := $($(a1)_objects:.o=.c) 3828 4333 … … 3840 4345 func := strip 3841 4346 endif 3842 4347 3843 4348 bar := a d b g q c 3844 4349 3845 4350 foo := $($(func) $(bar)) 3846 4351 … … 3870 4375 File: make.info, Node: Values, Next: Setting, Prev: Advanced, Up: Using Variables 3871 4376 3872 How Variables Get Their Values3873 ============================== 4377 6.4 How Variables Get Their Values 4378 ================================== 3874 4379 3875 4380 Variables can get values in several different ways: … … 3895 4400 File: make.info, Node: Setting, Next: Appending, Prev: Values, Up: Using Variables 3896 4401 3897 Setting Variables3898 ================= 4402 6.5 Setting Variables 4403 ===================== 3899 4404 3900 4405 To set a variable from the makefile, write a line starting with the … … 3948 4453 File: make.info, Node: Appending, Next: Override Directive, Prev: Setting, Up: Using Variables 3949 4454 3950 Appending More Text to Variables3951 ================================ 4455 6.6 Appending More Text to Variables 4456 ==================================== 3952 4457 3953 4458 Often it is useful to add more text to the value of a variable already … … 3984 4489 it a simply-expanded variable, `+=' adds to that simply-expanded 3985 4490 definition, and expands the new text before appending it to the old 3986 value just as `:=' does ( *note Setting Variables: Setting., for a full3987 explanation of `:='). In fact,4491 value just as `:=' does (see *Note Setting Variables: Setting, for a 4492 full explanation of `:='). In fact, 3988 4493 3989 4494 variable := value … … 3991 4496 3992 4497 is exactly equivalent to: 4498 3993 4499 3994 4500 variable := value … … 4023 4529 The first line defines the `CFLAGS' variable with a reference to another 4024 4530 variable, `includes'. (`CFLAGS' is used by the rules for C 4025 compilation; *note Catalogue of Implicit Rules: Catalogue of Rules. .)4531 compilation; *note Catalogue of Implicit Rules: Catalogue of Rules.) 4026 4532 Using `=' for the definition makes `CFLAGS' a recursively-expanded 4027 4533 variable, meaning `$(includes) -O' is _not_ expanded when `make' … … 4045 4551 File: make.info, Node: Override Directive, Next: Defining, Prev: Appending, Up: Using Variables 4046 4552 4047 The `override' Directive4048 ======================== 4553 6.7 The `override' Directive 4554 ============================ 4049 4555 4050 4556 If a variable has been set with a command argument (*note Overriding … … 4089 4595 File: make.info, Node: Defining, Next: Environment, Prev: Override Directive, Up: Using Variables 4090 4596 4091 Defining Variables Verbatim4092 =========================== 4597 6.8 Defining Variables Verbatim 4598 =============================== 4093 4599 4094 4600 Another way to set the value of a variable is to use the `define' … … 4148 4654 File: make.info, Node: Environment, Next: Target-specific, Prev: Defining, Up: Using Variables 4149 4655 4150 Variables from the Environment4151 ============================== 4656 6.9 Variables from the Environment 4657 ================================== 4152 4658 4153 4659 Variables in `make' can come from the environment in which `make' is 4154 4660 run. Every environment variable that `make' sees when it starts up is 4155 transformed into a `make' variable with the same name and value. But4156 an explicit assignment in the makefile, or with a command argument, 4157 overrides the environment. (If the `-e' flag is specified, then values 4158 from the environment override assignments in the makefile. *Note 4159 Summary of Options: Options Summary. But this is not recommended4661 transformed into a `make' variable with the same name and value. 4662 However, an explicit assignment in the makefile, or with a command 4663 argument, overrides the environment. (If the `-e' flag is specified, 4664 then values from the environment override assignments in the makefile. 4665 *Note Summary of Options: Options Summary. But this is not recommended 4160 4666 practice.) 4161 4667 … … 4164 4670 you prefer. This is safe for variables with standard or conventional 4165 4671 meanings because you know that no makefile will use them for other 4166 things. ( But this is not totally reliable; some makefiles set `CFLAGS'4167 explicitly and therefore are not affected by the value in the4672 things. (Note this is not totally reliable; some makefiles set 4673 `CFLAGS' explicitly and therefore are not affected by the value in the 4168 4674 environment.) 4169 4675 4170 When `make' is invoked recursively, variables defined in the outer4171 invocation can be passed to inner invocations through the environment 4172 (*note Recursive Use of `make': Recursion.). By default, only 4173 variables that came from the environment or the command line are passed 4174 to recursive invocations. You can use the `export' directive to pass 4175 other variables. *Note Communicating Variables to a Sub-`make': 4176 Variables /Recursion, for full details.4676 When `make' runs a command script, variables defined in the makefile 4677 are placed into the environment of that command. This allows you to 4678 pass values to sub-`make' invocations (*note Recursive Use of `make': 4679 Recursion.). By default, only variables that came from the environment 4680 or the command line are passed to recursive invocations. You can use 4681 the `export' directive to pass other variables. *Note Communicating 4682 Variables to a Sub-`make': Variables/Recursion, for full details. 4177 4683 4178 4684 Other use of variables from the environment is not recommended. It … … 4185 4691 which is normally present in the environment to specify the user's 4186 4692 choice of interactive shell. It would be very undesirable for this 4187 choice to affect `make'. So `make' ignores the environment value of 4188 `SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not 4189 set. *Note Special handling of SHELL on MS-DOS: Execution.) 4693 choice to affect `make'; so, `make' handles the `SHELL' environment 4694 variable in a special way; see *Note Choosing the Shell::. 4190 4695 4191 4696 4192 4697 File: make.info, Node: Target-specific, Next: Pattern-specific, Prev: Environment, Up: Using Variables 4193 4698 4194 Target-specific Variable Values4195 =============================== 4699 6.10 Target-specific Variable Values 4700 ==================================== 4196 4701 4197 4702 Variable values in `make' are usually global; that is, they are the … … 4262 4767 File: make.info, Node: Pattern-specific, Prev: Target-specific, Up: Using Variables 4263 4768 4264 Pattern-specific Variable Values4265 ================================ 4769 6.11 Pattern-specific Variable Values 4770 ===================================== 4266 4771 4267 4772 In addition to target-specific variable values (*note Target-specific … … 4300 4805 File: make.info, Node: Conditionals, Next: Functions, Prev: Using Variables, Up: Top 4301 4806 4302 Conditional Parts of Makefiles4303 ****************************** 4807 7 Conditional Parts of Makefiles 4808 ******************************** 4304 4809 4305 4810 A "conditional" causes part of a makefile to be obeyed or ignored … … 4319 4824 File: make.info, Node: Conditional Example, Next: Conditional Syntax, Prev: Conditionals, Up: Conditionals 4320 4825 4321 Example of a Conditional4322 ======================== 4826 7.1 Example of a Conditional 4827 ============================ 4323 4828 4324 4829 The following example of a conditional tells `make' to use one set of … … 4331 4836 libs_for_gcc = -lgnu 4332 4837 normal_libs = 4333 4838 4334 4839 foo: $(objects) 4335 4840 ifeq ($(CC),gcc) … … 4381 4886 libs_for_gcc = -lgnu 4382 4887 normal_libs = 4383 4888 4384 4889 ifeq ($(CC),gcc) 4385 4890 libs=$(libs_for_gcc) … … 4387 4892 libs=$(normal_libs) 4388 4893 endif 4389 4894 4390 4895 foo: $(objects) 4391 4896 $(CC) -o foo $(objects) $(libs) … … 4394 4899 File: make.info, Node: Conditional Syntax, Next: Testing Flags, Prev: Conditional Example, Up: Conditionals 4395 4900 4396 Syntax of Conditionals4397 ====================== 4901 7.2 Syntax of Conditionals 4902 ========================== 4398 4903 4399 4904 The syntax of a simple conditional with no `else' is as follows: … … 4415 4920 endif 4416 4921 4417 If the condition is true, TEXT-IF-TRUE is used; otherwise, 4418 TEXT-IF-FALSE is used instead. The TEXT-IF-FALSE can be any number of 4419 lines of text. 4922 or: 4923 4924 CONDITIONAL-DIRECTIVE 4925 TEXT-IF-ONE-IS-TRUE 4926 else CONDITIONAL-DIRECTIVE 4927 TEXT-IF-TRUE 4928 else 4929 TEXT-IF-FALSE 4930 endif 4931 4932 There can be as many "`else' CONDITIONAL-DIRECTIVE" clauses as 4933 necessary. Once a given condition is true, TEXT-IF-TRUE is used and no 4934 other clause is used; if no condition is true then TEXT-IF-FALSE is 4935 used. The TEXT-IF-TRUE and TEXT-IF-FALSE can be any number of lines of 4936 text. 4420 4937 4421 4938 The syntax of the CONDITIONAL-DIRECTIVE is the same whether the 4422 conditional is simple or complex. There are four different directives 4423 that test different conditions. Here is a table of them: 4939 conditional is simple or complex; after an `else' or not. There are 4940 four different directives that test different conditions. Here is a 4941 table of them: 4424 4942 4425 4943 `ifeq (ARG1, ARG2)' … … 4457 4975 4458 4976 `ifdef VARIABLE-NAME' 4459 If the variable VARIABLE-NAME has a non-empty value, the 4460 TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any, 4461 is effective. Variables that have never been defined have an 4462 empty value. The variable VARIABLE-NAME is itself expanded, so it 4463 could be a variable or function that expands to the name of a 4464 variable. 4977 The `ifdef' form takes the _name_ of a variable as its argument, 4978 not a reference to a variable. The value of that variable has a 4979 non-empty value, the TEXT-IF-TRUE is effective; otherwise, the 4980 TEXT-IF-FALSE, if any, is effective. Variables that have never 4981 been defined have an empty value. The text VARIABLE-NAME is 4982 expanded, so it could be a variable or function that expands to 4983 the name of a variable. For example: 4984 4985 bar = true 4986 foo = bar 4987 ifdef $(foo) 4988 frobozz = yes 4989 endif 4990 4991 The variable reference `$(foo)' is expanded, yielding `bar', which 4992 is considered to be the name of a variable. The variable `bar' is 4993 not expanded, but its value is examined to determine if it is 4994 non-empty. 4465 4995 4466 4996 Note that `ifdef' only tests whether a variable has a value. It … … 4492 5022 If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE 4493 5023 is effective; otherwise, the TEXT-IF-FALSE, if any, is effective. 5024 The rules for expansion and testing of VARIABLE-NAME are identical 5025 to the `ifdef' directive. 4494 5026 4495 5027 Extra spaces are allowed and ignored at the beginning of the … … 4526 5058 File: make.info, Node: Testing Flags, Prev: Conditional Syntax, Up: Conditionals 4527 5059 4528 Conditionals that Test Flags4529 ============================ 5060 7.3 Conditionals that Test Flags 5061 ================================ 4530 5062 4531 5063 You can write a conditional that tests `make' command flags such as … … 4557 5089 File: make.info, Node: Functions, Next: Running, Prev: Conditionals, Up: Top 4558 5090 4559 Functions for Transforming Text4560 ******************************* 5091 8 Functions for Transforming Text 5092 ********************************* 4561 5093 4562 5094 "Functions" allow you to do text processing in the makefile to compute … … 4572 5104 * Text Functions:: General-purpose text manipulation functions. 4573 5105 * File Name Functions:: Functions for manipulating file names. 5106 * Conditional Functions:: Functions that implement conditions. 4574 5107 * Foreach Function:: Repeat some text with controlled variation. 4575 * If Function:: Conditionally expand a value.4576 5108 * Call Function:: Expand a user-defined function. 4577 5109 * Value Function:: Return the un-expanded value of a variable. 4578 5110 * Eval Function:: Evaluate the arguments as makefile syntax. 4579 5111 * Origin Function:: Find where a variable got its value. 5112 * Flavor Function:: Find out the flavor of a variable. 4580 5113 * Shell Function:: Substitute the output of a shell command. 4581 5114 * Make Control Functions:: Functions that control how make runs. … … 4584 5117 File: make.info, Node: Syntax of Functions, Next: Text Functions, Prev: Functions, Up: Functions 4585 5118 4586 Function Call Syntax4587 ==================== 5119 8.1 Function Call Syntax 5120 ======================== 4588 5121 4589 5122 A function call resembles a variable reference. It looks like this: … … 4638 5171 File: make.info, Node: Text Functions, Next: File Name Functions, Prev: Syntax of Functions, Up: Functions 4639 5172 4640 Functions for String Substitution and Analysis4641 ============================================== 5173 8.2 Functions for String Substitution and Analysis 5174 ================================================== 4642 5175 4643 5176 Here are some functions that operate on strings: … … 4804 5337 `$(wordlist S,E,TEXT)' 4805 5338 Returns the list of words in TEXT starting with word S and ending 4806 with word E (inclusive). The legitimate values of S and E start4807 from 1. If S is bigger than the number of words in TEXT, the4808 value is empty. If E is bigger than the number of words in TEXT,4809 words up to the end of TEXT are returned. If S is greater than E,4810 nothing is returned. For example,5339 with word E (inclusive). The legitimate values of S start from 1; 5340 E may start from 0. If S is bigger than the number of words in 5341 TEXT, the value is empty. If E is bigger than the number of words 5342 in TEXT, words up to the end of TEXT are returned. If S is 5343 greater than E, nothing is returned. For example, 4811 5344 4812 5345 $(wordlist 2, 3, foo bar baz) … … 4830 5363 same as `$(word 1,TEXT)', the `firstword' function is retained for 4831 5364 its simplicity. 5365 5366 `$(lastword NAMES...)' 5367 The argument NAMES is regarded as a series of names, separated by 5368 whitespace. The value is the last name in the series. 5369 5370 For example, 5371 5372 $(lastword foo bar) 5373 5374 produces the result `bar'. Although `$(lastword TEXT)' is the 5375 same as `$(word $(words TEXT),TEXT)', the `lastword' function was 5376 added for its simplicity and better performance. 4832 5377 4833 5378 Here is a realistic example of the use of `subst' and `patsubst'. … … 4858 5403 4859 5404 4860 File: make.info, Node: File Name Functions, Next: Foreach Function, Prev: Text Functions, Up: Functions4861 4862 Functions for File Names4863 ======================== 5405 File: make.info, Node: File Name Functions, Next: Conditional Functions, Prev: Text Functions, Up: Functions 5406 5407 8.3 Functions for File Names 5408 ============================ 4864 5409 4865 5410 Several of the built-in expansion functions relate specifically to … … 4970 5515 File Names: Wildcards. 4971 5516 4972 4973 File: make.info, Node: Foreach Function, Next: If Function, Prev: File Name Functions, Up: Functions 4974 4975 The `foreach' Function 4976 ====================== 5517 `$(realpath NAMES...)' 5518 For each file name in NAMES return the canonical absolute name. A 5519 canonical name does not contain any `.' or `..' components, nor 5520 any repeated path separators (`/') or symlinks. In case of a 5521 failure the empty string is returned. Consult the `realpath(3)' 5522 documentation for a list of possible failure causes. 5523 5524 `$(abspath NAMES...)' 5525 For each file name in NAMES return an absolute name that does not 5526 contain any `.' or `..' components, nor any repeated path 5527 separators (`/'). Note that, in contrast to `realpath' function, 5528 `abspath' does not resolve symlinks and does not require the file 5529 names to refer to an existing file or directory. Use the 5530 `wildcard' function to test for existence. 5531 5532 5533 File: make.info, Node: Conditional Functions, Next: Foreach Function, Prev: File Name Functions, Up: Functions 5534 5535 8.4 Functions for Conditionals 5536 ============================== 5537 5538 There are three functions that provide conditional expansion. A key 5539 aspect of these functions is that not all of the arguments are expanded 5540 initially. Only those arguments which need to be expanded, will be 5541 expanded. 5542 5543 `$(if CONDITION,THEN-PART[,ELSE-PART])' 5544 The `if' function provides support for conditional expansion in a 5545 functional context (as opposed to the GNU `make' makefile 5546 conditionals such as `ifeq' (*note Syntax of Conditionals: 5547 Conditional Syntax.). 5548 5549 The first argument, CONDITION, first has all preceding and 5550 trailing whitespace stripped, then is expanded. If it expands to 5551 any non-empty string, then the condition is considered to be true. 5552 If it expands to an empty string, the condition is considered to 5553 be false. 5554 5555 If the condition is true then the second argument, THEN-PART, is 5556 evaluated and this is used as the result of the evaluation of the 5557 entire `if' function. 5558 5559 If the condition is false then the third argument, ELSE-PART, is 5560 evaluated and this is the result of the `if' function. If there is 5561 no third argument, the `if' function evaluates to nothing (the 5562 empty string). 5563 5564 Note that only one of the THEN-PART or the ELSE-PART will be 5565 evaluated, never both. Thus, either can contain side-effects 5566 (such as `shell' function calls, etc.) 5567 5568 `$(or CONDITION1[,CONDITION2[,CONDITION3...]])' 5569 The `or' function provides a "short-circuiting" OR operation. 5570 Each argument is expanded, in order. If an argument expands to a 5571 non-empty string the processing stops and the result of the 5572 expansion is that string. If, after all arguments are expanded, 5573 all of them are false (empty), then the result of the expansion is 5574 the empty string. 5575 5576 `$(and CONDITION1[,CONDITION2[,CONDITION3...]])' 5577 The `and' function provides a "short-circuiting" AND operation. 5578 Each argument is expanded, in order. If an argument expands to an 5579 empty string the processing stops and the result of the expansion 5580 is the empty string. If all arguments expand to a non-empty 5581 string then the result of the expansion is the expansion of the 5582 last argument. 5583 5584 5585 5586 File: make.info, Node: Foreach Function, Next: Call Function, Prev: Conditional Functions, Up: Functions 5587 5588 8.5 The `foreach' Function 5589 ========================== 4977 5590 4978 5591 The `foreach' function is very different from other functions. It … … 5046 5659 5047 5660 5048 File: make.info, Node: If Function, Next: Call Function, Prev: Foreach Function, Up: Functions 5049 5050 The `if' Function 5051 ================= 5052 5053 The `if' function provides support for conditional expansion in a 5054 functional context (as opposed to the GNU `make' makefile conditionals 5055 such as `ifeq' (*note Syntax of Conditionals: Conditional Syntax.). 5056 5057 An `if' function call can contain either two or three arguments: 5058 5059 $(if CONDITION,THEN-PART[,ELSE-PART]) 5060 5061 The first argument, CONDITION, first has all preceding and trailing 5062 whitespace stripped, then is expanded. If it expands to any non-empty 5063 string, then the condition is considered to be true. If it expands to 5064 an empty string, the condition is considered to be false. 5065 5066 If the condition is true then the second argument, THEN-PART, is 5067 evaluated and this is used as the result of the evaluation of the entire 5068 `if' function. 5069 5070 If the condition is false then the third argument, ELSE-PART, is 5071 evaluated and this is the result of the `if' function. If there is no 5072 third argument, the `if' function evaluates to nothing (the empty 5073 string). 5074 5075 Note that only one of the THEN-PART or the ELSE-PART will be 5076 evaluated, never both. Thus, either can contain side-effects (such as 5077 `shell' function calls, etc.) 5078 5079 5080 File: make.info, Node: Call Function, Next: Value Function, Prev: If Function, Up: Functions 5081 5082 The `call' Function 5083 =================== 5661 File: make.info, Node: Call Function, Next: Value Function, Prev: Foreach Function, Up: Functions 5662 5663 8.6 The `call' Function 5664 ======================= 5084 5665 5085 5666 The `call' function is unique in that it can be used to create new … … 5120 5701 5121 5702 reverse = $(2) $(1) 5122 5703 5123 5704 foo = $(call reverse,a,b) 5124 5705 … … 5129 5710 5130 5711 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) 5131 5712 5132 5713 LS := $(call pathsearch,ls) 5133 5714 … … 5157 5738 File: make.info, Node: Value Function, Next: Eval Function, Prev: Call Function, Up: Functions 5158 5739 5159 The `value' Function5160 ==================== 5740 8.7 The `value' Function 5741 ======================== 5161 5742 5162 5743 The `value' function provides a way for you to use the value of a … … 5181 5762 5182 5763 FOO = $PATH 5183 5764 5184 5765 all: 5185 5766 @echo $(FOO) … … 5197 5778 File: make.info, Node: Eval Function, Next: Origin Function, Prev: Value Function, Up: Functions 5198 5779 5199 The `eval' Function5200 =================== 5780 8.8 The `eval' Function 5781 ======================= 5201 5782 5202 5783 The `eval' function is very special: it allows you to define new … … 5229 5810 5230 5811 PROGRAMS = server client 5231 5812 5232 5813 server_OBJS = server.o server_priv.o server_access.o 5233 5814 server_LIBS = priv protocol 5234 5815 5235 5816 client_OBJS = client.o client_api.o client_mem.o 5236 5817 client_LIBS = protocol 5237 5818 5238 5819 # Everything after this is generic 5239 5820 5240 5821 .PHONY: all 5241 5822 all: $(PROGRAMS) 5242 5823 5243 5824 define PROGRAM_template 5244 5825 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) 5245 5826 ALL_OBJS += $$($(1)_OBJS) 5246 5827 endef 5247 5828 5248 5829 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog)))) 5249 5830 5250 5831 $(PROGRAMS): 5251 5832 $(LINK.o) $^ $(LDLIBS) -o $@ 5252 5833 5253 5834 clean: 5254 5835 rm -f $(ALL_OBJS) $(PROGRAMS) 5255 5836 5256 5837 5257 File: make.info, Node: Origin Function, Next: ShellFunction, Prev: Eval Function, Up: Functions5258 5259 The `origin' Function5260 ===================== 5838 File: make.info, Node: Origin Function, Next: Flavor Function, Prev: Eval Function, Up: Functions 5839 5840 8.9 The `origin' Function 5841 ========================= 5261 5842 5262 5843 The `origin' function is unlike most other functions in that it does … … 5341 5922 5342 5923 5343 File: make.info, Node: Shell Function, Next: Make Control Functions, Prev: Origin Function, Up: Functions 5344 5345 The `shell' Function 5346 ==================== 5924 File: make.info, Node: Flavor Function, Next: Shell Function, Prev: Origin Function, Up: Functions 5925 5926 8.10 The `flavor' Function 5927 ========================== 5928 5929 The `flavor' function is unlike most other functions (and like `origin' 5930 function) in that it does not operate on the values of variables; it 5931 tells you something _about_ a variable. Specifically, it tells you the 5932 flavor of a variable (*note The Two Flavors of Variables: Flavors.). 5933 5934 The syntax of the `flavor' function is: 5935 5936 $(flavor VARIABLE) 5937 5938 Note that VARIABLE is the _name_ of a variable to inquire about; not 5939 a _reference_ to that variable. Therefore you would not normally use a 5940 `$' or parentheses when writing it. (You can, however, use a variable 5941 reference in the name if you want the name not to be a constant.) 5942 5943 The result of this function is a string that identifies the flavor 5944 of the variable VARIABLE: 5945 5946 `undefined' 5947 if VARIABLE was never defined. 5948 5949 `recursive' 5950 if VARIABLE is a recursively expanded variable. 5951 5952 `simple' 5953 if VARIABLE is a simply expanded variable. 5954 5955 5956 5957 File: make.info, Node: Shell Function, Next: Make Control Functions, Prev: Flavor Function, Up: Functions 5958 5959 8.11 The `shell' Function 5960 ========================= 5347 5961 5348 5962 The `shell' function is unlike any other function other than the … … 5375 5989 5376 5990 sets `files' to the expansion of `*.c'. Unless `make' is using a very 5377 strange shell, this has the same result as `$(wildcard *.c)'. 5991 strange shell, this has the same result as `$(wildcard *.c)' (as long 5992 as at least one `.c' file exists). 5378 5993 5379 5994 5380 5995 File: make.info, Node: Make Control Functions, Prev: Shell Function, Up: Functions 5381 5996 5382 Functions That Control Make5383 =========================== 5997 8.12 Functions That Control Make 5998 ================================ 5384 5999 5385 6000 These functions control the way make runs. Generally, they are used to … … 5404 6019 5405 6020 ERR = $(error found an error!) 5406 6021 5407 6022 .PHONY: err 5408 6023 err: ; $(ERR) … … 5419 6034 The result of the expansion of this function is the empty string. 5420 6035 6036 `$(info TEXT...)' 6037 This function does nothing more than print its (expanded) 6038 argument(s) to standard output. No makefile name or line number 6039 is added. The result of the expansion of this function is the 6040 empty string. 6041 5421 6042 5422 6043 File: make.info, Node: Running, Next: Implicit Rules, Prev: Functions, Up: Top 5423 6044 5424 How to Run `make'5425 ***************** 6045 9 How to Run `make' 6046 ******************* 5426 6047 5427 6048 A makefile that says how to recompile a program can be used in more … … 5469 6090 File: make.info, Node: Makefile Arguments, Next: Goals, Prev: Running, Up: Running 5470 6091 5471 Arguments to Specify the Makefile5472 ================================= 6092 9.1 Arguments to Specify the Makefile 6093 ===================================== 5473 6094 5474 6095 The way to specify the name of the makefile is with the `-f' or … … 5487 6108 File: make.info, Node: Goals, Next: Instead of Execution, Prev: Makefile Arguments, Up: Running 5488 6109 5489 Arguments to Specify the Goals5490 ============================== 6110 9.2 Arguments to Specify the Goals 6111 ================================== 5491 6112 5492 6113 The "goals" are the targets that `make' should strive ultimately to … … 5499 6120 program or programs they describe. If the first rule in the makefile 5500 6121 has several targets, only the first target in the rule becomes the 5501 default goal, not the whole list. 5502 5503 You can specify a different goal or goals with arguments to `make'. 5504 Use the name of the goal as an argument. If you specify several goals, 5505 `make' processes each of them in turn, in the order you name them. 6122 default goal, not the whole list. You can manage the selection of the 6123 default goal from within your makefile using the `.DEFAULT_GOAL' 6124 variable (*note Other Special Variables: Special Variables.). 6125 6126 You can also specify a different goal or goals with command-line 6127 arguments to `make'. Use the name of the goal as an argument. If you 6128 specify several goals, `make' processes each of them in turn, in the 6129 order you name them. 5506 6130 5507 6131 Any target in the makefile may be specified as a goal (unless it … … 5521 6145 5522 6146 sources = foo.c bar.c 5523 6147 5524 6148 ifneq ($(MAKECMDGOALS),clean) 5525 6149 include $(sources:.c=.d) … … 5601 6225 File: make.info, Node: Instead of Execution, Next: Avoiding Compilation, Prev: Goals, Up: Running 5602 6226 5603 Instead of Executing the Commands5604 ================================= 6227 9.3 Instead of Executing the Commands 6228 ===================================== 5605 6229 5606 6230 The makefile tells `make' how to tell whether a target is up to date, … … 5680 6304 File: make.info, Node: Avoiding Compilation, Next: Overriding, Prev: Instead of Execution, Up: Running 5681 6305 5682 Avoiding Recompilation of Some Files5683 ==================================== 6306 9.4 Avoiding Recompilation of Some Files 6307 ======================================== 5684 6308 5685 6309 Sometimes you may have changed a source file but you do not want to … … 5723 6347 File: make.info, Node: Overriding, Next: Testing, Prev: Avoiding Compilation, Up: Running 5724 6348 5725 Overriding Variables5726 ==================== 6349 9.5 Overriding Variables 6350 ======================== 5727 6351 5728 6352 An argument that contains `=' specifies the value of a variable: `V=X' … … 5775 6399 File: make.info, Node: Testing, Next: Options Summary, Prev: Overriding, Up: Running 5776 6400 5777 Testing the Compilation of a Program5778 ==================================== 6401 9.6 Testing the Compilation of a Program 6402 ======================================== 5779 6403 5780 6404 Normally, when an error happens in executing a shell command, `make' … … 5811 6435 File: make.info, Node: Options Summary, Prev: Testing, Up: Running 5812 6436 5813 Summary of Options5814 ================== 6437 9.7 Summary of Options 6438 ====================== 5815 6439 5816 6440 Here is a table of all the options `make' understands: … … 5825 6449 Consider all targets out-of-date. GNU `make' proceeds to consider 5826 6450 targets and their prerequisites using the normal algorithms; 5827 however, all these targets are remade, regardless of the status of 5828 their prerequisites. 6451 however, all targets so considered are always remade regardless of 6452 the status of their prerequisites. To avoid infinite recursion, if 6453 `MAKE_RESTARTS' (*note Other Special Variables: Special 6454 Variables.) is set to a number greater than 0 this option is 6455 disabled when considering whether to remake makefiles (*note How 6456 Makefiles Are Remade: Remaking Makefiles.). 5829 6457 5830 6458 `-C DIR' … … 5931 6559 floating-point number). With no argument, removes a previous load 5932 6560 limit. *Note Parallel Execution: Parallel. 6561 6562 `-L' 6563 `--check-symlink-times' 6564 On systems that support symbolic links, this option causes `make' 6565 to consider the timestamps on any symbolic links in addition to the 6566 timestamp on the file referenced by those links. When this option 6567 is provided, the most recent timestamp among the file and the 6568 symbolic links is taken as the modification time for this target 6569 file. 5933 6570 5934 6571 `-n' … … 6053 6690 File: make.info, Node: Implicit Rules, Next: Archives, Prev: Running, Up: Top 6054 6691 6055 Using Implicit Rules6056 ******************** 6692 10 Using Implicit Rules 6693 *********************** 6057 6694 6058 6695 Certain standard ways of remaking target files are used very often. For … … 6091 6728 * Chained Rules:: How to use a chain of implicit rules. 6092 6729 * Pattern Rules:: How to define new implicit rules. 6093 * Last Resort:: How to defin ing commands for rules6094 whichcannot find any.6730 * Last Resort:: How to define commands for rules which 6731 cannot find any. 6095 6732 * Suffix Rules:: The old-fashioned style of implicit rule. 6096 6733 * Implicit Rule Search:: The precise algorithm for applying … … 6100 6737 File: make.info, Node: Using Implicit, Next: Catalogue of Rules, Prev: Implicit Rules, Up: Implicit Rules 6101 6738 6102 Using Implicit Rules6103 ==================== 6739 10.1 Using Implicit Rules 6740 ========================= 6104 6741 6105 6742 To allow `make' to find a customary method for updating a target file, … … 6174 6811 File: make.info, Node: Catalogue of Rules, Next: Implicit Variables, Prev: Using Implicit, Up: Implicit Rules 6175 6812 6176 Catalogue of Implicit Rules6177 =========================== 6813 10.2 Catalogue of Implicit Rules 6814 ================================ 6178 6815 6179 6816 Here is a catalogue of predefined implicit rules which are always … … 6183 6820 `--no-builtin-rules' option cancels all predefined rules. 6184 6821 6822 This manual only documents the default rules available on POSIX-based 6823 operating systems. Other operating systems, such as VMS, Windows, 6824 OS/2, etc. may have different sets of default rules. To see the full 6825 list of default rules and variables available in your version of GNU 6826 `make', run `make -p' in a directory with no makefile. 6827 6185 6828 Not all of these rules will always be defined, even when the `-r' 6186 6829 option is not given. Many of the predefined implicit rules are … … 6188 6831 depends on the "suffix list" (the list of prerequisites of the special 6189 6832 target `.SUFFIXES'). The default suffix list is: `.out', `.a', `.ln', 6190 `.o', `.c', `.cc', `.C', `. p', `.f', `.F', `.r', `.y', `.l', `.s',6191 `. S', `.mod', `.sym', `.def', `.h', `.info', `.dvi', `.tex', `.texinfo',6192 `.texi ', `.txinfo', `.w', `.ch' `.web', `.sh', `.elc', `.el'. All of6193 the implicit rules described below whose prerequisites have one of 6194 these suffixes are actually suffix rules. If you modify the suffix 6195 list, the only predefined suffix rules in effect will be those named by 6196 one or two of the suffixes that are on the list you specify; rules 6197 whose suffixes fail to be on the list are disabled. *Note 6198 Old-Fashioned Suffix Rules: Suffix Rules, for full details on suffix 6199 rules.6833 `.o', `.c', `.cc', `.C', `.cpp', `.p', `.f', `.F', `.r', `.y', `.l', 6834 `.s', `.S', `.mod', `.sym', `.def', `.h', `.info', `.dvi', `.tex', 6835 `.texinfo', `.texi', `.txinfo', `.w', `.ch' `.web', `.sh', `.elc', 6836 `.el'. All of the implicit rules described below whose prerequisites 6837 have one of these suffixes are actually suffix rules. If you modify 6838 the suffix list, the only predefined suffix rules in effect will be 6839 those named by one or two of the suffixes that are on the list you 6840 specify; rules whose suffixes fail to be on the list are disabled. 6841 *Note Old-Fashioned Suffix Rules: Suffix Rules, for full details on 6842 suffix rules. 6200 6843 6201 6844 Compiling C programs … … 6204 6847 6205 6848 Compiling C++ programs 6206 `N.o' is made automatically from `N.cc' or `N.C' with a command of 6207 the form `$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'. We encourage you to 6208 use the suffix `.cc' for C++ source files instead of `.C'. 6849 `N.o' is made automatically from `N.cc', `N.cpp', or `N.C' with a 6850 command of the form `$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'. We 6851 encourage you to use the suffix `.cc' for C++ source files instead 6852 of `.C'. 6209 6853 6210 6854 Compiling Pascal programs … … 6380 7024 File: make.info, Node: Implicit Variables, Next: Chained Rules, Prev: Catalogue of Rules, Up: Implicit Rules 6381 7025 6382 Variables Used by Implicit Rules6383 ================================ 7026 10.3 Variables Used by Implicit Rules 7027 ===================================== 6384 7028 6385 7029 The commands in built-in implicit rules make liberal use of certain 6386 predefined variables. You can alter the se variables in the makefile,6387 with arguments to `make', or in the environment to alter how the 6388 implicit rules work without redefining the rules themselves. You can 6389 cancel all variables used by implicit rules with the `-R' or7030 predefined variables. You can alter the values of these variables in 7031 the makefile, with arguments to `make', or in the environment to alter 7032 how the implicit rules work without redefining the rules themselves. 7033 You can cancel all variables used by implicit rules with the `-R' or 6390 7034 `--no-builtin-variables' option. 6391 7035 … … 6407 7051 argument, separate them with spaces. 6408 7052 6409 Here is a table of variables used as names of programs in built-in 6410 rules: 7053 The following tables describe of some of the more commonly-used 7054 predefined variables. This list is not exhaustive, and the default 7055 values shown here may not be what are selected by `make' for your 7056 environment. To see the complete list of predefined variables for your 7057 instance of GNU `make' you can run `make -p' in a directory with no 7058 makefiles. 7059 7060 Here is a table of some of the more common variables used as names of 7061 programs in built-in rules: makefiles. 6411 7062 6412 7063 `AR' 6413 Archive-maintaining program; default `ar'. 7064 Archive-maintaining program; default `ar'. 6414 7065 6415 7066 `AS' 6416 Program for doing assembly; default `as'.7067 Program for compiling assembly files; default `as'. 6417 7068 6418 7069 `CC' 6419 Program for compiling C programs; default `cc'. 7070 Program for compiling C programs; default `cc'. 7071 7072 `CO' 7073 Program for checking out files from RCS; default `co'. 6420 7074 6421 7075 `CXX' 6422 Program for compiling C++ programs; default `g++'. 7076 Program for compiling C++ programs; default `g++'. 6423 7077 6424 7078 `CO' 6425 Program for extracting a file from RCS; default `co'. 7079 Program for extracting a file from RCS; default `co'. 6426 7080 6427 7081 `CPP' … … 6431 7085 `FC' 6432 7086 Program for compiling or preprocessing Fortran and Ratfor programs; 6433 default `f77'. 7087 default `f77'. 6434 7088 6435 7089 `GET' 6436 Program for extracting a file from SCCS; default `get'. 7090 Program for extracting a file from SCCS; default `get'. 6437 7091 6438 7092 `LEX' 6439 Program to use to turn Lex grammars into C programs or Ratfor 6440 programs; default `lex'. 7093 Program to use to turn Lex grammars into source code; default 7094 `lex'. 7095 7096 `YACC' 7097 Program to use to turn Yacc grammars into source code; default 7098 `yacc'. 7099 7100 `LINT' 7101 Program to use to run lint on source code; default `lint'. 7102 7103 `M2C' 7104 Program to use to compile Modula-2 source code; default `m2c'. 6441 7105 6442 7106 `PC' 6443 Program for compiling Pascal programs; default `pc'. 6444 6445 `YACC' 6446 Program to use to turn Yacc grammars into C programs; default 6447 `yacc'. 6448 6449 `YACCR' 6450 Program to use to turn Yacc grammars into Ratfor programs; default 6451 `yacc -r'. 7107 Program for compiling Pascal programs; default `pc'. 6452 7108 6453 7109 `MAKEINFO' 6454 7110 Program to convert a Texinfo source file into an Info file; default 6455 `makeinfo'. 7111 `makeinfo'. 6456 7112 6457 7113 `TEX' 6458 Program to make TeX DVI files from TeX source; default `tex'. 7114 Program to make TeX DVI files from TeX source; default `tex'. 6459 7115 6460 7116 `TEXI2DVI' 6461 7117 Program to make TeX DVI files from Texinfo source; default 6462 `texi2dvi'. 7118 `texi2dvi'. 6463 7119 6464 7120 `WEAVE' 6465 Program to translate Web into TeX; default `weave'. 7121 Program to translate Web into TeX; default `weave'. 6466 7122 6467 7123 `CWEAVE' 6468 Program to translate C Web into TeX; default `cweave'. 7124 Program to translate C Web into TeX; default `cweave'. 6469 7125 6470 7126 `TANGLE' 6471 Program to translate Web into Pascal; default `tangle'. 7127 Program to translate Web into Pascal; default `tangle'. 6472 7128 6473 7129 `CTANGLE' 6474 Program to translate C Web into C; default `ctangle'. 7130 Program to translate C Web into C; default `ctangle'. 6475 7131 6476 7132 `RM' 6477 Command to remove a file; default `rm -f'. 7133 Command to remove a file; default `rm -f'. 6478 7134 6479 7135 Here is a table of variables whose values are additional arguments … … 6514 7170 Extra flags to give to Lex. 6515 7171 7172 `YFLAGS' 7173 Extra flags to give to Yacc. 7174 6516 7175 `PFLAGS' 6517 7176 Extra flags to give to the Pascal compiler. … … 6520 7179 Extra flags to give to the Fortran compiler for Ratfor programs. 6521 7180 6522 ` YFLAGS'6523 Extra flags to give to Yacc.7181 `LINTFLAGS' 7182 Extra flags to give to lint. 6524 7183 6525 7184 6526 7185 File: make.info, Node: Chained Rules, Next: Pattern Rules, Prev: Implicit Variables, Up: Implicit Rules 6527 7186 6528 Chains of Implicit Rules6529 ======================== 7187 10.4 Chains of Implicit Rules 7188 ============================= 6530 7189 6531 7190 Sometimes a file can be made by a sequence of implicit rules. For … … 6579 7238 as a prerequisite of the special target `.PRECIOUS' to preserve 6580 7239 intermediate files made by implicit rules whose target patterns match 6581 that file's name; see *Note Interrupts::. 7240 that file's name; see *Note Interrupts::. 6582 7241 6583 7242 A chain can involve more than two implicit rules. For example, it is … … 6601 7260 earlier in the ordering of rules. 6602 7261 6603 6604 File: make.info, Node: Pattern Rules, Next: Last Resort, Prev: Chained Rules, Up: Implicit Rules6605 6606 Defining and Redefining Pattern Rules6607 =====================================6608 6609 You define an implicit rule by writing a "pattern rule". A pattern6610 rule looks like an ordinary rule, except that its target contains the6611 character `%' (exactly one of them). The target is considered a6612 pattern for matching file names; the `%' can match any nonempty6613 substring, while other characters match only themselves. The6614 prerequisites likewise use `%' to show how their names relate to the6615 target name.6616 6617 Thus, a pattern rule `%.o : %.c' says how to make any file `STEM.o'6618 from another file `STEM.c'.6619 6620 Note that expansion using `%' in pattern rules occurs *after* any6621 variable or function expansions, which take place when the makefile is6622 read. *Note How to Use Variables: Using Variables, and *Note Functions6623 for Transforming Text: Functions.6624 6625 * Menu:6626 6627 * Pattern Intro:: An introduction to pattern rules.6628 * Pattern Examples:: Examples of pattern rules.6629 * Automatic Variables:: How to use automatic variables in the6630 commands of implicit rules.6631 * Pattern Match:: How patterns match.6632 * Match-Anything Rules:: Precautions you should take prior to6633 defining rules that can match any6634 target file whatever.6635 * Canceling Rules:: How to override or cancel built-in rules.6636 6637 6638 File: make.info, Node: Pattern Intro, Next: Pattern Examples, Prev: Pattern Rules, Up: Pattern Rules6639 6640 Introduction to Pattern Rules6641 -----------------------------6642 6643 A pattern rule contains the character `%' (exactly one of them) in the6644 target; otherwise, it looks exactly like an ordinary rule. The target6645 is a pattern for matching file names; the `%' matches any nonempty6646 substring, while other characters match only themselves.6647 6648 For example, `%.c' as a pattern matches any file name that ends in6649 `.c'. `s.%.c' as a pattern matches any file name that starts with6650 `s.', ends in `.c' and is at least five characters long. (There must6651 be at least one character to match the `%'.) The substring that the6652 `%' matches is called the "stem".6653 6654 `%' in a prerequisite of a pattern rule stands for the same stem6655 that was matched by the `%' in the target. In order for the pattern6656 rule to apply, its target pattern must match the file name under6657 consideration, and its prerequisite patterns must name files that exist6658 or can be made. These files become prerequisites of the target.6659 6660 Thus, a rule of the form6661 6662 %.o : %.c ; COMMAND...6663 6664 specifies how to make a file `N.o', with another file `N.c' as its6665 prerequisite, provided that `N.c' exists or can be made.6666 6667 There may also be prerequisites that do not use `%'; such a6668 prerequisite attaches to every file made by this pattern rule. These6669 unvarying prerequisites are useful occasionally.6670 6671 A pattern rule need not have any prerequisites that contain `%', or6672 in fact any prerequisites at all. Such a rule is effectively a general6673 wildcard. It provides a way to make any file that matches the target6674 pattern. *Note Last Resort::.6675 6676 Pattern rules may have more than one target. Unlike normal rules,6677 this does not act as many different rules with the same prerequisites6678 and commands. If a pattern rule has multiple targets, `make' knows that6679 the rule's commands are responsible for making all of the targets. The6680 commands are executed only once to make all the targets. When searching6681 for a pattern rule to match a target, the target patterns of a rule6682 other than the one that matches the target in need of a rule are6683 incidental: `make' worries only about giving commands and prerequisites6684 to the file presently in question. However, when this file's commands6685 are run, the other targets are marked as having been updated themselves.6686 6687 The order in which pattern rules appear in the makefile is important6688 since this is the order in which they are considered. Of equally6689 applicable rules, only the first one found is used. The rules you6690 write take precedence over those that are built in. Note however, that6691 a rule whose prerequisites actually exist or are mentioned always takes6692 priority over a rule with prerequisites that must be made by chaining6693 other implicit rules.6694 6695 6696 File: make.info, Node: Pattern Examples, Next: Automatic Variables, Prev: Pattern Intro, Up: Pattern Rules6697 6698 Pattern Rule Examples6699 ---------------------6700 6701 Here are some examples of pattern rules actually predefined in `make'.6702 First, the rule that compiles `.c' files into `.o' files:6703 6704 %.o : %.c6705 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@6706 6707 defines a rule that can make any file `X.o' from `X.c'. The command6708 uses the automatic variables `$@' and `$<' to substitute the names of6709 the target file and the source file in each case where the rule applies6710 (*note Automatic Variables::).6711 6712 Here is a second built-in rule:6713 6714 % :: RCS/%,v6715 $(CO) $(COFLAGS) $<6716 6717 defines a rule that can make any file `X' whatsoever from a6718 corresponding file `X,v' in the subdirectory `RCS'. Since the target6719 is `%', this rule will apply to any file whatever, provided the6720 appropriate prerequisite file exists. The double colon makes the rule6721 "terminal", which means that its prerequisite may not be an intermediate6722 file (*note Match-Anything Pattern Rules: Match-Anything Rules.).6723 6724 This pattern rule has two targets:6725 6726 %.tab.c %.tab.h: %.y6727 bison -d $<6728 6729 This tells `make' that the command `bison -d X.y' will make both6730 `X.tab.c' and `X.tab.h'. If the file `foo' depends on the files6731 `parse.tab.o' and `scan.o' and the file `scan.o' depends on the file6732 `parse.tab.h', when `parse.y' is changed, the command `bison -d parse.y'6733 will be executed only once, and the prerequisites of both `parse.tab.o'6734 and `scan.o' will be satisfied. (Presumably the file `parse.tab.o'6735 will be recompiled from `parse.tab.c' and the file `scan.o' from6736 `scan.c', while `foo' is linked from `parse.tab.o', `scan.o', and its6737 other prerequisites, and it will execute happily ever after.)6738 6739 6740 File: make.info, Node: Automatic Variables, Next: Pattern Match, Prev: Pattern Examples, Up: Pattern Rules6741 6742 Automatic Variables6743 -------------------6744 6745 Suppose you are writing a pattern rule to compile a `.c' file into a6746 `.o' file: how do you write the `cc' command so that it operates on the6747 right source file name? You cannot write the name in the command,6748 because the name is different each time the implicit rule is applied.6749 6750 What you do is use a special feature of `make', the "automatic6751 variables". These variables have values computed afresh for each rule6752 that is executed, based on the target and prerequisites of the rule.6753 In this example, you would use `$@' for the object file name and `$<'6754 for the source file name.6755 6756 It's very important that you recognize the limited scope in which6757 automatic variable values are available: they only have values within6758 the command script. In particular, you cannot use them anywhere within6759 the target or prerequisite lists of a rule; they have no value there6760 and will expand to the empty string. A common mistake is attempting to6761 use `$@' within the prerequisites list in a rule; this will not work.6762 However, see below for information on the SysV-style `$$@' variables.6763 6764 Here is a table of automatic variables:6765 6766 `$@'6767 The file name of the target of the rule. If the target is an6768 archive member, then `$@' is the name of the archive file. In a6769 pattern rule that has multiple targets (*note Introduction to6770 Pattern Rules: Pattern Intro.), `$@' is the name of whichever6771 target caused the rule's commands to be run.6772 6773 `$%'6774 The target member name, when the target is an archive member.6775 *Note Archives::. For example, if the target is `foo.a(bar.o)'6776 then `$%' is `bar.o' and `$@' is `foo.a'. `$%' is empty when the6777 target is not an archive member.6778 6779 `$<'6780 The name of the first prerequisite. If the target got its6781 commands from an implicit rule, this will be the first6782 prerequisite added by the implicit rule (*note Implicit Rules::).6783 6784 `$?'6785 The names of all the prerequisites that are newer than the target,6786 with spaces between them. For prerequisites which are archive6787 members, only the member named is used (*note Archives::).6788 6789 `$^'6790 The names of all the prerequisites, with spaces between them. For6791 prerequisites which are archive members, only the member named is6792 used (*note Archives::). A target has only one prerequisite on6793 each other file it depends on, no matter how many times each file6794 is listed as a prerequisite. So if you list a prerequisite more6795 than once for a target, the value of `$^' contains just one copy6796 of the name.6797 6798 `$+'6799 This is like `$^', but prerequisites listed more than once are6800 duplicated in the order they were listed in the makefile. This is6801 primarily useful for use in linking commands where it is6802 meaningful to repeat library file names in a particular order.6803 6804 `$*'6805 The stem with which an implicit rule matches (*note How Patterns6806 Match: Pattern Match.). If the target is `dir/a.foo.b' and the6807 target pattern is `a.%.b' then the stem is `dir/foo'. The stem is6808 useful for constructing names of related files.6809 6810 In a static pattern rule, the stem is part of the file name that6811 matched the `%' in the target pattern.6812 6813 In an explicit rule, there is no stem; so `$*' cannot be determined6814 in that way. Instead, if the target name ends with a recognized6815 suffix (*note Old-Fashioned Suffix Rules: Suffix Rules.), `$*' is6816 set to the target name minus the suffix. For example, if the6817 target name is `foo.c', then `$*' is set to `foo', since `.c' is a6818 suffix. GNU `make' does this bizarre thing only for compatibility6819 with other implementations of `make'. You should generally avoid6820 using `$*' except in implicit rules or static pattern rules.6821 6822 If the target name in an explicit rule does not end with a6823 recognized suffix, `$*' is set to the empty string for that rule.6824 6825 `$?' is useful even in explicit rules when you wish to operate on6826 only the prerequisites that have changed. For example, suppose that an6827 archive named `lib' is supposed to contain copies of several object6828 files. This rule copies just the changed object files into the archive:6829 6830 lib: foo.o bar.o lose.o win.o6831 ar r lib $?6832 6833 Of the variables listed above, four have values that are single file6834 names, and three have values that are lists of file names. These seven6835 have variants that get just the file's directory name or just the file6836 name within the directory. The variant variables' names are formed by6837 appending `D' or `F', respectively. These variants are semi-obsolete6838 in GNU `make' since the functions `dir' and `notdir' can be used to get6839 a similar effect (*note Functions for File Names: File Name6840 Functions.). Note, however, that the `D' variants all omit the6841 trailing slash which always appears in the output of the `dir'6842 function. Here is a table of the variants:6843 6844 `$(@D)'6845 The directory part of the file name of the target, with the6846 trailing slash removed. If the value of `$@' is `dir/foo.o' then6847 `$(@D)' is `dir'. This value is `.' if `$@' does not contain a6848 slash.6849 6850 `$(@F)'6851 The file-within-directory part of the file name of the target. If6852 the value of `$@' is `dir/foo.o' then `$(@F)' is `foo.o'. `$(@F)'6853 is equivalent to `$(notdir $@)'.6854 6855 `$(*D)'6856 `$(*F)'6857 The directory part and the file-within-directory part of the stem;6858 `dir' and `foo' in this example.6859 6860 `$(%D)'6861 `$(%F)'6862 The directory part and the file-within-directory part of the target6863 archive member name. This makes sense only for archive member6864 targets of the form `ARCHIVE(MEMBER)' and is useful only when6865 MEMBER may contain a directory name. (*Note Archive Members as6866 Targets: Archive Members.)6867 6868 `$(<D)'6869 `$(<F)'6870 The directory part and the file-within-directory part of the first6871 prerequisite.6872 6873 `$(^D)'6874 `$(^F)'6875 Lists of the directory parts and the file-within-directory parts6876 of all prerequisites.6877 6878 `$(+D)'6879 `$(+F)'6880 Lists of the directory parts and the file-within-directory parts6881 of all prerequisites, including multiple instances of duplicated6882 prerequisites.6883 6884 `$(?D)'6885 `$(?F)'6886 Lists of the directory parts and the file-within-directory parts of6887 all prerequisites that are newer than the target.6888 6889 Note that we use a special stylistic convention when we talk about6890 these automatic variables; we write "the value of `$<'", rather than6891 "the variable `<'" as we would write for ordinary variables such as6892 `objects' and `CFLAGS'. We think this convention looks more natural in6893 this special case. Please do not assume it has a deep significance;6894 `$<' refers to the variable named `<' just as `$(CFLAGS)' refers to the6895 variable named `CFLAGS'. You could just as well use `$(<)' in place of6896 `$<'.6897 6898 GNU `make' provides support for the SysV `make' feature that allows6899 special variable references `$$@', `$$(@D)', and `$$(@F)' (note the6900 required double-"$"!) to appear with the _prerequisites list_ (normal6901 automatic variables are available only within a command script). When6902 appearing in a prerequisites list, these variables are expanded to the6903 name of the target, the directory component of the target, and the file6904 component of the target, respectively.6905 6906 Note that these variables are available only within explicit and6907 static pattern (*note Static Pattern Rules: Static Pattern.) rules;6908 they have no special significance within implicit (suffix or pattern)6909 rules. Also note that while SysV `make' actually expands its entire6910 prerequisite list _twice_, GNU `make' does not behave this way: instead6911 it simply expands these special variables without re-expanding any6912 other part of the prerequisites list.6913 6914 This somewhat bizarre feature is included only to provide some6915 compatibility with SysV makefiles. In a native GNU `make' file there6916 are other ways to accomplish the same results. This feature is6917 disabled if the special pseudo target `.POSIX' is defined.6918 6919 6920 File: make.info, Node: Pattern Match, Next: Match-Anything Rules, Prev: Automatic Variables, Up: Pattern Rules6921 6922 How Patterns Match6923 ------------------6924 6925 A target pattern is composed of a `%' between a prefix and a suffix,6926 either or both of which may be empty. The pattern matches a file name6927 only if the file name starts with the prefix and ends with the suffix,6928 without overlap. The text between the prefix and the suffix is called6929 the "stem". Thus, when the pattern `%.o' matches the file name6930 `test.o', the stem is `test'. The pattern rule prerequisites are6931 turned into actual file names by substituting the stem for the character6932 `%'. Thus, if in the same example one of the prerequisites is written6933 as `%.c', it expands to `test.c'.6934 6935 When the target pattern does not contain a slash (and it usually does6936 not), directory names in the file names are removed from the file name6937 before it is compared with the target prefix and suffix. After the6938 comparison of the file name to the target pattern, the directory names,6939 along with the slash that ends them, are added on to the prerequisite6940 file names generated from the pattern rule's prerequisite patterns and6941 the file name. The directories are ignored only for the purpose of6942 finding an implicit rule to use, not in the application of that rule.6943 Thus, `e%t' matches the file name `src/eat', with `src/a' as the stem.6944 When prerequisites are turned into file names, the directories from the6945 stem are added at the front, while the rest of the stem is substituted6946 for the `%'. The stem `src/a' with a prerequisite pattern `c%r' gives6947 the file name `src/car'.6948 6949 6950 File: make.info, Node: Match-Anything Rules, Next: Canceling Rules, Prev: Pattern Match, Up: Pattern Rules6951 6952 Match-Anything Pattern Rules6953 ----------------------------6954 6955 When a pattern rule's target is just `%', it matches any file name6956 whatever. We call these rules "match-anything" rules. They are very6957 useful, but it can take a lot of time for `make' to think about them,6958 because it must consider every such rule for each file name listed6959 either as a target or as a prerequisite.6960 6961 Suppose the makefile mentions `foo.c'. For this target, `make'6962 would have to consider making it by linking an object file `foo.c.o',6963 or by C compilation-and-linking in one step from `foo.c.c', or by6964 Pascal compilation-and-linking from `foo.c.p', and many other6965 possibilities.6966 6967 We know these possibilities are ridiculous since `foo.c' is a C6968 source file, not an executable. If `make' did consider these6969 possibilities, it would ultimately reject them, because files such as6970 `foo.c.o' and `foo.c.p' would not exist. But these possibilities are so6971 numerous that `make' would run very slowly if it had to consider them.6972 6973 To gain speed, we have put various constraints on the way `make'6974 considers match-anything rules. There are two different constraints6975 that can be applied, and each time you define a match-anything rule you6976 must choose one or the other for that rule.6977 6978 One choice is to mark the match-anything rule as "terminal" by6979 defining it with a double colon. When a rule is terminal, it does not6980 apply unless its prerequisites actually exist. Prerequisites that6981 could be made with other implicit rules are not good enough. In other6982 words, no further chaining is allowed beyond a terminal rule.6983 6984 For example, the built-in implicit rules for extracting sources from6985 RCS and SCCS files are terminal; as a result, if the file `foo.c,v' does6986 not exist, `make' will not even consider trying to make it as an6987 intermediate file from `foo.c,v.o' or from `RCS/SCCS/s.foo.c,v'. RCS6988 and SCCS files are generally ultimate source files, which should not be6989 remade from any other files; therefore, `make' can save time by not6990 looking for ways to remake them.6991 6992 If you do not mark the match-anything rule as terminal, then it is6993 nonterminal. A nonterminal match-anything rule cannot apply to a file6994 name that indicates a specific type of data. A file name indicates a6995 specific type of data if some non-match-anything implicit rule target6996 matches it.6997 6998 For example, the file name `foo.c' matches the target for the pattern6999 rule `%.c : %.y' (the rule to run Yacc). Regardless of whether this7000 rule is actually applicable (which happens only if there is a file7001 `foo.y'), the fact that its target matches is enough to prevent7002 consideration of any nonterminal match-anything rules for the file7003 `foo.c'. Thus, `make' will not even consider trying to make `foo.c' as7004 an executable file from `foo.c.o', `foo.c.c', `foo.c.p', etc.7005 7006 The motivation for this constraint is that nonterminal match-anything7007 rules are used for making files containing specific types of data (such7008 as executable files) and a file name with a recognized suffix indicates7009 some other specific type of data (such as a C source file).7010 7011 Special built-in dummy pattern rules are provided solely to recognize7012 certain file names so that nonterminal match-anything rules will not be7013 considered. These dummy rules have no prerequisites and no commands,7014 and they are ignored for all other purposes. For example, the built-in7015 implicit rule7016 7017 %.p :7018 7019 exists to make sure that Pascal source files such as `foo.p' match a7020 specific target pattern and thereby prevent time from being wasted7021 looking for `foo.p.o' or `foo.p.c'.7022 7023 Dummy pattern rules such as the one for `%.p' are made for every7024 suffix listed as valid for use in suffix rules (*note Old-Fashioned7025 Suffix Rules: Suffix Rules.).7026 7027 7028 File: make.info, Node: Canceling Rules, Prev: Match-Anything Rules, Up: Pattern Rules7029 7030 Canceling Implicit Rules7031 ------------------------7032 7033 You can override a built-in implicit rule (or one you have defined7034 yourself) by defining a new pattern rule with the same target and7035 prerequisites, but different commands. When the new rule is defined,7036 the built-in one is replaced. The new rule's position in the sequence7037 of implicit rules is determined by where you write the new rule.7038 7039 You can cancel a built-in implicit rule by defining a pattern rule7040 with the same target and prerequisites, but no commands. For example,7041 the following would cancel the rule that runs the assembler:7042 7043 %.o : %.s7044 7045 7046 File: make.info, Node: Last Resort, Next: Suffix Rules, Prev: Pattern Rules, Up: Implicit Rules7047 7048 Defining Last-Resort Default Rules7049 ==================================7050 7051 You can define a last-resort implicit rule by writing a terminal7052 match-anything pattern rule with no prerequisites (*note Match-Anything7053 Rules::). This is just like any other pattern rule; the only thing7054 special about it is that it will match any target. So such a rule's7055 commands are used for all targets and prerequisites that have no7056 commands of their own and for which no other implicit rule applies.7057 7058 For example, when testing a makefile, you might not care if the7059 source files contain real data, only that they exist. Then you might7060 do this:7061 7062 %::7063 touch $@7064 7065 to cause all the source files needed (as prerequisites) to be created7066 automatically.7067 7068 You can instead define commands to be used for targets for which7069 there are no rules at all, even ones which don't specify commands. You7070 do this by writing a rule for the target `.DEFAULT'. Such a rule's7071 commands are used for all prerequisites which do not appear as targets7072 in any explicit rule, and for which no implicit rule applies.7073 Naturally, there is no `.DEFAULT' rule unless you write one.7074 7075 If you use `.DEFAULT' with no commands or prerequisites:7076 7077 .DEFAULT:7078 7079 the commands previously stored for `.DEFAULT' are cleared. Then `make'7080 acts as if you had never defined `.DEFAULT' at all.7081 7082 If you do not want a target to get the commands from a match-anything7083 pattern rule or `.DEFAULT', but you also do not want any commands to be7084 run for the target, you can give it empty commands (*note Defining7085 Empty Commands: Empty Commands.).7086 7087 You can use a last-resort rule to override part of another makefile.7088 *Note Overriding Part of Another Makefile: Overriding Makefiles.7089 7090 7091 File: make.info, Node: Suffix Rules, Next: Implicit Rule Search, Prev: Last Resort, Up: Implicit Rules7092 7093 Old-Fashioned Suffix Rules7094 ==========================7095 7096 "Suffix rules" are the old-fashioned way of defining implicit rules for7097 `make'. Suffix rules are obsolete because pattern rules are more7098 general and clearer. They are supported in GNU `make' for7099 compatibility with old makefiles. They come in two kinds:7100 "double-suffix" and "single-suffix".7101 7102 A double-suffix rule is defined by a pair of suffixes: the target7103 suffix and the source suffix. It matches any file whose name ends with7104 the target suffix. The corresponding implicit prerequisite is made by7105 replacing the target suffix with the source suffix in the file name. A7106 two-suffix rule whose target and source suffixes are `.o' and `.c' is7107 equivalent to the pattern rule `%.o : %.c'.7108 7109 A single-suffix rule is defined by a single suffix, which is the7110 source suffix. It matches any file name, and the corresponding implicit7111 prerequisite name is made by appending the source suffix. A7112 single-suffix rule whose source suffix is `.c' is equivalent to the7113 pattern rule `% : %.c'.7114 7115 Suffix rule definitions are recognized by comparing each rule's7116 target against a defined list of known suffixes. When `make' sees a7117 rule whose target is a known suffix, this rule is considered a7118 single-suffix rule. When `make' sees a rule whose target is two known7119 suffixes concatenated, this rule is taken as a double-suffix rule.7120 7121 For example, `.c' and `.o' are both on the default list of known7122 suffixes. Therefore, if you define a rule whose target is `.c.o',7123 `make' takes it to be a double-suffix rule with source suffix `.c' and7124 target suffix `.o'. Here is the old-fashioned way to define the rule7125 for compiling a C source file:7126 7127 .c.o:7128 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7129 7130 Suffix rules cannot have any prerequisites of their own. If they7131 have any, they are treated as normal files with funny names, not as7132 suffix rules. Thus, the rule:7133 7134 .c.o: foo.h7135 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7136 7137 tells how to make the file `.c.o' from the prerequisite file `foo.h',7138 and is not at all like the pattern rule:7139 7140 %.o: %.c foo.h7141 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7142 7143 which tells how to make `.o' files from `.c' files, and makes all `.o'7144 files using this pattern rule also depend on `foo.h'.7145 7146 Suffix rules with no commands are also meaningless. They do not7147 remove previous rules as do pattern rules with no commands (*note7148 Canceling Implicit Rules: Canceling Rules.). They simply enter the7149 suffix or pair of suffixes concatenated as a target in the data base.7150 7151 The known suffixes are simply the names of the prerequisites of the7152 special target `.SUFFIXES'. You can add your own suffixes by writing a7153 rule for `.SUFFIXES' that adds more prerequisites, as in:7154 7155 .SUFFIXES: .hack .win7156 7157 which adds `.hack' and `.win' to the end of the list of suffixes.7158 7159 If you wish to eliminate the default known suffixes instead of just7160 adding to them, write a rule for `.SUFFIXES' with no prerequisites. By7161 special dispensation, this eliminates all existing prerequisites of7162 `.SUFFIXES'. You can then write another rule to add the suffixes you7163 want. For example,7164 7165 .SUFFIXES: # Delete the default suffixes7166 .SUFFIXES: .c .o .h # Define our suffix list7167 7168 The `-r' or `--no-builtin-rules' flag causes the default list of7169 suffixes to be empty.7170 7171 The variable `SUFFIXES' is defined to the default list of suffixes7172 before `make' reads any makefiles. You can change the list of suffixes7173 with a rule for the special target `.SUFFIXES', but that does not alter7174 this variable.7175
Note:
See TracChangeset
for help on using the changeset viewer.