Changeset 501 for vendor/gnumake/current/doc/make.info-1
- Timestamp:
- Sep 15, 2006, 4:30:32 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current/doc/make.info-1
r152 r501 1 This is make.info, produced by makeinfo version 4.6 from 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." 2 25 3 26 INFO-DIR-SECTION GNU Packages … … 6 29 END-INFO-DIR-ENTRY 7 30 8 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 9 38 automatically which pieces of a large program need to be recompiled, 10 39 and issues the commands to recompile them. 11 40 12 This is Edition 0.61, last updated 02 May 2003, of `The GNU Make 13 Manual', for `make', Version 3.81. 14 15 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 16 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. 17 18 Permission is granted to copy, distribute and/or modify this document 19 under the terms of the GNU Free Documentation License, Version 1.1 or 20 any later version published by the Free Software Foundation; with no 21 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover 22 Texts. A copy of the license is included in the section entitled "GNU 23 Free Documentation License". 24 25 26 File: make.info, Node: Top, Next: Overview, Prev: (dir), Up: (dir) 27 28 Make 29 **** 30 31 The GNU `make' utility automatically determines which pieces of a large 32 program need to be recompiled, and issues the commands to recompile 33 them. 34 35 This edition of the `GNU Make Manual', last updated 02 May 2003, 36 documents GNU `make' Version 3.81. 37 38 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." 39 59 40 60 * Menu: … … 96 116 with another makefile. 97 117 * Reading Makefiles:: How makefiles are parsed. 118 * Secondary Expansion:: How and when secondary expansion is performed. 98 119 99 120 Writing Rules … … 106 127 * Phony Targets:: Using a target that is not a real file's name. 107 128 * Force Targets:: You can use a target without commands 108 or prerequisites to mark other 109 targetsas phony.129 or prerequisites to mark other targets 130 as phony. 110 131 * Empty Targets:: When only the date matters and the 111 132 files are empty. … … 147 168 Writing the Commands in Rules 148 169 170 * Command Syntax:: Command syntax features and pitfalls. 149 171 * Echoing:: How to control when commands are echoed. 150 172 * Execution:: How commands are executed. … … 155 177 * Sequences:: Defining canned sequences of commands. 156 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. 157 189 158 190 Recursive Use of `make' … … 200 232 * Text Functions:: General-purpose text manipulation functions. 201 233 * File Name Functions:: Functions for manipulating file names. 234 * Conditional Functions:: Functions that implement conditions. 202 235 * Foreach Function:: Repeat some text with controlled variation. 203 * If Function:: Conditionally expand a value.204 236 * Call Function:: Expand a user-defined function. 205 237 * Value Function:: Return the un-expanded value of a variable. 206 238 * Eval Function:: Evaluate the arguments as makefile syntax. 207 239 * Origin Function:: Find where a variable got its value. 240 * Flavor Function:: Find out the flavor of a variable. 208 241 * Shell Function:: Substitute the output of a shell command. 209 242 * Make Control Functions:: Functions that control how make runs. … … 233 266 * Chained Rules:: How to use a chain of implicit rules. 234 267 * Pattern Rules:: How to define new implicit rules. 235 * Last Resort:: How to defin ing commands for rules236 whichcannot find any.268 * Last Resort:: How to define commands for rules which 269 cannot find any. 237 270 * Suffix Rules:: The old-fashioned style of implicit rule. 238 271 * Implicit Rule Search:: The precise algorithm for applying … … 263 296 * Archive Symbols:: How to update archive symbol directories. 264 297 265 Makefile Conventions266 267 * Makefile Basics:: General Conventions for Makefiles268 * Utilities in Makefiles:: Utilities in Makefiles269 * Command Variables:: Variables for Specifying Commands270 * Directory Variables:: Variables for Installation Directories271 * Standard Targets:: Standard Targets for Users272 * Install Command Categories:: Three categories of commands in the `install'273 274 Copying This Manual275 276 298 277 299 File: make.info, Node: Overview, Next: Introduction, Prev: Top, Up: Top 278 300 279 Overview of `make'280 ****************** 301 1 Overview of `make' 302 ******************** 281 303 282 304 The `make' utility automatically determines which pieces of a large … … 287 309 288 310 GNU `make' conforms to section 6.2 of `IEEE Standard 1003.2-1992' 289 (POSIX.2). 311 (POSIX.2). 290 312 291 313 Our examples show C programs, since they are most common, but you … … 329 351 File: make.info, Node: Reading, Next: Bugs, Prev: Preparing, Up: Overview 330 352 331 How to Read This Manual332 ======================= 353 1.1 How to Read This Manual 354 =========================== 333 355 334 356 If you are new to `make', or are looking for a general introduction, … … 350 372 File: make.info, Node: Bugs, Prev: Reading, Up: Overview 351 373 352 Problems and Bugs353 ================= 374 1.2 Problems and Bugs 375 ===================== 354 376 355 377 If you have problems with GNU `make' or think you've found a bug, … … 393 415 File: make.info, Node: Introduction, Next: Makefiles, Prev: Overview, Up: Top 394 416 395 An Introduction to Makefiles396 **************************** 417 2 An Introduction to Makefiles 418 ****************************** 397 419 398 420 You need a file called a "makefile" to tell `make' what to do. Most 399 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. 400 422 401 423 In this chapter, we will discuss a simple makefile that describes … … 412 434 Finally, if any source file has been recompiled, all the object files, 413 435 whether newly made or saved from previous compilations, must be linked 414 together to produce the new executable editor. 436 together to produce the new executable editor. 415 437 416 438 * Menu: … … 427 449 File: make.info, Node: Rule Introduction, Next: Simple Makefile, Prev: Introduction, Up: Introduction 428 450 429 What a Rule Looks Like430 ====================== 451 2.1 What a Rule Looks Like 452 ========================== 431 453 432 454 A simple makefile consists of "rules" with the following shape: … … 470 492 File: make.info, Node: Simple Makefile, Next: How Make Works, Prev: Rule Introduction, Up: Introduction 471 493 472 A Simple Makefile473 ================= 494 2.2 A Simple Makefile 495 ===================== 474 496 475 497 Here is a straightforward makefile that describes the way an executable … … 485 507 cc -o edit main.o kbd.o command.o display.o \ 486 508 insert.o search.o files.o utils.o 487 509 488 510 main.o : main.c defs.h 489 511 cc -c main.c … … 507 529 508 530 We split each long line into two lines using backslash-newline; this is 509 like using one long line, but is easier to read. 531 like using one long line, but is easier to read. 510 532 511 533 To use this makefile to create the executable file called `edit', … … 535 557 prerequisites. These shell commands say how to update the target file. 536 558 A tab character must come at the beginning of every command line to 537 distinguish command slines from other lines in the makefile. (Bear in559 distinguish command lines from other lines in the makefile. (Bear in 538 560 mind that `make' does not know anything about how the commands work. 539 561 It is up to you to supply commands that will update the target file 540 562 properly. All `make' does is execute the commands in the rule you have 541 specified when the target file needs to be updated.) 563 specified when the target file needs to be updated.) 542 564 543 565 The target `clean' is not a file, but merely the name of an action. … … 550 572 called "phony targets". *Note Phony Targets::, for information about 551 573 this kind of target. *Note Errors in Commands: Errors, to see how to 552 cause `make' to ignore errors from `rm' or any other command. 574 cause `make' to ignore errors from `rm' or any other command. 553 575 554 576 555 577 File: make.info, Node: How Make Works, Next: Variables Simplify, Prev: Simple Makefile, Up: Introduction 556 578 557 How `make' Processes a Makefile558 =============================== 579 2.3 How `make' Processes a Makefile 580 =================================== 559 581 560 582 By default, `make' starts with the first target (not targets whose 561 583 names start with `.'). This is called the "default goal". ("Goals" 562 are the targets that `make' strives ultimately to update. *Note 563 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.). 564 588 565 589 In the simple example of the previous section, the default goal is to … … 596 620 not exist, or if any of the object files are newer than it. If an 597 621 object file was just recompiled, it is now newer than `edit', so `edit' 598 is relinked. 622 is relinked. 599 623 600 624 Thus, if we change the file `insert.c' and run `make', `make' will … … 607 631 File: make.info, Node: Variables Simplify, Next: make Deduces, Prev: How Make Works, Up: Introduction 608 632 609 Variables Make Makefiles Simpler610 ================================ 633 2.4 Variables Make Makefiles Simpler 634 ==================================== 611 635 612 636 In our example, we had to list all the object files twice in the rule … … 641 665 objects = main.o kbd.o command.o display.o \ 642 666 insert.o search.o files.o utils.o 643 667 644 668 edit : $(objects) 645 669 cc -o edit $(objects) … … 666 690 File: make.info, Node: make Deduces, Next: Combine By Prerequisite, Prev: Variables Simplify, Up: Introduction 667 691 668 Letting `make' Deduce the Commands669 ================================== 692 2.5 Letting `make' Deduce the Commands 693 ====================================== 670 694 671 695 It is not necessary to spell out the commands for compiling the … … 686 710 objects = main.o kbd.o command.o display.o \ 687 711 insert.o search.o files.o utils.o 688 712 689 713 edit : $(objects) 690 714 cc -o edit $(objects) 691 715 692 716 main.o : defs.h 693 717 kbd.o : defs.h command.h … … 698 722 files.o : defs.h buffer.h command.h 699 723 utils.o : defs.h 700 724 701 725 .PHONY : clean 702 726 clean : … … 713 737 File: make.info, Node: Combine By Prerequisite, Next: Cleanup, Prev: make Deduces, Up: Introduction 714 738 715 Another Style of Makefile716 ========================= 739 2.6 Another Style of Makefile 740 ============================= 717 741 718 742 When the objects of a makefile are created only by implicit rules, an … … 723 747 objects = main.o kbd.o command.o display.o \ 724 748 insert.o search.o files.o utils.o 725 749 726 750 edit : $(objects) 727 751 cc -o edit $(objects) 728 752 729 753 $(objects) : defs.h 730 754 kbd.o command.o files.o : command.h … … 742 766 File: make.info, Node: Cleanup, Prev: Combine By Prerequisite, Up: Introduction 743 767 744 Rules for Cleaning the Directory745 ================================ 768 2.7 Rules for Cleaning the Directory 769 ==================================== 746 770 747 771 Compiling a program is not the only thing you might want to write rules … … 781 805 File: make.info, Node: Makefiles, Next: Rules, Prev: Introduction, Up: Top 782 806 783 Writing Makefiles784 ***************** 807 3 Writing Makefiles 808 ******************* 785 809 786 810 The information that tells `make' how to recompile a system comes from … … 799 823 with another makefile. 800 824 * Reading Makefiles:: How makefiles are parsed. 825 * Secondary Expansion:: How and when secondary expansion is performed. 801 826 802 827 803 828 File: make.info, Node: Makefile Contents, Next: Makefile Names, Prev: Makefiles, Up: Makefiles 804 829 805 What Makefiles Contain806 ====================== 830 3.1 What Makefiles Contain 831 ========================== 807 832 808 833 Makefiles contain five kinds of things: "explicit rules", "implicit … … 864 889 File: make.info, Node: Makefile Names, Next: Include, Prev: Makefile Contents, Up: Makefiles 865 890 866 What Name to Give Your Makefile867 =============================== 891 3.2 What Name to Give Your Makefile 892 =================================== 868 893 869 894 By default, when `make' looks for the makefile, it tries the following 870 names, in order: `GNUmakefile', `makefile' and `Makefile'. 895 names, in order: `GNUmakefile', `makefile' and `Makefile'. 871 896 872 897 Normally you should call your makefile either `makefile' or … … 891 916 concatenated in the order specified. The default makefile names 892 917 `GNUmakefile', `makefile' and `Makefile' are not checked automatically 893 if you specify `-f' or `--file'. 918 if you specify `-f' or `--file'. 894 919 895 920 896 921 File: make.info, Node: Include, Next: MAKEFILES Variable, Prev: Makefile Names, Up: Makefiles 897 922 898 Including Other Makefiles899 ========================= 923 3.3 Including Other Makefiles 924 ============================= 900 925 901 926 The `include' directive tells `make' to suspend reading the current … … 905 930 include FILENAMES... 906 931 907 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. 908 934 909 935 Extra spaces are allowed and ignored at the beginning of the line, … … 942 968 than that of somehow appending the prerequisites to the end of the main 943 969 makefile as has been traditionally done with other versions of `make'. 944 *Note Automatic Prerequisites::. 970 *Note Automatic Prerequisites::. 945 971 946 972 If the specified name does not start with a slash, and the file is … … 980 1006 File: make.info, Node: MAKEFILES Variable, Next: MAKEFILE_LIST Variable, Prev: Include, Up: Makefiles 981 1007 982 The Variable `MAKEFILES'983 ======================== 1008 3.4 The Variable `MAKEFILES' 1009 ============================ 984 1010 985 1011 If the environment variable `MAKEFILES' is defined, `make' considers … … 1009 1035 File: make.info, Node: MAKEFILE_LIST Variable, Next: Special Variables, Prev: MAKEFILES Variable, Up: Makefiles 1010 1036 1011 The Variable `MAKEFILE_LIST'1012 ============================ 1037 3.5 The Variable `MAKEFILE_LIST' 1038 ================================ 1013 1039 1014 1040 As `make' reads various makefiles, including any obtained from the … … 1025 1051 If a makefile named `Makefile' has this content: 1026 1052 1027 name1 := $( word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))1028 1053 name1 := $(lastword $(MAKEFILE_LIST)) 1054 1029 1055 include inc.mk 1030 1031 name2 := $( word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))1032 1056 1057 name2 := $(lastword $(MAKEFILE_LIST)) 1058 1033 1059 all: 1034 1060 @echo name1 = $(name1) … … 1048 1074 File: make.info, Node: Special Variables, Next: Remaking Makefiles, Prev: MAKEFILE_LIST Variable, Up: Makefiles 1049 1075 1050 Other Special Variables 1051 ======================= 1052 1053 GNU `make' also supports a special variable. Note that any value you 1054 assign to this variable will be ignored; it will always return its 1055 special value. 1056 1057 The first special variable is `.VARIABLES'. When expanded, the 1058 value consists of a list of the _names_ of all global variables defined 1059 in all makefiles read up until that point. This includes variables 1060 which have empty values, as well as built-in variables (*note Variables 1061 Used by Implicit Rules: Implicit Variables.), but does not include any 1062 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 1063 1175 1064 1176 1065 1177 File: make.info, Node: Remaking Makefiles, Next: Overriding Makefiles, Prev: Special Variables, Up: Makefiles 1066 1178 1067 How Makefiles Are Remade1068 ======================== 1179 3.7 How Makefiles Are Remade 1180 ============================ 1069 1181 1070 1182 Sometimes makefiles can be remade from other files, such as RCS or SCCS … … 1103 1215 If you do not specify any makefiles to be read with `-f' or `--file' 1104 1216 options, `make' will try the default makefile names; *note What Name to 1105 Give Your Makefile: Makefile Names. .Unlike makefiles explicitly1217 Give Your Makefile: Makefile Names. Unlike makefiles explicitly 1106 1218 requested with `-f' or `--file' options, `make' is not certain that 1107 1219 these makefiles should exist. However, if a default makefile does not … … 1143 1255 File: make.info, Node: Overriding Makefiles, Next: Reading Makefiles, Prev: Remaking Makefiles, Up: Makefiles 1144 1256 1145 Overriding Part of Another Makefile1146 =================================== 1257 3.8 Overriding Part of Another Makefile 1258 ======================================= 1147 1259 1148 1260 Sometimes it is useful to have a makefile that is mostly just like … … 1164 1276 foo: 1165 1277 frobnicate > foo 1166 1278 1167 1279 %: force 1168 1280 @$(MAKE) -f Makefile $@ … … 1186 1298 1187 1299 1188 File: make.info, Node: Reading Makefiles, Prev: Overriding Makefiles, Up: Makefiles1189 1190 How `make' Reads a Makefile1191 =========================== 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 =============================== 1192 1304 1193 1305 GNU `make' does its work in two distinct phases. During the first … … 1224 1336 IMMEDIATE := IMMEDIATE 1225 1337 IMMEDIATE += DEFERRED or IMMEDIATE 1226 1338 1227 1339 define IMMEDIATE 1228 1340 DEFERRED … … 1238 1350 All instances of conditional syntax are parsed immediately, in their 1239 1351 entirety; this includes the `ifdef', `ifeq', `ifndef', and `ifneq' 1240 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. 1241 1357 1242 1358 Rule Definition … … 1254 1370 1255 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 1256 1551 File: make.info, Node: Rules, Next: Commands, Prev: Makefiles, Up: Top 1257 1552 1258 Writing Rules1259 ************* 1553 4 Writing Rules 1554 *************** 1260 1555 1261 1556 A "rule" appears in the makefile and says when and how to remake … … 1288 1583 * Phony Targets:: Using a target that is not a real file's name. 1289 1584 * Force Targets:: You can use a target without commands 1290 or prerequisites to mark other 1291 targetsas phony.1585 or prerequisites to mark other targets 1586 as phony. 1292 1587 * Empty Targets:: When only the date matters and the 1293 1588 files are empty. … … 1306 1601 File: make.info, Node: Rule Example, Next: Rule Syntax, Prev: Rules, Up: Rules 1307 1602 1308 Rule Example1309 ============ 1603 4.1 Rule Example 1604 ================ 1310 1605 1311 1606 Here is an example of a rule: … … 1332 1627 File: make.info, Node: Rule Syntax, Next: Prerequisite Types, Prev: Rule Example, Up: Rules 1333 1628 1334 Rule Syntax1335 =========== 1629 4.2 Rule Syntax 1630 =============== 1336 1631 1337 1632 In general, a rule looks like this: … … 1358 1653 appear on the line after the prerequisites, with a tab character, or may 1359 1654 appear on the same line, with a semicolon. Either way, the effect is 1360 the same. *Note Writing the Commands in Rules: Commands. 1361 1362 Because dollar signs are used to start variable references, if you 1363 really want a dollar sign in a rule you must write two of them, `$$' 1364 (*note How to Use Variables: Using Variables.). You may split a long 1365 line by inserting a backslash followed by a newline, but this is not 1366 required, as `make' places no limit on the length of a line in a 1367 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. 1368 1668 1369 1669 A rule tells `make' two things: when the targets are out of date, … … 1387 1687 File: make.info, Node: Prerequisite Types, Next: Wildcards, Prev: Rule Syntax, Up: Rules 1388 1688 1389 Types of Prerequisites1390 ====================== 1689 4.3 Types of Prerequisites 1690 ========================== 1391 1691 1392 1692 There are actually two different types of prerequisites understood by … … 1423 1723 File: make.info, Node: Wildcards, Next: Directory Search, Prev: Prerequisite Types, Up: Rules 1424 1724 1425 Using Wildcard Characters in File Names1426 ======================================= 1725 4.4 Using Wildcard Characters in File Names 1726 =========================================== 1427 1727 1428 1728 A single file name can specify many files using "wildcard characters". … … 1440 1740 simulated by setting the environment variable HOME. 1441 1741 1442 Wildcard expansion happens automatically in targets, in1443 prerequisites, and in commands (where the shell does the expansion). 1444 In other contexts, wildcard expansion happens only if you request it 1445 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. 1446 1746 1447 1747 The special significance of a wildcard character can be turned off by … … 1459 1759 File: make.info, Node: Wildcard Examples, Next: Wildcard Pitfall, Prev: Wildcards, Up: Wildcards 1460 1760 1461 Wildcard Examples1462 ----------------- 1761 4.4.1 Wildcard Examples 1762 ----------------------- 1463 1763 1464 1764 Wildcards can be used in the commands of a rule, where they are expanded … … 1499 1799 File: make.info, Node: Wildcard Pitfall, Next: Wildcard Function, Prev: Wildcard Examples, Up: Wildcards 1500 1800 1501 Pitfalls of Using Wildcards1502 --------------------------- 1801 4.4.2 Pitfalls of Using Wildcards 1802 --------------------------------- 1503 1803 1504 1804 Now here is an example of a naive way of using wildcard expansion, that … … 1508 1808 1509 1809 objects = *.o 1510 1810 1511 1811 foo : $(objects) 1512 1812 cc -o foo $(CFLAGS) $(objects) … … 1542 1842 File: make.info, Node: Wildcard Function, Prev: Wildcard Pitfall, Up: Wildcards 1543 1843 1544 The Function `wildcard'1545 ----------------------- 1844 4.4.3 The Function `wildcard' 1845 ----------------------------- 1546 1846 1547 1847 Wildcard expansion happens automatically in rules. But wildcard … … 1578 1878 1579 1879 objects := $(patsubst %.c,%.o,$(wildcard *.c)) 1580 1880 1581 1881 foo : $(objects) 1582 1882 cc -o foo $(objects) … … 1590 1890 File: make.info, Node: Directory Search, Next: Phony Targets, Prev: Wildcards, Up: Rules 1591 1891 1592 Searching Directories for Prerequisites1593 ======================================= 1892 4.5 Searching Directories for Prerequisites 1893 =========================================== 1594 1894 1595 1895 For large systems, it is often desirable to put sources in a separate … … 1614 1914 File: make.info, Node: General Search, Next: Selective Search, Prev: Directory Search, Up: Directory Search 1615 1915 1616 `VPATH': Search Path for All Prerequisites1617 ------------------------------------------ 1916 4.5.1 `VPATH': Search Path for All Prerequisites 1917 ------------------------------------------------ 1618 1918 1619 1919 The value of the `make' variable `VPATH' specifies a list of … … 1658 1958 File: make.info, Node: Selective Search, Next: Search Algorithm, Prev: General Search, Up: Directory Search 1659 1959 1660 The `vpath' Directive1661 --------------------- 1960 4.5.2 The `vpath' Directive 1961 --------------------------- 1662 1962 1663 1963 Similar to the `VPATH' variable, but more selective, is the `vpath' … … 1737 2037 File: make.info, Node: Search Algorithm, Next: Commands/Search, Prev: Selective Search, Up: Directory Search 1738 2038 1739 How Directory Searches are Performed1740 ------------------------------------ 2039 4.5.3 How Directory Searches are Performed 2040 ------------------------------------------ 1741 2041 1742 2042 When a prerequisite is found through directory search, regardless of … … 1795 2095 File: make.info, Node: Commands/Search, Next: Implicit/Search, Prev: Search Algorithm, Up: Directory Search 1796 2096 1797 Writing Shell Commands with Directory Search1798 -------------------------------------------- 2097 4.5.4 Writing Shell Commands with Directory Search 2098 -------------------------------------------------- 1799 2099 1800 2100 When a prerequisite is found in another directory through directory … … 1816 2116 compilation by implicit rules; we use it here for consistency so it will 1817 2117 affect all C compilations uniformly; *note Variables Used by Implicit 1818 Rules: Implicit Variables. .)2118 Rules: Implicit Variables.) 1819 2119 1820 2120 Often the prerequisites include header files as well, which you do … … 1829 2129 File: make.info, Node: Implicit/Search, Next: Libraries/Search, Prev: Commands/Search, Up: Directory Search 1830 2130 1831 Directory Search and Implicit Rules1832 ----------------------------------- 2131 4.5.5 Directory Search and Implicit Rules 2132 ----------------------------------------- 1833 2133 1834 2134 The search through the directories specified in `VPATH' or with `vpath' … … 1850 2150 File: make.info, Node: Libraries/Search, Prev: Implicit/Search, Up: Directory Search 1851 2151 1852 Directory Search for Link Libraries1853 ----------------------------------- 2152 4.5.6 Directory Search for Link Libraries 2153 ----------------------------------------- 1854 2154 1855 2155 Directory search applies in a special way to libraries used with the … … 1889 2189 word in the list will be used. 1890 2190 1891 The default value for `.LIBPATTERNS' is "`lib%.so lib%.a'", which2191 The default value for `.LIBPATTERNS' is `lib%.so lib%.a', which 1892 2192 provides the default behavior described above. 1893 2193 … … 1898 2198 File: make.info, Node: Phony Targets, Next: Force Targets, Prev: Directory Search, Up: Rules 1899 2199 1900 Phony Targets1901 ============= 2200 4.6 Phony Targets 2201 ================= 1902 2202 1903 2203 A phony target is one that is not really the name of a file. It is … … 1915 2215 Because the `rm' command does not create a file named `clean', probably 1916 2216 no such file will ever exist. Therefore, the `rm' command will be 1917 executed every time you say `make clean'. 2217 executed every time you say `make clean'. 1918 2218 1919 2219 The phony target will cease to work if anything ever does create a … … 1945 2245 Another example of the usefulness of phony targets is in conjunction 1946 2246 with recursive invocations of `make' (for more information, see *Note 1947 Recursive Use of `make': Recursion ). In this case the makefile will2247 Recursive Use of `make': Recursion.). In this case the makefile will 1948 2248 often contain a variable which lists a number of subdirectories to be 1949 2249 built. One way to handle this is with one rule whose command is a … … 1951 2251 1952 2252 SUBDIRS = foo bar baz 1953 2253 1954 2254 subdirs: 1955 2255 for dir in $(SUBDIRS); do \ … … 1963 2263 it will do so even if `make' is invoked with the `-k' option, which is 1964 2264 unfortunate. Second, and perhaps more importantly, you cannot take 1965 advantage of the parallel build capabilities of make using this method,1966 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. 1967 2267 1968 2268 By declaring the subdirectories as phony targets (you must do this as … … 1971 2271 1972 2272 SUBDIRS = foo bar baz 1973 2273 1974 2274 .PHONY: subdirs $(SUBDIRS) 1975 2275 1976 2276 subdirs: $(SUBDIRS) 1977 2277 1978 2278 $(SUBDIRS): 1979 2279 $(MAKE) -C $@ 1980 2280 1981 2281 foo: baz 1982 2282 … … 2002 2302 all : prog1 prog2 prog3 2003 2303 .PHONY : all 2004 2304 2005 2305 prog1 : prog1.o utils.o 2006 2306 cc -o prog1 prog1.o utils.o 2007 2307 2008 2308 prog2 : prog2.o 2009 2309 cc -o prog2 prog2.o 2010 2310 2011 2311 prog3 : prog3.o sort.o utils.o 2012 2312 cc -o prog3 prog3.o sort.o utils.o 2013 2313 2014 2314 Now you can say just `make' to remake all three programs, or specify as 2015 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. 2016 2318 2017 2319 When one phony target is a prerequisite of another, it serves as a … … 2020 2322 2021 2323 .PHONY: cleanall cleanobj cleandiff 2022 2324 2023 2325 cleanall : cleanobj cleandiff 2024 2326 rm program 2025 2327 2026 2328 cleanobj : 2027 2329 rm *.o 2028 2330 2029 2331 cleandiff : 2030 2332 rm *.diff … … 2033 2335 File: make.info, Node: Force Targets, Next: Empty Targets, Prev: Phony Targets, Up: Rules 2034 2336 2035 Rules without Commands or Prerequisites2036 ======================================= 2337 4.7 Rules without Commands or Prerequisites 2338 =========================================== 2037 2339 2038 2340 If a rule has no prerequisites or commands, and the target of the rule … … 2062 2364 File: make.info, Node: Empty Targets, Next: Special Targets, Prev: Force Targets, Up: Rules 2063 2365 2064 Empty Target Files to Record Events2065 =================================== 2366 4.8 Empty Target Files to Record Events 2367 ======================================= 2066 2368 2067 2369 The "empty target" is a variant of the phony target; it is used to hold … … 2084 2386 lpr -p $? 2085 2387 touch print 2086 2388 2087 2389 With this rule, `make print' will execute the `lpr' command if either 2088 2390 source file has changed since the last `make print'. The automatic … … 2093 2395 File: make.info, Node: Special Targets, Next: Multiple Targets, Prev: Empty Targets, Up: Rules 2094 2396 2095 Special Built-in Target Names2096 ============================= 2397 4.9 Special Built-in Target Names 2398 ================================= 2097 2399 2098 2400 Certain names have special meanings if they appear as targets. … … 2147 2449 intermediate). 2148 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 2149 2461 `.DELETE_ON_ERROR' 2150 2462 If `.DELETE_ON_ERROR' is mentioned as a target anywhere in the … … 2233 2545 File: make.info, Node: Multiple Targets, Next: Multiple Rules, Prev: Special Targets, Up: Rules 2234 2546 2235 Multiple Targets in a Rule2236 ========================== 2547 4.10 Multiple Targets in a Rule 2548 =============================== 2237 2549 2238 2550 A rule with multiple targets is equivalent to writing many rules, each … … 2258 2570 bigoutput littleoutput : text.g 2259 2571 generate text.g -$(subst output,,$@) > $@ 2260 2572 2261 2573 is equivalent to 2262 2574 … … 2280 2592 File: make.info, Node: Multiple Rules, Next: Static Pattern, Prev: Multiple Targets, Up: Rules 2281 2593 2282 Multiple Rules for One Target2283 ============================= 2594 4.11 Multiple Rules for One Target 2595 ================================== 2284 2596 2285 2597 One file can be the target of several rules. All the prerequisites … … 2331 2643 File: make.info, Node: Static Pattern, Next: Double-Colon, Prev: Multiple Rules, Up: Rules 2332 2644 2333 Static Pattern Rules2334 ==================== 2645 4.12 Static Pattern Rules 2646 ========================= 2335 2647 2336 2648 "Static pattern rules" are rules which specify multiple targets and … … 2348 2660 File: make.info, Node: Static Usage, Next: Static versus Implicit, Prev: Static Pattern, Up: Static Pattern 2349 2661 2350 Syntax of Static Pattern Rules2351 ------------------------------ 2662 4.12.1 Syntax of Static Pattern Rules 2663 ------------------------------------- 2352 2664 2353 2665 Here is the syntax of a static pattern rule: … … 2397 2709 2398 2710 objects = foo.o bar.o 2399 2711 2400 2712 all: $(objects) 2401 2713 2402 2714 $(objects): %.o: %.c 2403 2715 $(CC) -c $(CFLAGS) $< -o $@ … … 2414 2726 2415 2727 files = foo.elc bar.o lose.o 2416 2728 2417 2729 $(filter %.o,$(files)): %.o: %.c 2418 2730 $(CC) -c $(CFLAGS) $< -o $@ … … 2426 2738 from `foo.el'. 2427 2739 2428 Another example shows how to use `$*' in static pattern rules: 2740 Another example shows how to use `$*' in static pattern rules: 2429 2741 2430 2742 bigoutput littleoutput : %output : text.g … … 2437 2749 File: make.info, Node: Static versus Implicit, Prev: Static Usage, Up: Static Pattern 2438 2750 2439 Static Pattern Rules versus Implicit Rules2440 ------------------------------------------ 2751 4.12.2 Static Pattern Rules versus Implicit Rules 2752 ------------------------------------------------- 2441 2753 2442 2754 A static pattern rule has much in common with an implicit rule defined … … 2474 2786 File: make.info, Node: Double-Colon, Next: Automatic Prerequisites, Prev: Static Pattern, Up: Rules 2475 2787 2476 Double-Colon Rules2477 ================== 2788 4.13 Double-Colon Rules 2789 ======================= 2478 2790 2479 2791 "Double-colon" rules are rules written with `::' instead of `:' after … … 2511 2823 File: make.info, Node: Automatic Prerequisites, Prev: Double-Colon, Up: Rules 2512 2824 2513 Generating Prerequisites Automatically2514 ====================================== 2825 4.14 Generating Prerequisites Automatically 2826 =========================================== 2515 2827 2516 2828 In the makefile for a program, many of the rules you need to write often … … 2524 2836 would have to write dozens of such rules in your makefile. And, you 2525 2837 must always be very careful to update the makefile every time you add 2526 or remove an `#include'. 2838 or remove an `#include'. 2527 2839 2528 2840 To avoid this hassle, most modern C compilers can write these rules … … 2543 2855 makefile, so it can never be considered an intermediate file by implicit 2544 2856 rule search. This means that `make' won't ever remove the file after 2545 using it; *note Chains of Implicit Rules: Chained Rules. .2857 using it; *note Chains of Implicit Rules: Chained Rules. 2546 2858 2547 2859 With old `make' programs, it was traditional practice to use this … … 2567 2879 2568 2880 %.d: %.c 2569 2881 @set -e; rm -f $@; \ 2570 2882 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ 2571 2883 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ … … 2574 2886 *Note Pattern Rules::, for information on defining pattern rules. The 2575 2887 `-e' flag to the shell causes it to exit immediately if the `$(CC)' 2576 command (or any other command) fails (exits with a nonzero status). 2888 command (or any other command) fails (exits with a nonzero status). 2577 2889 2578 2890 With the GNU C compiler, you may wish to use the `-MM' flag instead … … 2599 2911 2600 2912 sources = foo.c bar.c 2601 2913 2602 2914 include $(sources:.c=.d) 2603 2915 … … 2610 2922 2611 2923 Note that the `.d' files contain target definitions; you should be 2612 sure to place the `include' directive _after_ the first, default target2924 sure to place the `include' directive _after_ the first, default goal 2613 2925 in your makefiles or run the risk of having a random object file become 2614 the default target. *Note How Make Works::.2926 the default goal. *Note How Make Works::. 2615 2927 2616 2928 2617 2929 File: make.info, Node: Commands, Next: Using Variables, Prev: Rules, Up: Top 2618 2930 2619 Writing the Commands in Rules 2620 ***************************** 2621 2622 The commands of a rule consist of shell command lines to be executed one 2623 by one. Each command line must start with a tab, except that the first 2624 command line may be attached to the target-and-prerequisites line with a 2625 semicolon in between. Blank lines and lines of just comments may appear 2626 among the command lines; they are ignored. (But beware, an apparently 2627 "blank" line that begins with a tab is _not_ blank! It is an empty 2628 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. 2629 2938 2630 2939 Users use many different shell programs, but commands in makefiles … … 2632 2941 otherwise. *Note Command Execution: Execution. 2633 2942 2634 The shell that is in use determines whether comments can be written2635 on command lines, and what syntax they use. When the shell is2636 `/bin/sh', a `#' starts a comment that extends to the end of the line.2637 The `#' does not have to be at the beginning of a line. Text on a line2638 before a `#' is not part of the comment.2639 2640 2943 * Menu: 2641 2944 2945 * Command Syntax:: Command syntax features and pitfalls. 2642 2946 * Echoing:: How to control when commands are echoed. 2643 2947 * Execution:: How commands are executed. … … 2650 2954 2651 2955 2652 File: make.info, Node: Echoing, Next: Execution, Prev: Commands, Up: Commands 2653 2654 Command Echoing 2655 =============== 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 =================== 2656 3134 2657 3135 Normally `make' prints each command line before it is executed. We … … 2682 3160 File: make.info, Node: Execution, Next: Parallel, Prev: Echoing, Up: Commands 2683 3161 2684 Command Execution2685 ================= 3162 5.3 Command Execution 3163 ===================== 2686 3164 2687 3165 When it is time to execute commands to update a target, they are 2688 executed by making a new subshell for each line. (In practice, `make' 2689 may take shortcuts that do not affect the results.) 2690 2691 *Please note:* this implies that shell commands such as `cd' that 2692 set variables local to each process will not affect the following 2693 command lines. (1) If you want to use `cd' to affect the next command, 2694 put the two on a single line with a semicolon between them. Then 2695 `make' will consider them a single command and pass them, together, to 2696 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: 2697 3176 2698 3177 foo : bar/lose 2699 cd bar; gobble lose > ../foo 2700 2701 If you would like to split a single shell command into multiple 2702 lines of text, you must use a backslash at the end of all but the last 2703 subline. Such a sequence of lines is combined into a single line, by 2704 deleting the backslash-newline sequences, before passing it to the 2705 shell. Thus, the following is equivalent to the preceding example: 2706 2707 foo : bar/lose 2708 cd bar; \ 2709 gobble lose > ../foo 2710 2711 The program used as the shell is taken from the variable `SHELL'. 2712 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. 2713 3233 2714 3234 On MS-DOS, if `SHELL' is not set, the value of the variable … … 2756 3276 2757 3277 The effect of the above DOS-specific processing is that a Makefile 2758 that says `SHELL = /bin/sh' (as many Unix makefiles do), will work on 2759 MS-DOS unaltered if you have e.g. `sh.exe' installed in some directory 2760 along your `PATH'. 2761 2762 Unlike most variables, the variable `SHELL' is never set from the 2763 environment. This is because the `SHELL' environment variable is used 2764 to specify your personal choice of shell program for interactive use. 2765 It would be very bad for personal choices like this to affect the 2766 functioning of makefiles. *Note Variables from the Environment: 2767 Environment. However, on MS-DOS and MS-Windows the value of `SHELL' in 2768 the environment *is* used, since on those systems most users do not set 2769 this variable, and therefore it is most likely set specifically to be 2770 used by `make'. On MS-DOS, if the setting of `SHELL' is not suitable 2771 for `make', you can set the variable `MAKESHELL' to the shell that 2772 `make' should use; this will override the value of `SHELL'. 2773 2774 ---------- Footnotes ---------- 2775 2776 (1) On MS-DOS, the value of current working directory is *global*, 2777 so changing it _will_ affect the following command lines on those 2778 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'. 2779 3281 2780 3282 2781 3283 File: make.info, Node: Parallel, Next: Errors, Prev: Execution, Up: Commands 2782 3284 2783 Parallel Execution2784 ================== 3285 5.4 Parallel Execution 3286 ====================== 2785 3287 2786 3288 GNU `make' knows how to execute several commands at once. Normally, … … 2808 3310 streams of all but one running command. This means that attempting to 2809 3311 read from standard input will usually be a fatal error (a `Broken pipe' 2810 signal) for most child processes if there are several. 3312 signal) for most child processes if there are several. 2811 3313 2812 3314 It is unpredictable which command will have a valid standard input … … 2857 3359 File: make.info, Node: Errors, Next: Interrupts, Prev: Parallel, Up: Commands 2858 3360 2859 Errors in Commands2860 ================== 3361 5.5 Errors in Commands 3362 ====================== 2861 3363 2862 3364 After each shell command returns, `make' looks at its exit status. If … … 2916 3418 program as possible, perhaps to find several independent problems so 2917 3419 that you can correct them all before the next attempt to compile. This 2918 is why Emacs' `compile' command passes the `-k' flag by default. 3420 is why Emacs' `compile' command passes the `-k' flag by default. 2919 3421 2920 3422 Usually when a command fails, if it has changed the target file at … … 2933 3435 File: make.info, Node: Interrupts, Next: Recursion, Prev: Errors, Up: Commands 2934 3436 2935 Interrupting or Killing `make'2936 ============================== 3437 5.6 Interrupting or Killing `make' 3438 ================================== 2937 3439 2938 3440 If `make' gets a fatal signal while a command is executing, it may … … 2964 3466 File: make.info, Node: Recursion, Next: Sequences, Prev: Interrupts, Up: Commands 2965 3467 2966 Recursive Use of `make'2967 ======================= 3468 5.7 Recursive Use of `make' 3469 =========================== 2968 3470 2969 3471 Recursive use of `make' means using `make' as a command in a makefile. … … 2981 3483 subsystem: 2982 3484 $(MAKE) -C subdir 2983 3485 2984 3486 You can write recursive `make' commands just by copying this example, 2985 3487 but there are many things to know about how they work and why, and about … … 3011 3513 File: make.info, Node: MAKE Variable, Next: Variables/Recursion, Prev: Recursion, Up: Recursion 3012 3514 3013 How the `MAKE' Variable Works3014 ----------------------------- 3515 5.7.1 How the `MAKE' Variable Works 3516 ----------------------------------- 3015 3517 3016 3518 Recursive `make' commands should always use the variable `MAKE', not … … 3024 3526 is `cd subdir && /bin/make'. If you use a special version of `make' to 3025 3527 run the top-level makefile, the same special version will be executed 3026 for recursive invocations. 3528 for recursive invocations. 3027 3529 3028 3530 As a special feature, using the variable `MAKE' in the commands of a … … 3042 3544 file named `subsystem' and do nothing else. What you really want it to 3043 3545 do is run `cd subdir && make -t'; but that would require executing the 3044 command, and `-t' says not to execute commands. 3546 command, and `-t' says not to execute commands. 3045 3547 3046 3548 The special feature makes this do what you want: whenever a command … … 3056 3558 File: make.info, Node: Variables/Recursion, Next: Options/Recursion, Prev: MAKE Variable, Up: Recursion 3057 3559 3058 Communicating Variables to a Sub-`make'3059 --------------------------------------- 3560 5.7.2 Communicating Variables to a Sub-`make' 3561 --------------------------------------------- 3060 3562 3061 3563 Variable values of the top-level `make' can be passed to the sub-`make' … … 3076 3578 characters other than letters, numbers, and underscores. 3077 3579 3078 The special variables `SHELL' and `MAKEFLAGS' are always exported 3079 (unless you unexport them). `MAKEFILES' is exported if you set it to 3080 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. 3081 3588 3082 3589 `make' automatically passes down variable values that were defined … … 3153 3660 `.EXPORT_ALL_VARIABLES' instead of using the `export' directive. This 3154 3661 will be ignored by old `make's, while the `export' directive will cause 3155 a syntax error. 3662 a syntax error. 3156 3663 3157 3664 Likewise, you can use `unexport' by itself to tell `make' _not_ to … … 3186 3693 File: make.info, Node: Options/Recursion, Next: -w Option, Prev: Variables/Recursion, Up: Recursion 3187 3694 3188 Communicating Options to a Sub-`make'3189 ------------------------------------- 3695 5.7.3 Communicating Options to a Sub-`make' 3696 ------------------------------------------- 3190 3697 3191 3698 Flags such as `-s' and `-k' are passed automatically to the sub-`make' … … 3291 3798 File: make.info, Node: -w Option, Prev: Options/Recursion, Up: Recursion 3292 3799 3293 The `--print-directory' Option3294 ------------------------------ 3800 5.7.4 The `--print-directory' Option 3801 ------------------------------------ 3295 3802 3296 3803 If you use several levels of recursive `make' invocations, the `-w' or … … 3317 3824 File: make.info, Node: Sequences, Next: Empty Commands, Prev: Recursion, Up: Commands 3318 3825 3319 Defining Canned Command Sequences3320 ================================= 3826 5.8 Defining Canned Command Sequences 3827 ===================================== 3321 3828 3322 3829 When the same sequence of commands is useful in making various targets, … … 3332 3839 mv y.tab.c $@ 3333 3840 endef 3334 3841 3335 3842 Here `run-yacc' is the name of the variable being defined; `endef' 3336 3843 marks the end of the definition; the lines in between are the commands. … … 3392 3899 File: make.info, Node: Empty Commands, Prev: Sequences, Up: Commands 3393 3900 3394 Using Empty Commands3395 ==================== 3901 5.9 Using Empty Commands 3902 ======================== 3396 3903 3397 3904 It is sometimes useful to define commands which do nothing. This is … … 3421 3928 File: make.info, Node: Using Variables, Next: Conditionals, Prev: Commands, Up: Top 3422 3929 3423 How to Use Variables3424 ******************** 3930 6 How to Use Variables 3931 ********************** 3425 3932 3426 3933 A "variable" is a name defined in a makefile to represent a string of … … 3428 3935 explicit request into targets, prerequisites, commands, and other parts 3429 3936 of the makefile. (In some other versions of `make', variables are 3430 called "macros".) 3937 called "macros".) 3431 3938 3432 3939 Variables and functions in all parts of a makefile are expanded when … … 3483 3990 File: make.info, Node: Reference, Next: Flavors, Prev: Using Variables, Up: Using Variables 3484 3991 3485 Basics of Variable References3486 ============================= 3992 6.1 Basics of Variable References 3993 ================================= 3487 3994 3488 3995 To substitute a variable's value, write a dollar sign followed by the … … 3500 4007 program : $(objects) 3501 4008 cc -o program $(objects) 3502 4009 3503 4010 $(objects) : defs.h 3504 4011 … … 3523 4030 File: make.info, Node: Flavors, Next: Advanced, Prev: Reference, Up: Using Variables 3524 4031 3525 The Two Flavors of Variables3526 ============================ 4032 6.2 The Two Flavors of Variables 4033 ================================ 3527 4034 3528 4035 There are two ways that a variable in GNU `make' can have a value; we … … 3544 4051 bar = $(ugh) 3545 4052 ugh = Huh? 3546 4053 3547 4054 all:;echo $(foo) 3548 4055 … … 3564 4071 3565 4072 because it will cause an infinite loop in the variable expansion. 3566 (Actually `make' detects the infinite loop and reports an error.) 4073 (Actually `make' detects the infinite loop and reports an error.) 3567 4074 3568 4075 Another disadvantage is that any functions (*note Functions for … … 3605 4112 3606 4113 ifeq (0,${MAKELEVEL}) 3607 cur-dir := $(shell pwd)3608 4114 whoami := $(shell whoami) 3609 4115 host-type := $(shell arch) … … 3615 4121 3616 4122 ${subdirs}: 3617 ${MAKE} cur-dir=${cur-dir}/$@-C $@ all4123 ${MAKE} -C $@ all 3618 4124 3619 4125 Simply expanded variables generally make complicated makefile … … 3669 4175 File: make.info, Node: Advanced, Next: Values, Prev: Flavors, Up: Using Variables 3670 4176 3671 Advanced Features for Reference to Variables3672 ============================================ 4177 6.3 Advanced Features for Reference to Variables 4178 ================================================ 3673 4179 3674 4180 This section describes some advanced features you can use to reference … … 3684 4190 File: make.info, Node: Substitution Refs, Next: Computed Names, Prev: Advanced, Up: Advanced 3685 4191 3686 Substitution References3687 ----------------------- 4192 6.3.1 Substitution References 4193 ----------------------------- 3688 4194 3689 4195 A "substitution reference" substitutes the value of a variable with … … 3717 4223 3718 4224 For example: 3719 4225 3720 4226 foo := a.o b.o c.o 3721 4227 bar := $(foo:%.o=%.c) … … 3726 4232 File: make.info, Node: Computed Names, Prev: Substitution Refs, Up: Advanced 3727 4233 3728 Computed Variable Names3729 ----------------------- 4234 6.3.2 Computed Variable Names 4235 ----------------------------- 3730 4236 3731 4237 Computed variable names are a complicated concept needed only for … … 3798 4304 a_dirs := dira dirb 3799 4305 1_dirs := dir1 dir2 3800 4306 3801 4307 a_files := filea fileb 3802 4308 1_files := file1 file2 3803 4309 3804 4310 ifeq "$(use_a)" "yes" 3805 4311 a1 := a … … 3807 4313 a1 := 1 3808 4314 endif 3809 4315 3810 4316 ifeq "$(use_dirs)" "yes" 3811 4317 df := dirs … … 3813 4319 df := files 3814 4320 endif 3815 4321 3816 4322 dirs := $($(a1)_$(df)) 3817 4323 … … 3823 4329 a_objects := a.o b.o c.o 3824 4330 1_objects := 1.o 2.o 3.o 3825 4331 3826 4332 sources := $($(a1)_objects:.o=.c) 3827 4333 … … 3839 4345 func := strip 3840 4346 endif 3841 4347 3842 4348 bar := a d b g q c 3843 4349 3844 4350 foo := $($(func) $(bar)) 3845 4351 … … 3869 4375 File: make.info, Node: Values, Next: Setting, Prev: Advanced, Up: Using Variables 3870 4376 3871 How Variables Get Their Values3872 ============================== 4377 6.4 How Variables Get Their Values 4378 ================================== 3873 4379 3874 4380 Variables can get values in several different ways: … … 3894 4400 File: make.info, Node: Setting, Next: Appending, Prev: Values, Up: Using Variables 3895 4401 3896 Setting Variables3897 ================= 4402 6.5 Setting Variables 4403 ===================== 3898 4404 3899 4405 To set a variable from the makefile, write a line starting with the … … 3947 4453 File: make.info, Node: Appending, Next: Override Directive, Prev: Setting, Up: Using Variables 3948 4454 3949 Appending More Text to Variables3950 ================================ 4455 6.6 Appending More Text to Variables 4456 ==================================== 3951 4457 3952 4458 Often it is useful to add more text to the value of a variable already … … 3983 4489 it a simply-expanded variable, `+=' adds to that simply-expanded 3984 4490 definition, and expands the new text before appending it to the old 3985 value just as `:=' does ( *note Setting Variables: Setting., for a full3986 explanation of `:='). In fact,4491 value just as `:=' does (see *Note Setting Variables: Setting, for a 4492 full explanation of `:='). In fact, 3987 4493 3988 4494 variable := value … … 3990 4496 3991 4497 is exactly equivalent to: 4498 3992 4499 3993 4500 variable := value … … 4022 4529 The first line defines the `CFLAGS' variable with a reference to another 4023 4530 variable, `includes'. (`CFLAGS' is used by the rules for C 4024 compilation; *note Catalogue of Implicit Rules: Catalogue of Rules. .)4531 compilation; *note Catalogue of Implicit Rules: Catalogue of Rules.) 4025 4532 Using `=' for the definition makes `CFLAGS' a recursively-expanded 4026 4533 variable, meaning `$(includes) -O' is _not_ expanded when `make' … … 4044 4551 File: make.info, Node: Override Directive, Next: Defining, Prev: Appending, Up: Using Variables 4045 4552 4046 The `override' Directive4047 ======================== 4553 6.7 The `override' Directive 4554 ============================ 4048 4555 4049 4556 If a variable has been set with a command argument (*note Overriding … … 4088 4595 File: make.info, Node: Defining, Next: Environment, Prev: Override Directive, Up: Using Variables 4089 4596 4090 Defining Variables Verbatim4091 =========================== 4597 6.8 Defining Variables Verbatim 4598 =============================== 4092 4599 4093 4600 Another way to set the value of a variable is to use the `define' … … 4147 4654 File: make.info, Node: Environment, Next: Target-specific, Prev: Defining, Up: Using Variables 4148 4655 4149 Variables from the Environment4150 ============================== 4656 6.9 Variables from the Environment 4657 ================================== 4151 4658 4152 4659 Variables in `make' can come from the environment in which `make' is 4153 4660 run. Every environment variable that `make' sees when it starts up is 4154 transformed into a `make' variable with the same name and value. But4155 an explicit assignment in the makefile, or with a command argument, 4156 overrides the environment. (If the `-e' flag is specified, then values 4157 from the environment override assignments in the makefile. *Note 4158 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 4159 4666 practice.) 4160 4667 … … 4163 4670 you prefer. This is safe for variables with standard or conventional 4164 4671 meanings because you know that no makefile will use them for other 4165 things. ( But this is not totally reliable; some makefiles set `CFLAGS'4166 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 4167 4674 environment.) 4168 4675 4169 When `make' is invoked recursively, variables defined in the outer4170 invocation can be passed to inner invocations through the environment 4171 (*note Recursive Use of `make': Recursion.). By default, only 4172 variables that came from the environment or the command line are passed 4173 to recursive invocations. You can use the `export' directive to pass 4174 other variables. *Note Communicating Variables to a Sub-`make': 4175 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. 4176 4683 4177 4684 Other use of variables from the environment is not recommended. It … … 4184 4691 which is normally present in the environment to specify the user's 4185 4692 choice of interactive shell. It would be very undesirable for this 4186 choice to affect `make'. So `make' ignores the environment value of 4187 `SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not 4188 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::. 4189 4695 4190 4696 4191 4697 File: make.info, Node: Target-specific, Next: Pattern-specific, Prev: Environment, Up: Using Variables 4192 4698 4193 Target-specific Variable Values4194 =============================== 4699 6.10 Target-specific Variable Values 4700 ==================================== 4195 4701 4196 4702 Variable values in `make' are usually global; that is, they are the … … 4261 4767 File: make.info, Node: Pattern-specific, Prev: Target-specific, Up: Using Variables 4262 4768 4263 Pattern-specific Variable Values4264 ================================ 4769 6.11 Pattern-specific Variable Values 4770 ===================================== 4265 4771 4266 4772 In addition to target-specific variable values (*note Target-specific … … 4299 4805 File: make.info, Node: Conditionals, Next: Functions, Prev: Using Variables, Up: Top 4300 4806 4301 Conditional Parts of Makefiles4302 ****************************** 4807 7 Conditional Parts of Makefiles 4808 ******************************** 4303 4809 4304 4810 A "conditional" causes part of a makefile to be obeyed or ignored … … 4318 4824 File: make.info, Node: Conditional Example, Next: Conditional Syntax, Prev: Conditionals, Up: Conditionals 4319 4825 4320 Example of a Conditional4321 ======================== 4826 7.1 Example of a Conditional 4827 ============================ 4322 4828 4323 4829 The following example of a conditional tells `make' to use one set of … … 4330 4836 libs_for_gcc = -lgnu 4331 4837 normal_libs = 4332 4838 4333 4839 foo: $(objects) 4334 4840 ifeq ($(CC),gcc) … … 4380 4886 libs_for_gcc = -lgnu 4381 4887 normal_libs = 4382 4888 4383 4889 ifeq ($(CC),gcc) 4384 4890 libs=$(libs_for_gcc) … … 4386 4892 libs=$(normal_libs) 4387 4893 endif 4388 4894 4389 4895 foo: $(objects) 4390 4896 $(CC) -o foo $(objects) $(libs) … … 4393 4899 File: make.info, Node: Conditional Syntax, Next: Testing Flags, Prev: Conditional Example, Up: Conditionals 4394 4900 4395 Syntax of Conditionals4396 ====================== 4901 7.2 Syntax of Conditionals 4902 ========================== 4397 4903 4398 4904 The syntax of a simple conditional with no `else' is as follows: … … 4414 4920 endif 4415 4921 4416 If the condition is true, TEXT-IF-TRUE is used; otherwise, 4417 TEXT-IF-FALSE is used instead. The TEXT-IF-FALSE can be any number of 4418 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. 4419 4937 4420 4938 The syntax of the CONDITIONAL-DIRECTIVE is the same whether the 4421 conditional is simple or complex. There are four different directives 4422 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: 4423 4942 4424 4943 `ifeq (ARG1, ARG2)' … … 4456 4975 4457 4976 `ifdef VARIABLE-NAME' 4458 If the variable VARIABLE-NAME has a non-empty value, the 4459 TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any, 4460 is effective. Variables that have never been defined have an 4461 empty value. The variable VARIABLE-NAME is itself expanded, so it 4462 could be a variable or function that expands to the name of a 4463 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. 4464 4995 4465 4996 Note that `ifdef' only tests whether a variable has a value. It … … 4491 5022 If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE 4492 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. 4493 5026 4494 5027 Extra spaces are allowed and ignored at the beginning of the … … 4525 5058 File: make.info, Node: Testing Flags, Prev: Conditional Syntax, Up: Conditionals 4526 5059 4527 Conditionals that Test Flags4528 ============================ 5060 7.3 Conditionals that Test Flags 5061 ================================ 4529 5062 4530 5063 You can write a conditional that tests `make' command flags such as … … 4556 5089 File: make.info, Node: Functions, Next: Running, Prev: Conditionals, Up: Top 4557 5090 4558 Functions for Transforming Text4559 ******************************* 5091 8 Functions for Transforming Text 5092 ********************************* 4560 5093 4561 5094 "Functions" allow you to do text processing in the makefile to compute … … 4571 5104 * Text Functions:: General-purpose text manipulation functions. 4572 5105 * File Name Functions:: Functions for manipulating file names. 5106 * Conditional Functions:: Functions that implement conditions. 4573 5107 * Foreach Function:: Repeat some text with controlled variation. 4574 * If Function:: Conditionally expand a value.4575 5108 * Call Function:: Expand a user-defined function. 4576 5109 * Value Function:: Return the un-expanded value of a variable. 4577 5110 * Eval Function:: Evaluate the arguments as makefile syntax. 4578 5111 * Origin Function:: Find where a variable got its value. 5112 * Flavor Function:: Find out the flavor of a variable. 4579 5113 * Shell Function:: Substitute the output of a shell command. 4580 5114 * Make Control Functions:: Functions that control how make runs. … … 4583 5117 File: make.info, Node: Syntax of Functions, Next: Text Functions, Prev: Functions, Up: Functions 4584 5118 4585 Function Call Syntax4586 ==================== 5119 8.1 Function Call Syntax 5120 ======================== 4587 5121 4588 5122 A function call resembles a variable reference. It looks like this: … … 4637 5171 File: make.info, Node: Text Functions, Next: File Name Functions, Prev: Syntax of Functions, Up: Functions 4638 5172 4639 Functions for String Substitution and Analysis4640 ============================================== 5173 8.2 Functions for String Substitution and Analysis 5174 ================================================== 4641 5175 4642 5176 Here are some functions that operate on strings: … … 4803 5337 `$(wordlist S,E,TEXT)' 4804 5338 Returns the list of words in TEXT starting with word S and ending 4805 with word E (inclusive). The legitimate values of S and E start4806 from 1. If S is bigger than the number of words in TEXT, the4807 value is empty. If E is bigger than the number of words in TEXT,4808 words up to the end of TEXT are returned. If S is greater than E,4809 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, 4810 5344 4811 5345 $(wordlist 2, 3, foo bar baz) … … 4829 5363 same as `$(word 1,TEXT)', the `firstword' function is retained for 4830 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. 4831 5377 4832 5378 Here is a realistic example of the use of `subst' and `patsubst'. … … 4857 5403 4858 5404 4859 File: make.info, Node: File Name Functions, Next: Foreach Function, Prev: Text Functions, Up: Functions4860 4861 Functions for File Names4862 ======================== 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 ============================ 4863 5409 4864 5410 Several of the built-in expansion functions relate specifically to … … 4969 5515 File Names: Wildcards. 4970 5516 4971 4972 File: make.info, Node: Foreach Function, Next: If Function, Prev: File Name Functions, Up: Functions 4973 4974 The `foreach' Function 4975 ====================== 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 ========================== 4976 5590 4977 5591 The `foreach' function is very different from other functions. It … … 5045 5659 5046 5660 5047 File: make.info, Node: If Function, Next: Call Function, Prev: Foreach Function, Up: Functions 5048 5049 The `if' Function 5050 ================= 5051 5052 The `if' function provides support for conditional expansion in a 5053 functional context (as opposed to the GNU `make' makefile conditionals 5054 such as `ifeq' (*note Syntax of Conditionals: Conditional Syntax.). 5055 5056 An `if' function call can contain either two or three arguments: 5057 5058 $(if CONDITION,THEN-PART[,ELSE-PART]) 5059 5060 The first argument, CONDITION, first has all preceding and trailing 5061 whitespace stripped, then is expanded. If it expands to any non-empty 5062 string, then the condition is considered to be true. If it expands to 5063 an empty string, the condition is considered to be false. 5064 5065 If the condition is true then the second argument, THEN-PART, is 5066 evaluated and this is used as the result of the evaluation of the entire 5067 `if' function. 5068 5069 If the condition is false then the third argument, ELSE-PART, is 5070 evaluated and this is the result of the `if' function. If there is no 5071 third argument, the `if' function evaluates to nothing (the empty 5072 string). 5073 5074 Note that only one of the THEN-PART or the ELSE-PART will be 5075 evaluated, never both. Thus, either can contain side-effects (such as 5076 `shell' function calls, etc.) 5077 5078 5079 File: make.info, Node: Call Function, Next: Value Function, Prev: If Function, Up: Functions 5080 5081 The `call' Function 5082 =================== 5661 File: make.info, Node: Call Function, Next: Value Function, Prev: Foreach Function, Up: Functions 5662 5663 8.6 The `call' Function 5664 ======================= 5083 5665 5084 5666 The `call' function is unique in that it can be used to create new … … 5119 5701 5120 5702 reverse = $(2) $(1) 5121 5703 5122 5704 foo = $(call reverse,a,b) 5123 5705 … … 5128 5710 5129 5711 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) 5130 5712 5131 5713 LS := $(call pathsearch,ls) 5132 5714 … … 5156 5738 File: make.info, Node: Value Function, Next: Eval Function, Prev: Call Function, Up: Functions 5157 5739 5158 The `value' Function5159 ==================== 5740 8.7 The `value' Function 5741 ======================== 5160 5742 5161 5743 The `value' function provides a way for you to use the value of a … … 5180 5762 5181 5763 FOO = $PATH 5182 5764 5183 5765 all: 5184 5766 @echo $(FOO) … … 5196 5778 File: make.info, Node: Eval Function, Next: Origin Function, Prev: Value Function, Up: Functions 5197 5779 5198 The `eval' Function5199 =================== 5780 8.8 The `eval' Function 5781 ======================= 5200 5782 5201 5783 The `eval' function is very special: it allows you to define new … … 5228 5810 5229 5811 PROGRAMS = server client 5230 5812 5231 5813 server_OBJS = server.o server_priv.o server_access.o 5232 5814 server_LIBS = priv protocol 5233 5815 5234 5816 client_OBJS = client.o client_api.o client_mem.o 5235 5817 client_LIBS = protocol 5236 5818 5237 5819 # Everything after this is generic 5238 5820 5239 5821 .PHONY: all 5240 5822 all: $(PROGRAMS) 5241 5823 5242 5824 define PROGRAM_template 5243 5825 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) 5244 5826 ALL_OBJS += $$($(1)_OBJS) 5245 5827 endef 5246 5828 5247 5829 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog)))) 5248 5830 5249 5831 $(PROGRAMS): 5250 5832 $(LINK.o) $^ $(LDLIBS) -o $@ 5251 5833 5252 5834 clean: 5253 5835 rm -f $(ALL_OBJS) $(PROGRAMS) 5254 5836 5255 5837 5256 File: make.info, Node: Origin Function, Next: ShellFunction, Prev: Eval Function, Up: Functions5257 5258 The `origin' Function5259 ===================== 5838 File: make.info, Node: Origin Function, Next: Flavor Function, Prev: Eval Function, Up: Functions 5839 5840 8.9 The `origin' Function 5841 ========================= 5260 5842 5261 5843 The `origin' function is unlike most other functions in that it does … … 5340 5922 5341 5923 5342 File: make.info, Node: Shell Function, Next: Make Control Functions, Prev: Origin Function, Up: Functions 5343 5344 The `shell' Function 5345 ==================== 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 ========================= 5346 5961 5347 5962 The `shell' function is unlike any other function other than the … … 5374 5989 5375 5990 sets `files' to the expansion of `*.c'. Unless `make' is using a very 5376 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). 5377 5993 5378 5994 5379 5995 File: make.info, Node: Make Control Functions, Prev: Shell Function, Up: Functions 5380 5996 5381 Functions That Control Make5382 =========================== 5997 8.12 Functions That Control Make 5998 ================================ 5383 5999 5384 6000 These functions control the way make runs. Generally, they are used to … … 5403 6019 5404 6020 ERR = $(error found an error!) 5405 6021 5406 6022 .PHONY: err 5407 6023 err: ; $(ERR) … … 5418 6034 The result of the expansion of this function is the empty string. 5419 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 5420 6042 5421 6043 File: make.info, Node: Running, Next: Implicit Rules, Prev: Functions, Up: Top 5422 6044 5423 How to Run `make'5424 ***************** 6045 9 How to Run `make' 6046 ******************* 5425 6047 5426 6048 A makefile that says how to recompile a program can be used in more … … 5468 6090 File: make.info, Node: Makefile Arguments, Next: Goals, Prev: Running, Up: Running 5469 6091 5470 Arguments to Specify the Makefile5471 ================================= 6092 9.1 Arguments to Specify the Makefile 6093 ===================================== 5472 6094 5473 6095 The way to specify the name of the makefile is with the `-f' or … … 5486 6108 File: make.info, Node: Goals, Next: Instead of Execution, Prev: Makefile Arguments, Up: Running 5487 6109 5488 Arguments to Specify the Goals5489 ============================== 6110 9.2 Arguments to Specify the Goals 6111 ================================== 5490 6112 5491 6113 The "goals" are the targets that `make' should strive ultimately to … … 5498 6120 program or programs they describe. If the first rule in the makefile 5499 6121 has several targets, only the first target in the rule becomes the 5500 default goal, not the whole list. 5501 5502 You can specify a different goal or goals with arguments to `make'. 5503 Use the name of the goal as an argument. If you specify several goals, 5504 `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. 5505 6130 5506 6131 Any target in the makefile may be specified as a goal (unless it … … 5520 6145 5521 6146 sources = foo.c bar.c 5522 6147 5523 6148 ifneq ($(MAKECMDGOALS),clean) 5524 6149 include $(sources:.c=.d) … … 5600 6225 File: make.info, Node: Instead of Execution, Next: Avoiding Compilation, Prev: Goals, Up: Running 5601 6226 5602 Instead of Executing the Commands5603 ================================= 6227 9.3 Instead of Executing the Commands 6228 ===================================== 5604 6229 5605 6230 The makefile tells `make' how to tell whether a target is up to date, … … 5679 6304 File: make.info, Node: Avoiding Compilation, Next: Overriding, Prev: Instead of Execution, Up: Running 5680 6305 5681 Avoiding Recompilation of Some Files5682 ==================================== 6306 9.4 Avoiding Recompilation of Some Files 6307 ======================================== 5683 6308 5684 6309 Sometimes you may have changed a source file but you do not want to … … 5722 6347 File: make.info, Node: Overriding, Next: Testing, Prev: Avoiding Compilation, Up: Running 5723 6348 5724 Overriding Variables5725 ==================== 6349 9.5 Overriding Variables 6350 ======================== 5726 6351 5727 6352 An argument that contains `=' specifies the value of a variable: `V=X' … … 5774 6399 File: make.info, Node: Testing, Next: Options Summary, Prev: Overriding, Up: Running 5775 6400 5776 Testing the Compilation of a Program5777 ==================================== 6401 9.6 Testing the Compilation of a Program 6402 ======================================== 5778 6403 5779 6404 Normally, when an error happens in executing a shell command, `make' … … 5810 6435 File: make.info, Node: Options Summary, Prev: Testing, Up: Running 5811 6436 5812 Summary of Options5813 ================== 6437 9.7 Summary of Options 6438 ====================== 5814 6439 5815 6440 Here is a table of all the options `make' understands: … … 5824 6449 Consider all targets out-of-date. GNU `make' proceeds to consider 5825 6450 targets and their prerequisites using the normal algorithms; 5826 however, all these targets are remade, regardless of the status of 5827 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.). 5828 6457 5829 6458 `-C DIR' … … 5930 6559 floating-point number). With no argument, removes a previous load 5931 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. 5932 6570 5933 6571 `-n' … … 6052 6690 File: make.info, Node: Implicit Rules, Next: Archives, Prev: Running, Up: Top 6053 6691 6054 Using Implicit Rules6055 ******************** 6692 10 Using Implicit Rules 6693 *********************** 6056 6694 6057 6695 Certain standard ways of remaking target files are used very often. For … … 6090 6728 * Chained Rules:: How to use a chain of implicit rules. 6091 6729 * Pattern Rules:: How to define new implicit rules. 6092 * Last Resort:: How to defin ing commands for rules6093 whichcannot find any.6730 * Last Resort:: How to define commands for rules which 6731 cannot find any. 6094 6732 * Suffix Rules:: The old-fashioned style of implicit rule. 6095 6733 * Implicit Rule Search:: The precise algorithm for applying … … 6099 6737 File: make.info, Node: Using Implicit, Next: Catalogue of Rules, Prev: Implicit Rules, Up: Implicit Rules 6100 6738 6101 Using Implicit Rules6102 ==================== 6739 10.1 Using Implicit Rules 6740 ========================= 6103 6741 6104 6742 To allow `make' to find a customary method for updating a target file, … … 6173 6811 File: make.info, Node: Catalogue of Rules, Next: Implicit Variables, Prev: Using Implicit, Up: Implicit Rules 6174 6812 6175 Catalogue of Implicit Rules6176 =========================== 6813 10.2 Catalogue of Implicit Rules 6814 ================================ 6177 6815 6178 6816 Here is a catalogue of predefined implicit rules which are always … … 6182 6820 `--no-builtin-rules' option cancels all predefined rules. 6183 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 6184 6828 Not all of these rules will always be defined, even when the `-r' 6185 6829 option is not given. Many of the predefined implicit rules are … … 6187 6831 depends on the "suffix list" (the list of prerequisites of the special 6188 6832 target `.SUFFIXES'). The default suffix list is: `.out', `.a', `.ln', 6189 `.o', `.c', `.cc', `.C', `. p', `.f', `.F', `.r', `.y', `.l', `.s',6190 `. S', `.mod', `.sym', `.def', `.h', `.info', `.dvi', `.tex', `.texinfo',6191 `.texi ', `.txinfo', `.w', `.ch' `.web', `.sh', `.elc', `.el'. All of6192 the implicit rules described below whose prerequisites have one of 6193 these suffixes are actually suffix rules. If you modify the suffix 6194 list, the only predefined suffix rules in effect will be those named by 6195 one or two of the suffixes that are on the list you specify; rules 6196 whose suffixes fail to be on the list are disabled. *Note 6197 Old-Fashioned Suffix Rules: Suffix Rules, for full details on suffix 6198 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. 6199 6843 6200 6844 Compiling C programs … … 6203 6847 6204 6848 Compiling C++ programs 6205 `N.o' is made automatically from `N.cc' or `N.C' with a command of 6206 the form `$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'. We encourage you to 6207 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'. 6208 6853 6209 6854 Compiling Pascal programs … … 6379 7024 File: make.info, Node: Implicit Variables, Next: Chained Rules, Prev: Catalogue of Rules, Up: Implicit Rules 6380 7025 6381 Variables Used by Implicit Rules6382 ================================ 7026 10.3 Variables Used by Implicit Rules 7027 ===================================== 6383 7028 6384 7029 The commands in built-in implicit rules make liberal use of certain 6385 predefined variables. You can alter the se variables in the makefile,6386 with arguments to `make', or in the environment to alter how the 6387 implicit rules work without redefining the rules themselves. You can 6388 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 6389 7034 `--no-builtin-variables' option. 6390 7035 … … 6406 7051 argument, separate them with spaces. 6407 7052 6408 Here is a table of variables used as names of programs in built-in 6409 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. 6410 7062 6411 7063 `AR' 6412 Archive-maintaining program; default `ar'. 7064 Archive-maintaining program; default `ar'. 6413 7065 6414 7066 `AS' 6415 Program for doing assembly; default `as'.7067 Program for compiling assembly files; default `as'. 6416 7068 6417 7069 `CC' 6418 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'. 6419 7074 6420 7075 `CXX' 6421 Program for compiling C++ programs; default `g++'. 7076 Program for compiling C++ programs; default `g++'. 6422 7077 6423 7078 `CO' 6424 Program for extracting a file from RCS; default `co'. 7079 Program for extracting a file from RCS; default `co'. 6425 7080 6426 7081 `CPP' … … 6430 7085 `FC' 6431 7086 Program for compiling or preprocessing Fortran and Ratfor programs; 6432 default `f77'. 7087 default `f77'. 6433 7088 6434 7089 `GET' 6435 Program for extracting a file from SCCS; default `get'. 7090 Program for extracting a file from SCCS; default `get'. 6436 7091 6437 7092 `LEX' 6438 Program to use to turn Lex grammars into C programs or Ratfor 6439 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'. 6440 7105 6441 7106 `PC' 6442 Program for compiling Pascal programs; default `pc'. 6443 6444 `YACC' 6445 Program to use to turn Yacc grammars into C programs; default 6446 `yacc'. 6447 6448 `YACCR' 6449 Program to use to turn Yacc grammars into Ratfor programs; default 6450 `yacc -r'. 7107 Program for compiling Pascal programs; default `pc'. 6451 7108 6452 7109 `MAKEINFO' 6453 7110 Program to convert a Texinfo source file into an Info file; default 6454 `makeinfo'. 7111 `makeinfo'. 6455 7112 6456 7113 `TEX' 6457 Program to make TeX DVI files from TeX source; default `tex'. 7114 Program to make TeX DVI files from TeX source; default `tex'. 6458 7115 6459 7116 `TEXI2DVI' 6460 7117 Program to make TeX DVI files from Texinfo source; default 6461 `texi2dvi'. 7118 `texi2dvi'. 6462 7119 6463 7120 `WEAVE' 6464 Program to translate Web into TeX; default `weave'. 7121 Program to translate Web into TeX; default `weave'. 6465 7122 6466 7123 `CWEAVE' 6467 Program to translate C Web into TeX; default `cweave'. 7124 Program to translate C Web into TeX; default `cweave'. 6468 7125 6469 7126 `TANGLE' 6470 Program to translate Web into Pascal; default `tangle'. 7127 Program to translate Web into Pascal; default `tangle'. 6471 7128 6472 7129 `CTANGLE' 6473 Program to translate C Web into C; default `ctangle'. 7130 Program to translate C Web into C; default `ctangle'. 6474 7131 6475 7132 `RM' 6476 Command to remove a file; default `rm -f'. 7133 Command to remove a file; default `rm -f'. 6477 7134 6478 7135 Here is a table of variables whose values are additional arguments … … 6513 7170 Extra flags to give to Lex. 6514 7171 7172 `YFLAGS' 7173 Extra flags to give to Yacc. 7174 6515 7175 `PFLAGS' 6516 7176 Extra flags to give to the Pascal compiler. … … 6519 7179 Extra flags to give to the Fortran compiler for Ratfor programs. 6520 7180 6521 ` YFLAGS'6522 Extra flags to give to Yacc.7181 `LINTFLAGS' 7182 Extra flags to give to lint. 6523 7183 6524 7184 6525 7185 File: make.info, Node: Chained Rules, Next: Pattern Rules, Prev: Implicit Variables, Up: Implicit Rules 6526 7186 6527 Chains of Implicit Rules6528 ======================== 7187 10.4 Chains of Implicit Rules 7188 ============================= 6529 7189 6530 7190 Sometimes a file can be made by a sequence of implicit rules. For … … 6578 7238 as a prerequisite of the special target `.PRECIOUS' to preserve 6579 7239 intermediate files made by implicit rules whose target patterns match 6580 that file's name; see *Note Interrupts::. 7240 that file's name; see *Note Interrupts::. 6581 7241 6582 7242 A chain can involve more than two implicit rules. For example, it is … … 6600 7260 earlier in the ordering of rules. 6601 7261 6602 6603 File: make.info, Node: Pattern Rules, Next: Last Resort, Prev: Chained Rules, Up: Implicit Rules6604 6605 Defining and Redefining Pattern Rules6606 =====================================6607 6608 You define an implicit rule by writing a "pattern rule". A pattern6609 rule looks like an ordinary rule, except that its target contains the6610 character `%' (exactly one of them). The target is considered a6611 pattern for matching file names; the `%' can match any nonempty6612 substring, while other characters match only themselves. The6613 prerequisites likewise use `%' to show how their names relate to the6614 target name.6615 6616 Thus, a pattern rule `%.o : %.c' says how to make any file `STEM.o'6617 from another file `STEM.c'.6618 6619 Note that expansion using `%' in pattern rules occurs *after* any6620 variable or function expansions, which take place when the makefile is6621 read. *Note How to Use Variables: Using Variables, and *Note Functions6622 for Transforming Text: Functions.6623 6624 * Menu:6625 6626 * Pattern Intro:: An introduction to pattern rules.6627 * Pattern Examples:: Examples of pattern rules.6628 * Automatic Variables:: How to use automatic variables in the6629 commands of implicit rules.6630 * Pattern Match:: How patterns match.6631 * Match-Anything Rules:: Precautions you should take prior to6632 defining rules that can match any6633 target file whatever.6634 * Canceling Rules:: How to override or cancel built-in rules.6635 6636 6637 File: make.info, Node: Pattern Intro, Next: Pattern Examples, Prev: Pattern Rules, Up: Pattern Rules6638 6639 Introduction to Pattern Rules6640 -----------------------------6641 6642 A pattern rule contains the character `%' (exactly one of them) in the6643 target; otherwise, it looks exactly like an ordinary rule. The target6644 is a pattern for matching file names; the `%' matches any nonempty6645 substring, while other characters match only themselves.6646 6647 For example, `%.c' as a pattern matches any file name that ends in6648 `.c'. `s.%.c' as a pattern matches any file name that starts with6649 `s.', ends in `.c' and is at least five characters long. (There must6650 be at least one character to match the `%'.) The substring that the6651 `%' matches is called the "stem".6652 6653 `%' in a prerequisite of a pattern rule stands for the same stem6654 that was matched by the `%' in the target. In order for the pattern6655 rule to apply, its target pattern must match the file name under6656 consideration, and its prerequisite patterns must name files that exist6657 or can be made. These files become prerequisites of the target.6658 6659 Thus, a rule of the form6660 6661 %.o : %.c ; COMMAND...6662 6663 specifies how to make a file `N.o', with another file `N.c' as its6664 prerequisite, provided that `N.c' exists or can be made.6665 6666 There may also be prerequisites that do not use `%'; such a6667 prerequisite attaches to every file made by this pattern rule. These6668 unvarying prerequisites are useful occasionally.6669 6670 A pattern rule need not have any prerequisites that contain `%', or6671 in fact any prerequisites at all. Such a rule is effectively a general6672 wildcard. It provides a way to make any file that matches the target6673 pattern. *Note Last Resort::.6674 6675 Pattern rules may have more than one target. Unlike normal rules,6676 this does not act as many different rules with the same prerequisites6677 and commands. If a pattern rule has multiple targets, `make' knows that6678 the rule's commands are responsible for making all of the targets. The6679 commands are executed only once to make all the targets. When searching6680 for a pattern rule to match a target, the target patterns of a rule6681 other than the one that matches the target in need of a rule are6682 incidental: `make' worries only about giving commands and prerequisites6683 to the file presently in question. However, when this file's commands6684 are run, the other targets are marked as having been updated themselves.6685 6686 The order in which pattern rules appear in the makefile is important6687 since this is the order in which they are considered. Of equally6688 applicable rules, only the first one found is used. The rules you6689 write take precedence over those that are built in. Note however, that6690 a rule whose prerequisites actually exist or are mentioned always takes6691 priority over a rule with prerequisites that must be made by chaining6692 other implicit rules.6693 6694 6695 File: make.info, Node: Pattern Examples, Next: Automatic Variables, Prev: Pattern Intro, Up: Pattern Rules6696 6697 Pattern Rule Examples6698 ---------------------6699 6700 Here are some examples of pattern rules actually predefined in `make'.6701 First, the rule that compiles `.c' files into `.o' files:6702 6703 %.o : %.c6704 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@6705 6706 defines a rule that can make any file `X.o' from `X.c'. The command6707 uses the automatic variables `$@' and `$<' to substitute the names of6708 the target file and the source file in each case where the rule applies6709 (*note Automatic Variables::).6710 6711 Here is a second built-in rule:6712 6713 % :: RCS/%,v6714 $(CO) $(COFLAGS) $<6715 6716 defines a rule that can make any file `X' whatsoever from a6717 corresponding file `X,v' in the subdirectory `RCS'. Since the target6718 is `%', this rule will apply to any file whatever, provided the6719 appropriate prerequisite file exists. The double colon makes the rule6720 "terminal", which means that its prerequisite may not be an intermediate6721 file (*note Match-Anything Pattern Rules: Match-Anything Rules.).6722 6723 This pattern rule has two targets:6724 6725 %.tab.c %.tab.h: %.y6726 bison -d $<6727 6728 This tells `make' that the command `bison -d X.y' will make both6729 `X.tab.c' and `X.tab.h'. If the file `foo' depends on the files6730 `parse.tab.o' and `scan.o' and the file `scan.o' depends on the file6731 `parse.tab.h', when `parse.y' is changed, the command `bison -d parse.y'6732 will be executed only once, and the prerequisites of both `parse.tab.o'6733 and `scan.o' will be satisfied. (Presumably the file `parse.tab.o'6734 will be recompiled from `parse.tab.c' and the file `scan.o' from6735 `scan.c', while `foo' is linked from `parse.tab.o', `scan.o', and its6736 other prerequisites, and it will execute happily ever after.)6737 6738 6739 File: make.info, Node: Automatic Variables, Next: Pattern Match, Prev: Pattern Examples, Up: Pattern Rules6740 6741 Automatic Variables6742 -------------------6743 6744 Suppose you are writing a pattern rule to compile a `.c' file into a6745 `.o' file: how do you write the `cc' command so that it operates on the6746 right source file name? You cannot write the name in the command,6747 because the name is different each time the implicit rule is applied.6748 6749 What you do is use a special feature of `make', the "automatic6750 variables". These variables have values computed afresh for each rule6751 that is executed, based on the target and prerequisites of the rule.6752 In this example, you would use `$@' for the object file name and `$<'6753 for the source file name.6754 6755 It's very important that you recognize the limited scope in which6756 automatic variable values are available: they only have values within6757 the command script. In particular, you cannot use them anywhere within6758 the target or prerequisite lists of a rule; they have no value there6759 and will expand to the empty string. A common mistake is attempting to6760 use `$@' within the prerequisites list in a rule; this will not work.6761 However, see below for information on the SysV-style `$$@' variables.6762 6763 Here is a table of automatic variables:6764 6765 `$@'6766 The file name of the target of the rule. If the target is an6767 archive member, then `$@' is the name of the archive file. In a6768 pattern rule that has multiple targets (*note Introduction to6769 Pattern Rules: Pattern Intro.), `$@' is the name of whichever6770 target caused the rule's commands to be run.6771 6772 `$%'6773 The target member name, when the target is an archive member.6774 *Note Archives::. For example, if the target is `foo.a(bar.o)'6775 then `$%' is `bar.o' and `$@' is `foo.a'. `$%' is empty when the6776 target is not an archive member.6777 6778 `$<'6779 The name of the first prerequisite. If the target got its6780 commands from an implicit rule, this will be the first6781 prerequisite added by the implicit rule (*note Implicit Rules::).6782 6783 `$?'6784 The names of all the prerequisites that are newer than the target,6785 with spaces between them. For prerequisites which are archive6786 members, only the member named is used (*note Archives::).6787 6788 `$^'6789 The names of all the prerequisites, with spaces between them. For6790 prerequisites which are archive members, only the member named is6791 used (*note Archives::). A target has only one prerequisite on6792 each other file it depends on, no matter how many times each file6793 is listed as a prerequisite. So if you list a prerequisite more6794 than once for a target, the value of `$^' contains just one copy6795 of the name.6796 6797 `$+'6798 This is like `$^', but prerequisites listed more than once are6799 duplicated in the order they were listed in the makefile. This is6800 primarily useful for use in linking commands where it is6801 meaningful to repeat library file names in a particular order.6802 6803 `$*'6804 The stem with which an implicit rule matches (*note How Patterns6805 Match: Pattern Match.). If the target is `dir/a.foo.b' and the6806 target pattern is `a.%.b' then the stem is `dir/foo'. The stem is6807 useful for constructing names of related files.6808 6809 In a static pattern rule, the stem is part of the file name that6810 matched the `%' in the target pattern.6811 6812 In an explicit rule, there is no stem; so `$*' cannot be determined6813 in that way. Instead, if the target name ends with a recognized6814 suffix (*note Old-Fashioned Suffix Rules: Suffix Rules.), `$*' is6815 set to the target name minus the suffix. For example, if the6816 target name is `foo.c', then `$*' is set to `foo', since `.c' is a6817 suffix. GNU `make' does this bizarre thing only for compatibility6818 with other implementations of `make'. You should generally avoid6819 using `$*' except in implicit rules or static pattern rules.6820 6821 If the target name in an explicit rule does not end with a6822 recognized suffix, `$*' is set to the empty string for that rule.6823 6824 `$?' is useful even in explicit rules when you wish to operate on6825 only the prerequisites that have changed. For example, suppose that an6826 archive named `lib' is supposed to contain copies of several object6827 files. This rule copies just the changed object files into the archive:6828 6829 lib: foo.o bar.o lose.o win.o6830 ar r lib $?6831 6832 Of the variables listed above, four have values that are single file6833 names, and three have values that are lists of file names. These seven6834 have variants that get just the file's directory name or just the file6835 name within the directory. The variant variables' names are formed by6836 appending `D' or `F', respectively. These variants are semi-obsolete6837 in GNU `make' since the functions `dir' and `notdir' can be used to get6838 a similar effect (*note Functions for File Names: File Name6839 Functions.). Note, however, that the `D' variants all omit the6840 trailing slash which always appears in the output of the `dir'6841 function. Here is a table of the variants:6842 6843 `$(@D)'6844 The directory part of the file name of the target, with the6845 trailing slash removed. If the value of `$@' is `dir/foo.o' then6846 `$(@D)' is `dir'. This value is `.' if `$@' does not contain a6847 slash.6848 6849 `$(@F)'6850 The file-within-directory part of the file name of the target. If6851 the value of `$@' is `dir/foo.o' then `$(@F)' is `foo.o'. `$(@F)'6852 is equivalent to `$(notdir $@)'.6853 6854 `$(*D)'6855 `$(*F)'6856 The directory part and the file-within-directory part of the stem;6857 `dir' and `foo' in this example.6858 6859 `$(%D)'6860 `$(%F)'6861 The directory part and the file-within-directory part of the target6862 archive member name. This makes sense only for archive member6863 targets of the form `ARCHIVE(MEMBER)' and is useful only when6864 MEMBER may contain a directory name. (*Note Archive Members as6865 Targets: Archive Members.)6866 6867 `$(<D)'6868 `$(<F)'6869 The directory part and the file-within-directory part of the first6870 prerequisite.6871 6872 `$(^D)'6873 `$(^F)'6874 Lists of the directory parts and the file-within-directory parts6875 of all prerequisites.6876 6877 `$(+D)'6878 `$(+F)'6879 Lists of the directory parts and the file-within-directory parts6880 of all prerequisites, including multiple instances of duplicated6881 prerequisites.6882 6883 `$(?D)'6884 `$(?F)'6885 Lists of the directory parts and the file-within-directory parts of6886 all prerequisites that are newer than the target.6887 6888 Note that we use a special stylistic convention when we talk about6889 these automatic variables; we write "the value of `$<'", rather than6890 "the variable `<'" as we would write for ordinary variables such as6891 `objects' and `CFLAGS'. We think this convention looks more natural in6892 this special case. Please do not assume it has a deep significance;6893 `$<' refers to the variable named `<' just as `$(CFLAGS)' refers to the6894 variable named `CFLAGS'. You could just as well use `$(<)' in place of6895 `$<'.6896 6897 GNU `make' provides support for the SysV `make' feature that allows6898 special variable references `$$@', `$$(@D)', and `$$(@F)' (note the6899 required double-"$"!) to appear with the _prerequisites list_ (normal6900 automatic variables are available only within a command script). When6901 appearing in a prerequisites list, these variables are expanded to the6902 name of the target, the directory component of the target, and the file6903 component of the target, respectively.6904 6905 Note that these variables are available only within explicit and6906 static pattern (*note Static Pattern Rules: Static Pattern.) rules;6907 they have no special significance within implicit (suffix or pattern)6908 rules. Also note that while SysV `make' actually expands its entire6909 prerequisite list _twice_, GNU `make' does not behave this way: instead6910 it simply expands these special variables without re-expanding any6911 other part of the prerequisites list.6912 6913 This somewhat bizarre feature is included only to provide some6914 compatibility with SysV makefiles. In a native GNU `make' file there6915 are other ways to accomplish the same results. This feature is6916 disabled if the special pseudo target `.POSIX' is defined.6917 6918 6919 File: make.info, Node: Pattern Match, Next: Match-Anything Rules, Prev: Automatic Variables, Up: Pattern Rules6920 6921 How Patterns Match6922 ------------------6923 6924 A target pattern is composed of a `%' between a prefix and a suffix,6925 either or both of which may be empty. The pattern matches a file name6926 only if the file name starts with the prefix and ends with the suffix,6927 without overlap. The text between the prefix and the suffix is called6928 the "stem". Thus, when the pattern `%.o' matches the file name6929 `test.o', the stem is `test'. The pattern rule prerequisites are6930 turned into actual file names by substituting the stem for the character6931 `%'. Thus, if in the same example one of the prerequisites is written6932 as `%.c', it expands to `test.c'.6933 6934 When the target pattern does not contain a slash (and it usually does6935 not), directory names in the file names are removed from the file name6936 before it is compared with the target prefix and suffix. After the6937 comparison of the file name to the target pattern, the directory names,6938 along with the slash that ends them, are added on to the prerequisite6939 file names generated from the pattern rule's prerequisite patterns and6940 the file name. The directories are ignored only for the purpose of6941 finding an implicit rule to use, not in the application of that rule.6942 Thus, `e%t' matches the file name `src/eat', with `src/a' as the stem.6943 When prerequisites are turned into file names, the directories from the6944 stem are added at the front, while the rest of the stem is substituted6945 for the `%'. The stem `src/a' with a prerequisite pattern `c%r' gives6946 the file name `src/car'.6947 6948 6949 File: make.info, Node: Match-Anything Rules, Next: Canceling Rules, Prev: Pattern Match, Up: Pattern Rules6950 6951 Match-Anything Pattern Rules6952 ----------------------------6953 6954 When a pattern rule's target is just `%', it matches any file name6955 whatever. We call these rules "match-anything" rules. They are very6956 useful, but it can take a lot of time for `make' to think about them,6957 because it must consider every such rule for each file name listed6958 either as a target or as a prerequisite.6959 6960 Suppose the makefile mentions `foo.c'. For this target, `make'6961 would have to consider making it by linking an object file `foo.c.o',6962 or by C compilation-and-linking in one step from `foo.c.c', or by6963 Pascal compilation-and-linking from `foo.c.p', and many other6964 possibilities.6965 6966 We know these possibilities are ridiculous since `foo.c' is a C6967 source file, not an executable. If `make' did consider these6968 possibilities, it would ultimately reject them, because files such as6969 `foo.c.o' and `foo.c.p' would not exist. But these possibilities are so6970 numerous that `make' would run very slowly if it had to consider them.6971 6972 To gain speed, we have put various constraints on the way `make'6973 considers match-anything rules. There are two different constraints6974 that can be applied, and each time you define a match-anything rule you6975 must choose one or the other for that rule.6976 6977 One choice is to mark the match-anything rule as "terminal" by6978 defining it with a double colon. When a rule is terminal, it does not6979 apply unless its prerequisites actually exist. Prerequisites that6980 could be made with other implicit rules are not good enough. In other6981 words, no further chaining is allowed beyond a terminal rule.6982 6983 For example, the built-in implicit rules for extracting sources from6984 RCS and SCCS files are terminal; as a result, if the file `foo.c,v' does6985 not exist, `make' will not even consider trying to make it as an6986 intermediate file from `foo.c,v.o' or from `RCS/SCCS/s.foo.c,v'. RCS6987 and SCCS files are generally ultimate source files, which should not be6988 remade from any other files; therefore, `make' can save time by not6989 looking for ways to remake them.6990 6991 If you do not mark the match-anything rule as terminal, then it is6992 nonterminal. A nonterminal match-anything rule cannot apply to a file6993 name that indicates a specific type of data. A file name indicates a6994 specific type of data if some non-match-anything implicit rule target6995 matches it.6996 6997 For example, the file name `foo.c' matches the target for the pattern6998 rule `%.c : %.y' (the rule to run Yacc). Regardless of whether this6999 rule is actually applicable (which happens only if there is a file7000 `foo.y'), the fact that its target matches is enough to prevent7001 consideration of any nonterminal match-anything rules for the file7002 `foo.c'. Thus, `make' will not even consider trying to make `foo.c' as7003 an executable file from `foo.c.o', `foo.c.c', `foo.c.p', etc.7004 7005 The motivation for this constraint is that nonterminal match-anything7006 rules are used for making files containing specific types of data (such7007 as executable files) and a file name with a recognized suffix indicates7008 some other specific type of data (such as a C source file).7009 7010 Special built-in dummy pattern rules are provided solely to recognize7011 certain file names so that nonterminal match-anything rules will not be7012 considered. These dummy rules have no prerequisites and no commands,7013 and they are ignored for all other purposes. For example, the built-in7014 implicit rule7015 7016 %.p :7017 7018 exists to make sure that Pascal source files such as `foo.p' match a7019 specific target pattern and thereby prevent time from being wasted7020 looking for `foo.p.o' or `foo.p.c'.7021 7022 Dummy pattern rules such as the one for `%.p' are made for every7023 suffix listed as valid for use in suffix rules (*note Old-Fashioned7024 Suffix Rules: Suffix Rules.).7025 7026 7027 File: make.info, Node: Canceling Rules, Prev: Match-Anything Rules, Up: Pattern Rules7028 7029 Canceling Implicit Rules7030 ------------------------7031 7032 You can override a built-in implicit rule (or one you have defined7033 yourself) by defining a new pattern rule with the same target and7034 prerequisites, but different commands. When the new rule is defined,7035 the built-in one is replaced. The new rule's position in the sequence7036 of implicit rules is determined by where you write the new rule.7037 7038 You can cancel a built-in implicit rule by defining a pattern rule7039 with the same target and prerequisites, but no commands. For example,7040 the following would cancel the rule that runs the assembler:7041 7042 %.o : %.s7043 7044 7045 File: make.info, Node: Last Resort, Next: Suffix Rules, Prev: Pattern Rules, Up: Implicit Rules7046 7047 Defining Last-Resort Default Rules7048 ==================================7049 7050 You can define a last-resort implicit rule by writing a terminal7051 match-anything pattern rule with no prerequisites (*note Match-Anything7052 Rules::). This is just like any other pattern rule; the only thing7053 special about it is that it will match any target. So such a rule's7054 commands are used for all targets and prerequisites that have no7055 commands of their own and for which no other implicit rule applies.7056 7057 For example, when testing a makefile, you might not care if the7058 source files contain real data, only that they exist. Then you might7059 do this:7060 7061 %::7062 touch $@7063 7064 to cause all the source files needed (as prerequisites) to be created7065 automatically.7066 7067 You can instead define commands to be used for targets for which7068 there are no rules at all, even ones which don't specify commands. You7069 do this by writing a rule for the target `.DEFAULT'. Such a rule's7070 commands are used for all prerequisites which do not appear as targets7071 in any explicit rule, and for which no implicit rule applies.7072 Naturally, there is no `.DEFAULT' rule unless you write one.7073 7074 If you use `.DEFAULT' with no commands or prerequisites:7075 7076 .DEFAULT:7077 7078 the commands previously stored for `.DEFAULT' are cleared. Then `make'7079 acts as if you had never defined `.DEFAULT' at all.7080 7081 If you do not want a target to get the commands from a match-anything7082 pattern rule or `.DEFAULT', but you also do not want any commands to be7083 run for the target, you can give it empty commands (*note Defining7084 Empty Commands: Empty Commands.).7085 7086 You can use a last-resort rule to override part of another makefile.7087 *Note Overriding Part of Another Makefile: Overriding Makefiles.7088 7089 7090 File: make.info, Node: Suffix Rules, Next: Implicit Rule Search, Prev: Last Resort, Up: Implicit Rules7091 7092 Old-Fashioned Suffix Rules7093 ==========================7094 7095 "Suffix rules" are the old-fashioned way of defining implicit rules for7096 `make'. Suffix rules are obsolete because pattern rules are more7097 general and clearer. They are supported in GNU `make' for7098 compatibility with old makefiles. They come in two kinds:7099 "double-suffix" and "single-suffix".7100 7101 A double-suffix rule is defined by a pair of suffixes: the target7102 suffix and the source suffix. It matches any file whose name ends with7103 the target suffix. The corresponding implicit prerequisite is made by7104 replacing the target suffix with the source suffix in the file name. A7105 two-suffix rule whose target and source suffixes are `.o' and `.c' is7106 equivalent to the pattern rule `%.o : %.c'.7107 7108 A single-suffix rule is defined by a single suffix, which is the7109 source suffix. It matches any file name, and the corresponding implicit7110 prerequisite name is made by appending the source suffix. A7111 single-suffix rule whose source suffix is `.c' is equivalent to the7112 pattern rule `% : %.c'.7113 7114 Suffix rule definitions are recognized by comparing each rule's7115 target against a defined list of known suffixes. When `make' sees a7116 rule whose target is a known suffix, this rule is considered a7117 single-suffix rule. When `make' sees a rule whose target is two known7118 suffixes concatenated, this rule is taken as a double-suffix rule.7119 7120 For example, `.c' and `.o' are both on the default list of known7121 suffixes. Therefore, if you define a rule whose target is `.c.o',7122 `make' takes it to be a double-suffix rule with source suffix `.c' and7123 target suffix `.o'. Here is the old-fashioned way to define the rule7124 for compiling a C source file:7125 7126 .c.o:7127 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7128 7129 Suffix rules cannot have any prerequisites of their own. If they7130 have any, they are treated as normal files with funny names, not as7131 suffix rules. Thus, the rule:7132 7133 .c.o: foo.h7134 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7135 7136 tells how to make the file `.c.o' from the prerequisite file `foo.h',7137 and is not at all like the pattern rule:7138 7139 %.o: %.c foo.h7140 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<7141 7142 which tells how to make `.o' files from `.c' files, and makes all `.o'7143 files using this pattern rule also depend on `foo.h'.7144 7145 Suffix rules with no commands are also meaningless. They do not7146 remove previous rules as do pattern rules with no commands (*note7147 Canceling Implicit Rules: Canceling Rules.). They simply enter the7148 suffix or pair of suffixes concatenated as a target in the data base.7149 7150 The known suffixes are simply the names of the prerequisites of the7151 special target `.SUFFIXES'. You can add your own suffixes by writing a7152 rule for `.SUFFIXES' that adds more prerequisites, as in:7153 7154 .SUFFIXES: .hack .win7155 7156 which adds `.hack' and `.win' to the end of the list of suffixes.7157 7158 If you wish to eliminate the default known suffixes instead of just7159 adding to them, write a rule for `.SUFFIXES' with no prerequisites. By7160 special dispensation, this eliminates all existing prerequisites of7161 `.SUFFIXES'. You can then write another rule to add the suffixes you7162 want. For example,7163 7164 .SUFFIXES: # Delete the default suffixes7165 .SUFFIXES: .c .o .h # Define our suffix list7166 7167 The `-r' or `--no-builtin-rules' flag causes the default list of7168 suffixes to be empty.7169 7170 The variable `SUFFIXES' is defined to the default list of suffixes7171 before `make' reads any makefiles. You can change the list of suffixes7172 with a rule for the special target `.SUFFIXES', but that does not alter7173 this variable.7174
Note:
See TracChangeset
for help on using the changeset viewer.