1 1.1 christos \input texinfo @c -*- Texinfo -*- 2 1.1 christos @c %**start of header 3 1.1 christos @setfilename make.info 4 1.1 christos 5 1.1 christos @include version.texi 6 1.1 christos @set EDITION 0.70 7 1.1 christos @set RCSID $Id: make.texi,v 1.1.1.1 2014/08/18 06:47:47 christos Exp $ 8 1.1 christos 9 1.1 christos @settitle GNU @code{make} 10 1.1 christos @setchapternewpage odd 11 1.1 christos @c Combine the variable and function indices: 12 1.1 christos @syncodeindex vr fn 13 1.1 christos @c Combine the program and concept indices: 14 1.1 christos @syncodeindex pg cp 15 1.1 christos @c FSF publishers: format makebook.texi instead of using this file directly. 16 1.1 christos @c ISBN provided by Lisa M. Opus Goldstein <opus (a] gnu.org>, 5 May 2004 17 1.1 christos @set ISBN 1-882114-83-5 18 1.1 christos @c %**end of header 19 1.1 christos 20 1.1 christos @copying 21 1.1 christos This file documents the GNU @code{make} utility, which determines 22 1.1 christos automatically which pieces of a large program need to be recompiled, 23 1.1 christos and issues the commands to recompile them. 24 1.1 christos 25 1.1 christos This is Edition @value{EDITION}, last updated @value{UPDATED}, 26 1.1 christos of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}. 27 1.1 christos 28 1.1 christos Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 29 1.1 christos 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 30 1.1 christos Free Software Foundation, Inc. 31 1.1 christos 32 1.1 christos @quotation 33 1.1 christos Permission is granted to copy, distribute and/or modify this document 34 1.1 christos under the terms of the GNU Free Documentation License, Version 1.2 or 35 1.1 christos any later version published by the Free Software Foundation; with no 36 1.1 christos Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,'' 37 1.1 christos and with the Back-Cover Texts as in (a) below. A copy of the 38 1.1 christos license is included in the section entitled ``GNU Free Documentation 39 1.1 christos License.'' 40 1.1 christos 41 1.1 christos (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify 42 1.1 christos this GNU Manual, like GNU software. Copies published by the Free 43 1.1 christos Software Foundation raise funds for GNU development.'' 44 1.1 christos @end quotation 45 1.1 christos @end copying 46 1.1 christos 47 1.1 christos @c finalout 48 1.1 christos 49 1.1 christos @c ISPELL CHECK: done, 10 June 1993 --roland 50 1.1 christos @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz 51 1.1 christos 52 1.1 christos 53 1.1 christos @dircategory GNU Packages 54 1.1 christos @direntry 55 1.1 christos * Make: (make). Remake files automatically. 56 1.1 christos @end direntry 57 1.1 christos 58 1.1 christos @iftex 59 1.1 christos @shorttitlepage GNU Make 60 1.1 christos @end iftex 61 1.1 christos @titlepage 62 1.1 christos @title GNU Make 63 1.1 christos @subtitle A Program for Directing Recompilation 64 1.1 christos @subtitle GNU @code{make} Version @value{VERSION} 65 1.1 christos @subtitle @value{UPDATED-MONTH} 66 1.1 christos @author Richard M. Stallman, Roland McGrath, Paul D. Smith 67 1.1 christos @page 68 1.1 christos @vskip 0pt plus 1filll 69 1.1 christos @insertcopying 70 1.1 christos @sp 2 71 1.1 christos Published by the Free Software Foundation @* 72 1.1 christos 51 Franklin St. -- Fifth Floor @* 73 1.1 christos Boston, MA 02110-1301 USA @* 74 1.1 christos ISBN @value{ISBN} @* 75 1.1 christos @sp 2 76 1.1 christos Cover art by Etienne Suvasa. 77 1.1 christos @end titlepage 78 1.1 christos 79 1.1 christos @summarycontents 80 1.1 christos @contents 81 1.1 christos 82 1.1 christos @ifnottex 83 1.1 christos @node Top, Overview, (dir), (dir) 84 1.1 christos @top GNU @code{make} 85 1.1 christos 86 1.1 christos @insertcopying 87 1.1 christos @end ifnottex 88 1.1 christos 89 1.1 christos @menu 90 1.1 christos * Overview:: Overview of @code{make}. 91 1.1 christos * Introduction:: An introduction to @code{make}. 92 1.1 christos * Makefiles:: Makefiles tell @code{make} what to do. 93 1.1 christos * Rules:: Rules describe when a file must be remade. 94 1.1 christos * Commands:: Commands say how to remake a file. 95 1.1 christos * Using Variables:: You can use variables to avoid repetition. 96 1.1 christos * Conditionals:: Use or ignore parts of the makefile based 97 1.1 christos on the values of variables. 98 1.1 christos * Functions:: Many powerful ways to manipulate text. 99 1.1 christos * Invoking make: Running. How to invoke @code{make} on the command line. 100 1.1 christos * Implicit Rules:: Use implicit rules to treat many files alike, 101 1.1 christos based on their file names. 102 1.1 christos * Archives:: How @code{make} can update library archives. 103 1.1 christos * Features:: Features GNU @code{make} has over other @code{make}s. 104 1.1 christos * Missing:: What GNU @code{make} lacks from other @code{make}s. 105 1.1 christos * Makefile Conventions:: Conventions for writing makefiles for 106 1.1 christos GNU programs. 107 1.1 christos * Quick Reference:: A quick reference for experienced users. 108 1.1 christos * Error Messages:: A list of common errors generated by @code{make}. 109 1.1 christos * Complex Makefile:: A real example of a straightforward, 110 1.1 christos but nontrivial, makefile. 111 1.1 christos 112 1.1 christos * GNU Free Documentation License:: License for copying this manual 113 1.1 christos * Concept Index:: Index of Concepts 114 1.1 christos * Name Index:: Index of Functions, Variables, & Directives 115 1.1 christos 116 1.1 christos @detailmenu 117 1.1 christos --- The Detailed Node Listing --- 118 1.1 christos 119 1.1 christos Overview of @code{make} 120 1.1 christos 121 1.1 christos * Preparing:: Preparing and Running Make 122 1.1 christos * Reading:: On Reading this Text 123 1.1 christos * Bugs:: Problems and Bugs 124 1.1 christos 125 1.1 christos An Introduction to Makefiles 126 1.1 christos 127 1.1 christos * Rule Introduction:: What a rule looks like. 128 1.1 christos * Simple Makefile:: A Simple Makefile 129 1.1 christos * How Make Works:: How @code{make} Processes This Makefile 130 1.1 christos * Variables Simplify:: Variables Make Makefiles Simpler 131 1.1 christos * make Deduces:: Letting @code{make} Deduce the Commands 132 1.1 christos * Combine By Prerequisite:: Another Style of Makefile 133 1.1 christos * Cleanup:: Rules for Cleaning the Directory 134 1.1 christos 135 1.1 christos Writing Makefiles 136 1.1 christos 137 1.1 christos * Makefile Contents:: What makefiles contain. 138 1.1 christos * Makefile Names:: How to name your makefile. 139 1.1 christos * Include:: How one makefile can use another makefile. 140 1.1 christos * MAKEFILES Variable:: The environment can specify extra makefiles. 141 1.1 christos * MAKEFILE_LIST Variable:: Discover which makefiles have been read. 142 1.1 christos * Special Variables:: Other special variables. 143 1.1 christos * Remaking Makefiles:: How makefiles get remade. 144 1.1 christos * Overriding Makefiles:: How to override part of one makefile 145 1.1 christos with another makefile. 146 1.1 christos * Reading Makefiles:: How makefiles are parsed. 147 1.1 christos * Secondary Expansion:: How and when secondary expansion is performed. 148 1.1 christos 149 1.1 christos Writing Rules 150 1.1 christos 151 1.1 christos * Rule Example:: An example explained. 152 1.1 christos * Rule Syntax:: General syntax explained. 153 1.1 christos * Prerequisite Types:: There are two types of prerequisites. 154 1.1 christos * Wildcards:: Using wildcard characters such as `*'. 155 1.1 christos * Directory Search:: Searching other directories for source files. 156 1.1 christos * Phony Targets:: Using a target that is not a real file's name. 157 1.1 christos * Force Targets:: You can use a target without commands 158 1.1 christos or prerequisites to mark other targets 159 1.1 christos as phony. 160 1.1 christos * Empty Targets:: When only the date matters and the 161 1.1 christos files are empty. 162 1.1 christos * Special Targets:: Targets with special built-in meanings. 163 1.1 christos * Multiple Targets:: When to make use of several targets in a rule. 164 1.1 christos * Multiple Rules:: How to use several rules with the same target. 165 1.1 christos * Static Pattern:: Static pattern rules apply to multiple targets 166 1.1 christos and can vary the prerequisites according to 167 1.1 christos the target name. 168 1.1 christos * Double-Colon:: How to use a special kind of rule to allow 169 1.1 christos several independent rules for one target. 170 1.1 christos * Automatic Prerequisites:: How to automatically generate rules giving 171 1.1 christos prerequisites from source files themselves. 172 1.1 christos 173 1.1 christos Using Wildcard Characters in File Names 174 1.1 christos 175 1.1 christos * Wildcard Examples:: Several examples 176 1.1 christos * Wildcard Pitfall:: Problems to avoid. 177 1.1 christos * Wildcard Function:: How to cause wildcard expansion where 178 1.1 christos it does not normally take place. 179 1.1 christos 180 1.1 christos Searching Directories for Prerequisites 181 1.1 christos 182 1.1 christos * General Search:: Specifying a search path that applies 183 1.1 christos to every prerequisite. 184 1.1 christos * Selective Search:: Specifying a search path 185 1.1 christos for a specified class of names. 186 1.1 christos * Search Algorithm:: When and how search paths are applied. 187 1.1 christos * Commands/Search:: How to write shell commands that work together 188 1.1 christos with search paths. 189 1.1 christos * Implicit/Search:: How search paths affect implicit rules. 190 1.1 christos * Libraries/Search:: Directory search for link libraries. 191 1.1 christos 192 1.1 christos Static Pattern Rules 193 1.1 christos 194 1.1 christos * Static Usage:: The syntax of static pattern rules. 195 1.1 christos * Static versus Implicit:: When are they better than implicit rules? 196 1.1 christos 197 1.1 christos Writing the Commands in Rules 198 1.1 christos 199 1.1 christos * Command Syntax:: Command syntax features and pitfalls. 200 1.1 christos * Echoing:: How to control when commands are echoed. 201 1.1 christos * Execution:: How commands are executed. 202 1.1 christos * Parallel:: How commands can be executed in parallel. 203 1.1 christos * Errors:: What happens after a command execution error. 204 1.1 christos * Interrupts:: What happens when a command is interrupted. 205 1.1 christos * Recursion:: Invoking @code{make} from makefiles. 206 1.1 christos * Sequences:: Defining canned sequences of commands. 207 1.1 christos * Empty Commands:: Defining useful, do-nothing commands. 208 1.1 christos 209 1.1 christos Command Syntax 210 1.1 christos 211 1.1 christos * Splitting Lines:: Breaking long command lines for readability. 212 1.1 christos * Variables in Commands:: Using @code{make} variables in commands. 213 1.1 christos 214 1.1 christos Command Execution 215 1.1 christos 216 1.1 christos * Choosing the Shell:: How @code{make} chooses the shell used 217 1.1 christos to run commands. 218 1.1 christos 219 1.1 christos Recursive Use of @code{make} 220 1.1 christos 221 1.1 christos * MAKE Variable:: The special effects of using @samp{$(MAKE)}. 222 1.1 christos * Variables/Recursion:: How to communicate variables to a sub-@code{make}. 223 1.1 christos * Options/Recursion:: How to communicate options to a sub-@code{make}. 224 1.1 christos * -w Option:: How the @samp{-w} or @samp{--print-directory} option 225 1.1 christos helps debug use of recursive @code{make} commands. 226 1.1 christos 227 1.1 christos How to Use Variables 228 1.1 christos 229 1.1 christos * Reference:: How to use the value of a variable. 230 1.1 christos * Flavors:: Variables come in two flavors. 231 1.1 christos * Advanced:: Advanced features for referencing a variable. 232 1.1 christos * Values:: All the ways variables get their values. 233 1.1 christos * Setting:: How to set a variable in the makefile. 234 1.1 christos * Appending:: How to append more text to the old value 235 1.1 christos of a variable. 236 1.1 christos * Override Directive:: How to set a variable in the makefile even if 237 1.1 christos the user has set it with a command argument. 238 1.1 christos * Defining:: An alternate way to set a variable 239 1.1 christos to a verbatim string. 240 1.1 christos * Environment:: Variable values can come from the environment. 241 1.1 christos * Target-specific:: Variable values can be defined on a per-target 242 1.1 christos basis. 243 1.1 christos * Pattern-specific:: Target-specific variable values can be applied 244 1.1 christos to a group of targets that match a pattern. 245 1.1 christos 246 1.1 christos Advanced Features for Reference to Variables 247 1.1 christos 248 1.1 christos * Substitution Refs:: Referencing a variable with 249 1.1 christos substitutions on the value. 250 1.1 christos * Computed Names:: Computing the name of the variable to refer to. 251 1.1 christos 252 1.1 christos Conditional Parts of Makefiles 253 1.1 christos 254 1.1 christos * Conditional Example:: Example of a conditional 255 1.1 christos * Conditional Syntax:: The syntax of conditionals. 256 1.1 christos * Testing Flags:: Conditionals that test flags. 257 1.1 christos 258 1.1 christos Functions for Transforming Text 259 1.1 christos 260 1.1 christos * Syntax of Functions:: How to write a function call. 261 1.1 christos * Text Functions:: General-purpose text manipulation functions. 262 1.1 christos * File Name Functions:: Functions for manipulating file names. 263 1.1 christos * Conditional Functions:: Functions that implement conditions. 264 1.1 christos * Foreach Function:: Repeat some text with controlled variation. 265 1.1 christos * Call Function:: Expand a user-defined function. 266 1.1 christos * Value Function:: Return the un-expanded value of a variable. 267 1.1 christos * Eval Function:: Evaluate the arguments as makefile syntax. 268 1.1 christos * Origin Function:: Find where a variable got its value. 269 1.1 christos * Flavor Function:: Find out the flavor of a variable. 270 1.1 christos * Shell Function:: Substitute the output of a shell command. 271 1.1 christos * Make Control Functions:: Functions that control how make runs. 272 1.1 christos 273 1.1 christos How to Run @code{make} 274 1.1 christos 275 1.1 christos * Makefile Arguments:: How to specify which makefile to use. 276 1.1 christos * Goals:: How to use goal arguments to specify which 277 1.1 christos parts of the makefile to use. 278 1.1 christos * Instead of Execution:: How to use mode flags to specify what 279 1.1 christos kind of thing to do with the commands 280 1.1 christos in the makefile other than simply 281 1.1 christos execute them. 282 1.1 christos * Avoiding Compilation:: How to avoid recompiling certain files. 283 1.1 christos * Overriding:: How to override a variable to specify 284 1.1 christos an alternate compiler and other things. 285 1.1 christos * Testing:: How to proceed past some errors, to 286 1.1 christos test compilation. 287 1.1 christos * Options Summary:: Summary of Options 288 1.1 christos 289 1.1 christos Using Implicit Rules 290 1.1 christos 291 1.1 christos * Using Implicit:: How to use an existing implicit rule 292 1.1 christos to get the commands for updating a file. 293 1.1 christos * Catalogue of Rules:: A list of built-in implicit rules. 294 1.1 christos * Implicit Variables:: How to change what predefined rules do. 295 1.1 christos * Chained Rules:: How to use a chain of implicit rules. 296 1.1 christos * Pattern Rules:: How to define new implicit rules. 297 1.1 christos * Last Resort:: How to define commands for rules which 298 1.1 christos cannot find any. 299 1.1 christos * Suffix Rules:: The old-fashioned style of implicit rule. 300 1.1 christos * Implicit Rule Search:: The precise algorithm for applying 301 1.1 christos implicit rules. 302 1.1 christos 303 1.1 christos Defining and Redefining Pattern Rules 304 1.1 christos 305 1.1 christos * Pattern Intro:: An introduction to pattern rules. 306 1.1 christos * Pattern Examples:: Examples of pattern rules. 307 1.1 christos * Automatic Variables:: How to use automatic variables in the 308 1.1 christos commands of implicit rules. 309 1.1 christos * Pattern Match:: How patterns match. 310 1.1 christos * Match-Anything Rules:: Precautions you should take prior to 311 1.1 christos defining rules that can match any 312 1.1 christos target file whatever. 313 1.1 christos * Canceling Rules:: How to override or cancel built-in rules. 314 1.1 christos 315 1.1 christos Using @code{make} to Update Archive Files 316 1.1 christos 317 1.1 christos * Archive Members:: Archive members as targets. 318 1.1 christos * Archive Update:: The implicit rule for archive member targets. 319 1.1 christos * Archive Pitfalls:: Dangers to watch out for when using archives. 320 1.1 christos * Archive Suffix Rules:: You can write a special kind of suffix rule 321 1.1 christos for updating archives. 322 1.1 christos 323 1.1 christos Implicit Rule for Archive Member Targets 324 1.1 christos 325 1.1 christos * Archive Symbols:: How to update archive symbol directories. 326 1.1 christos 327 1.1 christos @end detailmenu 328 1.1 christos @end menu 329 1.1 christos 330 1.1 christos @node Overview, Introduction, Top, Top 331 1.1 christos @comment node-name, next, previous, up 332 1.1 christos @chapter Overview of @code{make} 333 1.1 christos 334 1.1 christos The @code{make} utility automatically determines which pieces of a large 335 1.1 christos program need to be recompiled, and issues commands to recompile them. 336 1.1 christos This manual describes GNU @code{make}, which was implemented by Richard 337 1.1 christos Stallman and Roland McGrath. Development since Version 3.76 has been 338 1.1 christos handled by Paul D. Smith. 339 1.1 christos 340 1.1 christos GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard 341 1.1 christos 1003.2-1992} (POSIX.2). 342 1.1 christos @cindex POSIX 343 1.1 christos @cindex IEEE Standard 1003.2 344 1.1 christos @cindex standards conformance 345 1.1 christos 346 1.1 christos Our examples show C programs, since they are most common, but you can use 347 1.1 christos @code{make} with any programming language whose compiler can be run with a 348 1.1 christos shell command. Indeed, @code{make} is not limited to programs. You can 349 1.1 christos use it to describe any task where some files must be updated automatically 350 1.1 christos from others whenever the others change. 351 1.1 christos 352 1.1 christos @menu 353 1.1 christos * Preparing:: Preparing and Running Make 354 1.1 christos * Reading:: On Reading this Text 355 1.1 christos * Bugs:: Problems and Bugs 356 1.1 christos @end menu 357 1.1 christos 358 1.1 christos @node Preparing, Reading, Overview, Overview 359 1.1 christos @ifnottex 360 1.1 christos @heading Preparing and Running Make 361 1.1 christos @end ifnottex 362 1.1 christos 363 1.1 christos To prepare to use @code{make}, you must write a file called 364 1.1 christos the @dfn{makefile} that describes the relationships among files 365 1.1 christos in your program and provides commands for updating each file. 366 1.1 christos In a program, typically, the executable file is updated from object 367 1.1 christos files, which are in turn made by compiling source files.@refill 368 1.1 christos 369 1.1 christos Once a suitable makefile exists, each time you change some source files, 370 1.1 christos this simple shell command: 371 1.1 christos 372 1.1 christos @example 373 1.1 christos make 374 1.1 christos @end example 375 1.1 christos 376 1.1 christos @noindent 377 1.1 christos suffices to perform all necessary recompilations. The @code{make} program 378 1.1 christos uses the makefile data base and the last-modification times of the files to 379 1.1 christos decide which of the files need to be updated. For each of those files, it 380 1.1 christos issues the commands recorded in the data base. 381 1.1 christos 382 1.1 christos You can provide command line arguments to @code{make} to control which 383 1.1 christos files should be recompiled, or how. @xref{Running, ,How to Run 384 1.1 christos @code{make}}. 385 1.1 christos 386 1.1 christos @node Reading, Bugs, Preparing, Overview 387 1.1 christos @section How to Read This Manual 388 1.1 christos 389 1.1 christos If you are new to @code{make}, or are looking for a general 390 1.1 christos introduction, read the first few sections of each chapter, skipping the 391 1.1 christos later sections. In each chapter, the first few sections contain 392 1.1 christos introductory or general information and the later sections contain 393 1.1 christos specialized or technical information. 394 1.1 christos @ifnottex 395 1.1 christos The exception is the second chapter, @ref{Introduction, ,An 396 1.1 christos Introduction to Makefiles}, all of which is introductory. 397 1.1 christos @end ifnottex 398 1.1 christos @iftex 399 1.1 christos The exception is @ref{Introduction, ,An Introduction to Makefiles}, 400 1.1 christos all of which is introductory. 401 1.1 christos @end iftex 402 1.1 christos 403 1.1 christos If you are familiar with other @code{make} programs, see @ref{Features, 404 1.1 christos ,Features of GNU @code{make}}, which lists the enhancements GNU 405 1.1 christos @code{make} has, and @ref{Missing, ,Incompatibilities and Missing 406 1.1 christos Features}, which explains the few things GNU @code{make} lacks that 407 1.1 christos others have. 408 1.1 christos 409 1.1 christos For a quick summary, see @ref{Options Summary}, @ref{Quick Reference}, 410 1.1 christos and @ref{Special Targets}. 411 1.1 christos 412 1.1 christos @node Bugs, , Reading, Overview 413 1.1 christos @section Problems and Bugs 414 1.1 christos @cindex reporting bugs 415 1.1 christos @cindex bugs, reporting 416 1.1 christos @cindex problems and bugs, reporting 417 1.1 christos 418 1.1 christos If you have problems with GNU @code{make} or think you've found a bug, 419 1.1 christos please report it to the developers; we cannot promise to do anything but 420 1.1 christos we might well want to fix it. 421 1.1 christos 422 1.1 christos Before reporting a bug, make sure you've actually found a real bug. 423 1.1 christos Carefully reread the documentation and see if it really says you can do 424 1.1 christos what you're trying to do. If it's not clear whether you should be able 425 1.1 christos to do something or not, report that too; it's a bug in the 426 1.1 christos documentation! 427 1.1 christos 428 1.1 christos Before reporting a bug or trying to fix it yourself, try to isolate it 429 1.1 christos to the smallest possible makefile that reproduces the problem. Then 430 1.1 christos send us the makefile and the exact results @code{make} gave you, 431 1.1 christos including any error or warning messages. Please don't paraphrase 432 1.1 christos these messages: it's best to cut and paste them into your report. 433 1.1 christos When generating this small makefile, be sure to not use any non-free 434 1.1 christos or unusual tools in your commands: you can almost always emulate what 435 1.1 christos such a tool would do with simple shell commands. Finally, be sure to 436 1.1 christos explain what you expected to occur; this will help us decide whether 437 1.1 christos the problem was really in the documentation. 438 1.1 christos 439 1.1 christos Once you have a precise problem you can report it in one of two ways. 440 1.1 christos Either send electronic mail to: 441 1.1 christos 442 1.1 christos @example 443 1.1 christos bug-make@@gnu.org 444 1.1 christos @end example 445 1.1 christos 446 1.1 christos @noindent 447 1.1 christos or use our Web-based project management tool, at: 448 1.1 christos 449 1.1 christos @example 450 1.1 christos http://savannah.gnu.org/projects/make/ 451 1.1 christos @end example 452 1.1 christos 453 1.1 christos @noindent 454 1.1 christos In addition to the information above, please be careful to include the 455 1.1 christos version number of @code{make} you are using. You can get this 456 1.1 christos information with the command @samp{make --version}. Be sure also to 457 1.1 christos include the type of machine and operating system you are using. One 458 1.1 christos way to obtain this information is by looking at the final lines of 459 1.1 christos output from the command @samp{make --help}. 460 1.1 christos 461 1.1 christos @node Introduction, Makefiles, Overview, Top 462 1.1 christos @comment node-name, next, previous, up 463 1.1 christos @chapter An Introduction to Makefiles 464 1.1 christos 465 1.1 christos You need a file called a @dfn{makefile} to tell @code{make} what to do. 466 1.1 christos Most often, the makefile tells @code{make} how to compile and link a 467 1.1 christos program. 468 1.1 christos @cindex makefile 469 1.1 christos 470 1.1 christos In this chapter, we will discuss a simple makefile that describes how to 471 1.1 christos compile and link a text editor which consists of eight C source files 472 1.1 christos and three header files. The makefile can also tell @code{make} how to 473 1.1 christos run miscellaneous commands when explicitly asked (for example, to remove 474 1.1 christos certain files as a clean-up operation). To see a more complex example 475 1.1 christos of a makefile, see @ref{Complex Makefile}. 476 1.1 christos 477 1.1 christos When @code{make} recompiles the editor, each changed C source file 478 1.1 christos must be recompiled. If a header file has changed, each C source file 479 1.1 christos that includes the header file must be recompiled to be safe. Each 480 1.1 christos compilation produces an object file corresponding to the source file. 481 1.1 christos Finally, if any source file has been recompiled, all the object files, 482 1.1 christos whether newly made or saved from previous compilations, must be linked 483 1.1 christos together to produce the new executable editor. 484 1.1 christos @cindex recompilation 485 1.1 christos @cindex editor 486 1.1 christos 487 1.1 christos @menu 488 1.1 christos * Rule Introduction:: What a rule looks like. 489 1.1 christos * Simple Makefile:: A Simple Makefile 490 1.1 christos * How Make Works:: How @code{make} Processes This Makefile 491 1.1 christos * Variables Simplify:: Variables Make Makefiles Simpler 492 1.1 christos * make Deduces:: Letting @code{make} Deduce the Commands 493 1.1 christos * Combine By Prerequisite:: Another Style of Makefile 494 1.1 christos * Cleanup:: Rules for Cleaning the Directory 495 1.1 christos @end menu 496 1.1 christos 497 1.1 christos @node Rule Introduction, Simple Makefile, Introduction, Introduction 498 1.1 christos @comment node-name, next, previous, up 499 1.1 christos @section What a Rule Looks Like 500 1.1 christos @cindex rule, introduction to 501 1.1 christos @cindex makefile rule parts 502 1.1 christos @cindex parts of makefile rule 503 1.1 christos 504 1.1 christos A simple makefile consists of ``rules'' with the following shape: 505 1.1 christos 506 1.1 christos @cindex targets, introduction to 507 1.1 christos @cindex prerequisites, introduction to 508 1.1 christos @cindex commands, introduction to 509 1.1 christos @example 510 1.1 christos @group 511 1.1 christos @var{target} @dots{} : @var{prerequisites} @dots{} 512 1.1 christos @var{command} 513 1.1 christos @dots{} 514 1.1 christos @dots{} 515 1.1 christos @end group 516 1.1 christos @end example 517 1.1 christos 518 1.1 christos A @dfn{target} is usually the name of a file that is generated by a 519 1.1 christos program; examples of targets are executable or object files. A target 520 1.1 christos can also be the name of an action to carry out, such as @samp{clean} 521 1.1 christos (@pxref{Phony Targets}). 522 1.1 christos 523 1.1 christos A @dfn{prerequisite} is a file that is used as input to create the 524 1.1 christos target. A target often depends on several files. 525 1.1 christos 526 1.1 christos @cindex tabs in rules 527 1.1 christos A @dfn{command} is an action that @code{make} carries out. 528 1.1 christos A rule may have more than one command, each on its own line. 529 1.1 christos @strong{Please note:} you need to put a tab character at the beginning of 530 1.1 christos every command line! This is an obscurity that catches the unwary. 531 1.1 christos 532 1.1 christos Usually a command is in a rule with prerequisites and serves to create a 533 1.1 christos target file if any of the prerequisites change. However, the rule that 534 1.1 christos specifies commands for the target need not have prerequisites. For 535 1.1 christos example, the rule containing the delete command associated with the 536 1.1 christos target @samp{clean} does not have prerequisites. 537 1.1 christos 538 1.1 christos A @dfn{rule}, then, explains how and when to remake certain files 539 1.1 christos which are the targets of the particular rule. @code{make} carries out 540 1.1 christos the commands on the prerequisites to create or update the target. A 541 1.1 christos rule can also explain how and when to carry out an action. 542 1.1 christos @xref{Rules, , Writing Rules}. 543 1.1 christos 544 1.1 christos A makefile may contain other text besides rules, but a simple makefile 545 1.1 christos need only contain rules. Rules may look somewhat more complicated 546 1.1 christos than shown in this template, but all fit the pattern more or less. 547 1.1 christos 548 1.1 christos @node Simple Makefile, How Make Works, Rule Introduction, Introduction 549 1.1 christos @section A Simple Makefile 550 1.1 christos @cindex simple makefile 551 1.1 christos @cindex makefile, simple 552 1.1 christos 553 1.1 christos Here is a straightforward makefile that describes the way an 554 1.1 christos executable file called @code{edit} depends on eight object files 555 1.1 christos which, in turn, depend on eight C source and three header files. 556 1.1 christos 557 1.1 christos In this example, all the C files include @file{defs.h}, but only those 558 1.1 christos defining editing commands include @file{command.h}, and only low 559 1.1 christos level files that change the editor buffer include @file{buffer.h}. 560 1.1 christos 561 1.1 christos @example 562 1.1 christos @group 563 1.1 christos edit : main.o kbd.o command.o display.o \ 564 1.1 christos insert.o search.o files.o utils.o 565 1.1 christos cc -o edit main.o kbd.o command.o display.o \ 566 1.1 christos insert.o search.o files.o utils.o 567 1.1 christos 568 1.1 christos main.o : main.c defs.h 569 1.1 christos cc -c main.c 570 1.1 christos kbd.o : kbd.c defs.h command.h 571 1.1 christos cc -c kbd.c 572 1.1 christos command.o : command.c defs.h command.h 573 1.1 christos cc -c command.c 574 1.1 christos display.o : display.c defs.h buffer.h 575 1.1 christos cc -c display.c 576 1.1 christos insert.o : insert.c defs.h buffer.h 577 1.1 christos cc -c insert.c 578 1.1 christos search.o : search.c defs.h buffer.h 579 1.1 christos cc -c search.c 580 1.1 christos files.o : files.c defs.h buffer.h command.h 581 1.1 christos cc -c files.c 582 1.1 christos utils.o : utils.c defs.h 583 1.1 christos cc -c utils.c 584 1.1 christos clean : 585 1.1 christos rm edit main.o kbd.o command.o display.o \ 586 1.1 christos insert.o search.o files.o utils.o 587 1.1 christos @end group 588 1.1 christos @end example 589 1.1 christos 590 1.1 christos @noindent 591 1.1 christos We split each long line into two lines using backslash-newline; this is 592 1.1 christos like using one long line, but is easier to read. 593 1.1 christos @cindex continuation lines 594 1.1 christos @cindex @code{\} (backslash), for continuation lines 595 1.1 christos @cindex backslash (@code{\}), for continuation lines 596 1.1 christos @cindex quoting newline, in makefile 597 1.1 christos @cindex newline, quoting, in makefile 598 1.1 christos 599 1.1 christos To use this makefile to create the executable file called @file{edit}, 600 1.1 christos type: 601 1.1 christos 602 1.1 christos @example 603 1.1 christos make 604 1.1 christos @end example 605 1.1 christos 606 1.1 christos To use this makefile to delete the executable file and all the object 607 1.1 christos files from the directory, type: 608 1.1 christos 609 1.1 christos @example 610 1.1 christos make clean 611 1.1 christos @end example 612 1.1 christos 613 1.1 christos In the example makefile, the targets include the executable file 614 1.1 christos @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The 615 1.1 christos prerequisites are files such as @samp{main.c} and @samp{defs.h}. 616 1.1 christos In fact, each @samp{.o} file is both a target and a prerequisite. 617 1.1 christos Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}. 618 1.1 christos 619 1.1 christos When a target is a file, it needs to be recompiled or relinked if any 620 1.1 christos of its prerequisites change. In addition, any prerequisites that are 621 1.1 christos themselves automatically generated should be updated first. In this 622 1.1 christos example, @file{edit} depends on each of the eight object files; the 623 1.1 christos object file @file{main.o} depends on the source file @file{main.c} and 624 1.1 christos on the header file @file{defs.h}. 625 1.1 christos 626 1.1 christos A shell command follows each line that contains a target and 627 1.1 christos prerequisites. These shell commands say how to update the target file. 628 1.1 christos A tab character must come at the beginning of every command line to 629 1.1 christos distinguish command lines from other lines in the makefile. (Bear in 630 1.1 christos mind that @code{make} does not know anything about how the commands 631 1.1 christos work. It is up to you to supply commands that will update the target 632 1.1 christos file properly. All @code{make} does is execute the commands in the rule 633 1.1 christos you have specified when the target file needs to be updated.) 634 1.1 christos @cindex shell command 635 1.1 christos 636 1.1 christos The target @samp{clean} is not a file, but merely the name of an 637 1.1 christos action. Since you 638 1.1 christos normally 639 1.1 christos do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule. 640 1.1 christos Consequently, @code{make} never does anything with it unless you tell 641 1.1 christos it specifically. Note that this rule not only is not a prerequisite, it 642 1.1 christos also does not have any prerequisites, so the only purpose of the rule 643 1.1 christos is to run the specified commands. Targets that do not refer to files 644 1.1 christos but are just actions are called @dfn{phony targets}. @xref{Phony 645 1.1 christos Targets}, for information about this kind of target. @xref{Errors, , 646 1.1 christos Errors in Commands}, to see how to cause @code{make} to ignore errors 647 1.1 christos from @code{rm} or any other command. 648 1.1 christos @cindex @code{clean} target 649 1.1 christos @cindex @code{rm} (shell command) 650 1.1 christos 651 1.1 christos @node How Make Works, Variables Simplify, Simple Makefile, Introduction 652 1.1 christos @comment node-name, next, previous, up 653 1.1 christos @section How @code{make} Processes a Makefile 654 1.1 christos @cindex processing a makefile 655 1.1 christos @cindex makefile, how @code{make} processes 656 1.1 christos 657 1.1 christos By default, @code{make} starts with the first target (not targets whose 658 1.1 christos names start with @samp{.}). This is called the @dfn{default goal}. 659 1.1 christos (@dfn{Goals} are the targets that @code{make} strives ultimately to 660 1.1 christos update. You can override this behavior using the command line 661 1.1 christos (@pxref{Goals, , Arguments to Specify the Goals}) or with the 662 1.1 christos @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, , 663 1.1 christos Other Special Variables}). 664 1.1 christos @cindex default goal 665 1.1 christos @cindex goal, default 666 1.1 christos @cindex goal 667 1.1 christos 668 1.1 christos In the simple example of the previous section, the default goal is to 669 1.1 christos update the executable program @file{edit}; therefore, we put that rule 670 1.1 christos first. 671 1.1 christos 672 1.1 christos Thus, when you give the command: 673 1.1 christos 674 1.1 christos @example 675 1.1 christos make 676 1.1 christos @end example 677 1.1 christos 678 1.1 christos @noindent 679 1.1 christos @code{make} reads the makefile in the current directory and begins by 680 1.1 christos processing the first rule. In the example, this rule is for relinking 681 1.1 christos @file{edit}; but before @code{make} can fully process this rule, it 682 1.1 christos must process the rules for the files that @file{edit} depends on, 683 1.1 christos which in this case are the object files. Each of these files is 684 1.1 christos processed according to its own rule. These rules say to update each 685 1.1 christos @samp{.o} file by compiling its source file. The recompilation must 686 1.1 christos be done if the source file, or any of the header files named as 687 1.1 christos prerequisites, is more recent than the object file, or if the object 688 1.1 christos file does not exist. 689 1.1 christos 690 1.1 christos The other rules are processed because their targets appear as 691 1.1 christos prerequisites of the goal. If some other rule is not depended on by the 692 1.1 christos goal (or anything it depends on, etc.), that rule is not processed, 693 1.1 christos unless you tell @code{make} to do so (with a command such as 694 1.1 christos @w{@code{make clean}}). 695 1.1 christos 696 1.1 christos Before recompiling an object file, @code{make} considers updating its 697 1.1 christos prerequisites, the source file and header files. This makefile does not 698 1.1 christos specify anything to be done for them---the @samp{.c} and @samp{.h} files 699 1.1 christos are not the targets of any rules---so @code{make} does nothing for these 700 1.1 christos files. But @code{make} would update automatically generated C programs, 701 1.1 christos such as those made by Bison or Yacc, by their own rules at this time. 702 1.1 christos 703 1.1 christos After recompiling whichever object files need it, @code{make} decides 704 1.1 christos whether to relink @file{edit}. This must be done if the file 705 1.1 christos @file{edit} does not exist, or if any of the object files are newer than 706 1.1 christos it. If an object file was just recompiled, it is now newer than 707 1.1 christos @file{edit}, so @file{edit} is relinked. 708 1.1 christos @cindex relinking 709 1.1 christos 710 1.1 christos Thus, if we change the file @file{insert.c} and run @code{make}, 711 1.1 christos @code{make} will compile that file to update @file{insert.o}, and then 712 1.1 christos link @file{edit}. If we change the file @file{command.h} and run 713 1.1 christos @code{make}, @code{make} will recompile the object files @file{kbd.o}, 714 1.1 christos @file{command.o} and @file{files.o} and then link the file @file{edit}. 715 1.1 christos 716 1.1 christos @node Variables Simplify, make Deduces, How Make Works, Introduction 717 1.1 christos @section Variables Make Makefiles Simpler 718 1.1 christos @cindex variables 719 1.1 christos @cindex simplifying with variables 720 1.1 christos 721 1.1 christos In our example, we had to list all the object files twice in the rule for 722 1.1 christos @file{edit} (repeated here): 723 1.1 christos 724 1.1 christos @example 725 1.1 christos @group 726 1.1 christos edit : main.o kbd.o command.o display.o \ 727 1.1 christos insert.o search.o files.o utils.o 728 1.1 christos cc -o edit main.o kbd.o command.o display.o \ 729 1.1 christos insert.o search.o files.o utils.o 730 1.1 christos @end group 731 1.1 christos @end example 732 1.1 christos 733 1.1 christos @cindex @code{objects} 734 1.1 christos Such duplication is error-prone; if a new object file is added to the 735 1.1 christos system, we might add it to one list and forget the other. We can eliminate 736 1.1 christos the risk and simplify the makefile by using a variable. @dfn{Variables} 737 1.1 christos allow a text string to be defined once and substituted in multiple places 738 1.1 christos later (@pxref{Using Variables, ,How to Use Variables}). 739 1.1 christos 740 1.1 christos @cindex @code{OBJECTS} 741 1.1 christos @cindex @code{objs} 742 1.1 christos @cindex @code{OBJS} 743 1.1 christos @cindex @code{obj} 744 1.1 christos @cindex @code{OBJ} 745 1.1 christos It is standard practice for every makefile to have a variable named 746 1.1 christos @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj}, 747 1.1 christos or @code{OBJ} which is a list of all object file names. We would 748 1.1 christos define such a variable @code{objects} with a line like this in the 749 1.1 christos makefile:@refill 750 1.1 christos 751 1.1 christos @example 752 1.1 christos @group 753 1.1 christos objects = main.o kbd.o command.o display.o \ 754 1.1 christos insert.o search.o files.o utils.o 755 1.1 christos @end group 756 1.1 christos @end example 757 1.1 christos 758 1.1 christos @noindent 759 1.1 christos Then, each place we want to put a list of the object file names, we can 760 1.1 christos substitute the variable's value by writing @samp{$(objects)} 761 1.1 christos (@pxref{Using Variables, ,How to Use Variables}). 762 1.1 christos 763 1.1 christos Here is how the complete simple makefile looks when you use a variable 764 1.1 christos for the object files: 765 1.1 christos 766 1.1 christos @example 767 1.1 christos @group 768 1.1 christos objects = main.o kbd.o command.o display.o \ 769 1.1 christos insert.o search.o files.o utils.o 770 1.1 christos 771 1.1 christos edit : $(objects) 772 1.1 christos cc -o edit $(objects) 773 1.1 christos main.o : main.c defs.h 774 1.1 christos cc -c main.c 775 1.1 christos kbd.o : kbd.c defs.h command.h 776 1.1 christos cc -c kbd.c 777 1.1 christos command.o : command.c defs.h command.h 778 1.1 christos cc -c command.c 779 1.1 christos display.o : display.c defs.h buffer.h 780 1.1 christos cc -c display.c 781 1.1 christos insert.o : insert.c defs.h buffer.h 782 1.1 christos cc -c insert.c 783 1.1 christos search.o : search.c defs.h buffer.h 784 1.1 christos cc -c search.c 785 1.1 christos files.o : files.c defs.h buffer.h command.h 786 1.1 christos cc -c files.c 787 1.1 christos utils.o : utils.c defs.h 788 1.1 christos cc -c utils.c 789 1.1 christos clean : 790 1.1 christos rm edit $(objects) 791 1.1 christos @end group 792 1.1 christos @end example 793 1.1 christos 794 1.1 christos @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction 795 1.1 christos @section Letting @code{make} Deduce the Commands 796 1.1 christos @cindex deducing commands (implicit rules) 797 1.1 christos @cindex implicit rule, introduction to 798 1.1 christos @cindex rule, implicit, introduction to 799 1.1 christos 800 1.1 christos It is not necessary to spell out the commands for compiling the individual 801 1.1 christos C source files, because @code{make} can figure them out: it has an 802 1.1 christos @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly 803 1.1 christos named @samp{.c} file using a @samp{cc -c} command. For example, it will 804 1.1 christos use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into 805 1.1 christos @file{main.o}. We can therefore omit the commands from the rules for the 806 1.1 christos object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill 807 1.1 christos 808 1.1 christos When a @samp{.c} file is used automatically in this way, it is also 809 1.1 christos automatically added to the list of prerequisites. We can therefore omit 810 1.1 christos the @samp{.c} files from the prerequisites, provided we omit the commands. 811 1.1 christos 812 1.1 christos Here is the entire example, with both of these changes, and a variable 813 1.1 christos @code{objects} as suggested above: 814 1.1 christos 815 1.1 christos @example 816 1.1 christos @group 817 1.1 christos objects = main.o kbd.o command.o display.o \ 818 1.1 christos insert.o search.o files.o utils.o 819 1.1 christos 820 1.1 christos edit : $(objects) 821 1.1 christos cc -o edit $(objects) 822 1.1 christos 823 1.1 christos main.o : defs.h 824 1.1 christos kbd.o : defs.h command.h 825 1.1 christos command.o : defs.h command.h 826 1.1 christos display.o : defs.h buffer.h 827 1.1 christos insert.o : defs.h buffer.h 828 1.1 christos search.o : defs.h buffer.h 829 1.1 christos files.o : defs.h buffer.h command.h 830 1.1 christos utils.o : defs.h 831 1.1 christos 832 1.1 christos .PHONY : clean 833 1.1 christos clean : 834 1.1 christos rm edit $(objects) 835 1.1 christos @end group 836 1.1 christos @end example 837 1.1 christos 838 1.1 christos @noindent 839 1.1 christos This is how we would write the makefile in actual practice. (The 840 1.1 christos complications associated with @samp{clean} are described elsewhere. 841 1.1 christos See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.) 842 1.1 christos 843 1.1 christos Because implicit rules are so convenient, they are important. You 844 1.1 christos will see them used frequently.@refill 845 1.1 christos 846 1.1 christos @node Combine By Prerequisite, Cleanup, make Deduces, Introduction 847 1.1 christos @section Another Style of Makefile 848 1.1 christos @cindex combining rules by prerequisite 849 1.1 christos 850 1.1 christos When the objects of a makefile are created only by implicit rules, an 851 1.1 christos alternative style of makefile is possible. In this style of makefile, 852 1.1 christos you group entries by their prerequisites instead of by their targets. 853 1.1 christos Here is what one looks like: 854 1.1 christos 855 1.1 christos @example 856 1.1 christos @group 857 1.1 christos objects = main.o kbd.o command.o display.o \ 858 1.1 christos insert.o search.o files.o utils.o 859 1.1 christos 860 1.1 christos edit : $(objects) 861 1.1 christos cc -o edit $(objects) 862 1.1 christos 863 1.1 christos $(objects) : defs.h 864 1.1 christos kbd.o command.o files.o : command.h 865 1.1 christos display.o insert.o search.o files.o : buffer.h 866 1.1 christos @end group 867 1.1 christos @end example 868 1.1 christos 869 1.1 christos @noindent 870 1.1 christos Here @file{defs.h} is given as a prerequisite of all the object files; 871 1.1 christos @file{command.h} and @file{buffer.h} are prerequisites of the specific 872 1.1 christos object files listed for them. 873 1.1 christos 874 1.1 christos Whether this is better is a matter of taste: it is more compact, but some 875 1.1 christos people dislike it because they find it clearer to put all the information 876 1.1 christos about each target in one place. 877 1.1 christos 878 1.1 christos @node Cleanup, , Combine By Prerequisite, Introduction 879 1.1 christos @section Rules for Cleaning the Directory 880 1.1 christos @cindex cleaning up 881 1.1 christos @cindex removing, to clean up 882 1.1 christos 883 1.1 christos Compiling a program is not the only thing you might want to write rules 884 1.1 christos for. Makefiles commonly tell how to do a few other things besides 885 1.1 christos compiling a program: for example, how to delete all the object files 886 1.1 christos and executables so that the directory is @samp{clean}. 887 1.1 christos 888 1.1 christos @cindex @code{clean} target 889 1.1 christos Here is how we 890 1.1 christos could write a @code{make} rule for cleaning our example editor: 891 1.1 christos 892 1.1 christos @example 893 1.1 christos @group 894 1.1 christos clean: 895 1.1 christos rm edit $(objects) 896 1.1 christos @end group 897 1.1 christos @end example 898 1.1 christos 899 1.1 christos In practice, we might want to write the rule in a somewhat more 900 1.1 christos complicated manner to handle unanticipated situations. We would do this: 901 1.1 christos 902 1.1 christos @example 903 1.1 christos @group 904 1.1 christos .PHONY : clean 905 1.1 christos clean : 906 1.1 christos -rm edit $(objects) 907 1.1 christos @end group 908 1.1 christos @end example 909 1.1 christos 910 1.1 christos @noindent 911 1.1 christos This prevents @code{make} from getting confused by an actual file 912 1.1 christos called @file{clean} and causes it to continue in spite of errors from 913 1.1 christos @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in 914 1.1 christos Commands}.) 915 1.1 christos 916 1.1 christos @noindent 917 1.1 christos A rule such as this should not be placed at the beginning of the 918 1.1 christos makefile, because we do not want it to run by default! Thus, in the 919 1.1 christos example makefile, we want the rule for @code{edit}, which recompiles 920 1.1 christos the editor, to remain the default goal. 921 1.1 christos 922 1.1 christos Since @code{clean} is not a prerequisite of @code{edit}, this rule will not 923 1.1 christos run at all if we give the command @samp{make} with no arguments. In 924 1.1 christos order to make the rule run, we have to type @samp{make clean}. 925 1.1 christos @xref{Running, ,How to Run @code{make}}. 926 1.1 christos 927 1.1 christos @node Makefiles, Rules, Introduction, Top 928 1.1 christos @chapter Writing Makefiles 929 1.1 christos 930 1.1 christos @cindex makefile, how to write 931 1.1 christos The information that tells @code{make} how to recompile a system comes from 932 1.1 christos reading a data base called the @dfn{makefile}. 933 1.1 christos 934 1.1 christos @menu 935 1.1 christos * Makefile Contents:: What makefiles contain. 936 1.1 christos * Makefile Names:: How to name your makefile. 937 1.1 christos * Include:: How one makefile can use another makefile. 938 1.1 christos * MAKEFILES Variable:: The environment can specify extra makefiles. 939 1.1 christos * MAKEFILE_LIST Variable:: Discover which makefiles have been read. 940 1.1 christos * Special Variables:: Other special variables. 941 1.1 christos * Remaking Makefiles:: How makefiles get remade. 942 1.1 christos * Overriding Makefiles:: How to override part of one makefile 943 1.1 christos with another makefile. 944 1.1 christos * Reading Makefiles:: How makefiles are parsed. 945 1.1 christos * Secondary Expansion:: How and when secondary expansion is performed. 946 1.1 christos @end menu 947 1.1 christos 948 1.1 christos @node Makefile Contents, Makefile Names, Makefiles, Makefiles 949 1.1 christos @section What Makefiles Contain 950 1.1 christos 951 1.1 christos Makefiles contain five kinds of things: @dfn{explicit rules}, 952 1.1 christos @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives}, 953 1.1 christos and @dfn{comments}. Rules, variables, and directives are described at 954 1.1 christos length in later chapters.@refill 955 1.1 christos 956 1.1 christos @itemize @bullet 957 1.1 christos @cindex rule, explicit, definition of 958 1.1 christos @cindex explicit rule, definition of 959 1.1 christos @item 960 1.1 christos An @dfn{explicit rule} says when and how to remake one or more files, 961 1.1 christos called the rule's @dfn{targets}. It lists the other files that the 962 1.1 christos targets depend on, called the @dfn{prerequisites} of the target, and 963 1.1 christos may also give commands to use to create or update the targets. 964 1.1 christos @xref{Rules, ,Writing Rules}. 965 1.1 christos 966 1.1 christos @cindex rule, implicit, definition of 967 1.1 christos @cindex implicit rule, definition of 968 1.1 christos @item 969 1.1 christos An @dfn{implicit rule} says when and how to remake a class of files 970 1.1 christos based on their names. It describes how a target may depend on a file 971 1.1 christos with a name similar to the target and gives commands to create or 972 1.1 christos update such a target. @xref{Implicit Rules, ,Using Implicit Rules}. 973 1.1 christos 974 1.1 christos @cindex variable definition 975 1.1 christos @item 976 1.1 christos A @dfn{variable definition} is a line that specifies a text string 977 1.1 christos value for a variable that can be substituted into the text later. The 978 1.1 christos simple makefile example shows a variable definition for @code{objects} 979 1.1 christos as a list of all object files (@pxref{Variables Simplify, , Variables 980 1.1 christos Make Makefiles Simpler}). 981 1.1 christos 982 1.1 christos @cindex directive 983 1.1 christos @item 984 1.1 christos A @dfn{directive} is a command for @code{make} to do something special while 985 1.1 christos reading the makefile. These include: 986 1.1 christos 987 1.1 christos @itemize @bullet 988 1.1 christos @item 989 1.1 christos Reading another makefile (@pxref{Include, ,Including Other Makefiles}). 990 1.1 christos 991 1.1 christos @item 992 1.1 christos Deciding (based on the values of variables) whether to use or 993 1.1 christos ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}). 994 1.1 christos 995 1.1 christos @item 996 1.1 christos Defining a variable from a verbatim string containing multiple lines 997 1.1 christos (@pxref{Defining, ,Defining Variables Verbatim}). 998 1.1 christos @end itemize 999 1.1 christos 1000 1.1 christos @cindex comments, in makefile 1001 1.1 christos @cindex @code{#} (comments), in makefile 1002 1.1 christos @item 1003 1.1 christos @samp{#} in a line of a makefile starts a @dfn{comment}. It and the 1004 1.1 christos rest of the line are ignored, except that a trailing backslash not 1005 1.1 christos escaped by another backslash will continue the comment across multiple 1006 1.1 christos lines. A line containing just a comment (with perhaps spaces before 1007 1.1 christos it) is effectively blank, and is ignored. If you want a literal 1008 1.1 christos @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may 1009 1.1 christos appear on any line in the makefile, although they are treated 1010 1.1 christos specially in certain situations. 1011 1.1 christos 1012 1.1 christos Within a command script (if the line begins with a TAB character) the 1013 1.1 christos entire line is passed to the shell, just as with any other line that 1014 1.1 christos begins with a TAB. The shell decides how to interpret the text: 1015 1.1 christos whether or not this is a comment is up to the shell. 1016 1.1 christos 1017 1.1 christos Within a @code{define} directive, comments are not ignored during the 1018 1.1 christos definition of the variable, but rather kept intact in the value of the 1019 1.1 christos variable. When the variable is expanded they will either be treated 1020 1.1 christos as @code{make} comments or as command script text, depending on the 1021 1.1 christos context in which the variable is evaluated. 1022 1.1 christos @end itemize 1023 1.1 christos 1024 1.1 christos @node Makefile Names, Include, Makefile Contents, Makefiles 1025 1.1 christos @section What Name to Give Your Makefile 1026 1.1 christos @cindex makefile name 1027 1.1 christos @cindex name of makefile 1028 1.1 christos @cindex default makefile name 1029 1.1 christos @cindex file name of makefile 1030 1.1 christos 1031 1.1 christos @c following paragraph rewritten to avoid overfull hbox 1032 1.1 christos By default, when @code{make} looks for the makefile, it tries the 1033 1.1 christos following names, in order: @file{GNUmakefile}, @file{makefile} 1034 1.1 christos and @file{Makefile}.@refill 1035 1.1 christos @findex Makefile 1036 1.1 christos @findex GNUmakefile 1037 1.1 christos @findex makefile 1038 1.1 christos 1039 1.1 christos @cindex @code{README} 1040 1.1 christos Normally you should call your makefile either @file{makefile} or 1041 1.1 christos @file{Makefile}. (We recommend @file{Makefile} because it appears 1042 1.1 christos prominently near the beginning of a directory listing, right near other 1043 1.1 christos important files such as @file{README}.) The first name checked, 1044 1.1 christos @file{GNUmakefile}, is not recommended for most makefiles. You should 1045 1.1 christos use this name if you have a makefile that is specific to GNU 1046 1.1 christos @code{make}, and will not be understood by other versions of 1047 1.1 christos @code{make}. Other @code{make} programs look for @file{makefile} and 1048 1.1 christos @file{Makefile}, but not @file{GNUmakefile}. 1049 1.1 christos 1050 1.1 christos If @code{make} finds none of these names, it does not use any makefile. 1051 1.1 christos Then you must specify a goal with a command argument, and @code{make} 1052 1.1 christos will attempt to figure out how to remake it using only its built-in 1053 1.1 christos implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}. 1054 1.1 christos 1055 1.1 christos @cindex @code{-f} 1056 1.1 christos @cindex @code{--file} 1057 1.1 christos @cindex @code{--makefile} 1058 1.1 christos If you want to use a nonstandard name for your makefile, you can specify 1059 1.1 christos the makefile name with the @samp{-f} or @samp{--file} option. The 1060 1.1 christos arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell 1061 1.1 christos @code{make} to read the file @var{name} as the makefile. If you use 1062 1.1 christos more than one @samp{-f} or @samp{--file} option, you can specify several 1063 1.1 christos makefiles. All the makefiles are effectively concatenated in the order 1064 1.1 christos specified. The default makefile names @file{GNUmakefile}, 1065 1.1 christos @file{makefile} and @file{Makefile} are not checked automatically if you 1066 1.1 christos specify @samp{-f} or @samp{--file}.@refill 1067 1.1 christos @cindex specifying makefile name 1068 1.1 christos @cindex makefile name, how to specify 1069 1.1 christos @cindex name of makefile, how to specify 1070 1.1 christos @cindex file name of makefile, how to specify 1071 1.1 christos 1072 1.1 christos @node Include, MAKEFILES Variable, Makefile Names, Makefiles 1073 1.1 christos @section Including Other Makefiles 1074 1.1 christos @cindex including other makefiles 1075 1.1 christos @cindex makefile, including 1076 1.1 christos 1077 1.1 christos @findex include 1078 1.1 christos The @code{include} directive tells @code{make} to suspend reading the 1079 1.1 christos current makefile and read one or more other makefiles before continuing. 1080 1.1 christos The directive is a line in the makefile that looks like this: 1081 1.1 christos 1082 1.1 christos @example 1083 1.1 christos include @var{filenames}@dots{} 1084 1.1 christos @end example 1085 1.1 christos 1086 1.1 christos @noindent 1087 1.1 christos @var{filenames} can contain shell file name patterns. If 1088 1.1 christos @var{filenames} is empty, nothing is included and no error is printed. 1089 1.1 christos @cindex shell file name pattern (in @code{include}) 1090 1.1 christos @cindex shell wildcards (in @code{include}) 1091 1.1 christos @cindex wildcard, in @code{include} 1092 1.1 christos 1093 1.1 christos Extra spaces are allowed and ignored at the beginning of the line, but 1094 1.1 christos a tab is not allowed. (If the line begins with a tab, it will be 1095 1.1 christos considered a command line.) Whitespace is required between 1096 1.1 christos @code{include} and the file names, and between file names; extra 1097 1.1 christos whitespace is ignored there and at the end of the directive. A 1098 1.1 christos comment starting with @samp{#} is allowed at the end of the line. If 1099 1.1 christos the file names contain any variable or function references, they are 1100 1.1 christos expanded. @xref{Using Variables, ,How to Use Variables}. 1101 1.1 christos 1102 1.1 christos For example, if you have three @file{.mk} files, @file{a.mk}, 1103 1.1 christos @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to 1104 1.1 christos @code{bish bash}, then the following expression 1105 1.1 christos 1106 1.1 christos @example 1107 1.1 christos include foo *.mk $(bar) 1108 1.1 christos @end example 1109 1.1 christos 1110 1.1 christos is equivalent to 1111 1.1 christos 1112 1.1 christos @example 1113 1.1 christos include foo a.mk b.mk c.mk bish bash 1114 1.1 christos @end example 1115 1.1 christos 1116 1.1 christos When @code{make} processes an @code{include} directive, it suspends 1117 1.1 christos reading of the containing makefile and reads from each listed file in 1118 1.1 christos turn. When that is finished, @code{make} resumes reading the 1119 1.1 christos makefile in which the directive appears. 1120 1.1 christos 1121 1.1 christos One occasion for using @code{include} directives is when several programs, 1122 1.1 christos handled by individual makefiles in various directories, need to use a 1123 1.1 christos common set of variable definitions 1124 1.1 christos (@pxref{Setting, ,Setting Variables}) or pattern rules 1125 1.1 christos (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}). 1126 1.1 christos 1127 1.1 christos Another such occasion is when you want to generate prerequisites from 1128 1.1 christos source files automatically; the prerequisites can be put in a file that 1129 1.1 christos is included by the main makefile. This practice is generally cleaner 1130 1.1 christos than that of somehow appending the prerequisites to the end of the main 1131 1.1 christos makefile as has been traditionally done with other versions of 1132 1.1 christos @code{make}. @xref{Automatic Prerequisites}. 1133 1.1 christos @cindex prerequisites, automatic generation 1134 1.1 christos @cindex automatic generation of prerequisites 1135 1.1 christos @cindex generating prerequisites automatically 1136 1.1 christos 1137 1.1 christos @cindex @code{-I} 1138 1.1 christos @cindex @code{--include-dir} 1139 1.1 christos @cindex included makefiles, default directories 1140 1.1 christos @cindex default directories for included makefiles 1141 1.1 christos @findex /usr/gnu/include 1142 1.1 christos @findex /usr/local/include 1143 1.1 christos @findex /usr/include 1144 1.1 christos If the specified name does not start with a slash, and the file is not 1145 1.1 christos found in the current directory, several other directories are searched. 1146 1.1 christos First, any directories you have specified with the @samp{-I} or 1147 1.1 christos @samp{--include-dir} option are searched 1148 1.1 christos (@pxref{Options Summary, ,Summary of Options}). 1149 1.1 christos Then the following directories (if they exist) 1150 1.1 christos are searched, in this order: 1151 1.1 christos @file{@var{prefix}/include} (normally @file{/usr/local/include} 1152 1.1 christos @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if 1153 1.1 christos @var{prefix} has been defined to be the root of the DJGPP tree 1154 1.1 christos hierarchy.}) 1155 1.1 christos @file{/usr/gnu/include}, 1156 1.1 christos @file{/usr/local/include}, @file{/usr/include}. 1157 1.1 christos 1158 1.1 christos If an included makefile cannot be found in any of these directories, a 1159 1.1 christos warning message is generated, but it is not an immediately fatal error; 1160 1.1 christos processing of the makefile containing the @code{include} continues. 1161 1.1 christos Once it has finished reading makefiles, @code{make} will try to remake 1162 1.1 christos any that are out of date or don't exist. 1163 1.1 christos @xref{Remaking Makefiles, ,How Makefiles Are Remade}. 1164 1.1 christos Only after it has tried to find a way to remake a makefile and failed, 1165 1.1 christos will @code{make} diagnose the missing makefile as a fatal error. 1166 1.1 christos 1167 1.1 christos If you want @code{make} to simply ignore a makefile which does not exist 1168 1.1 christos and cannot be remade, with no error message, use the @w{@code{-include}} 1169 1.1 christos directive instead of @code{include}, like this: 1170 1.1 christos 1171 1.1 christos @example 1172 1.1 christos -include @var{filenames}@dots{} 1173 1.1 christos @end example 1174 1.1 christos 1175 1.1 christos This acts like @code{include} in every way except that there is no 1176 1.1 christos error (not even a warning) if any of the @var{filenames} do not exist. 1177 1.1 christos For compatibility with some other @code{make} implementations, 1178 1.1 christos @code{sinclude} is another name for @w{@code{-include}}. 1179 1.1 christos 1180 1.1 christos @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles 1181 1.1 christos @section The Variable @code{MAKEFILES} 1182 1.1 christos @cindex makefile, and @code{MAKEFILES} variable 1183 1.1 christos @cindex including (@code{MAKEFILES} variable) 1184 1.1 christos 1185 1.1 christos @vindex MAKEFILES 1186 1.1 christos If the environment variable @code{MAKEFILES} is defined, @code{make} 1187 1.1 christos considers its value as a list of names (separated by whitespace) of 1188 1.1 christos additional makefiles to be read before the others. This works much like 1189 1.1 christos the @code{include} directive: various directories are searched for those 1190 1.1 christos files (@pxref{Include, ,Including Other Makefiles}). In addition, the 1191 1.1 christos default goal is never taken from one of these makefiles and it is not an 1192 1.1 christos error if the files listed in @code{MAKEFILES} are not found.@refill 1193 1.1 christos 1194 1.1 christos @cindex recursion, and @code{MAKEFILES} variable 1195 1.1 christos The main use of @code{MAKEFILES} is in communication between recursive 1196 1.1 christos invocations of @code{make} (@pxref{Recursion, ,Recursive Use of 1197 1.1 christos @code{make}}). It usually is not desirable to set the environment 1198 1.1 christos variable before a top-level invocation of @code{make}, because it is 1199 1.1 christos usually better not to mess with a makefile from outside. However, if 1200 1.1 christos you are running @code{make} without a specific makefile, a makefile in 1201 1.1 christos @code{MAKEFILES} can do useful things to help the built-in implicit 1202 1.1 christos rules work better, such as defining search paths (@pxref{Directory Search}). 1203 1.1 christos 1204 1.1 christos Some users are tempted to set @code{MAKEFILES} in the environment 1205 1.1 christos automatically on login, and program makefiles to expect this to be done. 1206 1.1 christos This is a very bad idea, because such makefiles will fail to work if run by 1207 1.1 christos anyone else. It is much better to write explicit @code{include} directives 1208 1.1 christos in the makefiles. @xref{Include, , Including Other Makefiles}. 1209 1.1 christos 1210 1.1 christos @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles 1211 1.1 christos @comment node-name, next, previous, up 1212 1.1 christos @section The Variable @code{MAKEFILE_LIST} 1213 1.1 christos @cindex makefiles, and @code{MAKEFILE_LIST} variable 1214 1.1 christos @cindex including (@code{MAKEFILE_LIST} variable) 1215 1.1 christos @vindex MAKEFILE_LIST 1216 1.1 christos 1217 1.1 christos As @code{make} reads various makefiles, including any obtained from the 1218 1.1 christos @code{MAKEFILES} variable, the command line, the default files, or 1219 1.1 christos from @code{include} directives, their names will be automatically 1220 1.1 christos appended to the @code{MAKEFILE_LIST} variable. They are added right 1221 1.1 christos before @code{make} begins to parse them. 1222 1.1 christos 1223 1.1 christos This means that if the first thing a makefile does is examine the last 1224 1.1 christos word in this variable, it will be the name of the current makefile. 1225 1.1 christos Once the current makefile has used @code{include}, however, the last 1226 1.1 christos word will be the just-included makefile. 1227 1.1 christos 1228 1.1 christos If a makefile named @code{Makefile} has this content: 1229 1.1 christos 1230 1.1 christos @example 1231 1.1 christos @group 1232 1.1 christos name1 := $(lastword $(MAKEFILE_LIST)) 1233 1.1 christos 1234 1.1 christos include inc.mk 1235 1.1 christos 1236 1.1 christos name2 := $(lastword $(MAKEFILE_LIST)) 1237 1.1 christos 1238 1.1 christos all: 1239 1.1 christos @@echo name1 = $(name1) 1240 1.1 christos @@echo name2 = $(name2) 1241 1.1 christos @end group 1242 1.1 christos @end example 1243 1.1 christos 1244 1.1 christos @noindent 1245 1.1 christos then you would expect to see this output: 1246 1.1 christos 1247 1.1 christos @example 1248 1.1 christos @group 1249 1.1 christos name1 = Makefile 1250 1.1 christos name2 = inc.mk 1251 1.1 christos @end group 1252 1.1 christos @end example 1253 1.1 christos 1254 1.1 christos @xref{Text Functions}, for more information on the @code{word} and 1255 1.1 christos @code{words} functions used above. @xref{Flavors, The Two Flavors of 1256 1.1 christos Variables}, for more information on simply-expanded (@code{:=}) 1257 1.1 christos variable definitions. 1258 1.1 christos 1259 1.1 christos @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles 1260 1.1 christos @comment node-name, next, previous, up 1261 1.1 christos @section Other Special Variables 1262 1.1 christos @cindex makefiles, and special variables 1263 1.1 christos @cindex special variables 1264 1.1 christos 1265 1.1 christos GNU @code{make} also supports other special variables. Unless 1266 1.1 christos otherwise documented here, these values lose their special properties 1267 1.1 christos if they are set by a makefile or on the command line. 1268 1.1 christos 1269 1.1 christos @table @code 1270 1.1 christos 1271 1.1 christos @vindex .DEFAULT_GOAL @r{(define default goal)} 1272 1.1 christos @item .DEFAULT_GOAL 1273 1.1 christos Sets the default goal to be used if no targets were specified on the 1274 1.1 christos command line (@pxref{Goals, , Arguments to Specify the Goals}). The 1275 1.1 christos @code{.DEFAULT_GOAL} variable allows you to discover the current 1276 1.1 christos default goal, restart the default goal selection algorithm by clearing 1277 1.1 christos its value, or to explicitly set the default goal. The following 1278 1.1 christos example illustrates these cases: 1279 1.1 christos 1280 1.1 christos @example 1281 1.1 christos @group 1282 1.1 christos # Query the default goal. 1283 1.1 christos ifeq ($(.DEFAULT_GOAL),) 1284 1.1 christos $(warning no default goal is set) 1285 1.1 christos endif 1286 1.1 christos 1287 1.1 christos .PHONY: foo 1288 1.1 christos foo: ; @@echo $@@ 1289 1.1 christos 1290 1.1 christos $(warning default goal is $(.DEFAULT_GOAL)) 1291 1.1 christos 1292 1.1 christos # Reset the default goal. 1293 1.1 christos .DEFAULT_GOAL := 1294 1.1 christos 1295 1.1 christos .PHONY: bar 1296 1.1 christos bar: ; @@echo $@@ 1297 1.1 christos 1298 1.1 christos $(warning default goal is $(.DEFAULT_GOAL)) 1299 1.1 christos 1300 1.1 christos # Set our own. 1301 1.1 christos .DEFAULT_GOAL := foo 1302 1.1 christos @end group 1303 1.1 christos @end example 1304 1.1 christos 1305 1.1 christos This makefile prints: 1306 1.1 christos 1307 1.1 christos @example 1308 1.1 christos @group 1309 1.1 christos no default goal is set 1310 1.1 christos default goal is foo 1311 1.1 christos default goal is bar 1312 1.1 christos foo 1313 1.1 christos @end group 1314 1.1 christos @end example 1315 1.1 christos 1316 1.1 christos Note that assigning more than one target name to @code{.DEFAULT_GOAL} is 1317 1.1 christos illegal and will result in an error. 1318 1.1 christos 1319 1.1 christos @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)} 1320 1.1 christos @item MAKE_RESTARTS 1321 1.1 christos This variable is set only if this instance of @code{make} has 1322 1.1 christos restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it 1323 1.1 christos will contain the number of times this instance has restarted. Note 1324 1.1 christos this is not the same as recursion (counted by the @code{MAKELEVEL} 1325 1.1 christos variable). You should not set, modify, or export this variable. 1326 1.1 christos 1327 1.1 christos @vindex .VARIABLES @r{(list of variables)} 1328 1.1 christos @item .VARIABLES 1329 1.1 christos Expands to a list of the @emph{names} of all global variables defined 1330 1.1 christos so far. This includes variables which have empty values, as well as 1331 1.1 christos built-in variables (@pxref{Implicit Variables, , Variables Used by 1332 1.1 christos Implicit Rules}), but does not include any variables which are only 1333 1.1 christos defined in a target-specific context. Note that any value you assign 1334 1.1 christos to this variable will be ignored; it will always return its special 1335 1.1 christos value. 1336 1.1 christos 1337 1.1 christos @c @vindex .TARGETS @r{(list of targets)} 1338 1.1 christos @c @item .TARGETS 1339 1.1 christos @c The second special variable is @code{.TARGETS}. When expanded, the 1340 1.1 christos @c value consists of a list of all targets defined in all makefiles read 1341 1.1 christos @c up until that point. Note it's not enough for a file to be simply 1342 1.1 christos @c mentioned in the makefile to be listed in this variable, even if it 1343 1.1 christos @c would match an implicit rule and become an ``implicit target''. The 1344 1.1 christos @c file must appear as a target, on the left-hand side of a ``:'', to be 1345 1.1 christos @c considered a target for the purposes of this variable. 1346 1.1 christos 1347 1.1 christos @vindex .FEATURES @r{(list of supported features)} 1348 1.1 christos @item .FEATURES 1349 1.1 christos Expands to a list of special features supported by this version of 1350 1.1 christos @code{make}. Possible values include: 1351 1.1 christos 1352 1.1 christos @table @samp 1353 1.1 christos 1354 1.1 christos @item archives 1355 1.1 christos Supports @code{ar} (archive) files using special filename syntax. 1356 1.1 christos @xref{Archives, ,Using @code{make} to Update Archive Files}. 1357 1.1 christos 1358 1.1 christos @item check-symlink 1359 1.1 christos Supports the @code{-L} (@code{--check-symlink-times}) flag. 1360 1.1 christos @xref{Options Summary, ,Summary of Options}. 1361 1.1 christos 1362 1.1 christos @item else-if 1363 1.1 christos Supports ``else if'' non-nested conditionals. @xref{Conditional 1364 1.1 christos Syntax, ,Syntax of Conditionals}. 1365 1.1 christos 1366 1.1 christos @item jobserver 1367 1.1 christos Supports ``job server'' enhanced parallel builds. @xref{Parallel, 1368 1.1 christos ,Parallel Execution}. 1369 1.1 christos 1370 1.1 christos @item second-expansion 1371 1.1 christos Supports secondary expansion of prerequisite lists. 1372 1.1 christos 1373 1.1 christos @item order-only 1374 1.1 christos Supports order-only prerequisites. @xref{Prerequisite Types, ,Types 1375 1.1 christos of Prerequisites}. 1376 1.1 christos 1377 1.1 christos @item target-specific 1378 1.1 christos Supports target-specific and pattern-specific variable assignments. 1379 1.1 christos @xref{Target-specific, ,Target-specific Variable Values}. 1380 1.1 christos 1381 1.1 christos @end table 1382 1.1 christos 1383 1.1 christos @vindex .INCLUDE_DIRS @r{(list of include directories)} 1384 1.1 christos @item .INCLUDE_DIRS 1385 1.1 christos Expands to a list of directories that @code{make} searches for 1386 1.1 christos included makefiles (@pxref{Include, , Including Other Makefiles}). 1387 1.1 christos 1388 1.1 christos @end table 1389 1.1 christos 1390 1.1 christos @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles 1391 1.1 christos @section How Makefiles Are Remade 1392 1.1 christos 1393 1.1 christos @cindex updating makefiles 1394 1.1 christos @cindex remaking makefiles 1395 1.1 christos @cindex makefile, remaking of 1396 1.1 christos Sometimes makefiles can be remade from other files, such as RCS or SCCS 1397 1.1 christos files. If a makefile can be remade from other files, you probably want 1398 1.1 christos @code{make} to get an up-to-date version of the makefile to read in. 1399 1.1 christos 1400 1.1 christos To this end, after reading in all makefiles, @code{make} will consider 1401 1.1 christos each as a goal target and attempt to update it. If a makefile has a 1402 1.1 christos rule which says how to update it (found either in that very makefile or 1403 1.1 christos in another one) or if an implicit rule applies to it (@pxref{Implicit 1404 1.1 christos Rules, ,Using Implicit Rules}), it will be updated if necessary. After 1405 1.1 christos all makefiles have been checked, if any have actually been changed, 1406 1.1 christos @code{make} starts with a clean slate and reads all the makefiles over 1407 1.1 christos again. (It will also attempt to update each of them over again, but 1408 1.1 christos normally this will not change them again, since they are already up to 1409 1.1 christos date.)@refill 1410 1.1 christos 1411 1.1 christos If you know that one or more of your makefiles cannot be remade and you 1412 1.1 christos want to keep @code{make} from performing an implicit rule search on 1413 1.1 christos them, perhaps for efficiency reasons, you can use any normal method of 1414 1.1 christos preventing implicit rule lookup to do so. For example, you can write an 1415 1.1 christos explicit rule with the makefile as the target, and an empty command 1416 1.1 christos string (@pxref{Empty Commands, ,Using Empty Commands}). 1417 1.1 christos 1418 1.1 christos If the makefiles specify a double-colon rule to remake a file with 1419 1.1 christos commands but no prerequisites, that file will always be remade 1420 1.1 christos (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a 1421 1.1 christos double-colon rule with commands but no prerequisites will be remade every 1422 1.1 christos time @code{make} is run, and then again after @code{make} starts over 1423 1.1 christos and reads the makefiles in again. This would cause an infinite loop: 1424 1.1 christos @code{make} would constantly remake the makefile, and never do anything 1425 1.1 christos else. So, to avoid this, @code{make} will @strong{not} attempt to 1426 1.1 christos remake makefiles which are specified as targets of a double-colon rule 1427 1.1 christos with commands but no prerequisites.@refill 1428 1.1 christos 1429 1.1 christos If you do not specify any makefiles to be read with @samp{-f} or 1430 1.1 christos @samp{--file} options, @code{make} will try the default makefile names; 1431 1.1 christos @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike 1432 1.1 christos makefiles explicitly requested with @samp{-f} or @samp{--file} options, 1433 1.1 christos @code{make} is not certain that these makefiles should exist. However, 1434 1.1 christos if a default makefile does not exist but can be created by running 1435 1.1 christos @code{make} rules, you probably want the rules to be run so that the 1436 1.1 christos makefile can be used. 1437 1.1 christos 1438 1.1 christos Therefore, if none of the default makefiles exists, @code{make} will try 1439 1.1 christos to make each of them in the same order in which they are searched for 1440 1.1 christos (@pxref{Makefile Names, ,What Name to Give Your Makefile}) 1441 1.1 christos until it succeeds in making one, or it runs out of names to try. Note 1442 1.1 christos that it is not an error if @code{make} cannot find or make any makefile; 1443 1.1 christos a makefile is not always necessary.@refill 1444 1.1 christos 1445 1.1 christos When you use the @samp{-t} or @samp{--touch} option 1446 1.1 christos (@pxref{Instead of Execution, ,Instead of Executing the Commands}), 1447 1.1 christos you would not want to use an out-of-date makefile to decide which 1448 1.1 christos targets to touch. So the @samp{-t} option has no effect on updating 1449 1.1 christos makefiles; they are really updated even if @samp{-t} is specified. 1450 1.1 christos Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or 1451 1.1 christos @samp{--just-print}) do not prevent updating of makefiles, because an 1452 1.1 christos out-of-date makefile would result in the wrong output for other targets. 1453 1.1 christos Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in, 1454 1.1 christos and then print the commands to update @file{foo} and its prerequisites 1455 1.1 christos without running them. The commands printed for @file{foo} will be those 1456 1.1 christos specified in the updated contents of @file{mfile}. 1457 1.1 christos 1458 1.1 christos However, on occasion you might actually wish to prevent updating of even 1459 1.1 christos the makefiles. You can do this by specifying the makefiles as goals in 1460 1.1 christos the command line as well as specifying them as makefiles. When the 1461 1.1 christos makefile name is specified explicitly as a goal, the options @samp{-t} 1462 1.1 christos and so on do apply to them. 1463 1.1 christos 1464 1.1 christos Thus, @samp{make -f mfile -n mfile foo} would read the makefile 1465 1.1 christos @file{mfile}, print the commands needed to update it without actually 1466 1.1 christos running them, and then print the commands needed to update @file{foo} 1467 1.1 christos without running them. The commands for @file{foo} will be those 1468 1.1 christos specified by the existing contents of @file{mfile}. 1469 1.1 christos 1470 1.1 christos @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles 1471 1.1 christos @section Overriding Part of Another Makefile 1472 1.1 christos 1473 1.1 christos @cindex overriding makefiles 1474 1.1 christos @cindex makefile, overriding 1475 1.1 christos Sometimes it is useful to have a makefile that is mostly just like 1476 1.1 christos another makefile. You can often use the @samp{include} directive to 1477 1.1 christos include one in the other, and add more targets or variable definitions. 1478 1.1 christos However, if the two makefiles give different commands for the same 1479 1.1 christos target, @code{make} will not let you just do this. But there is another way. 1480 1.1 christos 1481 1.1 christos @cindex match-anything rule, used to override 1482 1.1 christos In the containing makefile (the one that wants to include the other), 1483 1.1 christos you can use a match-anything pattern rule to say that to remake any 1484 1.1 christos target that cannot be made from the information in the containing 1485 1.1 christos makefile, @code{make} should look in another makefile. 1486 1.1 christos @xref{Pattern Rules}, for more information on pattern rules. 1487 1.1 christos 1488 1.1 christos For example, if you have a makefile called @file{Makefile} that says how 1489 1.1 christos to make the target @samp{foo} (and other targets), you can write a 1490 1.1 christos makefile called @file{GNUmakefile} that contains: 1491 1.1 christos 1492 1.1 christos @example 1493 1.1 christos foo: 1494 1.1 christos frobnicate > foo 1495 1.1 christos 1496 1.1 christos %: force 1497 1.1 christos @@$(MAKE) -f Makefile $@@ 1498 1.1 christos force: ; 1499 1.1 christos @end example 1500 1.1 christos 1501 1.1 christos If you say @samp{make foo}, @code{make} will find @file{GNUmakefile}, 1502 1.1 christos read it, and see that to make @file{foo}, it needs to run the command 1503 1.1 christos @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will 1504 1.1 christos find no way to make @file{bar} in @file{GNUmakefile}, so it will use the 1505 1.1 christos commands from the pattern rule: @samp{make -f Makefile bar}. If 1506 1.1 christos @file{Makefile} provides a rule for updating @file{bar}, @code{make} 1507 1.1 christos will apply the rule. And likewise for any other target that 1508 1.1 christos @file{GNUmakefile} does not say how to make. 1509 1.1 christos 1510 1.1 christos The way this works is that the pattern rule has a pattern of just 1511 1.1 christos @samp{%}, so it matches any target whatever. The rule specifies a 1512 1.1 christos prerequisite @file{force}, to guarantee that the commands will be run even 1513 1.1 christos if the target file already exists. We give @file{force} target empty 1514 1.1 christos commands to prevent @code{make} from searching for an implicit rule to 1515 1.1 christos build it---otherwise it would apply the same match-anything rule to 1516 1.1 christos @file{force} itself and create a prerequisite loop! 1517 1.1 christos 1518 1.1 christos @node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles 1519 1.1 christos @section How @code{make} Reads a Makefile 1520 1.1 christos @cindex reading makefiles 1521 1.1 christos @cindex makefile, parsing 1522 1.1 christos 1523 1.1 christos GNU @code{make} does its work in two distinct phases. During the first 1524 1.1 christos phase it reads all the makefiles, included makefiles, etc. and 1525 1.1 christos internalizes all the variables and their values, implicit and explicit 1526 1.1 christos rules, and constructs a dependency graph of all the targets and their 1527 1.1 christos prerequisites. During the second phase, @code{make} uses these internal 1528 1.1 christos structures to determine what targets will need to be rebuilt and to 1529 1.1 christos invoke the rules necessary to do so. 1530 1.1 christos 1531 1.1 christos It's important to understand this two-phase approach because it has a 1532 1.1 christos direct impact on how variable and function expansion happens; this is 1533 1.1 christos often a source of some confusion when writing makefiles. Here we will 1534 1.1 christos present a summary of the phases in which expansion happens for different 1535 1.1 christos constructs within the makefile. We say that expansion is 1536 1.1 christos @dfn{immediate} if it happens during the first phase: in this case 1537 1.1 christos @code{make} will expand any variables or functions in that section of a 1538 1.1 christos construct as the makefile is parsed. We say that expansion is 1539 1.1 christos @dfn{deferred} if expansion is not performed immediately. Expansion of 1540 1.1 christos deferred construct is not performed until either the construct appears 1541 1.1 christos later in an immediate context, or until the second phase. 1542 1.1 christos 1543 1.1 christos You may not be familiar with some of these constructs yet. You can 1544 1.1 christos reference this section as you become familiar with them, in later 1545 1.1 christos chapters. 1546 1.1 christos 1547 1.1 christos @subheading Variable Assignment 1548 1.1 christos @cindex +=, expansion 1549 1.1 christos @cindex =, expansion 1550 1.1 christos @cindex ?=, expansion 1551 1.1 christos @cindex +=, expansion 1552 1.1 christos @cindex define, expansion 1553 1.1 christos 1554 1.1 christos Variable definitions are parsed as follows: 1555 1.1 christos 1556 1.1 christos @example 1557 1.1 christos @var{immediate} = @var{deferred} 1558 1.1 christos @var{immediate} ?= @var{deferred} 1559 1.1 christos @var{immediate} := @var{immediate} 1560 1.1 christos @var{immediate} += @var{deferred} or @var{immediate} 1561 1.1 christos 1562 1.1 christos define @var{immediate} 1563 1.1 christos @var{deferred} 1564 1.1 christos endef 1565 1.1 christos @end example 1566 1.1 christos 1567 1.1 christos For the append operator, @samp{+=}, the right-hand side is considered 1568 1.1 christos immediate if the variable was previously set as a simple variable 1569 1.1 christos (@samp{:=}), and deferred otherwise. 1570 1.1 christos 1571 1.1 christos @subheading Conditional Statements 1572 1.1 christos @cindex ifdef, expansion 1573 1.1 christos @cindex ifeq, expansion 1574 1.1 christos @cindex ifndef, expansion 1575 1.1 christos @cindex ifneq, expansion 1576 1.1 christos 1577 1.1 christos All instances of conditional syntax are parsed immediately, in their 1578 1.1 christos entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef}, 1579 1.1 christos and @code{ifneq} forms. Of course this means that automatic variables 1580 1.1 christos cannot be used in conditional statements, as automatic variables are 1581 1.1 christos not set until the command script for that rule is invoked. If you 1582 1.1 christos need to use automatic variables in a conditional you @emph{must} use 1583 1.1 christos shell conditional syntax, in your command script proper, for these 1584 1.1 christos tests, not @code{make} conditionals. 1585 1.1 christos 1586 1.1 christos @subheading Rule Definition 1587 1.1 christos @cindex target, expansion 1588 1.1 christos @cindex prerequisite, expansion 1589 1.1 christos @cindex implicit rule, expansion 1590 1.1 christos @cindex pattern rule, expansion 1591 1.1 christos @cindex explicit rule, expansion 1592 1.1 christos 1593 1.1 christos A rule is always expanded the same way, regardless of the form: 1594 1.1 christos 1595 1.1 christos @example 1596 1.1 christos @var{immediate} : @var{immediate} ; @var{deferred} 1597 1.1 christos @var{deferred} 1598 1.1 christos @end example 1599 1.1 christos 1600 1.1 christos That is, the target and prerequisite sections are expanded immediately, 1601 1.1 christos and the commands used to construct the target are always deferred. This 1602 1.1 christos general rule is true for explicit rules, pattern rules, suffix rules, 1603 1.1 christos static pattern rules, and simple prerequisite definitions. 1604 1.1 christos 1605 1.1 christos @node Secondary Expansion, , Reading Makefiles, Makefiles 1606 1.1 christos @section Secondary Expansion 1607 1.1 christos @cindex secondary expansion 1608 1.1 christos @cindex expansion, secondary 1609 1.1 christos 1610 1.1 christos @findex .SECONDEXPANSION 1611 1.1 christos In the previous section we learned that GNU @code{make} works in two 1612 1.1 christos distinct phases: a read-in phase and a target-update phase 1613 1.1 christos (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU 1614 1.1 christos make also has the ability to enable a @emph{second expansion} of the 1615 1.1 christos prerequisites (only) for some or all targets defined in the makefile. 1616 1.1 christos In order for this second expansion to occur, the special target 1617 1.1 christos @code{.SECONDEXPANSION} must be defined before the first prerequisite 1618 1.1 christos list that makes use of this feature. 1619 1.1 christos 1620 1.1 christos If that special target is defined then in between the two phases 1621 1.1 christos mentioned above, right at the end of the read-in phase, all the 1622 1.1 christos prerequisites of the targets defined after the special target are 1623 1.1 christos expanded a @emph{second time}. In most circumstances this secondary 1624 1.1 christos expansion will have no effect, since all variable and function 1625 1.1 christos references will have been expanded during the initial parsing of the 1626 1.1 christos makefiles. In order to take advantage of the secondary expansion 1627 1.1 christos phase of the parser, then, it's necessary to @emph{escape} the 1628 1.1 christos variable or function reference in the makefile. In this case the 1629 1.1 christos first expansion merely un-escapes the reference but doesn't expand it, 1630 1.1 christos and expansion is left to the secondary expansion phase. For example, 1631 1.1 christos consider this makefile: 1632 1.1 christos 1633 1.1 christos @example 1634 1.1 christos .SECONDEXPANSION: 1635 1.1 christos ONEVAR = onefile 1636 1.1 christos TWOVAR = twofile 1637 1.1 christos myfile: $(ONEVAR) $$(TWOVAR) 1638 1.1 christos @end example 1639 1.1 christos 1640 1.1 christos After the first expansion phase the prerequisites list of the 1641 1.1 christos @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the 1642 1.1 christos first (unescaped) variable reference to @var{ONEVAR} is expanded, 1643 1.1 christos while the second (escaped) variable reference is simply unescaped, 1644 1.1 christos without being recognized as a variable reference. Now during the 1645 1.1 christos secondary expansion the first word is expanded again but since it 1646 1.1 christos contains no variable or function references it remains the static 1647 1.1 christos value @file{onefile}, while the second word is now a normal reference 1648 1.1 christos to the variable @var{TWOVAR}, which is expanded to the value 1649 1.1 christos @file{twofile}. The final result is that there are two prerequisites, 1650 1.1 christos @file{onefile} and @file{twofile}. 1651 1.1 christos 1652 1.1 christos Obviously, this is not a very interesting case since the same result 1653 1.1 christos could more easily have been achieved simply by having both variables 1654 1.1 christos appear, unescaped, in the prerequisites list. One difference becomes 1655 1.1 christos apparent if the variables are reset; consider this example: 1656 1.1 christos 1657 1.1 christos @example 1658 1.1 christos .SECONDEXPANSION: 1659 1.1 christos AVAR = top 1660 1.1 christos onefile: $(AVAR) 1661 1.1 christos twofile: $$(AVAR) 1662 1.1 christos AVAR = bottom 1663 1.1 christos @end example 1664 1.1 christos 1665 1.1 christos Here the prerequisite of @file{onefile} will be expanded immediately, 1666 1.1 christos and resolve to the value @file{top}, while the prerequisite of 1667 1.1 christos @file{twofile} will not be full expanded until the secondary expansion 1668 1.1 christos and yield a value of @file{bottom}. 1669 1.1 christos 1670 1.1 christos This is marginally more exciting, but the true power of this feature 1671 1.1 christos only becomes apparent when you discover that secondary expansions 1672 1.1 christos always take place within the scope of the automatic variables for that 1673 1.1 christos target. This means that you can use variables such as @code{$@@}, 1674 1.1 christos @code{$*}, etc. during the second expansion and they will have their 1675 1.1 christos expected values, just as in the command script. All you have to do is 1676 1.1 christos defer the expansion by escaping the @code{$}. Also, secondary 1677 1.1 christos expansion occurs for both explicit and implicit (pattern) rules. 1678 1.1 christos Knowing this, the possible uses for this feature increase 1679 1.1 christos dramatically. For example: 1680 1.1 christos 1681 1.1 christos @example 1682 1.1 christos .SECONDEXPANSION: 1683 1.1 christos main_OBJS := main.o try.o test.o 1684 1.1 christos lib_OBJS := lib.o api.o 1685 1.1 christos 1686 1.1 christos main lib: $$($$@@_OBJS) 1687 1.1 christos @end example 1688 1.1 christos 1689 1.1 christos Here, after the initial expansion the prerequisites of both the 1690 1.1 christos @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During 1691 1.1 christos the secondary expansion, the @code{$@@} variable is set to the name of 1692 1.1 christos the target and so the expansion for the @file{main} target will yield 1693 1.1 christos @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the 1694 1.1 christos secondary expansion for the @file{lib} target will yield 1695 1.1 christos @code{$(lib_OBJS)}, or @code{lib.o api.o}. 1696 1.1 christos 1697 1.1 christos You can also mix functions here, as long as they are properly escaped: 1698 1.1 christos 1699 1.1 christos @example 1700 1.1 christos main_SRCS := main.c try.c test.c 1701 1.1 christos lib_SRCS := lib.c api.c 1702 1.1 christos 1703 1.1 christos .SECONDEXPANSION: 1704 1.1 christos main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS)) 1705 1.1 christos @end example 1706 1.1 christos 1707 1.1 christos This version allows users to specify source files rather than object 1708 1.1 christos files, but gives the same resulting prerequisites list as the previous 1709 1.1 christos example. 1710 1.1 christos 1711 1.1 christos Evaluation of automatic variables during the secondary expansion 1712 1.1 christos phase, especially of the target name variable @code{$$@@}, behaves 1713 1.1 christos similarly to evaluation within command scripts. However, there are 1714 1.1 christos some subtle differences and ``corner cases'' which come into play for 1715 1.1 christos the different types of rule definitions that @code{make} understands. 1716 1.1 christos The subtleties of using the different automatic variables are 1717 1.1 christos described below. 1718 1.1 christos 1719 1.1 christos @subheading Secondary Expansion of Explicit Rules 1720 1.1 christos @cindex secondary expansion and explicit rules 1721 1.1 christos @cindex explicit rules, secondary expansion of 1722 1.1 christos 1723 1.1 christos During the secondary expansion of explicit rules, @code{$$@@} and 1724 1.1 christos @code{$$%} evaluate, respectively, to the file name of the target and, 1725 1.1 christos when the target is an archive member, the target member name. The 1726 1.1 christos @code{$$<} variable evaluates to the first prerequisite in the first 1727 1.1 christos rule for this target. @code{$$^} and @code{$$+} evaluate to the list 1728 1.1 christos of all prerequisites of rules @emph{that have already appeared} for 1729 1.1 christos the same target (@code{$$+} with repetitions and @code{$$^} 1730 1.1 christos without). The following example will help illustrate these behaviors: 1731 1.1 christos 1732 1.1 christos @example 1733 1.1 christos .SECONDEXPANSION: 1734 1.1 christos 1735 1.1 christos foo: foo.1 bar.1 $$< $$^ $$+ # line #1 1736 1.1 christos 1737 1.1 christos foo: foo.2 bar.2 $$< $$^ $$+ # line #2 1738 1.1 christos 1739 1.1 christos foo: foo.3 bar.3 $$< $$^ $$+ # line #3 1740 1.1 christos @end example 1741 1.1 christos 1742 1.1 christos In the first prerequisite list, all three variables (@code{$$<}, 1743 1.1 christos @code{$$^}, and @code{$$+}) expand to the empty string. In the 1744 1.1 christos second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and 1745 1.1 christos @code{foo.1 bar.1} respectively. In the third they will have values 1746 1.1 christos @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1 1747 1.1 christos foo.2 bar.2} respectively. 1748 1.1 christos 1749 1.1 christos Rules undergo secondary expansion in makefile order, except that 1750 1.1 christos the rule with the command script is always evaluated last. 1751 1.1 christos 1752 1.1 christos The variables @code{$$?} and @code{$$*} are not available and expand 1753 1.1 christos to the empty string. 1754 1.1 christos 1755 1.1 christos @subheading Secondary Expansion of Static Pattern Rules 1756 1.1 christos @cindex secondary expansion and static pattern rules 1757 1.1 christos @cindex static pattern rules, secondary expansion of 1758 1.1 christos 1759 1.1 christos Rules for secondary expansion of static pattern rules are identical to 1760 1.1 christos those for explicit rules, above, with one exception: for static 1761 1.1 christos pattern rules the @code{$$*} variable is set to the pattern stem. As 1762 1.1 christos with explicit rules, @code{$$?} is not available and expands to the 1763 1.1 christos empty string. 1764 1.1 christos 1765 1.1 christos @subheading Secondary Expansion of Implicit Rules 1766 1.1 christos @cindex secondary expansion and implicit rules 1767 1.1 christos @cindex implicit rules, secondary expansion of 1768 1.1 christos 1769 1.1 christos As @code{make} searches for an implicit rule, it substitutes the stem 1770 1.1 christos and then performs secondary expansion for every rule with a matching 1771 1.1 christos target pattern. The value of the automatic variables is derived in 1772 1.1 christos the same fashion as for static pattern rules. As an example: 1773 1.1 christos 1774 1.1 christos @example 1775 1.1 christos .SECONDEXPANSION: 1776 1.1 christos 1777 1.1 christos foo: bar 1778 1.1 christos 1779 1.1 christos foo foz: fo%: bo% 1780 1.1 christos 1781 1.1 christos %oo: $$< $$^ $$+ $$* 1782 1.1 christos @end example 1783 1.1 christos 1784 1.1 christos When the implicit rule is tried for target @file{foo}, @code{$$<} 1785 1.1 christos expands to @file{bar}, @code{$$^} expands to @file{bar boo}, 1786 1.1 christos @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to 1787 1.1 christos @file{f}. 1788 1.1 christos 1789 1.1 christos Note that the directory prefix (D), as described in @ref{Implicit Rule 1790 1.1 christos Search, ,Implicit Rule Search Algorithm}, is appended (after 1791 1.1 christos expansion) to all the patterns in the prerequisites list. As an 1792 1.1 christos example: 1793 1.1 christos 1794 1.1 christos @example 1795 1.1 christos .SECONDEXPANSION: 1796 1.1 christos 1797 1.1 christos /tmp/foo.o: 1798 1.1 christos 1799 1.1 christos %.o: $$(addsuffix /%.c,foo bar) foo.h 1800 1.1 christos @end example 1801 1.1 christos 1802 1.1 christos The prerequisite list after the secondary expansion and directory 1803 1.1 christos prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c 1804 1.1 christos foo.h}. If you are not interested in this reconstruction, you can use 1805 1.1 christos @code{$$*} instead of @code{%} in the prerequisites list. 1806 1.1 christos 1807 1.1 christos @node Rules, Commands, Makefiles, Top 1808 1.1 christos @chapter Writing Rules 1809 1.1 christos @cindex writing rules 1810 1.1 christos @cindex rule, how to write 1811 1.1 christos @cindex target 1812 1.1 christos @cindex prerequisite 1813 1.1 christos 1814 1.1 christos A @dfn{rule} appears in the makefile and says when and how to remake 1815 1.1 christos certain files, called the rule's @dfn{targets} (most often only one per rule). 1816 1.1 christos It lists the other files that are the @dfn{prerequisites} of the target, and 1817 1.1 christos @dfn{commands} to use to create or update the target. 1818 1.1 christos 1819 1.1 christos @cindex default goal 1820 1.1 christos @cindex goal, default 1821 1.1 christos The order of rules is not significant, except for determining the 1822 1.1 christos @dfn{default goal}: the target for @code{make} to consider, if you do 1823 1.1 christos not otherwise specify one. The default goal is the target of the first 1824 1.1 christos rule in the first makefile. If the first rule has multiple targets, 1825 1.1 christos only the first target is taken as the default. There are two 1826 1.1 christos exceptions: a target starting with a period is not a default unless it 1827 1.1 christos contains one or more slashes, @samp{/}, as well; and, a target that 1828 1.1 christos defines a pattern rule has no effect on the default goal. 1829 1.1 christos (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.) 1830 1.1 christos 1831 1.1 christos Therefore, we usually write the makefile so that the first rule is the 1832 1.1 christos one for compiling the entire program or all the programs described by 1833 1.1 christos the makefile (often with a target called @samp{all}). 1834 1.1 christos @xref{Goals, ,Arguments to Specify the Goals}. 1835 1.1 christos 1836 1.1 christos @menu 1837 1.1 christos * Rule Example:: An example explained. 1838 1.1 christos * Rule Syntax:: General syntax explained. 1839 1.1 christos * Prerequisite Types:: There are two types of prerequisites. 1840 1.1 christos * Wildcards:: Using wildcard characters such as `*'. 1841 1.1 christos * Directory Search:: Searching other directories for source files. 1842 1.1 christos * Phony Targets:: Using a target that is not a real file's name. 1843 1.1 christos * Force Targets:: You can use a target without commands 1844 1.1 christos or prerequisites to mark other targets 1845 1.1 christos as phony. 1846 1.1 christos * Empty Targets:: When only the date matters and the 1847 1.1 christos files are empty. 1848 1.1 christos * Special Targets:: Targets with special built-in meanings. 1849 1.1 christos * Multiple Targets:: When to make use of several targets in a rule. 1850 1.1 christos * Multiple Rules:: How to use several rules with the same target. 1851 1.1 christos * Static Pattern:: Static pattern rules apply to multiple targets 1852 1.1 christos and can vary the prerequisites according to 1853 1.1 christos the target name. 1854 1.1 christos * Double-Colon:: How to use a special kind of rule to allow 1855 1.1 christos several independent rules for one target. 1856 1.1 christos * Automatic Prerequisites:: How to automatically generate rules giving 1857 1.1 christos prerequisites from source files themselves. 1858 1.1 christos @end menu 1859 1.1 christos 1860 1.1 christos @ifnottex 1861 1.1 christos @node Rule Example, Rule Syntax, Rules, Rules 1862 1.1 christos @section Rule Example 1863 1.1 christos 1864 1.1 christos Here is an example of a rule: 1865 1.1 christos 1866 1.1 christos @example 1867 1.1 christos foo.o : foo.c defs.h # module for twiddling the frobs 1868 1.1 christos cc -c -g foo.c 1869 1.1 christos @end example 1870 1.1 christos 1871 1.1 christos Its target is @file{foo.o} and its prerequisites are @file{foo.c} and 1872 1.1 christos @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}. 1873 1.1 christos The command line starts with a tab to identify it as a command. 1874 1.1 christos 1875 1.1 christos This rule says two things: 1876 1.1 christos 1877 1.1 christos @itemize @bullet 1878 1.1 christos @item 1879 1.1 christos How to decide whether @file{foo.o} is out of date: it is out of date 1880 1.1 christos if it does not exist, or if either @file{foo.c} or @file{defs.h} is 1881 1.1 christos more recent than it. 1882 1.1 christos 1883 1.1 christos @item 1884 1.1 christos How to update the file @file{foo.o}: by running @code{cc} as stated. 1885 1.1 christos The command does not explicitly mention @file{defs.h}, but we presume 1886 1.1 christos that @file{foo.c} includes it, and that that is why @file{defs.h} was 1887 1.1 christos added to the prerequisites. 1888 1.1 christos @end itemize 1889 1.1 christos @end ifnottex 1890 1.1 christos 1891 1.1 christos @node Rule Syntax, Prerequisite Types, Rule Example, Rules 1892 1.1 christos @section Rule Syntax 1893 1.1 christos 1894 1.1 christos @cindex rule syntax 1895 1.1 christos @cindex syntax of rules 1896 1.1 christos In general, a rule looks like this: 1897 1.1 christos 1898 1.1 christos @example 1899 1.1 christos @var{targets} : @var{prerequisites} 1900 1.1 christos @var{command} 1901 1.1 christos @dots{} 1902 1.1 christos @end example 1903 1.1 christos 1904 1.1 christos @noindent 1905 1.1 christos or like this: 1906 1.1 christos 1907 1.1 christos @example 1908 1.1 christos @var{targets} : @var{prerequisites} ; @var{command} 1909 1.1 christos @var{command} 1910 1.1 christos @dots{} 1911 1.1 christos @end example 1912 1.1 christos 1913 1.1 christos @cindex targets 1914 1.1 christos @cindex rule targets 1915 1.1 christos The @var{targets} are file names, separated by spaces. Wildcard 1916 1.1 christos characters may be used (@pxref{Wildcards, ,Using Wildcard Characters 1917 1.1 christos in File Names}) and a name of the form @file{@var{a}(@var{m})} 1918 1.1 christos represents member @var{m} in archive file @var{a} 1919 1.1 christos (@pxref{Archive Members, ,Archive Members as Targets}). 1920 1.1 christos Usually there is only one 1921 1.1 christos target per rule, but occasionally there is a reason to have more 1922 1.1 christos (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill 1923 1.1 christos 1924 1.1 christos @cindex commands 1925 1.1 christos @cindex tab character (in commands) 1926 1.1 christos The @var{command} lines start with a tab character. The first command may 1927 1.1 christos appear on the line after the prerequisites, with a tab character, or may 1928 1.1 christos appear on the same line, with a semicolon. Either way, the effect is the 1929 1.1 christos same. There are other differences in the syntax of command lines. 1930 1.1 christos @xref{Commands, ,Writing the Commands in Rules}. 1931 1.1 christos 1932 1.1 christos @cindex dollar sign (@code{$}), in rules 1933 1.1 christos @cindex @code{$}, in rules 1934 1.1 christos @cindex rules, and @code{$} 1935 1.1 christos Because dollar signs are used to start @code{make} variable 1936 1.1 christos references, if you really want a dollar sign in a target or 1937 1.1 christos prerequisite you must write two of them, @samp{$$} (@pxref{Using 1938 1.1 christos Variables, ,How to Use Variables}). If you have enabled secondary 1939 1.1 christos expansion (@pxref{Secondary Expansion}) and you want a literal dollar 1940 1.1 christos sign in the prerequisites lise, you must actually write @emph{four} 1941 1.1 christos dollar signs (@samp{$$$$}). 1942 1.1 christos 1943 1.1 christos You may split a long line by inserting a backslash followed by a 1944 1.1 christos newline, but this is not required, as @code{make} places no limit on 1945 1.1 christos the length of a line in a makefile. 1946 1.1 christos 1947 1.1 christos A rule tells @code{make} two things: when the targets are out of date, 1948 1.1 christos and how to update them when necessary. 1949 1.1 christos 1950 1.1 christos @cindex prerequisites 1951 1.1 christos @cindex rule prerequisites 1952 1.1 christos The criterion for being out of date is specified in terms of the 1953 1.1 christos @var{prerequisites}, which consist of file names separated by spaces. 1954 1.1 christos (Wildcards and archive members (@pxref{Archives}) are allowed here too.) 1955 1.1 christos A target is out of date if it does not exist or if it is older than any 1956 1.1 christos of the prerequisites (by comparison of last-modification times). The 1957 1.1 christos idea is that the contents of the target file are computed based on 1958 1.1 christos information in the prerequisites, so if any of the prerequisites changes, 1959 1.1 christos the contents of the existing target file are no longer necessarily 1960 1.1 christos valid. 1961 1.1 christos 1962 1.1 christos How to update is specified by @var{commands}. These are lines to be 1963 1.1 christos executed by the shell (normally @samp{sh}), but with some extra features 1964 1.1 christos (@pxref{Commands, ,Writing the Commands in Rules}). 1965 1.1 christos 1966 1.1 christos @node Prerequisite Types, Wildcards, Rule Syntax, Rules 1967 1.1 christos @comment node-name, next, previous, up 1968 1.1 christos @section Types of Prerequisites 1969 1.1 christos @cindex prerequisite types 1970 1.1 christos @cindex types of prerequisites 1971 1.1 christos 1972 1.1 christos @cindex prerequisites, normal 1973 1.1 christos @cindex normal prerequisites 1974 1.1 christos @cindex prerequisites, order-only 1975 1.1 christos @cindex order-only prerequisites 1976 1.1 christos There are actually two different types of prerequisites understood by 1977 1.1 christos GNU @code{make}: normal prerequisites such as described in the 1978 1.1 christos previous section, and @dfn{order-only} prerequisites. A normal 1979 1.1 christos prerequisite makes two statements: first, it imposes an order of 1980 1.1 christos execution of build commands: any commands necessary to build any of a 1981 1.1 christos target's prerequisites will be fully executed before any commands 1982 1.1 christos necessary to build the target. Second, it imposes a dependency 1983 1.1 christos relationship: if any prerequisite is newer than the target, then the 1984 1.1 christos target is considered out-of-date and must be rebuilt. 1985 1.1 christos 1986 1.1 christos Normally, this is exactly what you want: if a target's prerequisite is 1987 1.1 christos updated, then the target should also be updated. 1988 1.1 christos 1989 1.1 christos Occasionally, however, you have a situation where you want to impose a 1990 1.1 christos specific ordering on the rules to be invoked @emph{without} forcing 1991 1.1 christos the target to be updated if one of those rules is executed. In that 1992 1.1 christos case, you want to define @dfn{order-only} prerequisites. Order-only 1993 1.1 christos prerequisites can be specified by placing a pipe symbol (@code{|}) 1994 1.1 christos in the prerequisites list: any prerequisites to the left of the pipe 1995 1.1 christos symbol are normal; any prerequisites to the right are order-only: 1996 1.1 christos 1997 1.1 christos @example 1998 1.1 christos @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites} 1999 1.1 christos @end example 2000 1.1 christos 2001 1.1 christos The normal prerequisites section may of course be empty. Also, you 2002 1.1 christos may still declare multiple lines of prerequisites for the same target: 2003 1.1 christos they are appended appropriately. Note that if you declare the same 2004 1.1 christos file to be both a normal and an order-only prerequisite, the normal 2005 1.1 christos prerequisite takes precedence (since they are a strict superset of the 2006 1.1 christos behavior of an order-only prerequisite). 2007 1.1 christos 2008 1.1 christos @node Wildcards, Directory Search, Prerequisite Types, Rules 2009 1.1 christos @section Using Wildcard Characters in File Names 2010 1.1 christos @cindex wildcard 2011 1.1 christos @cindex file name with wildcards 2012 1.1 christos @cindex globbing (wildcards) 2013 1.1 christos 2014 1.1 christos @cindex @code{*} (wildcard character) 2015 1.1 christos @cindex @code{?} (wildcard character) 2016 1.1 christos @cindex @code{[@dots{}]} (wildcard characters) 2017 1.1 christos A single file name can specify many files using @dfn{wildcard characters}. 2018 1.1 christos The wildcard characters in @code{make} are @samp{*}, @samp{?} and 2019 1.1 christos @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c} 2020 1.1 christos specifies a list of all the files (in the working directory) whose names 2021 1.1 christos end in @samp{.c}.@refill 2022 1.1 christos 2023 1.1 christos @cindex @code{~} (tilde) 2024 1.1 christos @cindex tilde (@code{~}) 2025 1.1 christos @cindex home directory 2026 1.1 christos The character @samp{~} at the beginning of a file name also has special 2027 1.1 christos significance. If alone, or followed by a slash, it represents your home 2028 1.1 christos directory. For example @file{~/bin} expands to @file{/home/you/bin}. 2029 1.1 christos If the @samp{~} is followed by a word, the string represents the home 2030 1.1 christos directory of the user named by that word. For example @file{~john/bin} 2031 1.1 christos expands to @file{/home/john/bin}. On systems which don't have a home 2032 1.1 christos directory for each user (such as MS-DOS or MS-Windows), this 2033 1.1 christos functionality can be simulated by setting the environment variable 2034 1.1 christos @var{HOME}.@refill 2035 1.1 christos 2036 1.1 christos Wildcard expansion is performed by @code{make} automatically in 2037 1.1 christos targets and in prerequisites. In commands the shell is responsible 2038 1.1 christos for wildcard expansion. In other contexts, wildcard expansion happens 2039 1.1 christos only if you request it explicitly with the @code{wildcard} function. 2040 1.1 christos 2041 1.1 christos The special significance of a wildcard character can be turned off by 2042 1.1 christos preceding it with a backslash. Thus, @file{foo\*bar} would refer to a 2043 1.1 christos specific file whose name consists of @samp{foo}, an asterisk, and 2044 1.1 christos @samp{bar}.@refill 2045 1.1 christos 2046 1.1 christos @menu 2047 1.1 christos * Wildcard Examples:: Several examples 2048 1.1 christos * Wildcard Pitfall:: Problems to avoid. 2049 1.1 christos * Wildcard Function:: How to cause wildcard expansion where 2050 1.1 christos it does not normally take place. 2051 1.1 christos @end menu 2052 1.1 christos 2053 1.1 christos @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards 2054 1.1 christos @subsection Wildcard Examples 2055 1.1 christos 2056 1.1 christos Wildcards can be used in the commands of a rule, where they are expanded 2057 1.1 christos by the shell. For example, here is a rule to delete all the object files: 2058 1.1 christos 2059 1.1 christos @example 2060 1.1 christos @group 2061 1.1 christos clean: 2062 1.1 christos rm -f *.o 2063 1.1 christos @end group 2064 1.1 christos @end example 2065 1.1 christos @cindex @code{rm} (shell command) 2066 1.1 christos 2067 1.1 christos Wildcards are also useful in the prerequisites of a rule. With the 2068 1.1 christos following rule in the makefile, @samp{make print} will print all the 2069 1.1 christos @samp{.c} files that have changed since the last time you printed them: 2070 1.1 christos 2071 1.1 christos @example 2072 1.1 christos print: *.c 2073 1.1 christos lpr -p $? 2074 1.1 christos touch print 2075 1.1 christos @end example 2076 1.1 christos 2077 1.1 christos @cindex @code{print} target 2078 1.1 christos @cindex @code{lpr} (shell command) 2079 1.1 christos @cindex @code{touch} (shell command) 2080 1.1 christos @noindent 2081 1.1 christos This rule uses @file{print} as an empty target file; see @ref{Empty 2082 1.1 christos Targets, ,Empty Target Files to Record Events}. (The automatic variable 2083 1.1 christos @samp{$?} is used to print only those files that have changed; see 2084 1.1 christos @ref{Automatic Variables}.)@refill 2085 1.1 christos 2086 1.1 christos Wildcard expansion does not happen when you define a variable. Thus, if 2087 1.1 christos you write this: 2088 1.1 christos 2089 1.1 christos @example 2090 1.1 christos objects = *.o 2091 1.1 christos @end example 2092 1.1 christos 2093 1.1 christos @noindent 2094 1.1 christos then the value of the variable @code{objects} is the actual string 2095 1.1 christos @samp{*.o}. However, if you use the value of @code{objects} in a target, 2096 1.1 christos prerequisite or command, wildcard expansion will take place at that time. 2097 1.1 christos To set @code{objects} to the expansion, instead use: 2098 1.1 christos 2099 1.1 christos @example 2100 1.1 christos objects := $(wildcard *.o) 2101 1.1 christos @end example 2102 1.1 christos 2103 1.1 christos @noindent 2104 1.1 christos @xref{Wildcard Function}. 2105 1.1 christos 2106 1.1 christos @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards 2107 1.1 christos @subsection Pitfalls of Using Wildcards 2108 1.1 christos @cindex wildcard pitfalls 2109 1.1 christos @cindex pitfalls of wildcards 2110 1.1 christos @cindex mistakes with wildcards 2111 1.1 christos @cindex errors with wildcards 2112 1.1 christos @cindex problems with wildcards 2113 1.1 christos 2114 1.1 christos Now here is an example of a naive way of using wildcard expansion, that 2115 1.1 christos does not do what you would intend. Suppose you would like to say that the 2116 1.1 christos executable file @file{foo} is made from all the object files in the 2117 1.1 christos directory, and you write this: 2118 1.1 christos 2119 1.1 christos @example 2120 1.1 christos objects = *.o 2121 1.1 christos 2122 1.1 christos foo : $(objects) 2123 1.1 christos cc -o foo $(CFLAGS) $(objects) 2124 1.1 christos @end example 2125 1.1 christos 2126 1.1 christos @noindent 2127 1.1 christos The value of @code{objects} is the actual string @samp{*.o}. Wildcard 2128 1.1 christos expansion happens in the rule for @file{foo}, so that each @emph{existing} 2129 1.1 christos @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if 2130 1.1 christos necessary. 2131 1.1 christos 2132 1.1 christos But what if you delete all the @samp{.o} files? When a wildcard matches 2133 1.1 christos no files, it is left as it is, so then @file{foo} will depend on the 2134 1.1 christos oddly-named file @file{*.o}. Since no such file is likely to exist, 2135 1.1 christos @code{make} will give you an error saying it cannot figure out how to 2136 1.1 christos make @file{*.o}. This is not what you want! 2137 1.1 christos 2138 1.1 christos Actually it is possible to obtain the desired result with wildcard 2139 1.1 christos expansion, but you need more sophisticated techniques, including the 2140 1.1 christos @code{wildcard} function and string substitution. 2141 1.1 christos @ifnottex 2142 1.1 christos @xref{Wildcard Function, ,The Function @code{wildcard}}. 2143 1.1 christos @end ifnottex 2144 1.1 christos @iftex 2145 1.1 christos These are described in the following section. 2146 1.1 christos @end iftex 2147 1.1 christos 2148 1.1 christos @cindex wildcards and MS-DOS/MS-Windows backslashes 2149 1.1 christos @cindex backslashes in pathnames and wildcard expansion 2150 1.1 christos 2151 1.1 christos Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to 2152 1.1 christos separate directories in pathnames, like so: 2153 1.1 christos 2154 1.1 christos @example 2155 1.1 christos c:\foo\bar\baz.c 2156 1.1 christos @end example 2157 1.1 christos 2158 1.1 christos This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the 2159 1.1 christos @file{c:} part is the so-called drive letter). When @code{make} runs on 2160 1.1 christos these systems, it supports backslashes as well as the Unix-style forward 2161 1.1 christos slashes in pathnames. However, this support does @emph{not} include the 2162 1.1 christos wildcard expansion, where backslash is a quote character. Therefore, 2163 1.1 christos you @emph{must} use Unix-style slashes in these cases. 2164 1.1 christos 2165 1.1 christos 2166 1.1 christos @node Wildcard Function, , Wildcard Pitfall, Wildcards 2167 1.1 christos @subsection The Function @code{wildcard} 2168 1.1 christos @findex wildcard 2169 1.1 christos 2170 1.1 christos Wildcard expansion happens automatically in rules. But wildcard expansion 2171 1.1 christos does not normally take place when a variable is set, or inside the 2172 1.1 christos arguments of a function. If you want to do wildcard expansion in such 2173 1.1 christos places, you need to use the @code{wildcard} function, like this: 2174 1.1 christos 2175 1.1 christos @example 2176 1.1 christos $(wildcard @var{pattern}@dots{}) 2177 1.1 christos @end example 2178 1.1 christos 2179 1.1 christos @noindent 2180 1.1 christos This string, used anywhere in a makefile, is replaced by a 2181 1.1 christos space-separated list of names of existing files that match one of the 2182 1.1 christos given file name patterns. If no existing file name matches a pattern, 2183 1.1 christos then that pattern is omitted from the output of the @code{wildcard} 2184 1.1 christos function. Note that this is different from how unmatched wildcards 2185 1.1 christos behave in rules, where they are used verbatim rather than ignored 2186 1.1 christos (@pxref{Wildcard Pitfall}). 2187 1.1 christos 2188 1.1 christos One use of the @code{wildcard} function is to get a list of all the C source 2189 1.1 christos files in a directory, like this: 2190 1.1 christos 2191 1.1 christos @example 2192 1.1 christos $(wildcard *.c) 2193 1.1 christos @end example 2194 1.1 christos 2195 1.1 christos We can change the list of C source files into a list of object files by 2196 1.1 christos replacing the @samp{.c} suffix with @samp{.o} in the result, like this: 2197 1.1 christos 2198 1.1 christos @example 2199 1.1 christos $(patsubst %.c,%.o,$(wildcard *.c)) 2200 1.1 christos @end example 2201 1.1 christos 2202 1.1 christos @noindent 2203 1.1 christos (Here we have used another function, @code{patsubst}. 2204 1.1 christos @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill 2205 1.1 christos 2206 1.1 christos Thus, a makefile to compile all C source files in the directory and then 2207 1.1 christos link them together could be written as follows: 2208 1.1 christos 2209 1.1 christos @example 2210 1.1 christos objects := $(patsubst %.c,%.o,$(wildcard *.c)) 2211 1.1 christos 2212 1.1 christos foo : $(objects) 2213 1.1 christos cc -o foo $(objects) 2214 1.1 christos @end example 2215 1.1 christos 2216 1.1 christos @noindent 2217 1.1 christos (This takes advantage of the implicit rule for compiling C programs, so 2218 1.1 christos there is no need to write explicit rules for compiling the files. 2219 1.1 christos @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of 2220 1.1 christos @samp{:=}, which is a variant of @samp{=}.) 2221 1.1 christos 2222 1.1 christos @node Directory Search, Phony Targets, Wildcards, Rules 2223 1.1 christos @section Searching Directories for Prerequisites 2224 1.1 christos @vindex VPATH 2225 1.1 christos @findex vpath 2226 1.1 christos @cindex vpath 2227 1.1 christos @cindex search path for prerequisites (@code{VPATH}) 2228 1.1 christos @cindex directory search (@code{VPATH}) 2229 1.1 christos 2230 1.1 christos For large systems, it is often desirable to put sources in a separate 2231 1.1 christos directory from the binaries. The @dfn{directory search} features of 2232 1.1 christos @code{make} facilitate this by searching several directories 2233 1.1 christos automatically to find a prerequisite. When you redistribute the files 2234 1.1 christos among directories, you do not need to change the individual rules, 2235 1.1 christos just the search paths. 2236 1.1 christos 2237 1.1 christos @menu 2238 1.1 christos * General Search:: Specifying a search path that applies 2239 1.1 christos to every prerequisite. 2240 1.1 christos * Selective Search:: Specifying a search path 2241 1.1 christos for a specified class of names. 2242 1.1 christos * Search Algorithm:: When and how search paths are applied. 2243 1.1 christos * Commands/Search:: How to write shell commands that work together 2244 1.1 christos with search paths. 2245 1.1 christos * Implicit/Search:: How search paths affect implicit rules. 2246 1.1 christos * Libraries/Search:: Directory search for link libraries. 2247 1.1 christos @end menu 2248 1.1 christos 2249 1.1 christos @node General Search, Selective Search, Directory Search, Directory Search 2250 1.1 christos @subsection @code{VPATH}: Search Path for All Prerequisites 2251 1.1 christos @vindex VPATH 2252 1.1 christos 2253 1.1 christos The value of the @code{make} variable @code{VPATH} specifies a list of 2254 1.1 christos directories that @code{make} should search. Most often, the 2255 1.1 christos directories are expected to contain prerequisite files that are not in the 2256 1.1 christos current directory; however, @code{make} uses @code{VPATH} as a search 2257 1.1 christos list for both prerequisites and targets of rules. 2258 1.1 christos 2259 1.1 christos Thus, if a file that is listed as a target or prerequisite does not exist 2260 1.1 christos in the current directory, @code{make} searches the directories listed in 2261 1.1 christos @code{VPATH} for a file with that name. If a file is found in one of 2262 1.1 christos them, that file may become the prerequisite (see below). Rules may then 2263 1.1 christos specify the names of files in the prerequisite list as if they all 2264 1.1 christos existed in the current directory. @xref{Commands/Search, ,Writing Shell 2265 1.1 christos Commands with Directory Search}. 2266 1.1 christos 2267 1.1 christos In the @code{VPATH} variable, directory names are separated by colons or 2268 1.1 christos blanks. The order in which directories are listed is the order followed 2269 1.1 christos by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons 2270 1.1 christos are used as separators of directory names in @code{VPATH}, since the 2271 1.1 christos colon can be used in the pathname itself, after the drive letter.) 2272 1.1 christos 2273 1.1 christos For example, 2274 1.1 christos 2275 1.1 christos @example 2276 1.1 christos VPATH = src:../headers 2277 1.1 christos @end example 2278 1.1 christos 2279 1.1 christos @noindent 2280 1.1 christos specifies a path containing two directories, @file{src} and 2281 1.1 christos @file{../headers}, which @code{make} searches in that order. 2282 1.1 christos 2283 1.1 christos With this value of @code{VPATH}, the following rule, 2284 1.1 christos 2285 1.1 christos @example 2286 1.1 christos foo.o : foo.c 2287 1.1 christos @end example 2288 1.1 christos 2289 1.1 christos @noindent 2290 1.1 christos is interpreted as if it were written like this: 2291 1.1 christos 2292 1.1 christos @example 2293 1.1 christos foo.o : src/foo.c 2294 1.1 christos @end example 2295 1.1 christos 2296 1.1 christos @noindent 2297 1.1 christos assuming the file @file{foo.c} does not exist in the current directory but 2298 1.1 christos is found in the directory @file{src}. 2299 1.1 christos 2300 1.1 christos @node Selective Search, Search Algorithm, General Search, Directory Search 2301 1.1 christos @subsection The @code{vpath} Directive 2302 1.1 christos @findex vpath 2303 1.1 christos 2304 1.1 christos Similar to the @code{VPATH} variable, but more selective, is the 2305 1.1 christos @code{vpath} directive (note lower case), which allows you to specify a 2306 1.1 christos search path for a particular class of file names: those that match a 2307 1.1 christos particular pattern. Thus you can supply certain search directories for 2308 1.1 christos one class of file names and other directories (or none) for other file 2309 1.1 christos names. 2310 1.1 christos 2311 1.1 christos There are three forms of the @code{vpath} directive: 2312 1.1 christos 2313 1.1 christos @table @code 2314 1.1 christos @item vpath @var{pattern} @var{directories} 2315 1.1 christos Specify the search path @var{directories} for file names that match 2316 1.1 christos @var{pattern}. 2317 1.1 christos 2318 1.1 christos The search path, @var{directories}, is a list of directories to be 2319 1.1 christos searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or 2320 1.1 christos blanks, just like the search path used in the @code{VPATH} variable. 2321 1.1 christos 2322 1.1 christos @item vpath @var{pattern} 2323 1.1 christos Clear out the search path associated with @var{pattern}. 2324 1.1 christos 2325 1.1 christos @c Extra blank line makes sure this gets two lines. 2326 1.1 christos @item vpath 2327 1.1 christos 2328 1.1 christos Clear all search paths previously specified with @code{vpath} directives. 2329 1.1 christos @end table 2330 1.1 christos 2331 1.1 christos A @code{vpath} pattern is a string containing a @samp{%} character. The 2332 1.1 christos string must match the file name of a prerequisite that is being searched 2333 1.1 christos for, the @samp{%} character matching any sequence of zero or more 2334 1.1 christos characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and 2335 1.1 christos Redefining Pattern Rules}). For example, @code{%.h} matches files that 2336 1.1 christos end in @code{.h}. (If there is no @samp{%}, the pattern must match the 2337 1.1 christos prerequisite exactly, which is not useful very often.) 2338 1.1 christos 2339 1.1 christos @cindex @code{%}, quoting in @code{vpath} 2340 1.1 christos @cindex @code{%}, quoting with @code{\} (backslash) 2341 1.1 christos @cindex @code{\} (backslash), to quote @code{%} 2342 1.1 christos @cindex backslash (@code{\}), to quote @code{%} 2343 1.1 christos @cindex quoting @code{%}, in @code{vpath} 2344 1.1 christos @samp{%} characters in a @code{vpath} directive's pattern can be quoted 2345 1.1 christos with preceding backslashes (@samp{\}). Backslashes that would otherwise 2346 1.1 christos quote @samp{%} characters can be quoted with more backslashes. 2347 1.1 christos Backslashes that quote @samp{%} characters or other backslashes are 2348 1.1 christos removed from the pattern before it is compared to file names. Backslashes 2349 1.1 christos that are not in danger of quoting @samp{%} characters go unmolested.@refill 2350 1.1 christos 2351 1.1 christos When a prerequisite fails to exist in the current directory, if the 2352 1.1 christos @var{pattern} in a @code{vpath} directive matches the name of the 2353 1.1 christos prerequisite file, then the @var{directories} in that directive are searched 2354 1.1 christos just like (and before) the directories in the @code{VPATH} variable. 2355 1.1 christos 2356 1.1 christos For example, 2357 1.1 christos 2358 1.1 christos @example 2359 1.1 christos vpath %.h ../headers 2360 1.1 christos @end example 2361 1.1 christos 2362 1.1 christos @noindent 2363 1.1 christos tells @code{make} to look for any prerequisite whose name ends in @file{.h} 2364 1.1 christos in the directory @file{../headers} if the file is not found in the current 2365 1.1 christos directory. 2366 1.1 christos 2367 1.1 christos If several @code{vpath} patterns match the prerequisite file's name, then 2368 1.1 christos @code{make} processes each matching @code{vpath} directive one by one, 2369 1.1 christos searching all the directories mentioned in each directive. @code{make} 2370 1.1 christos handles multiple @code{vpath} directives in the order in which they 2371 1.1 christos appear in the makefile; multiple directives with the same pattern are 2372 1.1 christos independent of each other. 2373 1.1 christos 2374 1.1 christos @need 750 2375 1.1 christos Thus, 2376 1.1 christos 2377 1.1 christos @example 2378 1.1 christos @group 2379 1.1 christos vpath %.c foo 2380 1.1 christos vpath % blish 2381 1.1 christos vpath %.c bar 2382 1.1 christos @end group 2383 1.1 christos @end example 2384 1.1 christos 2385 1.1 christos @noindent 2386 1.1 christos will look for a file ending in @samp{.c} in @file{foo}, then 2387 1.1 christos @file{blish}, then @file{bar}, while 2388 1.1 christos 2389 1.1 christos @example 2390 1.1 christos @group 2391 1.1 christos vpath %.c foo:bar 2392 1.1 christos vpath % blish 2393 1.1 christos @end group 2394 1.1 christos @end example 2395 1.1 christos 2396 1.1 christos @noindent 2397 1.1 christos will look for a file ending in @samp{.c} in @file{foo}, then 2398 1.1 christos @file{bar}, then @file{blish}. 2399 1.1 christos 2400 1.1 christos @node Search Algorithm, Commands/Search, Selective Search, Directory Search 2401 1.1 christos @subsection How Directory Searches are Performed 2402 1.1 christos @cindex algorithm for directory search 2403 1.1 christos @cindex directory search algorithm 2404 1.1 christos 2405 1.1 christos When a prerequisite is found through directory search, regardless of type 2406 1.1 christos (general or selective), the pathname located may not be the one that 2407 1.1 christos @code{make} actually provides you in the prerequisite list. Sometimes 2408 1.1 christos the path discovered through directory search is thrown away. 2409 1.1 christos 2410 1.1 christos The algorithm @code{make} uses to decide whether to keep or abandon a 2411 1.1 christos path found via directory search is as follows: 2412 1.1 christos 2413 1.1 christos @enumerate 2414 1.1 christos @item 2415 1.1 christos If a target file does not exist at the path specified in the makefile, 2416 1.1 christos directory search is performed. 2417 1.1 christos 2418 1.1 christos @item 2419 1.1 christos If the directory search is successful, that path is kept and this file 2420 1.1 christos is tentatively stored as the target. 2421 1.1 christos 2422 1.1 christos @item 2423 1.1 christos All prerequisites of this target are examined using this same method. 2424 1.1 christos 2425 1.1 christos @item 2426 1.1 christos After processing the prerequisites, the target may or may not need to be 2427 1.1 christos rebuilt: 2428 1.1 christos 2429 1.1 christos @enumerate a 2430 1.1 christos @item 2431 1.1 christos If the target does @emph{not} need to be rebuilt, the path to the file 2432 1.1 christos found during directory search is used for any prerequisite lists which 2433 1.1 christos contain this target. In short, if @code{make} doesn't need to rebuild 2434 1.1 christos the target then you use the path found via directory search. 2435 1.1 christos 2436 1.1 christos @item 2437 1.1 christos If the target @emph{does} need to be rebuilt (is out-of-date), the 2438 1.1 christos pathname found during directory search is @emph{thrown away}, and the 2439 1.1 christos target is rebuilt using the file name specified in the makefile. In 2440 1.1 christos short, if @code{make} must rebuild, then the target is rebuilt locally, 2441 1.1 christos not in the directory found via directory search. 2442 1.1 christos @end enumerate 2443 1.1 christos @end enumerate 2444 1.1 christos 2445 1.1 christos This algorithm may seem complex, but in practice it is quite often 2446 1.1 christos exactly what you want. 2447 1.1 christos 2448 1.1 christos @cindex traditional directory search (GPATH) 2449 1.1 christos @cindex directory search, traditional (GPATH) 2450 1.1 christos Other versions of @code{make} use a simpler algorithm: if the file does 2451 1.1 christos not exist, and it is found via directory search, then that pathname is 2452 1.1 christos always used whether or not the target needs to be built. Thus, if the 2453 1.1 christos target is rebuilt it is created at the pathname discovered during 2454 1.1 christos directory search. 2455 1.1 christos 2456 1.1 christos @vindex GPATH 2457 1.1 christos If, in fact, this is the behavior you want for some or all of your 2458 1.1 christos directories, you can use the @code{GPATH} variable to indicate this to 2459 1.1 christos @code{make}. 2460 1.1 christos 2461 1.1 christos @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a 2462 1.1 christos space- or colon-delimited list of pathnames). If an out-of-date target 2463 1.1 christos is found by directory search in a directory that also appears in 2464 1.1 christos @code{GPATH}, then that pathname is not thrown away. The target is 2465 1.1 christos rebuilt using the expanded path. 2466 1.1 christos 2467 1.1 christos @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search 2468 1.1 christos @subsection Writing Shell Commands with Directory Search 2469 1.1 christos @cindex shell command, and directory search 2470 1.1 christos @cindex directory search (@code{VPATH}), and shell commands 2471 1.1 christos 2472 1.1 christos When a prerequisite is found in another directory through directory search, 2473 1.1 christos this cannot change the commands of the rule; they will execute as written. 2474 1.1 christos Therefore, you must write the commands with care so that they will look for 2475 1.1 christos the prerequisite in the directory where @code{make} finds it. 2476 1.1 christos 2477 1.1 christos This is done with the @dfn{automatic variables} such as @samp{$^} 2478 1.1 christos (@pxref{Automatic Variables}). 2479 1.1 christos For instance, the value of @samp{$^} is a 2480 1.1 christos list of all the prerequisites of the rule, including the names of 2481 1.1 christos the directories in which they were found, and the value of 2482 1.1 christos @samp{$@@} is the target. Thus:@refill 2483 1.1 christos 2484 1.1 christos @example 2485 1.1 christos foo.o : foo.c 2486 1.1 christos cc -c $(CFLAGS) $^ -o $@@ 2487 1.1 christos @end example 2488 1.1 christos 2489 1.1 christos @noindent 2490 1.1 christos (The variable @code{CFLAGS} exists so you can specify flags for C 2491 1.1 christos compilation by implicit rules; we use it here for consistency so it will 2492 1.1 christos affect all C compilations uniformly; 2493 1.1 christos @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.) 2494 1.1 christos 2495 1.1 christos Often the prerequisites include header files as well, which you do not 2496 1.1 christos want to mention in the commands. The automatic variable @samp{$<} is 2497 1.1 christos just the first prerequisite: 2498 1.1 christos 2499 1.1 christos @example 2500 1.1 christos VPATH = src:../headers 2501 1.1 christos foo.o : foo.c defs.h hack.h 2502 1.1 christos cc -c $(CFLAGS) $< -o $@@ 2503 1.1 christos @end example 2504 1.1 christos 2505 1.1 christos @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search 2506 1.1 christos @subsection Directory Search and Implicit Rules 2507 1.1 christos @cindex @code{VPATH}, and implicit rules 2508 1.1 christos @cindex directory search (@code{VPATH}), and implicit rules 2509 1.1 christos @cindex search path for prerequisites (@code{VPATH}), and implicit rules 2510 1.1 christos @cindex implicit rule, and directory search 2511 1.1 christos @cindex implicit rule, and @code{VPATH} 2512 1.1 christos @cindex rule, implicit, and directory search 2513 1.1 christos @cindex rule, implicit, and @code{VPATH} 2514 1.1 christos 2515 1.1 christos The search through the directories specified in @code{VPATH} or with 2516 1.1 christos @code{vpath} also happens during consideration of implicit rules 2517 1.1 christos (@pxref{Implicit Rules, ,Using Implicit Rules}). 2518 1.1 christos 2519 1.1 christos For example, when a file @file{foo.o} has no explicit rule, @code{make} 2520 1.1 christos considers implicit rules, such as the built-in rule to compile 2521 1.1 christos @file{foo.c} if that file exists. If such a file is lacking in the 2522 1.1 christos current directory, the appropriate directories are searched for it. If 2523 1.1 christos @file{foo.c} exists (or is mentioned in the makefile) in any of the 2524 1.1 christos directories, the implicit rule for C compilation is applied. 2525 1.1 christos 2526 1.1 christos The commands of implicit rules normally use automatic variables as a 2527 1.1 christos matter of necessity; consequently they will use the file names found by 2528 1.1 christos directory search with no extra effort. 2529 1.1 christos 2530 1.1 christos @node Libraries/Search, , Implicit/Search, Directory Search 2531 1.1 christos @subsection Directory Search for Link Libraries 2532 1.1 christos @cindex link libraries, and directory search 2533 1.1 christos @cindex libraries for linking, directory search 2534 1.1 christos @cindex directory search (@code{VPATH}), and link libraries 2535 1.1 christos @cindex @code{VPATH}, and link libraries 2536 1.1 christos @cindex search path for prerequisites (@code{VPATH}), and link libraries 2537 1.1 christos @cindex @code{-l} (library search) 2538 1.1 christos @cindex link libraries, patterns matching 2539 1.1 christos @cindex @code{.LIBPATTERNS}, and link libraries 2540 1.1 christos @vindex .LIBPATTERNS 2541 1.1 christos 2542 1.1 christos Directory search applies in a special way to libraries used with the 2543 1.1 christos linker. This special feature comes into play when you write a prerequisite 2544 1.1 christos whose name is of the form @samp{-l@var{name}}. (You can tell something 2545 1.1 christos strange is going on here because the prerequisite is normally the name of a 2546 1.1 christos file, and the @emph{file name} of a library generally looks like 2547 1.1 christos @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill 2548 1.1 christos 2549 1.1 christos When a prerequisite's name has the form @samp{-l@var{name}}, @code{make} 2550 1.1 christos handles it specially by searching for the file @file{lib@var{name}.so} in 2551 1.1 christos the current directory, in directories specified by matching @code{vpath} 2552 1.1 christos search paths and the @code{VPATH} search path, and then in the 2553 1.1 christos directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib} 2554 1.1 christos (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of 2555 1.1 christos @code{make} behave as if @var{prefix} is defined to be the root of the 2556 1.1 christos DJGPP installation tree). 2557 1.1 christos 2558 1.1 christos If that file is not found, then the file @file{lib@var{name}.a} is 2559 1.1 christos searched for, in the same directories as above. 2560 1.1 christos 2561 1.1 christos For example, if there is a @file{/usr/lib/libcurses.a} library on your 2562 1.1 christos system (and no @file{/usr/lib/libcurses.so} file), then 2563 1.1 christos 2564 1.1 christos @example 2565 1.1 christos @group 2566 1.1 christos foo : foo.c -lcurses 2567 1.1 christos cc $^ -o $@@ 2568 1.1 christos @end group 2569 1.1 christos @end example 2570 1.1 christos 2571 1.1 christos @noindent 2572 1.1 christos would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to 2573 1.1 christos be executed when @file{foo} is older than @file{foo.c} or than 2574 1.1 christos @file{/usr/lib/libcurses.a}.@refill 2575 1.1 christos 2576 1.1 christos Although the default set of files to be searched for is 2577 1.1 christos @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable 2578 1.1 christos via the @code{.LIBPATTERNS} variable. Each word in the value of this 2579 1.1 christos variable is a pattern string. When a prerequisite like 2580 1.1 christos @samp{-l@var{name}} is seen, @code{make} will replace the percent in 2581 1.1 christos each pattern in the list with @var{name} and perform the above directory 2582 1.1 christos searches using that library filename. If no library is found, the next 2583 1.1 christos word in the list will be used. 2584 1.1 christos 2585 1.1 christos The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a}, 2586 1.1 christos which provides the default behavior described above. 2587 1.1 christos 2588 1.1 christos You can turn off link library expansion completely by setting this 2589 1.1 christos variable to an empty value. 2590 1.1 christos 2591 1.1 christos @node Phony Targets, Force Targets, Directory Search, Rules 2592 1.1 christos @section Phony Targets 2593 1.1 christos @cindex phony targets 2594 1.1 christos @cindex targets, phony 2595 1.1 christos @cindex targets without a file 2596 1.1 christos 2597 1.1 christos A phony target is one that is not really the name of a file. It is just a 2598 1.1 christos name for some commands to be executed when you make an explicit request. 2599 1.1 christos There are two reasons to use a phony target: to avoid a conflict with 2600 1.1 christos a file of the same name, and to improve performance. 2601 1.1 christos 2602 1.1 christos If you write a rule whose commands will not create the target file, the 2603 1.1 christos commands will be executed every time the target comes up for remaking. 2604 1.1 christos Here is an example: 2605 1.1 christos 2606 1.1 christos @example 2607 1.1 christos @group 2608 1.1 christos clean: 2609 1.1 christos rm *.o temp 2610 1.1 christos @end group 2611 1.1 christos @end example 2612 1.1 christos 2613 1.1 christos @noindent 2614 1.1 christos Because the @code{rm} command does not create a file named @file{clean}, 2615 1.1 christos probably no such file will ever exist. Therefore, the @code{rm} command 2616 1.1 christos will be executed every time you say @samp{make clean}. 2617 1.1 christos @cindex @code{rm} (shell command) 2618 1.1 christos 2619 1.1 christos @findex .PHONY 2620 1.1 christos The phony target will cease to work if anything ever does create a file 2621 1.1 christos named @file{clean} in this directory. Since it has no prerequisites, the 2622 1.1 christos file @file{clean} would inevitably be considered up to date, and its 2623 1.1 christos commands would not be executed. To avoid this problem, you can explicitly 2624 1.1 christos declare the target to be phony, using the special target @code{.PHONY} 2625 1.1 christos (@pxref{Special Targets, ,Special Built-in Target Names}) as follows: 2626 1.1 christos 2627 1.1 christos @example 2628 1.1 christos .PHONY : clean 2629 1.1 christos @end example 2630 1.1 christos 2631 1.1 christos @noindent 2632 1.1 christos Once this is done, @samp{make clean} will run the commands regardless of 2633 1.1 christos whether there is a file named @file{clean}. 2634 1.1 christos 2635 1.1 christos Since it knows that phony targets do not name actual files that could be 2636 1.1 christos remade from other files, @code{make} skips the implicit rule search for 2637 1.1 christos phony targets (@pxref{Implicit Rules}). This is why declaring a target 2638 1.1 christos phony is good for performance, even if you are not worried about the 2639 1.1 christos actual file existing. 2640 1.1 christos 2641 1.1 christos Thus, you first write the line that states that @code{clean} is a 2642 1.1 christos phony target, then you write the rule, like this: 2643 1.1 christos 2644 1.1 christos @example 2645 1.1 christos @group 2646 1.1 christos .PHONY: clean 2647 1.1 christos clean: 2648 1.1 christos rm *.o temp 2649 1.1 christos @end group 2650 1.1 christos @end example 2651 1.1 christos 2652 1.1 christos Another example of the usefulness of phony targets is in conjunction 2653 1.1 christos with recursive invocations of @code{make} (for more information, see 2654 1.1 christos @ref{Recursion, ,Recursive Use of @code{make}}). In this case the 2655 1.1 christos makefile will often contain a variable which lists a number of 2656 1.1 christos subdirectories to be built. One way to handle this is with one rule 2657 1.1 christos whose command is a shell loop over the subdirectories, like this: 2658 1.1 christos 2659 1.1 christos @example 2660 1.1 christos @group 2661 1.1 christos SUBDIRS = foo bar baz 2662 1.1 christos 2663 1.1 christos subdirs: 2664 1.1 christos for dir in $(SUBDIRS); do \ 2665 1.1 christos $(MAKE) -C $$dir; \ 2666 1.1 christos done 2667 1.1 christos @end group 2668 1.1 christos @end example 2669 1.1 christos 2670 1.1 christos There are a few problems with this method, however. First, any error 2671 1.1 christos detected in a submake is not noted by this rule, so it will continue to 2672 1.1 christos build the rest of the directories even when one fails. This can be 2673 1.1 christos overcome by adding shell commands to note the error and exit, but then 2674 1.1 christos it will do so even if @code{make} is invoked with the @code{-k} option, 2675 1.1 christos which is unfortunate. Second, and perhaps more importantly, you cannot 2676 1.1 christos take advantage of @code{make}'s ability to build targets in parallel 2677 1.1 christos (@pxref{Parallel, ,Parallel Execution}), since there is only one rule. 2678 1.1 christos 2679 1.1 christos By declaring the subdirectories as phony targets (you must do this as 2680 1.1 christos the subdirectory obviously always exists; otherwise it won't be built) 2681 1.1 christos you can remove these problems: 2682 1.1 christos 2683 1.1 christos @example 2684 1.1 christos @group 2685 1.1 christos SUBDIRS = foo bar baz 2686 1.1 christos 2687 1.1 christos .PHONY: subdirs $(SUBDIRS) 2688 1.1 christos 2689 1.1 christos subdirs: $(SUBDIRS) 2690 1.1 christos 2691 1.1 christos $(SUBDIRS): 2692 1.1 christos $(MAKE) -C $@@ 2693 1.1 christos 2694 1.1 christos foo: baz 2695 1.1 christos @end group 2696 1.1 christos @end example 2697 1.1 christos 2698 1.1 christos Here we've also declared that the @file{foo} subdirectory cannot be 2699 1.1 christos built until after the @file{baz} subdirectory is complete; this kind of 2700 1.1 christos relationship declaration is particularly important when attempting 2701 1.1 christos parallel builds. 2702 1.1 christos 2703 1.1 christos A phony target should not be a prerequisite of a real target file; if it 2704 1.1 christos is, its commands are run every time @code{make} goes to update that 2705 1.1 christos file. As long as a phony target is never a prerequisite of a real 2706 1.1 christos target, the phony target commands will be executed only when the phony 2707 1.1 christos target is a specified goal (@pxref{Goals, ,Arguments to Specify the 2708 1.1 christos Goals}). 2709 1.1 christos 2710 1.1 christos Phony targets can have prerequisites. When one directory contains multiple 2711 1.1 christos programs, it is most convenient to describe all of the programs in one 2712 1.1 christos makefile @file{./Makefile}. Since the target remade by default will be the 2713 1.1 christos first one in the makefile, it is common to make this a phony target named 2714 1.1 christos @samp{all} and give it, as prerequisites, all the individual programs. For 2715 1.1 christos example: 2716 1.1 christos 2717 1.1 christos @example 2718 1.1 christos all : prog1 prog2 prog3 2719 1.1 christos .PHONY : all 2720 1.1 christos 2721 1.1 christos prog1 : prog1.o utils.o 2722 1.1 christos cc -o prog1 prog1.o utils.o 2723 1.1 christos 2724 1.1 christos prog2 : prog2.o 2725 1.1 christos cc -o prog2 prog2.o 2726 1.1 christos 2727 1.1 christos prog3 : prog3.o sort.o utils.o 2728 1.1 christos cc -o prog3 prog3.o sort.o utils.o 2729 1.1 christos @end example 2730 1.1 christos 2731 1.1 christos @noindent 2732 1.1 christos Now you can say just @samp{make} to remake all three programs, or 2733 1.1 christos specify as arguments the ones to remake (as in @samp{make prog1 2734 1.1 christos prog3}). Phoniness is not inherited: the prerequisites of a phony 2735 1.1 christos target are not themselves phony, unless explicitly declared to be so. 2736 1.1 christos 2737 1.1 christos When one phony target is a prerequisite of another, it serves as a subroutine 2738 1.1 christos of the other. For example, here @samp{make cleanall} will delete the 2739 1.1 christos object files, the difference files, and the file @file{program}: 2740 1.1 christos 2741 1.1 christos @example 2742 1.1 christos .PHONY: cleanall cleanobj cleandiff 2743 1.1 christos 2744 1.1 christos cleanall : cleanobj cleandiff 2745 1.1 christos rm program 2746 1.1 christos 2747 1.1 christos cleanobj : 2748 1.1 christos rm *.o 2749 1.1 christos 2750 1.1 christos cleandiff : 2751 1.1 christos rm *.diff 2752 1.1 christos @end example 2753 1.1 christos 2754 1.1 christos @node Force Targets, Empty Targets, Phony Targets, Rules 2755 1.1 christos @section Rules without Commands or Prerequisites 2756 1.1 christos @cindex force targets 2757 1.1 christos @cindex targets, force 2758 1.1 christos @cindex @code{FORCE} 2759 1.1 christos @cindex rule, no commands or prerequisites 2760 1.1 christos 2761 1.1 christos If a rule has no prerequisites or commands, and the target of the rule 2762 1.1 christos is a nonexistent file, then @code{make} imagines this target to have 2763 1.1 christos been updated whenever its rule is run. This implies that all targets 2764 1.1 christos depending on this one will always have their commands run. 2765 1.1 christos 2766 1.1 christos An example will illustrate this: 2767 1.1 christos 2768 1.1 christos @example 2769 1.1 christos @group 2770 1.1 christos clean: FORCE 2771 1.1 christos rm $(objects) 2772 1.1 christos FORCE: 2773 1.1 christos @end group 2774 1.1 christos @end example 2775 1.1 christos 2776 1.1 christos Here the target @samp{FORCE} satisfies the special conditions, so the 2777 1.1 christos target @file{clean} that depends on it is forced to run its commands. 2778 1.1 christos There is nothing special about the name @samp{FORCE}, but that is one name 2779 1.1 christos commonly used this way. 2780 1.1 christos 2781 1.1 christos As you can see, using @samp{FORCE} this way has the same results as using 2782 1.1 christos @samp{.PHONY: clean}. 2783 1.1 christos 2784 1.1 christos Using @samp{.PHONY} is more explicit and more efficient. However, 2785 1.1 christos other versions of @code{make} do not support @samp{.PHONY}; thus 2786 1.1 christos @samp{FORCE} appears in many makefiles. @xref{Phony Targets}. 2787 1.1 christos 2788 1.1 christos @node Empty Targets, Special Targets, Force Targets, Rules 2789 1.1 christos @section Empty Target Files to Record Events 2790 1.1 christos @cindex empty targets 2791 1.1 christos @cindex targets, empty 2792 1.1 christos @cindex recording events with empty targets 2793 1.1 christos 2794 1.1 christos The @dfn{empty target} is a variant of the phony target; it is used to hold 2795 1.1 christos commands for an action that you request explicitly from time to time. 2796 1.1 christos Unlike a phony target, this target file can really exist; but the file's 2797 1.1 christos contents do not matter, and usually are empty. 2798 1.1 christos 2799 1.1 christos The purpose of the empty target file is to record, with its 2800 1.1 christos last-modification time, when the rule's commands were last executed. It 2801 1.1 christos does so because one of the commands is a @code{touch} command to update the 2802 1.1 christos target file. 2803 1.1 christos 2804 1.1 christos The empty target file should have some prerequisites (otherwise it 2805 1.1 christos doesn't make sense). When you ask to remake the empty target, the 2806 1.1 christos commands are executed if any prerequisite is more recent than the target; 2807 1.1 christos in other words, if a prerequisite has changed since the last time you 2808 1.1 christos remade the target. Here is an example: 2809 1.1 christos 2810 1.1 christos @example 2811 1.1 christos print: foo.c bar.c 2812 1.1 christos lpr -p $? 2813 1.1 christos touch print 2814 1.1 christos @end example 2815 1.1 christos @cindex @code{print} target 2816 1.1 christos @cindex @code{lpr} (shell command) 2817 1.1 christos @cindex @code{touch} (shell command) 2818 1.1 christos 2819 1.1 christos @noindent 2820 1.1 christos With this rule, @samp{make print} will execute the @code{lpr} command if 2821 1.1 christos either source file has changed since the last @samp{make print}. The 2822 1.1 christos automatic variable @samp{$?} is used to print only those files that have 2823 1.1 christos changed (@pxref{Automatic Variables}). 2824 1.1 christos 2825 1.1 christos @node Special Targets, Multiple Targets, Empty Targets, Rules 2826 1.1 christos @section Special Built-in Target Names 2827 1.1 christos @cindex special targets 2828 1.1 christos @cindex built-in special targets 2829 1.1 christos @cindex targets, built-in special 2830 1.1 christos 2831 1.1 christos Certain names have special meanings if they appear as targets. 2832 1.1 christos 2833 1.1 christos @table @code 2834 1.1 christos @findex .PHONY 2835 1.1 christos @item .PHONY 2836 1.1 christos 2837 1.1 christos The prerequisites of the special target @code{.PHONY} are considered to 2838 1.1 christos be phony targets. When it is time to consider such a target, 2839 1.1 christos @code{make} will run its commands unconditionally, regardless of 2840 1.1 christos whether a file with that name exists or what its last-modification 2841 1.1 christos time is. @xref{Phony Targets, ,Phony Targets}. 2842 1.1 christos 2843 1.1 christos @findex .SUFFIXES 2844 1.1 christos @item .SUFFIXES 2845 1.1 christos 2846 1.1 christos The prerequisites of the special target @code{.SUFFIXES} are the list 2847 1.1 christos of suffixes to be used in checking for suffix rules. 2848 1.1 christos @xref{Suffix Rules, , Old-Fashioned Suffix Rules}. 2849 1.1 christos 2850 1.1 christos @findex .DEFAULT 2851 1.1 christos @item .DEFAULT 2852 1.1 christos 2853 1.1 christos The commands specified for @code{.DEFAULT} are used for any target for 2854 1.1 christos which no rules are found (either explicit rules or implicit rules). 2855 1.1 christos @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every 2856 1.1 christos file mentioned as a prerequisite, but not as a target in a rule, will have 2857 1.1 christos these commands executed on its behalf. @xref{Implicit Rule Search, 2858 1.1 christos ,Implicit Rule Search Algorithm}. 2859 1.1 christos 2860 1.1 christos @findex .PRECIOUS 2861 1.1 christos @item .PRECIOUS 2862 1.1 christos @cindex precious targets 2863 1.1 christos @cindex preserving with @code{.PRECIOUS} 2864 1.1 christos 2865 1.1 christos The targets which @code{.PRECIOUS} depends on are given the following 2866 1.1 christos special treatment: if @code{make} is killed or interrupted during the 2867 1.1 christos execution of their commands, the target is not deleted. 2868 1.1 christos @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the 2869 1.1 christos target is an intermediate file, it will not be deleted after it is no 2870 1.1 christos longer needed, as is normally done. @xref{Chained Rules, ,Chains of 2871 1.1 christos Implicit Rules}. In this latter respect it overlaps with the 2872 1.1 christos @code{.SECONDARY} special target. 2873 1.1 christos 2874 1.1 christos You can also list the target pattern of an implicit rule (such as 2875 1.1 christos @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS} 2876 1.1 christos to preserve intermediate files created by rules whose target patterns 2877 1.1 christos match that file's name. 2878 1.1 christos 2879 1.1 christos @findex .INTERMEDIATE 2880 1.1 christos @item .INTERMEDIATE 2881 1.1 christos @cindex intermediate targets, explicit 2882 1.1 christos 2883 1.1 christos The targets which @code{.INTERMEDIATE} depends on are treated as 2884 1.1 christos intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}. 2885 1.1 christos @code{.INTERMEDIATE} with no prerequisites has no effect. 2886 1.1 christos 2887 1.1 christos @findex .SECONDARY 2888 1.1 christos @item .SECONDARY 2889 1.1 christos @cindex secondary targets 2890 1.1 christos @cindex preserving with @code{.SECONDARY} 2891 1.1 christos 2892 1.1 christos The targets which @code{.SECONDARY} depends on are treated as 2893 1.1 christos intermediate files, except that they are never automatically deleted. 2894 1.1 christos @xref{Chained Rules, ,Chains of Implicit Rules}. 2895 1.1 christos 2896 1.1 christos @code{.SECONDARY} with no prerequisites causes all targets to be treated 2897 1.1 christos as secondary (i.e., no target is removed because it is considered 2898 1.1 christos intermediate). 2899 1.1 christos 2900 1.1 christos @findex .SECONDEXPANSION 2901 1.1 christos @item .SECONDEXPANSION 2902 1.1 christos 2903 1.1 christos If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the 2904 1.1 christos makefile, then all prerequisite lists defined @emph{after} it appears 2905 1.1 christos will be expanded a second time after all makefiles have been read in. 2906 1.1 christos @xref{Secondary Expansion, ,Secondary Expansion}. 2907 1.1 christos 2908 1.1 christos The prerequisites of the special target @code{.SUFFIXES} are the list 2909 1.1 christos of suffixes to be used in checking for suffix rules. 2910 1.1 christos @xref{Suffix Rules, , Old-Fashioned Suffix Rules}. 2911 1.1 christos 2912 1.1 christos @findex .DELETE_ON_ERROR 2913 1.1 christos @item .DELETE_ON_ERROR 2914 1.1 christos @cindex removing targets on failure 2915 1.1 christos 2916 1.1 christos If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the 2917 1.1 christos makefile, then @code{make} will delete the target of a rule if it has 2918 1.1 christos changed and its commands exit with a nonzero exit status, just as it 2919 1.1 christos does when it receives a signal. @xref{Errors, ,Errors in Commands}. 2920 1.1 christos 2921 1.1 christos @findex .IGNORE 2922 1.1 christos @item .IGNORE 2923 1.1 christos 2924 1.1 christos If you specify prerequisites for @code{.IGNORE}, then @code{make} will 2925 1.1 christos ignore errors in execution of the commands run for those particular 2926 1.1 christos files. The commands for @code{.IGNORE} are not meaningful. 2927 1.1 christos 2928 1.1 christos If mentioned as a target with no prerequisites, @code{.IGNORE} says to 2929 1.1 christos ignore errors in execution of commands for all files. This usage of 2930 1.1 christos @samp{.IGNORE} is supported only for historical compatibility. Since 2931 1.1 christos this affects every command in the makefile, it is not very useful; we 2932 1.1 christos recommend you use the more selective ways to ignore errors in specific 2933 1.1 christos commands. @xref{Errors, ,Errors in Commands}. 2934 1.1 christos 2935 1.1 christos @findex .LOW_RESOLUTION_TIME 2936 1.1 christos @item .LOW_RESOLUTION_TIME 2937 1.1 christos 2938 1.1 christos If you specify prerequisites for @code{.LOW_RESOLUTION_TIME}, 2939 1.1 christos @command{make} assumes that these files are created by commands that 2940 1.1 christos generate low resolution time stamps. The commands for 2941 1.1 christos @code{.LOW_RESOLUTION_TIME} are not meaningful. 2942 1.1 christos 2943 1.1 christos The high resolution file time stamps of many modern hosts lessen the 2944 1.1 christos chance of @command{make} incorrectly concluding that a file is up to 2945 1.1 christos date. Unfortunately, these hosts provide no way to set a high 2946 1.1 christos resolution file time stamp, so commands like @samp{cp -p} that 2947 1.1 christos explicitly set a file's time stamp must discard its subsecond part. If 2948 1.1 christos a file is created by such a command, you should list it as a 2949 1.1 christos prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does 2950 1.1 christos not mistakenly conclude that the file is out of date. For example: 2951 1.1 christos 2952 1.1 christos @example 2953 1.1 christos @group 2954 1.1 christos .LOW_RESOLUTION_TIME: dst 2955 1.1 christos dst: src 2956 1.1 christos cp -p src dst 2957 1.1 christos @end group 2958 1.1 christos @end example 2959 1.1 christos 2960 1.1 christos Since @samp{cp -p} discards the subsecond part of @file{src}'s time 2961 1.1 christos stamp, @file{dst} is typically slightly older than @file{src} even when 2962 1.1 christos it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes 2963 1.1 christos @command{make} to consider @file{dst} to be up to date if its time stamp 2964 1.1 christos is at the start of the same second that @file{src}'s time stamp is in. 2965 1.1 christos 2966 1.1 christos Due to a limitation of the archive format, archive member time stamps 2967 1.1 christos are always low resolution. You need not list archive members as 2968 1.1 christos prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this 2969 1.1 christos automatically. 2970 1.1 christos 2971 1.1 christos @findex .SILENT 2972 1.1 christos @item .SILENT 2973 1.1 christos 2974 1.1 christos If you specify prerequisites for @code{.SILENT}, then @code{make} will 2975 1.1 christos not print the commands to remake those particular files before executing 2976 1.1 christos them. The commands for @code{.SILENT} are not meaningful. 2977 1.1 christos 2978 1.1 christos If mentioned as a target with no prerequisites, @code{.SILENT} says not 2979 1.1 christos to print any commands before executing them. This usage of 2980 1.1 christos @samp{.SILENT} is supported only for historical compatibility. We 2981 1.1 christos recommend you use the more selective ways to silence specific commands. 2982 1.1 christos @xref{Echoing, ,Command Echoing}. If you want to silence all commands 2983 1.1 christos for a particular run of @code{make}, use the @samp{-s} or 2984 1.1 christos @w{@samp{--silent}} option (@pxref{Options Summary}). 2985 1.1 christos 2986 1.1 christos @findex .EXPORT_ALL_VARIABLES 2987 1.1 christos @item .EXPORT_ALL_VARIABLES 2988 1.1 christos 2989 1.1 christos Simply by being mentioned as a target, this tells @code{make} to 2990 1.1 christos export all variables to child processes by default. 2991 1.1 christos @xref{Variables/Recursion, ,Communicating Variables to a 2992 1.1 christos Sub-@code{make}}. 2993 1.1 christos 2994 1.1 christos @findex .NOTPARALLEL 2995 1.1 christos @item .NOTPARALLEL 2996 1.1 christos @cindex parallel execution, overriding 2997 1.1 christos 2998 1.1 christos If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of 2999 1.1 christos @code{make} will be run serially, even if the @samp{-j} option is 3000 1.1 christos given. Any recursively invoked @code{make} command will still be run in 3001 1.1 christos parallel (unless its makefile contains this target). Any prerequisites 3002 1.1 christos on this target are ignored. 3003 1.1 christos @end table 3004 1.1 christos 3005 1.1 christos Any defined implicit rule suffix also counts as a special target if it 3006 1.1 christos appears as a target, and so does the concatenation of two suffixes, such 3007 1.1 christos as @samp{.c.o}. These targets are suffix rules, an obsolete way of 3008 1.1 christos defining implicit rules (but a way still widely used). In principle, any 3009 1.1 christos target name could be special in this way if you break it in two and add 3010 1.1 christos both pieces to the suffix list. In practice, suffixes normally begin with 3011 1.1 christos @samp{.}, so these special target names also begin with @samp{.}. 3012 1.1 christos @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}. 3013 1.1 christos 3014 1.1 christos @node Multiple Targets, Multiple Rules, Special Targets, Rules 3015 1.1 christos @section Multiple Targets in a Rule 3016 1.1 christos @cindex multiple targets 3017 1.1 christos @cindex several targets in a rule 3018 1.1 christos @cindex targets, multiple 3019 1.1 christos @cindex rule, with multiple targets 3020 1.1 christos 3021 1.1 christos A rule with multiple targets is equivalent to writing many rules, each with 3022 1.1 christos one target, and all identical aside from that. The same commands apply to 3023 1.1 christos all the targets, but their effects may vary because you can substitute the 3024 1.1 christos actual target name into the command using @samp{$@@}. The rule contributes 3025 1.1 christos the same prerequisites to all the targets also. 3026 1.1 christos 3027 1.1 christos This is useful in two cases. 3028 1.1 christos 3029 1.1 christos @itemize @bullet 3030 1.1 christos @item 3031 1.1 christos You want just prerequisites, no commands. For example: 3032 1.1 christos 3033 1.1 christos @example 3034 1.1 christos kbd.o command.o files.o: command.h 3035 1.1 christos @end example 3036 1.1 christos 3037 1.1 christos @noindent 3038 1.1 christos gives an additional prerequisite to each of the three object files 3039 1.1 christos mentioned. 3040 1.1 christos 3041 1.1 christos @item 3042 1.1 christos Similar commands work for all the targets. The commands do not need 3043 1.1 christos to be absolutely identical, since the automatic variable @samp{$@@} 3044 1.1 christos can be used to substitute the particular target to be remade into the 3045 1.1 christos commands (@pxref{Automatic Variables}). For example: 3046 1.1 christos 3047 1.1 christos @example 3048 1.1 christos @group 3049 1.1 christos bigoutput littleoutput : text.g 3050 1.1 christos generate text.g -$(subst output,,$@@) > $@@ 3051 1.1 christos @end group 3052 1.1 christos @end example 3053 1.1 christos @findex subst 3054 1.1 christos 3055 1.1 christos @noindent 3056 1.1 christos is equivalent to 3057 1.1 christos 3058 1.1 christos @example 3059 1.1 christos bigoutput : text.g 3060 1.1 christos generate text.g -big > bigoutput 3061 1.1 christos littleoutput : text.g 3062 1.1 christos generate text.g -little > littleoutput 3063 1.1 christos @end example 3064 1.1 christos 3065 1.1 christos @noindent 3066 1.1 christos Here we assume the hypothetical program @code{generate} makes two 3067 1.1 christos types of output, one if given @samp{-big} and one if given 3068 1.1 christos @samp{-little}. 3069 1.1 christos @xref{Text Functions, ,Functions for String Substitution and Analysis}, 3070 1.1 christos for an explanation of the @code{subst} function. 3071 1.1 christos @end itemize 3072 1.1 christos 3073 1.1 christos Suppose you would like to vary the prerequisites according to the target, 3074 1.1 christos much as the variable @samp{$@@} allows you to vary the commands. 3075 1.1 christos You cannot do this with multiple targets in an ordinary rule, but you can 3076 1.1 christos do it with a @dfn{static pattern rule}. 3077 1.1 christos @xref{Static Pattern, ,Static Pattern Rules}. 3078 1.1 christos 3079 1.1 christos @node Multiple Rules, Static Pattern, Multiple Targets, Rules 3080 1.1 christos @section Multiple Rules for One Target 3081 1.1 christos @cindex multiple rules for one target 3082 1.1 christos @cindex several rules for one target 3083 1.1 christos @cindex rule, multiple for one target 3084 1.1 christos @cindex target, multiple rules for one 3085 1.1 christos 3086 1.1 christos One file can be the target of several rules. All the prerequisites 3087 1.1 christos mentioned in all the rules are merged into one list of prerequisites for 3088 1.1 christos the target. If the target is older than any prerequisite from any rule, 3089 1.1 christos the commands are executed. 3090 1.1 christos 3091 1.1 christos There can only be one set of commands to be executed for a file. If 3092 1.1 christos more than one rule gives commands for the same file, @code{make} uses 3093 1.1 christos the last set given and prints an error message. (As a special case, 3094 1.1 christos if the file's name begins with a dot, no error message is printed. 3095 1.1 christos This odd behavior is only for compatibility with other implementations 3096 1.1 christos of @code{make}... you should avoid using it). Occasionally it is 3097 1.1 christos useful to have the same target invoke multiple commands which are 3098 1.1 christos defined in different parts of your makefile; you can use 3099 1.1 christos @dfn{double-colon rules} (@pxref{Double-Colon}) for this. 3100 1.1 christos 3101 1.1 christos An extra rule with just prerequisites can be used to give a few extra 3102 1.1 christos prerequisites to many files at once. For example, makefiles often 3103 1.1 christos have a variable, such as @code{objects}, containing a list of all the 3104 1.1 christos compiler output files in the system being made. An easy way to say 3105 1.1 christos that all of them must be recompiled if @file{config.h} changes is to 3106 1.1 christos write the following: 3107 1.1 christos 3108 1.1 christos @example 3109 1.1 christos objects = foo.o bar.o 3110 1.1 christos foo.o : defs.h 3111 1.1 christos bar.o : defs.h test.h 3112 1.1 christos $(objects) : config.h 3113 1.1 christos @end example 3114 1.1 christos 3115 1.1 christos This could be inserted or taken out without changing the rules that really 3116 1.1 christos specify how to make the object files, making it a convenient form to use if 3117 1.1 christos you wish to add the additional prerequisite intermittently. 3118 1.1 christos 3119 1.1 christos Another wrinkle is that the additional prerequisites could be specified with 3120 1.1 christos a variable that you set with a command argument to @code{make} 3121 1.1 christos (@pxref{Overriding, ,Overriding Variables}). For example, 3122 1.1 christos 3123 1.1 christos @example 3124 1.1 christos @group 3125 1.1 christos extradeps= 3126 1.1 christos $(objects) : $(extradeps) 3127 1.1 christos @end group 3128 1.1 christos @end example 3129 1.1 christos 3130 1.1 christos @noindent 3131 1.1 christos means that the command @samp{make extradeps=foo.h} will consider 3132 1.1 christos @file{foo.h} as a prerequisite of each object file, but plain @samp{make} 3133 1.1 christos will not. 3134 1.1 christos 3135 1.1 christos If none of the explicit rules for a target has commands, then @code{make} 3136 1.1 christos searches for an applicable implicit rule to find some commands 3137 1.1 christos @pxref{Implicit Rules, ,Using Implicit Rules}). 3138 1.1 christos 3139 1.1 christos @node Static Pattern, Double-Colon, Multiple Rules, Rules 3140 1.1 christos @section Static Pattern Rules 3141 1.1 christos @cindex static pattern rule 3142 1.1 christos @cindex rule, static pattern 3143 1.1 christos @cindex pattern rules, static (not implicit) 3144 1.1 christos @cindex varying prerequisites 3145 1.1 christos @cindex prerequisites, varying (static pattern) 3146 1.1 christos 3147 1.1 christos @dfn{Static pattern rules} are rules which specify multiple targets and 3148 1.1 christos construct the prerequisite names for each target based on the target name. 3149 1.1 christos They are more general than ordinary rules with multiple targets because the 3150 1.1 christos targets do not have to have identical prerequisites. Their prerequisites must 3151 1.1 christos be @emph{analogous}, but not necessarily @emph{identical}. 3152 1.1 christos 3153 1.1 christos @menu 3154 1.1 christos * Static Usage:: The syntax of static pattern rules. 3155 1.1 christos * Static versus Implicit:: When are they better than implicit rules? 3156 1.1 christos @end menu 3157 1.1 christos 3158 1.1 christos @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern 3159 1.1 christos @subsection Syntax of Static Pattern Rules 3160 1.1 christos @cindex static pattern rule, syntax of 3161 1.1 christos @cindex pattern rules, static, syntax of 3162 1.1 christos 3163 1.1 christos Here is the syntax of a static pattern rule: 3164 1.1 christos 3165 1.1 christos @example 3166 1.1 christos @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{} 3167 1.1 christos @var{commands} 3168 1.1 christos @dots{} 3169 1.1 christos @end example 3170 1.1 christos 3171 1.1 christos @noindent 3172 1.1 christos The @var{targets} list specifies the targets that the rule applies to. 3173 1.1 christos The targets can contain wildcard characters, just like the targets of 3174 1.1 christos ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File 3175 1.1 christos Names}). 3176 1.1 christos 3177 1.1 christos @cindex target pattern, static (not implicit) 3178 1.1 christos @cindex stem 3179 1.1 christos The @var{target-pattern} and @var{prereq-patterns} say how to compute the 3180 1.1 christos prerequisites of each target. Each target is matched against the 3181 1.1 christos @var{target-pattern} to extract a part of the target name, called the 3182 1.1 christos @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns} 3183 1.1 christos to make the prerequisite names (one from each @var{prereq-pattern}). 3184 1.1 christos 3185 1.1 christos Each pattern normally contains the character @samp{%} just once. When the 3186 1.1 christos @var{target-pattern} matches a target, the @samp{%} can match any part of 3187 1.1 christos the target name; this part is called the @dfn{stem}. The rest of the 3188 1.1 christos pattern must match exactly. For example, the target @file{foo.o} matches 3189 1.1 christos the pattern @samp{%.o}, with @samp{foo} as the stem. The targets 3190 1.1 christos @file{foo.c} and @file{foo.out} do not match that pattern.@refill 3191 1.1 christos 3192 1.1 christos @cindex prerequisite pattern, static (not implicit) 3193 1.1 christos The prerequisite names for each target are made by substituting the stem 3194 1.1 christos for the @samp{%} in each prerequisite pattern. For example, if one 3195 1.1 christos prerequisite pattern is @file{%.c}, then substitution of the stem 3196 1.1 christos @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate 3197 1.1 christos to write a prerequisite pattern that does not contain @samp{%}; then this 3198 1.1 christos prerequisite is the same for all targets. 3199 1.1 christos 3200 1.1 christos @cindex @code{%}, quoting in static pattern 3201 1.1 christos @cindex @code{%}, quoting with @code{\} (backslash) 3202 1.1 christos @cindex @code{\} (backslash), to quote @code{%} 3203 1.1 christos @cindex backslash (@code{\}), to quote @code{%} 3204 1.1 christos @cindex quoting @code{%}, in static pattern 3205 1.1 christos @samp{%} characters in pattern rules can be quoted with preceding 3206 1.1 christos backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%} 3207 1.1 christos characters can be quoted with more backslashes. Backslashes that quote 3208 1.1 christos @samp{%} characters or other backslashes are removed from the pattern 3209 1.1 christos before it is compared to file names or has a stem substituted into it. 3210 1.1 christos Backslashes that are not in danger of quoting @samp{%} characters go 3211 1.1 christos unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has 3212 1.1 christos @samp{the%weird\} preceding the operative @samp{%} character, and 3213 1.1 christos @samp{pattern\\} following it. The final two backslashes are left alone 3214 1.1 christos because they cannot affect any @samp{%} character.@refill 3215 1.1 christos 3216 1.1 christos Here is an example, which compiles each of @file{foo.o} and @file{bar.o} 3217 1.1 christos from the corresponding @file{.c} file: 3218 1.1 christos 3219 1.1 christos @example 3220 1.1 christos @group 3221 1.1 christos objects = foo.o bar.o 3222 1.1 christos 3223 1.1 christos all: $(objects) 3224 1.1 christos 3225 1.1 christos $(objects): %.o: %.c 3226 1.1 christos $(CC) -c $(CFLAGS) $< -o $@@ 3227 1.1 christos @end group 3228 1.1 christos @end example 3229 1.1 christos 3230 1.1 christos @noindent 3231 1.1 christos Here @samp{$<} is the automatic variable that holds the name of the 3232 1.1 christos prerequisite and @samp{$@@} is the automatic variable that holds the name 3233 1.1 christos of the target; see @ref{Automatic Variables}. 3234 1.1 christos 3235 1.1 christos Each target specified must match the target pattern; a warning is issued 3236 1.1 christos for each target that does not. If you have a list of files, only some of 3237 1.1 christos which will match the pattern, you can use the @code{filter} function to 3238 1.1 christos remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}): 3239 1.1 christos 3240 1.1 christos @example 3241 1.1 christos files = foo.elc bar.o lose.o 3242 1.1 christos 3243 1.1 christos $(filter %.o,$(files)): %.o: %.c 3244 1.1 christos $(CC) -c $(CFLAGS) $< -o $@@ 3245 1.1 christos $(filter %.elc,$(files)): %.elc: %.el 3246 1.1 christos emacs -f batch-byte-compile $< 3247 1.1 christos @end example 3248 1.1 christos 3249 1.1 christos @noindent 3250 1.1 christos In this example the result of @samp{$(filter %.o,$(files))} is 3251 1.1 christos @file{bar.o lose.o}, and the first static pattern rule causes each of 3252 1.1 christos these object files to be updated by compiling the corresponding C source 3253 1.1 christos file. The result of @w{@samp{$(filter %.elc,$(files))}} is 3254 1.1 christos @file{foo.elc}, so that file is made from @file{foo.el}.@refill 3255 1.1 christos 3256 1.1 christos Another example shows how to use @code{$*} in static pattern rules: 3257 1.1 christos @vindex $*@r{, and static pattern} 3258 1.1 christos 3259 1.1 christos @example 3260 1.1 christos @group 3261 1.1 christos bigoutput littleoutput : %output : text.g 3262 1.1 christos generate text.g -$* > $@@ 3263 1.1 christos @end group 3264 1.1 christos @end example 3265 1.1 christos 3266 1.1 christos @noindent 3267 1.1 christos When the @code{generate} command is run, @code{$*} will expand to the 3268 1.1 christos stem, either @samp{big} or @samp{little}. 3269 1.1 christos 3270 1.1 christos @node Static versus Implicit, , Static Usage, Static Pattern 3271 1.1 christos @subsection Static Pattern Rules versus Implicit Rules 3272 1.1 christos @cindex rule, static pattern versus implicit 3273 1.1 christos @cindex static pattern rule, versus implicit 3274 1.1 christos 3275 1.1 christos A static pattern rule has much in common with an implicit rule defined as a 3276 1.1 christos pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}). 3277 1.1 christos Both have a pattern for the target and patterns for constructing the 3278 1.1 christos names of prerequisites. The difference is in how @code{make} decides 3279 1.1 christos @emph{when} the rule applies. 3280 1.1 christos 3281 1.1 christos An implicit rule @emph{can} apply to any target that matches its pattern, 3282 1.1 christos but it @emph{does} apply only when the target has no commands otherwise 3283 1.1 christos specified, and only when the prerequisites can be found. If more than one 3284 1.1 christos implicit rule appears applicable, only one applies; the choice depends on 3285 1.1 christos the order of rules. 3286 1.1 christos 3287 1.1 christos By contrast, a static pattern rule applies to the precise list of targets 3288 1.1 christos that you specify in the rule. It cannot apply to any other target and it 3289 1.1 christos invariably does apply to each of the targets specified. If two conflicting 3290 1.1 christos rules apply, and both have commands, that's an error. 3291 1.1 christos 3292 1.1 christos The static pattern rule can be better than an implicit rule for these 3293 1.1 christos reasons: 3294 1.1 christos 3295 1.1 christos @itemize @bullet 3296 1.1 christos @item 3297 1.1 christos You may wish to override the usual implicit rule for a few 3298 1.1 christos files whose names cannot be categorized syntactically but 3299 1.1 christos can be given in an explicit list. 3300 1.1 christos 3301 1.1 christos @item 3302 1.1 christos If you cannot be sure of the precise contents of the directories 3303 1.1 christos you are using, you may not be sure which other irrelevant files 3304 1.1 christos might lead @code{make} to use the wrong implicit rule. The choice 3305 1.1 christos might depend on the order in which the implicit rule search is done. 3306 1.1 christos With static pattern rules, there is no uncertainty: each rule applies 3307 1.1 christos to precisely the targets specified. 3308 1.1 christos @end itemize 3309 1.1 christos 3310 1.1 christos @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules 3311 1.1 christos @section Double-Colon Rules 3312 1.1 christos @cindex double-colon rules 3313 1.1 christos @cindex rule, double-colon (@code{::}) 3314 1.1 christos @cindex multiple rules for one target (@code{::}) 3315 1.1 christos @cindex @code{::} rules (double-colon) 3316 1.1 christos 3317 1.1 christos @dfn{Double-colon} rules are rules written with @samp{::} instead of 3318 1.1 christos @samp{:} after the target names. They are handled differently from 3319 1.1 christos ordinary rules when the same target appears in more than one rule. 3320 1.1 christos 3321 1.1 christos When a target appears in multiple rules, all the rules must be the same 3322 1.1 christos type: all ordinary, or all double-colon. If they are double-colon, each 3323 1.1 christos of them is independent of the others. Each double-colon rule's commands 3324 1.1 christos are executed if the target is older than any prerequisites of that rule. 3325 1.1 christos If there are no prerequisites for that rule, its commands are always 3326 1.1 christos executed (even if the target already exists). This can result in 3327 1.1 christos executing none, any, or all of the double-colon rules. 3328 1.1 christos 3329 1.1 christos Double-colon rules with the same target are in fact completely separate 3330 1.1 christos from one another. Each double-colon rule is processed individually, just 3331 1.1 christos as rules with different targets are processed. 3332 1.1 christos 3333 1.1 christos The double-colon rules for a target are executed in the order they appear 3334 1.1 christos in the makefile. However, the cases where double-colon rules really make 3335 1.1 christos sense are those where the order of executing the commands would not matter. 3336 1.1 christos 3337 1.1 christos Double-colon rules are somewhat obscure and not often very useful; they 3338 1.1 christos provide a mechanism for cases in which the method used to update a target 3339 1.1 christos differs depending on which prerequisite files caused the update, and such 3340 1.1 christos cases are rare. 3341 1.1 christos 3342 1.1 christos Each double-colon rule should specify commands; if it does not, an 3343 1.1 christos implicit rule will be used if one applies. 3344 1.1 christos @xref{Implicit Rules, ,Using Implicit Rules}. 3345 1.1 christos 3346 1.1 christos @node Automatic Prerequisites, , Double-Colon, Rules 3347 1.1 christos @section Generating Prerequisites Automatically 3348 1.1 christos @cindex prerequisites, automatic generation 3349 1.1 christos @cindex automatic generation of prerequisites 3350 1.1 christos @cindex generating prerequisites automatically 3351 1.1 christos 3352 1.1 christos In the makefile for a program, many of the rules you need to write often 3353 1.1 christos say only that some object file depends on some header 3354 1.1 christos file. For example, if @file{main.c} uses @file{defs.h} via an 3355 1.1 christos @code{#include}, you would write: 3356 1.1 christos 3357 1.1 christos @example 3358 1.1 christos main.o: defs.h 3359 1.1 christos @end example 3360 1.1 christos 3361 1.1 christos @noindent 3362 1.1 christos You need this rule so that @code{make} knows that it must remake 3363 1.1 christos @file{main.o} whenever @file{defs.h} changes. You can see that for a 3364 1.1 christos large program you would have to write dozens of such rules in your 3365 1.1 christos makefile. And, you must always be very careful to update the makefile 3366 1.1 christos every time you add or remove an @code{#include}. 3367 1.1 christos @cindex @code{#include} 3368 1.1 christos 3369 1.1 christos @cindex @code{-M} (to compiler) 3370 1.1 christos To avoid this hassle, most modern C compilers can write these rules for 3371 1.1 christos you, by looking at the @code{#include} lines in the source files. 3372 1.1 christos Usually this is done with the @samp{-M} option to the compiler. 3373 1.1 christos For example, the command: 3374 1.1 christos 3375 1.1 christos @example 3376 1.1 christos cc -M main.c 3377 1.1 christos @end example 3378 1.1 christos 3379 1.1 christos @noindent 3380 1.1 christos generates the output: 3381 1.1 christos 3382 1.1 christos @example 3383 1.1 christos main.o : main.c defs.h 3384 1.1 christos @end example 3385 1.1 christos 3386 1.1 christos @noindent 3387 1.1 christos Thus you no longer have to write all those rules yourself. 3388 1.1 christos The compiler will do it for you. 3389 1.1 christos 3390 1.1 christos Note that such a prerequisite constitutes mentioning @file{main.o} in a 3391 1.1 christos makefile, so it can never be considered an intermediate file by implicit 3392 1.1 christos rule search. This means that @code{make} won't ever remove the file 3393 1.1 christos after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}. 3394 1.1 christos 3395 1.1 christos @cindex @code{make depend} 3396 1.1 christos With old @code{make} programs, it was traditional practice to use this 3397 1.1 christos compiler feature to generate prerequisites on demand with a command like 3398 1.1 christos @samp{make depend}. That command would create a file @file{depend} 3399 1.1 christos containing all the automatically-generated prerequisites; then the 3400 1.1 christos makefile could use @code{include} to read them in (@pxref{Include}). 3401 1.1 christos 3402 1.1 christos In GNU @code{make}, the feature of remaking makefiles makes this 3403 1.1 christos practice obsolete---you need never tell @code{make} explicitly to 3404 1.1 christos regenerate the prerequisites, because it always regenerates any makefile 3405 1.1 christos that is out of date. @xref{Remaking Makefiles}. 3406 1.1 christos 3407 1.1 christos The practice we recommend for automatic prerequisite generation is to have 3408 1.1 christos one makefile corresponding to each source file. For each source file 3409 1.1 christos @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists 3410 1.1 christos what files the object file @file{@var{name}.o} depends on. That way 3411 1.1 christos only the source files that have changed need to be rescanned to produce 3412 1.1 christos the new prerequisites. 3413 1.1 christos 3414 1.1 christos Here is the pattern rule to generate a file of prerequisites (i.e., a makefile) 3415 1.1 christos called @file{@var{name}.d} from a C source file called @file{@var{name}.c}: 3416 1.1 christos 3417 1.1 christos @smallexample 3418 1.1 christos @group 3419 1.1 christos %.d: %.c 3420 1.1 christos @@set -e; rm -f $@@; \ 3421 1.1 christos $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \ 3422 1.1 christos sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \ 3423 1.1 christos rm -f $@@.$$$$ 3424 1.1 christos @end group 3425 1.1 christos @end smallexample 3426 1.1 christos 3427 1.1 christos @noindent 3428 1.1 christos @xref{Pattern Rules}, for information on defining pattern rules. The 3429 1.1 christos @samp{-e} flag to the shell causes it to exit immediately if the 3430 1.1 christos @code{$(CC)} command (or any other command) fails (exits with a 3431 1.1 christos nonzero status). 3432 1.1 christos @cindex @code{-e} (shell flag) 3433 1.1 christos 3434 1.1 christos @cindex @code{-MM} (to GNU compiler) 3435 1.1 christos With the GNU C compiler, you may wish to use the @samp{-MM} flag instead 3436 1.1 christos of @samp{-M}. This omits prerequisites on system header files. 3437 1.1 christos @xref{Preprocessor Options, , Options Controlling the Preprocessor, 3438 1.1 christos gcc.info, Using GNU CC}, for details. 3439 1.1 christos 3440 1.1 christos @cindex @code{sed} (shell command) 3441 1.1 christos The purpose of the @code{sed} command is to translate (for example): 3442 1.1 christos 3443 1.1 christos @example 3444 1.1 christos main.o : main.c defs.h 3445 1.1 christos @end example 3446 1.1 christos 3447 1.1 christos @noindent 3448 1.1 christos into: 3449 1.1 christos 3450 1.1 christos @example 3451 1.1 christos main.o main.d : main.c defs.h 3452 1.1 christos @end example 3453 1.1 christos 3454 1.1 christos @noindent 3455 1.1 christos @cindex @code{.d} 3456 1.1 christos This makes each @samp{.d} file depend on all the source and header files 3457 1.1 christos that the corresponding @samp{.o} file depends on. @code{make} then 3458 1.1 christos knows it must regenerate the prerequisites whenever any of the source or 3459 1.1 christos header files changes. 3460 1.1 christos 3461 1.1 christos Once you've defined the rule to remake the @samp{.d} files, 3462 1.1 christos you then use the @code{include} directive to read them all in. 3463 1.1 christos @xref{Include}. For example: 3464 1.1 christos 3465 1.1 christos @example 3466 1.1 christos @group 3467 1.1 christos sources = foo.c bar.c 3468 1.1 christos 3469 1.1 christos include $(sources:.c=.d) 3470 1.1 christos @end group 3471 1.1 christos @end example 3472 1.1 christos 3473 1.1 christos @noindent 3474 1.1 christos (This example uses a substitution variable reference to translate the 3475 1.1 christos list of source files @samp{foo.c bar.c} into a list of prerequisite 3476 1.1 christos makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full 3477 1.1 christos information on substitution references.) Since the @samp{.d} files are 3478 1.1 christos makefiles like any others, @code{make} will remake them as necessary 3479 1.1 christos with no further work from you. @xref{Remaking Makefiles}. 3480 1.1 christos 3481 1.1 christos Note that the @samp{.d} files contain target definitions; you should 3482 1.1 christos be sure to place the @code{include} directive @emph{after} the first, 3483 1.1 christos default goal in your makefiles or run the risk of having a random 3484 1.1 christos object file become the default goal. 3485 1.1 christos @xref{How Make Works}. 3486 1.1 christos 3487 1.1 christos @node Commands, Using Variables, Rules, Top 3488 1.1 christos @chapter Writing the Commands in Rules 3489 1.1 christos @cindex commands, how to write 3490 1.1 christos @cindex rule commands 3491 1.1 christos @cindex writing rule commands 3492 1.1 christos 3493 1.1 christos The commands of a rule consist of one or more shell command lines to 3494 1.1 christos be executed, one at a time, in the order they appear. Typically, the 3495 1.1 christos result of executing these commands is that the target of the rule is 3496 1.1 christos brought up to date. 3497 1.1 christos 3498 1.1 christos Users use many different shell programs, but commands in makefiles are 3499 1.1 christos always interpreted by @file{/bin/sh} unless the makefile specifies 3500 1.1 christos otherwise. @xref{Execution, ,Command Execution}. 3501 1.1 christos 3502 1.1 christos @menu 3503 1.1 christos * Command Syntax:: Command syntax features and pitfalls. 3504 1.1 christos * Echoing:: How to control when commands are echoed. 3505 1.1 christos * Execution:: How commands are executed. 3506 1.1 christos * Parallel:: How commands can be executed in parallel. 3507 1.1 christos * Errors:: What happens after a command execution error. 3508 1.1 christos * Interrupts:: What happens when a command is interrupted. 3509 1.1 christos * Recursion:: Invoking @code{make} from makefiles. 3510 1.1 christos * Sequences:: Defining canned sequences of commands. 3511 1.1 christos * Empty Commands:: Defining useful, do-nothing commands. 3512 1.1 christos @end menu 3513 1.1 christos 3514 1.1 christos @node Command Syntax, Echoing, Commands, Commands 3515 1.1 christos @section Command Syntax 3516 1.1 christos @cindex command syntax 3517 1.1 christos @cindex syntax of commands 3518 1.1 christos 3519 1.1 christos Makefiles have the unusual property that there are really two distinct 3520 1.1 christos syntaxes in one file. Most of the makefile uses @code{make} syntax 3521 1.1 christos (@pxref{Makefiles, ,Writing Makefiles}). However, commands are meant to be 3522 1.1 christos interpreted by the shell and so they are written using shell syntax. 3523 1.1 christos The @code{make} program does not try to understand shell syntax: it 3524 1.1 christos performs only a very few specific translations on the content of the 3525 1.1 christos command before handing it to the shell. 3526 1.1 christos 3527 1.1 christos Each command line must start with a tab, except that the first command 3528 1.1 christos line may be attached to the target-and-prerequisites line with a 3529 1.1 christos semicolon in between. @emph{Any} line in the makefile that begins 3530 1.1 christos with a tab and appears in a ``rule context'' (that is, after a rule 3531 1.1 christos has been started until another rule or variable definition) will be 3532 1.1 christos considered a command line for that rule. Blank lines and lines of 3533 1.1 christos just comments may appear among the command lines; they are ignored. 3534 1.1 christos 3535 1.1 christos Some consequences of these rules include: 3536 1.1 christos 3537 1.1 christos @itemize @bullet 3538 1.1 christos @item 3539 1.1 christos A blank line that begins with a tab is not blank: it's an empty 3540 1.1 christos command (@pxref{Empty Commands}). 3541 1.1 christos 3542 1.1 christos @cindex comments, in commands 3543 1.1 christos @cindex commands, comments in 3544 1.1 christos @cindex @code{#} (comments), in commands 3545 1.1 christos @item 3546 1.1 christos A comment in a command line is not a @code{make} comment; it will be 3547 1.1 christos passed to the shell as-is. Whether the shell treats it as a comment 3548 1.1 christos or not depends on your shell. 3549 1.1 christos 3550 1.1 christos @item 3551 1.1 christos A variable definition in a ``rule context'' which is indented by a tab 3552 1.1 christos as the first character on the line, will be considered a command line, 3553 1.1 christos not a @code{make} variable definition, and passed to the shell. 3554 1.1 christos 3555 1.1 christos @item 3556 1.1 christos A conditional expression (@code{ifdef}, @code{ifeq}, 3557 1.1 christos etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule 3558 1.1 christos context'' which is indented by a tab as the first character on the 3559 1.1 christos line, will be considered a command line and be passed to the shell. 3560 1.1 christos 3561 1.1 christos @end itemize 3562 1.1 christos 3563 1.1 christos @menu 3564 1.1 christos * Splitting Lines:: Breaking long command lines for readability. 3565 1.1 christos * Variables in Commands:: Using @code{make} variables in commands. 3566 1.1 christos @end menu 3567 1.1 christos 3568 1.1 christos @node Splitting Lines, Variables in Commands, Command Syntax, Command Syntax 3569 1.1 christos @subsection Splitting Command Lines 3570 1.1 christos @cindex commands, splitting 3571 1.1 christos @cindex splitting commands 3572 1.1 christos @cindex commands, backslash (@code{\}) in 3573 1.1 christos @cindex commands, quoting newlines in 3574 1.1 christos @cindex backslash (@code{\}), in commands 3575 1.1 christos @cindex @code{\} (backslash), in commands 3576 1.1 christos @cindex quoting newline, in commands 3577 1.1 christos @cindex newline, quoting, in commands 3578 1.1 christos 3579 1.1 christos One of the few ways in which @code{make} does interpret command lines 3580 1.1 christos is checking for a backslash just before the newline. As in normal 3581 1.1 christos makefile syntax, a single command can be split into multiple lines in 3582 1.1 christos the makefile by placing a backslash before each newline. A sequence 3583 1.1 christos of lines like this is considered a single command, and one instance of 3584 1.1 christos the shell will be invoked to run it. 3585 1.1 christos 3586 1.1 christos However, in contrast to how they are treated in other places in a 3587 1.1 christos makefile, backslash-newline pairs are @emph{not} removed from the 3588 1.1 christos command. Both the backslash and the newline characters are preserved 3589 1.1 christos and passed to the shell. How the backslash-newline is interpreted 3590 1.1 christos depends on your shell. If the first character of the next line 3591 1.1 christos after the backslash-newline is a tab, then that tab (and only that 3592 1.1 christos tab) is removed. Whitespace is never added to the command. 3593 1.1 christos 3594 1.1 christos For example, this makefile: 3595 1.1 christos 3596 1.1 christos @example 3597 1.1 christos @group 3598 1.1 christos all : 3599 1.1 christos @@echo no\ 3600 1.1 christos space 3601 1.1 christos @@echo no\ 3602 1.1 christos space 3603 1.1 christos @@echo one \ 3604 1.1 christos space 3605 1.1 christos @@echo one\ 3606 1.1 christos space 3607 1.1 christos @end group 3608 1.1 christos @end example 3609 1.1 christos 3610 1.1 christos @noindent 3611 1.1 christos consists of four separate shell commands where the output is: 3612 1.1 christos 3613 1.1 christos @example 3614 1.1 christos @group 3615 1.1 christos nospace 3616 1.1 christos nospace 3617 1.1 christos one space 3618 1.1 christos one space 3619 1.1 christos @end group 3620 1.1 christos @end example 3621 1.1 christos 3622 1.1 christos As a more complex example, this makefile: 3623 1.1 christos 3624 1.1 christos @example 3625 1.1 christos @group 3626 1.1 christos all : ; @@echo 'hello \ 3627 1.1 christos world' ; echo "hello \ 3628 1.1 christos world" 3629 1.1 christos @end group 3630 1.1 christos @end example 3631 1.1 christos 3632 1.1 christos @noindent 3633 1.1 christos will run one shell with a command script of: 3634 1.1 christos 3635 1.1 christos @example 3636 1.1 christos @group 3637 1.1 christos echo 'hello \ 3638 1.1 christos world' ; echo "hello \ 3639 1.1 christos world" 3640 1.1 christos @end group 3641 1.1 christos @end example 3642 1.1 christos 3643 1.1 christos @noindent 3644 1.1 christos which, according to shell quoting rules, will yield the following output: 3645 1.1 christos 3646 1.1 christos @example 3647 1.1 christos @group 3648 1.1 christos hello \ 3649 1.1 christos world 3650 1.1 christos hello world 3651 1.1 christos @end group 3652 1.1 christos @end example 3653 1.1 christos 3654 1.1 christos @noindent 3655 1.1 christos Notice how the backslash/newline pair was removed inside the string quoted 3656 1.1 christos with double quotes (@code{"..."}), but not from the string quoted with single 3657 1.1 christos quotes (@code{'...'}). This is the way the default shell (@file{/bin/sh}) 3658 1.1 christos handles backslash/newline pairs. If you specify a different shell in your 3659 1.1 christos makefiles it may treat them differently. 3660 1.1 christos 3661 1.1 christos Sometimes you want to split a long line inside of single quotes, but 3662 1.1 christos you don't want the backslash-newline to appear in the quoted content. 3663 1.1 christos This is often the case when passing scripts to languages such as Perl, 3664 1.1 christos where extraneous backslashes inside the script can change its meaning 3665 1.1 christos or even be a syntax error. One simple way of handling this is to 3666 1.1 christos place the quoted string, or even the entire command, into a 3667 1.1 christos @code{make} variable then use the variable in the command. In this 3668 1.1 christos situation the newline quoting rules for makefiles will be used, and 3669 1.1 christos the backslash-newline will be removed. If we rewrite our example 3670 1.1 christos above using this method: 3671 1.1 christos 3672 1.1 christos @example 3673 1.1 christos @group 3674 1.1 christos HELLO = 'hello \ 3675 1.1 christos world' 3676 1.1 christos 3677 1.1 christos all : ; @@echo $(HELLO) 3678 1.1 christos @end group 3679 1.1 christos @end example 3680 1.1 christos 3681 1.1 christos @noindent 3682 1.1 christos we will get output like this: 3683 1.1 christos 3684 1.1 christos @example 3685 1.1 christos @group 3686 1.1 christos hello world 3687 1.1 christos @end group 3688 1.1 christos @end example 3689 1.1 christos 3690 1.1 christos If you like, you can also use target-specific variables 3691 1.1 christos (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain 3692 1.1 christos a tighter correspondence between the variable and the command that 3693 1.1 christos uses it. 3694 1.1 christos 3695 1.1 christos @node Variables in Commands, , Splitting Lines, Command Syntax 3696 1.1 christos @subsection Using Variables in Commands 3697 1.1 christos @cindex variable references in commands 3698 1.1 christos @cindex commands, using variables in 3699 1.1 christos 3700 1.1 christos The other way in which @code{make} processes commands is by expanding 3701 1.1 christos any variable references in them (@pxref{Reference,Basics of Variable 3702 1.1 christos References}). This occurs after make has finished reading all the 3703 1.1 christos makefiles and the target is determined to be out of date; so, the 3704 1.1 christos commands for targets which are not rebuilt are never expanded. 3705 1.1 christos 3706 1.1 christos Variable and function references in commands have identical syntax and 3707 1.1 christos semantics to references elsewhere in the makefile. They also have the 3708 1.1 christos same quoting rules: if you want a dollar sign to appear in your 3709 1.1 christos command, you must double it (@samp{$$}). For shells like the default 3710 1.1 christos shell, that use dollar signs to introduce variables, it's important to 3711 1.1 christos keep clear in your mind whether the variable you want to reference is 3712 1.1 christos a @code{make} variable (use a single dollar sign) or a shell variable 3713 1.1 christos (use two dollar signs). For example: 3714 1.1 christos 3715 1.1 christos @example 3716 1.1 christos @group 3717 1.1 christos LIST = one two three 3718 1.1 christos all: 3719 1.1 christos for i in $(LIST); do \ 3720 1.1 christos echo $$i; \ 3721 1.1 christos done 3722 1.1 christos @end group 3723 1.1 christos @end example 3724 1.1 christos 3725 1.1 christos @noindent 3726 1.1 christos results in the following command being passed to the shell: 3727 1.1 christos 3728 1.1 christos @example 3729 1.1 christos @group 3730 1.1 christos for i in one two three; do \ 3731 1.1 christos echo $i; \ 3732 1.1 christos done 3733 1.1 christos @end group 3734 1.1 christos @end example 3735 1.1 christos 3736 1.1 christos @noindent 3737 1.1 christos which generates the expected result: 3738 1.1 christos 3739 1.1 christos @example 3740 1.1 christos @group 3741 1.1 christos one 3742 1.1 christos two 3743 1.1 christos three 3744 1.1 christos @end group 3745 1.1 christos @end example 3746 1.1 christos 3747 1.1 christos @node Echoing, Execution, Command Syntax, Commands 3748 1.1 christos @section Command Echoing 3749 1.1 christos @cindex echoing of commands 3750 1.1 christos @cindex silent operation 3751 1.1 christos @cindex @code{@@} (in commands) 3752 1.1 christos @cindex commands, echoing 3753 1.1 christos @cindex printing of commands 3754 1.1 christos 3755 1.1 christos Normally @code{make} prints each command line before it is executed. 3756 1.1 christos We call this @dfn{echoing} because it gives the appearance that you 3757 1.1 christos are typing the commands yourself. 3758 1.1 christos 3759 1.1 christos When a line starts with @samp{@@}, the echoing of that line is suppressed. 3760 1.1 christos The @samp{@@} is discarded before the command is passed to the shell. 3761 1.1 christos Typically you would use this for a command whose only effect is to print 3762 1.1 christos something, such as an @code{echo} command to indicate progress through 3763 1.1 christos the makefile: 3764 1.1 christos 3765 1.1 christos @example 3766 1.1 christos @@echo About to make distribution files 3767 1.1 christos @end example 3768 1.1 christos 3769 1.1 christos @cindex @code{-n} 3770 1.1 christos @cindex @code{--just-print} 3771 1.1 christos @cindex @code{--dry-run} 3772 1.1 christos @cindex @code{--recon} 3773 1.1 christos When @code{make} is given the flag @samp{-n} or @samp{--just-print} 3774 1.1 christos it only echoes commands, it won't execute them. @xref{Options Summary, 3775 1.1 christos ,Summary of Options}. In this case and only this case, even the 3776 1.1 christos commands starting with @samp{@@} are printed. This flag is useful for 3777 1.1 christos finding out which commands @code{make} thinks are necessary without 3778 1.1 christos actually doing them. 3779 1.1 christos 3780 1.1 christos @cindex @code{-s} 3781 1.1 christos @cindex @code{--silent} 3782 1.1 christos @cindex @code{--quiet} 3783 1.1 christos @findex .SILENT 3784 1.1 christos The @samp{-s} or @samp{--silent} 3785 1.1 christos flag to @code{make} prevents all echoing, as if all commands 3786 1.1 christos started with @samp{@@}. A rule in the makefile for the special target 3787 1.1 christos @code{.SILENT} without prerequisites has the same effect 3788 1.1 christos (@pxref{Special Targets, ,Special Built-in Target Names}). 3789 1.1 christos @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill 3790 1.1 christos 3791 1.1 christos @node Execution, Parallel, Echoing, Commands 3792 1.1 christos @section Command Execution 3793 1.1 christos @cindex commands, execution 3794 1.1 christos @cindex execution, of commands 3795 1.1 christos @cindex shell command, execution 3796 1.1 christos @vindex @code{SHELL} @r{(command execution)} 3797 1.1 christos 3798 1.1 christos When it is time to execute commands to update a target, they are 3799 1.1 christos executed by invoking a new subshell for each command line. (In 3800 1.1 christos practice, @code{make} may take shortcuts that do not affect the 3801 1.1 christos results.) 3802 1.1 christos 3803 1.1 christos @cindex @code{cd} (shell command) 3804 1.1 christos @cindex shell variables, setting in commands 3805 1.1 christos @cindex commands setting shell variables 3806 1.1 christos @strong{Please note:} this implies that setting shell variables and 3807 1.1 christos invoking shell commands such as @code{cd} that set a context local to 3808 1.1 christos each process will not affect the following command lines.@footnote{On 3809 1.1 christos MS-DOS, the value of current working directory is @strong{global}, so 3810 1.1 christos changing it @emph{will} affect the following command lines on those 3811 1.1 christos systems.} If you want to use @code{cd} to affect the next statement, 3812 1.1 christos put both statements in a single command line. Then @code{make} will 3813 1.1 christos invoke one shell to run the entire line, and the shell will execute 3814 1.1 christos the statements in sequence. For example: 3815 1.1 christos 3816 1.1 christos @example 3817 1.1 christos foo : bar/lose 3818 1.1 christos cd $(@@D) && gobble $(@@F) > ../$@@ 3819 1.1 christos @end example 3820 1.1 christos 3821 1.1 christos @noindent 3822 1.1 christos Here we use the shell AND operator (@code{&&}) so that if the 3823 1.1 christos @code{cd} command fails, the script will fail without trying to invoke 3824 1.1 christos the @code{gobble} command in the wrong directory, which could cause 3825 1.1 christos problems (in this case it would certainly cause @file{../foo} to be 3826 1.1 christos truncated, at least). 3827 1.1 christos 3828 1.1 christos @menu 3829 1.1 christos * Choosing the Shell:: How @code{make} chooses the shell used 3830 1.1 christos to run commands. 3831 1.1 christos @end menu 3832 1.1 christos 3833 1.1 christos @node Choosing the Shell, , Execution, Execution 3834 1.1 christos @subsection Choosing the Shell 3835 1.1 christos @cindex shell, choosing the 3836 1.1 christos @cindex @code{SHELL}, value of 3837 1.1 christos 3838 1.1 christos @vindex SHELL 3839 1.1 christos The program used as the shell is taken from the variable @code{SHELL}. 3840 1.1 christos If this variable is not set in your makefile, the program 3841 1.1 christos @file{/bin/sh} is used as the shell. 3842 1.1 christos 3843 1.1 christos @cindex environment, @code{SHELL} in 3844 1.1 christos Unlike most variables, the variable @code{SHELL} is never set from the 3845 1.1 christos environment. This is because the @code{SHELL} environment variable is 3846 1.1 christos used to specify your personal choice of shell program for interactive 3847 1.1 christos use. It would be very bad for personal choices like this to affect the 3848 1.1 christos functioning of makefiles. @xref{Environment, ,Variables from the 3849 1.1 christos Environment}. 3850 1.1 christos 3851 1.1 christos Furthermore, when you do set @code{SHELL} in your makefile that value 3852 1.1 christos is @emph{not} exported in the environment to commands that @code{make} 3853 1.1 christos invokes. Instead, the value inherited from the user's environment, if 3854 1.1 christos any, is exported. You can override this behavior by explicitly 3855 1.1 christos exporting @code{SHELL} (@pxref{Variables/Recursion, ,Communicating 3856 1.1 christos Variables to a Sub-@code{make}}), forcing it to be passed in the 3857 1.1 christos environment to commands. 3858 1.1 christos 3859 1.1 christos @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})} 3860 1.1 christos However, on MS-DOS and MS-Windows the value of @code{SHELL} in the 3861 1.1 christos environment @strong{is} used, since on those systems most users do not 3862 1.1 christos set this variable, and therefore it is most likely set specifically to 3863 1.1 christos be used by @code{make}. On MS-DOS, if the setting of @code{SHELL} is 3864 1.1 christos not suitable for @code{make}, you can set the variable 3865 1.1 christos @code{MAKESHELL} to the shell that @code{make} should use; if set it 3866 1.1 christos will be used as the shell instead of the value of @code{SHELL}. 3867 1.1 christos 3868 1.1 christos @subsubheading Choosing a Shell in DOS and Windows 3869 1.1 christos @cindex shell, in DOS and Windows 3870 1.1 christos @cindex DOS, choosing a shell in 3871 1.1 christos @cindex Windows, choosing a shell in 3872 1.1 christos 3873 1.1 christos Choosing a shell in MS-DOS and MS-Windows is much more complex than on 3874 1.1 christos other systems. 3875 1.1 christos 3876 1.1 christos @vindex COMSPEC 3877 1.1 christos On MS-DOS, if @code{SHELL} is not set, the value of the variable 3878 1.1 christos @code{COMSPEC} (which is always set) is used instead. 3879 1.1 christos 3880 1.1 christos @cindex @code{SHELL}, MS-DOS specifics 3881 1.1 christos The processing of lines that set the variable @code{SHELL} in Makefiles 3882 1.1 christos is different on MS-DOS. The stock shell, @file{command.com}, is 3883 1.1 christos ridiculously limited in its functionality and many users of @code{make} 3884 1.1 christos tend to install a replacement shell. Therefore, on MS-DOS, @code{make} 3885 1.1 christos examines the value of @code{SHELL}, and changes its behavior based on 3886 1.1 christos whether it points to a Unix-style or DOS-style shell. This allows 3887 1.1 christos reasonable functionality even if @code{SHELL} points to 3888 1.1 christos @file{command.com}. 3889 1.1 christos 3890 1.1 christos If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS 3891 1.1 christos additionally checks whether that shell can indeed be found; if not, it 3892 1.1 christos ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make} 3893 1.1 christos searches for the shell in the following places: 3894 1.1 christos 3895 1.1 christos @enumerate 3896 1.1 christos @item 3897 1.1 christos In the precise place pointed to by the value of @code{SHELL}. For 3898 1.1 christos example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make} 3899 1.1 christos will look in the directory @file{/bin} on the current drive. 3900 1.1 christos 3901 1.1 christos @item 3902 1.1 christos In the current directory. 3903 1.1 christos 3904 1.1 christos @item 3905 1.1 christos In each of the directories in the @code{PATH} variable, in order. 3906 1.1 christos 3907 1.1 christos @end enumerate 3908 1.1 christos 3909 1.1 christos In every directory it examines, @code{make} will first look for the 3910 1.1 christos specific file (@file{sh} in the example above). If this is not found, 3911 1.1 christos it will also look in that directory for that file with one of the known 3912 1.1 christos extensions which identify executable files. For example @file{.exe}, 3913 1.1 christos @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others. 3914 1.1 christos 3915 1.1 christos If any of these attempts is successful, the value of @code{SHELL} will 3916 1.1 christos be set to the full pathname of the shell as found. However, if none of 3917 1.1 christos these is found, the value of @code{SHELL} will not be changed, and thus 3918 1.1 christos the line that sets it will be effectively ignored. This is so 3919 1.1 christos @code{make} will only support features specific to a Unix-style shell if 3920 1.1 christos such a shell is actually installed on the system where @code{make} runs. 3921 1.1 christos 3922 1.1 christos Note that this extended search for the shell is limited to the cases 3923 1.1 christos where @code{SHELL} is set from the Makefile; if it is set in the 3924 1.1 christos environment or command line, you are expected to set it to the full 3925 1.1 christos pathname of the shell, exactly as things are on Unix. 3926 1.1 christos 3927 1.1 christos The effect of the above DOS-specific processing is that a Makefile that 3928 1.1 christos contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work 3929 1.1 christos on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some 3930 1.1 christos directory along your @code{PATH}. 3931 1.1 christos 3932 1.1 christos @node Parallel, Errors, Execution, Commands 3933 1.1 christos @section Parallel Execution 3934 1.1 christos @cindex commands, execution in parallel 3935 1.1 christos @cindex parallel execution 3936 1.1 christos @cindex execution, in parallel 3937 1.1 christos @cindex job slots 3938 1.1 christos @cindex @code{-j} 3939 1.1 christos @cindex @code{--jobs} 3940 1.1 christos 3941 1.1 christos GNU @code{make} knows how to execute several commands at once. 3942 1.1 christos Normally, @code{make} will execute only one command at a time, waiting 3943 1.1 christos for it to finish before executing the next. However, the @samp{-j} or 3944 1.1 christos @samp{--jobs} option tells @code{make} to execute many commands 3945 1.1 christos simultaneously.@refill 3946 1.1 christos 3947 1.1 christos On MS-DOS, the @samp{-j} option has no effect, since that system doesn't 3948 1.1 christos support multi-processing. 3949 1.1 christos 3950 1.1 christos If the @samp{-j} option is followed by an integer, this is the number of 3951 1.1 christos commands to execute at once; this is called the number of @dfn{job slots}. 3952 1.1 christos If there is nothing looking like an integer after the @samp{-j} option, 3953 1.1 christos there is no limit on the number of job slots. The default number of job 3954 1.1 christos slots is one, which means serial execution (one thing at a time). 3955 1.1 christos 3956 1.1 christos One unpleasant consequence of running several commands simultaneously is 3957 1.1 christos that output generated by the commands appears whenever each command 3958 1.1 christos sends it, so messages from different commands may be interspersed. 3959 1.1 christos 3960 1.1 christos Another problem is that two processes cannot both take input from the 3961 1.1 christos same device; so to make sure that only one command tries to take input 3962 1.1 christos from the terminal at once, @code{make} will invalidate the standard 3963 1.1 christos input streams of all but one running command. This means that 3964 1.1 christos attempting to read from standard input will usually be a fatal error (a 3965 1.1 christos @samp{Broken pipe} signal) for most child processes if there are 3966 1.1 christos several. 3967 1.1 christos @cindex broken pipe 3968 1.1 christos @cindex standard input 3969 1.1 christos 3970 1.1 christos It is unpredictable which command will have a valid standard input stream 3971 1.1 christos (which will come from the terminal, or wherever you redirect the standard 3972 1.1 christos input of @code{make}). The first command run will always get it first, and 3973 1.1 christos the first command started after that one finishes will get it next, and so 3974 1.1 christos on. 3975 1.1 christos 3976 1.1 christos We will change how this aspect of @code{make} works if we find a better 3977 1.1 christos alternative. In the mean time, you should not rely on any command using 3978 1.1 christos standard input at all if you are using the parallel execution feature; but 3979 1.1 christos if you are not using this feature, then standard input works normally in 3980 1.1 christos all commands. 3981 1.1 christos 3982 1.1 christos Finally, handling recursive @code{make} invocations raises issues. For 3983 1.1 christos more information on this, see 3984 1.1 christos @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}. 3985 1.1 christos 3986 1.1 christos If a command fails (is killed by a signal or exits with a nonzero 3987 1.1 christos status), and errors are not ignored for that command 3988 1.1 christos (@pxref{Errors, ,Errors in Commands}), 3989 1.1 christos the remaining command lines to remake the same target will not be run. 3990 1.1 christos If a command fails and the @samp{-k} or @samp{--keep-going} 3991 1.1 christos option was not given 3992 1.1 christos (@pxref{Options Summary, ,Summary of Options}), 3993 1.1 christos @code{make} aborts execution. If make 3994 1.1 christos terminates for any reason (including a signal) with child processes 3995 1.1 christos running, it waits for them to finish before actually exiting.@refill 3996 1.1 christos 3997 1.1 christos @cindex load average 3998 1.1 christos @cindex limiting jobs based on load 3999 1.1 christos @cindex jobs, limiting based on load 4000 1.1 christos @cindex @code{-l} (load average) 4001 1.1 christos @cindex @code{--max-load} 4002 1.1 christos @cindex @code{--load-average} 4003 1.1 christos When the system is heavily loaded, you will probably want to run fewer jobs 4004 1.1 christos than when it is lightly loaded. You can use the @samp{-l} option to tell 4005 1.1 christos @code{make} to limit the number of jobs to run at once, based on the load 4006 1.1 christos average. The @samp{-l} or @samp{--max-load} 4007 1.1 christos option is followed by a floating-point number. For 4008 1.1 christos example, 4009 1.1 christos 4010 1.1 christos @example 4011 1.1 christos -l 2.5 4012 1.1 christos @end example 4013 1.1 christos 4014 1.1 christos @noindent 4015 1.1 christos will not let @code{make} start more than one job if the load average is 4016 1.1 christos above 2.5. The @samp{-l} option with no following number removes the 4017 1.1 christos load limit, if one was given with a previous @samp{-l} option.@refill 4018 1.1 christos 4019 1.1 christos More precisely, when @code{make} goes to start up a job, and it already has 4020 1.1 christos at least one job running, it checks the current load average; if it is not 4021 1.1 christos lower than the limit given with @samp{-l}, @code{make} waits until the load 4022 1.1 christos average goes below that limit, or until all the other jobs finish. 4023 1.1 christos 4024 1.1 christos By default, there is no load limit. 4025 1.1 christos 4026 1.1 christos @node Errors, Interrupts, Parallel, Commands 4027 1.1 christos @section Errors in Commands 4028 1.1 christos @cindex errors (in commands) 4029 1.1 christos @cindex commands, errors in 4030 1.1 christos @cindex exit status (errors) 4031 1.1 christos 4032 1.1 christos After each shell command returns, @code{make} looks at its exit status. 4033 1.1 christos If the command completed successfully, the next command line is executed 4034 1.1 christos in a new shell; after the last command line is finished, the rule is 4035 1.1 christos finished. 4036 1.1 christos 4037 1.1 christos If there is an error (the exit status is nonzero), @code{make} gives up on 4038 1.1 christos the current rule, and perhaps on all rules. 4039 1.1 christos 4040 1.1 christos Sometimes the failure of a certain command does not indicate a problem. 4041 1.1 christos For example, you may use the @code{mkdir} command to ensure that a 4042 1.1 christos directory exists. If the directory already exists, @code{mkdir} will 4043 1.1 christos report an error, but you probably want @code{make} to continue regardless. 4044 1.1 christos 4045 1.1 christos @cindex @code{-} (in commands) 4046 1.1 christos To ignore errors in a command line, write a @samp{-} at the beginning of 4047 1.1 christos the line's text (after the initial tab). The @samp{-} is discarded before 4048 1.1 christos the command is passed to the shell for execution. 4049 1.1 christos 4050 1.1 christos For example, 4051 1.1 christos 4052 1.1 christos @example 4053 1.1 christos @group 4054 1.1 christos clean: 4055 1.1 christos -rm -f *.o 4056 1.1 christos @end group 4057 1.1 christos @end example 4058 1.1 christos @cindex @code{rm} (shell command) 4059 1.1 christos 4060 1.1 christos @noindent 4061 1.1 christos This causes @code{rm} to continue even if it is unable to remove a file. 4062 1.1 christos 4063 1.1 christos @cindex @code{-i} 4064 1.1 christos @cindex @code{--ignore-errors} 4065 1.1 christos @findex .IGNORE 4066 1.1 christos When you run @code{make} with the @samp{-i} or @samp{--ignore-errors} 4067 1.1 christos flag, errors are ignored in all commands of all rules. A rule in the 4068 1.1 christos makefile for the special target @code{.IGNORE} has the same effect, if 4069 1.1 christos there are no prerequisites. These ways of ignoring errors are obsolete 4070 1.1 christos because @samp{-} is more flexible. 4071 1.1 christos 4072 1.1 christos When errors are to be ignored, because of either a @samp{-} or the 4073 1.1 christos @samp{-i} flag, @code{make} treats an error return just like success, 4074 1.1 christos except that it prints out a message that tells you the status code 4075 1.1 christos the command exited with, and says that the error has been ignored. 4076 1.1 christos 4077 1.1 christos When an error happens that @code{make} has not been told to ignore, 4078 1.1 christos it implies that the current target cannot be correctly remade, and neither 4079 1.1 christos can any other that depends on it either directly or indirectly. No further 4080 1.1 christos commands will be executed for these targets, since their preconditions 4081 1.1 christos have not been achieved. 4082 1.1 christos 4083 1.1 christos 4084 1.1 christos @cindex @code{-k} 4085 1.1 christos @cindex @code{--keep-going} 4086 1.1 christos Normally @code{make} gives up immediately in this circumstance, returning a 4087 1.1 christos nonzero status. However, if the @samp{-k} or @samp{--keep-going} 4088 1.1 christos flag is specified, @code{make} 4089 1.1 christos continues to consider the other prerequisites of the pending targets, 4090 1.1 christos remaking them if necessary, before it gives up and returns nonzero status. 4091 1.1 christos For example, after an error in compiling one object file, @samp{make -k} 4092 1.1 christos will continue compiling other object files even though it already knows 4093 1.1 christos that linking them will be impossible. @xref{Options Summary, ,Summary of Options}. 4094 1.1 christos 4095 1.1 christos The usual behavior assumes that your purpose is to get the specified 4096 1.1 christos targets up to date; once @code{make} learns that this is impossible, it 4097 1.1 christos might as well report the failure immediately. The @samp{-k} option says 4098 1.1 christos that the real purpose is to test as many of the changes made in the 4099 1.1 christos program as possible, perhaps to find several independent problems so 4100 1.1 christos that you can correct them all before the next attempt to compile. This 4101 1.1 christos is why Emacs' @code{compile} command passes the @samp{-k} flag by 4102 1.1 christos default. 4103 1.1 christos @cindex Emacs (@code{M-x compile}) 4104 1.1 christos 4105 1.1 christos @findex .DELETE_ON_ERROR 4106 1.1 christos @cindex deletion of target files 4107 1.1 christos @cindex removal of target files 4108 1.1 christos @cindex target, deleting on error 4109 1.1 christos Usually when a command fails, if it has changed the target file at all, 4110 1.1 christos the file is corrupted and cannot be used---or at least it is not 4111 1.1 christos completely updated. Yet the file's time stamp says that it is now up to 4112 1.1 christos date, so the next time @code{make} runs, it will not try to update that 4113 1.1 christos file. The situation is just the same as when the command is killed by a 4114 1.1 christos signal; @pxref{Interrupts}. So generally the right thing to do is to 4115 1.1 christos delete the target file if the command fails after beginning to change 4116 1.1 christos the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears 4117 1.1 christos as a target. This is almost always what you want @code{make} to do, but 4118 1.1 christos it is not historical practice; so for compatibility, you must explicitly 4119 1.1 christos request it. 4120 1.1 christos 4121 1.1 christos @node Interrupts, Recursion, Errors, Commands 4122 1.1 christos @section Interrupting or Killing @code{make} 4123 1.1 christos @cindex interrupt 4124 1.1 christos @cindex signal 4125 1.1 christos @cindex deletion of target files 4126 1.1 christos @cindex removal of target files 4127 1.1 christos @cindex target, deleting on interrupt 4128 1.1 christos @cindex killing (interruption) 4129 1.1 christos 4130 1.1 christos If @code{make} gets a fatal signal while a command is executing, it may 4131 1.1 christos delete the target file that the command was supposed to update. This is 4132 1.1 christos done if the target file's last-modification time has changed since 4133 1.1 christos @code{make} first checked it. 4134 1.1 christos 4135 1.1 christos The purpose of deleting the target is to make sure that it is remade from 4136 1.1 christos scratch when @code{make} is next run. Why is this? Suppose you type 4137 1.1 christos @kbd{Ctrl-c} while a compiler is running, and it has begun to write an 4138 1.1 christos object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting 4139 1.1 christos in an incomplete file whose last-modification time is newer than the source 4140 1.1 christos file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal 4141 1.1 christos and deletes this incomplete file. If @code{make} did not do this, the next 4142 1.1 christos invocation of @code{make} would think that @file{foo.o} did not require 4143 1.1 christos updating---resulting in a strange error message from the linker when it 4144 1.1 christos tries to link an object file half of which is missing. 4145 1.1 christos 4146 1.1 christos @findex .PRECIOUS 4147 1.1 christos You can prevent the deletion of a target file in this way by making the 4148 1.1 christos special target @code{.PRECIOUS} depend on it. Before remaking a target, 4149 1.1 christos @code{make} checks to see whether it appears on the prerequisites of 4150 1.1 christos @code{.PRECIOUS}, and thereby decides whether the target should be deleted 4151 1.1 christos if a signal happens. Some reasons why you might do this are that the 4152 1.1 christos target is updated in some atomic fashion, or exists only to record a 4153 1.1 christos modification-time (its contents do not matter), or must exist at all 4154 1.1 christos times to prevent other sorts of trouble. 4155 1.1 christos 4156 1.1 christos @node Recursion, Sequences, Interrupts, Commands 4157 1.1 christos @section Recursive Use of @code{make} 4158 1.1 christos @cindex recursion 4159 1.1 christos @cindex subdirectories, recursion for 4160 1.1 christos 4161 1.1 christos Recursive use of @code{make} means using @code{make} as a command in a 4162 1.1 christos makefile. This technique is useful when you want separate makefiles for 4163 1.1 christos various subsystems that compose a larger system. For example, suppose you 4164 1.1 christos have a subdirectory @file{subdir} which has its own makefile, and you would 4165 1.1 christos like the containing directory's makefile to run @code{make} on the 4166 1.1 christos subdirectory. You can do it by writing this: 4167 1.1 christos 4168 1.1 christos @example 4169 1.1 christos subsystem: 4170 1.1 christos cd subdir && $(MAKE) 4171 1.1 christos @end example 4172 1.1 christos 4173 1.1 christos @noindent 4174 1.1 christos or, equivalently, this (@pxref{Options Summary, ,Summary of Options}): 4175 1.1 christos 4176 1.1 christos @example 4177 1.1 christos subsystem: 4178 1.1 christos $(MAKE) -C subdir 4179 1.1 christos @end example 4180 1.1 christos @cindex @code{-C} 4181 1.1 christos @cindex @code{--directory} 4182 1.1 christos 4183 1.1 christos You can write recursive @code{make} commands just by copying this example, 4184 1.1 christos but there are many things to know about how they work and why, and about 4185 1.1 christos how the sub-@code{make} relates to the top-level @code{make}. You may 4186 1.1 christos also find it useful to declare targets that invoke recursive 4187 1.1 christos @code{make} commands as @samp{.PHONY} (for more discussion on when 4188 1.1 christos this is useful, see @ref{Phony Targets}). 4189 1.1 christos 4190 1.1 christos @vindex @code{CURDIR} 4191 1.1 christos For your convenience, when GNU @code{make} starts (after it has 4192 1.1 christos processed any @code{-C} options) it sets the variable @code{CURDIR} to 4193 1.1 christos the pathname of the current working directory. This value is never 4194 1.1 christos touched by @code{make} again: in particular note that if you include 4195 1.1 christos files from other directories the value of @code{CURDIR} does not 4196 1.1 christos change. The value has the same precedence it would have if it were 4197 1.1 christos set in the makefile (by default, an environment variable @code{CURDIR} 4198 1.1 christos will not override this value). Note that setting this variable has no 4199 1.1 christos impact on the operation of @code{make} (it does not cause @code{make} 4200 1.1 christos to change its working directory, for example). 4201 1.1 christos 4202 1.1 christos @menu 4203 1.1 christos * MAKE Variable:: The special effects of using @samp{$(MAKE)}. 4204 1.1 christos * Variables/Recursion:: How to communicate variables to a sub-@code{make}. 4205 1.1 christos * Options/Recursion:: How to communicate options to a sub-@code{make}. 4206 1.1 christos * -w Option:: How the @samp{-w} or @samp{--print-directory} option 4207 1.1 christos helps debug use of recursive @code{make} commands. 4208 1.1 christos @end menu 4209 1.1 christos 4210 1.1 christos @node MAKE Variable, Variables/Recursion, Recursion, Recursion 4211 1.1 christos @subsection How the @code{MAKE} Variable Works 4212 1.1 christos @vindex MAKE 4213 1.1 christos @cindex recursion, and @code{MAKE} variable 4214 1.1 christos 4215 1.1 christos Recursive @code{make} commands should always use the variable @code{MAKE}, 4216 1.1 christos not the explicit command name @samp{make}, as shown here: 4217 1.1 christos 4218 1.1 christos @example 4219 1.1 christos @group 4220 1.1 christos subsystem: 4221 1.1 christos cd subdir && $(MAKE) 4222 1.1 christos @end group 4223 1.1 christos @end example 4224 1.1 christos 4225 1.1 christos The value of this variable is the file name with which @code{make} was 4226 1.1 christos invoked. If this file name was @file{/bin/make}, then the command executed 4227 1.1 christos is @samp{cd subdir && /bin/make}. If you use a special version of 4228 1.1 christos @code{make} to run the top-level makefile, the same special version will be 4229 1.1 christos executed for recursive invocations. 4230 1.1 christos @cindex @code{cd} (shell command) 4231 1.1 christos 4232 1.1 christos @cindex +, and commands 4233 1.1 christos As a special feature, using the variable @code{MAKE} in the commands of 4234 1.1 christos a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n} 4235 1.1 christos (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option. 4236 1.1 christos Using the @code{MAKE} variable has the same effect as using a @samp{+} 4237 1.1 christos character at the beginning of the command line. @xref{Instead of 4238 1.1 christos Execution, ,Instead of Executing the Commands}. This special feature 4239 1.1 christos is only enabled if the @code{MAKE} variable appears directly in the 4240 1.1 christos command script: it does not apply if the @code{MAKE} variable is 4241 1.1 christos referenced through expansion of another variable. In the latter case 4242 1.1 christos you must use the @samp{+} token to get these special effects.@refill 4243 1.1 christos 4244 1.1 christos Consider the command @samp{make -t} in the above example. (The 4245 1.1 christos @samp{-t} option marks targets as up to date without actually running 4246 1.1 christos any commands; see @ref{Instead of Execution}.) Following the usual 4247 1.1 christos definition of @samp{-t}, a @samp{make -t} command in the example would 4248 1.1 christos create a file named @file{subsystem} and do nothing else. What you 4249 1.1 christos really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would 4250 1.1 christos require executing the command, and @samp{-t} says not to execute 4251 1.1 christos commands.@refill 4252 1.1 christos @cindex @code{-t}, and recursion 4253 1.1 christos @cindex recursion, and @code{-t} 4254 1.1 christos @cindex @code{--touch}, and recursion 4255 1.1 christos 4256 1.1 christos The special feature makes this do what you want: whenever a command 4257 1.1 christos line of a rule contains the variable @code{MAKE}, the flags @samp{-t}, 4258 1.1 christos @samp{-n} and @samp{-q} do not apply to that line. Command lines 4259 1.1 christos containing @code{MAKE} are executed normally despite the presence of a 4260 1.1 christos flag that causes most commands not to be run. The usual 4261 1.1 christos @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make} 4262 1.1 christos (@pxref{Options/Recursion, ,Communicating Options to a 4263 1.1 christos Sub-@code{make}}), so your request to touch the files, or print the 4264 1.1 christos commands, is propagated to the subsystem.@refill 4265 1.1 christos 4266 1.1 christos @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion 4267 1.1 christos @subsection Communicating Variables to a Sub-@code{make} 4268 1.1 christos @cindex sub-@code{make} 4269 1.1 christos @cindex environment, and recursion 4270 1.1 christos @cindex exporting variables 4271 1.1 christos @cindex variables, environment 4272 1.1 christos @cindex variables, exporting 4273 1.1 christos @cindex recursion, and environment 4274 1.1 christos @cindex recursion, and variables 4275 1.1 christos 4276 1.1 christos Variable values of the top-level @code{make} can be passed to the 4277 1.1 christos sub-@code{make} through the environment by explicit request. These 4278 1.1 christos variables are defined in the sub-@code{make} as defaults, but do not 4279 1.1 christos override what is specified in the makefile used by the sub-@code{make} 4280 1.1 christos makefile unless you use the @samp{-e} switch (@pxref{Options Summary, 4281 1.1 christos ,Summary of Options}).@refill 4282 1.1 christos 4283 1.1 christos To pass down, or @dfn{export}, a variable, @code{make} adds the variable 4284 1.1 christos and its value to the environment for running each command. The 4285 1.1 christos sub-@code{make}, in turn, uses the environment to initialize its table 4286 1.1 christos of variable values. @xref{Environment, ,Variables from the 4287 1.1 christos Environment}. 4288 1.1 christos 4289 1.1 christos Except by explicit request, @code{make} exports a variable only if it 4290 1.1 christos is either defined in the environment initially or set on the command 4291 1.1 christos line, and if its name consists only of letters, numbers, and underscores. 4292 1.1 christos Some shells cannot cope with environment variable names consisting of 4293 1.1 christos characters other than letters, numbers, and underscores. 4294 1.1 christos 4295 1.1 christos @cindex SHELL, exported value 4296 1.1 christos The value of the @code{make} variable @code{SHELL} is not exported. 4297 1.1 christos Instead, the value of the @code{SHELL} variable from the invoking 4298 1.1 christos environment is passed to the sub-@code{make}. You can force 4299 1.1 christos @code{make} to export its value for @code{SHELL} by using the 4300 1.1 christos @code{export} directive, described below. @xref{Choosing the Shell}. 4301 1.1 christos 4302 1.1 christos The special variable @code{MAKEFLAGS} is always exported (unless you 4303 1.1 christos unexport it). @code{MAKEFILES} is exported if you set it to anything. 4304 1.1 christos 4305 1.1 christos @code{make} automatically passes down variable values that were defined 4306 1.1 christos on the command line, by putting them in the @code{MAKEFLAGS} variable. 4307 1.1 christos @iftex 4308 1.1 christos See the next section. 4309 1.1 christos @end iftex 4310 1.1 christos @ifnottex 4311 1.1 christos @xref{Options/Recursion}. 4312 1.1 christos @end ifnottex 4313 1.1 christos 4314 1.1 christos Variables are @emph{not} normally passed down if they were created by 4315 1.1 christos default by @code{make} (@pxref{Implicit Variables, ,Variables Used by 4316 1.1 christos Implicit Rules}). The sub-@code{make} will define these for 4317 1.1 christos itself.@refill 4318 1.1 christos 4319 1.1 christos @findex export 4320 1.1 christos If you want to export specific variables to a sub-@code{make}, use the 4321 1.1 christos @code{export} directive, like this: 4322 1.1 christos 4323 1.1 christos @example 4324 1.1 christos export @var{variable} @dots{} 4325 1.1 christos @end example 4326 1.1 christos 4327 1.1 christos @noindent 4328 1.1 christos @findex unexport 4329 1.1 christos If you want to @emph{prevent} a variable from being exported, use the 4330 1.1 christos @code{unexport} directive, like this: 4331 1.1 christos 4332 1.1 christos @example 4333 1.1 christos unexport @var{variable} @dots{} 4334 1.1 christos @end example 4335 1.1 christos 4336 1.1 christos @noindent 4337 1.1 christos In both of these forms, the arguments to @code{export} and 4338 1.1 christos @code{unexport} are expanded, and so could be variables or functions 4339 1.1 christos which expand to a (list of) variable names to be (un)exported. 4340 1.1 christos 4341 1.1 christos As a convenience, you can define a variable and export it at the same 4342 1.1 christos time by doing: 4343 1.1 christos 4344 1.1 christos @example 4345 1.1 christos export @var{variable} = value 4346 1.1 christos @end example 4347 1.1 christos 4348 1.1 christos @noindent 4349 1.1 christos has the same result as: 4350 1.1 christos 4351 1.1 christos @example 4352 1.1 christos @var{variable} = value 4353 1.1 christos export @var{variable} 4354 1.1 christos @end example 4355 1.1 christos 4356 1.1 christos @noindent 4357 1.1 christos and 4358 1.1 christos 4359 1.1 christos @example 4360 1.1 christos export @var{variable} := value 4361 1.1 christos @end example 4362 1.1 christos 4363 1.1 christos @noindent 4364 1.1 christos has the same result as: 4365 1.1 christos 4366 1.1 christos @example 4367 1.1 christos @var{variable} := value 4368 1.1 christos export @var{variable} 4369 1.1 christos @end example 4370 1.1 christos 4371 1.1 christos Likewise, 4372 1.1 christos 4373 1.1 christos @example 4374 1.1 christos export @var{variable} += value 4375 1.1 christos @end example 4376 1.1 christos 4377 1.1 christos @noindent 4378 1.1 christos is just like: 4379 1.1 christos 4380 1.1 christos @example 4381 1.1 christos @var{variable} += value 4382 1.1 christos export @var{variable} 4383 1.1 christos @end example 4384 1.1 christos 4385 1.1 christos @noindent 4386 1.1 christos @xref{Appending, ,Appending More Text to Variables}. 4387 1.1 christos 4388 1.1 christos You may notice that the @code{export} and @code{unexport} directives 4389 1.1 christos work in @code{make} in the same way they work in the shell, @code{sh}. 4390 1.1 christos 4391 1.1 christos If you want all variables to be exported by default, you can use 4392 1.1 christos @code{export} by itself: 4393 1.1 christos 4394 1.1 christos @example 4395 1.1 christos export 4396 1.1 christos @end example 4397 1.1 christos 4398 1.1 christos @noindent 4399 1.1 christos This tells @code{make} that variables which are not explicitly mentioned 4400 1.1 christos in an @code{export} or @code{unexport} directive should be exported. 4401 1.1 christos Any variable given in an @code{unexport} directive will still @emph{not} 4402 1.1 christos be exported. If you use @code{export} by itself to export variables by 4403 1.1 christos default, variables whose names contain characters other than 4404 1.1 christos alphanumerics and underscores will not be exported unless specifically 4405 1.1 christos mentioned in an @code{export} directive.@refill 4406 1.1 christos 4407 1.1 christos @findex .EXPORT_ALL_VARIABLES 4408 1.1 christos The behavior elicited by an @code{export} directive by itself was the 4409 1.1 christos default in older versions of GNU @code{make}. If your makefiles depend 4410 1.1 christos on this behavior and you want to be compatible with old versions of 4411 1.1 christos @code{make}, you can write a rule for the special target 4412 1.1 christos @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive. 4413 1.1 christos This will be ignored by old @code{make}s, while the @code{export} 4414 1.1 christos directive will cause a syntax error.@refill 4415 1.1 christos @cindex compatibility in exporting 4416 1.1 christos 4417 1.1 christos Likewise, you can use @code{unexport} by itself to tell @code{make} 4418 1.1 christos @emph{not} to export variables by default. Since this is the default 4419 1.1 christos behavior, you would only need to do this if @code{export} had been used 4420 1.1 christos by itself earlier (in an included makefile, perhaps). You 4421 1.1 christos @strong{cannot} use @code{export} and @code{unexport} by themselves to 4422 1.1 christos have variables exported for some commands and not for others. The last 4423 1.1 christos @code{export} or @code{unexport} directive that appears by itself 4424 1.1 christos determines the behavior for the entire run of @code{make}.@refill 4425 1.1 christos 4426 1.1 christos @vindex MAKELEVEL 4427 1.1 christos @cindex recursion, level of 4428 1.1 christos As a special feature, the variable @code{MAKELEVEL} is changed when it 4429 1.1 christos is passed down from level to level. This variable's value is a string 4430 1.1 christos which is the depth of the level as a decimal number. The value is 4431 1.1 christos @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make}, 4432 1.1 christos @samp{2} for a sub-sub-@code{make}, and so on. The incrementation 4433 1.1 christos happens when @code{make} sets up the environment for a command.@refill 4434 1.1 christos 4435 1.1 christos The main use of @code{MAKELEVEL} is to test it in a conditional 4436 1.1 christos directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this 4437 1.1 christos way you can write a makefile that behaves one way if run recursively and 4438 1.1 christos another way if run directly by you.@refill 4439 1.1 christos 4440 1.1 christos @vindex MAKEFILES 4441 1.1 christos You can use the variable @code{MAKEFILES} to cause all sub-@code{make} 4442 1.1 christos commands to use additional makefiles. The value of @code{MAKEFILES} is 4443 1.1 christos a whitespace-separated list of file names. This variable, if defined in 4444 1.1 christos the outer-level makefile, is passed down through the environment; then 4445 1.1 christos it serves as a list of extra makefiles for the sub-@code{make} to read 4446 1.1 christos before the usual or specified ones. @xref{MAKEFILES Variable, ,The 4447 1.1 christos Variable @code{MAKEFILES}}.@refill 4448 1.1 christos 4449 1.1 christos @node Options/Recursion, -w Option, Variables/Recursion, Recursion 4450 1.1 christos @subsection Communicating Options to a Sub-@code{make} 4451 1.1 christos @cindex options, and recursion 4452 1.1 christos @cindex recursion, and options 4453 1.1 christos 4454 1.1 christos @vindex MAKEFLAGS 4455 1.1 christos Flags such as @samp{-s} and @samp{-k} are passed automatically to the 4456 1.1 christos sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is 4457 1.1 christos set up automatically by @code{make} to contain the flag letters that 4458 1.1 christos @code{make} received. Thus, if you do @w{@samp{make -ks}} then 4459 1.1 christos @code{MAKEFLAGS} gets the value @samp{ks}.@refill 4460 1.1 christos 4461 1.1 christos As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS} 4462 1.1 christos in its environment. In response, it takes the flags from that value and 4463 1.1 christos processes them as if they had been given as arguments. 4464 1.1 christos @xref{Options Summary, ,Summary of Options}. 4465 1.1 christos 4466 1.1 christos @cindex command line variable definitions, and recursion 4467 1.1 christos @cindex variables, command line, and recursion 4468 1.1 christos @cindex recursion, and command line variable definitions 4469 1.1 christos Likewise variables defined on the command line are passed to the 4470 1.1 christos sub-@code{make} through @code{MAKEFLAGS}. Words in the value of 4471 1.1 christos @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable 4472 1.1 christos definitions just as if they appeared on the command line. 4473 1.1 christos @xref{Overriding, ,Overriding Variables}. 4474 1.1 christos 4475 1.1 christos @cindex @code{-C}, and recursion 4476 1.1 christos @cindex @code{-f}, and recursion 4477 1.1 christos @cindex @code{-o}, and recursion 4478 1.1 christos @cindex @code{-W}, and recursion 4479 1.1 christos @cindex @code{--directory}, and recursion 4480 1.1 christos @cindex @code{--file}, and recursion 4481 1.1 christos @cindex @code{--old-file}, and recursion 4482 1.1 christos @cindex @code{--assume-old}, and recursion 4483 1.1 christos @cindex @code{--assume-new}, and recursion 4484 1.1 christos @cindex @code{--new-file}, and recursion 4485 1.1 christos @cindex recursion, and @code{-C} 4486 1.1 christos @cindex recursion, and @code{-f} 4487 1.1 christos @cindex recursion, and @code{-o} 4488 1.1 christos @cindex recursion, and @code{-W} 4489 1.1 christos The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put 4490 1.1 christos into @code{MAKEFLAGS}; these options are not passed down.@refill 4491 1.1 christos 4492 1.1 christos @cindex @code{-j}, and recursion 4493 1.1 christos @cindex @code{--jobs}, and recursion 4494 1.1 christos @cindex recursion, and @code{-j} 4495 1.1 christos @cindex job slots, and recursion 4496 1.1 christos The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}). 4497 1.1 christos If you set it to some numeric value @samp{N} and your operating system 4498 1.1 christos supports it (most any UNIX system will; others typically won't), the 4499 1.1 christos parent @code{make} and all the sub-@code{make}s will communicate to 4500 1.1 christos ensure that there are only @samp{N} jobs running at the same time 4501 1.1 christos between them all. Note that any job that is marked recursive 4502 1.1 christos (@pxref{Instead of Execution, ,Instead of Executing the Commands}) 4503 1.1 christos doesn't count against the total jobs (otherwise we could get @samp{N} 4504 1.1 christos sub-@code{make}s running and have no slots left over for any real work!) 4505 1.1 christos 4506 1.1 christos If your operating system doesn't support the above communication, then 4507 1.1 christos @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you 4508 1.1 christos specified. This is because if the @w{@samp{-j}} option were passed down 4509 1.1 christos to sub-@code{make}s, you would get many more jobs running in parallel 4510 1.1 christos than you asked for. If you give @samp{-j} with no numeric argument, 4511 1.1 christos meaning to run as many jobs as possible in parallel, this is passed 4512 1.1 christos down, since multiple infinities are no more than one.@refill 4513 1.1 christos 4514 1.1 christos If you do not want to pass the other flags down, you must change the 4515 1.1 christos value of @code{MAKEFLAGS}, like this: 4516 1.1 christos 4517 1.1 christos @example 4518 1.1 christos subsystem: 4519 1.1 christos cd subdir && $(MAKE) MAKEFLAGS= 4520 1.1 christos @end example 4521 1.1 christos 4522 1.1 christos @vindex MAKEOVERRIDES 4523 1.1 christos The command line variable definitions really appear in the variable 4524 1.1 christos @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this 4525 1.1 christos variable. If you do want to pass flags down normally, but don't want to 4526 1.1 christos pass down the command line variable definitions, you can reset 4527 1.1 christos @code{MAKEOVERRIDES} to empty, like this: 4528 1.1 christos 4529 1.1 christos @example 4530 1.1 christos MAKEOVERRIDES = 4531 1.1 christos @end example 4532 1.1 christos 4533 1.1 christos @noindent 4534 1.1 christos @cindex Arg list too long 4535 1.1 christos @cindex E2BIG 4536 1.1 christos This is not usually useful to do. However, some systems have a small 4537 1.1 christos fixed limit on the size of the environment, and putting so much 4538 1.1 christos information into the value of @code{MAKEFLAGS} can exceed it. If you 4539 1.1 christos see the error message @samp{Arg list too long}, this may be the problem. 4540 1.1 christos @findex .POSIX 4541 1.1 christos @cindex POSIX.2 4542 1.1 christos (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does 4543 1.1 christos not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears 4544 1.1 christos in the makefile. You probably do not care about this.) 4545 1.1 christos 4546 1.1 christos @vindex MFLAGS 4547 1.1 christos A similar variable @code{MFLAGS} exists also, for historical 4548 1.1 christos compatibility. It has the same value as @code{MAKEFLAGS} except that it 4549 1.1 christos does not contain the command line variable definitions, and it always 4550 1.1 christos begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a 4551 1.1 christos hyphen only when it begins with an option that has no single-letter 4552 1.1 christos version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was 4553 1.1 christos traditionally used explicitly in the recursive @code{make} command, like 4554 1.1 christos this: 4555 1.1 christos 4556 1.1 christos @example 4557 1.1 christos subsystem: 4558 1.1 christos cd subdir && $(MAKE) $(MFLAGS) 4559 1.1 christos @end example 4560 1.1 christos 4561 1.1 christos @noindent 4562 1.1 christos but now @code{MAKEFLAGS} makes this usage redundant. If you want your 4563 1.1 christos makefiles to be compatible with old @code{make} programs, use this 4564 1.1 christos technique; it will work fine with more modern @code{make} versions too. 4565 1.1 christos 4566 1.1 christos @cindex setting options from environment 4567 1.1 christos @cindex options, setting from environment 4568 1.1 christos @cindex setting options in makefiles 4569 1.1 christos @cindex options, setting in makefiles 4570 1.1 christos The @code{MAKEFLAGS} variable can also be useful if you want to have 4571 1.1 christos certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of 4572 1.1 christos Options}), set each time you run @code{make}. You simply put a value for 4573 1.1 christos @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in 4574 1.1 christos a makefile, to specify additional flags that should also be in effect for 4575 1.1 christos that makefile. (Note that you cannot use @code{MFLAGS} this way. That 4576 1.1 christos variable is set only for compatibility; @code{make} does not interpret a 4577 1.1 christos value you set for it in any way.) 4578 1.1 christos 4579 1.1 christos When @code{make} interprets the value of @code{MAKEFLAGS} (either from the 4580 1.1 christos environment or from a makefile), it first prepends a hyphen if the value 4581 1.1 christos does not already begin with one. Then it chops the value into words 4582 1.1 christos separated by blanks, and parses these words as if they were options given 4583 1.1 christos on the command line (except that @samp{-C}, @samp{-f}, @samp{-h}, 4584 1.1 christos @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there 4585 1.1 christos is no error for an invalid option). 4586 1.1 christos 4587 1.1 christos If you do put @code{MAKEFLAGS} in your environment, you should be sure not 4588 1.1 christos to include any options that will drastically affect the actions of 4589 1.1 christos @code{make} and undermine the purpose of makefiles and of @code{make} 4590 1.1 christos itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if 4591 1.1 christos put in one of these variables, could have disastrous consequences and would 4592 1.1 christos certainly have at least surprising and probably annoying effects.@refill 4593 1.1 christos 4594 1.1 christos @node -w Option, , Options/Recursion, Recursion 4595 1.1 christos @subsection The @samp{--print-directory} Option 4596 1.1 christos @cindex directories, printing them 4597 1.1 christos @cindex printing directories 4598 1.1 christos @cindex recursion, and printing directories 4599 1.1 christos 4600 1.1 christos If you use several levels of recursive @code{make} invocations, the 4601 1.1 christos @samp{-w} or @w{@samp{--print-directory}} option can make the output a 4602 1.1 christos lot easier to understand by showing each directory as @code{make} 4603 1.1 christos starts processing it and as @code{make} finishes processing it. For 4604 1.1 christos example, if @samp{make -w} is run in the directory @file{/u/gnu/make}, 4605 1.1 christos @code{make} will print a line of the form:@refill 4606 1.1 christos 4607 1.1 christos @example 4608 1.1 christos make: Entering directory `/u/gnu/make'. 4609 1.1 christos @end example 4610 1.1 christos 4611 1.1 christos @noindent 4612 1.1 christos before doing anything else, and a line of the form: 4613 1.1 christos 4614 1.1 christos @example 4615 1.1 christos make: Leaving directory `/u/gnu/make'. 4616 1.1 christos @end example 4617 1.1 christos 4618 1.1 christos @noindent 4619 1.1 christos when processing is completed. 4620 1.1 christos 4621 1.1 christos @cindex @code{-C}, and @code{-w} 4622 1.1 christos @cindex @code{--directory}, and @code{--print-directory} 4623 1.1 christos @cindex recursion, and @code{-w} 4624 1.1 christos @cindex @code{-w}, and @code{-C} 4625 1.1 christos @cindex @code{-w}, and recursion 4626 1.1 christos @cindex @code{--print-directory}, and @code{--directory} 4627 1.1 christos @cindex @code{--print-directory}, and recursion 4628 1.1 christos @cindex @code{--no-print-directory} 4629 1.1 christos @cindex @code{--print-directory}, disabling 4630 1.1 christos @cindex @code{-w}, disabling 4631 1.1 christos Normally, you do not need to specify this option because @samp{make} 4632 1.1 christos does it for you: @samp{-w} is turned on automatically when you use the 4633 1.1 christos @samp{-C} option, and in sub-@code{make}s. @code{make} will not 4634 1.1 christos automatically turn on @samp{-w} if you also use @samp{-s}, which says to 4635 1.1 christos be silent, or if you use @samp{--no-print-directory} to explicitly 4636 1.1 christos disable it. 4637 1.1 christos 4638 1.1 christos @node Sequences, Empty Commands, Recursion, Commands 4639 1.1 christos @section Defining Canned Command Sequences 4640 1.1 christos @cindex sequences of commands 4641 1.1 christos @cindex commands, sequences of 4642 1.1 christos 4643 1.1 christos When the same sequence of commands is useful in making various targets, you 4644 1.1 christos can define it as a canned sequence with the @code{define} directive, and 4645 1.1 christos refer to the canned sequence from the rules for those targets. The canned 4646 1.1 christos sequence is actually a variable, so the name must not conflict with other 4647 1.1 christos variable names. 4648 1.1 christos 4649 1.1 christos Here is an example of defining a canned sequence of commands: 4650 1.1 christos 4651 1.1 christos @example 4652 1.1 christos define run-yacc 4653 1.1 christos yacc $(firstword $^) 4654 1.1 christos mv y.tab.c $@@ 4655 1.1 christos endef 4656 1.1 christos @end example 4657 1.1 christos @cindex @code{yacc} 4658 1.1 christos 4659 1.1 christos @noindent 4660 1.1 christos Here @code{run-yacc} is the name of the variable being defined; 4661 1.1 christos @code{endef} marks the end of the definition; the lines in between are the 4662 1.1 christos commands. The @code{define} directive does not expand variable references 4663 1.1 christos and function calls in the canned sequence; the @samp{$} characters, 4664 1.1 christos parentheses, variable names, and so on, all become part of the value of the 4665 1.1 christos variable you are defining. 4666 1.1 christos @xref{Defining, ,Defining Variables Verbatim}, 4667 1.1 christos for a complete explanation of @code{define}. 4668 1.1 christos 4669 1.1 christos The first command in this example runs Yacc on the first prerequisite of 4670 1.1 christos whichever rule uses the canned sequence. The output file from Yacc is 4671 1.1 christos always named @file{y.tab.c}. The second command moves the output to the 4672 1.1 christos rule's target file name. 4673 1.1 christos 4674 1.1 christos To use the canned sequence, substitute the variable into the commands of a 4675 1.1 christos rule. You can substitute it like any other variable 4676 1.1 christos (@pxref{Reference, ,Basics of Variable References}). 4677 1.1 christos Because variables defined by @code{define} are recursively expanded 4678 1.1 christos variables, all the variable references you wrote inside the @code{define} 4679 1.1 christos are expanded now. For example: 4680 1.1 christos 4681 1.1 christos @example 4682 1.1 christos foo.c : foo.y 4683 1.1 christos $(run-yacc) 4684 1.1 christos @end example 4685 1.1 christos 4686 1.1 christos @noindent 4687 1.1 christos @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in 4688 1.1 christos @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill 4689 1.1 christos 4690 1.1 christos This is a realistic example, but this particular one is not needed in 4691 1.1 christos practice because @code{make} has an implicit rule to figure out these 4692 1.1 christos commands based on the file names involved 4693 1.1 christos (@pxref{Implicit Rules, ,Using Implicit Rules}). 4694 1.1 christos 4695 1.1 christos @cindex @@, and @code{define} 4696 1.1 christos @cindex -, and @code{define} 4697 1.1 christos @cindex +, and @code{define} 4698 1.1 christos In command execution, each line of a canned sequence is treated just as 4699 1.1 christos if the line appeared on its own in the rule, preceded by a tab. In 4700 1.1 christos particular, @code{make} invokes a separate subshell for each line. You 4701 1.1 christos can use the special prefix characters that affect command lines 4702 1.1 christos (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence. 4703 1.1 christos @xref{Commands, ,Writing the Commands in Rules}. 4704 1.1 christos For example, using this canned sequence: 4705 1.1 christos 4706 1.1 christos @example 4707 1.1 christos define frobnicate 4708 1.1 christos @@echo "frobnicating target $@@" 4709 1.1 christos frob-step-1 $< -o $@@-step-1 4710 1.1 christos frob-step-2 $@@-step-1 -o $@@ 4711 1.1 christos endef 4712 1.1 christos @end example 4713 1.1 christos 4714 1.1 christos @noindent 4715 1.1 christos @code{make} will not echo the first line, the @code{echo} command. 4716 1.1 christos But it @emph{will} echo the following two command lines. 4717 1.1 christos 4718 1.1 christos On the other hand, prefix characters on the command line that refers to 4719 1.1 christos a canned sequence apply to every line in the sequence. So the rule: 4720 1.1 christos 4721 1.1 christos @example 4722 1.1 christos frob.out: frob.in 4723 1.1 christos @@$(frobnicate) 4724 1.1 christos @end example 4725 1.1 christos 4726 1.1 christos @noindent 4727 1.1 christos does not echo @emph{any} commands. 4728 1.1 christos (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.) 4729 1.1 christos 4730 1.1 christos @node Empty Commands, , Sequences, Commands 4731 1.1 christos @section Using Empty Commands 4732 1.1 christos @cindex empty commands 4733 1.1 christos @cindex commands, empty 4734 1.1 christos 4735 1.1 christos It is sometimes useful to define commands which do nothing. This is done 4736 1.1 christos simply by giving a command that consists of nothing but whitespace. For 4737 1.1 christos example: 4738 1.1 christos 4739 1.1 christos @example 4740 1.1 christos target: ; 4741 1.1 christos @end example 4742 1.1 christos 4743 1.1 christos @noindent 4744 1.1 christos defines an empty command string for @file{target}. You could also use a 4745 1.1 christos line beginning with a tab character to define an empty command string, 4746 1.1 christos but this would be confusing because such a line looks empty. 4747 1.1 christos 4748 1.1 christos @findex .DEFAULT@r{, and empty commands} 4749 1.1 christos You may be wondering why you would want to define a command string that 4750 1.1 christos does nothing. The only reason this is useful is to prevent a target 4751 1.1 christos from getting implicit commands (from implicit rules or the 4752 1.1 christos @code{.DEFAULT} special target; @pxref{Implicit Rules} and 4753 1.1 christos @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill 4754 1.1 christos 4755 1.1 christos @c !!! another reason is for canonical stamp files: 4756 1.1 christos @ignore 4757 1.1 christos @example 4758 1.1 christos foo: stamp-foo ; 4759 1.1 christos stamp-foo: foo.in 4760 1.1 christos create foo frm foo.in 4761 1.1 christos touch $@ 4762 1.1 christos @end example 4763 1.1 christos @end ignore 4764 1.1 christos 4765 1.1 christos You may be inclined to define empty command strings for targets that are 4766 1.1 christos not actual files, but only exist so that their prerequisites can be 4767 1.1 christos remade. However, this is not the best way to do that, because the 4768 1.1 christos prerequisites may not be remade properly if the target file actually does exist. 4769 1.1 christos @xref{Phony Targets, ,Phony Targets}, for a better way to do this. 4770 1.1 christos 4771 1.1 christos @node Using Variables, Conditionals, Commands, Top 4772 1.1 christos @chapter How to Use Variables 4773 1.1 christos @cindex variable 4774 1.1 christos @cindex value 4775 1.1 christos @cindex recursive variable expansion 4776 1.1 christos @cindex simple variable expansion 4777 1.1 christos 4778 1.1 christos A @dfn{variable} is a name defined in a makefile to represent a string 4779 1.1 christos of text, called the variable's @dfn{value}. These values are 4780 1.1 christos substituted by explicit request into targets, prerequisites, commands, 4781 1.1 christos and other parts of the makefile. (In some other versions of @code{make}, 4782 1.1 christos variables are called @dfn{macros}.) 4783 1.1 christos @cindex macro 4784 1.1 christos 4785 1.1 christos Variables and functions in all parts of a makefile are expanded when 4786 1.1 christos read, except for the shell commands in rules, the right-hand sides of 4787 1.1 christos variable definitions using @samp{=}, and the bodies of variable 4788 1.1 christos definitions using the @code{define} directive.@refill 4789 1.1 christos 4790 1.1 christos Variables can represent lists of file names, options to pass to compilers, 4791 1.1 christos programs to run, directories to look in for source files, directories to 4792 1.1 christos write output in, or anything else you can imagine. 4793 1.1 christos 4794 1.1 christos A variable name may be any sequence of characters not containing @samp{:}, 4795 1.1 christos @samp{#}, @samp{=}, or leading or trailing whitespace. However, 4796 1.1 christos variable names containing characters other than letters, numbers, and 4797 1.1 christos underscores should be avoided, as they may be given special meanings in the 4798 1.1 christos future, and with some shells they cannot be passed through the environment to a 4799 1.1 christos sub-@code{make} 4800 1.1 christos (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}). 4801 1.1 christos 4802 1.1 christos Variable names are case-sensitive. The names @samp{foo}, @samp{FOO}, 4803 1.1 christos and @samp{Foo} all refer to different variables. 4804 1.1 christos 4805 1.1 christos It is traditional to use upper case letters in variable names, but we 4806 1.1 christos recommend using lower case letters for variable names that serve internal 4807 1.1 christos purposes in the makefile, and reserving upper case for parameters that 4808 1.1 christos control implicit rules or for parameters that the user should override with 4809 1.1 christos command options (@pxref{Overriding, ,Overriding Variables}). 4810 1.1 christos 4811 1.1 christos A few variables have names that are a single punctuation character or 4812 1.1 christos just a few characters. These are the @dfn{automatic variables}, and 4813 1.1 christos they have particular specialized uses. @xref{Automatic Variables}. 4814 1.1 christos 4815 1.1 christos @menu 4816 1.1 christos * Reference:: How to use the value of a variable. 4817 1.1 christos * Flavors:: Variables come in two flavors. 4818 1.1 christos * Advanced:: Advanced features for referencing a variable. 4819 1.1 christos * Values:: All the ways variables get their values. 4820 1.1 christos * Setting:: How to set a variable in the makefile. 4821 1.1 christos * Appending:: How to append more text to the old value 4822 1.1 christos of a variable. 4823 1.1 christos * Override Directive:: How to set a variable in the makefile even if 4824 1.1 christos the user has set it with a command argument. 4825 1.1 christos * Defining:: An alternate way to set a variable 4826 1.1 christos to a verbatim string. 4827 1.1 christos * Environment:: Variable values can come from the environment. 4828 1.1 christos * Target-specific:: Variable values can be defined on a per-target 4829 1.1 christos basis. 4830 1.1 christos * Pattern-specific:: Target-specific variable values can be applied 4831 1.1 christos to a group of targets that match a pattern. 4832 1.1 christos @end menu 4833 1.1 christos 4834 1.1 christos @node Reference, Flavors, Using Variables, Using Variables 4835 1.1 christos @section Basics of Variable References 4836 1.1 christos @cindex variables, how to reference 4837 1.1 christos @cindex reference to variables 4838 1.1 christos @cindex @code{$}, in variable reference 4839 1.1 christos @cindex dollar sign (@code{$}), in variable reference 4840 1.1 christos 4841 1.1 christos To substitute a variable's value, write a dollar sign followed by the name 4842 1.1 christos of the variable in parentheses or braces: either @samp{$(foo)} or 4843 1.1 christos @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This 4844 1.1 christos special significance of @samp{$} is why you must write @samp{$$} to have 4845 1.1 christos the effect of a single dollar sign in a file name or command. 4846 1.1 christos 4847 1.1 christos Variable references can be used in any context: targets, prerequisites, 4848 1.1 christos commands, most directives, and new variable values. Here is an 4849 1.1 christos example of a common case, where a variable holds the names of all the 4850 1.1 christos object files in a program: 4851 1.1 christos 4852 1.1 christos @example 4853 1.1 christos @group 4854 1.1 christos objects = program.o foo.o utils.o 4855 1.1 christos program : $(objects) 4856 1.1 christos cc -o program $(objects) 4857 1.1 christos 4858 1.1 christos $(objects) : defs.h 4859 1.1 christos @end group 4860 1.1 christos @end example 4861 1.1 christos 4862 1.1 christos Variable references work by strict textual substitution. Thus, the rule 4863 1.1 christos 4864 1.1 christos @example 4865 1.1 christos @group 4866 1.1 christos foo = c 4867 1.1 christos prog.o : prog.$(foo) 4868 1.1 christos $(foo)$(foo) -$(foo) prog.$(foo) 4869 1.1 christos @end group 4870 1.1 christos @end example 4871 1.1 christos 4872 1.1 christos @noindent 4873 1.1 christos could be used to compile a C program @file{prog.c}. Since spaces before 4874 1.1 christos the variable value are ignored in variable assignments, the value of 4875 1.1 christos @code{foo} is precisely @samp{c}. (Don't actually write your makefiles 4876 1.1 christos this way!) 4877 1.1 christos 4878 1.1 christos A dollar sign followed by a character other than a dollar sign, 4879 1.1 christos open-parenthesis or open-brace treats that single character as the 4880 1.1 christos variable name. Thus, you could reference the variable @code{x} with 4881 1.1 christos @samp{$x}. However, this practice is strongly discouraged, except in 4882 1.1 christos the case of the automatic variables (@pxref{Automatic Variables}). 4883 1.1 christos 4884 1.1 christos @node Flavors, Advanced, Reference, Using Variables 4885 1.1 christos @section The Two Flavors of Variables 4886 1.1 christos @cindex flavors of variables 4887 1.1 christos @cindex recursive variable expansion 4888 1.1 christos @cindex variables, flavors 4889 1.1 christos @cindex recursively expanded variables 4890 1.1 christos @cindex variables, recursively expanded 4891 1.1 christos 4892 1.1 christos There are two ways that a variable in GNU @code{make} can have a value; 4893 1.1 christos we call them the two @dfn{flavors} of variables. The two flavors are 4894 1.1 christos distinguished in how they are defined and in what they do when expanded. 4895 1.1 christos 4896 1.1 christos @cindex = 4897 1.1 christos The first flavor of variable is a @dfn{recursively expanded} variable. 4898 1.1 christos Variables of this sort are defined by lines using @samp{=} 4899 1.1 christos (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive 4900 1.1 christos (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify 4901 1.1 christos is installed verbatim; if it contains references to other variables, 4902 1.1 christos these references are expanded whenever this variable is substituted (in 4903 1.1 christos the course of expanding some other string). When this happens, it is 4904 1.1 christos called @dfn{recursive expansion}.@refill 4905 1.1 christos 4906 1.1 christos For example, 4907 1.1 christos 4908 1.1 christos @example 4909 1.1 christos foo = $(bar) 4910 1.1 christos bar = $(ugh) 4911 1.1 christos ugh = Huh? 4912 1.1 christos 4913 1.1 christos all:;echo $(foo) 4914 1.1 christos @end example 4915 1.1 christos 4916 1.1 christos @noindent 4917 1.1 christos will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which 4918 1.1 christos expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill 4919 1.1 christos 4920 1.1 christos This flavor of variable is the only sort supported by other versions of 4921 1.1 christos @code{make}. It has its advantages and its disadvantages. An advantage 4922 1.1 christos (most would say) is that: 4923 1.1 christos 4924 1.1 christos @example 4925 1.1 christos CFLAGS = $(include_dirs) -O 4926 1.1 christos include_dirs = -Ifoo -Ibar 4927 1.1 christos @end example 4928 1.1 christos 4929 1.1 christos @noindent 4930 1.1 christos will do what was intended: when @samp{CFLAGS} is expanded in a command, 4931 1.1 christos it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you 4932 1.1 christos cannot append something on the end of a variable, as in 4933 1.1 christos 4934 1.1 christos @example 4935 1.1 christos CFLAGS = $(CFLAGS) -O 4936 1.1 christos @end example 4937 1.1 christos 4938 1.1 christos @noindent 4939 1.1 christos because it will cause an infinite loop in the variable expansion. 4940 1.1 christos (Actually @code{make} detects the infinite loop and reports an error.) 4941 1.1 christos @cindex loops in variable expansion 4942 1.1 christos @cindex variables, loops in expansion 4943 1.1 christos 4944 1.1 christos Another disadvantage is that any functions 4945 1.1 christos (@pxref{Functions, ,Functions for Transforming Text}) 4946 1.1 christos referenced in the definition will be executed every time the variable is 4947 1.1 christos expanded. This makes @code{make} run slower; worse, it causes the 4948 1.1 christos @code{wildcard} and @code{shell} functions to give unpredictable results 4949 1.1 christos because you cannot easily control when they are called, or even how many 4950 1.1 christos times. 4951 1.1 christos 4952 1.1 christos To avoid all the problems and inconveniences of recursively expanded 4953 1.1 christos variables, there is another flavor: simply expanded variables. 4954 1.1 christos 4955 1.1 christos @cindex simply expanded variables 4956 1.1 christos @cindex variables, simply expanded 4957 1.1 christos @cindex := 4958 1.1 christos @dfn{Simply expanded variables} are defined by lines using @samp{:=} 4959 1.1 christos (@pxref{Setting, ,Setting Variables}). 4960 1.1 christos The value of a simply expanded variable is scanned 4961 1.1 christos once and for all, expanding any references to other variables and 4962 1.1 christos functions, when the variable is defined. The actual value of the simply 4963 1.1 christos expanded variable is the result of expanding the text that you write. 4964 1.1 christos It does not contain any references to other variables; it contains their 4965 1.1 christos values @emph{as of the time this variable was defined}. Therefore, 4966 1.1 christos 4967 1.1 christos @example 4968 1.1 christos x := foo 4969 1.1 christos y := $(x) bar 4970 1.1 christos x := later 4971 1.1 christos @end example 4972 1.1 christos 4973 1.1 christos @noindent 4974 1.1 christos is equivalent to 4975 1.1 christos 4976 1.1 christos @example 4977 1.1 christos y := foo bar 4978 1.1 christos x := later 4979 1.1 christos @end example 4980 1.1 christos 4981 1.1 christos When a simply expanded variable is referenced, its value is substituted 4982 1.1 christos verbatim. 4983 1.1 christos 4984 1.1 christos Here is a somewhat more complicated example, illustrating the use of 4985 1.1 christos @samp{:=} in conjunction with the @code{shell} function. 4986 1.1 christos (@xref{Shell Function, , The @code{shell} Function}.) This example 4987 1.1 christos also shows use of the variable @code{MAKELEVEL}, which is changed 4988 1.1 christos when it is passed down from level to level. 4989 1.1 christos (@xref{Variables/Recursion, , Communicating Variables to a 4990 1.1 christos Sub-@code{make}}, for information about @code{MAKELEVEL}.) 4991 1.1 christos 4992 1.1 christos @vindex MAKELEVEL 4993 1.1 christos @vindex MAKE 4994 1.1 christos @example 4995 1.1 christos @group 4996 1.1 christos ifeq (0,$@{MAKELEVEL@}) 4997 1.1 christos whoami := $(shell whoami) 4998 1.1 christos host-type := $(shell arch) 4999 1.1 christos MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@} 5000 1.1 christos endif 5001 1.1 christos @end group 5002 1.1 christos @end example 5003 1.1 christos 5004 1.1 christos @noindent 5005 1.1 christos An advantage of this use of @samp{:=} is that a typical 5006 1.1 christos `descend into a directory' command then looks like this: 5007 1.1 christos 5008 1.1 christos @example 5009 1.1 christos @group 5010 1.1 christos $@{subdirs@}: 5011 1.1 christos $@{MAKE@} -C $@@ all 5012 1.1 christos @end group 5013 1.1 christos @end example 5014 1.1 christos 5015 1.1 christos Simply expanded variables generally make complicated makefile programming 5016 1.1 christos more predictable because they work like variables in most programming 5017 1.1 christos languages. They allow you to redefine a variable using its own value (or 5018 1.1 christos its value processed in some way by one of the expansion functions) and to 5019 1.1 christos use the expansion functions much more efficiently 5020 1.1 christos (@pxref{Functions, ,Functions for Transforming Text}). 5021 1.1 christos 5022 1.1 christos @cindex spaces, in variable values 5023 1.1 christos @cindex whitespace, in variable values 5024 1.1 christos @cindex variables, spaces in values 5025 1.1 christos You can also use them to introduce controlled leading whitespace into 5026 1.1 christos variable values. Leading whitespace characters are discarded from your 5027 1.1 christos input before substitution of variable references and function calls; 5028 1.1 christos this means you can include leading spaces in a variable value by 5029 1.1 christos protecting them with variable references, like this: 5030 1.1 christos 5031 1.1 christos @example 5032 1.1 christos nullstring := 5033 1.1 christos space := $(nullstring) # end of the line 5034 1.1 christos @end example 5035 1.1 christos 5036 1.1 christos @noindent 5037 1.1 christos Here the value of the variable @code{space} is precisely one space. The 5038 1.1 christos comment @w{@samp{# end of the line}} is included here just for clarity. 5039 1.1 christos Since trailing space characters are @emph{not} stripped from variable 5040 1.1 christos values, just a space at the end of the line would have the same effect 5041 1.1 christos (but be rather hard to read). If you put whitespace at the end of a 5042 1.1 christos variable value, it is a good idea to put a comment like that at the end 5043 1.1 christos of the line to make your intent clear. Conversely, if you do @emph{not} 5044 1.1 christos want any whitespace characters at the end of your variable value, you 5045 1.1 christos must remember not to put a random comment on the end of the line after 5046 1.1 christos some whitespace, such as this: 5047 1.1 christos 5048 1.1 christos @example 5049 1.1 christos dir := /foo/bar # directory to put the frobs in 5050 1.1 christos @end example 5051 1.1 christos 5052 1.1 christos @noindent 5053 1.1 christos Here the value of the variable @code{dir} is @w{@samp{/foo/bar }} 5054 1.1 christos (with four trailing spaces), which was probably not the intention. 5055 1.1 christos (Imagine something like @w{@samp{$(dir)/file}} with this definition!) 5056 1.1 christos 5057 1.1 christos @cindex conditional variable assignment 5058 1.1 christos @cindex variables, conditional assignment 5059 1.1 christos @cindex ?= 5060 1.1 christos There is another assignment operator for variables, @samp{?=}. This 5061 1.1 christos is called a conditional variable assignment operator, because it only 5062 1.1 christos has an effect if the variable is not yet defined. This statement: 5063 1.1 christos 5064 1.1 christos @example 5065 1.1 christos FOO ?= bar 5066 1.1 christos @end example 5067 1.1 christos 5068 1.1 christos @noindent 5069 1.1 christos is exactly equivalent to this 5070 1.1 christos (@pxref{Origin Function, ,The @code{origin} Function}): 5071 1.1 christos 5072 1.1 christos @example 5073 1.1 christos ifeq ($(origin FOO), undefined) 5074 1.1 christos FOO = bar 5075 1.1 christos endif 5076 1.1 christos @end example 5077 1.1 christos 5078 1.1 christos Note that a variable set to an empty value is still defined, so 5079 1.1 christos @samp{?=} will not set that variable. 5080 1.1 christos 5081 1.1 christos @node Advanced, Values, Flavors, Using Variables 5082 1.1 christos @section Advanced Features for Reference to Variables 5083 1.1 christos @cindex reference to variables 5084 1.1 christos 5085 1.1 christos This section describes some advanced features you can use to reference 5086 1.1 christos variables in more flexible ways. 5087 1.1 christos 5088 1.1 christos @menu 5089 1.1 christos * Substitution Refs:: Referencing a variable with 5090 1.1 christos substitutions on the value. 5091 1.1 christos * Computed Names:: Computing the name of the variable to refer to. 5092 1.1 christos @end menu 5093 1.1 christos 5094 1.1 christos @node Substitution Refs, Computed Names, Advanced, Advanced 5095 1.1 christos @subsection Substitution References 5096 1.1 christos @cindex modified variable reference 5097 1.1 christos @cindex substitution variable reference 5098 1.1 christos @cindex variables, modified reference 5099 1.1 christos @cindex variables, substitution reference 5100 1.1 christos 5101 1.1 christos @cindex variables, substituting suffix in 5102 1.1 christos @cindex suffix, substituting in variables 5103 1.1 christos A @dfn{substitution reference} substitutes the value of a variable with 5104 1.1 christos alterations that you specify. It has the form 5105 1.1 christos @samp{$(@var{var}:@var{a}=@var{b})} (or 5106 1.1 christos @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value 5107 1.1 christos of the variable @var{var}, replace every @var{a} at the end of a word with 5108 1.1 christos @var{b} in that value, and substitute the resulting string. 5109 1.1 christos 5110 1.1 christos When we say ``at the end of a word'', we mean that @var{a} must appear 5111 1.1 christos either followed by whitespace or at the end of the value in order to be 5112 1.1 christos replaced; other occurrences of @var{a} in the value are unaltered. For 5113 1.1 christos example:@refill 5114 1.1 christos 5115 1.1 christos @example 5116 1.1 christos foo := a.o b.o c.o 5117 1.1 christos bar := $(foo:.o=.c) 5118 1.1 christos @end example 5119 1.1 christos 5120 1.1 christos @noindent 5121 1.1 christos sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}. 5122 1.1 christos 5123 1.1 christos A substitution reference is actually an abbreviation for use of the 5124 1.1 christos @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide 5125 1.1 christos substitution references as well as @code{patsubst} for compatibility with 5126 1.1 christos other implementations of @code{make}. 5127 1.1 christos 5128 1.1 christos @findex patsubst 5129 1.1 christos Another type of substitution reference lets you use the full power of 5130 1.1 christos the @code{patsubst} function. It has the same form 5131 1.1 christos @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now 5132 1.1 christos @var{a} must contain a single @samp{%} character. This case is 5133 1.1 christos equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}. 5134 1.1 christos @xref{Text Functions, ,Functions for String Substitution and Analysis}, 5135 1.1 christos for a description of the @code{patsubst} function.@refill 5136 1.1 christos 5137 1.1 christos @example 5138 1.1 christos @group 5139 1.1 christos @exdent For example: 5140 1.1 christos 5141 1.1 christos foo := a.o b.o c.o 5142 1.1 christos bar := $(foo:%.o=%.c) 5143 1.1 christos @end group 5144 1.1 christos @end example 5145 1.1 christos 5146 1.1 christos @noindent 5147 1.1 christos sets @samp{bar} to @samp{a.c b.c c.c}. 5148 1.1 christos 5149 1.1 christos @node Computed Names, , Substitution Refs, Advanced 5150 1.1 christos @subsection Computed Variable Names 5151 1.1 christos @cindex nested variable reference 5152 1.1 christos @cindex computed variable name 5153 1.1 christos @cindex variables, computed names 5154 1.1 christos @cindex variables, nested references 5155 1.1 christos @cindex variables, @samp{$} in name 5156 1.1 christos @cindex @code{$}, in variable name 5157 1.1 christos @cindex dollar sign (@code{$}), in variable name 5158 1.1 christos 5159 1.1 christos Computed variable names are a complicated concept needed only for 5160 1.1 christos sophisticated makefile programming. For most purposes you need not 5161 1.1 christos consider them, except to know that making a variable with a dollar sign 5162 1.1 christos in its name might have strange results. However, if you are the type 5163 1.1 christos that wants to understand everything, or you are actually interested in 5164 1.1 christos what they do, read on. 5165 1.1 christos 5166 1.1 christos Variables may be referenced inside the name of a variable. This is 5167 1.1 christos called a @dfn{computed variable name} or a @dfn{nested variable 5168 1.1 christos reference}. For example, 5169 1.1 christos 5170 1.1 christos @example 5171 1.1 christos x = y 5172 1.1 christos y = z 5173 1.1 christos a := $($(x)) 5174 1.1 christos @end example 5175 1.1 christos 5176 1.1 christos @noindent 5177 1.1 christos defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands 5178 1.1 christos to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands 5179 1.1 christos to @samp{z}. Here the name of the variable to reference is not stated 5180 1.1 christos explicitly; it is computed by expansion of @samp{$(x)}. The reference 5181 1.1 christos @samp{$(x)} here is nested within the outer variable reference. 5182 1.1 christos 5183 1.1 christos The previous example shows two levels of nesting, but any number of levels 5184 1.1 christos is possible. For example, here are three levels: 5185 1.1 christos 5186 1.1 christos @example 5187 1.1 christos x = y 5188 1.1 christos y = z 5189 1.1 christos z = u 5190 1.1 christos a := $($($(x))) 5191 1.1 christos @end example 5192 1.1 christos 5193 1.1 christos @noindent 5194 1.1 christos Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))} 5195 1.1 christos expands to @samp{$(y)} which in turn expands to @samp{z}; now we have 5196 1.1 christos @samp{$(z)}, which becomes @samp{u}. 5197 1.1 christos 5198 1.1 christos References to recursively-expanded variables within a variable name are 5199 1.1 christos reexpanded in the usual fashion. For example: 5200 1.1 christos 5201 1.1 christos @example 5202 1.1 christos x = $(y) 5203 1.1 christos y = z 5204 1.1 christos z = Hello 5205 1.1 christos a := $($(x)) 5206 1.1 christos @end example 5207 1.1 christos 5208 1.1 christos @noindent 5209 1.1 christos defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))} 5210 1.1 christos which becomes @samp{$(z)} which becomes @samp{Hello}. 5211 1.1 christos 5212 1.1 christos Nested variable references can also contain modified references and 5213 1.1 christos function invocations (@pxref{Functions, ,Functions for Transforming Text}), 5214 1.1 christos just like any other reference. 5215 1.1 christos For example, using the @code{subst} function 5216 1.1 christos (@pxref{Text Functions, ,Functions for String Substitution and Analysis}): 5217 1.1 christos 5218 1.1 christos @example 5219 1.1 christos @group 5220 1.1 christos x = variable1 5221 1.1 christos variable2 := Hello 5222 1.1 christos y = $(subst 1,2,$(x)) 5223 1.1 christos z = y 5224 1.1 christos a := $($($(z))) 5225 1.1 christos @end group 5226 1.1 christos @end example 5227 1.1 christos 5228 1.1 christos @noindent 5229 1.1 christos eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone 5230 1.1 christos would ever want to write a nested reference as convoluted as this one, but 5231 1.1 christos it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes 5232 1.1 christos @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from 5233 1.1 christos @code{x} and changes it by substitution to @samp{variable2}, so that the 5234 1.1 christos entire string becomes @samp{$(variable2)}, a simple variable reference 5235 1.1 christos whose value is @samp{Hello}.@refill 5236 1.1 christos 5237 1.1 christos A computed variable name need not consist entirely of a single variable 5238 1.1 christos reference. It can contain several variable references, as well as some 5239 1.1 christos invariant text. For example, 5240 1.1 christos 5241 1.1 christos @example 5242 1.1 christos @group 5243 1.1 christos a_dirs := dira dirb 5244 1.1 christos 1_dirs := dir1 dir2 5245 1.1 christos @end group 5246 1.1 christos 5247 1.1 christos @group 5248 1.1 christos a_files := filea fileb 5249 1.1 christos 1_files := file1 file2 5250 1.1 christos @end group 5251 1.1 christos 5252 1.1 christos @group 5253 1.1 christos ifeq "$(use_a)" "yes" 5254 1.1 christos a1 := a 5255 1.1 christos else 5256 1.1 christos a1 := 1 5257 1.1 christos endif 5258 1.1 christos @end group 5259 1.1 christos 5260 1.1 christos @group 5261 1.1 christos ifeq "$(use_dirs)" "yes" 5262 1.1 christos df := dirs 5263 1.1 christos else 5264 1.1 christos df := files 5265 1.1 christos endif 5266 1.1 christos 5267 1.1 christos dirs := $($(a1)_$(df)) 5268 1.1 christos @end group 5269 1.1 christos @end example 5270 1.1 christos 5271 1.1 christos @noindent 5272 1.1 christos will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs}, 5273 1.1 christos @code{a_files} or @code{1_files} depending on the settings of @code{use_a} 5274 1.1 christos and @code{use_dirs}.@refill 5275 1.1 christos 5276 1.1 christos Computed variable names can also be used in substitution references: 5277 1.1 christos 5278 1.1 christos @example 5279 1.1 christos @group 5280 1.1 christos a_objects := a.o b.o c.o 5281 1.1 christos 1_objects := 1.o 2.o 3.o 5282 1.1 christos 5283 1.1 christos sources := $($(a1)_objects:.o=.c) 5284 1.1 christos @end group 5285 1.1 christos @end example 5286 1.1 christos 5287 1.1 christos @noindent 5288 1.1 christos defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c}, 5289 1.1 christos depending on the value of @code{a1}. 5290 1.1 christos 5291 1.1 christos The only restriction on this sort of use of nested variable references 5292 1.1 christos is that they cannot specify part of the name of a function to be called. 5293 1.1 christos This is because the test for a recognized function name is done before 5294 1.1 christos the expansion of nested references. For example, 5295 1.1 christos 5296 1.1 christos @example 5297 1.1 christos @group 5298 1.1 christos ifdef do_sort 5299 1.1 christos func := sort 5300 1.1 christos else 5301 1.1 christos func := strip 5302 1.1 christos endif 5303 1.1 christos @end group 5304 1.1 christos 5305 1.1 christos @group 5306 1.1 christos bar := a d b g q c 5307 1.1 christos @end group 5308 1.1 christos 5309 1.1 christos @group 5310 1.1 christos foo := $($(func) $(bar)) 5311 1.1 christos @end group 5312 1.1 christos @end example 5313 1.1 christos 5314 1.1 christos @noindent 5315 1.1 christos attempts to give @samp{foo} the value of the variable @samp{sort a d b g 5316 1.1 christos q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c} 5317 1.1 christos as the argument to either the @code{sort} or the @code{strip} function. 5318 1.1 christos This restriction could be removed in the future if that change is shown 5319 1.1 christos to be a good idea. 5320 1.1 christos 5321 1.1 christos You can also use computed variable names in the left-hand side of a 5322 1.1 christos variable assignment, or in a @code{define} directive, as in: 5323 1.1 christos 5324 1.1 christos @example 5325 1.1 christos dir = foo 5326 1.1 christos $(dir)_sources := $(wildcard $(dir)/*.c) 5327 1.1 christos define $(dir)_print 5328 1.1 christos lpr $($(dir)_sources) 5329 1.1 christos endef 5330 1.1 christos @end example 5331 1.1 christos 5332 1.1 christos @noindent 5333 1.1 christos This example defines the variables @samp{dir}, @samp{foo_sources}, and 5334 1.1 christos @samp{foo_print}. 5335 1.1 christos 5336 1.1 christos Note that @dfn{nested variable references} are quite different from 5337 1.1 christos @dfn{recursively expanded variables} 5338 1.1 christos (@pxref{Flavors, ,The Two Flavors of Variables}), though both are 5339 1.1 christos used together in complex ways when doing makefile programming.@refill 5340 1.1 christos 5341 1.1 christos @node Values, Setting, Advanced, Using Variables 5342 1.1 christos @section How Variables Get Their Values 5343 1.1 christos @cindex variables, how they get their values 5344 1.1 christos @cindex value, how a variable gets it 5345 1.1 christos 5346 1.1 christos Variables can get values in several different ways: 5347 1.1 christos 5348 1.1 christos @itemize @bullet 5349 1.1 christos @item 5350 1.1 christos You can specify an overriding value when you run @code{make}. 5351 1.1 christos @xref{Overriding, ,Overriding Variables}. 5352 1.1 christos 5353 1.1 christos @item 5354 1.1 christos You can specify a value in the makefile, either 5355 1.1 christos with an assignment (@pxref{Setting, ,Setting Variables}) or with a 5356 1.1 christos verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill 5357 1.1 christos 5358 1.1 christos @item 5359 1.1 christos Variables in the environment become @code{make} variables. 5360 1.1 christos @xref{Environment, ,Variables from the Environment}. 5361 1.1 christos 5362 1.1 christos @item 5363 1.1 christos Several @dfn{automatic} variables are given new values for each rule. 5364 1.1 christos Each of these has a single conventional use. 5365 1.1 christos @xref{Automatic Variables}. 5366 1.1 christos 5367 1.1 christos @item 5368 1.1 christos Several variables have constant initial values. 5369 1.1 christos @xref{Implicit Variables, ,Variables Used by Implicit Rules}. 5370 1.1 christos @end itemize 5371 1.1 christos 5372 1.1 christos @node Setting, Appending, Values, Using Variables 5373 1.1 christos @section Setting Variables 5374 1.1 christos @cindex setting variables 5375 1.1 christos @cindex variables, setting 5376 1.1 christos @cindex = 5377 1.1 christos @cindex := 5378 1.1 christos @cindex ?= 5379 1.1 christos 5380 1.1 christos To set a variable from the makefile, write a line starting with the 5381 1.1 christos variable name followed by @samp{=} or @samp{:=}. Whatever follows the 5382 1.1 christos @samp{=} or @samp{:=} on the line becomes the value. For example, 5383 1.1 christos 5384 1.1 christos @example 5385 1.1 christos objects = main.o foo.o bar.o utils.o 5386 1.1 christos @end example 5387 1.1 christos 5388 1.1 christos @noindent 5389 1.1 christos defines a variable named @code{objects}. Whitespace around the variable 5390 1.1 christos name and immediately after the @samp{=} is ignored. 5391 1.1 christos 5392 1.1 christos Variables defined with @samp{=} are @dfn{recursively expanded} variables. 5393 1.1 christos Variables defined with @samp{:=} are @dfn{simply expanded} variables; these 5394 1.1 christos definitions can contain variable references which will be expanded before 5395 1.1 christos the definition is made. @xref{Flavors, ,The Two Flavors of Variables}. 5396 1.1 christos 5397 1.1 christos The variable name may contain function and variable references, which 5398 1.1 christos are expanded when the line is read to find the actual variable name to use. 5399 1.1 christos 5400 1.1 christos There is no limit on the length of the value of a variable except the 5401 1.1 christos amount of swapping space on the computer. When a variable definition is 5402 1.1 christos long, it is a good idea to break it into several lines by inserting 5403 1.1 christos backslash-newline at convenient places in the definition. This will not 5404 1.1 christos affect the functioning of @code{make}, but it will make the makefile easier 5405 1.1 christos to read. 5406 1.1 christos 5407 1.1 christos Most variable names are considered to have the empty string as a value if 5408 1.1 christos you have never set them. Several variables have built-in initial values 5409 1.1 christos that are not empty, but you can set them in the usual ways 5410 1.1 christos (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}). 5411 1.1 christos Several special variables are set 5412 1.1 christos automatically to a new value for each rule; these are called the 5413 1.1 christos @dfn{automatic} variables (@pxref{Automatic Variables}). 5414 1.1 christos 5415 1.1 christos If you'd like a variable to be set to a value only if it's not already 5416 1.1 christos set, then you can use the shorthand operator @samp{?=} instead of 5417 1.1 christos @samp{=}. These two settings of the variable @samp{FOO} are identical 5418 1.1 christos (@pxref{Origin Function, ,The @code{origin} Function}): 5419 1.1 christos 5420 1.1 christos @example 5421 1.1 christos FOO ?= bar 5422 1.1 christos @end example 5423 1.1 christos 5424 1.1 christos @noindent 5425 1.1 christos and 5426 1.1 christos 5427 1.1 christos @example 5428 1.1 christos ifeq ($(origin FOO), undefined) 5429 1.1 christos FOO = bar 5430 1.1 christos endif 5431 1.1 christos @end example 5432 1.1 christos 5433 1.1 christos @node Appending, Override Directive, Setting, Using Variables 5434 1.1 christos @section Appending More Text to Variables 5435 1.1 christos @cindex += 5436 1.1 christos @cindex appending to variables 5437 1.1 christos @cindex variables, appending to 5438 1.1 christos 5439 1.1 christos Often it is useful to add more text to the value of a variable already defined. 5440 1.1 christos You do this with a line containing @samp{+=}, like this: 5441 1.1 christos 5442 1.1 christos @example 5443 1.1 christos objects += another.o 5444 1.1 christos @end example 5445 1.1 christos 5446 1.1 christos @noindent 5447 1.1 christos This takes the value of the variable @code{objects}, and adds the text 5448 1.1 christos @samp{another.o} to it (preceded by a single space). Thus: 5449 1.1 christos 5450 1.1 christos @example 5451 1.1 christos objects = main.o foo.o bar.o utils.o 5452 1.1 christos objects += another.o 5453 1.1 christos @end example 5454 1.1 christos 5455 1.1 christos @noindent 5456 1.1 christos sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}. 5457 1.1 christos 5458 1.1 christos Using @samp{+=} is similar to: 5459 1.1 christos 5460 1.1 christos @example 5461 1.1 christos objects = main.o foo.o bar.o utils.o 5462 1.1 christos objects := $(objects) another.o 5463 1.1 christos @end example 5464 1.1 christos 5465 1.1 christos @noindent 5466 1.1 christos but differs in ways that become important when you use more complex values. 5467 1.1 christos 5468 1.1 christos When the variable in question has not been defined before, @samp{+=} 5469 1.1 christos acts just like normal @samp{=}: it defines a recursively-expanded 5470 1.1 christos variable. However, when there @emph{is} a previous definition, exactly 5471 1.1 christos what @samp{+=} does depends on what flavor of variable you defined 5472 1.1 christos originally. @xref{Flavors, ,The Two Flavors of Variables}, for an 5473 1.1 christos explanation of the two flavors of variables. 5474 1.1 christos 5475 1.1 christos When you add to a variable's value with @samp{+=}, @code{make} acts 5476 1.1 christos essentially as if you had included the extra text in the initial 5477 1.1 christos definition of the variable. If you defined it first with @samp{:=}, 5478 1.1 christos making it a simply-expanded variable, @samp{+=} adds to that 5479 1.1 christos simply-expanded definition, and expands the new text before appending it 5480 1.1 christos to the old value just as @samp{:=} does 5481 1.1 christos (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}). 5482 1.1 christos In fact, 5483 1.1 christos 5484 1.1 christos @example 5485 1.1 christos variable := value 5486 1.1 christos variable += more 5487 1.1 christos @end example 5488 1.1 christos 5489 1.1 christos @noindent 5490 1.1 christos is exactly equivalent to: 5491 1.1 christos 5492 1.1 christos @noindent 5493 1.1 christos @example 5494 1.1 christos variable := value 5495 1.1 christos variable := $(variable) more 5496 1.1 christos @end example 5497 1.1 christos 5498 1.1 christos On the other hand, when you use @samp{+=} with a variable that you defined 5499 1.1 christos first to be recursively-expanded using plain @samp{=}, @code{make} does 5500 1.1 christos something a bit different. Recall that when you define a 5501 1.1 christos recursively-expanded variable, @code{make} does not expand the value you set 5502 1.1 christos for variable and function references immediately. Instead it stores the text 5503 1.1 christos verbatim, and saves these variable and function references to be expanded 5504 1.1 christos later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors 5505 1.1 christos of Variables}). When you use @samp{+=} on a recursively-expanded variable, 5506 1.1 christos it is this unexpanded text to which @code{make} appends the new text you 5507 1.1 christos specify. 5508 1.1 christos 5509 1.1 christos @example 5510 1.1 christos @group 5511 1.1 christos variable = value 5512 1.1 christos variable += more 5513 1.1 christos @end group 5514 1.1 christos @end example 5515 1.1 christos 5516 1.1 christos @noindent 5517 1.1 christos is roughly equivalent to: 5518 1.1 christos 5519 1.1 christos @example 5520 1.1 christos @group 5521 1.1 christos temp = value 5522 1.1 christos variable = $(temp) more 5523 1.1 christos @end group 5524 1.1 christos @end example 5525 1.1 christos 5526 1.1 christos @noindent 5527 1.1 christos except that of course it never defines a variable called @code{temp}. 5528 1.1 christos The importance of this comes when the variable's old value contains 5529 1.1 christos variable references. Take this common example: 5530 1.1 christos 5531 1.1 christos @example 5532 1.1 christos CFLAGS = $(includes) -O 5533 1.1 christos @dots{} 5534 1.1 christos CFLAGS += -pg # enable profiling 5535 1.1 christos @end example 5536 1.1 christos 5537 1.1 christos @noindent 5538 1.1 christos The first line defines the @code{CFLAGS} variable with a reference to another 5539 1.1 christos variable, @code{includes}. (@code{CFLAGS} is used by the rules for C 5540 1.1 christos compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.) 5541 1.1 christos Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded 5542 1.1 christos variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when 5543 1.1 christos @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes} 5544 1.1 christos need not be defined yet for its value to take effect. It only has to be 5545 1.1 christos defined before any reference to @code{CFLAGS}. If we tried to append to the 5546 1.1 christos value of @code{CFLAGS} without using @samp{+=}, we might do it like this: 5547 1.1 christos 5548 1.1 christos @example 5549 1.1 christos CFLAGS := $(CFLAGS) -pg # enable profiling 5550 1.1 christos @end example 5551 1.1 christos 5552 1.1 christos @noindent 5553 1.1 christos This is pretty close, but not quite what we want. Using @samp{:=} 5554 1.1 christos redefines @code{CFLAGS} as a simply-expanded variable; this means 5555 1.1 christos @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the 5556 1.1 christos variable. If @code{includes} is not yet defined, we get @w{@samp{ -O 5557 1.1 christos -pg}}, and a later definition of @code{includes} will have no effect. 5558 1.1 christos Conversely, by using @samp{+=} we set @code{CFLAGS} to the 5559 1.1 christos @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve 5560 1.1 christos the reference to @code{includes}, so if that variable gets defined at 5561 1.1 christos any later point, a reference like @samp{$(CFLAGS)} still uses its 5562 1.1 christos value. 5563 1.1 christos 5564 1.1 christos @node Override Directive, Defining, Appending, Using Variables 5565 1.1 christos @section The @code{override} Directive 5566 1.1 christos @findex override 5567 1.1 christos @cindex overriding with @code{override} 5568 1.1 christos @cindex variables, overriding 5569 1.1 christos 5570 1.1 christos If a variable has been set with a command argument 5571 1.1 christos (@pxref{Overriding, ,Overriding Variables}), 5572 1.1 christos then ordinary assignments in the makefile are ignored. If you want to set 5573 1.1 christos the variable in the makefile even though it was set with a command 5574 1.1 christos argument, you can use an @code{override} directive, which is a line that 5575 1.1 christos looks like this:@refill 5576 1.1 christos 5577 1.1 christos @example 5578 1.1 christos override @var{variable} = @var{value} 5579 1.1 christos @end example 5580 1.1 christos 5581 1.1 christos @noindent 5582 1.1 christos or 5583 1.1 christos 5584 1.1 christos @example 5585 1.1 christos override @var{variable} := @var{value} 5586 1.1 christos @end example 5587 1.1 christos 5588 1.1 christos To append more text to a variable defined on the command line, use: 5589 1.1 christos 5590 1.1 christos @example 5591 1.1 christos override @var{variable} += @var{more text} 5592 1.1 christos @end example 5593 1.1 christos 5594 1.1 christos @noindent 5595 1.1 christos @xref{Appending, ,Appending More Text to Variables}. 5596 1.1 christos 5597 1.1 christos The @code{override} directive was not invented for escalation in the war 5598 1.1 christos between makefiles and command arguments. It was invented so you can alter 5599 1.1 christos and add to values that the user specifies with command arguments. 5600 1.1 christos 5601 1.1 christos For example, suppose you always want the @samp{-g} switch when you run the 5602 1.1 christos C compiler, but you would like to allow the user to specify the other 5603 1.1 christos switches with a command argument just as usual. You could use this 5604 1.1 christos @code{override} directive: 5605 1.1 christos 5606 1.1 christos @example 5607 1.1 christos override CFLAGS += -g 5608 1.1 christos @end example 5609 1.1 christos 5610 1.1 christos You can also use @code{override} directives with @code{define} directives. 5611 1.1 christos This is done as you might expect: 5612 1.1 christos 5613 1.1 christos @example 5614 1.1 christos override define foo 5615 1.1 christos bar 5616 1.1 christos endef 5617 1.1 christos @end example 5618 1.1 christos 5619 1.1 christos @noindent 5620 1.1 christos @iftex 5621 1.1 christos See the next section for information about @code{define}. 5622 1.1 christos @end iftex 5623 1.1 christos @ifnottex 5624 1.1 christos @xref{Defining, ,Defining Variables Verbatim}. 5625 1.1 christos @end ifnottex 5626 1.1 christos 5627 1.1 christos @node Defining, Environment, Override Directive, Using Variables 5628 1.1 christos @section Defining Variables Verbatim 5629 1.1 christos @findex define 5630 1.1 christos @findex endef 5631 1.1 christos @cindex verbatim variable definition 5632 1.1 christos @cindex defining variables verbatim 5633 1.1 christos @cindex variables, defining verbatim 5634 1.1 christos 5635 1.1 christos Another way to set the value of a variable is to use the @code{define} 5636 1.1 christos directive. This directive has an unusual syntax which allows newline 5637 1.1 christos characters to be included in the value, which is convenient for defining 5638 1.1 christos both canned sequences of commands 5639 1.1 christos (@pxref{Sequences, ,Defining Canned Command Sequences}), and also 5640 1.1 christos sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}). 5641 1.1 christos 5642 1.1 christos The @code{define} directive is followed on the same line by the name of the 5643 1.1 christos variable and nothing more. The value to give the variable appears on the 5644 1.1 christos following lines. The end of the value is marked by a line containing just 5645 1.1 christos the word @code{endef}. Aside from this difference in syntax, @code{define} 5646 1.1 christos works just like @samp{=}: it creates a recursively-expanded variable 5647 1.1 christos (@pxref{Flavors, ,The Two Flavors of Variables}). 5648 1.1 christos The variable name may contain function and variable references, which 5649 1.1 christos are expanded when the directive is read to find the actual variable name 5650 1.1 christos to use. 5651 1.1 christos 5652 1.1 christos You may nest @code{define} directives: @code{make} will keep track of 5653 1.1 christos nested directives and report an error if they are not all properly 5654 1.1 christos closed with @code{endef}. Note that lines beginning with tab 5655 1.1 christos characters are considered part of a command script, so any 5656 1.1 christos @code{define} or @code{endef} strings appearing on such a line will 5657 1.1 christos not be considered @code{make} operators. 5658 1.1 christos 5659 1.1 christos @example 5660 1.1 christos define two-lines 5661 1.1 christos echo foo 5662 1.1 christos echo $(bar) 5663 1.1 christos endef 5664 1.1 christos @end example 5665 1.1 christos 5666 1.1 christos The value in an ordinary assignment cannot contain a newline; but the 5667 1.1 christos newlines that separate the lines of the value in a @code{define} become 5668 1.1 christos part of the variable's value (except for the final newline which precedes 5669 1.1 christos the @code{endef} and is not considered part of the value).@refill 5670 1.1 christos 5671 1.1 christos @need 800 5672 1.1 christos When used in a command script, the previous example is functionally 5673 1.1 christos equivalent to this: 5674 1.1 christos 5675 1.1 christos @example 5676 1.1 christos two-lines = echo foo; echo $(bar) 5677 1.1 christos @end example 5678 1.1 christos 5679 1.1 christos @noindent 5680 1.1 christos since two commands separated by semicolon behave much like two separate 5681 1.1 christos shell commands. However, note that using two separate lines means 5682 1.1 christos @code{make} will invoke the shell twice, running an independent subshell 5683 1.1 christos for each line. @xref{Execution, ,Command Execution}. 5684 1.1 christos 5685 1.1 christos If you want variable definitions made with @code{define} to take 5686 1.1 christos precedence over command-line variable definitions, you can use the 5687 1.1 christos @code{override} directive together with @code{define}: 5688 1.1 christos 5689 1.1 christos @example 5690 1.1 christos override define two-lines 5691 1.1 christos foo 5692 1.1 christos $(bar) 5693 1.1 christos endef 5694 1.1 christos @end example 5695 1.1 christos 5696 1.1 christos @noindent 5697 1.1 christos @xref{Override Directive, ,The @code{override} Directive}. 5698 1.1 christos 5699 1.1 christos @node Environment, Target-specific, Defining, Using Variables 5700 1.1 christos @section Variables from the Environment 5701 1.1 christos 5702 1.1 christos @cindex variables, environment 5703 1.1 christos @cindex environment 5704 1.1 christos Variables in @code{make} can come from the environment in which 5705 1.1 christos @code{make} is run. Every environment variable that @code{make} sees 5706 1.1 christos when it starts up is transformed into a @code{make} variable with the 5707 1.1 christos same name and value. However, an explicit assignment in the makefile, 5708 1.1 christos or with a command argument, overrides the environment. (If the 5709 1.1 christos @samp{-e} flag is specified, then values from the environment override 5710 1.1 christos assignments in the makefile. @xref{Options Summary, ,Summary of 5711 1.1 christos Options}. But this is not recommended practice.) 5712 1.1 christos 5713 1.1 christos Thus, by setting the variable @code{CFLAGS} in your environment, you can 5714 1.1 christos cause all C compilations in most makefiles to use the compiler switches you 5715 1.1 christos prefer. This is safe for variables with standard or conventional meanings 5716 1.1 christos because you know that no makefile will use them for other things. (Note 5717 1.1 christos this is not totally reliable; some makefiles set @code{CFLAGS} explicitly 5718 1.1 christos and therefore are not affected by the value in the environment.) 5719 1.1 christos 5720 1.1 christos When @code{make} runs a command script, variables defined in the 5721 1.1 christos makefile are placed into the environment of that command. This allows 5722 1.1 christos you to pass values to sub-@code{make} invocations (@pxref{Recursion, 5723 1.1 christos ,Recursive Use of @code{make}}). By default, only variables that came 5724 1.1 christos from the environment or the command line are passed to recursive 5725 1.1 christos invocations. You can use the @code{export} directive to pass other 5726 1.1 christos variables. @xref{Variables/Recursion, , Communicating Variables to a 5727 1.1 christos Sub-@code{make}}, for full details. 5728 1.1 christos 5729 1.1 christos Other use of variables from the environment is not recommended. It is not 5730 1.1 christos wise for makefiles to depend for their functioning on environment variables 5731 1.1 christos set up outside their control, since this would cause different users to get 5732 1.1 christos different results from the same makefile. This is against the whole 5733 1.1 christos purpose of most makefiles. 5734 1.1 christos 5735 1.1 christos @cindex SHELL, import from environment 5736 1.1 christos Such problems would be especially likely with the variable 5737 1.1 christos @code{SHELL}, which is normally present in the environment to specify 5738 1.1 christos the user's choice of interactive shell. It would be very undesirable 5739 1.1 christos for this choice to affect @code{make}; so, @code{make} handles the 5740 1.1 christos @code{SHELL} environment variable in a special way; see @ref{Choosing 5741 1.1 christos the Shell}.@refill 5742 1.1 christos 5743 1.1 christos @node Target-specific, Pattern-specific, Environment, Using Variables 5744 1.1 christos @section Target-specific Variable Values 5745 1.1 christos @cindex target-specific variables 5746 1.1 christos @cindex variables, target-specific 5747 1.1 christos 5748 1.1 christos Variable values in @code{make} are usually global; that is, they are the 5749 1.1 christos same regardless of where they are evaluated (unless they're reset, of 5750 1.1 christos course). One exception to that is automatic variables 5751 1.1 christos (@pxref{Automatic Variables}). 5752 1.1 christos 5753 1.1 christos The other exception is @dfn{target-specific variable values}. This 5754 1.1 christos feature allows you to define different values for the same variable, 5755 1.1 christos based on the target that @code{make} is currently building. As with 5756 1.1 christos automatic variables, these values are only available within the context 5757 1.1 christos of a target's command script (and in other target-specific assignments). 5758 1.1 christos 5759 1.1 christos Set a target-specific variable value like this: 5760 1.1 christos 5761 1.1 christos @example 5762 1.1 christos @var{target} @dots{} : @var{variable-assignment} 5763 1.1 christos @end example 5764 1.1 christos 5765 1.1 christos @noindent 5766 1.1 christos or like this: 5767 1.1 christos 5768 1.1 christos @example 5769 1.1 christos @var{target} @dots{} : override @var{variable-assignment} 5770 1.1 christos @end example 5771 1.1 christos 5772 1.1 christos @noindent 5773 1.1 christos or like this: 5774 1.1 christos 5775 1.1 christos @example 5776 1.1 christos @var{target} @dots{} : export @var{variable-assignment} 5777 1.1 christos @end example 5778 1.1 christos 5779 1.1 christos Multiple @var{target} values create a target-specific variable value for 5780 1.1 christos each member of the target list individually. 5781 1.1 christos 5782 1.1 christos The @var{variable-assignment} can be any valid form of assignment; 5783 1.1 christos recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or 5784 1.1 christos conditional (@samp{?=}). All variables that appear within the 5785 1.1 christos @var{variable-assignment} are evaluated within the context of the 5786 1.1 christos target: thus, any previously-defined target-specific variable values 5787 1.1 christos will be in effect. Note that this variable is actually distinct from 5788 1.1 christos any ``global'' value: the two variables do not have to have the same 5789 1.1 christos flavor (recursive vs.@: static). 5790 1.1 christos 5791 1.1 christos Target-specific variables have the same priority as any other makefile 5792 1.1 christos variable. Variables provided on the command-line (and in the 5793 1.1 christos environment if the @samp{-e} option is in force) will take precedence. 5794 1.1 christos Specifying the @code{override} directive will allow the target-specific 5795 1.1 christos variable value to be preferred. 5796 1.1 christos 5797 1.1 christos There is one more special feature of target-specific variables: when 5798 1.1 christos you define a target-specific variable that variable value is also in 5799 1.1 christos effect for all prerequisites of this target, and all their 5800 1.1 christos prerequisites, etc.@: (unless those prerequisites override that variable 5801 1.1 christos with their own target-specific variable value). So, for example, a 5802 1.1 christos statement like this: 5803 1.1 christos 5804 1.1 christos @example 5805 1.1 christos prog : CFLAGS = -g 5806 1.1 christos prog : prog.o foo.o bar.o 5807 1.1 christos @end example 5808 1.1 christos 5809 1.1 christos @noindent 5810 1.1 christos will set @code{CFLAGS} to @samp{-g} in the command script for 5811 1.1 christos @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the 5812 1.1 christos command scripts that create @file{prog.o}, @file{foo.o}, and 5813 1.1 christos @file{bar.o}, and any command scripts which create their 5814 1.1 christos prerequisites. 5815 1.1 christos 5816 1.1 christos Be aware that a given prerequisite will only be built once per 5817 1.1 christos invocation of make, at most. If the same file is a prerequisite of 5818 1.1 christos multiple targets, and each of those targets has a different value for 5819 1.1 christos the same target-specific variable, then the first target to be built 5820 1.1 christos will cause that prerequisite to be built and the prerequisite will 5821 1.1 christos inherit the target-specific value from the first target. It will 5822 1.1 christos ignore the target-specific values from any other targets. 5823 1.1 christos 5824 1.1 christos @node Pattern-specific, , Target-specific, Using Variables 5825 1.1 christos @section Pattern-specific Variable Values 5826 1.1 christos @cindex pattern-specific variables 5827 1.1 christos @cindex variables, pattern-specific 5828 1.1 christos 5829 1.1 christos In addition to target-specific variable values 5830 1.1 christos (@pxref{Target-specific, ,Target-specific Variable Values}), GNU 5831 1.1 christos @code{make} supports pattern-specific variable values. In this form, 5832 1.1 christos the variable is defined for any target that matches the pattern 5833 1.1 christos specified. If a target matches more than one pattern, all the 5834 1.1 christos matching pattern-specific variables are interpreted in the order in 5835 1.1 christos which they were defined in the makefile, and collected together into 5836 1.1 christos one set. Variables defined in this way are searched after any 5837 1.1 christos target-specific variables defined explicitly for that target, and 5838 1.1 christos before target-specific variables defined for the parent target. 5839 1.1 christos 5840 1.1 christos Set a pattern-specific variable value like this: 5841 1.1 christos 5842 1.1 christos @example 5843 1.1 christos @var{pattern} @dots{} : @var{variable-assignment} 5844 1.1 christos @end example 5845 1.1 christos 5846 1.1 christos @noindent 5847 1.1 christos or like this: 5848 1.1 christos 5849 1.1 christos @example 5850 1.1 christos @var{pattern} @dots{} : override @var{variable-assignment} 5851 1.1 christos @end example 5852 1.1 christos 5853 1.1 christos @noindent 5854 1.1 christos where @var{pattern} is a %-pattern. As with target-specific variable 5855 1.1 christos values, multiple @var{pattern} values create a pattern-specific variable 5856 1.1 christos value for each pattern individually. The @var{variable-assignment} can 5857 1.1 christos be any valid form of assignment. Any command-line variable setting will 5858 1.1 christos take precedence, unless @code{override} is specified. 5859 1.1 christos 5860 1.1 christos For example: 5861 1.1 christos 5862 1.1 christos @example 5863 1.1 christos %.o : CFLAGS = -O 5864 1.1 christos @end example 5865 1.1 christos 5866 1.1 christos @noindent 5867 1.1 christos will assign @code{CFLAGS} the value of @samp{-O} for all targets 5868 1.1 christos matching the pattern @code{%.o}. 5869 1.1 christos 5870 1.1 christos @node Conditionals, Functions, Using Variables, Top 5871 1.1 christos @chapter Conditional Parts of Makefiles 5872 1.1 christos 5873 1.1 christos @cindex conditionals 5874 1.1 christos A @dfn{conditional} causes part of a makefile to be obeyed or ignored 5875 1.1 christos depending on the values of variables. Conditionals can compare the 5876 1.1 christos value of one variable to another, or the value of a variable to 5877 1.1 christos a constant string. Conditionals control what @code{make} actually 5878 1.1 christos ``sees'' in the makefile, so they @emph{cannot} be used to control shell 5879 1.1 christos commands at the time of execution.@refill 5880 1.1 christos 5881 1.1 christos @menu 5882 1.1 christos * Conditional Example:: Example of a conditional 5883 1.1 christos * Conditional Syntax:: The syntax of conditionals. 5884 1.1 christos * Testing Flags:: Conditionals that test flags. 5885 1.1 christos @end menu 5886 1.1 christos 5887 1.1 christos @node Conditional Example, Conditional Syntax, Conditionals, Conditionals 5888 1.1 christos @section Example of a Conditional 5889 1.1 christos 5890 1.1 christos The following example of a conditional tells @code{make} to use one set 5891 1.1 christos of libraries if the @code{CC} variable is @samp{gcc}, and a different 5892 1.1 christos set of libraries otherwise. It works by controlling which of two 5893 1.1 christos command lines will be used as the command for a rule. The result is 5894 1.1 christos that @samp{CC=gcc} as an argument to @code{make} changes not only which 5895 1.1 christos compiler is used but also which libraries are linked. 5896 1.1 christos 5897 1.1 christos @example 5898 1.1 christos libs_for_gcc = -lgnu 5899 1.1 christos normal_libs = 5900 1.1 christos 5901 1.1 christos foo: $(objects) 5902 1.1 christos ifeq ($(CC),gcc) 5903 1.1 christos $(CC) -o foo $(objects) $(libs_for_gcc) 5904 1.1 christos else 5905 1.1 christos $(CC) -o foo $(objects) $(normal_libs) 5906 1.1 christos endif 5907 1.1 christos @end example 5908 1.1 christos 5909 1.1 christos This conditional uses three directives: one @code{ifeq}, one @code{else} 5910 1.1 christos and one @code{endif}. 5911 1.1 christos 5912 1.1 christos The @code{ifeq} directive begins the conditional, and specifies the 5913 1.1 christos condition. It contains two arguments, separated by a comma and surrounded 5914 1.1 christos by parentheses. Variable substitution is performed on both arguments and 5915 1.1 christos then they are compared. The lines of the makefile following the 5916 1.1 christos @code{ifeq} are obeyed if the two arguments match; otherwise they are 5917 1.1 christos ignored. 5918 1.1 christos 5919 1.1 christos The @code{else} directive causes the following lines to be obeyed if the 5920 1.1 christos previous conditional failed. In the example above, this means that the 5921 1.1 christos second alternative linking command is used whenever the first alternative 5922 1.1 christos is not used. It is optional to have an @code{else} in a conditional. 5923 1.1 christos 5924 1.1 christos The @code{endif} directive ends the conditional. Every conditional must 5925 1.1 christos end with an @code{endif}. Unconditional makefile text follows. 5926 1.1 christos 5927 1.1 christos As this example illustrates, conditionals work at the textual level: 5928 1.1 christos the lines of the conditional are treated as part of the makefile, or 5929 1.1 christos ignored, according to the condition. This is why the larger syntactic 5930 1.1 christos units of the makefile, such as rules, may cross the beginning or the 5931 1.1 christos end of the conditional. 5932 1.1 christos 5933 1.1 christos When the variable @code{CC} has the value @samp{gcc}, the above example has 5934 1.1 christos this effect: 5935 1.1 christos 5936 1.1 christos @example 5937 1.1 christos foo: $(objects) 5938 1.1 christos $(CC) -o foo $(objects) $(libs_for_gcc) 5939 1.1 christos @end example 5940 1.1 christos 5941 1.1 christos @noindent 5942 1.1 christos When the variable @code{CC} has any other value, the effect is this: 5943 1.1 christos 5944 1.1 christos @example 5945 1.1 christos foo: $(objects) 5946 1.1 christos $(CC) -o foo $(objects) $(normal_libs) 5947 1.1 christos @end example 5948 1.1 christos 5949 1.1 christos Equivalent results can be obtained in another way by conditionalizing a 5950 1.1 christos variable assignment and then using the variable unconditionally: 5951 1.1 christos 5952 1.1 christos @example 5953 1.1 christos libs_for_gcc = -lgnu 5954 1.1 christos normal_libs = 5955 1.1 christos 5956 1.1 christos ifeq ($(CC),gcc) 5957 1.1 christos libs=$(libs_for_gcc) 5958 1.1 christos else 5959 1.1 christos libs=$(normal_libs) 5960 1.1 christos endif 5961 1.1 christos 5962 1.1 christos foo: $(objects) 5963 1.1 christos $(CC) -o foo $(objects) $(libs) 5964 1.1 christos @end example 5965 1.1 christos 5966 1.1 christos @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals 5967 1.1 christos @section Syntax of Conditionals 5968 1.1 christos @findex ifdef 5969 1.1 christos @findex ifeq 5970 1.1 christos @findex ifndef 5971 1.1 christos @findex ifneq 5972 1.1 christos @findex else 5973 1.1 christos @findex endif 5974 1.1 christos 5975 1.1 christos The syntax of a simple conditional with no @code{else} is as follows: 5976 1.1 christos 5977 1.1 christos @example 5978 1.1 christos @var{conditional-directive} 5979 1.1 christos @var{text-if-true} 5980 1.1 christos endif 5981 1.1 christos @end example 5982 1.1 christos 5983 1.1 christos @noindent 5984 1.1 christos The @var{text-if-true} may be any lines of text, to be considered as part 5985 1.1 christos of the makefile if the condition is true. If the condition is false, no 5986 1.1 christos text is used instead. 5987 1.1 christos 5988 1.1 christos The syntax of a complex conditional is as follows: 5989 1.1 christos 5990 1.1 christos @example 5991 1.1 christos @var{conditional-directive} 5992 1.1 christos @var{text-if-true} 5993 1.1 christos else 5994 1.1 christos @var{text-if-false} 5995 1.1 christos endif 5996 1.1 christos @end example 5997 1.1 christos 5998 1.1 christos or: 5999 1.1 christos 6000 1.1 christos @example 6001 1.1 christos @var{conditional-directive} 6002 1.1 christos @var{text-if-one-is-true} 6003 1.1 christos else @var{conditional-directive} 6004 1.1 christos @var{text-if-true} 6005 1.1 christos else 6006 1.1 christos @var{text-if-false} 6007 1.1 christos endif 6008 1.1 christos @end example 6009 1.1 christos 6010 1.1 christos @noindent 6011 1.1 christos There can be as many ``@code{else} @var{conditional-directive}'' 6012 1.1 christos clauses as necessary. Once a given condition is true, 6013 1.1 christos @var{text-if-true} is used and no other clause is used; if no 6014 1.1 christos condition is true then @var{text-if-false} is used. The 6015 1.1 christos @var{text-if-true} and @var{text-if-false} can be any number of lines 6016 1.1 christos of text. 6017 1.1 christos 6018 1.1 christos The syntax of the @var{conditional-directive} is the same whether the 6019 1.1 christos conditional is simple or complex; after an @code{else} or not. There 6020 1.1 christos are four different directives that test different conditions. Here is 6021 1.1 christos a table of them: 6022 1.1 christos 6023 1.1 christos @table @code 6024 1.1 christos @item ifeq (@var{arg1}, @var{arg2}) 6025 1.1 christos @itemx ifeq '@var{arg1}' '@var{arg2}' 6026 1.1 christos @itemx ifeq "@var{arg1}" "@var{arg2}" 6027 1.1 christos @itemx ifeq "@var{arg1}" '@var{arg2}' 6028 1.1 christos @itemx ifeq '@var{arg1}' "@var{arg2}" 6029 1.1 christos Expand all variable references in @var{arg1} and @var{arg2} and 6030 1.1 christos compare them. If they are identical, the @var{text-if-true} is 6031 1.1 christos effective; otherwise, the @var{text-if-false}, if any, is effective. 6032 1.1 christos 6033 1.1 christos Often you want to test if a variable has a non-empty value. When the 6034 1.1 christos value results from complex expansions of variables and functions, 6035 1.1 christos expansions you would consider empty may actually contain whitespace 6036 1.1 christos characters and thus are not seen as empty. However, you can use the 6037 1.1 christos @code{strip} function (@pxref{Text Functions}) to avoid interpreting 6038 1.1 christos whitespace as a non-empty value. For example: 6039 1.1 christos 6040 1.1 christos @example 6041 1.1 christos @group 6042 1.1 christos ifeq ($(strip $(foo)),) 6043 1.1 christos @var{text-if-empty} 6044 1.1 christos endif 6045 1.1 christos @end group 6046 1.1 christos @end example 6047 1.1 christos 6048 1.1 christos @noindent 6049 1.1 christos will evaluate @var{text-if-empty} even if the expansion of 6050 1.1 christos @code{$(foo)} contains whitespace characters. 6051 1.1 christos 6052 1.1 christos @item ifneq (@var{arg1}, @var{arg2}) 6053 1.1 christos @itemx ifneq '@var{arg1}' '@var{arg2}' 6054 1.1 christos @itemx ifneq "@var{arg1}" "@var{arg2}" 6055 1.1 christos @itemx ifneq "@var{arg1}" '@var{arg2}' 6056 1.1 christos @itemx ifneq '@var{arg1}' "@var{arg2}" 6057 1.1 christos Expand all variable references in @var{arg1} and @var{arg2} and 6058 1.1 christos compare them. If they are different, the @var{text-if-true} is 6059 1.1 christos effective; otherwise, the @var{text-if-false}, if any, is effective. 6060 1.1 christos 6061 1.1 christos @item ifdef @var{variable-name} 6062 1.1 christos The @code{ifdef} form takes the @emph{name} of a variable as its 6063 1.1 christos argument, not a reference to a variable. The value of that variable 6064 1.1 christos has a non-empty value, the @var{text-if-true} is effective; otherwise, 6065 1.1 christos the @var{text-if-false}, if any, is effective. Variables that have 6066 1.1 christos never been defined have an empty value. The text @var{variable-name} 6067 1.1 christos is expanded, so it could be a variable or function that expands 6068 1.1 christos to the name of a variable. For example: 6069 1.1 christos 6070 1.1 christos @example 6071 1.1 christos bar = true 6072 1.1 christos foo = bar 6073 1.1 christos ifdef $(foo) 6074 1.1 christos frobozz = yes 6075 1.1 christos endif 6076 1.1 christos @end example 6077 1.1 christos 6078 1.1 christos The variable reference @code{$(foo)} is expanded, yielding @code{bar}, 6079 1.1 christos which is considered to be the name of a variable. The variable 6080 1.1 christos @code{bar} is not expanded, but its value is examined to determine if 6081 1.1 christos it is non-empty. 6082 1.1 christos 6083 1.1 christos Note that @code{ifdef} only tests whether a variable has a value. It 6084 1.1 christos does not expand the variable to see if that value is nonempty. 6085 1.1 christos Consequently, tests using @code{ifdef} return true for all definitions 6086 1.1 christos except those like @code{foo =}. To test for an empty value, use 6087 1.1 christos @w{@code{ifeq ($(foo),)}}. For example, 6088 1.1 christos 6089 1.1 christos @example 6090 1.1 christos bar = 6091 1.1 christos foo = $(bar) 6092 1.1 christos ifdef foo 6093 1.1 christos frobozz = yes 6094 1.1 christos else 6095 1.1 christos frobozz = no 6096 1.1 christos endif 6097 1.1 christos @end example 6098 1.1 christos 6099 1.1 christos @noindent 6100 1.1 christos sets @samp{frobozz} to @samp{yes}, while: 6101 1.1 christos 6102 1.1 christos @example 6103 1.1 christos foo = 6104 1.1 christos ifdef foo 6105 1.1 christos frobozz = yes 6106 1.1 christos else 6107 1.1 christos frobozz = no 6108 1.1 christos endif 6109 1.1 christos @end example 6110 1.1 christos 6111 1.1 christos @noindent 6112 1.1 christos sets @samp{frobozz} to @samp{no}. 6113 1.1 christos 6114 1.1 christos @item ifndef @var{variable-name} 6115 1.1 christos If the variable @var{variable-name} has an empty value, the 6116 1.1 christos @var{text-if-true} is effective; otherwise, the @var{text-if-false}, 6117 1.1 christos if any, is effective. The rules for expansion and testing of 6118 1.1 christos @var{variable-name} are identical to the @code{ifdef} directive. 6119 1.1 christos @end table 6120 1.1 christos 6121 1.1 christos Extra spaces are allowed and ignored at the beginning of the conditional 6122 1.1 christos directive line, but a tab is not allowed. (If the line begins with a tab, 6123 1.1 christos it will be considered a command for a rule.) Aside from this, extra spaces 6124 1.1 christos or tabs may be inserted with no effect anywhere except within the directive 6125 1.1 christos name or within an argument. A comment starting with @samp{#} may appear at 6126 1.1 christos the end of the line. 6127 1.1 christos 6128 1.1 christos The other two directives that play a part in a conditional are @code{else} 6129 1.1 christos and @code{endif}. Each of these directives is written as one word, with no 6130 1.1 christos arguments. Extra spaces are allowed and ignored at the beginning of the 6131 1.1 christos line, and spaces or tabs at the end. A comment starting with @samp{#} may 6132 1.1 christos appear at the end of the line. 6133 1.1 christos 6134 1.1 christos Conditionals affect which lines of the makefile @code{make} uses. If 6135 1.1 christos the condition is true, @code{make} reads the lines of the 6136 1.1 christos @var{text-if-true} as part of the makefile; if the condition is false, 6137 1.1 christos @code{make} ignores those lines completely. It follows that syntactic 6138 1.1 christos units of the makefile, such as rules, may safely be split across the 6139 1.1 christos beginning or the end of the conditional.@refill 6140 1.1 christos 6141 1.1 christos @code{make} evaluates conditionals when it reads a makefile. 6142 1.1 christos Consequently, you cannot use automatic variables in the tests of 6143 1.1 christos conditionals because they are not defined until commands are run 6144 1.1 christos (@pxref{Automatic Variables}). 6145 1.1 christos 6146 1.1 christos To prevent intolerable confusion, it is not permitted to start a 6147 1.1 christos conditional in one makefile and end it in another. However, you may 6148 1.1 christos write an @code{include} directive within a conditional, provided you do 6149 1.1 christos not attempt to terminate the conditional inside the included file. 6150 1.1 christos 6151 1.1 christos @node Testing Flags, , Conditional Syntax, Conditionals 6152 1.1 christos @section Conditionals that Test Flags 6153 1.1 christos 6154 1.1 christos You can write a conditional that tests @code{make} command flags such as 6155 1.1 christos @samp{-t} by using the variable @code{MAKEFLAGS} together with the 6156 1.1 christos @code{findstring} function 6157 1.1 christos (@pxref{Text Functions, , Functions for String Substitution and Analysis}). 6158 1.1 christos This is useful when @code{touch} is not enough to make a file appear up 6159 1.1 christos to date. 6160 1.1 christos 6161 1.1 christos The @code{findstring} function determines whether one string appears as a 6162 1.1 christos substring of another. If you want to test for the @samp{-t} flag, 6163 1.1 christos use @samp{t} as the first string and the value of @code{MAKEFLAGS} as 6164 1.1 christos the other. 6165 1.1 christos 6166 1.1 christos For example, here is how to arrange to use @samp{ranlib -t} to finish 6167 1.1 christos marking an archive file up to date: 6168 1.1 christos 6169 1.1 christos @example 6170 1.1 christos archive.a: @dots{} 6171 1.1 christos ifneq (,$(findstring t,$(MAKEFLAGS))) 6172 1.1 christos +touch archive.a 6173 1.1 christos +ranlib -t archive.a 6174 1.1 christos else 6175 1.1 christos ranlib archive.a 6176 1.1 christos endif 6177 1.1 christos @end example 6178 1.1 christos 6179 1.1 christos @noindent 6180 1.1 christos The @samp{+} prefix marks those command lines as ``recursive'' so 6181 1.1 christos that they will be executed despite use of the @samp{-t} flag. 6182 1.1 christos @xref{Recursion, ,Recursive Use of @code{make}}. 6183 1.1 christos 6184 1.1 christos @node Functions, Running, Conditionals, Top 6185 1.1 christos @chapter Functions for Transforming Text 6186 1.1 christos @cindex functions 6187 1.1 christos 6188 1.1 christos @dfn{Functions} allow you to do text processing in the makefile to compute 6189 1.1 christos the files to operate on or the commands to use. You use a function in a 6190 1.1 christos @dfn{function call}, where you give the name of the function and some text 6191 1.1 christos (the @dfn{arguments}) for the function to operate on. The result of the 6192 1.1 christos function's processing is substituted into the makefile at the point of the 6193 1.1 christos call, just as a variable might be substituted. 6194 1.1 christos 6195 1.1 christos @menu 6196 1.1 christos * Syntax of Functions:: How to write a function call. 6197 1.1 christos * Text Functions:: General-purpose text manipulation functions. 6198 1.1 christos * File Name Functions:: Functions for manipulating file names. 6199 1.1 christos * Conditional Functions:: Functions that implement conditions. 6200 1.1 christos * Foreach Function:: Repeat some text with controlled variation. 6201 1.1 christos * Call Function:: Expand a user-defined function. 6202 1.1 christos * Value Function:: Return the un-expanded value of a variable. 6203 1.1 christos * Eval Function:: Evaluate the arguments as makefile syntax. 6204 1.1 christos * Origin Function:: Find where a variable got its value. 6205 1.1 christos * Flavor Function:: Find out the flavor of a variable. 6206 1.1 christos * Shell Function:: Substitute the output of a shell command. 6207 1.1 christos * Make Control Functions:: Functions that control how make runs. 6208 1.1 christos @end menu 6209 1.1 christos 6210 1.1 christos @node Syntax of Functions, Text Functions, Functions, Functions 6211 1.1 christos @section Function Call Syntax 6212 1.1 christos @cindex @code{$}, in function call 6213 1.1 christos @cindex dollar sign (@code{$}), in function call 6214 1.1 christos @cindex arguments of functions 6215 1.1 christos @cindex functions, syntax of 6216 1.1 christos 6217 1.1 christos A function call resembles a variable reference. It looks like this: 6218 1.1 christos 6219 1.1 christos @example 6220 1.1 christos $(@var{function} @var{arguments}) 6221 1.1 christos @end example 6222 1.1 christos 6223 1.1 christos @noindent 6224 1.1 christos or like this: 6225 1.1 christos 6226 1.1 christos @example 6227 1.1 christos $@{@var{function} @var{arguments}@} 6228 1.1 christos @end example 6229 1.1 christos 6230 1.1 christos Here @var{function} is a function name; one of a short list of names 6231 1.1 christos that are part of @code{make}. You can also essentially create your own 6232 1.1 christos functions by using the @code{call} builtin function. 6233 1.1 christos 6234 1.1 christos The @var{arguments} are the arguments of the function. They are 6235 1.1 christos separated from the function name by one or more spaces or tabs, and if 6236 1.1 christos there is more than one argument, then they are separated by commas. 6237 1.1 christos Such whitespace and commas are not part of an argument's value. The 6238 1.1 christos delimiters which you use to surround the function call, whether 6239 1.1 christos parentheses or braces, can appear in an argument only in matching pairs; 6240 1.1 christos the other kind of delimiters may appear singly. If the arguments 6241 1.1 christos themselves contain other function calls or variable references, it is 6242 1.1 christos wisest to use the same kind of delimiters for all the references; write 6243 1.1 christos @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This 6244 1.1 christos is because it is clearer, and because only one type of delimiter is 6245 1.1 christos matched to find the end of the reference. 6246 1.1 christos 6247 1.1 christos The text written for each argument is processed by substitution of 6248 1.1 christos variables and function calls to produce the argument value, which 6249 1.1 christos is the text on which the function acts. The substitution is done in the 6250 1.1 christos order in which the arguments appear. 6251 1.1 christos 6252 1.1 christos Commas and unmatched parentheses or braces cannot appear in the text of an 6253 1.1 christos argument as written; leading spaces cannot appear in the text of the first 6254 1.1 christos argument as written. These characters can be put into the argument value 6255 1.1 christos by variable substitution. First define variables @code{comma} and 6256 1.1 christos @code{space} whose values are isolated comma and space characters, then 6257 1.1 christos substitute these variables where such characters are wanted, like this: 6258 1.1 christos 6259 1.1 christos @example 6260 1.1 christos @group 6261 1.1 christos comma:= , 6262 1.1 christos empty:= 6263 1.1 christos space:= $(empty) $(empty) 6264 1.1 christos foo:= a b c 6265 1.1 christos bar:= $(subst $(space),$(comma),$(foo)) 6266 1.1 christos # @r{bar is now `a,b,c'.} 6267 1.1 christos @end group 6268 1.1 christos @end example 6269 1.1 christos 6270 1.1 christos @noindent 6271 1.1 christos Here the @code{subst} function replaces each space with a comma, through 6272 1.1 christos the value of @code{foo}, and substitutes the result. 6273 1.1 christos 6274 1.1 christos @node Text Functions, File Name Functions, Syntax of Functions, Functions 6275 1.1 christos @section Functions for String Substitution and Analysis 6276 1.1 christos @cindex functions, for text 6277 1.1 christos 6278 1.1 christos Here are some functions that operate on strings: 6279 1.1 christos 6280 1.1 christos @table @code 6281 1.1 christos @item $(subst @var{from},@var{to},@var{text}) 6282 1.1 christos @findex subst 6283 1.1 christos Performs a textual replacement on the text @var{text}: each occurrence 6284 1.1 christos of @var{from} is replaced by @var{to}. The result is substituted for 6285 1.1 christos the function call. For example, 6286 1.1 christos 6287 1.1 christos @example 6288 1.1 christos $(subst ee,EE,feet on the street) 6289 1.1 christos @end example 6290 1.1 christos 6291 1.1 christos substitutes the string @samp{fEEt on the strEEt}. 6292 1.1 christos 6293 1.1 christos @item $(patsubst @var{pattern},@var{replacement},@var{text}) 6294 1.1 christos @findex patsubst 6295 1.1 christos Finds whitespace-separated words in @var{text} that match 6296 1.1 christos @var{pattern} and replaces them with @var{replacement}. Here 6297 1.1 christos @var{pattern} may contain a @samp{%} which acts as a wildcard, 6298 1.1 christos matching any number of any characters within a word. If 6299 1.1 christos @var{replacement} also contains a @samp{%}, the @samp{%} is replaced 6300 1.1 christos by the text that matched the @samp{%} in @var{pattern}. Only the first 6301 1.1 christos @samp{%} in the @var{pattern} and @var{replacement} is treated this 6302 1.1 christos way; any subsequent @samp{%} is unchanged.@refill 6303 1.1 christos 6304 1.1 christos @cindex @code{%}, quoting in @code{patsubst} 6305 1.1 christos @cindex @code{%}, quoting with @code{\} (backslash) 6306 1.1 christos @cindex @code{\} (backslash), to quote @code{%} 6307 1.1 christos @cindex backslash (@code{\}), to quote @code{%} 6308 1.1 christos @cindex quoting @code{%}, in @code{patsubst} 6309 1.1 christos @samp{%} characters in @code{patsubst} function invocations can be 6310 1.1 christos quoted with preceding backslashes (@samp{\}). Backslashes that would 6311 1.1 christos otherwise quote @samp{%} characters can be quoted with more backslashes. 6312 1.1 christos Backslashes that quote @samp{%} characters or other backslashes are 6313 1.1 christos removed from the pattern before it is compared file names or has a stem 6314 1.1 christos substituted into it. Backslashes that are not in danger of quoting 6315 1.1 christos @samp{%} characters go unmolested. For example, the pattern 6316 1.1 christos @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the 6317 1.1 christos operative @samp{%} character, and @samp{pattern\\} following it. The 6318 1.1 christos final two backslashes are left alone because they cannot affect any 6319 1.1 christos @samp{%} character.@refill 6320 1.1 christos 6321 1.1 christos Whitespace between words is folded into single space characters; 6322 1.1 christos leading and trailing whitespace is discarded. 6323 1.1 christos 6324 1.1 christos For example, 6325 1.1 christos 6326 1.1 christos @example 6327 1.1 christos $(patsubst %.c,%.o,x.c.c bar.c) 6328 1.1 christos @end example 6329 1.1 christos 6330 1.1 christos @noindent 6331 1.1 christos produces the value @samp{x.c.o bar.o}. 6332 1.1 christos 6333 1.1 christos Substitution references (@pxref{Substitution Refs, ,Substitution 6334 1.1 christos References}) are a simpler way to get the effect of the @code{patsubst} 6335 1.1 christos function: 6336 1.1 christos 6337 1.1 christos @example 6338 1.1 christos $(@var{var}:@var{pattern}=@var{replacement}) 6339 1.1 christos @end example 6340 1.1 christos 6341 1.1 christos @noindent 6342 1.1 christos is equivalent to 6343 1.1 christos 6344 1.1 christos @example 6345 1.1 christos $(patsubst @var{pattern},@var{replacement},$(@var{var})) 6346 1.1 christos @end example 6347 1.1 christos 6348 1.1 christos The second shorthand simplifies one of the most common uses of 6349 1.1 christos @code{patsubst}: replacing the suffix at the end of file names. 6350 1.1 christos 6351 1.1 christos @example 6352 1.1 christos $(@var{var}:@var{suffix}=@var{replacement}) 6353 1.1 christos @end example 6354 1.1 christos 6355 1.1 christos @noindent 6356 1.1 christos is equivalent to 6357 1.1 christos 6358 1.1 christos @example 6359 1.1 christos $(patsubst %@var{suffix},%@var{replacement},$(@var{var})) 6360 1.1 christos @end example 6361 1.1 christos 6362 1.1 christos @noindent 6363 1.1 christos For example, you might have a list of object files: 6364 1.1 christos 6365 1.1 christos @example 6366 1.1 christos objects = foo.o bar.o baz.o 6367 1.1 christos @end example 6368 1.1 christos 6369 1.1 christos @noindent 6370 1.1 christos To get the list of corresponding source files, you could simply write: 6371 1.1 christos 6372 1.1 christos @example 6373 1.1 christos $(objects:.o=.c) 6374 1.1 christos @end example 6375 1.1 christos 6376 1.1 christos @noindent 6377 1.1 christos instead of using the general form: 6378 1.1 christos 6379 1.1 christos @example 6380 1.1 christos $(patsubst %.o,%.c,$(objects)) 6381 1.1 christos @end example 6382 1.1 christos 6383 1.1 christos @item $(strip @var{string}) 6384 1.1 christos @cindex stripping whitespace 6385 1.1 christos @cindex whitespace, stripping 6386 1.1 christos @cindex spaces, stripping 6387 1.1 christos @findex strip 6388 1.1 christos Removes leading and trailing whitespace from @var{string} and replaces 6389 1.1 christos each internal sequence of one or more whitespace characters with a 6390 1.1 christos single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}. 6391 1.1 christos 6392 1.1 christos The function @code{strip} can be very useful when used in conjunction 6393 1.1 christos with conditionals. When comparing something with the empty string 6394 1.1 christos @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of 6395 1.1 christos just whitespace to match the empty string (@pxref{Conditionals}). 6396 1.1 christos 6397 1.1 christos Thus, the following may fail to have the desired results: 6398 1.1 christos 6399 1.1 christos @example 6400 1.1 christos .PHONY: all 6401 1.1 christos ifneq "$(needs_made)" "" 6402 1.1 christos all: $(needs_made) 6403 1.1 christos else 6404 1.1 christos all:;@@echo 'Nothing to make!' 6405 1.1 christos endif 6406 1.1 christos @end example 6407 1.1 christos 6408 1.1 christos @noindent 6409 1.1 christos Replacing the variable reference @w{@samp{$(needs_made)}} with the 6410 1.1 christos function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq} 6411 1.1 christos directive would make it more robust.@refill 6412 1.1 christos 6413 1.1 christos @item $(findstring @var{find},@var{in}) 6414 1.1 christos @findex findstring 6415 1.1 christos @cindex searching for strings 6416 1.1 christos @cindex finding strings 6417 1.1 christos @cindex strings, searching for 6418 1.1 christos Searches @var{in} for an occurrence of @var{find}. If it occurs, the 6419 1.1 christos value is @var{find}; otherwise, the value is empty. You can use this 6420 1.1 christos function in a conditional to test for the presence of a specific 6421 1.1 christos substring in a given string. Thus, the two examples, 6422 1.1 christos 6423 1.1 christos @example 6424 1.1 christos $(findstring a,a b c) 6425 1.1 christos $(findstring a,b c) 6426 1.1 christos @end example 6427 1.1 christos 6428 1.1 christos @noindent 6429 1.1 christos produce the values @samp{a} and @samp{} (the empty string), 6430 1.1 christos respectively. @xref{Testing Flags}, for a practical application of 6431 1.1 christos @code{findstring}.@refill 6432 1.1 christos 6433 1.1 christos @need 750 6434 1.1 christos @findex filter 6435 1.1 christos @cindex filtering words 6436 1.1 christos @cindex words, filtering 6437 1.1 christos @item $(filter @var{pattern}@dots{},@var{text}) 6438 1.1 christos Returns all whitespace-separated words in @var{text} that @emph{do} match 6439 1.1 christos any of the @var{pattern} words, removing any words that @emph{do not} 6440 1.1 christos match. The patterns are written using @samp{%}, just like the patterns 6441 1.1 christos used in the @code{patsubst} function above.@refill 6442 1.1 christos 6443 1.1 christos The @code{filter} function can be used to separate out different types 6444 1.1 christos of strings (such as file names) in a variable. For example: 6445 1.1 christos 6446 1.1 christos @example 6447 1.1 christos sources := foo.c bar.c baz.s ugh.h 6448 1.1 christos foo: $(sources) 6449 1.1 christos cc $(filter %.c %.s,$(sources)) -o foo 6450 1.1 christos @end example 6451 1.1 christos 6452 1.1 christos @noindent 6453 1.1 christos says that @file{foo} depends of @file{foo.c}, @file{bar.c}, 6454 1.1 christos @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and 6455 1.1 christos @file{baz.s} should be specified in the command to the 6456 1.1 christos compiler.@refill 6457 1.1 christos 6458 1.1 christos @item $(filter-out @var{pattern}@dots{},@var{text}) 6459 1.1 christos @findex filter-out 6460 1.1 christos @cindex filtering out words 6461 1.1 christos @cindex words, filtering out 6462 1.1 christos Returns all whitespace-separated words in @var{text} that @emph{do not} 6463 1.1 christos match any of the @var{pattern} words, removing the words that @emph{do} 6464 1.1 christos match one or more. This is the exact opposite of the @code{filter} 6465 1.1 christos function.@refill 6466 1.1 christos 6467 1.1 christos For example, given: 6468 1.1 christos 6469 1.1 christos @example 6470 1.1 christos @group 6471 1.1 christos objects=main1.o foo.o main2.o bar.o 6472 1.1 christos mains=main1.o main2.o 6473 1.1 christos @end group 6474 1.1 christos @end example 6475 1.1 christos 6476 1.1 christos @noindent 6477 1.1 christos the following generates a list which contains all the object files not 6478 1.1 christos in @samp{mains}: 6479 1.1 christos 6480 1.1 christos @example 6481 1.1 christos $(filter-out $(mains),$(objects)) 6482 1.1 christos @end example 6483 1.1 christos 6484 1.1 christos @need 1500 6485 1.1 christos @findex sort 6486 1.1 christos @cindex sorting words 6487 1.1 christos @item $(sort @var{list}) 6488 1.1 christos Sorts the words of @var{list} in lexical order, removing duplicate 6489 1.1 christos words. The output is a list of words separated by single spaces. 6490 1.1 christos Thus, 6491 1.1 christos 6492 1.1 christos @example 6493 1.1 christos $(sort foo bar lose) 6494 1.1 christos @end example 6495 1.1 christos 6496 1.1 christos @noindent 6497 1.1 christos returns the value @samp{bar foo lose}. 6498 1.1 christos 6499 1.1 christos @cindex removing duplicate words 6500 1.1 christos @cindex duplicate words, removing 6501 1.1 christos @cindex words, removing duplicates 6502 1.1 christos Incidentally, since @code{sort} removes duplicate words, you can use 6503 1.1 christos it for this purpose even if you don't care about the sort order. 6504 1.1 christos 6505 1.1 christos @item $(word @var{n},@var{text}) 6506 1.1 christos @findex word 6507 1.1 christos @cindex word, selecting a 6508 1.1 christos @cindex selecting a word 6509 1.1 christos Returns the @var{n}th word of @var{text}. The legitimate values of 6510 1.1 christos @var{n} start from 1. If @var{n} is bigger than the number of words 6511 1.1 christos in @var{text}, the value is empty. For example, 6512 1.1 christos 6513 1.1 christos @example 6514 1.1 christos $(word 2, foo bar baz) 6515 1.1 christos @end example 6516 1.1 christos 6517 1.1 christos @noindent 6518 1.1 christos returns @samp{bar}. 6519 1.1 christos 6520 1.1 christos @item $(wordlist @var{s},@var{e},@var{text}) 6521 1.1 christos @findex wordlist 6522 1.1 christos @cindex words, selecting lists of 6523 1.1 christos @cindex selecting word lists 6524 1.1 christos Returns the list of words in @var{text} starting with word @var{s} and 6525 1.1 christos ending with word @var{e} (inclusive). The legitimate values of @var{s} 6526 1.1 christos start from 1; @var{e} may start from 0. If @var{s} is bigger than the 6527 1.1 christos number of words in @var{text}, the value is empty. If @var{e} is 6528 1.1 christos bigger than the number of words in @var{text}, words up to the end of 6529 1.1 christos @var{text} are returned. If @var{s} is greater than @var{e}, nothing 6530 1.1 christos is returned. For example, 6531 1.1 christos 6532 1.1 christos @example 6533 1.1 christos $(wordlist 2, 3, foo bar baz) 6534 1.1 christos @end example 6535 1.1 christos 6536 1.1 christos @noindent 6537 1.1 christos returns @samp{bar baz}. 6538 1.1 christos 6539 1.1 christos @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92 6540 1.1 christos @item $(words @var{text}) 6541 1.1 christos @findex words 6542 1.1 christos @cindex words, finding number 6543 1.1 christos Returns the number of words in @var{text}. 6544 1.1 christos Thus, the last word of @var{text} is 6545 1.1 christos @w{@code{$(word $(words @var{text}),@var{text})}}.@refill 6546 1.1 christos 6547 1.1 christos @item $(firstword @var{names}@dots{}) 6548 1.1 christos @findex firstword 6549 1.1 christos @cindex words, extracting first 6550 1.1 christos The argument @var{names} is regarded as a series of names, separated 6551 1.1 christos by whitespace. The value is the first name in the series. The rest 6552 1.1 christos of the names are ignored. 6553 1.1 christos 6554 1.1 christos For example, 6555 1.1 christos 6556 1.1 christos @example 6557 1.1 christos $(firstword foo bar) 6558 1.1 christos @end example 6559 1.1 christos 6560 1.1 christos @noindent 6561 1.1 christos produces the result @samp{foo}. Although @code{$(firstword 6562 1.1 christos @var{text})} is the same as @code{$(word 1,@var{text})}, the 6563 1.1 christos @code{firstword} function is retained for its simplicity.@refill 6564 1.1 christos 6565 1.1 christos 6566 1.1 christos @item $(lastword @var{names}@dots{}) 6567 1.1 christos @findex lastword 6568 1.1 christos @cindex words, extracting last 6569 1.1 christos The argument @var{names} is regarded as a series of names, separated 6570 1.1 christos by whitespace. The value is the last name in the series. 6571 1.1 christos 6572 1.1 christos For example, 6573 1.1 christos 6574 1.1 christos @example 6575 1.1 christos $(lastword foo bar) 6576 1.1 christos @end example 6577 1.1 christos 6578 1.1 christos @noindent 6579 1.1 christos produces the result @samp{bar}. Although @code{$(lastword 6580 1.1 christos @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})}, 6581 1.1 christos the @code{lastword} function was added for its simplicity and better 6582 1.1 christos performance.@refill 6583 1.1 christos @end table 6584 1.1 christos 6585 1.1 christos 6586 1.1 christos Here is a realistic example of the use of @code{subst} and 6587 1.1 christos @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable 6588 1.1 christos to specify a list of directories that @code{make} should search for 6589 1.1 christos prerequisite files 6590 1.1 christos (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}). 6591 1.1 christos This example shows how to 6592 1.1 christos tell the C compiler to search for header files in the same list of 6593 1.1 christos directories.@refill 6594 1.1 christos 6595 1.1 christos The value of @code{VPATH} is a list of directories separated by colons, 6596 1.1 christos such as @samp{src:../headers}. First, the @code{subst} function is used to 6597 1.1 christos change the colons to spaces: 6598 1.1 christos 6599 1.1 christos @example 6600 1.1 christos $(subst :, ,$(VPATH)) 6601 1.1 christos @end example 6602 1.1 christos 6603 1.1 christos @noindent 6604 1.1 christos This produces @samp{src ../headers}. Then @code{patsubst} is used to turn 6605 1.1 christos each directory name into a @samp{-I} flag. These can be added to the 6606 1.1 christos value of the variable @code{CFLAGS}, which is passed automatically to the C 6607 1.1 christos compiler, like this: 6608 1.1 christos 6609 1.1 christos @example 6610 1.1 christos override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH))) 6611 1.1 christos @end example 6612 1.1 christos 6613 1.1 christos @noindent 6614 1.1 christos The effect is to append the text @samp{-Isrc -I../headers} to the 6615 1.1 christos previously given value of @code{CFLAGS}. The @code{override} directive is 6616 1.1 christos used so that the new value is assigned even if the previous value of 6617 1.1 christos @code{CFLAGS} was specified with a command argument (@pxref{Override 6618 1.1 christos Directive, , The @code{override} Directive}). 6619 1.1 christos 6620 1.1 christos @node File Name Functions, Conditional Functions, Text Functions, Functions 6621 1.1 christos @section Functions for File Names 6622 1.1 christos @cindex functions, for file names 6623 1.1 christos @cindex file name functions 6624 1.1 christos 6625 1.1 christos Several of the built-in expansion functions relate specifically to 6626 1.1 christos taking apart file names or lists of file names. 6627 1.1 christos 6628 1.1 christos Each of the following functions performs a specific transformation on a 6629 1.1 christos file name. The argument of the function is regarded as a series of file 6630 1.1 christos names, separated by whitespace. (Leading and trailing whitespace is 6631 1.1 christos ignored.) Each file name in the series is transformed in the same way and 6632 1.1 christos the results are concatenated with single spaces between them. 6633 1.1 christos 6634 1.1 christos @table @code 6635 1.1 christos @item $(dir @var{names}@dots{}) 6636 1.1 christos @findex dir 6637 1.1 christos @cindex directory part 6638 1.1 christos @cindex file name, directory part 6639 1.1 christos Extracts the directory-part of each file name in @var{names}. The 6640 1.1 christos directory-part of the file name is everything up through (and 6641 1.1 christos including) the last slash in it. If the file name contains no slash, 6642 1.1 christos the directory part is the string @samp{./}. For example, 6643 1.1 christos 6644 1.1 christos @example 6645 1.1 christos $(dir src/foo.c hacks) 6646 1.1 christos @end example 6647 1.1 christos 6648 1.1 christos @noindent 6649 1.1 christos produces the result @samp{src/ ./}. 6650 1.1 christos 6651 1.1 christos @item $(notdir @var{names}@dots{}) 6652 1.1 christos @findex notdir 6653 1.1 christos @cindex file name, nondirectory part 6654 1.1 christos @cindex nondirectory part 6655 1.1 christos Extracts all but the directory-part of each file name in @var{names}. 6656 1.1 christos If the file name contains no slash, it is left unchanged. Otherwise, 6657 1.1 christos everything through the last slash is removed from it. 6658 1.1 christos 6659 1.1 christos A file name that ends with a slash becomes an empty string. This is 6660 1.1 christos unfortunate, because it means that the result does not always have the 6661 1.1 christos same number of whitespace-separated file names as the argument had; 6662 1.1 christos but we do not see any other valid alternative. 6663 1.1 christos 6664 1.1 christos For example, 6665 1.1 christos 6666 1.1 christos @example 6667 1.1 christos $(notdir src/foo.c hacks) 6668 1.1 christos @end example 6669 1.1 christos 6670 1.1 christos @noindent 6671 1.1 christos produces the result @samp{foo.c hacks}. 6672 1.1 christos 6673 1.1 christos @item $(suffix @var{names}@dots{}) 6674 1.1 christos @findex suffix 6675 1.1 christos @cindex suffix, function to find 6676 1.1 christos @cindex file name suffix 6677 1.1 christos Extracts the suffix of each file name in @var{names}. If the file name 6678 1.1 christos contains a period, the suffix is everything starting with the last 6679 1.1 christos period. Otherwise, the suffix is the empty string. This frequently 6680 1.1 christos means that the result will be empty when @var{names} is not, and if 6681 1.1 christos @var{names} contains multiple file names, the result may contain fewer 6682 1.1 christos file names. 6683 1.1 christos 6684 1.1 christos For example, 6685 1.1 christos 6686 1.1 christos @example 6687 1.1 christos $(suffix src/foo.c src-1.0/bar.c hacks) 6688 1.1 christos @end example 6689 1.1 christos 6690 1.1 christos @noindent 6691 1.1 christos produces the result @samp{.c .c}. 6692 1.1 christos 6693 1.1 christos @item $(basename @var{names}@dots{}) 6694 1.1 christos @findex basename 6695 1.1 christos @cindex basename 6696 1.1 christos @cindex file name, basename of 6697 1.1 christos Extracts all but the suffix of each file name in @var{names}. If the 6698 1.1 christos file name contains a period, the basename is everything starting up to 6699 1.1 christos (and not including) the last period. Periods in the directory part are 6700 1.1 christos ignored. If there is no period, the basename is the entire file name. 6701 1.1 christos For example, 6702 1.1 christos 6703 1.1 christos @example 6704 1.1 christos $(basename src/foo.c src-1.0/bar hacks) 6705 1.1 christos @end example 6706 1.1 christos 6707 1.1 christos @noindent 6708 1.1 christos produces the result @samp{src/foo src-1.0/bar hacks}. 6709 1.1 christos 6710 1.1 christos @c plural convention with dots (be consistent) 6711 1.1 christos @item $(addsuffix @var{suffix},@var{names}@dots{}) 6712 1.1 christos @findex addsuffix 6713 1.1 christos @cindex suffix, adding 6714 1.1 christos @cindex file name suffix, adding 6715 1.1 christos The argument @var{names} is regarded as a series of names, separated 6716 1.1 christos by whitespace; @var{suffix} is used as a unit. The value of 6717 1.1 christos @var{suffix} is appended to the end of each individual name and the 6718 1.1 christos resulting larger names are concatenated with single spaces between 6719 1.1 christos them. For example, 6720 1.1 christos 6721 1.1 christos @example 6722 1.1 christos $(addsuffix .c,foo bar) 6723 1.1 christos @end example 6724 1.1 christos 6725 1.1 christos @noindent 6726 1.1 christos produces the result @samp{foo.c bar.c}. 6727 1.1 christos 6728 1.1 christos @item $(addprefix @var{prefix},@var{names}@dots{}) 6729 1.1 christos @findex addprefix 6730 1.1 christos @cindex prefix, adding 6731 1.1 christos @cindex file name prefix, adding 6732 1.1 christos The argument @var{names} is regarded as a series of names, separated 6733 1.1 christos by whitespace; @var{prefix} is used as a unit. The value of 6734 1.1 christos @var{prefix} is prepended to the front of each individual name and the 6735 1.1 christos resulting larger names are concatenated with single spaces between 6736 1.1 christos them. For example, 6737 1.1 christos 6738 1.1 christos @example 6739 1.1 christos $(addprefix src/,foo bar) 6740 1.1 christos @end example 6741 1.1 christos 6742 1.1 christos @noindent 6743 1.1 christos produces the result @samp{src/foo src/bar}. 6744 1.1 christos 6745 1.1 christos @item $(join @var{list1},@var{list2}) 6746 1.1 christos @findex join 6747 1.1 christos @cindex joining lists of words 6748 1.1 christos @cindex words, joining lists 6749 1.1 christos Concatenates the two arguments word by word: the two first words (one 6750 1.1 christos from each argument) concatenated form the first word of the result, the 6751 1.1 christos two second words form the second word of the result, and so on. So the 6752 1.1 christos @var{n}th word of the result comes from the @var{n}th word of each 6753 1.1 christos argument. If one argument has more words that the other, the extra 6754 1.1 christos words are copied unchanged into the result. 6755 1.1 christos 6756 1.1 christos For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}. 6757 1.1 christos 6758 1.1 christos Whitespace between the words in the lists is not preserved; it is 6759 1.1 christos replaced with a single space. 6760 1.1 christos 6761 1.1 christos This function can merge the results of the @code{dir} and 6762 1.1 christos @code{notdir} functions, to produce the original list of files which 6763 1.1 christos was given to those two functions.@refill 6764 1.1 christos 6765 1.1 christos @item $(wildcard @var{pattern}) 6766 1.1 christos @findex wildcard 6767 1.1 christos @cindex wildcard, function 6768 1.1 christos The argument @var{pattern} is a file name pattern, typically containing 6769 1.1 christos wildcard characters (as in shell file name patterns). The result of 6770 1.1 christos @code{wildcard} is a space-separated list of the names of existing files 6771 1.1 christos that match the pattern. 6772 1.1 christos @xref{Wildcards, ,Using Wildcard Characters in File Names}. 6773 1.1 christos 6774 1.1 christos @item $(realpath @var{names}@dots{}) 6775 1.1 christos @findex realpath 6776 1.1 christos @cindex realpath 6777 1.1 christos @cindex file name, realpath of 6778 1.1 christos For each file name in @var{names} return the canonical absolute name. 6779 1.1 christos A canonical name does not contain any @code{.} or @code{..} components, 6780 1.1 christos nor any repeated path separators (@code{/}) or symlinks. In case of a 6781 1.1 christos failure the empty string is returned. Consult the @code{realpath(3)} 6782 1.1 christos documentation for a list of possible failure causes. 6783 1.1 christos 6784 1.1 christos @item $(abspath @var{names}@dots{}) 6785 1.1 christos @findex abspath 6786 1.1 christos @cindex abspath 6787 1.1 christos @cindex file name, abspath of 6788 1.1 christos For each file name in @var{names} return an absolute name that does 6789 1.1 christos not contain any @code{.} or @code{..} components, nor any repeated path 6790 1.1 christos separators (@code{/}). Note that, in contrast to @code{realpath} 6791 1.1 christos function, @code{abspath} does not resolve symlinks and does not require 6792 1.1 christos the file names to refer to an existing file or directory. Use the 6793 1.1 christos @code{wildcard} function to test for existence. 6794 1.1 christos @end table 6795 1.1 christos 6796 1.1 christos @node Conditional Functions, Foreach Function, File Name Functions, Functions 6797 1.1 christos @section Functions for Conditionals 6798 1.1 christos @findex if 6799 1.1 christos @cindex conditional expansion 6800 1.1 christos There are three functions that provide conditional expansion. A key 6801 1.1 christos aspect of these functions is that not all of the arguments are 6802 1.1 christos expanded initially. Only those arguments which need to be expanded, 6803 1.1 christos will be expanded. 6804 1.1 christos 6805 1.1 christos @table @code 6806 1.1 christos @item $(if @var{condition},@var{then-part}[,@var{else-part}]) 6807 1.1 christos @findex if 6808 1.1 christos The @code{if} function provides support for conditional expansion in a 6809 1.1 christos functional context (as opposed to the GNU @code{make} makefile 6810 1.1 christos conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of 6811 1.1 christos Conditionals}). 6812 1.1 christos 6813 1.1 christos The first argument, @var{condition}, first has all preceding and 6814 1.1 christos trailing whitespace stripped, then is expanded. If it expands to any 6815 1.1 christos non-empty string, then the condition is considered to be true. If it 6816 1.1 christos expands to an empty string, the condition is considered to be false. 6817 1.1 christos 6818 1.1 christos If the condition is true then the second argument, @var{then-part}, is 6819 1.1 christos evaluated and this is used as the result of the evaluation of the entire 6820 1.1 christos @code{if} function. 6821 1.1 christos 6822 1.1 christos If the condition is false then the third argument, @var{else-part}, is 6823 1.1 christos evaluated and this is the result of the @code{if} function. If there is 6824 1.1 christos no third argument, the @code{if} function evaluates to nothing (the 6825 1.1 christos empty string). 6826 1.1 christos 6827 1.1 christos Note that only one of the @var{then-part} or the @var{else-part} will be 6828 1.1 christos evaluated, never both. Thus, either can contain side-effects (such as 6829 1.1 christos @code{shell} function calls, etc.) 6830 1.1 christos 6831 1.1 christos @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]]) 6832 1.1 christos @findex or 6833 1.1 christos The @code{or} function provides a ``short-circuiting'' OR operation. 6834 1.1 christos Each argument is expanded, in order. If an argument expands to a 6835 1.1 christos non-empty string the processing stops and the result of the expansion 6836 1.1 christos is that string. If, after all arguments are expanded, all of them are 6837 1.1 christos false (empty), then the result of the expansion is the empty string. 6838 1.1 christos 6839 1.1 christos @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]]) 6840 1.1 christos @findex and 6841 1.1 christos The @code{and} function provides a ``short-circuiting'' AND operation. 6842 1.1 christos Each argument is expanded, in order. If an argument expands to an 6843 1.1 christos empty string the processing stops and the result of the expansion is 6844 1.1 christos the empty string. If all arguments expand to a non-empty string then 6845 1.1 christos the result of the expansion is the expansion of the last argument. 6846 1.1 christos 6847 1.1 christos @end table 6848 1.1 christos 6849 1.1 christos @node Foreach Function, Call Function, Conditional Functions, Functions 6850 1.1 christos @section The @code{foreach} Function 6851 1.1 christos @findex foreach 6852 1.1 christos @cindex words, iterating over 6853 1.1 christos 6854 1.1 christos The @code{foreach} function is very different from other functions. It 6855 1.1 christos causes one piece of text to be used repeatedly, each time with a different 6856 1.1 christos substitution performed on it. It resembles the @code{for} command in the 6857 1.1 christos shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}. 6858 1.1 christos 6859 1.1 christos The syntax of the @code{foreach} function is: 6860 1.1 christos 6861 1.1 christos @example 6862 1.1 christos $(foreach @var{var},@var{list},@var{text}) 6863 1.1 christos @end example 6864 1.1 christos 6865 1.1 christos @noindent 6866 1.1 christos The first two arguments, @var{var} and @var{list}, are expanded before 6867 1.1 christos anything else is done; note that the last argument, @var{text}, is 6868 1.1 christos @strong{not} expanded at the same time. Then for each word of the expanded 6869 1.1 christos value of @var{list}, the variable named by the expanded value of @var{var} 6870 1.1 christos is set to that word, and @var{text} is expanded. Presumably @var{text} 6871 1.1 christos contains references to that variable, so its expansion will be different 6872 1.1 christos each time. 6873 1.1 christos 6874 1.1 christos The result is that @var{text} is expanded as many times as there are 6875 1.1 christos whitespace-separated words in @var{list}. The multiple expansions of 6876 1.1 christos @var{text} are concatenated, with spaces between them, to make the result 6877 1.1 christos of @code{foreach}. 6878 1.1 christos 6879 1.1 christos This simple example sets the variable @samp{files} to the list of all files 6880 1.1 christos in the directories in the list @samp{dirs}: 6881 1.1 christos 6882 1.1 christos @example 6883 1.1 christos dirs := a b c d 6884 1.1 christos files := $(foreach dir,$(dirs),$(wildcard $(dir)/*)) 6885 1.1 christos @end example 6886 1.1 christos 6887 1.1 christos Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition 6888 1.1 christos finds the value @samp{a} for @code{dir}, so it produces the same result 6889 1.1 christos as @samp{$(wildcard a/*)}; the second repetition produces the result 6890 1.1 christos of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}. 6891 1.1 christos 6892 1.1 christos This example has the same result (except for setting @samp{dirs}) as 6893 1.1 christos the following example: 6894 1.1 christos 6895 1.1 christos @example 6896 1.1 christos files := $(wildcard a/* b/* c/* d/*) 6897 1.1 christos @end example 6898 1.1 christos 6899 1.1 christos When @var{text} is complicated, you can improve readability by giving it 6900 1.1 christos a name, with an additional variable: 6901 1.1 christos 6902 1.1 christos @example 6903 1.1 christos find_files = $(wildcard $(dir)/*) 6904 1.1 christos dirs := a b c d 6905 1.1 christos files := $(foreach dir,$(dirs),$(find_files)) 6906 1.1 christos @end example 6907 1.1 christos 6908 1.1 christos @noindent 6909 1.1 christos Here we use the variable @code{find_files} this way. We use plain @samp{=} 6910 1.1 christos to define a recursively-expanding variable, so that its value contains an 6911 1.1 christos actual function call to be reexpanded under the control of @code{foreach}; 6912 1.1 christos a simply-expanded variable would not do, since @code{wildcard} would be 6913 1.1 christos called only once at the time of defining @code{find_files}. 6914 1.1 christos 6915 1.1 christos The @code{foreach} function has no permanent effect on the variable 6916 1.1 christos @var{var}; its value and flavor after the @code{foreach} function call are 6917 1.1 christos the same as they were beforehand. The other values which are taken from 6918 1.1 christos @var{list} are in effect only temporarily, during the execution of 6919 1.1 christos @code{foreach}. The variable @var{var} is a simply-expanded variable 6920 1.1 christos during the execution of @code{foreach}. If @var{var} was undefined 6921 1.1 christos before the @code{foreach} function call, it is undefined after the call. 6922 1.1 christos @xref{Flavors, ,The Two Flavors of Variables}.@refill 6923 1.1 christos 6924 1.1 christos You must take care when using complex variable expressions that result in 6925 1.1 christos variable names because many strange things are valid variable names, but 6926 1.1 christos are probably not what you intended. For example, 6927 1.1 christos 6928 1.1 christos @smallexample 6929 1.1 christos files := $(foreach Esta escrito en espanol!,b c ch,$(find_files)) 6930 1.1 christos @end smallexample 6931 1.1 christos 6932 1.1 christos @noindent 6933 1.1 christos might be useful if the value of @code{find_files} references the variable 6934 1.1 christos whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo, 6935 1.1 christos no?), but it is more likely to be a mistake. 6936 1.1 christos 6937 1.1 christos @node Call Function, Value Function, Foreach Function, Functions 6938 1.1 christos @section The @code{call} Function 6939 1.1 christos @findex call 6940 1.1 christos @cindex functions, user defined 6941 1.1 christos @cindex user defined functions 6942 1.1 christos 6943 1.1 christos The @code{call} function is unique in that it can be used to create new 6944 1.1 christos parameterized functions. You can write a complex expression as the 6945 1.1 christos value of a variable, then use @code{call} to expand it with different 6946 1.1 christos values. 6947 1.1 christos 6948 1.1 christos The syntax of the @code{call} function is: 6949 1.1 christos 6950 1.1 christos @example 6951 1.1 christos $(call @var{variable},@var{param},@var{param},@dots{}) 6952 1.1 christos @end example 6953 1.1 christos 6954 1.1 christos When @code{make} expands this function, it assigns each @var{param} to 6955 1.1 christos temporary variables @code{$(1)}, @code{$(2)}, etc. The variable 6956 1.1 christos @code{$(0)} will contain @var{variable}. There is no maximum number of 6957 1.1 christos parameter arguments. There is no minimum, either, but it doesn't make 6958 1.1 christos sense to use @code{call} with no parameters. 6959 1.1 christos 6960 1.1 christos Then @var{variable} is expanded as a @code{make} variable in the context 6961 1.1 christos of these temporary assignments. Thus, any reference to @code{$(1)} in 6962 1.1 christos the value of @var{variable} will resolve to the first @var{param} in the 6963 1.1 christos invocation of @code{call}. 6964 1.1 christos 6965 1.1 christos Note that @var{variable} is the @emph{name} of a variable, not a 6966 1.1 christos @emph{reference} to that variable. Therefore you would not normally use 6967 1.1 christos a @samp{$} or parentheses when writing it. (You can, however, use a 6968 1.1 christos variable reference in the name if you want the name not to be a 6969 1.1 christos constant.) 6970 1.1 christos 6971 1.1 christos If @var{variable} is the name of a builtin function, the builtin function 6972 1.1 christos is always invoked (even if a @code{make} variable by that name also 6973 1.1 christos exists). 6974 1.1 christos 6975 1.1 christos The @code{call} function expands the @var{param} arguments before 6976 1.1 christos assigning them to temporary variables. This means that @var{variable} 6977 1.1 christos values containing references to builtin functions that have special 6978 1.1 christos expansion rules, like @code{foreach} or @code{if}, may not work as you 6979 1.1 christos expect. 6980 1.1 christos 6981 1.1 christos Some examples may make this clearer. 6982 1.1 christos 6983 1.1 christos This macro simply reverses its arguments: 6984 1.1 christos 6985 1.1 christos @smallexample 6986 1.1 christos reverse = $(2) $(1) 6987 1.1 christos 6988 1.1 christos foo = $(call reverse,a,b) 6989 1.1 christos @end smallexample 6990 1.1 christos 6991 1.1 christos @noindent 6992 1.1 christos Here @var{foo} will contain @samp{b a}. 6993 1.1 christos 6994 1.1 christos This one is slightly more interesting: it defines a macro to search for 6995 1.1 christos the first instance of a program in @code{PATH}: 6996 1.1 christos 6997 1.1 christos @smallexample 6998 1.1 christos pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) 6999 1.1 christos 7000 1.1 christos LS := $(call pathsearch,ls) 7001 1.1 christos @end smallexample 7002 1.1 christos 7003 1.1 christos @noindent 7004 1.1 christos Now the variable LS contains @code{/bin/ls} or similar. 7005 1.1 christos 7006 1.1 christos The @code{call} function can be nested. Each recursive invocation gets 7007 1.1 christos its own local values for @code{$(1)}, etc.@: that mask the values of 7008 1.1 christos higher-level @code{call}. For example, here is an implementation of a 7009 1.1 christos @dfn{map} function: 7010 1.1 christos 7011 1.1 christos @smallexample 7012 1.1 christos map = $(foreach a,$(2),$(call $(1),$(a))) 7013 1.1 christos @end smallexample 7014 1.1 christos 7015 1.1 christos Now you can @var{map} a function that normally takes only one argument, 7016 1.1 christos such as @code{origin}, to multiple values in one step: 7017 1.1 christos 7018 1.1 christos @smallexample 7019 1.1 christos o = $(call map,origin,o map MAKE) 7020 1.1 christos @end smallexample 7021 1.1 christos 7022 1.1 christos and end up with @var{o} containing something like @samp{file file default}. 7023 1.1 christos 7024 1.1 christos A final caution: be careful when adding whitespace to the arguments to 7025 1.1 christos @code{call}. As with other functions, any whitespace contained in the 7026 1.1 christos second and subsequent arguments is kept; this can cause strange 7027 1.1 christos effects. It's generally safest to remove all extraneous whitespace when 7028 1.1 christos providing parameters to @code{call}. 7029 1.1 christos 7030 1.1 christos @node Value Function, Eval Function, Call Function, Functions 7031 1.1 christos @comment node-name, next, previous, up 7032 1.1 christos @section The @code{value} Function 7033 1.1 christos @findex value 7034 1.1 christos @cindex variables, unexpanded value 7035 1.1 christos 7036 1.1 christos The @code{value} function provides a way for you to use the value of a 7037 1.1 christos variable @emph{without} having it expanded. Please note that this 7038 1.1 christos does not undo expansions which have already occurred; for example if 7039 1.1 christos you create a simply expanded variable its value is expanded during the 7040 1.1 christos definition; in that case the @code{value} function will return the 7041 1.1 christos same result as using the variable directly. 7042 1.1 christos 7043 1.1 christos The syntax of the @code{value} function is: 7044 1.1 christos 7045 1.1 christos @example 7046 1.1 christos $(value @var{variable}) 7047 1.1 christos @end example 7048 1.1 christos 7049 1.1 christos Note that @var{variable} is the @emph{name} of a variable; not a 7050 1.1 christos @emph{reference} to that variable. Therefore you would not normally 7051 1.1 christos use a @samp{$} or parentheses when writing it. (You can, however, use 7052 1.1 christos a variable reference in the name if you want the name not to be a 7053 1.1 christos constant.) 7054 1.1 christos 7055 1.1 christos The result of this function is a string containing the value of 7056 1.1 christos @var{variable}, without any expansion occurring. For example, in this 7057 1.1 christos makefile: 7058 1.1 christos 7059 1.1 christos @example 7060 1.1 christos @group 7061 1.1 christos FOO = $PATH 7062 1.1 christos 7063 1.1 christos all: 7064 1.1 christos @@echo $(FOO) 7065 1.1 christos @@echo $(value FOO) 7066 1.1 christos @end group 7067 1.1 christos @end example 7068 1.1 christos 7069 1.1 christos @noindent 7070 1.1 christos The first output line would be @code{ATH}, since the ``$P'' would be 7071 1.1 christos expanded as a @code{make} variable, while the second output line would 7072 1.1 christos be the current value of your @code{$PATH} environment variable, since 7073 1.1 christos the @code{value} function avoided the expansion. 7074 1.1 christos 7075 1.1 christos The @code{value} function is most often used in conjunction with the 7076 1.1 christos @code{eval} function (@pxref{Eval Function}). 7077 1.1 christos 7078 1.1 christos @node Eval Function, Origin Function, Value Function, Functions 7079 1.1 christos @comment node-name, next, previous, up 7080 1.1 christos @section The @code{eval} Function 7081 1.1 christos @findex eval 7082 1.1 christos @cindex evaluating makefile syntax 7083 1.1 christos @cindex makefile syntax, evaluating 7084 1.1 christos 7085 1.1 christos The @code{eval} function is very special: it allows you to define new 7086 1.1 christos makefile constructs that are not constant; which are the result of 7087 1.1 christos evaluating other variables and functions. The argument to the 7088 1.1 christos @code{eval} function is expanded, then the results of that expansion 7089 1.1 christos are parsed as makefile syntax. The expanded results can define new 7090 1.1 christos @code{make} variables, targets, implicit or explicit rules, etc. 7091 1.1 christos 7092 1.1 christos The result of the @code{eval} function is always the empty string; 7093 1.1 christos thus, it can be placed virtually anywhere in a makefile without 7094 1.1 christos causing syntax errors. 7095 1.1 christos 7096 1.1 christos It's important to realize that the @code{eval} argument is expanded 7097 1.1 christos @emph{twice}; first by the @code{eval} function, then the results of 7098 1.1 christos that expansion are expanded again when they are parsed as makefile 7099 1.1 christos syntax. This means you may need to provide extra levels of escaping 7100 1.1 christos for ``$'' characters when using @code{eval}. The @code{value} 7101 1.1 christos function (@pxref{Value Function}) can sometimes be useful in these 7102 1.1 christos situations, to circumvent unwanted expansions. 7103 1.1 christos 7104 1.1 christos Here is an example of how @code{eval} can be used; this example 7105 1.1 christos combines a number of concepts and other functions. Although it might 7106 1.1 christos seem overly complex to use @code{eval} in this example, rather than 7107 1.1 christos just writing out the rules, consider two things: first, the template 7108 1.1 christos definition (in @code{PROGRAM_template}) could need to be much more 7109 1.1 christos complex than it is here; and second, you might put the complex, 7110 1.1 christos ``generic'' part of this example into another makefile, then include 7111 1.1 christos it in all the individual makefiles. Now your individual makefiles are 7112 1.1 christos quite straightforward. 7113 1.1 christos 7114 1.1 christos @example 7115 1.1 christos @group 7116 1.1 christos PROGRAMS = server client 7117 1.1 christos 7118 1.1 christos server_OBJS = server.o server_priv.o server_access.o 7119 1.1 christos server_LIBS = priv protocol 7120 1.1 christos 7121 1.1 christos client_OBJS = client.o client_api.o client_mem.o 7122 1.1 christos client_LIBS = protocol 7123 1.1 christos 7124 1.1 christos # Everything after this is generic 7125 1.1 christos 7126 1.1 christos .PHONY: all 7127 1.1 christos all: $(PROGRAMS) 7128 1.1 christos 7129 1.1 christos define PROGRAM_template 7130 1.1 christos $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) 7131 1.1 christos ALL_OBJS += $$($(1)_OBJS) 7132 1.1 christos endef 7133 1.1 christos 7134 1.1 christos $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog)))) 7135 1.1 christos 7136 1.1 christos $(PROGRAMS): 7137 1.1 christos $(LINK.o) $^ $(LDLIBS) -o $@@ 7138 1.1 christos 7139 1.1 christos clean: 7140 1.1 christos rm -f $(ALL_OBJS) $(PROGRAMS) 7141 1.1 christos @end group 7142 1.1 christos @end example 7143 1.1 christos 7144 1.1 christos @node Origin Function, Flavor Function, Eval Function, Functions 7145 1.1 christos @section The @code{origin} Function 7146 1.1 christos @findex origin 7147 1.1 christos @cindex variables, origin of 7148 1.1 christos @cindex origin of variable 7149 1.1 christos 7150 1.1 christos The @code{origin} function is unlike most other functions in that it does 7151 1.1 christos not operate on the values of variables; it tells you something @emph{about} 7152 1.1 christos a variable. Specifically, it tells you where it came from. 7153 1.1 christos 7154 1.1 christos The syntax of the @code{origin} function is: 7155 1.1 christos 7156 1.1 christos @example 7157 1.1 christos $(origin @var{variable}) 7158 1.1 christos @end example 7159 1.1 christos 7160 1.1 christos Note that @var{variable} is the @emph{name} of a variable to inquire about; 7161 1.1 christos not a @emph{reference} to that variable. Therefore you would not normally 7162 1.1 christos use a @samp{$} or parentheses when writing it. (You can, however, use a 7163 1.1 christos variable reference in the name if you want the name not to be a constant.) 7164 1.1 christos 7165 1.1 christos The result of this function is a string telling you how the variable 7166 1.1 christos @var{variable} was defined: 7167 1.1 christos 7168 1.1 christos @table @samp 7169 1.1 christos @item undefined 7170 1.1 christos 7171 1.1 christos if @var{variable} was never defined. 7172 1.1 christos 7173 1.1 christos @item default 7174 1.1 christos 7175 1.1 christos if @var{variable} has a default definition, as is usual with @code{CC} 7176 1.1 christos and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}. 7177 1.1 christos Note that if you have redefined a default variable, the @code{origin} 7178 1.1 christos function will return the origin of the later definition. 7179 1.1 christos 7180 1.1 christos @item environment 7181 1.1 christos 7182 1.1 christos if @var{variable} was defined as an environment variable and the 7183 1.1 christos @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}). 7184 1.1 christos 7185 1.1 christos @item environment override 7186 1.1 christos 7187 1.1 christos if @var{variable} was defined as an environment variable and the 7188 1.1 christos @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary, 7189 1.1 christos ,Summary of Options}).@refill 7190 1.1 christos 7191 1.1 christos @item file 7192 1.1 christos 7193 1.1 christos if @var{variable} was defined in a makefile. 7194 1.1 christos 7195 1.1 christos @item command line 7196 1.1 christos 7197 1.1 christos if @var{variable} was defined on the command line. 7198 1.1 christos 7199 1.1 christos @item override 7200 1.1 christos 7201 1.1 christos if @var{variable} was defined with an @code{override} directive in a 7202 1.1 christos makefile (@pxref{Override Directive, ,The @code{override} Directive}). 7203 1.1 christos 7204 1.1 christos @item automatic 7205 1.1 christos 7206 1.1 christos if @var{variable} is an automatic variable defined for the 7207 1.1 christos execution of the commands for each rule 7208 1.1 christos (@pxref{Automatic Variables}). 7209 1.1 christos @end table 7210 1.1 christos 7211 1.1 christos This information is primarily useful (other than for your curiosity) to 7212 1.1 christos determine if you want to believe the value of a variable. For example, 7213 1.1 christos suppose you have a makefile @file{foo} that includes another makefile 7214 1.1 christos @file{bar}. You want a variable @code{bletch} to be defined in @file{bar} 7215 1.1 christos if you run the command @w{@samp{make -f bar}}, even if the environment contains 7216 1.1 christos a definition of @code{bletch}. However, if @file{foo} defined 7217 1.1 christos @code{bletch} before including @file{bar}, you do not want to override that 7218 1.1 christos definition. This could be done by using an @code{override} directive in 7219 1.1 christos @file{foo}, giving that definition precedence over the later definition in 7220 1.1 christos @file{bar}; unfortunately, the @code{override} directive would also 7221 1.1 christos override any command line definitions. So, @file{bar} could 7222 1.1 christos include:@refill 7223 1.1 christos 7224 1.1 christos @example 7225 1.1 christos @group 7226 1.1 christos ifdef bletch 7227 1.1 christos ifeq "$(origin bletch)" "environment" 7228 1.1 christos bletch = barf, gag, etc. 7229 1.1 christos endif 7230 1.1 christos endif 7231 1.1 christos @end group 7232 1.1 christos @end example 7233 1.1 christos 7234 1.1 christos @noindent 7235 1.1 christos If @code{bletch} has been defined from the environment, this will redefine 7236 1.1 christos it. 7237 1.1 christos 7238 1.1 christos If you want to override a previous definition of @code{bletch} if it came 7239 1.1 christos from the environment, even under @samp{-e}, you could instead write: 7240 1.1 christos 7241 1.1 christos @example 7242 1.1 christos @group 7243 1.1 christos ifneq "$(findstring environment,$(origin bletch))" "" 7244 1.1 christos bletch = barf, gag, etc. 7245 1.1 christos endif 7246 1.1 christos @end group 7247 1.1 christos @end example 7248 1.1 christos 7249 1.1 christos Here the redefinition takes place if @samp{$(origin bletch)} returns either 7250 1.1 christos @samp{environment} or @samp{environment override}. 7251 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 7252 1.1 christos 7253 1.1 christos @node Flavor Function, Shell Function, Origin Function, Functions 7254 1.1 christos @section The @code{flavor} Function 7255 1.1 christos @findex flavor 7256 1.1 christos @cindex variables, flavor of 7257 1.1 christos @cindex flavor of variable 7258 1.1 christos 7259 1.1 christos The @code{flavor} function is unlike most other functions (and like 7260 1.1 christos @code{origin} function) in that it does not operate on the values of 7261 1.1 christos variables; it tells you something @emph{about} a variable. 7262 1.1 christos Specifically, it tells you the flavor of a variable (@pxref{Flavors, 7263 1.1 christos ,The Two Flavors of Variables}). 7264 1.1 christos 7265 1.1 christos The syntax of the @code{flavor} function is: 7266 1.1 christos 7267 1.1 christos @example 7268 1.1 christos $(flavor @var{variable}) 7269 1.1 christos @end example 7270 1.1 christos 7271 1.1 christos Note that @var{variable} is the @emph{name} of a variable to inquire about; 7272 1.1 christos not a @emph{reference} to that variable. Therefore you would not normally 7273 1.1 christos use a @samp{$} or parentheses when writing it. (You can, however, use a 7274 1.1 christos variable reference in the name if you want the name not to be a constant.) 7275 1.1 christos 7276 1.1 christos The result of this function is a string that identifies the flavor of the 7277 1.1 christos variable @var{variable}: 7278 1.1 christos 7279 1.1 christos @table @samp 7280 1.1 christos @item undefined 7281 1.1 christos 7282 1.1 christos if @var{variable} was never defined. 7283 1.1 christos 7284 1.1 christos @item recursive 7285 1.1 christos 7286 1.1 christos if @var{variable} is a recursively expanded variable. 7287 1.1 christos 7288 1.1 christos @item simple 7289 1.1 christos 7290 1.1 christos if @var{variable} is a simply expanded variable. 7291 1.1 christos 7292 1.1 christos @end table 7293 1.1 christos 7294 1.1 christos 7295 1.1 christos @node Shell Function, Make Control Functions, Flavor Function, Functions 7296 1.1 christos @section The @code{shell} Function 7297 1.1 christos @findex shell 7298 1.1 christos @cindex commands, expansion 7299 1.1 christos @cindex backquotes 7300 1.1 christos @cindex shell command, function for 7301 1.1 christos 7302 1.1 christos The @code{shell} function is unlike any other function other than the 7303 1.1 christos @code{wildcard} function 7304 1.1 christos (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it 7305 1.1 christos communicates with the world outside of @code{make}. 7306 1.1 christos 7307 1.1 christos The @code{shell} function performs the same function that backquotes 7308 1.1 christos (@samp{`}) perform in most shells: it does @dfn{command expansion}. 7309 1.1 christos This means that it takes as an argument a shell command and evaluates 7310 1.1 christos to the output of the command. The only processing @code{make} does on 7311 1.1 christos the result is to convert each newline (or carriage-return / newline 7312 1.1 christos pair) to a single space. If there is a trailing (carriage-return 7313 1.1 christos and) newline it will simply be removed.@refill 7314 1.1 christos 7315 1.1 christos The commands run by calls to the @code{shell} function are run when the 7316 1.1 christos function calls are expanded (@pxref{Reading Makefiles, , How 7317 1.1 christos @code{make} Reads a Makefile}). Because this function involves 7318 1.1 christos spawning a new shell, you should carefully consider the performance 7319 1.1 christos implications of using the @code{shell} function within recursively 7320 1.1 christos expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The 7321 1.1 christos Two Flavors of Variables}). 7322 1.1 christos 7323 1.1 christos Here are some examples of the use of the @code{shell} function: 7324 1.1 christos 7325 1.1 christos @example 7326 1.1 christos contents := $(shell cat foo) 7327 1.1 christos @end example 7328 1.1 christos 7329 1.1 christos @noindent 7330 1.1 christos sets @code{contents} to the contents of the file @file{foo}, with a space 7331 1.1 christos (rather than a newline) separating each line. 7332 1.1 christos 7333 1.1 christos @example 7334 1.1 christos files := $(shell echo *.c) 7335 1.1 christos @end example 7336 1.1 christos 7337 1.1 christos @noindent 7338 1.1 christos sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is 7339 1.1 christos using a very strange shell, this has the same result as 7340 1.1 christos @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file 7341 1.1 christos exists).@refill 7342 1.1 christos 7343 1.1 christos @node Make Control Functions, , Shell Function, Functions 7344 1.1 christos @section Functions That Control Make 7345 1.1 christos @cindex functions, for controlling make 7346 1.1 christos @cindex controlling make 7347 1.1 christos 7348 1.1 christos These functions control the way make runs. Generally, they are used to 7349 1.1 christos provide information to the user of the makefile or to cause make to stop 7350 1.1 christos if some sort of environmental error is detected. 7351 1.1 christos 7352 1.1 christos @table @code 7353 1.1 christos @item $(error @var{text}@dots{}) 7354 1.1 christos @findex error 7355 1.1 christos @cindex error, stopping on 7356 1.1 christos @cindex stopping make 7357 1.1 christos Generates a fatal error where the message is @var{text}. Note that the 7358 1.1 christos error is generated whenever this function is evaluated. So, if you put 7359 1.1 christos it inside a command script or on the right side of a recursive variable 7360 1.1 christos assignment, it won't be evaluated until later. The @var{text} will be 7361 1.1 christos expanded before the error is generated. 7362 1.1 christos 7363 1.1 christos For example, 7364 1.1 christos 7365 1.1 christos @example 7366 1.1 christos ifdef ERROR1 7367 1.1 christos $(error error is $(ERROR1)) 7368 1.1 christos endif 7369 1.1 christos @end example 7370 1.1 christos 7371 1.1 christos @noindent 7372 1.1 christos will generate a fatal error during the read of the makefile if the 7373 1.1 christos @code{make} variable @code{ERROR1} is defined. Or, 7374 1.1 christos 7375 1.1 christos @example 7376 1.1 christos ERR = $(error found an error!) 7377 1.1 christos 7378 1.1 christos .PHONY: err 7379 1.1 christos err: ; $(ERR) 7380 1.1 christos @end example 7381 1.1 christos 7382 1.1 christos @noindent 7383 1.1 christos will generate a fatal error while @code{make} is running, if the 7384 1.1 christos @code{err} target is invoked. 7385 1.1 christos 7386 1.1 christos @item $(warning @var{text}@dots{}) 7387 1.1 christos @findex warning 7388 1.1 christos @cindex warnings, printing 7389 1.1 christos @cindex printing user warnings 7390 1.1 christos This function works similarly to the @code{error} function, above, 7391 1.1 christos except that @code{make} doesn't exit. Instead, @var{text} is expanded 7392 1.1 christos and the resulting message is displayed, but processing of the makefile 7393 1.1 christos continues. 7394 1.1 christos 7395 1.1 christos The result of the expansion of this function is the empty string. 7396 1.1 christos 7397 1.1 christos @item $(info @var{text}@dots{}) 7398 1.1 christos @findex info 7399 1.1 christos @cindex printing messages 7400 1.1 christos This function does nothing more than print its (expanded) argument(s) 7401 1.1 christos to standard output. No makefile name or line number is added. The 7402 1.1 christos result of the expansion of this function is the empty string. 7403 1.1 christos @end table 7404 1.1 christos 7405 1.1 christos @node Running, Implicit Rules, Functions, Top 7406 1.1 christos @chapter How to Run @code{make} 7407 1.1 christos 7408 1.1 christos A makefile that says how to recompile a program can be used in more 7409 1.1 christos than one way. The simplest use is to recompile every file that is out 7410 1.1 christos of date. Usually, makefiles are written so that if you run 7411 1.1 christos @code{make} with no arguments, it does just that. 7412 1.1 christos 7413 1.1 christos But you might want to update only some of the files; you might want to use 7414 1.1 christos a different compiler or different compiler options; you might want just to 7415 1.1 christos find out which files are out of date without changing them. 7416 1.1 christos 7417 1.1 christos By giving arguments when you run @code{make}, you can do any of these 7418 1.1 christos things and many others. 7419 1.1 christos 7420 1.1 christos @cindex exit status of make 7421 1.1 christos The exit status of @code{make} is always one of three values: 7422 1.1 christos @table @code 7423 1.1 christos @item 0 7424 1.1 christos The exit status is zero if @code{make} is successful. 7425 1.1 christos @item 2 7426 1.1 christos The exit status is two if @code{make} encounters any errors. 7427 1.1 christos It will print messages describing the particular errors. 7428 1.1 christos @item 1 7429 1.1 christos The exit status is one if you use the @samp{-q} flag and @code{make} 7430 1.1 christos determines that some target is not already up to date. 7431 1.1 christos @xref{Instead of Execution, ,Instead of Executing the Commands}. 7432 1.1 christos @end table 7433 1.1 christos 7434 1.1 christos @menu 7435 1.1 christos * Makefile Arguments:: How to specify which makefile to use. 7436 1.1 christos * Goals:: How to use goal arguments to specify which 7437 1.1 christos parts of the makefile to use. 7438 1.1 christos * Instead of Execution:: How to use mode flags to specify what 7439 1.1 christos kind of thing to do with the commands 7440 1.1 christos in the makefile other than simply 7441 1.1 christos execute them. 7442 1.1 christos * Avoiding Compilation:: How to avoid recompiling certain files. 7443 1.1 christos * Overriding:: How to override a variable to specify 7444 1.1 christos an alternate compiler and other things. 7445 1.1 christos * Testing:: How to proceed past some errors, to 7446 1.1 christos test compilation. 7447 1.1 christos * Options Summary:: Summary of Options 7448 1.1 christos @end menu 7449 1.1 christos 7450 1.1 christos @node Makefile Arguments, Goals, Running, Running 7451 1.1 christos @section Arguments to Specify the Makefile 7452 1.1 christos @cindex @code{--file} 7453 1.1 christos @cindex @code{--makefile} 7454 1.1 christos @cindex @code{-f} 7455 1.1 christos 7456 1.1 christos The way to specify the name of the makefile is with the @samp{-f} or 7457 1.1 christos @samp{--file} option (@samp{--makefile} also works). For example, 7458 1.1 christos @samp{-f altmake} says to use the file @file{altmake} as the makefile. 7459 1.1 christos 7460 1.1 christos If you use the @samp{-f} flag several times and follow each @samp{-f} 7461 1.1 christos with an argument, all the specified files are used jointly as 7462 1.1 christos makefiles. 7463 1.1 christos 7464 1.1 christos If you do not use the @samp{-f} or @samp{--file} flag, the default is 7465 1.1 christos to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in 7466 1.1 christos that order, and use the first of these three which exists or can be made 7467 1.1 christos (@pxref{Makefiles, ,Writing Makefiles}).@refill 7468 1.1 christos 7469 1.1 christos @node Goals, Instead of Execution, Makefile Arguments, Running 7470 1.1 christos @section Arguments to Specify the Goals 7471 1.1 christos @cindex goal, how to specify 7472 1.1 christos 7473 1.1 christos The @dfn{goals} are the targets that @code{make} should strive ultimately 7474 1.1 christos to update. Other targets are updated as well if they appear as 7475 1.1 christos prerequisites of goals, or prerequisites of prerequisites of goals, etc. 7476 1.1 christos 7477 1.1 christos By default, the goal is the first target in the makefile (not counting 7478 1.1 christos targets that start with a period). Therefore, makefiles are usually 7479 1.1 christos written so that the first target is for compiling the entire program or 7480 1.1 christos programs they describe. If the first rule in the makefile has several 7481 1.1 christos targets, only the first target in the rule becomes the default goal, not 7482 1.1 christos the whole list. You can manage the selection of the default goal from 7483 1.1 christos within your makefile using the @code{.DEFAULT_GOAL} variable 7484 1.1 christos (@pxref{Special Variables, , Other Special Variables}). 7485 1.1 christos 7486 1.1 christos You can also specify a different goal or goals with command-line 7487 1.1 christos arguments to @code{make}. Use the name of the goal as an argument. 7488 1.1 christos If you specify several goals, @code{make} processes each of them in 7489 1.1 christos turn, in the order you name them. 7490 1.1 christos 7491 1.1 christos Any target in the makefile may be specified as a goal (unless it 7492 1.1 christos starts with @samp{-} or contains an @samp{=}, in which case it will be 7493 1.1 christos parsed as a switch or variable definition, respectively). Even 7494 1.1 christos targets not in the makefile may be specified, if @code{make} can find 7495 1.1 christos implicit rules that say how to make them. 7496 1.1 christos 7497 1.1 christos @vindex MAKECMDGOALS 7498 1.1 christos @code{Make} will set the special variable @code{MAKECMDGOALS} to the 7499 1.1 christos list of goals you specified on the command line. If no goals were given 7500 1.1 christos on the command line, this variable is empty. Note that this variable 7501 1.1 christos should be used only in special circumstances. 7502 1.1 christos 7503 1.1 christos An example of appropriate use is to avoid including @file{.d} files 7504 1.1 christos during @code{clean} rules (@pxref{Automatic Prerequisites}), so 7505 1.1 christos @code{make} won't create them only to immediately remove them 7506 1.1 christos again:@refill 7507 1.1 christos 7508 1.1 christos @example 7509 1.1 christos @group 7510 1.1 christos sources = foo.c bar.c 7511 1.1 christos 7512 1.1 christos ifneq ($(MAKECMDGOALS),clean) 7513 1.1 christos include $(sources:.c=.d) 7514 1.1 christos endif 7515 1.1 christos @end group 7516 1.1 christos @end example 7517 1.1 christos 7518 1.1 christos One use of specifying a goal is if you want to compile only a part of 7519 1.1 christos the program, or only one of several programs. Specify as a goal each 7520 1.1 christos file that you wish to remake. For example, consider a directory containing 7521 1.1 christos several programs, with a makefile that starts like this: 7522 1.1 christos 7523 1.1 christos @example 7524 1.1 christos .PHONY: all 7525 1.1 christos all: size nm ld ar as 7526 1.1 christos @end example 7527 1.1 christos 7528 1.1 christos If you are working on the program @code{size}, you might want to say 7529 1.1 christos @w{@samp{make size}} so that only the files of that program are recompiled. 7530 1.1 christos 7531 1.1 christos Another use of specifying a goal is to make files that are not normally 7532 1.1 christos made. For example, there may be a file of debugging output, or a 7533 1.1 christos version of the program that is compiled specially for testing, which has 7534 1.1 christos a rule in the makefile but is not a prerequisite of the default goal. 7535 1.1 christos 7536 1.1 christos Another use of specifying a goal is to run the commands associated with 7537 1.1 christos a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty 7538 1.1 christos Targets, ,Empty Target Files to Record Events}). Many makefiles contain 7539 1.1 christos a phony target named @file{clean} which deletes everything except source 7540 1.1 christos files. Naturally, this is done only if you request it explicitly with 7541 1.1 christos @w{@samp{make clean}}. Following is a list of typical phony and empty 7542 1.1 christos target names. @xref{Standard Targets}, for a detailed list of all the 7543 1.1 christos standard target names which GNU software packages use. 7544 1.1 christos 7545 1.1 christos @table @file 7546 1.1 christos @item all 7547 1.1 christos @cindex @code{all} @r{(standard target)} 7548 1.1 christos Make all the top-level targets the makefile knows about. 7549 1.1 christos 7550 1.1 christos @item clean 7551 1.1 christos @cindex @code{clean} @r{(standard target)} 7552 1.1 christos Delete all files that are normally created by running @code{make}. 7553 1.1 christos 7554 1.1 christos @item mostlyclean 7555 1.1 christos @cindex @code{mostlyclean} @r{(standard target)} 7556 1.1 christos Like @samp{clean}, but may refrain from deleting a few files that people 7557 1.1 christos normally don't want to recompile. For example, the @samp{mostlyclean} 7558 1.1 christos target for GCC does not delete @file{libgcc.a}, because recompiling it 7559 1.1 christos is rarely necessary and takes a lot of time. 7560 1.1 christos 7561 1.1 christos @item distclean 7562 1.1 christos @cindex @code{distclean} @r{(standard target)} 7563 1.1 christos @itemx realclean 7564 1.1 christos @cindex @code{realclean} @r{(standard target)} 7565 1.1 christos @itemx clobber 7566 1.1 christos @cindex @code{clobber} @r{(standard target)} 7567 1.1 christos Any of these targets might be defined to delete @emph{more} files than 7568 1.1 christos @samp{clean} does. For example, this would delete configuration files 7569 1.1 christos or links that you would normally create as preparation for compilation, 7570 1.1 christos even if the makefile itself cannot create these files. 7571 1.1 christos 7572 1.1 christos @item install 7573 1.1 christos @cindex @code{install} @r{(standard target)} 7574 1.1 christos Copy the executable file into a directory that users typically search 7575 1.1 christos for commands; copy any auxiliary files that the executable uses into 7576 1.1 christos the directories where it will look for them. 7577 1.1 christos 7578 1.1 christos @item print 7579 1.1 christos @cindex @code{print} @r{(standard target)} 7580 1.1 christos Print listings of the source files that have changed. 7581 1.1 christos 7582 1.1 christos @item tar 7583 1.1 christos @cindex @code{tar} @r{(standard target)} 7584 1.1 christos Create a tar file of the source files. 7585 1.1 christos 7586 1.1 christos @item shar 7587 1.1 christos @cindex @code{shar} @r{(standard target)} 7588 1.1 christos Create a shell archive (shar file) of the source files. 7589 1.1 christos 7590 1.1 christos @item dist 7591 1.1 christos @cindex @code{dist} @r{(standard target)} 7592 1.1 christos Create a distribution file of the source files. This might 7593 1.1 christos be a tar file, or a shar file, or a compressed version of one of the 7594 1.1 christos above, or even more than one of the above. 7595 1.1 christos 7596 1.1 christos @item TAGS 7597 1.1 christos @cindex @code{TAGS} @r{(standard target)} 7598 1.1 christos Update a tags table for this program. 7599 1.1 christos 7600 1.1 christos @item check 7601 1.1 christos @cindex @code{check} @r{(standard target)} 7602 1.1 christos @itemx test 7603 1.1 christos @cindex @code{test} @r{(standard target)} 7604 1.1 christos Perform self tests on the program this makefile builds. 7605 1.1 christos @end table 7606 1.1 christos 7607 1.1 christos @node Instead of Execution, Avoiding Compilation, Goals, Running 7608 1.1 christos @section Instead of Executing the Commands 7609 1.1 christos @cindex execution, instead of 7610 1.1 christos @cindex commands, instead of executing 7611 1.1 christos 7612 1.1 christos The makefile tells @code{make} how to tell whether a target is up to date, 7613 1.1 christos and how to update each target. But updating the targets is not always 7614 1.1 christos what you want. Certain options specify other activities for @code{make}. 7615 1.1 christos 7616 1.1 christos @comment Extra blank lines make it print better. 7617 1.1 christos @table @samp 7618 1.1 christos @item -n 7619 1.1 christos @itemx --just-print 7620 1.1 christos @itemx --dry-run 7621 1.1 christos @itemx --recon 7622 1.1 christos @cindex @code{--just-print} 7623 1.1 christos @cindex @code{--dry-run} 7624 1.1 christos @cindex @code{--recon} 7625 1.1 christos @cindex @code{-n} 7626 1.1 christos 7627 1.1 christos ``No-op''. The activity is to print what commands would be used to make 7628 1.1 christos the targets up to date, but not actually execute them. 7629 1.1 christos 7630 1.1 christos @item -t 7631 1.1 christos @itemx --touch 7632 1.1 christos @cindex @code{--touch} 7633 1.1 christos @cindex touching files 7634 1.1 christos @cindex target, touching 7635 1.1 christos @cindex @code{-t} 7636 1.1 christos 7637 1.1 christos ``Touch''. The activity is to mark the targets as up to date without 7638 1.1 christos actually changing them. In other words, @code{make} pretends to compile 7639 1.1 christos the targets but does not really change their contents. 7640 1.1 christos 7641 1.1 christos @item -q 7642 1.1 christos @itemx --question 7643 1.1 christos @cindex @code{--question} 7644 1.1 christos @cindex @code{-q} 7645 1.1 christos @cindex question mode 7646 1.1 christos 7647 1.1 christos ``Question''. The activity is to find out silently whether the targets 7648 1.1 christos are up to date already; but execute no commands in either case. In other 7649 1.1 christos words, neither compilation nor output will occur. 7650 1.1 christos 7651 1.1 christos @item -W @var{file} 7652 1.1 christos @itemx --what-if=@var{file} 7653 1.1 christos @itemx --assume-new=@var{file} 7654 1.1 christos @itemx --new-file=@var{file} 7655 1.1 christos @cindex @code{--what-if} 7656 1.1 christos @cindex @code{-W} 7657 1.1 christos @cindex @code{--assume-new} 7658 1.1 christos @cindex @code{--new-file} 7659 1.1 christos @cindex what if 7660 1.1 christos @cindex files, assuming new 7661 1.1 christos 7662 1.1 christos ``What if''. Each @samp{-W} flag is followed by a file name. The given 7663 1.1 christos files' modification times are recorded by @code{make} as being the present 7664 1.1 christos time, although the actual modification times remain the same. 7665 1.1 christos You can use the @samp{-W} flag in conjunction with the @samp{-n} flag 7666 1.1 christos to see what would happen if you were to modify specific files.@refill 7667 1.1 christos @end table 7668 1.1 christos 7669 1.1 christos With the @samp{-n} flag, @code{make} prints the commands that it would 7670 1.1 christos normally execute but does not execute them. 7671 1.1 christos 7672 1.1 christos With the @samp{-t} flag, @code{make} ignores the commands in the rules 7673 1.1 christos and uses (in effect) the command @code{touch} for each target that needs to 7674 1.1 christos be remade. The @code{touch} command is also printed, unless @samp{-s} or 7675 1.1 christos @code{.SILENT} is used. For speed, @code{make} does not actually invoke 7676 1.1 christos the program @code{touch}. It does the work directly. 7677 1.1 christos 7678 1.1 christos With the @samp{-q} flag, @code{make} prints nothing and executes no 7679 1.1 christos commands, but the exit status code it returns is zero if and only if the 7680 1.1 christos targets to be considered are already up to date. If the exit status is 7681 1.1 christos one, then some updating needs to be done. If @code{make} encounters an 7682 1.1 christos error, the exit status is two, so you can distinguish an error from a 7683 1.1 christos target that is not up to date. 7684 1.1 christos 7685 1.1 christos It is an error to use more than one of these three flags in the same 7686 1.1 christos invocation of @code{make}. 7687 1.1 christos 7688 1.1 christos @cindex +, and command execution 7689 1.1 christos The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command 7690 1.1 christos lines that begin with @samp{+} characters or contain the strings 7691 1.1 christos @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing 7692 1.1 christos the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}} 7693 1.1 christos is run regardless of these options. Other lines in the same rule are 7694 1.1 christos not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or 7695 1.1 christos @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.) 7696 1.1 christos 7697 1.1 christos The @samp{-W} flag provides two features: 7698 1.1 christos 7699 1.1 christos @itemize @bullet 7700 1.1 christos @item 7701 1.1 christos If you also use the @samp{-n} or @samp{-q} flag, you can see what 7702 1.1 christos @code{make} would do if you were to modify some files. 7703 1.1 christos 7704 1.1 christos @item 7705 1.1 christos Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually 7706 1.1 christos executing commands, the @samp{-W} flag can direct @code{make} to act 7707 1.1 christos as if some files had been modified, without actually modifying the 7708 1.1 christos files.@refill 7709 1.1 christos @end itemize 7710 1.1 christos 7711 1.1 christos Note that the options @samp{-p} and @samp{-v} allow you to obtain other 7712 1.1 christos information about @code{make} or about the makefiles in use 7713 1.1 christos (@pxref{Options Summary, ,Summary of Options}).@refill 7714 1.1 christos 7715 1.1 christos @node Avoiding Compilation, Overriding, Instead of Execution, Running 7716 1.1 christos @section Avoiding Recompilation of Some Files 7717 1.1 christos @cindex @code{-o} 7718 1.1 christos @cindex @code{--old-file} 7719 1.1 christos @cindex @code{--assume-old} 7720 1.1 christos @cindex files, assuming old 7721 1.1 christos @cindex files, avoiding recompilation of 7722 1.1 christos @cindex recompilation, avoiding 7723 1.1 christos 7724 1.1 christos Sometimes you may have changed a source file but you do not want to 7725 1.1 christos recompile all the files that depend on it. For example, suppose you add 7726 1.1 christos a macro or a declaration to a header file that many other files depend 7727 1.1 christos on. Being conservative, @code{make} assumes that any change in the 7728 1.1 christos header file requires recompilation of all dependent files, but you know 7729 1.1 christos that they do not need to be recompiled and you would rather not waste 7730 1.1 christos the time waiting for them to compile. 7731 1.1 christos 7732 1.1 christos If you anticipate the problem before changing the header file, you can 7733 1.1 christos use the @samp{-t} flag. This flag tells @code{make} not to run the 7734 1.1 christos commands in the rules, but rather to mark the target up to date by 7735 1.1 christos changing its last-modification date. You would follow this procedure: 7736 1.1 christos 7737 1.1 christos @enumerate 7738 1.1 christos @item 7739 1.1 christos Use the command @samp{make} to recompile the source files that really 7740 1.1 christos need recompilation, ensuring that the object files are up-to-date 7741 1.1 christos before you begin. 7742 1.1 christos 7743 1.1 christos @item 7744 1.1 christos Make the changes in the header files. 7745 1.1 christos 7746 1.1 christos @item 7747 1.1 christos Use the command @samp{make -t} to mark all the object files as 7748 1.1 christos up to date. The next time you run @code{make}, the changes in the 7749 1.1 christos header files will not cause any recompilation. 7750 1.1 christos @end enumerate 7751 1.1 christos 7752 1.1 christos If you have already changed the header file at a time when some files 7753 1.1 christos do need recompilation, it is too late to do this. Instead, you can 7754 1.1 christos use the @w{@samp{-o @var{file}}} flag, which marks a specified file as 7755 1.1 christos ``old'' (@pxref{Options Summary, ,Summary of Options}). This means 7756 1.1 christos that the file itself will not be remade, and nothing else will be 7757 1.1 christos remade on its account. Follow this procedure: 7758 1.1 christos 7759 1.1 christos @enumerate 7760 1.1 christos @item 7761 1.1 christos Recompile the source files that need compilation for reasons independent 7762 1.1 christos of the particular header file, with @samp{make -o @var{headerfile}}. 7763 1.1 christos If several header files are involved, use a separate @samp{-o} option 7764 1.1 christos for each header file. 7765 1.1 christos 7766 1.1 christos @item 7767 1.1 christos Touch all the object files with @samp{make -t}. 7768 1.1 christos @end enumerate 7769 1.1 christos 7770 1.1 christos @node Overriding, Testing, Avoiding Compilation, Running 7771 1.1 christos @section Overriding Variables 7772 1.1 christos @cindex overriding variables with arguments 7773 1.1 christos @cindex variables, overriding with arguments 7774 1.1 christos @cindex command line variables 7775 1.1 christos @cindex variables, command line 7776 1.1 christos 7777 1.1 christos An argument that contains @samp{=} specifies the value of a variable: 7778 1.1 christos @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}. 7779 1.1 christos If you specify a value in this way, all ordinary assignments of the same 7780 1.1 christos variable in the makefile are ignored; we say they have been 7781 1.1 christos @dfn{overridden} by the command line argument. 7782 1.1 christos 7783 1.1 christos The most common way to use this facility is to pass extra flags to 7784 1.1 christos compilers. For example, in a properly written makefile, the variable 7785 1.1 christos @code{CFLAGS} is included in each command that runs the C compiler, so a 7786 1.1 christos file @file{foo.c} would be compiled something like this: 7787 1.1 christos 7788 1.1 christos @example 7789 1.1 christos cc -c $(CFLAGS) foo.c 7790 1.1 christos @end example 7791 1.1 christos 7792 1.1 christos Thus, whatever value you set for @code{CFLAGS} affects each compilation 7793 1.1 christos that occurs. The makefile probably specifies the usual value for 7794 1.1 christos @code{CFLAGS}, like this: 7795 1.1 christos 7796 1.1 christos @example 7797 1.1 christos CFLAGS=-g 7798 1.1 christos @end example 7799 1.1 christos 7800 1.1 christos Each time you run @code{make}, you can override this value if you 7801 1.1 christos wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C 7802 1.1 christos compilation will be done with @samp{cc -c -g -O}. (This also 7803 1.1 christos illustrates how you can use quoting in the shell to enclose spaces and 7804 1.1 christos other special characters in the value of a variable when you override 7805 1.1 christos it.) 7806 1.1 christos 7807 1.1 christos The variable @code{CFLAGS} is only one of many standard variables that 7808 1.1 christos exist just so that you can change them this way. @xref{Implicit 7809 1.1 christos Variables, , Variables Used by Implicit Rules}, for a complete list. 7810 1.1 christos 7811 1.1 christos You can also program the makefile to look at additional variables of your 7812 1.1 christos own, giving the user the ability to control other aspects of how the 7813 1.1 christos makefile works by changing the variables. 7814 1.1 christos 7815 1.1 christos When you override a variable with a command argument, you can define either 7816 1.1 christos a recursively-expanded variable or a simply-expanded variable. The 7817 1.1 christos examples shown above make a recursively-expanded variable; to make a 7818 1.1 christos simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless 7819 1.1 christos you want to include a variable reference or function call in the 7820 1.1 christos @emph{value} that you specify, it makes no difference which kind of 7821 1.1 christos variable you create. 7822 1.1 christos 7823 1.1 christos There is one way that the makefile can change a variable that you have 7824 1.1 christos overridden. This is to use the @code{override} directive, which is a line 7825 1.1 christos that looks like this: @samp{override @var{variable} = @var{value}} 7826 1.1 christos (@pxref{Override Directive, ,The @code{override} Directive}). 7827 1.1 christos 7828 1.1 christos @node Testing, Options Summary, Overriding, Running 7829 1.1 christos @section Testing the Compilation of a Program 7830 1.1 christos @cindex testing compilation 7831 1.1 christos @cindex compilation, testing 7832 1.1 christos 7833 1.1 christos Normally, when an error happens in executing a shell command, @code{make} 7834 1.1 christos gives up immediately, returning a nonzero status. No further commands are 7835 1.1 christos executed for any target. The error implies that the goal cannot be 7836 1.1 christos correctly remade, and @code{make} reports this as soon as it knows. 7837 1.1 christos 7838 1.1 christos When you are compiling a program that you have just changed, this is not 7839 1.1 christos what you want. Instead, you would rather that @code{make} try compiling 7840 1.1 christos every file that can be tried, to show you as many compilation errors 7841 1.1 christos as possible. 7842 1.1 christos 7843 1.1 christos @cindex @code{-k} 7844 1.1 christos @cindex @code{--keep-going} 7845 1.1 christos On these occasions, you should use the @samp{-k} or 7846 1.1 christos @samp{--keep-going} flag. This tells @code{make} to continue to 7847 1.1 christos consider the other prerequisites of the pending targets, remaking them 7848 1.1 christos if necessary, before it gives up and returns nonzero status. For 7849 1.1 christos example, after an error in compiling one object file, @samp{make -k} 7850 1.1 christos will continue compiling other object files even though it already 7851 1.1 christos knows that linking them will be impossible. In addition to continuing 7852 1.1 christos after failed shell commands, @samp{make -k} will continue as much as 7853 1.1 christos possible after discovering that it does not know how to make a target 7854 1.1 christos or prerequisite file. This will always cause an error message, but 7855 1.1 christos without @samp{-k}, it is a fatal error (@pxref{Options Summary, 7856 1.1 christos ,Summary of Options}).@refill 7857 1.1 christos 7858 1.1 christos The usual behavior of @code{make} assumes that your purpose is to get the 7859 1.1 christos goals up to date; once @code{make} learns that this is impossible, it might 7860 1.1 christos as well report the failure immediately. The @samp{-k} flag says that the 7861 1.1 christos real purpose is to test as much as possible of the changes made in the 7862 1.1 christos program, perhaps to find several independent problems so that you can 7863 1.1 christos correct them all before the next attempt to compile. This is why Emacs' 7864 1.1 christos @kbd{M-x compile} command passes the @samp{-k} flag by default. 7865 1.1 christos 7866 1.1 christos @node Options Summary, , Testing, Running 7867 1.1 christos @section Summary of Options 7868 1.1 christos @cindex options 7869 1.1 christos @cindex flags 7870 1.1 christos @cindex switches 7871 1.1 christos 7872 1.1 christos Here is a table of all the options @code{make} understands: 7873 1.1 christos 7874 1.1 christos @table @samp 7875 1.1 christos @item -b 7876 1.1 christos @cindex @code{-b} 7877 1.1 christos @itemx -m 7878 1.1 christos @cindex @code{-m} 7879 1.1 christos These options are ignored for compatibility with other versions of @code{make}. 7880 1.1 christos 7881 1.1 christos @item -B 7882 1.1 christos @cindex @code{-B} 7883 1.1 christos @itemx --always-make 7884 1.1 christos @cindex @code{--always-make} 7885 1.1 christos Consider all targets out-of-date. GNU @code{make} proceeds to 7886 1.1 christos consider targets and their prerequisites using the normal algorithms; 7887 1.1 christos however, all targets so considered are always remade regardless of the 7888 1.1 christos status of their prerequisites. To avoid infinite recursion, if 7889 1.1 christos @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special 7890 1.1 christos Variables}) is set to a number greater than 0 this option is disabled 7891 1.1 christos when considering whether to remake makefiles (@pxref{Remaking 7892 1.1 christos Makefiles, , How Makefiles Are Remade}). 7893 1.1 christos 7894 1.1 christos @item -C @var{dir} 7895 1.1 christos @cindex @code{-C} 7896 1.1 christos @itemx --directory=@var{dir} 7897 1.1 christos @cindex @code{--directory} 7898 1.1 christos Change to directory @var{dir} before reading the makefiles. If multiple 7899 1.1 christos @samp{-C} options are specified, each is interpreted relative to the 7900 1.1 christos previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}. 7901 1.1 christos This is typically used with recursive invocations of @code{make} 7902 1.1 christos (@pxref{Recursion, ,Recursive Use of @code{make}}). 7903 1.1 christos 7904 1.1 christos @item -d 7905 1.1 christos @cindex @code{-d} 7906 1.1 christos @c Extra blank line here makes the table look better. 7907 1.1 christos 7908 1.1 christos Print debugging information in addition to normal processing. The 7909 1.1 christos debugging information says which files are being considered for 7910 1.1 christos remaking, which file-times are being compared and with what results, 7911 1.1 christos which files actually need to be remade, which implicit rules are 7912 1.1 christos considered and which are applied---everything interesting about how 7913 1.1 christos @code{make} decides what to do. The @code{-d} option is equivalent to 7914 1.1 christos @samp{--debug=a} (see below). 7915 1.1 christos 7916 1.1 christos @item --debug[=@var{options}] 7917 1.1 christos @cindex @code{--debug} 7918 1.1 christos @c Extra blank line here makes the table look better. 7919 1.1 christos 7920 1.1 christos Print debugging information in addition to normal processing. Various 7921 1.1 christos levels and types of output can be chosen. With no arguments, print the 7922 1.1 christos ``basic'' level of debugging. Possible arguments are below; only the 7923 1.1 christos first character is considered, and values must be comma- or 7924 1.1 christos space-separated. 7925 1.1 christos 7926 1.1 christos @table @code 7927 1.1 christos @item a (@i{all}) 7928 1.1 christos All types of debugging output are enabled. This is equivalent to using 7929 1.1 christos @samp{-d}. 7930 1.1 christos 7931 1.1 christos @item b (@i{basic}) 7932 1.1 christos Basic debugging prints each target that was found to be out-of-date, and 7933 1.1 christos whether the build was successful or not. 7934 1.1 christos 7935 1.1 christos @item v (@i{verbose}) 7936 1.1 christos A level above @samp{basic}; includes messages about which makefiles were 7937 1.1 christos parsed, prerequisites that did not need to be rebuilt, etc. This option 7938 1.1 christos also enables @samp{basic} messages. 7939 1.1 christos 7940 1.1 christos @item i (@i{implicit}) 7941 1.1 christos Prints messages describing the implicit rule searches for each target. 7942 1.1 christos This option also enables @samp{basic} messages. 7943 1.1 christos 7944 1.1 christos @item j (@i{jobs}) 7945 1.1 christos Prints messages giving details on the invocation of specific subcommands. 7946 1.1 christos 7947 1.1 christos @item m (@i{makefile}) 7948 1.1 christos By default, the above messages are not enabled while trying to remake 7949 1.1 christos the makefiles. This option enables messages while rebuilding makefiles, 7950 1.1 christos too. Note that the @samp{all} option does enable this option. This 7951 1.1 christos option also enables @samp{basic} messages. 7952 1.1 christos @end table 7953 1.1 christos 7954 1.1 christos @item -e 7955 1.1 christos @cindex @code{-e} 7956 1.1 christos @itemx --environment-overrides 7957 1.1 christos @cindex @code{--environment-overrides} 7958 1.1 christos Give variables taken from the environment precedence 7959 1.1 christos over variables from makefiles. 7960 1.1 christos @xref{Environment, ,Variables from the Environment}. 7961 1.1 christos 7962 1.1 christos @item -f @var{file} 7963 1.1 christos @cindex @code{-f} 7964 1.1 christos @itemx --file=@var{file} 7965 1.1 christos @cindex @code{--file} 7966 1.1 christos @itemx --makefile=@var{file} 7967 1.1 christos @cindex @code{--makefile} 7968 1.1 christos Read the file named @var{file} as a makefile. 7969 1.1 christos @xref{Makefiles, ,Writing Makefiles}. 7970 1.1 christos 7971 1.1 christos @item -h 7972 1.1 christos @cindex @code{-h} 7973 1.1 christos @itemx --help 7974 1.1 christos @cindex @code{--help} 7975 1.1 christos @c Extra blank line here makes the table look better. 7976 1.1 christos 7977 1.1 christos Remind you of the options that @code{make} understands and then exit. 7978 1.1 christos 7979 1.1 christos @item -i 7980 1.1 christos @cindex @code{-i} 7981 1.1 christos @itemx --ignore-errors 7982 1.1 christos @cindex @code{--ignore-errors} 7983 1.1 christos Ignore all errors in commands executed to remake files. 7984 1.1 christos @xref{Errors, ,Errors in Commands}. 7985 1.1 christos 7986 1.1 christos @item -I @var{dir} 7987 1.1 christos @cindex @code{-I} 7988 1.1 christos @itemx --include-dir=@var{dir} 7989 1.1 christos @cindex @code{--include-dir} 7990 1.1 christos Specifies a directory @var{dir} to search for included makefiles. 7991 1.1 christos @xref{Include, ,Including Other Makefiles}. If several @samp{-I} 7992 1.1 christos options are used to specify several directories, the directories are 7993 1.1 christos searched in the order specified. 7994 1.1 christos 7995 1.1 christos @item -j [@var{jobs}] 7996 1.1 christos @cindex @code{-j} 7997 1.1 christos @itemx --jobs[=@var{jobs}] 7998 1.1 christos @cindex @code{--jobs} 7999 1.1 christos Specifies the number of jobs (commands) to run simultaneously. With no 8000 1.1 christos argument, @code{make} runs as many jobs simultaneously as possible. If 8001 1.1 christos there is more than one @samp{-j} option, the last one is effective. 8002 1.1 christos @xref{Parallel, ,Parallel Execution}, 8003 1.1 christos for more information on how commands are run. 8004 1.1 christos Note that this option is ignored on MS-DOS. 8005 1.1 christos 8006 1.1 christos @item -k 8007 1.1 christos @cindex @code{-k} 8008 1.1 christos @itemx --keep-going 8009 1.1 christos @cindex @code{--keep-going} 8010 1.1 christos Continue as much as possible after an error. While the target that 8011 1.1 christos failed, and those that depend on it, cannot be remade, the other 8012 1.1 christos prerequisites of these targets can be processed all the same. 8013 1.1 christos @xref{Testing, ,Testing the Compilation of a Program}. 8014 1.1 christos 8015 1.1 christos @item -l [@var{load}] 8016 1.1 christos @cindex @code{-l} 8017 1.1 christos @itemx --load-average[=@var{load}] 8018 1.1 christos @cindex @code{--load-average} 8019 1.1 christos @itemx --max-load[=@var{load}] 8020 1.1 christos @cindex @code{--max-load} 8021 1.1 christos Specifies that no new jobs (commands) should be started if there are 8022 1.1 christos other jobs running and the load average is at least @var{load} (a 8023 1.1 christos floating-point number). With no argument, removes a previous load 8024 1.1 christos limit. @xref{Parallel, ,Parallel Execution}. 8025 1.1 christos 8026 1.1 christos @item -L 8027 1.1 christos @cindex @code{-L} 8028 1.1 christos @itemx --check-symlink-times 8029 1.1 christos @cindex @code{--check-symlink-times} 8030 1.1 christos On systems that support symbolic links, this option causes @code{make} 8031 1.1 christos to consider the timestamps on any symbolic links in addition to the 8032 1.1 christos timestamp on the file referenced by those links. When this option is 8033 1.1 christos provided, the most recent timestamp among the file and the symbolic 8034 1.1 christos links is taken as the modification time for this target file. 8035 1.1 christos 8036 1.1 christos @item -n 8037 1.1 christos @cindex @code{-n} 8038 1.1 christos @itemx --just-print 8039 1.1 christos @cindex @code{--just-print} 8040 1.1 christos @itemx --dry-run 8041 1.1 christos @cindex @code{--dry-run} 8042 1.1 christos @itemx --recon 8043 1.1 christos @cindex @code{--recon} 8044 1.1 christos @c Extra blank line here makes the table look better. 8045 1.1 christos 8046 1.1 christos Print the commands that would be executed, but do not execute them. 8047 1.1 christos @xref{Instead of Execution, ,Instead of Executing the Commands}. 8048 1.1 christos 8049 1.1 christos @item -o @var{file} 8050 1.1 christos @cindex @code{-o} 8051 1.1 christos @itemx --old-file=@var{file} 8052 1.1 christos @cindex @code{--old-file} 8053 1.1 christos @itemx --assume-old=@var{file} 8054 1.1 christos @cindex @code{--assume-old} 8055 1.1 christos Do not remake the file @var{file} even if it is older than its 8056 1.1 christos prerequisites, and do not remake anything on account of changes in 8057 1.1 christos @var{file}. Essentially the file is treated as very old and its rules 8058 1.1 christos are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of 8059 1.1 christos Some Files}.@refill 8060 1.1 christos 8061 1.1 christos @item -p 8062 1.1 christos @cindex @code{-p} 8063 1.1 christos @itemx --print-data-base 8064 1.1 christos @cindex @code{--print-data-base} 8065 1.1 christos @cindex data base of @code{make} rules 8066 1.1 christos @cindex predefined rules and variables, printing 8067 1.1 christos Print the data base (rules and variable values) that results from 8068 1.1 christos reading the makefiles; then execute as usual or as otherwise specified. 8069 1.1 christos This also prints the version information given by the @samp{-v} switch 8070 1.1 christos (see below). To print the data base without trying to remake any files, 8071 1.1 christos use @w{@samp{make -qp}}. To print the data base of predefined rules and 8072 1.1 christos variables, use @w{@samp{make -p -f /dev/null}}. The data base output 8073 1.1 christos contains filename and linenumber information for command and variable 8074 1.1 christos definitions, so it can be a useful debugging tool in complex environments. 8075 1.1 christos 8076 1.1 christos @item -q 8077 1.1 christos @cindex @code{-q} 8078 1.1 christos @itemx --question 8079 1.1 christos @cindex @code{--question} 8080 1.1 christos ``Question mode''. Do not run any commands, or print anything; just 8081 1.1 christos return an exit status that is zero if the specified targets are already 8082 1.1 christos up to date, one if any remaking is required, or two if an error is 8083 1.1 christos encountered. @xref{Instead of Execution, ,Instead of Executing the 8084 1.1 christos Commands}.@refill 8085 1.1 christos 8086 1.1 christos @item -r 8087 1.1 christos @cindex @code{-r} 8088 1.1 christos @itemx --no-builtin-rules 8089 1.1 christos @cindex @code{--no-builtin-rules} 8090 1.1 christos Eliminate use of the built-in implicit rules (@pxref{Implicit Rules, 8091 1.1 christos ,Using Implicit Rules}). You can still define your own by writing 8092 1.1 christos pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern 8093 1.1 christos Rules}). The @samp{-r} option also clears out the default list of 8094 1.1 christos suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix 8095 1.1 christos Rules}). But you can still define your own suffixes with a rule for 8096 1.1 christos @code{.SUFFIXES}, and then define your own suffix rules. Note that only 8097 1.1 christos @emph{rules} are affected by the @code{-r} option; default variables 8098 1.1 christos remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit 8099 1.1 christos Rules}); see the @samp{-R} option below. 8100 1.1 christos 8101 1.1 christos @item -R 8102 1.1 christos @cindex @code{-R} 8103 1.1 christos @itemx --no-builtin-variables 8104 1.1 christos @cindex @code{--no-builtin-variables} 8105 1.1 christos Eliminate use of the built-in rule-specific variables (@pxref{Implicit 8106 1.1 christos Variables, ,Variables Used by Implicit Rules}). You can still define 8107 1.1 christos your own, of course. The @samp{-R} option also automatically enables 8108 1.1 christos the @samp{-r} option (see above), since it doesn't make sense to have 8109 1.1 christos implicit rules without any definitions for the variables that they use. 8110 1.1 christos 8111 1.1 christos @item -s 8112 1.1 christos @cindex @code{-s} 8113 1.1 christos @itemx --silent 8114 1.1 christos @cindex @code{--silent} 8115 1.1 christos @itemx --quiet 8116 1.1 christos @cindex @code{--quiet} 8117 1.1 christos @c Extra blank line here makes the table look better. 8118 1.1 christos 8119 1.1 christos Silent operation; do not print the commands as they are executed. 8120 1.1 christos @xref{Echoing, ,Command Echoing}. 8121 1.1 christos 8122 1.1 christos @item -S 8123 1.1 christos @cindex @code{-S} 8124 1.1 christos @itemx --no-keep-going 8125 1.1 christos @cindex @code{--no-keep-going} 8126 1.1 christos @itemx --stop 8127 1.1 christos @cindex @code{--stop} 8128 1.1 christos @c Extra blank line here makes the table look better. 8129 1.1 christos 8130 1.1 christos Cancel the effect of the @samp{-k} option. This is never necessary 8131 1.1 christos except in a recursive @code{make} where @samp{-k} might be inherited 8132 1.1 christos from the top-level @code{make} via @code{MAKEFLAGS} 8133 1.1 christos (@pxref{Recursion, ,Recursive Use of @code{make}}) 8134 1.1 christos or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill 8135 1.1 christos 8136 1.1 christos @item -t 8137 1.1 christos @cindex @code{-t} 8138 1.1 christos @itemx --touch 8139 1.1 christos @cindex @code{--touch} 8140 1.1 christos @c Extra blank line here makes the table look better. 8141 1.1 christos 8142 1.1 christos Touch files (mark them up to date without really changing them) 8143 1.1 christos instead of running their commands. This is used to pretend that the 8144 1.1 christos commands were done, in order to fool future invocations of 8145 1.1 christos @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}. 8146 1.1 christos 8147 1.1 christos @item -v 8148 1.1 christos @cindex @code{-v} 8149 1.1 christos @itemx --version 8150 1.1 christos @cindex @code{--version} 8151 1.1 christos Print the version of the @code{make} program plus a copyright, a list 8152 1.1 christos of authors, and a notice that there is no warranty; then exit. 8153 1.1 christos 8154 1.1 christos @item -w 8155 1.1 christos @cindex @code{-w} 8156 1.1 christos @itemx --print-directory 8157 1.1 christos @cindex @code{--print-directory} 8158 1.1 christos Print a message containing the working directory both before and after 8159 1.1 christos executing the makefile. This may be useful for tracking down errors 8160 1.1 christos from complicated nests of recursive @code{make} commands. 8161 1.1 christos @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you 8162 1.1 christos rarely need to specify this option since @samp{make} does it for you; 8163 1.1 christos see @ref{-w Option, ,The @samp{--print-directory} Option}.) 8164 1.1 christos 8165 1.1 christos @itemx --no-print-directory 8166 1.1 christos @cindex @code{--no-print-directory} 8167 1.1 christos Disable printing of the working directory under @code{-w}. 8168 1.1 christos This option is useful when @code{-w} is turned on automatically, 8169 1.1 christos but you do not want to see the extra messages. 8170 1.1 christos @xref{-w Option, ,The @samp{--print-directory} Option}. 8171 1.1 christos 8172 1.1 christos @item -W @var{file} 8173 1.1 christos @cindex @code{-W} 8174 1.1 christos @itemx --what-if=@var{file} 8175 1.1 christos @cindex @code{--what-if} 8176 1.1 christos @itemx --new-file=@var{file} 8177 1.1 christos @cindex @code{--new-file} 8178 1.1 christos @itemx --assume-new=@var{file} 8179 1.1 christos @cindex @code{--assume-new} 8180 1.1 christos Pretend that the target @var{file} has just been modified. When used 8181 1.1 christos with the @samp{-n} flag, this shows you what would happen if you were 8182 1.1 christos to modify that file. Without @samp{-n}, it is almost the same as 8183 1.1 christos running a @code{touch} command on the given file before running 8184 1.1 christos @code{make}, except that the modification time is changed only in the 8185 1.1 christos imagination of @code{make}. 8186 1.1 christos @xref{Instead of Execution, ,Instead of Executing the Commands}. 8187 1.1 christos 8188 1.1 christos @item --warn-undefined-variables 8189 1.1 christos @cindex @code{--warn-undefined-variables} 8190 1.1 christos @cindex variables, warning for undefined 8191 1.1 christos @cindex undefined variables, warning message 8192 1.1 christos Issue a warning message whenever @code{make} sees a reference to an 8193 1.1 christos undefined variable. This can be helpful when you are trying to debug 8194 1.1 christos makefiles which use variables in complex ways. 8195 1.1 christos @end table 8196 1.1 christos 8197 1.1 christos @node Implicit Rules, Archives, Running, Top 8198 1.1 christos @chapter Using Implicit Rules 8199 1.1 christos @cindex implicit rule 8200 1.1 christos @cindex rule, implicit 8201 1.1 christos 8202 1.1 christos Certain standard ways of remaking target files are used very often. For 8203 1.1 christos example, one customary way to make an object file is from a C source file 8204 1.1 christos using the C compiler, @code{cc}. 8205 1.1 christos 8206 1.1 christos @dfn{Implicit rules} tell @code{make} how to use customary techniques so 8207 1.1 christos that you do not have to specify them in detail when you want to use 8208 1.1 christos them. For example, there is an implicit rule for C compilation. File 8209 1.1 christos names determine which implicit rules are run. For example, C 8210 1.1 christos compilation typically takes a @file{.c} file and makes a @file{.o} file. 8211 1.1 christos So @code{make} applies the implicit rule for C compilation when it sees 8212 1.1 christos this combination of file name endings.@refill 8213 1.1 christos 8214 1.1 christos A chain of implicit rules can apply in sequence; for example, @code{make} 8215 1.1 christos will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file. 8216 1.1 christos @iftex 8217 1.1 christos @xref{Chained Rules, ,Chains of Implicit Rules}. 8218 1.1 christos @end iftex 8219 1.1 christos 8220 1.1 christos The built-in implicit rules use several variables in their commands so 8221 1.1 christos that, by changing the values of the variables, you can change the way the 8222 1.1 christos implicit rule works. For example, the variable @code{CFLAGS} controls the 8223 1.1 christos flags given to the C compiler by the implicit rule for C compilation. 8224 1.1 christos @iftex 8225 1.1 christos @xref{Implicit Variables, ,Variables Used by Implicit Rules}. 8226 1.1 christos @end iftex 8227 1.1 christos 8228 1.1 christos You can define your own implicit rules by writing @dfn{pattern rules}. 8229 1.1 christos @iftex 8230 1.1 christos @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}. 8231 1.1 christos @end iftex 8232 1.1 christos 8233 1.1 christos @dfn{Suffix rules} are a more limited way to define implicit rules. 8234 1.1 christos Pattern rules are more general and clearer, but suffix rules are 8235 1.1 christos retained for compatibility. 8236 1.1 christos @iftex 8237 1.1 christos @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}. 8238 1.1 christos @end iftex 8239 1.1 christos 8240 1.1 christos @menu 8241 1.1 christos * Using Implicit:: How to use an existing implicit rule 8242 1.1 christos to get the commands for updating a file. 8243 1.1 christos * Catalogue of Rules:: A list of built-in implicit rules. 8244 1.1 christos * Implicit Variables:: How to change what predefined rules do. 8245 1.1 christos * Chained Rules:: How to use a chain of implicit rules. 8246 1.1 christos * Pattern Rules:: How to define new implicit rules. 8247 1.1 christos * Last Resort:: How to define commands for rules which 8248 1.1 christos cannot find any. 8249 1.1 christos * Suffix Rules:: The old-fashioned style of implicit rule. 8250 1.1 christos * Implicit Rule Search:: The precise algorithm for applying 8251 1.1 christos implicit rules. 8252 1.1 christos @end menu 8253 1.1 christos 8254 1.1 christos @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules 8255 1.1 christos @section Using Implicit Rules 8256 1.1 christos @cindex implicit rule, how to use 8257 1.1 christos @cindex rule, implicit, how to use 8258 1.1 christos 8259 1.1 christos To allow @code{make} to find a customary method for updating a target file, 8260 1.1 christos all you have to do is refrain from specifying commands yourself. Either 8261 1.1 christos write a rule with no command lines, or don't write a rule at all. Then 8262 1.1 christos @code{make} will figure out which implicit rule to use based on which 8263 1.1 christos kind of source file exists or can be made. 8264 1.1 christos 8265 1.1 christos For example, suppose the makefile looks like this: 8266 1.1 christos 8267 1.1 christos @example 8268 1.1 christos foo : foo.o bar.o 8269 1.1 christos cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS) 8270 1.1 christos @end example 8271 1.1 christos 8272 1.1 christos @noindent 8273 1.1 christos Because you mention @file{foo.o} but do not give a rule for it, @code{make} 8274 1.1 christos will automatically look for an implicit rule that tells how to update it. 8275 1.1 christos This happens whether or not the file @file{foo.o} currently exists. 8276 1.1 christos 8277 1.1 christos If an implicit rule is found, it can supply both commands and one or 8278 1.1 christos more prerequisites (the source files). You would want to write a rule 8279 1.1 christos for @file{foo.o} with no command lines if you need to specify additional 8280 1.1 christos prerequisites, such as header files, that the implicit rule cannot 8281 1.1 christos supply. 8282 1.1 christos 8283 1.1 christos Each implicit rule has a target pattern and prerequisite patterns. There may 8284 1.1 christos be many implicit rules with the same target pattern. For example, numerous 8285 1.1 christos rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler; 8286 1.1 christos another, from a @samp{.p} file with the Pascal compiler; and so on. The rule 8287 1.1 christos that actually applies is the one whose prerequisites exist or can be made. 8288 1.1 christos So, if you have a file @file{foo.c}, @code{make} will run the C compiler; 8289 1.1 christos otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal 8290 1.1 christos compiler; and so on. 8291 1.1 christos 8292 1.1 christos Of course, when you write the makefile, you know which implicit rule you 8293 1.1 christos want @code{make} to use, and you know it will choose that one because you 8294 1.1 christos know which possible prerequisite files are supposed to exist. 8295 1.1 christos @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}, 8296 1.1 christos for a catalogue of all the predefined implicit rules. 8297 1.1 christos 8298 1.1 christos Above, we said an implicit rule applies if the required prerequisites ``exist 8299 1.1 christos or can be made''. A file ``can be made'' if it is mentioned explicitly in 8300 1.1 christos the makefile as a target or a prerequisite, or if an implicit rule can be 8301 1.1 christos recursively found for how to make it. When an implicit prerequisite is the 8302 1.1 christos result of another implicit rule, we say that @dfn{chaining} is occurring. 8303 1.1 christos @xref{Chained Rules, ,Chains of Implicit Rules}. 8304 1.1 christos 8305 1.1 christos In general, @code{make} searches for an implicit rule for each target, and 8306 1.1 christos for each double-colon rule, that has no commands. A file that is mentioned 8307 1.1 christos only as a prerequisite is considered a target whose rule specifies nothing, 8308 1.1 christos so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the 8309 1.1 christos details of how the search is done. 8310 1.1 christos 8311 1.1 christos Note that explicit prerequisites do not influence implicit rule search. 8312 1.1 christos For example, consider this explicit rule: 8313 1.1 christos 8314 1.1 christos @example 8315 1.1 christos foo.o: foo.p 8316 1.1 christos @end example 8317 1.1 christos 8318 1.1 christos @noindent 8319 1.1 christos The prerequisite on @file{foo.p} does not necessarily mean that 8320 1.1 christos @code{make} will remake @file{foo.o} according to the implicit rule to 8321 1.1 christos make an object file, a @file{.o} file, from a Pascal source file, a 8322 1.1 christos @file{.p} file. For example, if @file{foo.c} also exists, the implicit 8323 1.1 christos rule to make an object file from a C source file is used instead, 8324 1.1 christos because it appears before the Pascal rule in the list of predefined 8325 1.1 christos implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit 8326 1.1 christos Rules}). 8327 1.1 christos 8328 1.1 christos If you do not want an implicit rule to be used for a target that has no 8329 1.1 christos commands, you can give that target empty commands by writing a semicolon 8330 1.1 christos (@pxref{Empty Commands, ,Defining Empty Commands}). 8331 1.1 christos 8332 1.1 christos @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules 8333 1.1 christos @section Catalogue of Implicit Rules 8334 1.1 christos @cindex implicit rule, predefined 8335 1.1 christos @cindex rule, implicit, predefined 8336 1.1 christos 8337 1.1 christos Here is a catalogue of predefined implicit rules which are always 8338 1.1 christos available unless the makefile explicitly overrides or cancels them. 8339 1.1 christos @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on 8340 1.1 christos canceling or overriding an implicit rule. The @samp{-r} or 8341 1.1 christos @samp{--no-builtin-rules} option cancels all predefined rules. 8342 1.1 christos 8343 1.1 christos This manual only documents the default rules available on POSIX-based 8344 1.1 christos operating systems. Other operating systems, such as VMS, Windows, 8345 1.1 christos OS/2, etc. may have different sets of default rules. To see the full 8346 1.1 christos list of default rules and variables available in your version of GNU 8347 1.1 christos @code{make}, run @samp{make -p} in a directory with no makefile. 8348 1.1 christos 8349 1.1 christos Not all of these rules will always be defined, even when the @samp{-r} 8350 1.1 christos option is not given. Many of the predefined implicit rules are 8351 1.1 christos implemented in @code{make} as suffix rules, so which ones will be 8352 1.1 christos defined depends on the @dfn{suffix list} (the list of prerequisites of 8353 1.1 christos the special target @code{.SUFFIXES}). The default suffix list is: 8354 1.1 christos @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc}, 8355 1.1 christos @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y}, 8356 1.1 christos @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, 8357 1.1 christos @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, 8358 1.1 christos @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, 8359 1.1 christos @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules 8360 1.1 christos described below whose prerequisites have one of these suffixes are 8361 1.1 christos actually suffix rules. If you modify the suffix list, the only 8362 1.1 christos predefined suffix rules in effect will be those named by one or two of 8363 1.1 christos the suffixes that are on the list you specify; rules whose suffixes fail 8364 1.1 christos to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned 8365 1.1 christos Suffix Rules}, for full details on suffix rules. 8366 1.1 christos 8367 1.1 christos @table @asis 8368 1.1 christos @item Compiling C programs 8369 1.1 christos @cindex C, rule to compile 8370 1.1 christos @pindex cc 8371 1.1 christos @pindex gcc 8372 1.1 christos @pindex .o 8373 1.1 christos @pindex .c 8374 1.1 christos @file{@var{n}.o} is made automatically from @file{@var{n}.c} with 8375 1.1 christos a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill 8376 1.1 christos 8377 1.1 christos @item Compiling C++ programs 8378 1.1 christos @cindex C++, rule to compile 8379 1.1 christos @pindex g++ 8380 1.1 christos @pindex .cc 8381 1.1 christos @pindex .cpp 8382 1.1 christos @pindex .C 8383 1.1 christos @file{@var{n}.o} is made automatically from @file{@var{n}.cc}, 8384 1.1 christos @file{@var{n}.cpp}, or @file{@var{n}.C} with a command of the form 8385 1.1 christos @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}. We encourage you to use the 8386 1.1 christos suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill 8387 1.1 christos 8388 1.1 christos @item Compiling Pascal programs 8389 1.1 christos @cindex Pascal, rule to compile 8390 1.1 christos @pindex pc 8391 1.1 christos @pindex .p 8392 1.1 christos @file{@var{n}.o} is made automatically from @file{@var{n}.p} 8393 1.1 christos with the command @samp{$(PC) -c $(PFLAGS)}.@refill 8394 1.1 christos 8395 1.1 christos @item Compiling Fortran and Ratfor programs 8396 1.1 christos @cindex Fortran, rule to compile 8397 1.1 christos @cindex Ratfor, rule to compile 8398 1.1 christos @pindex f77 8399 1.1 christos @pindex .f 8400 1.1 christos @pindex .r 8401 1.1 christos @pindex .F 8402 1.1 christos @file{@var{n}.o} is made automatically from @file{@var{n}.r}, 8403 1.1 christos @file{@var{n}.F} or @file{@var{n}.f} by running the 8404 1.1 christos Fortran compiler. The precise command used is as follows:@refill 8405 1.1 christos 8406 1.1 christos @table @samp 8407 1.1 christos @item .f 8408 1.1 christos @samp{$(FC) -c $(FFLAGS)}. 8409 1.1 christos @item .F 8410 1.1 christos @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}. 8411 1.1 christos @item .r 8412 1.1 christos @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}. 8413 1.1 christos @end table 8414 1.1 christos 8415 1.1 christos @item Preprocessing Fortran and Ratfor programs 8416 1.1 christos @file{@var{n}.f} is made automatically from @file{@var{n}.r} or 8417 1.1 christos @file{@var{n}.F}. This rule runs just the preprocessor to convert a 8418 1.1 christos Ratfor or preprocessable Fortran program into a strict Fortran 8419 1.1 christos program. The precise command used is as follows:@refill 8420 1.1 christos 8421 1.1 christos @table @samp 8422 1.1 christos @item .F 8423 1.1 christos @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}. 8424 1.1 christos @item .r 8425 1.1 christos @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}. 8426 1.1 christos @end table 8427 1.1 christos 8428 1.1 christos @item Compiling Modula-2 programs 8429 1.1 christos @cindex Modula-2, rule to compile 8430 1.1 christos @pindex m2c 8431 1.1 christos @pindex .sym 8432 1.1 christos @pindex .def 8433 1.1 christos @pindex .mod 8434 1.1 christos @file{@var{n}.sym} is made from @file{@var{n}.def} with a command 8435 1.1 christos of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o} 8436 1.1 christos is made from @file{@var{n}.mod}; the form is: 8437 1.1 christos @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill 8438 1.1 christos 8439 1.1 christos @need 1200 8440 1.1 christos @item Assembling and preprocessing assembler programs 8441 1.1 christos @cindex assembly, rule to compile 8442 1.1 christos @pindex as 8443 1.1 christos @pindex .s 8444 1.1 christos @file{@var{n}.o} is made automatically from @file{@var{n}.s} by 8445 1.1 christos running the assembler, @code{as}. The precise command is 8446 1.1 christos @samp{$(AS) $(ASFLAGS)}.@refill 8447 1.1 christos 8448 1.1 christos @pindex .S 8449 1.1 christos @file{@var{n}.s} is made automatically from @file{@var{n}.S} by 8450 1.1 christos running the C preprocessor, @code{cpp}. The precise command is 8451 1.1 christos @w{@samp{$(CPP) $(CPPFLAGS)}}. 8452 1.1 christos 8453 1.1 christos @item Linking a single object file 8454 1.1 christos @cindex linking, predefined rule for 8455 1.1 christos @pindex ld 8456 1.1 christos @pindex .o 8457 1.1 christos @file{@var{n}} is made automatically from @file{@var{n}.o} by running 8458 1.1 christos the linker (usually called @code{ld}) via the C compiler. The precise 8459 1.1 christos command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}. 8460 1.1 christos 8461 1.1 christos This rule does the right thing for a simple program with only one 8462 1.1 christos source file. It will also do the right thing if there are multiple 8463 1.1 christos object files (presumably coming from various other source files), one 8464 1.1 christos of which has a name matching that of the executable file. Thus, 8465 1.1 christos 8466 1.1 christos @example 8467 1.1 christos x: y.o z.o 8468 1.1 christos @end example 8469 1.1 christos 8470 1.1 christos @noindent 8471 1.1 christos when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute: 8472 1.1 christos 8473 1.1 christos @example 8474 1.1 christos @group 8475 1.1 christos cc -c x.c -o x.o 8476 1.1 christos cc -c y.c -o y.o 8477 1.1 christos cc -c z.c -o z.o 8478 1.1 christos cc x.o y.o z.o -o x 8479 1.1 christos rm -f x.o 8480 1.1 christos rm -f y.o 8481 1.1 christos rm -f z.o 8482 1.1 christos @end group 8483 1.1 christos @end example 8484 1.1 christos 8485 1.1 christos @noindent 8486 1.1 christos In more complicated cases, such as when there is no object file whose 8487 1.1 christos name derives from the executable file name, you must write an explicit 8488 1.1 christos command for linking. 8489 1.1 christos 8490 1.1 christos Each kind of file automatically made into @samp{.o} object files will 8491 1.1 christos be automatically linked by using the compiler (@samp{$(CC)}, 8492 1.1 christos @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to 8493 1.1 christos assemble @samp{.s} files) without the @samp{-c} option. This could be 8494 1.1 christos done by using the @samp{.o} object files as intermediates, but it is 8495 1.1 christos faster to do the compiling and linking in one step, so that's how it's 8496 1.1 christos done.@refill 8497 1.1 christos 8498 1.1 christos @item Yacc for C programs 8499 1.1 christos @pindex yacc 8500 1.1 christos @cindex Yacc, rule to run 8501 1.1 christos @pindex .y 8502 1.1 christos @file{@var{n}.c} is made automatically from @file{@var{n}.y} by 8503 1.1 christos running Yacc with the command @samp{$(YACC) $(YFLAGS)}. 8504 1.1 christos 8505 1.1 christos @item Lex for C programs 8506 1.1 christos @pindex lex 8507 1.1 christos @cindex Lex, rule to run 8508 1.1 christos @pindex .l 8509 1.1 christos @file{@var{n}.c} is made automatically from @file{@var{n}.l} by 8510 1.1 christos running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}. 8511 1.1 christos 8512 1.1 christos @item Lex for Ratfor programs 8513 1.1 christos @file{@var{n}.r} is made automatically from @file{@var{n}.l} by 8514 1.1 christos running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}. 8515 1.1 christos 8516 1.1 christos The convention of using the same suffix @samp{.l} for all Lex files 8517 1.1 christos regardless of whether they produce C code or Ratfor code makes it 8518 1.1 christos impossible for @code{make} to determine automatically which of the two 8519 1.1 christos languages you are using in any particular case. If @code{make} is 8520 1.1 christos called upon to remake an object file from a @samp{.l} file, it must 8521 1.1 christos guess which compiler to use. It will guess the C compiler, because 8522 1.1 christos that is more common. If you are using Ratfor, make sure @code{make} 8523 1.1 christos knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you 8524 1.1 christos are using Ratfor exclusively, with no C files, remove @samp{.c} from 8525 1.1 christos the list of implicit rule suffixes with:@refill 8526 1.1 christos 8527 1.1 christos @example 8528 1.1 christos @group 8529 1.1 christos .SUFFIXES: 8530 1.1 christos .SUFFIXES: .o .r .f .l @dots{} 8531 1.1 christos @end group 8532 1.1 christos @end example 8533 1.1 christos 8534 1.1 christos @item Making Lint Libraries from C, Yacc, or Lex programs 8535 1.1 christos @pindex lint 8536 1.1 christos @cindex @code{lint}, rule to run 8537 1.1 christos @pindex .ln 8538 1.1 christos @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}. 8539 1.1 christos The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}. 8540 1.1 christos The same command is used on the C code produced from 8541 1.1 christos @file{@var{n}.y} or @file{@var{n}.l}.@refill 8542 1.1 christos 8543 1.1 christos @item @TeX{} and Web 8544 1.1 christos @cindex @TeX{}, rule to run 8545 1.1 christos @cindex Web, rule to run 8546 1.1 christos @pindex tex 8547 1.1 christos @pindex cweave 8548 1.1 christos @pindex weave 8549 1.1 christos @pindex tangle 8550 1.1 christos @pindex ctangle 8551 1.1 christos @pindex .dvi 8552 1.1 christos @pindex .tex 8553 1.1 christos @pindex .web 8554 1.1 christos @pindex .w 8555 1.1 christos @pindex .ch 8556 1.1 christos @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command 8557 1.1 christos @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with 8558 1.1 christos @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if 8559 1.1 christos it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is 8560 1.1 christos made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c} 8561 1.1 christos is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists 8562 1.1 christos or can be made) with @samp{$(CTANGLE)}.@refill 8563 1.1 christos 8564 1.1 christos @item Texinfo and Info 8565 1.1 christos @cindex Texinfo, rule to format 8566 1.1 christos @cindex Info, rule to format 8567 1.1 christos @pindex texi2dvi 8568 1.1 christos @pindex makeinfo 8569 1.1 christos @pindex .texinfo 8570 1.1 christos @pindex .info 8571 1.1 christos @pindex .texi 8572 1.1 christos @pindex .txinfo 8573 1.1 christos @file{@var{n}.dvi} is made from @file{@var{n}.texinfo}, 8574 1.1 christos @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command 8575 1.1 christos @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from 8576 1.1 christos @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with 8577 1.1 christos the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}. 8578 1.1 christos 8579 1.1 christos @item RCS 8580 1.1 christos @cindex RCS, rule to extract from 8581 1.1 christos @pindex co 8582 1.1 christos @pindex ,v @r{(RCS file extension)} 8583 1.1 christos Any file @file{@var{n}} is extracted if necessary from an RCS file 8584 1.1 christos named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise 8585 1.1 christos command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be 8586 1.1 christos extracted from RCS if it already exists, even if the RCS file is 8587 1.1 christos newer. The rules for RCS are terminal 8588 1.1 christos (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}), 8589 1.1 christos so RCS files cannot be generated from another source; they must 8590 1.1 christos actually exist.@refill 8591 1.1 christos 8592 1.1 christos @item SCCS 8593 1.1 christos @cindex SCCS, rule to extract from 8594 1.1 christos @pindex get 8595 1.1 christos @pindex s. @r{(SCCS file prefix)} 8596 1.1 christos Any file @file{@var{n}} is extracted if necessary from an SCCS file 8597 1.1 christos named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise 8598 1.1 christos command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are 8599 1.1 christos terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}), 8600 1.1 christos so SCCS files cannot be generated from another source; they must 8601 1.1 christos actually exist.@refill 8602 1.1 christos 8603 1.1 christos @pindex .sh 8604 1.1 christos For the benefit of SCCS, a file @file{@var{n}} is copied from 8605 1.1 christos @file{@var{n}.sh} and made executable (by everyone). This is for 8606 1.1 christos shell scripts that are checked into SCCS. Since RCS preserves the 8607 1.1 christos execution permission of a file, you do not need to use this feature 8608 1.1 christos with RCS.@refill 8609 1.1 christos 8610 1.1 christos We recommend that you avoid using of SCCS. RCS is widely held to be 8611 1.1 christos superior, and is also free. By choosing free software in place of 8612 1.1 christos comparable (or inferior) proprietary software, you support the free 8613 1.1 christos software movement. 8614 1.1 christos @end table 8615 1.1 christos 8616 1.1 christos Usually, you want to change only the variables listed in the table 8617 1.1 christos above, which are documented in the following section. 8618 1.1 christos 8619 1.1 christos However, the commands in built-in implicit rules actually use 8620 1.1 christos variables such as @code{COMPILE.c}, @code{LINK.p}, and 8621 1.1 christos @code{PREPROCESS.S}, whose values contain the commands listed above. 8622 1.1 christos 8623 1.1 christos @code{make} follows the convention that the rule to compile a 8624 1.1 christos @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}. 8625 1.1 christos Similarly, the rule to produce an executable from a @file{.@var{x}} 8626 1.1 christos file uses @code{LINK.@var{x}}; and the rule to preprocess a 8627 1.1 christos @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}. 8628 1.1 christos 8629 1.1 christos @vindex OUTPUT_OPTION 8630 1.1 christos Every rule that produces an object file uses the variable 8631 1.1 christos @code{OUTPUT_OPTION}. @code{make} defines this variable either to 8632 1.1 christos contain @samp{-o $@@}, or to be empty, depending on a compile-time 8633 1.1 christos option. You need the @samp{-o} option to ensure that the output goes 8634 1.1 christos into the right file when the source file is in a different directory, 8635 1.1 christos as when using @code{VPATH} (@pxref{Directory Search}). However, 8636 1.1 christos compilers on some systems do not accept a @samp{-o} switch for object 8637 1.1 christos files. If you use such a system, and use @code{VPATH}, some 8638 1.1 christos compilations will put their output in the wrong place. 8639 1.1 christos A possible workaround for this problem is to give @code{OUTPUT_OPTION} 8640 1.1 christos the value @w{@samp{; mv $*.o $@@}}. 8641 1.1 christos 8642 1.1 christos @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules 8643 1.1 christos @section Variables Used by Implicit Rules 8644 1.1 christos @cindex flags for compilers 8645 1.1 christos 8646 1.1 christos The commands in built-in implicit rules make liberal use of certain 8647 1.1 christos predefined variables. You can alter the values of these variables in 8648 1.1 christos the makefile, with arguments to @code{make}, or in the environment to 8649 1.1 christos alter how the implicit rules work without redefining the rules 8650 1.1 christos themselves. You can cancel all variables used by implicit rules with 8651 1.1 christos the @samp{-R} or @samp{--no-builtin-variables} option. 8652 1.1 christos 8653 1.1 christos For example, the command used to compile a C source file actually says 8654 1.1 christos @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables 8655 1.1 christos used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By 8656 1.1 christos redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be 8657 1.1 christos used for all C compilations performed by the implicit rule. By redefining 8658 1.1 christos @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to 8659 1.1 christos each compilation. @emph{All} implicit rules that do C compilation use 8660 1.1 christos @samp{$(CC)} to get the program name for the compiler and @emph{all} 8661 1.1 christos include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill 8662 1.1 christos 8663 1.1 christos The variables used in implicit rules fall into two classes: those that are 8664 1.1 christos names of programs (like @code{CC}) and those that contain arguments for the 8665 1.1 christos programs (like @code{CFLAGS}). (The ``name of a program'' may also contain 8666 1.1 christos some command arguments, but it must start with an actual executable program 8667 1.1 christos name.) If a variable value contains more than one argument, separate them 8668 1.1 christos with spaces. 8669 1.1 christos 8670 1.1 christos The following tables describe of some of the more commonly-used predefined 8671 1.1 christos variables. This list is not exhaustive, and the default values shown here may 8672 1.1 christos not be what are selected by @code{make} for your environment. To see the 8673 1.1 christos complete list of predefined variables for your instance of GNU @code{make} you 8674 1.1 christos can run @samp{make -p} in a directory with no makefiles. 8675 1.1 christos 8676 1.1 christos Here is a table of some of the more common variables used as names of 8677 1.1 christos programs in built-in rules: 8678 1.1 christos makefiles. 8679 1.1 christos 8680 1.1 christos @table @code 8681 1.1 christos @item AR 8682 1.1 christos @vindex AR 8683 1.1 christos Archive-maintaining program; default @samp{ar}. 8684 1.1 christos @pindex ar 8685 1.1 christos 8686 1.1 christos @item AS 8687 1.1 christos @vindex AS 8688 1.1 christos Program for compiling assembly files; default @samp{as}. 8689 1.1 christos @pindex as 8690 1.1 christos 8691 1.1 christos @item CC 8692 1.1 christos @vindex CC 8693 1.1 christos Program for compiling C programs; default @samp{cc}. 8694 1.1 christos @pindex cc 8695 1.1 christos 8696 1.1 christos @item CO 8697 1.1 christos @vindex CO 8698 1.1 christos Program for checking out files from RCS; default @samp{co}. 8699 1.1 christos @pindex cc 8700 1.1 christos 8701 1.1 christos @item CXX 8702 1.1 christos @vindex CXX 8703 1.1 christos Program for compiling C++ programs; default @samp{g++}. 8704 1.1 christos @pindex g++ 8705 1.1 christos 8706 1.1 christos @item CO 8707 1.1 christos @vindex CO 8708 1.1 christos Program for extracting a file from RCS; default @samp{co}. 8709 1.1 christos @pindex co 8710 1.1 christos 8711 1.1 christos @item CPP 8712 1.1 christos @vindex CPP 8713 1.1 christos Program for running the C preprocessor, with results to standard output; 8714 1.1 christos default @samp{$(CC) -E}. 8715 1.1 christos 8716 1.1 christos @item FC 8717 1.1 christos @vindex FC 8718 1.1 christos Program for compiling or preprocessing Fortran and Ratfor programs; 8719 1.1 christos default @samp{f77}. 8720 1.1 christos @pindex f77 8721 1.1 christos 8722 1.1 christos @item GET 8723 1.1 christos @vindex GET 8724 1.1 christos Program for extracting a file from SCCS; default @samp{get}. 8725 1.1 christos @pindex get 8726 1.1 christos 8727 1.1 christos @item LEX 8728 1.1 christos @vindex LEX 8729 1.1 christos Program to use to turn Lex grammars into source code; default @samp{lex}. 8730 1.1 christos @pindex lex 8731 1.1 christos 8732 1.1 christos @item YACC 8733 1.1 christos @vindex YACC 8734 1.1 christos Program to use to turn Yacc grammars into source code; default @samp{yacc}. 8735 1.1 christos @pindex yacc 8736 1.1 christos 8737 1.1 christos @item LINT 8738 1.1 christos @vindex LINT 8739 1.1 christos Program to use to run lint on source code; default @samp{lint}. 8740 1.1 christos @pindex lint 8741 1.1 christos 8742 1.1 christos @item M2C 8743 1.1 christos @vindex M2C 8744 1.1 christos Program to use to compile Modula-2 source code; default @samp{m2c}. 8745 1.1 christos @pindex m2c 8746 1.1 christos 8747 1.1 christos @item PC 8748 1.1 christos @vindex PC 8749 1.1 christos Program for compiling Pascal programs; default @samp{pc}. 8750 1.1 christos @pindex pc 8751 1.1 christos 8752 1.1 christos @item MAKEINFO 8753 1.1 christos @vindex MAKEINFO 8754 1.1 christos Program to convert a Texinfo source file into an Info file; default 8755 1.1 christos @samp{makeinfo}. 8756 1.1 christos @pindex makeinfo 8757 1.1 christos 8758 1.1 christos @item TEX 8759 1.1 christos @vindex TEX 8760 1.1 christos Program to make @TeX{} @sc{dvi} files from @TeX{} source; 8761 1.1 christos default @samp{tex}. 8762 1.1 christos @pindex tex 8763 1.1 christos 8764 1.1 christos @item TEXI2DVI 8765 1.1 christos @vindex TEXI2DVI 8766 1.1 christos Program to make @TeX{} @sc{dvi} files from Texinfo source; 8767 1.1 christos default @samp{texi2dvi}. 8768 1.1 christos @pindex texi2dvi 8769 1.1 christos 8770 1.1 christos @item WEAVE 8771 1.1 christos @vindex WEAVE 8772 1.1 christos Program to translate Web into @TeX{}; default @samp{weave}. 8773 1.1 christos @pindex weave 8774 1.1 christos 8775 1.1 christos @item CWEAVE 8776 1.1 christos @vindex CWEAVE 8777 1.1 christos Program to translate C Web into @TeX{}; default @samp{cweave}. 8778 1.1 christos @pindex cweave 8779 1.1 christos 8780 1.1 christos @item TANGLE 8781 1.1 christos @vindex TANGLE 8782 1.1 christos Program to translate Web into Pascal; default @samp{tangle}. 8783 1.1 christos @pindex tangle 8784 1.1 christos 8785 1.1 christos @item CTANGLE 8786 1.1 christos @vindex CTANGLE 8787 1.1 christos Program to translate C Web into C; default @samp{ctangle}. 8788 1.1 christos @pindex ctangle 8789 1.1 christos 8790 1.1 christos @item RM 8791 1.1 christos @vindex RM 8792 1.1 christos Command to remove a file; default @samp{rm -f}. 8793 1.1 christos @pindex rm 8794 1.1 christos @end table 8795 1.1 christos 8796 1.1 christos Here is a table of variables whose values are additional arguments for the 8797 1.1 christos programs above. The default values for all of these is the empty 8798 1.1 christos string, unless otherwise noted. 8799 1.1 christos 8800 1.1 christos @table @code 8801 1.1 christos @item ARFLAGS 8802 1.1 christos @vindex ARFLAGS 8803 1.1 christos Flags to give the archive-maintaining program; default @samp{rv}. 8804 1.1 christos 8805 1.1 christos @item ASFLAGS 8806 1.1 christos @vindex ASFLAGS 8807 1.1 christos Extra flags to give to the assembler (when explicitly 8808 1.1 christos invoked on a @samp{.s} or @samp{.S} file). 8809 1.1 christos 8810 1.1 christos @item CFLAGS 8811 1.1 christos @vindex CFLAGS 8812 1.1 christos Extra flags to give to the C compiler. 8813 1.1 christos 8814 1.1 christos @item CXXFLAGS 8815 1.1 christos @vindex CXXFLAGS 8816 1.1 christos Extra flags to give to the C++ compiler. 8817 1.1 christos 8818 1.1 christos @item COFLAGS 8819 1.1 christos @vindex COFLAGS 8820 1.1 christos Extra flags to give to the RCS @code{co} program. 8821 1.1 christos 8822 1.1 christos @item CPPFLAGS 8823 1.1 christos @vindex CPPFLAGS 8824 1.1 christos Extra flags to give to the C preprocessor and programs 8825 1.1 christos that use it (the C and Fortran compilers). 8826 1.1 christos 8827 1.1 christos @item FFLAGS 8828 1.1 christos @vindex FFLAGS 8829 1.1 christos Extra flags to give to the Fortran compiler. 8830 1.1 christos 8831 1.1 christos @item GFLAGS 8832 1.1 christos @vindex GFLAGS 8833 1.1 christos Extra flags to give to the SCCS @code{get} program. 8834 1.1 christos 8835 1.1 christos @item LDFLAGS 8836 1.1 christos @vindex LDFLAGS 8837 1.1 christos Extra flags to give to compilers when they are supposed to invoke the linker, 8838 1.1 christos @samp{ld}. 8839 1.1 christos 8840 1.1 christos @item LFLAGS 8841 1.1 christos @vindex LFLAGS 8842 1.1 christos Extra flags to give to Lex. 8843 1.1 christos 8844 1.1 christos @item YFLAGS 8845 1.1 christos @vindex YFLAGS 8846 1.1 christos Extra flags to give to Yacc. 8847 1.1 christos 8848 1.1 christos @item PFLAGS 8849 1.1 christos @vindex PFLAGS 8850 1.1 christos Extra flags to give to the Pascal compiler. 8851 1.1 christos 8852 1.1 christos @item RFLAGS 8853 1.1 christos @vindex RFLAGS 8854 1.1 christos Extra flags to give to the Fortran compiler for Ratfor programs. 8855 1.1 christos 8856 1.1 christos @item LINTFLAGS 8857 1.1 christos @vindex LINTFLAGS 8858 1.1 christos Extra flags to give to lint. 8859 1.1 christos @end table 8860 1.1 christos 8861 1.1 christos @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules 8862 1.1 christos @section Chains of Implicit Rules 8863 1.1 christos 8864 1.1 christos @cindex chains of rules 8865 1.1 christos @cindex rule, implicit, chains of 8866 1.1 christos Sometimes a file can be made by a sequence of implicit rules. For example, 8867 1.1 christos a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running 8868 1.1 christos first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}. 8869 1.1 christos 8870 1.1 christos If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no 8871 1.1 christos special searching is required: @code{make} finds that the object file can 8872 1.1 christos be made by C compilation from @file{@var{n}.c}; later on, when considering 8873 1.1 christos how to make @file{@var{n}.c}, the rule for running Yacc is 8874 1.1 christos used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are 8875 1.1 christos updated.@refill 8876 1.1 christos 8877 1.1 christos @cindex intermediate files 8878 1.1 christos @cindex files, intermediate 8879 1.1 christos However, even if @file{@var{n}.c} does not exist and is not mentioned, 8880 1.1 christos @code{make} knows how to envision it as the missing link between 8881 1.1 christos @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is 8882 1.1 christos called an @dfn{intermediate file}. Once @code{make} has decided to use the 8883 1.1 christos intermediate file, it is entered in the data base as if it had been 8884 1.1 christos mentioned in the makefile, along with the implicit rule that says how to 8885 1.1 christos create it.@refill 8886 1.1 christos 8887 1.1 christos Intermediate files are remade using their rules just like all other 8888 1.1 christos files. But intermediate files are treated differently in two ways. 8889 1.1 christos 8890 1.1 christos The first difference is what happens if the intermediate file does not 8891 1.1 christos exist. If an ordinary file @var{b} does not exist, and @code{make} 8892 1.1 christos considers a target that depends on @var{b}, it invariably creates 8893 1.1 christos @var{b} and then updates the target from @var{b}. But if @var{b} is an 8894 1.1 christos intermediate file, then @code{make} can leave well enough alone. It 8895 1.1 christos won't bother updating @var{b}, or the ultimate target, unless some 8896 1.1 christos prerequisite of @var{b} is newer than that target or there is some other 8897 1.1 christos reason to update that target. 8898 1.1 christos 8899 1.1 christos The second difference is that if @code{make} @emph{does} create @var{b} 8900 1.1 christos in order to update something else, it deletes @var{b} later on after it 8901 1.1 christos is no longer needed. Therefore, an intermediate file which did not 8902 1.1 christos exist before @code{make} also does not exist after @code{make}. 8903 1.1 christos @code{make} reports the deletion to you by printing a @samp{rm -f} 8904 1.1 christos command showing which file it is deleting. 8905 1.1 christos 8906 1.1 christos Ordinarily, a file cannot be intermediate if it is mentioned in the 8907 1.1 christos makefile as a target or prerequisite. However, you can explicitly mark a 8908 1.1 christos file as intermediate by listing it as a prerequisite of the special target 8909 1.1 christos @code{.INTERMEDIATE}. This takes effect even if the file is mentioned 8910 1.1 christos explicitly in some other way. 8911 1.1 christos 8912 1.1 christos @cindex intermediate files, preserving 8913 1.1 christos @cindex preserving intermediate files 8914 1.1 christos @cindex secondary files 8915 1.1 christos You can prevent automatic deletion of an intermediate file by marking it 8916 1.1 christos as a @dfn{secondary} file. To do this, list it as a prerequisite of the 8917 1.1 christos special target @code{.SECONDARY}. When a file is secondary, @code{make} 8918 1.1 christos will not create the file merely because it does not already exist, but 8919 1.1 christos @code{make} does not automatically delete the file. Marking a file as 8920 1.1 christos secondary also marks it as intermediate. 8921 1.1 christos 8922 1.1 christos You can list the target pattern of an implicit rule (such as @samp{%.o}) 8923 1.1 christos as a prerequisite of the special target @code{.PRECIOUS} to preserve 8924 1.1 christos intermediate files made by implicit rules whose target patterns match 8925 1.1 christos that file's name; see @ref{Interrupts}.@refill 8926 1.1 christos @cindex preserving with @code{.PRECIOUS} 8927 1.1 christos @cindex @code{.PRECIOUS} intermediate files 8928 1.1 christos 8929 1.1 christos A chain can involve more than two implicit rules. For example, it is 8930 1.1 christos possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS, 8931 1.1 christos Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are 8932 1.1 christos intermediate files that are deleted at the end.@refill 8933 1.1 christos 8934 1.1 christos No single implicit rule can appear more than once in a chain. This means 8935 1.1 christos that @code{make} will not even consider such a ridiculous thing as making 8936 1.1 christos @file{foo} from @file{foo.o.o} by running the linker twice. This 8937 1.1 christos constraint has the added benefit of preventing any infinite loop in the 8938 1.1 christos search for an implicit rule chain. 8939 1.1 christos 8940 1.1 christos There are some special implicit rules to optimize certain cases that would 8941 1.1 christos otherwise be handled by rule chains. For example, making @file{foo} from 8942 1.1 christos @file{foo.c} could be handled by compiling and linking with separate 8943 1.1 christos chained rules, using @file{foo.o} as an intermediate file. But what 8944 1.1 christos actually happens is that a special rule for this case does the compilation 8945 1.1 christos and linking with a single @code{cc} command. The optimized rule is used in 8946 1.1 christos preference to the step-by-step chain because it comes earlier in the 8947 1.1 christos ordering of rules. 8948 1.1 christos 8949 1.1 christos @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules 8950 1.1 christos @section Defining and Redefining Pattern Rules 8951 1.1 christos 8952 1.1 christos You define an implicit rule by writing a @dfn{pattern rule}. A pattern 8953 1.1 christos rule looks like an ordinary rule, except that its target contains the 8954 1.1 christos character @samp{%} (exactly one of them). The target is considered a 8955 1.1 christos pattern for matching file names; the @samp{%} can match any nonempty 8956 1.1 christos substring, while other characters match only themselves. The prerequisites 8957 1.1 christos likewise use @samp{%} to show how their names relate to the target name. 8958 1.1 christos 8959 1.1 christos Thus, a pattern rule @samp{%.o : %.c} says how to make any file 8960 1.1 christos @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill 8961 1.1 christos 8962 1.1 christos Note that expansion using @samp{%} in pattern rules occurs 8963 1.1 christos @strong{after} any variable or function expansions, which take place 8964 1.1 christos when the makefile is read. @xref{Using Variables, , How to Use 8965 1.1 christos Variables}, and @ref{Functions, ,Functions for Transforming Text}. 8966 1.1 christos 8967 1.1 christos @menu 8968 1.1 christos * Pattern Intro:: An introduction to pattern rules. 8969 1.1 christos * Pattern Examples:: Examples of pattern rules. 8970 1.1 christos * Automatic Variables:: How to use automatic variables in the 8971 1.1 christos commands of implicit rules. 8972 1.1 christos * Pattern Match:: How patterns match. 8973 1.1 christos * Match-Anything Rules:: Precautions you should take prior to 8974 1.1 christos defining rules that can match any 8975 1.1 christos target file whatever. 8976 1.1 christos * Canceling Rules:: How to override or cancel built-in rules. 8977 1.1 christos @end menu 8978 1.1 christos 8979 1.1 christos @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules 8980 1.1 christos @subsection Introduction to Pattern Rules 8981 1.1 christos @cindex pattern rule 8982 1.1 christos @cindex rule, pattern 8983 1.1 christos 8984 1.1 christos A pattern rule contains the character @samp{%} (exactly one of them) 8985 1.1 christos in the target; otherwise, it looks exactly like an ordinary rule. The 8986 1.1 christos target is a pattern for matching file names; the @samp{%} matches any 8987 1.1 christos nonempty substring, while other characters match only themselves. 8988 1.1 christos @cindex target pattern, implicit 8989 1.1 christos @cindex @code{%}, in pattern rules 8990 1.1 christos 8991 1.1 christos For example, @samp{%.c} as a pattern matches any file name that ends in 8992 1.1 christos @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts 8993 1.1 christos with @samp{s.}, ends in @samp{.c} and is at least five characters long. 8994 1.1 christos (There must be at least one character to match the @samp{%}.) The substring 8995 1.1 christos that the @samp{%} matches is called the @dfn{stem}.@refill 8996 1.1 christos 8997 1.1 christos @samp{%} in a prerequisite of a pattern rule stands for the same stem 8998 1.1 christos that was matched by the @samp{%} in the target. In order for the 8999 1.1 christos pattern rule to apply, its target pattern must match the file name 9000 1.1 christos under consideration and all of its prerequisites (after pattern 9001 1.1 christos substitution) must name files that exist or can be made. These files 9002 1.1 christos become prerequisites of the target. 9003 1.1 christos @cindex prerequisite pattern, implicit 9004 1.1 christos 9005 1.1 christos Thus, a rule of the form 9006 1.1 christos 9007 1.1 christos @example 9008 1.1 christos %.o : %.c ; @var{command}@dots{} 9009 1.1 christos @end example 9010 1.1 christos 9011 1.1 christos @noindent 9012 1.1 christos specifies how to make a file @file{@var{n}.o}, with another file 9013 1.1 christos @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c} 9014 1.1 christos exists or can be made. 9015 1.1 christos 9016 1.1 christos There may also be prerequisites that do not use @samp{%}; such a prerequisite 9017 1.1 christos attaches to every file made by this pattern rule. These unvarying 9018 1.1 christos prerequisites are useful occasionally. 9019 1.1 christos 9020 1.1 christos A pattern rule need not have any prerequisites that contain @samp{%}, or 9021 1.1 christos in fact any prerequisites at all. Such a rule is effectively a general 9022 1.1 christos wildcard. It provides a way to make any file that matches the target 9023 1.1 christos pattern. @xref{Last Resort}. 9024 1.1 christos 9025 1.1 christos @c !!! The end of of this paragraph should be rewritten. --bob 9026 1.1 christos Pattern rules may have more than one target. Unlike normal rules, this 9027 1.1 christos does not act as many different rules with the same prerequisites and 9028 1.1 christos commands. If a pattern rule has multiple targets, @code{make} knows that 9029 1.1 christos the rule's commands are responsible for making all of the targets. The 9030 1.1 christos commands are executed only once to make all the targets. When searching 9031 1.1 christos for a pattern rule to match a target, the target patterns of a rule other 9032 1.1 christos than the one that matches the target in need of a rule are incidental: 9033 1.1 christos @code{make} worries only about giving commands and prerequisites to the file 9034 1.1 christos presently in question. However, when this file's commands are run, the 9035 1.1 christos other targets are marked as having been updated themselves. 9036 1.1 christos @cindex multiple targets, in pattern rule 9037 1.1 christos @cindex target, multiple in pattern rule 9038 1.1 christos 9039 1.1 christos The order in which pattern rules appear in the makefile is important 9040 1.1 christos since this is the order in which they are considered. 9041 1.1 christos Of equally applicable 9042 1.1 christos rules, only the first one found is used. The rules you write take precedence 9043 1.1 christos over those that are built in. Note however, that a rule whose 9044 1.1 christos prerequisites actually exist or are mentioned always takes priority over a 9045 1.1 christos rule with prerequisites that must be made by chaining other implicit rules. 9046 1.1 christos @cindex pattern rules, order of 9047 1.1 christos @cindex order of pattern rules 9048 1.1 christos 9049 1.1 christos @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules 9050 1.1 christos @subsection Pattern Rule Examples 9051 1.1 christos 9052 1.1 christos Here are some examples of pattern rules actually predefined in 9053 1.1 christos @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o} 9054 1.1 christos files:@refill 9055 1.1 christos 9056 1.1 christos @example 9057 1.1 christos %.o : %.c 9058 1.1 christos $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@ 9059 1.1 christos @end example 9060 1.1 christos 9061 1.1 christos @noindent 9062 1.1 christos defines a rule that can make any file @file{@var{x}.o} from 9063 1.1 christos @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and 9064 1.1 christos @samp{$<} to substitute the names of the target file and the source file 9065 1.1 christos in each case where the rule applies (@pxref{Automatic Variables}).@refill 9066 1.1 christos 9067 1.1 christos Here is a second built-in rule: 9068 1.1 christos 9069 1.1 christos @example 9070 1.1 christos % :: RCS/%,v 9071 1.1 christos $(CO) $(COFLAGS) $< 9072 1.1 christos @end example 9073 1.1 christos 9074 1.1 christos @noindent 9075 1.1 christos defines a rule that can make any file @file{@var{x}} whatsoever from a 9076 1.1 christos corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since 9077 1.1 christos the target is @samp{%}, this rule will apply to any file whatever, provided 9078 1.1 christos the appropriate prerequisite file exists. The double colon makes the rule 9079 1.1 christos @dfn{terminal}, which means that its prerequisite may not be an intermediate 9080 1.1 christos file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill 9081 1.1 christos 9082 1.1 christos @need 500 9083 1.1 christos This pattern rule has two targets: 9084 1.1 christos 9085 1.1 christos @example 9086 1.1 christos @group 9087 1.1 christos %.tab.c %.tab.h: %.y 9088 1.1 christos bison -d $< 9089 1.1 christos @end group 9090 1.1 christos @end example 9091 1.1 christos 9092 1.1 christos @noindent 9093 1.1 christos @c The following paragraph is rewritten to avoid overfull hboxes 9094 1.1 christos This tells @code{make} that the command @samp{bison -d @var{x}.y} will 9095 1.1 christos make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file 9096 1.1 christos @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o} 9097 1.1 christos and the file @file{scan.o} depends on the file @file{parse.tab.h}, 9098 1.1 christos when @file{parse.y} is changed, the command @samp{bison -d parse.y} 9099 1.1 christos will be executed only once, and the prerequisites of both 9100 1.1 christos @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably 9101 1.1 christos the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c} 9102 1.1 christos and the file @file{scan.o} from @file{scan.c}, while @file{foo} is 9103 1.1 christos linked from @file{parse.tab.o}, @file{scan.o}, and its other 9104 1.1 christos prerequisites, and it will execute happily ever after.)@refill 9105 1.1 christos 9106 1.1 christos @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules 9107 1.1 christos @subsection Automatic Variables 9108 1.1 christos @cindex automatic variables 9109 1.1 christos @cindex variables, automatic 9110 1.1 christos @cindex variables, and implicit rule 9111 1.1 christos 9112 1.1 christos Suppose you are writing a pattern rule to compile a @samp{.c} file into a 9113 1.1 christos @samp{.o} file: how do you write the @samp{cc} command so that it operates 9114 1.1 christos on the right source file name? You cannot write the name in the command, 9115 1.1 christos because the name is different each time the implicit rule is applied. 9116 1.1 christos 9117 1.1 christos What you do is use a special feature of @code{make}, the @dfn{automatic 9118 1.1 christos variables}. These variables have values computed afresh for each rule that 9119 1.1 christos is executed, based on the target and prerequisites of the rule. In this 9120 1.1 christos example, you would use @samp{$@@} for the object file name and @samp{$<} 9121 1.1 christos for the source file name. 9122 1.1 christos 9123 1.1 christos @cindex automatic variables in prerequisites 9124 1.1 christos @cindex prerequisites, and automatic variables 9125 1.1 christos It's very important that you recognize the limited scope in which 9126 1.1 christos automatic variable values are available: they only have values within 9127 1.1 christos the command script. In particular, you cannot use them anywhere 9128 1.1 christos within the target list of a rule; they have no value there and will 9129 1.1 christos expand to the empty string. Also, they cannot be accessed directly 9130 1.1 christos within the prerequisite list of a rule. A common mistake is 9131 1.1 christos attempting to use @code{$@@} within the prerequisites list; this will 9132 1.1 christos not work. However, there is a special feature of GNU @code{make}, 9133 1.1 christos secondary expansion (@pxref{Secondary Expansion}), which will allow 9134 1.1 christos automatic variable values to be used in prerequisite lists. 9135 1.1 christos 9136 1.1 christos Here is a table of automatic variables: 9137 1.1 christos 9138 1.1 christos @table @code 9139 1.1 christos @vindex $@@ 9140 1.1 christos @vindex @@ @r{(automatic variable)} 9141 1.1 christos @item $@@ 9142 1.1 christos The file name of the target of the rule. If the target is an archive 9143 1.1 christos member, then @samp{$@@} is the name of the archive file. In a pattern 9144 1.1 christos rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to 9145 1.1 christos Pattern Rules}), @samp{$@@} is the name of whichever target caused the 9146 1.1 christos rule's commands to be run. 9147 1.1 christos 9148 1.1 christos @vindex $% 9149 1.1 christos @vindex % @r{(automatic variable)} 9150 1.1 christos @item $% 9151 1.1 christos The target member name, when the target is an archive member. 9152 1.1 christos @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then 9153 1.1 christos @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is 9154 1.1 christos empty when the target is not an archive member. 9155 1.1 christos 9156 1.1 christos @vindex $< 9157 1.1 christos @vindex < @r{(automatic variable)} 9158 1.1 christos @item $< 9159 1.1 christos The name of the first prerequisite. If the target got its commands from 9160 1.1 christos an implicit rule, this will be the first prerequisite added by the 9161 1.1 christos implicit rule (@pxref{Implicit Rules}). 9162 1.1 christos 9163 1.1 christos @vindex $? 9164 1.1 christos @vindex ? @r{(automatic variable)} 9165 1.1 christos @item $? 9166 1.1 christos The names of all the prerequisites that are newer than the target, with 9167 1.1 christos spaces between them. For prerequisites which are archive members, only 9168 1.1 christos the member named is used (@pxref{Archives}). 9169 1.1 christos @cindex prerequisites, list of changed 9170 1.1 christos @cindex list of changed prerequisites 9171 1.1 christos 9172 1.1 christos @vindex $^ 9173 1.1 christos @vindex ^ @r{(automatic variable)} 9174 1.1 christos @item $^ 9175 1.1 christos The names of all the prerequisites, with spaces between them. For 9176 1.1 christos prerequisites which are archive members, only the member named is used 9177 1.1 christos (@pxref{Archives}). A target has only one prerequisite on each other file 9178 1.1 christos it depends on, no matter how many times each file is listed as a 9179 1.1 christos prerequisite. So if you list a prerequisite more than once for a target, 9180 1.1 christos the value of @code{$^} contains just one copy of the name. This list 9181 1.1 christos does @strong{not} contain any of the order-only prerequisites; for those 9182 1.1 christos see the @samp{$|} variable, below. 9183 1.1 christos @cindex prerequisites, list of all 9184 1.1 christos @cindex list of all prerequisites 9185 1.1 christos 9186 1.1 christos @vindex $+ 9187 1.1 christos @vindex + @r{(automatic variable)} 9188 1.1 christos @item $+ 9189 1.1 christos This is like @samp{$^}, but prerequisites listed more than once are 9190 1.1 christos duplicated in the order they were listed in the makefile. This is 9191 1.1 christos primarily useful for use in linking commands where it is meaningful to 9192 1.1 christos repeat library file names in a particular order. 9193 1.1 christos 9194 1.1 christos @vindex $| 9195 1.1 christos @vindex | @r{(automatic variable)} 9196 1.1 christos @item $| 9197 1.1 christos The names of all the order-only prerequisites, with spaces between 9198 1.1 christos them. 9199 1.1 christos 9200 1.1 christos @vindex $* 9201 1.1 christos @vindex * @r{(automatic variable)} 9202 1.1 christos @item $* 9203 1.1 christos The stem with which an implicit rule matches (@pxref{Pattern Match, ,How 9204 1.1 christos Patterns Match}). If the target is @file{dir/a.foo.b} and the target 9205 1.1 christos pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is 9206 1.1 christos useful for constructing names of related files.@refill 9207 1.1 christos @cindex stem, variable for 9208 1.1 christos 9209 1.1 christos In a static pattern rule, the stem is part of the file name that matched 9210 1.1 christos the @samp{%} in the target pattern. 9211 1.1 christos 9212 1.1 christos In an explicit rule, there is no stem; so @samp{$*} cannot be determined 9213 1.1 christos in that way. Instead, if the target name ends with a recognized suffix 9214 1.1 christos (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to 9215 1.1 christos the target name minus the suffix. For example, if the target name is 9216 1.1 christos @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a 9217 1.1 christos suffix. GNU @code{make} does this bizarre thing only for compatibility 9218 1.1 christos with other implementations of @code{make}. You should generally avoid 9219 1.1 christos using @samp{$*} except in implicit rules or static pattern rules.@refill 9220 1.1 christos 9221 1.1 christos If the target name in an explicit rule does not end with a recognized 9222 1.1 christos suffix, @samp{$*} is set to the empty string for that rule. 9223 1.1 christos @end table 9224 1.1 christos 9225 1.1 christos @samp{$?} is useful even in explicit rules when you wish to operate on only 9226 1.1 christos the prerequisites that have changed. For example, suppose that an archive 9227 1.1 christos named @file{lib} is supposed to contain copies of several object files. 9228 1.1 christos This rule copies just the changed object files into the archive: 9229 1.1 christos 9230 1.1 christos @example 9231 1.1 christos @group 9232 1.1 christos lib: foo.o bar.o lose.o win.o 9233 1.1 christos ar r lib $? 9234 1.1 christos @end group 9235 1.1 christos @end example 9236 1.1 christos 9237 1.1 christos Of the variables listed above, four have values that are single file 9238 1.1 christos names, and three have values that are lists of file names. These seven 9239 1.1 christos have variants that get just the file's directory name or just the file 9240 1.1 christos name within the directory. The variant variables' names are formed by 9241 1.1 christos appending @samp{D} or @samp{F}, respectively. These variants are 9242 1.1 christos semi-obsolete in GNU @code{make} since the functions @code{dir} and 9243 1.1 christos @code{notdir} can be used to get a similar effect (@pxref{File Name 9244 1.1 christos Functions, , Functions for File Names}). Note, however, that the 9245 1.1 christos @samp{D} variants all omit the trailing slash which always appears in 9246 1.1 christos the output of the @code{dir} function. Here is a table of the variants: 9247 1.1 christos 9248 1.1 christos @table @samp 9249 1.1 christos @vindex $(@@D) 9250 1.1 christos @vindex @@D @r{(automatic variable)} 9251 1.1 christos @item $(@@D) 9252 1.1 christos The directory part of the file name of the target, with the trailing 9253 1.1 christos slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then 9254 1.1 christos @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does 9255 1.1 christos not contain a slash. 9256 1.1 christos 9257 1.1 christos @vindex $(@@F) 9258 1.1 christos @vindex @@F @r{(automatic variable)} 9259 1.1 christos @item $(@@F) 9260 1.1 christos The file-within-directory part of the file name of the target. If the 9261 1.1 christos value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is 9262 1.1 christos @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}. 9263 1.1 christos 9264 1.1 christos @vindex $(*D) 9265 1.1 christos @vindex *D @r{(automatic variable)} 9266 1.1 christos @item $(*D) 9267 1.1 christos @vindex $(*F) 9268 1.1 christos @vindex *F @r{(automatic variable)} 9269 1.1 christos @itemx $(*F) 9270 1.1 christos The directory part and the file-within-directory 9271 1.1 christos part of the stem; @file{dir} and @file{foo} in this example. 9272 1.1 christos 9273 1.1 christos @vindex $(%D) 9274 1.1 christos @vindex %D @r{(automatic variable)} 9275 1.1 christos @item $(%D) 9276 1.1 christos @vindex $(%F) 9277 1.1 christos @vindex %F @r{(automatic variable)} 9278 1.1 christos @itemx $(%F) 9279 1.1 christos The directory part and the file-within-directory part of the target 9280 1.1 christos archive member name. This makes sense only for archive member targets 9281 1.1 christos of the form @file{@var{archive}(@var{member})} and is useful only when 9282 1.1 christos @var{member} may contain a directory name. (@xref{Archive Members, 9283 1.1 christos ,Archive Members as Targets}.) 9284 1.1 christos 9285 1.1 christos @vindex $(<D) 9286 1.1 christos @vindex <D @r{(automatic variable)} 9287 1.1 christos @item $(<D) 9288 1.1 christos @vindex $(<F) 9289 1.1 christos @vindex <F @r{(automatic variable)} 9290 1.1 christos @itemx $(<F) 9291 1.1 christos The directory part and the file-within-directory 9292 1.1 christos part of the first prerequisite. 9293 1.1 christos 9294 1.1 christos @vindex $(^D) 9295 1.1 christos @vindex ^D @r{(automatic variable)} 9296 1.1 christos @item $(^D) 9297 1.1 christos @vindex $(^F) 9298 1.1 christos @vindex ^F @r{(automatic variable)} 9299 1.1 christos @itemx $(^F) 9300 1.1 christos Lists of the directory parts and the file-within-directory 9301 1.1 christos parts of all prerequisites. 9302 1.1 christos 9303 1.1 christos @vindex $(+D) 9304 1.1 christos @vindex +D @r{(automatic variable)} 9305 1.1 christos @item $(+D) 9306 1.1 christos @vindex $(+F) 9307 1.1 christos @vindex +F @r{(automatic variable)} 9308 1.1 christos @itemx $(+F) 9309 1.1 christos Lists of the directory parts and the file-within-directory 9310 1.1 christos parts of all prerequisites, including multiple instances of duplicated 9311 1.1 christos prerequisites. 9312 1.1 christos 9313 1.1 christos @vindex $(?D) 9314 1.1 christos @vindex ?D @r{(automatic variable)} 9315 1.1 christos @item $(?D) 9316 1.1 christos @vindex $(?F) 9317 1.1 christos @vindex ?F @r{(automatic variable)} 9318 1.1 christos @itemx $(?F) 9319 1.1 christos Lists of the directory parts and the file-within-directory parts of 9320 1.1 christos all prerequisites that are newer than the target. 9321 1.1 christos @end table 9322 1.1 christos 9323 1.1 christos Note that we use a special stylistic convention when we talk about these 9324 1.1 christos automatic variables; we write ``the value of @samp{$<}'', rather than 9325 1.1 christos @w{``the variable @code{<}''} as we would write for ordinary variables 9326 1.1 christos such as @code{objects} and @code{CFLAGS}. We think this convention 9327 1.1 christos looks more natural in this special case. Please do not assume it has a 9328 1.1 christos deep significance; @samp{$<} refers to the variable named @code{<} just 9329 1.1 christos as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}. 9330 1.1 christos You could just as well use @samp{$(<)} in place of @samp{$<}. 9331 1.1 christos 9332 1.1 christos @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules 9333 1.1 christos @subsection How Patterns Match 9334 1.1 christos 9335 1.1 christos @cindex stem 9336 1.1 christos A target pattern is composed of a @samp{%} between a prefix and a suffix, 9337 1.1 christos either or both of which may be empty. The pattern matches a file name only 9338 1.1 christos if the file name starts with the prefix and ends with the suffix, without 9339 1.1 christos overlap. The text between the prefix and the suffix is called the 9340 1.1 christos @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name 9341 1.1 christos @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are 9342 1.1 christos turned into actual file names by substituting the stem for the character 9343 1.1 christos @samp{%}. Thus, if in the same example one of the prerequisites is written 9344 1.1 christos as @samp{%.c}, it expands to @samp{test.c}.@refill 9345 1.1 christos 9346 1.1 christos When the target pattern does not contain a slash (and it usually does 9347 1.1 christos not), directory names in the file names are removed from the file name 9348 1.1 christos before it is compared with the target prefix and suffix. After the 9349 1.1 christos comparison of the file name to the target pattern, the directory 9350 1.1 christos names, along with the slash that ends them, are added on to the 9351 1.1 christos prerequisite file names generated from the pattern rule's prerequisite 9352 1.1 christos patterns and the file name. The directories are ignored only for the 9353 1.1 christos purpose of finding an implicit rule to use, not in the application of 9354 1.1 christos that rule. Thus, @samp{e%t} matches the file name @file{src/eat}, 9355 1.1 christos with @samp{src/a} as the stem. When prerequisites are turned into file 9356 1.1 christos names, the directories from the stem are added at the front, while the 9357 1.1 christos rest of the stem is substituted for the @samp{%}. The stem 9358 1.1 christos @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name 9359 1.1 christos @file{src/car}.@refill 9360 1.1 christos 9361 1.1 christos @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules 9362 1.1 christos @subsection Match-Anything Pattern Rules 9363 1.1 christos 9364 1.1 christos @cindex match-anything rule 9365 1.1 christos @cindex terminal rule 9366 1.1 christos When a pattern rule's target is just @samp{%}, it matches any file name 9367 1.1 christos whatever. We call these rules @dfn{match-anything} rules. They are very 9368 1.1 christos useful, but it can take a lot of time for @code{make} to think about them, 9369 1.1 christos because it must consider every such rule for each file name listed either 9370 1.1 christos as a target or as a prerequisite. 9371 1.1 christos 9372 1.1 christos Suppose the makefile mentions @file{foo.c}. For this target, @code{make} 9373 1.1 christos would have to consider making it by linking an object file @file{foo.c.o}, 9374 1.1 christos or by C compilation-and-linking in one step from @file{foo.c.c}, or by 9375 1.1 christos Pascal compilation-and-linking from @file{foo.c.p}, and many other 9376 1.1 christos possibilities. 9377 1.1 christos 9378 1.1 christos We know these possibilities are ridiculous since @file{foo.c} is a C source 9379 1.1 christos file, not an executable. If @code{make} did consider these possibilities, 9380 1.1 christos it would ultimately reject them, because files such as @file{foo.c.o} and 9381 1.1 christos @file{foo.c.p} would not exist. But these possibilities are so 9382 1.1 christos numerous that @code{make} would run very slowly if it had to consider 9383 1.1 christos them.@refill 9384 1.1 christos 9385 1.1 christos To gain speed, we have put various constraints on the way @code{make} 9386 1.1 christos considers match-anything rules. There are two different constraints that 9387 1.1 christos can be applied, and each time you define a match-anything rule you must 9388 1.1 christos choose one or the other for that rule. 9389 1.1 christos 9390 1.1 christos One choice is to mark the match-anything rule as @dfn{terminal} by defining 9391 1.1 christos it with a double colon. When a rule is terminal, it does not apply unless 9392 1.1 christos its prerequisites actually exist. Prerequisites that could be made with 9393 1.1 christos other implicit rules are not good enough. In other words, no further 9394 1.1 christos chaining is allowed beyond a terminal rule. 9395 1.1 christos 9396 1.1 christos For example, the built-in implicit rules for extracting sources from RCS 9397 1.1 christos and SCCS files are terminal; as a result, if the file @file{foo.c,v} does 9398 1.1 christos not exist, @code{make} will not even consider trying to make it as an 9399 1.1 christos intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}. 9400 1.1 christos RCS and SCCS files are generally ultimate source files, which should not be 9401 1.1 christos remade from any other files; therefore, @code{make} can save time by not 9402 1.1 christos looking for ways to remake them.@refill 9403 1.1 christos 9404 1.1 christos If you do not mark the match-anything rule as terminal, then it is 9405 1.1 christos nonterminal. A nonterminal match-anything rule cannot apply to a file name 9406 1.1 christos that indicates a specific type of data. A file name indicates a specific 9407 1.1 christos type of data if some non-match-anything implicit rule target matches it. 9408 1.1 christos 9409 1.1 christos For example, the file name @file{foo.c} matches the target for the pattern 9410 1.1 christos rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this 9411 1.1 christos rule is actually applicable (which happens only if there is a file 9412 1.1 christos @file{foo.y}), the fact that its target matches is enough to prevent 9413 1.1 christos consideration of any nonterminal match-anything rules for the file 9414 1.1 christos @file{foo.c}. Thus, @code{make} will not even consider trying to make 9415 1.1 christos @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c}, 9416 1.1 christos @file{foo.c.p}, etc.@refill 9417 1.1 christos 9418 1.1 christos The motivation for this constraint is that nonterminal match-anything 9419 1.1 christos rules are used for making files containing specific types of data (such as 9420 1.1 christos executable files) and a file name with a recognized suffix indicates some 9421 1.1 christos other specific type of data (such as a C source file). 9422 1.1 christos 9423 1.1 christos Special built-in dummy pattern rules are provided solely to recognize 9424 1.1 christos certain file names so that nonterminal match-anything rules will not be 9425 1.1 christos considered. These dummy rules have no prerequisites and no commands, and 9426 1.1 christos they are ignored for all other purposes. For example, the built-in 9427 1.1 christos implicit rule 9428 1.1 christos 9429 1.1 christos @example 9430 1.1 christos %.p : 9431 1.1 christos @end example 9432 1.1 christos 9433 1.1 christos @noindent 9434 1.1 christos exists to make sure that Pascal source files such as @file{foo.p} match a 9435 1.1 christos specific target pattern and thereby prevent time from being wasted looking 9436 1.1 christos for @file{foo.p.o} or @file{foo.p.c}. 9437 1.1 christos 9438 1.1 christos Dummy pattern rules such as the one for @samp{%.p} are made for every 9439 1.1 christos suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}). 9440 1.1 christos 9441 1.1 christos @node Canceling Rules, , Match-Anything Rules, Pattern Rules 9442 1.1 christos @subsection Canceling Implicit Rules 9443 1.1 christos 9444 1.1 christos You can override a built-in implicit rule (or one you have defined 9445 1.1 christos yourself) by defining a new pattern rule with the same target and 9446 1.1 christos prerequisites, but different commands. When the new rule is defined, the 9447 1.1 christos built-in one is replaced. The new rule's position in the sequence of 9448 1.1 christos implicit rules is determined by where you write the new rule. 9449 1.1 christos 9450 1.1 christos You can cancel a built-in implicit rule by defining a pattern rule with the 9451 1.1 christos same target and prerequisites, but no commands. For example, the following 9452 1.1 christos would cancel the rule that runs the assembler: 9453 1.1 christos 9454 1.1 christos @example 9455 1.1 christos %.o : %.s 9456 1.1 christos @end example 9457 1.1 christos 9458 1.1 christos @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules 9459 1.1 christos @section Defining Last-Resort Default Rules 9460 1.1 christos @cindex last-resort default rules 9461 1.1 christos @cindex default rules, last-resort 9462 1.1 christos 9463 1.1 christos You can define a last-resort implicit rule by writing a terminal 9464 1.1 christos match-anything pattern rule with no prerequisites (@pxref{Match-Anything 9465 1.1 christos Rules}). This is just like any other pattern rule; the only thing 9466 1.1 christos special about it is that it will match any target. So such a rule's 9467 1.1 christos commands are used for all targets and prerequisites that have no commands 9468 1.1 christos of their own and for which no other implicit rule applies. 9469 1.1 christos 9470 1.1 christos For example, when testing a makefile, you might not care if the source 9471 1.1 christos files contain real data, only that they exist. Then you might do this: 9472 1.1 christos 9473 1.1 christos @example 9474 1.1 christos %:: 9475 1.1 christos touch $@@ 9476 1.1 christos @end example 9477 1.1 christos 9478 1.1 christos @noindent 9479 1.1 christos to cause all the source files needed (as prerequisites) to be created 9480 1.1 christos automatically. 9481 1.1 christos 9482 1.1 christos @findex .DEFAULT 9483 1.1 christos You can instead define commands to be used for targets for which there 9484 1.1 christos are no rules at all, even ones which don't specify commands. You do 9485 1.1 christos this by writing a rule for the target @code{.DEFAULT}. Such a rule's 9486 1.1 christos commands are used for all prerequisites which do not appear as targets in 9487 1.1 christos any explicit rule, and for which no implicit rule applies. Naturally, 9488 1.1 christos there is no @code{.DEFAULT} rule unless you write one. 9489 1.1 christos 9490 1.1 christos If you use @code{.DEFAULT} with no commands or prerequisites: 9491 1.1 christos 9492 1.1 christos @example 9493 1.1 christos .DEFAULT: 9494 1.1 christos @end example 9495 1.1 christos 9496 1.1 christos @noindent 9497 1.1 christos the commands previously stored for @code{.DEFAULT} are cleared. 9498 1.1 christos Then @code{make} acts as if you had never defined @code{.DEFAULT} at all. 9499 1.1 christos 9500 1.1 christos If you do not want a target to get the commands from a match-anything 9501 1.1 christos pattern rule or @code{.DEFAULT}, but you also do not want any commands 9502 1.1 christos to be run for the target, you can give it empty commands (@pxref{Empty 9503 1.1 christos Commands, ,Defining Empty Commands}).@refill 9504 1.1 christos 9505 1.1 christos You can use a last-resort rule to override part of another makefile. 9506 1.1 christos @xref{Overriding Makefiles, , Overriding Part of Another Makefile}. 9507 1.1 christos 9508 1.1 christos @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules 9509 1.1 christos @section Old-Fashioned Suffix Rules 9510 1.1 christos @cindex old-fashioned suffix rules 9511 1.1 christos @cindex suffix rule 9512 1.1 christos 9513 1.1 christos @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for 9514 1.1 christos @code{make}. Suffix rules are obsolete because pattern rules are more 9515 1.1 christos general and clearer. They are supported in GNU @code{make} for 9516 1.1 christos compatibility with old makefiles. They come in two kinds: 9517 1.1 christos @dfn{double-suffix} and @dfn{single-suffix}.@refill 9518 1.1 christos 9519 1.1 christos A double-suffix rule is defined by a pair of suffixes: the target suffix 9520 1.1 christos and the source suffix. It matches any file whose name ends with the 9521 1.1 christos target suffix. The corresponding implicit prerequisite is made by 9522 1.1 christos replacing the target suffix with the source suffix in the file name. A 9523 1.1 christos two-suffix rule whose target and source suffixes are @samp{.o} and 9524 1.1 christos @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}. 9525 1.1 christos 9526 1.1 christos A single-suffix rule is defined by a single suffix, which is the source 9527 1.1 christos suffix. It matches any file name, and the corresponding implicit 9528 1.1 christos prerequisite name is made by appending the source suffix. A single-suffix 9529 1.1 christos rule whose source suffix is @samp{.c} is equivalent to the pattern rule 9530 1.1 christos @samp{% : %.c}. 9531 1.1 christos 9532 1.1 christos Suffix rule definitions are recognized by comparing each rule's target 9533 1.1 christos against a defined list of known suffixes. When @code{make} sees a rule 9534 1.1 christos whose target is a known suffix, this rule is considered a single-suffix 9535 1.1 christos rule. When @code{make} sees a rule whose target is two known suffixes 9536 1.1 christos concatenated, this rule is taken as a double-suffix rule. 9537 1.1 christos 9538 1.1 christos For example, @samp{.c} and @samp{.o} are both on the default list of 9539 1.1 christos known suffixes. Therefore, if you define a rule whose target is 9540 1.1 christos @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source 9541 1.1 christos suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned 9542 1.1 christos way to define the rule for compiling a C source file:@refill 9543 1.1 christos 9544 1.1 christos @example 9545 1.1 christos .c.o: 9546 1.1 christos $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $< 9547 1.1 christos @end example 9548 1.1 christos 9549 1.1 christos Suffix rules cannot have any prerequisites of their own. If they have any, 9550 1.1 christos they are treated as normal files with funny names, not as suffix rules. 9551 1.1 christos Thus, the rule: 9552 1.1 christos 9553 1.1 christos @example 9554 1.1 christos .c.o: foo.h 9555 1.1 christos $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $< 9556 1.1 christos @end example 9557 1.1 christos 9558 1.1 christos @noindent 9559 1.1 christos tells how to make the file @file{.c.o} from the prerequisite file 9560 1.1 christos @file{foo.h}, and is not at all like the pattern rule: 9561 1.1 christos 9562 1.1 christos @example 9563 1.1 christos %.o: %.c foo.h 9564 1.1 christos $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $< 9565 1.1 christos @end example 9566 1.1 christos 9567 1.1 christos @noindent 9568 1.1 christos which tells how to make @samp{.o} files from @samp{.c} files, and makes all 9569 1.1 christos @samp{.o} files using this pattern rule also depend on @file{foo.h}. 9570 1.1 christos 9571 1.1 christos Suffix rules with no commands are also meaningless. They do not remove 9572 1.1 christos previous rules as do pattern rules with no commands (@pxref{Canceling 9573 1.1 christos Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as 9574 1.1 christos a target in the data base.@refill 9575 1.1 christos 9576 1.1 christos @findex .SUFFIXES 9577 1.1 christos The known suffixes are simply the names of the prerequisites of the special 9578 1.1 christos target @code{.SUFFIXES}. You can add your own suffixes by writing a rule 9579 1.1 christos for @code{.SUFFIXES} that adds more prerequisites, as in: 9580 1.1 christos 9581 1.1 christos @example 9582 1.1 christos .SUFFIXES: .hack .win 9583 1.1 christos @end example 9584 1.1 christos 9585 1.1 christos @noindent 9586 1.1 christos which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes. 9587 1.1 christos 9588 1.1 christos If you wish to eliminate the default known suffixes instead of just adding 9589 1.1 christos to them, write a rule for @code{.SUFFIXES} with no prerequisites. By 9590 1.1 christos special dispensation, this eliminates all existing prerequisites of 9591 1.1 christos @code{.SUFFIXES}. You can then write another rule to add the suffixes you 9592 1.1 christos want. For example, 9593 1.1 christos 9594 1.1 christos @example 9595 1.1 christos @group 9596 1.1 christos .SUFFIXES: # @r{Delete the default suffixes} 9597 1.1 christos .SUFFIXES: .c .o .h # @r{Define our suffix list} 9598 1.1 christos @end group 9599 1.1 christos @end example 9600 1.1 christos 9601 1.1 christos The @samp{-r} or @samp{--no-builtin-rules} flag causes the default 9602 1.1 christos list of suffixes to be empty. 9603 1.1 christos 9604 1.1 christos @vindex SUFFIXES 9605 1.1 christos The variable @code{SUFFIXES} is defined to the default list of suffixes 9606 1.1 christos before @code{make} reads any makefiles. You can change the list of suffixes 9607 1.1 christos with a rule for the special target @code{.SUFFIXES}, but that does not alter 9608 1.1 christos this variable. 9609 1.1 christos 9610 1.1 christos @node Implicit Rule Search, , Suffix Rules, Implicit Rules 9611 1.1 christos @section Implicit Rule Search Algorithm 9612 1.1 christos @cindex implicit rule, search algorithm 9613 1.1 christos @cindex search algorithm, implicit rule 9614 1.1 christos 9615 1.1 christos Here is the procedure @code{make} uses for searching for an implicit rule 9616 1.1 christos for a target @var{t}. This procedure is followed for each double-colon 9617 1.1 christos rule with no commands, for each target of ordinary rules none of which have 9618 1.1 christos commands, and for each prerequisite that is not the target of any rule. It 9619 1.1 christos is also followed recursively for prerequisites that come from implicit 9620 1.1 christos rules, in the search for a chain of rules. 9621 1.1 christos 9622 1.1 christos Suffix rules are not mentioned in this algorithm because suffix rules are 9623 1.1 christos converted to equivalent pattern rules once the makefiles have been read in. 9624 1.1 christos 9625 1.1 christos For an archive member target of the form 9626 1.1 christos @samp{@var{archive}(@var{member})}, the following algorithm is run 9627 1.1 christos twice, first using the entire target name @var{t}, and second using 9628 1.1 christos @samp{(@var{member})} as the target @var{t} if the first run found no 9629 1.1 christos rule.@refill 9630 1.1 christos 9631 1.1 christos @enumerate 9632 1.1 christos @item 9633 1.1 christos Split @var{t} into a directory part, called @var{d}, and the rest, 9634 1.1 christos called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then 9635 1.1 christos @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill 9636 1.1 christos 9637 1.1 christos @item 9638 1.1 christos Make a list of all the pattern rules one of whose targets matches 9639 1.1 christos @var{t} or @var{n}. If the target pattern contains a slash, it is 9640 1.1 christos matched against @var{t}; otherwise, against @var{n}. 9641 1.1 christos 9642 1.1 christos @item 9643 1.1 christos If any rule in that list is @emph{not} a match-anything rule, then 9644 1.1 christos remove all nonterminal match-anything rules from the list. 9645 1.1 christos 9646 1.1 christos @item 9647 1.1 christos Remove from the list all rules with no commands. 9648 1.1 christos 9649 1.1 christos @item 9650 1.1 christos For each pattern rule in the list: 9651 1.1 christos 9652 1.1 christos @enumerate a 9653 1.1 christos @item 9654 1.1 christos Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n} 9655 1.1 christos matched by the @samp{%} in the target pattern.@refill 9656 1.1 christos 9657 1.1 christos @item 9658 1.1 christos Compute the prerequisite names by substituting @var{s} for @samp{%}; if 9659 1.1 christos the target pattern does not contain a slash, append @var{d} to 9660 1.1 christos the front of each prerequisite name.@refill 9661 1.1 christos 9662 1.1 christos @item 9663 1.1 christos Test whether all the prerequisites exist or ought to exist. (If a 9664 1.1 christos file name is mentioned in the makefile as a target or as an explicit 9665 1.1 christos prerequisite, then we say it ought to exist.) 9666 1.1 christos 9667 1.1 christos If all prerequisites exist or ought to exist, or there are no prerequisites, 9668 1.1 christos then this rule applies. 9669 1.1 christos @end enumerate 9670 1.1 christos 9671 1.1 christos @item 9672 1.1 christos If no pattern rule has been found so far, try harder. 9673 1.1 christos For each pattern rule in the list: 9674 1.1 christos 9675 1.1 christos @enumerate a 9676 1.1 christos @item 9677 1.1 christos If the rule is terminal, ignore it and go on to the next rule. 9678 1.1 christos 9679 1.1 christos @item 9680 1.1 christos Compute the prerequisite names as before. 9681 1.1 christos 9682 1.1 christos @item 9683 1.1 christos Test whether all the prerequisites exist or ought to exist. 9684 1.1 christos 9685 1.1 christos @item 9686 1.1 christos For each prerequisite that does not exist, follow this algorithm 9687 1.1 christos recursively to see if the prerequisite can be made by an implicit 9688 1.1 christos rule. 9689 1.1 christos 9690 1.1 christos @item 9691 1.1 christos If all prerequisites exist, ought to exist, or can be 9692 1.1 christos made by implicit rules, then this rule applies. 9693 1.1 christos @end enumerate 9694 1.1 christos 9695 1.1 christos @item 9696 1.1 christos If no implicit rule applies, the rule for @code{.DEFAULT}, if any, 9697 1.1 christos applies. In that case, give @var{t} the same commands that 9698 1.1 christos @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}. 9699 1.1 christos @end enumerate 9700 1.1 christos 9701 1.1 christos Once a rule that applies has been found, for each target pattern of the 9702 1.1 christos rule other than the one that matched @var{t} or @var{n}, the @samp{%} in 9703 1.1 christos the pattern is replaced with @var{s} and the resultant file name is stored 9704 1.1 christos until the commands to remake the target file @var{t} are executed. After 9705 1.1 christos these commands are executed, each of these stored file names are entered 9706 1.1 christos into the data base and marked as having been updated and having the same 9707 1.1 christos update status as the file @var{t}. 9708 1.1 christos 9709 1.1 christos When the commands of a pattern rule are executed for @var{t}, the automatic 9710 1.1 christos variables are set corresponding to the target and prerequisites. 9711 1.1 christos @xref{Automatic Variables}. 9712 1.1 christos 9713 1.1 christos @node Archives, Features, Implicit Rules, Top 9714 1.1 christos @chapter Using @code{make} to Update Archive Files 9715 1.1 christos @cindex archive 9716 1.1 christos 9717 1.1 christos @dfn{Archive files} are files containing named subfiles called 9718 1.1 christos @dfn{members}; they are maintained with the program @code{ar} and their 9719 1.1 christos main use is as subroutine libraries for linking. 9720 1.1 christos 9721 1.1 christos @menu 9722 1.1 christos * Archive Members:: Archive members as targets. 9723 1.1 christos * Archive Update:: The implicit rule for archive member targets. 9724 1.1 christos * Archive Pitfalls:: Dangers to watch out for when using archives. 9725 1.1 christos * Archive Suffix Rules:: You can write a special kind of suffix rule 9726 1.1 christos for updating archives. 9727 1.1 christos @end menu 9728 1.1 christos 9729 1.1 christos @node Archive Members, Archive Update, Archives, Archives 9730 1.1 christos @section Archive Members as Targets 9731 1.1 christos @cindex archive member targets 9732 1.1 christos 9733 1.1 christos An individual member of an archive file can be used as a target or 9734 1.1 christos prerequisite in @code{make}. You specify the member named @var{member} in 9735 1.1 christos archive file @var{archive} as follows: 9736 1.1 christos 9737 1.1 christos @example 9738 1.1 christos @var{archive}(@var{member}) 9739 1.1 christos @end example 9740 1.1 christos 9741 1.1 christos @noindent 9742 1.1 christos This construct is available only in targets and prerequisites, not in 9743 1.1 christos commands! Most programs that you might use in commands do not support this 9744 1.1 christos syntax and cannot act directly on archive members. Only @code{ar} and 9745 1.1 christos other programs specifically designed to operate on archives can do so. 9746 1.1 christos Therefore, valid commands to update an archive member target probably must 9747 1.1 christos use @code{ar}. For example, this rule says to create a member 9748 1.1 christos @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}: 9749 1.1 christos 9750 1.1 christos @example 9751 1.1 christos foolib(hack.o) : hack.o 9752 1.1 christos ar cr foolib hack.o 9753 1.1 christos @end example 9754 1.1 christos 9755 1.1 christos In fact, nearly all archive member targets are updated in just this way 9756 1.1 christos and there is an implicit rule to do it for you. @strong{Please note:} The 9757 1.1 christos @samp{c} flag to @code{ar} is required if the archive file does not 9758 1.1 christos already exist. 9759 1.1 christos 9760 1.1 christos To specify several members in the same archive, you can write all the 9761 1.1 christos member names together between the parentheses. For example: 9762 1.1 christos 9763 1.1 christos @example 9764 1.1 christos foolib(hack.o kludge.o) 9765 1.1 christos @end example 9766 1.1 christos 9767 1.1 christos @noindent 9768 1.1 christos is equivalent to: 9769 1.1 christos 9770 1.1 christos @example 9771 1.1 christos foolib(hack.o) foolib(kludge.o) 9772 1.1 christos @end example 9773 1.1 christos 9774 1.1 christos @cindex wildcard, in archive member 9775 1.1 christos You can also use shell-style wildcards in an archive member reference. 9776 1.1 christos @xref{Wildcards, ,Using Wildcard Characters in File Names}. For 9777 1.1 christos example, @w{@samp{foolib(*.o)}} expands to all existing members of the 9778 1.1 christos @file{foolib} archive whose names end in @samp{.o}; perhaps 9779 1.1 christos @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}. 9780 1.1 christos 9781 1.1 christos @node Archive Update, Archive Pitfalls, Archive Members, Archives 9782 1.1 christos @section Implicit Rule for Archive Member Targets 9783 1.1 christos 9784 1.1 christos Recall that a target that looks like @file{@var{a}(@var{m})} stands for the 9785 1.1 christos member named @var{m} in the archive file @var{a}. 9786 1.1 christos 9787 1.1 christos When @code{make} looks for an implicit rule for such a target, as a special 9788 1.1 christos feature it considers implicit rules that match @file{(@var{m})}, as well as 9789 1.1 christos those that match the actual target @file{@var{a}(@var{m})}. 9790 1.1 christos 9791 1.1 christos This causes one special rule whose target is @file{(%)} to match. This 9792 1.1 christos rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m} 9793 1.1 christos into the archive. For example, it will update the archive member target 9794 1.1 christos @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the 9795 1.1 christos archive @file{foo.a} as a @emph{member} named @file{bar.o}. 9796 1.1 christos 9797 1.1 christos When this rule is chained with others, the result is very powerful. 9798 1.1 christos Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the 9799 1.1 christos @samp{(} and @samp{)} from being interpreted specially by the shell) in 9800 1.1 christos the presence of a file @file{bar.c} is enough to cause the following 9801 1.1 christos commands to be run, even without a makefile: 9802 1.1 christos 9803 1.1 christos @example 9804 1.1 christos cc -c bar.c -o bar.o 9805 1.1 christos ar r foo.a bar.o 9806 1.1 christos rm -f bar.o 9807 1.1 christos @end example 9808 1.1 christos 9809 1.1 christos @noindent 9810 1.1 christos Here @code{make} has envisioned the file @file{bar.o} as an intermediate 9811 1.1 christos file. @xref{Chained Rules, ,Chains of Implicit Rules}. 9812 1.1 christos 9813 1.1 christos Implicit rules such as this one are written using the automatic variable 9814 1.1 christos @samp{$%}. @xref{Automatic Variables}. 9815 1.1 christos 9816 1.1 christos An archive member name in an archive cannot contain a directory name, but 9817 1.1 christos it may be useful in a makefile to pretend that it does. If you write an 9818 1.1 christos archive member target @file{foo.a(dir/file.o)}, @code{make} will perform 9819 1.1 christos automatic updating with this command: 9820 1.1 christos 9821 1.1 christos @example 9822 1.1 christos ar r foo.a dir/file.o 9823 1.1 christos @end example 9824 1.1 christos 9825 1.1 christos @noindent 9826 1.1 christos which has the effect of copying the file @file{dir/file.o} into a member 9827 1.1 christos named @file{file.o}. In connection with such usage, the automatic variables 9828 1.1 christos @code{%D} and @code{%F} may be useful. 9829 1.1 christos 9830 1.1 christos @menu 9831 1.1 christos * Archive Symbols:: How to update archive symbol directories. 9832 1.1 christos @end menu 9833 1.1 christos 9834 1.1 christos @node Archive Symbols, , Archive Update, Archive Update 9835 1.1 christos @subsection Updating Archive Symbol Directories 9836 1.1 christos @cindex @code{__.SYMDEF} 9837 1.1 christos @cindex updating archive symbol directories 9838 1.1 christos @cindex archive symbol directory updating 9839 1.1 christos @cindex symbol directories, updating archive 9840 1.1 christos @cindex directories, updating archive symbol 9841 1.1 christos 9842 1.1 christos An archive file that is used as a library usually contains a special member 9843 1.1 christos named @file{__.SYMDEF} that contains a directory of the external symbol 9844 1.1 christos names defined by all the other members. After you update any other 9845 1.1 christos members, you need to update @file{__.SYMDEF} so that it will summarize the 9846 1.1 christos other members properly. This is done by running the @code{ranlib} program: 9847 1.1 christos 9848 1.1 christos @example 9849 1.1 christos ranlib @var{archivefile} 9850 1.1 christos @end example 9851 1.1 christos 9852 1.1 christos Normally you would put this command in the rule for the archive file, 9853 1.1 christos and make all the members of the archive file prerequisites of that rule. 9854 1.1 christos For example, 9855 1.1 christos 9856 1.1 christos @example 9857 1.1 christos libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{} 9858 1.1 christos ranlib libfoo.a 9859 1.1 christos @end example 9860 1.1 christos 9861 1.1 christos @noindent 9862 1.1 christos The effect of this is to update archive members @file{x.o}, @file{y.o}, 9863 1.1 christos etc., and then update the symbol directory member @file{__.SYMDEF} by 9864 1.1 christos running @code{ranlib}. The rules for updating the members are not shown 9865 1.1 christos here; most likely you can omit them and use the implicit rule which copies 9866 1.1 christos files into the archive, as described in the preceding section. 9867 1.1 christos 9868 1.1 christos This is not necessary when using the GNU @code{ar} program, which 9869 1.1 christos updates the @file{__.SYMDEF} member automatically. 9870 1.1 christos 9871 1.1 christos @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives 9872 1.1 christos @section Dangers When Using Archives 9873 1.1 christos @cindex archive, and parallel execution 9874 1.1 christos @cindex parallel execution, and archive update 9875 1.1 christos @cindex archive, and @code{-j} 9876 1.1 christos @cindex @code{-j}, and archive update 9877 1.1 christos 9878 1.1 christos It is important to be careful when using parallel execution (the 9879 1.1 christos @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives. 9880 1.1 christos If multiple @code{ar} commands run at the same time on the same archive 9881 1.1 christos file, they will not know about each other and can corrupt the file. 9882 1.1 christos 9883 1.1 christos Possibly a future version of @code{make} will provide a mechanism to 9884 1.1 christos circumvent this problem by serializing all commands that operate on the 9885 1.1 christos same archive file. But for the time being, you must either write your 9886 1.1 christos makefiles to avoid this problem in some other way, or not use @code{-j}. 9887 1.1 christos 9888 1.1 christos @node Archive Suffix Rules, , Archive Pitfalls, Archives 9889 1.1 christos @section Suffix Rules for Archive Files 9890 1.1 christos @cindex suffix rule, for archive 9891 1.1 christos @cindex archive, suffix rule for 9892 1.1 christos @cindex library archive, suffix rule for 9893 1.1 christos @cindex @code{.a} (archives) 9894 1.1 christos 9895 1.1 christos You can write a special kind of suffix rule for dealing with archive 9896 1.1 christos files. @xref{Suffix Rules}, for a full explanation of suffix rules. 9897 1.1 christos Archive suffix rules are obsolete in GNU @code{make}, because pattern 9898 1.1 christos rules for archives are a more general mechanism (@pxref{Archive 9899 1.1 christos Update}). But they are retained for compatibility with other 9900 1.1 christos @code{make}s. 9901 1.1 christos 9902 1.1 christos To write a suffix rule for archives, you simply write a suffix rule 9903 1.1 christos using the target suffix @samp{.a} (the usual suffix for archive files). 9904 1.1 christos For example, here is the old-fashioned suffix rule to update a library 9905 1.1 christos archive from C source files: 9906 1.1 christos 9907 1.1 christos @example 9908 1.1 christos @group 9909 1.1 christos .c.a: 9910 1.1 christos $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o 9911 1.1 christos $(AR) r $@@ $*.o 9912 1.1 christos $(RM) $*.o 9913 1.1 christos @end group 9914 1.1 christos @end example 9915 1.1 christos 9916 1.1 christos @noindent 9917 1.1 christos This works just as if you had written the pattern rule: 9918 1.1 christos 9919 1.1 christos @example 9920 1.1 christos @group 9921 1.1 christos (%.o): %.c 9922 1.1 christos $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o 9923 1.1 christos $(AR) r $@@ $*.o 9924 1.1 christos $(RM) $*.o 9925 1.1 christos @end group 9926 1.1 christos @end example 9927 1.1 christos 9928 1.1 christos In fact, this is just what @code{make} does when it sees a suffix rule 9929 1.1 christos with @samp{.a} as the target suffix. Any double-suffix rule 9930 1.1 christos @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target 9931 1.1 christos pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}. 9932 1.1 christos 9933 1.1 christos Since you might want to use @samp{.a} as the suffix for some other kind 9934 1.1 christos of file, @code{make} also converts archive suffix rules to pattern rules 9935 1.1 christos in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule 9936 1.1 christos @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):} 9937 1.1 christos @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill 9938 1.1 christos 9939 1.1 christos @node Features, Missing, Archives, Top 9940 1.1 christos @chapter Features of GNU @code{make} 9941 1.1 christos @cindex features of GNU @code{make} 9942 1.1 christos @cindex portability 9943 1.1 christos @cindex compatibility 9944 1.1 christos 9945 1.1 christos Here is a summary of the features of GNU @code{make}, for comparison 9946 1.1 christos with and credit to other versions of @code{make}. We consider the 9947 1.1 christos features of @code{make} in 4.2 BSD systems as a baseline. If you are 9948 1.1 christos concerned with writing portable makefiles, you should not use the 9949 1.1 christos features of @code{make} listed here, nor the ones in @ref{Missing}. 9950 1.1 christos 9951 1.1 christos Many features come from the version of @code{make} in System V. 9952 1.1 christos 9953 1.1 christos @itemize @bullet 9954 1.1 christos @item 9955 1.1 christos The @code{VPATH} variable and its special meaning. 9956 1.1 christos @xref{Directory Search, , Searching Directories for Prerequisites}. 9957 1.1 christos This feature exists in System V @code{make}, but is undocumented. 9958 1.1 christos It is documented in 4.3 BSD @code{make} (which says it mimics System V's 9959 1.1 christos @code{VPATH} feature).@refill 9960 1.1 christos 9961 1.1 christos @item 9962 1.1 christos Included makefiles. @xref{Include, ,Including Other Makefiles}. 9963 1.1 christos Allowing multiple files to be included with a single directive is a GNU 9964 1.1 christos extension. 9965 1.1 christos 9966 1.1 christos @item 9967 1.1 christos Variables are read from and communicated via the environment. 9968 1.1 christos @xref{Environment, ,Variables from the Environment}. 9969 1.1 christos 9970 1.1 christos @item 9971 1.1 christos Options passed through the variable @code{MAKEFLAGS} to recursive 9972 1.1 christos invocations of @code{make}. 9973 1.1 christos @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}. 9974 1.1 christos 9975 1.1 christos @item 9976 1.1 christos The automatic variable @code{$%} is set to the member name 9977 1.1 christos in an archive reference. @xref{Automatic Variables}. 9978 1.1 christos 9979 1.1 christos @item 9980 1.1 christos The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%}, 9981 1.1 christos and @code{$?} have corresponding forms like @code{$(@@F)} and 9982 1.1 christos @code{$(@@D)}. We have generalized this to @code{$^} as an obvious 9983 1.1 christos extension. @xref{Automatic Variables}.@refill 9984 1.1 christos 9985 1.1 christos @item 9986 1.1 christos Substitution variable references. 9987 1.1 christos @xref{Reference, ,Basics of Variable References}. 9988 1.1 christos 9989 1.1 christos @item 9990 1.1 christos The command-line options @samp{-b} and @samp{-m}, accepted and 9991 1.1 christos ignored. In System V @code{make}, these options actually do something. 9992 1.1 christos 9993 1.1 christos @item 9994 1.1 christos Execution of recursive commands to run @code{make} via the variable 9995 1.1 christos @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified. 9996 1.1 christos @xref{Recursion, ,Recursive Use of @code{make}}. 9997 1.1 christos 9998 1.1 christos @item 9999 1.1 christos Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix 10000 1.1 christos Rules}. This feature is obsolete in GNU @code{make}, because the 10001 1.1 christos general feature of rule chaining (@pxref{Chained Rules, ,Chains of 10002 1.1 christos Implicit Rules}) allows one pattern rule for installing members in an 10003 1.1 christos archive (@pxref{Archive Update}) to be sufficient. 10004 1.1 christos 10005 1.1 christos @item 10006 1.1 christos The arrangement of lines and backslash-newline combinations in 10007 1.1 christos commands is retained when the commands are printed, so they appear as 10008 1.1 christos they do in the makefile, except for the stripping of initial 10009 1.1 christos whitespace. 10010 1.1 christos @end itemize 10011 1.1 christos 10012 1.1 christos The following features were inspired by various other versions of 10013 1.1 christos @code{make}. In some cases it is unclear exactly which versions inspired 10014 1.1 christos which others. 10015 1.1 christos 10016 1.1 christos @itemize @bullet 10017 1.1 christos @item 10018 1.1 christos Pattern rules using @samp{%}. 10019 1.1 christos This has been implemented in several versions of @code{make}. 10020 1.1 christos We're not sure who invented it first, but it's been spread around a bit. 10021 1.1 christos @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill 10022 1.1 christos 10023 1.1 christos @item 10024 1.1 christos Rule chaining and implicit intermediate files. 10025 1.1 christos This was implemented by Stu Feldman in his version of @code{make} 10026 1.1 christos for AT&T Eighth Edition Research Unix, and later by Andrew Hume of 10027 1.1 christos AT&T Bell Labs in his @code{mk} program (where he terms it 10028 1.1 christos ``transitive closure''). We do not really know if 10029 1.1 christos we got this from either of them or thought it up ourselves at the 10030 1.1 christos same time. @xref{Chained Rules, ,Chains of Implicit Rules}. 10031 1.1 christos 10032 1.1 christos @item 10033 1.1 christos The automatic variable @code{$^} containing a list of all prerequisites 10034 1.1 christos of the current target. We did not invent this, but we have no idea who 10035 1.1 christos did. @xref{Automatic Variables}. The automatic variable 10036 1.1 christos @code{$+} is a simple extension of @code{$^}. 10037 1.1 christos 10038 1.1 christos @item 10039 1.1 christos The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know) 10040 1.1 christos invented by Andrew Hume in @code{mk}. 10041 1.1 christos @xref{Instead of Execution, ,Instead of Executing the Commands}. 10042 1.1 christos 10043 1.1 christos @item 10044 1.1 christos The concept of doing several things at once (parallelism) exists in 10045 1.1 christos many incarnations of @code{make} and similar programs, though not in the 10046 1.1 christos System V or BSD implementations. @xref{Execution, ,Command Execution}. 10047 1.1 christos 10048 1.1 christos @item 10049 1.1 christos Modified variable references using pattern substitution come from 10050 1.1 christos SunOS 4. @xref{Reference, ,Basics of Variable References}. 10051 1.1 christos This functionality was provided in GNU @code{make} by the 10052 1.1 christos @code{patsubst} function before the alternate syntax was implemented 10053 1.1 christos for compatibility with SunOS 4. It is not altogether clear who 10054 1.1 christos inspired whom, since GNU @code{make} had @code{patsubst} before SunOS 10055 1.1 christos 4 was released.@refill 10056 1.1 christos 10057 1.1 christos @item 10058 1.1 christos The special significance of @samp{+} characters preceding command lines 10059 1.1 christos (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is 10060 1.1 christos mandated by 10061 1.1 christos @cite{IEEE Standard 1003.2-1992} (POSIX.2). 10062 1.1 christos 10063 1.1 christos @item 10064 1.1 christos The @samp{+=} syntax to append to the value of a variable comes from SunOS 10065 1.1 christos 4 @code{make}. @xref{Appending, , Appending More Text to Variables}. 10066 1.1 christos 10067 1.1 christos @item 10068 1.1 christos The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list 10069 1.1 christos multiple members in a single archive file comes from SunOS 4 @code{make}. 10070 1.1 christos @xref{Archive Members}. 10071 1.1 christos 10072 1.1 christos @item 10073 1.1 christos The @code{-include} directive to include makefiles with no error for a 10074 1.1 christos nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4 10075 1.1 christos @code{make} does not allow multiple makefiles to be specified in one 10076 1.1 christos @code{-include} directive.) The same feature appears with the name 10077 1.1 christos @code{sinclude} in SGI @code{make} and perhaps others. 10078 1.1 christos @end itemize 10079 1.1 christos 10080 1.1 christos The remaining features are inventions new in GNU @code{make}: 10081 1.1 christos 10082 1.1 christos @itemize @bullet 10083 1.1 christos @item 10084 1.1 christos Use the @samp{-v} or @samp{--version} option to print version and 10085 1.1 christos copyright information. 10086 1.1 christos 10087 1.1 christos @item 10088 1.1 christos Use the @samp{-h} or @samp{--help} option to summarize the options to 10089 1.1 christos @code{make}. 10090 1.1 christos 10091 1.1 christos @item 10092 1.1 christos Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}. 10093 1.1 christos 10094 1.1 christos @item 10095 1.1 christos Pass command-line variable assignments automatically through the 10096 1.1 christos variable @code{MAKE} to recursive @code{make} invocations. 10097 1.1 christos @xref{Recursion, ,Recursive Use of @code{make}}. 10098 1.1 christos 10099 1.1 christos @item 10100 1.1 christos Use the @samp{-C} or @samp{--directory} command option to change 10101 1.1 christos directory. @xref{Options Summary, ,Summary of Options}. 10102 1.1 christos 10103 1.1 christos @item 10104 1.1 christos Make verbatim variable definitions with @code{define}. 10105 1.1 christos @xref{Defining, ,Defining Variables Verbatim}. 10106 1.1 christos 10107 1.1 christos @item 10108 1.1 christos Declare phony targets with the special target @code{.PHONY}. 10109 1.1 christos 10110 1.1 christos Andrew Hume of AT&T Bell Labs implemented a similar feature with a 10111 1.1 christos different syntax in his @code{mk} program. This seems to be a case of 10112 1.1 christos parallel discovery. @xref{Phony Targets, ,Phony Targets}. 10113 1.1 christos 10114 1.1 christos @item 10115 1.1 christos Manipulate text by calling functions. 10116 1.1 christos @xref{Functions, ,Functions for Transforming Text}. 10117 1.1 christos 10118 1.1 christos @item 10119 1.1 christos Use the @samp{-o} or @samp{--old-file} 10120 1.1 christos option to pretend a file's modification-time is old. 10121 1.1 christos @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}. 10122 1.1 christos 10123 1.1 christos @item 10124 1.1 christos Conditional execution. 10125 1.1 christos 10126 1.1 christos This feature has been implemented numerous times in various versions 10127 1.1 christos of @code{make}; it seems a natural extension derived from the features 10128 1.1 christos of the C preprocessor and similar macro languages and is not a 10129 1.1 christos revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}. 10130 1.1 christos 10131 1.1 christos @item 10132 1.1 christos Specify a search path for included makefiles. 10133 1.1 christos @xref{Include, ,Including Other Makefiles}. 10134 1.1 christos 10135 1.1 christos @item 10136 1.1 christos Specify extra makefiles to read with an environment variable. 10137 1.1 christos @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}. 10138 1.1 christos 10139 1.1 christos @item 10140 1.1 christos Strip leading sequences of @samp{./} from file names, so that 10141 1.1 christos @file{./@var{file}} and @file{@var{file}} are considered to be the 10142 1.1 christos same file.@refill 10143 1.1 christos 10144 1.1 christos @item 10145 1.1 christos Use a special search method for library prerequisites written in the 10146 1.1 christos form @samp{-l@var{name}}. 10147 1.1 christos @xref{Libraries/Search, ,Directory Search for Link Libraries}. 10148 1.1 christos 10149 1.1 christos @item 10150 1.1 christos Allow suffixes for suffix rules 10151 1.1 christos (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any 10152 1.1 christos characters. In other versions of @code{make}, they must begin with 10153 1.1 christos @samp{.} and not contain any @samp{/} characters. 10154 1.1 christos 10155 1.1 christos @item 10156 1.1 christos Keep track of the current level of @code{make} recursion using the 10157 1.1 christos variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}. 10158 1.1 christos 10159 1.1 christos @item 10160 1.1 christos Provide any goals given on the command line in the variable 10161 1.1 christos @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}. 10162 1.1 christos 10163 1.1 christos @item 10164 1.1 christos Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}. 10165 1.1 christos 10166 1.1 christos @item 10167 1.1 christos Provide selective @code{vpath} search. 10168 1.1 christos @xref{Directory Search, ,Searching Directories for Prerequisites}. 10169 1.1 christos 10170 1.1 christos @item 10171 1.1 christos Provide computed variable references. 10172 1.1 christos @xref{Reference, ,Basics of Variable References}. 10173 1.1 christos 10174 1.1 christos @item 10175 1.1 christos Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}. 10176 1.1 christos System V @code{make} has a very, very limited form of this 10177 1.1 christos functionality in that it will check out SCCS files for makefiles. 10178 1.1 christos 10179 1.1 christos @item 10180 1.1 christos Various new built-in implicit rules. 10181 1.1 christos @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}. 10182 1.1 christos 10183 1.1 christos @item 10184 1.1 christos The built-in variable @samp{MAKE_VERSION} gives the version number of 10185 1.1 christos @code{make}. 10186 1.1 christos @vindex MAKE_VERSION 10187 1.1 christos @end itemize 10188 1.1 christos 10189 1.1 christos @node Missing, Makefile Conventions, Features, Top 10190 1.1 christos @chapter Incompatibilities and Missing Features 10191 1.1 christos @cindex incompatibilities 10192 1.1 christos @cindex missing features 10193 1.1 christos @cindex features, missing 10194 1.1 christos 10195 1.1 christos The @code{make} programs in various other systems support a few features 10196 1.1 christos that are not implemented in GNU @code{make}. The POSIX.2 standard 10197 1.1 christos (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not 10198 1.1 christos require any of these features.@refill 10199 1.1 christos 10200 1.1 christos @itemize @bullet 10201 1.1 christos @item 10202 1.1 christos A target of the form @samp{@var{file}((@var{entry}))} stands for a member 10203 1.1 christos of archive file @var{file}. The member is chosen, not by name, but by 10204 1.1 christos being an object file which defines the linker symbol @var{entry}.@refill 10205 1.1 christos 10206 1.1 christos This feature was not put into GNU @code{make} because of the 10207 1.1 christos nonmodularity of putting knowledge into @code{make} of the internal 10208 1.1 christos format of archive file symbol tables. 10209 1.1 christos @xref{Archive Symbols, ,Updating Archive Symbol Directories}. 10210 1.1 christos 10211 1.1 christos @item 10212 1.1 christos Suffixes (used in suffix rules) that end with the character @samp{~} 10213 1.1 christos have a special meaning to System V @code{make}; 10214 1.1 christos they refer to the SCCS file that corresponds 10215 1.1 christos to the file one would get without the @samp{~}. For example, the 10216 1.1 christos suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from 10217 1.1 christos the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole 10218 1.1 christos series of such suffix rules is required. 10219 1.1 christos @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}. 10220 1.1 christos 10221 1.1 christos In GNU @code{make}, this entire series of cases is handled by two 10222 1.1 christos pattern rules for extraction from SCCS, in combination with the 10223 1.1 christos general feature of rule chaining. 10224 1.1 christos @xref{Chained Rules, ,Chains of Implicit Rules}. 10225 1.1 christos 10226 1.1 christos @item 10227 1.1 christos In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search 10228 1.1 christos (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command 10229 1.1 christos strings. We feel it is much cleaner to always use automatic variables 10230 1.1 christos and thus make this feature obsolete.@refill 10231 1.1 christos 10232 1.1 christos @item 10233 1.1 christos In some Unix @code{make}s, the automatic variable @code{$*} appearing in 10234 1.1 christos the prerequisites of a rule has the amazingly strange ``feature'' of 10235 1.1 christos expanding to the full name of the @emph{target of that rule}. We cannot 10236 1.1 christos imagine what went on in the minds of Unix @code{make} developers to do 10237 1.1 christos this; it is utterly inconsistent with the normal definition of @code{$*}. 10238 1.1 christos @vindex * @r{(automatic variable), unsupported bizarre usage} 10239 1.1 christos 10240 1.1 christos @item 10241 1.1 christos In some Unix @code{make}s, implicit rule search 10242 1.1 christos (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for 10243 1.1 christos @emph{all} targets, not just those without commands. This means you can 10244 1.1 christos do:@refill 10245 1.1 christos 10246 1.1 christos @example 10247 1.1 christos @group 10248 1.1 christos foo.o: 10249 1.1 christos cc -c foo.c 10250 1.1 christos @end group 10251 1.1 christos @end example 10252 1.1 christos 10253 1.1 christos @noindent 10254 1.1 christos and Unix @code{make} will intuit that @file{foo.o} depends on 10255 1.1 christos @file{foo.c}.@refill 10256 1.1 christos 10257 1.1 christos We feel that such usage is broken. The prerequisite properties of 10258 1.1 christos @code{make} are well-defined (for GNU @code{make}, at least), 10259 1.1 christos and doing such a thing simply does not fit the model.@refill 10260 1.1 christos 10261 1.1 christos @item 10262 1.1 christos GNU @code{make} does not include any built-in implicit rules for 10263 1.1 christos compiling or preprocessing EFL programs. If we hear of anyone who is 10264 1.1 christos using EFL, we will gladly add them. 10265 1.1 christos 10266 1.1 christos @item 10267 1.1 christos It appears that in SVR4 @code{make}, a suffix rule can be specified with 10268 1.1 christos no commands, and it is treated as if it had empty commands 10269 1.1 christos (@pxref{Empty Commands}). For example: 10270 1.1 christos 10271 1.1 christos @example 10272 1.1 christos .c.a: 10273 1.1 christos @end example 10274 1.1 christos 10275 1.1 christos @noindent 10276 1.1 christos will override the built-in @file{.c.a} suffix rule. 10277 1.1 christos 10278 1.1 christos We feel that it is cleaner for a rule without commands to always simply 10279 1.1 christos add to the prerequisite list for the target. The above example can be 10280 1.1 christos easily rewritten to get the desired behavior in GNU @code{make}: 10281 1.1 christos 10282 1.1 christos @example 10283 1.1 christos .c.a: ; 10284 1.1 christos @end example 10285 1.1 christos 10286 1.1 christos @item 10287 1.1 christos Some versions of @code{make} invoke the shell with the @samp{-e} flag, 10288 1.1 christos except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a 10289 1.1 christos Program}). The @samp{-e} flag tells the shell to exit as soon as any 10290 1.1 christos program it runs returns a nonzero status. We feel it is cleaner to 10291 1.1 christos write each shell command line to stand on its own and not require this 10292 1.1 christos special treatment. 10293 1.1 christos @end itemize 10294 1.1 christos 10295 1.1 christos @comment The makefile standards are in a separate file that is also 10296 1.1 christos @comment included by standards.texi. 10297 1.1 christos @include make-stds.texi 10298 1.1 christos 10299 1.1 christos @node Quick Reference, Error Messages, Makefile Conventions, Top 10300 1.1 christos @appendix Quick Reference 10301 1.1 christos 10302 1.1 christos This appendix summarizes the directives, text manipulation functions, 10303 1.1 christos and special variables which GNU @code{make} understands. 10304 1.1 christos @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules}, 10305 1.1 christos and @ref{Options Summary, ,Summary of Options}, 10306 1.1 christos for other summaries. 10307 1.1 christos 10308 1.1 christos Here is a summary of the directives GNU @code{make} recognizes: 10309 1.1 christos 10310 1.1 christos @table @code 10311 1.1 christos @item define @var{variable} 10312 1.1 christos @itemx endef 10313 1.1 christos 10314 1.1 christos Define a multi-line, recursively-expanded variable.@* 10315 1.1 christos @xref{Sequences}. 10316 1.1 christos 10317 1.1 christos @item ifdef @var{variable} 10318 1.1 christos @itemx ifndef @var{variable} 10319 1.1 christos @itemx ifeq (@var{a},@var{b}) 10320 1.1 christos @itemx ifeq "@var{a}" "@var{b}" 10321 1.1 christos @itemx ifeq '@var{a}' '@var{b}' 10322 1.1 christos @itemx ifneq (@var{a},@var{b}) 10323 1.1 christos @itemx ifneq "@var{a}" "@var{b}" 10324 1.1 christos @itemx ifneq '@var{a}' '@var{b}' 10325 1.1 christos @itemx else 10326 1.1 christos @itemx endif 10327 1.1 christos 10328 1.1 christos Conditionally evaluate part of the makefile.@* 10329 1.1 christos @xref{Conditionals}. 10330 1.1 christos 10331 1.1 christos @item include @var{file} 10332 1.1 christos @itemx -include @var{file} 10333 1.1 christos @itemx sinclude @var{file} 10334 1.1 christos 10335 1.1 christos Include another makefile.@* 10336 1.1 christos @xref{Include, ,Including Other Makefiles}. 10337 1.1 christos 10338 1.1 christos @item override @var{variable} = @var{value} 10339 1.1 christos @itemx override @var{variable} := @var{value} 10340 1.1 christos @itemx override @var{variable} += @var{value} 10341 1.1 christos @itemx override @var{variable} ?= @var{value} 10342 1.1 christos @itemx override define @var{variable} 10343 1.1 christos @itemx endef 10344 1.1 christos 10345 1.1 christos Define a variable, overriding any previous definition, even one from 10346 1.1 christos the command line.@* 10347 1.1 christos @xref{Override Directive, ,The @code{override} Directive}. 10348 1.1 christos 10349 1.1 christos @item export 10350 1.1 christos 10351 1.1 christos Tell @code{make} to export all variables to child processes by default.@* 10352 1.1 christos @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}. 10353 1.1 christos 10354 1.1 christos @item export @var{variable} 10355 1.1 christos @itemx export @var{variable} = @var{value} 10356 1.1 christos @itemx export @var{variable} := @var{value} 10357 1.1 christos @itemx export @var{variable} += @var{value} 10358 1.1 christos @itemx export @var{variable} ?= @var{value} 10359 1.1 christos @itemx unexport @var{variable} 10360 1.1 christos Tell @code{make} whether or not to export a particular variable to child 10361 1.1 christos processes.@* 10362 1.1 christos @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}. 10363 1.1 christos 10364 1.1 christos @item vpath @var{pattern} @var{path} 10365 1.1 christos Specify a search path for files matching a @samp{%} pattern.@* 10366 1.1 christos @xref{Selective Search, , The @code{vpath} Directive}. 10367 1.1 christos 10368 1.1 christos @item vpath @var{pattern} 10369 1.1 christos Remove all search paths previously specified for @var{pattern}. 10370 1.1 christos 10371 1.1 christos @item vpath 10372 1.1 christos Remove all search paths previously specified in any @code{vpath} 10373 1.1 christos directive. 10374 1.1 christos @end table 10375 1.1 christos 10376 1.1 christos Here is a summary of the built-in functions (@pxref{Functions}): 10377 1.1 christos 10378 1.1 christos @table @code 10379 1.1 christos @item $(subst @var{from},@var{to},@var{text}) 10380 1.1 christos Replace @var{from} with @var{to} in @var{text}.@* 10381 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10382 1.1 christos 10383 1.1 christos @item $(patsubst @var{pattern},@var{replacement},@var{text}) 10384 1.1 christos Replace words matching @var{pattern} with @var{replacement} in @var{text}.@* 10385 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10386 1.1 christos 10387 1.1 christos @item $(strip @var{string}) 10388 1.1 christos Remove excess whitespace characters from @var{string}.@* 10389 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10390 1.1 christos 10391 1.1 christos @item $(findstring @var{find},@var{text}) 10392 1.1 christos Locate @var{find} in @var{text}.@* 10393 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10394 1.1 christos 10395 1.1 christos @item $(filter @var{pattern}@dots{},@var{text}) 10396 1.1 christos Select words in @var{text} that match one of the @var{pattern} words.@* 10397 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10398 1.1 christos 10399 1.1 christos @item $(filter-out @var{pattern}@dots{},@var{text}) 10400 1.1 christos Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@* 10401 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10402 1.1 christos 10403 1.1 christos @item $(sort @var{list}) 10404 1.1 christos Sort the words in @var{list} lexicographically, removing duplicates.@* 10405 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10406 1.1 christos 10407 1.1 christos @item $(word @var{n},@var{text}) 10408 1.1 christos Extract the @var{n}th word (one-origin) of @var{text}.@* 10409 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10410 1.1 christos 10411 1.1 christos @item $(words @var{text}) 10412 1.1 christos Count the number of words in @var{text}.@* 10413 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10414 1.1 christos 10415 1.1 christos @item $(wordlist @var{s},@var{e},@var{text}) 10416 1.1 christos Returns the list of words in @var{text} from @var{s} to @var{e}.@* 10417 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10418 1.1 christos 10419 1.1 christos @item $(firstword @var{names}@dots{}) 10420 1.1 christos Extract the first word of @var{names}.@* 10421 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10422 1.1 christos 10423 1.1 christos @item $(lastword @var{names}@dots{}) 10424 1.1 christos Extract the last word of @var{names}.@* 10425 1.1 christos @xref{Text Functions, , Functions for String Substitution and Analysis}. 10426 1.1 christos 10427 1.1 christos @item $(dir @var{names}@dots{}) 10428 1.1 christos Extract the directory part of each file name.@* 10429 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10430 1.1 christos 10431 1.1 christos @item $(notdir @var{names}@dots{}) 10432 1.1 christos Extract the non-directory part of each file name.@* 10433 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10434 1.1 christos 10435 1.1 christos @item $(suffix @var{names}@dots{}) 10436 1.1 christos Extract the suffix (the last @samp{.} and following characters) of each file name.@* 10437 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10438 1.1 christos 10439 1.1 christos @item $(basename @var{names}@dots{}) 10440 1.1 christos Extract the base name (name without suffix) of each file name.@* 10441 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10442 1.1 christos 10443 1.1 christos @item $(addsuffix @var{suffix},@var{names}@dots{}) 10444 1.1 christos Append @var{suffix} to each word in @var{names}.@* 10445 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10446 1.1 christos 10447 1.1 christos @item $(addprefix @var{prefix},@var{names}@dots{}) 10448 1.1 christos Prepend @var{prefix} to each word in @var{names}.@* 10449 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10450 1.1 christos 10451 1.1 christos @item $(join @var{list1},@var{list2}) 10452 1.1 christos Join two parallel lists of words.@* 10453 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10454 1.1 christos 10455 1.1 christos @item $(wildcard @var{pattern}@dots{}) 10456 1.1 christos Find file names matching a shell file name pattern (@emph{not} a 10457 1.1 christos @samp{%} pattern).@* 10458 1.1 christos @xref{Wildcard Function, ,The Function @code{wildcard}}. 10459 1.1 christos 10460 1.1 christos @item $(realpath @var{names}@dots{}) 10461 1.1 christos For each file name in @var{names}, expand to an absolute name that 10462 1.1 christos does not contain any @code{.}, @code{..}, nor symlinks.@* 10463 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10464 1.1 christos 10465 1.1 christos @item $(abspath @var{names}@dots{}) 10466 1.1 christos For each file name in @var{names}, expand to an absolute name that 10467 1.1 christos does not contain any @code{.} or @code{..} components, but preserves 10468 1.1 christos symlinks.@* 10469 1.1 christos @xref{File Name Functions, ,Functions for File Names}. 10470 1.1 christos 10471 1.1 christos @item $(error @var{text}@dots{}) 10472 1.1 christos 10473 1.1 christos When this function is evaluated, @code{make} generates a fatal error 10474 1.1 christos with the message @var{text}.@* 10475 1.1 christos @xref{Make Control Functions, ,Functions That Control Make}. 10476 1.1 christos 10477 1.1 christos @item $(warning @var{text}@dots{}) 10478 1.1 christos 10479 1.1 christos When this function is evaluated, @code{make} generates a warning with 10480 1.1 christos the message @var{text}.@* 10481 1.1 christos @xref{Make Control Functions, ,Functions That Control Make}. 10482 1.1 christos 10483 1.1 christos @item $(shell @var{command}) 10484 1.1 christos 10485 1.1 christos Execute a shell command and return its output.@* 10486 1.1 christos @xref{Shell Function, , The @code{shell} Function}. 10487 1.1 christos 10488 1.1 christos @item $(origin @var{variable}) 10489 1.1 christos 10490 1.1 christos Return a string describing how the @code{make} variable @var{variable} was 10491 1.1 christos defined.@* 10492 1.1 christos @xref{Origin Function, , The @code{origin} Function}. 10493 1.1 christos 10494 1.1 christos @item $(flavor @var{variable}) 10495 1.1 christos 10496 1.1 christos Return a string describing the flavor of the @code{make} variable 10497 1.1 christos @var{variable}.@* 10498 1.1 christos @xref{Flavor Function, , The @code{flavor} Function}. 10499 1.1 christos 10500 1.1 christos @item $(foreach @var{var},@var{words},@var{text}) 10501 1.1 christos 10502 1.1 christos Evaluate @var{text} with @var{var} bound to each word in @var{words}, 10503 1.1 christos and concatenate the results.@* 10504 1.1 christos @xref{Foreach Function, ,The @code{foreach} Function}. 10505 1.1 christos 10506 1.1 christos @item $(call @var{var},@var{param},@dots{}) 10507 1.1 christos 10508 1.1 christos Evaluate the variable @var{var} replacing any references to @code{$(1)}, 10509 1.1 christos @code{$(2)} with the first, second, etc.@: @var{param} values.@* 10510 1.1 christos @xref{Call Function, ,The @code{call} Function}. 10511 1.1 christos 10512 1.1 christos @item $(eval @var{text}) 10513 1.1 christos 10514 1.1 christos Evaluate @var{text} then read the results as makefile commands. 10515 1.1 christos Expands to the empty string.@* 10516 1.1 christos @xref{Eval Function, ,The @code{eval} Function}. 10517 1.1 christos 10518 1.1 christos @item $(value @var{var}) 10519 1.1 christos 10520 1.1 christos Evaluates to the contents of the variable @var{var}, with no expansion 10521 1.1 christos performed on it.@* 10522 1.1 christos @xref{Value Function, ,The @code{value} Function}. 10523 1.1 christos @end table 10524 1.1 christos 10525 1.1 christos Here is a summary of the automatic variables. 10526 1.1 christos @xref{Automatic Variables}, 10527 1.1 christos for full information. 10528 1.1 christos 10529 1.1 christos @table @code 10530 1.1 christos @item $@@ 10531 1.1 christos The file name of the target. 10532 1.1 christos 10533 1.1 christos @item $% 10534 1.1 christos The target member name, when the target is an archive member. 10535 1.1 christos 10536 1.1 christos @item $< 10537 1.1 christos The name of the first prerequisite. 10538 1.1 christos 10539 1.1 christos @item $? 10540 1.1 christos The names of all the prerequisites that are 10541 1.1 christos newer than the target, with spaces between them. 10542 1.1 christos For prerequisites which are archive members, only 10543 1.1 christos the member named is used (@pxref{Archives}). 10544 1.1 christos 10545 1.1 christos @item $^ 10546 1.1 christos @itemx $+ 10547 1.1 christos The names of all the prerequisites, with spaces between them. For 10548 1.1 christos prerequisites which are archive members, only the member named is used 10549 1.1 christos (@pxref{Archives}). The value of @code{$^} omits duplicate 10550 1.1 christos prerequisites, while @code{$+} retains them and preserves their order. 10551 1.1 christos 10552 1.1 christos @item $* 10553 1.1 christos The stem with which an implicit rule matches 10554 1.1 christos (@pxref{Pattern Match, ,How Patterns Match}). 10555 1.1 christos 10556 1.1 christos @item $(@@D) 10557 1.1 christos @itemx $(@@F) 10558 1.1 christos The directory part and the file-within-directory part of @code{$@@}. 10559 1.1 christos 10560 1.1 christos @item $(*D) 10561 1.1 christos @itemx $(*F) 10562 1.1 christos The directory part and the file-within-directory part of @code{$*}. 10563 1.1 christos 10564 1.1 christos @item $(%D) 10565 1.1 christos @itemx $(%F) 10566 1.1 christos The directory part and the file-within-directory part of @code{$%}. 10567 1.1 christos 10568 1.1 christos @item $(<D) 10569 1.1 christos @itemx $(<F) 10570 1.1 christos The directory part and the file-within-directory part of @code{$<}. 10571 1.1 christos 10572 1.1 christos @item $(^D) 10573 1.1 christos @itemx $(^F) 10574 1.1 christos The directory part and the file-within-directory part of @code{$^}. 10575 1.1 christos 10576 1.1 christos @item $(+D) 10577 1.1 christos @itemx $(+F) 10578 1.1 christos The directory part and the file-within-directory part of @code{$+}. 10579 1.1 christos 10580 1.1 christos @item $(?D) 10581 1.1 christos @itemx $(?F) 10582 1.1 christos The directory part and the file-within-directory part of @code{$?}. 10583 1.1 christos @end table 10584 1.1 christos 10585 1.1 christos These variables are used specially by GNU @code{make}: 10586 1.1 christos 10587 1.1 christos @table @code 10588 1.1 christos @item MAKEFILES 10589 1.1 christos 10590 1.1 christos Makefiles to be read on every invocation of @code{make}.@* 10591 1.1 christos @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}. 10592 1.1 christos 10593 1.1 christos @item VPATH 10594 1.1 christos 10595 1.1 christos Directory search path for files not found in the current directory.@* 10596 1.1 christos @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}. 10597 1.1 christos 10598 1.1 christos @item SHELL 10599 1.1 christos 10600 1.1 christos The name of the system default command interpreter, usually @file{/bin/sh}. 10601 1.1 christos You can set @code{SHELL} in the makefile to change the shell used to run 10602 1.1 christos commands. @xref{Execution, ,Command Execution}. The @code{SHELL} 10603 1.1 christos variable is handled specially when importing from and exporting to the 10604 1.1 christos environment. @xref{Choosing the Shell}. 10605 1.1 christos 10606 1.1 christos @item MAKESHELL 10607 1.1 christos 10608 1.1 christos On MS-DOS only, the name of the command interpreter that is to be used 10609 1.1 christos by @code{make}. This value takes precedence over the value of 10610 1.1 christos @code{SHELL}. @xref{Execution, ,MAKESHELL variable}. 10611 1.1 christos 10612 1.1 christos @item MAKE 10613 1.1 christos 10614 1.1 christos The name with which @code{make} was invoked. 10615 1.1 christos Using this variable in commands has special meaning. 10616 1.1 christos @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}. 10617 1.1 christos 10618 1.1 christos @item MAKELEVEL 10619 1.1 christos 10620 1.1 christos The number of levels of recursion (sub-@code{make}s).@* 10621 1.1 christos @xref{Variables/Recursion}. 10622 1.1 christos 10623 1.1 christos @item MAKEFLAGS 10624 1.1 christos 10625 1.1 christos The flags given to @code{make}. You can set this in the environment or 10626 1.1 christos a makefile to set flags.@* 10627 1.1 christos @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}. 10628 1.1 christos 10629 1.1 christos It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a 10630 1.1 christos command line: its contents may not be quoted correctly for use in the 10631 1.1 christos shell. Always allow recursive @code{make}'s to obtain these values 10632 1.1 christos through the environment from its parent. 10633 1.1 christos 10634 1.1 christos @item MAKECMDGOALS 10635 1.1 christos 10636 1.1 christos The targets given to @code{make} on the command line. Setting this 10637 1.1 christos variable has no effect on the operation of @code{make}.@* 10638 1.1 christos @xref{Goals, ,Arguments to Specify the Goals}. 10639 1.1 christos 10640 1.1 christos @item CURDIR 10641 1.1 christos 10642 1.1 christos Set to the pathname of the current working directory (after all 10643 1.1 christos @code{-C} options are processed, if any). Setting this variable has no 10644 1.1 christos effect on the operation of @code{make}.@* 10645 1.1 christos @xref{Recursion, ,Recursive Use of @code{make}}. 10646 1.1 christos 10647 1.1 christos @item SUFFIXES 10648 1.1 christos 10649 1.1 christos The default list of suffixes before @code{make} reads any makefiles. 10650 1.1 christos 10651 1.1 christos @item .LIBPATTERNS 10652 1.1 christos Defines the naming of the libraries @code{make} searches for, and their 10653 1.1 christos order.@* 10654 1.1 christos @xref{Libraries/Search, ,Directory Search for Link Libraries}. 10655 1.1 christos @end table 10656 1.1 christos 10657 1.1 christos @node Error Messages, Complex Makefile, Quick Reference, Top 10658 1.1 christos @comment node-name, next, previous, up 10659 1.1 christos @appendix Errors Generated by Make 10660 1.1 christos 10661 1.1 christos Here is a list of the more common errors you might see generated by 10662 1.1 christos @code{make}, and some information about what they mean and how to fix 10663 1.1 christos them. 10664 1.1 christos 10665 1.1 christos Sometimes @code{make} errors are not fatal, especially in the presence 10666 1.1 christos of a @code{-} prefix on a command script line, or the @code{-k} command 10667 1.1 christos line option. Errors that are fatal are prefixed with the string 10668 1.1 christos @code{***}. 10669 1.1 christos 10670 1.1 christos Error messages are all either prefixed with the name of the program 10671 1.1 christos (usually @samp{make}), or, if the error is found in a makefile, the name 10672 1.1 christos of the file and linenumber containing the problem. 10673 1.1 christos 10674 1.1 christos In the table below, these common prefixes are left off. 10675 1.1 christos 10676 1.1 christos @table @samp 10677 1.1 christos 10678 1.1 christos @item [@var{foo}] Error @var{NN} 10679 1.1 christos @itemx [@var{foo}] @var{signal description} 10680 1.1 christos These errors are not really @code{make} errors at all. They mean that a 10681 1.1 christos program that @code{make} invoked as part of a command script returned a 10682 1.1 christos non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets 10683 1.1 christos as failure, or it exited in some other abnormal fashion (with a 10684 1.1 christos signal of some type). @xref{Errors, ,Errors in Commands}. 10685 1.1 christos 10686 1.1 christos If no @code{***} is attached to the message, then the subprocess failed 10687 1.1 christos but the rule in the makefile was prefixed with the @code{-} special 10688 1.1 christos character, so @code{make} ignored the error. 10689 1.1 christos 10690 1.1 christos @item missing separator. Stop. 10691 1.1 christos @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop. 10692 1.1 christos This means that @code{make} could not understand much of anything about 10693 1.1 christos the command line it just read. GNU @code{make} looks for various kinds 10694 1.1 christos of separators (@code{:}, @code{=}, TAB characters, etc.) to help it 10695 1.1 christos decide what kind of commandline it's seeing. This means it couldn't 10696 1.1 christos find a valid one. 10697 1.1 christos 10698 1.1 christos One of the most common reasons for this message is that you (or perhaps 10699 1.1 christos your oh-so-helpful editor, as is the case with many MS-Windows editors) 10700 1.1 christos have attempted to indent your command scripts with spaces instead of a 10701 1.1 christos TAB character. In this case, @code{make} will use the second form of 10702 1.1 christos the error above. Remember that every line in the command script must 10703 1.1 christos begin with a TAB character. Eight spaces do not count. @xref{Rule 10704 1.1 christos Syntax}. 10705 1.1 christos 10706 1.1 christos @item commands commence before first target. Stop. 10707 1.1 christos @itemx missing rule before commands. Stop. 10708 1.1 christos This means the first thing in the makefile seems to be part of a command 10709 1.1 christos script: it begins with a TAB character and doesn't appear to be a legal 10710 1.1 christos @code{make} command (such as a variable assignment). Command scripts 10711 1.1 christos must always be associated with a target. 10712 1.1 christos 10713 1.1 christos The second form is generated if the line has a semicolon as the first 10714 1.1 christos non-whitespace character; @code{make} interprets this to mean you left 10715 1.1 christos out the "target: prerequisite" section of a rule. @xref{Rule Syntax}. 10716 1.1 christos 10717 1.1 christos @item No rule to make target `@var{xxx}'. 10718 1.1 christos @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'. 10719 1.1 christos This means that @code{make} decided it needed to build a target, but 10720 1.1 christos then couldn't find any instructions in the makefile on how to do that, 10721 1.1 christos either explicit or implicit (including in the default rules database). 10722 1.1 christos 10723 1.1 christos If you want that file to be built, you will need to add a rule to your 10724 1.1 christos makefile describing how that target can be built. Other possible 10725 1.1 christos sources of this problem are typos in the makefile (if that filename is 10726 1.1 christos wrong) or a corrupted source tree (if that file is not supposed to be 10727 1.1 christos built, but rather only a prerequisite). 10728 1.1 christos 10729 1.1 christos @item No targets specified and no makefile found. Stop. 10730 1.1 christos @itemx No targets. Stop. 10731 1.1 christos The former means that you didn't provide any targets to be built on the 10732 1.1 christos command line, and @code{make} couldn't find any makefiles to read in. 10733 1.1 christos The latter means that some makefile was found, but it didn't contain any 10734 1.1 christos default goal and none was given on the command line. GNU @code{make} 10735 1.1 christos has nothing to do in these situations. 10736 1.1 christos @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill 10737 1.1 christos 10738 1.1 christos @item Makefile `@var{xxx}' was not found. 10739 1.1 christos @itemx Included makefile `@var{xxx}' was not found. 10740 1.1 christos A makefile specified on the command line (first form) or included 10741 1.1 christos (second form) was not found. 10742 1.1 christos 10743 1.1 christos @item warning: overriding commands for target `@var{xxx}' 10744 1.1 christos @itemx warning: ignoring old commands for target `@var{xxx}' 10745 1.1 christos GNU @code{make} allows commands to be specified only once per target 10746 1.1 christos (except for double-colon rules). If you give commands for a target 10747 1.1 christos which already has been defined to have commands, this warning is issued 10748 1.1 christos and the second set of commands will overwrite the first set. 10749 1.1 christos @xref{Multiple Rules, ,Multiple Rules for One Target}. 10750 1.1 christos 10751 1.1 christos @item Circular @var{xxx} <- @var{yyy} dependency dropped. 10752 1.1 christos This means that @code{make} detected a loop in the dependency graph: 10753 1.1 christos after tracing the prerequisite @var{yyy} of target @var{xxx}, and its 10754 1.1 christos prerequisites, etc., one of them depended on @var{xxx} again. 10755 1.1 christos 10756 1.1 christos @item Recursive variable `@var{xxx}' references itself (eventually). Stop. 10757 1.1 christos This means you've defined a normal (recursive) @code{make} variable 10758 1.1 christos @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}). 10759 1.1 christos This is not allowed; either use simply-expanded variables (@code{:=}) or 10760 1.1 christos use the append operator (@code{+=}). @xref{Using Variables, ,How to Use 10761 1.1 christos Variables}. 10762 1.1 christos 10763 1.1 christos @item Unterminated variable reference. Stop. 10764 1.1 christos This means you forgot to provide the proper closing parenthesis 10765 1.1 christos or brace in your variable or function reference. 10766 1.1 christos 10767 1.1 christos @item insufficient arguments to function `@var{xxx}'. Stop. 10768 1.1 christos This means you haven't provided the requisite number of arguments for 10769 1.1 christos this function. See the documentation of the function for a description 10770 1.1 christos of its arguments. @xref{Functions, ,Functions for Transforming Text}. 10771 1.1 christos 10772 1.1 christos @item missing target pattern. Stop. 10773 1.1 christos @itemx multiple target patterns. Stop. 10774 1.1 christos @itemx target pattern contains no `%'. Stop. 10775 1.1 christos @itemx mixed implicit and static pattern rules. Stop. 10776 1.1 christos These are generated for malformed static pattern rules. The first means 10777 1.1 christos there's no pattern in the target section of the rule; the second means 10778 1.1 christos there are multiple patterns in the target section; the third means 10779 1.1 christos the target doesn't contain a pattern character (@code{%}); and the 10780 1.1 christos fourth means that all three parts of the static pattern rule contain 10781 1.1 christos pattern characters (@code{%})--only the first two parts should. 10782 1.1 christos @xref{Static Usage, ,Syntax of Static Pattern Rules}. 10783 1.1 christos 10784 1.1 christos @item warning: -jN forced in submake: disabling jobserver mode. 10785 1.1 christos This warning and the next are generated if @code{make} detects error 10786 1.1 christos conditions related to parallel processing on systems where 10787 1.1 christos sub-@code{make}s can communicate (@pxref{Options/Recursion, 10788 1.1 christos ,Communicating Options to a Sub-@code{make}}). This warning is 10789 1.1 christos generated if a recursive invocation of a @code{make} process is forced 10790 1.1 christos to have @samp{-j@var{N}} in its argument list (where @var{N} is greater 10791 1.1 christos than one). This could happen, for example, if you set the @code{MAKE} 10792 1.1 christos environment variable to @samp{make -j2}. In this case, the 10793 1.1 christos sub-@code{make} doesn't communicate with other @code{make} processes and 10794 1.1 christos will simply pretend it has two jobs of its own. 10795 1.1 christos 10796 1.1 christos @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule. 10797 1.1 christos In order for @code{make} processes to communicate, the parent will pass 10798 1.1 christos information to the child. Since this could result in problems if the 10799 1.1 christos child process isn't actually a @code{make}, the parent will only do this 10800 1.1 christos if it thinks the child is a @code{make}. The parent uses the normal 10801 1.1 christos algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE} 10802 1.1 christos Variable Works}). If the makefile is constructed such that the parent 10803 1.1 christos doesn't know the child is a @code{make} process, then the child will 10804 1.1 christos receive only part of the information necessary. In this case, the child 10805 1.1 christos will generate this warning message and proceed with its build in a 10806 1.1 christos sequential manner. 10807 1.1 christos 10808 1.1 christos @end table 10809 1.1 christos 10810 1.1 christos @node Complex Makefile, GNU Free Documentation License, Error Messages, Top 10811 1.1 christos @appendix Complex Makefile Example 10812 1.1 christos 10813 1.1 christos Here is the makefile for the GNU @code{tar} program. This is a 10814 1.1 christos moderately complex makefile. 10815 1.1 christos 10816 1.1 christos Because it is the first target, the default goal is @samp{all}. An 10817 1.1 christos interesting feature of this makefile is that @file{testpad.h} is a 10818 1.1 christos source file automatically created by the @code{testpad} program, 10819 1.1 christos itself compiled from @file{testpad.c}. 10820 1.1 christos 10821 1.1 christos If you type @samp{make} or @samp{make all}, then @code{make} creates 10822 1.1 christos the @file{tar} executable, the @file{rmt} daemon that provides 10823 1.1 christos remote tape access, and the @file{tar.info} Info file. 10824 1.1 christos 10825 1.1 christos If you type @samp{make install}, then @code{make} not only creates 10826 1.1 christos @file{tar}, @file{rmt}, and @file{tar.info}, but also installs 10827 1.1 christos them. 10828 1.1 christos 10829 1.1 christos If you type @samp{make clean}, then @code{make} removes the @samp{.o} 10830 1.1 christos files, and the @file{tar}, @file{rmt}, @file{testpad}, 10831 1.1 christos @file{testpad.h}, and @file{core} files. 10832 1.1 christos 10833 1.1 christos If you type @samp{make distclean}, then @code{make} not only removes 10834 1.1 christos the same files as does @samp{make clean} but also the 10835 1.1 christos @file{TAGS}, @file{Makefile}, and @file{config.status} files. 10836 1.1 christos (Although it is not evident, this makefile (and 10837 1.1 christos @file{config.status}) is generated by the user with the 10838 1.1 christos @code{configure} program, which is provided in the @code{tar} 10839 1.1 christos distribution, but is not shown here.) 10840 1.1 christos 10841 1.1 christos If you type @samp{make realclean}, then @code{make} removes the same 10842 1.1 christos files as does @samp{make distclean} and also removes the Info files 10843 1.1 christos generated from @file{tar.texinfo}. 10844 1.1 christos 10845 1.1 christos In addition, there are targets @code{shar} and @code{dist} that create 10846 1.1 christos distribution kits. 10847 1.1 christos 10848 1.1 christos @example 10849 1.1 christos @group 10850 1.1 christos # Generated automatically from Makefile.in by configure. 10851 1.1 christos # Un*x Makefile for GNU tar program. 10852 1.1 christos # Copyright (C) 1991 Free Software Foundation, Inc. 10853 1.1 christos @end group 10854 1.1 christos 10855 1.1 christos @group 10856 1.1 christos # This program is free software; you can redistribute 10857 1.1 christos # it and/or modify it under the terms of the GNU 10858 1.1 christos # General Public License @dots{} 10859 1.1 christos @dots{} 10860 1.1 christos @dots{} 10861 1.1 christos @end group 10862 1.1 christos 10863 1.1 christos SHELL = /bin/sh 10864 1.1 christos 10865 1.1 christos #### Start of system configuration section. #### 10866 1.1 christos 10867 1.1 christos srcdir = . 10868 1.1 christos 10869 1.1 christos @group 10870 1.1 christos # If you use gcc, you should either run the 10871 1.1 christos # fixincludes script that comes with it or else use 10872 1.1 christos # gcc with the -traditional option. Otherwise ioctl 10873 1.1 christos # calls will be compiled incorrectly on some systems. 10874 1.1 christos CC = gcc -O 10875 1.1 christos YACC = bison -y 10876 1.1 christos INSTALL = /usr/local/bin/install -c 10877 1.1 christos INSTALLDATA = /usr/local/bin/install -c -m 644 10878 1.1 christos @end group 10879 1.1 christos 10880 1.1 christos # Things you might add to DEFS: 10881 1.1 christos # -DSTDC_HEADERS If you have ANSI C headers and 10882 1.1 christos # libraries. 10883 1.1 christos # -DPOSIX If you have POSIX.1 headers and 10884 1.1 christos # libraries. 10885 1.1 christos # -DBSD42 If you have sys/dir.h (unless 10886 1.1 christos # you use -DPOSIX), sys/file.h, 10887 1.1 christos # and st_blocks in `struct stat'. 10888 1.1 christos # -DUSG If you have System V/ANSI C 10889 1.1 christos # string and memory functions 10890 1.1 christos # and headers, sys/sysmacros.h, 10891 1.1 christos # fcntl.h, getcwd, no valloc, 10892 1.1 christos # and ndir.h (unless 10893 1.1 christos # you use -DDIRENT). 10894 1.1 christos # -DNO_MEMORY_H If USG or STDC_HEADERS but do not 10895 1.1 christos # include memory.h. 10896 1.1 christos # -DDIRENT If USG and you have dirent.h 10897 1.1 christos # instead of ndir.h. 10898 1.1 christos # -DSIGTYPE=int If your signal handlers 10899 1.1 christos # return int, not void. 10900 1.1 christos # -DNO_MTIO If you lack sys/mtio.h 10901 1.1 christos # (magtape ioctls). 10902 1.1 christos # -DNO_REMOTE If you do not have a remote shell 10903 1.1 christos # or rexec. 10904 1.1 christos # -DUSE_REXEC To use rexec for remote tape 10905 1.1 christos # operations instead of 10906 1.1 christos # forking rsh or remsh. 10907 1.1 christos # -DVPRINTF_MISSING If you lack vprintf function 10908 1.1 christos # (but have _doprnt). 10909 1.1 christos # -DDOPRNT_MISSING If you lack _doprnt function. 10910 1.1 christos # Also need to define 10911 1.1 christos # -DVPRINTF_MISSING. 10912 1.1 christos # -DFTIME_MISSING If you lack ftime system call. 10913 1.1 christos # -DSTRSTR_MISSING If you lack strstr function. 10914 1.1 christos # -DVALLOC_MISSING If you lack valloc function. 10915 1.1 christos # -DMKDIR_MISSING If you lack mkdir and 10916 1.1 christos # rmdir system calls. 10917 1.1 christos # -DRENAME_MISSING If you lack rename system call. 10918 1.1 christos # -DFTRUNCATE_MISSING If you lack ftruncate 10919 1.1 christos # system call. 10920 1.1 christos # -DV7 On Version 7 Unix (not 10921 1.1 christos # tested in a long time). 10922 1.1 christos # -DEMUL_OPEN3 If you lack a 3-argument version 10923 1.1 christos # of open, and want to emulate it 10924 1.1 christos # with system calls you do have. 10925 1.1 christos # -DNO_OPEN3 If you lack the 3-argument open 10926 1.1 christos # and want to disable the tar -k 10927 1.1 christos # option instead of emulating open. 10928 1.1 christos # -DXENIX If you have sys/inode.h 10929 1.1 christos # and need it 94 to be included. 10930 1.1 christos 10931 1.1 christos DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \ 10932 1.1 christos -DVPRINTF_MISSING -DBSD42 10933 1.1 christos # Set this to rtapelib.o unless you defined NO_REMOTE, 10934 1.1 christos # in which case make it empty. 10935 1.1 christos RTAPELIB = rtapelib.o 10936 1.1 christos LIBS = 10937 1.1 christos DEF_AR_FILE = /dev/rmt8 10938 1.1 christos DEFBLOCKING = 20 10939 1.1 christos 10940 1.1 christos @group 10941 1.1 christos CDEBUG = -g 10942 1.1 christos CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \ 10943 1.1 christos -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \ 10944 1.1 christos -DDEFBLOCKING=$(DEFBLOCKING) 10945 1.1 christos LDFLAGS = -g 10946 1.1 christos @end group 10947 1.1 christos 10948 1.1 christos @group 10949 1.1 christos prefix = /usr/local 10950 1.1 christos # Prefix for each installed program, 10951 1.1 christos # normally empty or `g'. 10952 1.1 christos binprefix = 10953 1.1 christos 10954 1.1 christos # The directory to install tar in. 10955 1.1 christos bindir = $(prefix)/bin 10956 1.1 christos 10957 1.1 christos # The directory to install the info files in. 10958 1.1 christos infodir = $(prefix)/info 10959 1.1 christos @end group 10960 1.1 christos 10961 1.1 christos #### End of system configuration section. #### 10962 1.1 christos 10963 1.1 christos SRC1 = tar.c create.c extract.c buffer.c \ 10964 1.1 christos getoldopt.c update.c gnu.c mangle.c 10965 1.1 christos SRC2 = version.c list.c names.c diffarch.c \ 10966 1.1 christos port.c wildmat.c getopt.c 10967 1.1 christos SRC3 = getopt1.c regex.c getdate.y 10968 1.1 christos SRCS = $(SRC1) $(SRC2) $(SRC3) 10969 1.1 christos OBJ1 = tar.o create.o extract.o buffer.o \ 10970 1.1 christos getoldopt.o update.o gnu.o mangle.o 10971 1.1 christos OBJ2 = version.o list.o names.o diffarch.o \ 10972 1.1 christos port.o wildmat.o getopt.o 10973 1.1 christos OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB) 10974 1.1 christos OBJS = $(OBJ1) $(OBJ2) $(OBJ3) 10975 1.1 christos @group 10976 1.1 christos AUX = README COPYING ChangeLog Makefile.in \ 10977 1.1 christos makefile.pc configure configure.in \ 10978 1.1 christos tar.texinfo tar.info* texinfo.tex \ 10979 1.1 christos tar.h port.h open3.h getopt.h regex.h \ 10980 1.1 christos rmt.h rmt.c rtapelib.c alloca.c \ 10981 1.1 christos msd_dir.h msd_dir.c tcexparg.c \ 10982 1.1 christos level-0 level-1 backup-specs testpad.c 10983 1.1 christos @end group 10984 1.1 christos 10985 1.1 christos .PHONY: all 10986 1.1 christos all: tar rmt tar.info 10987 1.1 christos 10988 1.1 christos @group 10989 1.1 christos .PHONY: tar 10990 1.1 christos tar: $(OBJS) 10991 1.1 christos $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS) 10992 1.1 christos @end group 10993 1.1 christos 10994 1.1 christos @group 10995 1.1 christos rmt: rmt.c 10996 1.1 christos $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c 10997 1.1 christos @end group 10998 1.1 christos 10999 1.1 christos @group 11000 1.1 christos tar.info: tar.texinfo 11001 1.1 christos makeinfo tar.texinfo 11002 1.1 christos @end group 11003 1.1 christos 11004 1.1 christos @group 11005 1.1 christos .PHONY: install 11006 1.1 christos install: all 11007 1.1 christos $(INSTALL) tar $(bindir)/$(binprefix)tar 11008 1.1 christos -test ! -f rmt || $(INSTALL) rmt /etc/rmt 11009 1.1 christos $(INSTALLDATA) $(srcdir)/tar.info* $(infodir) 11010 1.1 christos @end group 11011 1.1 christos 11012 1.1 christos @group 11013 1.1 christos $(OBJS): tar.h port.h testpad.h 11014 1.1 christos regex.o buffer.o tar.o: regex.h 11015 1.1 christos # getdate.y has 8 shift/reduce conflicts. 11016 1.1 christos @end group 11017 1.1 christos 11018 1.1 christos @group 11019 1.1 christos testpad.h: testpad 11020 1.1 christos ./testpad 11021 1.1 christos @end group 11022 1.1 christos 11023 1.1 christos @group 11024 1.1 christos testpad: testpad.o 11025 1.1 christos $(CC) -o $@@ testpad.o 11026 1.1 christos @end group 11027 1.1 christos 11028 1.1 christos @group 11029 1.1 christos TAGS: $(SRCS) 11030 1.1 christos etags $(SRCS) 11031 1.1 christos @end group 11032 1.1 christos 11033 1.1 christos @group 11034 1.1 christos .PHONY: clean 11035 1.1 christos clean: 11036 1.1 christos rm -f *.o tar rmt testpad testpad.h core 11037 1.1 christos @end group 11038 1.1 christos 11039 1.1 christos @group 11040 1.1 christos .PHONY: distclean 11041 1.1 christos distclean: clean 11042 1.1 christos rm -f TAGS Makefile config.status 11043 1.1 christos @end group 11044 1.1 christos 11045 1.1 christos @group 11046 1.1 christos .PHONY: realclean 11047 1.1 christos realclean: distclean 11048 1.1 christos rm -f tar.info* 11049 1.1 christos @end group 11050 1.1 christos 11051 1.1 christos @group 11052 1.1 christos .PHONY: shar 11053 1.1 christos shar: $(SRCS) $(AUX) 11054 1.1 christos shar $(SRCS) $(AUX) | compress \ 11055 1.1 christos > tar-`sed -e '/version_string/!d' \ 11056 1.1 christos -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \ 11057 1.1 christos -e q 11058 1.1 christos version.c`.shar.Z 11059 1.1 christos @end group 11060 1.1 christos 11061 1.1 christos @group 11062 1.1 christos .PHONY: dist 11063 1.1 christos dist: $(SRCS) $(AUX) 11064 1.1 christos echo tar-`sed \ 11065 1.1 christos -e '/version_string/!d' \ 11066 1.1 christos -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \ 11067 1.1 christos -e q 11068 1.1 christos version.c` > .fname 11069 1.1 christos -rm -rf `cat .fname` 11070 1.1 christos mkdir `cat .fname` 11071 1.1 christos ln $(SRCS) $(AUX) `cat .fname` 11072 1.1 christos tar chZf `cat .fname`.tar.Z `cat .fname` 11073 1.1 christos -rm -rf `cat .fname` .fname 11074 1.1 christos @end group 11075 1.1 christos 11076 1.1 christos @group 11077 1.1 christos tar.zoo: $(SRCS) $(AUX) 11078 1.1 christos -rm -rf tmp.dir 11079 1.1 christos -mkdir tmp.dir 11080 1.1 christos -rm tar.zoo 11081 1.1 christos for X in $(SRCS) $(AUX) ; do \ 11082 1.1 christos echo $$X ; \ 11083 1.1 christos sed 's/$$/^M/' $$X \ 11084 1.1 christos > tmp.dir/$$X ; done 11085 1.1 christos cd tmp.dir ; zoo aM ../tar.zoo * 11086 1.1 christos -rm -rf tmp.dir 11087 1.1 christos @end group 11088 1.1 christos @end example 11089 1.1 christos 11090 1.1 christos @raisesections 11091 1.1 christos @include fdl.texi 11092 1.1 christos @lowersections 11093 1.1 christos 11094 1.1 christos @node Concept Index, Name Index, GNU Free Documentation License, Top 11095 1.1 christos @unnumbered Index of Concepts 11096 1.1 christos 11097 1.1 christos @printindex cp 11098 1.1 christos 11099 1.1 christos @node Name Index, , Concept Index, Top 11100 1.1 christos @unnumbered Index of Functions, Variables, & Directives 11101 1.1 christos 11102 1.1 christos @printindex fn 11103 1.1 christos 11104 1.1 christos @bye 11105