| 1 | \input texinfo @c -*- Texinfo -*-
|
|---|
| 2 | @c %**start of header
|
|---|
| 3 | @setfilename make.info
|
|---|
| 4 | @settitle GNU @code{make}
|
|---|
| 5 | @setchapternewpage odd
|
|---|
| 6 | @c %**end of header
|
|---|
| 7 |
|
|---|
| 8 | @c FSF publishers: format makebook.texi instead of using this file directly.
|
|---|
| 9 |
|
|---|
| 10 | @set RCSID $Id: make.texi,v 1.30 2005/05/13 12:45:31 psmith Exp $
|
|---|
| 11 | @set EDITION 0.70
|
|---|
| 12 | @set VERSION 3.81
|
|---|
| 13 | @set UPDATED 07 May 2005
|
|---|
| 14 | @set UPDATE-MONTH May 2005
|
|---|
| 15 | @c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
|
|---|
| 16 | @set ISBN 1-882114-83-5
|
|---|
| 17 |
|
|---|
| 18 | @c finalout
|
|---|
| 19 |
|
|---|
| 20 | @c ISPELL CHECK: done, 10 June 1993 --roland
|
|---|
| 21 | @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
|
|---|
| 22 |
|
|---|
| 23 | @c Combine the variable and function indices:
|
|---|
| 24 | @syncodeindex vr fn
|
|---|
| 25 | @c Combine the program and concept indices:
|
|---|
| 26 | @syncodeindex pg cp
|
|---|
| 27 |
|
|---|
| 28 | @dircategory GNU Packages
|
|---|
| 29 | @direntry
|
|---|
| 30 | * Make: (make). Remake files automatically.
|
|---|
| 31 | @end direntry
|
|---|
| 32 |
|
|---|
| 33 | @ifnottex
|
|---|
| 34 | This file documents the GNU Make utility, which determines
|
|---|
| 35 | automatically which pieces of a large program need to be recompiled,
|
|---|
| 36 | and issues the commands to recompile them.
|
|---|
| 37 |
|
|---|
| 38 | This is Edition @value{EDITION}, last updated @value{UPDATED},
|
|---|
| 39 | of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
|
|---|
| 40 |
|
|---|
| 41 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
|---|
| 42 | 1998, 1999, 2000, 2002, 2003, 2004, 2005
|
|---|
| 43 | Free Software Foundation, Inc.
|
|---|
| 44 |
|
|---|
| 45 | Permission is granted to copy, distribute and/or modify this document
|
|---|
| 46 | under the terms of the GNU Free Documentation License, Version 1.1 or
|
|---|
| 47 | any later version published by the Free Software Foundation; with no
|
|---|
| 48 | Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
|
|---|
| 49 | Texts. A copy of the license is included in the section entitled
|
|---|
| 50 | ``GNU Free Documentation License''.
|
|---|
| 51 | @end ifnottex
|
|---|
| 52 |
|
|---|
| 53 | @iftex
|
|---|
| 54 | @shorttitlepage GNU Make
|
|---|
| 55 | @end iftex
|
|---|
| 56 | @titlepage
|
|---|
| 57 | @title GNU Make
|
|---|
| 58 | @subtitle A Program for Directing Recompilation
|
|---|
| 59 | @subtitle GNU @code{make} Version @value{VERSION}
|
|---|
| 60 | @subtitle @value{UPDATE-MONTH}
|
|---|
| 61 | @author Richard M. Stallman, Roland McGrath, Paul D. Smith
|
|---|
| 62 | @page
|
|---|
| 63 | @vskip 0pt plus 1filll
|
|---|
| 64 | Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
|
|---|
| 65 | 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
|
|---|
| 66 | @sp 2
|
|---|
| 67 | Published by the Free Software Foundation @*
|
|---|
| 68 | 59 Temple Place -- Suite 330, @*
|
|---|
| 69 | Boston, MA 02111-1307 USA @*
|
|---|
| 70 | ISBN @value{ISBN} @*
|
|---|
| 71 |
|
|---|
| 72 | Permission is granted to copy, distribute and/or modify this document
|
|---|
| 73 | under the terms of the GNU Free Documentation License, Version 1.1 or
|
|---|
| 74 | any later version published by the Free Software Foundation; with the
|
|---|
| 75 | Invariant Sections being ``GNU General Public License'', the Front-Cover
|
|---|
| 76 | Texts being ``A GNU Manual'', and with the Back-Cover Texts being as in
|
|---|
| 77 | (a) below. A copy of the license is included in the section entitled
|
|---|
| 78 | ``GNU Free Documentation License''.
|
|---|
| 79 |
|
|---|
| 80 | (a) The FSF's Back-Cover Text is:
|
|---|
| 81 |
|
|---|
| 82 | @quotation
|
|---|
| 83 | You have freedom to copy and modify this GNU Manual, like GNU
|
|---|
| 84 | software. Copies published by the Free Software Foundation raise
|
|---|
| 85 | funds for GNU development.
|
|---|
| 86 | @end quotation
|
|---|
| 87 | @sp 2
|
|---|
| 88 | Cover art by Etienne Suvasa.
|
|---|
| 89 | @end titlepage
|
|---|
| 90 | @page
|
|---|
| 91 |
|
|---|
| 92 | @ifnottex
|
|---|
| 93 | @node Top, Overview, (dir), (dir)
|
|---|
| 94 | @top Make
|
|---|
| 95 |
|
|---|
| 96 | The GNU @code{make} utility automatically determines which pieces of a
|
|---|
| 97 | large program need to be recompiled, and issues the commands to
|
|---|
| 98 | recompile them.@refill
|
|---|
| 99 |
|
|---|
| 100 | This edition of the @cite{GNU Make Manual},
|
|---|
| 101 | last updated @value{UPDATED},
|
|---|
| 102 | documents GNU @code{make} Version @value{VERSION}.@refill
|
|---|
| 103 |
|
|---|
| 104 | This manual describes @code{make} and contains the following chapters:@refill
|
|---|
| 105 | @end ifnottex
|
|---|
| 106 |
|
|---|
| 107 | @menu
|
|---|
| 108 | * Overview:: Overview of @code{make}.
|
|---|
| 109 | * Introduction:: An introduction to @code{make}.
|
|---|
| 110 | * Makefiles:: Makefiles tell @code{make} what to do.
|
|---|
| 111 | * Rules:: Rules describe when a file must be remade.
|
|---|
| 112 | * Commands:: Commands say how to remake a file.
|
|---|
| 113 | * Using Variables:: You can use variables to avoid repetition.
|
|---|
| 114 | * Conditionals:: Use or ignore parts of the makefile based
|
|---|
| 115 | on the values of variables.
|
|---|
| 116 | * Functions:: Many powerful ways to manipulate text.
|
|---|
| 117 | * Invoking make: Running. How to invoke @code{make} on the command line.
|
|---|
| 118 | * Implicit Rules:: Use implicit rules to treat many files alike,
|
|---|
| 119 | based on their file names.
|
|---|
| 120 | * Archives:: How @code{make} can update library archives.
|
|---|
| 121 | * Features:: Features GNU @code{make} has over other @code{make}s.
|
|---|
| 122 | * Missing:: What GNU @code{make} lacks from other @code{make}s.
|
|---|
| 123 | * Makefile Conventions:: Conventions for writing makefiles for
|
|---|
| 124 | GNU programs.
|
|---|
| 125 | * Quick Reference:: A quick reference for experienced users.
|
|---|
| 126 | * Error Messages:: A list of common errors generated by @code{make}.
|
|---|
| 127 | * Complex Makefile:: A real example of a straightforward,
|
|---|
| 128 | but nontrivial, makefile.
|
|---|
| 129 |
|
|---|
| 130 | * GNU Free Documentation License:: License for copying this manual
|
|---|
| 131 | * Concept Index:: Index of Concepts
|
|---|
| 132 | * Name Index:: Index of Functions, Variables, & Directives
|
|---|
| 133 |
|
|---|
| 134 | @detailmenu
|
|---|
| 135 | --- The Detailed Node Listing ---
|
|---|
| 136 |
|
|---|
| 137 | Overview of @code{make}
|
|---|
| 138 |
|
|---|
| 139 | * Preparing:: Preparing and Running Make
|
|---|
| 140 | * Reading:: On Reading this Text
|
|---|
| 141 | * Bugs:: Problems and Bugs
|
|---|
| 142 |
|
|---|
| 143 | An Introduction to Makefiles
|
|---|
| 144 |
|
|---|
| 145 | * Rule Introduction:: What a rule looks like.
|
|---|
| 146 | * Simple Makefile:: A Simple Makefile
|
|---|
| 147 | * How Make Works:: How @code{make} Processes This Makefile
|
|---|
| 148 | * Variables Simplify:: Variables Make Makefiles Simpler
|
|---|
| 149 | * make Deduces:: Letting @code{make} Deduce the Commands
|
|---|
| 150 | * Combine By Prerequisite:: Another Style of Makefile
|
|---|
| 151 | * Cleanup:: Rules for Cleaning the Directory
|
|---|
| 152 |
|
|---|
| 153 | Writing Makefiles
|
|---|
| 154 |
|
|---|
| 155 | * Makefile Contents:: What makefiles contain.
|
|---|
| 156 | * Makefile Names:: How to name your makefile.
|
|---|
| 157 | * Include:: How one makefile can use another makefile.
|
|---|
| 158 | * MAKEFILES Variable:: The environment can specify extra makefiles.
|
|---|
| 159 | * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
|
|---|
| 160 | * Special Variables:: Other special variables.
|
|---|
| 161 | * Remaking Makefiles:: How makefiles get remade.
|
|---|
| 162 | * Overriding Makefiles:: How to override part of one makefile
|
|---|
| 163 | with another makefile.
|
|---|
| 164 | * Reading Makefiles:: How makefiles are parsed.
|
|---|
| 165 | * Secondary Expansion:: How and when secondary expansion is performed.
|
|---|
| 166 |
|
|---|
| 167 | Writing Rules
|
|---|
| 168 |
|
|---|
| 169 | * Rule Example:: An example explained.
|
|---|
| 170 | * Rule Syntax:: General syntax explained.
|
|---|
| 171 | * Prerequisite Types:: There are two types of prerequisites.
|
|---|
| 172 | * Wildcards:: Using wildcard characters such as `*'.
|
|---|
| 173 | * Directory Search:: Searching other directories for source files.
|
|---|
| 174 | * Phony Targets:: Using a target that is not a real file's name.
|
|---|
| 175 | * Force Targets:: You can use a target without commands
|
|---|
| 176 | or prerequisites to mark other
|
|---|
| 177 | targets as phony.
|
|---|
| 178 | * Empty Targets:: When only the date matters and the
|
|---|
| 179 | files are empty.
|
|---|
| 180 | * Special Targets:: Targets with special built-in meanings.
|
|---|
| 181 | * Multiple Targets:: When to make use of several targets in a rule.
|
|---|
| 182 | * Multiple Rules:: How to use several rules with the same target.
|
|---|
| 183 | * Static Pattern:: Static pattern rules apply to multiple targets
|
|---|
| 184 | and can vary the prerequisites according to
|
|---|
| 185 | the target name.
|
|---|
| 186 | * Double-Colon:: How to use a special kind of rule to allow
|
|---|
| 187 | several independent rules for one target.
|
|---|
| 188 | * Automatic Prerequisites:: How to automatically generate rules giving
|
|---|
| 189 | prerequisites from source files themselves.
|
|---|
| 190 |
|
|---|
| 191 | Using Wildcard Characters in File Names
|
|---|
| 192 |
|
|---|
| 193 | * Wildcard Examples:: Several examples
|
|---|
| 194 | * Wildcard Pitfall:: Problems to avoid.
|
|---|
| 195 | * Wildcard Function:: How to cause wildcard expansion where
|
|---|
| 196 | it does not normally take place.
|
|---|
| 197 |
|
|---|
| 198 | Searching Directories for Prerequisites
|
|---|
| 199 |
|
|---|
| 200 | * General Search:: Specifying a search path that applies
|
|---|
| 201 | to every prerequisite.
|
|---|
| 202 | * Selective Search:: Specifying a search path
|
|---|
| 203 | for a specified class of names.
|
|---|
| 204 | * Search Algorithm:: When and how search paths are applied.
|
|---|
| 205 | * Commands/Search:: How to write shell commands that work together
|
|---|
| 206 | with search paths.
|
|---|
| 207 | * Implicit/Search:: How search paths affect implicit rules.
|
|---|
| 208 | * Libraries/Search:: Directory search for link libraries.
|
|---|
| 209 |
|
|---|
| 210 | Static Pattern Rules
|
|---|
| 211 |
|
|---|
| 212 | * Static Usage:: The syntax of static pattern rules.
|
|---|
| 213 | * Static versus Implicit:: When are they better than implicit rules?
|
|---|
| 214 |
|
|---|
| 215 | Writing the Commands in Rules
|
|---|
| 216 |
|
|---|
| 217 | * Echoing:: How to control when commands are echoed.
|
|---|
| 218 | * Execution:: How commands are executed.
|
|---|
| 219 | * Parallel:: How commands can be executed in parallel.
|
|---|
| 220 | * Errors:: What happens after a command execution error.
|
|---|
| 221 | * Interrupts:: What happens when a command is interrupted.
|
|---|
| 222 | * Recursion:: Invoking @code{make} from makefiles.
|
|---|
| 223 | * Sequences:: Defining canned sequences of commands.
|
|---|
| 224 | * Empty Commands:: Defining useful, do-nothing commands.
|
|---|
| 225 |
|
|---|
| 226 | Recursive Use of @code{make}
|
|---|
| 227 |
|
|---|
| 228 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
|---|
| 229 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
|---|
| 230 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
|---|
| 231 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
|---|
| 232 | helps debug use of recursive @code{make} commands.
|
|---|
| 233 |
|
|---|
| 234 | How to Use Variables
|
|---|
| 235 |
|
|---|
| 236 | * Reference:: How to use the value of a variable.
|
|---|
| 237 | * Flavors:: Variables come in two flavors.
|
|---|
| 238 | * Advanced:: Advanced features for referencing a variable.
|
|---|
| 239 | * Values:: All the ways variables get their values.
|
|---|
| 240 | * Setting:: How to set a variable in the makefile.
|
|---|
| 241 | * Appending:: How to append more text to the old value
|
|---|
| 242 | of a variable.
|
|---|
| 243 | * Override Directive:: How to set a variable in the makefile even if
|
|---|
| 244 | the user has set it with a command argument.
|
|---|
| 245 | * Defining:: An alternate way to set a variable
|
|---|
| 246 | to a verbatim string.
|
|---|
| 247 | * Environment:: Variable values can come from the environment.
|
|---|
| 248 | * Target-specific:: Variable values can be defined on a per-target
|
|---|
| 249 | basis.
|
|---|
| 250 | * Pattern-specific:: Target-specific variable values can be applied
|
|---|
| 251 | to a group of targets that match a pattern.
|
|---|
| 252 |
|
|---|
| 253 | Advanced Features for Reference to Variables
|
|---|
| 254 |
|
|---|
| 255 | * Substitution Refs:: Referencing a variable with
|
|---|
| 256 | substitutions on the value.
|
|---|
| 257 | * Computed Names:: Computing the name of the variable to refer to.
|
|---|
| 258 |
|
|---|
| 259 | Conditional Parts of Makefiles
|
|---|
| 260 |
|
|---|
| 261 | * Conditional Example:: Example of a conditional
|
|---|
| 262 | * Conditional Syntax:: The syntax of conditionals.
|
|---|
| 263 | * Testing Flags:: Conditionals that test flags.
|
|---|
| 264 |
|
|---|
| 265 | Functions for Transforming Text
|
|---|
| 266 |
|
|---|
| 267 | * Syntax of Functions:: How to write a function call.
|
|---|
| 268 | * Text Functions:: General-purpose text manipulation functions.
|
|---|
| 269 | * File Name Functions:: Functions for manipulating file names.
|
|---|
| 270 | * Foreach Function:: Repeat some text with controlled variation.
|
|---|
| 271 | * If Function:: Conditionally expand a value.
|
|---|
| 272 | * Call Function:: Expand a user-defined function.
|
|---|
| 273 | * Value Function:: Return the un-expanded value of a variable.
|
|---|
| 274 | * Eval Function:: Evaluate the arguments as makefile syntax.
|
|---|
| 275 | * Origin Function:: Find where a variable got its value.
|
|---|
| 276 | * Shell Function:: Substitute the output of a shell command.
|
|---|
| 277 | * Make Control Functions:: Functions that control how make runs.
|
|---|
| 278 |
|
|---|
| 279 | How to Run @code{make}
|
|---|
| 280 |
|
|---|
| 281 | * Makefile Arguments:: How to specify which makefile to use.
|
|---|
| 282 | * Goals:: How to use goal arguments to specify which
|
|---|
| 283 | parts of the makefile to use.
|
|---|
| 284 | * Instead of Execution:: How to use mode flags to specify what
|
|---|
| 285 | kind of thing to do with the commands
|
|---|
| 286 | in the makefile other than simply
|
|---|
| 287 | execute them.
|
|---|
| 288 | * Avoiding Compilation:: How to avoid recompiling certain files.
|
|---|
| 289 | * Overriding:: How to override a variable to specify
|
|---|
| 290 | an alternate compiler and other things.
|
|---|
| 291 | * Testing:: How to proceed past some errors, to
|
|---|
| 292 | test compilation.
|
|---|
| 293 | * Options Summary:: Summary of Options
|
|---|
| 294 |
|
|---|
| 295 | Using Implicit Rules
|
|---|
| 296 |
|
|---|
| 297 | * Using Implicit:: How to use an existing implicit rule
|
|---|
| 298 | to get the commands for updating a file.
|
|---|
| 299 | * Catalogue of Rules:: A list of built-in implicit rules.
|
|---|
| 300 | * Implicit Variables:: How to change what predefined rules do.
|
|---|
| 301 | * Chained Rules:: How to use a chain of implicit rules.
|
|---|
| 302 | * Pattern Rules:: How to define new implicit rules.
|
|---|
| 303 | * Last Resort:: How to defining commands for rules
|
|---|
| 304 | which cannot find any.
|
|---|
| 305 | * Suffix Rules:: The old-fashioned style of implicit rule.
|
|---|
| 306 | * Implicit Rule Search:: The precise algorithm for applying
|
|---|
| 307 | implicit rules.
|
|---|
| 308 |
|
|---|
| 309 | Defining and Redefining Pattern Rules
|
|---|
| 310 |
|
|---|
| 311 | * Pattern Intro:: An introduction to pattern rules.
|
|---|
| 312 | * Pattern Examples:: Examples of pattern rules.
|
|---|
| 313 | * Automatic Variables:: How to use automatic variables in the
|
|---|
| 314 | commands of implicit rules.
|
|---|
| 315 | * Pattern Match:: How patterns match.
|
|---|
| 316 | * Match-Anything Rules:: Precautions you should take prior to
|
|---|
| 317 | defining rules that can match any
|
|---|
| 318 | target file whatever.
|
|---|
| 319 | * Canceling Rules:: How to override or cancel built-in rules.
|
|---|
| 320 |
|
|---|
| 321 | Using @code{make} to Update Archive Files
|
|---|
| 322 |
|
|---|
| 323 | * Archive Members:: Archive members as targets.
|
|---|
| 324 | * Archive Update:: The implicit rule for archive member targets.
|
|---|
| 325 | * Archive Pitfalls:: Dangers to watch out for when using archives.
|
|---|
| 326 | * Archive Suffix Rules:: You can write a special kind of suffix rule
|
|---|
| 327 | for updating archives.
|
|---|
| 328 |
|
|---|
| 329 | Implicit Rule for Archive Member Targets
|
|---|
| 330 |
|
|---|
| 331 | * Archive Symbols:: How to update archive symbol directories.
|
|---|
| 332 |
|
|---|
| 333 | Makefile Conventions
|
|---|
| 334 |
|
|---|
| 335 | * Makefile Basics:: General Conventions for Makefiles
|
|---|
| 336 | * Utilities in Makefiles:: Utilities in Makefiles
|
|---|
| 337 | * Command Variables:: Variables for Specifying Commands
|
|---|
| 338 | * Directory Variables:: Variables for Installation Directories
|
|---|
| 339 | * Standard Targets:: Standard Targets for Users
|
|---|
| 340 | * Install Command Categories:: Three categories of commands in the `install'
|
|---|
| 341 |
|
|---|
| 342 | @end detailmenu
|
|---|
| 343 | @end menu
|
|---|
| 344 |
|
|---|
| 345 | @node Overview, Introduction, Top, Top
|
|---|
| 346 | @comment node-name, next, previous, up
|
|---|
| 347 | @chapter Overview of @code{make}
|
|---|
| 348 |
|
|---|
| 349 | The @code{make} utility automatically determines which pieces of a large
|
|---|
| 350 | program need to be recompiled, and issues commands to recompile them.
|
|---|
| 351 | This manual describes GNU @code{make}, which was implemented by Richard
|
|---|
| 352 | Stallman and Roland McGrath. Development since Version 3.76 has been
|
|---|
| 353 | handled by Paul D. Smith.
|
|---|
| 354 |
|
|---|
| 355 | GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
|
|---|
| 356 | 1003.2-1992} (POSIX.2).
|
|---|
| 357 | @cindex POSIX
|
|---|
| 358 | @cindex IEEE Standard 1003.2
|
|---|
| 359 | @cindex standards conformance
|
|---|
| 360 |
|
|---|
| 361 | Our examples show C programs, since they are most common, but you can use
|
|---|
| 362 | @code{make} with any programming language whose compiler can be run with a
|
|---|
| 363 | shell command. Indeed, @code{make} is not limited to programs. You can
|
|---|
| 364 | use it to describe any task where some files must be updated automatically
|
|---|
| 365 | from others whenever the others change.
|
|---|
| 366 |
|
|---|
| 367 | @menu
|
|---|
| 368 | * Preparing:: Preparing and Running Make
|
|---|
| 369 | * Reading:: On Reading this Text
|
|---|
| 370 | * Bugs:: Problems and Bugs
|
|---|
| 371 | @end menu
|
|---|
| 372 |
|
|---|
| 373 | @node Preparing, Reading, Overview, Overview
|
|---|
| 374 | @ifnottex
|
|---|
| 375 | @heading Preparing and Running Make
|
|---|
| 376 | @end ifnottex
|
|---|
| 377 |
|
|---|
| 378 | To prepare to use @code{make}, you must write a file called
|
|---|
| 379 | the @dfn{makefile} that describes the relationships among files
|
|---|
| 380 | in your program and provides commands for updating each file.
|
|---|
| 381 | In a program, typically, the executable file is updated from object
|
|---|
| 382 | files, which are in turn made by compiling source files.@refill
|
|---|
| 383 |
|
|---|
| 384 | Once a suitable makefile exists, each time you change some source files,
|
|---|
| 385 | this simple shell command:
|
|---|
| 386 |
|
|---|
| 387 | @example
|
|---|
| 388 | make
|
|---|
| 389 | @end example
|
|---|
| 390 |
|
|---|
| 391 | @noindent
|
|---|
| 392 | suffices to perform all necessary recompilations. The @code{make} program
|
|---|
| 393 | uses the makefile data base and the last-modification times of the files to
|
|---|
| 394 | decide which of the files need to be updated. For each of those files, it
|
|---|
| 395 | issues the commands recorded in the data base.
|
|---|
| 396 |
|
|---|
| 397 | You can provide command line arguments to @code{make} to control which
|
|---|
| 398 | files should be recompiled, or how. @xref{Running, ,How to Run
|
|---|
| 399 | @code{make}}.
|
|---|
| 400 |
|
|---|
| 401 | @node Reading, Bugs, Preparing, Overview
|
|---|
| 402 | @section How to Read This Manual
|
|---|
| 403 |
|
|---|
| 404 | If you are new to @code{make}, or are looking for a general
|
|---|
| 405 | introduction, read the first few sections of each chapter, skipping the
|
|---|
| 406 | later sections. In each chapter, the first few sections contain
|
|---|
| 407 | introductory or general information and the later sections contain
|
|---|
| 408 | specialized or technical information.
|
|---|
| 409 | @ifnottex
|
|---|
| 410 | The exception is the second chapter, @ref{Introduction, ,An
|
|---|
| 411 | Introduction to Makefiles}, all of which is introductory.
|
|---|
| 412 | @end ifnottex
|
|---|
| 413 | @iftex
|
|---|
| 414 | The exception is @ref{Introduction, ,An Introduction to Makefiles},
|
|---|
| 415 | all of which is introductory.
|
|---|
| 416 | @end iftex
|
|---|
| 417 |
|
|---|
| 418 | If you are familiar with other @code{make} programs, see @ref{Features,
|
|---|
| 419 | ,Features of GNU @code{make}}, which lists the enhancements GNU
|
|---|
| 420 | @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
|
|---|
| 421 | Features}, which explains the few things GNU @code{make} lacks that
|
|---|
| 422 | others have.
|
|---|
| 423 |
|
|---|
| 424 | For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
|
|---|
| 425 | and @ref{Special Targets}.
|
|---|
| 426 |
|
|---|
| 427 | @node Bugs, , Reading, Overview
|
|---|
| 428 | @section Problems and Bugs
|
|---|
| 429 | @cindex reporting bugs
|
|---|
| 430 | @cindex bugs, reporting
|
|---|
| 431 | @cindex problems and bugs, reporting
|
|---|
| 432 |
|
|---|
| 433 | If you have problems with GNU @code{make} or think you've found a bug,
|
|---|
| 434 | please report it to the developers; we cannot promise to do anything but
|
|---|
| 435 | we might well want to fix it.
|
|---|
| 436 |
|
|---|
| 437 | Before reporting a bug, make sure you've actually found a real bug.
|
|---|
| 438 | Carefully reread the documentation and see if it really says you can do
|
|---|
| 439 | what you're trying to do. If it's not clear whether you should be able
|
|---|
| 440 | to do something or not, report that too; it's a bug in the
|
|---|
| 441 | documentation!
|
|---|
| 442 |
|
|---|
| 443 | Before reporting a bug or trying to fix it yourself, try to isolate it
|
|---|
| 444 | to the smallest possible makefile that reproduces the problem. Then
|
|---|
| 445 | send us the makefile and the exact results @code{make} gave you,
|
|---|
| 446 | including any error or warning messages. Please don't paraphrase
|
|---|
| 447 | these messages: it's best to cut and paste them into your report.
|
|---|
| 448 | When generating this small makefile, be sure to not use any non-free
|
|---|
| 449 | or unusual tools in your commands: you can almost always emulate what
|
|---|
| 450 | such a tool would do with simple shell commands. Finally, be sure to
|
|---|
| 451 | explain what you expected to occur; this will help us decide whether
|
|---|
| 452 | the problem was really in the documentation.
|
|---|
| 453 |
|
|---|
| 454 | Once you have a precise problem you can report it in one of two ways.
|
|---|
| 455 | Either send electronic mail to:
|
|---|
| 456 |
|
|---|
| 457 | @example
|
|---|
| 458 | bug-make@@gnu.org
|
|---|
| 459 | @end example
|
|---|
| 460 |
|
|---|
| 461 | @noindent
|
|---|
| 462 | or use our Web-based project management tool, at:
|
|---|
| 463 |
|
|---|
| 464 | @example
|
|---|
| 465 | http://savannah.gnu.org/projects/make/
|
|---|
| 466 | @end example
|
|---|
| 467 |
|
|---|
| 468 | @noindent
|
|---|
| 469 | In addition to the information above, please be careful to include the
|
|---|
| 470 | version number of @code{make} you are using. You can get this
|
|---|
| 471 | information with the command @samp{make --version}. Be sure also to
|
|---|
| 472 | include the type of machine and operating system you are using. One
|
|---|
| 473 | way to obtain this information is by looking at the final lines of
|
|---|
| 474 | output from the command @samp{make --help}.
|
|---|
| 475 |
|
|---|
| 476 | @node Introduction, Makefiles, Overview, Top
|
|---|
| 477 | @comment node-name, next, previous, up
|
|---|
| 478 | @chapter An Introduction to Makefiles
|
|---|
| 479 |
|
|---|
| 480 | You need a file called a @dfn{makefile} to tell @code{make} what to do.
|
|---|
| 481 | Most often, the makefile tells @code{make} how to compile and link a
|
|---|
| 482 | program.
|
|---|
| 483 | @cindex makefile
|
|---|
| 484 |
|
|---|
| 485 | In this chapter, we will discuss a simple makefile that describes how to
|
|---|
| 486 | compile and link a text editor which consists of eight C source files
|
|---|
| 487 | and three header files. The makefile can also tell @code{make} how to
|
|---|
| 488 | run miscellaneous commands when explicitly asked (for example, to remove
|
|---|
| 489 | certain files as a clean-up operation). To see a more complex example
|
|---|
| 490 | of a makefile, see @ref{Complex Makefile}.
|
|---|
| 491 |
|
|---|
| 492 | When @code{make} recompiles the editor, each changed C source file
|
|---|
| 493 | must be recompiled. If a header file has changed, each C source file
|
|---|
| 494 | that includes the header file must be recompiled to be safe. Each
|
|---|
| 495 | compilation produces an object file corresponding to the source file.
|
|---|
| 496 | Finally, if any source file has been recompiled, all the object files,
|
|---|
| 497 | whether newly made or saved from previous compilations, must be linked
|
|---|
| 498 | together to produce the new executable editor.
|
|---|
| 499 | @cindex recompilation
|
|---|
| 500 | @cindex editor
|
|---|
| 501 |
|
|---|
| 502 | @menu
|
|---|
| 503 | * Rule Introduction:: What a rule looks like.
|
|---|
| 504 | * Simple Makefile:: A Simple Makefile
|
|---|
| 505 | * How Make Works:: How @code{make} Processes This Makefile
|
|---|
| 506 | * Variables Simplify:: Variables Make Makefiles Simpler
|
|---|
| 507 | * make Deduces:: Letting @code{make} Deduce the Commands
|
|---|
| 508 | * Combine By Prerequisite:: Another Style of Makefile
|
|---|
| 509 | * Cleanup:: Rules for Cleaning the Directory
|
|---|
| 510 | @end menu
|
|---|
| 511 |
|
|---|
| 512 | @node Rule Introduction, Simple Makefile, Introduction, Introduction
|
|---|
| 513 | @comment node-name, next, previous, up
|
|---|
| 514 | @section What a Rule Looks Like
|
|---|
| 515 | @cindex rule, introduction to
|
|---|
| 516 | @cindex makefile rule parts
|
|---|
| 517 | @cindex parts of makefile rule
|
|---|
| 518 |
|
|---|
| 519 | A simple makefile consists of ``rules'' with the following shape:
|
|---|
| 520 |
|
|---|
| 521 | @cindex targets, introduction to
|
|---|
| 522 | @cindex prerequisites, introduction to
|
|---|
| 523 | @cindex commands, introduction to
|
|---|
| 524 | @example
|
|---|
| 525 | @group
|
|---|
| 526 | @var{target} @dots{} : @var{prerequisites} @dots{}
|
|---|
| 527 | @var{command}
|
|---|
| 528 | @dots{}
|
|---|
| 529 | @dots{}
|
|---|
| 530 | @end group
|
|---|
| 531 | @end example
|
|---|
| 532 |
|
|---|
| 533 | A @dfn{target} is usually the name of a file that is generated by a
|
|---|
| 534 | program; examples of targets are executable or object files. A target
|
|---|
| 535 | can also be the name of an action to carry out, such as @samp{clean}
|
|---|
| 536 | (@pxref{Phony Targets}).
|
|---|
| 537 |
|
|---|
| 538 | A @dfn{prerequisite} is a file that is used as input to create the
|
|---|
| 539 | target. A target often depends on several files.
|
|---|
| 540 |
|
|---|
| 541 | @cindex tabs in rules
|
|---|
| 542 | A @dfn{command} is an action that @code{make} carries out.
|
|---|
| 543 | A rule may have more than one command, each on its own line.
|
|---|
| 544 | @strong{Please note:} you need to put a tab character at the beginning of
|
|---|
| 545 | every command line! This is an obscurity that catches the unwary.
|
|---|
| 546 |
|
|---|
| 547 | Usually a command is in a rule with prerequisites and serves to create a
|
|---|
| 548 | target file if any of the prerequisites change. However, the rule that
|
|---|
| 549 | specifies commands for the target need not have prerequisites. For
|
|---|
| 550 | example, the rule containing the delete command associated with the
|
|---|
| 551 | target @samp{clean} does not have prerequisites.
|
|---|
| 552 |
|
|---|
| 553 | A @dfn{rule}, then, explains how and when to remake certain files
|
|---|
| 554 | which are the targets of the particular rule. @code{make} carries out
|
|---|
| 555 | the commands on the prerequisites to create or update the target. A
|
|---|
| 556 | rule can also explain how and when to carry out an action.
|
|---|
| 557 | @xref{Rules, , Writing Rules}.
|
|---|
| 558 |
|
|---|
| 559 | A makefile may contain other text besides rules, but a simple makefile
|
|---|
| 560 | need only contain rules. Rules may look somewhat more complicated
|
|---|
| 561 | than shown in this template, but all fit the pattern more or less.
|
|---|
| 562 |
|
|---|
| 563 | @node Simple Makefile, How Make Works, Rule Introduction, Introduction
|
|---|
| 564 | @section A Simple Makefile
|
|---|
| 565 | @cindex simple makefile
|
|---|
| 566 | @cindex makefile, simple
|
|---|
| 567 |
|
|---|
| 568 | Here is a straightforward makefile that describes the way an
|
|---|
| 569 | executable file called @code{edit} depends on eight object files
|
|---|
| 570 | which, in turn, depend on eight C source and three header files.
|
|---|
| 571 |
|
|---|
| 572 | In this example, all the C files include @file{defs.h}, but only those
|
|---|
| 573 | defining editing commands include @file{command.h}, and only low
|
|---|
| 574 | level files that change the editor buffer include @file{buffer.h}.
|
|---|
| 575 |
|
|---|
| 576 | @example
|
|---|
| 577 | @group
|
|---|
| 578 | edit : main.o kbd.o command.o display.o \
|
|---|
| 579 | insert.o search.o files.o utils.o
|
|---|
| 580 | cc -o edit main.o kbd.o command.o display.o \
|
|---|
| 581 | insert.o search.o files.o utils.o
|
|---|
| 582 |
|
|---|
| 583 | main.o : main.c defs.h
|
|---|
| 584 | cc -c main.c
|
|---|
| 585 | kbd.o : kbd.c defs.h command.h
|
|---|
| 586 | cc -c kbd.c
|
|---|
| 587 | command.o : command.c defs.h command.h
|
|---|
| 588 | cc -c command.c
|
|---|
| 589 | display.o : display.c defs.h buffer.h
|
|---|
| 590 | cc -c display.c
|
|---|
| 591 | insert.o : insert.c defs.h buffer.h
|
|---|
| 592 | cc -c insert.c
|
|---|
| 593 | search.o : search.c defs.h buffer.h
|
|---|
| 594 | cc -c search.c
|
|---|
| 595 | files.o : files.c defs.h buffer.h command.h
|
|---|
| 596 | cc -c files.c
|
|---|
| 597 | utils.o : utils.c defs.h
|
|---|
| 598 | cc -c utils.c
|
|---|
| 599 | clean :
|
|---|
| 600 | rm edit main.o kbd.o command.o display.o \
|
|---|
| 601 | insert.o search.o files.o utils.o
|
|---|
| 602 | @end group
|
|---|
| 603 | @end example
|
|---|
| 604 |
|
|---|
| 605 | @noindent
|
|---|
| 606 | We split each long line into two lines using backslash-newline; this is
|
|---|
| 607 | like using one long line, but is easier to read.
|
|---|
| 608 | @cindex continuation lines
|
|---|
| 609 | @cindex @code{\} (backslash), for continuation lines
|
|---|
| 610 | @cindex backslash (@code{\}), for continuation lines
|
|---|
| 611 | @cindex quoting newline, in makefile
|
|---|
| 612 | @cindex newline, quoting, in makefile
|
|---|
| 613 |
|
|---|
| 614 | To use this makefile to create the executable file called @file{edit},
|
|---|
| 615 | type:
|
|---|
| 616 |
|
|---|
| 617 | @example
|
|---|
| 618 | make
|
|---|
| 619 | @end example
|
|---|
| 620 |
|
|---|
| 621 | To use this makefile to delete the executable file and all the object
|
|---|
| 622 | files from the directory, type:
|
|---|
| 623 |
|
|---|
| 624 | @example
|
|---|
| 625 | make clean
|
|---|
| 626 | @end example
|
|---|
| 627 |
|
|---|
| 628 | In the example makefile, the targets include the executable file
|
|---|
| 629 | @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
|
|---|
| 630 | prerequisites are files such as @samp{main.c} and @samp{defs.h}.
|
|---|
| 631 | In fact, each @samp{.o} file is both a target and a prerequisite.
|
|---|
| 632 | Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
|
|---|
| 633 |
|
|---|
| 634 | When a target is a file, it needs to be recompiled or relinked if any
|
|---|
| 635 | of its prerequisites change. In addition, any prerequisites that are
|
|---|
| 636 | themselves automatically generated should be updated first. In this
|
|---|
| 637 | example, @file{edit} depends on each of the eight object files; the
|
|---|
| 638 | object file @file{main.o} depends on the source file @file{main.c} and
|
|---|
| 639 | on the header file @file{defs.h}.
|
|---|
| 640 |
|
|---|
| 641 | A shell command follows each line that contains a target and
|
|---|
| 642 | prerequisites. These shell commands say how to update the target file.
|
|---|
| 643 | A tab character must come at the beginning of every command line to
|
|---|
| 644 | distinguish command lines from other lines in the makefile. (Bear in
|
|---|
| 645 | mind that @code{make} does not know anything about how the commands
|
|---|
| 646 | work. It is up to you to supply commands that will update the target
|
|---|
| 647 | file properly. All @code{make} does is execute the commands in the rule
|
|---|
| 648 | you have specified when the target file needs to be updated.)
|
|---|
| 649 | @cindex shell command
|
|---|
| 650 |
|
|---|
| 651 | The target @samp{clean} is not a file, but merely the name of an
|
|---|
| 652 | action. Since you
|
|---|
| 653 | normally
|
|---|
| 654 | do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
|
|---|
| 655 | Consequently, @code{make} never does anything with it unless you tell
|
|---|
| 656 | it specifically. Note that this rule not only is not a prerequisite, it
|
|---|
| 657 | also does not have any prerequisites, so the only purpose of the rule
|
|---|
| 658 | is to run the specified commands. Targets that do not refer to files
|
|---|
| 659 | but are just actions are called @dfn{phony targets}. @xref{Phony
|
|---|
| 660 | Targets}, for information about this kind of target. @xref{Errors, ,
|
|---|
| 661 | Errors in Commands}, to see how to cause @code{make} to ignore errors
|
|---|
| 662 | from @code{rm} or any other command.
|
|---|
| 663 | @cindex @code{clean} target
|
|---|
| 664 | @cindex @code{rm} (shell command)
|
|---|
| 665 |
|
|---|
| 666 | @node How Make Works, Variables Simplify, Simple Makefile, Introduction
|
|---|
| 667 | @comment node-name, next, previous, up
|
|---|
| 668 | @section How @code{make} Processes a Makefile
|
|---|
| 669 | @cindex processing a makefile
|
|---|
| 670 | @cindex makefile, how @code{make} processes
|
|---|
| 671 |
|
|---|
| 672 | By default, @code{make} starts with the first target (not targets whose
|
|---|
| 673 | names start with @samp{.}). This is called the @dfn{default goal}.
|
|---|
| 674 | (@dfn{Goals} are the targets that @code{make} strives ultimately to
|
|---|
| 675 | update. You can override this behavior using the command line
|
|---|
| 676 | (@pxref{Goals, , Arguments to Specify the Goals}) or with the
|
|---|
| 677 | @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
|
|---|
| 678 | Other Special Variables}).
|
|---|
| 679 | @cindex default goal
|
|---|
| 680 | @cindex goal, default
|
|---|
| 681 | @cindex goal
|
|---|
| 682 |
|
|---|
| 683 | In the simple example of the previous section, the default goal is to
|
|---|
| 684 | update the executable program @file{edit}; therefore, we put that rule
|
|---|
| 685 | first.
|
|---|
| 686 |
|
|---|
| 687 | Thus, when you give the command:
|
|---|
| 688 |
|
|---|
| 689 | @example
|
|---|
| 690 | make
|
|---|
| 691 | @end example
|
|---|
| 692 |
|
|---|
| 693 | @noindent
|
|---|
| 694 | @code{make} reads the makefile in the current directory and begins by
|
|---|
| 695 | processing the first rule. In the example, this rule is for relinking
|
|---|
| 696 | @file{edit}; but before @code{make} can fully process this rule, it
|
|---|
| 697 | must process the rules for the files that @file{edit} depends on,
|
|---|
| 698 | which in this case are the object files. Each of these files is
|
|---|
| 699 | processed according to its own rule. These rules say to update each
|
|---|
| 700 | @samp{.o} file by compiling its source file. The recompilation must
|
|---|
| 701 | be done if the source file, or any of the header files named as
|
|---|
| 702 | prerequisites, is more recent than the object file, or if the object
|
|---|
| 703 | file does not exist.
|
|---|
| 704 |
|
|---|
| 705 | The other rules are processed because their targets appear as
|
|---|
| 706 | prerequisites of the goal. If some other rule is not depended on by the
|
|---|
| 707 | goal (or anything it depends on, etc.), that rule is not processed,
|
|---|
| 708 | unless you tell @code{make} to do so (with a command such as
|
|---|
| 709 | @w{@code{make clean}}).
|
|---|
| 710 |
|
|---|
| 711 | Before recompiling an object file, @code{make} considers updating its
|
|---|
| 712 | prerequisites, the source file and header files. This makefile does not
|
|---|
| 713 | specify anything to be done for them---the @samp{.c} and @samp{.h} files
|
|---|
| 714 | are not the targets of any rules---so @code{make} does nothing for these
|
|---|
| 715 | files. But @code{make} would update automatically generated C programs,
|
|---|
| 716 | such as those made by Bison or Yacc, by their own rules at this time.
|
|---|
| 717 |
|
|---|
| 718 | After recompiling whichever object files need it, @code{make} decides
|
|---|
| 719 | whether to relink @file{edit}. This must be done if the file
|
|---|
| 720 | @file{edit} does not exist, or if any of the object files are newer than
|
|---|
| 721 | it. If an object file was just recompiled, it is now newer than
|
|---|
| 722 | @file{edit}, so @file{edit} is relinked.
|
|---|
| 723 | @cindex relinking
|
|---|
| 724 |
|
|---|
| 725 | Thus, if we change the file @file{insert.c} and run @code{make},
|
|---|
| 726 | @code{make} will compile that file to update @file{insert.o}, and then
|
|---|
| 727 | link @file{edit}. If we change the file @file{command.h} and run
|
|---|
| 728 | @code{make}, @code{make} will recompile the object files @file{kbd.o},
|
|---|
| 729 | @file{command.o} and @file{files.o} and then link the file @file{edit}.
|
|---|
| 730 |
|
|---|
| 731 | @node Variables Simplify, make Deduces, How Make Works, Introduction
|
|---|
| 732 | @section Variables Make Makefiles Simpler
|
|---|
| 733 | @cindex variables
|
|---|
| 734 | @cindex simplifying with variables
|
|---|
| 735 |
|
|---|
| 736 | In our example, we had to list all the object files twice in the rule for
|
|---|
| 737 | @file{edit} (repeated here):
|
|---|
| 738 |
|
|---|
| 739 | @example
|
|---|
| 740 | @group
|
|---|
| 741 | edit : main.o kbd.o command.o display.o \
|
|---|
| 742 | insert.o search.o files.o utils.o
|
|---|
| 743 | cc -o edit main.o kbd.o command.o display.o \
|
|---|
| 744 | insert.o search.o files.o utils.o
|
|---|
| 745 | @end group
|
|---|
| 746 | @end example
|
|---|
| 747 |
|
|---|
| 748 | @cindex @code{objects}
|
|---|
| 749 | Such duplication is error-prone; if a new object file is added to the
|
|---|
| 750 | system, we might add it to one list and forget the other. We can eliminate
|
|---|
| 751 | the risk and simplify the makefile by using a variable. @dfn{Variables}
|
|---|
| 752 | allow a text string to be defined once and substituted in multiple places
|
|---|
| 753 | later (@pxref{Using Variables, ,How to Use Variables}).
|
|---|
| 754 |
|
|---|
| 755 | @cindex @code{OBJECTS}
|
|---|
| 756 | @cindex @code{objs}
|
|---|
| 757 | @cindex @code{OBJS}
|
|---|
| 758 | @cindex @code{obj}
|
|---|
| 759 | @cindex @code{OBJ}
|
|---|
| 760 | It is standard practice for every makefile to have a variable named
|
|---|
| 761 | @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
|
|---|
| 762 | or @code{OBJ} which is a list of all object file names. We would
|
|---|
| 763 | define such a variable @code{objects} with a line like this in the
|
|---|
| 764 | makefile:@refill
|
|---|
| 765 |
|
|---|
| 766 | @example
|
|---|
| 767 | @group
|
|---|
| 768 | objects = main.o kbd.o command.o display.o \
|
|---|
| 769 | insert.o search.o files.o utils.o
|
|---|
| 770 | @end group
|
|---|
| 771 | @end example
|
|---|
| 772 |
|
|---|
| 773 | @noindent
|
|---|
| 774 | Then, each place we want to put a list of the object file names, we can
|
|---|
| 775 | substitute the variable's value by writing @samp{$(objects)}
|
|---|
| 776 | (@pxref{Using Variables, ,How to Use Variables}).
|
|---|
| 777 |
|
|---|
| 778 | Here is how the complete simple makefile looks when you use a variable
|
|---|
| 779 | for the object files:
|
|---|
| 780 |
|
|---|
| 781 | @example
|
|---|
| 782 | @group
|
|---|
| 783 | objects = main.o kbd.o command.o display.o \
|
|---|
| 784 | insert.o search.o files.o utils.o
|
|---|
| 785 |
|
|---|
| 786 | edit : $(objects)
|
|---|
| 787 | cc -o edit $(objects)
|
|---|
| 788 | main.o : main.c defs.h
|
|---|
| 789 | cc -c main.c
|
|---|
| 790 | kbd.o : kbd.c defs.h command.h
|
|---|
| 791 | cc -c kbd.c
|
|---|
| 792 | command.o : command.c defs.h command.h
|
|---|
| 793 | cc -c command.c
|
|---|
| 794 | display.o : display.c defs.h buffer.h
|
|---|
| 795 | cc -c display.c
|
|---|
| 796 | insert.o : insert.c defs.h buffer.h
|
|---|
| 797 | cc -c insert.c
|
|---|
| 798 | search.o : search.c defs.h buffer.h
|
|---|
| 799 | cc -c search.c
|
|---|
| 800 | files.o : files.c defs.h buffer.h command.h
|
|---|
| 801 | cc -c files.c
|
|---|
| 802 | utils.o : utils.c defs.h
|
|---|
| 803 | cc -c utils.c
|
|---|
| 804 | clean :
|
|---|
| 805 | rm edit $(objects)
|
|---|
| 806 | @end group
|
|---|
| 807 | @end example
|
|---|
| 808 |
|
|---|
| 809 | @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
|
|---|
| 810 | @section Letting @code{make} Deduce the Commands
|
|---|
| 811 | @cindex deducing commands (implicit rules)
|
|---|
| 812 | @cindex implicit rule, introduction to
|
|---|
| 813 | @cindex rule, implicit, introduction to
|
|---|
| 814 |
|
|---|
| 815 | It is not necessary to spell out the commands for compiling the individual
|
|---|
| 816 | C source files, because @code{make} can figure them out: it has an
|
|---|
| 817 | @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
|
|---|
| 818 | named @samp{.c} file using a @samp{cc -c} command. For example, it will
|
|---|
| 819 | use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
|
|---|
| 820 | @file{main.o}. We can therefore omit the commands from the rules for the
|
|---|
| 821 | object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
|
|---|
| 822 |
|
|---|
| 823 | When a @samp{.c} file is used automatically in this way, it is also
|
|---|
| 824 | automatically added to the list of prerequisites. We can therefore omit
|
|---|
| 825 | the @samp{.c} files from the prerequisites, provided we omit the commands.
|
|---|
| 826 |
|
|---|
| 827 | Here is the entire example, with both of these changes, and a variable
|
|---|
| 828 | @code{objects} as suggested above:
|
|---|
| 829 |
|
|---|
| 830 | @example
|
|---|
| 831 | @group
|
|---|
| 832 | objects = main.o kbd.o command.o display.o \
|
|---|
| 833 | insert.o search.o files.o utils.o
|
|---|
| 834 |
|
|---|
| 835 | edit : $(objects)
|
|---|
| 836 | cc -o edit $(objects)
|
|---|
| 837 |
|
|---|
| 838 | main.o : defs.h
|
|---|
| 839 | kbd.o : defs.h command.h
|
|---|
| 840 | command.o : defs.h command.h
|
|---|
| 841 | display.o : defs.h buffer.h
|
|---|
| 842 | insert.o : defs.h buffer.h
|
|---|
| 843 | search.o : defs.h buffer.h
|
|---|
| 844 | files.o : defs.h buffer.h command.h
|
|---|
| 845 | utils.o : defs.h
|
|---|
| 846 |
|
|---|
| 847 | .PHONY : clean
|
|---|
| 848 | clean :
|
|---|
| 849 | rm edit $(objects)
|
|---|
| 850 | @end group
|
|---|
| 851 | @end example
|
|---|
| 852 |
|
|---|
| 853 | @noindent
|
|---|
| 854 | This is how we would write the makefile in actual practice. (The
|
|---|
| 855 | complications associated with @samp{clean} are described elsewhere.
|
|---|
| 856 | See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
|
|---|
| 857 |
|
|---|
| 858 | Because implicit rules are so convenient, they are important. You
|
|---|
| 859 | will see them used frequently.@refill
|
|---|
| 860 |
|
|---|
| 861 | @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
|
|---|
| 862 | @section Another Style of Makefile
|
|---|
| 863 | @cindex combining rules by prerequisite
|
|---|
| 864 |
|
|---|
| 865 | When the objects of a makefile are created only by implicit rules, an
|
|---|
| 866 | alternative style of makefile is possible. In this style of makefile,
|
|---|
| 867 | you group entries by their prerequisites instead of by their targets.
|
|---|
| 868 | Here is what one looks like:
|
|---|
| 869 |
|
|---|
| 870 | @example
|
|---|
| 871 | @group
|
|---|
| 872 | objects = main.o kbd.o command.o display.o \
|
|---|
| 873 | insert.o search.o files.o utils.o
|
|---|
| 874 |
|
|---|
| 875 | edit : $(objects)
|
|---|
| 876 | cc -o edit $(objects)
|
|---|
| 877 |
|
|---|
| 878 | $(objects) : defs.h
|
|---|
| 879 | kbd.o command.o files.o : command.h
|
|---|
| 880 | display.o insert.o search.o files.o : buffer.h
|
|---|
| 881 | @end group
|
|---|
| 882 | @end example
|
|---|
| 883 |
|
|---|
| 884 | @noindent
|
|---|
| 885 | Here @file{defs.h} is given as a prerequisite of all the object files;
|
|---|
| 886 | @file{command.h} and @file{buffer.h} are prerequisites of the specific
|
|---|
| 887 | object files listed for them.
|
|---|
| 888 |
|
|---|
| 889 | Whether this is better is a matter of taste: it is more compact, but some
|
|---|
| 890 | people dislike it because they find it clearer to put all the information
|
|---|
| 891 | about each target in one place.
|
|---|
| 892 |
|
|---|
| 893 | @node Cleanup, , Combine By Prerequisite, Introduction
|
|---|
| 894 | @section Rules for Cleaning the Directory
|
|---|
| 895 | @cindex cleaning up
|
|---|
| 896 | @cindex removing, to clean up
|
|---|
| 897 |
|
|---|
| 898 | Compiling a program is not the only thing you might want to write rules
|
|---|
| 899 | for. Makefiles commonly tell how to do a few other things besides
|
|---|
| 900 | compiling a program: for example, how to delete all the object files
|
|---|
| 901 | and executables so that the directory is @samp{clean}.
|
|---|
| 902 |
|
|---|
| 903 | @cindex @code{clean} target
|
|---|
| 904 | Here is how we
|
|---|
| 905 | could write a @code{make} rule for cleaning our example editor:
|
|---|
| 906 |
|
|---|
| 907 | @example
|
|---|
| 908 | @group
|
|---|
| 909 | clean:
|
|---|
| 910 | rm edit $(objects)
|
|---|
| 911 | @end group
|
|---|
| 912 | @end example
|
|---|
| 913 |
|
|---|
| 914 | In practice, we might want to write the rule in a somewhat more
|
|---|
| 915 | complicated manner to handle unanticipated situations. We would do this:
|
|---|
| 916 |
|
|---|
| 917 | @example
|
|---|
| 918 | @group
|
|---|
| 919 | .PHONY : clean
|
|---|
| 920 | clean :
|
|---|
| 921 | -rm edit $(objects)
|
|---|
| 922 | @end group
|
|---|
| 923 | @end example
|
|---|
| 924 |
|
|---|
| 925 | @noindent
|
|---|
| 926 | This prevents @code{make} from getting confused by an actual file
|
|---|
| 927 | called @file{clean} and causes it to continue in spite of errors from
|
|---|
| 928 | @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
|
|---|
| 929 | Commands}.)
|
|---|
| 930 |
|
|---|
| 931 | @noindent
|
|---|
| 932 | A rule such as this should not be placed at the beginning of the
|
|---|
| 933 | makefile, because we do not want it to run by default! Thus, in the
|
|---|
| 934 | example makefile, we want the rule for @code{edit}, which recompiles
|
|---|
| 935 | the editor, to remain the default goal.
|
|---|
| 936 |
|
|---|
| 937 | Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
|
|---|
| 938 | run at all if we give the command @samp{make} with no arguments. In
|
|---|
| 939 | order to make the rule run, we have to type @samp{make clean}.
|
|---|
| 940 | @xref{Running, ,How to Run @code{make}}.
|
|---|
| 941 |
|
|---|
| 942 | @node Makefiles, Rules, Introduction, Top
|
|---|
| 943 | @chapter Writing Makefiles
|
|---|
| 944 |
|
|---|
| 945 | @cindex makefile, how to write
|
|---|
| 946 | The information that tells @code{make} how to recompile a system comes from
|
|---|
| 947 | reading a data base called the @dfn{makefile}.
|
|---|
| 948 |
|
|---|
| 949 | @menu
|
|---|
| 950 | * Makefile Contents:: What makefiles contain.
|
|---|
| 951 | * Makefile Names:: How to name your makefile.
|
|---|
| 952 | * Include:: How one makefile can use another makefile.
|
|---|
| 953 | * MAKEFILES Variable:: The environment can specify extra makefiles.
|
|---|
| 954 | * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
|
|---|
| 955 | * Special Variables:: Other special variables.
|
|---|
| 956 | * Remaking Makefiles:: How makefiles get remade.
|
|---|
| 957 | * Overriding Makefiles:: How to override part of one makefile
|
|---|
| 958 | with another makefile.
|
|---|
| 959 | * Reading Makefiles:: How makefiles are parsed.
|
|---|
| 960 | * Secondary Expansion:: How and when secondary expansion is performed.
|
|---|
| 961 | @end menu
|
|---|
| 962 |
|
|---|
| 963 | @node Makefile Contents, Makefile Names, Makefiles, Makefiles
|
|---|
| 964 | @section What Makefiles Contain
|
|---|
| 965 |
|
|---|
| 966 | Makefiles contain five kinds of things: @dfn{explicit rules},
|
|---|
| 967 | @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
|
|---|
| 968 | and @dfn{comments}. Rules, variables, and directives are described at
|
|---|
| 969 | length in later chapters.@refill
|
|---|
| 970 |
|
|---|
| 971 | @itemize @bullet
|
|---|
| 972 | @cindex rule, explicit, definition of
|
|---|
| 973 | @cindex explicit rule, definition of
|
|---|
| 974 | @item
|
|---|
| 975 | An @dfn{explicit rule} says when and how to remake one or more files,
|
|---|
| 976 | called the rule's @dfn{targets}. It lists the other files that the
|
|---|
| 977 | targets depend on, called the @dfn{prerequisites} of the target, and
|
|---|
| 978 | may also give commands to use to create or update the targets.
|
|---|
| 979 | @xref{Rules, ,Writing Rules}.
|
|---|
| 980 |
|
|---|
| 981 | @cindex rule, implicit, definition of
|
|---|
| 982 | @cindex implicit rule, definition of
|
|---|
| 983 | @item
|
|---|
| 984 | An @dfn{implicit rule} says when and how to remake a class of files
|
|---|
| 985 | based on their names. It describes how a target may depend on a file
|
|---|
| 986 | with a name similar to the target and gives commands to create or
|
|---|
| 987 | update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 988 |
|
|---|
| 989 | @cindex variable definition
|
|---|
| 990 | @item
|
|---|
| 991 | A @dfn{variable definition} is a line that specifies a text string
|
|---|
| 992 | value for a variable that can be substituted into the text later. The
|
|---|
| 993 | simple makefile example shows a variable definition for @code{objects}
|
|---|
| 994 | as a list of all object files (@pxref{Variables Simplify, , Variables
|
|---|
| 995 | Make Makefiles Simpler}).
|
|---|
| 996 |
|
|---|
| 997 | @cindex directive
|
|---|
| 998 | @item
|
|---|
| 999 | A @dfn{directive} is a command for @code{make} to do something special while
|
|---|
| 1000 | reading the makefile. These include:
|
|---|
| 1001 |
|
|---|
| 1002 | @itemize @bullet
|
|---|
| 1003 | @item
|
|---|
| 1004 | Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
|
|---|
| 1005 |
|
|---|
| 1006 | @item
|
|---|
| 1007 | Deciding (based on the values of variables) whether to use or
|
|---|
| 1008 | ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
|
|---|
| 1009 |
|
|---|
| 1010 | @item
|
|---|
| 1011 | Defining a variable from a verbatim string containing multiple lines
|
|---|
| 1012 | (@pxref{Defining, ,Defining Variables Verbatim}).
|
|---|
| 1013 | @end itemize
|
|---|
| 1014 |
|
|---|
| 1015 | @cindex comments, in makefile
|
|---|
| 1016 | @cindex @code{#} (comments), in makefile
|
|---|
| 1017 | @item
|
|---|
| 1018 | @samp{#} in a line of a makefile starts a @dfn{comment}. It and the
|
|---|
| 1019 | rest of the line are ignored, except that a trailing backslash not
|
|---|
| 1020 | escaped by another backslash will continue the comment across multiple
|
|---|
| 1021 | lines. A line containing just a comment (with perhaps spaces before
|
|---|
| 1022 | it) is effectively blank, and is ignored. If you want a literal
|
|---|
| 1023 | @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
|
|---|
| 1024 | appear on any line in the makefile, although they are treated
|
|---|
| 1025 | specially in certain situations.
|
|---|
| 1026 |
|
|---|
| 1027 | Within a command script (if the line begins with a TAB character) the
|
|---|
| 1028 | entire line is passed to the shell, just as with any other line that
|
|---|
| 1029 | begins with a TAB. The shell decides how to interpret the text:
|
|---|
| 1030 | whether or not this is a comment is up to the shell.
|
|---|
| 1031 |
|
|---|
| 1032 | Within a @code{define} directive, comments are not ignored during the
|
|---|
| 1033 | definition of the variable, but rather kept intact in the value of the
|
|---|
| 1034 | variable. When the variable is expanded they will either be treated
|
|---|
| 1035 | as @code{make} comments or as command script text, depending on the
|
|---|
| 1036 | context in which the variable is evaluated.
|
|---|
| 1037 | @end itemize
|
|---|
| 1038 |
|
|---|
| 1039 | @node Makefile Names, Include, Makefile Contents, Makefiles
|
|---|
| 1040 | @section What Name to Give Your Makefile
|
|---|
| 1041 | @cindex makefile name
|
|---|
| 1042 | @cindex name of makefile
|
|---|
| 1043 | @cindex default makefile name
|
|---|
| 1044 | @cindex file name of makefile
|
|---|
| 1045 |
|
|---|
| 1046 | @c following paragraph rewritten to avoid overfull hbox
|
|---|
| 1047 | By default, when @code{make} looks for the makefile, it tries the
|
|---|
| 1048 | following names, in order: @file{GNUmakefile}, @file{makefile}
|
|---|
| 1049 | and @file{Makefile}.@refill
|
|---|
| 1050 | @findex Makefile
|
|---|
| 1051 | @findex GNUmakefile
|
|---|
| 1052 | @findex makefile
|
|---|
| 1053 |
|
|---|
| 1054 | @cindex @code{README}
|
|---|
| 1055 | Normally you should call your makefile either @file{makefile} or
|
|---|
| 1056 | @file{Makefile}. (We recommend @file{Makefile} because it appears
|
|---|
| 1057 | prominently near the beginning of a directory listing, right near other
|
|---|
| 1058 | important files such as @file{README}.) The first name checked,
|
|---|
| 1059 | @file{GNUmakefile}, is not recommended for most makefiles. You should
|
|---|
| 1060 | use this name if you have a makefile that is specific to GNU
|
|---|
| 1061 | @code{make}, and will not be understood by other versions of
|
|---|
| 1062 | @code{make}. Other @code{make} programs look for @file{makefile} and
|
|---|
| 1063 | @file{Makefile}, but not @file{GNUmakefile}.
|
|---|
| 1064 |
|
|---|
| 1065 | If @code{make} finds none of these names, it does not use any makefile.
|
|---|
| 1066 | Then you must specify a goal with a command argument, and @code{make}
|
|---|
| 1067 | will attempt to figure out how to remake it using only its built-in
|
|---|
| 1068 | implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 1069 |
|
|---|
| 1070 | @cindex @code{-f}
|
|---|
| 1071 | @cindex @code{--file}
|
|---|
| 1072 | @cindex @code{--makefile}
|
|---|
| 1073 | If you want to use a nonstandard name for your makefile, you can specify
|
|---|
| 1074 | the makefile name with the @samp{-f} or @samp{--file} option. The
|
|---|
| 1075 | arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
|
|---|
| 1076 | @code{make} to read the file @var{name} as the makefile. If you use
|
|---|
| 1077 | more than one @samp{-f} or @samp{--file} option, you can specify several
|
|---|
| 1078 | makefiles. All the makefiles are effectively concatenated in the order
|
|---|
| 1079 | specified. The default makefile names @file{GNUmakefile},
|
|---|
| 1080 | @file{makefile} and @file{Makefile} are not checked automatically if you
|
|---|
| 1081 | specify @samp{-f} or @samp{--file}.@refill
|
|---|
| 1082 | @cindex specifying makefile name
|
|---|
| 1083 | @cindex makefile name, how to specify
|
|---|
| 1084 | @cindex name of makefile, how to specify
|
|---|
| 1085 | @cindex file name of makefile, how to specify
|
|---|
| 1086 |
|
|---|
| 1087 | @node Include, MAKEFILES Variable, Makefile Names, Makefiles
|
|---|
| 1088 | @section Including Other Makefiles
|
|---|
| 1089 | @cindex including other makefiles
|
|---|
| 1090 | @cindex makefile, including
|
|---|
| 1091 |
|
|---|
| 1092 | @findex include
|
|---|
| 1093 | The @code{include} directive tells @code{make} to suspend reading the
|
|---|
| 1094 | current makefile and read one or more other makefiles before continuing.
|
|---|
| 1095 | The directive is a line in the makefile that looks like this:
|
|---|
| 1096 |
|
|---|
| 1097 | @example
|
|---|
| 1098 | include @var{filenames}@dots{}
|
|---|
| 1099 | @end example
|
|---|
| 1100 |
|
|---|
| 1101 | @noindent
|
|---|
| 1102 | @var{filenames} can contain shell file name patterns.
|
|---|
| 1103 | @cindex shell file name pattern (in @code{include})
|
|---|
| 1104 | @cindex shell wildcards (in @code{include})
|
|---|
| 1105 | @cindex wildcard, in @code{include}
|
|---|
| 1106 |
|
|---|
| 1107 | Extra spaces are allowed and ignored at the beginning of the line, but
|
|---|
| 1108 | a tab is not allowed. (If the line begins with a tab, it will be
|
|---|
| 1109 | considered a command line.) Whitespace is required between
|
|---|
| 1110 | @code{include} and the file names, and between file names; extra
|
|---|
| 1111 | whitespace is ignored there and at the end of the directive. A
|
|---|
| 1112 | comment starting with @samp{#} is allowed at the end of the line. If
|
|---|
| 1113 | the file names contain any variable or function references, they are
|
|---|
| 1114 | expanded. @xref{Using Variables, ,How to Use Variables}.
|
|---|
| 1115 |
|
|---|
| 1116 | For example, if you have three @file{.mk} files, @file{a.mk},
|
|---|
| 1117 | @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
|
|---|
| 1118 | @code{bish bash}, then the following expression
|
|---|
| 1119 |
|
|---|
| 1120 | @example
|
|---|
| 1121 | include foo *.mk $(bar)
|
|---|
| 1122 | @end example
|
|---|
| 1123 |
|
|---|
| 1124 | is equivalent to
|
|---|
| 1125 |
|
|---|
| 1126 | @example
|
|---|
| 1127 | include foo a.mk b.mk c.mk bish bash
|
|---|
| 1128 | @end example
|
|---|
| 1129 |
|
|---|
| 1130 | When @code{make} processes an @code{include} directive, it suspends
|
|---|
| 1131 | reading of the containing makefile and reads from each listed file in
|
|---|
| 1132 | turn. When that is finished, @code{make} resumes reading the
|
|---|
| 1133 | makefile in which the directive appears.
|
|---|
| 1134 |
|
|---|
| 1135 | One occasion for using @code{include} directives is when several programs,
|
|---|
| 1136 | handled by individual makefiles in various directories, need to use a
|
|---|
| 1137 | common set of variable definitions
|
|---|
| 1138 | (@pxref{Setting, ,Setting Variables}) or pattern rules
|
|---|
| 1139 | (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
|---|
| 1140 |
|
|---|
| 1141 | Another such occasion is when you want to generate prerequisites from
|
|---|
| 1142 | source files automatically; the prerequisites can be put in a file that
|
|---|
| 1143 | is included by the main makefile. This practice is generally cleaner
|
|---|
| 1144 | than that of somehow appending the prerequisites to the end of the main
|
|---|
| 1145 | makefile as has been traditionally done with other versions of
|
|---|
| 1146 | @code{make}. @xref{Automatic Prerequisites}.
|
|---|
| 1147 | @cindex prerequisites, automatic generation
|
|---|
| 1148 | @cindex automatic generation of prerequisites
|
|---|
| 1149 | @cindex generating prerequisites automatically
|
|---|
| 1150 |
|
|---|
| 1151 | @cindex @code{-I}
|
|---|
| 1152 | @cindex @code{--include-dir}
|
|---|
| 1153 | @cindex included makefiles, default directories
|
|---|
| 1154 | @cindex default directories for included makefiles
|
|---|
| 1155 | @findex /usr/gnu/include
|
|---|
| 1156 | @findex /usr/local/include
|
|---|
| 1157 | @findex /usr/include
|
|---|
| 1158 | If the specified name does not start with a slash, and the file is not
|
|---|
| 1159 | found in the current directory, several other directories are searched.
|
|---|
| 1160 | First, any directories you have specified with the @samp{-I} or
|
|---|
| 1161 | @samp{--include-dir} option are searched
|
|---|
| 1162 | (@pxref{Options Summary, ,Summary of Options}).
|
|---|
| 1163 | Then the following directories (if they exist)
|
|---|
| 1164 | are searched, in this order:
|
|---|
| 1165 | @file{@var{prefix}/include} (normally @file{/usr/local/include}
|
|---|
| 1166 | @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
|
|---|
| 1167 | @var{prefix} has been defined to be the root of the DJGPP tree
|
|---|
| 1168 | hierarchy.})
|
|---|
| 1169 | @file{/usr/gnu/include},
|
|---|
| 1170 | @file{/usr/local/include}, @file{/usr/include}.
|
|---|
| 1171 |
|
|---|
| 1172 | If an included makefile cannot be found in any of these directories, a
|
|---|
| 1173 | warning message is generated, but it is not an immediately fatal error;
|
|---|
| 1174 | processing of the makefile containing the @code{include} continues.
|
|---|
| 1175 | Once it has finished reading makefiles, @code{make} will try to remake
|
|---|
| 1176 | any that are out of date or don't exist.
|
|---|
| 1177 | @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
|---|
| 1178 | Only after it has tried to find a way to remake a makefile and failed,
|
|---|
| 1179 | will @code{make} diagnose the missing makefile as a fatal error.
|
|---|
| 1180 |
|
|---|
| 1181 | If you want @code{make} to simply ignore a makefile which does not exist
|
|---|
| 1182 | and cannot be remade, with no error message, use the @w{@code{-include}}
|
|---|
| 1183 | directive instead of @code{include}, like this:
|
|---|
| 1184 |
|
|---|
| 1185 | @example
|
|---|
| 1186 | -include @var{filenames}@dots{}
|
|---|
| 1187 | @end example
|
|---|
| 1188 |
|
|---|
| 1189 | This acts like @code{include} in every way except that there is no
|
|---|
| 1190 | error (not even a warning) if any of the @var{filenames} do not exist.
|
|---|
| 1191 | For compatibility with some other @code{make} implementations,
|
|---|
| 1192 | @code{sinclude} is another name for @w{@code{-include}}.
|
|---|
| 1193 |
|
|---|
| 1194 | @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
|
|---|
| 1195 | @section The Variable @code{MAKEFILES}
|
|---|
| 1196 | @cindex makefile, and @code{MAKEFILES} variable
|
|---|
| 1197 | @cindex including (@code{MAKEFILES} variable)
|
|---|
| 1198 |
|
|---|
| 1199 | @vindex MAKEFILES
|
|---|
| 1200 | If the environment variable @code{MAKEFILES} is defined, @code{make}
|
|---|
| 1201 | considers its value as a list of names (separated by whitespace) of
|
|---|
| 1202 | additional makefiles to be read before the others. This works much like
|
|---|
| 1203 | the @code{include} directive: various directories are searched for those
|
|---|
| 1204 | files (@pxref{Include, ,Including Other Makefiles}). In addition, the
|
|---|
| 1205 | default goal is never taken from one of these makefiles and it is not an
|
|---|
| 1206 | error if the files listed in @code{MAKEFILES} are not found.@refill
|
|---|
| 1207 |
|
|---|
| 1208 | @cindex recursion, and @code{MAKEFILES} variable
|
|---|
| 1209 | The main use of @code{MAKEFILES} is in communication between recursive
|
|---|
| 1210 | invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
|
|---|
| 1211 | @code{make}}). It usually is not desirable to set the environment
|
|---|
| 1212 | variable before a top-level invocation of @code{make}, because it is
|
|---|
| 1213 | usually better not to mess with a makefile from outside. However, if
|
|---|
| 1214 | you are running @code{make} without a specific makefile, a makefile in
|
|---|
| 1215 | @code{MAKEFILES} can do useful things to help the built-in implicit
|
|---|
| 1216 | rules work better, such as defining search paths (@pxref{Directory Search}).
|
|---|
| 1217 |
|
|---|
| 1218 | Some users are tempted to set @code{MAKEFILES} in the environment
|
|---|
| 1219 | automatically on login, and program makefiles to expect this to be done.
|
|---|
| 1220 | This is a very bad idea, because such makefiles will fail to work if run by
|
|---|
| 1221 | anyone else. It is much better to write explicit @code{include} directives
|
|---|
| 1222 | in the makefiles. @xref{Include, , Including Other Makefiles}.
|
|---|
| 1223 |
|
|---|
| 1224 | @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
|
|---|
| 1225 | @comment node-name, next, previous, up
|
|---|
| 1226 | @section The Variable @code{MAKEFILE_LIST}
|
|---|
| 1227 | @cindex makefiles, and @code{MAKEFILE_LIST} variable
|
|---|
| 1228 | @cindex including (@code{MAKEFILE_LIST} variable)
|
|---|
| 1229 | @vindex MAKEFILE_LIST
|
|---|
| 1230 |
|
|---|
| 1231 | As @code{make} reads various makefiles, including any obtained from the
|
|---|
| 1232 | @code{MAKEFILES} variable, the command line, the default files, or
|
|---|
| 1233 | from @code{include} directives, their names will be automatically
|
|---|
| 1234 | appended to the @code{MAKEFILE_LIST} variable. They are added right
|
|---|
| 1235 | before @code{make} begins to parse them.
|
|---|
| 1236 |
|
|---|
| 1237 | This means that if the first thing a makefile does is examine the last
|
|---|
| 1238 | word in this variable, it will be the name of the current makefile.
|
|---|
| 1239 | Once the current makefile has used @code{include}, however, the last
|
|---|
| 1240 | word will be the just-included makefile.
|
|---|
| 1241 |
|
|---|
| 1242 | If a makefile named @code{Makefile} has this content:
|
|---|
| 1243 |
|
|---|
| 1244 | @example
|
|---|
| 1245 | @group
|
|---|
| 1246 | name1 := $(lastword $(MAKEFILE_LIST))
|
|---|
| 1247 |
|
|---|
| 1248 | include inc.mk
|
|---|
| 1249 |
|
|---|
| 1250 | name2 := $(lastword $(MAKEFILE_LIST))
|
|---|
| 1251 |
|
|---|
| 1252 | all:
|
|---|
| 1253 | @@echo name1 = $(name1)
|
|---|
| 1254 | @@echo name2 = $(name2)
|
|---|
| 1255 | @end group
|
|---|
| 1256 | @end example
|
|---|
| 1257 |
|
|---|
| 1258 | @noindent
|
|---|
| 1259 | then you would expect to see this output:
|
|---|
| 1260 |
|
|---|
| 1261 | @example
|
|---|
| 1262 | @group
|
|---|
| 1263 | name1 = Makefile
|
|---|
| 1264 | name2 = inc.mk
|
|---|
| 1265 | @end group
|
|---|
| 1266 | @end example
|
|---|
| 1267 |
|
|---|
| 1268 | @xref{Text Functions}, for more information on the @code{word} and
|
|---|
| 1269 | @code{words} functions used above. @xref{Flavors, The Two Flavors of
|
|---|
| 1270 | Variables}, for more information on simply-expanded (@code{:=})
|
|---|
| 1271 | variable definitions.
|
|---|
| 1272 |
|
|---|
| 1273 | @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
|
|---|
| 1274 | @comment node-name, next, previous, up
|
|---|
| 1275 | @section Other Special Variables
|
|---|
| 1276 | @cindex makefiles, and special variables
|
|---|
| 1277 | @cindex special variables
|
|---|
| 1278 |
|
|---|
| 1279 | GNU @code{make} also supports other special variables. Unless
|
|---|
| 1280 | otherwise documented here, these values lose their special properties
|
|---|
| 1281 | if they are set by a makefile or on the command line.
|
|---|
| 1282 |
|
|---|
| 1283 | @table @code
|
|---|
| 1284 |
|
|---|
| 1285 | @vindex .DEFAULT_GOAL @r{(define default goal)}
|
|---|
| 1286 | @item .DEFAULT_GOAL
|
|---|
| 1287 | Sets the default goal to be used if no targets were specified on the
|
|---|
| 1288 | command line (@pxref{Goals, , Arguments to Specify the Goals}). The
|
|---|
| 1289 | @code{.DEFAULT_GOAL} variable allows you to discover the current
|
|---|
| 1290 | default goal, restart the default goal selection algorithm by clearing
|
|---|
| 1291 | its value, or to explicitly set the default goal. The following
|
|---|
| 1292 | example illustrates these cases:
|
|---|
| 1293 |
|
|---|
| 1294 | @example
|
|---|
| 1295 | @group
|
|---|
| 1296 | # Query the default goal.
|
|---|
| 1297 | ifeq ($(.DEFAULT_GOAL),)
|
|---|
| 1298 | $(warning no default goal is set)
|
|---|
| 1299 | endif
|
|---|
| 1300 |
|
|---|
| 1301 | .PHONY: foo
|
|---|
| 1302 | foo: ; @@echo $@@
|
|---|
| 1303 |
|
|---|
| 1304 | $(warning default goal is $(.DEFAULT_GOAL))
|
|---|
| 1305 |
|
|---|
| 1306 | # Reset the default goal.
|
|---|
| 1307 | .DEFAULT_GOAL :=
|
|---|
| 1308 |
|
|---|
| 1309 | .PHONY: bar
|
|---|
| 1310 | bar: ; @@echo $@@
|
|---|
| 1311 |
|
|---|
| 1312 | $(warning default goal is $(.DEFAULT_GOAL))
|
|---|
| 1313 |
|
|---|
| 1314 | # Set our own.
|
|---|
| 1315 | .DEFAULT_GOAL := foo
|
|---|
| 1316 | @end group
|
|---|
| 1317 | @end example
|
|---|
| 1318 |
|
|---|
| 1319 | This makefile prints:
|
|---|
| 1320 |
|
|---|
| 1321 | @example
|
|---|
| 1322 | @group
|
|---|
| 1323 | no default goal is set
|
|---|
| 1324 | default goal is foo
|
|---|
| 1325 | default goal is bar
|
|---|
| 1326 | foo
|
|---|
| 1327 | @end group
|
|---|
| 1328 | @end example
|
|---|
| 1329 |
|
|---|
| 1330 | Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
|
|---|
| 1331 | illegal and will result in an error.
|
|---|
| 1332 |
|
|---|
| 1333 | @vindex .VARIABLES @r{(list of variables)}
|
|---|
| 1334 | @item .VARIABLES
|
|---|
| 1335 | Expands to a list of the @emph{names} of all global variables defined
|
|---|
| 1336 | so far. This includes variables which have empty values, as well as
|
|---|
| 1337 | built-in variables (@pxref{Implicit Variables, , Variables Used by
|
|---|
| 1338 | Implicit Rules}), but does not include any variables which are only
|
|---|
| 1339 | defined in a target-specific context. Note that any value you assign
|
|---|
| 1340 | to this variable will be ignored; it will always return its special
|
|---|
| 1341 | value.
|
|---|
| 1342 |
|
|---|
| 1343 | @c @vindex .TARGETS @r{(list of targets)}
|
|---|
| 1344 | @c @item .TARGETS
|
|---|
| 1345 | @c The second special variable is @code{.TARGETS}. When expanded, the
|
|---|
| 1346 | @c value consists of a list of all targets defined in all makefiles read
|
|---|
| 1347 | @c up until that point. Note it's not enough for a file to be simply
|
|---|
| 1348 | @c mentioned in the makefile to be listed in this variable, even if it
|
|---|
| 1349 | @c would match an implicit rule and become an ``implicit target''. The
|
|---|
| 1350 | @c file must appear as a target, on the left-hand side of a ``:'', to be
|
|---|
| 1351 | @c considered a target for the purposes of this variable.
|
|---|
| 1352 |
|
|---|
| 1353 | @vindex .FEATURES @r{(list of supported features)}
|
|---|
| 1354 | @item .FEATURES
|
|---|
| 1355 | Expands to a list of special features supported by this version of
|
|---|
| 1356 | @code{make}. Possible values include:
|
|---|
| 1357 |
|
|---|
| 1358 | @table @samp
|
|---|
| 1359 | @item target-specific
|
|---|
| 1360 | Supports target-specific and pattern-specific variable assignments.
|
|---|
| 1361 | @xref{Target-specific, ,Target-specific Variable Values}.
|
|---|
| 1362 |
|
|---|
| 1363 | @item order-only
|
|---|
| 1364 | Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
|
|---|
| 1365 | of Prerequisites}.
|
|---|
| 1366 |
|
|---|
| 1367 | @item second-expansion
|
|---|
| 1368 | Supports secondary expansion of prerequisite lists.
|
|---|
| 1369 |
|
|---|
| 1370 | @item jobserver
|
|---|
| 1371 | Supports ``job server'' enhanced parallel builds. @xref{Parallel,
|
|---|
| 1372 | ,Parallel Execution}.
|
|---|
| 1373 |
|
|---|
| 1374 | @item check-symlink
|
|---|
| 1375 | Supports the @code{-L} (@code{--check-symlink-times}) flag.
|
|---|
| 1376 | @xref{Options Summary, ,Summary of Options}.
|
|---|
| 1377 |
|
|---|
| 1378 | @end table
|
|---|
| 1379 |
|
|---|
| 1380 | @end table
|
|---|
| 1381 |
|
|---|
| 1382 | @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
|
|---|
| 1383 | @section How Makefiles Are Remade
|
|---|
| 1384 |
|
|---|
| 1385 | @cindex updating makefiles
|
|---|
| 1386 | @cindex remaking makefiles
|
|---|
| 1387 | @cindex makefile, remaking of
|
|---|
| 1388 | Sometimes makefiles can be remade from other files, such as RCS or SCCS
|
|---|
| 1389 | files. If a makefile can be remade from other files, you probably want
|
|---|
| 1390 | @code{make} to get an up-to-date version of the makefile to read in.
|
|---|
| 1391 |
|
|---|
| 1392 | To this end, after reading in all makefiles, @code{make} will consider
|
|---|
| 1393 | each as a goal target and attempt to update it. If a makefile has a
|
|---|
| 1394 | rule which says how to update it (found either in that very makefile or
|
|---|
| 1395 | in another one) or if an implicit rule applies to it (@pxref{Implicit
|
|---|
| 1396 | Rules, ,Using Implicit Rules}), it will be updated if necessary. After
|
|---|
| 1397 | all makefiles have been checked, if any have actually been changed,
|
|---|
| 1398 | @code{make} starts with a clean slate and reads all the makefiles over
|
|---|
| 1399 | again. (It will also attempt to update each of them over again, but
|
|---|
| 1400 | normally this will not change them again, since they are already up to
|
|---|
| 1401 | date.)@refill
|
|---|
| 1402 |
|
|---|
| 1403 | If you know that one or more of your makefiles cannot be remade and you
|
|---|
| 1404 | want to keep @code{make} from performing an implicit rule search on
|
|---|
| 1405 | them, perhaps for efficiency reasons, you can use any normal method of
|
|---|
| 1406 | preventing implicit rule lookup to do so. For example, you can write an
|
|---|
| 1407 | explicit rule with the makefile as the target, and an empty command
|
|---|
| 1408 | string (@pxref{Empty Commands, ,Using Empty Commands}).
|
|---|
| 1409 |
|
|---|
| 1410 | If the makefiles specify a double-colon rule to remake a file with
|
|---|
| 1411 | commands but no prerequisites, that file will always be remade
|
|---|
| 1412 | (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
|
|---|
| 1413 | double-colon rule with commands but no prerequisites will be remade every
|
|---|
| 1414 | time @code{make} is run, and then again after @code{make} starts over
|
|---|
| 1415 | and reads the makefiles in again. This would cause an infinite loop:
|
|---|
| 1416 | @code{make} would constantly remake the makefile, and never do anything
|
|---|
| 1417 | else. So, to avoid this, @code{make} will @strong{not} attempt to
|
|---|
| 1418 | remake makefiles which are specified as targets of a double-colon rule
|
|---|
| 1419 | with commands but no prerequisites.@refill
|
|---|
| 1420 |
|
|---|
| 1421 | If you do not specify any makefiles to be read with @samp{-f} or
|
|---|
| 1422 | @samp{--file} options, @code{make} will try the default makefile names;
|
|---|
| 1423 | @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
|
|---|
| 1424 | makefiles explicitly requested with @samp{-f} or @samp{--file} options,
|
|---|
| 1425 | @code{make} is not certain that these makefiles should exist. However,
|
|---|
| 1426 | if a default makefile does not exist but can be created by running
|
|---|
| 1427 | @code{make} rules, you probably want the rules to be run so that the
|
|---|
| 1428 | makefile can be used.
|
|---|
| 1429 |
|
|---|
| 1430 | Therefore, if none of the default makefiles exists, @code{make} will try
|
|---|
| 1431 | to make each of them in the same order in which they are searched for
|
|---|
| 1432 | (@pxref{Makefile Names, ,What Name to Give Your Makefile})
|
|---|
| 1433 | until it succeeds in making one, or it runs out of names to try. Note
|
|---|
| 1434 | that it is not an error if @code{make} cannot find or make any makefile;
|
|---|
| 1435 | a makefile is not always necessary.@refill
|
|---|
| 1436 |
|
|---|
| 1437 | When you use the @samp{-t} or @samp{--touch} option
|
|---|
| 1438 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
|
|---|
| 1439 | you would not want to use an out-of-date makefile to decide which
|
|---|
| 1440 | targets to touch. So the @samp{-t} option has no effect on updating
|
|---|
| 1441 | makefiles; they are really updated even if @samp{-t} is specified.
|
|---|
| 1442 | Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
|
|---|
| 1443 | @samp{--just-print}) do not prevent updating of makefiles, because an
|
|---|
| 1444 | out-of-date makefile would result in the wrong output for other targets.
|
|---|
| 1445 | Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
|
|---|
| 1446 | and then print the commands to update @file{foo} and its prerequisites
|
|---|
| 1447 | without running them. The commands printed for @file{foo} will be those
|
|---|
| 1448 | specified in the updated contents of @file{mfile}.
|
|---|
| 1449 |
|
|---|
| 1450 | However, on occasion you might actually wish to prevent updating of even
|
|---|
| 1451 | the makefiles. You can do this by specifying the makefiles as goals in
|
|---|
| 1452 | the command line as well as specifying them as makefiles. When the
|
|---|
| 1453 | makefile name is specified explicitly as a goal, the options @samp{-t}
|
|---|
| 1454 | and so on do apply to them.
|
|---|
| 1455 |
|
|---|
| 1456 | Thus, @samp{make -f mfile -n mfile foo} would read the makefile
|
|---|
| 1457 | @file{mfile}, print the commands needed to update it without actually
|
|---|
| 1458 | running them, and then print the commands needed to update @file{foo}
|
|---|
| 1459 | without running them. The commands for @file{foo} will be those
|
|---|
| 1460 | specified by the existing contents of @file{mfile}.
|
|---|
| 1461 |
|
|---|
| 1462 | @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
|
|---|
| 1463 | @section Overriding Part of Another Makefile
|
|---|
| 1464 |
|
|---|
| 1465 | @cindex overriding makefiles
|
|---|
| 1466 | @cindex makefile, overriding
|
|---|
| 1467 | Sometimes it is useful to have a makefile that is mostly just like
|
|---|
| 1468 | another makefile. You can often use the @samp{include} directive to
|
|---|
| 1469 | include one in the other, and add more targets or variable definitions.
|
|---|
| 1470 | However, if the two makefiles give different commands for the same
|
|---|
| 1471 | target, @code{make} will not let you just do this. But there is another way.
|
|---|
| 1472 |
|
|---|
| 1473 | @cindex match-anything rule, used to override
|
|---|
| 1474 | In the containing makefile (the one that wants to include the other),
|
|---|
| 1475 | you can use a match-anything pattern rule to say that to remake any
|
|---|
| 1476 | target that cannot be made from the information in the containing
|
|---|
| 1477 | makefile, @code{make} should look in another makefile.
|
|---|
| 1478 | @xref{Pattern Rules}, for more information on pattern rules.
|
|---|
| 1479 |
|
|---|
| 1480 | For example, if you have a makefile called @file{Makefile} that says how
|
|---|
| 1481 | to make the target @samp{foo} (and other targets), you can write a
|
|---|
| 1482 | makefile called @file{GNUmakefile} that contains:
|
|---|
| 1483 |
|
|---|
| 1484 | @example
|
|---|
| 1485 | foo:
|
|---|
| 1486 | frobnicate > foo
|
|---|
| 1487 |
|
|---|
| 1488 | %: force
|
|---|
| 1489 | @@$(MAKE) -f Makefile $@@
|
|---|
| 1490 | force: ;
|
|---|
| 1491 | @end example
|
|---|
| 1492 |
|
|---|
| 1493 | If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
|
|---|
| 1494 | read it, and see that to make @file{foo}, it needs to run the command
|
|---|
| 1495 | @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
|
|---|
| 1496 | find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
|
|---|
| 1497 | commands from the pattern rule: @samp{make -f Makefile bar}. If
|
|---|
| 1498 | @file{Makefile} provides a rule for updating @file{bar}, @code{make}
|
|---|
| 1499 | will apply the rule. And likewise for any other target that
|
|---|
| 1500 | @file{GNUmakefile} does not say how to make.
|
|---|
| 1501 |
|
|---|
| 1502 | The way this works is that the pattern rule has a pattern of just
|
|---|
| 1503 | @samp{%}, so it matches any target whatever. The rule specifies a
|
|---|
| 1504 | prerequisite @file{force}, to guarantee that the commands will be run even
|
|---|
| 1505 | if the target file already exists. We give @file{force} target empty
|
|---|
| 1506 | commands to prevent @code{make} from searching for an implicit rule to
|
|---|
| 1507 | build it---otherwise it would apply the same match-anything rule to
|
|---|
| 1508 | @file{force} itself and create a prerequisite loop!
|
|---|
| 1509 |
|
|---|
| 1510 | @node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
|
|---|
| 1511 | @section How @code{make} Reads a Makefile
|
|---|
| 1512 | @cindex reading makefiles
|
|---|
| 1513 | @cindex makefile, parsing
|
|---|
| 1514 |
|
|---|
| 1515 | GNU @code{make} does its work in two distinct phases. During the first
|
|---|
| 1516 | phase it reads all the makefiles, included makefiles, etc. and
|
|---|
| 1517 | internalizes all the variables and their values, implicit and explicit
|
|---|
| 1518 | rules, and constructs a dependency graph of all the targets and their
|
|---|
| 1519 | prerequisites. During the second phase, @code{make} uses these internal
|
|---|
| 1520 | structures to determine what targets will need to be rebuilt and to
|
|---|
| 1521 | invoke the rules necessary to do so.
|
|---|
| 1522 |
|
|---|
| 1523 | It's important to understand this two-phase approach because it has a
|
|---|
| 1524 | direct impact on how variable and function expansion happens; this is
|
|---|
| 1525 | often a source of some confusion when writing makefiles. Here we will
|
|---|
| 1526 | present a summary of the phases in which expansion happens for different
|
|---|
| 1527 | constructs within the makefile. We say that expansion is
|
|---|
| 1528 | @dfn{immediate} if it happens during the first phase: in this case
|
|---|
| 1529 | @code{make} will expand any variables or functions in that section of a
|
|---|
| 1530 | construct as the makefile is parsed. We say that expansion is
|
|---|
| 1531 | @dfn{deferred} if expansion is not performed immediately. Expansion of
|
|---|
| 1532 | deferred construct is not performed until either the construct appears
|
|---|
| 1533 | later in an immediate context, or until the second phase.
|
|---|
| 1534 |
|
|---|
| 1535 | You may not be familiar with some of these constructs yet. You can
|
|---|
| 1536 | reference this section as you become familiar with them, in later
|
|---|
| 1537 | chapters.
|
|---|
| 1538 |
|
|---|
| 1539 | @subheading Variable Assignment
|
|---|
| 1540 | @cindex +=, expansion
|
|---|
| 1541 | @cindex =, expansion
|
|---|
| 1542 | @cindex ?=, expansion
|
|---|
| 1543 | @cindex +=, expansion
|
|---|
| 1544 | @cindex define, expansion
|
|---|
| 1545 |
|
|---|
| 1546 | Variable definitions are parsed as follows:
|
|---|
| 1547 |
|
|---|
| 1548 | @example
|
|---|
| 1549 | @var{immediate} = @var{deferred}
|
|---|
| 1550 | @var{immediate} ?= @var{deferred}
|
|---|
| 1551 | @var{immediate} := @var{immediate}
|
|---|
| 1552 | @var{immediate} += @var{deferred} or @var{immediate}
|
|---|
| 1553 |
|
|---|
| 1554 | define @var{immediate}
|
|---|
| 1555 | @var{deferred}
|
|---|
| 1556 | endef
|
|---|
| 1557 | @end example
|
|---|
| 1558 |
|
|---|
| 1559 | For the append operator, @samp{+=}, the right-hand side is considered
|
|---|
| 1560 | immediate if the variable was previously set as a simple variable
|
|---|
| 1561 | (@samp{:=}), and deferred otherwise.
|
|---|
| 1562 |
|
|---|
| 1563 | @subheading Conditional Statements
|
|---|
| 1564 | @cindex ifdef, expansion
|
|---|
| 1565 | @cindex ifeq, expansion
|
|---|
| 1566 | @cindex ifndef, expansion
|
|---|
| 1567 | @cindex ifneq, expansion
|
|---|
| 1568 |
|
|---|
| 1569 | All instances of conditional syntax are parsed immediately, in their
|
|---|
| 1570 | entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
|
|---|
| 1571 | and @code{ifneq} forms. Of course this means that automatic variables
|
|---|
| 1572 | cannot be used in conditional statements, as automatic variables are
|
|---|
| 1573 | not set until the command script for that rule is invoked. If you
|
|---|
| 1574 | need to use automatic variables in a conditional you @emph{must} use
|
|---|
| 1575 | shell conditional syntax, in your command script proper, for these
|
|---|
| 1576 | tests, not @code{make} conditionals.
|
|---|
| 1577 |
|
|---|
| 1578 | @subheading Rule Definition
|
|---|
| 1579 | @cindex target, expansion
|
|---|
| 1580 | @cindex prerequisite, expansion
|
|---|
| 1581 | @cindex implicit rule, expansion
|
|---|
| 1582 | @cindex pattern rule, expansion
|
|---|
| 1583 | @cindex explicit rule, expansion
|
|---|
| 1584 |
|
|---|
| 1585 | A rule is always expanded the same way, regardless of the form:
|
|---|
| 1586 |
|
|---|
| 1587 | @example
|
|---|
| 1588 | @var{immediate} : @var{immediate} ; @var{deferred}
|
|---|
| 1589 | @var{deferred}
|
|---|
| 1590 | @end example
|
|---|
| 1591 |
|
|---|
| 1592 | That is, the target and prerequisite sections are expanded immediately,
|
|---|
| 1593 | and the commands used to construct the target are always deferred. This
|
|---|
| 1594 | general rule is true for explicit rules, pattern rules, suffix rules,
|
|---|
| 1595 | static pattern rules, and simple prerequisite definitions.
|
|---|
| 1596 |
|
|---|
| 1597 | @node Secondary Expansion, , Reading Makefiles, Makefiles
|
|---|
| 1598 | @section Secondary Expansion
|
|---|
| 1599 | @cindex secondary expansion
|
|---|
| 1600 | @cindex expansion, secondary
|
|---|
| 1601 |
|
|---|
| 1602 | In the previous section we learned that GNU @code{make} works in two
|
|---|
| 1603 | distinct phases: a read-in phase and a target-update phase
|
|---|
| 1604 | (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).
|
|---|
| 1605 | There is an extra wrinkle that comes in between those two phases,
|
|---|
| 1606 | right at the end of the read-in phase: at that time, all the
|
|---|
| 1607 | prerequisites of all of the targets are expanded a @emph{second time}.
|
|---|
| 1608 | In most circumstances this secondary expansion will have no effect,
|
|---|
| 1609 | since all variable and function references will have been expanded
|
|---|
| 1610 | during the initial parsing of the makefiles. In order to take
|
|---|
| 1611 | advantage of the secondary expansion phase of the parser, then, it's
|
|---|
| 1612 | necessary to @emph{escape} the variable or function reference in the
|
|---|
| 1613 | makefile. In this case the first expansion merely un-escapes the
|
|---|
| 1614 | reference but doesn't expand it, and expansion is left to the
|
|---|
| 1615 | secondary expansion phase. For example, consider this makefile:
|
|---|
| 1616 |
|
|---|
| 1617 | @example
|
|---|
| 1618 | ONEVAR = onefile
|
|---|
| 1619 | TWOVAR = twofile
|
|---|
| 1620 | myfile: $(ONEVAR) $$(TWOVAR)
|
|---|
| 1621 | @end example
|
|---|
| 1622 |
|
|---|
| 1623 | After the first expansion phase the prerequisites list of the
|
|---|
| 1624 | @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
|
|---|
| 1625 | first (unescaped) variable reference to @var{ONEVAR} is expanded,
|
|---|
| 1626 | while the second (escaped) variable reference is simply unescaped,
|
|---|
| 1627 | without being recognized as a variable reference. Now during the
|
|---|
| 1628 | secondary expansion the first word is expanded again but since it
|
|---|
| 1629 | contains no variable or function references it remains the static
|
|---|
| 1630 | value @file{onefile}, while the second word is now a normal reference
|
|---|
| 1631 | to the variable @var{TWOVAR}, which is expanded to the value
|
|---|
| 1632 | @file{twofile}. The final result is that there are two prerequisites,
|
|---|
| 1633 | @file{onefile} and @file{twofile}.
|
|---|
| 1634 |
|
|---|
| 1635 | Obviously, this is not a very interesting case since the same result
|
|---|
| 1636 | could more easily have been achieved simply by having both variables
|
|---|
| 1637 | appear, unescaped, in the prerequisites list. One difference becomes
|
|---|
| 1638 | apparent if the variables are reset; consider this example:
|
|---|
| 1639 |
|
|---|
| 1640 | @example
|
|---|
| 1641 | AVAR = top
|
|---|
| 1642 | onefile: $(AVAR)
|
|---|
| 1643 | twofile: $$(AVAR)
|
|---|
| 1644 | AVAR = bottom
|
|---|
| 1645 | @end example
|
|---|
| 1646 |
|
|---|
| 1647 | Here the prerequisite of @file{onefile} will be expanded immediately,
|
|---|
| 1648 | and resolve to the value @file{top}, while the prerequisite of
|
|---|
| 1649 | @file{twofile} will not be full expanded until the secondary expansion
|
|---|
| 1650 | and yield a value of @file{bottom}.
|
|---|
| 1651 |
|
|---|
| 1652 | This is marginally more exciting, but the true power of this feature
|
|---|
| 1653 | only becomes apparent when you discover that secondary expansions
|
|---|
| 1654 | always take place within the scope of the automatic variables for that
|
|---|
| 1655 | target. This means that you can use variables such as @code{$@@},
|
|---|
| 1656 | @code{$*}, etc. during the second expansion and they will have their
|
|---|
| 1657 | expected values, just as in the command script. All you have to do is
|
|---|
| 1658 | defer the expansion by escaping the @code{$}. Also, secondary
|
|---|
| 1659 | expansion occurs for both explicit and implicit (pattern) rules.
|
|---|
| 1660 | Knowing this, the possible uses for this feature are almost endless.
|
|---|
| 1661 | For example:
|
|---|
| 1662 |
|
|---|
| 1663 | @example
|
|---|
| 1664 | main_OBJS := main.o try.o test.o
|
|---|
| 1665 | lib_OBJS := lib.o api.o
|
|---|
| 1666 |
|
|---|
| 1667 | main lib: $$($$@@_OBJS)
|
|---|
| 1668 | @end example
|
|---|
| 1669 |
|
|---|
| 1670 | Here, after the initial expansion the prerequisites of both the
|
|---|
| 1671 | @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
|
|---|
| 1672 | the secondary expansion, the @code{$@@} variable is set to the name of
|
|---|
| 1673 | the target and so the expansion for the @file{main} target will yield
|
|---|
| 1674 | @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
|
|---|
| 1675 | secondary expansion for the @file{lib} target will yield
|
|---|
| 1676 | @code{$(lib_OBJS)}, or @code{lib.o api.o}.
|
|---|
| 1677 |
|
|---|
| 1678 | You can also mix functions here, as long as they are properly escaped:
|
|---|
| 1679 |
|
|---|
| 1680 | @example
|
|---|
| 1681 | main_SRCS := main.c try.c test.c
|
|---|
| 1682 | lib_SRCS := lib.c api.c
|
|---|
| 1683 |
|
|---|
| 1684 | main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
|
|---|
| 1685 | @end example
|
|---|
| 1686 |
|
|---|
| 1687 | This version allows users to specify source files rather than object
|
|---|
| 1688 | files, but gives the same resulting prerequisites list as the previous
|
|---|
| 1689 | example.
|
|---|
| 1690 |
|
|---|
| 1691 | Evaluation of automatic variables during the secondary expansion
|
|---|
| 1692 | phase, especially of the target name variable @code{$$@@}, behaves
|
|---|
| 1693 | similarly to evaluation within command scripts. However, there are
|
|---|
| 1694 | some subtle differences and ``corner cases'' which come into play for
|
|---|
| 1695 | the different types of rule definitions that @code{make} understands.
|
|---|
| 1696 | The subtleties of using the different automatic variables are
|
|---|
| 1697 | described below.
|
|---|
| 1698 |
|
|---|
| 1699 | @subheading Secondary Expansion of Explicit Rules
|
|---|
| 1700 | @cindex secondary expansion and explicit rules
|
|---|
| 1701 | @cindex explicit rules, secondary expansion of
|
|---|
| 1702 |
|
|---|
| 1703 | During the secondary expansion of explicit rules, @code{$$@@} and
|
|---|
| 1704 | @code{$$%} evaluate, respectively, to the file name of the target and,
|
|---|
| 1705 | when the target is an archive member, the target member name. The
|
|---|
| 1706 | @code{$$<} variable evaluates to the first prerequisite in the first
|
|---|
| 1707 | rule for this target. @code{$$^} and @code{$$+} evaluate to the list
|
|---|
| 1708 | of all prerequisites of rules @emph{that have already appeared} for
|
|---|
| 1709 | the same target (@code{$$+} with repetitions and @code{$$^}
|
|---|
| 1710 | without). The following example will help illustrate these behaviors:
|
|---|
| 1711 |
|
|---|
| 1712 | @example
|
|---|
| 1713 | foo: foo.1 bar.1 $$< $$^ $$+ # line #1
|
|---|
| 1714 |
|
|---|
| 1715 | foo: foo.2 bar.2 $$< $$^ $$+ # line #2
|
|---|
| 1716 |
|
|---|
| 1717 | foo: foo.3 bar.3 $$< $$^ $$+ # line #3
|
|---|
| 1718 | @end example
|
|---|
| 1719 |
|
|---|
| 1720 | For the first line, all three variables (@code{$$<}, @code{$$^}, and
|
|---|
| 1721 | @code{$$+}) expand to the empty string. For the second line, they will
|
|---|
| 1722 | have values @code{foo.1}, @code{foo.1 bar.1}, and @code{foo.1 bar.1}
|
|---|
| 1723 | respectively. For the third they will have values @code{foo.1},
|
|---|
| 1724 | @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1 foo.2 bar.2}
|
|---|
| 1725 | respectively.
|
|---|
| 1726 |
|
|---|
| 1727 | Rules undergo secondary expansion in makefile order, except that
|
|---|
| 1728 | the rule with the command script is always evaluated last.
|
|---|
| 1729 |
|
|---|
| 1730 | The variables @code{$$?} and @code{$$*} are not available and expand
|
|---|
| 1731 | to the empty string.
|
|---|
| 1732 |
|
|---|
| 1733 | @subheading Secondary Expansion of Static Pattern Rules
|
|---|
| 1734 | @cindex secondary expansion and static pattern rules
|
|---|
| 1735 | @cindex static pattern rules, secondary expansion of
|
|---|
| 1736 |
|
|---|
| 1737 | Rules for secondary expansion of static pattern rules are identical to
|
|---|
| 1738 | those for explicit rules, above, with one exception: for static
|
|---|
| 1739 | pattern rules the @code{$$*} variable is set to the pattern stem. As
|
|---|
| 1740 | with explicit rules, @code{$$?} is not available and expands to the
|
|---|
| 1741 | empty string.
|
|---|
| 1742 |
|
|---|
| 1743 | @subheading Secondary Expansion of Implicit Rules
|
|---|
| 1744 | @cindex secondary expansion and implicit rules
|
|---|
| 1745 | @cindex implicit rules, secondary expansion of
|
|---|
| 1746 |
|
|---|
| 1747 | As @code{make} searches for an implicit rule, it substitutes the stem
|
|---|
| 1748 | and then performs secondary expansion for every rule with a matching
|
|---|
| 1749 | target pattern. The value of the automatic variables is derived in
|
|---|
| 1750 | the same fashion as for static pattern rules. As an example:
|
|---|
| 1751 |
|
|---|
| 1752 | @example
|
|---|
| 1753 | foo: bar
|
|---|
| 1754 |
|
|---|
| 1755 | foo foz: fo%: bo%
|
|---|
| 1756 |
|
|---|
| 1757 | %oo: $$< $$^ $$+ $$*
|
|---|
| 1758 | @end example
|
|---|
| 1759 |
|
|---|
| 1760 | When the implicit rule is tried for target @file{foo}, @code{$$<}
|
|---|
| 1761 | expands to @file{bar}, @code{$$^} expands to @file{bar boo},
|
|---|
| 1762 | @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
|
|---|
| 1763 | @file{f}.
|
|---|
| 1764 |
|
|---|
| 1765 | Note that the directory prefix (D), as described in @ref{Implicit Rule
|
|---|
| 1766 | Search, ,Implicit Rule Search Algorithm}, is appended (after
|
|---|
| 1767 | expansion) to all the patterns in the prerequisites list. As an
|
|---|
| 1768 | example:
|
|---|
| 1769 |
|
|---|
| 1770 | @example
|
|---|
| 1771 | /tmp/foo.o:
|
|---|
| 1772 |
|
|---|
| 1773 | %.o: $$(addsuffix /%.c,foo bar) foo.h
|
|---|
| 1774 | @end example
|
|---|
| 1775 |
|
|---|
| 1776 | The prerequisite list after the secondary expansion and directory
|
|---|
| 1777 | prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
|
|---|
| 1778 | foo.h}. If you are not interested in this reconstruction, you can use
|
|---|
| 1779 | @code{$$*} instead of @code{%} in the prerequisites list.
|
|---|
| 1780 |
|
|---|
| 1781 | @node Rules, Commands, Makefiles, Top
|
|---|
| 1782 | @chapter Writing Rules
|
|---|
| 1783 | @cindex writing rules
|
|---|
| 1784 | @cindex rule, how to write
|
|---|
| 1785 | @cindex target
|
|---|
| 1786 | @cindex prerequisite
|
|---|
| 1787 |
|
|---|
| 1788 | A @dfn{rule} appears in the makefile and says when and how to remake
|
|---|
| 1789 | certain files, called the rule's @dfn{targets} (most often only one per rule).
|
|---|
| 1790 | It lists the other files that are the @dfn{prerequisites} of the target, and
|
|---|
| 1791 | @dfn{commands} to use to create or update the target.
|
|---|
| 1792 |
|
|---|
| 1793 | @cindex default goal
|
|---|
| 1794 | @cindex goal, default
|
|---|
| 1795 | The order of rules is not significant, except for determining the
|
|---|
| 1796 | @dfn{default goal}: the target for @code{make} to consider, if you do
|
|---|
| 1797 | not otherwise specify one. The default goal is the target of the first
|
|---|
| 1798 | rule in the first makefile. If the first rule has multiple targets,
|
|---|
| 1799 | only the first target is taken as the default. There are two
|
|---|
| 1800 | exceptions: a target starting with a period is not a default unless it
|
|---|
| 1801 | contains one or more slashes, @samp{/}, as well; and, a target that
|
|---|
| 1802 | defines a pattern rule has no effect on the default goal.
|
|---|
| 1803 | (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
|
|---|
| 1804 |
|
|---|
| 1805 | Therefore, we usually write the makefile so that the first rule is the
|
|---|
| 1806 | one for compiling the entire program or all the programs described by
|
|---|
| 1807 | the makefile (often with a target called @samp{all}).
|
|---|
| 1808 | @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 1809 |
|
|---|
| 1810 | @menu
|
|---|
| 1811 | * Rule Example:: An example explained.
|
|---|
| 1812 | * Rule Syntax:: General syntax explained.
|
|---|
| 1813 | * Prerequisite Types:: There are two types of prerequisites.
|
|---|
| 1814 | * Wildcards:: Using wildcard characters such as `*'.
|
|---|
| 1815 | * Directory Search:: Searching other directories for source files.
|
|---|
| 1816 | * Phony Targets:: Using a target that is not a real file's name.
|
|---|
| 1817 | * Force Targets:: You can use a target without commands
|
|---|
| 1818 | or prerequisites to mark other
|
|---|
| 1819 | targets as phony.
|
|---|
| 1820 | * Empty Targets:: When only the date matters and the
|
|---|
| 1821 | files are empty.
|
|---|
| 1822 | * Special Targets:: Targets with special built-in meanings.
|
|---|
| 1823 | * Multiple Targets:: When to make use of several targets in a rule.
|
|---|
| 1824 | * Multiple Rules:: How to use several rules with the same target.
|
|---|
| 1825 | * Static Pattern:: Static pattern rules apply to multiple targets
|
|---|
| 1826 | and can vary the prerequisites according to
|
|---|
| 1827 | the target name.
|
|---|
| 1828 | * Double-Colon:: How to use a special kind of rule to allow
|
|---|
| 1829 | several independent rules for one target.
|
|---|
| 1830 | * Automatic Prerequisites:: How to automatically generate rules giving
|
|---|
| 1831 | prerequisites from source files themselves.
|
|---|
| 1832 | @end menu
|
|---|
| 1833 |
|
|---|
| 1834 | @ifnottex
|
|---|
| 1835 | @node Rule Example, Rule Syntax, Rules, Rules
|
|---|
| 1836 | @section Rule Example
|
|---|
| 1837 |
|
|---|
| 1838 | Here is an example of a rule:
|
|---|
| 1839 |
|
|---|
| 1840 | @example
|
|---|
| 1841 | foo.o : foo.c defs.h # module for twiddling the frobs
|
|---|
| 1842 | cc -c -g foo.c
|
|---|
| 1843 | @end example
|
|---|
| 1844 |
|
|---|
| 1845 | Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
|
|---|
| 1846 | @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
|
|---|
| 1847 | The command line starts with a tab to identify it as a command.
|
|---|
| 1848 |
|
|---|
| 1849 | This rule says two things:
|
|---|
| 1850 |
|
|---|
| 1851 | @itemize @bullet
|
|---|
| 1852 | @item
|
|---|
| 1853 | How to decide whether @file{foo.o} is out of date: it is out of date
|
|---|
| 1854 | if it does not exist, or if either @file{foo.c} or @file{defs.h} is
|
|---|
| 1855 | more recent than it.
|
|---|
| 1856 |
|
|---|
| 1857 | @item
|
|---|
| 1858 | How to update the file @file{foo.o}: by running @code{cc} as stated.
|
|---|
| 1859 | The command does not explicitly mention @file{defs.h}, but we presume
|
|---|
| 1860 | that @file{foo.c} includes it, and that that is why @file{defs.h} was
|
|---|
| 1861 | added to the prerequisites.
|
|---|
| 1862 | @end itemize
|
|---|
| 1863 | @end ifnottex
|
|---|
| 1864 |
|
|---|
| 1865 | @node Rule Syntax, Prerequisite Types, Rule Example, Rules
|
|---|
| 1866 | @section Rule Syntax
|
|---|
| 1867 |
|
|---|
| 1868 | @cindex rule syntax
|
|---|
| 1869 | @cindex syntax of rules
|
|---|
| 1870 | In general, a rule looks like this:
|
|---|
| 1871 |
|
|---|
| 1872 | @example
|
|---|
| 1873 | @var{targets} : @var{prerequisites}
|
|---|
| 1874 | @var{command}
|
|---|
| 1875 | @dots{}
|
|---|
| 1876 | @end example
|
|---|
| 1877 |
|
|---|
| 1878 | @noindent
|
|---|
| 1879 | or like this:
|
|---|
| 1880 |
|
|---|
| 1881 | @example
|
|---|
| 1882 | @var{targets} : @var{prerequisites} ; @var{command}
|
|---|
| 1883 | @var{command}
|
|---|
| 1884 | @dots{}
|
|---|
| 1885 | @end example
|
|---|
| 1886 |
|
|---|
| 1887 | @cindex targets
|
|---|
| 1888 | @cindex rule targets
|
|---|
| 1889 | The @var{targets} are file names, separated by spaces. Wildcard
|
|---|
| 1890 | characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
|
|---|
| 1891 | in File Names}) and a name of the form @file{@var{a}(@var{m})}
|
|---|
| 1892 | represents member @var{m} in archive file @var{a}
|
|---|
| 1893 | (@pxref{Archive Members, ,Archive Members as Targets}).
|
|---|
| 1894 | Usually there is only one
|
|---|
| 1895 | target per rule, but occasionally there is a reason to have more
|
|---|
| 1896 | (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
|
|---|
| 1897 |
|
|---|
| 1898 | @cindex commands
|
|---|
| 1899 | @cindex tab character (in commands)
|
|---|
| 1900 | The @var{command} lines start with a tab character. The first command may
|
|---|
| 1901 | appear on the line after the prerequisites, with a tab character, or may
|
|---|
| 1902 | appear on the same line, with a semicolon. Either way, the effect is the
|
|---|
| 1903 | same. @xref{Commands, ,Writing the Commands in Rules}.
|
|---|
| 1904 |
|
|---|
| 1905 | @cindex dollar sign (@code{$}), in rules
|
|---|
| 1906 | @cindex @code{$}, in rules
|
|---|
| 1907 | @cindex rule, and @code{$}
|
|---|
| 1908 | Because dollar signs are used to start variable references, if you really
|
|---|
| 1909 | want a dollar sign in a rule you must write two of them, @samp{$$}
|
|---|
| 1910 | (@pxref{Using Variables, ,How to Use Variables}). In prerequisite
|
|---|
| 1911 | lists you must actually write @emph{four} dollar signs (@samp{$$$$}),
|
|---|
| 1912 | due to secondary expansion (@pxref{Secondary Expansion}).
|
|---|
| 1913 | You may split a long line by inserting a backslash
|
|---|
| 1914 | followed by a newline, but this is not required, as @code{make} places no
|
|---|
| 1915 | limit on the length of a line in a makefile.
|
|---|
| 1916 |
|
|---|
| 1917 | A rule tells @code{make} two things: when the targets are out of date,
|
|---|
| 1918 | and how to update them when necessary.
|
|---|
| 1919 |
|
|---|
| 1920 | @cindex prerequisites
|
|---|
| 1921 | @cindex rule prerequisites
|
|---|
| 1922 | The criterion for being out of date is specified in terms of the
|
|---|
| 1923 | @var{prerequisites}, which consist of file names separated by spaces.
|
|---|
| 1924 | (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
|
|---|
| 1925 | A target is out of date if it does not exist or if it is older than any
|
|---|
| 1926 | of the prerequisites (by comparison of last-modification times). The
|
|---|
| 1927 | idea is that the contents of the target file are computed based on
|
|---|
| 1928 | information in the prerequisites, so if any of the prerequisites changes,
|
|---|
| 1929 | the contents of the existing target file are no longer necessarily
|
|---|
| 1930 | valid.
|
|---|
| 1931 |
|
|---|
| 1932 | How to update is specified by @var{commands}. These are lines to be
|
|---|
| 1933 | executed by the shell (normally @samp{sh}), but with some extra features
|
|---|
| 1934 | (@pxref{Commands, ,Writing the Commands in Rules}).
|
|---|
| 1935 |
|
|---|
| 1936 | @node Prerequisite Types, Wildcards, Rule Syntax, Rules
|
|---|
| 1937 | @comment node-name, next, previous, up
|
|---|
| 1938 | @section Types of Prerequisites
|
|---|
| 1939 | @cindex prerequisite types
|
|---|
| 1940 | @cindex types of prerequisites
|
|---|
| 1941 |
|
|---|
| 1942 | @cindex prerequisites, normal
|
|---|
| 1943 | @cindex normal prerequisites
|
|---|
| 1944 | @cindex prerequisites, order-only
|
|---|
| 1945 | @cindex order-only prerequisites
|
|---|
| 1946 | There are actually two different types of prerequisites understood by
|
|---|
| 1947 | GNU @code{make}: normal prerequisites such as described in the
|
|---|
| 1948 | previous section, and @dfn{order-only} prerequisites. A normal
|
|---|
| 1949 | prerequisite makes two statements: first, it imposes an order of
|
|---|
| 1950 | execution of build commands: any commands necessary to build any of a
|
|---|
| 1951 | target's prerequisites will be fully executed before any commands
|
|---|
| 1952 | necessary to build the target. Second, it imposes a dependency
|
|---|
| 1953 | relationship: if any prerequisite is newer than the target, then the
|
|---|
| 1954 | target is considered out-of-date and must be rebuilt.
|
|---|
| 1955 |
|
|---|
| 1956 | Normally, this is exactly what you want: if a target's prerequisite is
|
|---|
| 1957 | updated, then the target should also be updated.
|
|---|
| 1958 |
|
|---|
| 1959 | Occasionally, however, you have a situation where you want to impose a
|
|---|
| 1960 | specific ordering on the rules to be invoked @emph{without} forcing
|
|---|
| 1961 | the target to be updated if one of those rules is executed. In that
|
|---|
| 1962 | case, you want to define @dfn{order-only} prerequisites. Order-only
|
|---|
| 1963 | prerequisites can be specified by placing a pipe symbol (@code{|})
|
|---|
| 1964 | in the prerequisites list: any prerequisites to the left of the pipe
|
|---|
| 1965 | symbol are normal; any prerequisites to the right are order-only:
|
|---|
| 1966 |
|
|---|
| 1967 | @example
|
|---|
| 1968 | @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
|
|---|
| 1969 | @end example
|
|---|
| 1970 |
|
|---|
| 1971 | The normal prerequisites section may of course be empty. Also, you
|
|---|
| 1972 | may still declare multiple lines of prerequisites for the same target:
|
|---|
| 1973 | they are appended appropriately. Note that if you declare the same
|
|---|
| 1974 | file to be both a normal and an order-only prerequisite, the normal
|
|---|
| 1975 | prerequisite takes precedence (since they are a strict superset of the
|
|---|
| 1976 | behavior of an order-only prerequisite).
|
|---|
| 1977 |
|
|---|
| 1978 | @node Wildcards, Directory Search, Prerequisite Types, Rules
|
|---|
| 1979 | @section Using Wildcard Characters in File Names
|
|---|
| 1980 | @cindex wildcard
|
|---|
| 1981 | @cindex file name with wildcards
|
|---|
| 1982 | @cindex globbing (wildcards)
|
|---|
| 1983 |
|
|---|
| 1984 | @cindex @code{*} (wildcard character)
|
|---|
| 1985 | @cindex @code{?} (wildcard character)
|
|---|
| 1986 | @cindex @code{[@dots{}]} (wildcard characters)
|
|---|
| 1987 | A single file name can specify many files using @dfn{wildcard characters}.
|
|---|
| 1988 | The wildcard characters in @code{make} are @samp{*}, @samp{?} and
|
|---|
| 1989 | @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
|
|---|
| 1990 | specifies a list of all the files (in the working directory) whose names
|
|---|
| 1991 | end in @samp{.c}.@refill
|
|---|
| 1992 |
|
|---|
| 1993 | @cindex @code{~} (tilde)
|
|---|
| 1994 | @cindex tilde (@code{~})
|
|---|
| 1995 | @cindex home directory
|
|---|
| 1996 | The character @samp{~} at the beginning of a file name also has special
|
|---|
| 1997 | significance. If alone, or followed by a slash, it represents your home
|
|---|
| 1998 | directory. For example @file{~/bin} expands to @file{/home/you/bin}.
|
|---|
| 1999 | If the @samp{~} is followed by a word, the string represents the home
|
|---|
| 2000 | directory of the user named by that word. For example @file{~john/bin}
|
|---|
| 2001 | expands to @file{/home/john/bin}. On systems which don't have a home
|
|---|
| 2002 | directory for each user (such as MS-DOS or MS-Windows), this
|
|---|
| 2003 | functionality can be simulated by setting the environment variable
|
|---|
| 2004 | @var{HOME}.@refill
|
|---|
| 2005 |
|
|---|
| 2006 | Wildcard expansion happens automatically in targets, in prerequisites,
|
|---|
| 2007 | and in commands (where the shell does the expansion). In other
|
|---|
| 2008 | contexts, wildcard expansion happens only if you request it explicitly
|
|---|
| 2009 | with the @code{wildcard} function.
|
|---|
| 2010 |
|
|---|
| 2011 | The special significance of a wildcard character can be turned off by
|
|---|
| 2012 | preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
|
|---|
| 2013 | specific file whose name consists of @samp{foo}, an asterisk, and
|
|---|
| 2014 | @samp{bar}.@refill
|
|---|
| 2015 |
|
|---|
| 2016 | @menu
|
|---|
| 2017 | * Wildcard Examples:: Several examples
|
|---|
| 2018 | * Wildcard Pitfall:: Problems to avoid.
|
|---|
| 2019 | * Wildcard Function:: How to cause wildcard expansion where
|
|---|
| 2020 | it does not normally take place.
|
|---|
| 2021 | @end menu
|
|---|
| 2022 |
|
|---|
| 2023 | @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
|
|---|
| 2024 | @subsection Wildcard Examples
|
|---|
| 2025 |
|
|---|
| 2026 | Wildcards can be used in the commands of a rule, where they are expanded
|
|---|
| 2027 | by the shell. For example, here is a rule to delete all the object files:
|
|---|
| 2028 |
|
|---|
| 2029 | @example
|
|---|
| 2030 | @group
|
|---|
| 2031 | clean:
|
|---|
| 2032 | rm -f *.o
|
|---|
| 2033 | @end group
|
|---|
| 2034 | @end example
|
|---|
| 2035 | @cindex @code{rm} (shell command)
|
|---|
| 2036 |
|
|---|
| 2037 | Wildcards are also useful in the prerequisites of a rule. With the
|
|---|
| 2038 | following rule in the makefile, @samp{make print} will print all the
|
|---|
| 2039 | @samp{.c} files that have changed since the last time you printed them:
|
|---|
| 2040 |
|
|---|
| 2041 | @example
|
|---|
| 2042 | print: *.c
|
|---|
| 2043 | lpr -p $?
|
|---|
| 2044 | touch print
|
|---|
| 2045 | @end example
|
|---|
| 2046 |
|
|---|
| 2047 | @cindex @code{print} target
|
|---|
| 2048 | @cindex @code{lpr} (shell command)
|
|---|
| 2049 | @cindex @code{touch} (shell command)
|
|---|
| 2050 | @noindent
|
|---|
| 2051 | This rule uses @file{print} as an empty target file; see @ref{Empty
|
|---|
| 2052 | Targets, ,Empty Target Files to Record Events}. (The automatic variable
|
|---|
| 2053 | @samp{$?} is used to print only those files that have changed; see
|
|---|
| 2054 | @ref{Automatic Variables}.)@refill
|
|---|
| 2055 |
|
|---|
| 2056 | Wildcard expansion does not happen when you define a variable. Thus, if
|
|---|
| 2057 | you write this:
|
|---|
| 2058 |
|
|---|
| 2059 | @example
|
|---|
| 2060 | objects = *.o
|
|---|
| 2061 | @end example
|
|---|
| 2062 |
|
|---|
| 2063 | @noindent
|
|---|
| 2064 | then the value of the variable @code{objects} is the actual string
|
|---|
| 2065 | @samp{*.o}. However, if you use the value of @code{objects} in a target,
|
|---|
| 2066 | prerequisite or command, wildcard expansion will take place at that time.
|
|---|
| 2067 | To set @code{objects} to the expansion, instead use:
|
|---|
| 2068 |
|
|---|
| 2069 | @example
|
|---|
| 2070 | objects := $(wildcard *.o)
|
|---|
| 2071 | @end example
|
|---|
| 2072 |
|
|---|
| 2073 | @noindent
|
|---|
| 2074 | @xref{Wildcard Function}.
|
|---|
| 2075 |
|
|---|
| 2076 | @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
|
|---|
| 2077 | @subsection Pitfalls of Using Wildcards
|
|---|
| 2078 | @cindex wildcard pitfalls
|
|---|
| 2079 | @cindex pitfalls of wildcards
|
|---|
| 2080 | @cindex mistakes with wildcards
|
|---|
| 2081 | @cindex errors with wildcards
|
|---|
| 2082 | @cindex problems with wildcards
|
|---|
| 2083 |
|
|---|
| 2084 | Now here is an example of a naive way of using wildcard expansion, that
|
|---|
| 2085 | does not do what you would intend. Suppose you would like to say that the
|
|---|
| 2086 | executable file @file{foo} is made from all the object files in the
|
|---|
| 2087 | directory, and you write this:
|
|---|
| 2088 |
|
|---|
| 2089 | @example
|
|---|
| 2090 | objects = *.o
|
|---|
| 2091 |
|
|---|
| 2092 | foo : $(objects)
|
|---|
| 2093 | cc -o foo $(CFLAGS) $(objects)
|
|---|
| 2094 | @end example
|
|---|
| 2095 |
|
|---|
| 2096 | @noindent
|
|---|
| 2097 | The value of @code{objects} is the actual string @samp{*.o}. Wildcard
|
|---|
| 2098 | expansion happens in the rule for @file{foo}, so that each @emph{existing}
|
|---|
| 2099 | @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
|
|---|
| 2100 | necessary.
|
|---|
| 2101 |
|
|---|
| 2102 | But what if you delete all the @samp{.o} files? When a wildcard matches
|
|---|
| 2103 | no files, it is left as it is, so then @file{foo} will depend on the
|
|---|
| 2104 | oddly-named file @file{*.o}. Since no such file is likely to exist,
|
|---|
| 2105 | @code{make} will give you an error saying it cannot figure out how to
|
|---|
| 2106 | make @file{*.o}. This is not what you want!
|
|---|
| 2107 |
|
|---|
| 2108 | Actually it is possible to obtain the desired result with wildcard
|
|---|
| 2109 | expansion, but you need more sophisticated techniques, including the
|
|---|
| 2110 | @code{wildcard} function and string substitution.
|
|---|
| 2111 | @ifnottex
|
|---|
| 2112 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
|---|
| 2113 | @end ifnottex
|
|---|
| 2114 | @iftex
|
|---|
| 2115 | These are described in the following section.
|
|---|
| 2116 | @end iftex
|
|---|
| 2117 |
|
|---|
| 2118 | @cindex wildcards and MS-DOS/MS-Windows backslashes
|
|---|
| 2119 | @cindex backslashes in pathnames and wildcard expansion
|
|---|
| 2120 |
|
|---|
| 2121 | Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
|
|---|
| 2122 | separate directories in pathnames, like so:
|
|---|
| 2123 |
|
|---|
| 2124 | @example
|
|---|
| 2125 | c:\foo\bar\baz.c
|
|---|
| 2126 | @end example
|
|---|
| 2127 |
|
|---|
| 2128 | This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
|
|---|
| 2129 | @file{c:} part is the so-called drive letter). When @code{make} runs on
|
|---|
| 2130 | these systems, it supports backslashes as well as the Unix-style forward
|
|---|
| 2131 | slashes in pathnames. However, this support does @emph{not} include the
|
|---|
| 2132 | wildcard expansion, where backslash is a quote character. Therefore,
|
|---|
| 2133 | you @emph{must} use Unix-style slashes in these cases.
|
|---|
| 2134 |
|
|---|
| 2135 |
|
|---|
| 2136 | @node Wildcard Function, , Wildcard Pitfall, Wildcards
|
|---|
| 2137 | @subsection The Function @code{wildcard}
|
|---|
| 2138 | @findex wildcard
|
|---|
| 2139 |
|
|---|
| 2140 | Wildcard expansion happens automatically in rules. But wildcard expansion
|
|---|
| 2141 | does not normally take place when a variable is set, or inside the
|
|---|
| 2142 | arguments of a function. If you want to do wildcard expansion in such
|
|---|
| 2143 | places, you need to use the @code{wildcard} function, like this:
|
|---|
| 2144 |
|
|---|
| 2145 | @example
|
|---|
| 2146 | $(wildcard @var{pattern}@dots{})
|
|---|
| 2147 | @end example
|
|---|
| 2148 |
|
|---|
| 2149 | @noindent
|
|---|
| 2150 | This string, used anywhere in a makefile, is replaced by a
|
|---|
| 2151 | space-separated list of names of existing files that match one of the
|
|---|
| 2152 | given file name patterns. If no existing file name matches a pattern,
|
|---|
| 2153 | then that pattern is omitted from the output of the @code{wildcard}
|
|---|
| 2154 | function. Note that this is different from how unmatched wildcards
|
|---|
| 2155 | behave in rules, where they are used verbatim rather than ignored
|
|---|
| 2156 | (@pxref{Wildcard Pitfall}).
|
|---|
| 2157 |
|
|---|
| 2158 | One use of the @code{wildcard} function is to get a list of all the C source
|
|---|
| 2159 | files in a directory, like this:
|
|---|
| 2160 |
|
|---|
| 2161 | @example
|
|---|
| 2162 | $(wildcard *.c)
|
|---|
| 2163 | @end example
|
|---|
| 2164 |
|
|---|
| 2165 | We can change the list of C source files into a list of object files by
|
|---|
| 2166 | replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
|
|---|
| 2167 |
|
|---|
| 2168 | @example
|
|---|
| 2169 | $(patsubst %.c,%.o,$(wildcard *.c))
|
|---|
| 2170 | @end example
|
|---|
| 2171 |
|
|---|
| 2172 | @noindent
|
|---|
| 2173 | (Here we have used another function, @code{patsubst}.
|
|---|
| 2174 | @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
|
|---|
| 2175 |
|
|---|
| 2176 | Thus, a makefile to compile all C source files in the directory and then
|
|---|
| 2177 | link them together could be written as follows:
|
|---|
| 2178 |
|
|---|
| 2179 | @example
|
|---|
| 2180 | objects := $(patsubst %.c,%.o,$(wildcard *.c))
|
|---|
| 2181 |
|
|---|
| 2182 | foo : $(objects)
|
|---|
| 2183 | cc -o foo $(objects)
|
|---|
| 2184 | @end example
|
|---|
| 2185 |
|
|---|
| 2186 | @noindent
|
|---|
| 2187 | (This takes advantage of the implicit rule for compiling C programs, so
|
|---|
| 2188 | there is no need to write explicit rules for compiling the files.
|
|---|
| 2189 | @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
|
|---|
| 2190 | @samp{:=}, which is a variant of @samp{=}.)
|
|---|
| 2191 |
|
|---|
| 2192 | @node Directory Search, Phony Targets, Wildcards, Rules
|
|---|
| 2193 | @section Searching Directories for Prerequisites
|
|---|
| 2194 | @vindex VPATH
|
|---|
| 2195 | @findex vpath
|
|---|
| 2196 | @cindex vpath
|
|---|
| 2197 | @cindex search path for prerequisites (@code{VPATH})
|
|---|
| 2198 | @cindex directory search (@code{VPATH})
|
|---|
| 2199 |
|
|---|
| 2200 | For large systems, it is often desirable to put sources in a separate
|
|---|
| 2201 | directory from the binaries. The @dfn{directory search} features of
|
|---|
| 2202 | @code{make} facilitate this by searching several directories
|
|---|
| 2203 | automatically to find a prerequisite. When you redistribute the files
|
|---|
| 2204 | among directories, you do not need to change the individual rules,
|
|---|
| 2205 | just the search paths.
|
|---|
| 2206 |
|
|---|
| 2207 | @menu
|
|---|
| 2208 | * General Search:: Specifying a search path that applies
|
|---|
| 2209 | to every prerequisite.
|
|---|
| 2210 | * Selective Search:: Specifying a search path
|
|---|
| 2211 | for a specified class of names.
|
|---|
| 2212 | * Search Algorithm:: When and how search paths are applied.
|
|---|
| 2213 | * Commands/Search:: How to write shell commands that work together
|
|---|
| 2214 | with search paths.
|
|---|
| 2215 | * Implicit/Search:: How search paths affect implicit rules.
|
|---|
| 2216 | * Libraries/Search:: Directory search for link libraries.
|
|---|
| 2217 | @end menu
|
|---|
| 2218 |
|
|---|
| 2219 | @node General Search, Selective Search, Directory Search, Directory Search
|
|---|
| 2220 | @subsection @code{VPATH}: Search Path for All Prerequisites
|
|---|
| 2221 | @vindex VPATH
|
|---|
| 2222 |
|
|---|
| 2223 | The value of the @code{make} variable @code{VPATH} specifies a list of
|
|---|
| 2224 | directories that @code{make} should search. Most often, the
|
|---|
| 2225 | directories are expected to contain prerequisite files that are not in the
|
|---|
| 2226 | current directory; however, @code{make} uses @code{VPATH} as a search
|
|---|
| 2227 | list for both prerequisites and targets of rules.
|
|---|
| 2228 |
|
|---|
| 2229 | Thus, if a file that is listed as a target or prerequisite does not exist
|
|---|
| 2230 | in the current directory, @code{make} searches the directories listed in
|
|---|
| 2231 | @code{VPATH} for a file with that name. If a file is found in one of
|
|---|
| 2232 | them, that file may become the prerequisite (see below). Rules may then
|
|---|
| 2233 | specify the names of files in the prerequisite list as if they all
|
|---|
| 2234 | existed in the current directory. @xref{Commands/Search, ,Writing Shell
|
|---|
| 2235 | Commands with Directory Search}.
|
|---|
| 2236 |
|
|---|
| 2237 | In the @code{VPATH} variable, directory names are separated by colons or
|
|---|
| 2238 | blanks. The order in which directories are listed is the order followed
|
|---|
| 2239 | by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
|
|---|
| 2240 | are used as separators of directory names in @code{VPATH}, since the
|
|---|
| 2241 | colon can be used in the pathname itself, after the drive letter.)
|
|---|
| 2242 |
|
|---|
| 2243 | For example,
|
|---|
| 2244 |
|
|---|
| 2245 | @example
|
|---|
| 2246 | VPATH = src:../headers
|
|---|
| 2247 | @end example
|
|---|
| 2248 |
|
|---|
| 2249 | @noindent
|
|---|
| 2250 | specifies a path containing two directories, @file{src} and
|
|---|
| 2251 | @file{../headers}, which @code{make} searches in that order.
|
|---|
| 2252 |
|
|---|
| 2253 | With this value of @code{VPATH}, the following rule,
|
|---|
| 2254 |
|
|---|
| 2255 | @example
|
|---|
| 2256 | foo.o : foo.c
|
|---|
| 2257 | @end example
|
|---|
| 2258 |
|
|---|
| 2259 | @noindent
|
|---|
| 2260 | is interpreted as if it were written like this:
|
|---|
| 2261 |
|
|---|
| 2262 | @example
|
|---|
| 2263 | foo.o : src/foo.c
|
|---|
| 2264 | @end example
|
|---|
| 2265 |
|
|---|
| 2266 | @noindent
|
|---|
| 2267 | assuming the file @file{foo.c} does not exist in the current directory but
|
|---|
| 2268 | is found in the directory @file{src}.
|
|---|
| 2269 |
|
|---|
| 2270 | @node Selective Search, Search Algorithm, General Search, Directory Search
|
|---|
| 2271 | @subsection The @code{vpath} Directive
|
|---|
| 2272 | @findex vpath
|
|---|
| 2273 |
|
|---|
| 2274 | Similar to the @code{VPATH} variable, but more selective, is the
|
|---|
| 2275 | @code{vpath} directive (note lower case), which allows you to specify a
|
|---|
| 2276 | search path for a particular class of file names: those that match a
|
|---|
| 2277 | particular pattern. Thus you can supply certain search directories for
|
|---|
| 2278 | one class of file names and other directories (or none) for other file
|
|---|
| 2279 | names.
|
|---|
| 2280 |
|
|---|
| 2281 | There are three forms of the @code{vpath} directive:
|
|---|
| 2282 |
|
|---|
| 2283 | @table @code
|
|---|
| 2284 | @item vpath @var{pattern} @var{directories}
|
|---|
| 2285 | Specify the search path @var{directories} for file names that match
|
|---|
| 2286 | @var{pattern}.
|
|---|
| 2287 |
|
|---|
| 2288 | The search path, @var{directories}, is a list of directories to be
|
|---|
| 2289 | searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
|
|---|
| 2290 | blanks, just like the search path used in the @code{VPATH} variable.
|
|---|
| 2291 |
|
|---|
| 2292 | @item vpath @var{pattern}
|
|---|
| 2293 | Clear out the search path associated with @var{pattern}.
|
|---|
| 2294 |
|
|---|
| 2295 | @c Extra blank line makes sure this gets two lines.
|
|---|
| 2296 | @item vpath
|
|---|
| 2297 |
|
|---|
| 2298 | Clear all search paths previously specified with @code{vpath} directives.
|
|---|
| 2299 | @end table
|
|---|
| 2300 |
|
|---|
| 2301 | A @code{vpath} pattern is a string containing a @samp{%} character. The
|
|---|
| 2302 | string must match the file name of a prerequisite that is being searched
|
|---|
| 2303 | for, the @samp{%} character matching any sequence of zero or more
|
|---|
| 2304 | characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
|
|---|
| 2305 | Redefining Pattern Rules}). For example, @code{%.h} matches files that
|
|---|
| 2306 | end in @code{.h}. (If there is no @samp{%}, the pattern must match the
|
|---|
| 2307 | prerequisite exactly, which is not useful very often.)
|
|---|
| 2308 |
|
|---|
| 2309 | @cindex @code{%}, quoting in @code{vpath}
|
|---|
| 2310 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 2311 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 2312 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 2313 | @cindex quoting @code{%}, in @code{vpath}
|
|---|
| 2314 | @samp{%} characters in a @code{vpath} directive's pattern can be quoted
|
|---|
| 2315 | with preceding backslashes (@samp{\}). Backslashes that would otherwise
|
|---|
| 2316 | quote @samp{%} characters can be quoted with more backslashes.
|
|---|
| 2317 | Backslashes that quote @samp{%} characters or other backslashes are
|
|---|
| 2318 | removed from the pattern before it is compared to file names. Backslashes
|
|---|
| 2319 | that are not in danger of quoting @samp{%} characters go unmolested.@refill
|
|---|
| 2320 |
|
|---|
| 2321 | When a prerequisite fails to exist in the current directory, if the
|
|---|
| 2322 | @var{pattern} in a @code{vpath} directive matches the name of the
|
|---|
| 2323 | prerequisite file, then the @var{directories} in that directive are searched
|
|---|
| 2324 | just like (and before) the directories in the @code{VPATH} variable.
|
|---|
| 2325 |
|
|---|
| 2326 | For example,
|
|---|
| 2327 |
|
|---|
| 2328 | @example
|
|---|
| 2329 | vpath %.h ../headers
|
|---|
| 2330 | @end example
|
|---|
| 2331 |
|
|---|
| 2332 | @noindent
|
|---|
| 2333 | tells @code{make} to look for any prerequisite whose name ends in @file{.h}
|
|---|
| 2334 | in the directory @file{../headers} if the file is not found in the current
|
|---|
| 2335 | directory.
|
|---|
| 2336 |
|
|---|
| 2337 | If several @code{vpath} patterns match the prerequisite file's name, then
|
|---|
| 2338 | @code{make} processes each matching @code{vpath} directive one by one,
|
|---|
| 2339 | searching all the directories mentioned in each directive. @code{make}
|
|---|
| 2340 | handles multiple @code{vpath} directives in the order in which they
|
|---|
| 2341 | appear in the makefile; multiple directives with the same pattern are
|
|---|
| 2342 | independent of each other.
|
|---|
| 2343 |
|
|---|
| 2344 | @need 750
|
|---|
| 2345 | Thus,
|
|---|
| 2346 |
|
|---|
| 2347 | @example
|
|---|
| 2348 | @group
|
|---|
| 2349 | vpath %.c foo
|
|---|
| 2350 | vpath % blish
|
|---|
| 2351 | vpath %.c bar
|
|---|
| 2352 | @end group
|
|---|
| 2353 | @end example
|
|---|
| 2354 |
|
|---|
| 2355 | @noindent
|
|---|
| 2356 | will look for a file ending in @samp{.c} in @file{foo}, then
|
|---|
| 2357 | @file{blish}, then @file{bar}, while
|
|---|
| 2358 |
|
|---|
| 2359 | @example
|
|---|
| 2360 | @group
|
|---|
| 2361 | vpath %.c foo:bar
|
|---|
| 2362 | vpath % blish
|
|---|
| 2363 | @end group
|
|---|
| 2364 | @end example
|
|---|
| 2365 |
|
|---|
| 2366 | @noindent
|
|---|
| 2367 | will look for a file ending in @samp{.c} in @file{foo}, then
|
|---|
| 2368 | @file{bar}, then @file{blish}.
|
|---|
| 2369 |
|
|---|
| 2370 | @node Search Algorithm, Commands/Search, Selective Search, Directory Search
|
|---|
| 2371 | @subsection How Directory Searches are Performed
|
|---|
| 2372 | @cindex algorithm for directory search
|
|---|
| 2373 | @cindex directory search algorithm
|
|---|
| 2374 |
|
|---|
| 2375 | When a prerequisite is found through directory search, regardless of type
|
|---|
| 2376 | (general or selective), the pathname located may not be the one that
|
|---|
| 2377 | @code{make} actually provides you in the prerequisite list. Sometimes
|
|---|
| 2378 | the path discovered through directory search is thrown away.
|
|---|
| 2379 |
|
|---|
| 2380 | The algorithm @code{make} uses to decide whether to keep or abandon a
|
|---|
| 2381 | path found via directory search is as follows:
|
|---|
| 2382 |
|
|---|
| 2383 | @enumerate
|
|---|
| 2384 | @item
|
|---|
| 2385 | If a target file does not exist at the path specified in the makefile,
|
|---|
| 2386 | directory search is performed.
|
|---|
| 2387 |
|
|---|
| 2388 | @item
|
|---|
| 2389 | If the directory search is successful, that path is kept and this file
|
|---|
| 2390 | is tentatively stored as the target.
|
|---|
| 2391 |
|
|---|
| 2392 | @item
|
|---|
| 2393 | All prerequisites of this target are examined using this same method.
|
|---|
| 2394 |
|
|---|
| 2395 | @item
|
|---|
| 2396 | After processing the prerequisites, the target may or may not need to be
|
|---|
| 2397 | rebuilt:
|
|---|
| 2398 |
|
|---|
| 2399 | @enumerate a
|
|---|
| 2400 | @item
|
|---|
| 2401 | If the target does @emph{not} need to be rebuilt, the path to the file
|
|---|
| 2402 | found during directory search is used for any prerequisite lists which
|
|---|
| 2403 | contain this target. In short, if @code{make} doesn't need to rebuild
|
|---|
| 2404 | the target then you use the path found via directory search.
|
|---|
| 2405 |
|
|---|
| 2406 | @item
|
|---|
| 2407 | If the target @emph{does} need to be rebuilt (is out-of-date), the
|
|---|
| 2408 | pathname found during directory search is @emph{thrown away}, and the
|
|---|
| 2409 | target is rebuilt using the file name specified in the makefile. In
|
|---|
| 2410 | short, if @code{make} must rebuild, then the target is rebuilt locally,
|
|---|
| 2411 | not in the directory found via directory search.
|
|---|
| 2412 | @end enumerate
|
|---|
| 2413 | @end enumerate
|
|---|
| 2414 |
|
|---|
| 2415 | This algorithm may seem complex, but in practice it is quite often
|
|---|
| 2416 | exactly what you want.
|
|---|
| 2417 |
|
|---|
| 2418 | @cindex traditional directory search (GPATH)
|
|---|
| 2419 | @cindex directory search, traditional (GPATH)
|
|---|
| 2420 | Other versions of @code{make} use a simpler algorithm: if the file does
|
|---|
| 2421 | not exist, and it is found via directory search, then that pathname is
|
|---|
| 2422 | always used whether or not the target needs to be built. Thus, if the
|
|---|
| 2423 | target is rebuilt it is created at the pathname discovered during
|
|---|
| 2424 | directory search.
|
|---|
| 2425 |
|
|---|
| 2426 | @vindex GPATH
|
|---|
| 2427 | If, in fact, this is the behavior you want for some or all of your
|
|---|
| 2428 | directories, you can use the @code{GPATH} variable to indicate this to
|
|---|
| 2429 | @code{make}.
|
|---|
| 2430 |
|
|---|
| 2431 | @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
|
|---|
| 2432 | space- or colon-delimited list of pathnames). If an out-of-date target
|
|---|
| 2433 | is found by directory search in a directory that also appears in
|
|---|
| 2434 | @code{GPATH}, then that pathname is not thrown away. The target is
|
|---|
| 2435 | rebuilt using the expanded path.
|
|---|
| 2436 |
|
|---|
| 2437 | @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
|
|---|
| 2438 | @subsection Writing Shell Commands with Directory Search
|
|---|
| 2439 | @cindex shell command, and directory search
|
|---|
| 2440 | @cindex directory search (@code{VPATH}), and shell commands
|
|---|
| 2441 |
|
|---|
| 2442 | When a prerequisite is found in another directory through directory search,
|
|---|
| 2443 | this cannot change the commands of the rule; they will execute as written.
|
|---|
| 2444 | Therefore, you must write the commands with care so that they will look for
|
|---|
| 2445 | the prerequisite in the directory where @code{make} finds it.
|
|---|
| 2446 |
|
|---|
| 2447 | This is done with the @dfn{automatic variables} such as @samp{$^}
|
|---|
| 2448 | (@pxref{Automatic Variables}).
|
|---|
| 2449 | For instance, the value of @samp{$^} is a
|
|---|
| 2450 | list of all the prerequisites of the rule, including the names of
|
|---|
| 2451 | the directories in which they were found, and the value of
|
|---|
| 2452 | @samp{$@@} is the target. Thus:@refill
|
|---|
| 2453 |
|
|---|
| 2454 | @example
|
|---|
| 2455 | foo.o : foo.c
|
|---|
| 2456 | cc -c $(CFLAGS) $^ -o $@@
|
|---|
| 2457 | @end example
|
|---|
| 2458 |
|
|---|
| 2459 | @noindent
|
|---|
| 2460 | (The variable @code{CFLAGS} exists so you can specify flags for C
|
|---|
| 2461 | compilation by implicit rules; we use it here for consistency so it will
|
|---|
| 2462 | affect all C compilations uniformly;
|
|---|
| 2463 | @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
|
|---|
| 2464 |
|
|---|
| 2465 | Often the prerequisites include header files as well, which you do not
|
|---|
| 2466 | want to mention in the commands. The automatic variable @samp{$<} is
|
|---|
| 2467 | just the first prerequisite:
|
|---|
| 2468 |
|
|---|
| 2469 | @example
|
|---|
| 2470 | VPATH = src:../headers
|
|---|
| 2471 | foo.o : foo.c defs.h hack.h
|
|---|
| 2472 | cc -c $(CFLAGS) $< -o $@@
|
|---|
| 2473 | @end example
|
|---|
| 2474 |
|
|---|
| 2475 | @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
|
|---|
| 2476 | @subsection Directory Search and Implicit Rules
|
|---|
| 2477 | @cindex @code{VPATH}, and implicit rules
|
|---|
| 2478 | @cindex directory search (@code{VPATH}), and implicit rules
|
|---|
| 2479 | @cindex search path for prerequisites (@code{VPATH}), and implicit rules
|
|---|
| 2480 | @cindex implicit rule, and directory search
|
|---|
| 2481 | @cindex implicit rule, and @code{VPATH}
|
|---|
| 2482 | @cindex rule, implicit, and directory search
|
|---|
| 2483 | @cindex rule, implicit, and @code{VPATH}
|
|---|
| 2484 |
|
|---|
| 2485 | The search through the directories specified in @code{VPATH} or with
|
|---|
| 2486 | @code{vpath} also happens during consideration of implicit rules
|
|---|
| 2487 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 2488 |
|
|---|
| 2489 | For example, when a file @file{foo.o} has no explicit rule, @code{make}
|
|---|
| 2490 | considers implicit rules, such as the built-in rule to compile
|
|---|
| 2491 | @file{foo.c} if that file exists. If such a file is lacking in the
|
|---|
| 2492 | current directory, the appropriate directories are searched for it. If
|
|---|
| 2493 | @file{foo.c} exists (or is mentioned in the makefile) in any of the
|
|---|
| 2494 | directories, the implicit rule for C compilation is applied.
|
|---|
| 2495 |
|
|---|
| 2496 | The commands of implicit rules normally use automatic variables as a
|
|---|
| 2497 | matter of necessity; consequently they will use the file names found by
|
|---|
| 2498 | directory search with no extra effort.
|
|---|
| 2499 |
|
|---|
| 2500 | @node Libraries/Search, , Implicit/Search, Directory Search
|
|---|
| 2501 | @subsection Directory Search for Link Libraries
|
|---|
| 2502 | @cindex link libraries, and directory search
|
|---|
| 2503 | @cindex libraries for linking, directory search
|
|---|
| 2504 | @cindex directory search (@code{VPATH}), and link libraries
|
|---|
| 2505 | @cindex @code{VPATH}, and link libraries
|
|---|
| 2506 | @cindex search path for prerequisites (@code{VPATH}), and link libraries
|
|---|
| 2507 | @cindex @code{-l} (library search)
|
|---|
| 2508 | @cindex link libraries, patterns matching
|
|---|
| 2509 | @cindex @code{.LIBPATTERNS}, and link libraries
|
|---|
| 2510 | @vindex .LIBPATTERNS
|
|---|
| 2511 |
|
|---|
| 2512 | Directory search applies in a special way to libraries used with the
|
|---|
| 2513 | linker. This special feature comes into play when you write a prerequisite
|
|---|
| 2514 | whose name is of the form @samp{-l@var{name}}. (You can tell something
|
|---|
| 2515 | strange is going on here because the prerequisite is normally the name of a
|
|---|
| 2516 | file, and the @emph{file name} of a library generally looks like
|
|---|
| 2517 | @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
|
|---|
| 2518 |
|
|---|
| 2519 | When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
|
|---|
| 2520 | handles it specially by searching for the file @file{lib@var{name}.so} in
|
|---|
| 2521 | the current directory, in directories specified by matching @code{vpath}
|
|---|
| 2522 | search paths and the @code{VPATH} search path, and then in the
|
|---|
| 2523 | directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
|
|---|
| 2524 | (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
|
|---|
| 2525 | @code{make} behave as if @var{prefix} is defined to be the root of the
|
|---|
| 2526 | DJGPP installation tree).
|
|---|
| 2527 |
|
|---|
| 2528 | If that file is not found, then the file @file{lib@var{name}.a} is
|
|---|
| 2529 | searched for, in the same directories as above.
|
|---|
| 2530 |
|
|---|
| 2531 | For example, if there is a @file{/usr/lib/libcurses.a} library on your
|
|---|
| 2532 | system (and no @file{/usr/lib/libcurses.so} file), then
|
|---|
| 2533 |
|
|---|
| 2534 | @example
|
|---|
| 2535 | @group
|
|---|
| 2536 | foo : foo.c -lcurses
|
|---|
| 2537 | cc $^ -o $@@
|
|---|
| 2538 | @end group
|
|---|
| 2539 | @end example
|
|---|
| 2540 |
|
|---|
| 2541 | @noindent
|
|---|
| 2542 | would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
|
|---|
| 2543 | be executed when @file{foo} is older than @file{foo.c} or than
|
|---|
| 2544 | @file{/usr/lib/libcurses.a}.@refill
|
|---|
| 2545 |
|
|---|
| 2546 | Although the default set of files to be searched for is
|
|---|
| 2547 | @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
|
|---|
| 2548 | via the @code{.LIBPATTERNS} variable. Each word in the value of this
|
|---|
| 2549 | variable is a pattern string. When a prerequisite like
|
|---|
| 2550 | @samp{-l@var{name}} is seen, @code{make} will replace the percent in
|
|---|
| 2551 | each pattern in the list with @var{name} and perform the above directory
|
|---|
| 2552 | searches using that library filename. If no library is found, the next
|
|---|
| 2553 | word in the list will be used.
|
|---|
| 2554 |
|
|---|
| 2555 | The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
|
|---|
| 2556 | which provides the default behavior described above.
|
|---|
| 2557 |
|
|---|
| 2558 | You can turn off link library expansion completely by setting this
|
|---|
| 2559 | variable to an empty value.
|
|---|
| 2560 |
|
|---|
| 2561 | @node Phony Targets, Force Targets, Directory Search, Rules
|
|---|
| 2562 | @section Phony Targets
|
|---|
| 2563 | @cindex phony targets
|
|---|
| 2564 | @cindex targets, phony
|
|---|
| 2565 | @cindex targets without a file
|
|---|
| 2566 |
|
|---|
| 2567 | A phony target is one that is not really the name of a file. It is just a
|
|---|
| 2568 | name for some commands to be executed when you make an explicit request.
|
|---|
| 2569 | There are two reasons to use a phony target: to avoid a conflict with
|
|---|
| 2570 | a file of the same name, and to improve performance.
|
|---|
| 2571 |
|
|---|
| 2572 | If you write a rule whose commands will not create the target file, the
|
|---|
| 2573 | commands will be executed every time the target comes up for remaking.
|
|---|
| 2574 | Here is an example:
|
|---|
| 2575 |
|
|---|
| 2576 | @example
|
|---|
| 2577 | @group
|
|---|
| 2578 | clean:
|
|---|
| 2579 | rm *.o temp
|
|---|
| 2580 | @end group
|
|---|
| 2581 | @end example
|
|---|
| 2582 |
|
|---|
| 2583 | @noindent
|
|---|
| 2584 | Because the @code{rm} command does not create a file named @file{clean},
|
|---|
| 2585 | probably no such file will ever exist. Therefore, the @code{rm} command
|
|---|
| 2586 | will be executed every time you say @samp{make clean}.
|
|---|
| 2587 | @cindex @code{rm} (shell command)
|
|---|
| 2588 |
|
|---|
| 2589 | @findex .PHONY
|
|---|
| 2590 | The phony target will cease to work if anything ever does create a file
|
|---|
| 2591 | named @file{clean} in this directory. Since it has no prerequisites, the
|
|---|
| 2592 | file @file{clean} would inevitably be considered up to date, and its
|
|---|
| 2593 | commands would not be executed. To avoid this problem, you can explicitly
|
|---|
| 2594 | declare the target to be phony, using the special target @code{.PHONY}
|
|---|
| 2595 | (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
|
|---|
| 2596 |
|
|---|
| 2597 | @example
|
|---|
| 2598 | .PHONY : clean
|
|---|
| 2599 | @end example
|
|---|
| 2600 |
|
|---|
| 2601 | @noindent
|
|---|
| 2602 | Once this is done, @samp{make clean} will run the commands regardless of
|
|---|
| 2603 | whether there is a file named @file{clean}.
|
|---|
| 2604 |
|
|---|
| 2605 | Since it knows that phony targets do not name actual files that could be
|
|---|
| 2606 | remade from other files, @code{make} skips the implicit rule search for
|
|---|
| 2607 | phony targets (@pxref{Implicit Rules}). This is why declaring a target
|
|---|
| 2608 | phony is good for performance, even if you are not worried about the
|
|---|
| 2609 | actual file existing.
|
|---|
| 2610 |
|
|---|
| 2611 | Thus, you first write the line that states that @code{clean} is a
|
|---|
| 2612 | phony target, then you write the rule, like this:
|
|---|
| 2613 |
|
|---|
| 2614 | @example
|
|---|
| 2615 | @group
|
|---|
| 2616 | .PHONY: clean
|
|---|
| 2617 | clean:
|
|---|
| 2618 | rm *.o temp
|
|---|
| 2619 | @end group
|
|---|
| 2620 | @end example
|
|---|
| 2621 |
|
|---|
| 2622 | Another example of the usefulness of phony targets is in conjunction
|
|---|
| 2623 | with recursive invocations of @code{make} (for more information, see
|
|---|
| 2624 | @ref{Recursion, ,Recursive Use of @code{make}}). In this case the
|
|---|
| 2625 | makefile will often contain a variable which lists a number of
|
|---|
| 2626 | subdirectories to be built. One way to handle this is with one rule
|
|---|
| 2627 | whose command is a shell loop over the subdirectories, like this:
|
|---|
| 2628 |
|
|---|
| 2629 | @example
|
|---|
| 2630 | @group
|
|---|
| 2631 | SUBDIRS = foo bar baz
|
|---|
| 2632 |
|
|---|
| 2633 | subdirs:
|
|---|
| 2634 | for dir in $(SUBDIRS); do \
|
|---|
| 2635 | $(MAKE) -C $$dir; \
|
|---|
| 2636 | done
|
|---|
| 2637 | @end group
|
|---|
| 2638 | @end example
|
|---|
| 2639 |
|
|---|
| 2640 | There are a few problems with this method, however. First, any error
|
|---|
| 2641 | detected in a submake is not noted by this rule, so it will continue to
|
|---|
| 2642 | build the rest of the directories even when one fails. This can be
|
|---|
| 2643 | overcome by adding shell commands to note the error and exit, but then
|
|---|
| 2644 | it will do so even if @code{make} is invoked with the @code{-k} option,
|
|---|
| 2645 | which is unfortunate. Second, and perhaps more importantly, you cannot
|
|---|
| 2646 | take advantage of the parallel build capabilities of make using this
|
|---|
| 2647 | method, since there is only one rule.
|
|---|
| 2648 |
|
|---|
| 2649 | By declaring the subdirectories as phony targets (you must do this as
|
|---|
| 2650 | the subdirectory obviously always exists; otherwise it won't be built)
|
|---|
| 2651 | you can remove these problems:
|
|---|
| 2652 |
|
|---|
| 2653 | @example
|
|---|
| 2654 | @group
|
|---|
| 2655 | SUBDIRS = foo bar baz
|
|---|
| 2656 |
|
|---|
| 2657 | .PHONY: subdirs $(SUBDIRS)
|
|---|
| 2658 |
|
|---|
| 2659 | subdirs: $(SUBDIRS)
|
|---|
| 2660 |
|
|---|
| 2661 | $(SUBDIRS):
|
|---|
| 2662 | $(MAKE) -C $@@
|
|---|
| 2663 |
|
|---|
| 2664 | foo: baz
|
|---|
| 2665 | @end group
|
|---|
| 2666 | @end example
|
|---|
| 2667 |
|
|---|
| 2668 | Here we've also declared that the @file{foo} subdirectory cannot be
|
|---|
| 2669 | built until after the @file{baz} subdirectory is complete; this kind of
|
|---|
| 2670 | relationship declaration is particularly important when attempting
|
|---|
| 2671 | parallel builds.
|
|---|
| 2672 |
|
|---|
| 2673 | A phony target should not be a prerequisite of a real target file; if it
|
|---|
| 2674 | is, its commands are run every time @code{make} goes to update that
|
|---|
| 2675 | file. As long as a phony target is never a prerequisite of a real
|
|---|
| 2676 | target, the phony target commands will be executed only when the phony
|
|---|
| 2677 | target is a specified goal (@pxref{Goals, ,Arguments to Specify the
|
|---|
| 2678 | Goals}).
|
|---|
| 2679 |
|
|---|
| 2680 | Phony targets can have prerequisites. When one directory contains multiple
|
|---|
| 2681 | programs, it is most convenient to describe all of the programs in one
|
|---|
| 2682 | makefile @file{./Makefile}. Since the target remade by default will be the
|
|---|
| 2683 | first one in the makefile, it is common to make this a phony target named
|
|---|
| 2684 | @samp{all} and give it, as prerequisites, all the individual programs. For
|
|---|
| 2685 | example:
|
|---|
| 2686 |
|
|---|
| 2687 | @example
|
|---|
| 2688 | all : prog1 prog2 prog3
|
|---|
| 2689 | .PHONY : all
|
|---|
| 2690 |
|
|---|
| 2691 | prog1 : prog1.o utils.o
|
|---|
| 2692 | cc -o prog1 prog1.o utils.o
|
|---|
| 2693 |
|
|---|
| 2694 | prog2 : prog2.o
|
|---|
| 2695 | cc -o prog2 prog2.o
|
|---|
| 2696 |
|
|---|
| 2697 | prog3 : prog3.o sort.o utils.o
|
|---|
| 2698 | cc -o prog3 prog3.o sort.o utils.o
|
|---|
| 2699 | @end example
|
|---|
| 2700 |
|
|---|
| 2701 | @noindent
|
|---|
| 2702 | Now you can say just @samp{make} to remake all three programs, or
|
|---|
| 2703 | specify as arguments the ones to remake (as in @samp{make prog1
|
|---|
| 2704 | prog3}). Phoniness is not inherited: the prerequisites of a phony
|
|---|
| 2705 | target are not themselves phony, unless explicitly declared to be so.
|
|---|
| 2706 |
|
|---|
| 2707 | When one phony target is a prerequisite of another, it serves as a subroutine
|
|---|
| 2708 | of the other. For example, here @samp{make cleanall} will delete the
|
|---|
| 2709 | object files, the difference files, and the file @file{program}:
|
|---|
| 2710 |
|
|---|
| 2711 | @example
|
|---|
| 2712 | .PHONY: cleanall cleanobj cleandiff
|
|---|
| 2713 |
|
|---|
| 2714 | cleanall : cleanobj cleandiff
|
|---|
| 2715 | rm program
|
|---|
| 2716 |
|
|---|
| 2717 | cleanobj :
|
|---|
| 2718 | rm *.o
|
|---|
| 2719 |
|
|---|
| 2720 | cleandiff :
|
|---|
| 2721 | rm *.diff
|
|---|
| 2722 | @end example
|
|---|
| 2723 |
|
|---|
| 2724 | @node Force Targets, Empty Targets, Phony Targets, Rules
|
|---|
| 2725 | @section Rules without Commands or Prerequisites
|
|---|
| 2726 | @cindex force targets
|
|---|
| 2727 | @cindex targets, force
|
|---|
| 2728 | @cindex @code{FORCE}
|
|---|
| 2729 | @cindex rule, no commands or prerequisites
|
|---|
| 2730 |
|
|---|
| 2731 | If a rule has no prerequisites or commands, and the target of the rule
|
|---|
| 2732 | is a nonexistent file, then @code{make} imagines this target to have
|
|---|
| 2733 | been updated whenever its rule is run. This implies that all targets
|
|---|
| 2734 | depending on this one will always have their commands run.
|
|---|
| 2735 |
|
|---|
| 2736 | An example will illustrate this:
|
|---|
| 2737 |
|
|---|
| 2738 | @example
|
|---|
| 2739 | @group
|
|---|
| 2740 | clean: FORCE
|
|---|
| 2741 | rm $(objects)
|
|---|
| 2742 | FORCE:
|
|---|
| 2743 | @end group
|
|---|
| 2744 | @end example
|
|---|
| 2745 |
|
|---|
| 2746 | Here the target @samp{FORCE} satisfies the special conditions, so the
|
|---|
| 2747 | target @file{clean} that depends on it is forced to run its commands.
|
|---|
| 2748 | There is nothing special about the name @samp{FORCE}, but that is one name
|
|---|
| 2749 | commonly used this way.
|
|---|
| 2750 |
|
|---|
| 2751 | As you can see, using @samp{FORCE} this way has the same results as using
|
|---|
| 2752 | @samp{.PHONY: clean}.
|
|---|
| 2753 |
|
|---|
| 2754 | Using @samp{.PHONY} is more explicit and more efficient. However,
|
|---|
| 2755 | other versions of @code{make} do not support @samp{.PHONY}; thus
|
|---|
| 2756 | @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
|
|---|
| 2757 |
|
|---|
| 2758 | @node Empty Targets, Special Targets, Force Targets, Rules
|
|---|
| 2759 | @section Empty Target Files to Record Events
|
|---|
| 2760 | @cindex empty targets
|
|---|
| 2761 | @cindex targets, empty
|
|---|
| 2762 | @cindex recording events with empty targets
|
|---|
| 2763 |
|
|---|
| 2764 | The @dfn{empty target} is a variant of the phony target; it is used to hold
|
|---|
| 2765 | commands for an action that you request explicitly from time to time.
|
|---|
| 2766 | Unlike a phony target, this target file can really exist; but the file's
|
|---|
| 2767 | contents do not matter, and usually are empty.
|
|---|
| 2768 |
|
|---|
| 2769 | The purpose of the empty target file is to record, with its
|
|---|
| 2770 | last-modification time, when the rule's commands were last executed. It
|
|---|
| 2771 | does so because one of the commands is a @code{touch} command to update the
|
|---|
| 2772 | target file.
|
|---|
| 2773 |
|
|---|
| 2774 | The empty target file should have some prerequisites (otherwise it
|
|---|
| 2775 | doesn't make sense). When you ask to remake the empty target, the
|
|---|
| 2776 | commands are executed if any prerequisite is more recent than the target;
|
|---|
| 2777 | in other words, if a prerequisite has changed since the last time you
|
|---|
| 2778 | remade the target. Here is an example:
|
|---|
| 2779 |
|
|---|
| 2780 | @example
|
|---|
| 2781 | print: foo.c bar.c
|
|---|
| 2782 | lpr -p $?
|
|---|
| 2783 | touch print
|
|---|
| 2784 | @end example
|
|---|
| 2785 | @cindex @code{print} target
|
|---|
| 2786 | @cindex @code{lpr} (shell command)
|
|---|
| 2787 | @cindex @code{touch} (shell command)
|
|---|
| 2788 |
|
|---|
| 2789 | @noindent
|
|---|
| 2790 | With this rule, @samp{make print} will execute the @code{lpr} command if
|
|---|
| 2791 | either source file has changed since the last @samp{make print}. The
|
|---|
| 2792 | automatic variable @samp{$?} is used to print only those files that have
|
|---|
| 2793 | changed (@pxref{Automatic Variables}).
|
|---|
| 2794 |
|
|---|
| 2795 | @node Special Targets, Multiple Targets, Empty Targets, Rules
|
|---|
| 2796 | @section Special Built-in Target Names
|
|---|
| 2797 | @cindex special targets
|
|---|
| 2798 | @cindex built-in special targets
|
|---|
| 2799 | @cindex targets, built-in special
|
|---|
| 2800 |
|
|---|
| 2801 | Certain names have special meanings if they appear as targets.
|
|---|
| 2802 |
|
|---|
| 2803 | @table @code
|
|---|
| 2804 | @findex .PHONY
|
|---|
| 2805 | @item .PHONY
|
|---|
| 2806 |
|
|---|
| 2807 | The prerequisites of the special target @code{.PHONY} are considered to
|
|---|
| 2808 | be phony targets. When it is time to consider such a target,
|
|---|
| 2809 | @code{make} will run its commands unconditionally, regardless of
|
|---|
| 2810 | whether a file with that name exists or what its last-modification
|
|---|
| 2811 | time is. @xref{Phony Targets, ,Phony Targets}.
|
|---|
| 2812 |
|
|---|
| 2813 | @findex .SUFFIXES
|
|---|
| 2814 | @item .SUFFIXES
|
|---|
| 2815 |
|
|---|
| 2816 | The prerequisites of the special target @code{.SUFFIXES} are the list
|
|---|
| 2817 | of suffixes to be used in checking for suffix rules.
|
|---|
| 2818 | @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
|
|---|
| 2819 |
|
|---|
| 2820 | @findex .DEFAULT
|
|---|
| 2821 | @item .DEFAULT
|
|---|
| 2822 |
|
|---|
| 2823 | The commands specified for @code{.DEFAULT} are used for any target for
|
|---|
| 2824 | which no rules are found (either explicit rules or implicit rules).
|
|---|
| 2825 | @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
|
|---|
| 2826 | file mentioned as a prerequisite, but not as a target in a rule, will have
|
|---|
| 2827 | these commands executed on its behalf. @xref{Implicit Rule Search,
|
|---|
| 2828 | ,Implicit Rule Search Algorithm}.
|
|---|
| 2829 |
|
|---|
| 2830 | @findex .PRECIOUS
|
|---|
| 2831 | @item .PRECIOUS
|
|---|
| 2832 | @cindex precious targets
|
|---|
| 2833 | @cindex preserving with @code{.PRECIOUS}
|
|---|
| 2834 |
|
|---|
| 2835 | The targets which @code{.PRECIOUS} depends on are given the following
|
|---|
| 2836 | special treatment: if @code{make} is killed or interrupted during the
|
|---|
| 2837 | execution of their commands, the target is not deleted.
|
|---|
| 2838 | @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
|
|---|
| 2839 | target is an intermediate file, it will not be deleted after it is no
|
|---|
| 2840 | longer needed, as is normally done. @xref{Chained Rules, ,Chains of
|
|---|
| 2841 | Implicit Rules}. In this latter respect it overlaps with the
|
|---|
| 2842 | @code{.SECONDARY} special target.
|
|---|
| 2843 |
|
|---|
| 2844 | You can also list the target pattern of an implicit rule (such as
|
|---|
| 2845 | @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
|
|---|
| 2846 | to preserve intermediate files created by rules whose target patterns
|
|---|
| 2847 | match that file's name.
|
|---|
| 2848 |
|
|---|
| 2849 | @findex .INTERMEDIATE
|
|---|
| 2850 | @item .INTERMEDIATE
|
|---|
| 2851 | @cindex intermediate targets, explicit
|
|---|
| 2852 |
|
|---|
| 2853 | The targets which @code{.INTERMEDIATE} depends on are treated as
|
|---|
| 2854 | intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 2855 | @code{.INTERMEDIATE} with no prerequisites has no effect.
|
|---|
| 2856 |
|
|---|
| 2857 | @findex .SECONDARY
|
|---|
| 2858 | @item .SECONDARY
|
|---|
| 2859 | @cindex secondary targets
|
|---|
| 2860 | @cindex preserving with @code{.SECONDARY}
|
|---|
| 2861 |
|
|---|
| 2862 | The targets which @code{.SECONDARY} depends on are treated as
|
|---|
| 2863 | intermediate files, except that they are never automatically deleted.
|
|---|
| 2864 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 2865 |
|
|---|
| 2866 | @code{.SECONDARY} with no prerequisites causes all targets to be treated
|
|---|
| 2867 | as secondary (i.e., no target is removed because it is considered
|
|---|
| 2868 | intermediate).
|
|---|
| 2869 |
|
|---|
| 2870 | @findex .DELETE_ON_ERROR
|
|---|
| 2871 | @item .DELETE_ON_ERROR
|
|---|
| 2872 | @cindex removing targets on failure
|
|---|
| 2873 |
|
|---|
| 2874 | If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
|
|---|
| 2875 | makefile, then @code{make} will delete the target of a rule if it has
|
|---|
| 2876 | changed and its commands exit with a nonzero exit status, just as it
|
|---|
| 2877 | does when it receives a signal. @xref{Errors, ,Errors in Commands}.
|
|---|
| 2878 |
|
|---|
| 2879 | @findex .IGNORE
|
|---|
| 2880 | @item .IGNORE
|
|---|
| 2881 |
|
|---|
| 2882 | If you specify prerequisites for @code{.IGNORE}, then @code{make} will
|
|---|
| 2883 | ignore errors in execution of the commands run for those particular
|
|---|
| 2884 | files. The commands for @code{.IGNORE} are not meaningful.
|
|---|
| 2885 |
|
|---|
| 2886 | If mentioned as a target with no prerequisites, @code{.IGNORE} says to
|
|---|
| 2887 | ignore errors in execution of commands for all files. This usage of
|
|---|
| 2888 | @samp{.IGNORE} is supported only for historical compatibility. Since
|
|---|
| 2889 | this affects every command in the makefile, it is not very useful; we
|
|---|
| 2890 | recommend you use the more selective ways to ignore errors in specific
|
|---|
| 2891 | commands. @xref{Errors, ,Errors in Commands}.
|
|---|
| 2892 |
|
|---|
| 2893 | @findex .LOW_RESOLUTION_TIME
|
|---|
| 2894 | @item .LOW_RESOLUTION_TIME
|
|---|
| 2895 |
|
|---|
| 2896 | If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
|
|---|
| 2897 | @command{make} assumes that these files are created by commands that
|
|---|
| 2898 | generate low resolution time stamps. The commands for
|
|---|
| 2899 | @code{.LOW_RESOLUTION_TIME} are not meaningful.
|
|---|
| 2900 |
|
|---|
| 2901 | The high resolution file time stamps of many modern hosts lessen the
|
|---|
| 2902 | chance of @command{make} incorrectly concluding that a file is up to
|
|---|
| 2903 | date. Unfortunately, these hosts provide no way to set a high
|
|---|
| 2904 | resolution file time stamp, so commands like @samp{cp -p} that
|
|---|
| 2905 | explicitly set a file's time stamp must discard its subsecond part. If
|
|---|
| 2906 | a file is created by such a command, you should list it as a
|
|---|
| 2907 | prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
|
|---|
| 2908 | not mistakenly conclude that the file is out of date. For example:
|
|---|
| 2909 |
|
|---|
| 2910 | @example
|
|---|
| 2911 | @group
|
|---|
| 2912 | .LOW_RESOLUTION_TIME: dst
|
|---|
| 2913 | dst: src
|
|---|
| 2914 | cp -p src dst
|
|---|
| 2915 | @end group
|
|---|
| 2916 | @end example
|
|---|
| 2917 |
|
|---|
| 2918 | Since @samp{cp -p} discards the subsecond part of @file{src}'s time
|
|---|
| 2919 | stamp, @file{dst} is typically slightly older than @file{src} even when
|
|---|
| 2920 | it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
|
|---|
| 2921 | @command{make} to consider @file{dst} to be up to date if its time stamp
|
|---|
| 2922 | is at the start of the same second that @file{src}'s time stamp is in.
|
|---|
| 2923 |
|
|---|
| 2924 | Due to a limitation of the archive format, archive member time stamps
|
|---|
| 2925 | are always low resolution. You need not list archive members as
|
|---|
| 2926 | prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
|
|---|
| 2927 | automatically.
|
|---|
| 2928 |
|
|---|
| 2929 | @findex .SILENT
|
|---|
| 2930 | @item .SILENT
|
|---|
| 2931 |
|
|---|
| 2932 | If you specify prerequisites for @code{.SILENT}, then @code{make} will
|
|---|
| 2933 | not print the commands to remake those particular files before executing
|
|---|
| 2934 | them. The commands for @code{.SILENT} are not meaningful.
|
|---|
| 2935 |
|
|---|
| 2936 | If mentioned as a target with no prerequisites, @code{.SILENT} says not
|
|---|
| 2937 | to print any commands before executing them. This usage of
|
|---|
| 2938 | @samp{.SILENT} is supported only for historical compatibility. We
|
|---|
| 2939 | recommend you use the more selective ways to silence specific commands.
|
|---|
| 2940 | @xref{Echoing, ,Command Echoing}. If you want to silence all commands
|
|---|
| 2941 | for a particular run of @code{make}, use the @samp{-s} or
|
|---|
| 2942 | @w{@samp{--silent}} option (@pxref{Options Summary}).
|
|---|
| 2943 |
|
|---|
| 2944 | @findex .EXPORT_ALL_VARIABLES
|
|---|
| 2945 | @item .EXPORT_ALL_VARIABLES
|
|---|
| 2946 |
|
|---|
| 2947 | Simply by being mentioned as a target, this tells @code{make} to
|
|---|
| 2948 | export all variables to child processes by default.
|
|---|
| 2949 | @xref{Variables/Recursion, ,Communicating Variables to a
|
|---|
| 2950 | Sub-@code{make}}.
|
|---|
| 2951 |
|
|---|
| 2952 | @findex .NOTPARALLEL
|
|---|
| 2953 | @item .NOTPARALLEL
|
|---|
| 2954 | @cindex parallel execution, overriding
|
|---|
| 2955 |
|
|---|
| 2956 | If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
|
|---|
| 2957 | @code{make} will be run serially, even if the @samp{-j} option is
|
|---|
| 2958 | given. Any recursively invoked @code{make} command will still be run in
|
|---|
| 2959 | parallel (unless its makefile contains this target). Any prerequisites
|
|---|
| 2960 | on this target are ignored.
|
|---|
| 2961 | @end table
|
|---|
| 2962 |
|
|---|
| 2963 | Any defined implicit rule suffix also counts as a special target if it
|
|---|
| 2964 | appears as a target, and so does the concatenation of two suffixes, such
|
|---|
| 2965 | as @samp{.c.o}. These targets are suffix rules, an obsolete way of
|
|---|
| 2966 | defining implicit rules (but a way still widely used). In principle, any
|
|---|
| 2967 | target name could be special in this way if you break it in two and add
|
|---|
| 2968 | both pieces to the suffix list. In practice, suffixes normally begin with
|
|---|
| 2969 | @samp{.}, so these special target names also begin with @samp{.}.
|
|---|
| 2970 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 2971 |
|
|---|
| 2972 | @node Multiple Targets, Multiple Rules, Special Targets, Rules
|
|---|
| 2973 | @section Multiple Targets in a Rule
|
|---|
| 2974 | @cindex multiple targets
|
|---|
| 2975 | @cindex several targets in a rule
|
|---|
| 2976 | @cindex targets, multiple
|
|---|
| 2977 | @cindex rule, with multiple targets
|
|---|
| 2978 |
|
|---|
| 2979 | A rule with multiple targets is equivalent to writing many rules, each with
|
|---|
| 2980 | one target, and all identical aside from that. The same commands apply to
|
|---|
| 2981 | all the targets, but their effects may vary because you can substitute the
|
|---|
| 2982 | actual target name into the command using @samp{$@@}. The rule contributes
|
|---|
| 2983 | the same prerequisites to all the targets also.
|
|---|
| 2984 |
|
|---|
| 2985 | This is useful in two cases.
|
|---|
| 2986 |
|
|---|
| 2987 | @itemize @bullet
|
|---|
| 2988 | @item
|
|---|
| 2989 | You want just prerequisites, no commands. For example:
|
|---|
| 2990 |
|
|---|
| 2991 | @example
|
|---|
| 2992 | kbd.o command.o files.o: command.h
|
|---|
| 2993 | @end example
|
|---|
| 2994 |
|
|---|
| 2995 | @noindent
|
|---|
| 2996 | gives an additional prerequisite to each of the three object files
|
|---|
| 2997 | mentioned.
|
|---|
| 2998 |
|
|---|
| 2999 | @item
|
|---|
| 3000 | Similar commands work for all the targets. The commands do not need
|
|---|
| 3001 | to be absolutely identical, since the automatic variable @samp{$@@}
|
|---|
| 3002 | can be used to substitute the particular target to be remade into the
|
|---|
| 3003 | commands (@pxref{Automatic Variables}). For example:
|
|---|
| 3004 |
|
|---|
| 3005 | @example
|
|---|
| 3006 | @group
|
|---|
| 3007 | bigoutput littleoutput : text.g
|
|---|
| 3008 | generate text.g -$(subst output,,$@@) > $@@
|
|---|
| 3009 | @end group
|
|---|
| 3010 | @end example
|
|---|
| 3011 | @findex subst
|
|---|
| 3012 |
|
|---|
| 3013 | @noindent
|
|---|
| 3014 | is equivalent to
|
|---|
| 3015 |
|
|---|
| 3016 | @example
|
|---|
| 3017 | bigoutput : text.g
|
|---|
| 3018 | generate text.g -big > bigoutput
|
|---|
| 3019 | littleoutput : text.g
|
|---|
| 3020 | generate text.g -little > littleoutput
|
|---|
| 3021 | @end example
|
|---|
| 3022 |
|
|---|
| 3023 | @noindent
|
|---|
| 3024 | Here we assume the hypothetical program @code{generate} makes two
|
|---|
| 3025 | types of output, one if given @samp{-big} and one if given
|
|---|
| 3026 | @samp{-little}.
|
|---|
| 3027 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
|---|
| 3028 | for an explanation of the @code{subst} function.
|
|---|
| 3029 | @end itemize
|
|---|
| 3030 |
|
|---|
| 3031 | Suppose you would like to vary the prerequisites according to the target,
|
|---|
| 3032 | much as the variable @samp{$@@} allows you to vary the commands.
|
|---|
| 3033 | You cannot do this with multiple targets in an ordinary rule, but you can
|
|---|
| 3034 | do it with a @dfn{static pattern rule}.
|
|---|
| 3035 | @xref{Static Pattern, ,Static Pattern Rules}.
|
|---|
| 3036 |
|
|---|
| 3037 | @node Multiple Rules, Static Pattern, Multiple Targets, Rules
|
|---|
| 3038 | @section Multiple Rules for One Target
|
|---|
| 3039 | @cindex multiple rules for one target
|
|---|
| 3040 | @cindex several rules for one target
|
|---|
| 3041 | @cindex rule, multiple for one target
|
|---|
| 3042 | @cindex target, multiple rules for one
|
|---|
| 3043 |
|
|---|
| 3044 | One file can be the target of several rules. All the prerequisites
|
|---|
| 3045 | mentioned in all the rules are merged into one list of prerequisites for
|
|---|
| 3046 | the target. If the target is older than any prerequisite from any rule,
|
|---|
| 3047 | the commands are executed.
|
|---|
| 3048 |
|
|---|
| 3049 | There can only be one set of commands to be executed for a file. If
|
|---|
| 3050 | more than one rule gives commands for the same file, @code{make} uses
|
|---|
| 3051 | the last set given and prints an error message. (As a special case,
|
|---|
| 3052 | if the file's name begins with a dot, no error message is printed.
|
|---|
| 3053 | This odd behavior is only for compatibility with other implementations
|
|---|
| 3054 | of @code{make}... you should avoid using it). Occasionally it is
|
|---|
| 3055 | useful to have the same target invoke multiple commands which are
|
|---|
| 3056 | defined in different parts of your makefile; you can use
|
|---|
| 3057 | @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
|
|---|
| 3058 |
|
|---|
| 3059 | An extra rule with just prerequisites can be used to give a few extra
|
|---|
| 3060 | prerequisites to many files at once. For example, makefiles often
|
|---|
| 3061 | have a variable, such as @code{objects}, containing a list of all the
|
|---|
| 3062 | compiler output files in the system being made. An easy way to say
|
|---|
| 3063 | that all of them must be recompiled if @file{config.h} changes is to
|
|---|
| 3064 | write the following:
|
|---|
| 3065 |
|
|---|
| 3066 | @example
|
|---|
| 3067 | objects = foo.o bar.o
|
|---|
| 3068 | foo.o : defs.h
|
|---|
| 3069 | bar.o : defs.h test.h
|
|---|
| 3070 | $(objects) : config.h
|
|---|
| 3071 | @end example
|
|---|
| 3072 |
|
|---|
| 3073 | This could be inserted or taken out without changing the rules that really
|
|---|
| 3074 | specify how to make the object files, making it a convenient form to use if
|
|---|
| 3075 | you wish to add the additional prerequisite intermittently.
|
|---|
| 3076 |
|
|---|
| 3077 | Another wrinkle is that the additional prerequisites could be specified with
|
|---|
| 3078 | a variable that you set with a command argument to @code{make}
|
|---|
| 3079 | (@pxref{Overriding, ,Overriding Variables}). For example,
|
|---|
| 3080 |
|
|---|
| 3081 | @example
|
|---|
| 3082 | @group
|
|---|
| 3083 | extradeps=
|
|---|
| 3084 | $(objects) : $(extradeps)
|
|---|
| 3085 | @end group
|
|---|
| 3086 | @end example
|
|---|
| 3087 |
|
|---|
| 3088 | @noindent
|
|---|
| 3089 | means that the command @samp{make extradeps=foo.h} will consider
|
|---|
| 3090 | @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
|
|---|
| 3091 | will not.
|
|---|
| 3092 |
|
|---|
| 3093 | If none of the explicit rules for a target has commands, then @code{make}
|
|---|
| 3094 | searches for an applicable implicit rule to find some commands
|
|---|
| 3095 | @pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 3096 |
|
|---|
| 3097 | @node Static Pattern, Double-Colon, Multiple Rules, Rules
|
|---|
| 3098 | @section Static Pattern Rules
|
|---|
| 3099 | @cindex static pattern rule
|
|---|
| 3100 | @cindex rule, static pattern
|
|---|
| 3101 | @cindex pattern rules, static (not implicit)
|
|---|
| 3102 | @cindex varying prerequisites
|
|---|
| 3103 | @cindex prerequisites, varying (static pattern)
|
|---|
| 3104 |
|
|---|
| 3105 | @dfn{Static pattern rules} are rules which specify multiple targets and
|
|---|
| 3106 | construct the prerequisite names for each target based on the target name.
|
|---|
| 3107 | They are more general than ordinary rules with multiple targets because the
|
|---|
| 3108 | targets do not have to have identical prerequisites. Their prerequisites must
|
|---|
| 3109 | be @emph{analogous}, but not necessarily @emph{identical}.
|
|---|
| 3110 |
|
|---|
| 3111 | @menu
|
|---|
| 3112 | * Static Usage:: The syntax of static pattern rules.
|
|---|
| 3113 | * Static versus Implicit:: When are they better than implicit rules?
|
|---|
| 3114 | @end menu
|
|---|
| 3115 |
|
|---|
| 3116 | @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
|
|---|
| 3117 | @subsection Syntax of Static Pattern Rules
|
|---|
| 3118 | @cindex static pattern rule, syntax of
|
|---|
| 3119 | @cindex pattern rules, static, syntax of
|
|---|
| 3120 |
|
|---|
| 3121 | Here is the syntax of a static pattern rule:
|
|---|
| 3122 |
|
|---|
| 3123 | @example
|
|---|
| 3124 | @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
|
|---|
| 3125 | @var{commands}
|
|---|
| 3126 | @dots{}
|
|---|
| 3127 | @end example
|
|---|
| 3128 |
|
|---|
| 3129 | @noindent
|
|---|
| 3130 | The @var{targets} list specifies the targets that the rule applies to.
|
|---|
| 3131 | The targets can contain wildcard characters, just like the targets of
|
|---|
| 3132 | ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
|
|---|
| 3133 | Names}).
|
|---|
| 3134 |
|
|---|
| 3135 | @cindex target pattern, static (not implicit)
|
|---|
| 3136 | @cindex stem
|
|---|
| 3137 | The @var{target-pattern} and @var{prereq-patterns} say how to compute the
|
|---|
| 3138 | prerequisites of each target. Each target is matched against the
|
|---|
| 3139 | @var{target-pattern} to extract a part of the target name, called the
|
|---|
| 3140 | @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
|
|---|
| 3141 | to make the prerequisite names (one from each @var{prereq-pattern}).
|
|---|
| 3142 |
|
|---|
| 3143 | Each pattern normally contains the character @samp{%} just once. When the
|
|---|
| 3144 | @var{target-pattern} matches a target, the @samp{%} can match any part of
|
|---|
| 3145 | the target name; this part is called the @dfn{stem}. The rest of the
|
|---|
| 3146 | pattern must match exactly. For example, the target @file{foo.o} matches
|
|---|
| 3147 | the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
|
|---|
| 3148 | @file{foo.c} and @file{foo.out} do not match that pattern.@refill
|
|---|
| 3149 |
|
|---|
| 3150 | @cindex prerequisite pattern, static (not implicit)
|
|---|
| 3151 | The prerequisite names for each target are made by substituting the stem
|
|---|
| 3152 | for the @samp{%} in each prerequisite pattern. For example, if one
|
|---|
| 3153 | prerequisite pattern is @file{%.c}, then substitution of the stem
|
|---|
| 3154 | @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
|
|---|
| 3155 | to write a prerequisite pattern that does not contain @samp{%}; then this
|
|---|
| 3156 | prerequisite is the same for all targets.
|
|---|
| 3157 |
|
|---|
| 3158 | @cindex @code{%}, quoting in static pattern
|
|---|
| 3159 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 3160 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 3161 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 3162 | @cindex quoting @code{%}, in static pattern
|
|---|
| 3163 | @samp{%} characters in pattern rules can be quoted with preceding
|
|---|
| 3164 | backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
|
|---|
| 3165 | characters can be quoted with more backslashes. Backslashes that quote
|
|---|
| 3166 | @samp{%} characters or other backslashes are removed from the pattern
|
|---|
| 3167 | before it is compared to file names or has a stem substituted into it.
|
|---|
| 3168 | Backslashes that are not in danger of quoting @samp{%} characters go
|
|---|
| 3169 | unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
|
|---|
| 3170 | @samp{the%weird\} preceding the operative @samp{%} character, and
|
|---|
| 3171 | @samp{pattern\\} following it. The final two backslashes are left alone
|
|---|
| 3172 | because they cannot affect any @samp{%} character.@refill
|
|---|
| 3173 |
|
|---|
| 3174 | Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
|
|---|
| 3175 | from the corresponding @file{.c} file:
|
|---|
| 3176 |
|
|---|
| 3177 | @example
|
|---|
| 3178 | @group
|
|---|
| 3179 | objects = foo.o bar.o
|
|---|
| 3180 |
|
|---|
| 3181 | all: $(objects)
|
|---|
| 3182 |
|
|---|
| 3183 | $(objects): %.o: %.c
|
|---|
| 3184 | $(CC) -c $(CFLAGS) $< -o $@@
|
|---|
| 3185 | @end group
|
|---|
| 3186 | @end example
|
|---|
| 3187 |
|
|---|
| 3188 | @noindent
|
|---|
| 3189 | Here @samp{$<} is the automatic variable that holds the name of the
|
|---|
| 3190 | prerequisite and @samp{$@@} is the automatic variable that holds the name
|
|---|
| 3191 | of the target; see @ref{Automatic Variables}.
|
|---|
| 3192 |
|
|---|
| 3193 | Each target specified must match the target pattern; a warning is issued
|
|---|
| 3194 | for each target that does not. If you have a list of files, only some of
|
|---|
| 3195 | which will match the pattern, you can use the @code{filter} function to
|
|---|
| 3196 | remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
|---|
| 3197 |
|
|---|
| 3198 | @example
|
|---|
| 3199 | files = foo.elc bar.o lose.o
|
|---|
| 3200 |
|
|---|
| 3201 | $(filter %.o,$(files)): %.o: %.c
|
|---|
| 3202 | $(CC) -c $(CFLAGS) $< -o $@@
|
|---|
| 3203 | $(filter %.elc,$(files)): %.elc: %.el
|
|---|
| 3204 | emacs -f batch-byte-compile $<
|
|---|
| 3205 | @end example
|
|---|
| 3206 |
|
|---|
| 3207 | @noindent
|
|---|
| 3208 | In this example the result of @samp{$(filter %.o,$(files))} is
|
|---|
| 3209 | @file{bar.o lose.o}, and the first static pattern rule causes each of
|
|---|
| 3210 | these object files to be updated by compiling the corresponding C source
|
|---|
| 3211 | file. The result of @w{@samp{$(filter %.elc,$(files))}} is
|
|---|
| 3212 | @file{foo.elc}, so that file is made from @file{foo.el}.@refill
|
|---|
| 3213 |
|
|---|
| 3214 | Another example shows how to use @code{$*} in static pattern rules:
|
|---|
| 3215 | @vindex $*@r{, and static pattern}
|
|---|
| 3216 |
|
|---|
| 3217 | @example
|
|---|
| 3218 | @group
|
|---|
| 3219 | bigoutput littleoutput : %output : text.g
|
|---|
| 3220 | generate text.g -$* > $@@
|
|---|
| 3221 | @end group
|
|---|
| 3222 | @end example
|
|---|
| 3223 |
|
|---|
| 3224 | @noindent
|
|---|
| 3225 | When the @code{generate} command is run, @code{$*} will expand to the
|
|---|
| 3226 | stem, either @samp{big} or @samp{little}.
|
|---|
| 3227 |
|
|---|
| 3228 | @node Static versus Implicit, , Static Usage, Static Pattern
|
|---|
| 3229 | @subsection Static Pattern Rules versus Implicit Rules
|
|---|
| 3230 | @cindex rule, static pattern versus implicit
|
|---|
| 3231 | @cindex static pattern rule, versus implicit
|
|---|
| 3232 |
|
|---|
| 3233 | A static pattern rule has much in common with an implicit rule defined as a
|
|---|
| 3234 | pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
|---|
| 3235 | Both have a pattern for the target and patterns for constructing the
|
|---|
| 3236 | names of prerequisites. The difference is in how @code{make} decides
|
|---|
| 3237 | @emph{when} the rule applies.
|
|---|
| 3238 |
|
|---|
| 3239 | An implicit rule @emph{can} apply to any target that matches its pattern,
|
|---|
| 3240 | but it @emph{does} apply only when the target has no commands otherwise
|
|---|
| 3241 | specified, and only when the prerequisites can be found. If more than one
|
|---|
| 3242 | implicit rule appears applicable, only one applies; the choice depends on
|
|---|
| 3243 | the order of rules.
|
|---|
| 3244 |
|
|---|
| 3245 | By contrast, a static pattern rule applies to the precise list of targets
|
|---|
| 3246 | that you specify in the rule. It cannot apply to any other target and it
|
|---|
| 3247 | invariably does apply to each of the targets specified. If two conflicting
|
|---|
| 3248 | rules apply, and both have commands, that's an error.
|
|---|
| 3249 |
|
|---|
| 3250 | The static pattern rule can be better than an implicit rule for these
|
|---|
| 3251 | reasons:
|
|---|
| 3252 |
|
|---|
| 3253 | @itemize @bullet
|
|---|
| 3254 | @item
|
|---|
| 3255 | You may wish to override the usual implicit rule for a few
|
|---|
| 3256 | files whose names cannot be categorized syntactically but
|
|---|
| 3257 | can be given in an explicit list.
|
|---|
| 3258 |
|
|---|
| 3259 | @item
|
|---|
| 3260 | If you cannot be sure of the precise contents of the directories
|
|---|
| 3261 | you are using, you may not be sure which other irrelevant files
|
|---|
| 3262 | might lead @code{make} to use the wrong implicit rule. The choice
|
|---|
| 3263 | might depend on the order in which the implicit rule search is done.
|
|---|
| 3264 | With static pattern rules, there is no uncertainty: each rule applies
|
|---|
| 3265 | to precisely the targets specified.
|
|---|
| 3266 | @end itemize
|
|---|
| 3267 |
|
|---|
| 3268 | @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
|
|---|
| 3269 | @section Double-Colon Rules
|
|---|
| 3270 | @cindex double-colon rules
|
|---|
| 3271 | @cindex rule, double-colon (@code{::})
|
|---|
| 3272 | @cindex multiple rules for one target (@code{::})
|
|---|
| 3273 | @cindex @code{::} rules (double-colon)
|
|---|
| 3274 |
|
|---|
| 3275 | @dfn{Double-colon} rules are rules written with @samp{::} instead of
|
|---|
| 3276 | @samp{:} after the target names. They are handled differently from
|
|---|
| 3277 | ordinary rules when the same target appears in more than one rule.
|
|---|
| 3278 |
|
|---|
| 3279 | When a target appears in multiple rules, all the rules must be the same
|
|---|
| 3280 | type: all ordinary, or all double-colon. If they are double-colon, each
|
|---|
| 3281 | of them is independent of the others. Each double-colon rule's commands
|
|---|
| 3282 | are executed if the target is older than any prerequisites of that rule.
|
|---|
| 3283 | If there are no prerequisites for that rule, its commands are always
|
|---|
| 3284 | executed (even if the target already exists). This can result in
|
|---|
| 3285 | executing none, any, or all of the double-colon rules.
|
|---|
| 3286 |
|
|---|
| 3287 | Double-colon rules with the same target are in fact completely separate
|
|---|
| 3288 | from one another. Each double-colon rule is processed individually, just
|
|---|
| 3289 | as rules with different targets are processed.
|
|---|
| 3290 |
|
|---|
| 3291 | The double-colon rules for a target are executed in the order they appear
|
|---|
| 3292 | in the makefile. However, the cases where double-colon rules really make
|
|---|
| 3293 | sense are those where the order of executing the commands would not matter.
|
|---|
| 3294 |
|
|---|
| 3295 | Double-colon rules are somewhat obscure and not often very useful; they
|
|---|
| 3296 | provide a mechanism for cases in which the method used to update a target
|
|---|
| 3297 | differs depending on which prerequisite files caused the update, and such
|
|---|
| 3298 | cases are rare.
|
|---|
| 3299 |
|
|---|
| 3300 | Each double-colon rule should specify commands; if it does not, an
|
|---|
| 3301 | implicit rule will be used if one applies.
|
|---|
| 3302 | @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 3303 |
|
|---|
| 3304 | @node Automatic Prerequisites, , Double-Colon, Rules
|
|---|
| 3305 | @section Generating Prerequisites Automatically
|
|---|
| 3306 | @cindex prerequisites, automatic generation
|
|---|
| 3307 | @cindex automatic generation of prerequisites
|
|---|
| 3308 | @cindex generating prerequisites automatically
|
|---|
| 3309 |
|
|---|
| 3310 | In the makefile for a program, many of the rules you need to write often
|
|---|
| 3311 | say only that some object file depends on some header
|
|---|
| 3312 | file. For example, if @file{main.c} uses @file{defs.h} via an
|
|---|
| 3313 | @code{#include}, you would write:
|
|---|
| 3314 |
|
|---|
| 3315 | @example
|
|---|
| 3316 | main.o: defs.h
|
|---|
| 3317 | @end example
|
|---|
| 3318 |
|
|---|
| 3319 | @noindent
|
|---|
| 3320 | You need this rule so that @code{make} knows that it must remake
|
|---|
| 3321 | @file{main.o} whenever @file{defs.h} changes. You can see that for a
|
|---|
| 3322 | large program you would have to write dozens of such rules in your
|
|---|
| 3323 | makefile. And, you must always be very careful to update the makefile
|
|---|
| 3324 | every time you add or remove an @code{#include}.
|
|---|
| 3325 | @cindex @code{#include}
|
|---|
| 3326 |
|
|---|
| 3327 | @cindex @code{-M} (to compiler)
|
|---|
| 3328 | To avoid this hassle, most modern C compilers can write these rules for
|
|---|
| 3329 | you, by looking at the @code{#include} lines in the source files.
|
|---|
| 3330 | Usually this is done with the @samp{-M} option to the compiler.
|
|---|
| 3331 | For example, the command:
|
|---|
| 3332 |
|
|---|
| 3333 | @example
|
|---|
| 3334 | cc -M main.c
|
|---|
| 3335 | @end example
|
|---|
| 3336 |
|
|---|
| 3337 | @noindent
|
|---|
| 3338 | generates the output:
|
|---|
| 3339 |
|
|---|
| 3340 | @example
|
|---|
| 3341 | main.o : main.c defs.h
|
|---|
| 3342 | @end example
|
|---|
| 3343 |
|
|---|
| 3344 | @noindent
|
|---|
| 3345 | Thus you no longer have to write all those rules yourself.
|
|---|
| 3346 | The compiler will do it for you.
|
|---|
| 3347 |
|
|---|
| 3348 | Note that such a prerequisite constitutes mentioning @file{main.o} in a
|
|---|
| 3349 | makefile, so it can never be considered an intermediate file by implicit
|
|---|
| 3350 | rule search. This means that @code{make} won't ever remove the file
|
|---|
| 3351 | after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 3352 |
|
|---|
| 3353 | @cindex @code{make depend}
|
|---|
| 3354 | With old @code{make} programs, it was traditional practice to use this
|
|---|
| 3355 | compiler feature to generate prerequisites on demand with a command like
|
|---|
| 3356 | @samp{make depend}. That command would create a file @file{depend}
|
|---|
| 3357 | containing all the automatically-generated prerequisites; then the
|
|---|
| 3358 | makefile could use @code{include} to read them in (@pxref{Include}).
|
|---|
| 3359 |
|
|---|
| 3360 | In GNU @code{make}, the feature of remaking makefiles makes this
|
|---|
| 3361 | practice obsolete---you need never tell @code{make} explicitly to
|
|---|
| 3362 | regenerate the prerequisites, because it always regenerates any makefile
|
|---|
| 3363 | that is out of date. @xref{Remaking Makefiles}.
|
|---|
| 3364 |
|
|---|
| 3365 | The practice we recommend for automatic prerequisite generation is to have
|
|---|
| 3366 | one makefile corresponding to each source file. For each source file
|
|---|
| 3367 | @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
|
|---|
| 3368 | what files the object file @file{@var{name}.o} depends on. That way
|
|---|
| 3369 | only the source files that have changed need to be rescanned to produce
|
|---|
| 3370 | the new prerequisites.
|
|---|
| 3371 |
|
|---|
| 3372 | Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
|
|---|
| 3373 | called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
|
|---|
| 3374 |
|
|---|
| 3375 | @smallexample
|
|---|
| 3376 | @group
|
|---|
| 3377 | %.d: %.c
|
|---|
| 3378 | @set -e; rm -f $@@; \
|
|---|
| 3379 | $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
|
|---|
| 3380 | sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
|
|---|
| 3381 | rm -f $@@.$$$$
|
|---|
| 3382 | @end group
|
|---|
| 3383 | @end smallexample
|
|---|
| 3384 |
|
|---|
| 3385 | @noindent
|
|---|
| 3386 | @xref{Pattern Rules}, for information on defining pattern rules. The
|
|---|
| 3387 | @samp{-e} flag to the shell causes it to exit immediately if the
|
|---|
| 3388 | @code{$(CC)} command (or any other command) fails (exits with a
|
|---|
| 3389 | nonzero status).
|
|---|
| 3390 | @cindex @code{-e} (shell flag)
|
|---|
| 3391 |
|
|---|
| 3392 | @cindex @code{-MM} (to GNU compiler)
|
|---|
| 3393 | With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
|
|---|
| 3394 | of @samp{-M}. This omits prerequisites on system header files.
|
|---|
| 3395 | @xref{Preprocessor Options, , Options Controlling the Preprocessor,
|
|---|
| 3396 | gcc.info, Using GNU CC}, for details.
|
|---|
| 3397 |
|
|---|
| 3398 | @cindex @code{sed} (shell command)
|
|---|
| 3399 | The purpose of the @code{sed} command is to translate (for example):
|
|---|
| 3400 |
|
|---|
| 3401 | @example
|
|---|
| 3402 | main.o : main.c defs.h
|
|---|
| 3403 | @end example
|
|---|
| 3404 |
|
|---|
| 3405 | @noindent
|
|---|
| 3406 | into:
|
|---|
| 3407 |
|
|---|
| 3408 | @example
|
|---|
| 3409 | main.o main.d : main.c defs.h
|
|---|
| 3410 | @end example
|
|---|
| 3411 |
|
|---|
| 3412 | @noindent
|
|---|
| 3413 | @cindex @code{.d}
|
|---|
| 3414 | This makes each @samp{.d} file depend on all the source and header files
|
|---|
| 3415 | that the corresponding @samp{.o} file depends on. @code{make} then
|
|---|
| 3416 | knows it must regenerate the prerequisites whenever any of the source or
|
|---|
| 3417 | header files changes.
|
|---|
| 3418 |
|
|---|
| 3419 | Once you've defined the rule to remake the @samp{.d} files,
|
|---|
| 3420 | you then use the @code{include} directive to read them all in.
|
|---|
| 3421 | @xref{Include}. For example:
|
|---|
| 3422 |
|
|---|
| 3423 | @example
|
|---|
| 3424 | @group
|
|---|
| 3425 | sources = foo.c bar.c
|
|---|
| 3426 |
|
|---|
| 3427 | include $(sources:.c=.d)
|
|---|
| 3428 | @end group
|
|---|
| 3429 | @end example
|
|---|
| 3430 |
|
|---|
| 3431 | @noindent
|
|---|
| 3432 | (This example uses a substitution variable reference to translate the
|
|---|
| 3433 | list of source files @samp{foo.c bar.c} into a list of prerequisite
|
|---|
| 3434 | makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
|
|---|
| 3435 | information on substitution references.) Since the @samp{.d} files are
|
|---|
| 3436 | makefiles like any others, @code{make} will remake them as necessary
|
|---|
| 3437 | with no further work from you. @xref{Remaking Makefiles}.
|
|---|
| 3438 |
|
|---|
| 3439 | Note that the @samp{.d} files contain target definitions; you should
|
|---|
| 3440 | be sure to place the @code{include} directive @emph{after} the first,
|
|---|
| 3441 | default goal in your makefiles or run the risk of having a random
|
|---|
| 3442 | object file become the default goal.
|
|---|
| 3443 | @xref{How Make Works}.
|
|---|
| 3444 |
|
|---|
| 3445 | @node Commands, Using Variables, Rules, Top
|
|---|
| 3446 | @chapter Writing the Commands in Rules
|
|---|
| 3447 | @cindex commands, how to write
|
|---|
| 3448 | @cindex rule commands
|
|---|
| 3449 | @cindex writing rule commands
|
|---|
| 3450 |
|
|---|
| 3451 | The commands of a rule consist of shell command lines to be executed one
|
|---|
| 3452 | by one. Each command line must start with a tab, except that the first
|
|---|
| 3453 | command line may be attached to the target-and-prerequisites line with a
|
|---|
| 3454 | semicolon in between. Blank lines and lines of just comments may appear
|
|---|
| 3455 | among the command lines; they are ignored. (But beware, an apparently
|
|---|
| 3456 | ``blank'' line that begins with a tab is @emph{not} blank! It is an
|
|---|
| 3457 | empty command; @pxref{Empty Commands}.)
|
|---|
| 3458 |
|
|---|
| 3459 | Users use many different shell programs, but commands in makefiles are
|
|---|
| 3460 | always interpreted by @file{/bin/sh} unless the makefile specifies
|
|---|
| 3461 | otherwise. @xref{Execution, ,Command Execution}.
|
|---|
| 3462 |
|
|---|
| 3463 | @cindex comments, in commands
|
|---|
| 3464 | @cindex commands, comments in
|
|---|
| 3465 | @cindex @code{#} (comments), in commands
|
|---|
| 3466 | The shell that is in use determines whether comments can be written on
|
|---|
| 3467 | command lines, and what syntax they use. When the shell is
|
|---|
| 3468 | @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
|
|---|
| 3469 | the line. The @samp{#} does not have to be at the beginning of a line.
|
|---|
| 3470 | Text on a line before a @samp{#} is not part of the comment.
|
|---|
| 3471 |
|
|---|
| 3472 | @menu
|
|---|
| 3473 | * Echoing:: How to control when commands are echoed.
|
|---|
| 3474 | * Execution:: How commands are executed.
|
|---|
| 3475 | * Parallel:: How commands can be executed in parallel.
|
|---|
| 3476 | * Errors:: What happens after a command execution error.
|
|---|
| 3477 | * Interrupts:: What happens when a command is interrupted.
|
|---|
| 3478 | * Recursion:: Invoking @code{make} from makefiles.
|
|---|
| 3479 | * Sequences:: Defining canned sequences of commands.
|
|---|
| 3480 | * Empty Commands:: Defining useful, do-nothing commands.
|
|---|
| 3481 | @end menu
|
|---|
| 3482 |
|
|---|
| 3483 | @node Echoing, Execution, Commands, Commands
|
|---|
| 3484 | @section Command Echoing
|
|---|
| 3485 | @cindex echoing of commands
|
|---|
| 3486 | @cindex silent operation
|
|---|
| 3487 | @cindex @code{@@} (in commands)
|
|---|
| 3488 | @cindex commands, echoing
|
|---|
| 3489 | @cindex printing of commands
|
|---|
| 3490 |
|
|---|
| 3491 | Normally @code{make} prints each command line before it is executed.
|
|---|
| 3492 | We call this @dfn{echoing} because it gives the appearance that you
|
|---|
| 3493 | are typing the commands yourself.
|
|---|
| 3494 |
|
|---|
| 3495 | When a line starts with @samp{@@}, the echoing of that line is suppressed.
|
|---|
| 3496 | The @samp{@@} is discarded before the command is passed to the shell.
|
|---|
| 3497 | Typically you would use this for a command whose only effect is to print
|
|---|
| 3498 | something, such as an @code{echo} command to indicate progress through
|
|---|
| 3499 | the makefile:
|
|---|
| 3500 |
|
|---|
| 3501 | @example
|
|---|
| 3502 | @@echo About to make distribution files
|
|---|
| 3503 | @end example
|
|---|
| 3504 |
|
|---|
| 3505 | @cindex @code{-n}
|
|---|
| 3506 | @cindex @code{--just-print}
|
|---|
| 3507 | @cindex @code{--dry-run}
|
|---|
| 3508 | @cindex @code{--recon}
|
|---|
| 3509 | When @code{make} is given the flag @samp{-n} or @samp{--just-print}
|
|---|
| 3510 | it only echoes commands, it won't execute them. @xref{Options Summary,
|
|---|
| 3511 | ,Summary of Options}. In this case and only this case, even the
|
|---|
| 3512 | commands starting with @samp{@@} are printed. This flag is useful for
|
|---|
| 3513 | finding out which commands @code{make} thinks are necessary without
|
|---|
| 3514 | actually doing them.
|
|---|
| 3515 |
|
|---|
| 3516 | @cindex @code{-s}
|
|---|
| 3517 | @cindex @code{--silent}
|
|---|
| 3518 | @cindex @code{--quiet}
|
|---|
| 3519 | @findex .SILENT
|
|---|
| 3520 | The @samp{-s} or @samp{--silent}
|
|---|
| 3521 | flag to @code{make} prevents all echoing, as if all commands
|
|---|
| 3522 | started with @samp{@@}. A rule in the makefile for the special target
|
|---|
| 3523 | @code{.SILENT} without prerequisites has the same effect
|
|---|
| 3524 | (@pxref{Special Targets, ,Special Built-in Target Names}).
|
|---|
| 3525 | @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
|
|---|
| 3526 |
|
|---|
| 3527 | @node Execution, Parallel, Echoing, Commands
|
|---|
| 3528 | @section Command Execution
|
|---|
| 3529 | @cindex commands, execution
|
|---|
| 3530 | @cindex execution, of commands
|
|---|
| 3531 | @cindex shell command, execution
|
|---|
| 3532 | @vindex SHELL @r{(command execution)}
|
|---|
| 3533 |
|
|---|
| 3534 | When it is time to execute commands to update a target, they are executed
|
|---|
| 3535 | by making a new subshell for each line. (In practice, @code{make} may
|
|---|
| 3536 | take shortcuts that do not affect the results.)
|
|---|
| 3537 |
|
|---|
| 3538 | @cindex @code{cd} (shell command)
|
|---|
| 3539 | @strong{Please note:} this implies that shell commands such as @code{cd}
|
|---|
| 3540 | that set variables local to each process will not affect the following
|
|---|
| 3541 | command lines. @footnote{On MS-DOS, the value of current working
|
|---|
| 3542 | directory is @strong{global}, so changing it @emph{will} affect the
|
|---|
| 3543 | following command lines on those systems.} If you want to use @code{cd}
|
|---|
| 3544 | to affect the next command, put the two on a single line with a
|
|---|
| 3545 | semicolon between them. Then @code{make} will consider them a single
|
|---|
| 3546 | command and pass them, together, to a shell which will execute them in
|
|---|
| 3547 | sequence. For example:
|
|---|
| 3548 |
|
|---|
| 3549 | @example
|
|---|
| 3550 | foo : bar/lose
|
|---|
| 3551 | cd bar; gobble lose > ../foo
|
|---|
| 3552 | @end example
|
|---|
| 3553 |
|
|---|
| 3554 | @cindex commands, backslash (@code{\}) in
|
|---|
| 3555 | @cindex commands, quoting newlines in
|
|---|
| 3556 | @cindex backslash (@code{\}), in commands
|
|---|
| 3557 | @cindex @code{\} (backslash), in commands
|
|---|
| 3558 | @cindex quoting newline, in commands
|
|---|
| 3559 | @cindex newline, quoting, in commands
|
|---|
| 3560 | If you would like to split a single shell command into multiple lines of
|
|---|
| 3561 | text, you must use a backslash at the end of all but the last subline.
|
|---|
| 3562 | Such a sequence of lines is combined into a single line, by deleting the
|
|---|
| 3563 | backslash-newline sequences, before passing it to the shell. Thus, the
|
|---|
| 3564 | following is equivalent to the preceding example:
|
|---|
| 3565 |
|
|---|
| 3566 | @example
|
|---|
| 3567 | @group
|
|---|
| 3568 | foo : bar/lose
|
|---|
| 3569 | cd bar; \
|
|---|
| 3570 | gobble lose > ../foo
|
|---|
| 3571 | @end group
|
|---|
| 3572 | @end example
|
|---|
| 3573 |
|
|---|
| 3574 | @vindex SHELL
|
|---|
| 3575 | The program used as the shell is taken from the variable @code{SHELL}.
|
|---|
| 3576 | By default, the program @file{/bin/sh} is used.
|
|---|
| 3577 |
|
|---|
| 3578 | @vindex COMSPEC
|
|---|
| 3579 | On MS-DOS, if @code{SHELL} is not set, the value of the variable
|
|---|
| 3580 | @code{COMSPEC} (which is always set) is used instead.
|
|---|
| 3581 |
|
|---|
| 3582 | @cindex @code{SHELL}, MS-DOS specifics
|
|---|
| 3583 | The processing of lines that set the variable @code{SHELL} in Makefiles
|
|---|
| 3584 | is different on MS-DOS. The stock shell, @file{command.com}, is
|
|---|
| 3585 | ridiculously limited in its functionality and many users of @code{make}
|
|---|
| 3586 | tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
|
|---|
| 3587 | examines the value of @code{SHELL}, and changes its behavior based on
|
|---|
| 3588 | whether it points to a Unix-style or DOS-style shell. This allows
|
|---|
| 3589 | reasonable functionality even if @code{SHELL} points to
|
|---|
| 3590 | @file{command.com}.
|
|---|
| 3591 |
|
|---|
| 3592 | If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
|
|---|
| 3593 | additionally checks whether that shell can indeed be found; if not, it
|
|---|
| 3594 | ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
|
|---|
| 3595 | searches for the shell in the following places:
|
|---|
| 3596 |
|
|---|
| 3597 | @enumerate
|
|---|
| 3598 | @item
|
|---|
| 3599 | In the precise place pointed to by the value of @code{SHELL}. For
|
|---|
| 3600 | example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
|
|---|
| 3601 | will look in the directory @file{/bin} on the current drive.
|
|---|
| 3602 |
|
|---|
| 3603 | @item
|
|---|
| 3604 | In the current directory.
|
|---|
| 3605 |
|
|---|
| 3606 | @item
|
|---|
| 3607 | In each of the directories in the @code{PATH} variable, in order.
|
|---|
| 3608 |
|
|---|
| 3609 | @end enumerate
|
|---|
| 3610 |
|
|---|
| 3611 | In every directory it examines, @code{make} will first look for the
|
|---|
| 3612 | specific file (@file{sh} in the example above). If this is not found,
|
|---|
| 3613 | it will also look in that directory for that file with one of the known
|
|---|
| 3614 | extensions which identify executable files. For example @file{.exe},
|
|---|
| 3615 | @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
|
|---|
| 3616 |
|
|---|
| 3617 | If any of these attempts is successful, the value of @code{SHELL} will
|
|---|
| 3618 | be set to the full pathname of the shell as found. However, if none of
|
|---|
| 3619 | these is found, the value of @code{SHELL} will not be changed, and thus
|
|---|
| 3620 | the line that sets it will be effectively ignored. This is so
|
|---|
| 3621 | @code{make} will only support features specific to a Unix-style shell if
|
|---|
| 3622 | such a shell is actually installed on the system where @code{make} runs.
|
|---|
| 3623 |
|
|---|
| 3624 | Note that this extended search for the shell is limited to the cases
|
|---|
| 3625 | where @code{SHELL} is set from the Makefile; if it is set in the
|
|---|
| 3626 | environment or command line, you are expected to set it to the full
|
|---|
| 3627 | pathname of the shell, exactly as things are on Unix.
|
|---|
| 3628 |
|
|---|
| 3629 | The effect of the above DOS-specific processing is that a Makefile that
|
|---|
| 3630 | says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
|
|---|
| 3631 | on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
|
|---|
| 3632 | directory along your @code{PATH}.
|
|---|
| 3633 |
|
|---|
| 3634 | @cindex environment, @code{SHELL} in
|
|---|
| 3635 | @vindex MAKESHELL @r{(MS-DOS alternative to @code{SHELL})}
|
|---|
| 3636 | Unlike most variables, the variable @code{SHELL} is never set from the
|
|---|
| 3637 | environment. This is because the @code{SHELL} environment variable is
|
|---|
| 3638 | used to specify your personal choice of shell program for interactive
|
|---|
| 3639 | use. It would be very bad for personal choices like this to affect the
|
|---|
| 3640 | functioning of makefiles. @xref{Environment, ,Variables from the
|
|---|
| 3641 | Environment}. However, on MS-DOS and MS-Windows the value of
|
|---|
| 3642 | @code{SHELL} in the environment @strong{is} used, since on those systems
|
|---|
| 3643 | most users do not set this variable, and therefore it is most likely set
|
|---|
| 3644 | specifically to be used by @code{make}. On MS-DOS, if the setting of
|
|---|
| 3645 | @code{SHELL} is not suitable for @code{make}, you can set the variable
|
|---|
| 3646 | @code{MAKESHELL} to the shell that @code{make} should use; this will
|
|---|
| 3647 | override the value of @code{SHELL}.
|
|---|
| 3648 |
|
|---|
| 3649 | @node Parallel, Errors, Execution, Commands
|
|---|
| 3650 | @section Parallel Execution
|
|---|
| 3651 | @cindex commands, execution in parallel
|
|---|
| 3652 | @cindex parallel execution
|
|---|
| 3653 | @cindex execution, in parallel
|
|---|
| 3654 | @cindex job slots
|
|---|
| 3655 | @cindex @code{-j}
|
|---|
| 3656 | @cindex @code{--jobs}
|
|---|
| 3657 |
|
|---|
| 3658 | GNU @code{make} knows how to execute several commands at once.
|
|---|
| 3659 | Normally, @code{make} will execute only one command at a time, waiting
|
|---|
| 3660 | for it to finish before executing the next. However, the @samp{-j} or
|
|---|
| 3661 | @samp{--jobs} option tells @code{make} to execute many commands
|
|---|
| 3662 | simultaneously.@refill
|
|---|
| 3663 |
|
|---|
| 3664 | On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
|
|---|
| 3665 | support multi-processing.
|
|---|
| 3666 |
|
|---|
| 3667 | If the @samp{-j} option is followed by an integer, this is the number of
|
|---|
| 3668 | commands to execute at once; this is called the number of @dfn{job slots}.
|
|---|
| 3669 | If there is nothing looking like an integer after the @samp{-j} option,
|
|---|
| 3670 | there is no limit on the number of job slots. The default number of job
|
|---|
| 3671 | slots is one, which means serial execution (one thing at a time).
|
|---|
| 3672 |
|
|---|
| 3673 | One unpleasant consequence of running several commands simultaneously is
|
|---|
| 3674 | that output generated by the commands appears whenever each command
|
|---|
| 3675 | sends it, so messages from different commands may be interspersed.
|
|---|
| 3676 |
|
|---|
| 3677 | Another problem is that two processes cannot both take input from the
|
|---|
| 3678 | same device; so to make sure that only one command tries to take input
|
|---|
| 3679 | from the terminal at once, @code{make} will invalidate the standard
|
|---|
| 3680 | input streams of all but one running command. This means that
|
|---|
| 3681 | attempting to read from standard input will usually be a fatal error (a
|
|---|
| 3682 | @samp{Broken pipe} signal) for most child processes if there are
|
|---|
| 3683 | several.
|
|---|
| 3684 | @cindex broken pipe
|
|---|
| 3685 | @cindex standard input
|
|---|
| 3686 |
|
|---|
| 3687 | It is unpredictable which command will have a valid standard input stream
|
|---|
| 3688 | (which will come from the terminal, or wherever you redirect the standard
|
|---|
| 3689 | input of @code{make}). The first command run will always get it first, and
|
|---|
| 3690 | the first command started after that one finishes will get it next, and so
|
|---|
| 3691 | on.
|
|---|
| 3692 |
|
|---|
| 3693 | We will change how this aspect of @code{make} works if we find a better
|
|---|
| 3694 | alternative. In the mean time, you should not rely on any command using
|
|---|
| 3695 | standard input at all if you are using the parallel execution feature; but
|
|---|
| 3696 | if you are not using this feature, then standard input works normally in
|
|---|
| 3697 | all commands.
|
|---|
| 3698 |
|
|---|
| 3699 | Finally, handling recursive @code{make} invocations raises issues. For
|
|---|
| 3700 | more information on this, see
|
|---|
| 3701 | @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 3702 |
|
|---|
| 3703 | If a command fails (is killed by a signal or exits with a nonzero
|
|---|
| 3704 | status), and errors are not ignored for that command
|
|---|
| 3705 | (@pxref{Errors, ,Errors in Commands}),
|
|---|
| 3706 | the remaining command lines to remake the same target will not be run.
|
|---|
| 3707 | If a command fails and the @samp{-k} or @samp{--keep-going}
|
|---|
| 3708 | option was not given
|
|---|
| 3709 | (@pxref{Options Summary, ,Summary of Options}),
|
|---|
| 3710 | @code{make} aborts execution. If make
|
|---|
| 3711 | terminates for any reason (including a signal) with child processes
|
|---|
| 3712 | running, it waits for them to finish before actually exiting.@refill
|
|---|
| 3713 |
|
|---|
| 3714 | @cindex load average
|
|---|
| 3715 | @cindex limiting jobs based on load
|
|---|
| 3716 | @cindex jobs, limiting based on load
|
|---|
| 3717 | @cindex @code{-l} (load average)
|
|---|
| 3718 | @cindex @code{--max-load}
|
|---|
| 3719 | @cindex @code{--load-average}
|
|---|
| 3720 | When the system is heavily loaded, you will probably want to run fewer jobs
|
|---|
| 3721 | than when it is lightly loaded. You can use the @samp{-l} option to tell
|
|---|
| 3722 | @code{make} to limit the number of jobs to run at once, based on the load
|
|---|
| 3723 | average. The @samp{-l} or @samp{--max-load}
|
|---|
| 3724 | option is followed by a floating-point number. For
|
|---|
| 3725 | example,
|
|---|
| 3726 |
|
|---|
| 3727 | @example
|
|---|
| 3728 | -l 2.5
|
|---|
| 3729 | @end example
|
|---|
| 3730 |
|
|---|
| 3731 | @noindent
|
|---|
| 3732 | will not let @code{make} start more than one job if the load average is
|
|---|
| 3733 | above 2.5. The @samp{-l} option with no following number removes the
|
|---|
| 3734 | load limit, if one was given with a previous @samp{-l} option.@refill
|
|---|
| 3735 |
|
|---|
| 3736 | More precisely, when @code{make} goes to start up a job, and it already has
|
|---|
| 3737 | at least one job running, it checks the current load average; if it is not
|
|---|
| 3738 | lower than the limit given with @samp{-l}, @code{make} waits until the load
|
|---|
| 3739 | average goes below that limit, or until all the other jobs finish.
|
|---|
| 3740 |
|
|---|
| 3741 | By default, there is no load limit.
|
|---|
| 3742 |
|
|---|
| 3743 | @node Errors, Interrupts, Parallel, Commands
|
|---|
| 3744 | @section Errors in Commands
|
|---|
| 3745 | @cindex errors (in commands)
|
|---|
| 3746 | @cindex commands, errors in
|
|---|
| 3747 | @cindex exit status (errors)
|
|---|
| 3748 |
|
|---|
| 3749 | After each shell command returns, @code{make} looks at its exit status.
|
|---|
| 3750 | If the command completed successfully, the next command line is executed
|
|---|
| 3751 | in a new shell; after the last command line is finished, the rule is
|
|---|
| 3752 | finished.
|
|---|
| 3753 |
|
|---|
| 3754 | If there is an error (the exit status is nonzero), @code{make} gives up on
|
|---|
| 3755 | the current rule, and perhaps on all rules.
|
|---|
| 3756 |
|
|---|
| 3757 | Sometimes the failure of a certain command does not indicate a problem.
|
|---|
| 3758 | For example, you may use the @code{mkdir} command to ensure that a
|
|---|
| 3759 | directory exists. If the directory already exists, @code{mkdir} will
|
|---|
| 3760 | report an error, but you probably want @code{make} to continue regardless.
|
|---|
| 3761 |
|
|---|
| 3762 | @cindex @code{-} (in commands)
|
|---|
| 3763 | To ignore errors in a command line, write a @samp{-} at the beginning of
|
|---|
| 3764 | the line's text (after the initial tab). The @samp{-} is discarded before
|
|---|
| 3765 | the command is passed to the shell for execution.
|
|---|
| 3766 |
|
|---|
| 3767 | For example,
|
|---|
| 3768 |
|
|---|
| 3769 | @example
|
|---|
| 3770 | @group
|
|---|
| 3771 | clean:
|
|---|
| 3772 | -rm -f *.o
|
|---|
| 3773 | @end group
|
|---|
| 3774 | @end example
|
|---|
| 3775 | @cindex @code{rm} (shell command)
|
|---|
| 3776 |
|
|---|
| 3777 | @noindent
|
|---|
| 3778 | This causes @code{rm} to continue even if it is unable to remove a file.
|
|---|
| 3779 |
|
|---|
| 3780 | @cindex @code{-i}
|
|---|
| 3781 | @cindex @code{--ignore-errors}
|
|---|
| 3782 | @findex .IGNORE
|
|---|
| 3783 | When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
|
|---|
| 3784 | flag, errors are ignored in all commands of all rules. A rule in the
|
|---|
| 3785 | makefile for the special target @code{.IGNORE} has the same effect, if
|
|---|
| 3786 | there are no prerequisites. These ways of ignoring errors are obsolete
|
|---|
| 3787 | because @samp{-} is more flexible.
|
|---|
| 3788 |
|
|---|
| 3789 | When errors are to be ignored, because of either a @samp{-} or the
|
|---|
| 3790 | @samp{-i} flag, @code{make} treats an error return just like success,
|
|---|
| 3791 | except that it prints out a message that tells you the status code
|
|---|
| 3792 | the command exited with, and says that the error has been ignored.
|
|---|
| 3793 |
|
|---|
| 3794 | When an error happens that @code{make} has not been told to ignore,
|
|---|
| 3795 | it implies that the current target cannot be correctly remade, and neither
|
|---|
| 3796 | can any other that depends on it either directly or indirectly. No further
|
|---|
| 3797 | commands will be executed for these targets, since their preconditions
|
|---|
| 3798 | have not been achieved.
|
|---|
| 3799 |
|
|---|
| 3800 |
|
|---|
| 3801 | @cindex @code{-k}
|
|---|
| 3802 | @cindex @code{--keep-going}
|
|---|
| 3803 | Normally @code{make} gives up immediately in this circumstance, returning a
|
|---|
| 3804 | nonzero status. However, if the @samp{-k} or @samp{--keep-going}
|
|---|
| 3805 | flag is specified, @code{make}
|
|---|
| 3806 | continues to consider the other prerequisites of the pending targets,
|
|---|
| 3807 | remaking them if necessary, before it gives up and returns nonzero status.
|
|---|
| 3808 | For example, after an error in compiling one object file, @samp{make -k}
|
|---|
| 3809 | will continue compiling other object files even though it already knows
|
|---|
| 3810 | that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
|
|---|
| 3811 |
|
|---|
| 3812 | The usual behavior assumes that your purpose is to get the specified
|
|---|
| 3813 | targets up to date; once @code{make} learns that this is impossible, it
|
|---|
| 3814 | might as well report the failure immediately. The @samp{-k} option says
|
|---|
| 3815 | that the real purpose is to test as many of the changes made in the
|
|---|
| 3816 | program as possible, perhaps to find several independent problems so
|
|---|
| 3817 | that you can correct them all before the next attempt to compile. This
|
|---|
| 3818 | is why Emacs' @code{compile} command passes the @samp{-k} flag by
|
|---|
| 3819 | default.
|
|---|
| 3820 | @cindex Emacs (@code{M-x compile})
|
|---|
| 3821 |
|
|---|
| 3822 | @findex .DELETE_ON_ERROR
|
|---|
| 3823 | @cindex deletion of target files
|
|---|
| 3824 | @cindex removal of target files
|
|---|
| 3825 | @cindex target, deleting on error
|
|---|
| 3826 | Usually when a command fails, if it has changed the target file at all,
|
|---|
| 3827 | the file is corrupted and cannot be used---or at least it is not
|
|---|
| 3828 | completely updated. Yet the file's time stamp says that it is now up to
|
|---|
| 3829 | date, so the next time @code{make} runs, it will not try to update that
|
|---|
| 3830 | file. The situation is just the same as when the command is killed by a
|
|---|
| 3831 | signal; @pxref{Interrupts}. So generally the right thing to do is to
|
|---|
| 3832 | delete the target file if the command fails after beginning to change
|
|---|
| 3833 | the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
|
|---|
| 3834 | as a target. This is almost always what you want @code{make} to do, but
|
|---|
| 3835 | it is not historical practice; so for compatibility, you must explicitly
|
|---|
| 3836 | request it.
|
|---|
| 3837 |
|
|---|
| 3838 | @node Interrupts, Recursion, Errors, Commands
|
|---|
| 3839 | @section Interrupting or Killing @code{make}
|
|---|
| 3840 | @cindex interrupt
|
|---|
| 3841 | @cindex signal
|
|---|
| 3842 | @cindex deletion of target files
|
|---|
| 3843 | @cindex removal of target files
|
|---|
| 3844 | @cindex target, deleting on interrupt
|
|---|
| 3845 | @cindex killing (interruption)
|
|---|
| 3846 |
|
|---|
| 3847 | If @code{make} gets a fatal signal while a command is executing, it may
|
|---|
| 3848 | delete the target file that the command was supposed to update. This is
|
|---|
| 3849 | done if the target file's last-modification time has changed since
|
|---|
| 3850 | @code{make} first checked it.
|
|---|
| 3851 |
|
|---|
| 3852 | The purpose of deleting the target is to make sure that it is remade from
|
|---|
| 3853 | scratch when @code{make} is next run. Why is this? Suppose you type
|
|---|
| 3854 | @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
|
|---|
| 3855 | object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
|
|---|
| 3856 | in an incomplete file whose last-modification time is newer than the source
|
|---|
| 3857 | file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
|
|---|
| 3858 | and deletes this incomplete file. If @code{make} did not do this, the next
|
|---|
| 3859 | invocation of @code{make} would think that @file{foo.o} did not require
|
|---|
| 3860 | updating---resulting in a strange error message from the linker when it
|
|---|
| 3861 | tries to link an object file half of which is missing.
|
|---|
| 3862 |
|
|---|
| 3863 | @findex .PRECIOUS
|
|---|
| 3864 | You can prevent the deletion of a target file in this way by making the
|
|---|
| 3865 | special target @code{.PRECIOUS} depend on it. Before remaking a target,
|
|---|
| 3866 | @code{make} checks to see whether it appears on the prerequisites of
|
|---|
| 3867 | @code{.PRECIOUS}, and thereby decides whether the target should be deleted
|
|---|
| 3868 | if a signal happens. Some reasons why you might do this are that the
|
|---|
| 3869 | target is updated in some atomic fashion, or exists only to record a
|
|---|
| 3870 | modification-time (its contents do not matter), or must exist at all
|
|---|
| 3871 | times to prevent other sorts of trouble.
|
|---|
| 3872 |
|
|---|
| 3873 | @node Recursion, Sequences, Interrupts, Commands
|
|---|
| 3874 | @section Recursive Use of @code{make}
|
|---|
| 3875 | @cindex recursion
|
|---|
| 3876 | @cindex subdirectories, recursion for
|
|---|
| 3877 |
|
|---|
| 3878 | Recursive use of @code{make} means using @code{make} as a command in a
|
|---|
| 3879 | makefile. This technique is useful when you want separate makefiles for
|
|---|
| 3880 | various subsystems that compose a larger system. For example, suppose you
|
|---|
| 3881 | have a subdirectory @file{subdir} which has its own makefile, and you would
|
|---|
| 3882 | like the containing directory's makefile to run @code{make} on the
|
|---|
| 3883 | subdirectory. You can do it by writing this:
|
|---|
| 3884 |
|
|---|
| 3885 | @example
|
|---|
| 3886 | subsystem:
|
|---|
| 3887 | cd subdir && $(MAKE)
|
|---|
| 3888 | @end example
|
|---|
| 3889 |
|
|---|
| 3890 | @noindent
|
|---|
| 3891 | or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
|
|---|
| 3892 |
|
|---|
| 3893 | @example
|
|---|
| 3894 | subsystem:
|
|---|
| 3895 | $(MAKE) -C subdir
|
|---|
| 3896 | @end example
|
|---|
| 3897 | @cindex @code{-C}
|
|---|
| 3898 | @cindex @code{--directory}
|
|---|
| 3899 |
|
|---|
| 3900 | You can write recursive @code{make} commands just by copying this example,
|
|---|
| 3901 | but there are many things to know about how they work and why, and about
|
|---|
| 3902 | how the sub-@code{make} relates to the top-level @code{make}. You may
|
|---|
| 3903 | also find it useful to declare targets that invoke recursive
|
|---|
| 3904 | @code{make} commands as @samp{.PHONY} (for more discussion on when
|
|---|
| 3905 | this is useful, see @ref{Phony Targets}).
|
|---|
| 3906 |
|
|---|
| 3907 | @vindex @code{CURDIR}
|
|---|
| 3908 | For your convenience, when GNU @code{make} starts (after it has
|
|---|
| 3909 | processed any @code{-C} options) it sets the variable @code{CURDIR} to
|
|---|
| 3910 | the pathname of the current working directory. This value is never
|
|---|
| 3911 | touched by @code{make} again: in particular note that if you include
|
|---|
| 3912 | files from other directories the value of @code{CURDIR} does not
|
|---|
| 3913 | change. The value has the same precedence it would have if it were
|
|---|
| 3914 | set in the makefile (by default, an environment variable @code{CURDIR}
|
|---|
| 3915 | will not override this value). Note that setting this variable has no
|
|---|
| 3916 | impact on the operation of @code{make} (it does not cause @code{make}
|
|---|
| 3917 | to change its working directory, for example).
|
|---|
| 3918 |
|
|---|
| 3919 | @menu
|
|---|
| 3920 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
|---|
| 3921 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
|---|
| 3922 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
|---|
| 3923 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
|---|
| 3924 | helps debug use of recursive @code{make} commands.
|
|---|
| 3925 | @end menu
|
|---|
| 3926 |
|
|---|
| 3927 | @node MAKE Variable, Variables/Recursion, Recursion, Recursion
|
|---|
| 3928 | @subsection How the @code{MAKE} Variable Works
|
|---|
| 3929 | @vindex MAKE
|
|---|
| 3930 | @cindex recursion, and @code{MAKE} variable
|
|---|
| 3931 |
|
|---|
| 3932 | Recursive @code{make} commands should always use the variable @code{MAKE},
|
|---|
| 3933 | not the explicit command name @samp{make}, as shown here:
|
|---|
| 3934 |
|
|---|
| 3935 | @example
|
|---|
| 3936 | @group
|
|---|
| 3937 | subsystem:
|
|---|
| 3938 | cd subdir && $(MAKE)
|
|---|
| 3939 | @end group
|
|---|
| 3940 | @end example
|
|---|
| 3941 |
|
|---|
| 3942 | The value of this variable is the file name with which @code{make} was
|
|---|
| 3943 | invoked. If this file name was @file{/bin/make}, then the command executed
|
|---|
| 3944 | is @samp{cd subdir && /bin/make}. If you use a special version of
|
|---|
| 3945 | @code{make} to run the top-level makefile, the same special version will be
|
|---|
| 3946 | executed for recursive invocations.
|
|---|
| 3947 | @cindex @code{cd} (shell command)
|
|---|
| 3948 |
|
|---|
| 3949 | @cindex +, and commands
|
|---|
| 3950 | As a special feature, using the variable @code{MAKE} in the commands of
|
|---|
| 3951 | a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
|
|---|
| 3952 | (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
|
|---|
| 3953 | Using the @code{MAKE} variable has the same effect as using a @samp{+}
|
|---|
| 3954 | character at the beginning of the command line. @xref{Instead of
|
|---|
| 3955 | Execution, ,Instead of Executing the Commands}. This special feature
|
|---|
| 3956 | is only enabled if the @code{MAKE} variable appears directly in the
|
|---|
| 3957 | command script: it does not apply if the @code{MAKE} variable is
|
|---|
| 3958 | referenced through expansion of another variable. In the latter case
|
|---|
| 3959 | you must use the @samp{+} token to get these special effects.@refill
|
|---|
| 3960 |
|
|---|
| 3961 | Consider the command @samp{make -t} in the above example. (The
|
|---|
| 3962 | @samp{-t} option marks targets as up to date without actually running
|
|---|
| 3963 | any commands; see @ref{Instead of Execution}.) Following the usual
|
|---|
| 3964 | definition of @samp{-t}, a @samp{make -t} command in the example would
|
|---|
| 3965 | create a file named @file{subsystem} and do nothing else. What you
|
|---|
| 3966 | really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
|
|---|
| 3967 | require executing the command, and @samp{-t} says not to execute
|
|---|
| 3968 | commands.@refill
|
|---|
| 3969 | @cindex @code{-t}, and recursion
|
|---|
| 3970 | @cindex recursion, and @code{-t}
|
|---|
| 3971 | @cindex @code{--touch}, and recursion
|
|---|
| 3972 |
|
|---|
| 3973 | The special feature makes this do what you want: whenever a command
|
|---|
| 3974 | line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
|
|---|
| 3975 | @samp{-n} and @samp{-q} do not apply to that line. Command lines
|
|---|
| 3976 | containing @code{MAKE} are executed normally despite the presence of a
|
|---|
| 3977 | flag that causes most commands not to be run. The usual
|
|---|
| 3978 | @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
|
|---|
| 3979 | (@pxref{Options/Recursion, ,Communicating Options to a
|
|---|
| 3980 | Sub-@code{make}}), so your request to touch the files, or print the
|
|---|
| 3981 | commands, is propagated to the subsystem.@refill
|
|---|
| 3982 |
|
|---|
| 3983 | @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
|
|---|
| 3984 | @subsection Communicating Variables to a Sub-@code{make}
|
|---|
| 3985 | @cindex sub-@code{make}
|
|---|
| 3986 | @cindex environment, and recursion
|
|---|
| 3987 | @cindex exporting variables
|
|---|
| 3988 | @cindex variables, environment
|
|---|
| 3989 | @cindex variables, exporting
|
|---|
| 3990 | @cindex recursion, and environment
|
|---|
| 3991 | @cindex recursion, and variables
|
|---|
| 3992 |
|
|---|
| 3993 | Variable values of the top-level @code{make} can be passed to the
|
|---|
| 3994 | sub-@code{make} through the environment by explicit request. These
|
|---|
| 3995 | variables are defined in the sub-@code{make} as defaults, but do not
|
|---|
| 3996 | override what is specified in the makefile used by the sub-@code{make}
|
|---|
| 3997 | makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
|
|---|
| 3998 | ,Summary of Options}).@refill
|
|---|
| 3999 |
|
|---|
| 4000 | To pass down, or @dfn{export}, a variable, @code{make} adds the variable
|
|---|
| 4001 | and its value to the environment for running each command. The
|
|---|
| 4002 | sub-@code{make}, in turn, uses the environment to initialize its table
|
|---|
| 4003 | of variable values. @xref{Environment, ,Variables from the
|
|---|
| 4004 | Environment}.
|
|---|
| 4005 |
|
|---|
| 4006 | Except by explicit request, @code{make} exports a variable only if it
|
|---|
| 4007 | is either defined in the environment initially or set on the command
|
|---|
| 4008 | line, and if its name consists only of letters, numbers, and underscores.
|
|---|
| 4009 | Some shells cannot cope with environment variable names consisting of
|
|---|
| 4010 | characters other than letters, numbers, and underscores.
|
|---|
| 4011 |
|
|---|
| 4012 | @cindex SHELL, exported value
|
|---|
| 4013 | The value of the @code{make} variable @code{SHELL} is not exported.
|
|---|
| 4014 | Instead, the value of the @code{SHELL} variable from the invoking
|
|---|
| 4015 | environment is passed to the sub-@code{make}. You can force
|
|---|
| 4016 | @code{make} to export its value for @code{SHELL} by using the
|
|---|
| 4017 | @code{export} directive, described below.
|
|---|
| 4018 |
|
|---|
| 4019 | The special variable @code{MAKEFLAGS} is always exported (unless you
|
|---|
| 4020 | unexport it). @code{MAKEFILES} is exported if you set it to anything.
|
|---|
| 4021 |
|
|---|
| 4022 | @code{make} automatically passes down variable values that were defined
|
|---|
| 4023 | on the command line, by putting them in the @code{MAKEFLAGS} variable.
|
|---|
| 4024 | @iftex
|
|---|
| 4025 | See the next section.
|
|---|
| 4026 | @end iftex
|
|---|
| 4027 | @ifnottex
|
|---|
| 4028 | @xref{Options/Recursion}.
|
|---|
| 4029 | @end ifnottex
|
|---|
| 4030 |
|
|---|
| 4031 | Variables are @emph{not} normally passed down if they were created by
|
|---|
| 4032 | default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
|
|---|
| 4033 | Implicit Rules}). The sub-@code{make} will define these for
|
|---|
| 4034 | itself.@refill
|
|---|
| 4035 |
|
|---|
| 4036 | @findex export
|
|---|
| 4037 | If you want to export specific variables to a sub-@code{make}, use the
|
|---|
| 4038 | @code{export} directive, like this:
|
|---|
| 4039 |
|
|---|
| 4040 | @example
|
|---|
| 4041 | export @var{variable} @dots{}
|
|---|
| 4042 | @end example
|
|---|
| 4043 |
|
|---|
| 4044 | @noindent
|
|---|
| 4045 | @findex unexport
|
|---|
| 4046 | If you want to @emph{prevent} a variable from being exported, use the
|
|---|
| 4047 | @code{unexport} directive, like this:
|
|---|
| 4048 |
|
|---|
| 4049 | @example
|
|---|
| 4050 | unexport @var{variable} @dots{}
|
|---|
| 4051 | @end example
|
|---|
| 4052 |
|
|---|
| 4053 | @noindent
|
|---|
| 4054 | In both of these forms, the arguments to @code{export} and
|
|---|
| 4055 | @code{unexport} are expanded, and so could be variables or functions
|
|---|
| 4056 | which expand to a (list of) variable names to be (un)exported.
|
|---|
| 4057 |
|
|---|
| 4058 | As a convenience, you can define a variable and export it at the same
|
|---|
| 4059 | time by doing:
|
|---|
| 4060 |
|
|---|
| 4061 | @example
|
|---|
| 4062 | export @var{variable} = value
|
|---|
| 4063 | @end example
|
|---|
| 4064 |
|
|---|
| 4065 | @noindent
|
|---|
| 4066 | has the same result as:
|
|---|
| 4067 |
|
|---|
| 4068 | @example
|
|---|
| 4069 | @var{variable} = value
|
|---|
| 4070 | export @var{variable}
|
|---|
| 4071 | @end example
|
|---|
| 4072 |
|
|---|
| 4073 | @noindent
|
|---|
| 4074 | and
|
|---|
| 4075 |
|
|---|
| 4076 | @example
|
|---|
| 4077 | export @var{variable} := value
|
|---|
| 4078 | @end example
|
|---|
| 4079 |
|
|---|
| 4080 | @noindent
|
|---|
| 4081 | has the same result as:
|
|---|
| 4082 |
|
|---|
| 4083 | @example
|
|---|
| 4084 | @var{variable} := value
|
|---|
| 4085 | export @var{variable}
|
|---|
| 4086 | @end example
|
|---|
| 4087 |
|
|---|
| 4088 | Likewise,
|
|---|
| 4089 |
|
|---|
| 4090 | @example
|
|---|
| 4091 | export @var{variable} += value
|
|---|
| 4092 | @end example
|
|---|
| 4093 |
|
|---|
| 4094 | @noindent
|
|---|
| 4095 | is just like:
|
|---|
| 4096 |
|
|---|
| 4097 | @example
|
|---|
| 4098 | @var{variable} += value
|
|---|
| 4099 | export @var{variable}
|
|---|
| 4100 | @end example
|
|---|
| 4101 |
|
|---|
| 4102 | @noindent
|
|---|
| 4103 | @xref{Appending, ,Appending More Text to Variables}.
|
|---|
| 4104 |
|
|---|
| 4105 | You may notice that the @code{export} and @code{unexport} directives
|
|---|
| 4106 | work in @code{make} in the same way they work in the shell, @code{sh}.
|
|---|
| 4107 |
|
|---|
| 4108 | If you want all variables to be exported by default, you can use
|
|---|
| 4109 | @code{export} by itself:
|
|---|
| 4110 |
|
|---|
| 4111 | @example
|
|---|
| 4112 | export
|
|---|
| 4113 | @end example
|
|---|
| 4114 |
|
|---|
| 4115 | @noindent
|
|---|
| 4116 | This tells @code{make} that variables which are not explicitly mentioned
|
|---|
| 4117 | in an @code{export} or @code{unexport} directive should be exported.
|
|---|
| 4118 | Any variable given in an @code{unexport} directive will still @emph{not}
|
|---|
| 4119 | be exported. If you use @code{export} by itself to export variables by
|
|---|
| 4120 | default, variables whose names contain characters other than
|
|---|
| 4121 | alphanumerics and underscores will not be exported unless specifically
|
|---|
| 4122 | mentioned in an @code{export} directive.@refill
|
|---|
| 4123 |
|
|---|
| 4124 | @findex .EXPORT_ALL_VARIABLES
|
|---|
| 4125 | The behavior elicited by an @code{export} directive by itself was the
|
|---|
| 4126 | default in older versions of GNU @code{make}. If your makefiles depend
|
|---|
| 4127 | on this behavior and you want to be compatible with old versions of
|
|---|
| 4128 | @code{make}, you can write a rule for the special target
|
|---|
| 4129 | @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
|
|---|
| 4130 | This will be ignored by old @code{make}s, while the @code{export}
|
|---|
| 4131 | directive will cause a syntax error.@refill
|
|---|
| 4132 | @cindex compatibility in exporting
|
|---|
| 4133 |
|
|---|
| 4134 | Likewise, you can use @code{unexport} by itself to tell @code{make}
|
|---|
| 4135 | @emph{not} to export variables by default. Since this is the default
|
|---|
| 4136 | behavior, you would only need to do this if @code{export} had been used
|
|---|
| 4137 | by itself earlier (in an included makefile, perhaps). You
|
|---|
| 4138 | @strong{cannot} use @code{export} and @code{unexport} by themselves to
|
|---|
| 4139 | have variables exported for some commands and not for others. The last
|
|---|
| 4140 | @code{export} or @code{unexport} directive that appears by itself
|
|---|
| 4141 | determines the behavior for the entire run of @code{make}.@refill
|
|---|
| 4142 |
|
|---|
| 4143 | @vindex MAKELEVEL
|
|---|
| 4144 | @cindex recursion, level of
|
|---|
| 4145 | As a special feature, the variable @code{MAKELEVEL} is changed when it
|
|---|
| 4146 | is passed down from level to level. This variable's value is a string
|
|---|
| 4147 | which is the depth of the level as a decimal number. The value is
|
|---|
| 4148 | @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
|
|---|
| 4149 | @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
|
|---|
| 4150 | happens when @code{make} sets up the environment for a command.@refill
|
|---|
| 4151 |
|
|---|
| 4152 | The main use of @code{MAKELEVEL} is to test it in a conditional
|
|---|
| 4153 | directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
|
|---|
| 4154 | way you can write a makefile that behaves one way if run recursively and
|
|---|
| 4155 | another way if run directly by you.@refill
|
|---|
| 4156 |
|
|---|
| 4157 | @vindex MAKEFILES
|
|---|
| 4158 | You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
|
|---|
| 4159 | commands to use additional makefiles. The value of @code{MAKEFILES} is
|
|---|
| 4160 | a whitespace-separated list of file names. This variable, if defined in
|
|---|
| 4161 | the outer-level makefile, is passed down through the environment; then
|
|---|
| 4162 | it serves as a list of extra makefiles for the sub-@code{make} to read
|
|---|
| 4163 | before the usual or specified ones. @xref{MAKEFILES Variable, ,The
|
|---|
| 4164 | Variable @code{MAKEFILES}}.@refill
|
|---|
| 4165 |
|
|---|
| 4166 | @node Options/Recursion, -w Option, Variables/Recursion, Recursion
|
|---|
| 4167 | @subsection Communicating Options to a Sub-@code{make}
|
|---|
| 4168 | @cindex options, and recursion
|
|---|
| 4169 | @cindex recursion, and options
|
|---|
| 4170 |
|
|---|
| 4171 | @vindex MAKEFLAGS
|
|---|
| 4172 | Flags such as @samp{-s} and @samp{-k} are passed automatically to the
|
|---|
| 4173 | sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
|
|---|
| 4174 | set up automatically by @code{make} to contain the flag letters that
|
|---|
| 4175 | @code{make} received. Thus, if you do @w{@samp{make -ks}} then
|
|---|
| 4176 | @code{MAKEFLAGS} gets the value @samp{ks}.@refill
|
|---|
| 4177 |
|
|---|
| 4178 | As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
|
|---|
| 4179 | in its environment. In response, it takes the flags from that value and
|
|---|
| 4180 | processes them as if they had been given as arguments.
|
|---|
| 4181 | @xref{Options Summary, ,Summary of Options}.
|
|---|
| 4182 |
|
|---|
| 4183 | @cindex command line variable definitions, and recursion
|
|---|
| 4184 | @cindex variables, command line, and recursion
|
|---|
| 4185 | @cindex recursion, and command line variable definitions
|
|---|
| 4186 | Likewise variables defined on the command line are passed to the
|
|---|
| 4187 | sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
|
|---|
| 4188 | @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
|
|---|
| 4189 | definitions just as if they appeared on the command line.
|
|---|
| 4190 | @xref{Overriding, ,Overriding Variables}.
|
|---|
| 4191 |
|
|---|
| 4192 | @cindex @code{-C}, and recursion
|
|---|
| 4193 | @cindex @code{-f}, and recursion
|
|---|
| 4194 | @cindex @code{-o}, and recursion
|
|---|
| 4195 | @cindex @code{-W}, and recursion
|
|---|
| 4196 | @cindex @code{--directory}, and recursion
|
|---|
| 4197 | @cindex @code{--file}, and recursion
|
|---|
| 4198 | @cindex @code{--old-file}, and recursion
|
|---|
| 4199 | @cindex @code{--assume-old}, and recursion
|
|---|
| 4200 | @cindex @code{--assume-new}, and recursion
|
|---|
| 4201 | @cindex @code{--new-file}, and recursion
|
|---|
| 4202 | @cindex recursion, and @code{-C}
|
|---|
| 4203 | @cindex recursion, and @code{-f}
|
|---|
| 4204 | @cindex recursion, and @code{-o}
|
|---|
| 4205 | @cindex recursion, and @code{-W}
|
|---|
| 4206 | The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
|
|---|
| 4207 | into @code{MAKEFLAGS}; these options are not passed down.@refill
|
|---|
| 4208 |
|
|---|
| 4209 | @cindex @code{-j}, and recursion
|
|---|
| 4210 | @cindex @code{--jobs}, and recursion
|
|---|
| 4211 | @cindex recursion, and @code{-j}
|
|---|
| 4212 | @cindex job slots, and recursion
|
|---|
| 4213 | The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
|
|---|
| 4214 | If you set it to some numeric value @samp{N} and your operating system
|
|---|
| 4215 | supports it (most any UNIX system will; others typically won't), the
|
|---|
| 4216 | parent @code{make} and all the sub-@code{make}s will communicate to
|
|---|
| 4217 | ensure that there are only @samp{N} jobs running at the same time
|
|---|
| 4218 | between them all. Note that any job that is marked recursive
|
|---|
| 4219 | (@pxref{Instead of Execution, ,Instead of Executing the Commands})
|
|---|
| 4220 | doesn't count against the total jobs (otherwise we could get @samp{N}
|
|---|
| 4221 | sub-@code{make}s running and have no slots left over for any real work!)
|
|---|
| 4222 |
|
|---|
| 4223 | If your operating system doesn't support the above communication, then
|
|---|
| 4224 | @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
|
|---|
| 4225 | specified. This is because if the @w{@samp{-j}} option were passed down
|
|---|
| 4226 | to sub-@code{make}s, you would get many more jobs running in parallel
|
|---|
| 4227 | than you asked for. If you give @samp{-j} with no numeric argument,
|
|---|
| 4228 | meaning to run as many jobs as possible in parallel, this is passed
|
|---|
| 4229 | down, since multiple infinities are no more than one.@refill
|
|---|
| 4230 |
|
|---|
| 4231 | If you do not want to pass the other flags down, you must change the
|
|---|
| 4232 | value of @code{MAKEFLAGS}, like this:
|
|---|
| 4233 |
|
|---|
| 4234 | @example
|
|---|
| 4235 | subsystem:
|
|---|
| 4236 | cd subdir && $(MAKE) MAKEFLAGS=
|
|---|
| 4237 | @end example
|
|---|
| 4238 |
|
|---|
| 4239 | @vindex MAKEOVERRIDES
|
|---|
| 4240 | The command line variable definitions really appear in the variable
|
|---|
| 4241 | @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
|
|---|
| 4242 | variable. If you do want to pass flags down normally, but don't want to
|
|---|
| 4243 | pass down the command line variable definitions, you can reset
|
|---|
| 4244 | @code{MAKEOVERRIDES} to empty, like this:
|
|---|
| 4245 |
|
|---|
| 4246 | @example
|
|---|
| 4247 | MAKEOVERRIDES =
|
|---|
| 4248 | @end example
|
|---|
| 4249 |
|
|---|
| 4250 | @noindent
|
|---|
| 4251 | @cindex Arg list too long
|
|---|
| 4252 | @cindex E2BIG
|
|---|
| 4253 | This is not usually useful to do. However, some systems have a small
|
|---|
| 4254 | fixed limit on the size of the environment, and putting so much
|
|---|
| 4255 | information into the value of @code{MAKEFLAGS} can exceed it. If you
|
|---|
| 4256 | see the error message @samp{Arg list too long}, this may be the problem.
|
|---|
| 4257 | @findex .POSIX
|
|---|
| 4258 | @cindex POSIX.2
|
|---|
| 4259 | (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
|
|---|
| 4260 | not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
|
|---|
| 4261 | in the makefile. You probably do not care about this.)
|
|---|
| 4262 |
|
|---|
| 4263 | @vindex MFLAGS
|
|---|
| 4264 | A similar variable @code{MFLAGS} exists also, for historical
|
|---|
| 4265 | compatibility. It has the same value as @code{MAKEFLAGS} except that it
|
|---|
| 4266 | does not contain the command line variable definitions, and it always
|
|---|
| 4267 | begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
|
|---|
| 4268 | hyphen only when it begins with an option that has no single-letter
|
|---|
| 4269 | version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
|
|---|
| 4270 | traditionally used explicitly in the recursive @code{make} command, like
|
|---|
| 4271 | this:
|
|---|
| 4272 |
|
|---|
| 4273 | @example
|
|---|
| 4274 | subsystem:
|
|---|
| 4275 | cd subdir && $(MAKE) $(MFLAGS)
|
|---|
| 4276 | @end example
|
|---|
| 4277 |
|
|---|
| 4278 | @noindent
|
|---|
| 4279 | but now @code{MAKEFLAGS} makes this usage redundant. If you want your
|
|---|
| 4280 | makefiles to be compatible with old @code{make} programs, use this
|
|---|
| 4281 | technique; it will work fine with more modern @code{make} versions too.
|
|---|
| 4282 |
|
|---|
| 4283 | @cindex setting options from environment
|
|---|
| 4284 | @cindex options, setting from environment
|
|---|
| 4285 | @cindex setting options in makefiles
|
|---|
| 4286 | @cindex options, setting in makefiles
|
|---|
| 4287 | The @code{MAKEFLAGS} variable can also be useful if you want to have
|
|---|
| 4288 | certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
|
|---|
| 4289 | Options}), set each time you run @code{make}. You simply put a value for
|
|---|
| 4290 | @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
|
|---|
| 4291 | a makefile, to specify additional flags that should also be in effect for
|
|---|
| 4292 | that makefile. (Note that you cannot use @code{MFLAGS} this way. That
|
|---|
| 4293 | variable is set only for compatibility; @code{make} does not interpret a
|
|---|
| 4294 | value you set for it in any way.)
|
|---|
| 4295 |
|
|---|
| 4296 | When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
|
|---|
| 4297 | environment or from a makefile), it first prepends a hyphen if the value
|
|---|
| 4298 | does not already begin with one. Then it chops the value into words
|
|---|
| 4299 | separated by blanks, and parses these words as if they were options given
|
|---|
| 4300 | on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
|
|---|
| 4301 | @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
|
|---|
| 4302 | is no error for an invalid option).
|
|---|
| 4303 |
|
|---|
| 4304 | If you do put @code{MAKEFLAGS} in your environment, you should be sure not
|
|---|
| 4305 | to include any options that will drastically affect the actions of
|
|---|
| 4306 | @code{make} and undermine the purpose of makefiles and of @code{make}
|
|---|
| 4307 | itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
|
|---|
| 4308 | put in one of these variables, could have disastrous consequences and would
|
|---|
| 4309 | certainly have at least surprising and probably annoying effects.@refill
|
|---|
| 4310 |
|
|---|
| 4311 | @node -w Option, , Options/Recursion, Recursion
|
|---|
| 4312 | @subsection The @samp{--print-directory} Option
|
|---|
| 4313 | @cindex directories, printing them
|
|---|
| 4314 | @cindex printing directories
|
|---|
| 4315 | @cindex recursion, and printing directories
|
|---|
| 4316 |
|
|---|
| 4317 | If you use several levels of recursive @code{make} invocations, the
|
|---|
| 4318 | @samp{-w} or @w{@samp{--print-directory}} option can make the output a
|
|---|
| 4319 | lot easier to understand by showing each directory as @code{make}
|
|---|
| 4320 | starts processing it and as @code{make} finishes processing it. For
|
|---|
| 4321 | example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
|
|---|
| 4322 | @code{make} will print a line of the form:@refill
|
|---|
| 4323 |
|
|---|
| 4324 | @example
|
|---|
| 4325 | make: Entering directory `/u/gnu/make'.
|
|---|
| 4326 | @end example
|
|---|
| 4327 |
|
|---|
| 4328 | @noindent
|
|---|
| 4329 | before doing anything else, and a line of the form:
|
|---|
| 4330 |
|
|---|
| 4331 | @example
|
|---|
| 4332 | make: Leaving directory `/u/gnu/make'.
|
|---|
| 4333 | @end example
|
|---|
| 4334 |
|
|---|
| 4335 | @noindent
|
|---|
| 4336 | when processing is completed.
|
|---|
| 4337 |
|
|---|
| 4338 | @cindex @code{-C}, and @code{-w}
|
|---|
| 4339 | @cindex @code{--directory}, and @code{--print-directory}
|
|---|
| 4340 | @cindex recursion, and @code{-w}
|
|---|
| 4341 | @cindex @code{-w}, and @code{-C}
|
|---|
| 4342 | @cindex @code{-w}, and recursion
|
|---|
| 4343 | @cindex @code{--print-directory}, and @code{--directory}
|
|---|
| 4344 | @cindex @code{--print-directory}, and recursion
|
|---|
| 4345 | @cindex @code{--no-print-directory}
|
|---|
| 4346 | @cindex @code{--print-directory}, disabling
|
|---|
| 4347 | @cindex @code{-w}, disabling
|
|---|
| 4348 | Normally, you do not need to specify this option because @samp{make}
|
|---|
| 4349 | does it for you: @samp{-w} is turned on automatically when you use the
|
|---|
| 4350 | @samp{-C} option, and in sub-@code{make}s. @code{make} will not
|
|---|
| 4351 | automatically turn on @samp{-w} if you also use @samp{-s}, which says to
|
|---|
| 4352 | be silent, or if you use @samp{--no-print-directory} to explicitly
|
|---|
| 4353 | disable it.
|
|---|
| 4354 |
|
|---|
| 4355 | @node Sequences, Empty Commands, Recursion, Commands
|
|---|
| 4356 | @section Defining Canned Command Sequences
|
|---|
| 4357 | @cindex sequences of commands
|
|---|
| 4358 | @cindex commands, sequences of
|
|---|
| 4359 |
|
|---|
| 4360 | When the same sequence of commands is useful in making various targets, you
|
|---|
| 4361 | can define it as a canned sequence with the @code{define} directive, and
|
|---|
| 4362 | refer to the canned sequence from the rules for those targets. The canned
|
|---|
| 4363 | sequence is actually a variable, so the name must not conflict with other
|
|---|
| 4364 | variable names.
|
|---|
| 4365 |
|
|---|
| 4366 | Here is an example of defining a canned sequence of commands:
|
|---|
| 4367 |
|
|---|
| 4368 | @example
|
|---|
| 4369 | define run-yacc
|
|---|
| 4370 | yacc $(firstword $^)
|
|---|
| 4371 | mv y.tab.c $@@
|
|---|
| 4372 | endef
|
|---|
| 4373 | @end example
|
|---|
| 4374 | @cindex @code{yacc}
|
|---|
| 4375 |
|
|---|
| 4376 | @noindent
|
|---|
| 4377 | Here @code{run-yacc} is the name of the variable being defined;
|
|---|
| 4378 | @code{endef} marks the end of the definition; the lines in between are the
|
|---|
| 4379 | commands. The @code{define} directive does not expand variable references
|
|---|
| 4380 | and function calls in the canned sequence; the @samp{$} characters,
|
|---|
| 4381 | parentheses, variable names, and so on, all become part of the value of the
|
|---|
| 4382 | variable you are defining.
|
|---|
| 4383 | @xref{Defining, ,Defining Variables Verbatim},
|
|---|
| 4384 | for a complete explanation of @code{define}.
|
|---|
| 4385 |
|
|---|
| 4386 | The first command in this example runs Yacc on the first prerequisite of
|
|---|
| 4387 | whichever rule uses the canned sequence. The output file from Yacc is
|
|---|
| 4388 | always named @file{y.tab.c}. The second command moves the output to the
|
|---|
| 4389 | rule's target file name.
|
|---|
| 4390 |
|
|---|
| 4391 | To use the canned sequence, substitute the variable into the commands of a
|
|---|
| 4392 | rule. You can substitute it like any other variable
|
|---|
| 4393 | (@pxref{Reference, ,Basics of Variable References}).
|
|---|
| 4394 | Because variables defined by @code{define} are recursively expanded
|
|---|
| 4395 | variables, all the variable references you wrote inside the @code{define}
|
|---|
| 4396 | are expanded now. For example:
|
|---|
| 4397 |
|
|---|
| 4398 | @example
|
|---|
| 4399 | foo.c : foo.y
|
|---|
| 4400 | $(run-yacc)
|
|---|
| 4401 | @end example
|
|---|
| 4402 |
|
|---|
| 4403 | @noindent
|
|---|
| 4404 | @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
|
|---|
| 4405 | @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
|
|---|
| 4406 |
|
|---|
| 4407 | This is a realistic example, but this particular one is not needed in
|
|---|
| 4408 | practice because @code{make} has an implicit rule to figure out these
|
|---|
| 4409 | commands based on the file names involved
|
|---|
| 4410 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 4411 |
|
|---|
| 4412 | @cindex @@, and @code{define}
|
|---|
| 4413 | @cindex -, and @code{define}
|
|---|
| 4414 | @cindex +, and @code{define}
|
|---|
| 4415 | In command execution, each line of a canned sequence is treated just as
|
|---|
| 4416 | if the line appeared on its own in the rule, preceded by a tab. In
|
|---|
| 4417 | particular, @code{make} invokes a separate subshell for each line. You
|
|---|
| 4418 | can use the special prefix characters that affect command lines
|
|---|
| 4419 | (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
|
|---|
| 4420 | @xref{Commands, ,Writing the Commands in Rules}.
|
|---|
| 4421 | For example, using this canned sequence:
|
|---|
| 4422 |
|
|---|
| 4423 | @example
|
|---|
| 4424 | define frobnicate
|
|---|
| 4425 | @@echo "frobnicating target $@@"
|
|---|
| 4426 | frob-step-1 $< -o $@@-step-1
|
|---|
| 4427 | frob-step-2 $@@-step-1 -o $@@
|
|---|
| 4428 | endef
|
|---|
| 4429 | @end example
|
|---|
| 4430 |
|
|---|
| 4431 | @noindent
|
|---|
| 4432 | @code{make} will not echo the first line, the @code{echo} command.
|
|---|
| 4433 | But it @emph{will} echo the following two command lines.
|
|---|
| 4434 |
|
|---|
| 4435 | On the other hand, prefix characters on the command line that refers to
|
|---|
| 4436 | a canned sequence apply to every line in the sequence. So the rule:
|
|---|
| 4437 |
|
|---|
| 4438 | @example
|
|---|
| 4439 | frob.out: frob.in
|
|---|
| 4440 | @@$(frobnicate)
|
|---|
| 4441 | @end example
|
|---|
| 4442 |
|
|---|
| 4443 | @noindent
|
|---|
| 4444 | does not echo @emph{any} commands.
|
|---|
| 4445 | (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
|
|---|
| 4446 |
|
|---|
| 4447 | @node Empty Commands, , Sequences, Commands
|
|---|
| 4448 | @section Using Empty Commands
|
|---|
| 4449 | @cindex empty commands
|
|---|
| 4450 | @cindex commands, empty
|
|---|
| 4451 |
|
|---|
| 4452 | It is sometimes useful to define commands which do nothing. This is done
|
|---|
| 4453 | simply by giving a command that consists of nothing but whitespace. For
|
|---|
| 4454 | example:
|
|---|
| 4455 |
|
|---|
| 4456 | @example
|
|---|
| 4457 | target: ;
|
|---|
| 4458 | @end example
|
|---|
| 4459 |
|
|---|
| 4460 | @noindent
|
|---|
| 4461 | defines an empty command string for @file{target}. You could also use a
|
|---|
| 4462 | line beginning with a tab character to define an empty command string,
|
|---|
| 4463 | but this would be confusing because such a line looks empty.
|
|---|
| 4464 |
|
|---|
| 4465 | @findex .DEFAULT@r{, and empty commands}
|
|---|
| 4466 | You may be wondering why you would want to define a command string that
|
|---|
| 4467 | does nothing. The only reason this is useful is to prevent a target
|
|---|
| 4468 | from getting implicit commands (from implicit rules or the
|
|---|
| 4469 | @code{.DEFAULT} special target; @pxref{Implicit Rules} and
|
|---|
| 4470 | @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
|
|---|
| 4471 |
|
|---|
| 4472 | @c !!! another reason is for canonical stamp files:
|
|---|
| 4473 | @ignore
|
|---|
| 4474 | @example
|
|---|
| 4475 | foo: stamp-foo ;
|
|---|
| 4476 | stamp-foo: foo.in
|
|---|
| 4477 | create foo frm foo.in
|
|---|
| 4478 | touch $@
|
|---|
| 4479 | @end example
|
|---|
| 4480 | @end ignore
|
|---|
| 4481 |
|
|---|
| 4482 | You may be inclined to define empty command strings for targets that are
|
|---|
| 4483 | not actual files, but only exist so that their prerequisites can be
|
|---|
| 4484 | remade. However, this is not the best way to do that, because the
|
|---|
| 4485 | prerequisites may not be remade properly if the target file actually does exist.
|
|---|
| 4486 | @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
|
|---|
| 4487 |
|
|---|
| 4488 | @node Using Variables, Conditionals, Commands, Top
|
|---|
| 4489 | @chapter How to Use Variables
|
|---|
| 4490 | @cindex variable
|
|---|
| 4491 | @cindex value
|
|---|
| 4492 | @cindex recursive variable expansion
|
|---|
| 4493 | @cindex simple variable expansion
|
|---|
| 4494 |
|
|---|
| 4495 | A @dfn{variable} is a name defined in a makefile to represent a string
|
|---|
| 4496 | of text, called the variable's @dfn{value}. These values are
|
|---|
| 4497 | substituted by explicit request into targets, prerequisites, commands,
|
|---|
| 4498 | and other parts of the makefile. (In some other versions of @code{make},
|
|---|
| 4499 | variables are called @dfn{macros}.)
|
|---|
| 4500 | @cindex macro
|
|---|
| 4501 |
|
|---|
| 4502 | Variables and functions in all parts of a makefile are expanded when
|
|---|
| 4503 | read, except for the shell commands in rules, the right-hand sides of
|
|---|
| 4504 | variable definitions using @samp{=}, and the bodies of variable
|
|---|
| 4505 | definitions using the @code{define} directive.@refill
|
|---|
| 4506 |
|
|---|
| 4507 | Variables can represent lists of file names, options to pass to compilers,
|
|---|
| 4508 | programs to run, directories to look in for source files, directories to
|
|---|
| 4509 | write output in, or anything else you can imagine.
|
|---|
| 4510 |
|
|---|
| 4511 | A variable name may be any sequence of characters not containing @samp{:},
|
|---|
| 4512 | @samp{#}, @samp{=}, or leading or trailing whitespace. However,
|
|---|
| 4513 | variable names containing characters other than letters, numbers, and
|
|---|
| 4514 | underscores should be avoided, as they may be given special meanings in the
|
|---|
| 4515 | future, and with some shells they cannot be passed through the environment to a
|
|---|
| 4516 | sub-@code{make}
|
|---|
| 4517 | (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
|
|---|
| 4518 |
|
|---|
| 4519 | Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
|
|---|
| 4520 | and @samp{Foo} all refer to different variables.
|
|---|
| 4521 |
|
|---|
| 4522 | It is traditional to use upper case letters in variable names, but we
|
|---|
| 4523 | recommend using lower case letters for variable names that serve internal
|
|---|
| 4524 | purposes in the makefile, and reserving upper case for parameters that
|
|---|
| 4525 | control implicit rules or for parameters that the user should override with
|
|---|
| 4526 | command options (@pxref{Overriding, ,Overriding Variables}).
|
|---|
| 4527 |
|
|---|
| 4528 | A few variables have names that are a single punctuation character or
|
|---|
| 4529 | just a few characters. These are the @dfn{automatic variables}, and
|
|---|
| 4530 | they have particular specialized uses. @xref{Automatic Variables}.
|
|---|
| 4531 |
|
|---|
| 4532 | @menu
|
|---|
| 4533 | * Reference:: How to use the value of a variable.
|
|---|
| 4534 | * Flavors:: Variables come in two flavors.
|
|---|
| 4535 | * Advanced:: Advanced features for referencing a variable.
|
|---|
| 4536 | * Values:: All the ways variables get their values.
|
|---|
| 4537 | * Setting:: How to set a variable in the makefile.
|
|---|
| 4538 | * Appending:: How to append more text to the old value
|
|---|
| 4539 | of a variable.
|
|---|
| 4540 | * Override Directive:: How to set a variable in the makefile even if
|
|---|
| 4541 | the user has set it with a command argument.
|
|---|
| 4542 | * Defining:: An alternate way to set a variable
|
|---|
| 4543 | to a verbatim string.
|
|---|
| 4544 | * Environment:: Variable values can come from the environment.
|
|---|
| 4545 | * Target-specific:: Variable values can be defined on a per-target
|
|---|
| 4546 | basis.
|
|---|
| 4547 | * Pattern-specific:: Target-specific variable values can be applied
|
|---|
| 4548 | to a group of targets that match a pattern.
|
|---|
| 4549 | @end menu
|
|---|
| 4550 |
|
|---|
| 4551 | @node Reference, Flavors, Using Variables, Using Variables
|
|---|
| 4552 | @section Basics of Variable References
|
|---|
| 4553 | @cindex variables, how to reference
|
|---|
| 4554 | @cindex reference to variables
|
|---|
| 4555 | @cindex @code{$}, in variable reference
|
|---|
| 4556 | @cindex dollar sign (@code{$}), in variable reference
|
|---|
| 4557 |
|
|---|
| 4558 | To substitute a variable's value, write a dollar sign followed by the name
|
|---|
| 4559 | of the variable in parentheses or braces: either @samp{$(foo)} or
|
|---|
| 4560 | @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
|
|---|
| 4561 | special significance of @samp{$} is why you must write @samp{$$} to have
|
|---|
| 4562 | the effect of a single dollar sign in a file name or command.
|
|---|
| 4563 |
|
|---|
| 4564 | Variable references can be used in any context: targets, prerequisites,
|
|---|
| 4565 | commands, most directives, and new variable values. Here is an
|
|---|
| 4566 | example of a common case, where a variable holds the names of all the
|
|---|
| 4567 | object files in a program:
|
|---|
| 4568 |
|
|---|
| 4569 | @example
|
|---|
| 4570 | @group
|
|---|
| 4571 | objects = program.o foo.o utils.o
|
|---|
| 4572 | program : $(objects)
|
|---|
| 4573 | cc -o program $(objects)
|
|---|
| 4574 |
|
|---|
| 4575 | $(objects) : defs.h
|
|---|
| 4576 | @end group
|
|---|
| 4577 | @end example
|
|---|
| 4578 |
|
|---|
| 4579 | Variable references work by strict textual substitution. Thus, the rule
|
|---|
| 4580 |
|
|---|
| 4581 | @example
|
|---|
| 4582 | @group
|
|---|
| 4583 | foo = c
|
|---|
| 4584 | prog.o : prog.$(foo)
|
|---|
| 4585 | $(foo)$(foo) -$(foo) prog.$(foo)
|
|---|
| 4586 | @end group
|
|---|
| 4587 | @end example
|
|---|
| 4588 |
|
|---|
| 4589 | @noindent
|
|---|
| 4590 | could be used to compile a C program @file{prog.c}. Since spaces before
|
|---|
| 4591 | the variable value are ignored in variable assignments, the value of
|
|---|
| 4592 | @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
|
|---|
| 4593 | this way!)
|
|---|
| 4594 |
|
|---|
| 4595 | A dollar sign followed by a character other than a dollar sign,
|
|---|
| 4596 | open-parenthesis or open-brace treats that single character as the
|
|---|
| 4597 | variable name. Thus, you could reference the variable @code{x} with
|
|---|
| 4598 | @samp{$x}. However, this practice is strongly discouraged, except in
|
|---|
| 4599 | the case of the automatic variables (@pxref{Automatic Variables}).
|
|---|
| 4600 |
|
|---|
| 4601 | @node Flavors, Advanced, Reference, Using Variables
|
|---|
| 4602 | @section The Two Flavors of Variables
|
|---|
| 4603 | @cindex flavors of variables
|
|---|
| 4604 | @cindex recursive variable expansion
|
|---|
| 4605 | @cindex variables, flavors
|
|---|
| 4606 | @cindex recursively expanded variables
|
|---|
| 4607 | @cindex variables, recursively expanded
|
|---|
| 4608 |
|
|---|
| 4609 | There are two ways that a variable in GNU @code{make} can have a value;
|
|---|
| 4610 | we call them the two @dfn{flavors} of variables. The two flavors are
|
|---|
| 4611 | distinguished in how they are defined and in what they do when expanded.
|
|---|
| 4612 |
|
|---|
| 4613 | @cindex =
|
|---|
| 4614 | The first flavor of variable is a @dfn{recursively expanded} variable.
|
|---|
| 4615 | Variables of this sort are defined by lines using @samp{=}
|
|---|
| 4616 | (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
|
|---|
| 4617 | (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
|
|---|
| 4618 | is installed verbatim; if it contains references to other variables,
|
|---|
| 4619 | these references are expanded whenever this variable is substituted (in
|
|---|
| 4620 | the course of expanding some other string). When this happens, it is
|
|---|
| 4621 | called @dfn{recursive expansion}.@refill
|
|---|
| 4622 |
|
|---|
| 4623 | For example,
|
|---|
| 4624 |
|
|---|
| 4625 | @example
|
|---|
| 4626 | foo = $(bar)
|
|---|
| 4627 | bar = $(ugh)
|
|---|
| 4628 | ugh = Huh?
|
|---|
| 4629 |
|
|---|
| 4630 | all:;echo $(foo)
|
|---|
| 4631 | @end example
|
|---|
| 4632 |
|
|---|
| 4633 | @noindent
|
|---|
| 4634 | will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
|
|---|
| 4635 | expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
|
|---|
| 4636 |
|
|---|
| 4637 | This flavor of variable is the only sort supported by other versions of
|
|---|
| 4638 | @code{make}. It has its advantages and its disadvantages. An advantage
|
|---|
| 4639 | (most would say) is that:
|
|---|
| 4640 |
|
|---|
| 4641 | @example
|
|---|
| 4642 | CFLAGS = $(include_dirs) -O
|
|---|
| 4643 | include_dirs = -Ifoo -Ibar
|
|---|
| 4644 | @end example
|
|---|
| 4645 |
|
|---|
| 4646 | @noindent
|
|---|
| 4647 | will do what was intended: when @samp{CFLAGS} is expanded in a command,
|
|---|
| 4648 | it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
|
|---|
| 4649 | cannot append something on the end of a variable, as in
|
|---|
| 4650 |
|
|---|
| 4651 | @example
|
|---|
| 4652 | CFLAGS = $(CFLAGS) -O
|
|---|
| 4653 | @end example
|
|---|
| 4654 |
|
|---|
| 4655 | @noindent
|
|---|
| 4656 | because it will cause an infinite loop in the variable expansion.
|
|---|
| 4657 | (Actually @code{make} detects the infinite loop and reports an error.)
|
|---|
| 4658 | @cindex loops in variable expansion
|
|---|
| 4659 | @cindex variables, loops in expansion
|
|---|
| 4660 |
|
|---|
| 4661 | Another disadvantage is that any functions
|
|---|
| 4662 | (@pxref{Functions, ,Functions for Transforming Text})
|
|---|
| 4663 | referenced in the definition will be executed every time the variable is
|
|---|
| 4664 | expanded. This makes @code{make} run slower; worse, it causes the
|
|---|
| 4665 | @code{wildcard} and @code{shell} functions to give unpredictable results
|
|---|
| 4666 | because you cannot easily control when they are called, or even how many
|
|---|
| 4667 | times.
|
|---|
| 4668 |
|
|---|
| 4669 | To avoid all the problems and inconveniences of recursively expanded
|
|---|
| 4670 | variables, there is another flavor: simply expanded variables.
|
|---|
| 4671 |
|
|---|
| 4672 | @cindex simply expanded variables
|
|---|
| 4673 | @cindex variables, simply expanded
|
|---|
| 4674 | @cindex :=
|
|---|
| 4675 | @dfn{Simply expanded variables} are defined by lines using @samp{:=}
|
|---|
| 4676 | (@pxref{Setting, ,Setting Variables}).
|
|---|
| 4677 | The value of a simply expanded variable is scanned
|
|---|
| 4678 | once and for all, expanding any references to other variables and
|
|---|
| 4679 | functions, when the variable is defined. The actual value of the simply
|
|---|
| 4680 | expanded variable is the result of expanding the text that you write.
|
|---|
| 4681 | It does not contain any references to other variables; it contains their
|
|---|
| 4682 | values @emph{as of the time this variable was defined}. Therefore,
|
|---|
| 4683 |
|
|---|
| 4684 | @example
|
|---|
| 4685 | x := foo
|
|---|
| 4686 | y := $(x) bar
|
|---|
| 4687 | x := later
|
|---|
| 4688 | @end example
|
|---|
| 4689 |
|
|---|
| 4690 | @noindent
|
|---|
| 4691 | is equivalent to
|
|---|
| 4692 |
|
|---|
| 4693 | @example
|
|---|
| 4694 | y := foo bar
|
|---|
| 4695 | x := later
|
|---|
| 4696 | @end example
|
|---|
| 4697 |
|
|---|
| 4698 | When a simply expanded variable is referenced, its value is substituted
|
|---|
| 4699 | verbatim.
|
|---|
| 4700 |
|
|---|
| 4701 | Here is a somewhat more complicated example, illustrating the use of
|
|---|
| 4702 | @samp{:=} in conjunction with the @code{shell} function.
|
|---|
| 4703 | (@xref{Shell Function, , The @code{shell} Function}.) This example
|
|---|
| 4704 | also shows use of the variable @code{MAKELEVEL}, which is changed
|
|---|
| 4705 | when it is passed down from level to level.
|
|---|
| 4706 | (@xref{Variables/Recursion, , Communicating Variables to a
|
|---|
| 4707 | Sub-@code{make}}, for information about @code{MAKELEVEL}.)
|
|---|
| 4708 |
|
|---|
| 4709 | @vindex MAKELEVEL
|
|---|
| 4710 | @vindex MAKE
|
|---|
| 4711 | @example
|
|---|
| 4712 | @group
|
|---|
| 4713 | ifeq (0,$@{MAKELEVEL@})
|
|---|
| 4714 | cur-dir := $(shell pwd)
|
|---|
| 4715 | whoami := $(shell whoami)
|
|---|
| 4716 | host-type := $(shell arch)
|
|---|
| 4717 | MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
|
|---|
| 4718 | endif
|
|---|
| 4719 | @end group
|
|---|
| 4720 | @end example
|
|---|
| 4721 |
|
|---|
| 4722 | @noindent
|
|---|
| 4723 | An advantage of this use of @samp{:=} is that a typical
|
|---|
| 4724 | `descend into a directory' command then looks like this:
|
|---|
| 4725 |
|
|---|
| 4726 | @example
|
|---|
| 4727 | @group
|
|---|
| 4728 | $@{subdirs@}:
|
|---|
| 4729 | $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
|
|---|
| 4730 | @end group
|
|---|
| 4731 | @end example
|
|---|
| 4732 |
|
|---|
| 4733 | Simply expanded variables generally make complicated makefile programming
|
|---|
| 4734 | more predictable because they work like variables in most programming
|
|---|
| 4735 | languages. They allow you to redefine a variable using its own value (or
|
|---|
| 4736 | its value processed in some way by one of the expansion functions) and to
|
|---|
| 4737 | use the expansion functions much more efficiently
|
|---|
| 4738 | (@pxref{Functions, ,Functions for Transforming Text}).
|
|---|
| 4739 |
|
|---|
| 4740 | @cindex spaces, in variable values
|
|---|
| 4741 | @cindex whitespace, in variable values
|
|---|
| 4742 | @cindex variables, spaces in values
|
|---|
| 4743 | You can also use them to introduce controlled leading whitespace into
|
|---|
| 4744 | variable values. Leading whitespace characters are discarded from your
|
|---|
| 4745 | input before substitution of variable references and function calls;
|
|---|
| 4746 | this means you can include leading spaces in a variable value by
|
|---|
| 4747 | protecting them with variable references, like this:
|
|---|
| 4748 |
|
|---|
| 4749 | @example
|
|---|
| 4750 | nullstring :=
|
|---|
| 4751 | space := $(nullstring) # end of the line
|
|---|
| 4752 | @end example
|
|---|
| 4753 |
|
|---|
| 4754 | @noindent
|
|---|
| 4755 | Here the value of the variable @code{space} is precisely one space. The
|
|---|
| 4756 | comment @w{@samp{# end of the line}} is included here just for clarity.
|
|---|
| 4757 | Since trailing space characters are @emph{not} stripped from variable
|
|---|
| 4758 | values, just a space at the end of the line would have the same effect
|
|---|
| 4759 | (but be rather hard to read). If you put whitespace at the end of a
|
|---|
| 4760 | variable value, it is a good idea to put a comment like that at the end
|
|---|
| 4761 | of the line to make your intent clear. Conversely, if you do @emph{not}
|
|---|
| 4762 | want any whitespace characters at the end of your variable value, you
|
|---|
| 4763 | must remember not to put a random comment on the end of the line after
|
|---|
| 4764 | some whitespace, such as this:
|
|---|
| 4765 |
|
|---|
| 4766 | @example
|
|---|
| 4767 | dir := /foo/bar # directory to put the frobs in
|
|---|
| 4768 | @end example
|
|---|
| 4769 |
|
|---|
| 4770 | @noindent
|
|---|
| 4771 | Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
|
|---|
| 4772 | (with four trailing spaces), which was probably not the intention.
|
|---|
| 4773 | (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
|
|---|
| 4774 |
|
|---|
| 4775 | @cindex conditional variable assignment
|
|---|
| 4776 | @cindex variables, conditional assignment
|
|---|
| 4777 | @cindex ?=
|
|---|
| 4778 | There is another assignment operator for variables, @samp{?=}. This
|
|---|
| 4779 | is called a conditional variable assignment operator, because it only
|
|---|
| 4780 | has an effect if the variable is not yet defined. This statement:
|
|---|
| 4781 |
|
|---|
| 4782 | @example
|
|---|
| 4783 | FOO ?= bar
|
|---|
| 4784 | @end example
|
|---|
| 4785 |
|
|---|
| 4786 | @noindent
|
|---|
| 4787 | is exactly equivalent to this
|
|---|
| 4788 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
|---|
| 4789 |
|
|---|
| 4790 | @example
|
|---|
| 4791 | ifeq ($(origin FOO), undefined)
|
|---|
| 4792 | FOO = bar
|
|---|
| 4793 | endif
|
|---|
| 4794 | @end example
|
|---|
| 4795 |
|
|---|
| 4796 | Note that a variable set to an empty value is still defined, so
|
|---|
| 4797 | @samp{?=} will not set that variable.
|
|---|
| 4798 |
|
|---|
| 4799 | @node Advanced, Values, Flavors, Using Variables
|
|---|
| 4800 | @section Advanced Features for Reference to Variables
|
|---|
| 4801 | @cindex reference to variables
|
|---|
| 4802 |
|
|---|
| 4803 | This section describes some advanced features you can use to reference
|
|---|
| 4804 | variables in more flexible ways.
|
|---|
| 4805 |
|
|---|
| 4806 | @menu
|
|---|
| 4807 | * Substitution Refs:: Referencing a variable with
|
|---|
| 4808 | substitutions on the value.
|
|---|
| 4809 | * Computed Names:: Computing the name of the variable to refer to.
|
|---|
| 4810 | @end menu
|
|---|
| 4811 |
|
|---|
| 4812 | @node Substitution Refs, Computed Names, Advanced, Advanced
|
|---|
| 4813 | @subsection Substitution References
|
|---|
| 4814 | @cindex modified variable reference
|
|---|
| 4815 | @cindex substitution variable reference
|
|---|
| 4816 | @cindex variables, modified reference
|
|---|
| 4817 | @cindex variables, substitution reference
|
|---|
| 4818 |
|
|---|
| 4819 | @cindex variables, substituting suffix in
|
|---|
| 4820 | @cindex suffix, substituting in variables
|
|---|
| 4821 | A @dfn{substitution reference} substitutes the value of a variable with
|
|---|
| 4822 | alterations that you specify. It has the form
|
|---|
| 4823 | @samp{$(@var{var}:@var{a}=@var{b})} (or
|
|---|
| 4824 | @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
|
|---|
| 4825 | of the variable @var{var}, replace every @var{a} at the end of a word with
|
|---|
| 4826 | @var{b} in that value, and substitute the resulting string.
|
|---|
| 4827 |
|
|---|
| 4828 | When we say ``at the end of a word'', we mean that @var{a} must appear
|
|---|
| 4829 | either followed by whitespace or at the end of the value in order to be
|
|---|
| 4830 | replaced; other occurrences of @var{a} in the value are unaltered. For
|
|---|
| 4831 | example:@refill
|
|---|
| 4832 |
|
|---|
| 4833 | @example
|
|---|
| 4834 | foo := a.o b.o c.o
|
|---|
| 4835 | bar := $(foo:.o=.c)
|
|---|
| 4836 | @end example
|
|---|
| 4837 |
|
|---|
| 4838 | @noindent
|
|---|
| 4839 | sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
|
|---|
| 4840 |
|
|---|
| 4841 | A substitution reference is actually an abbreviation for use of the
|
|---|
| 4842 | @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
|
|---|
| 4843 | substitution references as well as @code{patsubst} for compatibility with
|
|---|
| 4844 | other implementations of @code{make}.
|
|---|
| 4845 |
|
|---|
| 4846 | @findex patsubst
|
|---|
| 4847 | Another type of substitution reference lets you use the full power of
|
|---|
| 4848 | the @code{patsubst} function. It has the same form
|
|---|
| 4849 | @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
|
|---|
| 4850 | @var{a} must contain a single @samp{%} character. This case is
|
|---|
| 4851 | equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
|
|---|
| 4852 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
|---|
| 4853 | for a description of the @code{patsubst} function.@refill
|
|---|
| 4854 |
|
|---|
| 4855 | @example
|
|---|
| 4856 | @group
|
|---|
| 4857 | @exdent For example:
|
|---|
| 4858 |
|
|---|
| 4859 | foo := a.o b.o c.o
|
|---|
| 4860 | bar := $(foo:%.o=%.c)
|
|---|
| 4861 | @end group
|
|---|
| 4862 | @end example
|
|---|
| 4863 |
|
|---|
| 4864 | @noindent
|
|---|
| 4865 | sets @samp{bar} to @samp{a.c b.c c.c}.
|
|---|
| 4866 |
|
|---|
| 4867 | @node Computed Names, , Substitution Refs, Advanced
|
|---|
| 4868 | @subsection Computed Variable Names
|
|---|
| 4869 | @cindex nested variable reference
|
|---|
| 4870 | @cindex computed variable name
|
|---|
| 4871 | @cindex variables, computed names
|
|---|
| 4872 | @cindex variables, nested references
|
|---|
| 4873 | @cindex variables, @samp{$} in name
|
|---|
| 4874 | @cindex @code{$}, in variable name
|
|---|
| 4875 | @cindex dollar sign (@code{$}), in variable name
|
|---|
| 4876 |
|
|---|
| 4877 | Computed variable names are a complicated concept needed only for
|
|---|
| 4878 | sophisticated makefile programming. For most purposes you need not
|
|---|
| 4879 | consider them, except to know that making a variable with a dollar sign
|
|---|
| 4880 | in its name might have strange results. However, if you are the type
|
|---|
| 4881 | that wants to understand everything, or you are actually interested in
|
|---|
| 4882 | what they do, read on.
|
|---|
| 4883 |
|
|---|
| 4884 | Variables may be referenced inside the name of a variable. This is
|
|---|
| 4885 | called a @dfn{computed variable name} or a @dfn{nested variable
|
|---|
| 4886 | reference}. For example,
|
|---|
| 4887 |
|
|---|
| 4888 | @example
|
|---|
| 4889 | x = y
|
|---|
| 4890 | y = z
|
|---|
| 4891 | a := $($(x))
|
|---|
| 4892 | @end example
|
|---|
| 4893 |
|
|---|
| 4894 | @noindent
|
|---|
| 4895 | defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
|
|---|
| 4896 | to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
|
|---|
| 4897 | to @samp{z}. Here the name of the variable to reference is not stated
|
|---|
| 4898 | explicitly; it is computed by expansion of @samp{$(x)}. The reference
|
|---|
| 4899 | @samp{$(x)} here is nested within the outer variable reference.
|
|---|
| 4900 |
|
|---|
| 4901 | The previous example shows two levels of nesting, but any number of levels
|
|---|
| 4902 | is possible. For example, here are three levels:
|
|---|
| 4903 |
|
|---|
| 4904 | @example
|
|---|
| 4905 | x = y
|
|---|
| 4906 | y = z
|
|---|
| 4907 | z = u
|
|---|
| 4908 | a := $($($(x)))
|
|---|
| 4909 | @end example
|
|---|
| 4910 |
|
|---|
| 4911 | @noindent
|
|---|
| 4912 | Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
|
|---|
| 4913 | expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
|
|---|
| 4914 | @samp{$(z)}, which becomes @samp{u}.
|
|---|
| 4915 |
|
|---|
| 4916 | References to recursively-expanded variables within a variable name are
|
|---|
| 4917 | reexpanded in the usual fashion. For example:
|
|---|
| 4918 |
|
|---|
| 4919 | @example
|
|---|
| 4920 | x = $(y)
|
|---|
| 4921 | y = z
|
|---|
| 4922 | z = Hello
|
|---|
| 4923 | a := $($(x))
|
|---|
| 4924 | @end example
|
|---|
| 4925 |
|
|---|
| 4926 | @noindent
|
|---|
| 4927 | defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
|
|---|
| 4928 | which becomes @samp{$(z)} which becomes @samp{Hello}.
|
|---|
| 4929 |
|
|---|
| 4930 | Nested variable references can also contain modified references and
|
|---|
| 4931 | function invocations (@pxref{Functions, ,Functions for Transforming Text}),
|
|---|
| 4932 | just like any other reference.
|
|---|
| 4933 | For example, using the @code{subst} function
|
|---|
| 4934 | (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
|---|
| 4935 |
|
|---|
| 4936 | @example
|
|---|
| 4937 | @group
|
|---|
| 4938 | x = variable1
|
|---|
| 4939 | variable2 := Hello
|
|---|
| 4940 | y = $(subst 1,2,$(x))
|
|---|
| 4941 | z = y
|
|---|
| 4942 | a := $($($(z)))
|
|---|
| 4943 | @end group
|
|---|
| 4944 | @end example
|
|---|
| 4945 |
|
|---|
| 4946 | @noindent
|
|---|
| 4947 | eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
|
|---|
| 4948 | would ever want to write a nested reference as convoluted as this one, but
|
|---|
| 4949 | it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
|
|---|
| 4950 | @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
|
|---|
| 4951 | @code{x} and changes it by substitution to @samp{variable2}, so that the
|
|---|
| 4952 | entire string becomes @samp{$(variable2)}, a simple variable reference
|
|---|
| 4953 | whose value is @samp{Hello}.@refill
|
|---|
| 4954 |
|
|---|
| 4955 | A computed variable name need not consist entirely of a single variable
|
|---|
| 4956 | reference. It can contain several variable references, as well as some
|
|---|
| 4957 | invariant text. For example,
|
|---|
| 4958 |
|
|---|
| 4959 | @example
|
|---|
| 4960 | @group
|
|---|
| 4961 | a_dirs := dira dirb
|
|---|
| 4962 | 1_dirs := dir1 dir2
|
|---|
| 4963 | @end group
|
|---|
| 4964 |
|
|---|
| 4965 | @group
|
|---|
| 4966 | a_files := filea fileb
|
|---|
| 4967 | 1_files := file1 file2
|
|---|
| 4968 | @end group
|
|---|
| 4969 |
|
|---|
| 4970 | @group
|
|---|
| 4971 | ifeq "$(use_a)" "yes"
|
|---|
| 4972 | a1 := a
|
|---|
| 4973 | else
|
|---|
| 4974 | a1 := 1
|
|---|
| 4975 | endif
|
|---|
| 4976 | @end group
|
|---|
| 4977 |
|
|---|
| 4978 | @group
|
|---|
| 4979 | ifeq "$(use_dirs)" "yes"
|
|---|
| 4980 | df := dirs
|
|---|
| 4981 | else
|
|---|
| 4982 | df := files
|
|---|
| 4983 | endif
|
|---|
| 4984 |
|
|---|
| 4985 | dirs := $($(a1)_$(df))
|
|---|
| 4986 | @end group
|
|---|
| 4987 | @end example
|
|---|
| 4988 |
|
|---|
| 4989 | @noindent
|
|---|
| 4990 | will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
|
|---|
| 4991 | @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
|
|---|
| 4992 | and @code{use_dirs}.@refill
|
|---|
| 4993 |
|
|---|
| 4994 | Computed variable names can also be used in substitution references:
|
|---|
| 4995 |
|
|---|
| 4996 | @example
|
|---|
| 4997 | @group
|
|---|
| 4998 | a_objects := a.o b.o c.o
|
|---|
| 4999 | 1_objects := 1.o 2.o 3.o
|
|---|
| 5000 |
|
|---|
| 5001 | sources := $($(a1)_objects:.o=.c)
|
|---|
| 5002 | @end group
|
|---|
| 5003 | @end example
|
|---|
| 5004 |
|
|---|
| 5005 | @noindent
|
|---|
| 5006 | defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
|
|---|
| 5007 | depending on the value of @code{a1}.
|
|---|
| 5008 |
|
|---|
| 5009 | The only restriction on this sort of use of nested variable references
|
|---|
| 5010 | is that they cannot specify part of the name of a function to be called.
|
|---|
| 5011 | This is because the test for a recognized function name is done before
|
|---|
| 5012 | the expansion of nested references. For example,
|
|---|
| 5013 |
|
|---|
| 5014 | @example
|
|---|
| 5015 | @group
|
|---|
| 5016 | ifdef do_sort
|
|---|
| 5017 | func := sort
|
|---|
| 5018 | else
|
|---|
| 5019 | func := strip
|
|---|
| 5020 | endif
|
|---|
| 5021 | @end group
|
|---|
| 5022 |
|
|---|
| 5023 | @group
|
|---|
| 5024 | bar := a d b g q c
|
|---|
| 5025 | @end group
|
|---|
| 5026 |
|
|---|
| 5027 | @group
|
|---|
| 5028 | foo := $($(func) $(bar))
|
|---|
| 5029 | @end group
|
|---|
| 5030 | @end example
|
|---|
| 5031 |
|
|---|
| 5032 | @noindent
|
|---|
| 5033 | attempts to give @samp{foo} the value of the variable @samp{sort a d b g
|
|---|
| 5034 | q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
|
|---|
| 5035 | as the argument to either the @code{sort} or the @code{strip} function.
|
|---|
| 5036 | This restriction could be removed in the future if that change is shown
|
|---|
| 5037 | to be a good idea.
|
|---|
| 5038 |
|
|---|
| 5039 | You can also use computed variable names in the left-hand side of a
|
|---|
| 5040 | variable assignment, or in a @code{define} directive, as in:
|
|---|
| 5041 |
|
|---|
| 5042 | @example
|
|---|
| 5043 | dir = foo
|
|---|
| 5044 | $(dir)_sources := $(wildcard $(dir)/*.c)
|
|---|
| 5045 | define $(dir)_print
|
|---|
| 5046 | lpr $($(dir)_sources)
|
|---|
| 5047 | endef
|
|---|
| 5048 | @end example
|
|---|
| 5049 |
|
|---|
| 5050 | @noindent
|
|---|
| 5051 | This example defines the variables @samp{dir}, @samp{foo_sources}, and
|
|---|
| 5052 | @samp{foo_print}.
|
|---|
| 5053 |
|
|---|
| 5054 | Note that @dfn{nested variable references} are quite different from
|
|---|
| 5055 | @dfn{recursively expanded variables}
|
|---|
| 5056 | (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
|
|---|
| 5057 | used together in complex ways when doing makefile programming.@refill
|
|---|
| 5058 |
|
|---|
| 5059 | @node Values, Setting, Advanced, Using Variables
|
|---|
| 5060 | @section How Variables Get Their Values
|
|---|
| 5061 | @cindex variables, how they get their values
|
|---|
| 5062 | @cindex value, how a variable gets it
|
|---|
| 5063 |
|
|---|
| 5064 | Variables can get values in several different ways:
|
|---|
| 5065 |
|
|---|
| 5066 | @itemize @bullet
|
|---|
| 5067 | @item
|
|---|
| 5068 | You can specify an overriding value when you run @code{make}.
|
|---|
| 5069 | @xref{Overriding, ,Overriding Variables}.
|
|---|
| 5070 |
|
|---|
| 5071 | @item
|
|---|
| 5072 | You can specify a value in the makefile, either
|
|---|
| 5073 | with an assignment (@pxref{Setting, ,Setting Variables}) or with a
|
|---|
| 5074 | verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
|
|---|
| 5075 |
|
|---|
| 5076 | @item
|
|---|
| 5077 | Variables in the environment become @code{make} variables.
|
|---|
| 5078 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 5079 |
|
|---|
| 5080 | @item
|
|---|
| 5081 | Several @dfn{automatic} variables are given new values for each rule.
|
|---|
| 5082 | Each of these has a single conventional use.
|
|---|
| 5083 | @xref{Automatic Variables}.
|
|---|
| 5084 |
|
|---|
| 5085 | @item
|
|---|
| 5086 | Several variables have constant initial values.
|
|---|
| 5087 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 5088 | @end itemize
|
|---|
| 5089 |
|
|---|
| 5090 | @node Setting, Appending, Values, Using Variables
|
|---|
| 5091 | @section Setting Variables
|
|---|
| 5092 | @cindex setting variables
|
|---|
| 5093 | @cindex variables, setting
|
|---|
| 5094 | @cindex =
|
|---|
| 5095 | @cindex :=
|
|---|
| 5096 | @cindex ?=
|
|---|
| 5097 |
|
|---|
| 5098 | To set a variable from the makefile, write a line starting with the
|
|---|
| 5099 | variable name followed by @samp{=} or @samp{:=}. Whatever follows the
|
|---|
| 5100 | @samp{=} or @samp{:=} on the line becomes the value. For example,
|
|---|
| 5101 |
|
|---|
| 5102 | @example
|
|---|
| 5103 | objects = main.o foo.o bar.o utils.o
|
|---|
| 5104 | @end example
|
|---|
| 5105 |
|
|---|
| 5106 | @noindent
|
|---|
| 5107 | defines a variable named @code{objects}. Whitespace around the variable
|
|---|
| 5108 | name and immediately after the @samp{=} is ignored.
|
|---|
| 5109 |
|
|---|
| 5110 | Variables defined with @samp{=} are @dfn{recursively expanded} variables.
|
|---|
| 5111 | Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
|
|---|
| 5112 | definitions can contain variable references which will be expanded before
|
|---|
| 5113 | the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
|
|---|
| 5114 |
|
|---|
| 5115 | The variable name may contain function and variable references, which
|
|---|
| 5116 | are expanded when the line is read to find the actual variable name to use.
|
|---|
| 5117 |
|
|---|
| 5118 | There is no limit on the length of the value of a variable except the
|
|---|
| 5119 | amount of swapping space on the computer. When a variable definition is
|
|---|
| 5120 | long, it is a good idea to break it into several lines by inserting
|
|---|
| 5121 | backslash-newline at convenient places in the definition. This will not
|
|---|
| 5122 | affect the functioning of @code{make}, but it will make the makefile easier
|
|---|
| 5123 | to read.
|
|---|
| 5124 |
|
|---|
| 5125 | Most variable names are considered to have the empty string as a value if
|
|---|
| 5126 | you have never set them. Several variables have built-in initial values
|
|---|
| 5127 | that are not empty, but you can set them in the usual ways
|
|---|
| 5128 | (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
|
|---|
| 5129 | Several special variables are set
|
|---|
| 5130 | automatically to a new value for each rule; these are called the
|
|---|
| 5131 | @dfn{automatic} variables (@pxref{Automatic Variables}).
|
|---|
| 5132 |
|
|---|
| 5133 | If you'd like a variable to be set to a value only if it's not already
|
|---|
| 5134 | set, then you can use the shorthand operator @samp{?=} instead of
|
|---|
| 5135 | @samp{=}. These two settings of the variable @samp{FOO} are identical
|
|---|
| 5136 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
|---|
| 5137 |
|
|---|
| 5138 | @example
|
|---|
| 5139 | FOO ?= bar
|
|---|
| 5140 | @end example
|
|---|
| 5141 |
|
|---|
| 5142 | @noindent
|
|---|
| 5143 | and
|
|---|
| 5144 |
|
|---|
| 5145 | @example
|
|---|
| 5146 | ifeq ($(origin FOO), undefined)
|
|---|
| 5147 | FOO = bar
|
|---|
| 5148 | endif
|
|---|
| 5149 | @end example
|
|---|
| 5150 |
|
|---|
| 5151 | @node Appending, Override Directive, Setting, Using Variables
|
|---|
| 5152 | @section Appending More Text to Variables
|
|---|
| 5153 | @cindex +=
|
|---|
| 5154 | @cindex appending to variables
|
|---|
| 5155 | @cindex variables, appending to
|
|---|
| 5156 |
|
|---|
| 5157 | Often it is useful to add more text to the value of a variable already defined.
|
|---|
| 5158 | You do this with a line containing @samp{+=}, like this:
|
|---|
| 5159 |
|
|---|
| 5160 | @example
|
|---|
| 5161 | objects += another.o
|
|---|
| 5162 | @end example
|
|---|
| 5163 |
|
|---|
| 5164 | @noindent
|
|---|
| 5165 | This takes the value of the variable @code{objects}, and adds the text
|
|---|
| 5166 | @samp{another.o} to it (preceded by a single space). Thus:
|
|---|
| 5167 |
|
|---|
| 5168 | @example
|
|---|
| 5169 | objects = main.o foo.o bar.o utils.o
|
|---|
| 5170 | objects += another.o
|
|---|
| 5171 | @end example
|
|---|
| 5172 |
|
|---|
| 5173 | @noindent
|
|---|
| 5174 | sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
|
|---|
| 5175 |
|
|---|
| 5176 | Using @samp{+=} is similar to:
|
|---|
| 5177 |
|
|---|
| 5178 | @example
|
|---|
| 5179 | objects = main.o foo.o bar.o utils.o
|
|---|
| 5180 | objects := $(objects) another.o
|
|---|
| 5181 | @end example
|
|---|
| 5182 |
|
|---|
| 5183 | @noindent
|
|---|
| 5184 | but differs in ways that become important when you use more complex values.
|
|---|
| 5185 |
|
|---|
| 5186 | When the variable in question has not been defined before, @samp{+=}
|
|---|
| 5187 | acts just like normal @samp{=}: it defines a recursively-expanded
|
|---|
| 5188 | variable. However, when there @emph{is} a previous definition, exactly
|
|---|
| 5189 | what @samp{+=} does depends on what flavor of variable you defined
|
|---|
| 5190 | originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
|
|---|
| 5191 | explanation of the two flavors of variables.
|
|---|
| 5192 |
|
|---|
| 5193 | When you add to a variable's value with @samp{+=}, @code{make} acts
|
|---|
| 5194 | essentially as if you had included the extra text in the initial
|
|---|
| 5195 | definition of the variable. If you defined it first with @samp{:=},
|
|---|
| 5196 | making it a simply-expanded variable, @samp{+=} adds to that
|
|---|
| 5197 | simply-expanded definition, and expands the new text before appending it
|
|---|
| 5198 | to the old value just as @samp{:=} does
|
|---|
| 5199 | (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
|
|---|
| 5200 | In fact,
|
|---|
| 5201 |
|
|---|
| 5202 | @example
|
|---|
| 5203 | variable := value
|
|---|
| 5204 | variable += more
|
|---|
| 5205 | @end example
|
|---|
| 5206 |
|
|---|
| 5207 | @noindent
|
|---|
| 5208 | is exactly equivalent to:
|
|---|
| 5209 |
|
|---|
| 5210 | @noindent
|
|---|
| 5211 | @example
|
|---|
| 5212 | variable := value
|
|---|
| 5213 | variable := $(variable) more
|
|---|
| 5214 | @end example
|
|---|
| 5215 |
|
|---|
| 5216 | On the other hand, when you use @samp{+=} with a variable that you defined
|
|---|
| 5217 | first to be recursively-expanded using plain @samp{=}, @code{make} does
|
|---|
| 5218 | something a bit different. Recall that when you define a
|
|---|
| 5219 | recursively-expanded variable, @code{make} does not expand the value you set
|
|---|
| 5220 | for variable and function references immediately. Instead it stores the text
|
|---|
| 5221 | verbatim, and saves these variable and function references to be expanded
|
|---|
| 5222 | later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
|
|---|
| 5223 | of Variables}). When you use @samp{+=} on a recursively-expanded variable,
|
|---|
| 5224 | it is this unexpanded text to which @code{make} appends the new text you
|
|---|
| 5225 | specify.
|
|---|
| 5226 |
|
|---|
| 5227 | @example
|
|---|
| 5228 | @group
|
|---|
| 5229 | variable = value
|
|---|
| 5230 | variable += more
|
|---|
| 5231 | @end group
|
|---|
| 5232 | @end example
|
|---|
| 5233 |
|
|---|
| 5234 | @noindent
|
|---|
| 5235 | is roughly equivalent to:
|
|---|
| 5236 |
|
|---|
| 5237 | @example
|
|---|
| 5238 | @group
|
|---|
| 5239 | temp = value
|
|---|
| 5240 | variable = $(temp) more
|
|---|
| 5241 | @end group
|
|---|
| 5242 | @end example
|
|---|
| 5243 |
|
|---|
| 5244 | @noindent
|
|---|
| 5245 | except that of course it never defines a variable called @code{temp}.
|
|---|
| 5246 | The importance of this comes when the variable's old value contains
|
|---|
| 5247 | variable references. Take this common example:
|
|---|
| 5248 |
|
|---|
| 5249 | @example
|
|---|
| 5250 | CFLAGS = $(includes) -O
|
|---|
| 5251 | @dots{}
|
|---|
| 5252 | CFLAGS += -pg # enable profiling
|
|---|
| 5253 | @end example
|
|---|
| 5254 |
|
|---|
| 5255 | @noindent
|
|---|
| 5256 | The first line defines the @code{CFLAGS} variable with a reference to another
|
|---|
| 5257 | variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
|
|---|
| 5258 | compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
|
|---|
| 5259 | Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
|
|---|
| 5260 | variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
|
|---|
| 5261 | @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
|
|---|
| 5262 | need not be defined yet for its value to take effect. It only has to be
|
|---|
| 5263 | defined before any reference to @code{CFLAGS}. If we tried to append to the
|
|---|
| 5264 | value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
|
|---|
| 5265 |
|
|---|
| 5266 | @example
|
|---|
| 5267 | CFLAGS := $(CFLAGS) -pg # enable profiling
|
|---|
| 5268 | @end example
|
|---|
| 5269 |
|
|---|
| 5270 | @noindent
|
|---|
| 5271 | This is pretty close, but not quite what we want. Using @samp{:=}
|
|---|
| 5272 | redefines @code{CFLAGS} as a simply-expanded variable; this means
|
|---|
| 5273 | @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
|
|---|
| 5274 | variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
|
|---|
| 5275 | -pg}}, and a later definition of @code{includes} will have no effect.
|
|---|
| 5276 | Conversely, by using @samp{+=} we set @code{CFLAGS} to the
|
|---|
| 5277 | @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
|
|---|
| 5278 | the reference to @code{includes}, so if that variable gets defined at
|
|---|
| 5279 | any later point, a reference like @samp{$(CFLAGS)} still uses its
|
|---|
| 5280 | value.
|
|---|
| 5281 |
|
|---|
| 5282 | @node Override Directive, Defining, Appending, Using Variables
|
|---|
| 5283 | @section The @code{override} Directive
|
|---|
| 5284 | @findex override
|
|---|
| 5285 | @cindex overriding with @code{override}
|
|---|
| 5286 | @cindex variables, overriding
|
|---|
| 5287 |
|
|---|
| 5288 | If a variable has been set with a command argument
|
|---|
| 5289 | (@pxref{Overriding, ,Overriding Variables}),
|
|---|
| 5290 | then ordinary assignments in the makefile are ignored. If you want to set
|
|---|
| 5291 | the variable in the makefile even though it was set with a command
|
|---|
| 5292 | argument, you can use an @code{override} directive, which is a line that
|
|---|
| 5293 | looks like this:@refill
|
|---|
| 5294 |
|
|---|
| 5295 | @example
|
|---|
| 5296 | override @var{variable} = @var{value}
|
|---|
| 5297 | @end example
|
|---|
| 5298 |
|
|---|
| 5299 | @noindent
|
|---|
| 5300 | or
|
|---|
| 5301 |
|
|---|
| 5302 | @example
|
|---|
| 5303 | override @var{variable} := @var{value}
|
|---|
| 5304 | @end example
|
|---|
| 5305 |
|
|---|
| 5306 | To append more text to a variable defined on the command line, use:
|
|---|
| 5307 |
|
|---|
| 5308 | @example
|
|---|
| 5309 | override @var{variable} += @var{more text}
|
|---|
| 5310 | @end example
|
|---|
| 5311 |
|
|---|
| 5312 | @noindent
|
|---|
| 5313 | @xref{Appending, ,Appending More Text to Variables}.
|
|---|
| 5314 |
|
|---|
| 5315 | The @code{override} directive was not invented for escalation in the war
|
|---|
| 5316 | between makefiles and command arguments. It was invented so you can alter
|
|---|
| 5317 | and add to values that the user specifies with command arguments.
|
|---|
| 5318 |
|
|---|
| 5319 | For example, suppose you always want the @samp{-g} switch when you run the
|
|---|
| 5320 | C compiler, but you would like to allow the user to specify the other
|
|---|
| 5321 | switches with a command argument just as usual. You could use this
|
|---|
| 5322 | @code{override} directive:
|
|---|
| 5323 |
|
|---|
| 5324 | @example
|
|---|
| 5325 | override CFLAGS += -g
|
|---|
| 5326 | @end example
|
|---|
| 5327 |
|
|---|
| 5328 | You can also use @code{override} directives with @code{define} directives.
|
|---|
| 5329 | This is done as you might expect:
|
|---|
| 5330 |
|
|---|
| 5331 | @example
|
|---|
| 5332 | override define foo
|
|---|
| 5333 | bar
|
|---|
| 5334 | endef
|
|---|
| 5335 | @end example
|
|---|
| 5336 |
|
|---|
| 5337 | @noindent
|
|---|
| 5338 | @iftex
|
|---|
| 5339 | See the next section for information about @code{define}.
|
|---|
| 5340 | @end iftex
|
|---|
| 5341 | @ifnottex
|
|---|
| 5342 | @xref{Defining, ,Defining Variables Verbatim}.
|
|---|
| 5343 | @end ifnottex
|
|---|
| 5344 |
|
|---|
| 5345 | @node Defining, Environment, Override Directive, Using Variables
|
|---|
| 5346 | @section Defining Variables Verbatim
|
|---|
| 5347 | @findex define
|
|---|
| 5348 | @findex endef
|
|---|
| 5349 | @cindex verbatim variable definition
|
|---|
| 5350 | @cindex defining variables verbatim
|
|---|
| 5351 | @cindex variables, defining verbatim
|
|---|
| 5352 |
|
|---|
| 5353 | Another way to set the value of a variable is to use the @code{define}
|
|---|
| 5354 | directive. This directive has an unusual syntax which allows newline
|
|---|
| 5355 | characters to be included in the value, which is convenient for defining
|
|---|
| 5356 | both canned sequences of commands
|
|---|
| 5357 | (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
|
|---|
| 5358 | sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
|
|---|
| 5359 |
|
|---|
| 5360 | The @code{define} directive is followed on the same line by the name of the
|
|---|
| 5361 | variable and nothing more. The value to give the variable appears on the
|
|---|
| 5362 | following lines. The end of the value is marked by a line containing just
|
|---|
| 5363 | the word @code{endef}. Aside from this difference in syntax, @code{define}
|
|---|
| 5364 | works just like @samp{=}: it creates a recursively-expanded variable
|
|---|
| 5365 | (@pxref{Flavors, ,The Two Flavors of Variables}).
|
|---|
| 5366 | The variable name may contain function and variable references, which
|
|---|
| 5367 | are expanded when the directive is read to find the actual variable name
|
|---|
| 5368 | to use.
|
|---|
| 5369 |
|
|---|
| 5370 | You may nest @code{define} directives: @code{make} will keep track of
|
|---|
| 5371 | nested directives and report an error if they are not all properly
|
|---|
| 5372 | closed with @code{endef}. Note that lines beginning with tab
|
|---|
| 5373 | characters are considered part of a command script, so any
|
|---|
| 5374 | @code{define} or @code{endef} strings appearing on such a line will
|
|---|
| 5375 | not be considered @code{make} operators.
|
|---|
| 5376 |
|
|---|
| 5377 | @example
|
|---|
| 5378 | define two-lines
|
|---|
| 5379 | echo foo
|
|---|
| 5380 | echo $(bar)
|
|---|
| 5381 | endef
|
|---|
| 5382 | @end example
|
|---|
| 5383 |
|
|---|
| 5384 | The value in an ordinary assignment cannot contain a newline; but the
|
|---|
| 5385 | newlines that separate the lines of the value in a @code{define} become
|
|---|
| 5386 | part of the variable's value (except for the final newline which precedes
|
|---|
| 5387 | the @code{endef} and is not considered part of the value).@refill
|
|---|
| 5388 |
|
|---|
| 5389 | @need 800
|
|---|
| 5390 | When used in a command script, the previous example is functionally
|
|---|
| 5391 | equivalent to this:
|
|---|
| 5392 |
|
|---|
| 5393 | @example
|
|---|
| 5394 | two-lines = echo foo; echo $(bar)
|
|---|
| 5395 | @end example
|
|---|
| 5396 |
|
|---|
| 5397 | @noindent
|
|---|
| 5398 | since two commands separated by semicolon behave much like two separate
|
|---|
| 5399 | shell commands. However, note that using two separate lines means
|
|---|
| 5400 | @code{make} will invoke the shell twice, running an independent subshell
|
|---|
| 5401 | for each line. @xref{Execution, ,Command Execution}.
|
|---|
| 5402 |
|
|---|
| 5403 | If you want variable definitions made with @code{define} to take
|
|---|
| 5404 | precedence over command-line variable definitions, you can use the
|
|---|
| 5405 | @code{override} directive together with @code{define}:
|
|---|
| 5406 |
|
|---|
| 5407 | @example
|
|---|
| 5408 | override define two-lines
|
|---|
| 5409 | foo
|
|---|
| 5410 | $(bar)
|
|---|
| 5411 | endef
|
|---|
| 5412 | @end example
|
|---|
| 5413 |
|
|---|
| 5414 | @noindent
|
|---|
| 5415 | @xref{Override Directive, ,The @code{override} Directive}.
|
|---|
| 5416 |
|
|---|
| 5417 | @node Environment, Target-specific, Defining, Using Variables
|
|---|
| 5418 | @section Variables from the Environment
|
|---|
| 5419 |
|
|---|
| 5420 | @cindex variables, environment
|
|---|
| 5421 | @cindex environment
|
|---|
| 5422 | Variables in @code{make} can come from the environment in which
|
|---|
| 5423 | @code{make} is run. Every environment variable that @code{make} sees
|
|---|
| 5424 | when it starts up is transformed into a @code{make} variable with the
|
|---|
| 5425 | same name and value. However, an explicit assignment in the makefile,
|
|---|
| 5426 | or with a command argument, overrides the environment. (If the
|
|---|
| 5427 | @samp{-e} flag is specified, then values from the environment override
|
|---|
| 5428 | assignments in the makefile. @xref{Options Summary, ,Summary of
|
|---|
| 5429 | Options}. But this is not recommended practice.)
|
|---|
| 5430 |
|
|---|
| 5431 | Thus, by setting the variable @code{CFLAGS} in your environment, you can
|
|---|
| 5432 | cause all C compilations in most makefiles to use the compiler switches you
|
|---|
| 5433 | prefer. This is safe for variables with standard or conventional meanings
|
|---|
| 5434 | because you know that no makefile will use them for other things. (Note
|
|---|
| 5435 | this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
|
|---|
| 5436 | and therefore are not affected by the value in the environment.)
|
|---|
| 5437 |
|
|---|
| 5438 | When @code{make} runs a command script, variables defined in the
|
|---|
| 5439 | makefile are placed into the environment of that command. This allows
|
|---|
| 5440 | you to pass values to sub-@code{make} invocations. (@pxref{Recursion,
|
|---|
| 5441 | ,Recursive Use of @code{make}}). By default, only variables that came
|
|---|
| 5442 | from the environment or the command line are passed to recursive
|
|---|
| 5443 | invocations. You can use the @code{export} directive to pass other
|
|---|
| 5444 | variables. @xref{Variables/Recursion, , Communicating Variables to a
|
|---|
| 5445 | Sub-@code{make}}, for full details.
|
|---|
| 5446 |
|
|---|
| 5447 | Other use of variables from the environment is not recommended. It is not
|
|---|
| 5448 | wise for makefiles to depend for their functioning on environment variables
|
|---|
| 5449 | set up outside their control, since this would cause different users to get
|
|---|
| 5450 | different results from the same makefile. This is against the whole
|
|---|
| 5451 | purpose of most makefiles.
|
|---|
| 5452 |
|
|---|
| 5453 | @cindex SHELL, import from environment
|
|---|
| 5454 | Such problems would be especially likely with the variable @code{SHELL},
|
|---|
| 5455 | which is normally present in the environment to specify the user's choice
|
|---|
| 5456 | of interactive shell. It would be very undesirable for this choice to
|
|---|
| 5457 | affect @code{make}. So @code{make} ignores the environment value of
|
|---|
| 5458 | @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
|
|---|
| 5459 | usually not set. @xref{Execution, ,Special handling of SHELL on
|
|---|
| 5460 | MS-DOS}.)@refill
|
|---|
| 5461 |
|
|---|
| 5462 | @cindex SHELL, export to environment
|
|---|
| 5463 | The @code{SHELL} variable is special in another way: just as the value
|
|---|
| 5464 | of the @code{make} variable @code{SHELL} is not taken from the
|
|---|
| 5465 | environment, so also it is not placed into the environment of commands
|
|---|
| 5466 | that @code{make} invokes. Instead, the value of @code{SHELL} from the
|
|---|
| 5467 | invoking environment is provided to the command. You can use
|
|---|
| 5468 | @code{export SHELL} to force the value of the @code{make} variable
|
|---|
| 5469 | @code{SHELL} to be placed in the environment of commands.
|
|---|
| 5470 |
|
|---|
| 5471 | @node Target-specific, Pattern-specific, Environment, Using Variables
|
|---|
| 5472 | @section Target-specific Variable Values
|
|---|
| 5473 | @cindex target-specific variables
|
|---|
| 5474 | @cindex variables, target-specific
|
|---|
| 5475 |
|
|---|
| 5476 | Variable values in @code{make} are usually global; that is, they are the
|
|---|
| 5477 | same regardless of where they are evaluated (unless they're reset, of
|
|---|
| 5478 | course). One exception to that is automatic variables
|
|---|
| 5479 | (@pxref{Automatic Variables}).
|
|---|
| 5480 |
|
|---|
| 5481 | The other exception is @dfn{target-specific variable values}. This
|
|---|
| 5482 | feature allows you to define different values for the same variable,
|
|---|
| 5483 | based on the target that @code{make} is currently building. As with
|
|---|
| 5484 | automatic variables, these values are only available within the context
|
|---|
| 5485 | of a target's command script (and in other target-specific assignments).
|
|---|
| 5486 |
|
|---|
| 5487 | Set a target-specific variable value like this:
|
|---|
| 5488 |
|
|---|
| 5489 | @example
|
|---|
| 5490 | @var{target} @dots{} : @var{variable-assignment}
|
|---|
| 5491 | @end example
|
|---|
| 5492 |
|
|---|
| 5493 | @noindent
|
|---|
| 5494 | or like this:
|
|---|
| 5495 |
|
|---|
| 5496 | @example
|
|---|
| 5497 | @var{target} @dots{} : override @var{variable-assignment}
|
|---|
| 5498 | @end example
|
|---|
| 5499 |
|
|---|
| 5500 | @noindent
|
|---|
| 5501 | or like this:
|
|---|
| 5502 |
|
|---|
| 5503 | @example
|
|---|
| 5504 | @var{target} @dots{} : export @var{variable-assignment}
|
|---|
| 5505 | @end example
|
|---|
| 5506 |
|
|---|
| 5507 | Multiple @var{target} values create a target-specific variable value for
|
|---|
| 5508 | each member of the target list individually.
|
|---|
| 5509 |
|
|---|
| 5510 | The @var{variable-assignment} can be any valid form of assignment;
|
|---|
| 5511 | recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
|
|---|
| 5512 | conditional (@samp{?=}). All variables that appear within the
|
|---|
| 5513 | @var{variable-assignment} are evaluated within the context of the
|
|---|
| 5514 | target: thus, any previously-defined target-specific variable values
|
|---|
| 5515 | will be in effect. Note that this variable is actually distinct from
|
|---|
| 5516 | any ``global'' value: the two variables do not have to have the same
|
|---|
| 5517 | flavor (recursive vs. static).
|
|---|
| 5518 |
|
|---|
| 5519 | Target-specific variables have the same priority as any other makefile
|
|---|
| 5520 | variable. Variables provided on the command-line (and in the
|
|---|
| 5521 | environment if the @samp{-e} option is in force) will take precedence.
|
|---|
| 5522 | Specifying the @code{override} directive will allow the target-specific
|
|---|
| 5523 | variable value to be preferred.
|
|---|
| 5524 |
|
|---|
| 5525 | There is one more special feature of target-specific variables: when
|
|---|
| 5526 | you define a target-specific variable that variable value is also in
|
|---|
| 5527 | effect for all prerequisites of this target, and all their
|
|---|
| 5528 | prerequisites, etc. (unless those prerequisites override that variable
|
|---|
| 5529 | with their own target-specific variable value). So, for example, a
|
|---|
| 5530 | statement like this:
|
|---|
| 5531 |
|
|---|
| 5532 | @example
|
|---|
| 5533 | prog : CFLAGS = -g
|
|---|
| 5534 | prog : prog.o foo.o bar.o
|
|---|
| 5535 | @end example
|
|---|
| 5536 |
|
|---|
| 5537 | @noindent
|
|---|
| 5538 | will set @code{CFLAGS} to @samp{-g} in the command script for
|
|---|
| 5539 | @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
|
|---|
| 5540 | command scripts that create @file{prog.o}, @file{foo.o}, and
|
|---|
| 5541 | @file{bar.o}, and any command scripts which create their
|
|---|
| 5542 | prerequisites.
|
|---|
| 5543 |
|
|---|
| 5544 | Be aware that a given prerequisite will only be built once per
|
|---|
| 5545 | invocation of make, at most. If the same file is a prerequisite of
|
|---|
| 5546 | multiple targets, and each of those targets has a different value for
|
|---|
| 5547 | the same target-specific variable, then the first target to be built
|
|---|
| 5548 | will cause that prerequisite to be built and the prerequisite will
|
|---|
| 5549 | inherit the target-specific value from the first target. It will
|
|---|
| 5550 | ignore the target-specific values from any other targets.
|
|---|
| 5551 |
|
|---|
| 5552 | @node Pattern-specific, , Target-specific, Using Variables
|
|---|
| 5553 | @section Pattern-specific Variable Values
|
|---|
| 5554 | @cindex pattern-specific variables
|
|---|
| 5555 | @cindex variables, pattern-specific
|
|---|
| 5556 |
|
|---|
| 5557 | In addition to target-specific variable values
|
|---|
| 5558 | (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
|
|---|
| 5559 | @code{make} supports pattern-specific variable values. In this form,
|
|---|
| 5560 | the variable is defined for any target that matches the pattern
|
|---|
| 5561 | specified. If a target matches more than one pattern, all the
|
|---|
| 5562 | matching pattern-specific variables are interpreted in the order in
|
|---|
| 5563 | which they were defined in the makefile, and collected together into
|
|---|
| 5564 | one set. Variables defined in this way are searched after any
|
|---|
| 5565 | target-specific variables defined explicitly for that target, and
|
|---|
| 5566 | before target-specific variables defined for the parent target.
|
|---|
| 5567 |
|
|---|
| 5568 | Set a pattern-specific variable value like this:
|
|---|
| 5569 |
|
|---|
| 5570 | @example
|
|---|
| 5571 | @var{pattern} @dots{} : @var{variable-assignment}
|
|---|
| 5572 | @end example
|
|---|
| 5573 |
|
|---|
| 5574 | @noindent
|
|---|
| 5575 | or like this:
|
|---|
| 5576 |
|
|---|
| 5577 | @example
|
|---|
| 5578 | @var{pattern} @dots{} : override @var{variable-assignment}
|
|---|
| 5579 | @end example
|
|---|
| 5580 |
|
|---|
| 5581 | @noindent
|
|---|
| 5582 | where @var{pattern} is a %-pattern. As with target-specific variable
|
|---|
| 5583 | values, multiple @var{pattern} values create a pattern-specific variable
|
|---|
| 5584 | value for each pattern individually. The @var{variable-assignment} can
|
|---|
| 5585 | be any valid form of assignment. Any command-line variable setting will
|
|---|
| 5586 | take precedence, unless @code{override} is specified.
|
|---|
| 5587 |
|
|---|
| 5588 | For example:
|
|---|
| 5589 |
|
|---|
| 5590 | @example
|
|---|
| 5591 | %.o : CFLAGS = -O
|
|---|
| 5592 | @end example
|
|---|
| 5593 |
|
|---|
| 5594 | @noindent
|
|---|
| 5595 | will assign @code{CFLAGS} the value of @samp{-O} for all targets
|
|---|
| 5596 | matching the pattern @code{%.o}.
|
|---|
| 5597 |
|
|---|
| 5598 | @node Conditionals, Functions, Using Variables, Top
|
|---|
| 5599 | @chapter Conditional Parts of Makefiles
|
|---|
| 5600 |
|
|---|
| 5601 | @cindex conditionals
|
|---|
| 5602 | A @dfn{conditional} causes part of a makefile to be obeyed or ignored
|
|---|
| 5603 | depending on the values of variables. Conditionals can compare the
|
|---|
| 5604 | value of one variable to another, or the value of a variable to
|
|---|
| 5605 | a constant string. Conditionals control what @code{make} actually
|
|---|
| 5606 | ``sees'' in the makefile, so they @emph{cannot} be used to control shell
|
|---|
| 5607 | commands at the time of execution.@refill
|
|---|
| 5608 |
|
|---|
| 5609 | @menu
|
|---|
| 5610 | * Conditional Example:: Example of a conditional
|
|---|
| 5611 | * Conditional Syntax:: The syntax of conditionals.
|
|---|
| 5612 | * Testing Flags:: Conditionals that test flags.
|
|---|
| 5613 | @end menu
|
|---|
| 5614 |
|
|---|
| 5615 | @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
|
|---|
| 5616 | @section Example of a Conditional
|
|---|
| 5617 |
|
|---|
| 5618 | The following example of a conditional tells @code{make} to use one set
|
|---|
| 5619 | of libraries if the @code{CC} variable is @samp{gcc}, and a different
|
|---|
| 5620 | set of libraries otherwise. It works by controlling which of two
|
|---|
| 5621 | command lines will be used as the command for a rule. The result is
|
|---|
| 5622 | that @samp{CC=gcc} as an argument to @code{make} changes not only which
|
|---|
| 5623 | compiler is used but also which libraries are linked.
|
|---|
| 5624 |
|
|---|
| 5625 | @example
|
|---|
| 5626 | libs_for_gcc = -lgnu
|
|---|
| 5627 | normal_libs =
|
|---|
| 5628 |
|
|---|
| 5629 | foo: $(objects)
|
|---|
| 5630 | ifeq ($(CC),gcc)
|
|---|
| 5631 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
|---|
| 5632 | else
|
|---|
| 5633 | $(CC) -o foo $(objects) $(normal_libs)
|
|---|
| 5634 | endif
|
|---|
| 5635 | @end example
|
|---|
| 5636 |
|
|---|
| 5637 | This conditional uses three directives: one @code{ifeq}, one @code{else}
|
|---|
| 5638 | and one @code{endif}.
|
|---|
| 5639 |
|
|---|
| 5640 | The @code{ifeq} directive begins the conditional, and specifies the
|
|---|
| 5641 | condition. It contains two arguments, separated by a comma and surrounded
|
|---|
| 5642 | by parentheses. Variable substitution is performed on both arguments and
|
|---|
| 5643 | then they are compared. The lines of the makefile following the
|
|---|
| 5644 | @code{ifeq} are obeyed if the two arguments match; otherwise they are
|
|---|
| 5645 | ignored.
|
|---|
| 5646 |
|
|---|
| 5647 | The @code{else} directive causes the following lines to be obeyed if the
|
|---|
| 5648 | previous conditional failed. In the example above, this means that the
|
|---|
| 5649 | second alternative linking command is used whenever the first alternative
|
|---|
| 5650 | is not used. It is optional to have an @code{else} in a conditional.
|
|---|
| 5651 |
|
|---|
| 5652 | The @code{endif} directive ends the conditional. Every conditional must
|
|---|
| 5653 | end with an @code{endif}. Unconditional makefile text follows.
|
|---|
| 5654 |
|
|---|
| 5655 | As this example illustrates, conditionals work at the textual level:
|
|---|
| 5656 | the lines of the conditional are treated as part of the makefile, or
|
|---|
| 5657 | ignored, according to the condition. This is why the larger syntactic
|
|---|
| 5658 | units of the makefile, such as rules, may cross the beginning or the
|
|---|
| 5659 | end of the conditional.
|
|---|
| 5660 |
|
|---|
| 5661 | When the variable @code{CC} has the value @samp{gcc}, the above example has
|
|---|
| 5662 | this effect:
|
|---|
| 5663 |
|
|---|
| 5664 | @example
|
|---|
| 5665 | foo: $(objects)
|
|---|
| 5666 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
|---|
| 5667 | @end example
|
|---|
| 5668 |
|
|---|
| 5669 | @noindent
|
|---|
| 5670 | When the variable @code{CC} has any other value, the effect is this:
|
|---|
| 5671 |
|
|---|
| 5672 | @example
|
|---|
| 5673 | foo: $(objects)
|
|---|
| 5674 | $(CC) -o foo $(objects) $(normal_libs)
|
|---|
| 5675 | @end example
|
|---|
| 5676 |
|
|---|
| 5677 | Equivalent results can be obtained in another way by conditionalizing a
|
|---|
| 5678 | variable assignment and then using the variable unconditionally:
|
|---|
| 5679 |
|
|---|
| 5680 | @example
|
|---|
| 5681 | libs_for_gcc = -lgnu
|
|---|
| 5682 | normal_libs =
|
|---|
| 5683 |
|
|---|
| 5684 | ifeq ($(CC),gcc)
|
|---|
| 5685 | libs=$(libs_for_gcc)
|
|---|
| 5686 | else
|
|---|
| 5687 | libs=$(normal_libs)
|
|---|
| 5688 | endif
|
|---|
| 5689 |
|
|---|
| 5690 | foo: $(objects)
|
|---|
| 5691 | $(CC) -o foo $(objects) $(libs)
|
|---|
| 5692 | @end example
|
|---|
| 5693 |
|
|---|
| 5694 | @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
|
|---|
| 5695 | @section Syntax of Conditionals
|
|---|
| 5696 | @findex ifdef
|
|---|
| 5697 | @findex ifeq
|
|---|
| 5698 | @findex ifndef
|
|---|
| 5699 | @findex ifneq
|
|---|
| 5700 | @findex else
|
|---|
| 5701 | @findex endif
|
|---|
| 5702 |
|
|---|
| 5703 | The syntax of a simple conditional with no @code{else} is as follows:
|
|---|
| 5704 |
|
|---|
| 5705 | @example
|
|---|
| 5706 | @var{conditional-directive}
|
|---|
| 5707 | @var{text-if-true}
|
|---|
| 5708 | endif
|
|---|
| 5709 | @end example
|
|---|
| 5710 |
|
|---|
| 5711 | @noindent
|
|---|
| 5712 | The @var{text-if-true} may be any lines of text, to be considered as part
|
|---|
| 5713 | of the makefile if the condition is true. If the condition is false, no
|
|---|
| 5714 | text is used instead.
|
|---|
| 5715 |
|
|---|
| 5716 | The syntax of a complex conditional is as follows:
|
|---|
| 5717 |
|
|---|
| 5718 | @example
|
|---|
| 5719 | @var{conditional-directive}
|
|---|
| 5720 | @var{text-if-true}
|
|---|
| 5721 | else
|
|---|
| 5722 | @var{text-if-false}
|
|---|
| 5723 | endif
|
|---|
| 5724 | @end example
|
|---|
| 5725 |
|
|---|
| 5726 | or:
|
|---|
| 5727 |
|
|---|
| 5728 | @example
|
|---|
| 5729 | @var{conditional-directive}
|
|---|
| 5730 | @var{text-if-one-is-true}
|
|---|
| 5731 | else @var{conditional-directive}
|
|---|
| 5732 | @var{text-if-true}
|
|---|
| 5733 | else
|
|---|
| 5734 | @var{text-if-false}
|
|---|
| 5735 | endif
|
|---|
| 5736 | @end example
|
|---|
| 5737 |
|
|---|
| 5738 | @noindent
|
|---|
| 5739 | There can be as many ``@code{else} @var{conditional-directive}''
|
|---|
| 5740 | clauses as necessary. Once a given condition is true,
|
|---|
| 5741 | @var{text-if-true} is used and no other clause is used; if no
|
|---|
| 5742 | condition is true then @var{text-if-false} is used. The
|
|---|
| 5743 | @var{text-if-true} and @var{text-if-false} can be any number of lines
|
|---|
| 5744 | of text.
|
|---|
| 5745 |
|
|---|
| 5746 | The syntax of the @var{conditional-directive} is the same whether the
|
|---|
| 5747 | conditional is simple or complex; after an @code{else} or not. There
|
|---|
| 5748 | are four different directives that test different conditions. Here is
|
|---|
| 5749 | a table of them:
|
|---|
| 5750 |
|
|---|
| 5751 | @table @code
|
|---|
| 5752 | @item ifeq (@var{arg1}, @var{arg2})
|
|---|
| 5753 | @itemx ifeq '@var{arg1}' '@var{arg2}'
|
|---|
| 5754 | @itemx ifeq "@var{arg1}" "@var{arg2}"
|
|---|
| 5755 | @itemx ifeq "@var{arg1}" '@var{arg2}'
|
|---|
| 5756 | @itemx ifeq '@var{arg1}' "@var{arg2}"
|
|---|
| 5757 | Expand all variable references in @var{arg1} and @var{arg2} and
|
|---|
| 5758 | compare them. If they are identical, the @var{text-if-true} is
|
|---|
| 5759 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
|---|
| 5760 |
|
|---|
| 5761 | Often you want to test if a variable has a non-empty value. When the
|
|---|
| 5762 | value results from complex expansions of variables and functions,
|
|---|
| 5763 | expansions you would consider empty may actually contain whitespace
|
|---|
| 5764 | characters and thus are not seen as empty. However, you can use the
|
|---|
| 5765 | @code{strip} function (@pxref{Text Functions}) to avoid interpreting
|
|---|
| 5766 | whitespace as a non-empty value. For example:
|
|---|
| 5767 |
|
|---|
| 5768 | @example
|
|---|
| 5769 | @group
|
|---|
| 5770 | ifeq ($(strip $(foo)),)
|
|---|
| 5771 | @var{text-if-empty}
|
|---|
| 5772 | endif
|
|---|
| 5773 | @end group
|
|---|
| 5774 | @end example
|
|---|
| 5775 |
|
|---|
| 5776 | @noindent
|
|---|
| 5777 | will evaluate @var{text-if-empty} even if the expansion of
|
|---|
| 5778 | @code{$(foo)} contains whitespace characters.
|
|---|
| 5779 |
|
|---|
| 5780 | @item ifneq (@var{arg1}, @var{arg2})
|
|---|
| 5781 | @itemx ifneq '@var{arg1}' '@var{arg2}'
|
|---|
| 5782 | @itemx ifneq "@var{arg1}" "@var{arg2}"
|
|---|
| 5783 | @itemx ifneq "@var{arg1}" '@var{arg2}'
|
|---|
| 5784 | @itemx ifneq '@var{arg1}' "@var{arg2}"
|
|---|
| 5785 | Expand all variable references in @var{arg1} and @var{arg2} and
|
|---|
| 5786 | compare them. If they are different, the @var{text-if-true} is
|
|---|
| 5787 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
|---|
| 5788 |
|
|---|
| 5789 | @item ifdef @var{variable-name}
|
|---|
| 5790 | The @code{ifdef} form takes the @emph{name} of a variable as its
|
|---|
| 5791 | argument, not a reference to a variable. The value of that variable
|
|---|
| 5792 | has a non-empty value, the @var{text-if-true} is effective; otherwise,
|
|---|
| 5793 | the @var{text-if-false}, if any, is effective. Variables that have
|
|---|
| 5794 | never been defined have an empty value. The text @var{variable-name}
|
|---|
| 5795 | is expanded, so it could be a variable or function that expands
|
|---|
| 5796 | to the name of a variable. For example:
|
|---|
| 5797 |
|
|---|
| 5798 | @example
|
|---|
| 5799 | bar = true
|
|---|
| 5800 | foo = bar
|
|---|
| 5801 | ifdef $(foo)
|
|---|
| 5802 | frobozz = yes
|
|---|
| 5803 | endif
|
|---|
| 5804 | @end example
|
|---|
| 5805 |
|
|---|
| 5806 | The variable reference @code{$(foo)} is expanded, yielding @code{bar},
|
|---|
| 5807 | which is considered to be the name of a variable. The variable
|
|---|
| 5808 | @code{bar} is not expanded, but its value is examined to determine if
|
|---|
| 5809 | it is non-empty.
|
|---|
| 5810 |
|
|---|
| 5811 | Note that @code{ifdef} only tests whether a variable has a value. It
|
|---|
| 5812 | does not expand the variable to see if that value is nonempty.
|
|---|
| 5813 | Consequently, tests using @code{ifdef} return true for all definitions
|
|---|
| 5814 | except those like @code{foo =}. To test for an empty value, use
|
|---|
| 5815 | @w{@code{ifeq ($(foo),)}}. For example,
|
|---|
| 5816 |
|
|---|
| 5817 | @example
|
|---|
| 5818 | bar =
|
|---|
| 5819 | foo = $(bar)
|
|---|
| 5820 | ifdef foo
|
|---|
| 5821 | frobozz = yes
|
|---|
| 5822 | else
|
|---|
| 5823 | frobozz = no
|
|---|
| 5824 | endif
|
|---|
| 5825 | @end example
|
|---|
| 5826 |
|
|---|
| 5827 | @noindent
|
|---|
| 5828 | sets @samp{frobozz} to @samp{yes}, while:
|
|---|
| 5829 |
|
|---|
| 5830 | @example
|
|---|
| 5831 | foo =
|
|---|
| 5832 | ifdef foo
|
|---|
| 5833 | frobozz = yes
|
|---|
| 5834 | else
|
|---|
| 5835 | frobozz = no
|
|---|
| 5836 | endif
|
|---|
| 5837 | @end example
|
|---|
| 5838 |
|
|---|
| 5839 | @noindent
|
|---|
| 5840 | sets @samp{frobozz} to @samp{no}.
|
|---|
| 5841 |
|
|---|
| 5842 | @item ifndef @var{variable-name}
|
|---|
| 5843 | If the variable @var{variable-name} has an empty value, the
|
|---|
| 5844 | @var{text-if-true} is effective; otherwise, the @var{text-if-false},
|
|---|
| 5845 | if any, is effective. The rules for expansion and testing of
|
|---|
| 5846 | @var{variable-name} are identical to the @code{ifdef} directive.
|
|---|
| 5847 | @end table
|
|---|
| 5848 |
|
|---|
| 5849 | Extra spaces are allowed and ignored at the beginning of the conditional
|
|---|
| 5850 | directive line, but a tab is not allowed. (If the line begins with a tab,
|
|---|
| 5851 | it will be considered a command for a rule.) Aside from this, extra spaces
|
|---|
| 5852 | or tabs may be inserted with no effect anywhere except within the directive
|
|---|
| 5853 | name or within an argument. A comment starting with @samp{#} may appear at
|
|---|
| 5854 | the end of the line.
|
|---|
| 5855 |
|
|---|
| 5856 | The other two directives that play a part in a conditional are @code{else}
|
|---|
| 5857 | and @code{endif}. Each of these directives is written as one word, with no
|
|---|
| 5858 | arguments. Extra spaces are allowed and ignored at the beginning of the
|
|---|
| 5859 | line, and spaces or tabs at the end. A comment starting with @samp{#} may
|
|---|
| 5860 | appear at the end of the line.
|
|---|
| 5861 |
|
|---|
| 5862 | Conditionals affect which lines of the makefile @code{make} uses. If
|
|---|
| 5863 | the condition is true, @code{make} reads the lines of the
|
|---|
| 5864 | @var{text-if-true} as part of the makefile; if the condition is false,
|
|---|
| 5865 | @code{make} ignores those lines completely. It follows that syntactic
|
|---|
| 5866 | units of the makefile, such as rules, may safely be split across the
|
|---|
| 5867 | beginning or the end of the conditional.@refill
|
|---|
| 5868 |
|
|---|
| 5869 | @code{make} evaluates conditionals when it reads a makefile.
|
|---|
| 5870 | Consequently, you cannot use automatic variables in the tests of
|
|---|
| 5871 | conditionals because they are not defined until commands are run
|
|---|
| 5872 | (@pxref{Automatic Variables}).
|
|---|
| 5873 |
|
|---|
| 5874 | To prevent intolerable confusion, it is not permitted to start a
|
|---|
| 5875 | conditional in one makefile and end it in another. However, you may
|
|---|
| 5876 | write an @code{include} directive within a conditional, provided you do
|
|---|
| 5877 | not attempt to terminate the conditional inside the included file.
|
|---|
| 5878 |
|
|---|
| 5879 | @node Testing Flags, , Conditional Syntax, Conditionals
|
|---|
| 5880 | @section Conditionals that Test Flags
|
|---|
| 5881 |
|
|---|
| 5882 | You can write a conditional that tests @code{make} command flags such as
|
|---|
| 5883 | @samp{-t} by using the variable @code{MAKEFLAGS} together with the
|
|---|
| 5884 | @code{findstring} function
|
|---|
| 5885 | (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
|
|---|
| 5886 | This is useful when @code{touch} is not enough to make a file appear up
|
|---|
| 5887 | to date.
|
|---|
| 5888 |
|
|---|
| 5889 | The @code{findstring} function determines whether one string appears as a
|
|---|
| 5890 | substring of another. If you want to test for the @samp{-t} flag,
|
|---|
| 5891 | use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
|
|---|
| 5892 | the other.
|
|---|
| 5893 |
|
|---|
| 5894 | For example, here is how to arrange to use @samp{ranlib -t} to finish
|
|---|
| 5895 | marking an archive file up to date:
|
|---|
| 5896 |
|
|---|
| 5897 | @example
|
|---|
| 5898 | archive.a: @dots{}
|
|---|
| 5899 | ifneq (,$(findstring t,$(MAKEFLAGS)))
|
|---|
| 5900 | +touch archive.a
|
|---|
| 5901 | +ranlib -t archive.a
|
|---|
| 5902 | else
|
|---|
| 5903 | ranlib archive.a
|
|---|
| 5904 | endif
|
|---|
| 5905 | @end example
|
|---|
| 5906 |
|
|---|
| 5907 | @noindent
|
|---|
| 5908 | The @samp{+} prefix marks those command lines as ``recursive'' so
|
|---|
| 5909 | that they will be executed despite use of the @samp{-t} flag.
|
|---|
| 5910 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 5911 |
|
|---|
| 5912 | @node Functions, Running, Conditionals, Top
|
|---|
| 5913 | @chapter Functions for Transforming Text
|
|---|
| 5914 | @cindex functions
|
|---|
| 5915 |
|
|---|
| 5916 | @dfn{Functions} allow you to do text processing in the makefile to compute
|
|---|
| 5917 | the files to operate on or the commands to use. You use a function in a
|
|---|
| 5918 | @dfn{function call}, where you give the name of the function and some text
|
|---|
| 5919 | (the @dfn{arguments}) for the function to operate on. The result of the
|
|---|
| 5920 | function's processing is substituted into the makefile at the point of the
|
|---|
| 5921 | call, just as a variable might be substituted.
|
|---|
| 5922 |
|
|---|
| 5923 | @menu
|
|---|
| 5924 | * Syntax of Functions:: How to write a function call.
|
|---|
| 5925 | * Text Functions:: General-purpose text manipulation functions.
|
|---|
| 5926 | * File Name Functions:: Functions for manipulating file names.
|
|---|
| 5927 | * Foreach Function:: Repeat some text with controlled variation.
|
|---|
| 5928 | * If Function:: Conditionally expand a value.
|
|---|
| 5929 | * Call Function:: Expand a user-defined function.
|
|---|
| 5930 | * Value Function:: Return the un-expanded value of a variable.
|
|---|
| 5931 | * Eval Function:: Evaluate the arguments as makefile syntax.
|
|---|
| 5932 | * Origin Function:: Find where a variable got its value.
|
|---|
| 5933 | * Shell Function:: Substitute the output of a shell command.
|
|---|
| 5934 | * Make Control Functions:: Functions that control how make runs.
|
|---|
| 5935 | @end menu
|
|---|
| 5936 |
|
|---|
| 5937 | @node Syntax of Functions, Text Functions, Functions, Functions
|
|---|
| 5938 | @section Function Call Syntax
|
|---|
| 5939 | @cindex @code{$}, in function call
|
|---|
| 5940 | @cindex dollar sign (@code{$}), in function call
|
|---|
| 5941 | @cindex arguments of functions
|
|---|
| 5942 | @cindex functions, syntax of
|
|---|
| 5943 |
|
|---|
| 5944 | A function call resembles a variable reference. It looks like this:
|
|---|
| 5945 |
|
|---|
| 5946 | @example
|
|---|
| 5947 | $(@var{function} @var{arguments})
|
|---|
| 5948 | @end example
|
|---|
| 5949 |
|
|---|
| 5950 | @noindent
|
|---|
| 5951 | or like this:
|
|---|
| 5952 |
|
|---|
| 5953 | @example
|
|---|
| 5954 | $@{@var{function} @var{arguments}@}
|
|---|
| 5955 | @end example
|
|---|
| 5956 |
|
|---|
| 5957 | Here @var{function} is a function name; one of a short list of names
|
|---|
| 5958 | that are part of @code{make}. You can also essentially create your own
|
|---|
| 5959 | functions by using the @code{call} builtin function.
|
|---|
| 5960 |
|
|---|
| 5961 | The @var{arguments} are the arguments of the function. They are
|
|---|
| 5962 | separated from the function name by one or more spaces or tabs, and if
|
|---|
| 5963 | there is more than one argument, then they are separated by commas.
|
|---|
| 5964 | Such whitespace and commas are not part of an argument's value. The
|
|---|
| 5965 | delimiters which you use to surround the function call, whether
|
|---|
| 5966 | parentheses or braces, can appear in an argument only in matching pairs;
|
|---|
| 5967 | the other kind of delimiters may appear singly. If the arguments
|
|---|
| 5968 | themselves contain other function calls or variable references, it is
|
|---|
| 5969 | wisest to use the same kind of delimiters for all the references; write
|
|---|
| 5970 | @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
|
|---|
| 5971 | is because it is clearer, and because only one type of delimiter is
|
|---|
| 5972 | matched to find the end of the reference.
|
|---|
| 5973 |
|
|---|
| 5974 | The text written for each argument is processed by substitution of
|
|---|
| 5975 | variables and function calls to produce the argument value, which
|
|---|
| 5976 | is the text on which the function acts. The substitution is done in the
|
|---|
| 5977 | order in which the arguments appear.
|
|---|
| 5978 |
|
|---|
| 5979 | Commas and unmatched parentheses or braces cannot appear in the text of an
|
|---|
| 5980 | argument as written; leading spaces cannot appear in the text of the first
|
|---|
| 5981 | argument as written. These characters can be put into the argument value
|
|---|
| 5982 | by variable substitution. First define variables @code{comma} and
|
|---|
| 5983 | @code{space} whose values are isolated comma and space characters, then
|
|---|
| 5984 | substitute these variables where such characters are wanted, like this:
|
|---|
| 5985 |
|
|---|
| 5986 | @example
|
|---|
| 5987 | @group
|
|---|
| 5988 | comma:= ,
|
|---|
| 5989 | empty:=
|
|---|
| 5990 | space:= $(empty) $(empty)
|
|---|
| 5991 | foo:= a b c
|
|---|
| 5992 | bar:= $(subst $(space),$(comma),$(foo))
|
|---|
| 5993 | # @r{bar is now `a,b,c'.}
|
|---|
| 5994 | @end group
|
|---|
| 5995 | @end example
|
|---|
| 5996 |
|
|---|
| 5997 | @noindent
|
|---|
| 5998 | Here the @code{subst} function replaces each space with a comma, through
|
|---|
| 5999 | the value of @code{foo}, and substitutes the result.
|
|---|
| 6000 |
|
|---|
| 6001 | @node Text Functions, File Name Functions, Syntax of Functions, Functions
|
|---|
| 6002 | @section Functions for String Substitution and Analysis
|
|---|
| 6003 | @cindex functions, for text
|
|---|
| 6004 |
|
|---|
| 6005 | Here are some functions that operate on strings:
|
|---|
| 6006 |
|
|---|
| 6007 | @table @code
|
|---|
| 6008 | @item $(subst @var{from},@var{to},@var{text})
|
|---|
| 6009 | @findex subst
|
|---|
| 6010 | Performs a textual replacement on the text @var{text}: each occurrence
|
|---|
| 6011 | of @var{from} is replaced by @var{to}. The result is substituted for
|
|---|
| 6012 | the function call. For example,
|
|---|
| 6013 |
|
|---|
| 6014 | @example
|
|---|
| 6015 | $(subst ee,EE,feet on the street)
|
|---|
| 6016 | @end example
|
|---|
| 6017 |
|
|---|
| 6018 | substitutes the string @samp{fEEt on the strEEt}.
|
|---|
| 6019 |
|
|---|
| 6020 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
|---|
| 6021 | @findex patsubst
|
|---|
| 6022 | Finds whitespace-separated words in @var{text} that match
|
|---|
| 6023 | @var{pattern} and replaces them with @var{replacement}. Here
|
|---|
| 6024 | @var{pattern} may contain a @samp{%} which acts as a wildcard,
|
|---|
| 6025 | matching any number of any characters within a word. If
|
|---|
| 6026 | @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
|
|---|
| 6027 | by the text that matched the @samp{%} in @var{pattern}. Only the first
|
|---|
| 6028 | @samp{%} in the @var{pattern} and @var{replacement} is treated this
|
|---|
| 6029 | way; any subsequent @samp{%} is unchanged.@refill
|
|---|
| 6030 |
|
|---|
| 6031 | @cindex @code{%}, quoting in @code{patsubst}
|
|---|
| 6032 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 6033 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 6034 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 6035 | @cindex quoting @code{%}, in @code{patsubst}
|
|---|
| 6036 | @samp{%} characters in @code{patsubst} function invocations can be
|
|---|
| 6037 | quoted with preceding backslashes (@samp{\}). Backslashes that would
|
|---|
| 6038 | otherwise quote @samp{%} characters can be quoted with more backslashes.
|
|---|
| 6039 | Backslashes that quote @samp{%} characters or other backslashes are
|
|---|
| 6040 | removed from the pattern before it is compared file names or has a stem
|
|---|
| 6041 | substituted into it. Backslashes that are not in danger of quoting
|
|---|
| 6042 | @samp{%} characters go unmolested. For example, the pattern
|
|---|
| 6043 | @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
|
|---|
| 6044 | operative @samp{%} character, and @samp{pattern\\} following it. The
|
|---|
| 6045 | final two backslashes are left alone because they cannot affect any
|
|---|
| 6046 | @samp{%} character.@refill
|
|---|
| 6047 |
|
|---|
| 6048 | Whitespace between words is folded into single space characters;
|
|---|
| 6049 | leading and trailing whitespace is discarded.
|
|---|
| 6050 |
|
|---|
| 6051 | For example,
|
|---|
| 6052 |
|
|---|
| 6053 | @example
|
|---|
| 6054 | $(patsubst %.c,%.o,x.c.c bar.c)
|
|---|
| 6055 | @end example
|
|---|
| 6056 |
|
|---|
| 6057 | @noindent
|
|---|
| 6058 | produces the value @samp{x.c.o bar.o}.
|
|---|
| 6059 |
|
|---|
| 6060 | Substitution references (@pxref{Substitution Refs, ,Substitution
|
|---|
| 6061 | References}) are a simpler way to get the effect of the @code{patsubst}
|
|---|
| 6062 | function:
|
|---|
| 6063 |
|
|---|
| 6064 | @example
|
|---|
| 6065 | $(@var{var}:@var{pattern}=@var{replacement})
|
|---|
| 6066 | @end example
|
|---|
| 6067 |
|
|---|
| 6068 | @noindent
|
|---|
| 6069 | is equivalent to
|
|---|
| 6070 |
|
|---|
| 6071 | @example
|
|---|
| 6072 | $(patsubst @var{pattern},@var{replacement},$(@var{var}))
|
|---|
| 6073 | @end example
|
|---|
| 6074 |
|
|---|
| 6075 | The second shorthand simplifies one of the most common uses of
|
|---|
| 6076 | @code{patsubst}: replacing the suffix at the end of file names.
|
|---|
| 6077 |
|
|---|
| 6078 | @example
|
|---|
| 6079 | $(@var{var}:@var{suffix}=@var{replacement})
|
|---|
| 6080 | @end example
|
|---|
| 6081 |
|
|---|
| 6082 | @noindent
|
|---|
| 6083 | is equivalent to
|
|---|
| 6084 |
|
|---|
| 6085 | @example
|
|---|
| 6086 | $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
|
|---|
| 6087 | @end example
|
|---|
| 6088 |
|
|---|
| 6089 | @noindent
|
|---|
| 6090 | For example, you might have a list of object files:
|
|---|
| 6091 |
|
|---|
| 6092 | @example
|
|---|
| 6093 | objects = foo.o bar.o baz.o
|
|---|
| 6094 | @end example
|
|---|
| 6095 |
|
|---|
| 6096 | @noindent
|
|---|
| 6097 | To get the list of corresponding source files, you could simply write:
|
|---|
| 6098 |
|
|---|
| 6099 | @example
|
|---|
| 6100 | $(objects:.o=.c)
|
|---|
| 6101 | @end example
|
|---|
| 6102 |
|
|---|
| 6103 | @noindent
|
|---|
| 6104 | instead of using the general form:
|
|---|
| 6105 |
|
|---|
| 6106 | @example
|
|---|
| 6107 | $(patsubst %.o,%.c,$(objects))
|
|---|
| 6108 | @end example
|
|---|
| 6109 |
|
|---|
| 6110 | @item $(strip @var{string})
|
|---|
| 6111 | @cindex stripping whitespace
|
|---|
| 6112 | @cindex whitespace, stripping
|
|---|
| 6113 | @cindex spaces, stripping
|
|---|
| 6114 | @findex strip
|
|---|
| 6115 | Removes leading and trailing whitespace from @var{string} and replaces
|
|---|
| 6116 | each internal sequence of one or more whitespace characters with a
|
|---|
| 6117 | single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
|
|---|
| 6118 |
|
|---|
| 6119 | The function @code{strip} can be very useful when used in conjunction
|
|---|
| 6120 | with conditionals. When comparing something with the empty string
|
|---|
| 6121 | @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
|
|---|
| 6122 | just whitespace to match the empty string (@pxref{Conditionals}).
|
|---|
| 6123 |
|
|---|
| 6124 | Thus, the following may fail to have the desired results:
|
|---|
| 6125 |
|
|---|
| 6126 | @example
|
|---|
| 6127 | .PHONY: all
|
|---|
| 6128 | ifneq "$(needs_made)" ""
|
|---|
| 6129 | all: $(needs_made)
|
|---|
| 6130 | else
|
|---|
| 6131 | all:;@@echo 'Nothing to make!'
|
|---|
| 6132 | endif
|
|---|
| 6133 | @end example
|
|---|
| 6134 |
|
|---|
| 6135 | @noindent
|
|---|
| 6136 | Replacing the variable reference @w{@samp{$(needs_made)}} with the
|
|---|
| 6137 | function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
|
|---|
| 6138 | directive would make it more robust.@refill
|
|---|
| 6139 |
|
|---|
| 6140 | @item $(findstring @var{find},@var{in})
|
|---|
| 6141 | @findex findstring
|
|---|
| 6142 | @cindex searching for strings
|
|---|
| 6143 | @cindex finding strings
|
|---|
| 6144 | @cindex strings, searching for
|
|---|
| 6145 | Searches @var{in} for an occurrence of @var{find}. If it occurs, the
|
|---|
| 6146 | value is @var{find}; otherwise, the value is empty. You can use this
|
|---|
| 6147 | function in a conditional to test for the presence of a specific
|
|---|
| 6148 | substring in a given string. Thus, the two examples,
|
|---|
| 6149 |
|
|---|
| 6150 | @example
|
|---|
| 6151 | $(findstring a,a b c)
|
|---|
| 6152 | $(findstring a,b c)
|
|---|
| 6153 | @end example
|
|---|
| 6154 |
|
|---|
| 6155 | @noindent
|
|---|
| 6156 | produce the values @samp{a} and @samp{} (the empty string),
|
|---|
| 6157 | respectively. @xref{Testing Flags}, for a practical application of
|
|---|
| 6158 | @code{findstring}.@refill
|
|---|
| 6159 |
|
|---|
| 6160 | @need 750
|
|---|
| 6161 | @findex filter
|
|---|
| 6162 | @cindex filtering words
|
|---|
| 6163 | @cindex words, filtering
|
|---|
| 6164 | @item $(filter @var{pattern}@dots{},@var{text})
|
|---|
| 6165 | Returns all whitespace-separated words in @var{text} that @emph{do} match
|
|---|
| 6166 | any of the @var{pattern} words, removing any words that @emph{do not}
|
|---|
| 6167 | match. The patterns are written using @samp{%}, just like the patterns
|
|---|
| 6168 | used in the @code{patsubst} function above.@refill
|
|---|
| 6169 |
|
|---|
| 6170 | The @code{filter} function can be used to separate out different types
|
|---|
| 6171 | of strings (such as file names) in a variable. For example:
|
|---|
| 6172 |
|
|---|
| 6173 | @example
|
|---|
| 6174 | sources := foo.c bar.c baz.s ugh.h
|
|---|
| 6175 | foo: $(sources)
|
|---|
| 6176 | cc $(filter %.c %.s,$(sources)) -o foo
|
|---|
| 6177 | @end example
|
|---|
| 6178 |
|
|---|
| 6179 | @noindent
|
|---|
| 6180 | says that @file{foo} depends of @file{foo.c}, @file{bar.c},
|
|---|
| 6181 | @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
|
|---|
| 6182 | @file{baz.s} should be specified in the command to the
|
|---|
| 6183 | compiler.@refill
|
|---|
| 6184 |
|
|---|
| 6185 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
|---|
| 6186 | @findex filter-out
|
|---|
| 6187 | @cindex filtering out words
|
|---|
| 6188 | @cindex words, filtering out
|
|---|
| 6189 | Returns all whitespace-separated words in @var{text} that @emph{do not}
|
|---|
| 6190 | match any of the @var{pattern} words, removing the words that @emph{do}
|
|---|
| 6191 | match one or more. This is the exact opposite of the @code{filter}
|
|---|
| 6192 | function.@refill
|
|---|
| 6193 |
|
|---|
| 6194 | For example, given:
|
|---|
| 6195 |
|
|---|
| 6196 | @example
|
|---|
| 6197 | @group
|
|---|
| 6198 | objects=main1.o foo.o main2.o bar.o
|
|---|
| 6199 | mains=main1.o main2.o
|
|---|
| 6200 | @end group
|
|---|
| 6201 | @end example
|
|---|
| 6202 |
|
|---|
| 6203 | @noindent
|
|---|
| 6204 | the following generates a list which contains all the object files not
|
|---|
| 6205 | in @samp{mains}:
|
|---|
| 6206 |
|
|---|
| 6207 | @example
|
|---|
| 6208 | $(filter-out $(mains),$(objects))
|
|---|
| 6209 | @end example
|
|---|
| 6210 |
|
|---|
| 6211 | @need 1500
|
|---|
| 6212 | @findex sort
|
|---|
| 6213 | @cindex sorting words
|
|---|
| 6214 | @item $(sort @var{list})
|
|---|
| 6215 | Sorts the words of @var{list} in lexical order, removing duplicate
|
|---|
| 6216 | words. The output is a list of words separated by single spaces.
|
|---|
| 6217 | Thus,
|
|---|
| 6218 |
|
|---|
| 6219 | @example
|
|---|
| 6220 | $(sort foo bar lose)
|
|---|
| 6221 | @end example
|
|---|
| 6222 |
|
|---|
| 6223 | @noindent
|
|---|
| 6224 | returns the value @samp{bar foo lose}.
|
|---|
| 6225 |
|
|---|
| 6226 | @cindex removing duplicate words
|
|---|
| 6227 | @cindex duplicate words, removing
|
|---|
| 6228 | @cindex words, removing duplicates
|
|---|
| 6229 | Incidentally, since @code{sort} removes duplicate words, you can use
|
|---|
| 6230 | it for this purpose even if you don't care about the sort order.
|
|---|
| 6231 |
|
|---|
| 6232 | @item $(word @var{n},@var{text})
|
|---|
| 6233 | @findex word
|
|---|
| 6234 | @cindex word, selecting a
|
|---|
| 6235 | @cindex selecting a word
|
|---|
| 6236 | Returns the @var{n}th word of @var{text}. The legitimate values of
|
|---|
| 6237 | @var{n} start from 1. If @var{n} is bigger than the number of words
|
|---|
| 6238 | in @var{text}, the value is empty. For example,
|
|---|
| 6239 |
|
|---|
| 6240 | @example
|
|---|
| 6241 | $(word 2, foo bar baz)
|
|---|
| 6242 | @end example
|
|---|
| 6243 |
|
|---|
| 6244 | @noindent
|
|---|
| 6245 | returns @samp{bar}.
|
|---|
| 6246 |
|
|---|
| 6247 | @item $(wordlist @var{s},@var{e},@var{text})
|
|---|
| 6248 | @findex wordlist
|
|---|
| 6249 | @cindex words, selecting lists of
|
|---|
| 6250 | @cindex selecting word lists
|
|---|
| 6251 | Returns the list of words in @var{text} starting with word @var{s} and
|
|---|
| 6252 | ending with word @var{e} (inclusive). The legitimate values of @var{s}
|
|---|
| 6253 | start from 1; @var{e} may start from 0. If @var{s} is bigger than the
|
|---|
| 6254 | number of words in @var{text}, the value is empty. If @var{e} is
|
|---|
| 6255 | bigger than the number of words in @var{text}, words up to the end of
|
|---|
| 6256 | @var{text} are returned. If @var{s} is greater than @var{e}, nothing
|
|---|
| 6257 | is returned. For example,
|
|---|
| 6258 |
|
|---|
| 6259 | @example
|
|---|
| 6260 | $(wordlist 2, 3, foo bar baz)
|
|---|
| 6261 | @end example
|
|---|
| 6262 |
|
|---|
| 6263 | @noindent
|
|---|
| 6264 | returns @samp{bar baz}.
|
|---|
| 6265 |
|
|---|
| 6266 | @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
|
|---|
| 6267 | @item $(words @var{text})
|
|---|
| 6268 | @findex words
|
|---|
| 6269 | @cindex words, finding number
|
|---|
| 6270 | Returns the number of words in @var{text}.
|
|---|
| 6271 | Thus, the last word of @var{text} is
|
|---|
| 6272 | @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
|
|---|
| 6273 |
|
|---|
| 6274 | @item $(firstword @var{names}@dots{})
|
|---|
| 6275 | @findex firstword
|
|---|
| 6276 | @cindex words, extracting first
|
|---|
| 6277 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6278 | by whitespace. The value is the first name in the series. The rest
|
|---|
| 6279 | of the names are ignored.
|
|---|
| 6280 |
|
|---|
| 6281 | For example,
|
|---|
| 6282 |
|
|---|
| 6283 | @example
|
|---|
| 6284 | $(firstword foo bar)
|
|---|
| 6285 | @end example
|
|---|
| 6286 |
|
|---|
| 6287 | @noindent
|
|---|
| 6288 | produces the result @samp{foo}. Although @code{$(firstword
|
|---|
| 6289 | @var{text})} is the same as @code{$(word 1,@var{text})}, the
|
|---|
| 6290 | @code{firstword} function is retained for its simplicity.@refill
|
|---|
| 6291 |
|
|---|
| 6292 |
|
|---|
| 6293 | @item $(lastword @var{names}@dots{})
|
|---|
| 6294 | @findex lastword
|
|---|
| 6295 | @cindex words, extracting last
|
|---|
| 6296 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6297 | by whitespace. The value is the last name in the series.
|
|---|
| 6298 |
|
|---|
| 6299 | For example,
|
|---|
| 6300 |
|
|---|
| 6301 | @example
|
|---|
| 6302 | $(lastword foo bar)
|
|---|
| 6303 | @end example
|
|---|
| 6304 |
|
|---|
| 6305 | @noindent
|
|---|
| 6306 | produces the result @samp{bar}. Although @code{$(lastword
|
|---|
| 6307 | @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
|
|---|
| 6308 | the @code{lastword} function was added for its simplicity and better
|
|---|
| 6309 | performance.@refill
|
|---|
| 6310 | @end table
|
|---|
| 6311 |
|
|---|
| 6312 |
|
|---|
| 6313 | Here is a realistic example of the use of @code{subst} and
|
|---|
| 6314 | @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
|
|---|
| 6315 | to specify a list of directories that @code{make} should search for
|
|---|
| 6316 | prerequisite files
|
|---|
| 6317 | (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
|
|---|
| 6318 | This example shows how to
|
|---|
| 6319 | tell the C compiler to search for header files in the same list of
|
|---|
| 6320 | directories.@refill
|
|---|
| 6321 |
|
|---|
| 6322 | The value of @code{VPATH} is a list of directories separated by colons,
|
|---|
| 6323 | such as @samp{src:../headers}. First, the @code{subst} function is used to
|
|---|
| 6324 | change the colons to spaces:
|
|---|
| 6325 |
|
|---|
| 6326 | @example
|
|---|
| 6327 | $(subst :, ,$(VPATH))
|
|---|
| 6328 | @end example
|
|---|
| 6329 |
|
|---|
| 6330 | @noindent
|
|---|
| 6331 | This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
|
|---|
| 6332 | each directory name into a @samp{-I} flag. These can be added to the
|
|---|
| 6333 | value of the variable @code{CFLAGS}, which is passed automatically to the C
|
|---|
| 6334 | compiler, like this:
|
|---|
| 6335 |
|
|---|
| 6336 | @example
|
|---|
| 6337 | override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
|
|---|
| 6338 | @end example
|
|---|
| 6339 |
|
|---|
| 6340 | @noindent
|
|---|
| 6341 | The effect is to append the text @samp{-Isrc -I../headers} to the
|
|---|
| 6342 | previously given value of @code{CFLAGS}. The @code{override} directive is
|
|---|
| 6343 | used so that the new value is assigned even if the previous value of
|
|---|
| 6344 | @code{CFLAGS} was specified with a command argument (@pxref{Override
|
|---|
| 6345 | Directive, , The @code{override} Directive}).
|
|---|
| 6346 |
|
|---|
| 6347 | @node File Name Functions, Foreach Function, Text Functions, Functions
|
|---|
| 6348 | @section Functions for File Names
|
|---|
| 6349 | @cindex functions, for file names
|
|---|
| 6350 | @cindex file name functions
|
|---|
| 6351 |
|
|---|
| 6352 | Several of the built-in expansion functions relate specifically to
|
|---|
| 6353 | taking apart file names or lists of file names.
|
|---|
| 6354 |
|
|---|
| 6355 | Each of the following functions performs a specific transformation on a
|
|---|
| 6356 | file name. The argument of the function is regarded as a series of file
|
|---|
| 6357 | names, separated by whitespace. (Leading and trailing whitespace is
|
|---|
| 6358 | ignored.) Each file name in the series is transformed in the same way and
|
|---|
| 6359 | the results are concatenated with single spaces between them.
|
|---|
| 6360 |
|
|---|
| 6361 | @table @code
|
|---|
| 6362 | @item $(dir @var{names}@dots{})
|
|---|
| 6363 | @findex dir
|
|---|
| 6364 | @cindex directory part
|
|---|
| 6365 | @cindex file name, directory part
|
|---|
| 6366 | Extracts the directory-part of each file name in @var{names}. The
|
|---|
| 6367 | directory-part of the file name is everything up through (and
|
|---|
| 6368 | including) the last slash in it. If the file name contains no slash,
|
|---|
| 6369 | the directory part is the string @samp{./}. For example,
|
|---|
| 6370 |
|
|---|
| 6371 | @example
|
|---|
| 6372 | $(dir src/foo.c hacks)
|
|---|
| 6373 | @end example
|
|---|
| 6374 |
|
|---|
| 6375 | @noindent
|
|---|
| 6376 | produces the result @samp{src/ ./}.
|
|---|
| 6377 |
|
|---|
| 6378 | @item $(notdir @var{names}@dots{})
|
|---|
| 6379 | @findex notdir
|
|---|
| 6380 | @cindex file name, nondirectory part
|
|---|
| 6381 | @cindex nondirectory part
|
|---|
| 6382 | Extracts all but the directory-part of each file name in @var{names}.
|
|---|
| 6383 | If the file name contains no slash, it is left unchanged. Otherwise,
|
|---|
| 6384 | everything through the last slash is removed from it.
|
|---|
| 6385 |
|
|---|
| 6386 | A file name that ends with a slash becomes an empty string. This is
|
|---|
| 6387 | unfortunate, because it means that the result does not always have the
|
|---|
| 6388 | same number of whitespace-separated file names as the argument had;
|
|---|
| 6389 | but we do not see any other valid alternative.
|
|---|
| 6390 |
|
|---|
| 6391 | For example,
|
|---|
| 6392 |
|
|---|
| 6393 | @example
|
|---|
| 6394 | $(notdir src/foo.c hacks)
|
|---|
| 6395 | @end example
|
|---|
| 6396 |
|
|---|
| 6397 | @noindent
|
|---|
| 6398 | produces the result @samp{foo.c hacks}.
|
|---|
| 6399 |
|
|---|
| 6400 | @item $(suffix @var{names}@dots{})
|
|---|
| 6401 | @findex suffix
|
|---|
| 6402 | @cindex suffix, function to find
|
|---|
| 6403 | @cindex file name suffix
|
|---|
| 6404 | Extracts the suffix of each file name in @var{names}. If the file name
|
|---|
| 6405 | contains a period, the suffix is everything starting with the last
|
|---|
| 6406 | period. Otherwise, the suffix is the empty string. This frequently
|
|---|
| 6407 | means that the result will be empty when @var{names} is not, and if
|
|---|
| 6408 | @var{names} contains multiple file names, the result may contain fewer
|
|---|
| 6409 | file names.
|
|---|
| 6410 |
|
|---|
| 6411 | For example,
|
|---|
| 6412 |
|
|---|
| 6413 | @example
|
|---|
| 6414 | $(suffix src/foo.c src-1.0/bar.c hacks)
|
|---|
| 6415 | @end example
|
|---|
| 6416 |
|
|---|
| 6417 | @noindent
|
|---|
| 6418 | produces the result @samp{.c .c}.
|
|---|
| 6419 |
|
|---|
| 6420 | @item $(basename @var{names}@dots{})
|
|---|
| 6421 | @findex basename
|
|---|
| 6422 | @cindex basename
|
|---|
| 6423 | @cindex file name, basename of
|
|---|
| 6424 | Extracts all but the suffix of each file name in @var{names}. If the
|
|---|
| 6425 | file name contains a period, the basename is everything starting up to
|
|---|
| 6426 | (and not including) the last period. Periods in the directory part are
|
|---|
| 6427 | ignored. If there is no period, the basename is the entire file name.
|
|---|
| 6428 | For example,
|
|---|
| 6429 |
|
|---|
| 6430 | @example
|
|---|
| 6431 | $(basename src/foo.c src-1.0/bar hacks)
|
|---|
| 6432 | @end example
|
|---|
| 6433 |
|
|---|
| 6434 | @noindent
|
|---|
| 6435 | produces the result @samp{src/foo src-1.0/bar hacks}.
|
|---|
| 6436 |
|
|---|
| 6437 | @c plural convention with dots (be consistent)
|
|---|
| 6438 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
|---|
| 6439 | @findex addsuffix
|
|---|
| 6440 | @cindex suffix, adding
|
|---|
| 6441 | @cindex file name suffix, adding
|
|---|
| 6442 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6443 | by whitespace; @var{suffix} is used as a unit. The value of
|
|---|
| 6444 | @var{suffix} is appended to the end of each individual name and the
|
|---|
| 6445 | resulting larger names are concatenated with single spaces between
|
|---|
| 6446 | them. For example,
|
|---|
| 6447 |
|
|---|
| 6448 | @example
|
|---|
| 6449 | $(addsuffix .c,foo bar)
|
|---|
| 6450 | @end example
|
|---|
| 6451 |
|
|---|
| 6452 | @noindent
|
|---|
| 6453 | produces the result @samp{foo.c bar.c}.
|
|---|
| 6454 |
|
|---|
| 6455 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
|---|
| 6456 | @findex addprefix
|
|---|
| 6457 | @cindex prefix, adding
|
|---|
| 6458 | @cindex file name prefix, adding
|
|---|
| 6459 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6460 | by whitespace; @var{prefix} is used as a unit. The value of
|
|---|
| 6461 | @var{prefix} is prepended to the front of each individual name and the
|
|---|
| 6462 | resulting larger names are concatenated with single spaces between
|
|---|
| 6463 | them. For example,
|
|---|
| 6464 |
|
|---|
| 6465 | @example
|
|---|
| 6466 | $(addprefix src/,foo bar)
|
|---|
| 6467 | @end example
|
|---|
| 6468 |
|
|---|
| 6469 | @noindent
|
|---|
| 6470 | produces the result @samp{src/foo src/bar}.
|
|---|
| 6471 |
|
|---|
| 6472 | @item $(join @var{list1},@var{list2})
|
|---|
| 6473 | @findex join
|
|---|
| 6474 | @cindex joining lists of words
|
|---|
| 6475 | @cindex words, joining lists
|
|---|
| 6476 | Concatenates the two arguments word by word: the two first words (one
|
|---|
| 6477 | from each argument) concatenated form the first word of the result, the
|
|---|
| 6478 | two second words form the second word of the result, and so on. So the
|
|---|
| 6479 | @var{n}th word of the result comes from the @var{n}th word of each
|
|---|
| 6480 | argument. If one argument has more words that the other, the extra
|
|---|
| 6481 | words are copied unchanged into the result.
|
|---|
| 6482 |
|
|---|
| 6483 | For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
|
|---|
| 6484 |
|
|---|
| 6485 | Whitespace between the words in the lists is not preserved; it is
|
|---|
| 6486 | replaced with a single space.
|
|---|
| 6487 |
|
|---|
| 6488 | This function can merge the results of the @code{dir} and
|
|---|
| 6489 | @code{notdir} functions, to produce the original list of files which
|
|---|
| 6490 | was given to those two functions.@refill
|
|---|
| 6491 |
|
|---|
| 6492 | @item $(wildcard @var{pattern})
|
|---|
| 6493 | @findex wildcard
|
|---|
| 6494 | @cindex wildcard, function
|
|---|
| 6495 | The argument @var{pattern} is a file name pattern, typically containing
|
|---|
| 6496 | wildcard characters (as in shell file name patterns). The result of
|
|---|
| 6497 | @code{wildcard} is a space-separated list of the names of existing files
|
|---|
| 6498 | that match the pattern.
|
|---|
| 6499 | @xref{Wildcards, ,Using Wildcard Characters in File Names}.
|
|---|
| 6500 |
|
|---|
| 6501 | @item $(realpath @var{names}@dots{})
|
|---|
| 6502 | @findex realpath
|
|---|
| 6503 | @cindex realpath
|
|---|
| 6504 | @cindex file name, realpath of
|
|---|
| 6505 | For each file name in @var{names} return the canonical absolute name.
|
|---|
| 6506 | A canonical name does not contain any @code{.} or @code{..} components,
|
|---|
| 6507 | nor any repeated path separators (@code{/}) or symlinks. In case of a
|
|---|
| 6508 | failure the empty string is returned. Consult the @code{realpath(3)}
|
|---|
| 6509 | documentation for a list of possible failure causes.
|
|---|
| 6510 |
|
|---|
| 6511 | @item $(abspath @var{names}@dots{})
|
|---|
| 6512 | @findex abspath
|
|---|
| 6513 | @cindex abspath
|
|---|
| 6514 | @cindex file name, abspath of
|
|---|
| 6515 | For each file name in @var{names} return an absolute name that does
|
|---|
| 6516 | not contain any @code{.} or @code{..} components, nor any repeated path
|
|---|
| 6517 | separators (@code{/}). Note that in contrast to @code{realpath}
|
|---|
| 6518 | function, @code{abspath} does not resolve symlinks and does not require
|
|---|
| 6519 | the file names to refer to an existing file or directory. Use the
|
|---|
| 6520 | @code{wildcard} function to test for existence.
|
|---|
| 6521 | @end table
|
|---|
| 6522 |
|
|---|
| 6523 | @node Foreach Function, If Function, File Name Functions, Functions
|
|---|
| 6524 | @section The @code{foreach} Function
|
|---|
| 6525 | @findex foreach
|
|---|
| 6526 | @cindex words, iterating over
|
|---|
| 6527 |
|
|---|
| 6528 | The @code{foreach} function is very different from other functions. It
|
|---|
| 6529 | causes one piece of text to be used repeatedly, each time with a different
|
|---|
| 6530 | substitution performed on it. It resembles the @code{for} command in the
|
|---|
| 6531 | shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
|
|---|
| 6532 |
|
|---|
| 6533 | The syntax of the @code{foreach} function is:
|
|---|
| 6534 |
|
|---|
| 6535 | @example
|
|---|
| 6536 | $(foreach @var{var},@var{list},@var{text})
|
|---|
| 6537 | @end example
|
|---|
| 6538 |
|
|---|
| 6539 | @noindent
|
|---|
| 6540 | The first two arguments, @var{var} and @var{list}, are expanded before
|
|---|
| 6541 | anything else is done; note that the last argument, @var{text}, is
|
|---|
| 6542 | @strong{not} expanded at the same time. Then for each word of the expanded
|
|---|
| 6543 | value of @var{list}, the variable named by the expanded value of @var{var}
|
|---|
| 6544 | is set to that word, and @var{text} is expanded. Presumably @var{text}
|
|---|
| 6545 | contains references to that variable, so its expansion will be different
|
|---|
| 6546 | each time.
|
|---|
| 6547 |
|
|---|
| 6548 | The result is that @var{text} is expanded as many times as there are
|
|---|
| 6549 | whitespace-separated words in @var{list}. The multiple expansions of
|
|---|
| 6550 | @var{text} are concatenated, with spaces between them, to make the result
|
|---|
| 6551 | of @code{foreach}.
|
|---|
| 6552 |
|
|---|
| 6553 | This simple example sets the variable @samp{files} to the list of all files
|
|---|
| 6554 | in the directories in the list @samp{dirs}:
|
|---|
| 6555 |
|
|---|
| 6556 | @example
|
|---|
| 6557 | dirs := a b c d
|
|---|
| 6558 | files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
|
|---|
| 6559 | @end example
|
|---|
| 6560 |
|
|---|
| 6561 | Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
|
|---|
| 6562 | finds the value @samp{a} for @code{dir}, so it produces the same result
|
|---|
| 6563 | as @samp{$(wildcard a/*)}; the second repetition produces the result
|
|---|
| 6564 | of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
|
|---|
| 6565 |
|
|---|
| 6566 | This example has the same result (except for setting @samp{dirs}) as
|
|---|
| 6567 | the following example:
|
|---|
| 6568 |
|
|---|
| 6569 | @example
|
|---|
| 6570 | files := $(wildcard a/* b/* c/* d/*)
|
|---|
| 6571 | @end example
|
|---|
| 6572 |
|
|---|
| 6573 | When @var{text} is complicated, you can improve readability by giving it
|
|---|
| 6574 | a name, with an additional variable:
|
|---|
| 6575 |
|
|---|
| 6576 | @example
|
|---|
| 6577 | find_files = $(wildcard $(dir)/*)
|
|---|
| 6578 | dirs := a b c d
|
|---|
| 6579 | files := $(foreach dir,$(dirs),$(find_files))
|
|---|
| 6580 | @end example
|
|---|
| 6581 |
|
|---|
| 6582 | @noindent
|
|---|
| 6583 | Here we use the variable @code{find_files} this way. We use plain @samp{=}
|
|---|
| 6584 | to define a recursively-expanding variable, so that its value contains an
|
|---|
| 6585 | actual function call to be reexpanded under the control of @code{foreach};
|
|---|
| 6586 | a simply-expanded variable would not do, since @code{wildcard} would be
|
|---|
| 6587 | called only once at the time of defining @code{find_files}.
|
|---|
| 6588 |
|
|---|
| 6589 | The @code{foreach} function has no permanent effect on the variable
|
|---|
| 6590 | @var{var}; its value and flavor after the @code{foreach} function call are
|
|---|
| 6591 | the same as they were beforehand. The other values which are taken from
|
|---|
| 6592 | @var{list} are in effect only temporarily, during the execution of
|
|---|
| 6593 | @code{foreach}. The variable @var{var} is a simply-expanded variable
|
|---|
| 6594 | during the execution of @code{foreach}. If @var{var} was undefined
|
|---|
| 6595 | before the @code{foreach} function call, it is undefined after the call.
|
|---|
| 6596 | @xref{Flavors, ,The Two Flavors of Variables}.@refill
|
|---|
| 6597 |
|
|---|
| 6598 | You must take care when using complex variable expressions that result in
|
|---|
| 6599 | variable names because many strange things are valid variable names, but
|
|---|
| 6600 | are probably not what you intended. For example,
|
|---|
| 6601 |
|
|---|
| 6602 | @smallexample
|
|---|
| 6603 | files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
|
|---|
| 6604 | @end smallexample
|
|---|
| 6605 |
|
|---|
| 6606 | @noindent
|
|---|
| 6607 | might be useful if the value of @code{find_files} references the variable
|
|---|
| 6608 | whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
|
|---|
| 6609 | no?), but it is more likely to be a mistake.
|
|---|
| 6610 |
|
|---|
| 6611 | @node If Function, Call Function, Foreach Function, Functions
|
|---|
| 6612 | @section The @code{if} Function
|
|---|
| 6613 | @findex if
|
|---|
| 6614 | @cindex conditional expansion
|
|---|
| 6615 |
|
|---|
| 6616 | The @code{if} function provides support for conditional expansion in a
|
|---|
| 6617 | functional context (as opposed to the GNU @code{make} makefile
|
|---|
| 6618 | conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
|
|---|
| 6619 | Conditionals}).
|
|---|
| 6620 |
|
|---|
| 6621 | An @code{if} function call can contain either two or three arguments:
|
|---|
| 6622 |
|
|---|
| 6623 | @example
|
|---|
| 6624 | $(if @var{condition},@var{then-part}[,@var{else-part}])
|
|---|
| 6625 | @end example
|
|---|
| 6626 |
|
|---|
| 6627 | The first argument, @var{condition}, first has all preceding and
|
|---|
| 6628 | trailing whitespace stripped, then is expanded. If it expands to any
|
|---|
| 6629 | non-empty string, then the condition is considered to be true. If it
|
|---|
| 6630 | expands to an empty string, the condition is considered to be false.
|
|---|
| 6631 |
|
|---|
| 6632 | If the condition is true then the second argument, @var{then-part}, is
|
|---|
| 6633 | evaluated and this is used as the result of the evaluation of the entire
|
|---|
| 6634 | @code{if} function.
|
|---|
| 6635 |
|
|---|
| 6636 | If the condition is false then the third argument, @var{else-part}, is
|
|---|
| 6637 | evaluated and this is the result of the @code{if} function. If there is
|
|---|
| 6638 | no third argument, the @code{if} function evaluates to nothing (the
|
|---|
| 6639 | empty string).
|
|---|
| 6640 |
|
|---|
| 6641 | Note that only one of the @var{then-part} or the @var{else-part} will be
|
|---|
| 6642 | evaluated, never both. Thus, either can contain side-effects (such as
|
|---|
| 6643 | @code{shell} function calls, etc.)
|
|---|
| 6644 |
|
|---|
| 6645 | @node Call Function, Value Function, If Function, Functions
|
|---|
| 6646 | @section The @code{call} Function
|
|---|
| 6647 | @findex call
|
|---|
| 6648 | @cindex functions, user defined
|
|---|
| 6649 | @cindex user defined functions
|
|---|
| 6650 |
|
|---|
| 6651 | The @code{call} function is unique in that it can be used to create new
|
|---|
| 6652 | parameterized functions. You can write a complex expression as the
|
|---|
| 6653 | value of a variable, then use @code{call} to expand it with different
|
|---|
| 6654 | values.
|
|---|
| 6655 |
|
|---|
| 6656 | The syntax of the @code{call} function is:
|
|---|
| 6657 |
|
|---|
| 6658 | @example
|
|---|
| 6659 | $(call @var{variable},@var{param},@var{param},@dots{})
|
|---|
| 6660 | @end example
|
|---|
| 6661 |
|
|---|
| 6662 | When @code{make} expands this function, it assigns each @var{param} to
|
|---|
| 6663 | temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
|
|---|
| 6664 | @code{$(0)} will contain @var{variable}. There is no maximum number of
|
|---|
| 6665 | parameter arguments. There is no minimum, either, but it doesn't make
|
|---|
| 6666 | sense to use @code{call} with no parameters.
|
|---|
| 6667 |
|
|---|
| 6668 | Then @var{variable} is expanded as a @code{make} variable in the context
|
|---|
| 6669 | of these temporary assignments. Thus, any reference to @code{$(1)} in
|
|---|
| 6670 | the value of @var{variable} will resolve to the first @var{param} in the
|
|---|
| 6671 | invocation of @code{call}.
|
|---|
| 6672 |
|
|---|
| 6673 | Note that @var{variable} is the @emph{name} of a variable, not a
|
|---|
| 6674 | @emph{reference} to that variable. Therefore you would not normally use
|
|---|
| 6675 | a @samp{$} or parentheses when writing it. (You can, however, use a
|
|---|
| 6676 | variable reference in the name if you want the name not to be a
|
|---|
| 6677 | constant.)
|
|---|
| 6678 |
|
|---|
| 6679 | If @var{variable} is the name of a builtin function, the builtin function
|
|---|
| 6680 | is always invoked (even if a @code{make} variable by that name also
|
|---|
| 6681 | exists).
|
|---|
| 6682 |
|
|---|
| 6683 | The @code{call} function expands the @var{param} arguments before
|
|---|
| 6684 | assigning them to temporary variables. This means that @var{variable}
|
|---|
| 6685 | values containing references to builtin functions that have special
|
|---|
| 6686 | expansion rules, like @code{foreach} or @code{if}, may not work as you
|
|---|
| 6687 | expect.
|
|---|
| 6688 |
|
|---|
| 6689 | Some examples may make this clearer.
|
|---|
| 6690 |
|
|---|
| 6691 | This macro simply reverses its arguments:
|
|---|
| 6692 |
|
|---|
| 6693 | @smallexample
|
|---|
| 6694 | reverse = $(2) $(1)
|
|---|
| 6695 |
|
|---|
| 6696 | foo = $(call reverse,a,b)
|
|---|
| 6697 | @end smallexample
|
|---|
| 6698 |
|
|---|
| 6699 | @noindent
|
|---|
| 6700 | Here @var{foo} will contain @samp{b a}.
|
|---|
| 6701 |
|
|---|
| 6702 | This one is slightly more interesting: it defines a macro to search for
|
|---|
| 6703 | the first instance of a program in @code{PATH}:
|
|---|
| 6704 |
|
|---|
| 6705 | @smallexample
|
|---|
| 6706 | pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
|
|---|
| 6707 |
|
|---|
| 6708 | LS := $(call pathsearch,ls)
|
|---|
| 6709 | @end smallexample
|
|---|
| 6710 |
|
|---|
| 6711 | @noindent
|
|---|
| 6712 | Now the variable LS contains @code{/bin/ls} or similar.
|
|---|
| 6713 |
|
|---|
| 6714 | The @code{call} function can be nested. Each recursive invocation gets
|
|---|
| 6715 | its own local values for @code{$(1)}, etc. that mask the values of
|
|---|
| 6716 | higher-level @code{call}. For example, here is an implementation of a
|
|---|
| 6717 | @dfn{map} function:
|
|---|
| 6718 |
|
|---|
| 6719 | @smallexample
|
|---|
| 6720 | map = $(foreach a,$(2),$(call $(1),$(a)))
|
|---|
| 6721 | @end smallexample
|
|---|
| 6722 |
|
|---|
| 6723 | Now you can @var{map} a function that normally takes only one argument,
|
|---|
| 6724 | such as @code{origin}, to multiple values in one step:
|
|---|
| 6725 |
|
|---|
| 6726 | @smallexample
|
|---|
| 6727 | o = $(call map,origin,o map MAKE)
|
|---|
| 6728 | @end smallexample
|
|---|
| 6729 |
|
|---|
| 6730 | and end up with @var{o} containing something like @samp{file file default}.
|
|---|
| 6731 |
|
|---|
| 6732 | A final caution: be careful when adding whitespace to the arguments to
|
|---|
| 6733 | @code{call}. As with other functions, any whitespace contained in the
|
|---|
| 6734 | second and subsequent arguments is kept; this can cause strange
|
|---|
| 6735 | effects. It's generally safest to remove all extraneous whitespace when
|
|---|
| 6736 | providing parameters to @code{call}.
|
|---|
| 6737 |
|
|---|
| 6738 | @node Value Function, Eval Function, Call Function, Functions
|
|---|
| 6739 | @comment node-name, next, previous, up
|
|---|
| 6740 | @section The @code{value} Function
|
|---|
| 6741 | @findex value
|
|---|
| 6742 | @cindex variables, unexpanded value
|
|---|
| 6743 |
|
|---|
| 6744 | The @code{value} function provides a way for you to use the value of a
|
|---|
| 6745 | variable @emph{without} having it expanded. Please note that this
|
|---|
| 6746 | does not undo expansions which have already occurred; for example if
|
|---|
| 6747 | you create a simply expanded variable its value is expanded during the
|
|---|
| 6748 | definition; in that case the @code{value} function will return the
|
|---|
| 6749 | same result as using the variable directly.
|
|---|
| 6750 |
|
|---|
| 6751 | The syntax of the @code{value} function is:
|
|---|
| 6752 |
|
|---|
| 6753 | @example
|
|---|
| 6754 | $(value @var{variable})
|
|---|
| 6755 | @end example
|
|---|
| 6756 |
|
|---|
| 6757 | Note that @var{variable} is the @emph{name} of a variable; not a
|
|---|
| 6758 | @emph{reference} to that variable. Therefore you would not normally
|
|---|
| 6759 | use a @samp{$} or parentheses when writing it. (You can, however, use
|
|---|
| 6760 | a variable reference in the name if you want the name not to be a
|
|---|
| 6761 | constant.)
|
|---|
| 6762 |
|
|---|
| 6763 | The result of this function is a string containing the value of
|
|---|
| 6764 | @var{variable}, without any expansion occurring. For example, in this
|
|---|
| 6765 | makefile:
|
|---|
| 6766 |
|
|---|
| 6767 | @example
|
|---|
| 6768 | @group
|
|---|
| 6769 | FOO = $PATH
|
|---|
| 6770 |
|
|---|
| 6771 | all:
|
|---|
| 6772 | @@echo $(FOO)
|
|---|
| 6773 | @@echo $(value FOO)
|
|---|
| 6774 | @end group
|
|---|
| 6775 | @end example
|
|---|
| 6776 |
|
|---|
| 6777 | @noindent
|
|---|
| 6778 | The first output line would be @code{ATH}, since the ``$P'' would be
|
|---|
| 6779 | expanded as a @code{make} variable, while the second output line would
|
|---|
| 6780 | be the current value of your @code{$PATH} environment variable, since
|
|---|
| 6781 | the @code{value} function avoided the expansion.
|
|---|
| 6782 |
|
|---|
| 6783 | The @code{value} function is most often used in conjunction with the
|
|---|
| 6784 | @code{eval} function (@pxref{Eval Function}).
|
|---|
| 6785 |
|
|---|
| 6786 | @node Eval Function, Origin Function, Value Function, Functions
|
|---|
| 6787 | @comment node-name, next, previous, up
|
|---|
| 6788 | @section The @code{eval} Function
|
|---|
| 6789 | @findex eval
|
|---|
| 6790 | @cindex evaluating makefile syntax
|
|---|
| 6791 | @cindex makefile syntax, evaluating
|
|---|
| 6792 |
|
|---|
| 6793 | The @code{eval} function is very special: it allows you to define new
|
|---|
| 6794 | makefile constructs that are not constant; which are the result of
|
|---|
| 6795 | evaluating other variables and functions. The argument to the
|
|---|
| 6796 | @code{eval} function is expanded, then the results of that expansion
|
|---|
| 6797 | are parsed as makefile syntax. The expanded results can define new
|
|---|
| 6798 | @code{make} variables, targets, implicit or explicit rules, etc.
|
|---|
| 6799 |
|
|---|
| 6800 | The result of the @code{eval} function is always the empty string;
|
|---|
| 6801 | thus, it can be placed virtually anywhere in a makefile without
|
|---|
| 6802 | causing syntax errors.
|
|---|
| 6803 |
|
|---|
| 6804 | It's important to realize that the @code{eval} argument is expanded
|
|---|
| 6805 | @emph{twice}; first by the @code{eval} function, then the results of
|
|---|
| 6806 | that expansion are expanded again when they are parsed as makefile
|
|---|
| 6807 | syntax. This means you may need to provide extra levels of escaping
|
|---|
| 6808 | for ``$'' characters when using @code{eval}. The @code{value}
|
|---|
| 6809 | function (@pxref{Value Function}) can sometimes be useful in these
|
|---|
| 6810 | situations, to circumvent unwanted expansions.
|
|---|
| 6811 |
|
|---|
| 6812 | Here is an example of how @code{eval} can be used; this example
|
|---|
| 6813 | combines a number of concepts and other functions. Although it might
|
|---|
| 6814 | seem overly complex to use @code{eval} in this example, rather than
|
|---|
| 6815 | just writing out the rules, consider two things: first, the template
|
|---|
| 6816 | definition (in @code{PROGRAM_template}) could need to be much more
|
|---|
| 6817 | complex than it is here; and second, you might put the complex,
|
|---|
| 6818 | ``generic'' part of this example into another makefile, then include
|
|---|
| 6819 | it in all the individual makefiles. Now your individual makefiles are
|
|---|
| 6820 | quite straightforward.
|
|---|
| 6821 |
|
|---|
| 6822 | @example
|
|---|
| 6823 | @group
|
|---|
| 6824 | PROGRAMS = server client
|
|---|
| 6825 |
|
|---|
| 6826 | server_OBJS = server.o server_priv.o server_access.o
|
|---|
| 6827 | server_LIBS = priv protocol
|
|---|
| 6828 |
|
|---|
| 6829 | client_OBJS = client.o client_api.o client_mem.o
|
|---|
| 6830 | client_LIBS = protocol
|
|---|
| 6831 |
|
|---|
| 6832 | # Everything after this is generic
|
|---|
| 6833 |
|
|---|
| 6834 | .PHONY: all
|
|---|
| 6835 | all: $(PROGRAMS)
|
|---|
| 6836 |
|
|---|
| 6837 | define PROGRAM_template
|
|---|
| 6838 | $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
|
|---|
| 6839 | ALL_OBJS += $$($(1)_OBJS)
|
|---|
| 6840 | endef
|
|---|
| 6841 |
|
|---|
| 6842 | $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
|
|---|
| 6843 |
|
|---|
| 6844 | $(PROGRAMS):
|
|---|
| 6845 | $(LINK.o) $^ $(LDLIBS) -o $@@
|
|---|
| 6846 |
|
|---|
| 6847 | clean:
|
|---|
| 6848 | rm -f $(ALL_OBJS) $(PROGRAMS)
|
|---|
| 6849 | @end group
|
|---|
| 6850 | @end example
|
|---|
| 6851 |
|
|---|
| 6852 | @node Origin Function, Shell Function, Eval Function, Functions
|
|---|
| 6853 | @section The @code{origin} Function
|
|---|
| 6854 | @findex origin
|
|---|
| 6855 | @cindex variables, origin of
|
|---|
| 6856 | @cindex origin of variable
|
|---|
| 6857 |
|
|---|
| 6858 | The @code{origin} function is unlike most other functions in that it does
|
|---|
| 6859 | not operate on the values of variables; it tells you something @emph{about}
|
|---|
| 6860 | a variable. Specifically, it tells you where it came from.
|
|---|
| 6861 |
|
|---|
| 6862 | The syntax of the @code{origin} function is:
|
|---|
| 6863 |
|
|---|
| 6864 | @example
|
|---|
| 6865 | $(origin @var{variable})
|
|---|
| 6866 | @end example
|
|---|
| 6867 |
|
|---|
| 6868 | Note that @var{variable} is the @emph{name} of a variable to inquire about;
|
|---|
| 6869 | not a @emph{reference} to that variable. Therefore you would not normally
|
|---|
| 6870 | use a @samp{$} or parentheses when writing it. (You can, however, use a
|
|---|
| 6871 | variable reference in the name if you want the name not to be a constant.)
|
|---|
| 6872 |
|
|---|
| 6873 | The result of this function is a string telling you how the variable
|
|---|
| 6874 | @var{variable} was defined:
|
|---|
| 6875 |
|
|---|
| 6876 | @table @samp
|
|---|
| 6877 | @item undefined
|
|---|
| 6878 |
|
|---|
| 6879 | if @var{variable} was never defined.
|
|---|
| 6880 |
|
|---|
| 6881 | @item default
|
|---|
| 6882 |
|
|---|
| 6883 | if @var{variable} has a default definition, as is usual with @code{CC}
|
|---|
| 6884 | and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 6885 | Note that if you have redefined a default variable, the @code{origin}
|
|---|
| 6886 | function will return the origin of the later definition.
|
|---|
| 6887 |
|
|---|
| 6888 | @item environment
|
|---|
| 6889 |
|
|---|
| 6890 | if @var{variable} was defined as an environment variable and the
|
|---|
| 6891 | @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
|
|---|
| 6892 |
|
|---|
| 6893 | @item environment override
|
|---|
| 6894 |
|
|---|
| 6895 | if @var{variable} was defined as an environment variable and the
|
|---|
| 6896 | @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
|
|---|
| 6897 | ,Summary of Options}).@refill
|
|---|
| 6898 |
|
|---|
| 6899 | @item file
|
|---|
| 6900 |
|
|---|
| 6901 | if @var{variable} was defined in a makefile.
|
|---|
| 6902 |
|
|---|
| 6903 | @item command line
|
|---|
| 6904 |
|
|---|
| 6905 | if @var{variable} was defined on the command line.
|
|---|
| 6906 |
|
|---|
| 6907 | @item override
|
|---|
| 6908 |
|
|---|
| 6909 | if @var{variable} was defined with an @code{override} directive in a
|
|---|
| 6910 | makefile (@pxref{Override Directive, ,The @code{override} Directive}).
|
|---|
| 6911 |
|
|---|
| 6912 | @item automatic
|
|---|
| 6913 |
|
|---|
| 6914 | if @var{variable} is an automatic variable defined for the
|
|---|
| 6915 | execution of the commands for each rule
|
|---|
| 6916 | (@pxref{Automatic Variables}).
|
|---|
| 6917 | @end table
|
|---|
| 6918 |
|
|---|
| 6919 | This information is primarily useful (other than for your curiosity) to
|
|---|
| 6920 | determine if you want to believe the value of a variable. For example,
|
|---|
| 6921 | suppose you have a makefile @file{foo} that includes another makefile
|
|---|
| 6922 | @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
|
|---|
| 6923 | if you run the command @w{@samp{make -f bar}}, even if the environment contains
|
|---|
| 6924 | a definition of @code{bletch}. However, if @file{foo} defined
|
|---|
| 6925 | @code{bletch} before including @file{bar}, you do not want to override that
|
|---|
| 6926 | definition. This could be done by using an @code{override} directive in
|
|---|
| 6927 | @file{foo}, giving that definition precedence over the later definition in
|
|---|
| 6928 | @file{bar}; unfortunately, the @code{override} directive would also
|
|---|
| 6929 | override any command line definitions. So, @file{bar} could
|
|---|
| 6930 | include:@refill
|
|---|
| 6931 |
|
|---|
| 6932 | @example
|
|---|
| 6933 | @group
|
|---|
| 6934 | ifdef bletch
|
|---|
| 6935 | ifeq "$(origin bletch)" "environment"
|
|---|
| 6936 | bletch = barf, gag, etc.
|
|---|
| 6937 | endif
|
|---|
| 6938 | endif
|
|---|
| 6939 | @end group
|
|---|
| 6940 | @end example
|
|---|
| 6941 |
|
|---|
| 6942 | @noindent
|
|---|
| 6943 | If @code{bletch} has been defined from the environment, this will redefine
|
|---|
| 6944 | it.
|
|---|
| 6945 |
|
|---|
| 6946 | If you want to override a previous definition of @code{bletch} if it came
|
|---|
| 6947 | from the environment, even under @samp{-e}, you could instead write:
|
|---|
| 6948 |
|
|---|
| 6949 | @example
|
|---|
| 6950 | @group
|
|---|
| 6951 | ifneq "$(findstring environment,$(origin bletch))" ""
|
|---|
| 6952 | bletch = barf, gag, etc.
|
|---|
| 6953 | endif
|
|---|
| 6954 | @end group
|
|---|
| 6955 | @end example
|
|---|
| 6956 |
|
|---|
| 6957 | Here the redefinition takes place if @samp{$(origin bletch)} returns either
|
|---|
| 6958 | @samp{environment} or @samp{environment override}.
|
|---|
| 6959 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 6960 |
|
|---|
| 6961 | @node Shell Function, Make Control Functions, Origin Function, Functions
|
|---|
| 6962 | @section The @code{shell} Function
|
|---|
| 6963 | @findex shell
|
|---|
| 6964 | @cindex commands, expansion
|
|---|
| 6965 | @cindex backquotes
|
|---|
| 6966 | @cindex shell command, function for
|
|---|
| 6967 |
|
|---|
| 6968 | The @code{shell} function is unlike any other function other than the
|
|---|
| 6969 | @code{wildcard} function
|
|---|
| 6970 | (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
|
|---|
| 6971 | communicates with the world outside of @code{make}.
|
|---|
| 6972 |
|
|---|
| 6973 | The @code{shell} function performs the same function that backquotes
|
|---|
| 6974 | (@samp{`}) perform in most shells: it does @dfn{command expansion}.
|
|---|
| 6975 | This means that it takes as an argument a shell command and evaluates
|
|---|
| 6976 | to the output of the command. The only processing @code{make} does on
|
|---|
| 6977 | the result is to convert each newline (or carriage-return / newline
|
|---|
| 6978 | pair) to a single space. If there is a trailing (carriage-return
|
|---|
| 6979 | and) newline it will simply be removed.@refill
|
|---|
| 6980 |
|
|---|
| 6981 | The commands run by calls to the @code{shell} function are run when the
|
|---|
| 6982 | function calls are expanded (@pxref{Reading Makefiles, , How
|
|---|
| 6983 | @code{make} Reads a Makefile}). Because this function involves
|
|---|
| 6984 | spawning a new shell, you should carefully consider the performance
|
|---|
| 6985 | implications of using the @code{shell} function within recursively
|
|---|
| 6986 | expanded variables vs. simply expanded variables (@pxref{Flavors, ,The
|
|---|
| 6987 | Two Flavors of Variables}).
|
|---|
| 6988 |
|
|---|
| 6989 | Here are some examples of the use of the @code{shell} function:
|
|---|
| 6990 |
|
|---|
| 6991 | @example
|
|---|
| 6992 | contents := $(shell cat foo)
|
|---|
| 6993 | @end example
|
|---|
| 6994 |
|
|---|
| 6995 | @noindent
|
|---|
| 6996 | sets @code{contents} to the contents of the file @file{foo}, with a space
|
|---|
| 6997 | (rather than a newline) separating each line.
|
|---|
| 6998 |
|
|---|
| 6999 | @example
|
|---|
| 7000 | files := $(shell echo *.c)
|
|---|
| 7001 | @end example
|
|---|
| 7002 |
|
|---|
| 7003 | @noindent
|
|---|
| 7004 | sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
|
|---|
| 7005 | using a very strange shell, this has the same result as
|
|---|
| 7006 | @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
|
|---|
| 7007 | exists).@refill
|
|---|
| 7008 |
|
|---|
| 7009 | @node Make Control Functions, , Shell Function, Functions
|
|---|
| 7010 | @section Functions That Control Make
|
|---|
| 7011 | @cindex functions, for controlling make
|
|---|
| 7012 | @cindex controlling make
|
|---|
| 7013 |
|
|---|
| 7014 | These functions control the way make runs. Generally, they are used to
|
|---|
| 7015 | provide information to the user of the makefile or to cause make to stop
|
|---|
| 7016 | if some sort of environmental error is detected.
|
|---|
| 7017 |
|
|---|
| 7018 | @table @code
|
|---|
| 7019 | @item $(error @var{text}@dots{})
|
|---|
| 7020 | @findex error
|
|---|
| 7021 | @cindex error, stopping on
|
|---|
| 7022 | @cindex stopping make
|
|---|
| 7023 | Generates a fatal error where the message is @var{text}. Note that the
|
|---|
| 7024 | error is generated whenever this function is evaluated. So, if you put
|
|---|
| 7025 | it inside a command script or on the right side of a recursive variable
|
|---|
| 7026 | assignment, it won't be evaluated until later. The @var{text} will be
|
|---|
| 7027 | expanded before the error is generated.
|
|---|
| 7028 |
|
|---|
| 7029 | For example,
|
|---|
| 7030 |
|
|---|
| 7031 | @example
|
|---|
| 7032 | ifdef ERROR1
|
|---|
| 7033 | $(error error is $(ERROR1))
|
|---|
| 7034 | endif
|
|---|
| 7035 | @end example
|
|---|
| 7036 |
|
|---|
| 7037 | @noindent
|
|---|
| 7038 | will generate a fatal error during the read of the makefile if the
|
|---|
| 7039 | @code{make} variable @code{ERROR1} is defined. Or,
|
|---|
| 7040 |
|
|---|
| 7041 | @example
|
|---|
| 7042 | ERR = $(error found an error!)
|
|---|
| 7043 |
|
|---|
| 7044 | .PHONY: err
|
|---|
| 7045 | err: ; $(ERR)
|
|---|
| 7046 | @end example
|
|---|
| 7047 |
|
|---|
| 7048 | @noindent
|
|---|
| 7049 | will generate a fatal error while @code{make} is running, if the
|
|---|
| 7050 | @code{err} target is invoked.
|
|---|
| 7051 |
|
|---|
| 7052 | @item $(warning @var{text}@dots{})
|
|---|
| 7053 | @findex warning
|
|---|
| 7054 | @cindex warnings, printing
|
|---|
| 7055 | @cindex printing user warnings
|
|---|
| 7056 | This function works similarly to the @code{error} function, above,
|
|---|
| 7057 | except that @code{make} doesn't exit. Instead, @var{text} is expanded
|
|---|
| 7058 | and the resulting message is displayed, but processing of the makefile
|
|---|
| 7059 | continues.
|
|---|
| 7060 |
|
|---|
| 7061 | The result of the expansion of this function is the empty string.
|
|---|
| 7062 |
|
|---|
| 7063 | @item $(info @var{text}@dots{})
|
|---|
| 7064 | @findex info
|
|---|
| 7065 | @cindex printing messages
|
|---|
| 7066 | This function does nothing more than print its (expanded) argument(s)
|
|---|
| 7067 | to standard output. No makefile name or line number is added. The
|
|---|
| 7068 | result of the expansion of this function is the empty string.
|
|---|
| 7069 | @end table
|
|---|
| 7070 |
|
|---|
| 7071 | @node Running, Implicit Rules, Functions, Top
|
|---|
| 7072 | @chapter How to Run @code{make}
|
|---|
| 7073 |
|
|---|
| 7074 | A makefile that says how to recompile a program can be used in more
|
|---|
| 7075 | than one way. The simplest use is to recompile every file that is out
|
|---|
| 7076 | of date. Usually, makefiles are written so that if you run
|
|---|
| 7077 | @code{make} with no arguments, it does just that.
|
|---|
| 7078 |
|
|---|
| 7079 | But you might want to update only some of the files; you might want to use
|
|---|
| 7080 | a different compiler or different compiler options; you might want just to
|
|---|
| 7081 | find out which files are out of date without changing them.
|
|---|
| 7082 |
|
|---|
| 7083 | By giving arguments when you run @code{make}, you can do any of these
|
|---|
| 7084 | things and many others.
|
|---|
| 7085 |
|
|---|
| 7086 | The exit status of @code{make} is always one of three values:
|
|---|
| 7087 | @table @code
|
|---|
| 7088 | @item 0
|
|---|
| 7089 | The exit status is zero if @code{make} is successful.
|
|---|
| 7090 | @item 2
|
|---|
| 7091 | The exit status is two if @code{make} encounters any errors.
|
|---|
| 7092 | It will print messages describing the particular errors.
|
|---|
| 7093 | @item 1
|
|---|
| 7094 | The exit status is one if you use the @samp{-q} flag and @code{make}
|
|---|
| 7095 | determines that some target is not already up to date.
|
|---|
| 7096 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7097 | @end table
|
|---|
| 7098 |
|
|---|
| 7099 | @menu
|
|---|
| 7100 | * Makefile Arguments:: How to specify which makefile to use.
|
|---|
| 7101 | * Goals:: How to use goal arguments to specify which
|
|---|
| 7102 | parts of the makefile to use.
|
|---|
| 7103 | * Instead of Execution:: How to use mode flags to specify what
|
|---|
| 7104 | kind of thing to do with the commands
|
|---|
| 7105 | in the makefile other than simply
|
|---|
| 7106 | execute them.
|
|---|
| 7107 | * Avoiding Compilation:: How to avoid recompiling certain files.
|
|---|
| 7108 | * Overriding:: How to override a variable to specify
|
|---|
| 7109 | an alternate compiler and other things.
|
|---|
| 7110 | * Testing:: How to proceed past some errors, to
|
|---|
| 7111 | test compilation.
|
|---|
| 7112 | * Options Summary:: Summary of Options
|
|---|
| 7113 | @end menu
|
|---|
| 7114 |
|
|---|
| 7115 | @node Makefile Arguments, Goals, Running, Running
|
|---|
| 7116 | @section Arguments to Specify the Makefile
|
|---|
| 7117 | @cindex @code{--file}
|
|---|
| 7118 | @cindex @code{--makefile}
|
|---|
| 7119 | @cindex @code{-f}
|
|---|
| 7120 |
|
|---|
| 7121 | The way to specify the name of the makefile is with the @samp{-f} or
|
|---|
| 7122 | @samp{--file} option (@samp{--makefile} also works). For example,
|
|---|
| 7123 | @samp{-f altmake} says to use the file @file{altmake} as the makefile.
|
|---|
| 7124 |
|
|---|
| 7125 | If you use the @samp{-f} flag several times and follow each @samp{-f}
|
|---|
| 7126 | with an argument, all the specified files are used jointly as
|
|---|
| 7127 | makefiles.
|
|---|
| 7128 |
|
|---|
| 7129 | If you do not use the @samp{-f} or @samp{--file} flag, the default is
|
|---|
| 7130 | to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
|
|---|
| 7131 | that order, and use the first of these three which exists or can be made
|
|---|
| 7132 | (@pxref{Makefiles, ,Writing Makefiles}).@refill
|
|---|
| 7133 |
|
|---|
| 7134 | @node Goals, Instead of Execution, Makefile Arguments, Running
|
|---|
| 7135 | @section Arguments to Specify the Goals
|
|---|
| 7136 | @cindex goal, how to specify
|
|---|
| 7137 |
|
|---|
| 7138 | The @dfn{goals} are the targets that @code{make} should strive ultimately
|
|---|
| 7139 | to update. Other targets are updated as well if they appear as
|
|---|
| 7140 | prerequisites of goals, or prerequisites of prerequisites of goals, etc.
|
|---|
| 7141 |
|
|---|
| 7142 | By default, the goal is the first target in the makefile (not counting
|
|---|
| 7143 | targets that start with a period). Therefore, makefiles are usually
|
|---|
| 7144 | written so that the first target is for compiling the entire program or
|
|---|
| 7145 | programs they describe. If the first rule in the makefile has several
|
|---|
| 7146 | targets, only the first target in the rule becomes the default goal, not
|
|---|
| 7147 | the whole list. You can manage the selection of the default goal from
|
|---|
| 7148 | within your makefile using the @code{.DEFAULT_GOAL} variable
|
|---|
| 7149 | (@pxref{Special Variables, , Other Special Variables}).
|
|---|
| 7150 |
|
|---|
| 7151 | You can also specify a different goal or goals with command-line
|
|---|
| 7152 | arguments to @code{make}. Use the name of the goal as an argument.
|
|---|
| 7153 | If you specify several goals, @code{make} processes each of them in
|
|---|
| 7154 | turn, in the order you name them.
|
|---|
| 7155 |
|
|---|
| 7156 | Any target in the makefile may be specified as a goal (unless it
|
|---|
| 7157 | starts with @samp{-} or contains an @samp{=}, in which case it will be
|
|---|
| 7158 | parsed as a switch or variable definition, respectively). Even
|
|---|
| 7159 | targets not in the makefile may be specified, if @code{make} can find
|
|---|
| 7160 | implicit rules that say how to make them.
|
|---|
| 7161 |
|
|---|
| 7162 | @vindex MAKECMDGOALS
|
|---|
| 7163 | @code{Make} will set the special variable @code{MAKECMDGOALS} to the
|
|---|
| 7164 | list of goals you specified on the command line. If no goals were given
|
|---|
| 7165 | on the command line, this variable is empty. Note that this variable
|
|---|
| 7166 | should be used only in special circumstances.
|
|---|
| 7167 |
|
|---|
| 7168 | An example of appropriate use is to avoid including @file{.d} files
|
|---|
| 7169 | during @code{clean} rules (@pxref{Automatic Prerequisites}), so
|
|---|
| 7170 | @code{make} won't create them only to immediately remove them
|
|---|
| 7171 | again:@refill
|
|---|
| 7172 |
|
|---|
| 7173 | @example
|
|---|
| 7174 | @group
|
|---|
| 7175 | sources = foo.c bar.c
|
|---|
| 7176 |
|
|---|
| 7177 | ifneq ($(MAKECMDGOALS),clean)
|
|---|
| 7178 | include $(sources:.c=.d)
|
|---|
| 7179 | endif
|
|---|
| 7180 | @end group
|
|---|
| 7181 | @end example
|
|---|
| 7182 |
|
|---|
| 7183 | One use of specifying a goal is if you want to compile only a part of
|
|---|
| 7184 | the program, or only one of several programs. Specify as a goal each
|
|---|
| 7185 | file that you wish to remake. For example, consider a directory containing
|
|---|
| 7186 | several programs, with a makefile that starts like this:
|
|---|
| 7187 |
|
|---|
| 7188 | @example
|
|---|
| 7189 | .PHONY: all
|
|---|
| 7190 | all: size nm ld ar as
|
|---|
| 7191 | @end example
|
|---|
| 7192 |
|
|---|
| 7193 | If you are working on the program @code{size}, you might want to say
|
|---|
| 7194 | @w{@samp{make size}} so that only the files of that program are recompiled.
|
|---|
| 7195 |
|
|---|
| 7196 | Another use of specifying a goal is to make files that are not normally
|
|---|
| 7197 | made. For example, there may be a file of debugging output, or a
|
|---|
| 7198 | version of the program that is compiled specially for testing, which has
|
|---|
| 7199 | a rule in the makefile but is not a prerequisite of the default goal.
|
|---|
| 7200 |
|
|---|
| 7201 | Another use of specifying a goal is to run the commands associated with
|
|---|
| 7202 | a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
|
|---|
| 7203 | Targets, ,Empty Target Files to Record Events}). Many makefiles contain
|
|---|
| 7204 | a phony target named @file{clean} which deletes everything except source
|
|---|
| 7205 | files. Naturally, this is done only if you request it explicitly with
|
|---|
| 7206 | @w{@samp{make clean}}. Following is a list of typical phony and empty
|
|---|
| 7207 | target names. @xref{Standard Targets}, for a detailed list of all the
|
|---|
| 7208 | standard target names which GNU software packages use.
|
|---|
| 7209 |
|
|---|
| 7210 | @table @file
|
|---|
| 7211 | @item all
|
|---|
| 7212 | @cindex @code{all} @r{(standard target)}
|
|---|
| 7213 | Make all the top-level targets the makefile knows about.
|
|---|
| 7214 |
|
|---|
| 7215 | @item clean
|
|---|
| 7216 | @cindex @code{clean} @r{(standard target)}
|
|---|
| 7217 | Delete all files that are normally created by running @code{make}.
|
|---|
| 7218 |
|
|---|
| 7219 | @item mostlyclean
|
|---|
| 7220 | @cindex @code{mostlyclean} @r{(standard target)}
|
|---|
| 7221 | Like @samp{clean}, but may refrain from deleting a few files that people
|
|---|
| 7222 | normally don't want to recompile. For example, the @samp{mostlyclean}
|
|---|
| 7223 | target for GCC does not delete @file{libgcc.a}, because recompiling it
|
|---|
| 7224 | is rarely necessary and takes a lot of time.
|
|---|
| 7225 |
|
|---|
| 7226 | @item distclean
|
|---|
| 7227 | @cindex @code{distclean} @r{(standard target)}
|
|---|
| 7228 | @itemx realclean
|
|---|
| 7229 | @cindex @code{realclean} @r{(standard target)}
|
|---|
| 7230 | @itemx clobber
|
|---|
| 7231 | @cindex @code{clobber} @r{(standard target)}
|
|---|
| 7232 | Any of these targets might be defined to delete @emph{more} files than
|
|---|
| 7233 | @samp{clean} does. For example, this would delete configuration files
|
|---|
| 7234 | or links that you would normally create as preparation for compilation,
|
|---|
| 7235 | even if the makefile itself cannot create these files.
|
|---|
| 7236 |
|
|---|
| 7237 | @item install
|
|---|
| 7238 | @cindex @code{install} @r{(standard target)}
|
|---|
| 7239 | Copy the executable file into a directory that users typically search
|
|---|
| 7240 | for commands; copy any auxiliary files that the executable uses into
|
|---|
| 7241 | the directories where it will look for them.
|
|---|
| 7242 |
|
|---|
| 7243 | @item print
|
|---|
| 7244 | @cindex @code{print} @r{(standard target)}
|
|---|
| 7245 | Print listings of the source files that have changed.
|
|---|
| 7246 |
|
|---|
| 7247 | @item tar
|
|---|
| 7248 | @cindex @code{tar} @r{(standard target)}
|
|---|
| 7249 | Create a tar file of the source files.
|
|---|
| 7250 |
|
|---|
| 7251 | @item shar
|
|---|
| 7252 | @cindex @code{shar} @r{(standard target)}
|
|---|
| 7253 | Create a shell archive (shar file) of the source files.
|
|---|
| 7254 |
|
|---|
| 7255 | @item dist
|
|---|
| 7256 | @cindex @code{dist} @r{(standard target)}
|
|---|
| 7257 | Create a distribution file of the source files. This might
|
|---|
| 7258 | be a tar file, or a shar file, or a compressed version of one of the
|
|---|
| 7259 | above, or even more than one of the above.
|
|---|
| 7260 |
|
|---|
| 7261 | @item TAGS
|
|---|
| 7262 | @cindex @code{TAGS} @r{(standard target)}
|
|---|
| 7263 | Update a tags table for this program.
|
|---|
| 7264 |
|
|---|
| 7265 | @item check
|
|---|
| 7266 | @cindex @code{check} @r{(standard target)}
|
|---|
| 7267 | @itemx test
|
|---|
| 7268 | @cindex @code{test} @r{(standard target)}
|
|---|
| 7269 | Perform self tests on the program this makefile builds.
|
|---|
| 7270 | @end table
|
|---|
| 7271 |
|
|---|
| 7272 | @node Instead of Execution, Avoiding Compilation, Goals, Running
|
|---|
| 7273 | @section Instead of Executing the Commands
|
|---|
| 7274 | @cindex execution, instead of
|
|---|
| 7275 | @cindex commands, instead of executing
|
|---|
| 7276 |
|
|---|
| 7277 | The makefile tells @code{make} how to tell whether a target is up to date,
|
|---|
| 7278 | and how to update each target. But updating the targets is not always
|
|---|
| 7279 | what you want. Certain options specify other activities for @code{make}.
|
|---|
| 7280 |
|
|---|
| 7281 | @comment Extra blank lines make it print better.
|
|---|
| 7282 | @table @samp
|
|---|
| 7283 | @item -n
|
|---|
| 7284 | @itemx --just-print
|
|---|
| 7285 | @itemx --dry-run
|
|---|
| 7286 | @itemx --recon
|
|---|
| 7287 | @cindex @code{--just-print}
|
|---|
| 7288 | @cindex @code{--dry-run}
|
|---|
| 7289 | @cindex @code{--recon}
|
|---|
| 7290 | @cindex @code{-n}
|
|---|
| 7291 |
|
|---|
| 7292 | ``No-op''. The activity is to print what commands would be used to make
|
|---|
| 7293 | the targets up to date, but not actually execute them.
|
|---|
| 7294 |
|
|---|
| 7295 | @item -t
|
|---|
| 7296 | @itemx --touch
|
|---|
| 7297 | @cindex @code{--touch}
|
|---|
| 7298 | @cindex touching files
|
|---|
| 7299 | @cindex target, touching
|
|---|
| 7300 | @cindex @code{-t}
|
|---|
| 7301 |
|
|---|
| 7302 | ``Touch''. The activity is to mark the targets as up to date without
|
|---|
| 7303 | actually changing them. In other words, @code{make} pretends to compile
|
|---|
| 7304 | the targets but does not really change their contents.
|
|---|
| 7305 |
|
|---|
| 7306 | @item -q
|
|---|
| 7307 | @itemx --question
|
|---|
| 7308 | @cindex @code{--question}
|
|---|
| 7309 | @cindex @code{-q}
|
|---|
| 7310 | @cindex question mode
|
|---|
| 7311 |
|
|---|
| 7312 | ``Question''. The activity is to find out silently whether the targets
|
|---|
| 7313 | are up to date already; but execute no commands in either case. In other
|
|---|
| 7314 | words, neither compilation nor output will occur.
|
|---|
| 7315 |
|
|---|
| 7316 | @item -W @var{file}
|
|---|
| 7317 | @itemx --what-if=@var{file}
|
|---|
| 7318 | @itemx --assume-new=@var{file}
|
|---|
| 7319 | @itemx --new-file=@var{file}
|
|---|
| 7320 | @cindex @code{--what-if}
|
|---|
| 7321 | @cindex @code{-W}
|
|---|
| 7322 | @cindex @code{--assume-new}
|
|---|
| 7323 | @cindex @code{--new-file}
|
|---|
| 7324 | @cindex what if
|
|---|
| 7325 | @cindex files, assuming new
|
|---|
| 7326 |
|
|---|
| 7327 | ``What if''. Each @samp{-W} flag is followed by a file name. The given
|
|---|
| 7328 | files' modification times are recorded by @code{make} as being the present
|
|---|
| 7329 | time, although the actual modification times remain the same.
|
|---|
| 7330 | You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
|
|---|
| 7331 | to see what would happen if you were to modify specific files.@refill
|
|---|
| 7332 | @end table
|
|---|
| 7333 |
|
|---|
| 7334 | With the @samp{-n} flag, @code{make} prints the commands that it would
|
|---|
| 7335 | normally execute but does not execute them.
|
|---|
| 7336 |
|
|---|
| 7337 | With the @samp{-t} flag, @code{make} ignores the commands in the rules
|
|---|
| 7338 | and uses (in effect) the command @code{touch} for each target that needs to
|
|---|
| 7339 | be remade. The @code{touch} command is also printed, unless @samp{-s} or
|
|---|
| 7340 | @code{.SILENT} is used. For speed, @code{make} does not actually invoke
|
|---|
| 7341 | the program @code{touch}. It does the work directly.
|
|---|
| 7342 |
|
|---|
| 7343 | With the @samp{-q} flag, @code{make} prints nothing and executes no
|
|---|
| 7344 | commands, but the exit status code it returns is zero if and only if the
|
|---|
| 7345 | targets to be considered are already up to date. If the exit status is
|
|---|
| 7346 | one, then some updating needs to be done. If @code{make} encounters an
|
|---|
| 7347 | error, the exit status is two, so you can distinguish an error from a
|
|---|
| 7348 | target that is not up to date.
|
|---|
| 7349 |
|
|---|
| 7350 | It is an error to use more than one of these three flags in the same
|
|---|
| 7351 | invocation of @code{make}.
|
|---|
| 7352 |
|
|---|
| 7353 | @cindex +, and command execution
|
|---|
| 7354 | The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
|
|---|
| 7355 | lines that begin with @samp{+} characters or contain the strings
|
|---|
| 7356 | @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
|
|---|
| 7357 | the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
|
|---|
| 7358 | is run regardless of these options. Other lines in the same rule are
|
|---|
| 7359 | not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
|
|---|
| 7360 | @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
|
|---|
| 7361 |
|
|---|
| 7362 | The @samp{-W} flag provides two features:
|
|---|
| 7363 |
|
|---|
| 7364 | @itemize @bullet
|
|---|
| 7365 | @item
|
|---|
| 7366 | If you also use the @samp{-n} or @samp{-q} flag, you can see what
|
|---|
| 7367 | @code{make} would do if you were to modify some files.
|
|---|
| 7368 |
|
|---|
| 7369 | @item
|
|---|
| 7370 | Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
|
|---|
| 7371 | executing commands, the @samp{-W} flag can direct @code{make} to act
|
|---|
| 7372 | as if some files had been modified, without actually modifying the
|
|---|
| 7373 | files.@refill
|
|---|
| 7374 | @end itemize
|
|---|
| 7375 |
|
|---|
| 7376 | Note that the options @samp{-p} and @samp{-v} allow you to obtain other
|
|---|
| 7377 | information about @code{make} or about the makefiles in use
|
|---|
| 7378 | (@pxref{Options Summary, ,Summary of Options}).@refill
|
|---|
| 7379 |
|
|---|
| 7380 | @node Avoiding Compilation, Overriding, Instead of Execution, Running
|
|---|
| 7381 | @section Avoiding Recompilation of Some Files
|
|---|
| 7382 | @cindex @code{-o}
|
|---|
| 7383 | @cindex @code{--old-file}
|
|---|
| 7384 | @cindex @code{--assume-old}
|
|---|
| 7385 | @cindex files, assuming old
|
|---|
| 7386 | @cindex files, avoiding recompilation of
|
|---|
| 7387 | @cindex recompilation, avoiding
|
|---|
| 7388 |
|
|---|
| 7389 | Sometimes you may have changed a source file but you do not want to
|
|---|
| 7390 | recompile all the files that depend on it. For example, suppose you add
|
|---|
| 7391 | a macro or a declaration to a header file that many other files depend
|
|---|
| 7392 | on. Being conservative, @code{make} assumes that any change in the
|
|---|
| 7393 | header file requires recompilation of all dependent files, but you know
|
|---|
| 7394 | that they do not need to be recompiled and you would rather not waste
|
|---|
| 7395 | the time waiting for them to compile.
|
|---|
| 7396 |
|
|---|
| 7397 | If you anticipate the problem before changing the header file, you can
|
|---|
| 7398 | use the @samp{-t} flag. This flag tells @code{make} not to run the
|
|---|
| 7399 | commands in the rules, but rather to mark the target up to date by
|
|---|
| 7400 | changing its last-modification date. You would follow this procedure:
|
|---|
| 7401 |
|
|---|
| 7402 | @enumerate
|
|---|
| 7403 | @item
|
|---|
| 7404 | Use the command @samp{make} to recompile the source files that really
|
|---|
| 7405 | need recompilation, ensuring that the object files are up-to-date
|
|---|
| 7406 | before you begin.
|
|---|
| 7407 |
|
|---|
| 7408 | @item
|
|---|
| 7409 | Make the changes in the header files.
|
|---|
| 7410 |
|
|---|
| 7411 | @item
|
|---|
| 7412 | Use the command @samp{make -t} to mark all the object files as
|
|---|
| 7413 | up to date. The next time you run @code{make}, the changes in the
|
|---|
| 7414 | header files will not cause any recompilation.
|
|---|
| 7415 | @end enumerate
|
|---|
| 7416 |
|
|---|
| 7417 | If you have already changed the header file at a time when some files
|
|---|
| 7418 | do need recompilation, it is too late to do this. Instead, you can
|
|---|
| 7419 | use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
|
|---|
| 7420 | ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
|
|---|
| 7421 | that the file itself will not be remade, and nothing else will be
|
|---|
| 7422 | remade on its account. Follow this procedure:
|
|---|
| 7423 |
|
|---|
| 7424 | @enumerate
|
|---|
| 7425 | @item
|
|---|
| 7426 | Recompile the source files that need compilation for reasons independent
|
|---|
| 7427 | of the particular header file, with @samp{make -o @var{headerfile}}.
|
|---|
| 7428 | If several header files are involved, use a separate @samp{-o} option
|
|---|
| 7429 | for each header file.
|
|---|
| 7430 |
|
|---|
| 7431 | @item
|
|---|
| 7432 | Touch all the object files with @samp{make -t}.
|
|---|
| 7433 | @end enumerate
|
|---|
| 7434 |
|
|---|
| 7435 | @node Overriding, Testing, Avoiding Compilation, Running
|
|---|
| 7436 | @section Overriding Variables
|
|---|
| 7437 | @cindex overriding variables with arguments
|
|---|
| 7438 | @cindex variables, overriding with arguments
|
|---|
| 7439 | @cindex command line variables
|
|---|
| 7440 | @cindex variables, command line
|
|---|
| 7441 |
|
|---|
| 7442 | An argument that contains @samp{=} specifies the value of a variable:
|
|---|
| 7443 | @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
|
|---|
| 7444 | If you specify a value in this way, all ordinary assignments of the same
|
|---|
| 7445 | variable in the makefile are ignored; we say they have been
|
|---|
| 7446 | @dfn{overridden} by the command line argument.
|
|---|
| 7447 |
|
|---|
| 7448 | The most common way to use this facility is to pass extra flags to
|
|---|
| 7449 | compilers. For example, in a properly written makefile, the variable
|
|---|
| 7450 | @code{CFLAGS} is included in each command that runs the C compiler, so a
|
|---|
| 7451 | file @file{foo.c} would be compiled something like this:
|
|---|
| 7452 |
|
|---|
| 7453 | @example
|
|---|
| 7454 | cc -c $(CFLAGS) foo.c
|
|---|
| 7455 | @end example
|
|---|
| 7456 |
|
|---|
| 7457 | Thus, whatever value you set for @code{CFLAGS} affects each compilation
|
|---|
| 7458 | that occurs. The makefile probably specifies the usual value for
|
|---|
| 7459 | @code{CFLAGS}, like this:
|
|---|
| 7460 |
|
|---|
| 7461 | @example
|
|---|
| 7462 | CFLAGS=-g
|
|---|
| 7463 | @end example
|
|---|
| 7464 |
|
|---|
| 7465 | Each time you run @code{make}, you can override this value if you
|
|---|
| 7466 | wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
|
|---|
| 7467 | compilation will be done with @samp{cc -c -g -O}. (This also
|
|---|
| 7468 | illustrates how you can use quoting in the shell to enclose spaces and
|
|---|
| 7469 | other special characters in the value of a variable when you override
|
|---|
| 7470 | it.)
|
|---|
| 7471 |
|
|---|
| 7472 | The variable @code{CFLAGS} is only one of many standard variables that
|
|---|
| 7473 | exist just so that you can change them this way. @xref{Implicit
|
|---|
| 7474 | Variables, , Variables Used by Implicit Rules}, for a complete list.
|
|---|
| 7475 |
|
|---|
| 7476 | You can also program the makefile to look at additional variables of your
|
|---|
| 7477 | own, giving the user the ability to control other aspects of how the
|
|---|
| 7478 | makefile works by changing the variables.
|
|---|
| 7479 |
|
|---|
| 7480 | When you override a variable with a command argument, you can define either
|
|---|
| 7481 | a recursively-expanded variable or a simply-expanded variable. The
|
|---|
| 7482 | examples shown above make a recursively-expanded variable; to make a
|
|---|
| 7483 | simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
|
|---|
| 7484 | you want to include a variable reference or function call in the
|
|---|
| 7485 | @emph{value} that you specify, it makes no difference which kind of
|
|---|
| 7486 | variable you create.
|
|---|
| 7487 |
|
|---|
| 7488 | There is one way that the makefile can change a variable that you have
|
|---|
| 7489 | overridden. This is to use the @code{override} directive, which is a line
|
|---|
| 7490 | that looks like this: @samp{override @var{variable} = @var{value}}
|
|---|
| 7491 | (@pxref{Override Directive, ,The @code{override} Directive}).
|
|---|
| 7492 |
|
|---|
| 7493 | @node Testing, Options Summary, Overriding, Running
|
|---|
| 7494 | @section Testing the Compilation of a Program
|
|---|
| 7495 | @cindex testing compilation
|
|---|
| 7496 | @cindex compilation, testing
|
|---|
| 7497 |
|
|---|
| 7498 | Normally, when an error happens in executing a shell command, @code{make}
|
|---|
| 7499 | gives up immediately, returning a nonzero status. No further commands are
|
|---|
| 7500 | executed for any target. The error implies that the goal cannot be
|
|---|
| 7501 | correctly remade, and @code{make} reports this as soon as it knows.
|
|---|
| 7502 |
|
|---|
| 7503 | When you are compiling a program that you have just changed, this is not
|
|---|
| 7504 | what you want. Instead, you would rather that @code{make} try compiling
|
|---|
| 7505 | every file that can be tried, to show you as many compilation errors
|
|---|
| 7506 | as possible.
|
|---|
| 7507 |
|
|---|
| 7508 | @cindex @code{-k}
|
|---|
| 7509 | @cindex @code{--keep-going}
|
|---|
| 7510 | On these occasions, you should use the @samp{-k} or
|
|---|
| 7511 | @samp{--keep-going} flag. This tells @code{make} to continue to
|
|---|
| 7512 | consider the other prerequisites of the pending targets, remaking them
|
|---|
| 7513 | if necessary, before it gives up and returns nonzero status. For
|
|---|
| 7514 | example, after an error in compiling one object file, @samp{make -k}
|
|---|
| 7515 | will continue compiling other object files even though it already
|
|---|
| 7516 | knows that linking them will be impossible. In addition to continuing
|
|---|
| 7517 | after failed shell commands, @samp{make -k} will continue as much as
|
|---|
| 7518 | possible after discovering that it does not know how to make a target
|
|---|
| 7519 | or prerequisite file. This will always cause an error message, but
|
|---|
| 7520 | without @samp{-k}, it is a fatal error (@pxref{Options Summary,
|
|---|
| 7521 | ,Summary of Options}).@refill
|
|---|
| 7522 |
|
|---|
| 7523 | The usual behavior of @code{make} assumes that your purpose is to get the
|
|---|
| 7524 | goals up to date; once @code{make} learns that this is impossible, it might
|
|---|
| 7525 | as well report the failure immediately. The @samp{-k} flag says that the
|
|---|
| 7526 | real purpose is to test as much as possible of the changes made in the
|
|---|
| 7527 | program, perhaps to find several independent problems so that you can
|
|---|
| 7528 | correct them all before the next attempt to compile. This is why Emacs'
|
|---|
| 7529 | @kbd{M-x compile} command passes the @samp{-k} flag by default.
|
|---|
| 7530 |
|
|---|
| 7531 | @node Options Summary, , Testing, Running
|
|---|
| 7532 | @section Summary of Options
|
|---|
| 7533 | @cindex options
|
|---|
| 7534 | @cindex flags
|
|---|
| 7535 | @cindex switches
|
|---|
| 7536 |
|
|---|
| 7537 | Here is a table of all the options @code{make} understands:
|
|---|
| 7538 |
|
|---|
| 7539 | @table @samp
|
|---|
| 7540 | @item -b
|
|---|
| 7541 | @cindex @code{-b}
|
|---|
| 7542 | @itemx -m
|
|---|
| 7543 | @cindex @code{-m}
|
|---|
| 7544 | These options are ignored for compatibility with other versions of @code{make}.
|
|---|
| 7545 |
|
|---|
| 7546 | @item -B
|
|---|
| 7547 | @cindex @code{-B}
|
|---|
| 7548 | @itemx --always-make
|
|---|
| 7549 | @cindex @code{--always-make}
|
|---|
| 7550 | Consider all targets out-of-date. GNU @code{make} proceeds to
|
|---|
| 7551 | consider targets and their prerequisites using the normal algorithms;
|
|---|
| 7552 | however, all these targets are remade, regardless of the status of
|
|---|
| 7553 | their prerequisites.
|
|---|
| 7554 |
|
|---|
| 7555 | @item -C @var{dir}
|
|---|
| 7556 | @cindex @code{-C}
|
|---|
| 7557 | @itemx --directory=@var{dir}
|
|---|
| 7558 | @cindex @code{--directory}
|
|---|
| 7559 | Change to directory @var{dir} before reading the makefiles. If multiple
|
|---|
| 7560 | @samp{-C} options are specified, each is interpreted relative to the
|
|---|
| 7561 | previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
|
|---|
| 7562 | This is typically used with recursive invocations of @code{make}
|
|---|
| 7563 | (@pxref{Recursion, ,Recursive Use of @code{make}}).
|
|---|
| 7564 |
|
|---|
| 7565 | @item -d
|
|---|
| 7566 | @cindex @code{-d}
|
|---|
| 7567 | @c Extra blank line here makes the table look better.
|
|---|
| 7568 |
|
|---|
| 7569 | Print debugging information in addition to normal processing. The
|
|---|
| 7570 | debugging information says which files are being considered for
|
|---|
| 7571 | remaking, which file-times are being compared and with what results,
|
|---|
| 7572 | which files actually need to be remade, which implicit rules are
|
|---|
| 7573 | considered and which are applied---everything interesting about how
|
|---|
| 7574 | @code{make} decides what to do. The @code{-d} option is equivalent to
|
|---|
| 7575 | @samp{--debug=a} (see below).
|
|---|
| 7576 |
|
|---|
| 7577 | @item --debug[=@var{options}]
|
|---|
| 7578 | @cindex @code{--debug}
|
|---|
| 7579 | @c Extra blank line here makes the table look better.
|
|---|
| 7580 |
|
|---|
| 7581 | Print debugging information in addition to normal processing. Various
|
|---|
| 7582 | levels and types of output can be chosen. With no arguments, print the
|
|---|
| 7583 | ``basic'' level of debugging. Possible arguments are below; only the
|
|---|
| 7584 | first character is considered, and values must be comma- or
|
|---|
| 7585 | space-separated.
|
|---|
| 7586 |
|
|---|
| 7587 | @table @code
|
|---|
| 7588 | @item a (@i{all})
|
|---|
| 7589 | All types of debugging output are enabled. This is equivalent to using
|
|---|
| 7590 | @samp{-d}.
|
|---|
| 7591 |
|
|---|
| 7592 | @item b (@i{basic})
|
|---|
| 7593 | Basic debugging prints each target that was found to be out-of-date, and
|
|---|
| 7594 | whether the build was successful or not.
|
|---|
| 7595 |
|
|---|
| 7596 | @item v (@i{verbose})
|
|---|
| 7597 | A level above @samp{basic}; includes messages about which makefiles were
|
|---|
| 7598 | parsed, prerequisites that did not need to be rebuilt, etc. This option
|
|---|
| 7599 | also enables @samp{basic} messages.
|
|---|
| 7600 |
|
|---|
| 7601 | @item i (@i{implicit})
|
|---|
| 7602 | Prints messages describing the implicit rule searches for each target.
|
|---|
| 7603 | This option also enables @samp{basic} messages.
|
|---|
| 7604 |
|
|---|
| 7605 | @item j (@i{jobs})
|
|---|
| 7606 | Prints messages giving details on the invocation of specific subcommands.
|
|---|
| 7607 |
|
|---|
| 7608 | @item m (@i{makefile})
|
|---|
| 7609 | By default, the above messages are not enabled while trying to remake
|
|---|
| 7610 | the makefiles. This option enables messages while rebuilding makefiles,
|
|---|
| 7611 | too. Note that the @samp{all} option does enable this option. This
|
|---|
| 7612 | option also enables @samp{basic} messages.
|
|---|
| 7613 | @end table
|
|---|
| 7614 |
|
|---|
| 7615 | @item -e
|
|---|
| 7616 | @cindex @code{-e}
|
|---|
| 7617 | @itemx --environment-overrides
|
|---|
| 7618 | @cindex @code{--environment-overrides}
|
|---|
| 7619 | Give variables taken from the environment precedence
|
|---|
| 7620 | over variables from makefiles.
|
|---|
| 7621 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 7622 |
|
|---|
| 7623 | @item -f @var{file}
|
|---|
| 7624 | @cindex @code{-f}
|
|---|
| 7625 | @itemx --file=@var{file}
|
|---|
| 7626 | @cindex @code{--file}
|
|---|
| 7627 | @itemx --makefile=@var{file}
|
|---|
| 7628 | @cindex @code{--makefile}
|
|---|
| 7629 | Read the file named @var{file} as a makefile.
|
|---|
| 7630 | @xref{Makefiles, ,Writing Makefiles}.
|
|---|
| 7631 |
|
|---|
| 7632 | @item -h
|
|---|
| 7633 | @cindex @code{-h}
|
|---|
| 7634 | @itemx --help
|
|---|
| 7635 | @cindex @code{--help}
|
|---|
| 7636 | @c Extra blank line here makes the table look better.
|
|---|
| 7637 |
|
|---|
| 7638 | Remind you of the options that @code{make} understands and then exit.
|
|---|
| 7639 |
|
|---|
| 7640 | @item -i
|
|---|
| 7641 | @cindex @code{-i}
|
|---|
| 7642 | @itemx --ignore-errors
|
|---|
| 7643 | @cindex @code{--ignore-errors}
|
|---|
| 7644 | Ignore all errors in commands executed to remake files.
|
|---|
| 7645 | @xref{Errors, ,Errors in Commands}.
|
|---|
| 7646 |
|
|---|
| 7647 | @item -I @var{dir}
|
|---|
| 7648 | @cindex @code{-I}
|
|---|
| 7649 | @itemx --include-dir=@var{dir}
|
|---|
| 7650 | @cindex @code{--include-dir}
|
|---|
| 7651 | Specifies a directory @var{dir} to search for included makefiles.
|
|---|
| 7652 | @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
|
|---|
| 7653 | options are used to specify several directories, the directories are
|
|---|
| 7654 | searched in the order specified.
|
|---|
| 7655 |
|
|---|
| 7656 | @item -j [@var{jobs}]
|
|---|
| 7657 | @cindex @code{-j}
|
|---|
| 7658 | @itemx --jobs[=@var{jobs}]
|
|---|
| 7659 | @cindex @code{--jobs}
|
|---|
| 7660 | Specifies the number of jobs (commands) to run simultaneously. With no
|
|---|
| 7661 | argument, @code{make} runs as many jobs simultaneously as possible. If
|
|---|
| 7662 | there is more than one @samp{-j} option, the last one is effective.
|
|---|
| 7663 | @xref{Parallel, ,Parallel Execution},
|
|---|
| 7664 | for more information on how commands are run.
|
|---|
| 7665 | Note that this option is ignored on MS-DOS.
|
|---|
| 7666 |
|
|---|
| 7667 | @item -k
|
|---|
| 7668 | @cindex @code{-k}
|
|---|
| 7669 | @itemx --keep-going
|
|---|
| 7670 | @cindex @code{--keep-going}
|
|---|
| 7671 | Continue as much as possible after an error. While the target that
|
|---|
| 7672 | failed, and those that depend on it, cannot be remade, the other
|
|---|
| 7673 | prerequisites of these targets can be processed all the same.
|
|---|
| 7674 | @xref{Testing, ,Testing the Compilation of a Program}.
|
|---|
| 7675 |
|
|---|
| 7676 | @item -l [@var{load}]
|
|---|
| 7677 | @cindex @code{-l}
|
|---|
| 7678 | @itemx --load-average[=@var{load}]
|
|---|
| 7679 | @cindex @code{--load-average}
|
|---|
| 7680 | @itemx --max-load[=@var{load}]
|
|---|
| 7681 | @cindex @code{--max-load}
|
|---|
| 7682 | Specifies that no new jobs (commands) should be started if there are
|
|---|
| 7683 | other jobs running and the load average is at least @var{load} (a
|
|---|
| 7684 | floating-point number). With no argument, removes a previous load
|
|---|
| 7685 | limit. @xref{Parallel, ,Parallel Execution}.
|
|---|
| 7686 |
|
|---|
| 7687 | @item -L
|
|---|
| 7688 | @cindex @code{-L}
|
|---|
| 7689 | @itemx --check-symlink-times
|
|---|
| 7690 | @cindex @code{--check-symlink-times}
|
|---|
| 7691 | On systems that support symbolic links, this option causes @code{make}
|
|---|
| 7692 | to consider the timestamps on any symbolic links in addition to the
|
|---|
| 7693 | timestamp on the file referenced by those links. When this option is
|
|---|
| 7694 | provided, the most recent timestamp among the file and the symbolic
|
|---|
| 7695 | links is taken as the modification time for this target file.
|
|---|
| 7696 |
|
|---|
| 7697 | @item -n
|
|---|
| 7698 | @cindex @code{-n}
|
|---|
| 7699 | @itemx --just-print
|
|---|
| 7700 | @cindex @code{--just-print}
|
|---|
| 7701 | @itemx --dry-run
|
|---|
| 7702 | @cindex @code{--dry-run}
|
|---|
| 7703 | @itemx --recon
|
|---|
| 7704 | @cindex @code{--recon}
|
|---|
| 7705 | @c Extra blank line here makes the table look better.
|
|---|
| 7706 |
|
|---|
| 7707 | Print the commands that would be executed, but do not execute them.
|
|---|
| 7708 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7709 |
|
|---|
| 7710 | @item -o @var{file}
|
|---|
| 7711 | @cindex @code{-o}
|
|---|
| 7712 | @itemx --old-file=@var{file}
|
|---|
| 7713 | @cindex @code{--old-file}
|
|---|
| 7714 | @itemx --assume-old=@var{file}
|
|---|
| 7715 | @cindex @code{--assume-old}
|
|---|
| 7716 | Do not remake the file @var{file} even if it is older than its
|
|---|
| 7717 | prerequisites, and do not remake anything on account of changes in
|
|---|
| 7718 | @var{file}. Essentially the file is treated as very old and its rules
|
|---|
| 7719 | are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
|
|---|
| 7720 | Some Files}.@refill
|
|---|
| 7721 |
|
|---|
| 7722 | @item -p
|
|---|
| 7723 | @cindex @code{-p}
|
|---|
| 7724 | @itemx --print-data-base
|
|---|
| 7725 | @cindex @code{--print-data-base}
|
|---|
| 7726 | @cindex data base of @code{make} rules
|
|---|
| 7727 | @cindex predefined rules and variables, printing
|
|---|
| 7728 | Print the data base (rules and variable values) that results from
|
|---|
| 7729 | reading the makefiles; then execute as usual or as otherwise specified.
|
|---|
| 7730 | This also prints the version information given by the @samp{-v} switch
|
|---|
| 7731 | (see below). To print the data base without trying to remake any files,
|
|---|
| 7732 | use @w{@samp{make -qp}}. To print the data base of predefined rules and
|
|---|
| 7733 | variables, use @w{@samp{make -p -f /dev/null}}. The data base output
|
|---|
| 7734 | contains filename and linenumber information for command and variable
|
|---|
| 7735 | definitions, so it can be a useful debugging tool in complex environments.
|
|---|
| 7736 |
|
|---|
| 7737 | @item -q
|
|---|
| 7738 | @cindex @code{-q}
|
|---|
| 7739 | @itemx --question
|
|---|
| 7740 | @cindex @code{--question}
|
|---|
| 7741 | ``Question mode''. Do not run any commands, or print anything; just
|
|---|
| 7742 | return an exit status that is zero if the specified targets are already
|
|---|
| 7743 | up to date, one if any remaking is required, or two if an error is
|
|---|
| 7744 | encountered. @xref{Instead of Execution, ,Instead of Executing the
|
|---|
| 7745 | Commands}.@refill
|
|---|
| 7746 |
|
|---|
| 7747 | @item -r
|
|---|
| 7748 | @cindex @code{-r}
|
|---|
| 7749 | @itemx --no-builtin-rules
|
|---|
| 7750 | @cindex @code{--no-builtin-rules}
|
|---|
| 7751 | Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
|
|---|
| 7752 | ,Using Implicit Rules}). You can still define your own by writing
|
|---|
| 7753 | pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
|
|---|
| 7754 | Rules}). The @samp{-r} option also clears out the default list of
|
|---|
| 7755 | suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
|
|---|
| 7756 | Rules}). But you can still define your own suffixes with a rule for
|
|---|
| 7757 | @code{.SUFFIXES}, and then define your own suffix rules. Note that only
|
|---|
| 7758 | @emph{rules} are affected by the @code{-r} option; default variables
|
|---|
| 7759 | remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
|
|---|
| 7760 | Rules}); see the @samp{-R} option below.
|
|---|
| 7761 |
|
|---|
| 7762 | @item -R
|
|---|
| 7763 | @cindex @code{-R}
|
|---|
| 7764 | @itemx --no-builtin-variables
|
|---|
| 7765 | @cindex @code{--no-builtin-variables}
|
|---|
| 7766 | Eliminate use of the built-in rule-specific variables (@pxref{Implicit
|
|---|
| 7767 | Variables, ,Variables Used by Implicit Rules}). You can still define
|
|---|
| 7768 | your own, of course. The @samp{-R} option also automatically enables
|
|---|
| 7769 | the @samp{-r} option (see above), since it doesn't make sense to have
|
|---|
| 7770 | implicit rules without any definitions for the variables that they use.
|
|---|
| 7771 |
|
|---|
| 7772 | @item -s
|
|---|
| 7773 | @cindex @code{-s}
|
|---|
| 7774 | @itemx --silent
|
|---|
| 7775 | @cindex @code{--silent}
|
|---|
| 7776 | @itemx --quiet
|
|---|
| 7777 | @cindex @code{--quiet}
|
|---|
| 7778 | @c Extra blank line here makes the table look better.
|
|---|
| 7779 |
|
|---|
| 7780 | Silent operation; do not print the commands as they are executed.
|
|---|
| 7781 | @xref{Echoing, ,Command Echoing}.
|
|---|
| 7782 |
|
|---|
| 7783 | @item -S
|
|---|
| 7784 | @cindex @code{-S}
|
|---|
| 7785 | @itemx --no-keep-going
|
|---|
| 7786 | @cindex @code{--no-keep-going}
|
|---|
| 7787 | @itemx --stop
|
|---|
| 7788 | @cindex @code{--stop}
|
|---|
| 7789 | @c Extra blank line here makes the table look better.
|
|---|
| 7790 |
|
|---|
| 7791 | Cancel the effect of the @samp{-k} option. This is never necessary
|
|---|
| 7792 | except in a recursive @code{make} where @samp{-k} might be inherited
|
|---|
| 7793 | from the top-level @code{make} via @code{MAKEFLAGS}
|
|---|
| 7794 | (@pxref{Recursion, ,Recursive Use of @code{make}})
|
|---|
| 7795 | or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
|
|---|
| 7796 |
|
|---|
| 7797 | @item -t
|
|---|
| 7798 | @cindex @code{-t}
|
|---|
| 7799 | @itemx --touch
|
|---|
| 7800 | @cindex @code{--touch}
|
|---|
| 7801 | @c Extra blank line here makes the table look better.
|
|---|
| 7802 |
|
|---|
| 7803 | Touch files (mark them up to date without really changing them)
|
|---|
| 7804 | instead of running their commands. This is used to pretend that the
|
|---|
| 7805 | commands were done, in order to fool future invocations of
|
|---|
| 7806 | @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7807 |
|
|---|
| 7808 | @item -v
|
|---|
| 7809 | @cindex @code{-v}
|
|---|
| 7810 | @itemx --version
|
|---|
| 7811 | @cindex @code{--version}
|
|---|
| 7812 | Print the version of the @code{make} program plus a copyright, a list
|
|---|
| 7813 | of authors, and a notice that there is no warranty; then exit.
|
|---|
| 7814 |
|
|---|
| 7815 | @item -w
|
|---|
| 7816 | @cindex @code{-w}
|
|---|
| 7817 | @itemx --print-directory
|
|---|
| 7818 | @cindex @code{--print-directory}
|
|---|
| 7819 | Print a message containing the working directory both before and after
|
|---|
| 7820 | executing the makefile. This may be useful for tracking down errors
|
|---|
| 7821 | from complicated nests of recursive @code{make} commands.
|
|---|
| 7822 | @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
|
|---|
| 7823 | rarely need to specify this option since @samp{make} does it for you;
|
|---|
| 7824 | see @ref{-w Option, ,The @samp{--print-directory} Option}.)
|
|---|
| 7825 |
|
|---|
| 7826 | @itemx --no-print-directory
|
|---|
| 7827 | @cindex @code{--no-print-directory}
|
|---|
| 7828 | Disable printing of the working directory under @code{-w}.
|
|---|
| 7829 | This option is useful when @code{-w} is turned on automatically,
|
|---|
| 7830 | but you do not want to see the extra messages.
|
|---|
| 7831 | @xref{-w Option, ,The @samp{--print-directory} Option}.
|
|---|
| 7832 |
|
|---|
| 7833 | @item -W @var{file}
|
|---|
| 7834 | @cindex @code{-W}
|
|---|
| 7835 | @itemx --what-if=@var{file}
|
|---|
| 7836 | @cindex @code{--what-if}
|
|---|
| 7837 | @itemx --new-file=@var{file}
|
|---|
| 7838 | @cindex @code{--new-file}
|
|---|
| 7839 | @itemx --assume-new=@var{file}
|
|---|
| 7840 | @cindex @code{--assume-new}
|
|---|
| 7841 | Pretend that the target @var{file} has just been modified. When used
|
|---|
| 7842 | with the @samp{-n} flag, this shows you what would happen if you were
|
|---|
| 7843 | to modify that file. Without @samp{-n}, it is almost the same as
|
|---|
| 7844 | running a @code{touch} command on the given file before running
|
|---|
| 7845 | @code{make}, except that the modification time is changed only in the
|
|---|
| 7846 | imagination of @code{make}.
|
|---|
| 7847 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7848 |
|
|---|
| 7849 | @item --warn-undefined-variables
|
|---|
| 7850 | @cindex @code{--warn-undefined-variables}
|
|---|
| 7851 | @cindex variables, warning for undefined
|
|---|
| 7852 | @cindex undefined variables, warning message
|
|---|
| 7853 | Issue a warning message whenever @code{make} sees a reference to an
|
|---|
| 7854 | undefined variable. This can be helpful when you are trying to debug
|
|---|
| 7855 | makefiles which use variables in complex ways.
|
|---|
| 7856 | @end table
|
|---|
| 7857 |
|
|---|
| 7858 | @node Implicit Rules, Archives, Running, Top
|
|---|
| 7859 | @chapter Using Implicit Rules
|
|---|
| 7860 | @cindex implicit rule
|
|---|
| 7861 | @cindex rule, implicit
|
|---|
| 7862 |
|
|---|
| 7863 | Certain standard ways of remaking target files are used very often. For
|
|---|
| 7864 | example, one customary way to make an object file is from a C source file
|
|---|
| 7865 | using the C compiler, @code{cc}.
|
|---|
| 7866 |
|
|---|
| 7867 | @dfn{Implicit rules} tell @code{make} how to use customary techniques so
|
|---|
| 7868 | that you do not have to specify them in detail when you want to use
|
|---|
| 7869 | them. For example, there is an implicit rule for C compilation. File
|
|---|
| 7870 | names determine which implicit rules are run. For example, C
|
|---|
| 7871 | compilation typically takes a @file{.c} file and makes a @file{.o} file.
|
|---|
| 7872 | So @code{make} applies the implicit rule for C compilation when it sees
|
|---|
| 7873 | this combination of file name endings.@refill
|
|---|
| 7874 |
|
|---|
| 7875 | A chain of implicit rules can apply in sequence; for example, @code{make}
|
|---|
| 7876 | will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
|
|---|
| 7877 | @iftex
|
|---|
| 7878 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 7879 | @end iftex
|
|---|
| 7880 |
|
|---|
| 7881 | The built-in implicit rules use several variables in their commands so
|
|---|
| 7882 | that, by changing the values of the variables, you can change the way the
|
|---|
| 7883 | implicit rule works. For example, the variable @code{CFLAGS} controls the
|
|---|
| 7884 | flags given to the C compiler by the implicit rule for C compilation.
|
|---|
| 7885 | @iftex
|
|---|
| 7886 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 7887 | @end iftex
|
|---|
| 7888 |
|
|---|
| 7889 | You can define your own implicit rules by writing @dfn{pattern rules}.
|
|---|
| 7890 | @iftex
|
|---|
| 7891 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
|
|---|
| 7892 | @end iftex
|
|---|
| 7893 |
|
|---|
| 7894 | @dfn{Suffix rules} are a more limited way to define implicit rules.
|
|---|
| 7895 | Pattern rules are more general and clearer, but suffix rules are
|
|---|
| 7896 | retained for compatibility.
|
|---|
| 7897 | @iftex
|
|---|
| 7898 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 7899 | @end iftex
|
|---|
| 7900 |
|
|---|
| 7901 | @menu
|
|---|
| 7902 | * Using Implicit:: How to use an existing implicit rule
|
|---|
| 7903 | to get the commands for updating a file.
|
|---|
| 7904 | * Catalogue of Rules:: A list of built-in implicit rules.
|
|---|
| 7905 | * Implicit Variables:: How to change what predefined rules do.
|
|---|
| 7906 | * Chained Rules:: How to use a chain of implicit rules.
|
|---|
| 7907 | * Pattern Rules:: How to define new implicit rules.
|
|---|
| 7908 | * Last Resort:: How to define commands for rules which
|
|---|
| 7909 | cannot find any.
|
|---|
| 7910 | * Suffix Rules:: The old-fashioned style of implicit rule.
|
|---|
| 7911 | * Implicit Rule Search:: The precise algorithm for applying
|
|---|
| 7912 | implicit rules.
|
|---|
| 7913 | @end menu
|
|---|
| 7914 |
|
|---|
| 7915 | @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
|
|---|
| 7916 | @section Using Implicit Rules
|
|---|
| 7917 | @cindex implicit rule, how to use
|
|---|
| 7918 | @cindex rule, implicit, how to use
|
|---|
| 7919 |
|
|---|
| 7920 | To allow @code{make} to find a customary method for updating a target file,
|
|---|
| 7921 | all you have to do is refrain from specifying commands yourself. Either
|
|---|
| 7922 | write a rule with no command lines, or don't write a rule at all. Then
|
|---|
| 7923 | @code{make} will figure out which implicit rule to use based on which
|
|---|
| 7924 | kind of source file exists or can be made.
|
|---|
| 7925 |
|
|---|
| 7926 | For example, suppose the makefile looks like this:
|
|---|
| 7927 |
|
|---|
| 7928 | @example
|
|---|
| 7929 | foo : foo.o bar.o
|
|---|
| 7930 | cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
|
|---|
| 7931 | @end example
|
|---|
| 7932 |
|
|---|
| 7933 | @noindent
|
|---|
| 7934 | Because you mention @file{foo.o} but do not give a rule for it, @code{make}
|
|---|
| 7935 | will automatically look for an implicit rule that tells how to update it.
|
|---|
| 7936 | This happens whether or not the file @file{foo.o} currently exists.
|
|---|
| 7937 |
|
|---|
| 7938 | If an implicit rule is found, it can supply both commands and one or
|
|---|
| 7939 | more prerequisites (the source files). You would want to write a rule
|
|---|
| 7940 | for @file{foo.o} with no command lines if you need to specify additional
|
|---|
| 7941 | prerequisites, such as header files, that the implicit rule cannot
|
|---|
| 7942 | supply.
|
|---|
| 7943 |
|
|---|
| 7944 | Each implicit rule has a target pattern and prerequisite patterns. There may
|
|---|
| 7945 | be many implicit rules with the same target pattern. For example, numerous
|
|---|
| 7946 | rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
|
|---|
| 7947 | another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
|
|---|
| 7948 | that actually applies is the one whose prerequisites exist or can be made.
|
|---|
| 7949 | So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
|
|---|
| 7950 | otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
|
|---|
| 7951 | compiler; and so on.
|
|---|
| 7952 |
|
|---|
| 7953 | Of course, when you write the makefile, you know which implicit rule you
|
|---|
| 7954 | want @code{make} to use, and you know it will choose that one because you
|
|---|
| 7955 | know which possible prerequisite files are supposed to exist.
|
|---|
| 7956 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
|---|
| 7957 | for a catalogue of all the predefined implicit rules.
|
|---|
| 7958 |
|
|---|
| 7959 | Above, we said an implicit rule applies if the required prerequisites ``exist
|
|---|
| 7960 | or can be made''. A file ``can be made'' if it is mentioned explicitly in
|
|---|
| 7961 | the makefile as a target or a prerequisite, or if an implicit rule can be
|
|---|
| 7962 | recursively found for how to make it. When an implicit prerequisite is the
|
|---|
| 7963 | result of another implicit rule, we say that @dfn{chaining} is occurring.
|
|---|
| 7964 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 7965 |
|
|---|
| 7966 | In general, @code{make} searches for an implicit rule for each target, and
|
|---|
| 7967 | for each double-colon rule, that has no commands. A file that is mentioned
|
|---|
| 7968 | only as a prerequisite is considered a target whose rule specifies nothing,
|
|---|
| 7969 | so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
|
|---|
| 7970 | details of how the search is done.
|
|---|
| 7971 |
|
|---|
| 7972 | Note that explicit prerequisites do not influence implicit rule search.
|
|---|
| 7973 | For example, consider this explicit rule:
|
|---|
| 7974 |
|
|---|
| 7975 | @example
|
|---|
| 7976 | foo.o: foo.p
|
|---|
| 7977 | @end example
|
|---|
| 7978 |
|
|---|
| 7979 | @noindent
|
|---|
| 7980 | The prerequisite on @file{foo.p} does not necessarily mean that
|
|---|
| 7981 | @code{make} will remake @file{foo.o} according to the implicit rule to
|
|---|
| 7982 | make an object file, a @file{.o} file, from a Pascal source file, a
|
|---|
| 7983 | @file{.p} file. For example, if @file{foo.c} also exists, the implicit
|
|---|
| 7984 | rule to make an object file from a C source file is used instead,
|
|---|
| 7985 | because it appears before the Pascal rule in the list of predefined
|
|---|
| 7986 | implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
|
|---|
| 7987 | Rules}).
|
|---|
| 7988 |
|
|---|
| 7989 | If you do not want an implicit rule to be used for a target that has no
|
|---|
| 7990 | commands, you can give that target empty commands by writing a semicolon
|
|---|
| 7991 | (@pxref{Empty Commands, ,Defining Empty Commands}).
|
|---|
| 7992 |
|
|---|
| 7993 | @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
|
|---|
| 7994 | @section Catalogue of Implicit Rules
|
|---|
| 7995 | @cindex implicit rule, predefined
|
|---|
| 7996 | @cindex rule, implicit, predefined
|
|---|
| 7997 |
|
|---|
| 7998 | Here is a catalogue of predefined implicit rules which are always
|
|---|
| 7999 | available unless the makefile explicitly overrides or cancels them.
|
|---|
| 8000 | @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
|
|---|
| 8001 | canceling or overriding an implicit rule. The @samp{-r} or
|
|---|
| 8002 | @samp{--no-builtin-rules} option cancels all predefined rules.
|
|---|
| 8003 |
|
|---|
| 8004 | Not all of these rules will always be defined, even when the @samp{-r}
|
|---|
| 8005 | option is not given. Many of the predefined implicit rules are
|
|---|
| 8006 | implemented in @code{make} as suffix rules, so which ones will be
|
|---|
| 8007 | defined depends on the @dfn{suffix list} (the list of prerequisites of
|
|---|
| 8008 | the special target @code{.SUFFIXES}). The default suffix list is:
|
|---|
| 8009 | @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
|
|---|
| 8010 | @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
|
|---|
| 8011 | @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
|
|---|
| 8012 | @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
|
|---|
| 8013 | @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
|
|---|
| 8014 | @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
|
|---|
| 8015 | described below whose prerequisites have one of these suffixes are
|
|---|
| 8016 | actually suffix rules. If you modify the suffix list, the only
|
|---|
| 8017 | predefined suffix rules in effect will be those named by one or two of
|
|---|
| 8018 | the suffixes that are on the list you specify; rules whose suffixes fail
|
|---|
| 8019 | to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
|
|---|
| 8020 | Suffix Rules}, for full details on suffix rules.
|
|---|
| 8021 |
|
|---|
| 8022 | @table @asis
|
|---|
| 8023 | @item Compiling C programs
|
|---|
| 8024 | @cindex C, rule to compile
|
|---|
| 8025 | @pindex cc
|
|---|
| 8026 | @pindex gcc
|
|---|
| 8027 | @pindex .o
|
|---|
| 8028 | @pindex .c
|
|---|
| 8029 | @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
|
|---|
| 8030 | a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
|
|---|
| 8031 |
|
|---|
| 8032 | @item Compiling C++ programs
|
|---|
| 8033 | @cindex C++, rule to compile
|
|---|
| 8034 | @pindex g++
|
|---|
| 8035 | @pindex .C
|
|---|
| 8036 | @pindex .cc
|
|---|
| 8037 | @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
|
|---|
| 8038 | @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
|
|---|
| 8039 | $(CXXFLAGS)}. We encourage you to use the suffix @samp{.cc} for C++
|
|---|
| 8040 | source files instead of @samp{.C}.@refill
|
|---|
| 8041 |
|
|---|
| 8042 | @item Compiling Pascal programs
|
|---|
| 8043 | @cindex Pascal, rule to compile
|
|---|
| 8044 | @pindex pc
|
|---|
| 8045 | @pindex .p
|
|---|
| 8046 | @file{@var{n}.o} is made automatically from @file{@var{n}.p}
|
|---|
| 8047 | with the command @samp{$(PC) -c $(PFLAGS)}.@refill
|
|---|
| 8048 |
|
|---|
| 8049 | @item Compiling Fortran and Ratfor programs
|
|---|
| 8050 | @cindex Fortran, rule to compile
|
|---|
| 8051 | @cindex Ratfor, rule to compile
|
|---|
| 8052 | @pindex f77
|
|---|
| 8053 | @pindex .f
|
|---|
| 8054 | @pindex .r
|
|---|
| 8055 | @pindex .F
|
|---|
| 8056 | @file{@var{n}.o} is made automatically from @file{@var{n}.r},
|
|---|
| 8057 | @file{@var{n}.F} or @file{@var{n}.f} by running the
|
|---|
| 8058 | Fortran compiler. The precise command used is as follows:@refill
|
|---|
| 8059 |
|
|---|
| 8060 | @table @samp
|
|---|
| 8061 | @item .f
|
|---|
| 8062 | @samp{$(FC) -c $(FFLAGS)}.
|
|---|
| 8063 | @item .F
|
|---|
| 8064 | @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
|
|---|
| 8065 | @item .r
|
|---|
| 8066 | @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
|
|---|
| 8067 | @end table
|
|---|
| 8068 |
|
|---|
| 8069 | @item Preprocessing Fortran and Ratfor programs
|
|---|
| 8070 | @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
|
|---|
| 8071 | @file{@var{n}.F}. This rule runs just the preprocessor to convert a
|
|---|
| 8072 | Ratfor or preprocessable Fortran program into a strict Fortran
|
|---|
| 8073 | program. The precise command used is as follows:@refill
|
|---|
| 8074 |
|
|---|
| 8075 | @table @samp
|
|---|
| 8076 | @item .F
|
|---|
| 8077 | @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
|
|---|
| 8078 | @item .r
|
|---|
| 8079 | @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
|
|---|
| 8080 | @end table
|
|---|
| 8081 |
|
|---|
| 8082 | @item Compiling Modula-2 programs
|
|---|
| 8083 | @cindex Modula-2, rule to compile
|
|---|
| 8084 | @pindex m2c
|
|---|
| 8085 | @pindex .sym
|
|---|
| 8086 | @pindex .def
|
|---|
| 8087 | @pindex .mod
|
|---|
| 8088 | @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
|
|---|
| 8089 | of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
|
|---|
| 8090 | is made from @file{@var{n}.mod}; the form is:
|
|---|
| 8091 | @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
|
|---|
| 8092 |
|
|---|
| 8093 | @need 1200
|
|---|
| 8094 | @item Assembling and preprocessing assembler programs
|
|---|
| 8095 | @cindex assembly, rule to compile
|
|---|
| 8096 | @pindex as
|
|---|
| 8097 | @pindex .s
|
|---|
| 8098 | @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
|
|---|
| 8099 | running the assembler, @code{as}. The precise command is
|
|---|
| 8100 | @samp{$(AS) $(ASFLAGS)}.@refill
|
|---|
| 8101 |
|
|---|
| 8102 | @pindex .S
|
|---|
| 8103 | @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
|
|---|
| 8104 | running the C preprocessor, @code{cpp}. The precise command is
|
|---|
| 8105 | @w{@samp{$(CPP) $(CPPFLAGS)}}.
|
|---|
| 8106 |
|
|---|
| 8107 | @item Linking a single object file
|
|---|
| 8108 | @cindex linking, predefined rule for
|
|---|
| 8109 | @pindex ld
|
|---|
| 8110 | @pindex .o
|
|---|
| 8111 | @file{@var{n}} is made automatically from @file{@var{n}.o} by running
|
|---|
| 8112 | the linker (usually called @code{ld}) via the C compiler. The precise
|
|---|
| 8113 | command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
|
|---|
| 8114 |
|
|---|
| 8115 | This rule does the right thing for a simple program with only one
|
|---|
| 8116 | source file. It will also do the right thing if there are multiple
|
|---|
| 8117 | object files (presumably coming from various other source files), one
|
|---|
| 8118 | of which has a name matching that of the executable file. Thus,
|
|---|
| 8119 |
|
|---|
| 8120 | @example
|
|---|
| 8121 | x: y.o z.o
|
|---|
| 8122 | @end example
|
|---|
| 8123 |
|
|---|
| 8124 | @noindent
|
|---|
| 8125 | when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
|
|---|
| 8126 |
|
|---|
| 8127 | @example
|
|---|
| 8128 | @group
|
|---|
| 8129 | cc -c x.c -o x.o
|
|---|
| 8130 | cc -c y.c -o y.o
|
|---|
| 8131 | cc -c z.c -o z.o
|
|---|
| 8132 | cc x.o y.o z.o -o x
|
|---|
| 8133 | rm -f x.o
|
|---|
| 8134 | rm -f y.o
|
|---|
| 8135 | rm -f z.o
|
|---|
| 8136 | @end group
|
|---|
| 8137 | @end example
|
|---|
| 8138 |
|
|---|
| 8139 | @noindent
|
|---|
| 8140 | In more complicated cases, such as when there is no object file whose
|
|---|
| 8141 | name derives from the executable file name, you must write an explicit
|
|---|
| 8142 | command for linking.
|
|---|
| 8143 |
|
|---|
| 8144 | Each kind of file automatically made into @samp{.o} object files will
|
|---|
| 8145 | be automatically linked by using the compiler (@samp{$(CC)},
|
|---|
| 8146 | @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
|
|---|
| 8147 | assemble @samp{.s} files) without the @samp{-c} option. This could be
|
|---|
| 8148 | done by using the @samp{.o} object files as intermediates, but it is
|
|---|
| 8149 | faster to do the compiling and linking in one step, so that's how it's
|
|---|
| 8150 | done.@refill
|
|---|
| 8151 |
|
|---|
| 8152 | @item Yacc for C programs
|
|---|
| 8153 | @pindex yacc
|
|---|
| 8154 | @cindex Yacc, rule to run
|
|---|
| 8155 | @pindex .y
|
|---|
| 8156 | @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
|
|---|
| 8157 | running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
|
|---|
| 8158 |
|
|---|
| 8159 | @item Lex for C programs
|
|---|
| 8160 | @pindex lex
|
|---|
| 8161 | @cindex Lex, rule to run
|
|---|
| 8162 | @pindex .l
|
|---|
| 8163 | @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
|
|---|
| 8164 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
|---|
| 8165 |
|
|---|
| 8166 | @item Lex for Ratfor programs
|
|---|
| 8167 | @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
|
|---|
| 8168 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
|---|
| 8169 |
|
|---|
| 8170 | The convention of using the same suffix @samp{.l} for all Lex files
|
|---|
| 8171 | regardless of whether they produce C code or Ratfor code makes it
|
|---|
| 8172 | impossible for @code{make} to determine automatically which of the two
|
|---|
| 8173 | languages you are using in any particular case. If @code{make} is
|
|---|
| 8174 | called upon to remake an object file from a @samp{.l} file, it must
|
|---|
| 8175 | guess which compiler to use. It will guess the C compiler, because
|
|---|
| 8176 | that is more common. If you are using Ratfor, make sure @code{make}
|
|---|
| 8177 | knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
|
|---|
| 8178 | are using Ratfor exclusively, with no C files, remove @samp{.c} from
|
|---|
| 8179 | the list of implicit rule suffixes with:@refill
|
|---|
| 8180 |
|
|---|
| 8181 | @example
|
|---|
| 8182 | @group
|
|---|
| 8183 | .SUFFIXES:
|
|---|
| 8184 | .SUFFIXES: .o .r .f .l @dots{}
|
|---|
| 8185 | @end group
|
|---|
| 8186 | @end example
|
|---|
| 8187 |
|
|---|
| 8188 | @item Making Lint Libraries from C, Yacc, or Lex programs
|
|---|
| 8189 | @pindex lint
|
|---|
| 8190 | @cindex @code{lint}, rule to run
|
|---|
| 8191 | @pindex .ln
|
|---|
| 8192 | @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
|
|---|
| 8193 | The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
|
|---|
| 8194 | The same command is used on the C code produced from
|
|---|
| 8195 | @file{@var{n}.y} or @file{@var{n}.l}.@refill
|
|---|
| 8196 |
|
|---|
| 8197 | @item @TeX{} and Web
|
|---|
| 8198 | @cindex @TeX{}, rule to run
|
|---|
| 8199 | @cindex Web, rule to run
|
|---|
| 8200 | @pindex tex
|
|---|
| 8201 | @pindex cweave
|
|---|
| 8202 | @pindex weave
|
|---|
| 8203 | @pindex tangle
|
|---|
| 8204 | @pindex ctangle
|
|---|
| 8205 | @pindex .dvi
|
|---|
| 8206 | @pindex .tex
|
|---|
| 8207 | @pindex .web
|
|---|
| 8208 | @pindex .w
|
|---|
| 8209 | @pindex .ch
|
|---|
| 8210 | @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
|
|---|
| 8211 | @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
|
|---|
| 8212 | @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
|
|---|
| 8213 | it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
|
|---|
| 8214 | made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
|
|---|
| 8215 | is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
|
|---|
| 8216 | or can be made) with @samp{$(CTANGLE)}.@refill
|
|---|
| 8217 |
|
|---|
| 8218 | @item Texinfo and Info
|
|---|
| 8219 | @cindex Texinfo, rule to format
|
|---|
| 8220 | @cindex Info, rule to format
|
|---|
| 8221 | @pindex texi2dvi
|
|---|
| 8222 | @pindex makeinfo
|
|---|
| 8223 | @pindex .texinfo
|
|---|
| 8224 | @pindex .info
|
|---|
| 8225 | @pindex .texi
|
|---|
| 8226 | @pindex .txinfo
|
|---|
| 8227 | @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
|
|---|
| 8228 | @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
|
|---|
| 8229 | @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
|
|---|
| 8230 | @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
|
|---|
| 8231 | the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
|
|---|
| 8232 |
|
|---|
| 8233 | @item RCS
|
|---|
| 8234 | @cindex RCS, rule to extract from
|
|---|
| 8235 | @pindex co
|
|---|
| 8236 | @pindex ,v @r{(RCS file extension)}
|
|---|
| 8237 | Any file @file{@var{n}} is extracted if necessary from an RCS file
|
|---|
| 8238 | named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
|
|---|
| 8239 | command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
|
|---|
| 8240 | extracted from RCS if it already exists, even if the RCS file is
|
|---|
| 8241 | newer. The rules for RCS are terminal
|
|---|
| 8242 | (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
|---|
| 8243 | so RCS files cannot be generated from another source; they must
|
|---|
| 8244 | actually exist.@refill
|
|---|
| 8245 |
|
|---|
| 8246 | @item SCCS
|
|---|
| 8247 | @cindex SCCS, rule to extract from
|
|---|
| 8248 | @pindex get
|
|---|
| 8249 | @pindex s. @r{(SCCS file prefix)}
|
|---|
| 8250 | Any file @file{@var{n}} is extracted if necessary from an SCCS file
|
|---|
| 8251 | named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
|
|---|
| 8252 | command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
|
|---|
| 8253 | terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
|---|
| 8254 | so SCCS files cannot be generated from another source; they must
|
|---|
| 8255 | actually exist.@refill
|
|---|
| 8256 |
|
|---|
| 8257 | @pindex .sh
|
|---|
| 8258 | For the benefit of SCCS, a file @file{@var{n}} is copied from
|
|---|
| 8259 | @file{@var{n}.sh} and made executable (by everyone). This is for
|
|---|
| 8260 | shell scripts that are checked into SCCS. Since RCS preserves the
|
|---|
| 8261 | execution permission of a file, you do not need to use this feature
|
|---|
| 8262 | with RCS.@refill
|
|---|
| 8263 |
|
|---|
| 8264 | We recommend that you avoid using of SCCS. RCS is widely held to be
|
|---|
| 8265 | superior, and is also free. By choosing free software in place of
|
|---|
| 8266 | comparable (or inferior) proprietary software, you support the free
|
|---|
| 8267 | software movement.
|
|---|
| 8268 | @end table
|
|---|
| 8269 |
|
|---|
| 8270 | Usually, you want to change only the variables listed in the table
|
|---|
| 8271 | above, which are documented in the following section.
|
|---|
| 8272 |
|
|---|
| 8273 | However, the commands in built-in implicit rules actually use
|
|---|
| 8274 | variables such as @code{COMPILE.c}, @code{LINK.p}, and
|
|---|
| 8275 | @code{PREPROCESS.S}, whose values contain the commands listed above.
|
|---|
| 8276 |
|
|---|
| 8277 | @code{make} follows the convention that the rule to compile a
|
|---|
| 8278 | @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
|
|---|
| 8279 | Similarly, the rule to produce an executable from a @file{.@var{x}}
|
|---|
| 8280 | file uses @code{LINK.@var{x}}; and the rule to preprocess a
|
|---|
| 8281 | @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
|
|---|
| 8282 |
|
|---|
| 8283 | @vindex OUTPUT_OPTION
|
|---|
| 8284 | Every rule that produces an object file uses the variable
|
|---|
| 8285 | @code{OUTPUT_OPTION}. @code{make} defines this variable either to
|
|---|
| 8286 | contain @samp{-o $@@}, or to be empty, depending on a compile-time
|
|---|
| 8287 | option. You need the @samp{-o} option to ensure that the output goes
|
|---|
| 8288 | into the right file when the source file is in a different directory,
|
|---|
| 8289 | as when using @code{VPATH} (@pxref{Directory Search}). However,
|
|---|
| 8290 | compilers on some systems do not accept a @samp{-o} switch for object
|
|---|
| 8291 | files. If you use such a system, and use @code{VPATH}, some
|
|---|
| 8292 | compilations will put their output in the wrong place.
|
|---|
| 8293 | A possible workaround for this problem is to give @code{OUTPUT_OPTION}
|
|---|
| 8294 | the value @w{@samp{; mv $*.o $@@}}.
|
|---|
| 8295 |
|
|---|
| 8296 | @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
|
|---|
| 8297 | @section Variables Used by Implicit Rules
|
|---|
| 8298 | @cindex flags for compilers
|
|---|
| 8299 |
|
|---|
| 8300 | The commands in built-in implicit rules make liberal use of certain
|
|---|
| 8301 | predefined variables. You can alter these variables in the makefile,
|
|---|
| 8302 | with arguments to @code{make}, or in the environment to alter how the
|
|---|
| 8303 | implicit rules work without redefining the rules themselves. You can
|
|---|
| 8304 | cancel all variables used by implicit rules with the @samp{-R} or
|
|---|
| 8305 | @samp{--no-builtin-variables} option.
|
|---|
| 8306 |
|
|---|
| 8307 | For example, the command used to compile a C source file actually says
|
|---|
| 8308 | @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
|
|---|
| 8309 | used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
|
|---|
| 8310 | redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
|
|---|
| 8311 | used for all C compilations performed by the implicit rule. By redefining
|
|---|
| 8312 | @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
|
|---|
| 8313 | each compilation. @emph{All} implicit rules that do C compilation use
|
|---|
| 8314 | @samp{$(CC)} to get the program name for the compiler and @emph{all}
|
|---|
| 8315 | include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
|
|---|
| 8316 |
|
|---|
| 8317 | The variables used in implicit rules fall into two classes: those that are
|
|---|
| 8318 | names of programs (like @code{CC}) and those that contain arguments for the
|
|---|
| 8319 | programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
|
|---|
| 8320 | some command arguments, but it must start with an actual executable program
|
|---|
| 8321 | name.) If a variable value contains more than one argument, separate them
|
|---|
| 8322 | with spaces.
|
|---|
| 8323 |
|
|---|
| 8324 | Here is a table of variables used as names of programs in built-in rules:
|
|---|
| 8325 |
|
|---|
| 8326 | @table @code
|
|---|
| 8327 | @item AR
|
|---|
| 8328 | @vindex AR
|
|---|
| 8329 | Archive-maintaining program; default @samp{ar}.
|
|---|
| 8330 | @pindex ar
|
|---|
| 8331 |
|
|---|
| 8332 | @item AS
|
|---|
| 8333 | @vindex AS
|
|---|
| 8334 | Program for doing assembly; default @samp{as}.
|
|---|
| 8335 | @pindex as
|
|---|
| 8336 |
|
|---|
| 8337 | @item CC
|
|---|
| 8338 | @vindex CC
|
|---|
| 8339 | Program for compiling C programs; default @samp{cc}.
|
|---|
| 8340 | @pindex cc
|
|---|
| 8341 |
|
|---|
| 8342 | @item CXX
|
|---|
| 8343 | @vindex CXX
|
|---|
| 8344 | Program for compiling C++ programs; default @samp{g++}.
|
|---|
| 8345 | @pindex g++
|
|---|
| 8346 |
|
|---|
| 8347 | @item CO
|
|---|
| 8348 | @vindex CO
|
|---|
| 8349 | Program for extracting a file from RCS; default @samp{co}.
|
|---|
| 8350 | @pindex co
|
|---|
| 8351 |
|
|---|
| 8352 | @item CPP
|
|---|
| 8353 | @vindex CPP
|
|---|
| 8354 | Program for running the C preprocessor, with results to standard output;
|
|---|
| 8355 | default @samp{$(CC) -E}.
|
|---|
| 8356 |
|
|---|
| 8357 | @item FC
|
|---|
| 8358 | @vindex FC
|
|---|
| 8359 | Program for compiling or preprocessing Fortran and Ratfor programs;
|
|---|
| 8360 | default @samp{f77}.
|
|---|
| 8361 | @pindex f77
|
|---|
| 8362 |
|
|---|
| 8363 | @item GET
|
|---|
| 8364 | @vindex GET
|
|---|
| 8365 | Program for extracting a file from SCCS; default @samp{get}.
|
|---|
| 8366 | @pindex get
|
|---|
| 8367 |
|
|---|
| 8368 | @item LEX
|
|---|
| 8369 | @vindex LEX
|
|---|
| 8370 | Program to use to turn Lex grammars into C programs or Ratfor programs;
|
|---|
| 8371 | default @samp{lex}.
|
|---|
| 8372 | @pindex lex
|
|---|
| 8373 |
|
|---|
| 8374 | @item PC
|
|---|
| 8375 | @vindex PC
|
|---|
| 8376 | Program for compiling Pascal programs; default @samp{pc}.
|
|---|
| 8377 | @pindex pc
|
|---|
| 8378 |
|
|---|
| 8379 | @item YACC
|
|---|
| 8380 | @vindex YACC
|
|---|
| 8381 | Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
|
|---|
| 8382 | @pindex yacc
|
|---|
| 8383 |
|
|---|
| 8384 | @item YACCR
|
|---|
| 8385 | @vindex YACCR
|
|---|
| 8386 | Program to use to turn Yacc grammars into Ratfor
|
|---|
| 8387 | programs; default @samp{yacc -r}.
|
|---|
| 8388 |
|
|---|
| 8389 | @item MAKEINFO
|
|---|
| 8390 | @vindex MAKEINFO
|
|---|
| 8391 | Program to convert a Texinfo source file into an Info file; default
|
|---|
| 8392 | @samp{makeinfo}.
|
|---|
| 8393 | @pindex makeinfo
|
|---|
| 8394 |
|
|---|
| 8395 | @item TEX
|
|---|
| 8396 | @vindex TEX
|
|---|
| 8397 | Program to make @TeX{} @sc{dvi} files from @TeX{} source;
|
|---|
| 8398 | default @samp{tex}.
|
|---|
| 8399 | @pindex tex
|
|---|
| 8400 |
|
|---|
| 8401 | @item TEXI2DVI
|
|---|
| 8402 | @vindex TEXI2DVI
|
|---|
| 8403 | Program to make @TeX{} @sc{dvi} files from Texinfo source;
|
|---|
| 8404 | default @samp{texi2dvi}.
|
|---|
| 8405 | @pindex texi2dvi
|
|---|
| 8406 |
|
|---|
| 8407 | @item WEAVE
|
|---|
| 8408 | @vindex WEAVE
|
|---|
| 8409 | Program to translate Web into @TeX{}; default @samp{weave}.
|
|---|
| 8410 | @pindex weave
|
|---|
| 8411 |
|
|---|
| 8412 | @item CWEAVE
|
|---|
| 8413 | @vindex CWEAVE
|
|---|
| 8414 | Program to translate C Web into @TeX{}; default @samp{cweave}.
|
|---|
| 8415 | @pindex cweave
|
|---|
| 8416 |
|
|---|
| 8417 | @item TANGLE
|
|---|
| 8418 | @vindex TANGLE
|
|---|
| 8419 | Program to translate Web into Pascal; default @samp{tangle}.
|
|---|
| 8420 | @pindex tangle
|
|---|
| 8421 |
|
|---|
| 8422 | @item CTANGLE
|
|---|
| 8423 | @vindex CTANGLE
|
|---|
| 8424 | Program to translate C Web into C; default @samp{ctangle}.
|
|---|
| 8425 | @pindex ctangle
|
|---|
| 8426 |
|
|---|
| 8427 | @item RM
|
|---|
| 8428 | @vindex RM
|
|---|
| 8429 | Command to remove a file; default @samp{rm -f}.
|
|---|
| 8430 | @pindex rm
|
|---|
| 8431 | @end table
|
|---|
| 8432 |
|
|---|
| 8433 | Here is a table of variables whose values are additional arguments for the
|
|---|
| 8434 | programs above. The default values for all of these is the empty
|
|---|
| 8435 | string, unless otherwise noted.
|
|---|
| 8436 |
|
|---|
| 8437 | @table @code
|
|---|
| 8438 | @item ARFLAGS
|
|---|
| 8439 | @vindex ARFLAGS
|
|---|
| 8440 | Flags to give the archive-maintaining program; default @samp{rv}.
|
|---|
| 8441 |
|
|---|
| 8442 | @item ASFLAGS
|
|---|
| 8443 | @vindex ASFLAGS
|
|---|
| 8444 | Extra flags to give to the assembler (when explicitly
|
|---|
| 8445 | invoked on a @samp{.s} or @samp{.S} file).
|
|---|
| 8446 |
|
|---|
| 8447 | @item CFLAGS
|
|---|
| 8448 | @vindex CFLAGS
|
|---|
| 8449 | Extra flags to give to the C compiler.
|
|---|
| 8450 |
|
|---|
| 8451 | @item CXXFLAGS
|
|---|
| 8452 | @vindex CXXFLAGS
|
|---|
| 8453 | Extra flags to give to the C++ compiler.
|
|---|
| 8454 |
|
|---|
| 8455 | @item COFLAGS
|
|---|
| 8456 | @vindex COFLAGS
|
|---|
| 8457 | Extra flags to give to the RCS @code{co} program.
|
|---|
| 8458 |
|
|---|
| 8459 | @item CPPFLAGS
|
|---|
| 8460 | @vindex CPPFLAGS
|
|---|
| 8461 | Extra flags to give to the C preprocessor and programs
|
|---|
| 8462 | that use it (the C and Fortran compilers).
|
|---|
| 8463 |
|
|---|
| 8464 | @item FFLAGS
|
|---|
| 8465 | @vindex FFLAGS
|
|---|
| 8466 | Extra flags to give to the Fortran compiler.
|
|---|
| 8467 |
|
|---|
| 8468 | @item GFLAGS
|
|---|
| 8469 | @vindex GFLAGS
|
|---|
| 8470 | Extra flags to give to the SCCS @code{get} program.
|
|---|
| 8471 |
|
|---|
| 8472 | @item LDFLAGS
|
|---|
| 8473 | @vindex LDFLAGS
|
|---|
| 8474 | Extra flags to give to compilers when they are
|
|---|
| 8475 | supposed to invoke the linker, @samp{ld}.
|
|---|
| 8476 |
|
|---|
| 8477 | @item LFLAGS
|
|---|
| 8478 | @vindex LFLAGS
|
|---|
| 8479 | Extra flags to give to Lex.
|
|---|
| 8480 |
|
|---|
| 8481 | @item PFLAGS
|
|---|
| 8482 | @vindex PFLAGS
|
|---|
| 8483 | Extra flags to give to the Pascal compiler.
|
|---|
| 8484 |
|
|---|
| 8485 | @item RFLAGS
|
|---|
| 8486 | @vindex RFLAGS
|
|---|
| 8487 | Extra flags to give to the Fortran compiler for Ratfor programs.
|
|---|
| 8488 |
|
|---|
| 8489 | @item YFLAGS
|
|---|
| 8490 | @vindex YFLAGS
|
|---|
| 8491 | Extra flags to give to Yacc.
|
|---|
| 8492 | @end table
|
|---|
| 8493 |
|
|---|
| 8494 | @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
|
|---|
| 8495 | @section Chains of Implicit Rules
|
|---|
| 8496 |
|
|---|
| 8497 | @cindex chains of rules
|
|---|
| 8498 | @cindex rule, implicit, chains of
|
|---|
| 8499 | Sometimes a file can be made by a sequence of implicit rules. For example,
|
|---|
| 8500 | a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
|
|---|
| 8501 | first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
|
|---|
| 8502 |
|
|---|
| 8503 | If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
|
|---|
| 8504 | special searching is required: @code{make} finds that the object file can
|
|---|
| 8505 | be made by C compilation from @file{@var{n}.c}; later on, when considering
|
|---|
| 8506 | how to make @file{@var{n}.c}, the rule for running Yacc is
|
|---|
| 8507 | used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
|
|---|
| 8508 | updated.@refill
|
|---|
| 8509 |
|
|---|
| 8510 | @cindex intermediate files
|
|---|
| 8511 | @cindex files, intermediate
|
|---|
| 8512 | However, even if @file{@var{n}.c} does not exist and is not mentioned,
|
|---|
| 8513 | @code{make} knows how to envision it as the missing link between
|
|---|
| 8514 | @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
|
|---|
| 8515 | called an @dfn{intermediate file}. Once @code{make} has decided to use the
|
|---|
| 8516 | intermediate file, it is entered in the data base as if it had been
|
|---|
| 8517 | mentioned in the makefile, along with the implicit rule that says how to
|
|---|
| 8518 | create it.@refill
|
|---|
| 8519 |
|
|---|
| 8520 | Intermediate files are remade using their rules just like all other
|
|---|
| 8521 | files. But intermediate files are treated differently in two ways.
|
|---|
| 8522 |
|
|---|
| 8523 | The first difference is what happens if the intermediate file does not
|
|---|
| 8524 | exist. If an ordinary file @var{b} does not exist, and @code{make}
|
|---|
| 8525 | considers a target that depends on @var{b}, it invariably creates
|
|---|
| 8526 | @var{b} and then updates the target from @var{b}. But if @var{b} is an
|
|---|
| 8527 | intermediate file, then @code{make} can leave well enough alone. It
|
|---|
| 8528 | won't bother updating @var{b}, or the ultimate target, unless some
|
|---|
| 8529 | prerequisite of @var{b} is newer than that target or there is some other
|
|---|
| 8530 | reason to update that target.
|
|---|
| 8531 |
|
|---|
| 8532 | The second difference is that if @code{make} @emph{does} create @var{b}
|
|---|
| 8533 | in order to update something else, it deletes @var{b} later on after it
|
|---|
| 8534 | is no longer needed. Therefore, an intermediate file which did not
|
|---|
| 8535 | exist before @code{make} also does not exist after @code{make}.
|
|---|
| 8536 | @code{make} reports the deletion to you by printing a @samp{rm -f}
|
|---|
| 8537 | command showing which file it is deleting.
|
|---|
| 8538 |
|
|---|
| 8539 | Ordinarily, a file cannot be intermediate if it is mentioned in the
|
|---|
| 8540 | makefile as a target or prerequisite. However, you can explicitly mark a
|
|---|
| 8541 | file as intermediate by listing it as a prerequisite of the special target
|
|---|
| 8542 | @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
|
|---|
| 8543 | explicitly in some other way.
|
|---|
| 8544 |
|
|---|
| 8545 | @cindex intermediate files, preserving
|
|---|
| 8546 | @cindex preserving intermediate files
|
|---|
| 8547 | @cindex secondary files
|
|---|
| 8548 | You can prevent automatic deletion of an intermediate file by marking it
|
|---|
| 8549 | as a @dfn{secondary} file. To do this, list it as a prerequisite of the
|
|---|
| 8550 | special target @code{.SECONDARY}. When a file is secondary, @code{make}
|
|---|
| 8551 | will not create the file merely because it does not already exist, but
|
|---|
| 8552 | @code{make} does not automatically delete the file. Marking a file as
|
|---|
| 8553 | secondary also marks it as intermediate.
|
|---|
| 8554 |
|
|---|
| 8555 | You can list the target pattern of an implicit rule (such as @samp{%.o})
|
|---|
| 8556 | as a prerequisite of the special target @code{.PRECIOUS} to preserve
|
|---|
| 8557 | intermediate files made by implicit rules whose target patterns match
|
|---|
| 8558 | that file's name; see @ref{Interrupts}.@refill
|
|---|
| 8559 | @cindex preserving with @code{.PRECIOUS}
|
|---|
| 8560 | @cindex @code{.PRECIOUS} intermediate files
|
|---|
| 8561 |
|
|---|
| 8562 | A chain can involve more than two implicit rules. For example, it is
|
|---|
| 8563 | possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
|
|---|
| 8564 | Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
|
|---|
| 8565 | intermediate files that are deleted at the end.@refill
|
|---|
| 8566 |
|
|---|
| 8567 | No single implicit rule can appear more than once in a chain. This means
|
|---|
| 8568 | that @code{make} will not even consider such a ridiculous thing as making
|
|---|
| 8569 | @file{foo} from @file{foo.o.o} by running the linker twice. This
|
|---|
| 8570 | constraint has the added benefit of preventing any infinite loop in the
|
|---|
| 8571 | search for an implicit rule chain.
|
|---|
| 8572 |
|
|---|
| 8573 | There are some special implicit rules to optimize certain cases that would
|
|---|
| 8574 | otherwise be handled by rule chains. For example, making @file{foo} from
|
|---|
| 8575 | @file{foo.c} could be handled by compiling and linking with separate
|
|---|
| 8576 | chained rules, using @file{foo.o} as an intermediate file. But what
|
|---|
| 8577 | actually happens is that a special rule for this case does the compilation
|
|---|
| 8578 | and linking with a single @code{cc} command. The optimized rule is used in
|
|---|
| 8579 | preference to the step-by-step chain because it comes earlier in the
|
|---|
| 8580 | ordering of rules.
|
|---|
| 8581 |
|
|---|
| 8582 | @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
|
|---|
| 8583 | @section Defining and Redefining Pattern Rules
|
|---|
| 8584 |
|
|---|
| 8585 | You define an implicit rule by writing a @dfn{pattern rule}. A pattern
|
|---|
| 8586 | rule looks like an ordinary rule, except that its target contains the
|
|---|
| 8587 | character @samp{%} (exactly one of them). The target is considered a
|
|---|
| 8588 | pattern for matching file names; the @samp{%} can match any nonempty
|
|---|
| 8589 | substring, while other characters match only themselves. The prerequisites
|
|---|
| 8590 | likewise use @samp{%} to show how their names relate to the target name.
|
|---|
| 8591 |
|
|---|
| 8592 | Thus, a pattern rule @samp{%.o : %.c} says how to make any file
|
|---|
| 8593 | @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
|
|---|
| 8594 |
|
|---|
| 8595 | Note that expansion using @samp{%} in pattern rules occurs
|
|---|
| 8596 | @strong{after} any variable or function expansions, which take place
|
|---|
| 8597 | when the makefile is read. @xref{Using Variables, , How to Use
|
|---|
| 8598 | Variables}, and @ref{Functions, ,Functions for Transforming Text}.
|
|---|
| 8599 |
|
|---|
| 8600 | @menu
|
|---|
| 8601 | * Pattern Intro:: An introduction to pattern rules.
|
|---|
| 8602 | * Pattern Examples:: Examples of pattern rules.
|
|---|
| 8603 | * Automatic Variables:: How to use automatic variables in the
|
|---|
| 8604 | commands of implicit rules.
|
|---|
| 8605 | * Pattern Match:: How patterns match.
|
|---|
| 8606 | * Match-Anything Rules:: Precautions you should take prior to
|
|---|
| 8607 | defining rules that can match any
|
|---|
| 8608 | target file whatever.
|
|---|
| 8609 | * Canceling Rules:: How to override or cancel built-in rules.
|
|---|
| 8610 | @end menu
|
|---|
| 8611 |
|
|---|
| 8612 | @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
|
|---|
| 8613 | @subsection Introduction to Pattern Rules
|
|---|
| 8614 | @cindex pattern rule
|
|---|
| 8615 | @cindex rule, pattern
|
|---|
| 8616 |
|
|---|
| 8617 | A pattern rule contains the character @samp{%} (exactly one of them)
|
|---|
| 8618 | in the target; otherwise, it looks exactly like an ordinary rule. The
|
|---|
| 8619 | target is a pattern for matching file names; the @samp{%} matches any
|
|---|
| 8620 | nonempty substring, while other characters match only themselves.
|
|---|
| 8621 | @cindex target pattern, implicit
|
|---|
| 8622 | @cindex @code{%}, in pattern rules
|
|---|
| 8623 |
|
|---|
| 8624 | For example, @samp{%.c} as a pattern matches any file name that ends in
|
|---|
| 8625 | @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
|
|---|
| 8626 | with @samp{s.}, ends in @samp{.c} and is at least five characters long.
|
|---|
| 8627 | (There must be at least one character to match the @samp{%}.) The substring
|
|---|
| 8628 | that the @samp{%} matches is called the @dfn{stem}.@refill
|
|---|
| 8629 |
|
|---|
| 8630 | @samp{%} in a prerequisite of a pattern rule stands for the same stem
|
|---|
| 8631 | that was matched by the @samp{%} in the target. In order for
|
|---|
| 8632 | the pattern rule to apply, its target pattern must match the file name
|
|---|
| 8633 | under consideration, and its prerequisite patterns must name files that
|
|---|
| 8634 | exist or can be made. These files become prerequisites of the target.
|
|---|
| 8635 | @cindex prerequisite pattern, implicit
|
|---|
| 8636 |
|
|---|
| 8637 | Thus, a rule of the form
|
|---|
| 8638 |
|
|---|
| 8639 | @example
|
|---|
| 8640 | %.o : %.c ; @var{command}@dots{}
|
|---|
| 8641 | @end example
|
|---|
| 8642 |
|
|---|
| 8643 | @noindent
|
|---|
| 8644 | specifies how to make a file @file{@var{n}.o}, with another file
|
|---|
| 8645 | @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
|
|---|
| 8646 | exists or can be made.
|
|---|
| 8647 |
|
|---|
| 8648 | There may also be prerequisites that do not use @samp{%}; such a prerequisite
|
|---|
| 8649 | attaches to every file made by this pattern rule. These unvarying
|
|---|
| 8650 | prerequisites are useful occasionally.
|
|---|
| 8651 |
|
|---|
| 8652 | A pattern rule need not have any prerequisites that contain @samp{%}, or
|
|---|
| 8653 | in fact any prerequisites at all. Such a rule is effectively a general
|
|---|
| 8654 | wildcard. It provides a way to make any file that matches the target
|
|---|
| 8655 | pattern. @xref{Last Resort}.
|
|---|
| 8656 |
|
|---|
| 8657 | @c !!! The end of of this paragraph should be rewritten. --bob
|
|---|
| 8658 | Pattern rules may have more than one target. Unlike normal rules, this
|
|---|
| 8659 | does not act as many different rules with the same prerequisites and
|
|---|
| 8660 | commands. If a pattern rule has multiple targets, @code{make} knows that
|
|---|
| 8661 | the rule's commands are responsible for making all of the targets. The
|
|---|
| 8662 | commands are executed only once to make all the targets. When searching
|
|---|
| 8663 | for a pattern rule to match a target, the target patterns of a rule other
|
|---|
| 8664 | than the one that matches the target in need of a rule are incidental:
|
|---|
| 8665 | @code{make} worries only about giving commands and prerequisites to the file
|
|---|
| 8666 | presently in question. However, when this file's commands are run, the
|
|---|
| 8667 | other targets are marked as having been updated themselves.
|
|---|
| 8668 | @cindex multiple targets, in pattern rule
|
|---|
| 8669 | @cindex target, multiple in pattern rule
|
|---|
| 8670 |
|
|---|
| 8671 | The order in which pattern rules appear in the makefile is important
|
|---|
| 8672 | since this is the order in which they are considered.
|
|---|
| 8673 | Of equally applicable
|
|---|
| 8674 | rules, only the first one found is used. The rules you write take precedence
|
|---|
| 8675 | over those that are built in. Note however, that a rule whose
|
|---|
| 8676 | prerequisites actually exist or are mentioned always takes priority over a
|
|---|
| 8677 | rule with prerequisites that must be made by chaining other implicit rules.
|
|---|
| 8678 | @cindex pattern rules, order of
|
|---|
| 8679 | @cindex order of pattern rules
|
|---|
| 8680 |
|
|---|
| 8681 | @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
|
|---|
| 8682 | @subsection Pattern Rule Examples
|
|---|
| 8683 |
|
|---|
| 8684 | Here are some examples of pattern rules actually predefined in
|
|---|
| 8685 | @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
|
|---|
| 8686 | files:@refill
|
|---|
| 8687 |
|
|---|
| 8688 | @example
|
|---|
| 8689 | %.o : %.c
|
|---|
| 8690 | $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
|
|---|
| 8691 | @end example
|
|---|
| 8692 |
|
|---|
| 8693 | @noindent
|
|---|
| 8694 | defines a rule that can make any file @file{@var{x}.o} from
|
|---|
| 8695 | @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
|
|---|
| 8696 | @samp{$<} to substitute the names of the target file and the source file
|
|---|
| 8697 | in each case where the rule applies (@pxref{Automatic Variables}).@refill
|
|---|
| 8698 |
|
|---|
| 8699 | Here is a second built-in rule:
|
|---|
| 8700 |
|
|---|
| 8701 | @example
|
|---|
| 8702 | % :: RCS/%,v
|
|---|
| 8703 | $(CO) $(COFLAGS) $<
|
|---|
| 8704 | @end example
|
|---|
| 8705 |
|
|---|
| 8706 | @noindent
|
|---|
| 8707 | defines a rule that can make any file @file{@var{x}} whatsoever from a
|
|---|
| 8708 | corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
|
|---|
| 8709 | the target is @samp{%}, this rule will apply to any file whatever, provided
|
|---|
| 8710 | the appropriate prerequisite file exists. The double colon makes the rule
|
|---|
| 8711 | @dfn{terminal}, which means that its prerequisite may not be an intermediate
|
|---|
| 8712 | file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
|
|---|
| 8713 |
|
|---|
| 8714 | @need 500
|
|---|
| 8715 | This pattern rule has two targets:
|
|---|
| 8716 |
|
|---|
| 8717 | @example
|
|---|
| 8718 | @group
|
|---|
| 8719 | %.tab.c %.tab.h: %.y
|
|---|
| 8720 | bison -d $<
|
|---|
| 8721 | @end group
|
|---|
| 8722 | @end example
|
|---|
| 8723 |
|
|---|
| 8724 | @noindent
|
|---|
| 8725 | @c The following paragraph is rewritten to avoid overfull hboxes
|
|---|
| 8726 | This tells @code{make} that the command @samp{bison -d @var{x}.y} will
|
|---|
| 8727 | make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
|
|---|
| 8728 | @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
|
|---|
| 8729 | and the file @file{scan.o} depends on the file @file{parse.tab.h},
|
|---|
| 8730 | when @file{parse.y} is changed, the command @samp{bison -d parse.y}
|
|---|
| 8731 | will be executed only once, and the prerequisites of both
|
|---|
| 8732 | @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
|
|---|
| 8733 | the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
|
|---|
| 8734 | and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
|
|---|
| 8735 | linked from @file{parse.tab.o}, @file{scan.o}, and its other
|
|---|
| 8736 | prerequisites, and it will execute happily ever after.)@refill
|
|---|
| 8737 |
|
|---|
| 8738 | @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
|
|---|
| 8739 | @subsection Automatic Variables
|
|---|
| 8740 | @cindex automatic variables
|
|---|
| 8741 | @cindex variables, automatic
|
|---|
| 8742 | @cindex variables, and implicit rule
|
|---|
| 8743 |
|
|---|
| 8744 | Suppose you are writing a pattern rule to compile a @samp{.c} file into a
|
|---|
| 8745 | @samp{.o} file: how do you write the @samp{cc} command so that it operates
|
|---|
| 8746 | on the right source file name? You cannot write the name in the command,
|
|---|
| 8747 | because the name is different each time the implicit rule is applied.
|
|---|
| 8748 |
|
|---|
| 8749 | What you do is use a special feature of @code{make}, the @dfn{automatic
|
|---|
| 8750 | variables}. These variables have values computed afresh for each rule that
|
|---|
| 8751 | is executed, based on the target and prerequisites of the rule. In this
|
|---|
| 8752 | example, you would use @samp{$@@} for the object file name and @samp{$<}
|
|---|
| 8753 | for the source file name.
|
|---|
| 8754 |
|
|---|
| 8755 | @cindex automatic variables in prerequisites
|
|---|
| 8756 | @cindex prerequisites, and automatic variables
|
|---|
| 8757 | It's very important that you recognize the limited scope in which
|
|---|
| 8758 | automatic variable values are available: they only have values within
|
|---|
| 8759 | the command script. In particular, you cannot use them anywhere
|
|---|
| 8760 | within the target list of a rule; they have no value there and will
|
|---|
| 8761 | expand to the empty string. Also, they cannot be accessed directly
|
|---|
| 8762 | within the prerequisite list of a rule. A common mistake is
|
|---|
| 8763 | attempting to use @code{$@@} within the prerequisites list; this will
|
|---|
| 8764 | not work. However, there is a special feature of GNU @code{make},
|
|---|
| 8765 | secondary expansion (@pxref{Secondary Expansion}), which will allow
|
|---|
| 8766 | automatic variable values to be used in prerequisite lists.
|
|---|
| 8767 |
|
|---|
| 8768 | Here is a table of automatic variables:
|
|---|
| 8769 |
|
|---|
| 8770 | @table @code
|
|---|
| 8771 | @vindex $@@
|
|---|
| 8772 | @vindex @@ @r{(automatic variable)}
|
|---|
| 8773 | @item $@@
|
|---|
| 8774 | The file name of the target of the rule. If the target is an archive
|
|---|
| 8775 | member, then @samp{$@@} is the name of the archive file. In a pattern
|
|---|
| 8776 | rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
|
|---|
| 8777 | Pattern Rules}), @samp{$@@} is the name of whichever target caused the
|
|---|
| 8778 | rule's commands to be run.
|
|---|
| 8779 |
|
|---|
| 8780 | @vindex $%
|
|---|
| 8781 | @vindex % @r{(automatic variable)}
|
|---|
| 8782 | @item $%
|
|---|
| 8783 | The target member name, when the target is an archive member.
|
|---|
| 8784 | @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
|
|---|
| 8785 | @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
|
|---|
| 8786 | empty when the target is not an archive member.
|
|---|
| 8787 |
|
|---|
| 8788 | @vindex $<
|
|---|
| 8789 | @vindex < @r{(automatic variable)}
|
|---|
| 8790 | @item $<
|
|---|
| 8791 | The name of the first prerequisite. If the target got its commands from
|
|---|
| 8792 | an implicit rule, this will be the first prerequisite added by the
|
|---|
| 8793 | implicit rule (@pxref{Implicit Rules}).
|
|---|
| 8794 |
|
|---|
| 8795 | @vindex $?
|
|---|
| 8796 | @vindex ? @r{(automatic variable)}
|
|---|
| 8797 | @item $?
|
|---|
| 8798 | The names of all the prerequisites that are newer than the target, with
|
|---|
| 8799 | spaces between them. For prerequisites which are archive members, only
|
|---|
| 8800 | the member named is used (@pxref{Archives}).
|
|---|
| 8801 | @cindex prerequisites, list of changed
|
|---|
| 8802 | @cindex list of changed prerequisites
|
|---|
| 8803 |
|
|---|
| 8804 | @vindex $^
|
|---|
| 8805 | @vindex ^ @r{(automatic variable)}
|
|---|
| 8806 | @item $^
|
|---|
| 8807 | The names of all the prerequisites, with spaces between them. For
|
|---|
| 8808 | prerequisites which are archive members, only the member named is used
|
|---|
| 8809 | (@pxref{Archives}). A target has only one prerequisite on each other file
|
|---|
| 8810 | it depends on, no matter how many times each file is listed as a
|
|---|
| 8811 | prerequisite. So if you list a prerequisite more than once for a target,
|
|---|
| 8812 | the value of @code{$^} contains just one copy of the name. This list
|
|---|
| 8813 | does @strong{not} contain any of the order-only prerequisites; for those
|
|---|
| 8814 | see the @samp{$|} variable, below.
|
|---|
| 8815 | @cindex prerequisites, list of all
|
|---|
| 8816 | @cindex list of all prerequisites
|
|---|
| 8817 |
|
|---|
| 8818 | @vindex $+
|
|---|
| 8819 | @vindex + @r{(automatic variable)}
|
|---|
| 8820 | @item $+
|
|---|
| 8821 | This is like @samp{$^}, but prerequisites listed more than once are
|
|---|
| 8822 | duplicated in the order they were listed in the makefile. This is
|
|---|
| 8823 | primarily useful for use in linking commands where it is meaningful to
|
|---|
| 8824 | repeat library file names in a particular order.
|
|---|
| 8825 |
|
|---|
| 8826 | @vindex $|
|
|---|
| 8827 | @vindex | @r{(automatic variable)}
|
|---|
| 8828 | @item $|
|
|---|
| 8829 | The names of all the order-only prerequisites, with spaces between
|
|---|
| 8830 | them.
|
|---|
| 8831 |
|
|---|
| 8832 | @vindex $*
|
|---|
| 8833 | @vindex * @r{(automatic variable)}
|
|---|
| 8834 | @item $*
|
|---|
| 8835 | The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
|
|---|
| 8836 | Patterns Match}). If the target is @file{dir/a.foo.b} and the target
|
|---|
| 8837 | pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
|
|---|
| 8838 | useful for constructing names of related files.@refill
|
|---|
| 8839 | @cindex stem, variable for
|
|---|
| 8840 |
|
|---|
| 8841 | In a static pattern rule, the stem is part of the file name that matched
|
|---|
| 8842 | the @samp{%} in the target pattern.
|
|---|
| 8843 |
|
|---|
| 8844 | In an explicit rule, there is no stem; so @samp{$*} cannot be determined
|
|---|
| 8845 | in that way. Instead, if the target name ends with a recognized suffix
|
|---|
| 8846 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
|
|---|
| 8847 | the target name minus the suffix. For example, if the target name is
|
|---|
| 8848 | @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
|
|---|
| 8849 | suffix. GNU @code{make} does this bizarre thing only for compatibility
|
|---|
| 8850 | with other implementations of @code{make}. You should generally avoid
|
|---|
| 8851 | using @samp{$*} except in implicit rules or static pattern rules.@refill
|
|---|
| 8852 |
|
|---|
| 8853 | If the target name in an explicit rule does not end with a recognized
|
|---|
| 8854 | suffix, @samp{$*} is set to the empty string for that rule.
|
|---|
| 8855 | @end table
|
|---|
| 8856 |
|
|---|
| 8857 | @samp{$?} is useful even in explicit rules when you wish to operate on only
|
|---|
| 8858 | the prerequisites that have changed. For example, suppose that an archive
|
|---|
| 8859 | named @file{lib} is supposed to contain copies of several object files.
|
|---|
| 8860 | This rule copies just the changed object files into the archive:
|
|---|
| 8861 |
|
|---|
| 8862 | @example
|
|---|
| 8863 | @group
|
|---|
| 8864 | lib: foo.o bar.o lose.o win.o
|
|---|
| 8865 | ar r lib $?
|
|---|
| 8866 | @end group
|
|---|
| 8867 | @end example
|
|---|
| 8868 |
|
|---|
| 8869 | Of the variables listed above, four have values that are single file
|
|---|
| 8870 | names, and three have values that are lists of file names. These seven
|
|---|
| 8871 | have variants that get just the file's directory name or just the file
|
|---|
| 8872 | name within the directory. The variant variables' names are formed by
|
|---|
| 8873 | appending @samp{D} or @samp{F}, respectively. These variants are
|
|---|
| 8874 | semi-obsolete in GNU @code{make} since the functions @code{dir} and
|
|---|
| 8875 | @code{notdir} can be used to get a similar effect (@pxref{File Name
|
|---|
| 8876 | Functions, , Functions for File Names}). Note, however, that the
|
|---|
| 8877 | @samp{D} variants all omit the trailing slash which always appears in
|
|---|
| 8878 | the output of the @code{dir} function. Here is a table of the variants:
|
|---|
| 8879 |
|
|---|
| 8880 | @table @samp
|
|---|
| 8881 | @vindex $(@@D)
|
|---|
| 8882 | @vindex @@D @r{(automatic variable)}
|
|---|
| 8883 | @item $(@@D)
|
|---|
| 8884 | The directory part of the file name of the target, with the trailing
|
|---|
| 8885 | slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
|
|---|
| 8886 | @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
|
|---|
| 8887 | not contain a slash.
|
|---|
| 8888 |
|
|---|
| 8889 | @vindex $(@@F)
|
|---|
| 8890 | @vindex @@F @r{(automatic variable)}
|
|---|
| 8891 | @item $(@@F)
|
|---|
| 8892 | The file-within-directory part of the file name of the target. If the
|
|---|
| 8893 | value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
|
|---|
| 8894 | @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
|
|---|
| 8895 |
|
|---|
| 8896 | @vindex $(*D)
|
|---|
| 8897 | @vindex *D @r{(automatic variable)}
|
|---|
| 8898 | @item $(*D)
|
|---|
| 8899 | @vindex $(*F)
|
|---|
| 8900 | @vindex *F @r{(automatic variable)}
|
|---|
| 8901 | @itemx $(*F)
|
|---|
| 8902 | The directory part and the file-within-directory
|
|---|
| 8903 | part of the stem; @file{dir} and @file{foo} in this example.
|
|---|
| 8904 |
|
|---|
| 8905 | @vindex $(%D)
|
|---|
| 8906 | @vindex %D @r{(automatic variable)}
|
|---|
| 8907 | @item $(%D)
|
|---|
| 8908 | @vindex $(%F)
|
|---|
| 8909 | @vindex %F @r{(automatic variable)}
|
|---|
| 8910 | @itemx $(%F)
|
|---|
| 8911 | The directory part and the file-within-directory part of the target
|
|---|
| 8912 | archive member name. This makes sense only for archive member targets
|
|---|
| 8913 | of the form @file{@var{archive}(@var{member})} and is useful only when
|
|---|
| 8914 | @var{member} may contain a directory name. (@xref{Archive Members,
|
|---|
| 8915 | ,Archive Members as Targets}.)
|
|---|
| 8916 |
|
|---|
| 8917 | @vindex $(<D)
|
|---|
| 8918 | @vindex <D @r{(automatic variable)}
|
|---|
| 8919 | @item $(<D)
|
|---|
| 8920 | @vindex $(<F)
|
|---|
| 8921 | @vindex <F @r{(automatic variable)}
|
|---|
| 8922 | @itemx $(<F)
|
|---|
| 8923 | The directory part and the file-within-directory
|
|---|
| 8924 | part of the first prerequisite.
|
|---|
| 8925 |
|
|---|
| 8926 | @vindex $(^D)
|
|---|
| 8927 | @vindex ^D @r{(automatic variable)}
|
|---|
| 8928 | @item $(^D)
|
|---|
| 8929 | @vindex $(^F)
|
|---|
| 8930 | @vindex ^F @r{(automatic variable)}
|
|---|
| 8931 | @itemx $(^F)
|
|---|
| 8932 | Lists of the directory parts and the file-within-directory
|
|---|
| 8933 | parts of all prerequisites.
|
|---|
| 8934 |
|
|---|
| 8935 | @vindex $(+D)
|
|---|
| 8936 | @vindex +D @r{(automatic variable)}
|
|---|
| 8937 | @item $(+D)
|
|---|
| 8938 | @vindex $(+F)
|
|---|
| 8939 | @vindex +F @r{(automatic variable)}
|
|---|
| 8940 | @itemx $(+F)
|
|---|
| 8941 | Lists of the directory parts and the file-within-directory
|
|---|
| 8942 | parts of all prerequisites, including multiple instances of duplicated
|
|---|
| 8943 | prerequisites.
|
|---|
| 8944 |
|
|---|
| 8945 | @vindex $(?D)
|
|---|
| 8946 | @vindex ?D @r{(automatic variable)}
|
|---|
| 8947 | @item $(?D)
|
|---|
| 8948 | @vindex $(?F)
|
|---|
| 8949 | @vindex ?F @r{(automatic variable)}
|
|---|
| 8950 | @itemx $(?F)
|
|---|
| 8951 | Lists of the directory parts and the file-within-directory parts of
|
|---|
| 8952 | all prerequisites that are newer than the target.
|
|---|
| 8953 | @end table
|
|---|
| 8954 |
|
|---|
| 8955 | Note that we use a special stylistic convention when we talk about these
|
|---|
| 8956 | automatic variables; we write ``the value of @samp{$<}'', rather than
|
|---|
| 8957 | @w{``the variable @code{<}''} as we would write for ordinary variables
|
|---|
| 8958 | such as @code{objects} and @code{CFLAGS}. We think this convention
|
|---|
| 8959 | looks more natural in this special case. Please do not assume it has a
|
|---|
| 8960 | deep significance; @samp{$<} refers to the variable named @code{<} just
|
|---|
| 8961 | as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
|
|---|
| 8962 | You could just as well use @samp{$(<)} in place of @samp{$<}.
|
|---|
| 8963 |
|
|---|
| 8964 | @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
|
|---|
| 8965 | @subsection How Patterns Match
|
|---|
| 8966 |
|
|---|
| 8967 | @cindex stem
|
|---|
| 8968 | A target pattern is composed of a @samp{%} between a prefix and a suffix,
|
|---|
| 8969 | either or both of which may be empty. The pattern matches a file name only
|
|---|
| 8970 | if the file name starts with the prefix and ends with the suffix, without
|
|---|
| 8971 | overlap. The text between the prefix and the suffix is called the
|
|---|
| 8972 | @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
|
|---|
| 8973 | @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
|
|---|
| 8974 | turned into actual file names by substituting the stem for the character
|
|---|
| 8975 | @samp{%}. Thus, if in the same example one of the prerequisites is written
|
|---|
| 8976 | as @samp{%.c}, it expands to @samp{test.c}.@refill
|
|---|
| 8977 |
|
|---|
| 8978 | When the target pattern does not contain a slash (and it usually does
|
|---|
| 8979 | not), directory names in the file names are removed from the file name
|
|---|
| 8980 | before it is compared with the target prefix and suffix. After the
|
|---|
| 8981 | comparison of the file name to the target pattern, the directory
|
|---|
| 8982 | names, along with the slash that ends them, are added on to the
|
|---|
| 8983 | prerequisite file names generated from the pattern rule's prerequisite
|
|---|
| 8984 | patterns and the file name. The directories are ignored only for the
|
|---|
| 8985 | purpose of finding an implicit rule to use, not in the application of
|
|---|
| 8986 | that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
|
|---|
| 8987 | with @samp{src/a} as the stem. When prerequisites are turned into file
|
|---|
| 8988 | names, the directories from the stem are added at the front, while the
|
|---|
| 8989 | rest of the stem is substituted for the @samp{%}. The stem
|
|---|
| 8990 | @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
|
|---|
| 8991 | @file{src/car}.@refill
|
|---|
| 8992 |
|
|---|
| 8993 | @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
|
|---|
| 8994 | @subsection Match-Anything Pattern Rules
|
|---|
| 8995 |
|
|---|
| 8996 | @cindex match-anything rule
|
|---|
| 8997 | @cindex terminal rule
|
|---|
| 8998 | When a pattern rule's target is just @samp{%}, it matches any file name
|
|---|
| 8999 | whatever. We call these rules @dfn{match-anything} rules. They are very
|
|---|
| 9000 | useful, but it can take a lot of time for @code{make} to think about them,
|
|---|
| 9001 | because it must consider every such rule for each file name listed either
|
|---|
| 9002 | as a target or as a prerequisite.
|
|---|
| 9003 |
|
|---|
| 9004 | Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
|
|---|
| 9005 | would have to consider making it by linking an object file @file{foo.c.o},
|
|---|
| 9006 | or by C compilation-and-linking in one step from @file{foo.c.c}, or by
|
|---|
| 9007 | Pascal compilation-and-linking from @file{foo.c.p}, and many other
|
|---|
| 9008 | possibilities.
|
|---|
| 9009 |
|
|---|
| 9010 | We know these possibilities are ridiculous since @file{foo.c} is a C source
|
|---|
| 9011 | file, not an executable. If @code{make} did consider these possibilities,
|
|---|
| 9012 | it would ultimately reject them, because files such as @file{foo.c.o} and
|
|---|
| 9013 | @file{foo.c.p} would not exist. But these possibilities are so
|
|---|
| 9014 | numerous that @code{make} would run very slowly if it had to consider
|
|---|
| 9015 | them.@refill
|
|---|
| 9016 |
|
|---|
| 9017 | To gain speed, we have put various constraints on the way @code{make}
|
|---|
| 9018 | considers match-anything rules. There are two different constraints that
|
|---|
| 9019 | can be applied, and each time you define a match-anything rule you must
|
|---|
| 9020 | choose one or the other for that rule.
|
|---|
| 9021 |
|
|---|
| 9022 | One choice is to mark the match-anything rule as @dfn{terminal} by defining
|
|---|
| 9023 | it with a double colon. When a rule is terminal, it does not apply unless
|
|---|
| 9024 | its prerequisites actually exist. Prerequisites that could be made with
|
|---|
| 9025 | other implicit rules are not good enough. In other words, no further
|
|---|
| 9026 | chaining is allowed beyond a terminal rule.
|
|---|
| 9027 |
|
|---|
| 9028 | For example, the built-in implicit rules for extracting sources from RCS
|
|---|
| 9029 | and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
|
|---|
| 9030 | not exist, @code{make} will not even consider trying to make it as an
|
|---|
| 9031 | intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
|
|---|
| 9032 | RCS and SCCS files are generally ultimate source files, which should not be
|
|---|
| 9033 | remade from any other files; therefore, @code{make} can save time by not
|
|---|
| 9034 | looking for ways to remake them.@refill
|
|---|
| 9035 |
|
|---|
| 9036 | If you do not mark the match-anything rule as terminal, then it is
|
|---|
| 9037 | nonterminal. A nonterminal match-anything rule cannot apply to a file name
|
|---|
| 9038 | that indicates a specific type of data. A file name indicates a specific
|
|---|
| 9039 | type of data if some non-match-anything implicit rule target matches it.
|
|---|
| 9040 |
|
|---|
| 9041 | For example, the file name @file{foo.c} matches the target for the pattern
|
|---|
| 9042 | rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
|
|---|
| 9043 | rule is actually applicable (which happens only if there is a file
|
|---|
| 9044 | @file{foo.y}), the fact that its target matches is enough to prevent
|
|---|
| 9045 | consideration of any nonterminal match-anything rules for the file
|
|---|
| 9046 | @file{foo.c}. Thus, @code{make} will not even consider trying to make
|
|---|
| 9047 | @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
|
|---|
| 9048 | @file{foo.c.p}, etc.@refill
|
|---|
| 9049 |
|
|---|
| 9050 | The motivation for this constraint is that nonterminal match-anything
|
|---|
| 9051 | rules are used for making files containing specific types of data (such as
|
|---|
| 9052 | executable files) and a file name with a recognized suffix indicates some
|
|---|
| 9053 | other specific type of data (such as a C source file).
|
|---|
| 9054 |
|
|---|
| 9055 | Special built-in dummy pattern rules are provided solely to recognize
|
|---|
| 9056 | certain file names so that nonterminal match-anything rules will not be
|
|---|
| 9057 | considered. These dummy rules have no prerequisites and no commands, and
|
|---|
| 9058 | they are ignored for all other purposes. For example, the built-in
|
|---|
| 9059 | implicit rule
|
|---|
| 9060 |
|
|---|
| 9061 | @example
|
|---|
| 9062 | %.p :
|
|---|
| 9063 | @end example
|
|---|
| 9064 |
|
|---|
| 9065 | @noindent
|
|---|
| 9066 | exists to make sure that Pascal source files such as @file{foo.p} match a
|
|---|
| 9067 | specific target pattern and thereby prevent time from being wasted looking
|
|---|
| 9068 | for @file{foo.p.o} or @file{foo.p.c}.
|
|---|
| 9069 |
|
|---|
| 9070 | Dummy pattern rules such as the one for @samp{%.p} are made for every
|
|---|
| 9071 | suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
|
|---|
| 9072 |
|
|---|
| 9073 | @node Canceling Rules, , Match-Anything Rules, Pattern Rules
|
|---|
| 9074 | @subsection Canceling Implicit Rules
|
|---|
| 9075 |
|
|---|
| 9076 | You can override a built-in implicit rule (or one you have defined
|
|---|
| 9077 | yourself) by defining a new pattern rule with the same target and
|
|---|
| 9078 | prerequisites, but different commands. When the new rule is defined, the
|
|---|
| 9079 | built-in one is replaced. The new rule's position in the sequence of
|
|---|
| 9080 | implicit rules is determined by where you write the new rule.
|
|---|
| 9081 |
|
|---|
| 9082 | You can cancel a built-in implicit rule by defining a pattern rule with the
|
|---|
| 9083 | same target and prerequisites, but no commands. For example, the following
|
|---|
| 9084 | would cancel the rule that runs the assembler:
|
|---|
| 9085 |
|
|---|
| 9086 | @example
|
|---|
| 9087 | %.o : %.s
|
|---|
| 9088 | @end example
|
|---|
| 9089 |
|
|---|
| 9090 | @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
|
|---|
| 9091 | @section Defining Last-Resort Default Rules
|
|---|
| 9092 | @cindex last-resort default rules
|
|---|
| 9093 | @cindex default rules, last-resort
|
|---|
| 9094 |
|
|---|
| 9095 | You can define a last-resort implicit rule by writing a terminal
|
|---|
| 9096 | match-anything pattern rule with no prerequisites (@pxref{Match-Anything
|
|---|
| 9097 | Rules}). This is just like any other pattern rule; the only thing
|
|---|
| 9098 | special about it is that it will match any target. So such a rule's
|
|---|
| 9099 | commands are used for all targets and prerequisites that have no commands
|
|---|
| 9100 | of their own and for which no other implicit rule applies.
|
|---|
| 9101 |
|
|---|
| 9102 | For example, when testing a makefile, you might not care if the source
|
|---|
| 9103 | files contain real data, only that they exist. Then you might do this:
|
|---|
| 9104 |
|
|---|
| 9105 | @example
|
|---|
| 9106 | %::
|
|---|
| 9107 | touch $@@
|
|---|
| 9108 | @end example
|
|---|
| 9109 |
|
|---|
| 9110 | @noindent
|
|---|
| 9111 | to cause all the source files needed (as prerequisites) to be created
|
|---|
| 9112 | automatically.
|
|---|
| 9113 |
|
|---|
| 9114 | @findex .DEFAULT
|
|---|
| 9115 | You can instead define commands to be used for targets for which there
|
|---|
| 9116 | are no rules at all, even ones which don't specify commands. You do
|
|---|
| 9117 | this by writing a rule for the target @code{.DEFAULT}. Such a rule's
|
|---|
| 9118 | commands are used for all prerequisites which do not appear as targets in
|
|---|
| 9119 | any explicit rule, and for which no implicit rule applies. Naturally,
|
|---|
| 9120 | there is no @code{.DEFAULT} rule unless you write one.
|
|---|
| 9121 |
|
|---|
| 9122 | If you use @code{.DEFAULT} with no commands or prerequisites:
|
|---|
| 9123 |
|
|---|
| 9124 | @example
|
|---|
| 9125 | .DEFAULT:
|
|---|
| 9126 | @end example
|
|---|
| 9127 |
|
|---|
| 9128 | @noindent
|
|---|
| 9129 | the commands previously stored for @code{.DEFAULT} are cleared.
|
|---|
| 9130 | Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
|
|---|
| 9131 |
|
|---|
| 9132 | If you do not want a target to get the commands from a match-anything
|
|---|
| 9133 | pattern rule or @code{.DEFAULT}, but you also do not want any commands
|
|---|
| 9134 | to be run for the target, you can give it empty commands (@pxref{Empty
|
|---|
| 9135 | Commands, ,Defining Empty Commands}).@refill
|
|---|
| 9136 |
|
|---|
| 9137 | You can use a last-resort rule to override part of another makefile.
|
|---|
| 9138 | @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
|
|---|
| 9139 |
|
|---|
| 9140 | @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
|
|---|
| 9141 | @section Old-Fashioned Suffix Rules
|
|---|
| 9142 | @cindex old-fashioned suffix rules
|
|---|
| 9143 | @cindex suffix rule
|
|---|
| 9144 |
|
|---|
| 9145 | @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
|
|---|
| 9146 | @code{make}. Suffix rules are obsolete because pattern rules are more
|
|---|
| 9147 | general and clearer. They are supported in GNU @code{make} for
|
|---|
| 9148 | compatibility with old makefiles. They come in two kinds:
|
|---|
| 9149 | @dfn{double-suffix} and @dfn{single-suffix}.@refill
|
|---|
| 9150 |
|
|---|
| 9151 | A double-suffix rule is defined by a pair of suffixes: the target suffix
|
|---|
| 9152 | and the source suffix. It matches any file whose name ends with the
|
|---|
| 9153 | target suffix. The corresponding implicit prerequisite is made by
|
|---|
| 9154 | replacing the target suffix with the source suffix in the file name. A
|
|---|
| 9155 | two-suffix rule whose target and source suffixes are @samp{.o} and
|
|---|
| 9156 | @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
|
|---|
| 9157 |
|
|---|
| 9158 | A single-suffix rule is defined by a single suffix, which is the source
|
|---|
| 9159 | suffix. It matches any file name, and the corresponding implicit
|
|---|
| 9160 | prerequisite name is made by appending the source suffix. A single-suffix
|
|---|
| 9161 | rule whose source suffix is @samp{.c} is equivalent to the pattern rule
|
|---|
| 9162 | @samp{% : %.c}.
|
|---|
| 9163 |
|
|---|
| 9164 | Suffix rule definitions are recognized by comparing each rule's target
|
|---|
| 9165 | against a defined list of known suffixes. When @code{make} sees a rule
|
|---|
| 9166 | whose target is a known suffix, this rule is considered a single-suffix
|
|---|
| 9167 | rule. When @code{make} sees a rule whose target is two known suffixes
|
|---|
| 9168 | concatenated, this rule is taken as a double-suffix rule.
|
|---|
| 9169 |
|
|---|
| 9170 | For example, @samp{.c} and @samp{.o} are both on the default list of
|
|---|
| 9171 | known suffixes. Therefore, if you define a rule whose target is
|
|---|
| 9172 | @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
|
|---|
| 9173 | suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
|
|---|
| 9174 | way to define the rule for compiling a C source file:@refill
|
|---|
| 9175 |
|
|---|
| 9176 | @example
|
|---|
| 9177 | .c.o:
|
|---|
| 9178 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 9179 | @end example
|
|---|
| 9180 |
|
|---|
| 9181 | Suffix rules cannot have any prerequisites of their own. If they have any,
|
|---|
| 9182 | they are treated as normal files with funny names, not as suffix rules.
|
|---|
| 9183 | Thus, the rule:
|
|---|
| 9184 |
|
|---|
| 9185 | @example
|
|---|
| 9186 | .c.o: foo.h
|
|---|
| 9187 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 9188 | @end example
|
|---|
| 9189 |
|
|---|
| 9190 | @noindent
|
|---|
| 9191 | tells how to make the file @file{.c.o} from the prerequisite file
|
|---|
| 9192 | @file{foo.h}, and is not at all like the pattern rule:
|
|---|
| 9193 |
|
|---|
| 9194 | @example
|
|---|
| 9195 | %.o: %.c foo.h
|
|---|
| 9196 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 9197 | @end example
|
|---|
| 9198 |
|
|---|
| 9199 | @noindent
|
|---|
| 9200 | which tells how to make @samp{.o} files from @samp{.c} files, and makes all
|
|---|
| 9201 | @samp{.o} files using this pattern rule also depend on @file{foo.h}.
|
|---|
| 9202 |
|
|---|
| 9203 | Suffix rules with no commands are also meaningless. They do not remove
|
|---|
| 9204 | previous rules as do pattern rules with no commands (@pxref{Canceling
|
|---|
| 9205 | Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
|
|---|
| 9206 | a target in the data base.@refill
|
|---|
| 9207 |
|
|---|
| 9208 | @findex .SUFFIXES
|
|---|
| 9209 | The known suffixes are simply the names of the prerequisites of the special
|
|---|
| 9210 | target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
|
|---|
| 9211 | for @code{.SUFFIXES} that adds more prerequisites, as in:
|
|---|
| 9212 |
|
|---|
| 9213 | @example
|
|---|
| 9214 | .SUFFIXES: .hack .win
|
|---|
| 9215 | @end example
|
|---|
| 9216 |
|
|---|
| 9217 | @noindent
|
|---|
| 9218 | which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
|
|---|
| 9219 |
|
|---|
| 9220 | If you wish to eliminate the default known suffixes instead of just adding
|
|---|
| 9221 | to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
|
|---|
| 9222 | special dispensation, this eliminates all existing prerequisites of
|
|---|
| 9223 | @code{.SUFFIXES}. You can then write another rule to add the suffixes you
|
|---|
| 9224 | want. For example,
|
|---|
| 9225 |
|
|---|
| 9226 | @example
|
|---|
| 9227 | @group
|
|---|
| 9228 | .SUFFIXES: # @r{Delete the default suffixes}
|
|---|
| 9229 | .SUFFIXES: .c .o .h # @r{Define our suffix list}
|
|---|
| 9230 | @end group
|
|---|
| 9231 | @end example
|
|---|
| 9232 |
|
|---|
| 9233 | The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
|
|---|
| 9234 | list of suffixes to be empty.
|
|---|
| 9235 |
|
|---|
| 9236 | @vindex SUFFIXES
|
|---|
| 9237 | The variable @code{SUFFIXES} is defined to the default list of suffixes
|
|---|
| 9238 | before @code{make} reads any makefiles. You can change the list of suffixes
|
|---|
| 9239 | with a rule for the special target @code{.SUFFIXES}, but that does not alter
|
|---|
| 9240 | this variable.
|
|---|
| 9241 |
|
|---|
| 9242 | @node Implicit Rule Search, , Suffix Rules, Implicit Rules
|
|---|
| 9243 | @section Implicit Rule Search Algorithm
|
|---|
| 9244 | @cindex implicit rule, search algorithm
|
|---|
| 9245 | @cindex search algorithm, implicit rule
|
|---|
| 9246 |
|
|---|
| 9247 | Here is the procedure @code{make} uses for searching for an implicit rule
|
|---|
| 9248 | for a target @var{t}. This procedure is followed for each double-colon
|
|---|
| 9249 | rule with no commands, for each target of ordinary rules none of which have
|
|---|
| 9250 | commands, and for each prerequisite that is not the target of any rule. It
|
|---|
| 9251 | is also followed recursively for prerequisites that come from implicit
|
|---|
| 9252 | rules, in the search for a chain of rules.
|
|---|
| 9253 |
|
|---|
| 9254 | Suffix rules are not mentioned in this algorithm because suffix rules are
|
|---|
| 9255 | converted to equivalent pattern rules once the makefiles have been read in.
|
|---|
| 9256 |
|
|---|
| 9257 | For an archive member target of the form
|
|---|
| 9258 | @samp{@var{archive}(@var{member})}, the following algorithm is run
|
|---|
| 9259 | twice, first using the entire target name @var{t}, and second using
|
|---|
| 9260 | @samp{(@var{member})} as the target @var{t} if the first run found no
|
|---|
| 9261 | rule.@refill
|
|---|
| 9262 |
|
|---|
| 9263 | @enumerate
|
|---|
| 9264 | @item
|
|---|
| 9265 | Split @var{t} into a directory part, called @var{d}, and the rest,
|
|---|
| 9266 | called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
|
|---|
| 9267 | @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
|
|---|
| 9268 |
|
|---|
| 9269 | @item
|
|---|
| 9270 | Make a list of all the pattern rules one of whose targets matches
|
|---|
| 9271 | @var{t} or @var{n}. If the target pattern contains a slash, it is
|
|---|
| 9272 | matched against @var{t}; otherwise, against @var{n}.
|
|---|
| 9273 |
|
|---|
| 9274 | @item
|
|---|
| 9275 | If any rule in that list is @emph{not} a match-anything rule, then
|
|---|
| 9276 | remove all nonterminal match-anything rules from the list.
|
|---|
| 9277 |
|
|---|
| 9278 | @item
|
|---|
| 9279 | Remove from the list all rules with no commands.
|
|---|
| 9280 |
|
|---|
| 9281 | @item
|
|---|
| 9282 | For each pattern rule in the list:
|
|---|
| 9283 |
|
|---|
| 9284 | @enumerate a
|
|---|
| 9285 | @item
|
|---|
| 9286 | Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
|
|---|
| 9287 | matched by the @samp{%} in the target pattern.@refill
|
|---|
| 9288 |
|
|---|
| 9289 | @item
|
|---|
| 9290 | Compute the prerequisite names by substituting @var{s} for @samp{%}; if
|
|---|
| 9291 | the target pattern does not contain a slash, append @var{d} to
|
|---|
| 9292 | the front of each prerequisite name.@refill
|
|---|
| 9293 |
|
|---|
| 9294 | @item
|
|---|
| 9295 | Test whether all the prerequisites exist or ought to exist. (If a
|
|---|
| 9296 | file name is mentioned in the makefile as a target or as an explicit
|
|---|
| 9297 | prerequisite, then we say it ought to exist.)
|
|---|
| 9298 |
|
|---|
| 9299 | If all prerequisites exist or ought to exist, or there are no prerequisites,
|
|---|
| 9300 | then this rule applies.
|
|---|
| 9301 | @end enumerate
|
|---|
| 9302 |
|
|---|
| 9303 | @item
|
|---|
| 9304 | If no pattern rule has been found so far, try harder.
|
|---|
| 9305 | For each pattern rule in the list:
|
|---|
| 9306 |
|
|---|
| 9307 | @enumerate a
|
|---|
| 9308 | @item
|
|---|
| 9309 | If the rule is terminal, ignore it and go on to the next rule.
|
|---|
| 9310 |
|
|---|
| 9311 | @item
|
|---|
| 9312 | Compute the prerequisite names as before.
|
|---|
| 9313 |
|
|---|
| 9314 | @item
|
|---|
| 9315 | Test whether all the prerequisites exist or ought to exist.
|
|---|
| 9316 |
|
|---|
| 9317 | @item
|
|---|
| 9318 | For each prerequisite that does not exist, follow this algorithm
|
|---|
| 9319 | recursively to see if the prerequisite can be made by an implicit
|
|---|
| 9320 | rule.
|
|---|
| 9321 |
|
|---|
| 9322 | @item
|
|---|
| 9323 | If all prerequisites exist, ought to exist, or can be
|
|---|
| 9324 | made by implicit rules, then this rule applies.
|
|---|
| 9325 | @end enumerate
|
|---|
| 9326 |
|
|---|
| 9327 | @item
|
|---|
| 9328 | If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
|
|---|
| 9329 | applies. In that case, give @var{t} the same commands that
|
|---|
| 9330 | @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
|
|---|
| 9331 | @end enumerate
|
|---|
| 9332 |
|
|---|
| 9333 | Once a rule that applies has been found, for each target pattern of the
|
|---|
| 9334 | rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
|
|---|
| 9335 | the pattern is replaced with @var{s} and the resultant file name is stored
|
|---|
| 9336 | until the commands to remake the target file @var{t} are executed. After
|
|---|
| 9337 | these commands are executed, each of these stored file names are entered
|
|---|
| 9338 | into the data base and marked as having been updated and having the same
|
|---|
| 9339 | update status as the file @var{t}.
|
|---|
| 9340 |
|
|---|
| 9341 | When the commands of a pattern rule are executed for @var{t}, the automatic
|
|---|
| 9342 | variables are set corresponding to the target and prerequisites.
|
|---|
| 9343 | @xref{Automatic Variables}.
|
|---|
| 9344 |
|
|---|
| 9345 | @node Archives, Features, Implicit Rules, Top
|
|---|
| 9346 | @chapter Using @code{make} to Update Archive Files
|
|---|
| 9347 | @cindex archive
|
|---|
| 9348 |
|
|---|
| 9349 | @dfn{Archive files} are files containing named subfiles called
|
|---|
| 9350 | @dfn{members}; they are maintained with the program @code{ar} and their
|
|---|
| 9351 | main use is as subroutine libraries for linking.
|
|---|
| 9352 |
|
|---|
| 9353 | @menu
|
|---|
| 9354 | * Archive Members:: Archive members as targets.
|
|---|
| 9355 | * Archive Update:: The implicit rule for archive member targets.
|
|---|
| 9356 | * Archive Pitfalls:: Dangers to watch out for when using archives.
|
|---|
| 9357 | * Archive Suffix Rules:: You can write a special kind of suffix rule
|
|---|
| 9358 | for updating archives.
|
|---|
| 9359 | @end menu
|
|---|
| 9360 |
|
|---|
| 9361 | @node Archive Members, Archive Update, Archives, Archives
|
|---|
| 9362 | @section Archive Members as Targets
|
|---|
| 9363 | @cindex archive member targets
|
|---|
| 9364 |
|
|---|
| 9365 | An individual member of an archive file can be used as a target or
|
|---|
| 9366 | prerequisite in @code{make}. You specify the member named @var{member} in
|
|---|
| 9367 | archive file @var{archive} as follows:
|
|---|
| 9368 |
|
|---|
| 9369 | @example
|
|---|
| 9370 | @var{archive}(@var{member})
|
|---|
| 9371 | @end example
|
|---|
| 9372 |
|
|---|
| 9373 | @noindent
|
|---|
| 9374 | This construct is available only in targets and prerequisites, not in
|
|---|
| 9375 | commands! Most programs that you might use in commands do not support this
|
|---|
| 9376 | syntax and cannot act directly on archive members. Only @code{ar} and
|
|---|
| 9377 | other programs specifically designed to operate on archives can do so.
|
|---|
| 9378 | Therefore, valid commands to update an archive member target probably must
|
|---|
| 9379 | use @code{ar}. For example, this rule says to create a member
|
|---|
| 9380 | @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
|
|---|
| 9381 |
|
|---|
| 9382 | @example
|
|---|
| 9383 | foolib(hack.o) : hack.o
|
|---|
| 9384 | ar cr foolib hack.o
|
|---|
| 9385 | @end example
|
|---|
| 9386 |
|
|---|
| 9387 | In fact, nearly all archive member targets are updated in just this way
|
|---|
| 9388 | and there is an implicit rule to do it for you. @strong{Note:} The
|
|---|
| 9389 | @samp{c} flag to @code{ar} is required if the archive file does not
|
|---|
| 9390 | already exist.
|
|---|
| 9391 |
|
|---|
| 9392 | To specify several members in the same archive, you can write all the
|
|---|
| 9393 | member names together between the parentheses. For example:
|
|---|
| 9394 |
|
|---|
| 9395 | @example
|
|---|
| 9396 | foolib(hack.o kludge.o)
|
|---|
| 9397 | @end example
|
|---|
| 9398 |
|
|---|
| 9399 | @noindent
|
|---|
| 9400 | is equivalent to:
|
|---|
| 9401 |
|
|---|
| 9402 | @example
|
|---|
| 9403 | foolib(hack.o) foolib(kludge.o)
|
|---|
| 9404 | @end example
|
|---|
| 9405 |
|
|---|
| 9406 | @cindex wildcard, in archive member
|
|---|
| 9407 | You can also use shell-style wildcards in an archive member reference.
|
|---|
| 9408 | @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
|
|---|
| 9409 | example, @w{@samp{foolib(*.o)}} expands to all existing members of the
|
|---|
| 9410 | @file{foolib} archive whose names end in @samp{.o}; perhaps
|
|---|
| 9411 | @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
|
|---|
| 9412 |
|
|---|
| 9413 | @node Archive Update, Archive Pitfalls, Archive Members, Archives
|
|---|
| 9414 | @section Implicit Rule for Archive Member Targets
|
|---|
| 9415 |
|
|---|
| 9416 | Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
|
|---|
| 9417 | member named @var{m} in the archive file @var{a}.
|
|---|
| 9418 |
|
|---|
| 9419 | When @code{make} looks for an implicit rule for such a target, as a special
|
|---|
| 9420 | feature it considers implicit rules that match @file{(@var{m})}, as well as
|
|---|
| 9421 | those that match the actual target @file{@var{a}(@var{m})}.
|
|---|
| 9422 |
|
|---|
| 9423 | This causes one special rule whose target is @file{(%)} to match. This
|
|---|
| 9424 | rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
|
|---|
| 9425 | into the archive. For example, it will update the archive member target
|
|---|
| 9426 | @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
|
|---|
| 9427 | archive @file{foo.a} as a @emph{member} named @file{bar.o}.
|
|---|
| 9428 |
|
|---|
| 9429 | When this rule is chained with others, the result is very powerful.
|
|---|
| 9430 | Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
|
|---|
| 9431 | @samp{(} and @samp{)} from being interpreted specially by the shell) in
|
|---|
| 9432 | the presence of a file @file{bar.c} is enough to cause the following
|
|---|
| 9433 | commands to be run, even without a makefile:
|
|---|
| 9434 |
|
|---|
| 9435 | @example
|
|---|
| 9436 | cc -c bar.c -o bar.o
|
|---|
| 9437 | ar r foo.a bar.o
|
|---|
| 9438 | rm -f bar.o
|
|---|
| 9439 | @end example
|
|---|
| 9440 |
|
|---|
| 9441 | @noindent
|
|---|
| 9442 | Here @code{make} has envisioned the file @file{bar.o} as an intermediate
|
|---|
| 9443 | file. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9444 |
|
|---|
| 9445 | Implicit rules such as this one are written using the automatic variable
|
|---|
| 9446 | @samp{$%}. @xref{Automatic Variables}.
|
|---|
| 9447 |
|
|---|
| 9448 | An archive member name in an archive cannot contain a directory name, but
|
|---|
| 9449 | it may be useful in a makefile to pretend that it does. If you write an
|
|---|
| 9450 | archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
|
|---|
| 9451 | automatic updating with this command:
|
|---|
| 9452 |
|
|---|
| 9453 | @example
|
|---|
| 9454 | ar r foo.a dir/file.o
|
|---|
| 9455 | @end example
|
|---|
| 9456 |
|
|---|
| 9457 | @noindent
|
|---|
| 9458 | which has the effect of copying the file @file{dir/file.o} into a member
|
|---|
| 9459 | named @file{file.o}. In connection with such usage, the automatic variables
|
|---|
| 9460 | @code{%D} and @code{%F} may be useful.
|
|---|
| 9461 |
|
|---|
| 9462 | @menu
|
|---|
| 9463 | * Archive Symbols:: How to update archive symbol directories.
|
|---|
| 9464 | @end menu
|
|---|
| 9465 |
|
|---|
| 9466 | @node Archive Symbols, , Archive Update, Archive Update
|
|---|
| 9467 | @subsection Updating Archive Symbol Directories
|
|---|
| 9468 | @cindex @code{__.SYMDEF}
|
|---|
| 9469 | @cindex updating archive symbol directories
|
|---|
| 9470 | @cindex archive symbol directory updating
|
|---|
| 9471 | @cindex symbol directories, updating archive
|
|---|
| 9472 | @cindex directories, updating archive symbol
|
|---|
| 9473 |
|
|---|
| 9474 | An archive file that is used as a library usually contains a special member
|
|---|
| 9475 | named @file{__.SYMDEF} that contains a directory of the external symbol
|
|---|
| 9476 | names defined by all the other members. After you update any other
|
|---|
| 9477 | members, you need to update @file{__.SYMDEF} so that it will summarize the
|
|---|
| 9478 | other members properly. This is done by running the @code{ranlib} program:
|
|---|
| 9479 |
|
|---|
| 9480 | @example
|
|---|
| 9481 | ranlib @var{archivefile}
|
|---|
| 9482 | @end example
|
|---|
| 9483 |
|
|---|
| 9484 | Normally you would put this command in the rule for the archive file,
|
|---|
| 9485 | and make all the members of the archive file prerequisites of that rule.
|
|---|
| 9486 | For example,
|
|---|
| 9487 |
|
|---|
| 9488 | @example
|
|---|
| 9489 | libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
|
|---|
| 9490 | ranlib libfoo.a
|
|---|
| 9491 | @end example
|
|---|
| 9492 |
|
|---|
| 9493 | @noindent
|
|---|
| 9494 | The effect of this is to update archive members @file{x.o}, @file{y.o},
|
|---|
| 9495 | etc., and then update the symbol directory member @file{__.SYMDEF} by
|
|---|
| 9496 | running @code{ranlib}. The rules for updating the members are not shown
|
|---|
| 9497 | here; most likely you can omit them and use the implicit rule which copies
|
|---|
| 9498 | files into the archive, as described in the preceding section.
|
|---|
| 9499 |
|
|---|
| 9500 | This is not necessary when using the GNU @code{ar} program, which
|
|---|
| 9501 | updates the @file{__.SYMDEF} member automatically.
|
|---|
| 9502 |
|
|---|
| 9503 | @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
|
|---|
| 9504 | @section Dangers When Using Archives
|
|---|
| 9505 | @cindex archive, and parallel execution
|
|---|
| 9506 | @cindex parallel execution, and archive update
|
|---|
| 9507 | @cindex archive, and @code{-j}
|
|---|
| 9508 | @cindex @code{-j}, and archive update
|
|---|
| 9509 |
|
|---|
| 9510 | It is important to be careful when using parallel execution (the
|
|---|
| 9511 | @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
|
|---|
| 9512 | If multiple @code{ar} commands run at the same time on the same archive
|
|---|
| 9513 | file, they will not know about each other and can corrupt the file.
|
|---|
| 9514 |
|
|---|
| 9515 | Possibly a future version of @code{make} will provide a mechanism to
|
|---|
| 9516 | circumvent this problem by serializing all commands that operate on the
|
|---|
| 9517 | same archive file. But for the time being, you must either write your
|
|---|
| 9518 | makefiles to avoid this problem in some other way, or not use @code{-j}.
|
|---|
| 9519 |
|
|---|
| 9520 | @node Archive Suffix Rules, , Archive Pitfalls, Archives
|
|---|
| 9521 | @section Suffix Rules for Archive Files
|
|---|
| 9522 | @cindex suffix rule, for archive
|
|---|
| 9523 | @cindex archive, suffix rule for
|
|---|
| 9524 | @cindex library archive, suffix rule for
|
|---|
| 9525 | @cindex @code{.a} (archives)
|
|---|
| 9526 |
|
|---|
| 9527 | You can write a special kind of suffix rule for dealing with archive
|
|---|
| 9528 | files. @xref{Suffix Rules}, for a full explanation of suffix rules.
|
|---|
| 9529 | Archive suffix rules are obsolete in GNU @code{make}, because pattern
|
|---|
| 9530 | rules for archives are a more general mechanism (@pxref{Archive
|
|---|
| 9531 | Update}). But they are retained for compatibility with other
|
|---|
| 9532 | @code{make}s.
|
|---|
| 9533 |
|
|---|
| 9534 | To write a suffix rule for archives, you simply write a suffix rule
|
|---|
| 9535 | using the target suffix @samp{.a} (the usual suffix for archive files).
|
|---|
| 9536 | For example, here is the old-fashioned suffix rule to update a library
|
|---|
| 9537 | archive from C source files:
|
|---|
| 9538 |
|
|---|
| 9539 | @example
|
|---|
| 9540 | @group
|
|---|
| 9541 | .c.a:
|
|---|
| 9542 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|---|
| 9543 | $(AR) r $@@ $*.o
|
|---|
| 9544 | $(RM) $*.o
|
|---|
| 9545 | @end group
|
|---|
| 9546 | @end example
|
|---|
| 9547 |
|
|---|
| 9548 | @noindent
|
|---|
| 9549 | This works just as if you had written the pattern rule:
|
|---|
| 9550 |
|
|---|
| 9551 | @example
|
|---|
| 9552 | @group
|
|---|
| 9553 | (%.o): %.c
|
|---|
| 9554 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|---|
| 9555 | $(AR) r $@@ $*.o
|
|---|
| 9556 | $(RM) $*.o
|
|---|
| 9557 | @end group
|
|---|
| 9558 | @end example
|
|---|
| 9559 |
|
|---|
| 9560 | In fact, this is just what @code{make} does when it sees a suffix rule
|
|---|
| 9561 | with @samp{.a} as the target suffix. Any double-suffix rule
|
|---|
| 9562 | @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
|
|---|
| 9563 | pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
|
|---|
| 9564 |
|
|---|
| 9565 | Since you might want to use @samp{.a} as the suffix for some other kind
|
|---|
| 9566 | of file, @code{make} also converts archive suffix rules to pattern rules
|
|---|
| 9567 | in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
|
|---|
| 9568 | @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
|
|---|
| 9569 | @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
|
|---|
| 9570 |
|
|---|
| 9571 | @node Features, Missing, Archives, Top
|
|---|
| 9572 | @chapter Features of GNU @code{make}
|
|---|
| 9573 | @cindex features of GNU @code{make}
|
|---|
| 9574 | @cindex portability
|
|---|
| 9575 | @cindex compatibility
|
|---|
| 9576 |
|
|---|
| 9577 | Here is a summary of the features of GNU @code{make}, for comparison
|
|---|
| 9578 | with and credit to other versions of @code{make}. We consider the
|
|---|
| 9579 | features of @code{make} in 4.2 BSD systems as a baseline. If you are
|
|---|
| 9580 | concerned with writing portable makefiles, you should not use the
|
|---|
| 9581 | features of @code{make} listed here, nor the ones in @ref{Missing}.
|
|---|
| 9582 |
|
|---|
| 9583 | Many features come from the version of @code{make} in System V.
|
|---|
| 9584 |
|
|---|
| 9585 | @itemize @bullet
|
|---|
| 9586 | @item
|
|---|
| 9587 | The @code{VPATH} variable and its special meaning.
|
|---|
| 9588 | @xref{Directory Search, , Searching Directories for Prerequisites}.
|
|---|
| 9589 | This feature exists in System V @code{make}, but is undocumented.
|
|---|
| 9590 | It is documented in 4.3 BSD @code{make} (which says it mimics System V's
|
|---|
| 9591 | @code{VPATH} feature).@refill
|
|---|
| 9592 |
|
|---|
| 9593 | @item
|
|---|
| 9594 | Included makefiles. @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9595 | Allowing multiple files to be included with a single directive is a GNU
|
|---|
| 9596 | extension.
|
|---|
| 9597 |
|
|---|
| 9598 | @item
|
|---|
| 9599 | Variables are read from and communicated via the environment.
|
|---|
| 9600 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 9601 |
|
|---|
| 9602 | @item
|
|---|
| 9603 | Options passed through the variable @code{MAKEFLAGS} to recursive
|
|---|
| 9604 | invocations of @code{make}.
|
|---|
| 9605 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 9606 |
|
|---|
| 9607 | @item
|
|---|
| 9608 | The automatic variable @code{$%} is set to the member name
|
|---|
| 9609 | in an archive reference. @xref{Automatic Variables}.
|
|---|
| 9610 |
|
|---|
| 9611 | @item
|
|---|
| 9612 | The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
|
|---|
| 9613 | and @code{$?} have corresponding forms like @code{$(@@F)} and
|
|---|
| 9614 | @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
|
|---|
| 9615 | extension. @xref{Automatic Variables}.@refill
|
|---|
| 9616 |
|
|---|
| 9617 | @item
|
|---|
| 9618 | Substitution variable references.
|
|---|
| 9619 | @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9620 |
|
|---|
| 9621 | @item
|
|---|
| 9622 | The command-line options @samp{-b} and @samp{-m}, accepted and
|
|---|
| 9623 | ignored. In System V @code{make}, these options actually do something.
|
|---|
| 9624 |
|
|---|
| 9625 | @item
|
|---|
| 9626 | Execution of recursive commands to run @code{make} via the variable
|
|---|
| 9627 | @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
|
|---|
| 9628 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9629 |
|
|---|
| 9630 | @item
|
|---|
| 9631 | Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
|
|---|
| 9632 | Rules}. This feature is obsolete in GNU @code{make}, because the
|
|---|
| 9633 | general feature of rule chaining (@pxref{Chained Rules, ,Chains of
|
|---|
| 9634 | Implicit Rules}) allows one pattern rule for installing members in an
|
|---|
| 9635 | archive (@pxref{Archive Update}) to be sufficient.
|
|---|
| 9636 |
|
|---|
| 9637 | @item
|
|---|
| 9638 | The arrangement of lines and backslash-newline combinations in
|
|---|
| 9639 | commands is retained when the commands are printed, so they appear as
|
|---|
| 9640 | they do in the makefile, except for the stripping of initial
|
|---|
| 9641 | whitespace.
|
|---|
| 9642 | @end itemize
|
|---|
| 9643 |
|
|---|
| 9644 | The following features were inspired by various other versions of
|
|---|
| 9645 | @code{make}. In some cases it is unclear exactly which versions inspired
|
|---|
| 9646 | which others.
|
|---|
| 9647 |
|
|---|
| 9648 | @itemize @bullet
|
|---|
| 9649 | @item
|
|---|
| 9650 | Pattern rules using @samp{%}.
|
|---|
| 9651 | This has been implemented in several versions of @code{make}.
|
|---|
| 9652 | We're not sure who invented it first, but it's been spread around a bit.
|
|---|
| 9653 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
|
|---|
| 9654 |
|
|---|
| 9655 | @item
|
|---|
| 9656 | Rule chaining and implicit intermediate files.
|
|---|
| 9657 | This was implemented by Stu Feldman in his version of @code{make}
|
|---|
| 9658 | for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
|
|---|
| 9659 | AT&T Bell Labs in his @code{mk} program (where he terms it
|
|---|
| 9660 | ``transitive closure''). We do not really know if
|
|---|
| 9661 | we got this from either of them or thought it up ourselves at the
|
|---|
| 9662 | same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9663 |
|
|---|
| 9664 | @item
|
|---|
| 9665 | The automatic variable @code{$^} containing a list of all prerequisites
|
|---|
| 9666 | of the current target. We did not invent this, but we have no idea who
|
|---|
| 9667 | did. @xref{Automatic Variables}. The automatic variable
|
|---|
| 9668 | @code{$+} is a simple extension of @code{$^}.
|
|---|
| 9669 |
|
|---|
| 9670 | @item
|
|---|
| 9671 | The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
|
|---|
| 9672 | invented by Andrew Hume in @code{mk}.
|
|---|
| 9673 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 9674 |
|
|---|
| 9675 | @item
|
|---|
| 9676 | The concept of doing several things at once (parallelism) exists in
|
|---|
| 9677 | many incarnations of @code{make} and similar programs, though not in the
|
|---|
| 9678 | System V or BSD implementations. @xref{Execution, ,Command Execution}.
|
|---|
| 9679 |
|
|---|
| 9680 | @item
|
|---|
| 9681 | Modified variable references using pattern substitution come from
|
|---|
| 9682 | SunOS 4. @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9683 | This functionality was provided in GNU @code{make} by the
|
|---|
| 9684 | @code{patsubst} function before the alternate syntax was implemented
|
|---|
| 9685 | for compatibility with SunOS 4. It is not altogether clear who
|
|---|
| 9686 | inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
|
|---|
| 9687 | 4 was released.@refill
|
|---|
| 9688 |
|
|---|
| 9689 | @item
|
|---|
| 9690 | The special significance of @samp{+} characters preceding command lines
|
|---|
| 9691 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
|
|---|
| 9692 | mandated by
|
|---|
| 9693 | @cite{IEEE Standard 1003.2-1992} (POSIX.2).
|
|---|
| 9694 |
|
|---|
| 9695 | @item
|
|---|
| 9696 | The @samp{+=} syntax to append to the value of a variable comes from SunOS
|
|---|
| 9697 | 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
|
|---|
| 9698 |
|
|---|
| 9699 | @item
|
|---|
| 9700 | The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
|
|---|
| 9701 | multiple members in a single archive file comes from SunOS 4 @code{make}.
|
|---|
| 9702 | @xref{Archive Members}.
|
|---|
| 9703 |
|
|---|
| 9704 | @item
|
|---|
| 9705 | The @code{-include} directive to include makefiles with no error for a
|
|---|
| 9706 | nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
|
|---|
| 9707 | @code{make} does not allow multiple makefiles to be specified in one
|
|---|
| 9708 | @code{-include} directive.) The same feature appears with the name
|
|---|
| 9709 | @code{sinclude} in SGI @code{make} and perhaps others.
|
|---|
| 9710 | @end itemize
|
|---|
| 9711 |
|
|---|
| 9712 | The remaining features are inventions new in GNU @code{make}:
|
|---|
| 9713 |
|
|---|
| 9714 | @itemize @bullet
|
|---|
| 9715 | @item
|
|---|
| 9716 | Use the @samp{-v} or @samp{--version} option to print version and
|
|---|
| 9717 | copyright information.
|
|---|
| 9718 |
|
|---|
| 9719 | @item
|
|---|
| 9720 | Use the @samp{-h} or @samp{--help} option to summarize the options to
|
|---|
| 9721 | @code{make}.
|
|---|
| 9722 |
|
|---|
| 9723 | @item
|
|---|
| 9724 | Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
|
|---|
| 9725 |
|
|---|
| 9726 | @item
|
|---|
| 9727 | Pass command-line variable assignments automatically through the
|
|---|
| 9728 | variable @code{MAKE} to recursive @code{make} invocations.
|
|---|
| 9729 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9730 |
|
|---|
| 9731 | @item
|
|---|
| 9732 | Use the @samp{-C} or @samp{--directory} command option to change
|
|---|
| 9733 | directory. @xref{Options Summary, ,Summary of Options}.
|
|---|
| 9734 |
|
|---|
| 9735 | @item
|
|---|
| 9736 | Make verbatim variable definitions with @code{define}.
|
|---|
| 9737 | @xref{Defining, ,Defining Variables Verbatim}.
|
|---|
| 9738 |
|
|---|
| 9739 | @item
|
|---|
| 9740 | Declare phony targets with the special target @code{.PHONY}.
|
|---|
| 9741 |
|
|---|
| 9742 | Andrew Hume of AT&T Bell Labs implemented a similar feature with a
|
|---|
| 9743 | different syntax in his @code{mk} program. This seems to be a case of
|
|---|
| 9744 | parallel discovery. @xref{Phony Targets, ,Phony Targets}.
|
|---|
| 9745 |
|
|---|
| 9746 | @item
|
|---|
| 9747 | Manipulate text by calling functions.
|
|---|
| 9748 | @xref{Functions, ,Functions for Transforming Text}.
|
|---|
| 9749 |
|
|---|
| 9750 | @item
|
|---|
| 9751 | Use the @samp{-o} or @samp{--old-file}
|
|---|
| 9752 | option to pretend a file's modification-time is old.
|
|---|
| 9753 | @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
|
|---|
| 9754 |
|
|---|
| 9755 | @item
|
|---|
| 9756 | Conditional execution.
|
|---|
| 9757 |
|
|---|
| 9758 | This feature has been implemented numerous times in various versions
|
|---|
| 9759 | of @code{make}; it seems a natural extension derived from the features
|
|---|
| 9760 | of the C preprocessor and similar macro languages and is not a
|
|---|
| 9761 | revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
|
|---|
| 9762 |
|
|---|
| 9763 | @item
|
|---|
| 9764 | Specify a search path for included makefiles.
|
|---|
| 9765 | @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9766 |
|
|---|
| 9767 | @item
|
|---|
| 9768 | Specify extra makefiles to read with an environment variable.
|
|---|
| 9769 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
|---|
| 9770 |
|
|---|
| 9771 | @item
|
|---|
| 9772 | Strip leading sequences of @samp{./} from file names, so that
|
|---|
| 9773 | @file{./@var{file}} and @file{@var{file}} are considered to be the
|
|---|
| 9774 | same file.@refill
|
|---|
| 9775 |
|
|---|
| 9776 | @item
|
|---|
| 9777 | Use a special search method for library prerequisites written in the
|
|---|
| 9778 | form @samp{-l@var{name}}.
|
|---|
| 9779 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
|---|
| 9780 |
|
|---|
| 9781 | @item
|
|---|
| 9782 | Allow suffixes for suffix rules
|
|---|
| 9783 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
|
|---|
| 9784 | characters. In other versions of @code{make}, they must begin with
|
|---|
| 9785 | @samp{.} and not contain any @samp{/} characters.
|
|---|
| 9786 |
|
|---|
| 9787 | @item
|
|---|
| 9788 | Keep track of the current level of @code{make} recursion using the
|
|---|
| 9789 | variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9790 |
|
|---|
| 9791 | @item
|
|---|
| 9792 | Provide any goals given on the command line in the variable
|
|---|
| 9793 | @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 9794 |
|
|---|
| 9795 | @item
|
|---|
| 9796 | Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
|
|---|
| 9797 |
|
|---|
| 9798 | @item
|
|---|
| 9799 | Provide selective @code{vpath} search.
|
|---|
| 9800 | @xref{Directory Search, ,Searching Directories for Prerequisites}.
|
|---|
| 9801 |
|
|---|
| 9802 | @item
|
|---|
| 9803 | Provide computed variable references.
|
|---|
| 9804 | @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9805 |
|
|---|
| 9806 | @item
|
|---|
| 9807 | Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
|---|
| 9808 | System V @code{make} has a very, very limited form of this
|
|---|
| 9809 | functionality in that it will check out SCCS files for makefiles.
|
|---|
| 9810 |
|
|---|
| 9811 | @item
|
|---|
| 9812 | Various new built-in implicit rules.
|
|---|
| 9813 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
|
|---|
| 9814 |
|
|---|
| 9815 | @item
|
|---|
| 9816 | The built-in variable @samp{MAKE_VERSION} gives the version number of
|
|---|
| 9817 | @code{make}.
|
|---|
| 9818 | @vindex MAKE_VERSION
|
|---|
| 9819 | @end itemize
|
|---|
| 9820 |
|
|---|
| 9821 | @node Missing, Makefile Conventions, Features, Top
|
|---|
| 9822 | @chapter Incompatibilities and Missing Features
|
|---|
| 9823 | @cindex incompatibilities
|
|---|
| 9824 | @cindex missing features
|
|---|
| 9825 | @cindex features, missing
|
|---|
| 9826 |
|
|---|
| 9827 | The @code{make} programs in various other systems support a few features
|
|---|
| 9828 | that are not implemented in GNU @code{make}. The POSIX.2 standard
|
|---|
| 9829 | (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
|
|---|
| 9830 | require any of these features.@refill
|
|---|
| 9831 |
|
|---|
| 9832 | @itemize @bullet
|
|---|
| 9833 | @item
|
|---|
| 9834 | A target of the form @samp{@var{file}((@var{entry}))} stands for a member
|
|---|
| 9835 | of archive file @var{file}. The member is chosen, not by name, but by
|
|---|
| 9836 | being an object file which defines the linker symbol @var{entry}.@refill
|
|---|
| 9837 |
|
|---|
| 9838 | This feature was not put into GNU @code{make} because of the
|
|---|
| 9839 | nonmodularity of putting knowledge into @code{make} of the internal
|
|---|
| 9840 | format of archive file symbol tables.
|
|---|
| 9841 | @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
|
|---|
| 9842 |
|
|---|
| 9843 | @item
|
|---|
| 9844 | Suffixes (used in suffix rules) that end with the character @samp{~}
|
|---|
| 9845 | have a special meaning to System V @code{make};
|
|---|
| 9846 | they refer to the SCCS file that corresponds
|
|---|
| 9847 | to the file one would get without the @samp{~}. For example, the
|
|---|
| 9848 | suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
|
|---|
| 9849 | the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
|
|---|
| 9850 | series of such suffix rules is required.
|
|---|
| 9851 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 9852 |
|
|---|
| 9853 | In GNU @code{make}, this entire series of cases is handled by two
|
|---|
| 9854 | pattern rules for extraction from SCCS, in combination with the
|
|---|
| 9855 | general feature of rule chaining.
|
|---|
| 9856 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9857 |
|
|---|
| 9858 | @item
|
|---|
| 9859 | In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
|
|---|
| 9860 | (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
|
|---|
| 9861 | strings. We feel it is much cleaner to always use automatic variables
|
|---|
| 9862 | and thus make this feature obsolete.@refill
|
|---|
| 9863 |
|
|---|
| 9864 | @item
|
|---|
| 9865 | In some Unix @code{make}s, the automatic variable @code{$*} appearing in
|
|---|
| 9866 | the prerequisites of a rule has the amazingly strange ``feature'' of
|
|---|
| 9867 | expanding to the full name of the @emph{target of that rule}. We cannot
|
|---|
| 9868 | imagine what went on in the minds of Unix @code{make} developers to do
|
|---|
| 9869 | this; it is utterly inconsistent with the normal definition of @code{$*}.
|
|---|
| 9870 | @vindex * @r{(automatic variable), unsupported bizarre usage}
|
|---|
| 9871 |
|
|---|
| 9872 | @item
|
|---|
| 9873 | In some Unix @code{make}s, implicit rule search
|
|---|
| 9874 | (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
|
|---|
| 9875 | @emph{all} targets, not just those without commands. This means you can
|
|---|
| 9876 | do:@refill
|
|---|
| 9877 |
|
|---|
| 9878 | @example
|
|---|
| 9879 | @group
|
|---|
| 9880 | foo.o:
|
|---|
| 9881 | cc -c foo.c
|
|---|
| 9882 | @end group
|
|---|
| 9883 | @end example
|
|---|
| 9884 |
|
|---|
| 9885 | @noindent
|
|---|
| 9886 | and Unix @code{make} will intuit that @file{foo.o} depends on
|
|---|
| 9887 | @file{foo.c}.@refill
|
|---|
| 9888 |
|
|---|
| 9889 | We feel that such usage is broken. The prerequisite properties of
|
|---|
| 9890 | @code{make} are well-defined (for GNU @code{make}, at least),
|
|---|
| 9891 | and doing such a thing simply does not fit the model.@refill
|
|---|
| 9892 |
|
|---|
| 9893 | @item
|
|---|
| 9894 | GNU @code{make} does not include any built-in implicit rules for
|
|---|
| 9895 | compiling or preprocessing EFL programs. If we hear of anyone who is
|
|---|
| 9896 | using EFL, we will gladly add them.
|
|---|
| 9897 |
|
|---|
| 9898 | @item
|
|---|
| 9899 | It appears that in SVR4 @code{make}, a suffix rule can be specified with
|
|---|
| 9900 | no commands, and it is treated as if it had empty commands
|
|---|
| 9901 | (@pxref{Empty Commands}). For example:
|
|---|
| 9902 |
|
|---|
| 9903 | @example
|
|---|
| 9904 | .c.a:
|
|---|
| 9905 | @end example
|
|---|
| 9906 |
|
|---|
| 9907 | @noindent
|
|---|
| 9908 | will override the built-in @file{.c.a} suffix rule.
|
|---|
| 9909 |
|
|---|
| 9910 | We feel that it is cleaner for a rule without commands to always simply
|
|---|
| 9911 | add to the prerequisite list for the target. The above example can be
|
|---|
| 9912 | easily rewritten to get the desired behavior in GNU @code{make}:
|
|---|
| 9913 |
|
|---|
| 9914 | @example
|
|---|
| 9915 | .c.a: ;
|
|---|
| 9916 | @end example
|
|---|
| 9917 |
|
|---|
| 9918 | @item
|
|---|
| 9919 | Some versions of @code{make} invoke the shell with the @samp{-e} flag,
|
|---|
| 9920 | except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
|
|---|
| 9921 | Program}). The @samp{-e} flag tells the shell to exit as soon as any
|
|---|
| 9922 | program it runs returns a nonzero status. We feel it is cleaner to
|
|---|
| 9923 | write each shell command line to stand on its own and not require this
|
|---|
| 9924 | special treatment.
|
|---|
| 9925 | @end itemize
|
|---|
| 9926 |
|
|---|
| 9927 | @comment The makefile standards are in a separate file that is also
|
|---|
| 9928 | @comment included by standards.texi.
|
|---|
| 9929 | @include make-stds.texi
|
|---|
| 9930 |
|
|---|
| 9931 | @node Quick Reference, Error Messages, Makefile Conventions, Top
|
|---|
| 9932 | @appendix Quick Reference
|
|---|
| 9933 |
|
|---|
| 9934 | This appendix summarizes the directives, text manipulation functions,
|
|---|
| 9935 | and special variables which GNU @code{make} understands.
|
|---|
| 9936 | @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
|---|
| 9937 | and @ref{Options Summary, ,Summary of Options},
|
|---|
| 9938 | for other summaries.
|
|---|
| 9939 |
|
|---|
| 9940 | Here is a summary of the directives GNU @code{make} recognizes:
|
|---|
| 9941 |
|
|---|
| 9942 | @table @code
|
|---|
| 9943 | @item define @var{variable}
|
|---|
| 9944 | @itemx endef
|
|---|
| 9945 |
|
|---|
| 9946 | Define a multi-line, recursively-expanded variable.@*
|
|---|
| 9947 | @xref{Sequences}.
|
|---|
| 9948 |
|
|---|
| 9949 | @item ifdef @var{variable}
|
|---|
| 9950 | @itemx ifndef @var{variable}
|
|---|
| 9951 | @itemx ifeq (@var{a},@var{b})
|
|---|
| 9952 | @itemx ifeq "@var{a}" "@var{b}"
|
|---|
| 9953 | @itemx ifeq '@var{a}' '@var{b}'
|
|---|
| 9954 | @itemx ifneq (@var{a},@var{b})
|
|---|
| 9955 | @itemx ifneq "@var{a}" "@var{b}"
|
|---|
| 9956 | @itemx ifneq '@var{a}' '@var{b}'
|
|---|
| 9957 | @itemx else
|
|---|
| 9958 | @itemx endif
|
|---|
| 9959 |
|
|---|
| 9960 | Conditionally evaluate part of the makefile.@*
|
|---|
| 9961 | @xref{Conditionals}.
|
|---|
| 9962 |
|
|---|
| 9963 | @item include @var{file}
|
|---|
| 9964 | @itemx -include @var{file}
|
|---|
| 9965 | @itemx sinclude @var{file}
|
|---|
| 9966 |
|
|---|
| 9967 | Include another makefile.@*
|
|---|
| 9968 | @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9969 |
|
|---|
| 9970 | @item override @var{variable} = @var{value}
|
|---|
| 9971 | @itemx override @var{variable} := @var{value}
|
|---|
| 9972 | @itemx override @var{variable} += @var{value}
|
|---|
| 9973 | @itemx override @var{variable} ?= @var{value}
|
|---|
| 9974 | @itemx override define @var{variable}
|
|---|
| 9975 | @itemx endef
|
|---|
| 9976 |
|
|---|
| 9977 | Define a variable, overriding any previous definition, even one from
|
|---|
| 9978 | the command line.@*
|
|---|
| 9979 | @xref{Override Directive, ,The @code{override} Directive}.
|
|---|
| 9980 |
|
|---|
| 9981 | @item export
|
|---|
| 9982 |
|
|---|
| 9983 | Tell @code{make} to export all variables to child processes by default.@*
|
|---|
| 9984 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
|---|
| 9985 |
|
|---|
| 9986 | @item export @var{variable}
|
|---|
| 9987 | @itemx export @var{variable} = @var{value}
|
|---|
| 9988 | @itemx export @var{variable} := @var{value}
|
|---|
| 9989 | @itemx export @var{variable} += @var{value}
|
|---|
| 9990 | @itemx export @var{variable} ?= @var{value}
|
|---|
| 9991 | @itemx unexport @var{variable}
|
|---|
| 9992 | Tell @code{make} whether or not to export a particular variable to child
|
|---|
| 9993 | processes.@*
|
|---|
| 9994 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
|---|
| 9995 |
|
|---|
| 9996 | @item vpath @var{pattern} @var{path}
|
|---|
| 9997 | Specify a search path for files matching a @samp{%} pattern.@*
|
|---|
| 9998 | @xref{Selective Search, , The @code{vpath} Directive}.
|
|---|
| 9999 |
|
|---|
| 10000 | @item vpath @var{pattern}
|
|---|
| 10001 | Remove all search paths previously specified for @var{pattern}.
|
|---|
| 10002 |
|
|---|
| 10003 | @item vpath
|
|---|
| 10004 | Remove all search paths previously specified in any @code{vpath}
|
|---|
| 10005 | directive.
|
|---|
| 10006 | @end table
|
|---|
| 10007 |
|
|---|
| 10008 | Here is a summary of the built-in functions (@pxref{Functions}):
|
|---|
| 10009 |
|
|---|
| 10010 | @table @code
|
|---|
| 10011 | @item $(subst @var{from},@var{to},@var{text})
|
|---|
| 10012 | Replace @var{from} with @var{to} in @var{text}.@*
|
|---|
| 10013 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10014 |
|
|---|
| 10015 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
|---|
| 10016 | Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
|
|---|
| 10017 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10018 |
|
|---|
| 10019 | @item $(strip @var{string})
|
|---|
| 10020 | Remove excess whitespace characters from @var{string}.@*
|
|---|
| 10021 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10022 |
|
|---|
| 10023 | @item $(findstring @var{find},@var{text})
|
|---|
| 10024 | Locate @var{find} in @var{text}.@*
|
|---|
| 10025 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10026 |
|
|---|
| 10027 | @item $(filter @var{pattern}@dots{},@var{text})
|
|---|
| 10028 | Select words in @var{text} that match one of the @var{pattern} words.@*
|
|---|
| 10029 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10030 |
|
|---|
| 10031 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
|---|
| 10032 | Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
|
|---|
| 10033 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10034 |
|
|---|
| 10035 | @item $(sort @var{list})
|
|---|
| 10036 | Sort the words in @var{list} lexicographically, removing duplicates.@*
|
|---|
| 10037 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10038 |
|
|---|
| 10039 | @item $(word @var{n},@var{text})
|
|---|
| 10040 | Extract the @var{n}th word (one-origin) of @var{text}.@*
|
|---|
| 10041 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10042 |
|
|---|
| 10043 | @item $(words @var{text})
|
|---|
| 10044 | Count the number of words in @var{text}.@*
|
|---|
| 10045 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10046 |
|
|---|
| 10047 | @item $(wordlist @var{s},@var{e},@var{text})
|
|---|
| 10048 | Returns the list of words in @var{text} from @var{s} to @var{e}.@*
|
|---|
| 10049 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10050 |
|
|---|
| 10051 | @item $(firstword @var{names}@dots{})
|
|---|
| 10052 | Extract the first word of @var{names}.@*
|
|---|
| 10053 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10054 |
|
|---|
| 10055 | @item $(lastword @var{names}@dots{})
|
|---|
| 10056 | Extract the last word of @var{names}.@*
|
|---|
| 10057 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 10058 |
|
|---|
| 10059 | @item $(dir @var{names}@dots{})
|
|---|
| 10060 | Extract the directory part of each file name.@*
|
|---|
| 10061 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10062 |
|
|---|
| 10063 | @item $(notdir @var{names}@dots{})
|
|---|
| 10064 | Extract the non-directory part of each file name.@*
|
|---|
| 10065 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10066 |
|
|---|
| 10067 | @item $(suffix @var{names}@dots{})
|
|---|
| 10068 | Extract the suffix (the last @samp{.} and following characters) of each file name.@*
|
|---|
| 10069 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10070 |
|
|---|
| 10071 | @item $(basename @var{names}@dots{})
|
|---|
| 10072 | Extract the base name (name without suffix) of each file name.@*
|
|---|
| 10073 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10074 |
|
|---|
| 10075 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
|---|
| 10076 | Append @var{suffix} to each word in @var{names}.@*
|
|---|
| 10077 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10078 |
|
|---|
| 10079 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
|---|
| 10080 | Prepend @var{prefix} to each word in @var{names}.@*
|
|---|
| 10081 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10082 |
|
|---|
| 10083 | @item $(join @var{list1},@var{list2})
|
|---|
| 10084 | Join two parallel lists of words.@*
|
|---|
| 10085 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10086 |
|
|---|
| 10087 | @item $(wildcard @var{pattern}@dots{})
|
|---|
| 10088 | Find file names matching a shell file name pattern (@emph{not} a
|
|---|
| 10089 | @samp{%} pattern).@*
|
|---|
| 10090 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
|---|
| 10091 |
|
|---|
| 10092 | @item $(realpath @var{names}@dots{})
|
|---|
| 10093 | For each file name in @var{names}, expand to an absolute name that
|
|---|
| 10094 | does not contain any @code{.}, @code{..}, nor symlinks.@*
|
|---|
| 10095 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10096 |
|
|---|
| 10097 | @item $(abspath @var{names}@dots{})
|
|---|
| 10098 | For each file name in @var{names}, expand to an absolute name that
|
|---|
| 10099 | does not contain any @code{.} or @code{..} components, but preserves
|
|---|
| 10100 | symlinks.@*
|
|---|
| 10101 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 10102 |
|
|---|
| 10103 | @item $(error @var{text}@dots{})
|
|---|
| 10104 |
|
|---|
| 10105 | When this function is evaluated, @code{make} generates a fatal error
|
|---|
| 10106 | with the message @var{text}.@*
|
|---|
| 10107 | @xref{Make Control Functions, ,Functions That Control Make}.
|
|---|
| 10108 |
|
|---|
| 10109 | @item $(warning @var{text}@dots{})
|
|---|
| 10110 |
|
|---|
| 10111 | When this function is evaluated, @code{make} generates a warning with
|
|---|
| 10112 | the message @var{text}.@*
|
|---|
| 10113 | @xref{Make Control Functions, ,Functions That Control Make}.
|
|---|
| 10114 |
|
|---|
| 10115 | @item $(shell @var{command})
|
|---|
| 10116 |
|
|---|
| 10117 | Execute a shell command and return its output.@*
|
|---|
| 10118 | @xref{Shell Function, , The @code{shell} Function}.
|
|---|
| 10119 |
|
|---|
| 10120 | @item $(origin @var{variable})
|
|---|
| 10121 |
|
|---|
| 10122 | Return a string describing how the @code{make} variable @var{variable} was
|
|---|
| 10123 | defined.@*
|
|---|
| 10124 | @xref{Origin Function, , The @code{origin} Function}.
|
|---|
| 10125 |
|
|---|
| 10126 | @item $(foreach @var{var},@var{words},@var{text})
|
|---|
| 10127 |
|
|---|
| 10128 | Evaluate @var{text} with @var{var} bound to each word in @var{words},
|
|---|
| 10129 | and concatenate the results.@*
|
|---|
| 10130 | @xref{Foreach Function, ,The @code{foreach} Function}.
|
|---|
| 10131 |
|
|---|
| 10132 | @item $(call @var{var},@var{param},@dots{})
|
|---|
| 10133 |
|
|---|
| 10134 | Evaluate the variable @var{var} replacing any references to @code{$(1)},
|
|---|
| 10135 | @code{$(2)} with the first, second, etc. @var{param} values.@*
|
|---|
| 10136 | @xref{Call Function, ,The @code{call} Function}.
|
|---|
| 10137 |
|
|---|
| 10138 | @item $(eval @var{text})
|
|---|
| 10139 |
|
|---|
| 10140 | Evaluate @var{text} then read the results as makefile commands.
|
|---|
| 10141 | Expands to the empty string.@*
|
|---|
| 10142 | @xref{Eval Function, ,The @code{eval} Function}.
|
|---|
| 10143 |
|
|---|
| 10144 | @item $(value @var{var})
|
|---|
| 10145 |
|
|---|
| 10146 | Evaluates to the contents of the variable @var{var}, with no expansion
|
|---|
| 10147 | performed on it.@*
|
|---|
| 10148 | @xref{Value Function, ,The @code{value} Function}.
|
|---|
| 10149 | @end table
|
|---|
| 10150 |
|
|---|
| 10151 | Here is a summary of the automatic variables.
|
|---|
| 10152 | @xref{Automatic Variables},
|
|---|
| 10153 | for full information.
|
|---|
| 10154 |
|
|---|
| 10155 | @table @code
|
|---|
| 10156 | @item $@@
|
|---|
| 10157 | The file name of the target.
|
|---|
| 10158 |
|
|---|
| 10159 | @item $%
|
|---|
| 10160 | The target member name, when the target is an archive member.
|
|---|
| 10161 |
|
|---|
| 10162 | @item $<
|
|---|
| 10163 | The name of the first prerequisite.
|
|---|
| 10164 |
|
|---|
| 10165 | @item $?
|
|---|
| 10166 | The names of all the prerequisites that are
|
|---|
| 10167 | newer than the target, with spaces between them.
|
|---|
| 10168 | For prerequisites which are archive members, only
|
|---|
| 10169 | the member named is used (@pxref{Archives}).
|
|---|
| 10170 |
|
|---|
| 10171 | @item $^
|
|---|
| 10172 | @itemx $+
|
|---|
| 10173 | The names of all the prerequisites, with spaces between them. For
|
|---|
| 10174 | prerequisites which are archive members, only the member named is used
|
|---|
| 10175 | (@pxref{Archives}). The value of @code{$^} omits duplicate
|
|---|
| 10176 | prerequisites, while @code{$+} retains them and preserves their order.
|
|---|
| 10177 |
|
|---|
| 10178 | @item $*
|
|---|
| 10179 | The stem with which an implicit rule matches
|
|---|
| 10180 | (@pxref{Pattern Match, ,How Patterns Match}).
|
|---|
| 10181 |
|
|---|
| 10182 | @item $(@@D)
|
|---|
| 10183 | @itemx $(@@F)
|
|---|
| 10184 | The directory part and the file-within-directory part of @code{$@@}.
|
|---|
| 10185 |
|
|---|
| 10186 | @item $(*D)
|
|---|
| 10187 | @itemx $(*F)
|
|---|
| 10188 | The directory part and the file-within-directory part of @code{$*}.
|
|---|
| 10189 |
|
|---|
| 10190 | @item $(%D)
|
|---|
| 10191 | @itemx $(%F)
|
|---|
| 10192 | The directory part and the file-within-directory part of @code{$%}.
|
|---|
| 10193 |
|
|---|
| 10194 | @item $(<D)
|
|---|
| 10195 | @itemx $(<F)
|
|---|
| 10196 | The directory part and the file-within-directory part of @code{$<}.
|
|---|
| 10197 |
|
|---|
| 10198 | @item $(^D)
|
|---|
| 10199 | @itemx $(^F)
|
|---|
| 10200 | The directory part and the file-within-directory part of @code{$^}.
|
|---|
| 10201 |
|
|---|
| 10202 | @item $(+D)
|
|---|
| 10203 | @itemx $(+F)
|
|---|
| 10204 | The directory part and the file-within-directory part of @code{$+}.
|
|---|
| 10205 |
|
|---|
| 10206 | @item $(?D)
|
|---|
| 10207 | @itemx $(?F)
|
|---|
| 10208 | The directory part and the file-within-directory part of @code{$?}.
|
|---|
| 10209 | @end table
|
|---|
| 10210 |
|
|---|
| 10211 | These variables are used specially by GNU @code{make}:
|
|---|
| 10212 |
|
|---|
| 10213 | @table @code
|
|---|
| 10214 | @item MAKEFILES
|
|---|
| 10215 |
|
|---|
| 10216 | Makefiles to be read on every invocation of @code{make}.@*
|
|---|
| 10217 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
|---|
| 10218 |
|
|---|
| 10219 | @item VPATH
|
|---|
| 10220 |
|
|---|
| 10221 | Directory search path for files not found in the current directory.@*
|
|---|
| 10222 | @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
|
|---|
| 10223 |
|
|---|
| 10224 | @item SHELL
|
|---|
| 10225 |
|
|---|
| 10226 | The name of the system default command interpreter, usually @file{/bin/sh}.
|
|---|
| 10227 | You can set @code{SHELL} in the makefile to change the shell used to run
|
|---|
| 10228 | commands. @xref{Execution, ,Command Execution}. The @code{SHELL}
|
|---|
| 10229 | variable is handled specially when importing from and exporting to the
|
|---|
| 10230 | environment. @xref{Environment, ,Using Variable from the Environment}.
|
|---|
| 10231 |
|
|---|
| 10232 | @item MAKESHELL
|
|---|
| 10233 |
|
|---|
| 10234 | On MS-DOS only, the name of the command interpreter that is to be used
|
|---|
| 10235 | by @code{make}. This value takes precedence over the value of
|
|---|
| 10236 | @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
|
|---|
| 10237 |
|
|---|
| 10238 | @item MAKE
|
|---|
| 10239 |
|
|---|
| 10240 | The name with which @code{make} was invoked.
|
|---|
| 10241 | Using this variable in commands has special meaning.
|
|---|
| 10242 | @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
|
|---|
| 10243 |
|
|---|
| 10244 | @item MAKELEVEL
|
|---|
| 10245 |
|
|---|
| 10246 | The number of levels of recursion (sub-@code{make}s).@*
|
|---|
| 10247 | @xref{Variables/Recursion}.
|
|---|
| 10248 |
|
|---|
| 10249 | @item MAKEFLAGS
|
|---|
| 10250 |
|
|---|
| 10251 | The flags given to @code{make}. You can set this in the environment or
|
|---|
| 10252 | a makefile to set flags.@*
|
|---|
| 10253 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 10254 |
|
|---|
| 10255 | It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
|
|---|
| 10256 | command line: its contents may not be quoted correctly for use in the
|
|---|
| 10257 | shell. Always allow recursive @code{make}'s to obtain these values
|
|---|
| 10258 | through the environment from its parent.
|
|---|
| 10259 |
|
|---|
| 10260 | @item MAKECMDGOALS
|
|---|
| 10261 |
|
|---|
| 10262 | The targets given to @code{make} on the command line. Setting this
|
|---|
| 10263 | variable has no effect on the operation of @code{make}.@*
|
|---|
| 10264 | @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 10265 |
|
|---|
| 10266 | @item CURDIR
|
|---|
| 10267 |
|
|---|
| 10268 | Set to the pathname of the current working directory (after all
|
|---|
| 10269 | @code{-C} options are processed, if any). Setting this variable has no
|
|---|
| 10270 | effect on the operation of @code{make}.@*
|
|---|
| 10271 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 10272 |
|
|---|
| 10273 | @item SUFFIXES
|
|---|
| 10274 |
|
|---|
| 10275 | The default list of suffixes before @code{make} reads any makefiles.
|
|---|
| 10276 |
|
|---|
| 10277 | @item .LIBPATTERNS
|
|---|
| 10278 | Defines the naming of the libraries @code{make} searches for, and their
|
|---|
| 10279 | order.@*
|
|---|
| 10280 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
|---|
| 10281 | @end table
|
|---|
| 10282 |
|
|---|
| 10283 | @node Error Messages, Complex Makefile, Quick Reference, Top
|
|---|
| 10284 | @comment node-name, next, previous, up
|
|---|
| 10285 | @appendix Errors Generated by Make
|
|---|
| 10286 |
|
|---|
| 10287 | Here is a list of the more common errors you might see generated by
|
|---|
| 10288 | @code{make}, and some information about what they mean and how to fix
|
|---|
| 10289 | them.
|
|---|
| 10290 |
|
|---|
| 10291 | Sometimes @code{make} errors are not fatal, especially in the presence
|
|---|
| 10292 | of a @code{-} prefix on a command script line, or the @code{-k} command
|
|---|
| 10293 | line option. Errors that are fatal are prefixed with the string
|
|---|
| 10294 | @code{***}.
|
|---|
| 10295 |
|
|---|
| 10296 | Error messages are all either prefixed with the name of the program
|
|---|
| 10297 | (usually @samp{make}), or, if the error is found in a makefile, the name
|
|---|
| 10298 | of the file and linenumber containing the problem.
|
|---|
| 10299 |
|
|---|
| 10300 | In the table below, these common prefixes are left off.
|
|---|
| 10301 |
|
|---|
| 10302 | @table @samp
|
|---|
| 10303 |
|
|---|
| 10304 | @item [@var{foo}] Error @var{NN}
|
|---|
| 10305 | @itemx [@var{foo}] @var{signal description}
|
|---|
| 10306 | These errors are not really @code{make} errors at all. They mean that a
|
|---|
| 10307 | program that @code{make} invoked as part of a command script returned a
|
|---|
| 10308 | non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
|
|---|
| 10309 | as failure, or it exited in some other abnormal fashion (with a
|
|---|
| 10310 | signal of some type). @xref{Errors, ,Errors in Commands}.
|
|---|
| 10311 |
|
|---|
| 10312 | If no @code{***} is attached to the message, then the subprocess failed
|
|---|
| 10313 | but the rule in the makefile was prefixed with the @code{-} special
|
|---|
| 10314 | character, so @code{make} ignored the error.
|
|---|
| 10315 |
|
|---|
| 10316 | @item missing separator. Stop.
|
|---|
| 10317 | @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
|
|---|
| 10318 | This means that @code{make} could not understand much of anything about
|
|---|
| 10319 | the command line it just read. GNU @code{make} looks for various kinds
|
|---|
| 10320 | of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
|
|---|
| 10321 | decide what kind of commandline it's seeing. This means it couldn't
|
|---|
| 10322 | find a valid one.
|
|---|
| 10323 |
|
|---|
| 10324 | One of the most common reasons for this message is that you (or perhaps
|
|---|
| 10325 | your oh-so-helpful editor, as is the case with many MS-Windows editors)
|
|---|
| 10326 | have attempted to indent your command scripts with spaces instead of a
|
|---|
| 10327 | TAB character. In this case, @code{make} will use the second form of
|
|---|
| 10328 | the error above. Remember that every line in the command script must
|
|---|
| 10329 | begin with a TAB character. Eight spaces do not count. @xref{Rule
|
|---|
| 10330 | Syntax}.
|
|---|
| 10331 |
|
|---|
| 10332 | @item commands commence before first target. Stop.
|
|---|
| 10333 | @itemx missing rule before commands. Stop.
|
|---|
| 10334 | This means the first thing in the makefile seems to be part of a command
|
|---|
| 10335 | script: it begins with a TAB character and doesn't appear to be a legal
|
|---|
| 10336 | @code{make} command (such as a variable assignment). Command scripts
|
|---|
| 10337 | must always be associated with a target.
|
|---|
| 10338 |
|
|---|
| 10339 | The second form is generated if the line has a semicolon as the first
|
|---|
| 10340 | non-whitespace character; @code{make} interprets this to mean you left
|
|---|
| 10341 | out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
|
|---|
| 10342 |
|
|---|
| 10343 | @item No rule to make target `@var{xxx}'.
|
|---|
| 10344 | @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
|
|---|
| 10345 | This means that @code{make} decided it needed to build a target, but
|
|---|
| 10346 | then couldn't find any instructions in the makefile on how to do that,
|
|---|
| 10347 | either explicit or implicit (including in the default rules database).
|
|---|
| 10348 |
|
|---|
| 10349 | If you want that file to be built, you will need to add a rule to your
|
|---|
| 10350 | makefile describing how that target can be built. Other possible
|
|---|
| 10351 | sources of this problem are typos in the makefile (if that filename is
|
|---|
| 10352 | wrong) or a corrupted source tree (if that file is not supposed to be
|
|---|
| 10353 | built, but rather only a prerequisite).
|
|---|
| 10354 |
|
|---|
| 10355 | @item No targets specified and no makefile found. Stop.
|
|---|
| 10356 | @itemx No targets. Stop.
|
|---|
| 10357 | The former means that you didn't provide any targets to be built on the
|
|---|
| 10358 | command line, and @code{make} couldn't find any makefiles to read in.
|
|---|
| 10359 | The latter means that some makefile was found, but it didn't contain any
|
|---|
| 10360 | default goal and none was given on the command line. GNU @code{make}
|
|---|
| 10361 | has nothing to do in these situations.
|
|---|
| 10362 | @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
|
|---|
| 10363 |
|
|---|
| 10364 | @item Makefile `@var{xxx}' was not found.
|
|---|
| 10365 | @itemx Included makefile `@var{xxx}' was not found.
|
|---|
| 10366 | A makefile specified on the command line (first form) or included
|
|---|
| 10367 | (second form) was not found.
|
|---|
| 10368 |
|
|---|
| 10369 | @item warning: overriding commands for target `@var{xxx}'
|
|---|
| 10370 | @itemx warning: ignoring old commands for target `@var{xxx}'
|
|---|
| 10371 | GNU @code{make} allows commands to be specified only once per target
|
|---|
| 10372 | (except for double-colon rules). If you give commands for a target
|
|---|
| 10373 | which already has been defined to have commands, this warning is issued
|
|---|
| 10374 | and the second set of commands will overwrite the first set.
|
|---|
| 10375 | @xref{Multiple Rules, ,Multiple Rules for One Target}.
|
|---|
| 10376 |
|
|---|
| 10377 | @item Circular @var{xxx} <- @var{yyy} dependency dropped.
|
|---|
| 10378 | This means that @code{make} detected a loop in the dependency graph:
|
|---|
| 10379 | after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
|
|---|
| 10380 | prerequisites, etc., one of them depended on @var{xxx} again.
|
|---|
| 10381 |
|
|---|
| 10382 | @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
|
|---|
| 10383 | This means you've defined a normal (recursive) @code{make} variable
|
|---|
| 10384 | @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
|
|---|
| 10385 | This is not allowed; either use simply-expanded variables (@code{:=}) or
|
|---|
| 10386 | use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
|
|---|
| 10387 | Variables}.
|
|---|
| 10388 |
|
|---|
| 10389 | @item Unterminated variable reference. Stop.
|
|---|
| 10390 | This means you forgot to provide the proper closing parenthesis
|
|---|
| 10391 | or brace in your variable or function reference.
|
|---|
| 10392 |
|
|---|
| 10393 | @item insufficient arguments to function `@var{xxx}'. Stop.
|
|---|
| 10394 | This means you haven't provided the requisite number of arguments for
|
|---|
| 10395 | this function. See the documentation of the function for a description
|
|---|
| 10396 | of its arguments. @xref{Functions, ,Functions for Transforming Text}.
|
|---|
| 10397 |
|
|---|
| 10398 | @item missing target pattern. Stop.
|
|---|
| 10399 | @itemx multiple target patterns. Stop.
|
|---|
| 10400 | @itemx target pattern contains no `%'. Stop.
|
|---|
| 10401 | @itemx mixed implicit and static pattern rules. Stop.
|
|---|
| 10402 | These are generated for malformed static pattern rules. The first means
|
|---|
| 10403 | there's no pattern in the target section of the rule; the second means
|
|---|
| 10404 | there are multiple patterns in the target section; the third means
|
|---|
| 10405 | the target doesn't contain a pattern character (@code{%}); and the
|
|---|
| 10406 | fourth means that all three parts of the static pattern rule contain
|
|---|
| 10407 | pattern characters (@code{%})--only the first two parts should.
|
|---|
| 10408 | @xref{Static Usage, ,Syntax of Static Pattern Rules}.
|
|---|
| 10409 |
|
|---|
| 10410 | @item warning: -jN forced in submake: disabling jobserver mode.
|
|---|
| 10411 | This warning and the next are generated if @code{make} detects error
|
|---|
| 10412 | conditions related to parallel processing on systems where
|
|---|
| 10413 | sub-@code{make}s can communicate (@pxref{Options/Recursion,
|
|---|
| 10414 | ,Communicating Options to a Sub-@code{make}}). This warning is
|
|---|
| 10415 | generated if a recursive invocation of a @code{make} process is forced
|
|---|
| 10416 | to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
|
|---|
| 10417 | than one). This could happen, for example, if you set the @code{MAKE}
|
|---|
| 10418 | environment variable to @samp{make -j2}. In this case, the
|
|---|
| 10419 | sub-@code{make} doesn't communicate with other @code{make} processes and
|
|---|
| 10420 | will simply pretend it has two jobs of its own.
|
|---|
| 10421 |
|
|---|
| 10422 | @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
|
|---|
| 10423 | In order for @code{make} processes to communicate, the parent will pass
|
|---|
| 10424 | information to the child. Since this could result in problems if the
|
|---|
| 10425 | child process isn't actually a @code{make}, the parent will only do this
|
|---|
| 10426 | if it thinks the child is a @code{make}. The parent uses the normal
|
|---|
| 10427 | algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
|
|---|
| 10428 | Variable Works}). If the makefile is constructed such that the parent
|
|---|
| 10429 | doesn't know the child is a @code{make} process, then the child will
|
|---|
| 10430 | receive only part of the information necessary. In this case, the child
|
|---|
| 10431 | will generate this warning message and proceed with its build in a
|
|---|
| 10432 | sequential manner.
|
|---|
| 10433 |
|
|---|
| 10434 | @end table
|
|---|
| 10435 |
|
|---|
| 10436 | @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
|
|---|
| 10437 | @appendix Complex Makefile Example
|
|---|
| 10438 |
|
|---|
| 10439 | Here is the makefile for the GNU @code{tar} program. This is a
|
|---|
| 10440 | moderately complex makefile.
|
|---|
| 10441 |
|
|---|
| 10442 | Because it is the first target, the default goal is @samp{all}. An
|
|---|
| 10443 | interesting feature of this makefile is that @file{testpad.h} is a
|
|---|
| 10444 | source file automatically created by the @code{testpad} program,
|
|---|
| 10445 | itself compiled from @file{testpad.c}.
|
|---|
| 10446 |
|
|---|
| 10447 | If you type @samp{make} or @samp{make all}, then @code{make} creates
|
|---|
| 10448 | the @file{tar} executable, the @file{rmt} daemon that provides
|
|---|
| 10449 | remote tape access, and the @file{tar.info} Info file.
|
|---|
| 10450 |
|
|---|
| 10451 | If you type @samp{make install}, then @code{make} not only creates
|
|---|
| 10452 | @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
|
|---|
| 10453 | them.
|
|---|
| 10454 |
|
|---|
| 10455 | If you type @samp{make clean}, then @code{make} removes the @samp{.o}
|
|---|
| 10456 | files, and the @file{tar}, @file{rmt}, @file{testpad},
|
|---|
| 10457 | @file{testpad.h}, and @file{core} files.
|
|---|
| 10458 |
|
|---|
| 10459 | If you type @samp{make distclean}, then @code{make} not only removes
|
|---|
| 10460 | the same files as does @samp{make clean} but also the
|
|---|
| 10461 | @file{TAGS}, @file{Makefile}, and @file{config.status} files.
|
|---|
| 10462 | (Although it is not evident, this makefile (and
|
|---|
| 10463 | @file{config.status}) is generated by the user with the
|
|---|
| 10464 | @code{configure} program, which is provided in the @code{tar}
|
|---|
| 10465 | distribution, but is not shown here.)
|
|---|
| 10466 |
|
|---|
| 10467 | If you type @samp{make realclean}, then @code{make} removes the same
|
|---|
| 10468 | files as does @samp{make distclean} and also removes the Info files
|
|---|
| 10469 | generated from @file{tar.texinfo}.
|
|---|
| 10470 |
|
|---|
| 10471 | In addition, there are targets @code{shar} and @code{dist} that create
|
|---|
| 10472 | distribution kits.
|
|---|
| 10473 |
|
|---|
| 10474 | @example
|
|---|
| 10475 | @group
|
|---|
| 10476 | # Generated automatically from Makefile.in by configure.
|
|---|
| 10477 | # Un*x Makefile for GNU tar program.
|
|---|
| 10478 | # Copyright (C) 1991 Free Software Foundation, Inc.
|
|---|
| 10479 | @end group
|
|---|
| 10480 |
|
|---|
| 10481 | @group
|
|---|
| 10482 | # This program is free software; you can redistribute
|
|---|
| 10483 | # it and/or modify it under the terms of the GNU
|
|---|
| 10484 | # General Public License @dots{}
|
|---|
| 10485 | @dots{}
|
|---|
| 10486 | @dots{}
|
|---|
| 10487 | @end group
|
|---|
| 10488 |
|
|---|
| 10489 | SHELL = /bin/sh
|
|---|
| 10490 |
|
|---|
| 10491 | #### Start of system configuration section. ####
|
|---|
| 10492 |
|
|---|
| 10493 | srcdir = .
|
|---|
| 10494 |
|
|---|
| 10495 | @group
|
|---|
| 10496 | # If you use gcc, you should either run the
|
|---|
| 10497 | # fixincludes script that comes with it or else use
|
|---|
| 10498 | # gcc with the -traditional option. Otherwise ioctl
|
|---|
| 10499 | # calls will be compiled incorrectly on some systems.
|
|---|
| 10500 | CC = gcc -O
|
|---|
| 10501 | YACC = bison -y
|
|---|
| 10502 | INSTALL = /usr/local/bin/install -c
|
|---|
| 10503 | INSTALLDATA = /usr/local/bin/install -c -m 644
|
|---|
| 10504 | @end group
|
|---|
| 10505 |
|
|---|
| 10506 | # Things you might add to DEFS:
|
|---|
| 10507 | # -DSTDC_HEADERS If you have ANSI C headers and
|
|---|
| 10508 | # libraries.
|
|---|
| 10509 | # -DPOSIX If you have POSIX.1 headers and
|
|---|
| 10510 | # libraries.
|
|---|
| 10511 | # -DBSD42 If you have sys/dir.h (unless
|
|---|
| 10512 | # you use -DPOSIX), sys/file.h,
|
|---|
| 10513 | # and st_blocks in `struct stat'.
|
|---|
| 10514 | # -DUSG If you have System V/ANSI C
|
|---|
| 10515 | # string and memory functions
|
|---|
| 10516 | # and headers, sys/sysmacros.h,
|
|---|
| 10517 | # fcntl.h, getcwd, no valloc,
|
|---|
| 10518 | # and ndir.h (unless
|
|---|
| 10519 | # you use -DDIRENT).
|
|---|
| 10520 | # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
|
|---|
| 10521 | # include memory.h.
|
|---|
| 10522 | # -DDIRENT If USG and you have dirent.h
|
|---|
| 10523 | # instead of ndir.h.
|
|---|
| 10524 | # -DSIGTYPE=int If your signal handlers
|
|---|
| 10525 | # return int, not void.
|
|---|
| 10526 | # -DNO_MTIO If you lack sys/mtio.h
|
|---|
| 10527 | # (magtape ioctls).
|
|---|
| 10528 | # -DNO_REMOTE If you do not have a remote shell
|
|---|
| 10529 | # or rexec.
|
|---|
| 10530 | # -DUSE_REXEC To use rexec for remote tape
|
|---|
| 10531 | # operations instead of
|
|---|
| 10532 | # forking rsh or remsh.
|
|---|
| 10533 | # -DVPRINTF_MISSING If you lack vprintf function
|
|---|
| 10534 | # (but have _doprnt).
|
|---|
| 10535 | # -DDOPRNT_MISSING If you lack _doprnt function.
|
|---|
| 10536 | # Also need to define
|
|---|
| 10537 | # -DVPRINTF_MISSING.
|
|---|
| 10538 | # -DFTIME_MISSING If you lack ftime system call.
|
|---|
| 10539 | # -DSTRSTR_MISSING If you lack strstr function.
|
|---|
| 10540 | # -DVALLOC_MISSING If you lack valloc function.
|
|---|
| 10541 | # -DMKDIR_MISSING If you lack mkdir and
|
|---|
| 10542 | # rmdir system calls.
|
|---|
| 10543 | # -DRENAME_MISSING If you lack rename system call.
|
|---|
| 10544 | # -DFTRUNCATE_MISSING If you lack ftruncate
|
|---|
| 10545 | # system call.
|
|---|
| 10546 | # -DV7 On Version 7 Unix (not
|
|---|
| 10547 | # tested in a long time).
|
|---|
| 10548 | # -DEMUL_OPEN3 If you lack a 3-argument version
|
|---|
| 10549 | # of open, and want to emulate it
|
|---|
| 10550 | # with system calls you do have.
|
|---|
| 10551 | # -DNO_OPEN3 If you lack the 3-argument open
|
|---|
| 10552 | # and want to disable the tar -k
|
|---|
| 10553 | # option instead of emulating open.
|
|---|
| 10554 | # -DXENIX If you have sys/inode.h
|
|---|
| 10555 | # and need it 94 to be included.
|
|---|
| 10556 |
|
|---|
| 10557 | DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
|
|---|
| 10558 | -DVPRINTF_MISSING -DBSD42
|
|---|
| 10559 | # Set this to rtapelib.o unless you defined NO_REMOTE,
|
|---|
| 10560 | # in which case make it empty.
|
|---|
| 10561 | RTAPELIB = rtapelib.o
|
|---|
| 10562 | LIBS =
|
|---|
| 10563 | DEF_AR_FILE = /dev/rmt8
|
|---|
| 10564 | DEFBLOCKING = 20
|
|---|
| 10565 |
|
|---|
| 10566 | @group
|
|---|
| 10567 | CDEBUG = -g
|
|---|
| 10568 | CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
|
|---|
| 10569 | -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
|
|---|
| 10570 | -DDEFBLOCKING=$(DEFBLOCKING)
|
|---|
| 10571 | LDFLAGS = -g
|
|---|
| 10572 | @end group
|
|---|
| 10573 |
|
|---|
| 10574 | @group
|
|---|
| 10575 | prefix = /usr/local
|
|---|
| 10576 | # Prefix for each installed program,
|
|---|
| 10577 | # normally empty or `g'.
|
|---|
| 10578 | binprefix =
|
|---|
| 10579 |
|
|---|
| 10580 | # The directory to install tar in.
|
|---|
| 10581 | bindir = $(prefix)/bin
|
|---|
| 10582 |
|
|---|
| 10583 | # The directory to install the info files in.
|
|---|
| 10584 | infodir = $(prefix)/info
|
|---|
| 10585 | @end group
|
|---|
| 10586 |
|
|---|
| 10587 | #### End of system configuration section. ####
|
|---|
| 10588 |
|
|---|
| 10589 | SRC1 = tar.c create.c extract.c buffer.c \
|
|---|
| 10590 | getoldopt.c update.c gnu.c mangle.c
|
|---|
| 10591 | SRC2 = version.c list.c names.c diffarch.c \
|
|---|
| 10592 | port.c wildmat.c getopt.c
|
|---|
| 10593 | SRC3 = getopt1.c regex.c getdate.y
|
|---|
| 10594 | SRCS = $(SRC1) $(SRC2) $(SRC3)
|
|---|
| 10595 | OBJ1 = tar.o create.o extract.o buffer.o \
|
|---|
| 10596 | getoldopt.o update.o gnu.o mangle.o
|
|---|
| 10597 | OBJ2 = version.o list.o names.o diffarch.o \
|
|---|
| 10598 | port.o wildmat.o getopt.o
|
|---|
| 10599 | OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
|
|---|
| 10600 | OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
|
|---|
| 10601 | @group
|
|---|
| 10602 | AUX = README COPYING ChangeLog Makefile.in \
|
|---|
| 10603 | makefile.pc configure configure.in \
|
|---|
| 10604 | tar.texinfo tar.info* texinfo.tex \
|
|---|
| 10605 | tar.h port.h open3.h getopt.h regex.h \
|
|---|
| 10606 | rmt.h rmt.c rtapelib.c alloca.c \
|
|---|
| 10607 | msd_dir.h msd_dir.c tcexparg.c \
|
|---|
| 10608 | level-0 level-1 backup-specs testpad.c
|
|---|
| 10609 | @end group
|
|---|
| 10610 |
|
|---|
| 10611 | .PHONY: all
|
|---|
| 10612 | all: tar rmt tar.info
|
|---|
| 10613 |
|
|---|
| 10614 | @group
|
|---|
| 10615 | .PHONY: tar
|
|---|
| 10616 | tar: $(OBJS)
|
|---|
| 10617 | $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
|
|---|
| 10618 | @end group
|
|---|
| 10619 |
|
|---|
| 10620 | @group
|
|---|
| 10621 | rmt: rmt.c
|
|---|
| 10622 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
|
|---|
| 10623 | @end group
|
|---|
| 10624 |
|
|---|
| 10625 | @group
|
|---|
| 10626 | tar.info: tar.texinfo
|
|---|
| 10627 | makeinfo tar.texinfo
|
|---|
| 10628 | @end group
|
|---|
| 10629 |
|
|---|
| 10630 | @group
|
|---|
| 10631 | .PHONY: install
|
|---|
| 10632 | install: all
|
|---|
| 10633 | $(INSTALL) tar $(bindir)/$(binprefix)tar
|
|---|
| 10634 | -test ! -f rmt || $(INSTALL) rmt /etc/rmt
|
|---|
| 10635 | $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
|
|---|
| 10636 | @end group
|
|---|
| 10637 |
|
|---|
| 10638 | @group
|
|---|
| 10639 | $(OBJS): tar.h port.h testpad.h
|
|---|
| 10640 | regex.o buffer.o tar.o: regex.h
|
|---|
| 10641 | # getdate.y has 8 shift/reduce conflicts.
|
|---|
| 10642 | @end group
|
|---|
| 10643 |
|
|---|
| 10644 | @group
|
|---|
| 10645 | testpad.h: testpad
|
|---|
| 10646 | ./testpad
|
|---|
| 10647 | @end group
|
|---|
| 10648 |
|
|---|
| 10649 | @group
|
|---|
| 10650 | testpad: testpad.o
|
|---|
| 10651 | $(CC) -o $@@ testpad.o
|
|---|
| 10652 | @end group
|
|---|
| 10653 |
|
|---|
| 10654 | @group
|
|---|
| 10655 | TAGS: $(SRCS)
|
|---|
| 10656 | etags $(SRCS)
|
|---|
| 10657 | @end group
|
|---|
| 10658 |
|
|---|
| 10659 | @group
|
|---|
| 10660 | .PHONY: clean
|
|---|
| 10661 | clean:
|
|---|
| 10662 | rm -f *.o tar rmt testpad testpad.h core
|
|---|
| 10663 | @end group
|
|---|
| 10664 |
|
|---|
| 10665 | @group
|
|---|
| 10666 | .PHONY: distclean
|
|---|
| 10667 | distclean: clean
|
|---|
| 10668 | rm -f TAGS Makefile config.status
|
|---|
| 10669 | @end group
|
|---|
| 10670 |
|
|---|
| 10671 | @group
|
|---|
| 10672 | .PHONY: realclean
|
|---|
| 10673 | realclean: distclean
|
|---|
| 10674 | rm -f tar.info*
|
|---|
| 10675 | @end group
|
|---|
| 10676 |
|
|---|
| 10677 | @group
|
|---|
| 10678 | .PHONY: shar
|
|---|
| 10679 | shar: $(SRCS) $(AUX)
|
|---|
| 10680 | shar $(SRCS) $(AUX) | compress \
|
|---|
| 10681 | > tar-`sed -e '/version_string/!d' \
|
|---|
| 10682 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
|---|
| 10683 | -e q
|
|---|
| 10684 | version.c`.shar.Z
|
|---|
| 10685 | @end group
|
|---|
| 10686 |
|
|---|
| 10687 | @group
|
|---|
| 10688 | .PHONY: dist
|
|---|
| 10689 | dist: $(SRCS) $(AUX)
|
|---|
| 10690 | echo tar-`sed \
|
|---|
| 10691 | -e '/version_string/!d' \
|
|---|
| 10692 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
|---|
| 10693 | -e q
|
|---|
| 10694 | version.c` > .fname
|
|---|
| 10695 | -rm -rf `cat .fname`
|
|---|
| 10696 | mkdir `cat .fname`
|
|---|
| 10697 | ln $(SRCS) $(AUX) `cat .fname`
|
|---|
| 10698 | tar chZf `cat .fname`.tar.Z `cat .fname`
|
|---|
| 10699 | -rm -rf `cat .fname` .fname
|
|---|
| 10700 | @end group
|
|---|
| 10701 |
|
|---|
| 10702 | @group
|
|---|
| 10703 | tar.zoo: $(SRCS) $(AUX)
|
|---|
| 10704 | -rm -rf tmp.dir
|
|---|
| 10705 | -mkdir tmp.dir
|
|---|
| 10706 | -rm tar.zoo
|
|---|
| 10707 | for X in $(SRCS) $(AUX) ; do \
|
|---|
| 10708 | echo $$X ; \
|
|---|
| 10709 | sed 's/$$/^M/' $$X \
|
|---|
| 10710 | > tmp.dir/$$X ; done
|
|---|
| 10711 | cd tmp.dir ; zoo aM ../tar.zoo *
|
|---|
| 10712 | -rm -rf tmp.dir
|
|---|
| 10713 | @end group
|
|---|
| 10714 | @end example
|
|---|
| 10715 |
|
|---|
| 10716 | @raisesections
|
|---|
| 10717 | @include fdl.texi
|
|---|
| 10718 | @lowersections
|
|---|
| 10719 |
|
|---|
| 10720 | @node Concept Index, Name Index, GNU Free Documentation License, Top
|
|---|
| 10721 | @unnumbered Index of Concepts
|
|---|
| 10722 |
|
|---|
| 10723 | @printindex cp
|
|---|
| 10724 |
|
|---|
| 10725 | @node Name Index, , Concept Index, Top
|
|---|
| 10726 | @unnumbered Index of Functions, Variables, & Directives
|
|---|
| 10727 |
|
|---|
| 10728 | @printindex fn
|
|---|
| 10729 |
|
|---|
| 10730 | @summarycontents
|
|---|
| 10731 | @contents
|
|---|
| 10732 | @bye
|
|---|