| 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.18 2004/02/23 06:25:54 psmith Exp $
|
|---|
| 11 | @set EDITION 0.61
|
|---|
| 12 | @set VERSION 3.81
|
|---|
| 13 | @set UPDATED 02 May 2003
|
|---|
| 14 | @set UPDATE-MONTH May 2003
|
|---|
| 15 | @comment The ISBN number might need to change on next publication.
|
|---|
| 16 | @set ISBN 1-882114-81-7 @c From Brian Youmans <3diff@gnu.org>, 25 Apr 2000
|
|---|
| 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
|
|---|
| 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 |
|
|---|
| 166 | Writing Rules
|
|---|
| 167 |
|
|---|
| 168 | * Rule Example:: An example explained.
|
|---|
| 169 | * Rule Syntax:: General syntax explained.
|
|---|
| 170 | * Prerequisite Types:: There are two types of prerequisites.
|
|---|
| 171 | * Wildcards:: Using wildcard characters such as `*'.
|
|---|
| 172 | * Directory Search:: Searching other directories for source files.
|
|---|
| 173 | * Phony Targets:: Using a target that is not a real file's name.
|
|---|
| 174 | * Force Targets:: You can use a target without commands
|
|---|
| 175 | or prerequisites to mark other
|
|---|
| 176 | targets as phony.
|
|---|
| 177 | * Empty Targets:: When only the date matters and the
|
|---|
| 178 | files are empty.
|
|---|
| 179 | * Special Targets:: Targets with special built-in meanings.
|
|---|
| 180 | * Multiple Targets:: When to make use of several targets in a rule.
|
|---|
| 181 | * Multiple Rules:: How to use several rules with the same target.
|
|---|
| 182 | * Static Pattern:: Static pattern rules apply to multiple targets
|
|---|
| 183 | and can vary the prerequisites according to
|
|---|
| 184 | the target name.
|
|---|
| 185 | * Double-Colon:: How to use a special kind of rule to allow
|
|---|
| 186 | several independent rules for one target.
|
|---|
| 187 | * Automatic Prerequisites:: How to automatically generate rules giving
|
|---|
| 188 | prerequisites from source files themselves.
|
|---|
| 189 |
|
|---|
| 190 | Using Wildcard Characters in File Names
|
|---|
| 191 |
|
|---|
| 192 | * Wildcard Examples:: Several examples
|
|---|
| 193 | * Wildcard Pitfall:: Problems to avoid.
|
|---|
| 194 | * Wildcard Function:: How to cause wildcard expansion where
|
|---|
| 195 | it does not normally take place.
|
|---|
| 196 |
|
|---|
| 197 | Searching Directories for Prerequisites
|
|---|
| 198 |
|
|---|
| 199 | * General Search:: Specifying a search path that applies
|
|---|
| 200 | to every prerequisite.
|
|---|
| 201 | * Selective Search:: Specifying a search path
|
|---|
| 202 | for a specified class of names.
|
|---|
| 203 | * Search Algorithm:: When and how search paths are applied.
|
|---|
| 204 | * Commands/Search:: How to write shell commands that work together
|
|---|
| 205 | with search paths.
|
|---|
| 206 | * Implicit/Search:: How search paths affect implicit rules.
|
|---|
| 207 | * Libraries/Search:: Directory search for link libraries.
|
|---|
| 208 |
|
|---|
| 209 | Static Pattern Rules
|
|---|
| 210 |
|
|---|
| 211 | * Static Usage:: The syntax of static pattern rules.
|
|---|
| 212 | * Static versus Implicit:: When are they better than implicit rules?
|
|---|
| 213 |
|
|---|
| 214 | Writing the Commands in Rules
|
|---|
| 215 |
|
|---|
| 216 | * Echoing:: How to control when commands are echoed.
|
|---|
| 217 | * Execution:: How commands are executed.
|
|---|
| 218 | * Parallel:: How commands can be executed in parallel.
|
|---|
| 219 | * Errors:: What happens after a command execution error.
|
|---|
| 220 | * Interrupts:: What happens when a command is interrupted.
|
|---|
| 221 | * Recursion:: Invoking @code{make} from makefiles.
|
|---|
| 222 | * Sequences:: Defining canned sequences of commands.
|
|---|
| 223 | * Empty Commands:: Defining useful, do-nothing commands.
|
|---|
| 224 |
|
|---|
| 225 | Recursive Use of @code{make}
|
|---|
| 226 |
|
|---|
| 227 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
|---|
| 228 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
|---|
| 229 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
|---|
| 230 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
|---|
| 231 | helps debug use of recursive @code{make} commands.
|
|---|
| 232 |
|
|---|
| 233 | How to Use Variables
|
|---|
| 234 |
|
|---|
| 235 | * Reference:: How to use the value of a variable.
|
|---|
| 236 | * Flavors:: Variables come in two flavors.
|
|---|
| 237 | * Advanced:: Advanced features for referencing a variable.
|
|---|
| 238 | * Values:: All the ways variables get their values.
|
|---|
| 239 | * Setting:: How to set a variable in the makefile.
|
|---|
| 240 | * Appending:: How to append more text to the old value
|
|---|
| 241 | of a variable.
|
|---|
| 242 | * Override Directive:: How to set a variable in the makefile even if
|
|---|
| 243 | the user has set it with a command argument.
|
|---|
| 244 | * Defining:: An alternate way to set a variable
|
|---|
| 245 | to a verbatim string.
|
|---|
| 246 | * Environment:: Variable values can come from the environment.
|
|---|
| 247 | * Target-specific:: Variable values can be defined on a per-target
|
|---|
| 248 | basis.
|
|---|
| 249 | * Pattern-specific:: Target-specific variable values can be applied
|
|---|
| 250 | to a group of targets that match a pattern.
|
|---|
| 251 |
|
|---|
| 252 | Advanced Features for Reference to Variables
|
|---|
| 253 |
|
|---|
| 254 | * Substitution Refs:: Referencing a variable with
|
|---|
| 255 | substitutions on the value.
|
|---|
| 256 | * Computed Names:: Computing the name of the variable to refer to.
|
|---|
| 257 |
|
|---|
| 258 | Conditional Parts of Makefiles
|
|---|
| 259 |
|
|---|
| 260 | * Conditional Example:: Example of a conditional
|
|---|
| 261 | * Conditional Syntax:: The syntax of conditionals.
|
|---|
| 262 | * Testing Flags:: Conditionals that test flags.
|
|---|
| 263 |
|
|---|
| 264 | Functions for Transforming Text
|
|---|
| 265 |
|
|---|
| 266 | * Syntax of Functions:: How to write a function call.
|
|---|
| 267 | * Text Functions:: General-purpose text manipulation functions.
|
|---|
| 268 | * File Name Functions:: Functions for manipulating file names.
|
|---|
| 269 | * Foreach Function:: Repeat some text with controlled variation.
|
|---|
| 270 | * If Function:: Conditionally expand a value.
|
|---|
| 271 | * Call Function:: Expand a user-defined function.
|
|---|
| 272 | * Value Function:: Return the un-expanded value of a variable.
|
|---|
| 273 | * Eval Function:: Evaluate the arguments as makefile syntax.
|
|---|
| 274 | * Origin Function:: Find where a variable got its value.
|
|---|
| 275 | * Shell Function:: Substitute the output of a shell command.
|
|---|
| 276 | * Make Control Functions:: Functions that control how make runs.
|
|---|
| 277 |
|
|---|
| 278 | How to Run @code{make}
|
|---|
| 279 |
|
|---|
| 280 | * Makefile Arguments:: How to specify which makefile to use.
|
|---|
| 281 | * Goals:: How to use goal arguments to specify which
|
|---|
| 282 | parts of the makefile to use.
|
|---|
| 283 | * Instead of Execution:: How to use mode flags to specify what
|
|---|
| 284 | kind of thing to do with the commands
|
|---|
| 285 | in the makefile other than simply
|
|---|
| 286 | execute them.
|
|---|
| 287 | * Avoiding Compilation:: How to avoid recompiling certain files.
|
|---|
| 288 | * Overriding:: How to override a variable to specify
|
|---|
| 289 | an alternate compiler and other things.
|
|---|
| 290 | * Testing:: How to proceed past some errors, to
|
|---|
| 291 | test compilation.
|
|---|
| 292 | * Options Summary:: Summary of Options
|
|---|
| 293 |
|
|---|
| 294 | Using Implicit Rules
|
|---|
| 295 |
|
|---|
| 296 | * Using Implicit:: How to use an existing implicit rule
|
|---|
| 297 | to get the commands for updating a file.
|
|---|
| 298 | * Catalogue of Rules:: A list of built-in implicit rules.
|
|---|
| 299 | * Implicit Variables:: How to change what predefined rules do.
|
|---|
| 300 | * Chained Rules:: How to use a chain of implicit rules.
|
|---|
| 301 | * Pattern Rules:: How to define new implicit rules.
|
|---|
| 302 | * Last Resort:: How to defining commands for rules
|
|---|
| 303 | which cannot find any.
|
|---|
| 304 | * Suffix Rules:: The old-fashioned style of implicit rule.
|
|---|
| 305 | * Implicit Rule Search:: The precise algorithm for applying
|
|---|
| 306 | implicit rules.
|
|---|
| 307 |
|
|---|
| 308 | Defining and Redefining Pattern Rules
|
|---|
| 309 |
|
|---|
| 310 | * Pattern Intro:: An introduction to pattern rules.
|
|---|
| 311 | * Pattern Examples:: Examples of pattern rules.
|
|---|
| 312 | * Automatic Variables:: How to use automatic variables in the
|
|---|
| 313 | commands of implicit rules.
|
|---|
| 314 | * Pattern Match:: How patterns match.
|
|---|
| 315 | * Match-Anything Rules:: Precautions you should take prior to
|
|---|
| 316 | defining rules that can match any
|
|---|
| 317 | target file whatever.
|
|---|
| 318 | * Canceling Rules:: How to override or cancel built-in rules.
|
|---|
| 319 |
|
|---|
| 320 | Using @code{make} to Update Archive Files
|
|---|
| 321 |
|
|---|
| 322 | * Archive Members:: Archive members as targets.
|
|---|
| 323 | * Archive Update:: The implicit rule for archive member targets.
|
|---|
| 324 | * Archive Pitfalls:: Dangers to watch out for when using archives.
|
|---|
| 325 | * Archive Suffix Rules:: You can write a special kind of suffix rule
|
|---|
| 326 | for updating archives.
|
|---|
| 327 |
|
|---|
| 328 | Implicit Rule for Archive Member Targets
|
|---|
| 329 |
|
|---|
| 330 | * Archive Symbols:: How to update archive symbol directories.
|
|---|
| 331 |
|
|---|
| 332 | Makefile Conventions
|
|---|
| 333 |
|
|---|
| 334 | * Makefile Basics:: General Conventions for Makefiles
|
|---|
| 335 | * Utilities in Makefiles:: Utilities in Makefiles
|
|---|
| 336 | * Command Variables:: Variables for Specifying Commands
|
|---|
| 337 | * Directory Variables:: Variables for Installation Directories
|
|---|
| 338 | * Standard Targets:: Standard Targets for Users
|
|---|
| 339 | * Install Command Categories:: Three categories of commands in the `install'
|
|---|
| 340 |
|
|---|
| 341 | Copying This Manual
|
|---|
| 342 |
|
|---|
| 343 | @end detailmenu
|
|---|
| 344 | @end menu
|
|---|
| 345 |
|
|---|
| 346 | @node Overview, Introduction, Top, Top
|
|---|
| 347 | @comment node-name, next, previous, up
|
|---|
| 348 | @chapter Overview of @code{make}
|
|---|
| 349 |
|
|---|
| 350 | The @code{make} utility automatically determines which pieces of a large
|
|---|
| 351 | program need to be recompiled, and issues commands to recompile them.
|
|---|
| 352 | This manual describes GNU @code{make}, which was implemented by Richard
|
|---|
| 353 | Stallman and Roland McGrath. Development since Version 3.76 has been
|
|---|
| 354 | handled by Paul D. Smith.
|
|---|
| 355 |
|
|---|
| 356 | GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
|
|---|
| 357 | 1003.2-1992} (POSIX.2).
|
|---|
| 358 | @cindex POSIX
|
|---|
| 359 | @cindex IEEE Standard 1003.2
|
|---|
| 360 | @cindex standards conformance
|
|---|
| 361 |
|
|---|
| 362 | Our examples show C programs, since they are most common, but you can use
|
|---|
| 363 | @code{make} with any programming language whose compiler can be run with a
|
|---|
| 364 | shell command. Indeed, @code{make} is not limited to programs. You can
|
|---|
| 365 | use it to describe any task where some files must be updated automatically
|
|---|
| 366 | from others whenever the others change.
|
|---|
| 367 |
|
|---|
| 368 | @menu
|
|---|
| 369 | * Preparing:: Preparing and Running Make
|
|---|
| 370 | * Reading:: On Reading this Text
|
|---|
| 371 | * Bugs:: Problems and Bugs
|
|---|
| 372 | @end menu
|
|---|
| 373 |
|
|---|
| 374 | @node Preparing, Reading, Overview, Overview
|
|---|
| 375 | @ifnottex
|
|---|
| 376 | @heading Preparing and Running Make
|
|---|
| 377 | @end ifnottex
|
|---|
| 378 |
|
|---|
| 379 | To prepare to use @code{make}, you must write a file called
|
|---|
| 380 | the @dfn{makefile} that describes the relationships among files
|
|---|
| 381 | in your program and provides commands for updating each file.
|
|---|
| 382 | In a program, typically, the executable file is updated from object
|
|---|
| 383 | files, which are in turn made by compiling source files.@refill
|
|---|
| 384 |
|
|---|
| 385 | Once a suitable makefile exists, each time you change some source files,
|
|---|
| 386 | this simple shell command:
|
|---|
| 387 |
|
|---|
| 388 | @example
|
|---|
| 389 | make
|
|---|
| 390 | @end example
|
|---|
| 391 |
|
|---|
| 392 | @noindent
|
|---|
| 393 | suffices to perform all necessary recompilations. The @code{make} program
|
|---|
| 394 | uses the makefile data base and the last-modification times of the files to
|
|---|
| 395 | decide which of the files need to be updated. For each of those files, it
|
|---|
| 396 | issues the commands recorded in the data base.
|
|---|
| 397 |
|
|---|
| 398 | You can provide command line arguments to @code{make} to control which
|
|---|
| 399 | files should be recompiled, or how. @xref{Running, ,How to Run
|
|---|
| 400 | @code{make}}.
|
|---|
| 401 |
|
|---|
| 402 | @node Reading, Bugs, Preparing, Overview
|
|---|
| 403 | @section How to Read This Manual
|
|---|
| 404 |
|
|---|
| 405 | If you are new to @code{make}, or are looking for a general
|
|---|
| 406 | introduction, read the first few sections of each chapter, skipping the
|
|---|
| 407 | later sections. In each chapter, the first few sections contain
|
|---|
| 408 | introductory or general information and the later sections contain
|
|---|
| 409 | specialized or technical information.
|
|---|
| 410 | @ifnottex
|
|---|
| 411 | The exception is the second chapter, @ref{Introduction, ,An
|
|---|
| 412 | Introduction to Makefiles}, all of which is introductory.
|
|---|
| 413 | @end ifnottex
|
|---|
| 414 | @iftex
|
|---|
| 415 | The exception is @ref{Introduction, ,An Introduction to Makefiles},
|
|---|
| 416 | all of which is introductory.
|
|---|
| 417 | @end iftex
|
|---|
| 418 |
|
|---|
| 419 | If you are familiar with other @code{make} programs, see @ref{Features,
|
|---|
| 420 | ,Features of GNU @code{make}}, which lists the enhancements GNU
|
|---|
| 421 | @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
|
|---|
| 422 | Features}, which explains the few things GNU @code{make} lacks that
|
|---|
| 423 | others have.
|
|---|
| 424 |
|
|---|
| 425 | For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
|
|---|
| 426 | and @ref{Special Targets}.
|
|---|
| 427 |
|
|---|
| 428 | @node Bugs, , Reading, Overview
|
|---|
| 429 | @section Problems and Bugs
|
|---|
| 430 | @cindex reporting bugs
|
|---|
| 431 | @cindex bugs, reporting
|
|---|
| 432 | @cindex problems and bugs, reporting
|
|---|
| 433 |
|
|---|
| 434 | If you have problems with GNU @code{make} or think you've found a bug,
|
|---|
| 435 | please report it to the developers; we cannot promise to do anything but
|
|---|
| 436 | we might well want to fix it.
|
|---|
| 437 |
|
|---|
| 438 | Before reporting a bug, make sure you've actually found a real bug.
|
|---|
| 439 | Carefully reread the documentation and see if it really says you can do
|
|---|
| 440 | what you're trying to do. If it's not clear whether you should be able
|
|---|
| 441 | to do something or not, report that too; it's a bug in the
|
|---|
| 442 | documentation!
|
|---|
| 443 |
|
|---|
| 444 | Before reporting a bug or trying to fix it yourself, try to isolate it
|
|---|
| 445 | to the smallest possible makefile that reproduces the problem. Then
|
|---|
| 446 | send us the makefile and the exact results @code{make} gave you,
|
|---|
| 447 | including any error or warning messages. Please don't paraphrase
|
|---|
| 448 | these messages: it's best to cut and paste them into your report.
|
|---|
| 449 | When generating this small makefile, be sure to not use any non-free
|
|---|
| 450 | or unusual tools in your commands: you can almost always emulate what
|
|---|
| 451 | such a tool would do with simple shell commands. Finally, be sure to
|
|---|
| 452 | explain what you expected to occur; this will help us decide whether
|
|---|
| 453 | the problem was really in the documentation.
|
|---|
| 454 |
|
|---|
| 455 | Once you have a precise problem you can report it in one of two ways.
|
|---|
| 456 | Either send electronic mail to:
|
|---|
| 457 |
|
|---|
| 458 | @example
|
|---|
| 459 | bug-make@@gnu.org
|
|---|
| 460 | @end example
|
|---|
| 461 |
|
|---|
| 462 | @noindent
|
|---|
| 463 | or use our Web-based project management tool, at:
|
|---|
| 464 |
|
|---|
| 465 | @example
|
|---|
| 466 | http://savannah.gnu.org/projects/make/
|
|---|
| 467 | @end example
|
|---|
| 468 |
|
|---|
| 469 | @noindent
|
|---|
| 470 | In addition to the information above, please be careful to include the
|
|---|
| 471 | version number of @code{make} you are using. You can get this
|
|---|
| 472 | information with the command @samp{make --version}. Be sure also to
|
|---|
| 473 | include the type of machine and operating system you are using. One
|
|---|
| 474 | way to obtain this information is by looking at the final lines of
|
|---|
| 475 | output from the command @samp{make --help}.
|
|---|
| 476 |
|
|---|
| 477 | @node Introduction, Makefiles, Overview, Top
|
|---|
| 478 | @comment node-name, next, previous, up
|
|---|
| 479 | @chapter An Introduction to Makefiles
|
|---|
| 480 |
|
|---|
| 481 | You need a file called a @dfn{makefile} to tell @code{make} what to do.
|
|---|
| 482 | Most often, the makefile tells @code{make} how to compile and link a
|
|---|
| 483 | program.
|
|---|
| 484 | @cindex makefile
|
|---|
| 485 |
|
|---|
| 486 | In this chapter, we will discuss a simple makefile that describes how to
|
|---|
| 487 | compile and link a text editor which consists of eight C source files
|
|---|
| 488 | and three header files. The makefile can also tell @code{make} how to
|
|---|
| 489 | run miscellaneous commands when explicitly asked (for example, to remove
|
|---|
| 490 | certain files as a clean-up operation). To see a more complex example
|
|---|
| 491 | of a makefile, see @ref{Complex Makefile}.
|
|---|
| 492 |
|
|---|
| 493 | When @code{make} recompiles the editor, each changed C source file
|
|---|
| 494 | must be recompiled. If a header file has changed, each C source file
|
|---|
| 495 | that includes the header file must be recompiled to be safe. Each
|
|---|
| 496 | compilation produces an object file corresponding to the source file.
|
|---|
| 497 | Finally, if any source file has been recompiled, all the object files,
|
|---|
| 498 | whether newly made or saved from previous compilations, must be linked
|
|---|
| 499 | together to produce the new executable editor.
|
|---|
| 500 | @cindex recompilation
|
|---|
| 501 | @cindex editor
|
|---|
| 502 |
|
|---|
| 503 | @menu
|
|---|
| 504 | * Rule Introduction:: What a rule looks like.
|
|---|
| 505 | * Simple Makefile:: A Simple Makefile
|
|---|
| 506 | * How Make Works:: How @code{make} Processes This Makefile
|
|---|
| 507 | * Variables Simplify:: Variables Make Makefiles Simpler
|
|---|
| 508 | * make Deduces:: Letting @code{make} Deduce the Commands
|
|---|
| 509 | * Combine By Prerequisite:: Another Style of Makefile
|
|---|
| 510 | * Cleanup:: Rules for Cleaning the Directory
|
|---|
| 511 | @end menu
|
|---|
| 512 |
|
|---|
| 513 | @node Rule Introduction, Simple Makefile, Introduction, Introduction
|
|---|
| 514 | @comment node-name, next, previous, up
|
|---|
| 515 | @section What a Rule Looks Like
|
|---|
| 516 | @cindex rule, introduction to
|
|---|
| 517 | @cindex makefile rule parts
|
|---|
| 518 | @cindex parts of makefile rule
|
|---|
| 519 |
|
|---|
| 520 | A simple makefile consists of ``rules'' with the following shape:
|
|---|
| 521 |
|
|---|
| 522 | @cindex targets, introduction to
|
|---|
| 523 | @cindex prerequisites, introduction to
|
|---|
| 524 | @cindex commands, introduction to
|
|---|
| 525 | @example
|
|---|
| 526 | @group
|
|---|
| 527 | @var{target} @dots{} : @var{prerequisites} @dots{}
|
|---|
| 528 | @var{command}
|
|---|
| 529 | @dots{}
|
|---|
| 530 | @dots{}
|
|---|
| 531 | @end group
|
|---|
| 532 | @end example
|
|---|
| 533 |
|
|---|
| 534 | A @dfn{target} is usually the name of a file that is generated by a
|
|---|
| 535 | program; examples of targets are executable or object files. A target
|
|---|
| 536 | can also be the name of an action to carry out, such as @samp{clean}
|
|---|
| 537 | (@pxref{Phony Targets}).
|
|---|
| 538 |
|
|---|
| 539 | A @dfn{prerequisite} is a file that is used as input to create the
|
|---|
| 540 | target. A target often depends on several files.
|
|---|
| 541 |
|
|---|
| 542 | @cindex tabs in rules
|
|---|
| 543 | A @dfn{command} is an action that @code{make} carries out.
|
|---|
| 544 | A rule may have more than one command, each on its own line.
|
|---|
| 545 | @strong{Please note:} you need to put a tab character at the beginning of
|
|---|
| 546 | every command line! This is an obscurity that catches the unwary.
|
|---|
| 547 |
|
|---|
| 548 | Usually a command is in a rule with prerequisites and serves to create a
|
|---|
| 549 | target file if any of the prerequisites change. However, the rule that
|
|---|
| 550 | specifies commands for the target need not have prerequisites. For
|
|---|
| 551 | example, the rule containing the delete command associated with the
|
|---|
| 552 | target @samp{clean} does not have prerequisites.
|
|---|
| 553 |
|
|---|
| 554 | A @dfn{rule}, then, explains how and when to remake certain files
|
|---|
| 555 | which are the targets of the particular rule. @code{make} carries out
|
|---|
| 556 | the commands on the prerequisites to create or update the target. A
|
|---|
| 557 | rule can also explain how and when to carry out an action.
|
|---|
| 558 | @xref{Rules, , Writing Rules}.
|
|---|
| 559 |
|
|---|
| 560 | A makefile may contain other text besides rules, but a simple makefile
|
|---|
| 561 | need only contain rules. Rules may look somewhat more complicated
|
|---|
| 562 | than shown in this template, but all fit the pattern more or less.
|
|---|
| 563 |
|
|---|
| 564 | @node Simple Makefile, How Make Works, Rule Introduction, Introduction
|
|---|
| 565 | @section A Simple Makefile
|
|---|
| 566 | @cindex simple makefile
|
|---|
| 567 | @cindex makefile, simple
|
|---|
| 568 |
|
|---|
| 569 | Here is a straightforward makefile that describes the way an
|
|---|
| 570 | executable file called @code{edit} depends on eight object files
|
|---|
| 571 | which, in turn, depend on eight C source and three header files.
|
|---|
| 572 |
|
|---|
| 573 | In this example, all the C files include @file{defs.h}, but only those
|
|---|
| 574 | defining editing commands include @file{command.h}, and only low
|
|---|
| 575 | level files that change the editor buffer include @file{buffer.h}.
|
|---|
| 576 |
|
|---|
| 577 | @example
|
|---|
| 578 | @group
|
|---|
| 579 | edit : main.o kbd.o command.o display.o \
|
|---|
| 580 | insert.o search.o files.o utils.o
|
|---|
| 581 | cc -o edit main.o kbd.o command.o display.o \
|
|---|
| 582 | insert.o search.o files.o utils.o
|
|---|
| 583 |
|
|---|
| 584 | main.o : main.c defs.h
|
|---|
| 585 | cc -c main.c
|
|---|
| 586 | kbd.o : kbd.c defs.h command.h
|
|---|
| 587 | cc -c kbd.c
|
|---|
| 588 | command.o : command.c defs.h command.h
|
|---|
| 589 | cc -c command.c
|
|---|
| 590 | display.o : display.c defs.h buffer.h
|
|---|
| 591 | cc -c display.c
|
|---|
| 592 | insert.o : insert.c defs.h buffer.h
|
|---|
| 593 | cc -c insert.c
|
|---|
| 594 | search.o : search.c defs.h buffer.h
|
|---|
| 595 | cc -c search.c
|
|---|
| 596 | files.o : files.c defs.h buffer.h command.h
|
|---|
| 597 | cc -c files.c
|
|---|
| 598 | utils.o : utils.c defs.h
|
|---|
| 599 | cc -c utils.c
|
|---|
| 600 | clean :
|
|---|
| 601 | rm edit main.o kbd.o command.o display.o \
|
|---|
| 602 | insert.o search.o files.o utils.o
|
|---|
| 603 | @end group
|
|---|
| 604 | @end example
|
|---|
| 605 |
|
|---|
| 606 | @noindent
|
|---|
| 607 | We split each long line into two lines using backslash-newline; this is
|
|---|
| 608 | like using one long line, but is easier to read.
|
|---|
| 609 | @cindex continuation lines
|
|---|
| 610 | @cindex @code{\} (backslash), for continuation lines
|
|---|
| 611 | @cindex backslash (@code{\}), for continuation lines
|
|---|
| 612 | @cindex quoting newline, in makefile
|
|---|
| 613 | @cindex newline, quoting, in makefile
|
|---|
| 614 |
|
|---|
| 615 | To use this makefile to create the executable file called @file{edit},
|
|---|
| 616 | type:
|
|---|
| 617 |
|
|---|
| 618 | @example
|
|---|
| 619 | make
|
|---|
| 620 | @end example
|
|---|
| 621 |
|
|---|
| 622 | To use this makefile to delete the executable file and all the object
|
|---|
| 623 | files from the directory, type:
|
|---|
| 624 |
|
|---|
| 625 | @example
|
|---|
| 626 | make clean
|
|---|
| 627 | @end example
|
|---|
| 628 |
|
|---|
| 629 | In the example makefile, the targets include the executable file
|
|---|
| 630 | @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
|
|---|
| 631 | prerequisites are files such as @samp{main.c} and @samp{defs.h}.
|
|---|
| 632 | In fact, each @samp{.o} file is both a target and a prerequisite.
|
|---|
| 633 | Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
|
|---|
| 634 |
|
|---|
| 635 | When a target is a file, it needs to be recompiled or relinked if any
|
|---|
| 636 | of its prerequisites change. In addition, any prerequisites that are
|
|---|
| 637 | themselves automatically generated should be updated first. In this
|
|---|
| 638 | example, @file{edit} depends on each of the eight object files; the
|
|---|
| 639 | object file @file{main.o} depends on the source file @file{main.c} and
|
|---|
| 640 | on the header file @file{defs.h}.
|
|---|
| 641 |
|
|---|
| 642 | A shell command follows each line that contains a target and
|
|---|
| 643 | prerequisites. These shell commands say how to update the target file.
|
|---|
| 644 | A tab character must come at the beginning of every command line to
|
|---|
| 645 | distinguish commands lines from other lines in the makefile. (Bear in
|
|---|
| 646 | mind that @code{make} does not know anything about how the commands
|
|---|
| 647 | work. It is up to you to supply commands that will update the target
|
|---|
| 648 | file properly. All @code{make} does is execute the commands in the rule
|
|---|
| 649 | you have specified when the target file needs to be updated.)
|
|---|
| 650 | @cindex shell command
|
|---|
| 651 |
|
|---|
| 652 | The target @samp{clean} is not a file, but merely the name of an
|
|---|
| 653 | action. Since you
|
|---|
| 654 | normally
|
|---|
| 655 | do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
|
|---|
| 656 | Consequently, @code{make} never does anything with it unless you tell
|
|---|
| 657 | it specifically. Note that this rule not only is not a prerequisite, it
|
|---|
| 658 | also does not have any prerequisites, so the only purpose of the rule
|
|---|
| 659 | is to run the specified commands. Targets that do not refer to files
|
|---|
| 660 | but are just actions are called @dfn{phony targets}. @xref{Phony
|
|---|
| 661 | Targets}, for information about this kind of target. @xref{Errors, ,
|
|---|
| 662 | Errors in Commands}, to see how to cause @code{make} to ignore errors
|
|---|
| 663 | from @code{rm} or any other command.
|
|---|
| 664 | @cindex @code{clean} target
|
|---|
| 665 | @cindex @code{rm} (shell command)
|
|---|
| 666 |
|
|---|
| 667 | @node How Make Works, Variables Simplify, Simple Makefile, Introduction
|
|---|
| 668 | @comment node-name, next, previous, up
|
|---|
| 669 | @section How @code{make} Processes a Makefile
|
|---|
| 670 | @cindex processing a makefile
|
|---|
| 671 | @cindex makefile, how @code{make} processes
|
|---|
| 672 |
|
|---|
| 673 | By default, @code{make} starts with the first target (not targets whose
|
|---|
| 674 | names start with @samp{.}). This is called the @dfn{default goal}.
|
|---|
| 675 | (@dfn{Goals} are the targets that @code{make} strives ultimately to
|
|---|
| 676 | update. @xref{Goals, , Arguments to Specify the Goals}.)
|
|---|
| 677 | @cindex default goal
|
|---|
| 678 | @cindex goal, default
|
|---|
| 679 | @cindex goal
|
|---|
| 680 |
|
|---|
| 681 | In the simple example of the previous section, the default goal is to
|
|---|
| 682 | update the executable program @file{edit}; therefore, we put that rule
|
|---|
| 683 | first.
|
|---|
| 684 |
|
|---|
| 685 | Thus, when you give the command:
|
|---|
| 686 |
|
|---|
| 687 | @example
|
|---|
| 688 | make
|
|---|
| 689 | @end example
|
|---|
| 690 |
|
|---|
| 691 | @noindent
|
|---|
| 692 | @code{make} reads the makefile in the current directory and begins by
|
|---|
| 693 | processing the first rule. In the example, this rule is for relinking
|
|---|
| 694 | @file{edit}; but before @code{make} can fully process this rule, it
|
|---|
| 695 | must process the rules for the files that @file{edit} depends on,
|
|---|
| 696 | which in this case are the object files. Each of these files is
|
|---|
| 697 | processed according to its own rule. These rules say to update each
|
|---|
| 698 | @samp{.o} file by compiling its source file. The recompilation must
|
|---|
| 699 | be done if the source file, or any of the header files named as
|
|---|
| 700 | prerequisites, is more recent than the object file, or if the object
|
|---|
| 701 | file does not exist.
|
|---|
| 702 |
|
|---|
| 703 | The other rules are processed because their targets appear as
|
|---|
| 704 | prerequisites of the goal. If some other rule is not depended on by the
|
|---|
| 705 | goal (or anything it depends on, etc.), that rule is not processed,
|
|---|
| 706 | unless you tell @code{make} to do so (with a command such as
|
|---|
| 707 | @w{@code{make clean}}).
|
|---|
| 708 |
|
|---|
| 709 | Before recompiling an object file, @code{make} considers updating its
|
|---|
| 710 | prerequisites, the source file and header files. This makefile does not
|
|---|
| 711 | specify anything to be done for them---the @samp{.c} and @samp{.h} files
|
|---|
| 712 | are not the targets of any rules---so @code{make} does nothing for these
|
|---|
| 713 | files. But @code{make} would update automatically generated C programs,
|
|---|
| 714 | such as those made by Bison or Yacc, by their own rules at this time.
|
|---|
| 715 |
|
|---|
| 716 | After recompiling whichever object files need it, @code{make} decides
|
|---|
| 717 | whether to relink @file{edit}. This must be done if the file
|
|---|
| 718 | @file{edit} does not exist, or if any of the object files are newer than
|
|---|
| 719 | it. If an object file was just recompiled, it is now newer than
|
|---|
| 720 | @file{edit}, so @file{edit} is relinked.
|
|---|
| 721 | @cindex relinking
|
|---|
| 722 |
|
|---|
| 723 | Thus, if we change the file @file{insert.c} and run @code{make},
|
|---|
| 724 | @code{make} will compile that file to update @file{insert.o}, and then
|
|---|
| 725 | link @file{edit}. If we change the file @file{command.h} and run
|
|---|
| 726 | @code{make}, @code{make} will recompile the object files @file{kbd.o},
|
|---|
| 727 | @file{command.o} and @file{files.o} and then link the file @file{edit}.
|
|---|
| 728 |
|
|---|
| 729 | @node Variables Simplify, make Deduces, How Make Works, Introduction
|
|---|
| 730 | @section Variables Make Makefiles Simpler
|
|---|
| 731 | @cindex variables
|
|---|
| 732 | @cindex simplifying with variables
|
|---|
| 733 |
|
|---|
| 734 | In our example, we had to list all the object files twice in the rule for
|
|---|
| 735 | @file{edit} (repeated here):
|
|---|
| 736 |
|
|---|
| 737 | @example
|
|---|
| 738 | @group
|
|---|
| 739 | edit : main.o kbd.o command.o display.o \
|
|---|
| 740 | insert.o search.o files.o utils.o
|
|---|
| 741 | cc -o edit main.o kbd.o command.o display.o \
|
|---|
| 742 | insert.o search.o files.o utils.o
|
|---|
| 743 | @end group
|
|---|
| 744 | @end example
|
|---|
| 745 |
|
|---|
| 746 | @cindex @code{objects}
|
|---|
| 747 | Such duplication is error-prone; if a new object file is added to the
|
|---|
| 748 | system, we might add it to one list and forget the other. We can eliminate
|
|---|
| 749 | the risk and simplify the makefile by using a variable. @dfn{Variables}
|
|---|
| 750 | allow a text string to be defined once and substituted in multiple places
|
|---|
| 751 | later (@pxref{Using Variables, ,How to Use Variables}).
|
|---|
| 752 |
|
|---|
| 753 | @cindex @code{OBJECTS}
|
|---|
| 754 | @cindex @code{objs}
|
|---|
| 755 | @cindex @code{OBJS}
|
|---|
| 756 | @cindex @code{obj}
|
|---|
| 757 | @cindex @code{OBJ}
|
|---|
| 758 | It is standard practice for every makefile to have a variable named
|
|---|
| 759 | @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
|
|---|
| 760 | or @code{OBJ} which is a list of all object file names. We would
|
|---|
| 761 | define such a variable @code{objects} with a line like this in the
|
|---|
| 762 | makefile:@refill
|
|---|
| 763 |
|
|---|
| 764 | @example
|
|---|
| 765 | @group
|
|---|
| 766 | objects = main.o kbd.o command.o display.o \
|
|---|
| 767 | insert.o search.o files.o utils.o
|
|---|
| 768 | @end group
|
|---|
| 769 | @end example
|
|---|
| 770 |
|
|---|
| 771 | @noindent
|
|---|
| 772 | Then, each place we want to put a list of the object file names, we can
|
|---|
| 773 | substitute the variable's value by writing @samp{$(objects)}
|
|---|
| 774 | (@pxref{Using Variables, ,How to Use Variables}).
|
|---|
| 775 |
|
|---|
| 776 | Here is how the complete simple makefile looks when you use a variable
|
|---|
| 777 | for the object files:
|
|---|
| 778 |
|
|---|
| 779 | @example
|
|---|
| 780 | @group
|
|---|
| 781 | objects = main.o kbd.o command.o display.o \
|
|---|
| 782 | insert.o search.o files.o utils.o
|
|---|
| 783 |
|
|---|
| 784 | edit : $(objects)
|
|---|
| 785 | cc -o edit $(objects)
|
|---|
| 786 | main.o : main.c defs.h
|
|---|
| 787 | cc -c main.c
|
|---|
| 788 | kbd.o : kbd.c defs.h command.h
|
|---|
| 789 | cc -c kbd.c
|
|---|
| 790 | command.o : command.c defs.h command.h
|
|---|
| 791 | cc -c command.c
|
|---|
| 792 | display.o : display.c defs.h buffer.h
|
|---|
| 793 | cc -c display.c
|
|---|
| 794 | insert.o : insert.c defs.h buffer.h
|
|---|
| 795 | cc -c insert.c
|
|---|
| 796 | search.o : search.c defs.h buffer.h
|
|---|
| 797 | cc -c search.c
|
|---|
| 798 | files.o : files.c defs.h buffer.h command.h
|
|---|
| 799 | cc -c files.c
|
|---|
| 800 | utils.o : utils.c defs.h
|
|---|
| 801 | cc -c utils.c
|
|---|
| 802 | clean :
|
|---|
| 803 | rm edit $(objects)
|
|---|
| 804 | @end group
|
|---|
| 805 | @end example
|
|---|
| 806 |
|
|---|
| 807 | @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
|
|---|
| 808 | @section Letting @code{make} Deduce the Commands
|
|---|
| 809 | @cindex deducing commands (implicit rules)
|
|---|
| 810 | @cindex implicit rule, introduction to
|
|---|
| 811 | @cindex rule, implicit, introduction to
|
|---|
| 812 |
|
|---|
| 813 | It is not necessary to spell out the commands for compiling the individual
|
|---|
| 814 | C source files, because @code{make} can figure them out: it has an
|
|---|
| 815 | @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
|
|---|
| 816 | named @samp{.c} file using a @samp{cc -c} command. For example, it will
|
|---|
| 817 | use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
|
|---|
| 818 | @file{main.o}. We can therefore omit the commands from the rules for the
|
|---|
| 819 | object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
|
|---|
| 820 |
|
|---|
| 821 | When a @samp{.c} file is used automatically in this way, it is also
|
|---|
| 822 | automatically added to the list of prerequisites. We can therefore omit
|
|---|
| 823 | the @samp{.c} files from the prerequisites, provided we omit the commands.
|
|---|
| 824 |
|
|---|
| 825 | Here is the entire example, with both of these changes, and a variable
|
|---|
| 826 | @code{objects} as suggested above:
|
|---|
| 827 |
|
|---|
| 828 | @example
|
|---|
| 829 | @group
|
|---|
| 830 | objects = main.o kbd.o command.o display.o \
|
|---|
| 831 | insert.o search.o files.o utils.o
|
|---|
| 832 |
|
|---|
| 833 | edit : $(objects)
|
|---|
| 834 | cc -o edit $(objects)
|
|---|
| 835 |
|
|---|
| 836 | main.o : defs.h
|
|---|
| 837 | kbd.o : defs.h command.h
|
|---|
| 838 | command.o : defs.h command.h
|
|---|
| 839 | display.o : defs.h buffer.h
|
|---|
| 840 | insert.o : defs.h buffer.h
|
|---|
| 841 | search.o : defs.h buffer.h
|
|---|
| 842 | files.o : defs.h buffer.h command.h
|
|---|
| 843 | utils.o : defs.h
|
|---|
| 844 |
|
|---|
| 845 | .PHONY : clean
|
|---|
| 846 | clean :
|
|---|
| 847 | rm edit $(objects)
|
|---|
| 848 | @end group
|
|---|
| 849 | @end example
|
|---|
| 850 |
|
|---|
| 851 | @noindent
|
|---|
| 852 | This is how we would write the makefile in actual practice. (The
|
|---|
| 853 | complications associated with @samp{clean} are described elsewhere.
|
|---|
| 854 | See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
|
|---|
| 855 |
|
|---|
| 856 | Because implicit rules are so convenient, they are important. You
|
|---|
| 857 | will see them used frequently.@refill
|
|---|
| 858 |
|
|---|
| 859 | @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
|
|---|
| 860 | @section Another Style of Makefile
|
|---|
| 861 | @cindex combining rules by prerequisite
|
|---|
| 862 |
|
|---|
| 863 | When the objects of a makefile are created only by implicit rules, an
|
|---|
| 864 | alternative style of makefile is possible. In this style of makefile,
|
|---|
| 865 | you group entries by their prerequisites instead of by their targets.
|
|---|
| 866 | Here is what one looks like:
|
|---|
| 867 |
|
|---|
| 868 | @example
|
|---|
| 869 | @group
|
|---|
| 870 | objects = main.o kbd.o command.o display.o \
|
|---|
| 871 | insert.o search.o files.o utils.o
|
|---|
| 872 |
|
|---|
| 873 | edit : $(objects)
|
|---|
| 874 | cc -o edit $(objects)
|
|---|
| 875 |
|
|---|
| 876 | $(objects) : defs.h
|
|---|
| 877 | kbd.o command.o files.o : command.h
|
|---|
| 878 | display.o insert.o search.o files.o : buffer.h
|
|---|
| 879 | @end group
|
|---|
| 880 | @end example
|
|---|
| 881 |
|
|---|
| 882 | @noindent
|
|---|
| 883 | Here @file{defs.h} is given as a prerequisite of all the object files;
|
|---|
| 884 | @file{command.h} and @file{buffer.h} are prerequisites of the specific
|
|---|
| 885 | object files listed for them.
|
|---|
| 886 |
|
|---|
| 887 | Whether this is better is a matter of taste: it is more compact, but some
|
|---|
| 888 | people dislike it because they find it clearer to put all the information
|
|---|
| 889 | about each target in one place.
|
|---|
| 890 |
|
|---|
| 891 | @node Cleanup, , Combine By Prerequisite, Introduction
|
|---|
| 892 | @section Rules for Cleaning the Directory
|
|---|
| 893 | @cindex cleaning up
|
|---|
| 894 | @cindex removing, to clean up
|
|---|
| 895 |
|
|---|
| 896 | Compiling a program is not the only thing you might want to write rules
|
|---|
| 897 | for. Makefiles commonly tell how to do a few other things besides
|
|---|
| 898 | compiling a program: for example, how to delete all the object files
|
|---|
| 899 | and executables so that the directory is @samp{clean}.
|
|---|
| 900 |
|
|---|
| 901 | @cindex @code{clean} target
|
|---|
| 902 | Here is how we
|
|---|
| 903 | could write a @code{make} rule for cleaning our example editor:
|
|---|
| 904 |
|
|---|
| 905 | @example
|
|---|
| 906 | @group
|
|---|
| 907 | clean:
|
|---|
| 908 | rm edit $(objects)
|
|---|
| 909 | @end group
|
|---|
| 910 | @end example
|
|---|
| 911 |
|
|---|
| 912 | In practice, we might want to write the rule in a somewhat more
|
|---|
| 913 | complicated manner to handle unanticipated situations. We would do this:
|
|---|
| 914 |
|
|---|
| 915 | @example
|
|---|
| 916 | @group
|
|---|
| 917 | .PHONY : clean
|
|---|
| 918 | clean :
|
|---|
| 919 | -rm edit $(objects)
|
|---|
| 920 | @end group
|
|---|
| 921 | @end example
|
|---|
| 922 |
|
|---|
| 923 | @noindent
|
|---|
| 924 | This prevents @code{make} from getting confused by an actual file
|
|---|
| 925 | called @file{clean} and causes it to continue in spite of errors from
|
|---|
| 926 | @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
|
|---|
| 927 | Commands}.)
|
|---|
| 928 |
|
|---|
| 929 | @noindent
|
|---|
| 930 | A rule such as this should not be placed at the beginning of the
|
|---|
| 931 | makefile, because we do not want it to run by default! Thus, in the
|
|---|
| 932 | example makefile, we want the rule for @code{edit}, which recompiles
|
|---|
| 933 | the editor, to remain the default goal.
|
|---|
| 934 |
|
|---|
| 935 | Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
|
|---|
| 936 | run at all if we give the command @samp{make} with no arguments. In
|
|---|
| 937 | order to make the rule run, we have to type @samp{make clean}.
|
|---|
| 938 | @xref{Running, ,How to Run @code{make}}.
|
|---|
| 939 |
|
|---|
| 940 | @node Makefiles, Rules, Introduction, Top
|
|---|
| 941 | @chapter Writing Makefiles
|
|---|
| 942 |
|
|---|
| 943 | @cindex makefile, how to write
|
|---|
| 944 | The information that tells @code{make} how to recompile a system comes from
|
|---|
| 945 | reading a data base called the @dfn{makefile}.
|
|---|
| 946 |
|
|---|
| 947 | @menu
|
|---|
| 948 | * Makefile Contents:: What makefiles contain.
|
|---|
| 949 | * Makefile Names:: How to name your makefile.
|
|---|
| 950 | * Include:: How one makefile can use another makefile.
|
|---|
| 951 | * MAKEFILES Variable:: The environment can specify extra makefiles.
|
|---|
| 952 | * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
|
|---|
| 953 | * Special Variables:: Other special variables.
|
|---|
| 954 | * Remaking Makefiles:: How makefiles get remade.
|
|---|
| 955 | * Overriding Makefiles:: How to override part of one makefile
|
|---|
| 956 | with another makefile.
|
|---|
| 957 | * Reading Makefiles:: How makefiles are parsed.
|
|---|
| 958 | @end menu
|
|---|
| 959 |
|
|---|
| 960 | @node Makefile Contents, Makefile Names, Makefiles, Makefiles
|
|---|
| 961 | @section What Makefiles Contain
|
|---|
| 962 |
|
|---|
| 963 | Makefiles contain five kinds of things: @dfn{explicit rules},
|
|---|
| 964 | @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
|
|---|
| 965 | and @dfn{comments}. Rules, variables, and directives are described at
|
|---|
| 966 | length in later chapters.@refill
|
|---|
| 967 |
|
|---|
| 968 | @itemize @bullet
|
|---|
| 969 | @cindex rule, explicit, definition of
|
|---|
| 970 | @cindex explicit rule, definition of
|
|---|
| 971 | @item
|
|---|
| 972 | An @dfn{explicit rule} says when and how to remake one or more files,
|
|---|
| 973 | called the rule's @dfn{targets}. It lists the other files that the
|
|---|
| 974 | targets depend on, called the @dfn{prerequisites} of the target, and
|
|---|
| 975 | may also give commands to use to create or update the targets.
|
|---|
| 976 | @xref{Rules, ,Writing Rules}.
|
|---|
| 977 |
|
|---|
| 978 | @cindex rule, implicit, definition of
|
|---|
| 979 | @cindex implicit rule, definition of
|
|---|
| 980 | @item
|
|---|
| 981 | An @dfn{implicit rule} says when and how to remake a class of files
|
|---|
| 982 | based on their names. It describes how a target may depend on a file
|
|---|
| 983 | with a name similar to the target and gives commands to create or
|
|---|
| 984 | update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 985 |
|
|---|
| 986 | @cindex variable definition
|
|---|
| 987 | @item
|
|---|
| 988 | A @dfn{variable definition} is a line that specifies a text string
|
|---|
| 989 | value for a variable that can be substituted into the text later. The
|
|---|
| 990 | simple makefile example shows a variable definition for @code{objects}
|
|---|
| 991 | as a list of all object files (@pxref{Variables Simplify, , Variables
|
|---|
| 992 | Make Makefiles Simpler}).
|
|---|
| 993 |
|
|---|
| 994 | @cindex directive
|
|---|
| 995 | @item
|
|---|
| 996 | A @dfn{directive} is a command for @code{make} to do something special while
|
|---|
| 997 | reading the makefile. These include:
|
|---|
| 998 |
|
|---|
| 999 | @itemize @bullet
|
|---|
| 1000 | @item
|
|---|
| 1001 | Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
|
|---|
| 1002 |
|
|---|
| 1003 | @item
|
|---|
| 1004 | Deciding (based on the values of variables) whether to use or
|
|---|
| 1005 | ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
|
|---|
| 1006 |
|
|---|
| 1007 | @item
|
|---|
| 1008 | Defining a variable from a verbatim string containing multiple lines
|
|---|
| 1009 | (@pxref{Defining, ,Defining Variables Verbatim}).
|
|---|
| 1010 | @end itemize
|
|---|
| 1011 |
|
|---|
| 1012 | @cindex comments, in makefile
|
|---|
| 1013 | @cindex @code{#} (comments), in makefile
|
|---|
| 1014 | @item
|
|---|
| 1015 | @samp{#} in a line of a makefile starts a @dfn{comment}. It and the
|
|---|
| 1016 | rest of the line are ignored, except that a trailing backslash not
|
|---|
| 1017 | escaped by another backslash will continue the comment across multiple
|
|---|
| 1018 | lines. A line containing just a comment (with perhaps spaces before
|
|---|
| 1019 | it) is effectively blank, and is ignored. If you want a literal
|
|---|
| 1020 | @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
|
|---|
| 1021 | appear on any line in the makefile, although they are treated
|
|---|
| 1022 | specially in certain situations.
|
|---|
| 1023 |
|
|---|
| 1024 | Within a command script (if the line begins with a TAB character) the
|
|---|
| 1025 | entire line is passed to the shell, just as with any other line that
|
|---|
| 1026 | begins with a TAB. The shell decides how to interpret the text:
|
|---|
| 1027 | whether or not this is a comment is up to the shell.
|
|---|
| 1028 |
|
|---|
| 1029 | Within a @code{define} directive, comments are not ignored during the
|
|---|
| 1030 | definition of the variable, but rather kept intact in the value of the
|
|---|
| 1031 | variable. When the variable is expanded they will either be treated
|
|---|
| 1032 | as @code{make} comments or as command script text, depending on the
|
|---|
| 1033 | context in which the variable is evaluated.
|
|---|
| 1034 | @end itemize
|
|---|
| 1035 |
|
|---|
| 1036 | @node Makefile Names, Include, Makefile Contents, Makefiles
|
|---|
| 1037 | @section What Name to Give Your Makefile
|
|---|
| 1038 | @cindex makefile name
|
|---|
| 1039 | @cindex name of makefile
|
|---|
| 1040 | @cindex default makefile name
|
|---|
| 1041 | @cindex file name of makefile
|
|---|
| 1042 |
|
|---|
| 1043 | @c following paragraph rewritten to avoid overfull hbox
|
|---|
| 1044 | By default, when @code{make} looks for the makefile, it tries the
|
|---|
| 1045 | following names, in order: @file{GNUmakefile}, @file{makefile}
|
|---|
| 1046 | and @file{Makefile}.@refill
|
|---|
| 1047 | @findex Makefile
|
|---|
| 1048 | @findex GNUmakefile
|
|---|
| 1049 | @findex makefile
|
|---|
| 1050 |
|
|---|
| 1051 | @cindex @code{README}
|
|---|
| 1052 | Normally you should call your makefile either @file{makefile} or
|
|---|
| 1053 | @file{Makefile}. (We recommend @file{Makefile} because it appears
|
|---|
| 1054 | prominently near the beginning of a directory listing, right near other
|
|---|
| 1055 | important files such as @file{README}.) The first name checked,
|
|---|
| 1056 | @file{GNUmakefile}, is not recommended for most makefiles. You should
|
|---|
| 1057 | use this name if you have a makefile that is specific to GNU
|
|---|
| 1058 | @code{make}, and will not be understood by other versions of
|
|---|
| 1059 | @code{make}. Other @code{make} programs look for @file{makefile} and
|
|---|
| 1060 | @file{Makefile}, but not @file{GNUmakefile}.
|
|---|
| 1061 |
|
|---|
| 1062 | If @code{make} finds none of these names, it does not use any makefile.
|
|---|
| 1063 | Then you must specify a goal with a command argument, and @code{make}
|
|---|
| 1064 | will attempt to figure out how to remake it using only its built-in
|
|---|
| 1065 | implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 1066 |
|
|---|
| 1067 | @cindex @code{-f}
|
|---|
| 1068 | @cindex @code{--file}
|
|---|
| 1069 | @cindex @code{--makefile}
|
|---|
| 1070 | If you want to use a nonstandard name for your makefile, you can specify
|
|---|
| 1071 | the makefile name with the @samp{-f} or @samp{--file} option. The
|
|---|
| 1072 | arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
|
|---|
| 1073 | @code{make} to read the file @var{name} as the makefile. If you use
|
|---|
| 1074 | more than one @samp{-f} or @samp{--file} option, you can specify several
|
|---|
| 1075 | makefiles. All the makefiles are effectively concatenated in the order
|
|---|
| 1076 | specified. The default makefile names @file{GNUmakefile},
|
|---|
| 1077 | @file{makefile} and @file{Makefile} are not checked automatically if you
|
|---|
| 1078 | specify @samp{-f} or @samp{--file}.@refill
|
|---|
| 1079 | @cindex specifying makefile name
|
|---|
| 1080 | @cindex makefile name, how to specify
|
|---|
| 1081 | @cindex name of makefile, how to specify
|
|---|
| 1082 | @cindex file name of makefile, how to specify
|
|---|
| 1083 |
|
|---|
| 1084 | @node Include, MAKEFILES Variable, Makefile Names, Makefiles
|
|---|
| 1085 | @section Including Other Makefiles
|
|---|
| 1086 | @cindex including other makefiles
|
|---|
| 1087 | @cindex makefile, including
|
|---|
| 1088 |
|
|---|
| 1089 | @findex include
|
|---|
| 1090 | The @code{include} directive tells @code{make} to suspend reading the
|
|---|
| 1091 | current makefile and read one or more other makefiles before continuing.
|
|---|
| 1092 | The directive is a line in the makefile that looks like this:
|
|---|
| 1093 |
|
|---|
| 1094 | @example
|
|---|
| 1095 | include @var{filenames}@dots{}
|
|---|
| 1096 | @end example
|
|---|
| 1097 |
|
|---|
| 1098 | @noindent
|
|---|
| 1099 | @var{filenames} can contain shell file name patterns.
|
|---|
| 1100 | @cindex shell file name pattern (in @code{include})
|
|---|
| 1101 | @cindex shell wildcards (in @code{include})
|
|---|
| 1102 | @cindex wildcard, in @code{include}
|
|---|
| 1103 |
|
|---|
| 1104 | Extra spaces are allowed and ignored at the beginning of the line, but
|
|---|
| 1105 | a tab is not allowed. (If the line begins with a tab, it will be
|
|---|
| 1106 | considered a command line.) Whitespace is required between
|
|---|
| 1107 | @code{include} and the file names, and between file names; extra
|
|---|
| 1108 | whitespace is ignored there and at the end of the directive. A
|
|---|
| 1109 | comment starting with @samp{#} is allowed at the end of the line. If
|
|---|
| 1110 | the file names contain any variable or function references, they are
|
|---|
| 1111 | expanded. @xref{Using Variables, ,How to Use Variables}.
|
|---|
| 1112 |
|
|---|
| 1113 | For example, if you have three @file{.mk} files, @file{a.mk},
|
|---|
| 1114 | @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
|
|---|
| 1115 | @code{bish bash}, then the following expression
|
|---|
| 1116 |
|
|---|
| 1117 | @example
|
|---|
| 1118 | include foo *.mk $(bar)
|
|---|
| 1119 | @end example
|
|---|
| 1120 |
|
|---|
| 1121 | is equivalent to
|
|---|
| 1122 |
|
|---|
| 1123 | @example
|
|---|
| 1124 | include foo a.mk b.mk c.mk bish bash
|
|---|
| 1125 | @end example
|
|---|
| 1126 |
|
|---|
| 1127 | When @code{make} processes an @code{include} directive, it suspends
|
|---|
| 1128 | reading of the containing makefile and reads from each listed file in
|
|---|
| 1129 | turn. When that is finished, @code{make} resumes reading the
|
|---|
| 1130 | makefile in which the directive appears.
|
|---|
| 1131 |
|
|---|
| 1132 | One occasion for using @code{include} directives is when several programs,
|
|---|
| 1133 | handled by individual makefiles in various directories, need to use a
|
|---|
| 1134 | common set of variable definitions
|
|---|
| 1135 | (@pxref{Setting, ,Setting Variables}) or pattern rules
|
|---|
| 1136 | (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
|---|
| 1137 |
|
|---|
| 1138 | Another such occasion is when you want to generate prerequisites from
|
|---|
| 1139 | source files automatically; the prerequisites can be put in a file that
|
|---|
| 1140 | is included by the main makefile. This practice is generally cleaner
|
|---|
| 1141 | than that of somehow appending the prerequisites to the end of the main
|
|---|
| 1142 | makefile as has been traditionally done with other versions of
|
|---|
| 1143 | @code{make}. @xref{Automatic Prerequisites}.
|
|---|
| 1144 | @cindex prerequisites, automatic generation
|
|---|
| 1145 | @cindex automatic generation of prerequisites
|
|---|
| 1146 | @cindex generating prerequisites automatically
|
|---|
| 1147 |
|
|---|
| 1148 | @cindex @code{-I}
|
|---|
| 1149 | @cindex @code{--include-dir}
|
|---|
| 1150 | @cindex included makefiles, default directories
|
|---|
| 1151 | @cindex default directories for included makefiles
|
|---|
| 1152 | @findex /usr/gnu/include
|
|---|
| 1153 | @findex /usr/local/include
|
|---|
| 1154 | @findex /usr/include
|
|---|
| 1155 | If the specified name does not start with a slash, and the file is not
|
|---|
| 1156 | found in the current directory, several other directories are searched.
|
|---|
| 1157 | First, any directories you have specified with the @samp{-I} or
|
|---|
| 1158 | @samp{--include-dir} option are searched
|
|---|
| 1159 | (@pxref{Options Summary, ,Summary of Options}).
|
|---|
| 1160 | Then the following directories (if they exist)
|
|---|
| 1161 | are searched, in this order:
|
|---|
| 1162 | @file{@var{prefix}/include} (normally @file{/usr/local/include}
|
|---|
| 1163 | @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
|
|---|
| 1164 | @var{prefix} has been defined to be the root of the DJGPP tree
|
|---|
| 1165 | hierarchy.})
|
|---|
| 1166 | @file{/usr/gnu/include},
|
|---|
| 1167 | @file{/usr/local/include}, @file{/usr/include}.
|
|---|
| 1168 |
|
|---|
| 1169 | If an included makefile cannot be found in any of these directories, a
|
|---|
| 1170 | warning message is generated, but it is not an immediately fatal error;
|
|---|
| 1171 | processing of the makefile containing the @code{include} continues.
|
|---|
| 1172 | Once it has finished reading makefiles, @code{make} will try to remake
|
|---|
| 1173 | any that are out of date or don't exist.
|
|---|
| 1174 | @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
|---|
| 1175 | Only after it has tried to find a way to remake a makefile and failed,
|
|---|
| 1176 | will @code{make} diagnose the missing makefile as a fatal error.
|
|---|
| 1177 |
|
|---|
| 1178 | If you want @code{make} to simply ignore a makefile which does not exist
|
|---|
| 1179 | and cannot be remade, with no error message, use the @w{@code{-include}}
|
|---|
| 1180 | directive instead of @code{include}, like this:
|
|---|
| 1181 |
|
|---|
| 1182 | @example
|
|---|
| 1183 | -include @var{filenames}@dots{}
|
|---|
| 1184 | @end example
|
|---|
| 1185 |
|
|---|
| 1186 | This acts like @code{include} in every way except that there is no
|
|---|
| 1187 | error (not even a warning) if any of the @var{filenames} do not exist.
|
|---|
| 1188 | For compatibility with some other @code{make} implementations,
|
|---|
| 1189 | @code{sinclude} is another name for @w{@code{-include}}.
|
|---|
| 1190 |
|
|---|
| 1191 | @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
|
|---|
| 1192 | @section The Variable @code{MAKEFILES}
|
|---|
| 1193 | @cindex makefile, and @code{MAKEFILES} variable
|
|---|
| 1194 | @cindex including (@code{MAKEFILES} variable)
|
|---|
| 1195 |
|
|---|
| 1196 | @vindex MAKEFILES
|
|---|
| 1197 | If the environment variable @code{MAKEFILES} is defined, @code{make}
|
|---|
| 1198 | considers its value as a list of names (separated by whitespace) of
|
|---|
| 1199 | additional makefiles to be read before the others. This works much like
|
|---|
| 1200 | the @code{include} directive: various directories are searched for those
|
|---|
| 1201 | files (@pxref{Include, ,Including Other Makefiles}). In addition, the
|
|---|
| 1202 | default goal is never taken from one of these makefiles and it is not an
|
|---|
| 1203 | error if the files listed in @code{MAKEFILES} are not found.@refill
|
|---|
| 1204 |
|
|---|
| 1205 | @cindex recursion, and @code{MAKEFILES} variable
|
|---|
| 1206 | The main use of @code{MAKEFILES} is in communication between recursive
|
|---|
| 1207 | invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
|
|---|
| 1208 | @code{make}}). It usually is not desirable to set the environment
|
|---|
| 1209 | variable before a top-level invocation of @code{make}, because it is
|
|---|
| 1210 | usually better not to mess with a makefile from outside. However, if
|
|---|
| 1211 | you are running @code{make} without a specific makefile, a makefile in
|
|---|
| 1212 | @code{MAKEFILES} can do useful things to help the built-in implicit
|
|---|
| 1213 | rules work better, such as defining search paths (@pxref{Directory Search}).
|
|---|
| 1214 |
|
|---|
| 1215 | Some users are tempted to set @code{MAKEFILES} in the environment
|
|---|
| 1216 | automatically on login, and program makefiles to expect this to be done.
|
|---|
| 1217 | This is a very bad idea, because such makefiles will fail to work if run by
|
|---|
| 1218 | anyone else. It is much better to write explicit @code{include} directives
|
|---|
| 1219 | in the makefiles. @xref{Include, , Including Other Makefiles}.
|
|---|
| 1220 |
|
|---|
| 1221 | @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
|
|---|
| 1222 | @comment node-name, next, previous, up
|
|---|
| 1223 | @section The Variable @code{MAKEFILE_LIST}
|
|---|
| 1224 | @cindex makefiles, and @code{MAKEFILE_LIST} variable
|
|---|
| 1225 | @cindex including (@code{MAKEFILE_LIST} variable)
|
|---|
| 1226 |
|
|---|
| 1227 | As @code{make} reads various makefiles, including any obtained from the
|
|---|
| 1228 | @code{MAKEFILES} variable, the command line, the default files, or
|
|---|
| 1229 | from @code{include} directives, their names will be automatically
|
|---|
| 1230 | appended to the @code{MAKEFILE_LIST} variable. They are added right
|
|---|
| 1231 | before @code{make} begins to parse them.
|
|---|
| 1232 |
|
|---|
| 1233 | This means that if the first thing a makefile does is examine the last
|
|---|
| 1234 | word in this variable, it will be the name of the current makefile.
|
|---|
| 1235 | Once the current makefile has used @code{include}, however, the last
|
|---|
| 1236 | word will be the just-included makefile.
|
|---|
| 1237 |
|
|---|
| 1238 | If a makefile named @code{Makefile} has this content:
|
|---|
| 1239 |
|
|---|
| 1240 | @example
|
|---|
| 1241 | @group
|
|---|
| 1242 | name1 := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
|
|---|
| 1243 |
|
|---|
| 1244 | include inc.mk
|
|---|
| 1245 |
|
|---|
| 1246 | name2 := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
|
|---|
| 1247 |
|
|---|
| 1248 | all:
|
|---|
| 1249 | @@echo name1 = $(name1)
|
|---|
| 1250 | @@echo name2 = $(name2)
|
|---|
| 1251 | @end group
|
|---|
| 1252 | @end example
|
|---|
| 1253 |
|
|---|
| 1254 | @noindent
|
|---|
| 1255 | then you would expect to see this output:
|
|---|
| 1256 |
|
|---|
| 1257 | @example
|
|---|
| 1258 | @group
|
|---|
| 1259 | name1 = Makefile
|
|---|
| 1260 | name2 = inc.mk
|
|---|
| 1261 | @end group
|
|---|
| 1262 | @end example
|
|---|
| 1263 |
|
|---|
| 1264 | @xref{Text Functions}, for more information on the @code{word} and
|
|---|
| 1265 | @code{words} functions used above. @xref{Flavors, The Two Flavors of
|
|---|
| 1266 | Variables}, for more information on simply-expanded (@code{:=})
|
|---|
| 1267 | variable definitions.
|
|---|
| 1268 |
|
|---|
| 1269 | @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
|
|---|
| 1270 | @comment node-name, next, previous, up
|
|---|
| 1271 | @section Other Special Variables
|
|---|
| 1272 | @cindex makefiles, and special variables
|
|---|
| 1273 | @cindex special variables
|
|---|
| 1274 |
|
|---|
| 1275 | GNU @code{make} also supports a special variable. Note that any value
|
|---|
| 1276 | you assign to this variable will be ignored; it will always return its
|
|---|
| 1277 | special value.
|
|---|
| 1278 |
|
|---|
| 1279 | @vindex $(.VARIABLES)
|
|---|
| 1280 | @vindex .VARIABLES @r{(list of variables)}
|
|---|
| 1281 | The first special variable is @code{.VARIABLES}. When expanded, the
|
|---|
| 1282 | value consists of a list of the @emph{names} of all global variables
|
|---|
| 1283 | defined in all makefiles read up until that point. This includes
|
|---|
| 1284 | variables which have empty values, as well as built-in variables
|
|---|
| 1285 | (@pxref{Implicit Variables, , Variables Used by Implicit Rules}), but
|
|---|
| 1286 | does not include any variables which are only defined in a
|
|---|
| 1287 | target-specific context.
|
|---|
| 1288 |
|
|---|
| 1289 | @c @vindex $(.TARGETS)
|
|---|
| 1290 | @c @vindex .TARGETS @r{(list of targets)}
|
|---|
| 1291 | @c The second special variable is @code{.TARGETS}. When expanded, the
|
|---|
| 1292 | @c value consists of a list of all targets defined in all makefiles read
|
|---|
| 1293 | @c up until that point. Note it's not enough for a file to be simply
|
|---|
| 1294 | @c mentioned in the makefile to be listed in this variable, even if it
|
|---|
| 1295 | @c would match an implicit rule and become an ``implicit target''. The
|
|---|
| 1296 | @c file must appear as a target, on the left-hand side of a ``:'', to be
|
|---|
| 1297 | @c considered a target for the purposes of this variable.
|
|---|
| 1298 |
|
|---|
| 1299 | @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
|
|---|
| 1300 | @section How Makefiles Are Remade
|
|---|
| 1301 |
|
|---|
| 1302 | @cindex updating makefiles
|
|---|
| 1303 | @cindex remaking makefiles
|
|---|
| 1304 | @cindex makefile, remaking of
|
|---|
| 1305 | Sometimes makefiles can be remade from other files, such as RCS or SCCS
|
|---|
| 1306 | files. If a makefile can be remade from other files, you probably want
|
|---|
| 1307 | @code{make} to get an up-to-date version of the makefile to read in.
|
|---|
| 1308 |
|
|---|
| 1309 | To this end, after reading in all makefiles, @code{make} will consider
|
|---|
| 1310 | each as a goal target and attempt to update it. If a makefile has a
|
|---|
| 1311 | rule which says how to update it (found either in that very makefile or
|
|---|
| 1312 | in another one) or if an implicit rule applies to it (@pxref{Implicit
|
|---|
| 1313 | Rules, ,Using Implicit Rules}), it will be updated if necessary. After
|
|---|
| 1314 | all makefiles have been checked, if any have actually been changed,
|
|---|
| 1315 | @code{make} starts with a clean slate and reads all the makefiles over
|
|---|
| 1316 | again. (It will also attempt to update each of them over again, but
|
|---|
| 1317 | normally this will not change them again, since they are already up to
|
|---|
| 1318 | date.)@refill
|
|---|
| 1319 |
|
|---|
| 1320 | If you know that one or more of your makefiles cannot be remade and you
|
|---|
| 1321 | want to keep @code{make} from performing an implicit rule search on
|
|---|
| 1322 | them, perhaps for efficiency reasons, you can use any normal method of
|
|---|
| 1323 | preventing implicit rule lookup to do so. For example, you can write an
|
|---|
| 1324 | explicit rule with the makefile as the target, and an empty command
|
|---|
| 1325 | string (@pxref{Empty Commands, ,Using Empty Commands}).
|
|---|
| 1326 |
|
|---|
| 1327 | If the makefiles specify a double-colon rule to remake a file with
|
|---|
| 1328 | commands but no prerequisites, that file will always be remade
|
|---|
| 1329 | (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
|
|---|
| 1330 | double-colon rule with commands but no prerequisites will be remade every
|
|---|
| 1331 | time @code{make} is run, and then again after @code{make} starts over
|
|---|
| 1332 | and reads the makefiles in again. This would cause an infinite loop:
|
|---|
| 1333 | @code{make} would constantly remake the makefile, and never do anything
|
|---|
| 1334 | else. So, to avoid this, @code{make} will @strong{not} attempt to
|
|---|
| 1335 | remake makefiles which are specified as targets of a double-colon rule
|
|---|
| 1336 | with commands but no prerequisites.@refill
|
|---|
| 1337 |
|
|---|
| 1338 | If you do not specify any makefiles to be read with @samp{-f} or
|
|---|
| 1339 | @samp{--file} options, @code{make} will try the default makefile names;
|
|---|
| 1340 | @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
|
|---|
| 1341 | makefiles explicitly requested with @samp{-f} or @samp{--file} options,
|
|---|
| 1342 | @code{make} is not certain that these makefiles should exist. However,
|
|---|
| 1343 | if a default makefile does not exist but can be created by running
|
|---|
| 1344 | @code{make} rules, you probably want the rules to be run so that the
|
|---|
| 1345 | makefile can be used.
|
|---|
| 1346 |
|
|---|
| 1347 | Therefore, if none of the default makefiles exists, @code{make} will try
|
|---|
| 1348 | to make each of them in the same order in which they are searched for
|
|---|
| 1349 | (@pxref{Makefile Names, ,What Name to Give Your Makefile})
|
|---|
| 1350 | until it succeeds in making one, or it runs out of names to try. Note
|
|---|
| 1351 | that it is not an error if @code{make} cannot find or make any makefile;
|
|---|
| 1352 | a makefile is not always necessary.@refill
|
|---|
| 1353 |
|
|---|
| 1354 | When you use the @samp{-t} or @samp{--touch} option
|
|---|
| 1355 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
|
|---|
| 1356 | you would not want to use an out-of-date makefile to decide which
|
|---|
| 1357 | targets to touch. So the @samp{-t} option has no effect on updating
|
|---|
| 1358 | makefiles; they are really updated even if @samp{-t} is specified.
|
|---|
| 1359 | Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
|
|---|
| 1360 | @samp{--just-print}) do not prevent updating of makefiles, because an
|
|---|
| 1361 | out-of-date makefile would result in the wrong output for other targets.
|
|---|
| 1362 | Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
|
|---|
| 1363 | and then print the commands to update @file{foo} and its prerequisites
|
|---|
| 1364 | without running them. The commands printed for @file{foo} will be those
|
|---|
| 1365 | specified in the updated contents of @file{mfile}.
|
|---|
| 1366 |
|
|---|
| 1367 | However, on occasion you might actually wish to prevent updating of even
|
|---|
| 1368 | the makefiles. You can do this by specifying the makefiles as goals in
|
|---|
| 1369 | the command line as well as specifying them as makefiles. When the
|
|---|
| 1370 | makefile name is specified explicitly as a goal, the options @samp{-t}
|
|---|
| 1371 | and so on do apply to them.
|
|---|
| 1372 |
|
|---|
| 1373 | Thus, @samp{make -f mfile -n mfile foo} would read the makefile
|
|---|
| 1374 | @file{mfile}, print the commands needed to update it without actually
|
|---|
| 1375 | running them, and then print the commands needed to update @file{foo}
|
|---|
| 1376 | without running them. The commands for @file{foo} will be those
|
|---|
| 1377 | specified by the existing contents of @file{mfile}.
|
|---|
| 1378 |
|
|---|
| 1379 | @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
|
|---|
| 1380 | @section Overriding Part of Another Makefile
|
|---|
| 1381 |
|
|---|
| 1382 | @cindex overriding makefiles
|
|---|
| 1383 | @cindex makefile, overriding
|
|---|
| 1384 | Sometimes it is useful to have a makefile that is mostly just like
|
|---|
| 1385 | another makefile. You can often use the @samp{include} directive to
|
|---|
| 1386 | include one in the other, and add more targets or variable definitions.
|
|---|
| 1387 | However, if the two makefiles give different commands for the same
|
|---|
| 1388 | target, @code{make} will not let you just do this. But there is another way.
|
|---|
| 1389 |
|
|---|
| 1390 | @cindex match-anything rule, used to override
|
|---|
| 1391 | In the containing makefile (the one that wants to include the other),
|
|---|
| 1392 | you can use a match-anything pattern rule to say that to remake any
|
|---|
| 1393 | target that cannot be made from the information in the containing
|
|---|
| 1394 | makefile, @code{make} should look in another makefile.
|
|---|
| 1395 | @xref{Pattern Rules}, for more information on pattern rules.
|
|---|
| 1396 |
|
|---|
| 1397 | For example, if you have a makefile called @file{Makefile} that says how
|
|---|
| 1398 | to make the target @samp{foo} (and other targets), you can write a
|
|---|
| 1399 | makefile called @file{GNUmakefile} that contains:
|
|---|
| 1400 |
|
|---|
| 1401 | @example
|
|---|
| 1402 | foo:
|
|---|
| 1403 | frobnicate > foo
|
|---|
| 1404 |
|
|---|
| 1405 | %: force
|
|---|
| 1406 | @@$(MAKE) -f Makefile $@@
|
|---|
| 1407 | force: ;
|
|---|
| 1408 | @end example
|
|---|
| 1409 |
|
|---|
| 1410 | If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
|
|---|
| 1411 | read it, and see that to make @file{foo}, it needs to run the command
|
|---|
| 1412 | @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
|
|---|
| 1413 | find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
|
|---|
| 1414 | commands from the pattern rule: @samp{make -f Makefile bar}. If
|
|---|
| 1415 | @file{Makefile} provides a rule for updating @file{bar}, @code{make}
|
|---|
| 1416 | will apply the rule. And likewise for any other target that
|
|---|
| 1417 | @file{GNUmakefile} does not say how to make.
|
|---|
| 1418 |
|
|---|
| 1419 | The way this works is that the pattern rule has a pattern of just
|
|---|
| 1420 | @samp{%}, so it matches any target whatever. The rule specifies a
|
|---|
| 1421 | prerequisite @file{force}, to guarantee that the commands will be run even
|
|---|
| 1422 | if the target file already exists. We give @file{force} target empty
|
|---|
| 1423 | commands to prevent @code{make} from searching for an implicit rule to
|
|---|
| 1424 | build it---otherwise it would apply the same match-anything rule to
|
|---|
| 1425 | @file{force} itself and create a prerequisite loop!
|
|---|
| 1426 |
|
|---|
| 1427 | @node Reading Makefiles, , Overriding Makefiles, Makefiles
|
|---|
| 1428 | @section How @code{make} Reads a Makefile
|
|---|
| 1429 | @cindex reading makefiles
|
|---|
| 1430 | @cindex makefile, parsing
|
|---|
| 1431 |
|
|---|
| 1432 | GNU @code{make} does its work in two distinct phases. During the first
|
|---|
| 1433 | phase it reads all the makefiles, included makefiles, etc. and
|
|---|
| 1434 | internalizes all the variables and their values, implicit and explicit
|
|---|
| 1435 | rules, and constructs a dependency graph of all the targets and their
|
|---|
| 1436 | prerequisites. During the second phase, @code{make} uses these internal
|
|---|
| 1437 | structures to determine what targets will need to be rebuilt and to
|
|---|
| 1438 | invoke the rules necessary to do so.
|
|---|
| 1439 |
|
|---|
| 1440 | It's important to understand this two-phase approach because it has a
|
|---|
| 1441 | direct impact on how variable and function expansion happens; this is
|
|---|
| 1442 | often a source of some confusion when writing makefiles. Here we will
|
|---|
| 1443 | present a summary of the phases in which expansion happens for different
|
|---|
| 1444 | constructs within the makefile. We say that expansion is
|
|---|
| 1445 | @dfn{immediate} if it happens during the first phase: in this case
|
|---|
| 1446 | @code{make} will expand any variables or functions in that section of a
|
|---|
| 1447 | construct as the makefile is parsed. We say that expansion is
|
|---|
| 1448 | @dfn{deferred} if expansion is not performed immediately. Expansion of
|
|---|
| 1449 | deferred construct is not performed until either the construct appears
|
|---|
| 1450 | later in an immediate context, or until the second phase.
|
|---|
| 1451 |
|
|---|
| 1452 | You may not be familiar with some of these constructs yet. You can
|
|---|
| 1453 | reference this section as you become familiar with them, in later
|
|---|
| 1454 | chapters.
|
|---|
| 1455 |
|
|---|
| 1456 | @subheading Variable Assignment
|
|---|
| 1457 | @cindex +=, expansion
|
|---|
| 1458 | @cindex =, expansion
|
|---|
| 1459 | @cindex ?=, expansion
|
|---|
| 1460 | @cindex +=, expansion
|
|---|
| 1461 | @cindex define, expansion
|
|---|
| 1462 |
|
|---|
| 1463 | Variable definitions are parsed as follows:
|
|---|
| 1464 |
|
|---|
| 1465 | @example
|
|---|
| 1466 | @var{immediate} = @var{deferred}
|
|---|
| 1467 | @var{immediate} ?= @var{deferred}
|
|---|
| 1468 | @var{immediate} := @var{immediate}
|
|---|
| 1469 | @var{immediate} += @var{deferred} or @var{immediate}
|
|---|
| 1470 |
|
|---|
| 1471 | define @var{immediate}
|
|---|
| 1472 | @var{deferred}
|
|---|
| 1473 | endef
|
|---|
| 1474 | @end example
|
|---|
| 1475 |
|
|---|
| 1476 | For the append operator, @samp{+=}, the right-hand side is considered
|
|---|
| 1477 | immediate if the variable was previously set as a simple variable
|
|---|
| 1478 | (@samp{:=}), and deferred otherwise.
|
|---|
| 1479 |
|
|---|
| 1480 | @subheading Conditional Statements
|
|---|
| 1481 | @cindex ifdef, expansion
|
|---|
| 1482 | @cindex ifeq, expansion
|
|---|
| 1483 | @cindex ifndef, expansion
|
|---|
| 1484 | @cindex ifneq, expansion
|
|---|
| 1485 |
|
|---|
| 1486 | All instances of conditional syntax are parsed immediately, in their
|
|---|
| 1487 | entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
|
|---|
| 1488 | and @code{ifneq} forms.
|
|---|
| 1489 |
|
|---|
| 1490 | @subheading Rule Definition
|
|---|
| 1491 | @cindex target, expansion
|
|---|
| 1492 | @cindex prerequisite, expansion
|
|---|
| 1493 | @cindex implicit rule, expansion
|
|---|
| 1494 | @cindex pattern rule, expansion
|
|---|
| 1495 | @cindex explicit rule, expansion
|
|---|
| 1496 |
|
|---|
| 1497 | A rule is always expanded the same way, regardless of the form:
|
|---|
| 1498 |
|
|---|
| 1499 | @example
|
|---|
| 1500 | @var{immediate} : @var{immediate} ; @var{deferred}
|
|---|
| 1501 | @var{deferred}
|
|---|
| 1502 | @end example
|
|---|
| 1503 |
|
|---|
| 1504 | That is, the target and prerequisite sections are expanded immediately,
|
|---|
| 1505 | and the commands used to construct the target are always deferred. This
|
|---|
| 1506 | general rule is true for explicit rules, pattern rules, suffix rules,
|
|---|
| 1507 | static pattern rules, and simple prerequisite definitions.
|
|---|
| 1508 |
|
|---|
| 1509 | @node Rules, Commands, Makefiles, Top
|
|---|
| 1510 | @chapter Writing Rules
|
|---|
| 1511 | @cindex writing rules
|
|---|
| 1512 | @cindex rule, how to write
|
|---|
| 1513 | @cindex target
|
|---|
| 1514 | @cindex prerequisite
|
|---|
| 1515 |
|
|---|
| 1516 | A @dfn{rule} appears in the makefile and says when and how to remake
|
|---|
| 1517 | certain files, called the rule's @dfn{targets} (most often only one per rule).
|
|---|
| 1518 | It lists the other files that are the @dfn{prerequisites} of the target, and
|
|---|
| 1519 | @dfn{commands} to use to create or update the target.
|
|---|
| 1520 |
|
|---|
| 1521 | @cindex default goal
|
|---|
| 1522 | @cindex goal, default
|
|---|
| 1523 | The order of rules is not significant, except for determining the
|
|---|
| 1524 | @dfn{default goal}: the target for @code{make} to consider, if you do
|
|---|
| 1525 | not otherwise specify one. The default goal is the target of the first
|
|---|
| 1526 | rule in the first makefile. If the first rule has multiple targets,
|
|---|
| 1527 | only the first target is taken as the default. There are two
|
|---|
| 1528 | exceptions: a target starting with a period is not a default unless it
|
|---|
| 1529 | contains one or more slashes, @samp{/}, as well; and, a target that
|
|---|
| 1530 | defines a pattern rule has no effect on the default goal.
|
|---|
| 1531 | (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
|
|---|
| 1532 |
|
|---|
| 1533 | Therefore, we usually write the makefile so that the first rule is the
|
|---|
| 1534 | one for compiling the entire program or all the programs described by
|
|---|
| 1535 | the makefile (often with a target called @samp{all}).
|
|---|
| 1536 | @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 1537 |
|
|---|
| 1538 | @menu
|
|---|
| 1539 | * Rule Example:: An example explained.
|
|---|
| 1540 | * Rule Syntax:: General syntax explained.
|
|---|
| 1541 | * Prerequisite Types:: There are two types of prerequisites.
|
|---|
| 1542 | * Wildcards:: Using wildcard characters such as `*'.
|
|---|
| 1543 | * Directory Search:: Searching other directories for source files.
|
|---|
| 1544 | * Phony Targets:: Using a target that is not a real file's name.
|
|---|
| 1545 | * Force Targets:: You can use a target without commands
|
|---|
| 1546 | or prerequisites to mark other
|
|---|
| 1547 | targets as phony.
|
|---|
| 1548 | * Empty Targets:: When only the date matters and the
|
|---|
| 1549 | files are empty.
|
|---|
| 1550 | * Special Targets:: Targets with special built-in meanings.
|
|---|
| 1551 | * Multiple Targets:: When to make use of several targets in a rule.
|
|---|
| 1552 | * Multiple Rules:: How to use several rules with the same target.
|
|---|
| 1553 | * Static Pattern:: Static pattern rules apply to multiple targets
|
|---|
| 1554 | and can vary the prerequisites according to
|
|---|
| 1555 | the target name.
|
|---|
| 1556 | * Double-Colon:: How to use a special kind of rule to allow
|
|---|
| 1557 | several independent rules for one target.
|
|---|
| 1558 | * Automatic Prerequisites:: How to automatically generate rules giving
|
|---|
| 1559 | prerequisites from source files themselves.
|
|---|
| 1560 | @end menu
|
|---|
| 1561 |
|
|---|
| 1562 | @ifnottex
|
|---|
| 1563 | @node Rule Example, Rule Syntax, Rules, Rules
|
|---|
| 1564 | @section Rule Example
|
|---|
| 1565 |
|
|---|
| 1566 | Here is an example of a rule:
|
|---|
| 1567 |
|
|---|
| 1568 | @example
|
|---|
| 1569 | foo.o : foo.c defs.h # module for twiddling the frobs
|
|---|
| 1570 | cc -c -g foo.c
|
|---|
| 1571 | @end example
|
|---|
| 1572 |
|
|---|
| 1573 | Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
|
|---|
| 1574 | @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
|
|---|
| 1575 | The command line starts with a tab to identify it as a command.
|
|---|
| 1576 |
|
|---|
| 1577 | This rule says two things:
|
|---|
| 1578 |
|
|---|
| 1579 | @itemize @bullet
|
|---|
| 1580 | @item
|
|---|
| 1581 | How to decide whether @file{foo.o} is out of date: it is out of date
|
|---|
| 1582 | if it does not exist, or if either @file{foo.c} or @file{defs.h} is
|
|---|
| 1583 | more recent than it.
|
|---|
| 1584 |
|
|---|
| 1585 | @item
|
|---|
| 1586 | How to update the file @file{foo.o}: by running @code{cc} as stated.
|
|---|
| 1587 | The command does not explicitly mention @file{defs.h}, but we presume
|
|---|
| 1588 | that @file{foo.c} includes it, and that that is why @file{defs.h} was
|
|---|
| 1589 | added to the prerequisites.
|
|---|
| 1590 | @end itemize
|
|---|
| 1591 | @end ifnottex
|
|---|
| 1592 |
|
|---|
| 1593 | @node Rule Syntax, Prerequisite Types, Rule Example, Rules
|
|---|
| 1594 | @section Rule Syntax
|
|---|
| 1595 |
|
|---|
| 1596 | @cindex rule syntax
|
|---|
| 1597 | @cindex syntax of rules
|
|---|
| 1598 | In general, a rule looks like this:
|
|---|
| 1599 |
|
|---|
| 1600 | @example
|
|---|
| 1601 | @var{targets} : @var{prerequisites}
|
|---|
| 1602 | @var{command}
|
|---|
| 1603 | @dots{}
|
|---|
| 1604 | @end example
|
|---|
| 1605 |
|
|---|
| 1606 | @noindent
|
|---|
| 1607 | or like this:
|
|---|
| 1608 |
|
|---|
| 1609 | @example
|
|---|
| 1610 | @var{targets} : @var{prerequisites} ; @var{command}
|
|---|
| 1611 | @var{command}
|
|---|
| 1612 | @dots{}
|
|---|
| 1613 | @end example
|
|---|
| 1614 |
|
|---|
| 1615 | @cindex targets
|
|---|
| 1616 | @cindex rule targets
|
|---|
| 1617 | The @var{targets} are file names, separated by spaces. Wildcard
|
|---|
| 1618 | characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
|
|---|
| 1619 | in File Names}) and a name of the form @file{@var{a}(@var{m})}
|
|---|
| 1620 | represents member @var{m} in archive file @var{a}
|
|---|
| 1621 | (@pxref{Archive Members, ,Archive Members as Targets}).
|
|---|
| 1622 | Usually there is only one
|
|---|
| 1623 | target per rule, but occasionally there is a reason to have more
|
|---|
| 1624 | (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
|
|---|
| 1625 |
|
|---|
| 1626 | @cindex commands
|
|---|
| 1627 | @cindex tab character (in commands)
|
|---|
| 1628 | The @var{command} lines start with a tab character. The first command may
|
|---|
| 1629 | appear on the line after the prerequisites, with a tab character, or may
|
|---|
| 1630 | appear on the same line, with a semicolon. Either way, the effect is the
|
|---|
| 1631 | same. @xref{Commands, ,Writing the Commands in Rules}.
|
|---|
| 1632 |
|
|---|
| 1633 | @cindex dollar sign (@code{$}), in rules
|
|---|
| 1634 | @cindex @code{$}, in rules
|
|---|
| 1635 | @cindex rule, and @code{$}
|
|---|
| 1636 | Because dollar signs are used to start variable references, if you really
|
|---|
| 1637 | want a dollar sign in a rule you must write two of them, @samp{$$}
|
|---|
| 1638 | (@pxref{Using Variables, ,How to Use Variables}).
|
|---|
| 1639 | You may split a long line by inserting a backslash
|
|---|
| 1640 | followed by a newline, but this is not required, as @code{make} places no
|
|---|
| 1641 | limit on the length of a line in a makefile.
|
|---|
| 1642 |
|
|---|
| 1643 | A rule tells @code{make} two things: when the targets are out of date,
|
|---|
| 1644 | and how to update them when necessary.
|
|---|
| 1645 |
|
|---|
| 1646 | @cindex prerequisites
|
|---|
| 1647 | @cindex rule prerequisites
|
|---|
| 1648 | The criterion for being out of date is specified in terms of the
|
|---|
| 1649 | @var{prerequisites}, which consist of file names separated by spaces.
|
|---|
| 1650 | (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
|
|---|
| 1651 | A target is out of date if it does not exist or if it is older than any
|
|---|
| 1652 | of the prerequisites (by comparison of last-modification times). The
|
|---|
| 1653 | idea is that the contents of the target file are computed based on
|
|---|
| 1654 | information in the prerequisites, so if any of the prerequisites changes,
|
|---|
| 1655 | the contents of the existing target file are no longer necessarily
|
|---|
| 1656 | valid.
|
|---|
| 1657 |
|
|---|
| 1658 | How to update is specified by @var{commands}. These are lines to be
|
|---|
| 1659 | executed by the shell (normally @samp{sh}), but with some extra features
|
|---|
| 1660 | (@pxref{Commands, ,Writing the Commands in Rules}).
|
|---|
| 1661 |
|
|---|
| 1662 | @node Prerequisite Types, Wildcards, Rule Syntax, Rules
|
|---|
| 1663 | @comment node-name, next, previous, up
|
|---|
| 1664 | @section Types of Prerequisites
|
|---|
| 1665 | @cindex prerequisite types
|
|---|
| 1666 | @cindex types of prerequisites
|
|---|
| 1667 |
|
|---|
| 1668 | @cindex prerequisites, normal
|
|---|
| 1669 | @cindex normal prerequisites
|
|---|
| 1670 | @cindex prerequisites, order-only
|
|---|
| 1671 | @cindex order-only prerequisites
|
|---|
| 1672 | There are actually two different types of prerequisites understood by
|
|---|
| 1673 | GNU @code{make}: normal prerequisites such as described in the
|
|---|
| 1674 | previous section, and @dfn{order-only} prerequisites. A normal
|
|---|
| 1675 | prerequisite makes two statements: first, it imposes an order of
|
|---|
| 1676 | execution of build commands: any commands necessary to build any of a
|
|---|
| 1677 | target's prerequisites will be fully executed before any commands
|
|---|
| 1678 | necessary to build the target. Second, it imposes a dependency
|
|---|
| 1679 | relationship: if any prerequisite is newer than the target, then the
|
|---|
| 1680 | target is considered out-of-date and must be rebuilt.
|
|---|
| 1681 |
|
|---|
| 1682 | Normally, this is exactly what you want: if a target's prerequisite is
|
|---|
| 1683 | updated, then the target should also be updated.
|
|---|
| 1684 |
|
|---|
| 1685 | Occasionally, however, you have a situation where you want to impose a
|
|---|
| 1686 | specific ordering on the rules to be invoked @emph{without} forcing
|
|---|
| 1687 | the target to be updated if one of those rules is executed. In that
|
|---|
| 1688 | case, you want to define @dfn{order-only} prerequisites. Order-only
|
|---|
| 1689 | prerequisites can be specified by placing a pipe symbol (@code{|})
|
|---|
| 1690 | in the prerequisites list: any prerequisites to the left of the pipe
|
|---|
| 1691 | symbol are normal; any prerequisites to the right are order-only:
|
|---|
| 1692 |
|
|---|
| 1693 | @example
|
|---|
| 1694 | @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
|
|---|
| 1695 | @end example
|
|---|
| 1696 |
|
|---|
| 1697 | The normal prerequisites section may of course be empty. Also, you
|
|---|
| 1698 | may still declare multiple lines of prerequisites for the same target:
|
|---|
| 1699 | they are appended appropriately. Note that if you declare the same
|
|---|
| 1700 | file to be both a normal and an order-only prerequisite, the normal
|
|---|
| 1701 | prerequisite takes precedence (since they are a strict superset of the
|
|---|
| 1702 | behavior of an order-only prerequisite).
|
|---|
| 1703 |
|
|---|
| 1704 | @node Wildcards, Directory Search, Prerequisite Types, Rules
|
|---|
| 1705 | @section Using Wildcard Characters in File Names
|
|---|
| 1706 | @cindex wildcard
|
|---|
| 1707 | @cindex file name with wildcards
|
|---|
| 1708 | @cindex globbing (wildcards)
|
|---|
| 1709 |
|
|---|
| 1710 | @cindex @code{*} (wildcard character)
|
|---|
| 1711 | @cindex @code{?} (wildcard character)
|
|---|
| 1712 | @cindex @code{[@dots{}]} (wildcard characters)
|
|---|
| 1713 | A single file name can specify many files using @dfn{wildcard characters}.
|
|---|
| 1714 | The wildcard characters in @code{make} are @samp{*}, @samp{?} and
|
|---|
| 1715 | @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
|
|---|
| 1716 | specifies a list of all the files (in the working directory) whose names
|
|---|
| 1717 | end in @samp{.c}.@refill
|
|---|
| 1718 |
|
|---|
| 1719 | @cindex @code{~} (tilde)
|
|---|
| 1720 | @cindex tilde (@code{~})
|
|---|
| 1721 | @cindex home directory
|
|---|
| 1722 | The character @samp{~} at the beginning of a file name also has special
|
|---|
| 1723 | significance. If alone, or followed by a slash, it represents your home
|
|---|
| 1724 | directory. For example @file{~/bin} expands to @file{/home/you/bin}.
|
|---|
| 1725 | If the @samp{~} is followed by a word, the string represents the home
|
|---|
| 1726 | directory of the user named by that word. For example @file{~john/bin}
|
|---|
| 1727 | expands to @file{/home/john/bin}. On systems which don't have a home
|
|---|
| 1728 | directory for each user (such as MS-DOS or MS-Windows), this
|
|---|
| 1729 | functionality can be simulated by setting the environment variable
|
|---|
| 1730 | @var{HOME}.@refill
|
|---|
| 1731 |
|
|---|
| 1732 | Wildcard expansion happens automatically in targets, in prerequisites,
|
|---|
| 1733 | and in commands (where the shell does the expansion). In other
|
|---|
| 1734 | contexts, wildcard expansion happens only if you request it explicitly
|
|---|
| 1735 | with the @code{wildcard} function.
|
|---|
| 1736 |
|
|---|
| 1737 | The special significance of a wildcard character can be turned off by
|
|---|
| 1738 | preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
|
|---|
| 1739 | specific file whose name consists of @samp{foo}, an asterisk, and
|
|---|
| 1740 | @samp{bar}.@refill
|
|---|
| 1741 |
|
|---|
| 1742 | @menu
|
|---|
| 1743 | * Wildcard Examples:: Several examples
|
|---|
| 1744 | * Wildcard Pitfall:: Problems to avoid.
|
|---|
| 1745 | * Wildcard Function:: How to cause wildcard expansion where
|
|---|
| 1746 | it does not normally take place.
|
|---|
| 1747 | @end menu
|
|---|
| 1748 |
|
|---|
| 1749 | @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
|
|---|
| 1750 | @subsection Wildcard Examples
|
|---|
| 1751 |
|
|---|
| 1752 | Wildcards can be used in the commands of a rule, where they are expanded
|
|---|
| 1753 | by the shell. For example, here is a rule to delete all the object files:
|
|---|
| 1754 |
|
|---|
| 1755 | @example
|
|---|
| 1756 | @group
|
|---|
| 1757 | clean:
|
|---|
| 1758 | rm -f *.o
|
|---|
| 1759 | @end group
|
|---|
| 1760 | @end example
|
|---|
| 1761 | @cindex @code{rm} (shell command)
|
|---|
| 1762 |
|
|---|
| 1763 | Wildcards are also useful in the prerequisites of a rule. With the
|
|---|
| 1764 | following rule in the makefile, @samp{make print} will print all the
|
|---|
| 1765 | @samp{.c} files that have changed since the last time you printed them:
|
|---|
| 1766 |
|
|---|
| 1767 | @example
|
|---|
| 1768 | print: *.c
|
|---|
| 1769 | lpr -p $?
|
|---|
| 1770 | touch print
|
|---|
| 1771 | @end example
|
|---|
| 1772 |
|
|---|
| 1773 | @cindex @code{print} target
|
|---|
| 1774 | @cindex @code{lpr} (shell command)
|
|---|
| 1775 | @cindex @code{touch} (shell command)
|
|---|
| 1776 | @noindent
|
|---|
| 1777 | This rule uses @file{print} as an empty target file; see @ref{Empty
|
|---|
| 1778 | Targets, ,Empty Target Files to Record Events}. (The automatic variable
|
|---|
| 1779 | @samp{$?} is used to print only those files that have changed; see
|
|---|
| 1780 | @ref{Automatic Variables}.)@refill
|
|---|
| 1781 |
|
|---|
| 1782 | Wildcard expansion does not happen when you define a variable. Thus, if
|
|---|
| 1783 | you write this:
|
|---|
| 1784 |
|
|---|
| 1785 | @example
|
|---|
| 1786 | objects = *.o
|
|---|
| 1787 | @end example
|
|---|
| 1788 |
|
|---|
| 1789 | @noindent
|
|---|
| 1790 | then the value of the variable @code{objects} is the actual string
|
|---|
| 1791 | @samp{*.o}. However, if you use the value of @code{objects} in a target,
|
|---|
| 1792 | prerequisite or command, wildcard expansion will take place at that time.
|
|---|
| 1793 | To set @code{objects} to the expansion, instead use:
|
|---|
| 1794 |
|
|---|
| 1795 | @example
|
|---|
| 1796 | objects := $(wildcard *.o)
|
|---|
| 1797 | @end example
|
|---|
| 1798 |
|
|---|
| 1799 | @noindent
|
|---|
| 1800 | @xref{Wildcard Function}.
|
|---|
| 1801 |
|
|---|
| 1802 | @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
|
|---|
| 1803 | @subsection Pitfalls of Using Wildcards
|
|---|
| 1804 | @cindex wildcard pitfalls
|
|---|
| 1805 | @cindex pitfalls of wildcards
|
|---|
| 1806 | @cindex mistakes with wildcards
|
|---|
| 1807 | @cindex errors with wildcards
|
|---|
| 1808 | @cindex problems with wildcards
|
|---|
| 1809 |
|
|---|
| 1810 | Now here is an example of a naive way of using wildcard expansion, that
|
|---|
| 1811 | does not do what you would intend. Suppose you would like to say that the
|
|---|
| 1812 | executable file @file{foo} is made from all the object files in the
|
|---|
| 1813 | directory, and you write this:
|
|---|
| 1814 |
|
|---|
| 1815 | @example
|
|---|
| 1816 | objects = *.o
|
|---|
| 1817 |
|
|---|
| 1818 | foo : $(objects)
|
|---|
| 1819 | cc -o foo $(CFLAGS) $(objects)
|
|---|
| 1820 | @end example
|
|---|
| 1821 |
|
|---|
| 1822 | @noindent
|
|---|
| 1823 | The value of @code{objects} is the actual string @samp{*.o}. Wildcard
|
|---|
| 1824 | expansion happens in the rule for @file{foo}, so that each @emph{existing}
|
|---|
| 1825 | @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
|
|---|
| 1826 | necessary.
|
|---|
| 1827 |
|
|---|
| 1828 | But what if you delete all the @samp{.o} files? When a wildcard matches
|
|---|
| 1829 | no files, it is left as it is, so then @file{foo} will depend on the
|
|---|
| 1830 | oddly-named file @file{*.o}. Since no such file is likely to exist,
|
|---|
| 1831 | @code{make} will give you an error saying it cannot figure out how to
|
|---|
| 1832 | make @file{*.o}. This is not what you want!
|
|---|
| 1833 |
|
|---|
| 1834 | Actually it is possible to obtain the desired result with wildcard
|
|---|
| 1835 | expansion, but you need more sophisticated techniques, including the
|
|---|
| 1836 | @code{wildcard} function and string substitution.
|
|---|
| 1837 | @ifnottex
|
|---|
| 1838 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
|---|
| 1839 | @end ifnottex
|
|---|
| 1840 | @iftex
|
|---|
| 1841 | These are described in the following section.
|
|---|
| 1842 | @end iftex
|
|---|
| 1843 |
|
|---|
| 1844 | @cindex wildcards and MS-DOS/MS-Windows backslashes
|
|---|
| 1845 | @cindex backslashes in pathnames and wildcard expansion
|
|---|
| 1846 |
|
|---|
| 1847 | Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
|
|---|
| 1848 | separate directories in pathnames, like so:
|
|---|
| 1849 |
|
|---|
| 1850 | @example
|
|---|
| 1851 | c:\foo\bar\baz.c
|
|---|
| 1852 | @end example
|
|---|
| 1853 |
|
|---|
| 1854 | This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
|
|---|
| 1855 | @file{c:} part is the so-called drive letter). When @code{make} runs on
|
|---|
| 1856 | these systems, it supports backslashes as well as the Unix-style forward
|
|---|
| 1857 | slashes in pathnames. However, this support does @emph{not} include the
|
|---|
| 1858 | wildcard expansion, where backslash is a quote character. Therefore,
|
|---|
| 1859 | you @emph{must} use Unix-style slashes in these cases.
|
|---|
| 1860 |
|
|---|
| 1861 |
|
|---|
| 1862 | @node Wildcard Function, , Wildcard Pitfall, Wildcards
|
|---|
| 1863 | @subsection The Function @code{wildcard}
|
|---|
| 1864 | @findex wildcard
|
|---|
| 1865 |
|
|---|
| 1866 | Wildcard expansion happens automatically in rules. But wildcard expansion
|
|---|
| 1867 | does not normally take place when a variable is set, or inside the
|
|---|
| 1868 | arguments of a function. If you want to do wildcard expansion in such
|
|---|
| 1869 | places, you need to use the @code{wildcard} function, like this:
|
|---|
| 1870 |
|
|---|
| 1871 | @example
|
|---|
| 1872 | $(wildcard @var{pattern}@dots{})
|
|---|
| 1873 | @end example
|
|---|
| 1874 |
|
|---|
| 1875 | @noindent
|
|---|
| 1876 | This string, used anywhere in a makefile, is replaced by a
|
|---|
| 1877 | space-separated list of names of existing files that match one of the
|
|---|
| 1878 | given file name patterns. If no existing file name matches a pattern,
|
|---|
| 1879 | then that pattern is omitted from the output of the @code{wildcard}
|
|---|
| 1880 | function. Note that this is different from how unmatched wildcards
|
|---|
| 1881 | behave in rules, where they are used verbatim rather than ignored
|
|---|
| 1882 | (@pxref{Wildcard Pitfall}).
|
|---|
| 1883 |
|
|---|
| 1884 | One use of the @code{wildcard} function is to get a list of all the C source
|
|---|
| 1885 | files in a directory, like this:
|
|---|
| 1886 |
|
|---|
| 1887 | @example
|
|---|
| 1888 | $(wildcard *.c)
|
|---|
| 1889 | @end example
|
|---|
| 1890 |
|
|---|
| 1891 | We can change the list of C source files into a list of object files by
|
|---|
| 1892 | replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
|
|---|
| 1893 |
|
|---|
| 1894 | @example
|
|---|
| 1895 | $(patsubst %.c,%.o,$(wildcard *.c))
|
|---|
| 1896 | @end example
|
|---|
| 1897 |
|
|---|
| 1898 | @noindent
|
|---|
| 1899 | (Here we have used another function, @code{patsubst}.
|
|---|
| 1900 | @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
|
|---|
| 1901 |
|
|---|
| 1902 | Thus, a makefile to compile all C source files in the directory and then
|
|---|
| 1903 | link them together could be written as follows:
|
|---|
| 1904 |
|
|---|
| 1905 | @example
|
|---|
| 1906 | objects := $(patsubst %.c,%.o,$(wildcard *.c))
|
|---|
| 1907 |
|
|---|
| 1908 | foo : $(objects)
|
|---|
| 1909 | cc -o foo $(objects)
|
|---|
| 1910 | @end example
|
|---|
| 1911 |
|
|---|
| 1912 | @noindent
|
|---|
| 1913 | (This takes advantage of the implicit rule for compiling C programs, so
|
|---|
| 1914 | there is no need to write explicit rules for compiling the files.
|
|---|
| 1915 | @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
|
|---|
| 1916 | @samp{:=}, which is a variant of @samp{=}.)
|
|---|
| 1917 |
|
|---|
| 1918 | @node Directory Search, Phony Targets, Wildcards, Rules
|
|---|
| 1919 | @section Searching Directories for Prerequisites
|
|---|
| 1920 | @vindex VPATH
|
|---|
| 1921 | @findex vpath
|
|---|
| 1922 | @cindex vpath
|
|---|
| 1923 | @cindex search path for prerequisites (@code{VPATH})
|
|---|
| 1924 | @cindex directory search (@code{VPATH})
|
|---|
| 1925 |
|
|---|
| 1926 | For large systems, it is often desirable to put sources in a separate
|
|---|
| 1927 | directory from the binaries. The @dfn{directory search} features of
|
|---|
| 1928 | @code{make} facilitate this by searching several directories
|
|---|
| 1929 | automatically to find a prerequisite. When you redistribute the files
|
|---|
| 1930 | among directories, you do not need to change the individual rules,
|
|---|
| 1931 | just the search paths.
|
|---|
| 1932 |
|
|---|
| 1933 | @menu
|
|---|
| 1934 | * General Search:: Specifying a search path that applies
|
|---|
| 1935 | to every prerequisite.
|
|---|
| 1936 | * Selective Search:: Specifying a search path
|
|---|
| 1937 | for a specified class of names.
|
|---|
| 1938 | * Search Algorithm:: When and how search paths are applied.
|
|---|
| 1939 | * Commands/Search:: How to write shell commands that work together
|
|---|
| 1940 | with search paths.
|
|---|
| 1941 | * Implicit/Search:: How search paths affect implicit rules.
|
|---|
| 1942 | * Libraries/Search:: Directory search for link libraries.
|
|---|
| 1943 | @end menu
|
|---|
| 1944 |
|
|---|
| 1945 | @node General Search, Selective Search, Directory Search, Directory Search
|
|---|
| 1946 | @subsection @code{VPATH}: Search Path for All Prerequisites
|
|---|
| 1947 | @vindex VPATH
|
|---|
| 1948 |
|
|---|
| 1949 | The value of the @code{make} variable @code{VPATH} specifies a list of
|
|---|
| 1950 | directories that @code{make} should search. Most often, the
|
|---|
| 1951 | directories are expected to contain prerequisite files that are not in the
|
|---|
| 1952 | current directory; however, @code{make} uses @code{VPATH} as a search
|
|---|
| 1953 | list for both prerequisites and targets of rules.
|
|---|
| 1954 |
|
|---|
| 1955 | Thus, if a file that is listed as a target or prerequisite does not exist
|
|---|
| 1956 | in the current directory, @code{make} searches the directories listed in
|
|---|
| 1957 | @code{VPATH} for a file with that name. If a file is found in one of
|
|---|
| 1958 | them, that file may become the prerequisite (see below). Rules may then
|
|---|
| 1959 | specify the names of files in the prerequisite list as if they all
|
|---|
| 1960 | existed in the current directory. @xref{Commands/Search, ,Writing Shell
|
|---|
| 1961 | Commands with Directory Search}.
|
|---|
| 1962 |
|
|---|
| 1963 | In the @code{VPATH} variable, directory names are separated by colons or
|
|---|
| 1964 | blanks. The order in which directories are listed is the order followed
|
|---|
| 1965 | by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
|
|---|
| 1966 | are used as separators of directory names in @code{VPATH}, since the
|
|---|
| 1967 | colon can be used in the pathname itself, after the drive letter.)
|
|---|
| 1968 |
|
|---|
| 1969 | For example,
|
|---|
| 1970 |
|
|---|
| 1971 | @example
|
|---|
| 1972 | VPATH = src:../headers
|
|---|
| 1973 | @end example
|
|---|
| 1974 |
|
|---|
| 1975 | @noindent
|
|---|
| 1976 | specifies a path containing two directories, @file{src} and
|
|---|
| 1977 | @file{../headers}, which @code{make} searches in that order.
|
|---|
| 1978 |
|
|---|
| 1979 | With this value of @code{VPATH}, the following rule,
|
|---|
| 1980 |
|
|---|
| 1981 | @example
|
|---|
| 1982 | foo.o : foo.c
|
|---|
| 1983 | @end example
|
|---|
| 1984 |
|
|---|
| 1985 | @noindent
|
|---|
| 1986 | is interpreted as if it were written like this:
|
|---|
| 1987 |
|
|---|
| 1988 | @example
|
|---|
| 1989 | foo.o : src/foo.c
|
|---|
| 1990 | @end example
|
|---|
| 1991 |
|
|---|
| 1992 | @noindent
|
|---|
| 1993 | assuming the file @file{foo.c} does not exist in the current directory but
|
|---|
| 1994 | is found in the directory @file{src}.
|
|---|
| 1995 |
|
|---|
| 1996 | @node Selective Search, Search Algorithm, General Search, Directory Search
|
|---|
| 1997 | @subsection The @code{vpath} Directive
|
|---|
| 1998 | @findex vpath
|
|---|
| 1999 |
|
|---|
| 2000 | Similar to the @code{VPATH} variable, but more selective, is the
|
|---|
| 2001 | @code{vpath} directive (note lower case), which allows you to specify a
|
|---|
| 2002 | search path for a particular class of file names: those that match a
|
|---|
| 2003 | particular pattern. Thus you can supply certain search directories for
|
|---|
| 2004 | one class of file names and other directories (or none) for other file
|
|---|
| 2005 | names.
|
|---|
| 2006 |
|
|---|
| 2007 | There are three forms of the @code{vpath} directive:
|
|---|
| 2008 |
|
|---|
| 2009 | @table @code
|
|---|
| 2010 | @item vpath @var{pattern} @var{directories}
|
|---|
| 2011 | Specify the search path @var{directories} for file names that match
|
|---|
| 2012 | @var{pattern}.
|
|---|
| 2013 |
|
|---|
| 2014 | The search path, @var{directories}, is a list of directories to be
|
|---|
| 2015 | searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
|
|---|
| 2016 | blanks, just like the search path used in the @code{VPATH} variable.
|
|---|
| 2017 |
|
|---|
| 2018 | @item vpath @var{pattern}
|
|---|
| 2019 | Clear out the search path associated with @var{pattern}.
|
|---|
| 2020 |
|
|---|
| 2021 | @c Extra blank line makes sure this gets two lines.
|
|---|
| 2022 | @item vpath
|
|---|
| 2023 |
|
|---|
| 2024 | Clear all search paths previously specified with @code{vpath} directives.
|
|---|
| 2025 | @end table
|
|---|
| 2026 |
|
|---|
| 2027 | A @code{vpath} pattern is a string containing a @samp{%} character. The
|
|---|
| 2028 | string must match the file name of a prerequisite that is being searched
|
|---|
| 2029 | for, the @samp{%} character matching any sequence of zero or more
|
|---|
| 2030 | characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
|
|---|
| 2031 | Redefining Pattern Rules}). For example, @code{%.h} matches files that
|
|---|
| 2032 | end in @code{.h}. (If there is no @samp{%}, the pattern must match the
|
|---|
| 2033 | prerequisite exactly, which is not useful very often.)
|
|---|
| 2034 |
|
|---|
| 2035 | @cindex @code{%}, quoting in @code{vpath}
|
|---|
| 2036 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 2037 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 2038 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 2039 | @cindex quoting @code{%}, in @code{vpath}
|
|---|
| 2040 | @samp{%} characters in a @code{vpath} directive's pattern can be quoted
|
|---|
| 2041 | with preceding backslashes (@samp{\}). Backslashes that would otherwise
|
|---|
| 2042 | quote @samp{%} characters can be quoted with more backslashes.
|
|---|
| 2043 | Backslashes that quote @samp{%} characters or other backslashes are
|
|---|
| 2044 | removed from the pattern before it is compared to file names. Backslashes
|
|---|
| 2045 | that are not in danger of quoting @samp{%} characters go unmolested.@refill
|
|---|
| 2046 |
|
|---|
| 2047 | When a prerequisite fails to exist in the current directory, if the
|
|---|
| 2048 | @var{pattern} in a @code{vpath} directive matches the name of the
|
|---|
| 2049 | prerequisite file, then the @var{directories} in that directive are searched
|
|---|
| 2050 | just like (and before) the directories in the @code{VPATH} variable.
|
|---|
| 2051 |
|
|---|
| 2052 | For example,
|
|---|
| 2053 |
|
|---|
| 2054 | @example
|
|---|
| 2055 | vpath %.h ../headers
|
|---|
| 2056 | @end example
|
|---|
| 2057 |
|
|---|
| 2058 | @noindent
|
|---|
| 2059 | tells @code{make} to look for any prerequisite whose name ends in @file{.h}
|
|---|
| 2060 | in the directory @file{../headers} if the file is not found in the current
|
|---|
| 2061 | directory.
|
|---|
| 2062 |
|
|---|
| 2063 | If several @code{vpath} patterns match the prerequisite file's name, then
|
|---|
| 2064 | @code{make} processes each matching @code{vpath} directive one by one,
|
|---|
| 2065 | searching all the directories mentioned in each directive. @code{make}
|
|---|
| 2066 | handles multiple @code{vpath} directives in the order in which they
|
|---|
| 2067 | appear in the makefile; multiple directives with the same pattern are
|
|---|
| 2068 | independent of each other.
|
|---|
| 2069 |
|
|---|
| 2070 | @need 750
|
|---|
| 2071 | Thus,
|
|---|
| 2072 |
|
|---|
| 2073 | @example
|
|---|
| 2074 | @group
|
|---|
| 2075 | vpath %.c foo
|
|---|
| 2076 | vpath % blish
|
|---|
| 2077 | vpath %.c bar
|
|---|
| 2078 | @end group
|
|---|
| 2079 | @end example
|
|---|
| 2080 |
|
|---|
| 2081 | @noindent
|
|---|
| 2082 | will look for a file ending in @samp{.c} in @file{foo}, then
|
|---|
| 2083 | @file{blish}, then @file{bar}, while
|
|---|
| 2084 |
|
|---|
| 2085 | @example
|
|---|
| 2086 | @group
|
|---|
| 2087 | vpath %.c foo:bar
|
|---|
| 2088 | vpath % blish
|
|---|
| 2089 | @end group
|
|---|
| 2090 | @end example
|
|---|
| 2091 |
|
|---|
| 2092 | @noindent
|
|---|
| 2093 | will look for a file ending in @samp{.c} in @file{foo}, then
|
|---|
| 2094 | @file{bar}, then @file{blish}.
|
|---|
| 2095 |
|
|---|
| 2096 | @node Search Algorithm, Commands/Search, Selective Search, Directory Search
|
|---|
| 2097 | @subsection How Directory Searches are Performed
|
|---|
| 2098 | @cindex algorithm for directory search
|
|---|
| 2099 | @cindex directory search algorithm
|
|---|
| 2100 |
|
|---|
| 2101 | When a prerequisite is found through directory search, regardless of type
|
|---|
| 2102 | (general or selective), the pathname located may not be the one that
|
|---|
| 2103 | @code{make} actually provides you in the prerequisite list. Sometimes
|
|---|
| 2104 | the path discovered through directory search is thrown away.
|
|---|
| 2105 |
|
|---|
| 2106 | The algorithm @code{make} uses to decide whether to keep or abandon a
|
|---|
| 2107 | path found via directory search is as follows:
|
|---|
| 2108 |
|
|---|
| 2109 | @enumerate
|
|---|
| 2110 | @item
|
|---|
| 2111 | If a target file does not exist at the path specified in the makefile,
|
|---|
| 2112 | directory search is performed.
|
|---|
| 2113 |
|
|---|
| 2114 | @item
|
|---|
| 2115 | If the directory search is successful, that path is kept and this file
|
|---|
| 2116 | is tentatively stored as the target.
|
|---|
| 2117 |
|
|---|
| 2118 | @item
|
|---|
| 2119 | All prerequisites of this target are examined using this same method.
|
|---|
| 2120 |
|
|---|
| 2121 | @item
|
|---|
| 2122 | After processing the prerequisites, the target may or may not need to be
|
|---|
| 2123 | rebuilt:
|
|---|
| 2124 |
|
|---|
| 2125 | @enumerate a
|
|---|
| 2126 | @item
|
|---|
| 2127 | If the target does @emph{not} need to be rebuilt, the path to the file
|
|---|
| 2128 | found during directory search is used for any prerequisite lists which
|
|---|
| 2129 | contain this target. In short, if @code{make} doesn't need to rebuild
|
|---|
| 2130 | the target then you use the path found via directory search.
|
|---|
| 2131 |
|
|---|
| 2132 | @item
|
|---|
| 2133 | If the target @emph{does} need to be rebuilt (is out-of-date), the
|
|---|
| 2134 | pathname found during directory search is @emph{thrown away}, and the
|
|---|
| 2135 | target is rebuilt using the file name specified in the makefile. In
|
|---|
| 2136 | short, if @code{make} must rebuild, then the target is rebuilt locally,
|
|---|
| 2137 | not in the directory found via directory search.
|
|---|
| 2138 | @end enumerate
|
|---|
| 2139 | @end enumerate
|
|---|
| 2140 |
|
|---|
| 2141 | This algorithm may seem complex, but in practice it is quite often
|
|---|
| 2142 | exactly what you want.
|
|---|
| 2143 |
|
|---|
| 2144 | @cindex traditional directory search (GPATH)
|
|---|
| 2145 | @cindex directory search, traditional (GPATH)
|
|---|
| 2146 | Other versions of @code{make} use a simpler algorithm: if the file does
|
|---|
| 2147 | not exist, and it is found via directory search, then that pathname is
|
|---|
| 2148 | always used whether or not the target needs to be built. Thus, if the
|
|---|
| 2149 | target is rebuilt it is created at the pathname discovered during
|
|---|
| 2150 | directory search.
|
|---|
| 2151 |
|
|---|
| 2152 | @vindex GPATH
|
|---|
| 2153 | If, in fact, this is the behavior you want for some or all of your
|
|---|
| 2154 | directories, you can use the @code{GPATH} variable to indicate this to
|
|---|
| 2155 | @code{make}.
|
|---|
| 2156 |
|
|---|
| 2157 | @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
|
|---|
| 2158 | space- or colon-delimited list of pathnames). If an out-of-date target
|
|---|
| 2159 | is found by directory search in a directory that also appears in
|
|---|
| 2160 | @code{GPATH}, then that pathname is not thrown away. The target is
|
|---|
| 2161 | rebuilt using the expanded path.
|
|---|
| 2162 |
|
|---|
| 2163 | @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
|
|---|
| 2164 | @subsection Writing Shell Commands with Directory Search
|
|---|
| 2165 | @cindex shell command, and directory search
|
|---|
| 2166 | @cindex directory search (@code{VPATH}), and shell commands
|
|---|
| 2167 |
|
|---|
| 2168 | When a prerequisite is found in another directory through directory search,
|
|---|
| 2169 | this cannot change the commands of the rule; they will execute as written.
|
|---|
| 2170 | Therefore, you must write the commands with care so that they will look for
|
|---|
| 2171 | the prerequisite in the directory where @code{make} finds it.
|
|---|
| 2172 |
|
|---|
| 2173 | This is done with the @dfn{automatic variables} such as @samp{$^}
|
|---|
| 2174 | (@pxref{Automatic Variables}).
|
|---|
| 2175 | For instance, the value of @samp{$^} is a
|
|---|
| 2176 | list of all the prerequisites of the rule, including the names of
|
|---|
| 2177 | the directories in which they were found, and the value of
|
|---|
| 2178 | @samp{$@@} is the target. Thus:@refill
|
|---|
| 2179 |
|
|---|
| 2180 | @example
|
|---|
| 2181 | foo.o : foo.c
|
|---|
| 2182 | cc -c $(CFLAGS) $^ -o $@@
|
|---|
| 2183 | @end example
|
|---|
| 2184 |
|
|---|
| 2185 | @noindent
|
|---|
| 2186 | (The variable @code{CFLAGS} exists so you can specify flags for C
|
|---|
| 2187 | compilation by implicit rules; we use it here for consistency so it will
|
|---|
| 2188 | affect all C compilations uniformly;
|
|---|
| 2189 | @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
|
|---|
| 2190 |
|
|---|
| 2191 | Often the prerequisites include header files as well, which you do not
|
|---|
| 2192 | want to mention in the commands. The automatic variable @samp{$<} is
|
|---|
| 2193 | just the first prerequisite:
|
|---|
| 2194 |
|
|---|
| 2195 | @example
|
|---|
| 2196 | VPATH = src:../headers
|
|---|
| 2197 | foo.o : foo.c defs.h hack.h
|
|---|
| 2198 | cc -c $(CFLAGS) $< -o $@@
|
|---|
| 2199 | @end example
|
|---|
| 2200 |
|
|---|
| 2201 | @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
|
|---|
| 2202 | @subsection Directory Search and Implicit Rules
|
|---|
| 2203 | @cindex @code{VPATH}, and implicit rules
|
|---|
| 2204 | @cindex directory search (@code{VPATH}), and implicit rules
|
|---|
| 2205 | @cindex search path for prerequisites (@code{VPATH}), and implicit rules
|
|---|
| 2206 | @cindex implicit rule, and directory search
|
|---|
| 2207 | @cindex implicit rule, and @code{VPATH}
|
|---|
| 2208 | @cindex rule, implicit, and directory search
|
|---|
| 2209 | @cindex rule, implicit, and @code{VPATH}
|
|---|
| 2210 |
|
|---|
| 2211 | The search through the directories specified in @code{VPATH} or with
|
|---|
| 2212 | @code{vpath} also happens during consideration of implicit rules
|
|---|
| 2213 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 2214 |
|
|---|
| 2215 | For example, when a file @file{foo.o} has no explicit rule, @code{make}
|
|---|
| 2216 | considers implicit rules, such as the built-in rule to compile
|
|---|
| 2217 | @file{foo.c} if that file exists. If such a file is lacking in the
|
|---|
| 2218 | current directory, the appropriate directories are searched for it. If
|
|---|
| 2219 | @file{foo.c} exists (or is mentioned in the makefile) in any of the
|
|---|
| 2220 | directories, the implicit rule for C compilation is applied.
|
|---|
| 2221 |
|
|---|
| 2222 | The commands of implicit rules normally use automatic variables as a
|
|---|
| 2223 | matter of necessity; consequently they will use the file names found by
|
|---|
| 2224 | directory search with no extra effort.
|
|---|
| 2225 |
|
|---|
| 2226 | @node Libraries/Search, , Implicit/Search, Directory Search
|
|---|
| 2227 | @subsection Directory Search for Link Libraries
|
|---|
| 2228 | @cindex link libraries, and directory search
|
|---|
| 2229 | @cindex libraries for linking, directory search
|
|---|
| 2230 | @cindex directory search (@code{VPATH}), and link libraries
|
|---|
| 2231 | @cindex @code{VPATH}, and link libraries
|
|---|
| 2232 | @cindex search path for prerequisites (@code{VPATH}), and link libraries
|
|---|
| 2233 | @cindex @code{-l} (library search)
|
|---|
| 2234 | @cindex link libraries, patterns matching
|
|---|
| 2235 | @cindex @code{.LIBPATTERNS}, and link libraries
|
|---|
| 2236 | @vindex .LIBPATTERNS
|
|---|
| 2237 |
|
|---|
| 2238 | Directory search applies in a special way to libraries used with the
|
|---|
| 2239 | linker. This special feature comes into play when you write a prerequisite
|
|---|
| 2240 | whose name is of the form @samp{-l@var{name}}. (You can tell something
|
|---|
| 2241 | strange is going on here because the prerequisite is normally the name of a
|
|---|
| 2242 | file, and the @emph{file name} of a library generally looks like
|
|---|
| 2243 | @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
|
|---|
| 2244 |
|
|---|
| 2245 | When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
|
|---|
| 2246 | handles it specially by searching for the file @file{lib@var{name}.so} in
|
|---|
| 2247 | the current directory, in directories specified by matching @code{vpath}
|
|---|
| 2248 | search paths and the @code{VPATH} search path, and then in the
|
|---|
| 2249 | directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
|
|---|
| 2250 | (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
|
|---|
| 2251 | @code{make} behave as if @var{prefix} is defined to be the root of the
|
|---|
| 2252 | DJGPP installation tree).
|
|---|
| 2253 |
|
|---|
| 2254 | If that file is not found, then the file @file{lib@var{name}.a} is
|
|---|
| 2255 | searched for, in the same directories as above.
|
|---|
| 2256 |
|
|---|
| 2257 | For example, if there is a @file{/usr/lib/libcurses.a} library on your
|
|---|
| 2258 | system (and no @file{/usr/lib/libcurses.so} file), then
|
|---|
| 2259 |
|
|---|
| 2260 | @example
|
|---|
| 2261 | @group
|
|---|
| 2262 | foo : foo.c -lcurses
|
|---|
| 2263 | cc $^ -o $@@
|
|---|
| 2264 | @end group
|
|---|
| 2265 | @end example
|
|---|
| 2266 |
|
|---|
| 2267 | @noindent
|
|---|
| 2268 | would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
|
|---|
| 2269 | be executed when @file{foo} is older than @file{foo.c} or than
|
|---|
| 2270 | @file{/usr/lib/libcurses.a}.@refill
|
|---|
| 2271 |
|
|---|
| 2272 | Although the default set of files to be searched for is
|
|---|
| 2273 | @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
|
|---|
| 2274 | via the @code{.LIBPATTERNS} variable. Each word in the value of this
|
|---|
| 2275 | variable is a pattern string. When a prerequisite like
|
|---|
| 2276 | @samp{-l@var{name}} is seen, @code{make} will replace the percent in
|
|---|
| 2277 | each pattern in the list with @var{name} and perform the above directory
|
|---|
| 2278 | searches using that library filename. If no library is found, the next
|
|---|
| 2279 | word in the list will be used.
|
|---|
| 2280 |
|
|---|
| 2281 | The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
|
|---|
| 2282 | which provides the default behavior described above.
|
|---|
| 2283 |
|
|---|
| 2284 | You can turn off link library expansion completely by setting this
|
|---|
| 2285 | variable to an empty value.
|
|---|
| 2286 |
|
|---|
| 2287 | @node Phony Targets, Force Targets, Directory Search, Rules
|
|---|
| 2288 | @section Phony Targets
|
|---|
| 2289 | @cindex phony targets
|
|---|
| 2290 | @cindex targets, phony
|
|---|
| 2291 | @cindex targets without a file
|
|---|
| 2292 |
|
|---|
| 2293 | A phony target is one that is not really the name of a file. It is just a
|
|---|
| 2294 | name for some commands to be executed when you make an explicit request.
|
|---|
| 2295 | There are two reasons to use a phony target: to avoid a conflict with
|
|---|
| 2296 | a file of the same name, and to improve performance.
|
|---|
| 2297 |
|
|---|
| 2298 | If you write a rule whose commands will not create the target file, the
|
|---|
| 2299 | commands will be executed every time the target comes up for remaking.
|
|---|
| 2300 | Here is an example:
|
|---|
| 2301 |
|
|---|
| 2302 | @example
|
|---|
| 2303 | @group
|
|---|
| 2304 | clean:
|
|---|
| 2305 | rm *.o temp
|
|---|
| 2306 | @end group
|
|---|
| 2307 | @end example
|
|---|
| 2308 |
|
|---|
| 2309 | @noindent
|
|---|
| 2310 | Because the @code{rm} command does not create a file named @file{clean},
|
|---|
| 2311 | probably no such file will ever exist. Therefore, the @code{rm} command
|
|---|
| 2312 | will be executed every time you say @samp{make clean}.
|
|---|
| 2313 | @cindex @code{rm} (shell command)
|
|---|
| 2314 |
|
|---|
| 2315 | @findex .PHONY
|
|---|
| 2316 | The phony target will cease to work if anything ever does create a file
|
|---|
| 2317 | named @file{clean} in this directory. Since it has no prerequisites, the
|
|---|
| 2318 | file @file{clean} would inevitably be considered up to date, and its
|
|---|
| 2319 | commands would not be executed. To avoid this problem, you can explicitly
|
|---|
| 2320 | declare the target to be phony, using the special target @code{.PHONY}
|
|---|
| 2321 | (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
|
|---|
| 2322 |
|
|---|
| 2323 | @example
|
|---|
| 2324 | .PHONY : clean
|
|---|
| 2325 | @end example
|
|---|
| 2326 |
|
|---|
| 2327 | @noindent
|
|---|
| 2328 | Once this is done, @samp{make clean} will run the commands regardless of
|
|---|
| 2329 | whether there is a file named @file{clean}.
|
|---|
| 2330 |
|
|---|
| 2331 | Since it knows that phony targets do not name actual files that could be
|
|---|
| 2332 | remade from other files, @code{make} skips the implicit rule search for
|
|---|
| 2333 | phony targets (@pxref{Implicit Rules}). This is why declaring a target
|
|---|
| 2334 | phony is good for performance, even if you are not worried about the
|
|---|
| 2335 | actual file existing.
|
|---|
| 2336 |
|
|---|
| 2337 | Thus, you first write the line that states that @code{clean} is a
|
|---|
| 2338 | phony target, then you write the rule, like this:
|
|---|
| 2339 |
|
|---|
| 2340 | @example
|
|---|
| 2341 | @group
|
|---|
| 2342 | .PHONY: clean
|
|---|
| 2343 | clean:
|
|---|
| 2344 | rm *.o temp
|
|---|
| 2345 | @end group
|
|---|
| 2346 | @end example
|
|---|
| 2347 |
|
|---|
| 2348 | Another example of the usefulness of phony targets is in conjunction
|
|---|
| 2349 | with recursive invocations of @code{make} (for more information, see
|
|---|
| 2350 | @ref{Recursion, ,Recursive Use of @code{make}}). In this case the
|
|---|
| 2351 | makefile will often contain a variable which lists a number of
|
|---|
| 2352 | subdirectories to be built. One way to handle this is with one rule
|
|---|
| 2353 | whose command is a shell loop over the subdirectories, like this:
|
|---|
| 2354 |
|
|---|
| 2355 | @example
|
|---|
| 2356 | @group
|
|---|
| 2357 | SUBDIRS = foo bar baz
|
|---|
| 2358 |
|
|---|
| 2359 | subdirs:
|
|---|
| 2360 | for dir in $(SUBDIRS); do \
|
|---|
| 2361 | $(MAKE) -C $$dir; \
|
|---|
| 2362 | done
|
|---|
| 2363 | @end group
|
|---|
| 2364 | @end example
|
|---|
| 2365 |
|
|---|
| 2366 | There are a few problems with this method, however. First, any error
|
|---|
| 2367 | detected in a submake is not noted by this rule, so it will continue to
|
|---|
| 2368 | build the rest of the directories even when one fails. This can be
|
|---|
| 2369 | overcome by adding shell commands to note the error and exit, but then
|
|---|
| 2370 | it will do so even if @code{make} is invoked with the @code{-k} option,
|
|---|
| 2371 | which is unfortunate. Second, and perhaps more importantly, you cannot
|
|---|
| 2372 | take advantage of the parallel build capabilities of make using this
|
|---|
| 2373 | method, since there is only one rule.
|
|---|
| 2374 |
|
|---|
| 2375 | By declaring the subdirectories as phony targets (you must do this as
|
|---|
| 2376 | the subdirectory obviously always exists; otherwise it won't be built)
|
|---|
| 2377 | you can remove these problems:
|
|---|
| 2378 |
|
|---|
| 2379 | @example
|
|---|
| 2380 | @group
|
|---|
| 2381 | SUBDIRS = foo bar baz
|
|---|
| 2382 |
|
|---|
| 2383 | .PHONY: subdirs $(SUBDIRS)
|
|---|
| 2384 |
|
|---|
| 2385 | subdirs: $(SUBDIRS)
|
|---|
| 2386 |
|
|---|
| 2387 | $(SUBDIRS):
|
|---|
| 2388 | $(MAKE) -C $@@
|
|---|
| 2389 |
|
|---|
| 2390 | foo: baz
|
|---|
| 2391 | @end group
|
|---|
| 2392 | @end example
|
|---|
| 2393 |
|
|---|
| 2394 | Here we've also declared that the @file{foo} subdirectory cannot be
|
|---|
| 2395 | built until after the @file{baz} subdirectory is complete; this kind of
|
|---|
| 2396 | relationship declaration is particularly important when attempting
|
|---|
| 2397 | parallel builds.
|
|---|
| 2398 |
|
|---|
| 2399 | A phony target should not be a prerequisite of a real target file; if it
|
|---|
| 2400 | is, its commands are run every time @code{make} goes to update that
|
|---|
| 2401 | file. As long as a phony target is never a prerequisite of a real
|
|---|
| 2402 | target, the phony target commands will be executed only when the phony
|
|---|
| 2403 | target is a specified goal (@pxref{Goals, ,Arguments to Specify the
|
|---|
| 2404 | Goals}).
|
|---|
| 2405 |
|
|---|
| 2406 | Phony targets can have prerequisites. When one directory contains multiple
|
|---|
| 2407 | programs, it is most convenient to describe all of the programs in one
|
|---|
| 2408 | makefile @file{./Makefile}. Since the target remade by default will be the
|
|---|
| 2409 | first one in the makefile, it is common to make this a phony target named
|
|---|
| 2410 | @samp{all} and give it, as prerequisites, all the individual programs. For
|
|---|
| 2411 | example:
|
|---|
| 2412 |
|
|---|
| 2413 | @example
|
|---|
| 2414 | all : prog1 prog2 prog3
|
|---|
| 2415 | .PHONY : all
|
|---|
| 2416 |
|
|---|
| 2417 | prog1 : prog1.o utils.o
|
|---|
| 2418 | cc -o prog1 prog1.o utils.o
|
|---|
| 2419 |
|
|---|
| 2420 | prog2 : prog2.o
|
|---|
| 2421 | cc -o prog2 prog2.o
|
|---|
| 2422 |
|
|---|
| 2423 | prog3 : prog3.o sort.o utils.o
|
|---|
| 2424 | cc -o prog3 prog3.o sort.o utils.o
|
|---|
| 2425 | @end example
|
|---|
| 2426 |
|
|---|
| 2427 | @noindent
|
|---|
| 2428 | Now you can say just @samp{make} to remake all three programs, or specify
|
|---|
| 2429 | as arguments the ones to remake (as in @samp{make prog1 prog3}).
|
|---|
| 2430 |
|
|---|
| 2431 | When one phony target is a prerequisite of another, it serves as a subroutine
|
|---|
| 2432 | of the other. For example, here @samp{make cleanall} will delete the
|
|---|
| 2433 | object files, the difference files, and the file @file{program}:
|
|---|
| 2434 |
|
|---|
| 2435 | @example
|
|---|
| 2436 | .PHONY: cleanall cleanobj cleandiff
|
|---|
| 2437 |
|
|---|
| 2438 | cleanall : cleanobj cleandiff
|
|---|
| 2439 | rm program
|
|---|
| 2440 |
|
|---|
| 2441 | cleanobj :
|
|---|
| 2442 | rm *.o
|
|---|
| 2443 |
|
|---|
| 2444 | cleandiff :
|
|---|
| 2445 | rm *.diff
|
|---|
| 2446 | @end example
|
|---|
| 2447 |
|
|---|
| 2448 | @node Force Targets, Empty Targets, Phony Targets, Rules
|
|---|
| 2449 | @section Rules without Commands or Prerequisites
|
|---|
| 2450 | @cindex force targets
|
|---|
| 2451 | @cindex targets, force
|
|---|
| 2452 | @cindex @code{FORCE}
|
|---|
| 2453 | @cindex rule, no commands or prerequisites
|
|---|
| 2454 |
|
|---|
| 2455 | If a rule has no prerequisites or commands, and the target of the rule
|
|---|
| 2456 | is a nonexistent file, then @code{make} imagines this target to have
|
|---|
| 2457 | been updated whenever its rule is run. This implies that all targets
|
|---|
| 2458 | depending on this one will always have their commands run.
|
|---|
| 2459 |
|
|---|
| 2460 | An example will illustrate this:
|
|---|
| 2461 |
|
|---|
| 2462 | @example
|
|---|
| 2463 | @group
|
|---|
| 2464 | clean: FORCE
|
|---|
| 2465 | rm $(objects)
|
|---|
| 2466 | FORCE:
|
|---|
| 2467 | @end group
|
|---|
| 2468 | @end example
|
|---|
| 2469 |
|
|---|
| 2470 | Here the target @samp{FORCE} satisfies the special conditions, so the
|
|---|
| 2471 | target @file{clean} that depends on it is forced to run its commands.
|
|---|
| 2472 | There is nothing special about the name @samp{FORCE}, but that is one name
|
|---|
| 2473 | commonly used this way.
|
|---|
| 2474 |
|
|---|
| 2475 | As you can see, using @samp{FORCE} this way has the same results as using
|
|---|
| 2476 | @samp{.PHONY: clean}.
|
|---|
| 2477 |
|
|---|
| 2478 | Using @samp{.PHONY} is more explicit and more efficient. However,
|
|---|
| 2479 | other versions of @code{make} do not support @samp{.PHONY}; thus
|
|---|
| 2480 | @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
|
|---|
| 2481 |
|
|---|
| 2482 | @node Empty Targets, Special Targets, Force Targets, Rules
|
|---|
| 2483 | @section Empty Target Files to Record Events
|
|---|
| 2484 | @cindex empty targets
|
|---|
| 2485 | @cindex targets, empty
|
|---|
| 2486 | @cindex recording events with empty targets
|
|---|
| 2487 |
|
|---|
| 2488 | The @dfn{empty target} is a variant of the phony target; it is used to hold
|
|---|
| 2489 | commands for an action that you request explicitly from time to time.
|
|---|
| 2490 | Unlike a phony target, this target file can really exist; but the file's
|
|---|
| 2491 | contents do not matter, and usually are empty.
|
|---|
| 2492 |
|
|---|
| 2493 | The purpose of the empty target file is to record, with its
|
|---|
| 2494 | last-modification time, when the rule's commands were last executed. It
|
|---|
| 2495 | does so because one of the commands is a @code{touch} command to update the
|
|---|
| 2496 | target file.
|
|---|
| 2497 |
|
|---|
| 2498 | The empty target file should have some prerequisites (otherwise it
|
|---|
| 2499 | doesn't make sense). When you ask to remake the empty target, the
|
|---|
| 2500 | commands are executed if any prerequisite is more recent than the target;
|
|---|
| 2501 | in other words, if a prerequisite has changed since the last time you
|
|---|
| 2502 | remade the target. Here is an example:
|
|---|
| 2503 |
|
|---|
| 2504 | @example
|
|---|
| 2505 | print: foo.c bar.c
|
|---|
| 2506 | lpr -p $?
|
|---|
| 2507 | touch print
|
|---|
| 2508 | @end example
|
|---|
| 2509 | @cindex @code{print} target
|
|---|
| 2510 | @cindex @code{lpr} (shell command)
|
|---|
| 2511 | @cindex @code{touch} (shell command)
|
|---|
| 2512 |
|
|---|
| 2513 | @noindent
|
|---|
| 2514 | With this rule, @samp{make print} will execute the @code{lpr} command if
|
|---|
| 2515 | either source file has changed since the last @samp{make print}. The
|
|---|
| 2516 | automatic variable @samp{$?} is used to print only those files that have
|
|---|
| 2517 | changed (@pxref{Automatic Variables}).
|
|---|
| 2518 |
|
|---|
| 2519 | @node Special Targets, Multiple Targets, Empty Targets, Rules
|
|---|
| 2520 | @section Special Built-in Target Names
|
|---|
| 2521 | @cindex special targets
|
|---|
| 2522 | @cindex built-in special targets
|
|---|
| 2523 | @cindex targets, built-in special
|
|---|
| 2524 |
|
|---|
| 2525 | Certain names have special meanings if they appear as targets.
|
|---|
| 2526 |
|
|---|
| 2527 | @table @code
|
|---|
| 2528 | @findex .PHONY
|
|---|
| 2529 | @item .PHONY
|
|---|
| 2530 |
|
|---|
| 2531 | The prerequisites of the special target @code{.PHONY} are considered to
|
|---|
| 2532 | be phony targets. When it is time to consider such a target,
|
|---|
| 2533 | @code{make} will run its commands unconditionally, regardless of
|
|---|
| 2534 | whether a file with that name exists or what its last-modification
|
|---|
| 2535 | time is. @xref{Phony Targets, ,Phony Targets}.
|
|---|
| 2536 |
|
|---|
| 2537 | @findex .SUFFIXES
|
|---|
| 2538 | @item .SUFFIXES
|
|---|
| 2539 |
|
|---|
| 2540 | The prerequisites of the special target @code{.SUFFIXES} are the list
|
|---|
| 2541 | of suffixes to be used in checking for suffix rules.
|
|---|
| 2542 | @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
|
|---|
| 2543 |
|
|---|
| 2544 | @findex .DEFAULT
|
|---|
| 2545 | @item .DEFAULT
|
|---|
| 2546 |
|
|---|
| 2547 | The commands specified for @code{.DEFAULT} are used for any target for
|
|---|
| 2548 | which no rules are found (either explicit rules or implicit rules).
|
|---|
| 2549 | @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
|
|---|
| 2550 | file mentioned as a prerequisite, but not as a target in a rule, will have
|
|---|
| 2551 | these commands executed on its behalf. @xref{Implicit Rule Search,
|
|---|
| 2552 | ,Implicit Rule Search Algorithm}.
|
|---|
| 2553 |
|
|---|
| 2554 | @findex .PRECIOUS
|
|---|
| 2555 | @item .PRECIOUS
|
|---|
| 2556 | @cindex precious targets
|
|---|
| 2557 | @cindex preserving with @code{.PRECIOUS}
|
|---|
| 2558 |
|
|---|
| 2559 | The targets which @code{.PRECIOUS} depends on are given the following
|
|---|
| 2560 | special treatment: if @code{make} is killed or interrupted during the
|
|---|
| 2561 | execution of their commands, the target is not deleted.
|
|---|
| 2562 | @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
|
|---|
| 2563 | target is an intermediate file, it will not be deleted after it is no
|
|---|
| 2564 | longer needed, as is normally done. @xref{Chained Rules, ,Chains of
|
|---|
| 2565 | Implicit Rules}. In this latter respect it overlaps with the
|
|---|
| 2566 | @code{.SECONDARY} special target.
|
|---|
| 2567 |
|
|---|
| 2568 | You can also list the target pattern of an implicit rule (such as
|
|---|
| 2569 | @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
|
|---|
| 2570 | to preserve intermediate files created by rules whose target patterns
|
|---|
| 2571 | match that file's name.
|
|---|
| 2572 |
|
|---|
| 2573 | @findex .INTERMEDIATE
|
|---|
| 2574 | @item .INTERMEDIATE
|
|---|
| 2575 | @cindex intermediate targets, explicit
|
|---|
| 2576 |
|
|---|
| 2577 | The targets which @code{.INTERMEDIATE} depends on are treated as
|
|---|
| 2578 | intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 2579 | @code{.INTERMEDIATE} with no prerequisites has no effect.
|
|---|
| 2580 |
|
|---|
| 2581 | @findex .SECONDARY
|
|---|
| 2582 | @item .SECONDARY
|
|---|
| 2583 | @cindex secondary targets
|
|---|
| 2584 | @cindex preserving with @code{.SECONDARY}
|
|---|
| 2585 |
|
|---|
| 2586 | The targets which @code{.SECONDARY} depends on are treated as
|
|---|
| 2587 | intermediate files, except that they are never automatically deleted.
|
|---|
| 2588 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 2589 |
|
|---|
| 2590 | @code{.SECONDARY} with no prerequisites causes all targets to be treated
|
|---|
| 2591 | as secondary (i.e., no target is removed because it is considered
|
|---|
| 2592 | intermediate).
|
|---|
| 2593 |
|
|---|
| 2594 | @findex .DELETE_ON_ERROR
|
|---|
| 2595 | @item .DELETE_ON_ERROR
|
|---|
| 2596 | @cindex removing targets on failure
|
|---|
| 2597 |
|
|---|
| 2598 | If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
|
|---|
| 2599 | makefile, then @code{make} will delete the target of a rule if it has
|
|---|
| 2600 | changed and its commands exit with a nonzero exit status, just as it
|
|---|
| 2601 | does when it receives a signal. @xref{Errors, ,Errors in Commands}.
|
|---|
| 2602 |
|
|---|
| 2603 | @findex .IGNORE
|
|---|
| 2604 | @item .IGNORE
|
|---|
| 2605 |
|
|---|
| 2606 | If you specify prerequisites for @code{.IGNORE}, then @code{make} will
|
|---|
| 2607 | ignore errors in execution of the commands run for those particular
|
|---|
| 2608 | files. The commands for @code{.IGNORE} are not meaningful.
|
|---|
| 2609 |
|
|---|
| 2610 | If mentioned as a target with no prerequisites, @code{.IGNORE} says to
|
|---|
| 2611 | ignore errors in execution of commands for all files. This usage of
|
|---|
| 2612 | @samp{.IGNORE} is supported only for historical compatibility. Since
|
|---|
| 2613 | this affects every command in the makefile, it is not very useful; we
|
|---|
| 2614 | recommend you use the more selective ways to ignore errors in specific
|
|---|
| 2615 | commands. @xref{Errors, ,Errors in Commands}.
|
|---|
| 2616 |
|
|---|
| 2617 | @findex .LOW_RESOLUTION_TIME
|
|---|
| 2618 | @item .LOW_RESOLUTION_TIME
|
|---|
| 2619 |
|
|---|
| 2620 | If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
|
|---|
| 2621 | @command{make} assumes that these files are created by commands that
|
|---|
| 2622 | generate low resolution time stamps. The commands for
|
|---|
| 2623 | @code{.LOW_RESOLUTION_TIME} are not meaningful.
|
|---|
| 2624 |
|
|---|
| 2625 | The high resolution file time stamps of many modern hosts lessen the
|
|---|
| 2626 | chance of @command{make} incorrectly concluding that a file is up to
|
|---|
| 2627 | date. Unfortunately, these hosts provide no way to set a high
|
|---|
| 2628 | resolution file time stamp, so commands like @samp{cp -p} that
|
|---|
| 2629 | explicitly set a file's time stamp must discard its subsecond part. If
|
|---|
| 2630 | a file is created by such a command, you should list it as a
|
|---|
| 2631 | prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
|
|---|
| 2632 | not mistakenly conclude that the file is out of date. For example:
|
|---|
| 2633 |
|
|---|
| 2634 | @example
|
|---|
| 2635 | @group
|
|---|
| 2636 | .LOW_RESOLUTION_TIME: dst
|
|---|
| 2637 | dst: src
|
|---|
| 2638 | cp -p src dst
|
|---|
| 2639 | @end group
|
|---|
| 2640 | @end example
|
|---|
| 2641 |
|
|---|
| 2642 | Since @samp{cp -p} discards the subsecond part of @file{src}'s time
|
|---|
| 2643 | stamp, @file{dst} is typically slightly older than @file{src} even when
|
|---|
| 2644 | it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
|
|---|
| 2645 | @command{make} to consider @file{dst} to be up to date if its time stamp
|
|---|
| 2646 | is at the start of the same second that @file{src}'s time stamp is in.
|
|---|
| 2647 |
|
|---|
| 2648 | Due to a limitation of the archive format, archive member time stamps
|
|---|
| 2649 | are always low resolution. You need not list archive members as
|
|---|
| 2650 | prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
|
|---|
| 2651 | automatically.
|
|---|
| 2652 |
|
|---|
| 2653 | @findex .SILENT
|
|---|
| 2654 | @item .SILENT
|
|---|
| 2655 |
|
|---|
| 2656 | If you specify prerequisites for @code{.SILENT}, then @code{make} will
|
|---|
| 2657 | not print the commands to remake those particular files before executing
|
|---|
| 2658 | them. The commands for @code{.SILENT} are not meaningful.
|
|---|
| 2659 |
|
|---|
| 2660 | If mentioned as a target with no prerequisites, @code{.SILENT} says not
|
|---|
| 2661 | to print any commands before executing them. This usage of
|
|---|
| 2662 | @samp{.SILENT} is supported only for historical compatibility. We
|
|---|
| 2663 | recommend you use the more selective ways to silence specific commands.
|
|---|
| 2664 | @xref{Echoing, ,Command Echoing}. If you want to silence all commands
|
|---|
| 2665 | for a particular run of @code{make}, use the @samp{-s} or
|
|---|
| 2666 | @w{@samp{--silent}} option (@pxref{Options Summary}).
|
|---|
| 2667 |
|
|---|
| 2668 | @findex .EXPORT_ALL_VARIABLES
|
|---|
| 2669 | @item .EXPORT_ALL_VARIABLES
|
|---|
| 2670 |
|
|---|
| 2671 | Simply by being mentioned as a target, this tells @code{make} to
|
|---|
| 2672 | export all variables to child processes by default.
|
|---|
| 2673 | @xref{Variables/Recursion, ,Communicating Variables to a
|
|---|
| 2674 | Sub-@code{make}}.
|
|---|
| 2675 |
|
|---|
| 2676 | @findex .NOTPARALLEL
|
|---|
| 2677 | @item .NOTPARALLEL
|
|---|
| 2678 | @cindex parallel execution, overriding
|
|---|
| 2679 |
|
|---|
| 2680 | If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
|
|---|
| 2681 | @code{make} will be run serially, even if the @samp{-j} option is
|
|---|
| 2682 | given. Any recursively invoked @code{make} command will still be run in
|
|---|
| 2683 | parallel (unless its makefile contains this target). Any prerequisites
|
|---|
| 2684 | on this target are ignored.
|
|---|
| 2685 | @end table
|
|---|
| 2686 |
|
|---|
| 2687 | Any defined implicit rule suffix also counts as a special target if it
|
|---|
| 2688 | appears as a target, and so does the concatenation of two suffixes, such
|
|---|
| 2689 | as @samp{.c.o}. These targets are suffix rules, an obsolete way of
|
|---|
| 2690 | defining implicit rules (but a way still widely used). In principle, any
|
|---|
| 2691 | target name could be special in this way if you break it in two and add
|
|---|
| 2692 | both pieces to the suffix list. In practice, suffixes normally begin with
|
|---|
| 2693 | @samp{.}, so these special target names also begin with @samp{.}.
|
|---|
| 2694 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 2695 |
|
|---|
| 2696 | @node Multiple Targets, Multiple Rules, Special Targets, Rules
|
|---|
| 2697 | @section Multiple Targets in a Rule
|
|---|
| 2698 | @cindex multiple targets
|
|---|
| 2699 | @cindex several targets in a rule
|
|---|
| 2700 | @cindex targets, multiple
|
|---|
| 2701 | @cindex rule, with multiple targets
|
|---|
| 2702 |
|
|---|
| 2703 | A rule with multiple targets is equivalent to writing many rules, each with
|
|---|
| 2704 | one target, and all identical aside from that. The same commands apply to
|
|---|
| 2705 | all the targets, but their effects may vary because you can substitute the
|
|---|
| 2706 | actual target name into the command using @samp{$@@}. The rule contributes
|
|---|
| 2707 | the same prerequisites to all the targets also.
|
|---|
| 2708 |
|
|---|
| 2709 | This is useful in two cases.
|
|---|
| 2710 |
|
|---|
| 2711 | @itemize @bullet
|
|---|
| 2712 | @item
|
|---|
| 2713 | You want just prerequisites, no commands. For example:
|
|---|
| 2714 |
|
|---|
| 2715 | @example
|
|---|
| 2716 | kbd.o command.o files.o: command.h
|
|---|
| 2717 | @end example
|
|---|
| 2718 |
|
|---|
| 2719 | @noindent
|
|---|
| 2720 | gives an additional prerequisite to each of the three object files
|
|---|
| 2721 | mentioned.
|
|---|
| 2722 |
|
|---|
| 2723 | @item
|
|---|
| 2724 | Similar commands work for all the targets. The commands do not need
|
|---|
| 2725 | to be absolutely identical, since the automatic variable @samp{$@@}
|
|---|
| 2726 | can be used to substitute the particular target to be remade into the
|
|---|
| 2727 | commands (@pxref{Automatic Variables}). For example:
|
|---|
| 2728 |
|
|---|
| 2729 | @example
|
|---|
| 2730 | @group
|
|---|
| 2731 | bigoutput littleoutput : text.g
|
|---|
| 2732 | generate text.g -$(subst output,,$@@) > $@@
|
|---|
| 2733 | @end group
|
|---|
| 2734 | @end example
|
|---|
| 2735 | @findex subst
|
|---|
| 2736 |
|
|---|
| 2737 | @noindent
|
|---|
| 2738 | is equivalent to
|
|---|
| 2739 |
|
|---|
| 2740 | @example
|
|---|
| 2741 | bigoutput : text.g
|
|---|
| 2742 | generate text.g -big > bigoutput
|
|---|
| 2743 | littleoutput : text.g
|
|---|
| 2744 | generate text.g -little > littleoutput
|
|---|
| 2745 | @end example
|
|---|
| 2746 |
|
|---|
| 2747 | @noindent
|
|---|
| 2748 | Here we assume the hypothetical program @code{generate} makes two
|
|---|
| 2749 | types of output, one if given @samp{-big} and one if given
|
|---|
| 2750 | @samp{-little}.
|
|---|
| 2751 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
|---|
| 2752 | for an explanation of the @code{subst} function.
|
|---|
| 2753 | @end itemize
|
|---|
| 2754 |
|
|---|
| 2755 | Suppose you would like to vary the prerequisites according to the target,
|
|---|
| 2756 | much as the variable @samp{$@@} allows you to vary the commands.
|
|---|
| 2757 | You cannot do this with multiple targets in an ordinary rule, but you can
|
|---|
| 2758 | do it with a @dfn{static pattern rule}.
|
|---|
| 2759 | @xref{Static Pattern, ,Static Pattern Rules}.
|
|---|
| 2760 |
|
|---|
| 2761 | @node Multiple Rules, Static Pattern, Multiple Targets, Rules
|
|---|
| 2762 | @section Multiple Rules for One Target
|
|---|
| 2763 | @cindex multiple rules for one target
|
|---|
| 2764 | @cindex several rules for one target
|
|---|
| 2765 | @cindex rule, multiple for one target
|
|---|
| 2766 | @cindex target, multiple rules for one
|
|---|
| 2767 |
|
|---|
| 2768 | One file can be the target of several rules. All the prerequisites
|
|---|
| 2769 | mentioned in all the rules are merged into one list of prerequisites for
|
|---|
| 2770 | the target. If the target is older than any prerequisite from any rule,
|
|---|
| 2771 | the commands are executed.
|
|---|
| 2772 |
|
|---|
| 2773 | There can only be one set of commands to be executed for a file. If
|
|---|
| 2774 | more than one rule gives commands for the same file, @code{make} uses
|
|---|
| 2775 | the last set given and prints an error message. (As a special case,
|
|---|
| 2776 | if the file's name begins with a dot, no error message is printed.
|
|---|
| 2777 | This odd behavior is only for compatibility with other implementations
|
|---|
| 2778 | of @code{make}... you should avoid using it). Occasionally it is
|
|---|
| 2779 | useful to have the same target invoke multiple commands which are
|
|---|
| 2780 | defined in different parts of your makefile; you can use
|
|---|
| 2781 | @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
|
|---|
| 2782 |
|
|---|
| 2783 | An extra rule with just prerequisites can be used to give a few extra
|
|---|
| 2784 | prerequisites to many files at once. For example, makefiles often
|
|---|
| 2785 | have a variable, such as @code{objects}, containing a list of all the
|
|---|
| 2786 | compiler output files in the system being made. An easy way to say
|
|---|
| 2787 | that all of them must be recompiled if @file{config.h} changes is to
|
|---|
| 2788 | write the following:
|
|---|
| 2789 |
|
|---|
| 2790 | @example
|
|---|
| 2791 | objects = foo.o bar.o
|
|---|
| 2792 | foo.o : defs.h
|
|---|
| 2793 | bar.o : defs.h test.h
|
|---|
| 2794 | $(objects) : config.h
|
|---|
| 2795 | @end example
|
|---|
| 2796 |
|
|---|
| 2797 | This could be inserted or taken out without changing the rules that really
|
|---|
| 2798 | specify how to make the object files, making it a convenient form to use if
|
|---|
| 2799 | you wish to add the additional prerequisite intermittently.
|
|---|
| 2800 |
|
|---|
| 2801 | Another wrinkle is that the additional prerequisites could be specified with
|
|---|
| 2802 | a variable that you set with a command argument to @code{make}
|
|---|
| 2803 | (@pxref{Overriding, ,Overriding Variables}). For example,
|
|---|
| 2804 |
|
|---|
| 2805 | @example
|
|---|
| 2806 | @group
|
|---|
| 2807 | extradeps=
|
|---|
| 2808 | $(objects) : $(extradeps)
|
|---|
| 2809 | @end group
|
|---|
| 2810 | @end example
|
|---|
| 2811 |
|
|---|
| 2812 | @noindent
|
|---|
| 2813 | means that the command @samp{make extradeps=foo.h} will consider
|
|---|
| 2814 | @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
|
|---|
| 2815 | will not.
|
|---|
| 2816 |
|
|---|
| 2817 | If none of the explicit rules for a target has commands, then @code{make}
|
|---|
| 2818 | searches for an applicable implicit rule to find some commands
|
|---|
| 2819 | @pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 2820 |
|
|---|
| 2821 | @node Static Pattern, Double-Colon, Multiple Rules, Rules
|
|---|
| 2822 | @section Static Pattern Rules
|
|---|
| 2823 | @cindex static pattern rule
|
|---|
| 2824 | @cindex rule, static pattern
|
|---|
| 2825 | @cindex pattern rules, static (not implicit)
|
|---|
| 2826 | @cindex varying prerequisites
|
|---|
| 2827 | @cindex prerequisites, varying (static pattern)
|
|---|
| 2828 |
|
|---|
| 2829 | @dfn{Static pattern rules} are rules which specify multiple targets and
|
|---|
| 2830 | construct the prerequisite names for each target based on the target name.
|
|---|
| 2831 | They are more general than ordinary rules with multiple targets because the
|
|---|
| 2832 | targets do not have to have identical prerequisites. Their prerequisites must
|
|---|
| 2833 | be @emph{analogous}, but not necessarily @emph{identical}.
|
|---|
| 2834 |
|
|---|
| 2835 | @menu
|
|---|
| 2836 | * Static Usage:: The syntax of static pattern rules.
|
|---|
| 2837 | * Static versus Implicit:: When are they better than implicit rules?
|
|---|
| 2838 | @end menu
|
|---|
| 2839 |
|
|---|
| 2840 | @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
|
|---|
| 2841 | @subsection Syntax of Static Pattern Rules
|
|---|
| 2842 | @cindex static pattern rule, syntax of
|
|---|
| 2843 | @cindex pattern rules, static, syntax of
|
|---|
| 2844 |
|
|---|
| 2845 | Here is the syntax of a static pattern rule:
|
|---|
| 2846 |
|
|---|
| 2847 | @example
|
|---|
| 2848 | @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
|
|---|
| 2849 | @var{commands}
|
|---|
| 2850 | @dots{}
|
|---|
| 2851 | @end example
|
|---|
| 2852 |
|
|---|
| 2853 | @noindent
|
|---|
| 2854 | The @var{targets} list specifies the targets that the rule applies to.
|
|---|
| 2855 | The targets can contain wildcard characters, just like the targets of
|
|---|
| 2856 | ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
|
|---|
| 2857 | Names}).
|
|---|
| 2858 |
|
|---|
| 2859 | @cindex target pattern, static (not implicit)
|
|---|
| 2860 | @cindex stem
|
|---|
| 2861 | The @var{target-pattern} and @var{prereq-patterns} say how to compute the
|
|---|
| 2862 | prerequisites of each target. Each target is matched against the
|
|---|
| 2863 | @var{target-pattern} to extract a part of the target name, called the
|
|---|
| 2864 | @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
|
|---|
| 2865 | to make the prerequisite names (one from each @var{prereq-pattern}).
|
|---|
| 2866 |
|
|---|
| 2867 | Each pattern normally contains the character @samp{%} just once. When the
|
|---|
| 2868 | @var{target-pattern} matches a target, the @samp{%} can match any part of
|
|---|
| 2869 | the target name; this part is called the @dfn{stem}. The rest of the
|
|---|
| 2870 | pattern must match exactly. For example, the target @file{foo.o} matches
|
|---|
| 2871 | the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
|
|---|
| 2872 | @file{foo.c} and @file{foo.out} do not match that pattern.@refill
|
|---|
| 2873 |
|
|---|
| 2874 | @cindex prerequisite pattern, static (not implicit)
|
|---|
| 2875 | The prerequisite names for each target are made by substituting the stem
|
|---|
| 2876 | for the @samp{%} in each prerequisite pattern. For example, if one
|
|---|
| 2877 | prerequisite pattern is @file{%.c}, then substitution of the stem
|
|---|
| 2878 | @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
|
|---|
| 2879 | to write a prerequisite pattern that does not contain @samp{%}; then this
|
|---|
| 2880 | prerequisite is the same for all targets.
|
|---|
| 2881 |
|
|---|
| 2882 | @cindex @code{%}, quoting in static pattern
|
|---|
| 2883 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 2884 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 2885 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 2886 | @cindex quoting @code{%}, in static pattern
|
|---|
| 2887 | @samp{%} characters in pattern rules can be quoted with preceding
|
|---|
| 2888 | backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
|
|---|
| 2889 | characters can be quoted with more backslashes. Backslashes that quote
|
|---|
| 2890 | @samp{%} characters or other backslashes are removed from the pattern
|
|---|
| 2891 | before it is compared to file names or has a stem substituted into it.
|
|---|
| 2892 | Backslashes that are not in danger of quoting @samp{%} characters go
|
|---|
| 2893 | unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
|
|---|
| 2894 | @samp{the%weird\} preceding the operative @samp{%} character, and
|
|---|
| 2895 | @samp{pattern\\} following it. The final two backslashes are left alone
|
|---|
| 2896 | because they cannot affect any @samp{%} character.@refill
|
|---|
| 2897 |
|
|---|
| 2898 | Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
|
|---|
| 2899 | from the corresponding @file{.c} file:
|
|---|
| 2900 |
|
|---|
| 2901 | @example
|
|---|
| 2902 | @group
|
|---|
| 2903 | objects = foo.o bar.o
|
|---|
| 2904 |
|
|---|
| 2905 | all: $(objects)
|
|---|
| 2906 |
|
|---|
| 2907 | $(objects): %.o: %.c
|
|---|
| 2908 | $(CC) -c $(CFLAGS) $< -o $@@
|
|---|
| 2909 | @end group
|
|---|
| 2910 | @end example
|
|---|
| 2911 |
|
|---|
| 2912 | @noindent
|
|---|
| 2913 | Here @samp{$<} is the automatic variable that holds the name of the
|
|---|
| 2914 | prerequisite and @samp{$@@} is the automatic variable that holds the name
|
|---|
| 2915 | of the target; see @ref{Automatic Variables}.
|
|---|
| 2916 |
|
|---|
| 2917 | Each target specified must match the target pattern; a warning is issued
|
|---|
| 2918 | for each target that does not. If you have a list of files, only some of
|
|---|
| 2919 | which will match the pattern, you can use the @code{filter} function to
|
|---|
| 2920 | remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
|---|
| 2921 |
|
|---|
| 2922 | @example
|
|---|
| 2923 | files = foo.elc bar.o lose.o
|
|---|
| 2924 |
|
|---|
| 2925 | $(filter %.o,$(files)): %.o: %.c
|
|---|
| 2926 | $(CC) -c $(CFLAGS) $< -o $@@
|
|---|
| 2927 | $(filter %.elc,$(files)): %.elc: %.el
|
|---|
| 2928 | emacs -f batch-byte-compile $<
|
|---|
| 2929 | @end example
|
|---|
| 2930 |
|
|---|
| 2931 | @noindent
|
|---|
| 2932 | In this example the result of @samp{$(filter %.o,$(files))} is
|
|---|
| 2933 | @file{bar.o lose.o}, and the first static pattern rule causes each of
|
|---|
| 2934 | these object files to be updated by compiling the corresponding C source
|
|---|
| 2935 | file. The result of @w{@samp{$(filter %.elc,$(files))}} is
|
|---|
| 2936 | @file{foo.elc}, so that file is made from @file{foo.el}.@refill
|
|---|
| 2937 |
|
|---|
| 2938 | Another example shows how to use @code{$*} in static pattern rules:
|
|---|
| 2939 | @vindex $*@r{, and static pattern}
|
|---|
| 2940 |
|
|---|
| 2941 | @example
|
|---|
| 2942 | @group
|
|---|
| 2943 | bigoutput littleoutput : %output : text.g
|
|---|
| 2944 | generate text.g -$* > $@@
|
|---|
| 2945 | @end group
|
|---|
| 2946 | @end example
|
|---|
| 2947 |
|
|---|
| 2948 | @noindent
|
|---|
| 2949 | When the @code{generate} command is run, @code{$*} will expand to the
|
|---|
| 2950 | stem, either @samp{big} or @samp{little}.
|
|---|
| 2951 |
|
|---|
| 2952 | @node Static versus Implicit, , Static Usage, Static Pattern
|
|---|
| 2953 | @subsection Static Pattern Rules versus Implicit Rules
|
|---|
| 2954 | @cindex rule, static pattern versus implicit
|
|---|
| 2955 | @cindex static pattern rule, versus implicit
|
|---|
| 2956 |
|
|---|
| 2957 | A static pattern rule has much in common with an implicit rule defined as a
|
|---|
| 2958 | pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
|
|---|
| 2959 | Both have a pattern for the target and patterns for constructing the
|
|---|
| 2960 | names of prerequisites. The difference is in how @code{make} decides
|
|---|
| 2961 | @emph{when} the rule applies.
|
|---|
| 2962 |
|
|---|
| 2963 | An implicit rule @emph{can} apply to any target that matches its pattern,
|
|---|
| 2964 | but it @emph{does} apply only when the target has no commands otherwise
|
|---|
| 2965 | specified, and only when the prerequisites can be found. If more than one
|
|---|
| 2966 | implicit rule appears applicable, only one applies; the choice depends on
|
|---|
| 2967 | the order of rules.
|
|---|
| 2968 |
|
|---|
| 2969 | By contrast, a static pattern rule applies to the precise list of targets
|
|---|
| 2970 | that you specify in the rule. It cannot apply to any other target and it
|
|---|
| 2971 | invariably does apply to each of the targets specified. If two conflicting
|
|---|
| 2972 | rules apply, and both have commands, that's an error.
|
|---|
| 2973 |
|
|---|
| 2974 | The static pattern rule can be better than an implicit rule for these
|
|---|
| 2975 | reasons:
|
|---|
| 2976 |
|
|---|
| 2977 | @itemize @bullet
|
|---|
| 2978 | @item
|
|---|
| 2979 | You may wish to override the usual implicit rule for a few
|
|---|
| 2980 | files whose names cannot be categorized syntactically but
|
|---|
| 2981 | can be given in an explicit list.
|
|---|
| 2982 |
|
|---|
| 2983 | @item
|
|---|
| 2984 | If you cannot be sure of the precise contents of the directories
|
|---|
| 2985 | you are using, you may not be sure which other irrelevant files
|
|---|
| 2986 | might lead @code{make} to use the wrong implicit rule. The choice
|
|---|
| 2987 | might depend on the order in which the implicit rule search is done.
|
|---|
| 2988 | With static pattern rules, there is no uncertainty: each rule applies
|
|---|
| 2989 | to precisely the targets specified.
|
|---|
| 2990 | @end itemize
|
|---|
| 2991 |
|
|---|
| 2992 | @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
|
|---|
| 2993 | @section Double-Colon Rules
|
|---|
| 2994 | @cindex double-colon rules
|
|---|
| 2995 | @cindex rule, double-colon (@code{::})
|
|---|
| 2996 | @cindex multiple rules for one target (@code{::})
|
|---|
| 2997 | @cindex @code{::} rules (double-colon)
|
|---|
| 2998 |
|
|---|
| 2999 | @dfn{Double-colon} rules are rules written with @samp{::} instead of
|
|---|
| 3000 | @samp{:} after the target names. They are handled differently from
|
|---|
| 3001 | ordinary rules when the same target appears in more than one rule.
|
|---|
| 3002 |
|
|---|
| 3003 | When a target appears in multiple rules, all the rules must be the same
|
|---|
| 3004 | type: all ordinary, or all double-colon. If they are double-colon, each
|
|---|
| 3005 | of them is independent of the others. Each double-colon rule's commands
|
|---|
| 3006 | are executed if the target is older than any prerequisites of that rule.
|
|---|
| 3007 | If there are no prerequisites for that rule, its commands are always
|
|---|
| 3008 | executed (even if the target already exists). This can result in
|
|---|
| 3009 | executing none, any, or all of the double-colon rules.
|
|---|
| 3010 |
|
|---|
| 3011 | Double-colon rules with the same target are in fact completely separate
|
|---|
| 3012 | from one another. Each double-colon rule is processed individually, just
|
|---|
| 3013 | as rules with different targets are processed.
|
|---|
| 3014 |
|
|---|
| 3015 | The double-colon rules for a target are executed in the order they appear
|
|---|
| 3016 | in the makefile. However, the cases where double-colon rules really make
|
|---|
| 3017 | sense are those where the order of executing the commands would not matter.
|
|---|
| 3018 |
|
|---|
| 3019 | Double-colon rules are somewhat obscure and not often very useful; they
|
|---|
| 3020 | provide a mechanism for cases in which the method used to update a target
|
|---|
| 3021 | differs depending on which prerequisite files caused the update, and such
|
|---|
| 3022 | cases are rare.
|
|---|
| 3023 |
|
|---|
| 3024 | Each double-colon rule should specify commands; if it does not, an
|
|---|
| 3025 | implicit rule will be used if one applies.
|
|---|
| 3026 | @xref{Implicit Rules, ,Using Implicit Rules}.
|
|---|
| 3027 |
|
|---|
| 3028 | @node Automatic Prerequisites, , Double-Colon, Rules
|
|---|
| 3029 | @section Generating Prerequisites Automatically
|
|---|
| 3030 | @cindex prerequisites, automatic generation
|
|---|
| 3031 | @cindex automatic generation of prerequisites
|
|---|
| 3032 | @cindex generating prerequisites automatically
|
|---|
| 3033 |
|
|---|
| 3034 | In the makefile for a program, many of the rules you need to write often
|
|---|
| 3035 | say only that some object file depends on some header
|
|---|
| 3036 | file. For example, if @file{main.c} uses @file{defs.h} via an
|
|---|
| 3037 | @code{#include}, you would write:
|
|---|
| 3038 |
|
|---|
| 3039 | @example
|
|---|
| 3040 | main.o: defs.h
|
|---|
| 3041 | @end example
|
|---|
| 3042 |
|
|---|
| 3043 | @noindent
|
|---|
| 3044 | You need this rule so that @code{make} knows that it must remake
|
|---|
| 3045 | @file{main.o} whenever @file{defs.h} changes. You can see that for a
|
|---|
| 3046 | large program you would have to write dozens of such rules in your
|
|---|
| 3047 | makefile. And, you must always be very careful to update the makefile
|
|---|
| 3048 | every time you add or remove an @code{#include}.
|
|---|
| 3049 | @cindex @code{#include}
|
|---|
| 3050 |
|
|---|
| 3051 | @cindex @code{-M} (to compiler)
|
|---|
| 3052 | To avoid this hassle, most modern C compilers can write these rules for
|
|---|
| 3053 | you, by looking at the @code{#include} lines in the source files.
|
|---|
| 3054 | Usually this is done with the @samp{-M} option to the compiler.
|
|---|
| 3055 | For example, the command:
|
|---|
| 3056 |
|
|---|
| 3057 | @example
|
|---|
| 3058 | cc -M main.c
|
|---|
| 3059 | @end example
|
|---|
| 3060 |
|
|---|
| 3061 | @noindent
|
|---|
| 3062 | generates the output:
|
|---|
| 3063 |
|
|---|
| 3064 | @example
|
|---|
| 3065 | main.o : main.c defs.h
|
|---|
| 3066 | @end example
|
|---|
| 3067 |
|
|---|
| 3068 | @noindent
|
|---|
| 3069 | Thus you no longer have to write all those rules yourself.
|
|---|
| 3070 | The compiler will do it for you.
|
|---|
| 3071 |
|
|---|
| 3072 | Note that such a prerequisite constitutes mentioning @file{main.o} in a
|
|---|
| 3073 | makefile, so it can never be considered an intermediate file by implicit
|
|---|
| 3074 | rule search. This means that @code{make} won't ever remove the file
|
|---|
| 3075 | after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 3076 |
|
|---|
| 3077 | @cindex @code{make depend}
|
|---|
| 3078 | With old @code{make} programs, it was traditional practice to use this
|
|---|
| 3079 | compiler feature to generate prerequisites on demand with a command like
|
|---|
| 3080 | @samp{make depend}. That command would create a file @file{depend}
|
|---|
| 3081 | containing all the automatically-generated prerequisites; then the
|
|---|
| 3082 | makefile could use @code{include} to read them in (@pxref{Include}).
|
|---|
| 3083 |
|
|---|
| 3084 | In GNU @code{make}, the feature of remaking makefiles makes this
|
|---|
| 3085 | practice obsolete---you need never tell @code{make} explicitly to
|
|---|
| 3086 | regenerate the prerequisites, because it always regenerates any makefile
|
|---|
| 3087 | that is out of date. @xref{Remaking Makefiles}.
|
|---|
| 3088 |
|
|---|
| 3089 | The practice we recommend for automatic prerequisite generation is to have
|
|---|
| 3090 | one makefile corresponding to each source file. For each source file
|
|---|
| 3091 | @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
|
|---|
| 3092 | what files the object file @file{@var{name}.o} depends on. That way
|
|---|
| 3093 | only the source files that have changed need to be rescanned to produce
|
|---|
| 3094 | the new prerequisites.
|
|---|
| 3095 |
|
|---|
| 3096 | Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
|
|---|
| 3097 | called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
|
|---|
| 3098 |
|
|---|
| 3099 | @smallexample
|
|---|
| 3100 | @group
|
|---|
| 3101 | %.d: %.c
|
|---|
| 3102 | @set -e; rm -f $@@; \
|
|---|
| 3103 | $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
|
|---|
| 3104 | sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
|
|---|
| 3105 | rm -f $@@.$$$$
|
|---|
| 3106 | @end group
|
|---|
| 3107 | @end smallexample
|
|---|
| 3108 |
|
|---|
| 3109 | @noindent
|
|---|
| 3110 | @xref{Pattern Rules}, for information on defining pattern rules. The
|
|---|
| 3111 | @samp{-e} flag to the shell causes it to exit immediately if the
|
|---|
| 3112 | @code{$(CC)} command (or any other command) fails (exits with a
|
|---|
| 3113 | nonzero status).
|
|---|
| 3114 | @cindex @code{-e} (shell flag)
|
|---|
| 3115 |
|
|---|
| 3116 | @cindex @code{-MM} (to GNU compiler)
|
|---|
| 3117 | With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
|
|---|
| 3118 | of @samp{-M}. This omits prerequisites on system header files.
|
|---|
| 3119 | @xref{Preprocessor Options, , Options Controlling the Preprocessor,
|
|---|
| 3120 | gcc.info, Using GNU CC}, for details.
|
|---|
| 3121 |
|
|---|
| 3122 | @cindex @code{sed} (shell command)
|
|---|
| 3123 | The purpose of the @code{sed} command is to translate (for example):
|
|---|
| 3124 |
|
|---|
| 3125 | @example
|
|---|
| 3126 | main.o : main.c defs.h
|
|---|
| 3127 | @end example
|
|---|
| 3128 |
|
|---|
| 3129 | @noindent
|
|---|
| 3130 | into:
|
|---|
| 3131 |
|
|---|
| 3132 | @example
|
|---|
| 3133 | main.o main.d : main.c defs.h
|
|---|
| 3134 | @end example
|
|---|
| 3135 |
|
|---|
| 3136 | @noindent
|
|---|
| 3137 | @cindex @code{.d}
|
|---|
| 3138 | This makes each @samp{.d} file depend on all the source and header files
|
|---|
| 3139 | that the corresponding @samp{.o} file depends on. @code{make} then
|
|---|
| 3140 | knows it must regenerate the prerequisites whenever any of the source or
|
|---|
| 3141 | header files changes.
|
|---|
| 3142 |
|
|---|
| 3143 | Once you've defined the rule to remake the @samp{.d} files,
|
|---|
| 3144 | you then use the @code{include} directive to read them all in.
|
|---|
| 3145 | @xref{Include}. For example:
|
|---|
| 3146 |
|
|---|
| 3147 | @example
|
|---|
| 3148 | @group
|
|---|
| 3149 | sources = foo.c bar.c
|
|---|
| 3150 |
|
|---|
| 3151 | include $(sources:.c=.d)
|
|---|
| 3152 | @end group
|
|---|
| 3153 | @end example
|
|---|
| 3154 |
|
|---|
| 3155 | @noindent
|
|---|
| 3156 | (This example uses a substitution variable reference to translate the
|
|---|
| 3157 | list of source files @samp{foo.c bar.c} into a list of prerequisite
|
|---|
| 3158 | makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
|
|---|
| 3159 | information on substitution references.) Since the @samp{.d} files are
|
|---|
| 3160 | makefiles like any others, @code{make} will remake them as necessary
|
|---|
| 3161 | with no further work from you. @xref{Remaking Makefiles}.
|
|---|
| 3162 |
|
|---|
| 3163 | Note that the @samp{.d} files contain target definitions; you should
|
|---|
| 3164 | be sure to place the @code{include} directive @emph{after} the first,
|
|---|
| 3165 | default target in your makefiles or run the risk of having a random
|
|---|
| 3166 | object file become the default target.
|
|---|
| 3167 | @xref{How Make Works}.
|
|---|
| 3168 |
|
|---|
| 3169 | @node Commands, Using Variables, Rules, Top
|
|---|
| 3170 | @chapter Writing the Commands in Rules
|
|---|
| 3171 | @cindex commands, how to write
|
|---|
| 3172 | @cindex rule commands
|
|---|
| 3173 | @cindex writing rule commands
|
|---|
| 3174 |
|
|---|
| 3175 | The commands of a rule consist of shell command lines to be executed one
|
|---|
| 3176 | by one. Each command line must start with a tab, except that the first
|
|---|
| 3177 | command line may be attached to the target-and-prerequisites line with a
|
|---|
| 3178 | semicolon in between. Blank lines and lines of just comments may appear
|
|---|
| 3179 | among the command lines; they are ignored. (But beware, an apparently
|
|---|
| 3180 | ``blank'' line that begins with a tab is @emph{not} blank! It is an
|
|---|
| 3181 | empty command; @pxref{Empty Commands}.)
|
|---|
| 3182 |
|
|---|
| 3183 | Users use many different shell programs, but commands in makefiles are
|
|---|
| 3184 | always interpreted by @file{/bin/sh} unless the makefile specifies
|
|---|
| 3185 | otherwise. @xref{Execution, ,Command Execution}.
|
|---|
| 3186 |
|
|---|
| 3187 | @cindex comments, in commands
|
|---|
| 3188 | @cindex commands, comments in
|
|---|
| 3189 | @cindex @code{#} (comments), in commands
|
|---|
| 3190 | The shell that is in use determines whether comments can be written on
|
|---|
| 3191 | command lines, and what syntax they use. When the shell is
|
|---|
| 3192 | @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
|
|---|
| 3193 | the line. The @samp{#} does not have to be at the beginning of a line.
|
|---|
| 3194 | Text on a line before a @samp{#} is not part of the comment.
|
|---|
| 3195 |
|
|---|
| 3196 | @menu
|
|---|
| 3197 | * Echoing:: How to control when commands are echoed.
|
|---|
| 3198 | * Execution:: How commands are executed.
|
|---|
| 3199 | * Parallel:: How commands can be executed in parallel.
|
|---|
| 3200 | * Errors:: What happens after a command execution error.
|
|---|
| 3201 | * Interrupts:: What happens when a command is interrupted.
|
|---|
| 3202 | * Recursion:: Invoking @code{make} from makefiles.
|
|---|
| 3203 | * Sequences:: Defining canned sequences of commands.
|
|---|
| 3204 | * Empty Commands:: Defining useful, do-nothing commands.
|
|---|
| 3205 | @end menu
|
|---|
| 3206 |
|
|---|
| 3207 | @node Echoing, Execution, Commands, Commands
|
|---|
| 3208 | @section Command Echoing
|
|---|
| 3209 | @cindex echoing of commands
|
|---|
| 3210 | @cindex silent operation
|
|---|
| 3211 | @cindex @code{@@} (in commands)
|
|---|
| 3212 | @cindex commands, echoing
|
|---|
| 3213 | @cindex printing of commands
|
|---|
| 3214 |
|
|---|
| 3215 | Normally @code{make} prints each command line before it is executed.
|
|---|
| 3216 | We call this @dfn{echoing} because it gives the appearance that you
|
|---|
| 3217 | are typing the commands yourself.
|
|---|
| 3218 |
|
|---|
| 3219 | When a line starts with @samp{@@}, the echoing of that line is suppressed.
|
|---|
| 3220 | The @samp{@@} is discarded before the command is passed to the shell.
|
|---|
| 3221 | Typically you would use this for a command whose only effect is to print
|
|---|
| 3222 | something, such as an @code{echo} command to indicate progress through
|
|---|
| 3223 | the makefile:
|
|---|
| 3224 |
|
|---|
| 3225 | @example
|
|---|
| 3226 | @@echo About to make distribution files
|
|---|
| 3227 | @end example
|
|---|
| 3228 |
|
|---|
| 3229 | @cindex @code{-n}
|
|---|
| 3230 | @cindex @code{--just-print}
|
|---|
| 3231 | @cindex @code{--dry-run}
|
|---|
| 3232 | @cindex @code{--recon}
|
|---|
| 3233 | When @code{make} is given the flag @samp{-n} or @samp{--just-print}
|
|---|
| 3234 | it only echoes commands, it won't execute them. @xref{Options Summary,
|
|---|
| 3235 | ,Summary of Options}. In this case and only this case, even the
|
|---|
| 3236 | commands starting with @samp{@@} are printed. This flag is useful for
|
|---|
| 3237 | finding out which commands @code{make} thinks are necessary without
|
|---|
| 3238 | actually doing them.
|
|---|
| 3239 |
|
|---|
| 3240 | @cindex @code{-s}
|
|---|
| 3241 | @cindex @code{--silent}
|
|---|
| 3242 | @cindex @code{--quiet}
|
|---|
| 3243 | @findex .SILENT
|
|---|
| 3244 | The @samp{-s} or @samp{--silent}
|
|---|
| 3245 | flag to @code{make} prevents all echoing, as if all commands
|
|---|
| 3246 | started with @samp{@@}. A rule in the makefile for the special target
|
|---|
| 3247 | @code{.SILENT} without prerequisites has the same effect
|
|---|
| 3248 | (@pxref{Special Targets, ,Special Built-in Target Names}).
|
|---|
| 3249 | @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
|
|---|
| 3250 |
|
|---|
| 3251 | @node Execution, Parallel, Echoing, Commands
|
|---|
| 3252 | @section Command Execution
|
|---|
| 3253 | @cindex commands, execution
|
|---|
| 3254 | @cindex execution, of commands
|
|---|
| 3255 | @cindex shell command, execution
|
|---|
| 3256 | @vindex SHELL @r{(command execution)}
|
|---|
| 3257 |
|
|---|
| 3258 | When it is time to execute commands to update a target, they are executed
|
|---|
| 3259 | by making a new subshell for each line. (In practice, @code{make} may
|
|---|
| 3260 | take shortcuts that do not affect the results.)
|
|---|
| 3261 |
|
|---|
| 3262 | @cindex @code{cd} (shell command)
|
|---|
| 3263 | @strong{Please note:} this implies that shell commands such as @code{cd}
|
|---|
| 3264 | that set variables local to each process will not affect the following
|
|---|
| 3265 | command lines. @footnote{On MS-DOS, the value of current working
|
|---|
| 3266 | directory is @strong{global}, so changing it @emph{will} affect the
|
|---|
| 3267 | following command lines on those systems.} If you want to use @code{cd}
|
|---|
| 3268 | to affect the next command, put the two on a single line with a
|
|---|
| 3269 | semicolon between them. Then @code{make} will consider them a single
|
|---|
| 3270 | command and pass them, together, to a shell which will execute them in
|
|---|
| 3271 | sequence. For example:
|
|---|
| 3272 |
|
|---|
| 3273 | @example
|
|---|
| 3274 | foo : bar/lose
|
|---|
| 3275 | cd bar; gobble lose > ../foo
|
|---|
| 3276 | @end example
|
|---|
| 3277 |
|
|---|
| 3278 | @cindex commands, backslash (@code{\}) in
|
|---|
| 3279 | @cindex commands, quoting newlines in
|
|---|
| 3280 | @cindex backslash (@code{\}), in commands
|
|---|
| 3281 | @cindex @code{\} (backslash), in commands
|
|---|
| 3282 | @cindex quoting newline, in commands
|
|---|
| 3283 | @cindex newline, quoting, in commands
|
|---|
| 3284 | If you would like to split a single shell command into multiple lines of
|
|---|
| 3285 | text, you must use a backslash at the end of all but the last subline.
|
|---|
| 3286 | Such a sequence of lines is combined into a single line, by deleting the
|
|---|
| 3287 | backslash-newline sequences, before passing it to the shell. Thus, the
|
|---|
| 3288 | following is equivalent to the preceding example:
|
|---|
| 3289 |
|
|---|
| 3290 | @example
|
|---|
| 3291 | @group
|
|---|
| 3292 | foo : bar/lose
|
|---|
| 3293 | cd bar; \
|
|---|
| 3294 | gobble lose > ../foo
|
|---|
| 3295 | @end group
|
|---|
| 3296 | @end example
|
|---|
| 3297 |
|
|---|
| 3298 | @vindex SHELL
|
|---|
| 3299 | The program used as the shell is taken from the variable @code{SHELL}.
|
|---|
| 3300 | By default, the program @file{/bin/sh} is used.
|
|---|
| 3301 |
|
|---|
| 3302 | @vindex COMSPEC
|
|---|
| 3303 | On MS-DOS, if @code{SHELL} is not set, the value of the variable
|
|---|
| 3304 | @code{COMSPEC} (which is always set) is used instead.
|
|---|
| 3305 |
|
|---|
| 3306 | @cindex @code{SHELL}, MS-DOS specifics
|
|---|
| 3307 | The processing of lines that set the variable @code{SHELL} in Makefiles
|
|---|
| 3308 | is different on MS-DOS. The stock shell, @file{command.com}, is
|
|---|
| 3309 | ridiculously limited in its functionality and many users of @code{make}
|
|---|
| 3310 | tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
|
|---|
| 3311 | examines the value of @code{SHELL}, and changes its behavior based on
|
|---|
| 3312 | whether it points to a Unix-style or DOS-style shell. This allows
|
|---|
| 3313 | reasonable functionality even if @code{SHELL} points to
|
|---|
| 3314 | @file{command.com}.
|
|---|
| 3315 |
|
|---|
| 3316 | If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
|
|---|
| 3317 | additionally checks whether that shell can indeed be found; if not, it
|
|---|
| 3318 | ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
|
|---|
| 3319 | searches for the shell in the following places:
|
|---|
| 3320 |
|
|---|
| 3321 | @enumerate
|
|---|
| 3322 | @item
|
|---|
| 3323 | In the precise place pointed to by the value of @code{SHELL}. For
|
|---|
| 3324 | example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
|
|---|
| 3325 | will look in the directory @file{/bin} on the current drive.
|
|---|
| 3326 |
|
|---|
| 3327 | @item
|
|---|
| 3328 | In the current directory.
|
|---|
| 3329 |
|
|---|
| 3330 | @item
|
|---|
| 3331 | In each of the directories in the @code{PATH} variable, in order.
|
|---|
| 3332 |
|
|---|
| 3333 | @end enumerate
|
|---|
| 3334 |
|
|---|
| 3335 | In every directory it examines, @code{make} will first look for the
|
|---|
| 3336 | specific file (@file{sh} in the example above). If this is not found,
|
|---|
| 3337 | it will also look in that directory for that file with one of the known
|
|---|
| 3338 | extensions which identify executable files. For example @file{.exe},
|
|---|
| 3339 | @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
|
|---|
| 3340 |
|
|---|
| 3341 | If any of these attempts is successful, the value of @code{SHELL} will
|
|---|
| 3342 | be set to the full pathname of the shell as found. However, if none of
|
|---|
| 3343 | these is found, the value of @code{SHELL} will not be changed, and thus
|
|---|
| 3344 | the line that sets it will be effectively ignored. This is so
|
|---|
| 3345 | @code{make} will only support features specific to a Unix-style shell if
|
|---|
| 3346 | such a shell is actually installed on the system where @code{make} runs.
|
|---|
| 3347 |
|
|---|
| 3348 | Note that this extended search for the shell is limited to the cases
|
|---|
| 3349 | where @code{SHELL} is set from the Makefile; if it is set in the
|
|---|
| 3350 | environment or command line, you are expected to set it to the full
|
|---|
| 3351 | pathname of the shell, exactly as things are on Unix.
|
|---|
| 3352 |
|
|---|
| 3353 | The effect of the above DOS-specific processing is that a Makefile that
|
|---|
| 3354 | says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
|
|---|
| 3355 | on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
|
|---|
| 3356 | directory along your @code{PATH}.
|
|---|
| 3357 |
|
|---|
| 3358 | @cindex environment, @code{SHELL} in
|
|---|
| 3359 | Unlike most variables, the variable @code{SHELL} is never set from the
|
|---|
| 3360 | environment. This is because the @code{SHELL} environment variable is
|
|---|
| 3361 | used to specify your personal choice of shell program for interactive
|
|---|
| 3362 | use. It would be very bad for personal choices like this to affect the
|
|---|
| 3363 | functioning of makefiles. @xref{Environment, ,Variables from the
|
|---|
| 3364 | Environment}. However, on MS-DOS and MS-Windows the value of
|
|---|
| 3365 | @code{SHELL} in the environment @strong{is} used, since on those systems
|
|---|
| 3366 | most users do not set this variable, and therefore it is most likely set
|
|---|
| 3367 | specifically to be used by @code{make}. On MS-DOS, if the setting of
|
|---|
| 3368 | @code{SHELL} is not suitable for @code{make}, you can set the variable
|
|---|
| 3369 | @code{MAKESHELL} to the shell that @code{make} should use; this will
|
|---|
| 3370 | override the value of @code{SHELL}.
|
|---|
| 3371 |
|
|---|
| 3372 | @node Parallel, Errors, Execution, Commands
|
|---|
| 3373 | @section Parallel Execution
|
|---|
| 3374 | @cindex commands, execution in parallel
|
|---|
| 3375 | @cindex parallel execution
|
|---|
| 3376 | @cindex execution, in parallel
|
|---|
| 3377 | @cindex job slots
|
|---|
| 3378 | @cindex @code{-j}
|
|---|
| 3379 | @cindex @code{--jobs}
|
|---|
| 3380 |
|
|---|
| 3381 | GNU @code{make} knows how to execute several commands at once.
|
|---|
| 3382 | Normally, @code{make} will execute only one command at a time, waiting
|
|---|
| 3383 | for it to finish before executing the next. However, the @samp{-j} or
|
|---|
| 3384 | @samp{--jobs} option tells @code{make} to execute many commands
|
|---|
| 3385 | simultaneously.@refill
|
|---|
| 3386 |
|
|---|
| 3387 | On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
|
|---|
| 3388 | support multi-processing.
|
|---|
| 3389 |
|
|---|
| 3390 | If the @samp{-j} option is followed by an integer, this is the number of
|
|---|
| 3391 | commands to execute at once; this is called the number of @dfn{job slots}.
|
|---|
| 3392 | If there is nothing looking like an integer after the @samp{-j} option,
|
|---|
| 3393 | there is no limit on the number of job slots. The default number of job
|
|---|
| 3394 | slots is one, which means serial execution (one thing at a time).
|
|---|
| 3395 |
|
|---|
| 3396 | One unpleasant consequence of running several commands simultaneously is
|
|---|
| 3397 | that output generated by the commands appears whenever each command
|
|---|
| 3398 | sends it, so messages from different commands may be interspersed.
|
|---|
| 3399 |
|
|---|
| 3400 | Another problem is that two processes cannot both take input from the
|
|---|
| 3401 | same device; so to make sure that only one command tries to take input
|
|---|
| 3402 | from the terminal at once, @code{make} will invalidate the standard
|
|---|
| 3403 | input streams of all but one running command. This means that
|
|---|
| 3404 | attempting to read from standard input will usually be a fatal error (a
|
|---|
| 3405 | @samp{Broken pipe} signal) for most child processes if there are
|
|---|
| 3406 | several.
|
|---|
| 3407 | @cindex broken pipe
|
|---|
| 3408 | @cindex standard input
|
|---|
| 3409 |
|
|---|
| 3410 | It is unpredictable which command will have a valid standard input stream
|
|---|
| 3411 | (which will come from the terminal, or wherever you redirect the standard
|
|---|
| 3412 | input of @code{make}). The first command run will always get it first, and
|
|---|
| 3413 | the first command started after that one finishes will get it next, and so
|
|---|
| 3414 | on.
|
|---|
| 3415 |
|
|---|
| 3416 | We will change how this aspect of @code{make} works if we find a better
|
|---|
| 3417 | alternative. In the mean time, you should not rely on any command using
|
|---|
| 3418 | standard input at all if you are using the parallel execution feature; but
|
|---|
| 3419 | if you are not using this feature, then standard input works normally in
|
|---|
| 3420 | all commands.
|
|---|
| 3421 |
|
|---|
| 3422 | Finally, handling recursive @code{make} invocations raises issues. For
|
|---|
| 3423 | more information on this, see
|
|---|
| 3424 | @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 3425 |
|
|---|
| 3426 | If a command fails (is killed by a signal or exits with a nonzero
|
|---|
| 3427 | status), and errors are not ignored for that command
|
|---|
| 3428 | (@pxref{Errors, ,Errors in Commands}),
|
|---|
| 3429 | the remaining command lines to remake the same target will not be run.
|
|---|
| 3430 | If a command fails and the @samp{-k} or @samp{--keep-going}
|
|---|
| 3431 | option was not given
|
|---|
| 3432 | (@pxref{Options Summary, ,Summary of Options}),
|
|---|
| 3433 | @code{make} aborts execution. If make
|
|---|
| 3434 | terminates for any reason (including a signal) with child processes
|
|---|
| 3435 | running, it waits for them to finish before actually exiting.@refill
|
|---|
| 3436 |
|
|---|
| 3437 | @cindex load average
|
|---|
| 3438 | @cindex limiting jobs based on load
|
|---|
| 3439 | @cindex jobs, limiting based on load
|
|---|
| 3440 | @cindex @code{-l} (load average)
|
|---|
| 3441 | @cindex @code{--max-load}
|
|---|
| 3442 | @cindex @code{--load-average}
|
|---|
| 3443 | When the system is heavily loaded, you will probably want to run fewer jobs
|
|---|
| 3444 | than when it is lightly loaded. You can use the @samp{-l} option to tell
|
|---|
| 3445 | @code{make} to limit the number of jobs to run at once, based on the load
|
|---|
| 3446 | average. The @samp{-l} or @samp{--max-load}
|
|---|
| 3447 | option is followed by a floating-point number. For
|
|---|
| 3448 | example,
|
|---|
| 3449 |
|
|---|
| 3450 | @example
|
|---|
| 3451 | -l 2.5
|
|---|
| 3452 | @end example
|
|---|
| 3453 |
|
|---|
| 3454 | @noindent
|
|---|
| 3455 | will not let @code{make} start more than one job if the load average is
|
|---|
| 3456 | above 2.5. The @samp{-l} option with no following number removes the
|
|---|
| 3457 | load limit, if one was given with a previous @samp{-l} option.@refill
|
|---|
| 3458 |
|
|---|
| 3459 | More precisely, when @code{make} goes to start up a job, and it already has
|
|---|
| 3460 | at least one job running, it checks the current load average; if it is not
|
|---|
| 3461 | lower than the limit given with @samp{-l}, @code{make} waits until the load
|
|---|
| 3462 | average goes below that limit, or until all the other jobs finish.
|
|---|
| 3463 |
|
|---|
| 3464 | By default, there is no load limit.
|
|---|
| 3465 |
|
|---|
| 3466 | @node Errors, Interrupts, Parallel, Commands
|
|---|
| 3467 | @section Errors in Commands
|
|---|
| 3468 | @cindex errors (in commands)
|
|---|
| 3469 | @cindex commands, errors in
|
|---|
| 3470 | @cindex exit status (errors)
|
|---|
| 3471 |
|
|---|
| 3472 | After each shell command returns, @code{make} looks at its exit status.
|
|---|
| 3473 | If the command completed successfully, the next command line is executed
|
|---|
| 3474 | in a new shell; after the last command line is finished, the rule is
|
|---|
| 3475 | finished.
|
|---|
| 3476 |
|
|---|
| 3477 | If there is an error (the exit status is nonzero), @code{make} gives up on
|
|---|
| 3478 | the current rule, and perhaps on all rules.
|
|---|
| 3479 |
|
|---|
| 3480 | Sometimes the failure of a certain command does not indicate a problem.
|
|---|
| 3481 | For example, you may use the @code{mkdir} command to ensure that a
|
|---|
| 3482 | directory exists. If the directory already exists, @code{mkdir} will
|
|---|
| 3483 | report an error, but you probably want @code{make} to continue regardless.
|
|---|
| 3484 |
|
|---|
| 3485 | @cindex @code{-} (in commands)
|
|---|
| 3486 | To ignore errors in a command line, write a @samp{-} at the beginning of
|
|---|
| 3487 | the line's text (after the initial tab). The @samp{-} is discarded before
|
|---|
| 3488 | the command is passed to the shell for execution.
|
|---|
| 3489 |
|
|---|
| 3490 | For example,
|
|---|
| 3491 |
|
|---|
| 3492 | @example
|
|---|
| 3493 | @group
|
|---|
| 3494 | clean:
|
|---|
| 3495 | -rm -f *.o
|
|---|
| 3496 | @end group
|
|---|
| 3497 | @end example
|
|---|
| 3498 | @cindex @code{rm} (shell command)
|
|---|
| 3499 |
|
|---|
| 3500 | @noindent
|
|---|
| 3501 | This causes @code{rm} to continue even if it is unable to remove a file.
|
|---|
| 3502 |
|
|---|
| 3503 | @cindex @code{-i}
|
|---|
| 3504 | @cindex @code{--ignore-errors}
|
|---|
| 3505 | @findex .IGNORE
|
|---|
| 3506 | When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
|
|---|
| 3507 | flag, errors are ignored in all commands of all rules. A rule in the
|
|---|
| 3508 | makefile for the special target @code{.IGNORE} has the same effect, if
|
|---|
| 3509 | there are no prerequisites. These ways of ignoring errors are obsolete
|
|---|
| 3510 | because @samp{-} is more flexible.
|
|---|
| 3511 |
|
|---|
| 3512 | When errors are to be ignored, because of either a @samp{-} or the
|
|---|
| 3513 | @samp{-i} flag, @code{make} treats an error return just like success,
|
|---|
| 3514 | except that it prints out a message that tells you the status code
|
|---|
| 3515 | the command exited with, and says that the error has been ignored.
|
|---|
| 3516 |
|
|---|
| 3517 | When an error happens that @code{make} has not been told to ignore,
|
|---|
| 3518 | it implies that the current target cannot be correctly remade, and neither
|
|---|
| 3519 | can any other that depends on it either directly or indirectly. No further
|
|---|
| 3520 | commands will be executed for these targets, since their preconditions
|
|---|
| 3521 | have not been achieved.
|
|---|
| 3522 |
|
|---|
| 3523 |
|
|---|
| 3524 | @cindex @code{-k}
|
|---|
| 3525 | @cindex @code{--keep-going}
|
|---|
| 3526 | Normally @code{make} gives up immediately in this circumstance, returning a
|
|---|
| 3527 | nonzero status. However, if the @samp{-k} or @samp{--keep-going}
|
|---|
| 3528 | flag is specified, @code{make}
|
|---|
| 3529 | continues to consider the other prerequisites of the pending targets,
|
|---|
| 3530 | remaking them if necessary, before it gives up and returns nonzero status.
|
|---|
| 3531 | For example, after an error in compiling one object file, @samp{make -k}
|
|---|
| 3532 | will continue compiling other object files even though it already knows
|
|---|
| 3533 | that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
|
|---|
| 3534 |
|
|---|
| 3535 | The usual behavior assumes that your purpose is to get the specified
|
|---|
| 3536 | targets up to date; once @code{make} learns that this is impossible, it
|
|---|
| 3537 | might as well report the failure immediately. The @samp{-k} option says
|
|---|
| 3538 | that the real purpose is to test as many of the changes made in the
|
|---|
| 3539 | program as possible, perhaps to find several independent problems so
|
|---|
| 3540 | that you can correct them all before the next attempt to compile. This
|
|---|
| 3541 | is why Emacs' @code{compile} command passes the @samp{-k} flag by
|
|---|
| 3542 | default.
|
|---|
| 3543 | @cindex Emacs (@code{M-x compile})
|
|---|
| 3544 |
|
|---|
| 3545 | @findex .DELETE_ON_ERROR
|
|---|
| 3546 | @cindex deletion of target files
|
|---|
| 3547 | @cindex removal of target files
|
|---|
| 3548 | @cindex target, deleting on error
|
|---|
| 3549 | Usually when a command fails, if it has changed the target file at all,
|
|---|
| 3550 | the file is corrupted and cannot be used---or at least it is not
|
|---|
| 3551 | completely updated. Yet the file's time stamp says that it is now up to
|
|---|
| 3552 | date, so the next time @code{make} runs, it will not try to update that
|
|---|
| 3553 | file. The situation is just the same as when the command is killed by a
|
|---|
| 3554 | signal; @pxref{Interrupts}. So generally the right thing to do is to
|
|---|
| 3555 | delete the target file if the command fails after beginning to change
|
|---|
| 3556 | the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
|
|---|
| 3557 | as a target. This is almost always what you want @code{make} to do, but
|
|---|
| 3558 | it is not historical practice; so for compatibility, you must explicitly
|
|---|
| 3559 | request it.
|
|---|
| 3560 |
|
|---|
| 3561 | @node Interrupts, Recursion, Errors, Commands
|
|---|
| 3562 | @section Interrupting or Killing @code{make}
|
|---|
| 3563 | @cindex interrupt
|
|---|
| 3564 | @cindex signal
|
|---|
| 3565 | @cindex deletion of target files
|
|---|
| 3566 | @cindex removal of target files
|
|---|
| 3567 | @cindex target, deleting on interrupt
|
|---|
| 3568 | @cindex killing (interruption)
|
|---|
| 3569 |
|
|---|
| 3570 | If @code{make} gets a fatal signal while a command is executing, it may
|
|---|
| 3571 | delete the target file that the command was supposed to update. This is
|
|---|
| 3572 | done if the target file's last-modification time has changed since
|
|---|
| 3573 | @code{make} first checked it.
|
|---|
| 3574 |
|
|---|
| 3575 | The purpose of deleting the target is to make sure that it is remade from
|
|---|
| 3576 | scratch when @code{make} is next run. Why is this? Suppose you type
|
|---|
| 3577 | @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
|
|---|
| 3578 | object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
|
|---|
| 3579 | in an incomplete file whose last-modification time is newer than the source
|
|---|
| 3580 | file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
|
|---|
| 3581 | and deletes this incomplete file. If @code{make} did not do this, the next
|
|---|
| 3582 | invocation of @code{make} would think that @file{foo.o} did not require
|
|---|
| 3583 | updating---resulting in a strange error message from the linker when it
|
|---|
| 3584 | tries to link an object file half of which is missing.
|
|---|
| 3585 |
|
|---|
| 3586 | @findex .PRECIOUS
|
|---|
| 3587 | You can prevent the deletion of a target file in this way by making the
|
|---|
| 3588 | special target @code{.PRECIOUS} depend on it. Before remaking a target,
|
|---|
| 3589 | @code{make} checks to see whether it appears on the prerequisites of
|
|---|
| 3590 | @code{.PRECIOUS}, and thereby decides whether the target should be deleted
|
|---|
| 3591 | if a signal happens. Some reasons why you might do this are that the
|
|---|
| 3592 | target is updated in some atomic fashion, or exists only to record a
|
|---|
| 3593 | modification-time (its contents do not matter), or must exist at all
|
|---|
| 3594 | times to prevent other sorts of trouble.
|
|---|
| 3595 |
|
|---|
| 3596 | @node Recursion, Sequences, Interrupts, Commands
|
|---|
| 3597 | @section Recursive Use of @code{make}
|
|---|
| 3598 | @cindex recursion
|
|---|
| 3599 | @cindex subdirectories, recursion for
|
|---|
| 3600 |
|
|---|
| 3601 | Recursive use of @code{make} means using @code{make} as a command in a
|
|---|
| 3602 | makefile. This technique is useful when you want separate makefiles for
|
|---|
| 3603 | various subsystems that compose a larger system. For example, suppose you
|
|---|
| 3604 | have a subdirectory @file{subdir} which has its own makefile, and you would
|
|---|
| 3605 | like the containing directory's makefile to run @code{make} on the
|
|---|
| 3606 | subdirectory. You can do it by writing this:
|
|---|
| 3607 |
|
|---|
| 3608 | @example
|
|---|
| 3609 | subsystem:
|
|---|
| 3610 | cd subdir && $(MAKE)
|
|---|
| 3611 | @end example
|
|---|
| 3612 |
|
|---|
| 3613 | @noindent
|
|---|
| 3614 | or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
|
|---|
| 3615 |
|
|---|
| 3616 | @example
|
|---|
| 3617 | subsystem:
|
|---|
| 3618 | $(MAKE) -C subdir
|
|---|
| 3619 | @end example
|
|---|
| 3620 | @cindex @code{-C}
|
|---|
| 3621 | @cindex @code{--directory}
|
|---|
| 3622 |
|
|---|
| 3623 | You can write recursive @code{make} commands just by copying this example,
|
|---|
| 3624 | but there are many things to know about how they work and why, and about
|
|---|
| 3625 | how the sub-@code{make} relates to the top-level @code{make}. You may
|
|---|
| 3626 | also find it useful to declare targets that invoke recursive
|
|---|
| 3627 | @code{make} commands as @samp{.PHONY} (for more discussion on when
|
|---|
| 3628 | this is useful, see @ref{Phony Targets}).
|
|---|
| 3629 |
|
|---|
| 3630 | @vindex @code{CURDIR}
|
|---|
| 3631 | For your convenience, when GNU @code{make} starts (after it has
|
|---|
| 3632 | processed any @code{-C} options) it sets the variable @code{CURDIR} to
|
|---|
| 3633 | the pathname of the current working directory. This value is never
|
|---|
| 3634 | touched by @code{make} again: in particular note that if you include
|
|---|
| 3635 | files from other directories the value of @code{CURDIR} does not
|
|---|
| 3636 | change. The value has the same precedence it would have if it were
|
|---|
| 3637 | set in the makefile (by default, an environment variable @code{CURDIR}
|
|---|
| 3638 | will not override this value). Note that setting this variable has no
|
|---|
| 3639 | impact on the operation of @code{make} (it does not cause @code{make}
|
|---|
| 3640 | to change its working directory, for example).
|
|---|
| 3641 |
|
|---|
| 3642 | @menu
|
|---|
| 3643 | * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
|
|---|
| 3644 | * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
|
|---|
| 3645 | * Options/Recursion:: How to communicate options to a sub-@code{make}.
|
|---|
| 3646 | * -w Option:: How the @samp{-w} or @samp{--print-directory} option
|
|---|
| 3647 | helps debug use of recursive @code{make} commands.
|
|---|
| 3648 | @end menu
|
|---|
| 3649 |
|
|---|
| 3650 | @node MAKE Variable, Variables/Recursion, Recursion, Recursion
|
|---|
| 3651 | @subsection How the @code{MAKE} Variable Works
|
|---|
| 3652 | @vindex MAKE
|
|---|
| 3653 | @cindex recursion, and @code{MAKE} variable
|
|---|
| 3654 |
|
|---|
| 3655 | Recursive @code{make} commands should always use the variable @code{MAKE},
|
|---|
| 3656 | not the explicit command name @samp{make}, as shown here:
|
|---|
| 3657 |
|
|---|
| 3658 | @example
|
|---|
| 3659 | @group
|
|---|
| 3660 | subsystem:
|
|---|
| 3661 | cd subdir && $(MAKE)
|
|---|
| 3662 | @end group
|
|---|
| 3663 | @end example
|
|---|
| 3664 |
|
|---|
| 3665 | The value of this variable is the file name with which @code{make} was
|
|---|
| 3666 | invoked. If this file name was @file{/bin/make}, then the command executed
|
|---|
| 3667 | is @samp{cd subdir && /bin/make}. If you use a special version of
|
|---|
| 3668 | @code{make} to run the top-level makefile, the same special version will be
|
|---|
| 3669 | executed for recursive invocations.
|
|---|
| 3670 | @cindex @code{cd} (shell command)
|
|---|
| 3671 |
|
|---|
| 3672 | @cindex +, and commands
|
|---|
| 3673 | As a special feature, using the variable @code{MAKE} in the commands of
|
|---|
| 3674 | a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
|
|---|
| 3675 | (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
|
|---|
| 3676 | Using the @code{MAKE} variable has the same effect as using a @samp{+}
|
|---|
| 3677 | character at the beginning of the command line. @xref{Instead of
|
|---|
| 3678 | Execution, ,Instead of Executing the Commands}. This special feature
|
|---|
| 3679 | is only enabled if the @code{MAKE} variable appears directly in the
|
|---|
| 3680 | command script: it does not apply if the @code{MAKE} variable is
|
|---|
| 3681 | referenced through expansion of another variable. In the latter case
|
|---|
| 3682 | you must use the @samp{+} token to get these special effects.@refill
|
|---|
| 3683 |
|
|---|
| 3684 | Consider the command @samp{make -t} in the above example. (The
|
|---|
| 3685 | @samp{-t} option marks targets as up to date without actually running
|
|---|
| 3686 | any commands; see @ref{Instead of Execution}.) Following the usual
|
|---|
| 3687 | definition of @samp{-t}, a @samp{make -t} command in the example would
|
|---|
| 3688 | create a file named @file{subsystem} and do nothing else. What you
|
|---|
| 3689 | really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
|
|---|
| 3690 | require executing the command, and @samp{-t} says not to execute
|
|---|
| 3691 | commands.@refill
|
|---|
| 3692 | @cindex @code{-t}, and recursion
|
|---|
| 3693 | @cindex recursion, and @code{-t}
|
|---|
| 3694 | @cindex @code{--touch}, and recursion
|
|---|
| 3695 |
|
|---|
| 3696 | The special feature makes this do what you want: whenever a command
|
|---|
| 3697 | line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
|
|---|
| 3698 | @samp{-n} and @samp{-q} do not apply to that line. Command lines
|
|---|
| 3699 | containing @code{MAKE} are executed normally despite the presence of a
|
|---|
| 3700 | flag that causes most commands not to be run. The usual
|
|---|
| 3701 | @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
|
|---|
| 3702 | (@pxref{Options/Recursion, ,Communicating Options to a
|
|---|
| 3703 | Sub-@code{make}}), so your request to touch the files, or print the
|
|---|
| 3704 | commands, is propagated to the subsystem.@refill
|
|---|
| 3705 |
|
|---|
| 3706 | @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
|
|---|
| 3707 | @subsection Communicating Variables to a Sub-@code{make}
|
|---|
| 3708 | @cindex sub-@code{make}
|
|---|
| 3709 | @cindex environment, and recursion
|
|---|
| 3710 | @cindex exporting variables
|
|---|
| 3711 | @cindex variables, environment
|
|---|
| 3712 | @cindex variables, exporting
|
|---|
| 3713 | @cindex recursion, and environment
|
|---|
| 3714 | @cindex recursion, and variables
|
|---|
| 3715 |
|
|---|
| 3716 | Variable values of the top-level @code{make} can be passed to the
|
|---|
| 3717 | sub-@code{make} through the environment by explicit request. These
|
|---|
| 3718 | variables are defined in the sub-@code{make} as defaults, but do not
|
|---|
| 3719 | override what is specified in the makefile used by the sub-@code{make}
|
|---|
| 3720 | makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
|
|---|
| 3721 | ,Summary of Options}).@refill
|
|---|
| 3722 |
|
|---|
| 3723 | To pass down, or @dfn{export}, a variable, @code{make} adds the variable
|
|---|
| 3724 | and its value to the environment for running each command. The
|
|---|
| 3725 | sub-@code{make}, in turn, uses the environment to initialize its table
|
|---|
| 3726 | of variable values. @xref{Environment, ,Variables from the
|
|---|
| 3727 | Environment}.
|
|---|
| 3728 |
|
|---|
| 3729 | Except by explicit request, @code{make} exports a variable only if it
|
|---|
| 3730 | is either defined in the environment initially or set on the command
|
|---|
| 3731 | line, and if its name consists only of letters, numbers, and underscores.
|
|---|
| 3732 | Some shells cannot cope with environment variable names consisting of
|
|---|
| 3733 | characters other than letters, numbers, and underscores.
|
|---|
| 3734 |
|
|---|
| 3735 | The special variables @code{SHELL} and @code{MAKEFLAGS} are always
|
|---|
| 3736 | exported (unless you unexport them).
|
|---|
| 3737 | @code{MAKEFILES} is exported if you set it to anything.
|
|---|
| 3738 |
|
|---|
| 3739 | @code{make} automatically passes down variable values that were defined
|
|---|
| 3740 | on the command line, by putting them in the @code{MAKEFLAGS} variable.
|
|---|
| 3741 | @iftex
|
|---|
| 3742 | See the next section.
|
|---|
| 3743 | @end iftex
|
|---|
| 3744 | @ifnottex
|
|---|
| 3745 | @xref{Options/Recursion}.
|
|---|
| 3746 | @end ifnottex
|
|---|
| 3747 |
|
|---|
| 3748 | Variables are @emph{not} normally passed down if they were created by
|
|---|
| 3749 | default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
|
|---|
| 3750 | Implicit Rules}). The sub-@code{make} will define these for
|
|---|
| 3751 | itself.@refill
|
|---|
| 3752 |
|
|---|
| 3753 | @findex export
|
|---|
| 3754 | If you want to export specific variables to a sub-@code{make}, use the
|
|---|
| 3755 | @code{export} directive, like this:
|
|---|
| 3756 |
|
|---|
| 3757 | @example
|
|---|
| 3758 | export @var{variable} @dots{}
|
|---|
| 3759 | @end example
|
|---|
| 3760 |
|
|---|
| 3761 | @noindent
|
|---|
| 3762 | @findex unexport
|
|---|
| 3763 | If you want to @emph{prevent} a variable from being exported, use the
|
|---|
| 3764 | @code{unexport} directive, like this:
|
|---|
| 3765 |
|
|---|
| 3766 | @example
|
|---|
| 3767 | unexport @var{variable} @dots{}
|
|---|
| 3768 | @end example
|
|---|
| 3769 |
|
|---|
| 3770 | @noindent
|
|---|
| 3771 | In both of these forms, the arguments to @code{export} and
|
|---|
| 3772 | @code{unexport} are expanded, and so could be variables or functions
|
|---|
| 3773 | which expand to a (list of) variable names to be (un)exported.
|
|---|
| 3774 |
|
|---|
| 3775 | As a convenience, you can define a variable and export it at the same
|
|---|
| 3776 | time by doing:
|
|---|
| 3777 |
|
|---|
| 3778 | @example
|
|---|
| 3779 | export @var{variable} = value
|
|---|
| 3780 | @end example
|
|---|
| 3781 |
|
|---|
| 3782 | @noindent
|
|---|
| 3783 | has the same result as:
|
|---|
| 3784 |
|
|---|
| 3785 | @example
|
|---|
| 3786 | @var{variable} = value
|
|---|
| 3787 | export @var{variable}
|
|---|
| 3788 | @end example
|
|---|
| 3789 |
|
|---|
| 3790 | @noindent
|
|---|
| 3791 | and
|
|---|
| 3792 |
|
|---|
| 3793 | @example
|
|---|
| 3794 | export @var{variable} := value
|
|---|
| 3795 | @end example
|
|---|
| 3796 |
|
|---|
| 3797 | @noindent
|
|---|
| 3798 | has the same result as:
|
|---|
| 3799 |
|
|---|
| 3800 | @example
|
|---|
| 3801 | @var{variable} := value
|
|---|
| 3802 | export @var{variable}
|
|---|
| 3803 | @end example
|
|---|
| 3804 |
|
|---|
| 3805 | Likewise,
|
|---|
| 3806 |
|
|---|
| 3807 | @example
|
|---|
| 3808 | export @var{variable} += value
|
|---|
| 3809 | @end example
|
|---|
| 3810 |
|
|---|
| 3811 | @noindent
|
|---|
| 3812 | is just like:
|
|---|
| 3813 |
|
|---|
| 3814 | @example
|
|---|
| 3815 | @var{variable} += value
|
|---|
| 3816 | export @var{variable}
|
|---|
| 3817 | @end example
|
|---|
| 3818 |
|
|---|
| 3819 | @noindent
|
|---|
| 3820 | @xref{Appending, ,Appending More Text to Variables}.
|
|---|
| 3821 |
|
|---|
| 3822 | You may notice that the @code{export} and @code{unexport} directives
|
|---|
| 3823 | work in @code{make} in the same way they work in the shell, @code{sh}.
|
|---|
| 3824 |
|
|---|
| 3825 | If you want all variables to be exported by default, you can use
|
|---|
| 3826 | @code{export} by itself:
|
|---|
| 3827 |
|
|---|
| 3828 | @example
|
|---|
| 3829 | export
|
|---|
| 3830 | @end example
|
|---|
| 3831 |
|
|---|
| 3832 | @noindent
|
|---|
| 3833 | This tells @code{make} that variables which are not explicitly mentioned
|
|---|
| 3834 | in an @code{export} or @code{unexport} directive should be exported.
|
|---|
| 3835 | Any variable given in an @code{unexport} directive will still @emph{not}
|
|---|
| 3836 | be exported. If you use @code{export} by itself to export variables by
|
|---|
| 3837 | default, variables whose names contain characters other than
|
|---|
| 3838 | alphanumerics and underscores will not be exported unless specifically
|
|---|
| 3839 | mentioned in an @code{export} directive.@refill
|
|---|
| 3840 |
|
|---|
| 3841 | @findex .EXPORT_ALL_VARIABLES
|
|---|
| 3842 | The behavior elicited by an @code{export} directive by itself was the
|
|---|
| 3843 | default in older versions of GNU @code{make}. If your makefiles depend
|
|---|
| 3844 | on this behavior and you want to be compatible with old versions of
|
|---|
| 3845 | @code{make}, you can write a rule for the special target
|
|---|
| 3846 | @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
|
|---|
| 3847 | This will be ignored by old @code{make}s, while the @code{export}
|
|---|
| 3848 | directive will cause a syntax error.@refill
|
|---|
| 3849 | @cindex compatibility in exporting
|
|---|
| 3850 |
|
|---|
| 3851 | Likewise, you can use @code{unexport} by itself to tell @code{make}
|
|---|
| 3852 | @emph{not} to export variables by default. Since this is the default
|
|---|
| 3853 | behavior, you would only need to do this if @code{export} had been used
|
|---|
| 3854 | by itself earlier (in an included makefile, perhaps). You
|
|---|
| 3855 | @strong{cannot} use @code{export} and @code{unexport} by themselves to
|
|---|
| 3856 | have variables exported for some commands and not for others. The last
|
|---|
| 3857 | @code{export} or @code{unexport} directive that appears by itself
|
|---|
| 3858 | determines the behavior for the entire run of @code{make}.@refill
|
|---|
| 3859 |
|
|---|
| 3860 | @vindex MAKELEVEL
|
|---|
| 3861 | @cindex recursion, level of
|
|---|
| 3862 | As a special feature, the variable @code{MAKELEVEL} is changed when it
|
|---|
| 3863 | is passed down from level to level. This variable's value is a string
|
|---|
| 3864 | which is the depth of the level as a decimal number. The value is
|
|---|
| 3865 | @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
|
|---|
| 3866 | @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
|
|---|
| 3867 | happens when @code{make} sets up the environment for a command.@refill
|
|---|
| 3868 |
|
|---|
| 3869 | The main use of @code{MAKELEVEL} is to test it in a conditional
|
|---|
| 3870 | directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
|
|---|
| 3871 | way you can write a makefile that behaves one way if run recursively and
|
|---|
| 3872 | another way if run directly by you.@refill
|
|---|
| 3873 |
|
|---|
| 3874 | @vindex MAKEFILES
|
|---|
| 3875 | You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
|
|---|
| 3876 | commands to use additional makefiles. The value of @code{MAKEFILES} is
|
|---|
| 3877 | a whitespace-separated list of file names. This variable, if defined in
|
|---|
| 3878 | the outer-level makefile, is passed down through the environment; then
|
|---|
| 3879 | it serves as a list of extra makefiles for the sub-@code{make} to read
|
|---|
| 3880 | before the usual or specified ones. @xref{MAKEFILES Variable, ,The
|
|---|
| 3881 | Variable @code{MAKEFILES}}.@refill
|
|---|
| 3882 |
|
|---|
| 3883 | @node Options/Recursion, -w Option, Variables/Recursion, Recursion
|
|---|
| 3884 | @subsection Communicating Options to a Sub-@code{make}
|
|---|
| 3885 | @cindex options, and recursion
|
|---|
| 3886 | @cindex recursion, and options
|
|---|
| 3887 |
|
|---|
| 3888 | @vindex MAKEFLAGS
|
|---|
| 3889 | Flags such as @samp{-s} and @samp{-k} are passed automatically to the
|
|---|
| 3890 | sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
|
|---|
| 3891 | set up automatically by @code{make} to contain the flag letters that
|
|---|
| 3892 | @code{make} received. Thus, if you do @w{@samp{make -ks}} then
|
|---|
| 3893 | @code{MAKEFLAGS} gets the value @samp{ks}.@refill
|
|---|
| 3894 |
|
|---|
| 3895 | As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
|
|---|
| 3896 | in its environment. In response, it takes the flags from that value and
|
|---|
| 3897 | processes them as if they had been given as arguments.
|
|---|
| 3898 | @xref{Options Summary, ,Summary of Options}.
|
|---|
| 3899 |
|
|---|
| 3900 | @cindex command line variable definitions, and recursion
|
|---|
| 3901 | @cindex variables, command line, and recursion
|
|---|
| 3902 | @cindex recursion, and command line variable definitions
|
|---|
| 3903 | Likewise variables defined on the command line are passed to the
|
|---|
| 3904 | sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
|
|---|
| 3905 | @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
|
|---|
| 3906 | definitions just as if they appeared on the command line.
|
|---|
| 3907 | @xref{Overriding, ,Overriding Variables}.
|
|---|
| 3908 |
|
|---|
| 3909 | @cindex @code{-C}, and recursion
|
|---|
| 3910 | @cindex @code{-f}, and recursion
|
|---|
| 3911 | @cindex @code{-o}, and recursion
|
|---|
| 3912 | @cindex @code{-W}, and recursion
|
|---|
| 3913 | @cindex @code{--directory}, and recursion
|
|---|
| 3914 | @cindex @code{--file}, and recursion
|
|---|
| 3915 | @cindex @code{--old-file}, and recursion
|
|---|
| 3916 | @cindex @code{--assume-old}, and recursion
|
|---|
| 3917 | @cindex @code{--assume-new}, and recursion
|
|---|
| 3918 | @cindex @code{--new-file}, and recursion
|
|---|
| 3919 | @cindex recursion, and @code{-C}
|
|---|
| 3920 | @cindex recursion, and @code{-f}
|
|---|
| 3921 | @cindex recursion, and @code{-o}
|
|---|
| 3922 | @cindex recursion, and @code{-W}
|
|---|
| 3923 | The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
|
|---|
| 3924 | into @code{MAKEFLAGS}; these options are not passed down.@refill
|
|---|
| 3925 |
|
|---|
| 3926 | @cindex @code{-j}, and recursion
|
|---|
| 3927 | @cindex @code{--jobs}, and recursion
|
|---|
| 3928 | @cindex recursion, and @code{-j}
|
|---|
| 3929 | @cindex job slots, and recursion
|
|---|
| 3930 | The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
|
|---|
| 3931 | If you set it to some numeric value @samp{N} and your operating system
|
|---|
| 3932 | supports it (most any UNIX system will; others typically won't), the
|
|---|
| 3933 | parent @code{make} and all the sub-@code{make}s will communicate to
|
|---|
| 3934 | ensure that there are only @samp{N} jobs running at the same time
|
|---|
| 3935 | between them all. Note that any job that is marked recursive
|
|---|
| 3936 | (@pxref{Instead of Execution, ,Instead of Executing the Commands})
|
|---|
| 3937 | doesn't count against the total jobs (otherwise we could get @samp{N}
|
|---|
| 3938 | sub-@code{make}s running and have no slots left over for any real work!)
|
|---|
| 3939 |
|
|---|
| 3940 | If your operating system doesn't support the above communication, then
|
|---|
| 3941 | @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
|
|---|
| 3942 | specified. This is because if the @w{@samp{-j}} option were passed down
|
|---|
| 3943 | to sub-@code{make}s, you would get many more jobs running in parallel
|
|---|
| 3944 | than you asked for. If you give @samp{-j} with no numeric argument,
|
|---|
| 3945 | meaning to run as many jobs as possible in parallel, this is passed
|
|---|
| 3946 | down, since multiple infinities are no more than one.@refill
|
|---|
| 3947 |
|
|---|
| 3948 | If you do not want to pass the other flags down, you must change the
|
|---|
| 3949 | value of @code{MAKEFLAGS}, like this:
|
|---|
| 3950 |
|
|---|
| 3951 | @example
|
|---|
| 3952 | subsystem:
|
|---|
| 3953 | cd subdir && $(MAKE) MAKEFLAGS=
|
|---|
| 3954 | @end example
|
|---|
| 3955 |
|
|---|
| 3956 | @vindex MAKEOVERRIDES
|
|---|
| 3957 | The command line variable definitions really appear in the variable
|
|---|
| 3958 | @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
|
|---|
| 3959 | variable. If you do want to pass flags down normally, but don't want to
|
|---|
| 3960 | pass down the command line variable definitions, you can reset
|
|---|
| 3961 | @code{MAKEOVERRIDES} to empty, like this:
|
|---|
| 3962 |
|
|---|
| 3963 | @example
|
|---|
| 3964 | MAKEOVERRIDES =
|
|---|
| 3965 | @end example
|
|---|
| 3966 |
|
|---|
| 3967 | @noindent
|
|---|
| 3968 | @cindex Arg list too long
|
|---|
| 3969 | @cindex E2BIG
|
|---|
| 3970 | This is not usually useful to do. However, some systems have a small
|
|---|
| 3971 | fixed limit on the size of the environment, and putting so much
|
|---|
| 3972 | information into the value of @code{MAKEFLAGS} can exceed it. If you
|
|---|
| 3973 | see the error message @samp{Arg list too long}, this may be the problem.
|
|---|
| 3974 | @findex .POSIX
|
|---|
| 3975 | @cindex POSIX.2
|
|---|
| 3976 | (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
|
|---|
| 3977 | not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
|
|---|
| 3978 | in the makefile. You probably do not care about this.)
|
|---|
| 3979 |
|
|---|
| 3980 | @vindex MFLAGS
|
|---|
| 3981 | A similar variable @code{MFLAGS} exists also, for historical
|
|---|
| 3982 | compatibility. It has the same value as @code{MAKEFLAGS} except that it
|
|---|
| 3983 | does not contain the command line variable definitions, and it always
|
|---|
| 3984 | begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
|
|---|
| 3985 | hyphen only when it begins with an option that has no single-letter
|
|---|
| 3986 | version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
|
|---|
| 3987 | traditionally used explicitly in the recursive @code{make} command, like
|
|---|
| 3988 | this:
|
|---|
| 3989 |
|
|---|
| 3990 | @example
|
|---|
| 3991 | subsystem:
|
|---|
| 3992 | cd subdir && $(MAKE) $(MFLAGS)
|
|---|
| 3993 | @end example
|
|---|
| 3994 |
|
|---|
| 3995 | @noindent
|
|---|
| 3996 | but now @code{MAKEFLAGS} makes this usage redundant. If you want your
|
|---|
| 3997 | makefiles to be compatible with old @code{make} programs, use this
|
|---|
| 3998 | technique; it will work fine with more modern @code{make} versions too.
|
|---|
| 3999 |
|
|---|
| 4000 | @cindex setting options from environment
|
|---|
| 4001 | @cindex options, setting from environment
|
|---|
| 4002 | @cindex setting options in makefiles
|
|---|
| 4003 | @cindex options, setting in makefiles
|
|---|
| 4004 | The @code{MAKEFLAGS} variable can also be useful if you want to have
|
|---|
| 4005 | certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
|
|---|
| 4006 | Options}), set each time you run @code{make}. You simply put a value for
|
|---|
| 4007 | @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
|
|---|
| 4008 | a makefile, to specify additional flags that should also be in effect for
|
|---|
| 4009 | that makefile. (Note that you cannot use @code{MFLAGS} this way. That
|
|---|
| 4010 | variable is set only for compatibility; @code{make} does not interpret a
|
|---|
| 4011 | value you set for it in any way.)
|
|---|
| 4012 |
|
|---|
| 4013 | When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
|
|---|
| 4014 | environment or from a makefile), it first prepends a hyphen if the value
|
|---|
| 4015 | does not already begin with one. Then it chops the value into words
|
|---|
| 4016 | separated by blanks, and parses these words as if they were options given
|
|---|
| 4017 | on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
|
|---|
| 4018 | @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
|
|---|
| 4019 | is no error for an invalid option).
|
|---|
| 4020 |
|
|---|
| 4021 | If you do put @code{MAKEFLAGS} in your environment, you should be sure not
|
|---|
| 4022 | to include any options that will drastically affect the actions of
|
|---|
| 4023 | @code{make} and undermine the purpose of makefiles and of @code{make}
|
|---|
| 4024 | itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
|
|---|
| 4025 | put in one of these variables, could have disastrous consequences and would
|
|---|
| 4026 | certainly have at least surprising and probably annoying effects.@refill
|
|---|
| 4027 |
|
|---|
| 4028 | @node -w Option, , Options/Recursion, Recursion
|
|---|
| 4029 | @subsection The @samp{--print-directory} Option
|
|---|
| 4030 | @cindex directories, printing them
|
|---|
| 4031 | @cindex printing directories
|
|---|
| 4032 | @cindex recursion, and printing directories
|
|---|
| 4033 |
|
|---|
| 4034 | If you use several levels of recursive @code{make} invocations, the
|
|---|
| 4035 | @samp{-w} or @w{@samp{--print-directory}} option can make the output a
|
|---|
| 4036 | lot easier to understand by showing each directory as @code{make}
|
|---|
| 4037 | starts processing it and as @code{make} finishes processing it. For
|
|---|
| 4038 | example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
|
|---|
| 4039 | @code{make} will print a line of the form:@refill
|
|---|
| 4040 |
|
|---|
| 4041 | @example
|
|---|
| 4042 | make: Entering directory `/u/gnu/make'.
|
|---|
| 4043 | @end example
|
|---|
| 4044 |
|
|---|
| 4045 | @noindent
|
|---|
| 4046 | before doing anything else, and a line of the form:
|
|---|
| 4047 |
|
|---|
| 4048 | @example
|
|---|
| 4049 | make: Leaving directory `/u/gnu/make'.
|
|---|
| 4050 | @end example
|
|---|
| 4051 |
|
|---|
| 4052 | @noindent
|
|---|
| 4053 | when processing is completed.
|
|---|
| 4054 |
|
|---|
| 4055 | @cindex @code{-C}, and @code{-w}
|
|---|
| 4056 | @cindex @code{--directory}, and @code{--print-directory}
|
|---|
| 4057 | @cindex recursion, and @code{-w}
|
|---|
| 4058 | @cindex @code{-w}, and @code{-C}
|
|---|
| 4059 | @cindex @code{-w}, and recursion
|
|---|
| 4060 | @cindex @code{--print-directory}, and @code{--directory}
|
|---|
| 4061 | @cindex @code{--print-directory}, and recursion
|
|---|
| 4062 | @cindex @code{--no-print-directory}
|
|---|
| 4063 | @cindex @code{--print-directory}, disabling
|
|---|
| 4064 | @cindex @code{-w}, disabling
|
|---|
| 4065 | Normally, you do not need to specify this option because @samp{make}
|
|---|
| 4066 | does it for you: @samp{-w} is turned on automatically when you use the
|
|---|
| 4067 | @samp{-C} option, and in sub-@code{make}s. @code{make} will not
|
|---|
| 4068 | automatically turn on @samp{-w} if you also use @samp{-s}, which says to
|
|---|
| 4069 | be silent, or if you use @samp{--no-print-directory} to explicitly
|
|---|
| 4070 | disable it.
|
|---|
| 4071 |
|
|---|
| 4072 | @node Sequences, Empty Commands, Recursion, Commands
|
|---|
| 4073 | @section Defining Canned Command Sequences
|
|---|
| 4074 | @cindex sequences of commands
|
|---|
| 4075 | @cindex commands, sequences of
|
|---|
| 4076 |
|
|---|
| 4077 | When the same sequence of commands is useful in making various targets, you
|
|---|
| 4078 | can define it as a canned sequence with the @code{define} directive, and
|
|---|
| 4079 | refer to the canned sequence from the rules for those targets. The canned
|
|---|
| 4080 | sequence is actually a variable, so the name must not conflict with other
|
|---|
| 4081 | variable names.
|
|---|
| 4082 |
|
|---|
| 4083 | Here is an example of defining a canned sequence of commands:
|
|---|
| 4084 |
|
|---|
| 4085 | @example
|
|---|
| 4086 | define run-yacc
|
|---|
| 4087 | yacc $(firstword $^)
|
|---|
| 4088 | mv y.tab.c $@@
|
|---|
| 4089 | endef
|
|---|
| 4090 | @end example
|
|---|
| 4091 | @cindex @code{yacc}
|
|---|
| 4092 |
|
|---|
| 4093 | @noindent
|
|---|
| 4094 | Here @code{run-yacc} is the name of the variable being defined;
|
|---|
| 4095 | @code{endef} marks the end of the definition; the lines in between are the
|
|---|
| 4096 | commands. The @code{define} directive does not expand variable references
|
|---|
| 4097 | and function calls in the canned sequence; the @samp{$} characters,
|
|---|
| 4098 | parentheses, variable names, and so on, all become part of the value of the
|
|---|
| 4099 | variable you are defining.
|
|---|
| 4100 | @xref{Defining, ,Defining Variables Verbatim},
|
|---|
| 4101 | for a complete explanation of @code{define}.
|
|---|
| 4102 |
|
|---|
| 4103 | The first command in this example runs Yacc on the first prerequisite of
|
|---|
| 4104 | whichever rule uses the canned sequence. The output file from Yacc is
|
|---|
| 4105 | always named @file{y.tab.c}. The second command moves the output to the
|
|---|
| 4106 | rule's target file name.
|
|---|
| 4107 |
|
|---|
| 4108 | To use the canned sequence, substitute the variable into the commands of a
|
|---|
| 4109 | rule. You can substitute it like any other variable
|
|---|
| 4110 | (@pxref{Reference, ,Basics of Variable References}).
|
|---|
| 4111 | Because variables defined by @code{define} are recursively expanded
|
|---|
| 4112 | variables, all the variable references you wrote inside the @code{define}
|
|---|
| 4113 | are expanded now. For example:
|
|---|
| 4114 |
|
|---|
| 4115 | @example
|
|---|
| 4116 | foo.c : foo.y
|
|---|
| 4117 | $(run-yacc)
|
|---|
| 4118 | @end example
|
|---|
| 4119 |
|
|---|
| 4120 | @noindent
|
|---|
| 4121 | @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
|
|---|
| 4122 | @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
|
|---|
| 4123 |
|
|---|
| 4124 | This is a realistic example, but this particular one is not needed in
|
|---|
| 4125 | practice because @code{make} has an implicit rule to figure out these
|
|---|
| 4126 | commands based on the file names involved
|
|---|
| 4127 | (@pxref{Implicit Rules, ,Using Implicit Rules}).
|
|---|
| 4128 |
|
|---|
| 4129 | @cindex @@, and @code{define}
|
|---|
| 4130 | @cindex -, and @code{define}
|
|---|
| 4131 | @cindex +, and @code{define}
|
|---|
| 4132 | In command execution, each line of a canned sequence is treated just as
|
|---|
| 4133 | if the line appeared on its own in the rule, preceded by a tab. In
|
|---|
| 4134 | particular, @code{make} invokes a separate subshell for each line. You
|
|---|
| 4135 | can use the special prefix characters that affect command lines
|
|---|
| 4136 | (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
|
|---|
| 4137 | @xref{Commands, ,Writing the Commands in Rules}.
|
|---|
| 4138 | For example, using this canned sequence:
|
|---|
| 4139 |
|
|---|
| 4140 | @example
|
|---|
| 4141 | define frobnicate
|
|---|
| 4142 | @@echo "frobnicating target $@@"
|
|---|
| 4143 | frob-step-1 $< -o $@@-step-1
|
|---|
| 4144 | frob-step-2 $@@-step-1 -o $@@
|
|---|
| 4145 | endef
|
|---|
| 4146 | @end example
|
|---|
| 4147 |
|
|---|
| 4148 | @noindent
|
|---|
| 4149 | @code{make} will not echo the first line, the @code{echo} command.
|
|---|
| 4150 | But it @emph{will} echo the following two command lines.
|
|---|
| 4151 |
|
|---|
| 4152 | On the other hand, prefix characters on the command line that refers to
|
|---|
| 4153 | a canned sequence apply to every line in the sequence. So the rule:
|
|---|
| 4154 |
|
|---|
| 4155 | @example
|
|---|
| 4156 | frob.out: frob.in
|
|---|
| 4157 | @@$(frobnicate)
|
|---|
| 4158 | @end example
|
|---|
| 4159 |
|
|---|
| 4160 | @noindent
|
|---|
| 4161 | does not echo @emph{any} commands.
|
|---|
| 4162 | (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
|
|---|
| 4163 |
|
|---|
| 4164 | @node Empty Commands, , Sequences, Commands
|
|---|
| 4165 | @section Using Empty Commands
|
|---|
| 4166 | @cindex empty commands
|
|---|
| 4167 | @cindex commands, empty
|
|---|
| 4168 |
|
|---|
| 4169 | It is sometimes useful to define commands which do nothing. This is done
|
|---|
| 4170 | simply by giving a command that consists of nothing but whitespace. For
|
|---|
| 4171 | example:
|
|---|
| 4172 |
|
|---|
| 4173 | @example
|
|---|
| 4174 | target: ;
|
|---|
| 4175 | @end example
|
|---|
| 4176 |
|
|---|
| 4177 | @noindent
|
|---|
| 4178 | defines an empty command string for @file{target}. You could also use a
|
|---|
| 4179 | line beginning with a tab character to define an empty command string,
|
|---|
| 4180 | but this would be confusing because such a line looks empty.
|
|---|
| 4181 |
|
|---|
| 4182 | @findex .DEFAULT@r{, and empty commands}
|
|---|
| 4183 | You may be wondering why you would want to define a command string that
|
|---|
| 4184 | does nothing. The only reason this is useful is to prevent a target
|
|---|
| 4185 | from getting implicit commands (from implicit rules or the
|
|---|
| 4186 | @code{.DEFAULT} special target; @pxref{Implicit Rules} and
|
|---|
| 4187 | @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
|
|---|
| 4188 |
|
|---|
| 4189 | @c !!! another reason is for canonical stamp files:
|
|---|
| 4190 | @ignore
|
|---|
| 4191 | @example
|
|---|
| 4192 | foo: stamp-foo ;
|
|---|
| 4193 | stamp-foo: foo.in
|
|---|
| 4194 | create foo frm foo.in
|
|---|
| 4195 | touch $@
|
|---|
| 4196 | @end example
|
|---|
| 4197 | @end ignore
|
|---|
| 4198 |
|
|---|
| 4199 | You may be inclined to define empty command strings for targets that are
|
|---|
| 4200 | not actual files, but only exist so that their prerequisites can be
|
|---|
| 4201 | remade. However, this is not the best way to do that, because the
|
|---|
| 4202 | prerequisites may not be remade properly if the target file actually does exist.
|
|---|
| 4203 | @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
|
|---|
| 4204 |
|
|---|
| 4205 | @node Using Variables, Conditionals, Commands, Top
|
|---|
| 4206 | @chapter How to Use Variables
|
|---|
| 4207 | @cindex variable
|
|---|
| 4208 | @cindex value
|
|---|
| 4209 | @cindex recursive variable expansion
|
|---|
| 4210 | @cindex simple variable expansion
|
|---|
| 4211 |
|
|---|
| 4212 | A @dfn{variable} is a name defined in a makefile to represent a string
|
|---|
| 4213 | of text, called the variable's @dfn{value}. These values are
|
|---|
| 4214 | substituted by explicit request into targets, prerequisites, commands,
|
|---|
| 4215 | and other parts of the makefile. (In some other versions of @code{make},
|
|---|
| 4216 | variables are called @dfn{macros}.)
|
|---|
| 4217 | @cindex macro
|
|---|
| 4218 |
|
|---|
| 4219 | Variables and functions in all parts of a makefile are expanded when
|
|---|
| 4220 | read, except for the shell commands in rules, the right-hand sides of
|
|---|
| 4221 | variable definitions using @samp{=}, and the bodies of variable
|
|---|
| 4222 | definitions using the @code{define} directive.@refill
|
|---|
| 4223 |
|
|---|
| 4224 | Variables can represent lists of file names, options to pass to compilers,
|
|---|
| 4225 | programs to run, directories to look in for source files, directories to
|
|---|
| 4226 | write output in, or anything else you can imagine.
|
|---|
| 4227 |
|
|---|
| 4228 | A variable name may be any sequence of characters not containing @samp{:},
|
|---|
| 4229 | @samp{#}, @samp{=}, or leading or trailing whitespace. However,
|
|---|
| 4230 | variable names containing characters other than letters, numbers, and
|
|---|
| 4231 | underscores should be avoided, as they may be given special meanings in the
|
|---|
| 4232 | future, and with some shells they cannot be passed through the environment to a
|
|---|
| 4233 | sub-@code{make}
|
|---|
| 4234 | (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
|
|---|
| 4235 |
|
|---|
| 4236 | Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
|
|---|
| 4237 | and @samp{Foo} all refer to different variables.
|
|---|
| 4238 |
|
|---|
| 4239 | It is traditional to use upper case letters in variable names, but we
|
|---|
| 4240 | recommend using lower case letters for variable names that serve internal
|
|---|
| 4241 | purposes in the makefile, and reserving upper case for parameters that
|
|---|
| 4242 | control implicit rules or for parameters that the user should override with
|
|---|
| 4243 | command options (@pxref{Overriding, ,Overriding Variables}).
|
|---|
| 4244 |
|
|---|
| 4245 | A few variables have names that are a single punctuation character or
|
|---|
| 4246 | just a few characters. These are the @dfn{automatic variables}, and
|
|---|
| 4247 | they have particular specialized uses. @xref{Automatic Variables}.
|
|---|
| 4248 |
|
|---|
| 4249 | @menu
|
|---|
| 4250 | * Reference:: How to use the value of a variable.
|
|---|
| 4251 | * Flavors:: Variables come in two flavors.
|
|---|
| 4252 | * Advanced:: Advanced features for referencing a variable.
|
|---|
| 4253 | * Values:: All the ways variables get their values.
|
|---|
| 4254 | * Setting:: How to set a variable in the makefile.
|
|---|
| 4255 | * Appending:: How to append more text to the old value
|
|---|
| 4256 | of a variable.
|
|---|
| 4257 | * Override Directive:: How to set a variable in the makefile even if
|
|---|
| 4258 | the user has set it with a command argument.
|
|---|
| 4259 | * Defining:: An alternate way to set a variable
|
|---|
| 4260 | to a verbatim string.
|
|---|
| 4261 | * Environment:: Variable values can come from the environment.
|
|---|
| 4262 | * Target-specific:: Variable values can be defined on a per-target
|
|---|
| 4263 | basis.
|
|---|
| 4264 | * Pattern-specific:: Target-specific variable values can be applied
|
|---|
| 4265 | to a group of targets that match a pattern.
|
|---|
| 4266 | @end menu
|
|---|
| 4267 |
|
|---|
| 4268 | @node Reference, Flavors, Using Variables, Using Variables
|
|---|
| 4269 | @section Basics of Variable References
|
|---|
| 4270 | @cindex variables, how to reference
|
|---|
| 4271 | @cindex reference to variables
|
|---|
| 4272 | @cindex @code{$}, in variable reference
|
|---|
| 4273 | @cindex dollar sign (@code{$}), in variable reference
|
|---|
| 4274 |
|
|---|
| 4275 | To substitute a variable's value, write a dollar sign followed by the name
|
|---|
| 4276 | of the variable in parentheses or braces: either @samp{$(foo)} or
|
|---|
| 4277 | @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
|
|---|
| 4278 | special significance of @samp{$} is why you must write @samp{$$} to have
|
|---|
| 4279 | the effect of a single dollar sign in a file name or command.
|
|---|
| 4280 |
|
|---|
| 4281 | Variable references can be used in any context: targets, prerequisites,
|
|---|
| 4282 | commands, most directives, and new variable values. Here is an
|
|---|
| 4283 | example of a common case, where a variable holds the names of all the
|
|---|
| 4284 | object files in a program:
|
|---|
| 4285 |
|
|---|
| 4286 | @example
|
|---|
| 4287 | @group
|
|---|
| 4288 | objects = program.o foo.o utils.o
|
|---|
| 4289 | program : $(objects)
|
|---|
| 4290 | cc -o program $(objects)
|
|---|
| 4291 |
|
|---|
| 4292 | $(objects) : defs.h
|
|---|
| 4293 | @end group
|
|---|
| 4294 | @end example
|
|---|
| 4295 |
|
|---|
| 4296 | Variable references work by strict textual substitution. Thus, the rule
|
|---|
| 4297 |
|
|---|
| 4298 | @example
|
|---|
| 4299 | @group
|
|---|
| 4300 | foo = c
|
|---|
| 4301 | prog.o : prog.$(foo)
|
|---|
| 4302 | $(foo)$(foo) -$(foo) prog.$(foo)
|
|---|
| 4303 | @end group
|
|---|
| 4304 | @end example
|
|---|
| 4305 |
|
|---|
| 4306 | @noindent
|
|---|
| 4307 | could be used to compile a C program @file{prog.c}. Since spaces before
|
|---|
| 4308 | the variable value are ignored in variable assignments, the value of
|
|---|
| 4309 | @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
|
|---|
| 4310 | this way!)
|
|---|
| 4311 |
|
|---|
| 4312 | A dollar sign followed by a character other than a dollar sign,
|
|---|
| 4313 | open-parenthesis or open-brace treats that single character as the
|
|---|
| 4314 | variable name. Thus, you could reference the variable @code{x} with
|
|---|
| 4315 | @samp{$x}. However, this practice is strongly discouraged, except in
|
|---|
| 4316 | the case of the automatic variables (@pxref{Automatic Variables}).
|
|---|
| 4317 |
|
|---|
| 4318 | @node Flavors, Advanced, Reference, Using Variables
|
|---|
| 4319 | @section The Two Flavors of Variables
|
|---|
| 4320 | @cindex flavors of variables
|
|---|
| 4321 | @cindex recursive variable expansion
|
|---|
| 4322 | @cindex variables, flavors
|
|---|
| 4323 | @cindex recursively expanded variables
|
|---|
| 4324 | @cindex variables, recursively expanded
|
|---|
| 4325 |
|
|---|
| 4326 | There are two ways that a variable in GNU @code{make} can have a value;
|
|---|
| 4327 | we call them the two @dfn{flavors} of variables. The two flavors are
|
|---|
| 4328 | distinguished in how they are defined and in what they do when expanded.
|
|---|
| 4329 |
|
|---|
| 4330 | @cindex =
|
|---|
| 4331 | The first flavor of variable is a @dfn{recursively expanded} variable.
|
|---|
| 4332 | Variables of this sort are defined by lines using @samp{=}
|
|---|
| 4333 | (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
|
|---|
| 4334 | (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
|
|---|
| 4335 | is installed verbatim; if it contains references to other variables,
|
|---|
| 4336 | these references are expanded whenever this variable is substituted (in
|
|---|
| 4337 | the course of expanding some other string). When this happens, it is
|
|---|
| 4338 | called @dfn{recursive expansion}.@refill
|
|---|
| 4339 |
|
|---|
| 4340 | For example,
|
|---|
| 4341 |
|
|---|
| 4342 | @example
|
|---|
| 4343 | foo = $(bar)
|
|---|
| 4344 | bar = $(ugh)
|
|---|
| 4345 | ugh = Huh?
|
|---|
| 4346 |
|
|---|
| 4347 | all:;echo $(foo)
|
|---|
| 4348 | @end example
|
|---|
| 4349 |
|
|---|
| 4350 | @noindent
|
|---|
| 4351 | will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
|
|---|
| 4352 | expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
|
|---|
| 4353 |
|
|---|
| 4354 | This flavor of variable is the only sort supported by other versions of
|
|---|
| 4355 | @code{make}. It has its advantages and its disadvantages. An advantage
|
|---|
| 4356 | (most would say) is that:
|
|---|
| 4357 |
|
|---|
| 4358 | @example
|
|---|
| 4359 | CFLAGS = $(include_dirs) -O
|
|---|
| 4360 | include_dirs = -Ifoo -Ibar
|
|---|
| 4361 | @end example
|
|---|
| 4362 |
|
|---|
| 4363 | @noindent
|
|---|
| 4364 | will do what was intended: when @samp{CFLAGS} is expanded in a command,
|
|---|
| 4365 | it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
|
|---|
| 4366 | cannot append something on the end of a variable, as in
|
|---|
| 4367 |
|
|---|
| 4368 | @example
|
|---|
| 4369 | CFLAGS = $(CFLAGS) -O
|
|---|
| 4370 | @end example
|
|---|
| 4371 |
|
|---|
| 4372 | @noindent
|
|---|
| 4373 | because it will cause an infinite loop in the variable expansion.
|
|---|
| 4374 | (Actually @code{make} detects the infinite loop and reports an error.)
|
|---|
| 4375 | @cindex loops in variable expansion
|
|---|
| 4376 | @cindex variables, loops in expansion
|
|---|
| 4377 |
|
|---|
| 4378 | Another disadvantage is that any functions
|
|---|
| 4379 | (@pxref{Functions, ,Functions for Transforming Text})
|
|---|
| 4380 | referenced in the definition will be executed every time the variable is
|
|---|
| 4381 | expanded. This makes @code{make} run slower; worse, it causes the
|
|---|
| 4382 | @code{wildcard} and @code{shell} functions to give unpredictable results
|
|---|
| 4383 | because you cannot easily control when they are called, or even how many
|
|---|
| 4384 | times.
|
|---|
| 4385 |
|
|---|
| 4386 | To avoid all the problems and inconveniences of recursively expanded
|
|---|
| 4387 | variables, there is another flavor: simply expanded variables.
|
|---|
| 4388 |
|
|---|
| 4389 | @cindex simply expanded variables
|
|---|
| 4390 | @cindex variables, simply expanded
|
|---|
| 4391 | @cindex :=
|
|---|
| 4392 | @dfn{Simply expanded variables} are defined by lines using @samp{:=}
|
|---|
| 4393 | (@pxref{Setting, ,Setting Variables}).
|
|---|
| 4394 | The value of a simply expanded variable is scanned
|
|---|
| 4395 | once and for all, expanding any references to other variables and
|
|---|
| 4396 | functions, when the variable is defined. The actual value of the simply
|
|---|
| 4397 | expanded variable is the result of expanding the text that you write.
|
|---|
| 4398 | It does not contain any references to other variables; it contains their
|
|---|
| 4399 | values @emph{as of the time this variable was defined}. Therefore,
|
|---|
| 4400 |
|
|---|
| 4401 | @example
|
|---|
| 4402 | x := foo
|
|---|
| 4403 | y := $(x) bar
|
|---|
| 4404 | x := later
|
|---|
| 4405 | @end example
|
|---|
| 4406 |
|
|---|
| 4407 | @noindent
|
|---|
| 4408 | is equivalent to
|
|---|
| 4409 |
|
|---|
| 4410 | @example
|
|---|
| 4411 | y := foo bar
|
|---|
| 4412 | x := later
|
|---|
| 4413 | @end example
|
|---|
| 4414 |
|
|---|
| 4415 | When a simply expanded variable is referenced, its value is substituted
|
|---|
| 4416 | verbatim.
|
|---|
| 4417 |
|
|---|
| 4418 | Here is a somewhat more complicated example, illustrating the use of
|
|---|
| 4419 | @samp{:=} in conjunction with the @code{shell} function.
|
|---|
| 4420 | (@xref{Shell Function, , The @code{shell} Function}.) This example
|
|---|
| 4421 | also shows use of the variable @code{MAKELEVEL}, which is changed
|
|---|
| 4422 | when it is passed down from level to level.
|
|---|
| 4423 | (@xref{Variables/Recursion, , Communicating Variables to a
|
|---|
| 4424 | Sub-@code{make}}, for information about @code{MAKELEVEL}.)
|
|---|
| 4425 |
|
|---|
| 4426 | @vindex MAKELEVEL
|
|---|
| 4427 | @vindex MAKE
|
|---|
| 4428 | @example
|
|---|
| 4429 | @group
|
|---|
| 4430 | ifeq (0,$@{MAKELEVEL@})
|
|---|
| 4431 | cur-dir := $(shell pwd)
|
|---|
| 4432 | whoami := $(shell whoami)
|
|---|
| 4433 | host-type := $(shell arch)
|
|---|
| 4434 | MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
|
|---|
| 4435 | endif
|
|---|
| 4436 | @end group
|
|---|
| 4437 | @end example
|
|---|
| 4438 |
|
|---|
| 4439 | @noindent
|
|---|
| 4440 | An advantage of this use of @samp{:=} is that a typical
|
|---|
| 4441 | `descend into a directory' command then looks like this:
|
|---|
| 4442 |
|
|---|
| 4443 | @example
|
|---|
| 4444 | @group
|
|---|
| 4445 | $@{subdirs@}:
|
|---|
| 4446 | $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
|
|---|
| 4447 | @end group
|
|---|
| 4448 | @end example
|
|---|
| 4449 |
|
|---|
| 4450 | Simply expanded variables generally make complicated makefile programming
|
|---|
| 4451 | more predictable because they work like variables in most programming
|
|---|
| 4452 | languages. They allow you to redefine a variable using its own value (or
|
|---|
| 4453 | its value processed in some way by one of the expansion functions) and to
|
|---|
| 4454 | use the expansion functions much more efficiently
|
|---|
| 4455 | (@pxref{Functions, ,Functions for Transforming Text}).
|
|---|
| 4456 |
|
|---|
| 4457 | @cindex spaces, in variable values
|
|---|
| 4458 | @cindex whitespace, in variable values
|
|---|
| 4459 | @cindex variables, spaces in values
|
|---|
| 4460 | You can also use them to introduce controlled leading whitespace into
|
|---|
| 4461 | variable values. Leading whitespace characters are discarded from your
|
|---|
| 4462 | input before substitution of variable references and function calls;
|
|---|
| 4463 | this means you can include leading spaces in a variable value by
|
|---|
| 4464 | protecting them with variable references, like this:
|
|---|
| 4465 |
|
|---|
| 4466 | @example
|
|---|
| 4467 | nullstring :=
|
|---|
| 4468 | space := $(nullstring) # end of the line
|
|---|
| 4469 | @end example
|
|---|
| 4470 |
|
|---|
| 4471 | @noindent
|
|---|
| 4472 | Here the value of the variable @code{space} is precisely one space. The
|
|---|
| 4473 | comment @w{@samp{# end of the line}} is included here just for clarity.
|
|---|
| 4474 | Since trailing space characters are @emph{not} stripped from variable
|
|---|
| 4475 | values, just a space at the end of the line would have the same effect
|
|---|
| 4476 | (but be rather hard to read). If you put whitespace at the end of a
|
|---|
| 4477 | variable value, it is a good idea to put a comment like that at the end
|
|---|
| 4478 | of the line to make your intent clear. Conversely, if you do @emph{not}
|
|---|
| 4479 | want any whitespace characters at the end of your variable value, you
|
|---|
| 4480 | must remember not to put a random comment on the end of the line after
|
|---|
| 4481 | some whitespace, such as this:
|
|---|
| 4482 |
|
|---|
| 4483 | @example
|
|---|
| 4484 | dir := /foo/bar # directory to put the frobs in
|
|---|
| 4485 | @end example
|
|---|
| 4486 |
|
|---|
| 4487 | @noindent
|
|---|
| 4488 | Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
|
|---|
| 4489 | (with four trailing spaces), which was probably not the intention.
|
|---|
| 4490 | (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
|
|---|
| 4491 |
|
|---|
| 4492 | @cindex conditional variable assignment
|
|---|
| 4493 | @cindex variables, conditional assignment
|
|---|
| 4494 | @cindex ?=
|
|---|
| 4495 | There is another assignment operator for variables, @samp{?=}. This
|
|---|
| 4496 | is called a conditional variable assignment operator, because it only
|
|---|
| 4497 | has an effect if the variable is not yet defined. This statement:
|
|---|
| 4498 |
|
|---|
| 4499 | @example
|
|---|
| 4500 | FOO ?= bar
|
|---|
| 4501 | @end example
|
|---|
| 4502 |
|
|---|
| 4503 | @noindent
|
|---|
| 4504 | is exactly equivalent to this
|
|---|
| 4505 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
|---|
| 4506 |
|
|---|
| 4507 | @example
|
|---|
| 4508 | ifeq ($(origin FOO), undefined)
|
|---|
| 4509 | FOO = bar
|
|---|
| 4510 | endif
|
|---|
| 4511 | @end example
|
|---|
| 4512 |
|
|---|
| 4513 | Note that a variable set to an empty value is still defined, so
|
|---|
| 4514 | @samp{?=} will not set that variable.
|
|---|
| 4515 |
|
|---|
| 4516 | @node Advanced, Values, Flavors, Using Variables
|
|---|
| 4517 | @section Advanced Features for Reference to Variables
|
|---|
| 4518 | @cindex reference to variables
|
|---|
| 4519 |
|
|---|
| 4520 | This section describes some advanced features you can use to reference
|
|---|
| 4521 | variables in more flexible ways.
|
|---|
| 4522 |
|
|---|
| 4523 | @menu
|
|---|
| 4524 | * Substitution Refs:: Referencing a variable with
|
|---|
| 4525 | substitutions on the value.
|
|---|
| 4526 | * Computed Names:: Computing the name of the variable to refer to.
|
|---|
| 4527 | @end menu
|
|---|
| 4528 |
|
|---|
| 4529 | @node Substitution Refs, Computed Names, Advanced, Advanced
|
|---|
| 4530 | @subsection Substitution References
|
|---|
| 4531 | @cindex modified variable reference
|
|---|
| 4532 | @cindex substitution variable reference
|
|---|
| 4533 | @cindex variables, modified reference
|
|---|
| 4534 | @cindex variables, substitution reference
|
|---|
| 4535 |
|
|---|
| 4536 | @cindex variables, substituting suffix in
|
|---|
| 4537 | @cindex suffix, substituting in variables
|
|---|
| 4538 | A @dfn{substitution reference} substitutes the value of a variable with
|
|---|
| 4539 | alterations that you specify. It has the form
|
|---|
| 4540 | @samp{$(@var{var}:@var{a}=@var{b})} (or
|
|---|
| 4541 | @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
|
|---|
| 4542 | of the variable @var{var}, replace every @var{a} at the end of a word with
|
|---|
| 4543 | @var{b} in that value, and substitute the resulting string.
|
|---|
| 4544 |
|
|---|
| 4545 | When we say ``at the end of a word'', we mean that @var{a} must appear
|
|---|
| 4546 | either followed by whitespace or at the end of the value in order to be
|
|---|
| 4547 | replaced; other occurrences of @var{a} in the value are unaltered. For
|
|---|
| 4548 | example:@refill
|
|---|
| 4549 |
|
|---|
| 4550 | @example
|
|---|
| 4551 | foo := a.o b.o c.o
|
|---|
| 4552 | bar := $(foo:.o=.c)
|
|---|
| 4553 | @end example
|
|---|
| 4554 |
|
|---|
| 4555 | @noindent
|
|---|
| 4556 | sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
|
|---|
| 4557 |
|
|---|
| 4558 | A substitution reference is actually an abbreviation for use of the
|
|---|
| 4559 | @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
|
|---|
| 4560 | substitution references as well as @code{patsubst} for compatibility with
|
|---|
| 4561 | other implementations of @code{make}.
|
|---|
| 4562 |
|
|---|
| 4563 | @findex patsubst
|
|---|
| 4564 | Another type of substitution reference lets you use the full power of
|
|---|
| 4565 | the @code{patsubst} function. It has the same form
|
|---|
| 4566 | @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
|
|---|
| 4567 | @var{a} must contain a single @samp{%} character. This case is
|
|---|
| 4568 | equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
|
|---|
| 4569 | @xref{Text Functions, ,Functions for String Substitution and Analysis},
|
|---|
| 4570 | for a description of the @code{patsubst} function.@refill
|
|---|
| 4571 |
|
|---|
| 4572 | @example
|
|---|
| 4573 | @group
|
|---|
| 4574 | @exdent For example:
|
|---|
| 4575 |
|
|---|
| 4576 | foo := a.o b.o c.o
|
|---|
| 4577 | bar := $(foo:%.o=%.c)
|
|---|
| 4578 | @end group
|
|---|
| 4579 | @end example
|
|---|
| 4580 |
|
|---|
| 4581 | @noindent
|
|---|
| 4582 | sets @samp{bar} to @samp{a.c b.c c.c}.
|
|---|
| 4583 |
|
|---|
| 4584 | @node Computed Names, , Substitution Refs, Advanced
|
|---|
| 4585 | @subsection Computed Variable Names
|
|---|
| 4586 | @cindex nested variable reference
|
|---|
| 4587 | @cindex computed variable name
|
|---|
| 4588 | @cindex variables, computed names
|
|---|
| 4589 | @cindex variables, nested references
|
|---|
| 4590 | @cindex variables, @samp{$} in name
|
|---|
| 4591 | @cindex @code{$}, in variable name
|
|---|
| 4592 | @cindex dollar sign (@code{$}), in variable name
|
|---|
| 4593 |
|
|---|
| 4594 | Computed variable names are a complicated concept needed only for
|
|---|
| 4595 | sophisticated makefile programming. For most purposes you need not
|
|---|
| 4596 | consider them, except to know that making a variable with a dollar sign
|
|---|
| 4597 | in its name might have strange results. However, if you are the type
|
|---|
| 4598 | that wants to understand everything, or you are actually interested in
|
|---|
| 4599 | what they do, read on.
|
|---|
| 4600 |
|
|---|
| 4601 | Variables may be referenced inside the name of a variable. This is
|
|---|
| 4602 | called a @dfn{computed variable name} or a @dfn{nested variable
|
|---|
| 4603 | reference}. For example,
|
|---|
| 4604 |
|
|---|
| 4605 | @example
|
|---|
| 4606 | x = y
|
|---|
| 4607 | y = z
|
|---|
| 4608 | a := $($(x))
|
|---|
| 4609 | @end example
|
|---|
| 4610 |
|
|---|
| 4611 | @noindent
|
|---|
| 4612 | defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
|
|---|
| 4613 | to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
|
|---|
| 4614 | to @samp{z}. Here the name of the variable to reference is not stated
|
|---|
| 4615 | explicitly; it is computed by expansion of @samp{$(x)}. The reference
|
|---|
| 4616 | @samp{$(x)} here is nested within the outer variable reference.
|
|---|
| 4617 |
|
|---|
| 4618 | The previous example shows two levels of nesting, but any number of levels
|
|---|
| 4619 | is possible. For example, here are three levels:
|
|---|
| 4620 |
|
|---|
| 4621 | @example
|
|---|
| 4622 | x = y
|
|---|
| 4623 | y = z
|
|---|
| 4624 | z = u
|
|---|
| 4625 | a := $($($(x)))
|
|---|
| 4626 | @end example
|
|---|
| 4627 |
|
|---|
| 4628 | @noindent
|
|---|
| 4629 | Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
|
|---|
| 4630 | expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
|
|---|
| 4631 | @samp{$(z)}, which becomes @samp{u}.
|
|---|
| 4632 |
|
|---|
| 4633 | References to recursively-expanded variables within a variable name are
|
|---|
| 4634 | reexpanded in the usual fashion. For example:
|
|---|
| 4635 |
|
|---|
| 4636 | @example
|
|---|
| 4637 | x = $(y)
|
|---|
| 4638 | y = z
|
|---|
| 4639 | z = Hello
|
|---|
| 4640 | a := $($(x))
|
|---|
| 4641 | @end example
|
|---|
| 4642 |
|
|---|
| 4643 | @noindent
|
|---|
| 4644 | defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
|
|---|
| 4645 | which becomes @samp{$(z)} which becomes @samp{Hello}.
|
|---|
| 4646 |
|
|---|
| 4647 | Nested variable references can also contain modified references and
|
|---|
| 4648 | function invocations (@pxref{Functions, ,Functions for Transforming Text}),
|
|---|
| 4649 | just like any other reference.
|
|---|
| 4650 | For example, using the @code{subst} function
|
|---|
| 4651 | (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
|
|---|
| 4652 |
|
|---|
| 4653 | @example
|
|---|
| 4654 | @group
|
|---|
| 4655 | x = variable1
|
|---|
| 4656 | variable2 := Hello
|
|---|
| 4657 | y = $(subst 1,2,$(x))
|
|---|
| 4658 | z = y
|
|---|
| 4659 | a := $($($(z)))
|
|---|
| 4660 | @end group
|
|---|
| 4661 | @end example
|
|---|
| 4662 |
|
|---|
| 4663 | @noindent
|
|---|
| 4664 | eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
|
|---|
| 4665 | would ever want to write a nested reference as convoluted as this one, but
|
|---|
| 4666 | it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
|
|---|
| 4667 | @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
|
|---|
| 4668 | @code{x} and changes it by substitution to @samp{variable2}, so that the
|
|---|
| 4669 | entire string becomes @samp{$(variable2)}, a simple variable reference
|
|---|
| 4670 | whose value is @samp{Hello}.@refill
|
|---|
| 4671 |
|
|---|
| 4672 | A computed variable name need not consist entirely of a single variable
|
|---|
| 4673 | reference. It can contain several variable references, as well as some
|
|---|
| 4674 | invariant text. For example,
|
|---|
| 4675 |
|
|---|
| 4676 | @example
|
|---|
| 4677 | @group
|
|---|
| 4678 | a_dirs := dira dirb
|
|---|
| 4679 | 1_dirs := dir1 dir2
|
|---|
| 4680 | @end group
|
|---|
| 4681 |
|
|---|
| 4682 | @group
|
|---|
| 4683 | a_files := filea fileb
|
|---|
| 4684 | 1_files := file1 file2
|
|---|
| 4685 | @end group
|
|---|
| 4686 |
|
|---|
| 4687 | @group
|
|---|
| 4688 | ifeq "$(use_a)" "yes"
|
|---|
| 4689 | a1 := a
|
|---|
| 4690 | else
|
|---|
| 4691 | a1 := 1
|
|---|
| 4692 | endif
|
|---|
| 4693 | @end group
|
|---|
| 4694 |
|
|---|
| 4695 | @group
|
|---|
| 4696 | ifeq "$(use_dirs)" "yes"
|
|---|
| 4697 | df := dirs
|
|---|
| 4698 | else
|
|---|
| 4699 | df := files
|
|---|
| 4700 | endif
|
|---|
| 4701 |
|
|---|
| 4702 | dirs := $($(a1)_$(df))
|
|---|
| 4703 | @end group
|
|---|
| 4704 | @end example
|
|---|
| 4705 |
|
|---|
| 4706 | @noindent
|
|---|
| 4707 | will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
|
|---|
| 4708 | @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
|
|---|
| 4709 | and @code{use_dirs}.@refill
|
|---|
| 4710 |
|
|---|
| 4711 | Computed variable names can also be used in substitution references:
|
|---|
| 4712 |
|
|---|
| 4713 | @example
|
|---|
| 4714 | @group
|
|---|
| 4715 | a_objects := a.o b.o c.o
|
|---|
| 4716 | 1_objects := 1.o 2.o 3.o
|
|---|
| 4717 |
|
|---|
| 4718 | sources := $($(a1)_objects:.o=.c)
|
|---|
| 4719 | @end group
|
|---|
| 4720 | @end example
|
|---|
| 4721 |
|
|---|
| 4722 | @noindent
|
|---|
| 4723 | defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
|
|---|
| 4724 | depending on the value of @code{a1}.
|
|---|
| 4725 |
|
|---|
| 4726 | The only restriction on this sort of use of nested variable references
|
|---|
| 4727 | is that they cannot specify part of the name of a function to be called.
|
|---|
| 4728 | This is because the test for a recognized function name is done before
|
|---|
| 4729 | the expansion of nested references. For example,
|
|---|
| 4730 |
|
|---|
| 4731 | @example
|
|---|
| 4732 | @group
|
|---|
| 4733 | ifdef do_sort
|
|---|
| 4734 | func := sort
|
|---|
| 4735 | else
|
|---|
| 4736 | func := strip
|
|---|
| 4737 | endif
|
|---|
| 4738 | @end group
|
|---|
| 4739 |
|
|---|
| 4740 | @group
|
|---|
| 4741 | bar := a d b g q c
|
|---|
| 4742 | @end group
|
|---|
| 4743 |
|
|---|
| 4744 | @group
|
|---|
| 4745 | foo := $($(func) $(bar))
|
|---|
| 4746 | @end group
|
|---|
| 4747 | @end example
|
|---|
| 4748 |
|
|---|
| 4749 | @noindent
|
|---|
| 4750 | attempts to give @samp{foo} the value of the variable @samp{sort a d b g
|
|---|
| 4751 | q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
|
|---|
| 4752 | as the argument to either the @code{sort} or the @code{strip} function.
|
|---|
| 4753 | This restriction could be removed in the future if that change is shown
|
|---|
| 4754 | to be a good idea.
|
|---|
| 4755 |
|
|---|
| 4756 | You can also use computed variable names in the left-hand side of a
|
|---|
| 4757 | variable assignment, or in a @code{define} directive, as in:
|
|---|
| 4758 |
|
|---|
| 4759 | @example
|
|---|
| 4760 | dir = foo
|
|---|
| 4761 | $(dir)_sources := $(wildcard $(dir)/*.c)
|
|---|
| 4762 | define $(dir)_print
|
|---|
| 4763 | lpr $($(dir)_sources)
|
|---|
| 4764 | endef
|
|---|
| 4765 | @end example
|
|---|
| 4766 |
|
|---|
| 4767 | @noindent
|
|---|
| 4768 | This example defines the variables @samp{dir}, @samp{foo_sources}, and
|
|---|
| 4769 | @samp{foo_print}.
|
|---|
| 4770 |
|
|---|
| 4771 | Note that @dfn{nested variable references} are quite different from
|
|---|
| 4772 | @dfn{recursively expanded variables}
|
|---|
| 4773 | (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
|
|---|
| 4774 | used together in complex ways when doing makefile programming.@refill
|
|---|
| 4775 |
|
|---|
| 4776 | @node Values, Setting, Advanced, Using Variables
|
|---|
| 4777 | @section How Variables Get Their Values
|
|---|
| 4778 | @cindex variables, how they get their values
|
|---|
| 4779 | @cindex value, how a variable gets it
|
|---|
| 4780 |
|
|---|
| 4781 | Variables can get values in several different ways:
|
|---|
| 4782 |
|
|---|
| 4783 | @itemize @bullet
|
|---|
| 4784 | @item
|
|---|
| 4785 | You can specify an overriding value when you run @code{make}.
|
|---|
| 4786 | @xref{Overriding, ,Overriding Variables}.
|
|---|
| 4787 |
|
|---|
| 4788 | @item
|
|---|
| 4789 | You can specify a value in the makefile, either
|
|---|
| 4790 | with an assignment (@pxref{Setting, ,Setting Variables}) or with a
|
|---|
| 4791 | verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
|
|---|
| 4792 |
|
|---|
| 4793 | @item
|
|---|
| 4794 | Variables in the environment become @code{make} variables.
|
|---|
| 4795 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 4796 |
|
|---|
| 4797 | @item
|
|---|
| 4798 | Several @dfn{automatic} variables are given new values for each rule.
|
|---|
| 4799 | Each of these has a single conventional use.
|
|---|
| 4800 | @xref{Automatic Variables}.
|
|---|
| 4801 |
|
|---|
| 4802 | @item
|
|---|
| 4803 | Several variables have constant initial values.
|
|---|
| 4804 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 4805 | @end itemize
|
|---|
| 4806 |
|
|---|
| 4807 | @node Setting, Appending, Values, Using Variables
|
|---|
| 4808 | @section Setting Variables
|
|---|
| 4809 | @cindex setting variables
|
|---|
| 4810 | @cindex variables, setting
|
|---|
| 4811 | @cindex =
|
|---|
| 4812 | @cindex :=
|
|---|
| 4813 | @cindex ?=
|
|---|
| 4814 |
|
|---|
| 4815 | To set a variable from the makefile, write a line starting with the
|
|---|
| 4816 | variable name followed by @samp{=} or @samp{:=}. Whatever follows the
|
|---|
| 4817 | @samp{=} or @samp{:=} on the line becomes the value. For example,
|
|---|
| 4818 |
|
|---|
| 4819 | @example
|
|---|
| 4820 | objects = main.o foo.o bar.o utils.o
|
|---|
| 4821 | @end example
|
|---|
| 4822 |
|
|---|
| 4823 | @noindent
|
|---|
| 4824 | defines a variable named @code{objects}. Whitespace around the variable
|
|---|
| 4825 | name and immediately after the @samp{=} is ignored.
|
|---|
| 4826 |
|
|---|
| 4827 | Variables defined with @samp{=} are @dfn{recursively expanded} variables.
|
|---|
| 4828 | Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
|
|---|
| 4829 | definitions can contain variable references which will be expanded before
|
|---|
| 4830 | the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
|
|---|
| 4831 |
|
|---|
| 4832 | The variable name may contain function and variable references, which
|
|---|
| 4833 | are expanded when the line is read to find the actual variable name to use.
|
|---|
| 4834 |
|
|---|
| 4835 | There is no limit on the length of the value of a variable except the
|
|---|
| 4836 | amount of swapping space on the computer. When a variable definition is
|
|---|
| 4837 | long, it is a good idea to break it into several lines by inserting
|
|---|
| 4838 | backslash-newline at convenient places in the definition. This will not
|
|---|
| 4839 | affect the functioning of @code{make}, but it will make the makefile easier
|
|---|
| 4840 | to read.
|
|---|
| 4841 |
|
|---|
| 4842 | Most variable names are considered to have the empty string as a value if
|
|---|
| 4843 | you have never set them. Several variables have built-in initial values
|
|---|
| 4844 | that are not empty, but you can set them in the usual ways
|
|---|
| 4845 | (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
|
|---|
| 4846 | Several special variables are set
|
|---|
| 4847 | automatically to a new value for each rule; these are called the
|
|---|
| 4848 | @dfn{automatic} variables (@pxref{Automatic Variables}).
|
|---|
| 4849 |
|
|---|
| 4850 | If you'd like a variable to be set to a value only if it's not already
|
|---|
| 4851 | set, then you can use the shorthand operator @samp{?=} instead of
|
|---|
| 4852 | @samp{=}. These two settings of the variable @samp{FOO} are identical
|
|---|
| 4853 | (@pxref{Origin Function, ,The @code{origin} Function}):
|
|---|
| 4854 |
|
|---|
| 4855 | @example
|
|---|
| 4856 | FOO ?= bar
|
|---|
| 4857 | @end example
|
|---|
| 4858 |
|
|---|
| 4859 | @noindent
|
|---|
| 4860 | and
|
|---|
| 4861 |
|
|---|
| 4862 | @example
|
|---|
| 4863 | ifeq ($(origin FOO), undefined)
|
|---|
| 4864 | FOO = bar
|
|---|
| 4865 | endif
|
|---|
| 4866 | @end example
|
|---|
| 4867 |
|
|---|
| 4868 | @node Appending, Override Directive, Setting, Using Variables
|
|---|
| 4869 | @section Appending More Text to Variables
|
|---|
| 4870 | @cindex +=
|
|---|
| 4871 | @cindex appending to variables
|
|---|
| 4872 | @cindex variables, appending to
|
|---|
| 4873 |
|
|---|
| 4874 | Often it is useful to add more text to the value of a variable already defined.
|
|---|
| 4875 | You do this with a line containing @samp{+=}, like this:
|
|---|
| 4876 |
|
|---|
| 4877 | @example
|
|---|
| 4878 | objects += another.o
|
|---|
| 4879 | @end example
|
|---|
| 4880 |
|
|---|
| 4881 | @noindent
|
|---|
| 4882 | This takes the value of the variable @code{objects}, and adds the text
|
|---|
| 4883 | @samp{another.o} to it (preceded by a single space). Thus:
|
|---|
| 4884 |
|
|---|
| 4885 | @example
|
|---|
| 4886 | objects = main.o foo.o bar.o utils.o
|
|---|
| 4887 | objects += another.o
|
|---|
| 4888 | @end example
|
|---|
| 4889 |
|
|---|
| 4890 | @noindent
|
|---|
| 4891 | sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
|
|---|
| 4892 |
|
|---|
| 4893 | Using @samp{+=} is similar to:
|
|---|
| 4894 |
|
|---|
| 4895 | @example
|
|---|
| 4896 | objects = main.o foo.o bar.o utils.o
|
|---|
| 4897 | objects := $(objects) another.o
|
|---|
| 4898 | @end example
|
|---|
| 4899 |
|
|---|
| 4900 | @noindent
|
|---|
| 4901 | but differs in ways that become important when you use more complex values.
|
|---|
| 4902 |
|
|---|
| 4903 | When the variable in question has not been defined before, @samp{+=}
|
|---|
| 4904 | acts just like normal @samp{=}: it defines a recursively-expanded
|
|---|
| 4905 | variable. However, when there @emph{is} a previous definition, exactly
|
|---|
| 4906 | what @samp{+=} does depends on what flavor of variable you defined
|
|---|
| 4907 | originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
|
|---|
| 4908 | explanation of the two flavors of variables.
|
|---|
| 4909 |
|
|---|
| 4910 | When you add to a variable's value with @samp{+=}, @code{make} acts
|
|---|
| 4911 | essentially as if you had included the extra text in the initial
|
|---|
| 4912 | definition of the variable. If you defined it first with @samp{:=},
|
|---|
| 4913 | making it a simply-expanded variable, @samp{+=} adds to that
|
|---|
| 4914 | simply-expanded definition, and expands the new text before appending it
|
|---|
| 4915 | to the old value just as @samp{:=} does
|
|---|
| 4916 | (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
|
|---|
| 4917 | In fact,
|
|---|
| 4918 |
|
|---|
| 4919 | @example
|
|---|
| 4920 | variable := value
|
|---|
| 4921 | variable += more
|
|---|
| 4922 | @end example
|
|---|
| 4923 |
|
|---|
| 4924 | @noindent
|
|---|
| 4925 | is exactly equivalent to:
|
|---|
| 4926 |
|
|---|
| 4927 | @noindent
|
|---|
| 4928 | @example
|
|---|
| 4929 | variable := value
|
|---|
| 4930 | variable := $(variable) more
|
|---|
| 4931 | @end example
|
|---|
| 4932 |
|
|---|
| 4933 | On the other hand, when you use @samp{+=} with a variable that you defined
|
|---|
| 4934 | first to be recursively-expanded using plain @samp{=}, @code{make} does
|
|---|
| 4935 | something a bit different. Recall that when you define a
|
|---|
| 4936 | recursively-expanded variable, @code{make} does not expand the value you set
|
|---|
| 4937 | for variable and function references immediately. Instead it stores the text
|
|---|
| 4938 | verbatim, and saves these variable and function references to be expanded
|
|---|
| 4939 | later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
|
|---|
| 4940 | of Variables}). When you use @samp{+=} on a recursively-expanded variable,
|
|---|
| 4941 | it is this unexpanded text to which @code{make} appends the new text you
|
|---|
| 4942 | specify.
|
|---|
| 4943 |
|
|---|
| 4944 | @example
|
|---|
| 4945 | @group
|
|---|
| 4946 | variable = value
|
|---|
| 4947 | variable += more
|
|---|
| 4948 | @end group
|
|---|
| 4949 | @end example
|
|---|
| 4950 |
|
|---|
| 4951 | @noindent
|
|---|
| 4952 | is roughly equivalent to:
|
|---|
| 4953 |
|
|---|
| 4954 | @example
|
|---|
| 4955 | @group
|
|---|
| 4956 | temp = value
|
|---|
| 4957 | variable = $(temp) more
|
|---|
| 4958 | @end group
|
|---|
| 4959 | @end example
|
|---|
| 4960 |
|
|---|
| 4961 | @noindent
|
|---|
| 4962 | except that of course it never defines a variable called @code{temp}.
|
|---|
| 4963 | The importance of this comes when the variable's old value contains
|
|---|
| 4964 | variable references. Take this common example:
|
|---|
| 4965 |
|
|---|
| 4966 | @example
|
|---|
| 4967 | CFLAGS = $(includes) -O
|
|---|
| 4968 | @dots{}
|
|---|
| 4969 | CFLAGS += -pg # enable profiling
|
|---|
| 4970 | @end example
|
|---|
| 4971 |
|
|---|
| 4972 | @noindent
|
|---|
| 4973 | The first line defines the @code{CFLAGS} variable with a reference to another
|
|---|
| 4974 | variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
|
|---|
| 4975 | compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
|
|---|
| 4976 | Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
|
|---|
| 4977 | variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
|
|---|
| 4978 | @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
|
|---|
| 4979 | need not be defined yet for its value to take effect. It only has to be
|
|---|
| 4980 | defined before any reference to @code{CFLAGS}. If we tried to append to the
|
|---|
| 4981 | value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
|
|---|
| 4982 |
|
|---|
| 4983 | @example
|
|---|
| 4984 | CFLAGS := $(CFLAGS) -pg # enable profiling
|
|---|
| 4985 | @end example
|
|---|
| 4986 |
|
|---|
| 4987 | @noindent
|
|---|
| 4988 | This is pretty close, but not quite what we want. Using @samp{:=}
|
|---|
| 4989 | redefines @code{CFLAGS} as a simply-expanded variable; this means
|
|---|
| 4990 | @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
|
|---|
| 4991 | variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
|
|---|
| 4992 | -pg}}, and a later definition of @code{includes} will have no effect.
|
|---|
| 4993 | Conversely, by using @samp{+=} we set @code{CFLAGS} to the
|
|---|
| 4994 | @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
|
|---|
| 4995 | the reference to @code{includes}, so if that variable gets defined at
|
|---|
| 4996 | any later point, a reference like @samp{$(CFLAGS)} still uses its
|
|---|
| 4997 | value.
|
|---|
| 4998 |
|
|---|
| 4999 | @node Override Directive, Defining, Appending, Using Variables
|
|---|
| 5000 | @section The @code{override} Directive
|
|---|
| 5001 | @findex override
|
|---|
| 5002 | @cindex overriding with @code{override}
|
|---|
| 5003 | @cindex variables, overriding
|
|---|
| 5004 |
|
|---|
| 5005 | If a variable has been set with a command argument
|
|---|
| 5006 | (@pxref{Overriding, ,Overriding Variables}),
|
|---|
| 5007 | then ordinary assignments in the makefile are ignored. If you want to set
|
|---|
| 5008 | the variable in the makefile even though it was set with a command
|
|---|
| 5009 | argument, you can use an @code{override} directive, which is a line that
|
|---|
| 5010 | looks like this:@refill
|
|---|
| 5011 |
|
|---|
| 5012 | @example
|
|---|
| 5013 | override @var{variable} = @var{value}
|
|---|
| 5014 | @end example
|
|---|
| 5015 |
|
|---|
| 5016 | @noindent
|
|---|
| 5017 | or
|
|---|
| 5018 |
|
|---|
| 5019 | @example
|
|---|
| 5020 | override @var{variable} := @var{value}
|
|---|
| 5021 | @end example
|
|---|
| 5022 |
|
|---|
| 5023 | To append more text to a variable defined on the command line, use:
|
|---|
| 5024 |
|
|---|
| 5025 | @example
|
|---|
| 5026 | override @var{variable} += @var{more text}
|
|---|
| 5027 | @end example
|
|---|
| 5028 |
|
|---|
| 5029 | @noindent
|
|---|
| 5030 | @xref{Appending, ,Appending More Text to Variables}.
|
|---|
| 5031 |
|
|---|
| 5032 | The @code{override} directive was not invented for escalation in the war
|
|---|
| 5033 | between makefiles and command arguments. It was invented so you can alter
|
|---|
| 5034 | and add to values that the user specifies with command arguments.
|
|---|
| 5035 |
|
|---|
| 5036 | For example, suppose you always want the @samp{-g} switch when you run the
|
|---|
| 5037 | C compiler, but you would like to allow the user to specify the other
|
|---|
| 5038 | switches with a command argument just as usual. You could use this
|
|---|
| 5039 | @code{override} directive:
|
|---|
| 5040 |
|
|---|
| 5041 | @example
|
|---|
| 5042 | override CFLAGS += -g
|
|---|
| 5043 | @end example
|
|---|
| 5044 |
|
|---|
| 5045 | You can also use @code{override} directives with @code{define} directives.
|
|---|
| 5046 | This is done as you might expect:
|
|---|
| 5047 |
|
|---|
| 5048 | @example
|
|---|
| 5049 | override define foo
|
|---|
| 5050 | bar
|
|---|
| 5051 | endef
|
|---|
| 5052 | @end example
|
|---|
| 5053 |
|
|---|
| 5054 | @noindent
|
|---|
| 5055 | @iftex
|
|---|
| 5056 | See the next section for information about @code{define}.
|
|---|
| 5057 | @end iftex
|
|---|
| 5058 | @ifnottex
|
|---|
| 5059 | @xref{Defining, ,Defining Variables Verbatim}.
|
|---|
| 5060 | @end ifnottex
|
|---|
| 5061 |
|
|---|
| 5062 | @node Defining, Environment, Override Directive, Using Variables
|
|---|
| 5063 | @section Defining Variables Verbatim
|
|---|
| 5064 | @findex define
|
|---|
| 5065 | @findex endef
|
|---|
| 5066 | @cindex verbatim variable definition
|
|---|
| 5067 | @cindex defining variables verbatim
|
|---|
| 5068 | @cindex variables, defining verbatim
|
|---|
| 5069 |
|
|---|
| 5070 | Another way to set the value of a variable is to use the @code{define}
|
|---|
| 5071 | directive. This directive has an unusual syntax which allows newline
|
|---|
| 5072 | characters to be included in the value, which is convenient for defining
|
|---|
| 5073 | both canned sequences of commands
|
|---|
| 5074 | (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
|
|---|
| 5075 | sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
|
|---|
| 5076 |
|
|---|
| 5077 | The @code{define} directive is followed on the same line by the name of the
|
|---|
| 5078 | variable and nothing more. The value to give the variable appears on the
|
|---|
| 5079 | following lines. The end of the value is marked by a line containing just
|
|---|
| 5080 | the word @code{endef}. Aside from this difference in syntax, @code{define}
|
|---|
| 5081 | works just like @samp{=}: it creates a recursively-expanded variable
|
|---|
| 5082 | (@pxref{Flavors, ,The Two Flavors of Variables}).
|
|---|
| 5083 | The variable name may contain function and variable references, which
|
|---|
| 5084 | are expanded when the directive is read to find the actual variable name
|
|---|
| 5085 | to use.
|
|---|
| 5086 |
|
|---|
| 5087 | You may nest @code{define} directives: @code{make} will keep track of
|
|---|
| 5088 | nested directives and report an error if they are not all properly
|
|---|
| 5089 | closed with @code{endef}. Note that lines beginning with tab
|
|---|
| 5090 | characters are considered part of a command script, so any
|
|---|
| 5091 | @code{define} or @code{endef} strings appearing on such a line will
|
|---|
| 5092 | not be considered @code{make} operators.
|
|---|
| 5093 |
|
|---|
| 5094 | @example
|
|---|
| 5095 | define two-lines
|
|---|
| 5096 | echo foo
|
|---|
| 5097 | echo $(bar)
|
|---|
| 5098 | endef
|
|---|
| 5099 | @end example
|
|---|
| 5100 |
|
|---|
| 5101 | The value in an ordinary assignment cannot contain a newline; but the
|
|---|
| 5102 | newlines that separate the lines of the value in a @code{define} become
|
|---|
| 5103 | part of the variable's value (except for the final newline which precedes
|
|---|
| 5104 | the @code{endef} and is not considered part of the value).@refill
|
|---|
| 5105 |
|
|---|
| 5106 | @need 800
|
|---|
| 5107 | When used in a command script, the previous example is functionally
|
|---|
| 5108 | equivalent to this:
|
|---|
| 5109 |
|
|---|
| 5110 | @example
|
|---|
| 5111 | two-lines = echo foo; echo $(bar)
|
|---|
| 5112 | @end example
|
|---|
| 5113 |
|
|---|
| 5114 | @noindent
|
|---|
| 5115 | since two commands separated by semicolon behave much like two separate
|
|---|
| 5116 | shell commands. However, note that using two separate lines means
|
|---|
| 5117 | @code{make} will invoke the shell twice, running an independent subshell
|
|---|
| 5118 | for each line. @xref{Execution, ,Command Execution}.
|
|---|
| 5119 |
|
|---|
| 5120 | If you want variable definitions made with @code{define} to take
|
|---|
| 5121 | precedence over command-line variable definitions, you can use the
|
|---|
| 5122 | @code{override} directive together with @code{define}:
|
|---|
| 5123 |
|
|---|
| 5124 | @example
|
|---|
| 5125 | override define two-lines
|
|---|
| 5126 | foo
|
|---|
| 5127 | $(bar)
|
|---|
| 5128 | endef
|
|---|
| 5129 | @end example
|
|---|
| 5130 |
|
|---|
| 5131 | @noindent
|
|---|
| 5132 | @xref{Override Directive, ,The @code{override} Directive}.
|
|---|
| 5133 |
|
|---|
| 5134 | @node Environment, Target-specific, Defining, Using Variables
|
|---|
| 5135 | @section Variables from the Environment
|
|---|
| 5136 |
|
|---|
| 5137 | @cindex variables, environment
|
|---|
| 5138 | @cindex environment
|
|---|
| 5139 | Variables in @code{make} can come from the environment in which
|
|---|
| 5140 | @code{make} is run. Every environment variable that @code{make} sees when
|
|---|
| 5141 | it starts up is transformed into a @code{make} variable with the same name
|
|---|
| 5142 | and value. But an explicit assignment in the makefile, or with a command
|
|---|
| 5143 | argument, overrides the environment. (If the @samp{-e} flag is specified,
|
|---|
| 5144 | then values from the environment override assignments in the makefile.
|
|---|
| 5145 | @xref{Options Summary, ,Summary of Options}.
|
|---|
| 5146 | But this is not recommended practice.)
|
|---|
| 5147 |
|
|---|
| 5148 | Thus, by setting the variable @code{CFLAGS} in your environment, you can
|
|---|
| 5149 | cause all C compilations in most makefiles to use the compiler switches you
|
|---|
| 5150 | prefer. This is safe for variables with standard or conventional meanings
|
|---|
| 5151 | because you know that no makefile will use them for other things. (But
|
|---|
| 5152 | this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
|
|---|
| 5153 | and therefore are not affected by the value in the environment.)
|
|---|
| 5154 |
|
|---|
| 5155 | When @code{make} is invoked recursively, variables defined in the
|
|---|
| 5156 | outer invocation can be passed to inner invocations through the
|
|---|
| 5157 | environment (@pxref{Recursion, ,Recursive Use of @code{make}}). By
|
|---|
| 5158 | default, only variables that came from the environment or the command
|
|---|
| 5159 | line are passed to recursive invocations. You can use the
|
|---|
| 5160 | @code{export} directive to pass other variables.
|
|---|
| 5161 | @xref{Variables/Recursion, , Communicating Variables to a
|
|---|
| 5162 | Sub-@code{make}}, for full details.
|
|---|
| 5163 |
|
|---|
| 5164 | Other use of variables from the environment is not recommended. It is not
|
|---|
| 5165 | wise for makefiles to depend for their functioning on environment variables
|
|---|
| 5166 | set up outside their control, since this would cause different users to get
|
|---|
| 5167 | different results from the same makefile. This is against the whole
|
|---|
| 5168 | purpose of most makefiles.
|
|---|
| 5169 |
|
|---|
| 5170 | Such problems would be especially likely with the variable @code{SHELL},
|
|---|
| 5171 | which is normally present in the environment to specify the user's choice
|
|---|
| 5172 | of interactive shell. It would be very undesirable for this choice to
|
|---|
| 5173 | affect @code{make}. So @code{make} ignores the environment value of
|
|---|
| 5174 | @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
|
|---|
| 5175 | usually not set. @xref{Execution, ,Special handling of SHELL on
|
|---|
| 5176 | MS-DOS}.)@refill
|
|---|
| 5177 |
|
|---|
| 5178 | @node Target-specific, Pattern-specific, Environment, Using Variables
|
|---|
| 5179 | @section Target-specific Variable Values
|
|---|
| 5180 | @cindex target-specific variables
|
|---|
| 5181 | @cindex variables, target-specific
|
|---|
| 5182 |
|
|---|
| 5183 | Variable values in @code{make} are usually global; that is, they are the
|
|---|
| 5184 | same regardless of where they are evaluated (unless they're reset, of
|
|---|
| 5185 | course). One exception to that is automatic variables
|
|---|
| 5186 | (@pxref{Automatic Variables}).
|
|---|
| 5187 |
|
|---|
| 5188 | The other exception is @dfn{target-specific variable values}. This
|
|---|
| 5189 | feature allows you to define different values for the same variable,
|
|---|
| 5190 | based on the target that @code{make} is currently building. As with
|
|---|
| 5191 | automatic variables, these values are only available within the context
|
|---|
| 5192 | of a target's command script (and in other target-specific assignments).
|
|---|
| 5193 |
|
|---|
| 5194 | Set a target-specific variable value like this:
|
|---|
| 5195 |
|
|---|
| 5196 | @example
|
|---|
| 5197 | @var{target} @dots{} : @var{variable-assignment}
|
|---|
| 5198 | @end example
|
|---|
| 5199 |
|
|---|
| 5200 | @noindent
|
|---|
| 5201 | or like this:
|
|---|
| 5202 |
|
|---|
| 5203 | @example
|
|---|
| 5204 | @var{target} @dots{} : override @var{variable-assignment}
|
|---|
| 5205 | @end example
|
|---|
| 5206 |
|
|---|
| 5207 | @noindent
|
|---|
| 5208 | or like this:
|
|---|
| 5209 |
|
|---|
| 5210 | @example
|
|---|
| 5211 | @var{target} @dots{} : export @var{variable-assignment}
|
|---|
| 5212 | @end example
|
|---|
| 5213 |
|
|---|
| 5214 | Multiple @var{target} values create a target-specific variable value for
|
|---|
| 5215 | each member of the target list individually.
|
|---|
| 5216 |
|
|---|
| 5217 | The @var{variable-assignment} can be any valid form of assignment;
|
|---|
| 5218 | recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
|
|---|
| 5219 | conditional (@samp{?=}). All variables that appear within the
|
|---|
| 5220 | @var{variable-assignment} are evaluated within the context of the
|
|---|
| 5221 | target: thus, any previously-defined target-specific variable values
|
|---|
| 5222 | will be in effect. Note that this variable is actually distinct from
|
|---|
| 5223 | any ``global'' value: the two variables do not have to have the same
|
|---|
| 5224 | flavor (recursive vs. static).
|
|---|
| 5225 |
|
|---|
| 5226 | Target-specific variables have the same priority as any other makefile
|
|---|
| 5227 | variable. Variables provided on the command-line (and in the
|
|---|
| 5228 | environment if the @samp{-e} option is in force) will take precedence.
|
|---|
| 5229 | Specifying the @code{override} directive will allow the target-specific
|
|---|
| 5230 | variable value to be preferred.
|
|---|
| 5231 |
|
|---|
| 5232 | There is one more special feature of target-specific variables: when
|
|---|
| 5233 | you define a target-specific variable that variable value is also in
|
|---|
| 5234 | effect for all prerequisites of this target, and all their
|
|---|
| 5235 | prerequisites, etc. (unless those prerequisites override that variable
|
|---|
| 5236 | with their own target-specific variable value). So, for example, a
|
|---|
| 5237 | statement like this:
|
|---|
| 5238 |
|
|---|
| 5239 | @example
|
|---|
| 5240 | prog : CFLAGS = -g
|
|---|
| 5241 | prog : prog.o foo.o bar.o
|
|---|
| 5242 | @end example
|
|---|
| 5243 |
|
|---|
| 5244 | @noindent
|
|---|
| 5245 | will set @code{CFLAGS} to @samp{-g} in the command script for
|
|---|
| 5246 | @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
|
|---|
| 5247 | command scripts that create @file{prog.o}, @file{foo.o}, and
|
|---|
| 5248 | @file{bar.o}, and any command scripts which create their
|
|---|
| 5249 | prerequisites.
|
|---|
| 5250 |
|
|---|
| 5251 | Be aware that a given prerequisite will only be built once per
|
|---|
| 5252 | invocation of make, at most. If the same file is a prerequisite of
|
|---|
| 5253 | multiple targets, and each of those targets has a different value for
|
|---|
| 5254 | the same target-specific variable, then the first target to be built
|
|---|
| 5255 | will cause that prerequisite to be built and the prerequisite will
|
|---|
| 5256 | inherit the target-specific value from the first target. It will
|
|---|
| 5257 | ignore the target-specific values from any other targets.
|
|---|
| 5258 |
|
|---|
| 5259 | @node Pattern-specific, , Target-specific, Using Variables
|
|---|
| 5260 | @section Pattern-specific Variable Values
|
|---|
| 5261 | @cindex pattern-specific variables
|
|---|
| 5262 | @cindex variables, pattern-specific
|
|---|
| 5263 |
|
|---|
| 5264 | In addition to target-specific variable values
|
|---|
| 5265 | (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
|
|---|
| 5266 | @code{make} supports pattern-specific variable values. In this form,
|
|---|
| 5267 | the variable is defined for any target that matches the pattern
|
|---|
| 5268 | specified. If a target matches more than one pattern, all the
|
|---|
| 5269 | matching pattern-specific variables are interpreted in the order in
|
|---|
| 5270 | which they were defined in the makefile, and collected together into
|
|---|
| 5271 | one set. Variables defined in this way are searched after any
|
|---|
| 5272 | target-specific variables defined explicitly for that target, and
|
|---|
| 5273 | before target-specific variables defined for the parent target.
|
|---|
| 5274 |
|
|---|
| 5275 | Set a pattern-specific variable value like this:
|
|---|
| 5276 |
|
|---|
| 5277 | @example
|
|---|
| 5278 | @var{pattern} @dots{} : @var{variable-assignment}
|
|---|
| 5279 | @end example
|
|---|
| 5280 |
|
|---|
| 5281 | @noindent
|
|---|
| 5282 | or like this:
|
|---|
| 5283 |
|
|---|
| 5284 | @example
|
|---|
| 5285 | @var{pattern} @dots{} : override @var{variable-assignment}
|
|---|
| 5286 | @end example
|
|---|
| 5287 |
|
|---|
| 5288 | @noindent
|
|---|
| 5289 | where @var{pattern} is a %-pattern. As with target-specific variable
|
|---|
| 5290 | values, multiple @var{pattern} values create a pattern-specific variable
|
|---|
| 5291 | value for each pattern individually. The @var{variable-assignment} can
|
|---|
| 5292 | be any valid form of assignment. Any command-line variable setting will
|
|---|
| 5293 | take precedence, unless @code{override} is specified.
|
|---|
| 5294 |
|
|---|
| 5295 | For example:
|
|---|
| 5296 |
|
|---|
| 5297 | @example
|
|---|
| 5298 | %.o : CFLAGS = -O
|
|---|
| 5299 | @end example
|
|---|
| 5300 |
|
|---|
| 5301 | @noindent
|
|---|
| 5302 | will assign @code{CFLAGS} the value of @samp{-O} for all targets
|
|---|
| 5303 | matching the pattern @code{%.o}.
|
|---|
| 5304 |
|
|---|
| 5305 | @node Conditionals, Functions, Using Variables, Top
|
|---|
| 5306 | @chapter Conditional Parts of Makefiles
|
|---|
| 5307 |
|
|---|
| 5308 | @cindex conditionals
|
|---|
| 5309 | A @dfn{conditional} causes part of a makefile to be obeyed or ignored
|
|---|
| 5310 | depending on the values of variables. Conditionals can compare the
|
|---|
| 5311 | value of one variable to another, or the value of a variable to
|
|---|
| 5312 | a constant string. Conditionals control what @code{make} actually
|
|---|
| 5313 | ``sees'' in the makefile, so they @emph{cannot} be used to control shell
|
|---|
| 5314 | commands at the time of execution.@refill
|
|---|
| 5315 |
|
|---|
| 5316 | @menu
|
|---|
| 5317 | * Conditional Example:: Example of a conditional
|
|---|
| 5318 | * Conditional Syntax:: The syntax of conditionals.
|
|---|
| 5319 | * Testing Flags:: Conditionals that test flags.
|
|---|
| 5320 | @end menu
|
|---|
| 5321 |
|
|---|
| 5322 | @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
|
|---|
| 5323 | @section Example of a Conditional
|
|---|
| 5324 |
|
|---|
| 5325 | The following example of a conditional tells @code{make} to use one set
|
|---|
| 5326 | of libraries if the @code{CC} variable is @samp{gcc}, and a different
|
|---|
| 5327 | set of libraries otherwise. It works by controlling which of two
|
|---|
| 5328 | command lines will be used as the command for a rule. The result is
|
|---|
| 5329 | that @samp{CC=gcc} as an argument to @code{make} changes not only which
|
|---|
| 5330 | compiler is used but also which libraries are linked.
|
|---|
| 5331 |
|
|---|
| 5332 | @example
|
|---|
| 5333 | libs_for_gcc = -lgnu
|
|---|
| 5334 | normal_libs =
|
|---|
| 5335 |
|
|---|
| 5336 | foo: $(objects)
|
|---|
| 5337 | ifeq ($(CC),gcc)
|
|---|
| 5338 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
|---|
| 5339 | else
|
|---|
| 5340 | $(CC) -o foo $(objects) $(normal_libs)
|
|---|
| 5341 | endif
|
|---|
| 5342 | @end example
|
|---|
| 5343 |
|
|---|
| 5344 | This conditional uses three directives: one @code{ifeq}, one @code{else}
|
|---|
| 5345 | and one @code{endif}.
|
|---|
| 5346 |
|
|---|
| 5347 | The @code{ifeq} directive begins the conditional, and specifies the
|
|---|
| 5348 | condition. It contains two arguments, separated by a comma and surrounded
|
|---|
| 5349 | by parentheses. Variable substitution is performed on both arguments and
|
|---|
| 5350 | then they are compared. The lines of the makefile following the
|
|---|
| 5351 | @code{ifeq} are obeyed if the two arguments match; otherwise they are
|
|---|
| 5352 | ignored.
|
|---|
| 5353 |
|
|---|
| 5354 | The @code{else} directive causes the following lines to be obeyed if the
|
|---|
| 5355 | previous conditional failed. In the example above, this means that the
|
|---|
| 5356 | second alternative linking command is used whenever the first alternative
|
|---|
| 5357 | is not used. It is optional to have an @code{else} in a conditional.
|
|---|
| 5358 |
|
|---|
| 5359 | The @code{endif} directive ends the conditional. Every conditional must
|
|---|
| 5360 | end with an @code{endif}. Unconditional makefile text follows.
|
|---|
| 5361 |
|
|---|
| 5362 | As this example illustrates, conditionals work at the textual level:
|
|---|
| 5363 | the lines of the conditional are treated as part of the makefile, or
|
|---|
| 5364 | ignored, according to the condition. This is why the larger syntactic
|
|---|
| 5365 | units of the makefile, such as rules, may cross the beginning or the
|
|---|
| 5366 | end of the conditional.
|
|---|
| 5367 |
|
|---|
| 5368 | When the variable @code{CC} has the value @samp{gcc}, the above example has
|
|---|
| 5369 | this effect:
|
|---|
| 5370 |
|
|---|
| 5371 | @example
|
|---|
| 5372 | foo: $(objects)
|
|---|
| 5373 | $(CC) -o foo $(objects) $(libs_for_gcc)
|
|---|
| 5374 | @end example
|
|---|
| 5375 |
|
|---|
| 5376 | @noindent
|
|---|
| 5377 | When the variable @code{CC} has any other value, the effect is this:
|
|---|
| 5378 |
|
|---|
| 5379 | @example
|
|---|
| 5380 | foo: $(objects)
|
|---|
| 5381 | $(CC) -o foo $(objects) $(normal_libs)
|
|---|
| 5382 | @end example
|
|---|
| 5383 |
|
|---|
| 5384 | Equivalent results can be obtained in another way by conditionalizing a
|
|---|
| 5385 | variable assignment and then using the variable unconditionally:
|
|---|
| 5386 |
|
|---|
| 5387 | @example
|
|---|
| 5388 | libs_for_gcc = -lgnu
|
|---|
| 5389 | normal_libs =
|
|---|
| 5390 |
|
|---|
| 5391 | ifeq ($(CC),gcc)
|
|---|
| 5392 | libs=$(libs_for_gcc)
|
|---|
| 5393 | else
|
|---|
| 5394 | libs=$(normal_libs)
|
|---|
| 5395 | endif
|
|---|
| 5396 |
|
|---|
| 5397 | foo: $(objects)
|
|---|
| 5398 | $(CC) -o foo $(objects) $(libs)
|
|---|
| 5399 | @end example
|
|---|
| 5400 |
|
|---|
| 5401 | @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
|
|---|
| 5402 | @section Syntax of Conditionals
|
|---|
| 5403 | @findex ifdef
|
|---|
| 5404 | @findex ifeq
|
|---|
| 5405 | @findex ifndef
|
|---|
| 5406 | @findex ifneq
|
|---|
| 5407 | @findex else
|
|---|
| 5408 | @findex endif
|
|---|
| 5409 |
|
|---|
| 5410 | The syntax of a simple conditional with no @code{else} is as follows:
|
|---|
| 5411 |
|
|---|
| 5412 | @example
|
|---|
| 5413 | @var{conditional-directive}
|
|---|
| 5414 | @var{text-if-true}
|
|---|
| 5415 | endif
|
|---|
| 5416 | @end example
|
|---|
| 5417 |
|
|---|
| 5418 | @noindent
|
|---|
| 5419 | The @var{text-if-true} may be any lines of text, to be considered as part
|
|---|
| 5420 | of the makefile if the condition is true. If the condition is false, no
|
|---|
| 5421 | text is used instead.
|
|---|
| 5422 |
|
|---|
| 5423 | The syntax of a complex conditional is as follows:
|
|---|
| 5424 |
|
|---|
| 5425 | @example
|
|---|
| 5426 | @var{conditional-directive}
|
|---|
| 5427 | @var{text-if-true}
|
|---|
| 5428 | else
|
|---|
| 5429 | @var{text-if-false}
|
|---|
| 5430 | endif
|
|---|
| 5431 | @end example
|
|---|
| 5432 |
|
|---|
| 5433 | @noindent
|
|---|
| 5434 | If the condition is true, @var{text-if-true} is used; otherwise,
|
|---|
| 5435 | @var{text-if-false} is used instead. The @var{text-if-false} can be any
|
|---|
| 5436 | number of lines of text.
|
|---|
| 5437 |
|
|---|
| 5438 | The syntax of the @var{conditional-directive} is the same whether the
|
|---|
| 5439 | conditional is simple or complex. There are four different directives that
|
|---|
| 5440 | test different conditions. Here is a table of them:
|
|---|
| 5441 |
|
|---|
| 5442 | @table @code
|
|---|
| 5443 | @item ifeq (@var{arg1}, @var{arg2})
|
|---|
| 5444 | @itemx ifeq '@var{arg1}' '@var{arg2}'
|
|---|
| 5445 | @itemx ifeq "@var{arg1}" "@var{arg2}"
|
|---|
| 5446 | @itemx ifeq "@var{arg1}" '@var{arg2}'
|
|---|
| 5447 | @itemx ifeq '@var{arg1}' "@var{arg2}"
|
|---|
| 5448 | Expand all variable references in @var{arg1} and @var{arg2} and
|
|---|
| 5449 | compare them. If they are identical, the @var{text-if-true} is
|
|---|
| 5450 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
|---|
| 5451 |
|
|---|
| 5452 | Often you want to test if a variable has a non-empty value. When the
|
|---|
| 5453 | value results from complex expansions of variables and functions,
|
|---|
| 5454 | expansions you would consider empty may actually contain whitespace
|
|---|
| 5455 | characters and thus are not seen as empty. However, you can use the
|
|---|
| 5456 | @code{strip} function (@pxref{Text Functions}) to avoid interpreting
|
|---|
| 5457 | whitespace as a non-empty value. For example:
|
|---|
| 5458 |
|
|---|
| 5459 | @example
|
|---|
| 5460 | @group
|
|---|
| 5461 | ifeq ($(strip $(foo)),)
|
|---|
| 5462 | @var{text-if-empty}
|
|---|
| 5463 | endif
|
|---|
| 5464 | @end group
|
|---|
| 5465 | @end example
|
|---|
| 5466 |
|
|---|
| 5467 | @noindent
|
|---|
| 5468 | will evaluate @var{text-if-empty} even if the expansion of
|
|---|
| 5469 | @code{$(foo)} contains whitespace characters.
|
|---|
| 5470 |
|
|---|
| 5471 | @item ifneq (@var{arg1}, @var{arg2})
|
|---|
| 5472 | @itemx ifneq '@var{arg1}' '@var{arg2}'
|
|---|
| 5473 | @itemx ifneq "@var{arg1}" "@var{arg2}"
|
|---|
| 5474 | @itemx ifneq "@var{arg1}" '@var{arg2}'
|
|---|
| 5475 | @itemx ifneq '@var{arg1}' "@var{arg2}"
|
|---|
| 5476 | Expand all variable references in @var{arg1} and @var{arg2} and
|
|---|
| 5477 | compare them. If they are different, the @var{text-if-true} is
|
|---|
| 5478 | effective; otherwise, the @var{text-if-false}, if any, is effective.
|
|---|
| 5479 |
|
|---|
| 5480 | @item ifdef @var{variable-name}
|
|---|
| 5481 | If the variable @var{variable-name} has a non-empty value, the
|
|---|
| 5482 | @var{text-if-true} is effective; otherwise, the @var{text-if-false},
|
|---|
| 5483 | if any, is effective. Variables that have never been defined have an
|
|---|
| 5484 | empty value. The variable @var{variable-name} is itself expanded, so
|
|---|
| 5485 | it could be a variable or function that expands to the name of a
|
|---|
| 5486 | variable.
|
|---|
| 5487 |
|
|---|
| 5488 | Note that @code{ifdef} only tests whether a variable has a value. It
|
|---|
| 5489 | does not expand the variable to see if that value is nonempty.
|
|---|
| 5490 | Consequently, tests using @code{ifdef} return true for all definitions
|
|---|
| 5491 | except those like @code{foo =}. To test for an empty value, use
|
|---|
| 5492 | @w{@code{ifeq ($(foo),)}}. For example,
|
|---|
| 5493 |
|
|---|
| 5494 | @example
|
|---|
| 5495 | bar =
|
|---|
| 5496 | foo = $(bar)
|
|---|
| 5497 | ifdef foo
|
|---|
| 5498 | frobozz = yes
|
|---|
| 5499 | else
|
|---|
| 5500 | frobozz = no
|
|---|
| 5501 | endif
|
|---|
| 5502 | @end example
|
|---|
| 5503 |
|
|---|
| 5504 | @noindent
|
|---|
| 5505 | sets @samp{frobozz} to @samp{yes}, while:
|
|---|
| 5506 |
|
|---|
| 5507 | @example
|
|---|
| 5508 | foo =
|
|---|
| 5509 | ifdef foo
|
|---|
| 5510 | frobozz = yes
|
|---|
| 5511 | else
|
|---|
| 5512 | frobozz = no
|
|---|
| 5513 | endif
|
|---|
| 5514 | @end example
|
|---|
| 5515 |
|
|---|
| 5516 | @noindent
|
|---|
| 5517 | sets @samp{frobozz} to @samp{no}.
|
|---|
| 5518 |
|
|---|
| 5519 | @item ifndef @var{variable-name}
|
|---|
| 5520 | If the variable @var{variable-name} has an empty value, the
|
|---|
| 5521 | @var{text-if-true} is effective; otherwise, the @var{text-if-false},
|
|---|
| 5522 | if any, is effective.
|
|---|
| 5523 | @end table
|
|---|
| 5524 |
|
|---|
| 5525 | Extra spaces are allowed and ignored at the beginning of the conditional
|
|---|
| 5526 | directive line, but a tab is not allowed. (If the line begins with a tab,
|
|---|
| 5527 | it will be considered a command for a rule.) Aside from this, extra spaces
|
|---|
| 5528 | or tabs may be inserted with no effect anywhere except within the directive
|
|---|
| 5529 | name or within an argument. A comment starting with @samp{#} may appear at
|
|---|
| 5530 | the end of the line.
|
|---|
| 5531 |
|
|---|
| 5532 | The other two directives that play a part in a conditional are @code{else}
|
|---|
| 5533 | and @code{endif}. Each of these directives is written as one word, with no
|
|---|
| 5534 | arguments. Extra spaces are allowed and ignored at the beginning of the
|
|---|
| 5535 | line, and spaces or tabs at the end. A comment starting with @samp{#} may
|
|---|
| 5536 | appear at the end of the line.
|
|---|
| 5537 |
|
|---|
| 5538 | Conditionals affect which lines of the makefile @code{make} uses. If
|
|---|
| 5539 | the condition is true, @code{make} reads the lines of the
|
|---|
| 5540 | @var{text-if-true} as part of the makefile; if the condition is false,
|
|---|
| 5541 | @code{make} ignores those lines completely. It follows that syntactic
|
|---|
| 5542 | units of the makefile, such as rules, may safely be split across the
|
|---|
| 5543 | beginning or the end of the conditional.@refill
|
|---|
| 5544 |
|
|---|
| 5545 | @code{make} evaluates conditionals when it reads a makefile.
|
|---|
| 5546 | Consequently, you cannot use automatic variables in the tests of
|
|---|
| 5547 | conditionals because they are not defined until commands are run
|
|---|
| 5548 | (@pxref{Automatic Variables}).
|
|---|
| 5549 |
|
|---|
| 5550 | To prevent intolerable confusion, it is not permitted to start a
|
|---|
| 5551 | conditional in one makefile and end it in another. However, you may
|
|---|
| 5552 | write an @code{include} directive within a conditional, provided you do
|
|---|
| 5553 | not attempt to terminate the conditional inside the included file.
|
|---|
| 5554 |
|
|---|
| 5555 | @node Testing Flags, , Conditional Syntax, Conditionals
|
|---|
| 5556 | @section Conditionals that Test Flags
|
|---|
| 5557 |
|
|---|
| 5558 | You can write a conditional that tests @code{make} command flags such as
|
|---|
| 5559 | @samp{-t} by using the variable @code{MAKEFLAGS} together with the
|
|---|
| 5560 | @code{findstring} function
|
|---|
| 5561 | (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
|
|---|
| 5562 | This is useful when @code{touch} is not enough to make a file appear up
|
|---|
| 5563 | to date.
|
|---|
| 5564 |
|
|---|
| 5565 | The @code{findstring} function determines whether one string appears as a
|
|---|
| 5566 | substring of another. If you want to test for the @samp{-t} flag,
|
|---|
| 5567 | use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
|
|---|
| 5568 | the other.
|
|---|
| 5569 |
|
|---|
| 5570 | For example, here is how to arrange to use @samp{ranlib -t} to finish
|
|---|
| 5571 | marking an archive file up to date:
|
|---|
| 5572 |
|
|---|
| 5573 | @example
|
|---|
| 5574 | archive.a: @dots{}
|
|---|
| 5575 | ifneq (,$(findstring t,$(MAKEFLAGS)))
|
|---|
| 5576 | +touch archive.a
|
|---|
| 5577 | +ranlib -t archive.a
|
|---|
| 5578 | else
|
|---|
| 5579 | ranlib archive.a
|
|---|
| 5580 | endif
|
|---|
| 5581 | @end example
|
|---|
| 5582 |
|
|---|
| 5583 | @noindent
|
|---|
| 5584 | The @samp{+} prefix marks those command lines as ``recursive'' so
|
|---|
| 5585 | that they will be executed despite use of the @samp{-t} flag.
|
|---|
| 5586 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 5587 |
|
|---|
| 5588 | @node Functions, Running, Conditionals, Top
|
|---|
| 5589 | @chapter Functions for Transforming Text
|
|---|
| 5590 | @cindex functions
|
|---|
| 5591 |
|
|---|
| 5592 | @dfn{Functions} allow you to do text processing in the makefile to compute
|
|---|
| 5593 | the files to operate on or the commands to use. You use a function in a
|
|---|
| 5594 | @dfn{function call}, where you give the name of the function and some text
|
|---|
| 5595 | (the @dfn{arguments}) for the function to operate on. The result of the
|
|---|
| 5596 | function's processing is substituted into the makefile at the point of the
|
|---|
| 5597 | call, just as a variable might be substituted.
|
|---|
| 5598 |
|
|---|
| 5599 | @menu
|
|---|
| 5600 | * Syntax of Functions:: How to write a function call.
|
|---|
| 5601 | * Text Functions:: General-purpose text manipulation functions.
|
|---|
| 5602 | * File Name Functions:: Functions for manipulating file names.
|
|---|
| 5603 | * Foreach Function:: Repeat some text with controlled variation.
|
|---|
| 5604 | * If Function:: Conditionally expand a value.
|
|---|
| 5605 | * Call Function:: Expand a user-defined function.
|
|---|
| 5606 | * Value Function:: Return the un-expanded value of a variable.
|
|---|
| 5607 | * Eval Function:: Evaluate the arguments as makefile syntax.
|
|---|
| 5608 | * Origin Function:: Find where a variable got its value.
|
|---|
| 5609 | * Shell Function:: Substitute the output of a shell command.
|
|---|
| 5610 | * Make Control Functions:: Functions that control how make runs.
|
|---|
| 5611 | @end menu
|
|---|
| 5612 |
|
|---|
| 5613 | @node Syntax of Functions, Text Functions, Functions, Functions
|
|---|
| 5614 | @section Function Call Syntax
|
|---|
| 5615 | @cindex @code{$}, in function call
|
|---|
| 5616 | @cindex dollar sign (@code{$}), in function call
|
|---|
| 5617 | @cindex arguments of functions
|
|---|
| 5618 | @cindex functions, syntax of
|
|---|
| 5619 |
|
|---|
| 5620 | A function call resembles a variable reference. It looks like this:
|
|---|
| 5621 |
|
|---|
| 5622 | @example
|
|---|
| 5623 | $(@var{function} @var{arguments})
|
|---|
| 5624 | @end example
|
|---|
| 5625 |
|
|---|
| 5626 | @noindent
|
|---|
| 5627 | or like this:
|
|---|
| 5628 |
|
|---|
| 5629 | @example
|
|---|
| 5630 | $@{@var{function} @var{arguments}@}
|
|---|
| 5631 | @end example
|
|---|
| 5632 |
|
|---|
| 5633 | Here @var{function} is a function name; one of a short list of names
|
|---|
| 5634 | that are part of @code{make}. You can also essentially create your own
|
|---|
| 5635 | functions by using the @code{call} builtin function.
|
|---|
| 5636 |
|
|---|
| 5637 | The @var{arguments} are the arguments of the function. They are
|
|---|
| 5638 | separated from the function name by one or more spaces or tabs, and if
|
|---|
| 5639 | there is more than one argument, then they are separated by commas.
|
|---|
| 5640 | Such whitespace and commas are not part of an argument's value. The
|
|---|
| 5641 | delimiters which you use to surround the function call, whether
|
|---|
| 5642 | parentheses or braces, can appear in an argument only in matching pairs;
|
|---|
| 5643 | the other kind of delimiters may appear singly. If the arguments
|
|---|
| 5644 | themselves contain other function calls or variable references, it is
|
|---|
| 5645 | wisest to use the same kind of delimiters for all the references; write
|
|---|
| 5646 | @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
|
|---|
| 5647 | is because it is clearer, and because only one type of delimiter is
|
|---|
| 5648 | matched to find the end of the reference.
|
|---|
| 5649 |
|
|---|
| 5650 | The text written for each argument is processed by substitution of
|
|---|
| 5651 | variables and function calls to produce the argument value, which
|
|---|
| 5652 | is the text on which the function acts. The substitution is done in the
|
|---|
| 5653 | order in which the arguments appear.
|
|---|
| 5654 |
|
|---|
| 5655 | Commas and unmatched parentheses or braces cannot appear in the text of an
|
|---|
| 5656 | argument as written; leading spaces cannot appear in the text of the first
|
|---|
| 5657 | argument as written. These characters can be put into the argument value
|
|---|
| 5658 | by variable substitution. First define variables @code{comma} and
|
|---|
| 5659 | @code{space} whose values are isolated comma and space characters, then
|
|---|
| 5660 | substitute these variables where such characters are wanted, like this:
|
|---|
| 5661 |
|
|---|
| 5662 | @example
|
|---|
| 5663 | @group
|
|---|
| 5664 | comma:= ,
|
|---|
| 5665 | empty:=
|
|---|
| 5666 | space:= $(empty) $(empty)
|
|---|
| 5667 | foo:= a b c
|
|---|
| 5668 | bar:= $(subst $(space),$(comma),$(foo))
|
|---|
| 5669 | # @r{bar is now `a,b,c'.}
|
|---|
| 5670 | @end group
|
|---|
| 5671 | @end example
|
|---|
| 5672 |
|
|---|
| 5673 | @noindent
|
|---|
| 5674 | Here the @code{subst} function replaces each space with a comma, through
|
|---|
| 5675 | the value of @code{foo}, and substitutes the result.
|
|---|
| 5676 |
|
|---|
| 5677 | @node Text Functions, File Name Functions, Syntax of Functions, Functions
|
|---|
| 5678 | @section Functions for String Substitution and Analysis
|
|---|
| 5679 | @cindex functions, for text
|
|---|
| 5680 |
|
|---|
| 5681 | Here are some functions that operate on strings:
|
|---|
| 5682 |
|
|---|
| 5683 | @table @code
|
|---|
| 5684 | @item $(subst @var{from},@var{to},@var{text})
|
|---|
| 5685 | @findex subst
|
|---|
| 5686 | Performs a textual replacement on the text @var{text}: each occurrence
|
|---|
| 5687 | of @var{from} is replaced by @var{to}. The result is substituted for
|
|---|
| 5688 | the function call. For example,
|
|---|
| 5689 |
|
|---|
| 5690 | @example
|
|---|
| 5691 | $(subst ee,EE,feet on the street)
|
|---|
| 5692 | @end example
|
|---|
| 5693 |
|
|---|
| 5694 | substitutes the string @samp{fEEt on the strEEt}.
|
|---|
| 5695 |
|
|---|
| 5696 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
|---|
| 5697 | @findex patsubst
|
|---|
| 5698 | Finds whitespace-separated words in @var{text} that match
|
|---|
| 5699 | @var{pattern} and replaces them with @var{replacement}. Here
|
|---|
| 5700 | @var{pattern} may contain a @samp{%} which acts as a wildcard,
|
|---|
| 5701 | matching any number of any characters within a word. If
|
|---|
| 5702 | @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
|
|---|
| 5703 | by the text that matched the @samp{%} in @var{pattern}. Only the first
|
|---|
| 5704 | @samp{%} in the @var{pattern} and @var{replacement} is treated this
|
|---|
| 5705 | way; any subsequent @samp{%} is unchanged.@refill
|
|---|
| 5706 |
|
|---|
| 5707 | @cindex @code{%}, quoting in @code{patsubst}
|
|---|
| 5708 | @cindex @code{%}, quoting with @code{\} (backslash)
|
|---|
| 5709 | @cindex @code{\} (backslash), to quote @code{%}
|
|---|
| 5710 | @cindex backslash (@code{\}), to quote @code{%}
|
|---|
| 5711 | @cindex quoting @code{%}, in @code{patsubst}
|
|---|
| 5712 | @samp{%} characters in @code{patsubst} function invocations can be
|
|---|
| 5713 | quoted with preceding backslashes (@samp{\}). Backslashes that would
|
|---|
| 5714 | otherwise quote @samp{%} characters can be quoted with more backslashes.
|
|---|
| 5715 | Backslashes that quote @samp{%} characters or other backslashes are
|
|---|
| 5716 | removed from the pattern before it is compared file names or has a stem
|
|---|
| 5717 | substituted into it. Backslashes that are not in danger of quoting
|
|---|
| 5718 | @samp{%} characters go unmolested. For example, the pattern
|
|---|
| 5719 | @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
|
|---|
| 5720 | operative @samp{%} character, and @samp{pattern\\} following it. The
|
|---|
| 5721 | final two backslashes are left alone because they cannot affect any
|
|---|
| 5722 | @samp{%} character.@refill
|
|---|
| 5723 |
|
|---|
| 5724 | Whitespace between words is folded into single space characters;
|
|---|
| 5725 | leading and trailing whitespace is discarded.
|
|---|
| 5726 |
|
|---|
| 5727 | For example,
|
|---|
| 5728 |
|
|---|
| 5729 | @example
|
|---|
| 5730 | $(patsubst %.c,%.o,x.c.c bar.c)
|
|---|
| 5731 | @end example
|
|---|
| 5732 |
|
|---|
| 5733 | @noindent
|
|---|
| 5734 | produces the value @samp{x.c.o bar.o}.
|
|---|
| 5735 |
|
|---|
| 5736 | Substitution references (@pxref{Substitution Refs, ,Substitution
|
|---|
| 5737 | References}) are a simpler way to get the effect of the @code{patsubst}
|
|---|
| 5738 | function:
|
|---|
| 5739 |
|
|---|
| 5740 | @example
|
|---|
| 5741 | $(@var{var}:@var{pattern}=@var{replacement})
|
|---|
| 5742 | @end example
|
|---|
| 5743 |
|
|---|
| 5744 | @noindent
|
|---|
| 5745 | is equivalent to
|
|---|
| 5746 |
|
|---|
| 5747 | @example
|
|---|
| 5748 | $(patsubst @var{pattern},@var{replacement},$(@var{var}))
|
|---|
| 5749 | @end example
|
|---|
| 5750 |
|
|---|
| 5751 | The second shorthand simplifies one of the most common uses of
|
|---|
| 5752 | @code{patsubst}: replacing the suffix at the end of file names.
|
|---|
| 5753 |
|
|---|
| 5754 | @example
|
|---|
| 5755 | $(@var{var}:@var{suffix}=@var{replacement})
|
|---|
| 5756 | @end example
|
|---|
| 5757 |
|
|---|
| 5758 | @noindent
|
|---|
| 5759 | is equivalent to
|
|---|
| 5760 |
|
|---|
| 5761 | @example
|
|---|
| 5762 | $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
|
|---|
| 5763 | @end example
|
|---|
| 5764 |
|
|---|
| 5765 | @noindent
|
|---|
| 5766 | For example, you might have a list of object files:
|
|---|
| 5767 |
|
|---|
| 5768 | @example
|
|---|
| 5769 | objects = foo.o bar.o baz.o
|
|---|
| 5770 | @end example
|
|---|
| 5771 |
|
|---|
| 5772 | @noindent
|
|---|
| 5773 | To get the list of corresponding source files, you could simply write:
|
|---|
| 5774 |
|
|---|
| 5775 | @example
|
|---|
| 5776 | $(objects:.o=.c)
|
|---|
| 5777 | @end example
|
|---|
| 5778 |
|
|---|
| 5779 | @noindent
|
|---|
| 5780 | instead of using the general form:
|
|---|
| 5781 |
|
|---|
| 5782 | @example
|
|---|
| 5783 | $(patsubst %.o,%.c,$(objects))
|
|---|
| 5784 | @end example
|
|---|
| 5785 |
|
|---|
| 5786 | @item $(strip @var{string})
|
|---|
| 5787 | @cindex stripping whitespace
|
|---|
| 5788 | @cindex whitespace, stripping
|
|---|
| 5789 | @cindex spaces, stripping
|
|---|
| 5790 | @findex strip
|
|---|
| 5791 | Removes leading and trailing whitespace from @var{string} and replaces
|
|---|
| 5792 | each internal sequence of one or more whitespace characters with a
|
|---|
| 5793 | single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
|
|---|
| 5794 |
|
|---|
| 5795 | The function @code{strip} can be very useful when used in conjunction
|
|---|
| 5796 | with conditionals. When comparing something with the empty string
|
|---|
| 5797 | @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
|
|---|
| 5798 | just whitespace to match the empty string (@pxref{Conditionals}).
|
|---|
| 5799 |
|
|---|
| 5800 | Thus, the following may fail to have the desired results:
|
|---|
| 5801 |
|
|---|
| 5802 | @example
|
|---|
| 5803 | .PHONY: all
|
|---|
| 5804 | ifneq "$(needs_made)" ""
|
|---|
| 5805 | all: $(needs_made)
|
|---|
| 5806 | else
|
|---|
| 5807 | all:;@@echo 'Nothing to make!'
|
|---|
| 5808 | endif
|
|---|
| 5809 | @end example
|
|---|
| 5810 |
|
|---|
| 5811 | @noindent
|
|---|
| 5812 | Replacing the variable reference @w{@samp{$(needs_made)}} with the
|
|---|
| 5813 | function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
|
|---|
| 5814 | directive would make it more robust.@refill
|
|---|
| 5815 |
|
|---|
| 5816 | @item $(findstring @var{find},@var{in})
|
|---|
| 5817 | @findex findstring
|
|---|
| 5818 | @cindex searching for strings
|
|---|
| 5819 | @cindex finding strings
|
|---|
| 5820 | @cindex strings, searching for
|
|---|
| 5821 | Searches @var{in} for an occurrence of @var{find}. If it occurs, the
|
|---|
| 5822 | value is @var{find}; otherwise, the value is empty. You can use this
|
|---|
| 5823 | function in a conditional to test for the presence of a specific
|
|---|
| 5824 | substring in a given string. Thus, the two examples,
|
|---|
| 5825 |
|
|---|
| 5826 | @example
|
|---|
| 5827 | $(findstring a,a b c)
|
|---|
| 5828 | $(findstring a,b c)
|
|---|
| 5829 | @end example
|
|---|
| 5830 |
|
|---|
| 5831 | @noindent
|
|---|
| 5832 | produce the values @samp{a} and @samp{} (the empty string),
|
|---|
| 5833 | respectively. @xref{Testing Flags}, for a practical application of
|
|---|
| 5834 | @code{findstring}.@refill
|
|---|
| 5835 |
|
|---|
| 5836 | @need 750
|
|---|
| 5837 | @findex filter
|
|---|
| 5838 | @cindex filtering words
|
|---|
| 5839 | @cindex words, filtering
|
|---|
| 5840 | @item $(filter @var{pattern}@dots{},@var{text})
|
|---|
| 5841 | Returns all whitespace-separated words in @var{text} that @emph{do} match
|
|---|
| 5842 | any of the @var{pattern} words, removing any words that @emph{do not}
|
|---|
| 5843 | match. The patterns are written using @samp{%}, just like the patterns
|
|---|
| 5844 | used in the @code{patsubst} function above.@refill
|
|---|
| 5845 |
|
|---|
| 5846 | The @code{filter} function can be used to separate out different types
|
|---|
| 5847 | of strings (such as file names) in a variable. For example:
|
|---|
| 5848 |
|
|---|
| 5849 | @example
|
|---|
| 5850 | sources := foo.c bar.c baz.s ugh.h
|
|---|
| 5851 | foo: $(sources)
|
|---|
| 5852 | cc $(filter %.c %.s,$(sources)) -o foo
|
|---|
| 5853 | @end example
|
|---|
| 5854 |
|
|---|
| 5855 | @noindent
|
|---|
| 5856 | says that @file{foo} depends of @file{foo.c}, @file{bar.c},
|
|---|
| 5857 | @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
|
|---|
| 5858 | @file{baz.s} should be specified in the command to the
|
|---|
| 5859 | compiler.@refill
|
|---|
| 5860 |
|
|---|
| 5861 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
|---|
| 5862 | @findex filter-out
|
|---|
| 5863 | @cindex filtering out words
|
|---|
| 5864 | @cindex words, filtering out
|
|---|
| 5865 | Returns all whitespace-separated words in @var{text} that @emph{do not}
|
|---|
| 5866 | match any of the @var{pattern} words, removing the words that @emph{do}
|
|---|
| 5867 | match one or more. This is the exact opposite of the @code{filter}
|
|---|
| 5868 | function.@refill
|
|---|
| 5869 |
|
|---|
| 5870 | For example, given:
|
|---|
| 5871 |
|
|---|
| 5872 | @example
|
|---|
| 5873 | @group
|
|---|
| 5874 | objects=main1.o foo.o main2.o bar.o
|
|---|
| 5875 | mains=main1.o main2.o
|
|---|
| 5876 | @end group
|
|---|
| 5877 | @end example
|
|---|
| 5878 |
|
|---|
| 5879 | @noindent
|
|---|
| 5880 | the following generates a list which contains all the object files not
|
|---|
| 5881 | in @samp{mains}:
|
|---|
| 5882 |
|
|---|
| 5883 | @example
|
|---|
| 5884 | $(filter-out $(mains),$(objects))
|
|---|
| 5885 | @end example
|
|---|
| 5886 |
|
|---|
| 5887 | @need 1500
|
|---|
| 5888 | @findex sort
|
|---|
| 5889 | @cindex sorting words
|
|---|
| 5890 | @item $(sort @var{list})
|
|---|
| 5891 | Sorts the words of @var{list} in lexical order, removing duplicate
|
|---|
| 5892 | words. The output is a list of words separated by single spaces.
|
|---|
| 5893 | Thus,
|
|---|
| 5894 |
|
|---|
| 5895 | @example
|
|---|
| 5896 | $(sort foo bar lose)
|
|---|
| 5897 | @end example
|
|---|
| 5898 |
|
|---|
| 5899 | @noindent
|
|---|
| 5900 | returns the value @samp{bar foo lose}.
|
|---|
| 5901 |
|
|---|
| 5902 | @cindex removing duplicate words
|
|---|
| 5903 | @cindex duplicate words, removing
|
|---|
| 5904 | @cindex words, removing duplicates
|
|---|
| 5905 | Incidentally, since @code{sort} removes duplicate words, you can use
|
|---|
| 5906 | it for this purpose even if you don't care about the sort order.
|
|---|
| 5907 |
|
|---|
| 5908 | @item $(word @var{n},@var{text})
|
|---|
| 5909 | @findex word
|
|---|
| 5910 | @cindex word, selecting a
|
|---|
| 5911 | @cindex selecting a word
|
|---|
| 5912 | Returns the @var{n}th word of @var{text}. The legitimate values of
|
|---|
| 5913 | @var{n} start from 1. If @var{n} is bigger than the number of words
|
|---|
| 5914 | in @var{text}, the value is empty. For example,
|
|---|
| 5915 |
|
|---|
| 5916 | @example
|
|---|
| 5917 | $(word 2, foo bar baz)
|
|---|
| 5918 | @end example
|
|---|
| 5919 |
|
|---|
| 5920 | @noindent
|
|---|
| 5921 | returns @samp{bar}.
|
|---|
| 5922 |
|
|---|
| 5923 | @item $(wordlist @var{s},@var{e},@var{text})
|
|---|
| 5924 | @findex wordlist
|
|---|
| 5925 | @cindex words, selecting lists of
|
|---|
| 5926 | @cindex selecting word lists
|
|---|
| 5927 | Returns the list of words in @var{text} starting with word @var{s} and
|
|---|
| 5928 | ending with word @var{e} (inclusive). The legitimate values of @var{s}
|
|---|
| 5929 | and @var{e} start from 1. If @var{s} is bigger than the number of words
|
|---|
| 5930 | in @var{text}, the value is empty. If @var{e} is bigger than the number
|
|---|
| 5931 | of words in @var{text}, words up to the end of @var{text} are returned.
|
|---|
| 5932 | If @var{s} is greater than @var{e}, nothing is returned. For example,
|
|---|
| 5933 |
|
|---|
| 5934 | @example
|
|---|
| 5935 | $(wordlist 2, 3, foo bar baz)
|
|---|
| 5936 | @end example
|
|---|
| 5937 |
|
|---|
| 5938 | @noindent
|
|---|
| 5939 | returns @samp{bar baz}.
|
|---|
| 5940 |
|
|---|
| 5941 | @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
|
|---|
| 5942 | @item $(words @var{text})
|
|---|
| 5943 | @findex words
|
|---|
| 5944 | @cindex words, finding number
|
|---|
| 5945 | Returns the number of words in @var{text}.
|
|---|
| 5946 | Thus, the last word of @var{text} is
|
|---|
| 5947 | @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
|
|---|
| 5948 |
|
|---|
| 5949 | @item $(firstword @var{names}@dots{})
|
|---|
| 5950 | @findex firstword
|
|---|
| 5951 | @cindex words, extracting first
|
|---|
| 5952 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 5953 | by whitespace. The value is the first name in the series. The rest
|
|---|
| 5954 | of the names are ignored.
|
|---|
| 5955 |
|
|---|
| 5956 | For example,
|
|---|
| 5957 |
|
|---|
| 5958 | @example
|
|---|
| 5959 | $(firstword foo bar)
|
|---|
| 5960 | @end example
|
|---|
| 5961 |
|
|---|
| 5962 | @noindent
|
|---|
| 5963 | produces the result @samp{foo}. Although @code{$(firstword
|
|---|
| 5964 | @var{text})} is the same as @code{$(word 1,@var{text})}, the
|
|---|
| 5965 | @code{firstword} function is retained for its simplicity.@refill
|
|---|
| 5966 | @end table
|
|---|
| 5967 |
|
|---|
| 5968 | Here is a realistic example of the use of @code{subst} and
|
|---|
| 5969 | @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
|
|---|
| 5970 | to specify a list of directories that @code{make} should search for
|
|---|
| 5971 | prerequisite files
|
|---|
| 5972 | (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
|
|---|
| 5973 | This example shows how to
|
|---|
| 5974 | tell the C compiler to search for header files in the same list of
|
|---|
| 5975 | directories.@refill
|
|---|
| 5976 |
|
|---|
| 5977 | The value of @code{VPATH} is a list of directories separated by colons,
|
|---|
| 5978 | such as @samp{src:../headers}. First, the @code{subst} function is used to
|
|---|
| 5979 | change the colons to spaces:
|
|---|
| 5980 |
|
|---|
| 5981 | @example
|
|---|
| 5982 | $(subst :, ,$(VPATH))
|
|---|
| 5983 | @end example
|
|---|
| 5984 |
|
|---|
| 5985 | @noindent
|
|---|
| 5986 | This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
|
|---|
| 5987 | each directory name into a @samp{-I} flag. These can be added to the
|
|---|
| 5988 | value of the variable @code{CFLAGS}, which is passed automatically to the C
|
|---|
| 5989 | compiler, like this:
|
|---|
| 5990 |
|
|---|
| 5991 | @example
|
|---|
| 5992 | override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
|
|---|
| 5993 | @end example
|
|---|
| 5994 |
|
|---|
| 5995 | @noindent
|
|---|
| 5996 | The effect is to append the text @samp{-Isrc -I../headers} to the
|
|---|
| 5997 | previously given value of @code{CFLAGS}. The @code{override} directive is
|
|---|
| 5998 | used so that the new value is assigned even if the previous value of
|
|---|
| 5999 | @code{CFLAGS} was specified with a command argument (@pxref{Override
|
|---|
| 6000 | Directive, , The @code{override} Directive}).
|
|---|
| 6001 |
|
|---|
| 6002 | @node File Name Functions, Foreach Function, Text Functions, Functions
|
|---|
| 6003 | @section Functions for File Names
|
|---|
| 6004 | @cindex functions, for file names
|
|---|
| 6005 | @cindex file name functions
|
|---|
| 6006 |
|
|---|
| 6007 | Several of the built-in expansion functions relate specifically to
|
|---|
| 6008 | taking apart file names or lists of file names.
|
|---|
| 6009 |
|
|---|
| 6010 | Each of the following functions performs a specific transformation on a
|
|---|
| 6011 | file name. The argument of the function is regarded as a series of file
|
|---|
| 6012 | names, separated by whitespace. (Leading and trailing whitespace is
|
|---|
| 6013 | ignored.) Each file name in the series is transformed in the same way and
|
|---|
| 6014 | the results are concatenated with single spaces between them.
|
|---|
| 6015 |
|
|---|
| 6016 | @table @code
|
|---|
| 6017 | @item $(dir @var{names}@dots{})
|
|---|
| 6018 | @findex dir
|
|---|
| 6019 | @cindex directory part
|
|---|
| 6020 | @cindex file name, directory part
|
|---|
| 6021 | Extracts the directory-part of each file name in @var{names}. The
|
|---|
| 6022 | directory-part of the file name is everything up through (and
|
|---|
| 6023 | including) the last slash in it. If the file name contains no slash,
|
|---|
| 6024 | the directory part is the string @samp{./}. For example,
|
|---|
| 6025 |
|
|---|
| 6026 | @example
|
|---|
| 6027 | $(dir src/foo.c hacks)
|
|---|
| 6028 | @end example
|
|---|
| 6029 |
|
|---|
| 6030 | @noindent
|
|---|
| 6031 | produces the result @samp{src/ ./}.
|
|---|
| 6032 |
|
|---|
| 6033 | @item $(notdir @var{names}@dots{})
|
|---|
| 6034 | @findex notdir
|
|---|
| 6035 | @cindex file name, nondirectory part
|
|---|
| 6036 | @cindex nondirectory part
|
|---|
| 6037 | Extracts all but the directory-part of each file name in @var{names}.
|
|---|
| 6038 | If the file name contains no slash, it is left unchanged. Otherwise,
|
|---|
| 6039 | everything through the last slash is removed from it.
|
|---|
| 6040 |
|
|---|
| 6041 | A file name that ends with a slash becomes an empty string. This is
|
|---|
| 6042 | unfortunate, because it means that the result does not always have the
|
|---|
| 6043 | same number of whitespace-separated file names as the argument had;
|
|---|
| 6044 | but we do not see any other valid alternative.
|
|---|
| 6045 |
|
|---|
| 6046 | For example,
|
|---|
| 6047 |
|
|---|
| 6048 | @example
|
|---|
| 6049 | $(notdir src/foo.c hacks)
|
|---|
| 6050 | @end example
|
|---|
| 6051 |
|
|---|
| 6052 | @noindent
|
|---|
| 6053 | produces the result @samp{foo.c hacks}.
|
|---|
| 6054 |
|
|---|
| 6055 | @item $(suffix @var{names}@dots{})
|
|---|
| 6056 | @findex suffix
|
|---|
| 6057 | @cindex suffix, function to find
|
|---|
| 6058 | @cindex file name suffix
|
|---|
| 6059 | Extracts the suffix of each file name in @var{names}. If the file name
|
|---|
| 6060 | contains a period, the suffix is everything starting with the last
|
|---|
| 6061 | period. Otherwise, the suffix is the empty string. This frequently
|
|---|
| 6062 | means that the result will be empty when @var{names} is not, and if
|
|---|
| 6063 | @var{names} contains multiple file names, the result may contain fewer
|
|---|
| 6064 | file names.
|
|---|
| 6065 |
|
|---|
| 6066 | For example,
|
|---|
| 6067 |
|
|---|
| 6068 | @example
|
|---|
| 6069 | $(suffix src/foo.c src-1.0/bar.c hacks)
|
|---|
| 6070 | @end example
|
|---|
| 6071 |
|
|---|
| 6072 | @noindent
|
|---|
| 6073 | produces the result @samp{.c .c}.
|
|---|
| 6074 |
|
|---|
| 6075 | @item $(basename @var{names}@dots{})
|
|---|
| 6076 | @findex basename
|
|---|
| 6077 | @cindex basename
|
|---|
| 6078 | @cindex file name, basename of
|
|---|
| 6079 | Extracts all but the suffix of each file name in @var{names}. If the
|
|---|
| 6080 | file name contains a period, the basename is everything starting up to
|
|---|
| 6081 | (and not including) the last period. Periods in the directory part are
|
|---|
| 6082 | ignored. If there is no period, the basename is the entire file name.
|
|---|
| 6083 | For example,
|
|---|
| 6084 |
|
|---|
| 6085 | @example
|
|---|
| 6086 | $(basename src/foo.c src-1.0/bar hacks)
|
|---|
| 6087 | @end example
|
|---|
| 6088 |
|
|---|
| 6089 | @noindent
|
|---|
| 6090 | produces the result @samp{src/foo src-1.0/bar hacks}.
|
|---|
| 6091 |
|
|---|
| 6092 | @c plural convention with dots (be consistent)
|
|---|
| 6093 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
|---|
| 6094 | @findex addsuffix
|
|---|
| 6095 | @cindex suffix, adding
|
|---|
| 6096 | @cindex file name suffix, adding
|
|---|
| 6097 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6098 | by whitespace; @var{suffix} is used as a unit. The value of
|
|---|
| 6099 | @var{suffix} is appended to the end of each individual name and the
|
|---|
| 6100 | resulting larger names are concatenated with single spaces between
|
|---|
| 6101 | them. For example,
|
|---|
| 6102 |
|
|---|
| 6103 | @example
|
|---|
| 6104 | $(addsuffix .c,foo bar)
|
|---|
| 6105 | @end example
|
|---|
| 6106 |
|
|---|
| 6107 | @noindent
|
|---|
| 6108 | produces the result @samp{foo.c bar.c}.
|
|---|
| 6109 |
|
|---|
| 6110 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
|---|
| 6111 | @findex addprefix
|
|---|
| 6112 | @cindex prefix, adding
|
|---|
| 6113 | @cindex file name prefix, adding
|
|---|
| 6114 | The argument @var{names} is regarded as a series of names, separated
|
|---|
| 6115 | by whitespace; @var{prefix} is used as a unit. The value of
|
|---|
| 6116 | @var{prefix} is prepended to the front of each individual name and the
|
|---|
| 6117 | resulting larger names are concatenated with single spaces between
|
|---|
| 6118 | them. For example,
|
|---|
| 6119 |
|
|---|
| 6120 | @example
|
|---|
| 6121 | $(addprefix src/,foo bar)
|
|---|
| 6122 | @end example
|
|---|
| 6123 |
|
|---|
| 6124 | @noindent
|
|---|
| 6125 | produces the result @samp{src/foo src/bar}.
|
|---|
| 6126 |
|
|---|
| 6127 | @item $(join @var{list1},@var{list2})
|
|---|
| 6128 | @findex join
|
|---|
| 6129 | @cindex joining lists of words
|
|---|
| 6130 | @cindex words, joining lists
|
|---|
| 6131 | Concatenates the two arguments word by word: the two first words (one
|
|---|
| 6132 | from each argument) concatenated form the first word of the result, the
|
|---|
| 6133 | two second words form the second word of the result, and so on. So the
|
|---|
| 6134 | @var{n}th word of the result comes from the @var{n}th word of each
|
|---|
| 6135 | argument. If one argument has more words that the other, the extra
|
|---|
| 6136 | words are copied unchanged into the result.
|
|---|
| 6137 |
|
|---|
| 6138 | For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
|
|---|
| 6139 |
|
|---|
| 6140 | Whitespace between the words in the lists is not preserved; it is
|
|---|
| 6141 | replaced with a single space.
|
|---|
| 6142 |
|
|---|
| 6143 | This function can merge the results of the @code{dir} and
|
|---|
| 6144 | @code{notdir} functions, to produce the original list of files which
|
|---|
| 6145 | was given to those two functions.@refill
|
|---|
| 6146 |
|
|---|
| 6147 | @item $(wildcard @var{pattern})
|
|---|
| 6148 | @findex wildcard
|
|---|
| 6149 | @cindex wildcard, function
|
|---|
| 6150 | The argument @var{pattern} is a file name pattern, typically containing
|
|---|
| 6151 | wildcard characters (as in shell file name patterns). The result of
|
|---|
| 6152 | @code{wildcard} is a space-separated list of the names of existing files
|
|---|
| 6153 | that match the pattern.
|
|---|
| 6154 | @xref{Wildcards, ,Using Wildcard Characters in File Names}.
|
|---|
| 6155 | @end table
|
|---|
| 6156 |
|
|---|
| 6157 | @node Foreach Function, If Function, File Name Functions, Functions
|
|---|
| 6158 | @section The @code{foreach} Function
|
|---|
| 6159 | @findex foreach
|
|---|
| 6160 | @cindex words, iterating over
|
|---|
| 6161 |
|
|---|
| 6162 | The @code{foreach} function is very different from other functions. It
|
|---|
| 6163 | causes one piece of text to be used repeatedly, each time with a different
|
|---|
| 6164 | substitution performed on it. It resembles the @code{for} command in the
|
|---|
| 6165 | shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
|
|---|
| 6166 |
|
|---|
| 6167 | The syntax of the @code{foreach} function is:
|
|---|
| 6168 |
|
|---|
| 6169 | @example
|
|---|
| 6170 | $(foreach @var{var},@var{list},@var{text})
|
|---|
| 6171 | @end example
|
|---|
| 6172 |
|
|---|
| 6173 | @noindent
|
|---|
| 6174 | The first two arguments, @var{var} and @var{list}, are expanded before
|
|---|
| 6175 | anything else is done; note that the last argument, @var{text}, is
|
|---|
| 6176 | @strong{not} expanded at the same time. Then for each word of the expanded
|
|---|
| 6177 | value of @var{list}, the variable named by the expanded value of @var{var}
|
|---|
| 6178 | is set to that word, and @var{text} is expanded. Presumably @var{text}
|
|---|
| 6179 | contains references to that variable, so its expansion will be different
|
|---|
| 6180 | each time.
|
|---|
| 6181 |
|
|---|
| 6182 | The result is that @var{text} is expanded as many times as there are
|
|---|
| 6183 | whitespace-separated words in @var{list}. The multiple expansions of
|
|---|
| 6184 | @var{text} are concatenated, with spaces between them, to make the result
|
|---|
| 6185 | of @code{foreach}.
|
|---|
| 6186 |
|
|---|
| 6187 | This simple example sets the variable @samp{files} to the list of all files
|
|---|
| 6188 | in the directories in the list @samp{dirs}:
|
|---|
| 6189 |
|
|---|
| 6190 | @example
|
|---|
| 6191 | dirs := a b c d
|
|---|
| 6192 | files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
|
|---|
| 6193 | @end example
|
|---|
| 6194 |
|
|---|
| 6195 | Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
|
|---|
| 6196 | finds the value @samp{a} for @code{dir}, so it produces the same result
|
|---|
| 6197 | as @samp{$(wildcard a/*)}; the second repetition produces the result
|
|---|
| 6198 | of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
|
|---|
| 6199 |
|
|---|
| 6200 | This example has the same result (except for setting @samp{dirs}) as
|
|---|
| 6201 | the following example:
|
|---|
| 6202 |
|
|---|
| 6203 | @example
|
|---|
| 6204 | files := $(wildcard a/* b/* c/* d/*)
|
|---|
| 6205 | @end example
|
|---|
| 6206 |
|
|---|
| 6207 | When @var{text} is complicated, you can improve readability by giving it
|
|---|
| 6208 | a name, with an additional variable:
|
|---|
| 6209 |
|
|---|
| 6210 | @example
|
|---|
| 6211 | find_files = $(wildcard $(dir)/*)
|
|---|
| 6212 | dirs := a b c d
|
|---|
| 6213 | files := $(foreach dir,$(dirs),$(find_files))
|
|---|
| 6214 | @end example
|
|---|
| 6215 |
|
|---|
| 6216 | @noindent
|
|---|
| 6217 | Here we use the variable @code{find_files} this way. We use plain @samp{=}
|
|---|
| 6218 | to define a recursively-expanding variable, so that its value contains an
|
|---|
| 6219 | actual function call to be reexpanded under the control of @code{foreach};
|
|---|
| 6220 | a simply-expanded variable would not do, since @code{wildcard} would be
|
|---|
| 6221 | called only once at the time of defining @code{find_files}.
|
|---|
| 6222 |
|
|---|
| 6223 | The @code{foreach} function has no permanent effect on the variable
|
|---|
| 6224 | @var{var}; its value and flavor after the @code{foreach} function call are
|
|---|
| 6225 | the same as they were beforehand. The other values which are taken from
|
|---|
| 6226 | @var{list} are in effect only temporarily, during the execution of
|
|---|
| 6227 | @code{foreach}. The variable @var{var} is a simply-expanded variable
|
|---|
| 6228 | during the execution of @code{foreach}. If @var{var} was undefined
|
|---|
| 6229 | before the @code{foreach} function call, it is undefined after the call.
|
|---|
| 6230 | @xref{Flavors, ,The Two Flavors of Variables}.@refill
|
|---|
| 6231 |
|
|---|
| 6232 | You must take care when using complex variable expressions that result in
|
|---|
| 6233 | variable names because many strange things are valid variable names, but
|
|---|
| 6234 | are probably not what you intended. For example,
|
|---|
| 6235 |
|
|---|
| 6236 | @smallexample
|
|---|
| 6237 | files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
|
|---|
| 6238 | @end smallexample
|
|---|
| 6239 |
|
|---|
| 6240 | @noindent
|
|---|
| 6241 | might be useful if the value of @code{find_files} references the variable
|
|---|
| 6242 | whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
|
|---|
| 6243 | no?), but it is more likely to be a mistake.
|
|---|
| 6244 |
|
|---|
| 6245 | @node If Function, Call Function, Foreach Function, Functions
|
|---|
| 6246 | @section The @code{if} Function
|
|---|
| 6247 | @findex if
|
|---|
| 6248 | @cindex conditional expansion
|
|---|
| 6249 |
|
|---|
| 6250 | The @code{if} function provides support for conditional expansion in a
|
|---|
| 6251 | functional context (as opposed to the GNU @code{make} makefile
|
|---|
| 6252 | conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
|
|---|
| 6253 | Conditionals}).
|
|---|
| 6254 |
|
|---|
| 6255 | An @code{if} function call can contain either two or three arguments:
|
|---|
| 6256 |
|
|---|
| 6257 | @example
|
|---|
| 6258 | $(if @var{condition},@var{then-part}[,@var{else-part}])
|
|---|
| 6259 | @end example
|
|---|
| 6260 |
|
|---|
| 6261 | The first argument, @var{condition}, first has all preceding and
|
|---|
| 6262 | trailing whitespace stripped, then is expanded. If it expands to any
|
|---|
| 6263 | non-empty string, then the condition is considered to be true. If it
|
|---|
| 6264 | expands to an empty string, the condition is considered to be false.
|
|---|
| 6265 |
|
|---|
| 6266 | If the condition is true then the second argument, @var{then-part}, is
|
|---|
| 6267 | evaluated and this is used as the result of the evaluation of the entire
|
|---|
| 6268 | @code{if} function.
|
|---|
| 6269 |
|
|---|
| 6270 | If the condition is false then the third argument, @var{else-part}, is
|
|---|
| 6271 | evaluated and this is the result of the @code{if} function. If there is
|
|---|
| 6272 | no third argument, the @code{if} function evaluates to nothing (the
|
|---|
| 6273 | empty string).
|
|---|
| 6274 |
|
|---|
| 6275 | Note that only one of the @var{then-part} or the @var{else-part} will be
|
|---|
| 6276 | evaluated, never both. Thus, either can contain side-effects (such as
|
|---|
| 6277 | @code{shell} function calls, etc.)
|
|---|
| 6278 |
|
|---|
| 6279 | @node Call Function, Value Function, If Function, Functions
|
|---|
| 6280 | @section The @code{call} Function
|
|---|
| 6281 | @findex call
|
|---|
| 6282 | @cindex functions, user defined
|
|---|
| 6283 | @cindex user defined functions
|
|---|
| 6284 |
|
|---|
| 6285 | The @code{call} function is unique in that it can be used to create new
|
|---|
| 6286 | parameterized functions. You can write a complex expression as the
|
|---|
| 6287 | value of a variable, then use @code{call} to expand it with different
|
|---|
| 6288 | values.
|
|---|
| 6289 |
|
|---|
| 6290 | The syntax of the @code{call} function is:
|
|---|
| 6291 |
|
|---|
| 6292 | @example
|
|---|
| 6293 | $(call @var{variable},@var{param},@var{param},@dots{})
|
|---|
| 6294 | @end example
|
|---|
| 6295 |
|
|---|
| 6296 | When @code{make} expands this function, it assigns each @var{param} to
|
|---|
| 6297 | temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
|
|---|
| 6298 | @code{$(0)} will contain @var{variable}. There is no maximum number of
|
|---|
| 6299 | parameter arguments. There is no minimum, either, but it doesn't make
|
|---|
| 6300 | sense to use @code{call} with no parameters.
|
|---|
| 6301 |
|
|---|
| 6302 | Then @var{variable} is expanded as a @code{make} variable in the context
|
|---|
| 6303 | of these temporary assignments. Thus, any reference to @code{$(1)} in
|
|---|
| 6304 | the value of @var{variable} will resolve to the first @var{param} in the
|
|---|
| 6305 | invocation of @code{call}.
|
|---|
| 6306 |
|
|---|
| 6307 | Note that @var{variable} is the @emph{name} of a variable, not a
|
|---|
| 6308 | @emph{reference} to that variable. Therefore you would not normally use
|
|---|
| 6309 | a @samp{$} or parentheses when writing it. (You can, however, use a
|
|---|
| 6310 | variable reference in the name if you want the name not to be a
|
|---|
| 6311 | constant.)
|
|---|
| 6312 |
|
|---|
| 6313 | If @var{variable} is the name of a builtin function, the builtin function
|
|---|
| 6314 | is always invoked (even if a @code{make} variable by that name also
|
|---|
| 6315 | exists).
|
|---|
| 6316 |
|
|---|
| 6317 | The @code{call} function expands the @var{param} arguments before
|
|---|
| 6318 | assigning them to temporary variables. This means that @var{variable}
|
|---|
| 6319 | values containing references to builtin functions that have special
|
|---|
| 6320 | expansion rules, like @code{foreach} or @code{if}, may not work as you
|
|---|
| 6321 | expect.
|
|---|
| 6322 |
|
|---|
| 6323 | Some examples may make this clearer.
|
|---|
| 6324 |
|
|---|
| 6325 | This macro simply reverses its arguments:
|
|---|
| 6326 |
|
|---|
| 6327 | @smallexample
|
|---|
| 6328 | reverse = $(2) $(1)
|
|---|
| 6329 |
|
|---|
| 6330 | foo = $(call reverse,a,b)
|
|---|
| 6331 | @end smallexample
|
|---|
| 6332 |
|
|---|
| 6333 | @noindent
|
|---|
| 6334 | Here @var{foo} will contain @samp{b a}.
|
|---|
| 6335 |
|
|---|
| 6336 | This one is slightly more interesting: it defines a macro to search for
|
|---|
| 6337 | the first instance of a program in @code{PATH}:
|
|---|
| 6338 |
|
|---|
| 6339 | @smallexample
|
|---|
| 6340 | pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
|
|---|
| 6341 |
|
|---|
| 6342 | LS := $(call pathsearch,ls)
|
|---|
| 6343 | @end smallexample
|
|---|
| 6344 |
|
|---|
| 6345 | @noindent
|
|---|
| 6346 | Now the variable LS contains @code{/bin/ls} or similar.
|
|---|
| 6347 |
|
|---|
| 6348 | The @code{call} function can be nested. Each recursive invocation gets
|
|---|
| 6349 | its own local values for @code{$(1)}, etc. that mask the values of
|
|---|
| 6350 | higher-level @code{call}. For example, here is an implementation of a
|
|---|
| 6351 | @dfn{map} function:
|
|---|
| 6352 |
|
|---|
| 6353 | @smallexample
|
|---|
| 6354 | map = $(foreach a,$(2),$(call $(1),$(a)))
|
|---|
| 6355 | @end smallexample
|
|---|
| 6356 |
|
|---|
| 6357 | Now you can @var{map} a function that normally takes only one argument,
|
|---|
| 6358 | such as @code{origin}, to multiple values in one step:
|
|---|
| 6359 |
|
|---|
| 6360 | @smallexample
|
|---|
| 6361 | o = $(call map,origin,o map MAKE)
|
|---|
| 6362 | @end smallexample
|
|---|
| 6363 |
|
|---|
| 6364 | and end up with @var{o} containing something like @samp{file file default}.
|
|---|
| 6365 |
|
|---|
| 6366 | A final caution: be careful when adding whitespace to the arguments to
|
|---|
| 6367 | @code{call}. As with other functions, any whitespace contained in the
|
|---|
| 6368 | second and subsequent arguments is kept; this can cause strange
|
|---|
| 6369 | effects. It's generally safest to remove all extraneous whitespace when
|
|---|
| 6370 | providing parameters to @code{call}.
|
|---|
| 6371 |
|
|---|
| 6372 | @node Value Function, Eval Function, Call Function, Functions
|
|---|
| 6373 | @comment node-name, next, previous, up
|
|---|
| 6374 | @section The @code{value} Function
|
|---|
| 6375 | @findex value
|
|---|
| 6376 | @cindex variables, unexpanded value
|
|---|
| 6377 |
|
|---|
| 6378 | The @code{value} function provides a way for you to use the value of a
|
|---|
| 6379 | variable @emph{without} having it expanded. Please note that this
|
|---|
| 6380 | does not undo expansions which have already occurred; for example if
|
|---|
| 6381 | you create a simply expanded variable its value is expanded during the
|
|---|
| 6382 | definition; in that case the @code{value} function will return the
|
|---|
| 6383 | same result as using the variable directly.
|
|---|
| 6384 |
|
|---|
| 6385 | The syntax of the @code{value} function is:
|
|---|
| 6386 |
|
|---|
| 6387 | @example
|
|---|
| 6388 | $(value @var{variable})
|
|---|
| 6389 | @end example
|
|---|
| 6390 |
|
|---|
| 6391 | Note that @var{variable} is the @emph{name} of a variable; not a
|
|---|
| 6392 | @emph{reference} to that variable. Therefore you would not normally
|
|---|
| 6393 | use a @samp{$} or parentheses when writing it. (You can, however, use
|
|---|
| 6394 | a variable reference in the name if you want the name not to be a
|
|---|
| 6395 | constant.)
|
|---|
| 6396 |
|
|---|
| 6397 | The result of this function is a string containing the value of
|
|---|
| 6398 | @var{variable}, without any expansion occurring. For example, in this
|
|---|
| 6399 | makefile:
|
|---|
| 6400 |
|
|---|
| 6401 | @example
|
|---|
| 6402 | @group
|
|---|
| 6403 | FOO = $PATH
|
|---|
| 6404 |
|
|---|
| 6405 | all:
|
|---|
| 6406 | @@echo $(FOO)
|
|---|
| 6407 | @@echo $(value FOO)
|
|---|
| 6408 | @end group
|
|---|
| 6409 | @end example
|
|---|
| 6410 |
|
|---|
| 6411 | @noindent
|
|---|
| 6412 | The first output line would be @code{ATH}, since the ``$P'' would be
|
|---|
| 6413 | expanded as a @code{make} variable, while the second output line would
|
|---|
| 6414 | be the current value of your @code{$PATH} environment variable, since
|
|---|
| 6415 | the @code{value} function avoided the expansion.
|
|---|
| 6416 |
|
|---|
| 6417 | The @code{value} function is most often used in conjunction with the
|
|---|
| 6418 | @code{eval} function (@pxref{Eval Function}).
|
|---|
| 6419 |
|
|---|
| 6420 | @node Eval Function, Origin Function, Value Function, Functions
|
|---|
| 6421 | @comment node-name, next, previous, up
|
|---|
| 6422 | @section The @code{eval} Function
|
|---|
| 6423 | @findex eval
|
|---|
| 6424 | @cindex evaluating makefile syntax
|
|---|
| 6425 | @cindex makefile syntax, evaluating
|
|---|
| 6426 |
|
|---|
| 6427 | The @code{eval} function is very special: it allows you to define new
|
|---|
| 6428 | makefile constructs that are not constant; which are the result of
|
|---|
| 6429 | evaluating other variables and functions. The argument to the
|
|---|
| 6430 | @code{eval} function is expanded, then the results of that expansion
|
|---|
| 6431 | are parsed as makefile syntax. The expanded results can define new
|
|---|
| 6432 | @code{make} variables, targets, implicit or explicit rules, etc.
|
|---|
| 6433 |
|
|---|
| 6434 | The result of the @code{eval} function is always the empty string;
|
|---|
| 6435 | thus, it can be placed virtually anywhere in a makefile without
|
|---|
| 6436 | causing syntax errors.
|
|---|
| 6437 |
|
|---|
| 6438 | It's important to realize that the @code{eval} argument is expanded
|
|---|
| 6439 | @emph{twice}; first by the @code{eval} function, then the results of
|
|---|
| 6440 | that expansion are expanded again when they are parsed as makefile
|
|---|
| 6441 | syntax. This means you may need to provide extra levels of escaping
|
|---|
| 6442 | for ``$'' characters when using @code{eval}. The @code{value}
|
|---|
| 6443 | function (@pxref{Value Function}) can sometimes be useful in these
|
|---|
| 6444 | situations, to circumvent unwanted expansions.
|
|---|
| 6445 |
|
|---|
| 6446 | Here is an example of how @code{eval} can be used; this example
|
|---|
| 6447 | combines a number of concepts and other functions. Although it might
|
|---|
| 6448 | seem overly complex to use @code{eval} in this example, rather than
|
|---|
| 6449 | just writing out the rules, consider two things: first, the template
|
|---|
| 6450 | definition (in @code{PROGRAM_template}) could need to be much more
|
|---|
| 6451 | complex than it is here; and second, you might put the complex,
|
|---|
| 6452 | ``generic'' part of this example into another makefile, then include
|
|---|
| 6453 | it in all the individual makefiles. Now your individual makefiles are
|
|---|
| 6454 | quite straightforward.
|
|---|
| 6455 |
|
|---|
| 6456 | @example
|
|---|
| 6457 | @group
|
|---|
| 6458 | PROGRAMS = server client
|
|---|
| 6459 |
|
|---|
| 6460 | server_OBJS = server.o server_priv.o server_access.o
|
|---|
| 6461 | server_LIBS = priv protocol
|
|---|
| 6462 |
|
|---|
| 6463 | client_OBJS = client.o client_api.o client_mem.o
|
|---|
| 6464 | client_LIBS = protocol
|
|---|
| 6465 |
|
|---|
| 6466 | # Everything after this is generic
|
|---|
| 6467 |
|
|---|
| 6468 | .PHONY: all
|
|---|
| 6469 | all: $(PROGRAMS)
|
|---|
| 6470 |
|
|---|
| 6471 | define PROGRAM_template
|
|---|
| 6472 | $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
|
|---|
| 6473 | ALL_OBJS += $$($(1)_OBJS)
|
|---|
| 6474 | endef
|
|---|
| 6475 |
|
|---|
| 6476 | $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
|
|---|
| 6477 |
|
|---|
| 6478 | $(PROGRAMS):
|
|---|
| 6479 | $(LINK.o) $^ $(LDLIBS) -o $@@
|
|---|
| 6480 |
|
|---|
| 6481 | clean:
|
|---|
| 6482 | rm -f $(ALL_OBJS) $(PROGRAMS)
|
|---|
| 6483 | @end group
|
|---|
| 6484 | @end example
|
|---|
| 6485 |
|
|---|
| 6486 | @node Origin Function, Shell Function, Eval Function, Functions
|
|---|
| 6487 | @section The @code{origin} Function
|
|---|
| 6488 | @findex origin
|
|---|
| 6489 | @cindex variables, origin of
|
|---|
| 6490 | @cindex origin of variable
|
|---|
| 6491 |
|
|---|
| 6492 | The @code{origin} function is unlike most other functions in that it does
|
|---|
| 6493 | not operate on the values of variables; it tells you something @emph{about}
|
|---|
| 6494 | a variable. Specifically, it tells you where it came from.
|
|---|
| 6495 |
|
|---|
| 6496 | The syntax of the @code{origin} function is:
|
|---|
| 6497 |
|
|---|
| 6498 | @example
|
|---|
| 6499 | $(origin @var{variable})
|
|---|
| 6500 | @end example
|
|---|
| 6501 |
|
|---|
| 6502 | Note that @var{variable} is the @emph{name} of a variable to inquire about;
|
|---|
| 6503 | not a @emph{reference} to that variable. Therefore you would not normally
|
|---|
| 6504 | use a @samp{$} or parentheses when writing it. (You can, however, use a
|
|---|
| 6505 | variable reference in the name if you want the name not to be a constant.)
|
|---|
| 6506 |
|
|---|
| 6507 | The result of this function is a string telling you how the variable
|
|---|
| 6508 | @var{variable} was defined:
|
|---|
| 6509 |
|
|---|
| 6510 | @table @samp
|
|---|
| 6511 | @item undefined
|
|---|
| 6512 |
|
|---|
| 6513 | if @var{variable} was never defined.
|
|---|
| 6514 |
|
|---|
| 6515 | @item default
|
|---|
| 6516 |
|
|---|
| 6517 | if @var{variable} has a default definition, as is usual with @code{CC}
|
|---|
| 6518 | and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 6519 | Note that if you have redefined a default variable, the @code{origin}
|
|---|
| 6520 | function will return the origin of the later definition.
|
|---|
| 6521 |
|
|---|
| 6522 | @item environment
|
|---|
| 6523 |
|
|---|
| 6524 | if @var{variable} was defined as an environment variable and the
|
|---|
| 6525 | @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
|
|---|
| 6526 |
|
|---|
| 6527 | @item environment override
|
|---|
| 6528 |
|
|---|
| 6529 | if @var{variable} was defined as an environment variable and the
|
|---|
| 6530 | @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
|
|---|
| 6531 | ,Summary of Options}).@refill
|
|---|
| 6532 |
|
|---|
| 6533 | @item file
|
|---|
| 6534 |
|
|---|
| 6535 | if @var{variable} was defined in a makefile.
|
|---|
| 6536 |
|
|---|
| 6537 | @item command line
|
|---|
| 6538 |
|
|---|
| 6539 | if @var{variable} was defined on the command line.
|
|---|
| 6540 |
|
|---|
| 6541 | @item override
|
|---|
| 6542 |
|
|---|
| 6543 | if @var{variable} was defined with an @code{override} directive in a
|
|---|
| 6544 | makefile (@pxref{Override Directive, ,The @code{override} Directive}).
|
|---|
| 6545 |
|
|---|
| 6546 | @item automatic
|
|---|
| 6547 |
|
|---|
| 6548 | if @var{variable} is an automatic variable defined for the
|
|---|
| 6549 | execution of the commands for each rule
|
|---|
| 6550 | (@pxref{Automatic Variables}).
|
|---|
| 6551 | @end table
|
|---|
| 6552 |
|
|---|
| 6553 | This information is primarily useful (other than for your curiosity) to
|
|---|
| 6554 | determine if you want to believe the value of a variable. For example,
|
|---|
| 6555 | suppose you have a makefile @file{foo} that includes another makefile
|
|---|
| 6556 | @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
|
|---|
| 6557 | if you run the command @w{@samp{make -f bar}}, even if the environment contains
|
|---|
| 6558 | a definition of @code{bletch}. However, if @file{foo} defined
|
|---|
| 6559 | @code{bletch} before including @file{bar}, you do not want to override that
|
|---|
| 6560 | definition. This could be done by using an @code{override} directive in
|
|---|
| 6561 | @file{foo}, giving that definition precedence over the later definition in
|
|---|
| 6562 | @file{bar}; unfortunately, the @code{override} directive would also
|
|---|
| 6563 | override any command line definitions. So, @file{bar} could
|
|---|
| 6564 | include:@refill
|
|---|
| 6565 |
|
|---|
| 6566 | @example
|
|---|
| 6567 | @group
|
|---|
| 6568 | ifdef bletch
|
|---|
| 6569 | ifeq "$(origin bletch)" "environment"
|
|---|
| 6570 | bletch = barf, gag, etc.
|
|---|
| 6571 | endif
|
|---|
| 6572 | endif
|
|---|
| 6573 | @end group
|
|---|
| 6574 | @end example
|
|---|
| 6575 |
|
|---|
| 6576 | @noindent
|
|---|
| 6577 | If @code{bletch} has been defined from the environment, this will redefine
|
|---|
| 6578 | it.
|
|---|
| 6579 |
|
|---|
| 6580 | If you want to override a previous definition of @code{bletch} if it came
|
|---|
| 6581 | from the environment, even under @samp{-e}, you could instead write:
|
|---|
| 6582 |
|
|---|
| 6583 | @example
|
|---|
| 6584 | @group
|
|---|
| 6585 | ifneq "$(findstring environment,$(origin bletch))" ""
|
|---|
| 6586 | bletch = barf, gag, etc.
|
|---|
| 6587 | endif
|
|---|
| 6588 | @end group
|
|---|
| 6589 | @end example
|
|---|
| 6590 |
|
|---|
| 6591 | Here the redefinition takes place if @samp{$(origin bletch)} returns either
|
|---|
| 6592 | @samp{environment} or @samp{environment override}.
|
|---|
| 6593 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 6594 |
|
|---|
| 6595 | @node Shell Function, Make Control Functions, Origin Function, Functions
|
|---|
| 6596 | @section The @code{shell} Function
|
|---|
| 6597 | @findex shell
|
|---|
| 6598 | @cindex commands, expansion
|
|---|
| 6599 | @cindex backquotes
|
|---|
| 6600 | @cindex shell command, function for
|
|---|
| 6601 |
|
|---|
| 6602 | The @code{shell} function is unlike any other function other than the
|
|---|
| 6603 | @code{wildcard} function
|
|---|
| 6604 | (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
|
|---|
| 6605 | communicates with the world outside of @code{make}.
|
|---|
| 6606 |
|
|---|
| 6607 | The @code{shell} function performs the same function that backquotes
|
|---|
| 6608 | (@samp{`}) perform in most shells: it does @dfn{command expansion}.
|
|---|
| 6609 | This means that it takes as an argument a shell command and evaluates
|
|---|
| 6610 | to the output of the command. The only processing @code{make} does on
|
|---|
| 6611 | the result is to convert each newline (or carriage-return / newline
|
|---|
| 6612 | pair) to a single space. If there is a trailing (carriage-return
|
|---|
| 6613 | and) newline it will simply be removed.@refill
|
|---|
| 6614 |
|
|---|
| 6615 | The commands run by calls to the @code{shell} function are run when the
|
|---|
| 6616 | function calls are expanded (@pxref{Reading Makefiles, , How
|
|---|
| 6617 | @code{make} Reads a Makefile}). Because this function involves
|
|---|
| 6618 | spawning a new shell, you should carefully consider the performance
|
|---|
| 6619 | implications of using the @code{shell} function within recursively
|
|---|
| 6620 | expanded variables vs. simply expanded variables (@pxref{Flavors, ,The
|
|---|
| 6621 | Two Flavors of Variables}).
|
|---|
| 6622 |
|
|---|
| 6623 | Here are some examples of the use of the @code{shell} function:
|
|---|
| 6624 |
|
|---|
| 6625 | @example
|
|---|
| 6626 | contents := $(shell cat foo)
|
|---|
| 6627 | @end example
|
|---|
| 6628 |
|
|---|
| 6629 | @noindent
|
|---|
| 6630 | sets @code{contents} to the contents of the file @file{foo}, with a space
|
|---|
| 6631 | (rather than a newline) separating each line.
|
|---|
| 6632 |
|
|---|
| 6633 | @example
|
|---|
| 6634 | files := $(shell echo *.c)
|
|---|
| 6635 | @end example
|
|---|
| 6636 |
|
|---|
| 6637 | @noindent
|
|---|
| 6638 | sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
|
|---|
| 6639 | using a very strange shell, this has the same result as
|
|---|
| 6640 | @w{@samp{$(wildcard *.c)}}.@refill
|
|---|
| 6641 |
|
|---|
| 6642 | @node Make Control Functions, , Shell Function, Functions
|
|---|
| 6643 | @section Functions That Control Make
|
|---|
| 6644 | @cindex functions, for controlling make
|
|---|
| 6645 | @cindex controlling make
|
|---|
| 6646 |
|
|---|
| 6647 | These functions control the way make runs. Generally, they are used to
|
|---|
| 6648 | provide information to the user of the makefile or to cause make to stop
|
|---|
| 6649 | if some sort of environmental error is detected.
|
|---|
| 6650 |
|
|---|
| 6651 | @table @code
|
|---|
| 6652 | @item $(error @var{text}@dots{})
|
|---|
| 6653 | @findex error
|
|---|
| 6654 | @cindex error, stopping on
|
|---|
| 6655 | @cindex stopping make
|
|---|
| 6656 | Generates a fatal error where the message is @var{text}. Note that the
|
|---|
| 6657 | error is generated whenever this function is evaluated. So, if you put
|
|---|
| 6658 | it inside a command script or on the right side of a recursive variable
|
|---|
| 6659 | assignment, it won't be evaluated until later. The @var{text} will be
|
|---|
| 6660 | expanded before the error is generated.
|
|---|
| 6661 |
|
|---|
| 6662 | For example,
|
|---|
| 6663 |
|
|---|
| 6664 | @example
|
|---|
| 6665 | ifdef ERROR1
|
|---|
| 6666 | $(error error is $(ERROR1))
|
|---|
| 6667 | endif
|
|---|
| 6668 | @end example
|
|---|
| 6669 |
|
|---|
| 6670 | @noindent
|
|---|
| 6671 | will generate a fatal error during the read of the makefile if the
|
|---|
| 6672 | @code{make} variable @code{ERROR1} is defined. Or,
|
|---|
| 6673 |
|
|---|
| 6674 | @example
|
|---|
| 6675 | ERR = $(error found an error!)
|
|---|
| 6676 |
|
|---|
| 6677 | .PHONY: err
|
|---|
| 6678 | err: ; $(ERR)
|
|---|
| 6679 | @end example
|
|---|
| 6680 |
|
|---|
| 6681 | @noindent
|
|---|
| 6682 | will generate a fatal error while @code{make} is running, if the
|
|---|
| 6683 | @code{err} target is invoked.
|
|---|
| 6684 |
|
|---|
| 6685 | @item $(warning @var{text}@dots{})
|
|---|
| 6686 | @findex warning
|
|---|
| 6687 | @cindex warnings, printing
|
|---|
| 6688 | @cindex printing user warnings
|
|---|
| 6689 | This function works similarly to the @code{error} function, above,
|
|---|
| 6690 | except that @code{make} doesn't exit. Instead, @var{text} is expanded
|
|---|
| 6691 | and the resulting message is displayed, but processing of the makefile
|
|---|
| 6692 | continues.
|
|---|
| 6693 |
|
|---|
| 6694 | The result of the expansion of this function is the empty string.
|
|---|
| 6695 | @end table
|
|---|
| 6696 |
|
|---|
| 6697 | @node Running, Implicit Rules, Functions, Top
|
|---|
| 6698 | @chapter How to Run @code{make}
|
|---|
| 6699 |
|
|---|
| 6700 | A makefile that says how to recompile a program can be used in more
|
|---|
| 6701 | than one way. The simplest use is to recompile every file that is out
|
|---|
| 6702 | of date. Usually, makefiles are written so that if you run
|
|---|
| 6703 | @code{make} with no arguments, it does just that.
|
|---|
| 6704 |
|
|---|
| 6705 | But you might want to update only some of the files; you might want to use
|
|---|
| 6706 | a different compiler or different compiler options; you might want just to
|
|---|
| 6707 | find out which files are out of date without changing them.
|
|---|
| 6708 |
|
|---|
| 6709 | By giving arguments when you run @code{make}, you can do any of these
|
|---|
| 6710 | things and many others.
|
|---|
| 6711 |
|
|---|
| 6712 | The exit status of @code{make} is always one of three values:
|
|---|
| 6713 | @table @code
|
|---|
| 6714 | @item 0
|
|---|
| 6715 | The exit status is zero if @code{make} is successful.
|
|---|
| 6716 | @item 2
|
|---|
| 6717 | The exit status is two if @code{make} encounters any errors.
|
|---|
| 6718 | It will print messages describing the particular errors.
|
|---|
| 6719 | @item 1
|
|---|
| 6720 | The exit status is one if you use the @samp{-q} flag and @code{make}
|
|---|
| 6721 | determines that some target is not already up to date.
|
|---|
| 6722 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 6723 | @end table
|
|---|
| 6724 |
|
|---|
| 6725 | @menu
|
|---|
| 6726 | * Makefile Arguments:: How to specify which makefile to use.
|
|---|
| 6727 | * Goals:: How to use goal arguments to specify which
|
|---|
| 6728 | parts of the makefile to use.
|
|---|
| 6729 | * Instead of Execution:: How to use mode flags to specify what
|
|---|
| 6730 | kind of thing to do with the commands
|
|---|
| 6731 | in the makefile other than simply
|
|---|
| 6732 | execute them.
|
|---|
| 6733 | * Avoiding Compilation:: How to avoid recompiling certain files.
|
|---|
| 6734 | * Overriding:: How to override a variable to specify
|
|---|
| 6735 | an alternate compiler and other things.
|
|---|
| 6736 | * Testing:: How to proceed past some errors, to
|
|---|
| 6737 | test compilation.
|
|---|
| 6738 | * Options Summary:: Summary of Options
|
|---|
| 6739 | @end menu
|
|---|
| 6740 |
|
|---|
| 6741 | @node Makefile Arguments, Goals, Running, Running
|
|---|
| 6742 | @section Arguments to Specify the Makefile
|
|---|
| 6743 | @cindex @code{--file}
|
|---|
| 6744 | @cindex @code{--makefile}
|
|---|
| 6745 | @cindex @code{-f}
|
|---|
| 6746 |
|
|---|
| 6747 | The way to specify the name of the makefile is with the @samp{-f} or
|
|---|
| 6748 | @samp{--file} option (@samp{--makefile} also works). For example,
|
|---|
| 6749 | @samp{-f altmake} says to use the file @file{altmake} as the makefile.
|
|---|
| 6750 |
|
|---|
| 6751 | If you use the @samp{-f} flag several times and follow each @samp{-f}
|
|---|
| 6752 | with an argument, all the specified files are used jointly as
|
|---|
| 6753 | makefiles.
|
|---|
| 6754 |
|
|---|
| 6755 | If you do not use the @samp{-f} or @samp{--file} flag, the default is
|
|---|
| 6756 | to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
|
|---|
| 6757 | that order, and use the first of these three which exists or can be made
|
|---|
| 6758 | (@pxref{Makefiles, ,Writing Makefiles}).@refill
|
|---|
| 6759 |
|
|---|
| 6760 | @node Goals, Instead of Execution, Makefile Arguments, Running
|
|---|
| 6761 | @section Arguments to Specify the Goals
|
|---|
| 6762 | @cindex goal, how to specify
|
|---|
| 6763 |
|
|---|
| 6764 | The @dfn{goals} are the targets that @code{make} should strive ultimately
|
|---|
| 6765 | to update. Other targets are updated as well if they appear as
|
|---|
| 6766 | prerequisites of goals, or prerequisites of prerequisites of goals, etc.
|
|---|
| 6767 |
|
|---|
| 6768 | By default, the goal is the first target in the makefile (not counting
|
|---|
| 6769 | targets that start with a period). Therefore, makefiles are usually
|
|---|
| 6770 | written so that the first target is for compiling the entire program or
|
|---|
| 6771 | programs they describe. If the first rule in the makefile has several
|
|---|
| 6772 | targets, only the first target in the rule becomes the default goal, not
|
|---|
| 6773 | the whole list.
|
|---|
| 6774 |
|
|---|
| 6775 | You can specify a different goal or goals with arguments to @code{make}.
|
|---|
| 6776 | Use the name of the goal as an argument. If you specify several goals,
|
|---|
| 6777 | @code{make} processes each of them in turn, in the order you name them.
|
|---|
| 6778 |
|
|---|
| 6779 | Any target in the makefile may be specified as a goal (unless it
|
|---|
| 6780 | starts with @samp{-} or contains an @samp{=}, in which case it will be
|
|---|
| 6781 | parsed as a switch or variable definition, respectively). Even
|
|---|
| 6782 | targets not in the makefile may be specified, if @code{make} can find
|
|---|
| 6783 | implicit rules that say how to make them.
|
|---|
| 6784 |
|
|---|
| 6785 | @cindex @code{MAKECMDGOALS}
|
|---|
| 6786 | @vindex MAKECMDGOALS
|
|---|
| 6787 | @code{Make} will set the special variable @code{MAKECMDGOALS} to the
|
|---|
| 6788 | list of goals you specified on the command line. If no goals were given
|
|---|
| 6789 | on the command line, this variable is empty. Note that this variable
|
|---|
| 6790 | should be used only in special circumstances.
|
|---|
| 6791 |
|
|---|
| 6792 | An example of appropriate use is to avoid including @file{.d} files
|
|---|
| 6793 | during @code{clean} rules (@pxref{Automatic Prerequisites}), so
|
|---|
| 6794 | @code{make} won't create them only to immediately remove them
|
|---|
| 6795 | again:@refill
|
|---|
| 6796 |
|
|---|
| 6797 | @example
|
|---|
| 6798 | @group
|
|---|
| 6799 | sources = foo.c bar.c
|
|---|
| 6800 |
|
|---|
| 6801 | ifneq ($(MAKECMDGOALS),clean)
|
|---|
| 6802 | include $(sources:.c=.d)
|
|---|
| 6803 | endif
|
|---|
| 6804 | @end group
|
|---|
| 6805 | @end example
|
|---|
| 6806 |
|
|---|
| 6807 | One use of specifying a goal is if you want to compile only a part of
|
|---|
| 6808 | the program, or only one of several programs. Specify as a goal each
|
|---|
| 6809 | file that you wish to remake. For example, consider a directory containing
|
|---|
| 6810 | several programs, with a makefile that starts like this:
|
|---|
| 6811 |
|
|---|
| 6812 | @example
|
|---|
| 6813 | .PHONY: all
|
|---|
| 6814 | all: size nm ld ar as
|
|---|
| 6815 | @end example
|
|---|
| 6816 |
|
|---|
| 6817 | If you are working on the program @code{size}, you might want to say
|
|---|
| 6818 | @w{@samp{make size}} so that only the files of that program are recompiled.
|
|---|
| 6819 |
|
|---|
| 6820 | Another use of specifying a goal is to make files that are not normally
|
|---|
| 6821 | made. For example, there may be a file of debugging output, or a
|
|---|
| 6822 | version of the program that is compiled specially for testing, which has
|
|---|
| 6823 | a rule in the makefile but is not a prerequisite of the default goal.
|
|---|
| 6824 |
|
|---|
| 6825 | Another use of specifying a goal is to run the commands associated with
|
|---|
| 6826 | a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
|
|---|
| 6827 | Targets, ,Empty Target Files to Record Events}). Many makefiles contain
|
|---|
| 6828 | a phony target named @file{clean} which deletes everything except source
|
|---|
| 6829 | files. Naturally, this is done only if you request it explicitly with
|
|---|
| 6830 | @w{@samp{make clean}}. Following is a list of typical phony and empty
|
|---|
| 6831 | target names. @xref{Standard Targets}, for a detailed list of all the
|
|---|
| 6832 | standard target names which GNU software packages use.
|
|---|
| 6833 |
|
|---|
| 6834 | @table @file
|
|---|
| 6835 | @item all
|
|---|
| 6836 | @cindex @code{all} @r{(standard target)}
|
|---|
| 6837 | Make all the top-level targets the makefile knows about.
|
|---|
| 6838 |
|
|---|
| 6839 | @item clean
|
|---|
| 6840 | @cindex @code{clean} @r{(standard target)}
|
|---|
| 6841 | Delete all files that are normally created by running @code{make}.
|
|---|
| 6842 |
|
|---|
| 6843 | @item mostlyclean
|
|---|
| 6844 | @cindex @code{mostlyclean} @r{(standard target)}
|
|---|
| 6845 | Like @samp{clean}, but may refrain from deleting a few files that people
|
|---|
| 6846 | normally don't want to recompile. For example, the @samp{mostlyclean}
|
|---|
| 6847 | target for GCC does not delete @file{libgcc.a}, because recompiling it
|
|---|
| 6848 | is rarely necessary and takes a lot of time.
|
|---|
| 6849 |
|
|---|
| 6850 | @item distclean
|
|---|
| 6851 | @cindex @code{distclean} @r{(standard target)}
|
|---|
| 6852 | @itemx realclean
|
|---|
| 6853 | @cindex @code{realclean} @r{(standard target)}
|
|---|
| 6854 | @itemx clobber
|
|---|
| 6855 | @cindex @code{clobber} @r{(standard target)}
|
|---|
| 6856 | Any of these targets might be defined to delete @emph{more} files than
|
|---|
| 6857 | @samp{clean} does. For example, this would delete configuration files
|
|---|
| 6858 | or links that you would normally create as preparation for compilation,
|
|---|
| 6859 | even if the makefile itself cannot create these files.
|
|---|
| 6860 |
|
|---|
| 6861 | @item install
|
|---|
| 6862 | @cindex @code{install} @r{(standard target)}
|
|---|
| 6863 | Copy the executable file into a directory that users typically search
|
|---|
| 6864 | for commands; copy any auxiliary files that the executable uses into
|
|---|
| 6865 | the directories where it will look for them.
|
|---|
| 6866 |
|
|---|
| 6867 | @item print
|
|---|
| 6868 | @cindex @code{print} @r{(standard target)}
|
|---|
| 6869 | Print listings of the source files that have changed.
|
|---|
| 6870 |
|
|---|
| 6871 | @item tar
|
|---|
| 6872 | @cindex @code{tar} @r{(standard target)}
|
|---|
| 6873 | Create a tar file of the source files.
|
|---|
| 6874 |
|
|---|
| 6875 | @item shar
|
|---|
| 6876 | @cindex @code{shar} @r{(standard target)}
|
|---|
| 6877 | Create a shell archive (shar file) of the source files.
|
|---|
| 6878 |
|
|---|
| 6879 | @item dist
|
|---|
| 6880 | @cindex @code{dist} @r{(standard target)}
|
|---|
| 6881 | Create a distribution file of the source files. This might
|
|---|
| 6882 | be a tar file, or a shar file, or a compressed version of one of the
|
|---|
| 6883 | above, or even more than one of the above.
|
|---|
| 6884 |
|
|---|
| 6885 | @item TAGS
|
|---|
| 6886 | @cindex @code{TAGS} @r{(standard target)}
|
|---|
| 6887 | Update a tags table for this program.
|
|---|
| 6888 |
|
|---|
| 6889 | @item check
|
|---|
| 6890 | @cindex @code{check} @r{(standard target)}
|
|---|
| 6891 | @itemx test
|
|---|
| 6892 | @cindex @code{test} @r{(standard target)}
|
|---|
| 6893 | Perform self tests on the program this makefile builds.
|
|---|
| 6894 | @end table
|
|---|
| 6895 |
|
|---|
| 6896 | @node Instead of Execution, Avoiding Compilation, Goals, Running
|
|---|
| 6897 | @section Instead of Executing the Commands
|
|---|
| 6898 | @cindex execution, instead of
|
|---|
| 6899 | @cindex commands, instead of executing
|
|---|
| 6900 |
|
|---|
| 6901 | The makefile tells @code{make} how to tell whether a target is up to date,
|
|---|
| 6902 | and how to update each target. But updating the targets is not always
|
|---|
| 6903 | what you want. Certain options specify other activities for @code{make}.
|
|---|
| 6904 |
|
|---|
| 6905 | @comment Extra blank lines make it print better.
|
|---|
| 6906 | @table @samp
|
|---|
| 6907 | @item -n
|
|---|
| 6908 | @itemx --just-print
|
|---|
| 6909 | @itemx --dry-run
|
|---|
| 6910 | @itemx --recon
|
|---|
| 6911 | @cindex @code{--just-print}
|
|---|
| 6912 | @cindex @code{--dry-run}
|
|---|
| 6913 | @cindex @code{--recon}
|
|---|
| 6914 | @cindex @code{-n}
|
|---|
| 6915 |
|
|---|
| 6916 | ``No-op''. The activity is to print what commands would be used to make
|
|---|
| 6917 | the targets up to date, but not actually execute them.
|
|---|
| 6918 |
|
|---|
| 6919 | @item -t
|
|---|
| 6920 | @itemx --touch
|
|---|
| 6921 | @cindex @code{--touch}
|
|---|
| 6922 | @cindex touching files
|
|---|
| 6923 | @cindex target, touching
|
|---|
| 6924 | @cindex @code{-t}
|
|---|
| 6925 |
|
|---|
| 6926 | ``Touch''. The activity is to mark the targets as up to date without
|
|---|
| 6927 | actually changing them. In other words, @code{make} pretends to compile
|
|---|
| 6928 | the targets but does not really change their contents.
|
|---|
| 6929 |
|
|---|
| 6930 | @item -q
|
|---|
| 6931 | @itemx --question
|
|---|
| 6932 | @cindex @code{--question}
|
|---|
| 6933 | @cindex @code{-q}
|
|---|
| 6934 | @cindex question mode
|
|---|
| 6935 |
|
|---|
| 6936 | ``Question''. The activity is to find out silently whether the targets
|
|---|
| 6937 | are up to date already; but execute no commands in either case. In other
|
|---|
| 6938 | words, neither compilation nor output will occur.
|
|---|
| 6939 |
|
|---|
| 6940 | @item -W @var{file}
|
|---|
| 6941 | @itemx --what-if=@var{file}
|
|---|
| 6942 | @itemx --assume-new=@var{file}
|
|---|
| 6943 | @itemx --new-file=@var{file}
|
|---|
| 6944 | @cindex @code{--what-if}
|
|---|
| 6945 | @cindex @code{-W}
|
|---|
| 6946 | @cindex @code{--assume-new}
|
|---|
| 6947 | @cindex @code{--new-file}
|
|---|
| 6948 | @cindex what if
|
|---|
| 6949 | @cindex files, assuming new
|
|---|
| 6950 |
|
|---|
| 6951 | ``What if''. Each @samp{-W} flag is followed by a file name. The given
|
|---|
| 6952 | files' modification times are recorded by @code{make} as being the present
|
|---|
| 6953 | time, although the actual modification times remain the same.
|
|---|
| 6954 | You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
|
|---|
| 6955 | to see what would happen if you were to modify specific files.@refill
|
|---|
| 6956 | @end table
|
|---|
| 6957 |
|
|---|
| 6958 | With the @samp{-n} flag, @code{make} prints the commands that it would
|
|---|
| 6959 | normally execute but does not execute them.
|
|---|
| 6960 |
|
|---|
| 6961 | With the @samp{-t} flag, @code{make} ignores the commands in the rules
|
|---|
| 6962 | and uses (in effect) the command @code{touch} for each target that needs to
|
|---|
| 6963 | be remade. The @code{touch} command is also printed, unless @samp{-s} or
|
|---|
| 6964 | @code{.SILENT} is used. For speed, @code{make} does not actually invoke
|
|---|
| 6965 | the program @code{touch}. It does the work directly.
|
|---|
| 6966 |
|
|---|
| 6967 | With the @samp{-q} flag, @code{make} prints nothing and executes no
|
|---|
| 6968 | commands, but the exit status code it returns is zero if and only if the
|
|---|
| 6969 | targets to be considered are already up to date. If the exit status is
|
|---|
| 6970 | one, then some updating needs to be done. If @code{make} encounters an
|
|---|
| 6971 | error, the exit status is two, so you can distinguish an error from a
|
|---|
| 6972 | target that is not up to date.
|
|---|
| 6973 |
|
|---|
| 6974 | It is an error to use more than one of these three flags in the same
|
|---|
| 6975 | invocation of @code{make}.
|
|---|
| 6976 |
|
|---|
| 6977 | @cindex +, and command execution
|
|---|
| 6978 | The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
|
|---|
| 6979 | lines that begin with @samp{+} characters or contain the strings
|
|---|
| 6980 | @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
|
|---|
| 6981 | the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
|
|---|
| 6982 | is run regardless of these options. Other lines in the same rule are
|
|---|
| 6983 | not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
|
|---|
| 6984 | @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
|
|---|
| 6985 |
|
|---|
| 6986 | The @samp{-W} flag provides two features:
|
|---|
| 6987 |
|
|---|
| 6988 | @itemize @bullet
|
|---|
| 6989 | @item
|
|---|
| 6990 | If you also use the @samp{-n} or @samp{-q} flag, you can see what
|
|---|
| 6991 | @code{make} would do if you were to modify some files.
|
|---|
| 6992 |
|
|---|
| 6993 | @item
|
|---|
| 6994 | Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
|
|---|
| 6995 | executing commands, the @samp{-W} flag can direct @code{make} to act
|
|---|
| 6996 | as if some files had been modified, without actually modifying the
|
|---|
| 6997 | files.@refill
|
|---|
| 6998 | @end itemize
|
|---|
| 6999 |
|
|---|
| 7000 | Note that the options @samp{-p} and @samp{-v} allow you to obtain other
|
|---|
| 7001 | information about @code{make} or about the makefiles in use
|
|---|
| 7002 | (@pxref{Options Summary, ,Summary of Options}).@refill
|
|---|
| 7003 |
|
|---|
| 7004 | @node Avoiding Compilation, Overriding, Instead of Execution, Running
|
|---|
| 7005 | @section Avoiding Recompilation of Some Files
|
|---|
| 7006 | @cindex @code{-o}
|
|---|
| 7007 | @cindex @code{--old-file}
|
|---|
| 7008 | @cindex @code{--assume-old}
|
|---|
| 7009 | @cindex files, assuming old
|
|---|
| 7010 | @cindex files, avoiding recompilation of
|
|---|
| 7011 | @cindex recompilation, avoiding
|
|---|
| 7012 |
|
|---|
| 7013 | Sometimes you may have changed a source file but you do not want to
|
|---|
| 7014 | recompile all the files that depend on it. For example, suppose you add
|
|---|
| 7015 | a macro or a declaration to a header file that many other files depend
|
|---|
| 7016 | on. Being conservative, @code{make} assumes that any change in the
|
|---|
| 7017 | header file requires recompilation of all dependent files, but you know
|
|---|
| 7018 | that they do not need to be recompiled and you would rather not waste
|
|---|
| 7019 | the time waiting for them to compile.
|
|---|
| 7020 |
|
|---|
| 7021 | If you anticipate the problem before changing the header file, you can
|
|---|
| 7022 | use the @samp{-t} flag. This flag tells @code{make} not to run the
|
|---|
| 7023 | commands in the rules, but rather to mark the target up to date by
|
|---|
| 7024 | changing its last-modification date. You would follow this procedure:
|
|---|
| 7025 |
|
|---|
| 7026 | @enumerate
|
|---|
| 7027 | @item
|
|---|
| 7028 | Use the command @samp{make} to recompile the source files that really
|
|---|
| 7029 | need recompilation, ensuring that the object files are up-to-date
|
|---|
| 7030 | before you begin.
|
|---|
| 7031 |
|
|---|
| 7032 | @item
|
|---|
| 7033 | Make the changes in the header files.
|
|---|
| 7034 |
|
|---|
| 7035 | @item
|
|---|
| 7036 | Use the command @samp{make -t} to mark all the object files as
|
|---|
| 7037 | up to date. The next time you run @code{make}, the changes in the
|
|---|
| 7038 | header files will not cause any recompilation.
|
|---|
| 7039 | @end enumerate
|
|---|
| 7040 |
|
|---|
| 7041 | If you have already changed the header file at a time when some files
|
|---|
| 7042 | do need recompilation, it is too late to do this. Instead, you can
|
|---|
| 7043 | use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
|
|---|
| 7044 | ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
|
|---|
| 7045 | that the file itself will not be remade, and nothing else will be
|
|---|
| 7046 | remade on its account. Follow this procedure:
|
|---|
| 7047 |
|
|---|
| 7048 | @enumerate
|
|---|
| 7049 | @item
|
|---|
| 7050 | Recompile the source files that need compilation for reasons independent
|
|---|
| 7051 | of the particular header file, with @samp{make -o @var{headerfile}}.
|
|---|
| 7052 | If several header files are involved, use a separate @samp{-o} option
|
|---|
| 7053 | for each header file.
|
|---|
| 7054 |
|
|---|
| 7055 | @item
|
|---|
| 7056 | Touch all the object files with @samp{make -t}.
|
|---|
| 7057 | @end enumerate
|
|---|
| 7058 |
|
|---|
| 7059 | @node Overriding, Testing, Avoiding Compilation, Running
|
|---|
| 7060 | @section Overriding Variables
|
|---|
| 7061 | @cindex overriding variables with arguments
|
|---|
| 7062 | @cindex variables, overriding with arguments
|
|---|
| 7063 | @cindex command line variables
|
|---|
| 7064 | @cindex variables, command line
|
|---|
| 7065 |
|
|---|
| 7066 | An argument that contains @samp{=} specifies the value of a variable:
|
|---|
| 7067 | @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
|
|---|
| 7068 | If you specify a value in this way, all ordinary assignments of the same
|
|---|
| 7069 | variable in the makefile are ignored; we say they have been
|
|---|
| 7070 | @dfn{overridden} by the command line argument.
|
|---|
| 7071 |
|
|---|
| 7072 | The most common way to use this facility is to pass extra flags to
|
|---|
| 7073 | compilers. For example, in a properly written makefile, the variable
|
|---|
| 7074 | @code{CFLAGS} is included in each command that runs the C compiler, so a
|
|---|
| 7075 | file @file{foo.c} would be compiled something like this:
|
|---|
| 7076 |
|
|---|
| 7077 | @example
|
|---|
| 7078 | cc -c $(CFLAGS) foo.c
|
|---|
| 7079 | @end example
|
|---|
| 7080 |
|
|---|
| 7081 | Thus, whatever value you set for @code{CFLAGS} affects each compilation
|
|---|
| 7082 | that occurs. The makefile probably specifies the usual value for
|
|---|
| 7083 | @code{CFLAGS}, like this:
|
|---|
| 7084 |
|
|---|
| 7085 | @example
|
|---|
| 7086 | CFLAGS=-g
|
|---|
| 7087 | @end example
|
|---|
| 7088 |
|
|---|
| 7089 | Each time you run @code{make}, you can override this value if you
|
|---|
| 7090 | wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
|
|---|
| 7091 | compilation will be done with @samp{cc -c -g -O}. (This also
|
|---|
| 7092 | illustrates how you can use quoting in the shell to enclose spaces and
|
|---|
| 7093 | other special characters in the value of a variable when you override
|
|---|
| 7094 | it.)
|
|---|
| 7095 |
|
|---|
| 7096 | The variable @code{CFLAGS} is only one of many standard variables that
|
|---|
| 7097 | exist just so that you can change them this way. @xref{Implicit
|
|---|
| 7098 | Variables, , Variables Used by Implicit Rules}, for a complete list.
|
|---|
| 7099 |
|
|---|
| 7100 | You can also program the makefile to look at additional variables of your
|
|---|
| 7101 | own, giving the user the ability to control other aspects of how the
|
|---|
| 7102 | makefile works by changing the variables.
|
|---|
| 7103 |
|
|---|
| 7104 | When you override a variable with a command argument, you can define either
|
|---|
| 7105 | a recursively-expanded variable or a simply-expanded variable. The
|
|---|
| 7106 | examples shown above make a recursively-expanded variable; to make a
|
|---|
| 7107 | simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
|
|---|
| 7108 | you want to include a variable reference or function call in the
|
|---|
| 7109 | @emph{value} that you specify, it makes no difference which kind of
|
|---|
| 7110 | variable you create.
|
|---|
| 7111 |
|
|---|
| 7112 | There is one way that the makefile can change a variable that you have
|
|---|
| 7113 | overridden. This is to use the @code{override} directive, which is a line
|
|---|
| 7114 | that looks like this: @samp{override @var{variable} = @var{value}}
|
|---|
| 7115 | (@pxref{Override Directive, ,The @code{override} Directive}).
|
|---|
| 7116 |
|
|---|
| 7117 | @node Testing, Options Summary, Overriding, Running
|
|---|
| 7118 | @section Testing the Compilation of a Program
|
|---|
| 7119 | @cindex testing compilation
|
|---|
| 7120 | @cindex compilation, testing
|
|---|
| 7121 |
|
|---|
| 7122 | Normally, when an error happens in executing a shell command, @code{make}
|
|---|
| 7123 | gives up immediately, returning a nonzero status. No further commands are
|
|---|
| 7124 | executed for any target. The error implies that the goal cannot be
|
|---|
| 7125 | correctly remade, and @code{make} reports this as soon as it knows.
|
|---|
| 7126 |
|
|---|
| 7127 | When you are compiling a program that you have just changed, this is not
|
|---|
| 7128 | what you want. Instead, you would rather that @code{make} try compiling
|
|---|
| 7129 | every file that can be tried, to show you as many compilation errors
|
|---|
| 7130 | as possible.
|
|---|
| 7131 |
|
|---|
| 7132 | @cindex @code{-k}
|
|---|
| 7133 | @cindex @code{--keep-going}
|
|---|
| 7134 | On these occasions, you should use the @samp{-k} or
|
|---|
| 7135 | @samp{--keep-going} flag. This tells @code{make} to continue to
|
|---|
| 7136 | consider the other prerequisites of the pending targets, remaking them
|
|---|
| 7137 | if necessary, before it gives up and returns nonzero status. For
|
|---|
| 7138 | example, after an error in compiling one object file, @samp{make -k}
|
|---|
| 7139 | will continue compiling other object files even though it already
|
|---|
| 7140 | knows that linking them will be impossible. In addition to continuing
|
|---|
| 7141 | after failed shell commands, @samp{make -k} will continue as much as
|
|---|
| 7142 | possible after discovering that it does not know how to make a target
|
|---|
| 7143 | or prerequisite file. This will always cause an error message, but
|
|---|
| 7144 | without @samp{-k}, it is a fatal error (@pxref{Options Summary,
|
|---|
| 7145 | ,Summary of Options}).@refill
|
|---|
| 7146 |
|
|---|
| 7147 | The usual behavior of @code{make} assumes that your purpose is to get the
|
|---|
| 7148 | goals up to date; once @code{make} learns that this is impossible, it might
|
|---|
| 7149 | as well report the failure immediately. The @samp{-k} flag says that the
|
|---|
| 7150 | real purpose is to test as much as possible of the changes made in the
|
|---|
| 7151 | program, perhaps to find several independent problems so that you can
|
|---|
| 7152 | correct them all before the next attempt to compile. This is why Emacs'
|
|---|
| 7153 | @kbd{M-x compile} command passes the @samp{-k} flag by default.
|
|---|
| 7154 |
|
|---|
| 7155 | @node Options Summary, , Testing, Running
|
|---|
| 7156 | @section Summary of Options
|
|---|
| 7157 | @cindex options
|
|---|
| 7158 | @cindex flags
|
|---|
| 7159 | @cindex switches
|
|---|
| 7160 |
|
|---|
| 7161 | Here is a table of all the options @code{make} understands:
|
|---|
| 7162 |
|
|---|
| 7163 | @table @samp
|
|---|
| 7164 | @item -b
|
|---|
| 7165 | @cindex @code{-b}
|
|---|
| 7166 | @itemx -m
|
|---|
| 7167 | @cindex @code{-m}
|
|---|
| 7168 | These options are ignored for compatibility with other versions of @code{make}.
|
|---|
| 7169 |
|
|---|
| 7170 | @item -B
|
|---|
| 7171 | @cindex @code{-B}
|
|---|
| 7172 | @itemx --always-make
|
|---|
| 7173 | @cindex @code{--always-make}
|
|---|
| 7174 | Consider all targets out-of-date. GNU @code{make} proceeds to
|
|---|
| 7175 | consider targets and their prerequisites using the normal algorithms;
|
|---|
| 7176 | however, all these targets are remade, regardless of the status of
|
|---|
| 7177 | their prerequisites.
|
|---|
| 7178 |
|
|---|
| 7179 | @item -C @var{dir}
|
|---|
| 7180 | @cindex @code{-C}
|
|---|
| 7181 | @itemx --directory=@var{dir}
|
|---|
| 7182 | @cindex @code{--directory}
|
|---|
| 7183 | Change to directory @var{dir} before reading the makefiles. If multiple
|
|---|
| 7184 | @samp{-C} options are specified, each is interpreted relative to the
|
|---|
| 7185 | previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
|
|---|
| 7186 | This is typically used with recursive invocations of @code{make}
|
|---|
| 7187 | (@pxref{Recursion, ,Recursive Use of @code{make}}).
|
|---|
| 7188 |
|
|---|
| 7189 | @item -d
|
|---|
| 7190 | @cindex @code{-d}
|
|---|
| 7191 | @c Extra blank line here makes the table look better.
|
|---|
| 7192 |
|
|---|
| 7193 | Print debugging information in addition to normal processing. The
|
|---|
| 7194 | debugging information says which files are being considered for
|
|---|
| 7195 | remaking, which file-times are being compared and with what results,
|
|---|
| 7196 | which files actually need to be remade, which implicit rules are
|
|---|
| 7197 | considered and which are applied---everything interesting about how
|
|---|
| 7198 | @code{make} decides what to do. The @code{-d} option is equivalent to
|
|---|
| 7199 | @samp{--debug=a} (see below).
|
|---|
| 7200 |
|
|---|
| 7201 | @item --debug[=@var{options}]
|
|---|
| 7202 | @cindex @code{--debug}
|
|---|
| 7203 | @c Extra blank line here makes the table look better.
|
|---|
| 7204 |
|
|---|
| 7205 | Print debugging information in addition to normal processing. Various
|
|---|
| 7206 | levels and types of output can be chosen. With no arguments, print the
|
|---|
| 7207 | ``basic'' level of debugging. Possible arguments are below; only the
|
|---|
| 7208 | first character is considered, and values must be comma- or
|
|---|
| 7209 | space-separated.
|
|---|
| 7210 |
|
|---|
| 7211 | @table @code
|
|---|
| 7212 | @item a (@i{all})
|
|---|
| 7213 | All types of debugging output are enabled. This is equivalent to using
|
|---|
| 7214 | @samp{-d}.
|
|---|
| 7215 |
|
|---|
| 7216 | @item b (@i{basic})
|
|---|
| 7217 | Basic debugging prints each target that was found to be out-of-date, and
|
|---|
| 7218 | whether the build was successful or not.
|
|---|
| 7219 |
|
|---|
| 7220 | @item v (@i{verbose})
|
|---|
| 7221 | A level above @samp{basic}; includes messages about which makefiles were
|
|---|
| 7222 | parsed, prerequisites that did not need to be rebuilt, etc. This option
|
|---|
| 7223 | also enables @samp{basic} messages.
|
|---|
| 7224 |
|
|---|
| 7225 | @item i (@i{implicit})
|
|---|
| 7226 | Prints messages describing the implicit rule searches for each target.
|
|---|
| 7227 | This option also enables @samp{basic} messages.
|
|---|
| 7228 |
|
|---|
| 7229 | @item j (@i{jobs})
|
|---|
| 7230 | Prints messages giving details on the invocation of specific subcommands.
|
|---|
| 7231 |
|
|---|
| 7232 | @item m (@i{makefile})
|
|---|
| 7233 | By default, the above messages are not enabled while trying to remake
|
|---|
| 7234 | the makefiles. This option enables messages while rebuilding makefiles,
|
|---|
| 7235 | too. Note that the @samp{all} option does enable this option. This
|
|---|
| 7236 | option also enables @samp{basic} messages.
|
|---|
| 7237 | @end table
|
|---|
| 7238 |
|
|---|
| 7239 | @item -e
|
|---|
| 7240 | @cindex @code{-e}
|
|---|
| 7241 | @itemx --environment-overrides
|
|---|
| 7242 | @cindex @code{--environment-overrides}
|
|---|
| 7243 | Give variables taken from the environment precedence
|
|---|
| 7244 | over variables from makefiles.
|
|---|
| 7245 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 7246 |
|
|---|
| 7247 | @item -f @var{file}
|
|---|
| 7248 | @cindex @code{-f}
|
|---|
| 7249 | @itemx --file=@var{file}
|
|---|
| 7250 | @cindex @code{--file}
|
|---|
| 7251 | @itemx --makefile=@var{file}
|
|---|
| 7252 | @cindex @code{--makefile}
|
|---|
| 7253 | Read the file named @var{file} as a makefile.
|
|---|
| 7254 | @xref{Makefiles, ,Writing Makefiles}.
|
|---|
| 7255 |
|
|---|
| 7256 | @item -h
|
|---|
| 7257 | @cindex @code{-h}
|
|---|
| 7258 | @itemx --help
|
|---|
| 7259 | @cindex @code{--help}
|
|---|
| 7260 | @c Extra blank line here makes the table look better.
|
|---|
| 7261 |
|
|---|
| 7262 | Remind you of the options that @code{make} understands and then exit.
|
|---|
| 7263 |
|
|---|
| 7264 | @item -i
|
|---|
| 7265 | @cindex @code{-i}
|
|---|
| 7266 | @itemx --ignore-errors
|
|---|
| 7267 | @cindex @code{--ignore-errors}
|
|---|
| 7268 | Ignore all errors in commands executed to remake files.
|
|---|
| 7269 | @xref{Errors, ,Errors in Commands}.
|
|---|
| 7270 |
|
|---|
| 7271 | @item -I @var{dir}
|
|---|
| 7272 | @cindex @code{-I}
|
|---|
| 7273 | @itemx --include-dir=@var{dir}
|
|---|
| 7274 | @cindex @code{--include-dir}
|
|---|
| 7275 | Specifies a directory @var{dir} to search for included makefiles.
|
|---|
| 7276 | @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
|
|---|
| 7277 | options are used to specify several directories, the directories are
|
|---|
| 7278 | searched in the order specified.
|
|---|
| 7279 |
|
|---|
| 7280 | @item -j [@var{jobs}]
|
|---|
| 7281 | @cindex @code{-j}
|
|---|
| 7282 | @itemx --jobs[=@var{jobs}]
|
|---|
| 7283 | @cindex @code{--jobs}
|
|---|
| 7284 | Specifies the number of jobs (commands) to run simultaneously. With no
|
|---|
| 7285 | argument, @code{make} runs as many jobs simultaneously as possible. If
|
|---|
| 7286 | there is more than one @samp{-j} option, the last one is effective.
|
|---|
| 7287 | @xref{Parallel, ,Parallel Execution},
|
|---|
| 7288 | for more information on how commands are run.
|
|---|
| 7289 | Note that this option is ignored on MS-DOS.
|
|---|
| 7290 |
|
|---|
| 7291 | @item -k
|
|---|
| 7292 | @cindex @code{-k}
|
|---|
| 7293 | @itemx --keep-going
|
|---|
| 7294 | @cindex @code{--keep-going}
|
|---|
| 7295 | Continue as much as possible after an error. While the target that
|
|---|
| 7296 | failed, and those that depend on it, cannot be remade, the other
|
|---|
| 7297 | prerequisites of these targets can be processed all the same.
|
|---|
| 7298 | @xref{Testing, ,Testing the Compilation of a Program}.
|
|---|
| 7299 |
|
|---|
| 7300 | @item -l [@var{load}]
|
|---|
| 7301 | @cindex @code{-l}
|
|---|
| 7302 | @itemx --load-average[=@var{load}]
|
|---|
| 7303 | @cindex @code{--load-average}
|
|---|
| 7304 | @itemx --max-load[=@var{load}]
|
|---|
| 7305 | @cindex @code{--max-load}
|
|---|
| 7306 | Specifies that no new jobs (commands) should be started if there are
|
|---|
| 7307 | other jobs running and the load average is at least @var{load} (a
|
|---|
| 7308 | floating-point number). With no argument, removes a previous load
|
|---|
| 7309 | limit. @xref{Parallel, ,Parallel Execution}.
|
|---|
| 7310 |
|
|---|
| 7311 | @item -n
|
|---|
| 7312 | @cindex @code{-n}
|
|---|
| 7313 | @itemx --just-print
|
|---|
| 7314 | @cindex @code{--just-print}
|
|---|
| 7315 | @itemx --dry-run
|
|---|
| 7316 | @cindex @code{--dry-run}
|
|---|
| 7317 | @itemx --recon
|
|---|
| 7318 | @cindex @code{--recon}
|
|---|
| 7319 | @c Extra blank line here makes the table look better.
|
|---|
| 7320 |
|
|---|
| 7321 | Print the commands that would be executed, but do not execute them.
|
|---|
| 7322 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7323 |
|
|---|
| 7324 | @item -o @var{file}
|
|---|
| 7325 | @cindex @code{-o}
|
|---|
| 7326 | @itemx --old-file=@var{file}
|
|---|
| 7327 | @cindex @code{--old-file}
|
|---|
| 7328 | @itemx --assume-old=@var{file}
|
|---|
| 7329 | @cindex @code{--assume-old}
|
|---|
| 7330 | Do not remake the file @var{file} even if it is older than its
|
|---|
| 7331 | prerequisites, and do not remake anything on account of changes in
|
|---|
| 7332 | @var{file}. Essentially the file is treated as very old and its rules
|
|---|
| 7333 | are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
|
|---|
| 7334 | Some Files}.@refill
|
|---|
| 7335 |
|
|---|
| 7336 | @item -p
|
|---|
| 7337 | @cindex @code{-p}
|
|---|
| 7338 | @itemx --print-data-base
|
|---|
| 7339 | @cindex @code{--print-data-base}
|
|---|
| 7340 | @cindex data base of @code{make} rules
|
|---|
| 7341 | @cindex predefined rules and variables, printing
|
|---|
| 7342 | Print the data base (rules and variable values) that results from
|
|---|
| 7343 | reading the makefiles; then execute as usual or as otherwise specified.
|
|---|
| 7344 | This also prints the version information given by the @samp{-v} switch
|
|---|
| 7345 | (see below). To print the data base without trying to remake any files,
|
|---|
| 7346 | use @w{@samp{make -qp}}. To print the data base of predefined rules and
|
|---|
| 7347 | variables, use @w{@samp{make -p -f /dev/null}}. The data base output
|
|---|
| 7348 | contains filename and linenumber information for command and variable
|
|---|
| 7349 | definitions, so it can be a useful debugging tool in complex environments.
|
|---|
| 7350 |
|
|---|
| 7351 | @item -q
|
|---|
| 7352 | @cindex @code{-q}
|
|---|
| 7353 | @itemx --question
|
|---|
| 7354 | @cindex @code{--question}
|
|---|
| 7355 | ``Question mode''. Do not run any commands, or print anything; just
|
|---|
| 7356 | return an exit status that is zero if the specified targets are already
|
|---|
| 7357 | up to date, one if any remaking is required, or two if an error is
|
|---|
| 7358 | encountered. @xref{Instead of Execution, ,Instead of Executing the
|
|---|
| 7359 | Commands}.@refill
|
|---|
| 7360 |
|
|---|
| 7361 | @item -r
|
|---|
| 7362 | @cindex @code{-r}
|
|---|
| 7363 | @itemx --no-builtin-rules
|
|---|
| 7364 | @cindex @code{--no-builtin-rules}
|
|---|
| 7365 | Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
|
|---|
| 7366 | ,Using Implicit Rules}). You can still define your own by writing
|
|---|
| 7367 | pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
|
|---|
| 7368 | Rules}). The @samp{-r} option also clears out the default list of
|
|---|
| 7369 | suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
|
|---|
| 7370 | Rules}). But you can still define your own suffixes with a rule for
|
|---|
| 7371 | @code{.SUFFIXES}, and then define your own suffix rules. Note that only
|
|---|
| 7372 | @emph{rules} are affected by the @code{-r} option; default variables
|
|---|
| 7373 | remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
|
|---|
| 7374 | Rules}); see the @samp{-R} option below.
|
|---|
| 7375 |
|
|---|
| 7376 | @item -R
|
|---|
| 7377 | @cindex @code{-R}
|
|---|
| 7378 | @itemx --no-builtin-variables
|
|---|
| 7379 | @cindex @code{--no-builtin-variables}
|
|---|
| 7380 | Eliminate use of the built-in rule-specific variables (@pxref{Implicit
|
|---|
| 7381 | Variables, ,Variables Used by Implicit Rules}). You can still define
|
|---|
| 7382 | your own, of course. The @samp{-R} option also automatically enables
|
|---|
| 7383 | the @samp{-r} option (see above), since it doesn't make sense to have
|
|---|
| 7384 | implicit rules without any definitions for the variables that they use.
|
|---|
| 7385 |
|
|---|
| 7386 | @item -s
|
|---|
| 7387 | @cindex @code{-s}
|
|---|
| 7388 | @itemx --silent
|
|---|
| 7389 | @cindex @code{--silent}
|
|---|
| 7390 | @itemx --quiet
|
|---|
| 7391 | @cindex @code{--quiet}
|
|---|
| 7392 | @c Extra blank line here makes the table look better.
|
|---|
| 7393 |
|
|---|
| 7394 | Silent operation; do not print the commands as they are executed.
|
|---|
| 7395 | @xref{Echoing, ,Command Echoing}.
|
|---|
| 7396 |
|
|---|
| 7397 | @item -S
|
|---|
| 7398 | @cindex @code{-S}
|
|---|
| 7399 | @itemx --no-keep-going
|
|---|
| 7400 | @cindex @code{--no-keep-going}
|
|---|
| 7401 | @itemx --stop
|
|---|
| 7402 | @cindex @code{--stop}
|
|---|
| 7403 | @c Extra blank line here makes the table look better.
|
|---|
| 7404 |
|
|---|
| 7405 | Cancel the effect of the @samp{-k} option. This is never necessary
|
|---|
| 7406 | except in a recursive @code{make} where @samp{-k} might be inherited
|
|---|
| 7407 | from the top-level @code{make} via @code{MAKEFLAGS}
|
|---|
| 7408 | (@pxref{Recursion, ,Recursive Use of @code{make}})
|
|---|
| 7409 | or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
|
|---|
| 7410 |
|
|---|
| 7411 | @item -t
|
|---|
| 7412 | @cindex @code{-t}
|
|---|
| 7413 | @itemx --touch
|
|---|
| 7414 | @cindex @code{--touch}
|
|---|
| 7415 | @c Extra blank line here makes the table look better.
|
|---|
| 7416 |
|
|---|
| 7417 | Touch files (mark them up to date without really changing them)
|
|---|
| 7418 | instead of running their commands. This is used to pretend that the
|
|---|
| 7419 | commands were done, in order to fool future invocations of
|
|---|
| 7420 | @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7421 |
|
|---|
| 7422 | @item -v
|
|---|
| 7423 | @cindex @code{-v}
|
|---|
| 7424 | @itemx --version
|
|---|
| 7425 | @cindex @code{--version}
|
|---|
| 7426 | Print the version of the @code{make} program plus a copyright, a list
|
|---|
| 7427 | of authors, and a notice that there is no warranty; then exit.
|
|---|
| 7428 |
|
|---|
| 7429 | @item -w
|
|---|
| 7430 | @cindex @code{-w}
|
|---|
| 7431 | @itemx --print-directory
|
|---|
| 7432 | @cindex @code{--print-directory}
|
|---|
| 7433 | Print a message containing the working directory both before and after
|
|---|
| 7434 | executing the makefile. This may be useful for tracking down errors
|
|---|
| 7435 | from complicated nests of recursive @code{make} commands.
|
|---|
| 7436 | @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
|
|---|
| 7437 | rarely need to specify this option since @samp{make} does it for you;
|
|---|
| 7438 | see @ref{-w Option, ,The @samp{--print-directory} Option}.)
|
|---|
| 7439 |
|
|---|
| 7440 | @itemx --no-print-directory
|
|---|
| 7441 | @cindex @code{--no-print-directory}
|
|---|
| 7442 | Disable printing of the working directory under @code{-w}.
|
|---|
| 7443 | This option is useful when @code{-w} is turned on automatically,
|
|---|
| 7444 | but you do not want to see the extra messages.
|
|---|
| 7445 | @xref{-w Option, ,The @samp{--print-directory} Option}.
|
|---|
| 7446 |
|
|---|
| 7447 | @item -W @var{file}
|
|---|
| 7448 | @cindex @code{-W}
|
|---|
| 7449 | @itemx --what-if=@var{file}
|
|---|
| 7450 | @cindex @code{--what-if}
|
|---|
| 7451 | @itemx --new-file=@var{file}
|
|---|
| 7452 | @cindex @code{--new-file}
|
|---|
| 7453 | @itemx --assume-new=@var{file}
|
|---|
| 7454 | @cindex @code{--assume-new}
|
|---|
| 7455 | Pretend that the target @var{file} has just been modified. When used
|
|---|
| 7456 | with the @samp{-n} flag, this shows you what would happen if you were
|
|---|
| 7457 | to modify that file. Without @samp{-n}, it is almost the same as
|
|---|
| 7458 | running a @code{touch} command on the given file before running
|
|---|
| 7459 | @code{make}, except that the modification time is changed only in the
|
|---|
| 7460 | imagination of @code{make}.
|
|---|
| 7461 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 7462 |
|
|---|
| 7463 | @item --warn-undefined-variables
|
|---|
| 7464 | @cindex @code{--warn-undefined-variables}
|
|---|
| 7465 | @cindex variables, warning for undefined
|
|---|
| 7466 | @cindex undefined variables, warning message
|
|---|
| 7467 | Issue a warning message whenever @code{make} sees a reference to an
|
|---|
| 7468 | undefined variable. This can be helpful when you are trying to debug
|
|---|
| 7469 | makefiles which use variables in complex ways.
|
|---|
| 7470 | @end table
|
|---|
| 7471 |
|
|---|
| 7472 | @node Implicit Rules, Archives, Running, Top
|
|---|
| 7473 | @chapter Using Implicit Rules
|
|---|
| 7474 | @cindex implicit rule
|
|---|
| 7475 | @cindex rule, implicit
|
|---|
| 7476 |
|
|---|
| 7477 | Certain standard ways of remaking target files are used very often. For
|
|---|
| 7478 | example, one customary way to make an object file is from a C source file
|
|---|
| 7479 | using the C compiler, @code{cc}.
|
|---|
| 7480 |
|
|---|
| 7481 | @dfn{Implicit rules} tell @code{make} how to use customary techniques so
|
|---|
| 7482 | that you do not have to specify them in detail when you want to use
|
|---|
| 7483 | them. For example, there is an implicit rule for C compilation. File
|
|---|
| 7484 | names determine which implicit rules are run. For example, C
|
|---|
| 7485 | compilation typically takes a @file{.c} file and makes a @file{.o} file.
|
|---|
| 7486 | So @code{make} applies the implicit rule for C compilation when it sees
|
|---|
| 7487 | this combination of file name endings.@refill
|
|---|
| 7488 |
|
|---|
| 7489 | A chain of implicit rules can apply in sequence; for example, @code{make}
|
|---|
| 7490 | will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
|
|---|
| 7491 | @iftex
|
|---|
| 7492 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 7493 | @end iftex
|
|---|
| 7494 |
|
|---|
| 7495 | The built-in implicit rules use several variables in their commands so
|
|---|
| 7496 | that, by changing the values of the variables, you can change the way the
|
|---|
| 7497 | implicit rule works. For example, the variable @code{CFLAGS} controls the
|
|---|
| 7498 | flags given to the C compiler by the implicit rule for C compilation.
|
|---|
| 7499 | @iftex
|
|---|
| 7500 | @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
|
|---|
| 7501 | @end iftex
|
|---|
| 7502 |
|
|---|
| 7503 | You can define your own implicit rules by writing @dfn{pattern rules}.
|
|---|
| 7504 | @iftex
|
|---|
| 7505 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
|
|---|
| 7506 | @end iftex
|
|---|
| 7507 |
|
|---|
| 7508 | @dfn{Suffix rules} are a more limited way to define implicit rules.
|
|---|
| 7509 | Pattern rules are more general and clearer, but suffix rules are
|
|---|
| 7510 | retained for compatibility.
|
|---|
| 7511 | @iftex
|
|---|
| 7512 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 7513 | @end iftex
|
|---|
| 7514 |
|
|---|
| 7515 | @menu
|
|---|
| 7516 | * Using Implicit:: How to use an existing implicit rule
|
|---|
| 7517 | to get the commands for updating a file.
|
|---|
| 7518 | * Catalogue of Rules:: A list of built-in implicit rules.
|
|---|
| 7519 | * Implicit Variables:: How to change what predefined rules do.
|
|---|
| 7520 | * Chained Rules:: How to use a chain of implicit rules.
|
|---|
| 7521 | * Pattern Rules:: How to define new implicit rules.
|
|---|
| 7522 | * Last Resort:: How to defining commands for rules
|
|---|
| 7523 | which cannot find any.
|
|---|
| 7524 | * Suffix Rules:: The old-fashioned style of implicit rule.
|
|---|
| 7525 | * Implicit Rule Search:: The precise algorithm for applying
|
|---|
| 7526 | implicit rules.
|
|---|
| 7527 | @end menu
|
|---|
| 7528 |
|
|---|
| 7529 | @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
|
|---|
| 7530 | @section Using Implicit Rules
|
|---|
| 7531 | @cindex implicit rule, how to use
|
|---|
| 7532 | @cindex rule, implicit, how to use
|
|---|
| 7533 |
|
|---|
| 7534 | To allow @code{make} to find a customary method for updating a target file,
|
|---|
| 7535 | all you have to do is refrain from specifying commands yourself. Either
|
|---|
| 7536 | write a rule with no command lines, or don't write a rule at all. Then
|
|---|
| 7537 | @code{make} will figure out which implicit rule to use based on which
|
|---|
| 7538 | kind of source file exists or can be made.
|
|---|
| 7539 |
|
|---|
| 7540 | For example, suppose the makefile looks like this:
|
|---|
| 7541 |
|
|---|
| 7542 | @example
|
|---|
| 7543 | foo : foo.o bar.o
|
|---|
| 7544 | cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
|
|---|
| 7545 | @end example
|
|---|
| 7546 |
|
|---|
| 7547 | @noindent
|
|---|
| 7548 | Because you mention @file{foo.o} but do not give a rule for it, @code{make}
|
|---|
| 7549 | will automatically look for an implicit rule that tells how to update it.
|
|---|
| 7550 | This happens whether or not the file @file{foo.o} currently exists.
|
|---|
| 7551 |
|
|---|
| 7552 | If an implicit rule is found, it can supply both commands and one or
|
|---|
| 7553 | more prerequisites (the source files). You would want to write a rule
|
|---|
| 7554 | for @file{foo.o} with no command lines if you need to specify additional
|
|---|
| 7555 | prerequisites, such as header files, that the implicit rule cannot
|
|---|
| 7556 | supply.
|
|---|
| 7557 |
|
|---|
| 7558 | Each implicit rule has a target pattern and prerequisite patterns. There may
|
|---|
| 7559 | be many implicit rules with the same target pattern. For example, numerous
|
|---|
| 7560 | rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
|
|---|
| 7561 | another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
|
|---|
| 7562 | that actually applies is the one whose prerequisites exist or can be made.
|
|---|
| 7563 | So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
|
|---|
| 7564 | otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
|
|---|
| 7565 | compiler; and so on.
|
|---|
| 7566 |
|
|---|
| 7567 | Of course, when you write the makefile, you know which implicit rule you
|
|---|
| 7568 | want @code{make} to use, and you know it will choose that one because you
|
|---|
| 7569 | know which possible prerequisite files are supposed to exist.
|
|---|
| 7570 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
|---|
| 7571 | for a catalogue of all the predefined implicit rules.
|
|---|
| 7572 |
|
|---|
| 7573 | Above, we said an implicit rule applies if the required prerequisites ``exist
|
|---|
| 7574 | or can be made''. A file ``can be made'' if it is mentioned explicitly in
|
|---|
| 7575 | the makefile as a target or a prerequisite, or if an implicit rule can be
|
|---|
| 7576 | recursively found for how to make it. When an implicit prerequisite is the
|
|---|
| 7577 | result of another implicit rule, we say that @dfn{chaining} is occurring.
|
|---|
| 7578 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 7579 |
|
|---|
| 7580 | In general, @code{make} searches for an implicit rule for each target, and
|
|---|
| 7581 | for each double-colon rule, that has no commands. A file that is mentioned
|
|---|
| 7582 | only as a prerequisite is considered a target whose rule specifies nothing,
|
|---|
| 7583 | so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
|
|---|
| 7584 | details of how the search is done.
|
|---|
| 7585 |
|
|---|
| 7586 | Note that explicit prerequisites do not influence implicit rule search.
|
|---|
| 7587 | For example, consider this explicit rule:
|
|---|
| 7588 |
|
|---|
| 7589 | @example
|
|---|
| 7590 | foo.o: foo.p
|
|---|
| 7591 | @end example
|
|---|
| 7592 |
|
|---|
| 7593 | @noindent
|
|---|
| 7594 | The prerequisite on @file{foo.p} does not necessarily mean that
|
|---|
| 7595 | @code{make} will remake @file{foo.o} according to the implicit rule to
|
|---|
| 7596 | make an object file, a @file{.o} file, from a Pascal source file, a
|
|---|
| 7597 | @file{.p} file. For example, if @file{foo.c} also exists, the implicit
|
|---|
| 7598 | rule to make an object file from a C source file is used instead,
|
|---|
| 7599 | because it appears before the Pascal rule in the list of predefined
|
|---|
| 7600 | implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
|
|---|
| 7601 | Rules}).
|
|---|
| 7602 |
|
|---|
| 7603 | If you do not want an implicit rule to be used for a target that has no
|
|---|
| 7604 | commands, you can give that target empty commands by writing a semicolon
|
|---|
| 7605 | (@pxref{Empty Commands, ,Defining Empty Commands}).
|
|---|
| 7606 |
|
|---|
| 7607 | @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
|
|---|
| 7608 | @section Catalogue of Implicit Rules
|
|---|
| 7609 | @cindex implicit rule, predefined
|
|---|
| 7610 | @cindex rule, implicit, predefined
|
|---|
| 7611 |
|
|---|
| 7612 | Here is a catalogue of predefined implicit rules which are always
|
|---|
| 7613 | available unless the makefile explicitly overrides or cancels them.
|
|---|
| 7614 | @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
|
|---|
| 7615 | canceling or overriding an implicit rule. The @samp{-r} or
|
|---|
| 7616 | @samp{--no-builtin-rules} option cancels all predefined rules.
|
|---|
| 7617 |
|
|---|
| 7618 | Not all of these rules will always be defined, even when the @samp{-r}
|
|---|
| 7619 | option is not given. Many of the predefined implicit rules are
|
|---|
| 7620 | implemented in @code{make} as suffix rules, so which ones will be
|
|---|
| 7621 | defined depends on the @dfn{suffix list} (the list of prerequisites of
|
|---|
| 7622 | the special target @code{.SUFFIXES}). The default suffix list is:
|
|---|
| 7623 | @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
|
|---|
| 7624 | @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
|
|---|
| 7625 | @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
|
|---|
| 7626 | @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
|
|---|
| 7627 | @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
|
|---|
| 7628 | @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
|
|---|
| 7629 | described below whose prerequisites have one of these suffixes are
|
|---|
| 7630 | actually suffix rules. If you modify the suffix list, the only
|
|---|
| 7631 | predefined suffix rules in effect will be those named by one or two of
|
|---|
| 7632 | the suffixes that are on the list you specify; rules whose suffixes fail
|
|---|
| 7633 | to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
|
|---|
| 7634 | Suffix Rules}, for full details on suffix rules.
|
|---|
| 7635 |
|
|---|
| 7636 | @table @asis
|
|---|
| 7637 | @item Compiling C programs
|
|---|
| 7638 | @cindex C, rule to compile
|
|---|
| 7639 | @pindex cc
|
|---|
| 7640 | @pindex gcc
|
|---|
| 7641 | @pindex .o
|
|---|
| 7642 | @pindex .c
|
|---|
| 7643 | @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
|
|---|
| 7644 | a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
|
|---|
| 7645 |
|
|---|
| 7646 | @item Compiling C++ programs
|
|---|
| 7647 | @cindex C++, rule to compile
|
|---|
| 7648 | @pindex g++
|
|---|
| 7649 | @pindex .C
|
|---|
| 7650 | @pindex .cc
|
|---|
| 7651 | @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
|
|---|
| 7652 | @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
|
|---|
| 7653 | $(CXXFLAGS)}. We encourage you to use the suffix @samp{.cc} for C++
|
|---|
| 7654 | source files instead of @samp{.C}.@refill
|
|---|
| 7655 |
|
|---|
| 7656 | @item Compiling Pascal programs
|
|---|
| 7657 | @cindex Pascal, rule to compile
|
|---|
| 7658 | @pindex pc
|
|---|
| 7659 | @pindex .p
|
|---|
| 7660 | @file{@var{n}.o} is made automatically from @file{@var{n}.p}
|
|---|
| 7661 | with the command @samp{$(PC) -c $(PFLAGS)}.@refill
|
|---|
| 7662 |
|
|---|
| 7663 | @item Compiling Fortran and Ratfor programs
|
|---|
| 7664 | @cindex Fortran, rule to compile
|
|---|
| 7665 | @cindex Ratfor, rule to compile
|
|---|
| 7666 | @pindex f77
|
|---|
| 7667 | @pindex .f
|
|---|
| 7668 | @pindex .r
|
|---|
| 7669 | @pindex .F
|
|---|
| 7670 | @file{@var{n}.o} is made automatically from @file{@var{n}.r},
|
|---|
| 7671 | @file{@var{n}.F} or @file{@var{n}.f} by running the
|
|---|
| 7672 | Fortran compiler. The precise command used is as follows:@refill
|
|---|
| 7673 |
|
|---|
| 7674 | @table @samp
|
|---|
| 7675 | @item .f
|
|---|
| 7676 | @samp{$(FC) -c $(FFLAGS)}.
|
|---|
| 7677 | @item .F
|
|---|
| 7678 | @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
|
|---|
| 7679 | @item .r
|
|---|
| 7680 | @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
|
|---|
| 7681 | @end table
|
|---|
| 7682 |
|
|---|
| 7683 | @item Preprocessing Fortran and Ratfor programs
|
|---|
| 7684 | @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
|
|---|
| 7685 | @file{@var{n}.F}. This rule runs just the preprocessor to convert a
|
|---|
| 7686 | Ratfor or preprocessable Fortran program into a strict Fortran
|
|---|
| 7687 | program. The precise command used is as follows:@refill
|
|---|
| 7688 |
|
|---|
| 7689 | @table @samp
|
|---|
| 7690 | @item .F
|
|---|
| 7691 | @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
|
|---|
| 7692 | @item .r
|
|---|
| 7693 | @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
|
|---|
| 7694 | @end table
|
|---|
| 7695 |
|
|---|
| 7696 | @item Compiling Modula-2 programs
|
|---|
| 7697 | @cindex Modula-2, rule to compile
|
|---|
| 7698 | @pindex m2c
|
|---|
| 7699 | @pindex .sym
|
|---|
| 7700 | @pindex .def
|
|---|
| 7701 | @pindex .mod
|
|---|
| 7702 | @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
|
|---|
| 7703 | of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
|
|---|
| 7704 | is made from @file{@var{n}.mod}; the form is:
|
|---|
| 7705 | @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
|
|---|
| 7706 |
|
|---|
| 7707 | @need 1200
|
|---|
| 7708 | @item Assembling and preprocessing assembler programs
|
|---|
| 7709 | @cindex assembly, rule to compile
|
|---|
| 7710 | @pindex as
|
|---|
| 7711 | @pindex .s
|
|---|
| 7712 | @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
|
|---|
| 7713 | running the assembler, @code{as}. The precise command is
|
|---|
| 7714 | @samp{$(AS) $(ASFLAGS)}.@refill
|
|---|
| 7715 |
|
|---|
| 7716 | @pindex .S
|
|---|
| 7717 | @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
|
|---|
| 7718 | running the C preprocessor, @code{cpp}. The precise command is
|
|---|
| 7719 | @w{@samp{$(CPP) $(CPPFLAGS)}}.
|
|---|
| 7720 |
|
|---|
| 7721 | @item Linking a single object file
|
|---|
| 7722 | @cindex linking, predefined rule for
|
|---|
| 7723 | @pindex ld
|
|---|
| 7724 | @pindex .o
|
|---|
| 7725 | @file{@var{n}} is made automatically from @file{@var{n}.o} by running
|
|---|
| 7726 | the linker (usually called @code{ld}) via the C compiler. The precise
|
|---|
| 7727 | command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
|
|---|
| 7728 |
|
|---|
| 7729 | This rule does the right thing for a simple program with only one
|
|---|
| 7730 | source file. It will also do the right thing if there are multiple
|
|---|
| 7731 | object files (presumably coming from various other source files), one
|
|---|
| 7732 | of which has a name matching that of the executable file. Thus,
|
|---|
| 7733 |
|
|---|
| 7734 | @example
|
|---|
| 7735 | x: y.o z.o
|
|---|
| 7736 | @end example
|
|---|
| 7737 |
|
|---|
| 7738 | @noindent
|
|---|
| 7739 | when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
|
|---|
| 7740 |
|
|---|
| 7741 | @example
|
|---|
| 7742 | @group
|
|---|
| 7743 | cc -c x.c -o x.o
|
|---|
| 7744 | cc -c y.c -o y.o
|
|---|
| 7745 | cc -c z.c -o z.o
|
|---|
| 7746 | cc x.o y.o z.o -o x
|
|---|
| 7747 | rm -f x.o
|
|---|
| 7748 | rm -f y.o
|
|---|
| 7749 | rm -f z.o
|
|---|
| 7750 | @end group
|
|---|
| 7751 | @end example
|
|---|
| 7752 |
|
|---|
| 7753 | @noindent
|
|---|
| 7754 | In more complicated cases, such as when there is no object file whose
|
|---|
| 7755 | name derives from the executable file name, you must write an explicit
|
|---|
| 7756 | command for linking.
|
|---|
| 7757 |
|
|---|
| 7758 | Each kind of file automatically made into @samp{.o} object files will
|
|---|
| 7759 | be automatically linked by using the compiler (@samp{$(CC)},
|
|---|
| 7760 | @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
|
|---|
| 7761 | assemble @samp{.s} files) without the @samp{-c} option. This could be
|
|---|
| 7762 | done by using the @samp{.o} object files as intermediates, but it is
|
|---|
| 7763 | faster to do the compiling and linking in one step, so that's how it's
|
|---|
| 7764 | done.@refill
|
|---|
| 7765 |
|
|---|
| 7766 | @item Yacc for C programs
|
|---|
| 7767 | @pindex yacc
|
|---|
| 7768 | @cindex Yacc, rule to run
|
|---|
| 7769 | @pindex .y
|
|---|
| 7770 | @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
|
|---|
| 7771 | running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
|
|---|
| 7772 |
|
|---|
| 7773 | @item Lex for C programs
|
|---|
| 7774 | @pindex lex
|
|---|
| 7775 | @cindex Lex, rule to run
|
|---|
| 7776 | @pindex .l
|
|---|
| 7777 | @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
|
|---|
| 7778 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
|---|
| 7779 |
|
|---|
| 7780 | @item Lex for Ratfor programs
|
|---|
| 7781 | @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
|
|---|
| 7782 | running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
|
|---|
| 7783 |
|
|---|
| 7784 | The convention of using the same suffix @samp{.l} for all Lex files
|
|---|
| 7785 | regardless of whether they produce C code or Ratfor code makes it
|
|---|
| 7786 | impossible for @code{make} to determine automatically which of the two
|
|---|
| 7787 | languages you are using in any particular case. If @code{make} is
|
|---|
| 7788 | called upon to remake an object file from a @samp{.l} file, it must
|
|---|
| 7789 | guess which compiler to use. It will guess the C compiler, because
|
|---|
| 7790 | that is more common. If you are using Ratfor, make sure @code{make}
|
|---|
| 7791 | knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
|
|---|
| 7792 | are using Ratfor exclusively, with no C files, remove @samp{.c} from
|
|---|
| 7793 | the list of implicit rule suffixes with:@refill
|
|---|
| 7794 |
|
|---|
| 7795 | @example
|
|---|
| 7796 | @group
|
|---|
| 7797 | .SUFFIXES:
|
|---|
| 7798 | .SUFFIXES: .o .r .f .l @dots{}
|
|---|
| 7799 | @end group
|
|---|
| 7800 | @end example
|
|---|
| 7801 |
|
|---|
| 7802 | @item Making Lint Libraries from C, Yacc, or Lex programs
|
|---|
| 7803 | @pindex lint
|
|---|
| 7804 | @cindex @code{lint}, rule to run
|
|---|
| 7805 | @pindex .ln
|
|---|
| 7806 | @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
|
|---|
| 7807 | The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
|
|---|
| 7808 | The same command is used on the C code produced from
|
|---|
| 7809 | @file{@var{n}.y} or @file{@var{n}.l}.@refill
|
|---|
| 7810 |
|
|---|
| 7811 | @item @TeX{} and Web
|
|---|
| 7812 | @cindex @TeX{}, rule to run
|
|---|
| 7813 | @cindex Web, rule to run
|
|---|
| 7814 | @pindex tex
|
|---|
| 7815 | @pindex cweave
|
|---|
| 7816 | @pindex weave
|
|---|
| 7817 | @pindex tangle
|
|---|
| 7818 | @pindex ctangle
|
|---|
| 7819 | @pindex .dvi
|
|---|
| 7820 | @pindex .tex
|
|---|
| 7821 | @pindex .web
|
|---|
| 7822 | @pindex .w
|
|---|
| 7823 | @pindex .ch
|
|---|
| 7824 | @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
|
|---|
| 7825 | @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
|
|---|
| 7826 | @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
|
|---|
| 7827 | it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
|
|---|
| 7828 | made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
|
|---|
| 7829 | is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
|
|---|
| 7830 | or can be made) with @samp{$(CTANGLE)}.@refill
|
|---|
| 7831 |
|
|---|
| 7832 | @item Texinfo and Info
|
|---|
| 7833 | @cindex Texinfo, rule to format
|
|---|
| 7834 | @cindex Info, rule to format
|
|---|
| 7835 | @pindex texi2dvi
|
|---|
| 7836 | @pindex makeinfo
|
|---|
| 7837 | @pindex .texinfo
|
|---|
| 7838 | @pindex .info
|
|---|
| 7839 | @pindex .texi
|
|---|
| 7840 | @pindex .txinfo
|
|---|
| 7841 | @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
|
|---|
| 7842 | @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
|
|---|
| 7843 | @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
|
|---|
| 7844 | @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
|
|---|
| 7845 | the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
|
|---|
| 7846 |
|
|---|
| 7847 | @item RCS
|
|---|
| 7848 | @cindex RCS, rule to extract from
|
|---|
| 7849 | @pindex co
|
|---|
| 7850 | @pindex ,v @r{(RCS file extension)}
|
|---|
| 7851 | Any file @file{@var{n}} is extracted if necessary from an RCS file
|
|---|
| 7852 | named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
|
|---|
| 7853 | command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
|
|---|
| 7854 | extracted from RCS if it already exists, even if the RCS file is
|
|---|
| 7855 | newer. The rules for RCS are terminal
|
|---|
| 7856 | (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
|---|
| 7857 | so RCS files cannot be generated from another source; they must
|
|---|
| 7858 | actually exist.@refill
|
|---|
| 7859 |
|
|---|
| 7860 | @item SCCS
|
|---|
| 7861 | @cindex SCCS, rule to extract from
|
|---|
| 7862 | @pindex get
|
|---|
| 7863 | @pindex s. @r{(SCCS file prefix)}
|
|---|
| 7864 | Any file @file{@var{n}} is extracted if necessary from an SCCS file
|
|---|
| 7865 | named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
|
|---|
| 7866 | command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
|
|---|
| 7867 | terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
|
|---|
| 7868 | so SCCS files cannot be generated from another source; they must
|
|---|
| 7869 | actually exist.@refill
|
|---|
| 7870 |
|
|---|
| 7871 | @pindex .sh
|
|---|
| 7872 | For the benefit of SCCS, a file @file{@var{n}} is copied from
|
|---|
| 7873 | @file{@var{n}.sh} and made executable (by everyone). This is for
|
|---|
| 7874 | shell scripts that are checked into SCCS. Since RCS preserves the
|
|---|
| 7875 | execution permission of a file, you do not need to use this feature
|
|---|
| 7876 | with RCS.@refill
|
|---|
| 7877 |
|
|---|
| 7878 | We recommend that you avoid using of SCCS. RCS is widely held to be
|
|---|
| 7879 | superior, and is also free. By choosing free software in place of
|
|---|
| 7880 | comparable (or inferior) proprietary software, you support the free
|
|---|
| 7881 | software movement.
|
|---|
| 7882 | @end table
|
|---|
| 7883 |
|
|---|
| 7884 | Usually, you want to change only the variables listed in the table
|
|---|
| 7885 | above, which are documented in the following section.
|
|---|
| 7886 |
|
|---|
| 7887 | However, the commands in built-in implicit rules actually use
|
|---|
| 7888 | variables such as @code{COMPILE.c}, @code{LINK.p}, and
|
|---|
| 7889 | @code{PREPROCESS.S}, whose values contain the commands listed above.
|
|---|
| 7890 |
|
|---|
| 7891 | @code{make} follows the convention that the rule to compile a
|
|---|
| 7892 | @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
|
|---|
| 7893 | Similarly, the rule to produce an executable from a @file{.@var{x}}
|
|---|
| 7894 | file uses @code{LINK.@var{x}}; and the rule to preprocess a
|
|---|
| 7895 | @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
|
|---|
| 7896 |
|
|---|
| 7897 | @vindex OUTPUT_OPTION
|
|---|
| 7898 | Every rule that produces an object file uses the variable
|
|---|
| 7899 | @code{OUTPUT_OPTION}. @code{make} defines this variable either to
|
|---|
| 7900 | contain @samp{-o $@@}, or to be empty, depending on a compile-time
|
|---|
| 7901 | option. You need the @samp{-o} option to ensure that the output goes
|
|---|
| 7902 | into the right file when the source file is in a different directory,
|
|---|
| 7903 | as when using @code{VPATH} (@pxref{Directory Search}). However,
|
|---|
| 7904 | compilers on some systems do not accept a @samp{-o} switch for object
|
|---|
| 7905 | files. If you use such a system, and use @code{VPATH}, some
|
|---|
| 7906 | compilations will put their output in the wrong place.
|
|---|
| 7907 | A possible workaround for this problem is to give @code{OUTPUT_OPTION}
|
|---|
| 7908 | the value @w{@samp{; mv $*.o $@@}}.
|
|---|
| 7909 |
|
|---|
| 7910 | @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
|
|---|
| 7911 | @section Variables Used by Implicit Rules
|
|---|
| 7912 | @cindex flags for compilers
|
|---|
| 7913 |
|
|---|
| 7914 | The commands in built-in implicit rules make liberal use of certain
|
|---|
| 7915 | predefined variables. You can alter these variables in the makefile,
|
|---|
| 7916 | with arguments to @code{make}, or in the environment to alter how the
|
|---|
| 7917 | implicit rules work without redefining the rules themselves. You can
|
|---|
| 7918 | cancel all variables used by implicit rules with the @samp{-R} or
|
|---|
| 7919 | @samp{--no-builtin-variables} option.
|
|---|
| 7920 |
|
|---|
| 7921 | For example, the command used to compile a C source file actually says
|
|---|
| 7922 | @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
|
|---|
| 7923 | used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
|
|---|
| 7924 | redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
|
|---|
| 7925 | used for all C compilations performed by the implicit rule. By redefining
|
|---|
| 7926 | @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
|
|---|
| 7927 | each compilation. @emph{All} implicit rules that do C compilation use
|
|---|
| 7928 | @samp{$(CC)} to get the program name for the compiler and @emph{all}
|
|---|
| 7929 | include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
|
|---|
| 7930 |
|
|---|
| 7931 | The variables used in implicit rules fall into two classes: those that are
|
|---|
| 7932 | names of programs (like @code{CC}) and those that contain arguments for the
|
|---|
| 7933 | programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
|
|---|
| 7934 | some command arguments, but it must start with an actual executable program
|
|---|
| 7935 | name.) If a variable value contains more than one argument, separate them
|
|---|
| 7936 | with spaces.
|
|---|
| 7937 |
|
|---|
| 7938 | Here is a table of variables used as names of programs in built-in rules:
|
|---|
| 7939 |
|
|---|
| 7940 | @table @code
|
|---|
| 7941 | @item AR
|
|---|
| 7942 | @vindex AR
|
|---|
| 7943 | Archive-maintaining program; default @samp{ar}.
|
|---|
| 7944 | @pindex ar
|
|---|
| 7945 |
|
|---|
| 7946 | @item AS
|
|---|
| 7947 | @vindex AS
|
|---|
| 7948 | Program for doing assembly; default @samp{as}.
|
|---|
| 7949 | @pindex as
|
|---|
| 7950 |
|
|---|
| 7951 | @item CC
|
|---|
| 7952 | @vindex CC
|
|---|
| 7953 | Program for compiling C programs; default @samp{cc}.
|
|---|
| 7954 | @pindex cc
|
|---|
| 7955 |
|
|---|
| 7956 | @item CXX
|
|---|
| 7957 | @vindex CXX
|
|---|
| 7958 | Program for compiling C++ programs; default @samp{g++}.
|
|---|
| 7959 | @pindex g++
|
|---|
| 7960 |
|
|---|
| 7961 | @item CO
|
|---|
| 7962 | @vindex CO
|
|---|
| 7963 | Program for extracting a file from RCS; default @samp{co}.
|
|---|
| 7964 | @pindex co
|
|---|
| 7965 |
|
|---|
| 7966 | @item CPP
|
|---|
| 7967 | @vindex CPP
|
|---|
| 7968 | Program for running the C preprocessor, with results to standard output;
|
|---|
| 7969 | default @samp{$(CC) -E}.
|
|---|
| 7970 |
|
|---|
| 7971 | @item FC
|
|---|
| 7972 | @vindex FC
|
|---|
| 7973 | Program for compiling or preprocessing Fortran and Ratfor programs;
|
|---|
| 7974 | default @samp{f77}.
|
|---|
| 7975 | @pindex f77
|
|---|
| 7976 |
|
|---|
| 7977 | @item GET
|
|---|
| 7978 | @vindex GET
|
|---|
| 7979 | Program for extracting a file from SCCS; default @samp{get}.
|
|---|
| 7980 | @pindex get
|
|---|
| 7981 |
|
|---|
| 7982 | @item LEX
|
|---|
| 7983 | @vindex LEX
|
|---|
| 7984 | Program to use to turn Lex grammars into C programs or Ratfor programs;
|
|---|
| 7985 | default @samp{lex}.
|
|---|
| 7986 | @pindex lex
|
|---|
| 7987 |
|
|---|
| 7988 | @item PC
|
|---|
| 7989 | @vindex PC
|
|---|
| 7990 | Program for compiling Pascal programs; default @samp{pc}.
|
|---|
| 7991 | @pindex pc
|
|---|
| 7992 |
|
|---|
| 7993 | @item YACC
|
|---|
| 7994 | @vindex YACC
|
|---|
| 7995 | Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
|
|---|
| 7996 | @pindex yacc
|
|---|
| 7997 |
|
|---|
| 7998 | @item YACCR
|
|---|
| 7999 | @vindex YACCR
|
|---|
| 8000 | Program to use to turn Yacc grammars into Ratfor
|
|---|
| 8001 | programs; default @samp{yacc -r}.
|
|---|
| 8002 |
|
|---|
| 8003 | @item MAKEINFO
|
|---|
| 8004 | @vindex MAKEINFO
|
|---|
| 8005 | Program to convert a Texinfo source file into an Info file; default
|
|---|
| 8006 | @samp{makeinfo}.
|
|---|
| 8007 | @pindex makeinfo
|
|---|
| 8008 |
|
|---|
| 8009 | @item TEX
|
|---|
| 8010 | @vindex TEX
|
|---|
| 8011 | Program to make @TeX{} @sc{dvi} files from @TeX{} source;
|
|---|
| 8012 | default @samp{tex}.
|
|---|
| 8013 | @pindex tex
|
|---|
| 8014 |
|
|---|
| 8015 | @item TEXI2DVI
|
|---|
| 8016 | @vindex TEXI2DVI
|
|---|
| 8017 | Program to make @TeX{} @sc{dvi} files from Texinfo source;
|
|---|
| 8018 | default @samp{texi2dvi}.
|
|---|
| 8019 | @pindex texi2dvi
|
|---|
| 8020 |
|
|---|
| 8021 | @item WEAVE
|
|---|
| 8022 | @vindex WEAVE
|
|---|
| 8023 | Program to translate Web into @TeX{}; default @samp{weave}.
|
|---|
| 8024 | @pindex weave
|
|---|
| 8025 |
|
|---|
| 8026 | @item CWEAVE
|
|---|
| 8027 | @vindex CWEAVE
|
|---|
| 8028 | Program to translate C Web into @TeX{}; default @samp{cweave}.
|
|---|
| 8029 | @pindex cweave
|
|---|
| 8030 |
|
|---|
| 8031 | @item TANGLE
|
|---|
| 8032 | @vindex TANGLE
|
|---|
| 8033 | Program to translate Web into Pascal; default @samp{tangle}.
|
|---|
| 8034 | @pindex tangle
|
|---|
| 8035 |
|
|---|
| 8036 | @item CTANGLE
|
|---|
| 8037 | @vindex CTANGLE
|
|---|
| 8038 | Program to translate C Web into C; default @samp{ctangle}.
|
|---|
| 8039 | @pindex ctangle
|
|---|
| 8040 |
|
|---|
| 8041 | @item RM
|
|---|
| 8042 | @vindex RM
|
|---|
| 8043 | Command to remove a file; default @samp{rm -f}.
|
|---|
| 8044 | @pindex rm
|
|---|
| 8045 | @end table
|
|---|
| 8046 |
|
|---|
| 8047 | Here is a table of variables whose values are additional arguments for the
|
|---|
| 8048 | programs above. The default values for all of these is the empty
|
|---|
| 8049 | string, unless otherwise noted.
|
|---|
| 8050 |
|
|---|
| 8051 | @table @code
|
|---|
| 8052 | @item ARFLAGS
|
|---|
| 8053 | @vindex ARFLAGS
|
|---|
| 8054 | Flags to give the archive-maintaining program; default @samp{rv}.
|
|---|
| 8055 |
|
|---|
| 8056 | @item ASFLAGS
|
|---|
| 8057 | @vindex ASFLAGS
|
|---|
| 8058 | Extra flags to give to the assembler (when explicitly
|
|---|
| 8059 | invoked on a @samp{.s} or @samp{.S} file).
|
|---|
| 8060 |
|
|---|
| 8061 | @item CFLAGS
|
|---|
| 8062 | @vindex CFLAGS
|
|---|
| 8063 | Extra flags to give to the C compiler.
|
|---|
| 8064 |
|
|---|
| 8065 | @item CXXFLAGS
|
|---|
| 8066 | @vindex CXXFLAGS
|
|---|
| 8067 | Extra flags to give to the C++ compiler.
|
|---|
| 8068 |
|
|---|
| 8069 | @item COFLAGS
|
|---|
| 8070 | @vindex COFLAGS
|
|---|
| 8071 | Extra flags to give to the RCS @code{co} program.
|
|---|
| 8072 |
|
|---|
| 8073 | @item CPPFLAGS
|
|---|
| 8074 | @vindex CPPFLAGS
|
|---|
| 8075 | Extra flags to give to the C preprocessor and programs
|
|---|
| 8076 | that use it (the C and Fortran compilers).
|
|---|
| 8077 |
|
|---|
| 8078 | @item FFLAGS
|
|---|
| 8079 | @vindex FFLAGS
|
|---|
| 8080 | Extra flags to give to the Fortran compiler.
|
|---|
| 8081 |
|
|---|
| 8082 | @item GFLAGS
|
|---|
| 8083 | @vindex GFLAGS
|
|---|
| 8084 | Extra flags to give to the SCCS @code{get} program.
|
|---|
| 8085 |
|
|---|
| 8086 | @item LDFLAGS
|
|---|
| 8087 | @vindex LDFLAGS
|
|---|
| 8088 | Extra flags to give to compilers when they are
|
|---|
| 8089 | supposed to invoke the linker, @samp{ld}.
|
|---|
| 8090 |
|
|---|
| 8091 | @item LFLAGS
|
|---|
| 8092 | @vindex LFLAGS
|
|---|
| 8093 | Extra flags to give to Lex.
|
|---|
| 8094 |
|
|---|
| 8095 | @item PFLAGS
|
|---|
| 8096 | @vindex PFLAGS
|
|---|
| 8097 | Extra flags to give to the Pascal compiler.
|
|---|
| 8098 |
|
|---|
| 8099 | @item RFLAGS
|
|---|
| 8100 | @vindex RFLAGS
|
|---|
| 8101 | Extra flags to give to the Fortran compiler for Ratfor programs.
|
|---|
| 8102 |
|
|---|
| 8103 | @item YFLAGS
|
|---|
| 8104 | @vindex YFLAGS
|
|---|
| 8105 | Extra flags to give to Yacc.
|
|---|
| 8106 | @end table
|
|---|
| 8107 |
|
|---|
| 8108 | @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
|
|---|
| 8109 | @section Chains of Implicit Rules
|
|---|
| 8110 |
|
|---|
| 8111 | @cindex chains of rules
|
|---|
| 8112 | @cindex rule, implicit, chains of
|
|---|
| 8113 | Sometimes a file can be made by a sequence of implicit rules. For example,
|
|---|
| 8114 | a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
|
|---|
| 8115 | first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
|
|---|
| 8116 |
|
|---|
| 8117 | If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
|
|---|
| 8118 | special searching is required: @code{make} finds that the object file can
|
|---|
| 8119 | be made by C compilation from @file{@var{n}.c}; later on, when considering
|
|---|
| 8120 | how to make @file{@var{n}.c}, the rule for running Yacc is
|
|---|
| 8121 | used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
|
|---|
| 8122 | updated.@refill
|
|---|
| 8123 |
|
|---|
| 8124 | @cindex intermediate files
|
|---|
| 8125 | @cindex files, intermediate
|
|---|
| 8126 | However, even if @file{@var{n}.c} does not exist and is not mentioned,
|
|---|
| 8127 | @code{make} knows how to envision it as the missing link between
|
|---|
| 8128 | @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
|
|---|
| 8129 | called an @dfn{intermediate file}. Once @code{make} has decided to use the
|
|---|
| 8130 | intermediate file, it is entered in the data base as if it had been
|
|---|
| 8131 | mentioned in the makefile, along with the implicit rule that says how to
|
|---|
| 8132 | create it.@refill
|
|---|
| 8133 |
|
|---|
| 8134 | Intermediate files are remade using their rules just like all other
|
|---|
| 8135 | files. But intermediate files are treated differently in two ways.
|
|---|
| 8136 |
|
|---|
| 8137 | The first difference is what happens if the intermediate file does not
|
|---|
| 8138 | exist. If an ordinary file @var{b} does not exist, and @code{make}
|
|---|
| 8139 | considers a target that depends on @var{b}, it invariably creates
|
|---|
| 8140 | @var{b} and then updates the target from @var{b}. But if @var{b} is an
|
|---|
| 8141 | intermediate file, then @code{make} can leave well enough alone. It
|
|---|
| 8142 | won't bother updating @var{b}, or the ultimate target, unless some
|
|---|
| 8143 | prerequisite of @var{b} is newer than that target or there is some other
|
|---|
| 8144 | reason to update that target.
|
|---|
| 8145 |
|
|---|
| 8146 | The second difference is that if @code{make} @emph{does} create @var{b}
|
|---|
| 8147 | in order to update something else, it deletes @var{b} later on after it
|
|---|
| 8148 | is no longer needed. Therefore, an intermediate file which did not
|
|---|
| 8149 | exist before @code{make} also does not exist after @code{make}.
|
|---|
| 8150 | @code{make} reports the deletion to you by printing a @samp{rm -f}
|
|---|
| 8151 | command showing which file it is deleting.
|
|---|
| 8152 |
|
|---|
| 8153 | Ordinarily, a file cannot be intermediate if it is mentioned in the
|
|---|
| 8154 | makefile as a target or prerequisite. However, you can explicitly mark a
|
|---|
| 8155 | file as intermediate by listing it as a prerequisite of the special target
|
|---|
| 8156 | @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
|
|---|
| 8157 | explicitly in some other way.
|
|---|
| 8158 |
|
|---|
| 8159 | @cindex intermediate files, preserving
|
|---|
| 8160 | @cindex preserving intermediate files
|
|---|
| 8161 | @cindex secondary files
|
|---|
| 8162 | You can prevent automatic deletion of an intermediate file by marking it
|
|---|
| 8163 | as a @dfn{secondary} file. To do this, list it as a prerequisite of the
|
|---|
| 8164 | special target @code{.SECONDARY}. When a file is secondary, @code{make}
|
|---|
| 8165 | will not create the file merely because it does not already exist, but
|
|---|
| 8166 | @code{make} does not automatically delete the file. Marking a file as
|
|---|
| 8167 | secondary also marks it as intermediate.
|
|---|
| 8168 |
|
|---|
| 8169 | You can list the target pattern of an implicit rule (such as @samp{%.o})
|
|---|
| 8170 | as a prerequisite of the special target @code{.PRECIOUS} to preserve
|
|---|
| 8171 | intermediate files made by implicit rules whose target patterns match
|
|---|
| 8172 | that file's name; see @ref{Interrupts}.@refill
|
|---|
| 8173 | @cindex preserving with @code{.PRECIOUS}
|
|---|
| 8174 | @cindex @code{.PRECIOUS} intermediate files
|
|---|
| 8175 |
|
|---|
| 8176 | A chain can involve more than two implicit rules. For example, it is
|
|---|
| 8177 | possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
|
|---|
| 8178 | Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
|
|---|
| 8179 | intermediate files that are deleted at the end.@refill
|
|---|
| 8180 |
|
|---|
| 8181 | No single implicit rule can appear more than once in a chain. This means
|
|---|
| 8182 | that @code{make} will not even consider such a ridiculous thing as making
|
|---|
| 8183 | @file{foo} from @file{foo.o.o} by running the linker twice. This
|
|---|
| 8184 | constraint has the added benefit of preventing any infinite loop in the
|
|---|
| 8185 | search for an implicit rule chain.
|
|---|
| 8186 |
|
|---|
| 8187 | There are some special implicit rules to optimize certain cases that would
|
|---|
| 8188 | otherwise be handled by rule chains. For example, making @file{foo} from
|
|---|
| 8189 | @file{foo.c} could be handled by compiling and linking with separate
|
|---|
| 8190 | chained rules, using @file{foo.o} as an intermediate file. But what
|
|---|
| 8191 | actually happens is that a special rule for this case does the compilation
|
|---|
| 8192 | and linking with a single @code{cc} command. The optimized rule is used in
|
|---|
| 8193 | preference to the step-by-step chain because it comes earlier in the
|
|---|
| 8194 | ordering of rules.
|
|---|
| 8195 |
|
|---|
| 8196 | @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
|
|---|
| 8197 | @section Defining and Redefining Pattern Rules
|
|---|
| 8198 |
|
|---|
| 8199 | You define an implicit rule by writing a @dfn{pattern rule}. A pattern
|
|---|
| 8200 | rule looks like an ordinary rule, except that its target contains the
|
|---|
| 8201 | character @samp{%} (exactly one of them). The target is considered a
|
|---|
| 8202 | pattern for matching file names; the @samp{%} can match any nonempty
|
|---|
| 8203 | substring, while other characters match only themselves. The prerequisites
|
|---|
| 8204 | likewise use @samp{%} to show how their names relate to the target name.
|
|---|
| 8205 |
|
|---|
| 8206 | Thus, a pattern rule @samp{%.o : %.c} says how to make any file
|
|---|
| 8207 | @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
|
|---|
| 8208 |
|
|---|
| 8209 | Note that expansion using @samp{%} in pattern rules occurs
|
|---|
| 8210 | @strong{after} any variable or function expansions, which take place
|
|---|
| 8211 | when the makefile is read. @xref{Using Variables, , How to Use
|
|---|
| 8212 | Variables}, and @ref{Functions, ,Functions for Transforming Text}.
|
|---|
| 8213 |
|
|---|
| 8214 | @menu
|
|---|
| 8215 | * Pattern Intro:: An introduction to pattern rules.
|
|---|
| 8216 | * Pattern Examples:: Examples of pattern rules.
|
|---|
| 8217 | * Automatic Variables:: How to use automatic variables in the
|
|---|
| 8218 | commands of implicit rules.
|
|---|
| 8219 | * Pattern Match:: How patterns match.
|
|---|
| 8220 | * Match-Anything Rules:: Precautions you should take prior to
|
|---|
| 8221 | defining rules that can match any
|
|---|
| 8222 | target file whatever.
|
|---|
| 8223 | * Canceling Rules:: How to override or cancel built-in rules.
|
|---|
| 8224 | @end menu
|
|---|
| 8225 |
|
|---|
| 8226 | @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
|
|---|
| 8227 | @subsection Introduction to Pattern Rules
|
|---|
| 8228 | @cindex pattern rule
|
|---|
| 8229 | @cindex rule, pattern
|
|---|
| 8230 |
|
|---|
| 8231 | A pattern rule contains the character @samp{%} (exactly one of them)
|
|---|
| 8232 | in the target; otherwise, it looks exactly like an ordinary rule. The
|
|---|
| 8233 | target is a pattern for matching file names; the @samp{%} matches any
|
|---|
| 8234 | nonempty substring, while other characters match only themselves.
|
|---|
| 8235 | @cindex target pattern, implicit
|
|---|
| 8236 | @cindex @code{%}, in pattern rules
|
|---|
| 8237 |
|
|---|
| 8238 | For example, @samp{%.c} as a pattern matches any file name that ends in
|
|---|
| 8239 | @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
|
|---|
| 8240 | with @samp{s.}, ends in @samp{.c} and is at least five characters long.
|
|---|
| 8241 | (There must be at least one character to match the @samp{%}.) The substring
|
|---|
| 8242 | that the @samp{%} matches is called the @dfn{stem}.@refill
|
|---|
| 8243 |
|
|---|
| 8244 | @samp{%} in a prerequisite of a pattern rule stands for the same stem
|
|---|
| 8245 | that was matched by the @samp{%} in the target. In order for
|
|---|
| 8246 | the pattern rule to apply, its target pattern must match the file name
|
|---|
| 8247 | under consideration, and its prerequisite patterns must name files that
|
|---|
| 8248 | exist or can be made. These files become prerequisites of the target.
|
|---|
| 8249 | @cindex prerequisite pattern, implicit
|
|---|
| 8250 |
|
|---|
| 8251 | Thus, a rule of the form
|
|---|
| 8252 |
|
|---|
| 8253 | @example
|
|---|
| 8254 | %.o : %.c ; @var{command}@dots{}
|
|---|
| 8255 | @end example
|
|---|
| 8256 |
|
|---|
| 8257 | @noindent
|
|---|
| 8258 | specifies how to make a file @file{@var{n}.o}, with another file
|
|---|
| 8259 | @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
|
|---|
| 8260 | exists or can be made.
|
|---|
| 8261 |
|
|---|
| 8262 | There may also be prerequisites that do not use @samp{%}; such a prerequisite
|
|---|
| 8263 | attaches to every file made by this pattern rule. These unvarying
|
|---|
| 8264 | prerequisites are useful occasionally.
|
|---|
| 8265 |
|
|---|
| 8266 | A pattern rule need not have any prerequisites that contain @samp{%}, or
|
|---|
| 8267 | in fact any prerequisites at all. Such a rule is effectively a general
|
|---|
| 8268 | wildcard. It provides a way to make any file that matches the target
|
|---|
| 8269 | pattern. @xref{Last Resort}.
|
|---|
| 8270 |
|
|---|
| 8271 | @c !!! The end of of this paragraph should be rewritten. --bob
|
|---|
| 8272 | Pattern rules may have more than one target. Unlike normal rules, this
|
|---|
| 8273 | does not act as many different rules with the same prerequisites and
|
|---|
| 8274 | commands. If a pattern rule has multiple targets, @code{make} knows that
|
|---|
| 8275 | the rule's commands are responsible for making all of the targets. The
|
|---|
| 8276 | commands are executed only once to make all the targets. When searching
|
|---|
| 8277 | for a pattern rule to match a target, the target patterns of a rule other
|
|---|
| 8278 | than the one that matches the target in need of a rule are incidental:
|
|---|
| 8279 | @code{make} worries only about giving commands and prerequisites to the file
|
|---|
| 8280 | presently in question. However, when this file's commands are run, the
|
|---|
| 8281 | other targets are marked as having been updated themselves.
|
|---|
| 8282 | @cindex multiple targets, in pattern rule
|
|---|
| 8283 | @cindex target, multiple in pattern rule
|
|---|
| 8284 |
|
|---|
| 8285 | The order in which pattern rules appear in the makefile is important
|
|---|
| 8286 | since this is the order in which they are considered.
|
|---|
| 8287 | Of equally applicable
|
|---|
| 8288 | rules, only the first one found is used. The rules you write take precedence
|
|---|
| 8289 | over those that are built in. Note however, that a rule whose
|
|---|
| 8290 | prerequisites actually exist or are mentioned always takes priority over a
|
|---|
| 8291 | rule with prerequisites that must be made by chaining other implicit rules.
|
|---|
| 8292 | @cindex pattern rules, order of
|
|---|
| 8293 | @cindex order of pattern rules
|
|---|
| 8294 |
|
|---|
| 8295 | @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
|
|---|
| 8296 | @subsection Pattern Rule Examples
|
|---|
| 8297 |
|
|---|
| 8298 | Here are some examples of pattern rules actually predefined in
|
|---|
| 8299 | @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
|
|---|
| 8300 | files:@refill
|
|---|
| 8301 |
|
|---|
| 8302 | @example
|
|---|
| 8303 | %.o : %.c
|
|---|
| 8304 | $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
|
|---|
| 8305 | @end example
|
|---|
| 8306 |
|
|---|
| 8307 | @noindent
|
|---|
| 8308 | defines a rule that can make any file @file{@var{x}.o} from
|
|---|
| 8309 | @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
|
|---|
| 8310 | @samp{$<} to substitute the names of the target file and the source file
|
|---|
| 8311 | in each case where the rule applies (@pxref{Automatic Variables}).@refill
|
|---|
| 8312 |
|
|---|
| 8313 | Here is a second built-in rule:
|
|---|
| 8314 |
|
|---|
| 8315 | @example
|
|---|
| 8316 | % :: RCS/%,v
|
|---|
| 8317 | $(CO) $(COFLAGS) $<
|
|---|
| 8318 | @end example
|
|---|
| 8319 |
|
|---|
| 8320 | @noindent
|
|---|
| 8321 | defines a rule that can make any file @file{@var{x}} whatsoever from a
|
|---|
| 8322 | corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
|
|---|
| 8323 | the target is @samp{%}, this rule will apply to any file whatever, provided
|
|---|
| 8324 | the appropriate prerequisite file exists. The double colon makes the rule
|
|---|
| 8325 | @dfn{terminal}, which means that its prerequisite may not be an intermediate
|
|---|
| 8326 | file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
|
|---|
| 8327 |
|
|---|
| 8328 | @need 500
|
|---|
| 8329 | This pattern rule has two targets:
|
|---|
| 8330 |
|
|---|
| 8331 | @example
|
|---|
| 8332 | @group
|
|---|
| 8333 | %.tab.c %.tab.h: %.y
|
|---|
| 8334 | bison -d $<
|
|---|
| 8335 | @end group
|
|---|
| 8336 | @end example
|
|---|
| 8337 |
|
|---|
| 8338 | @noindent
|
|---|
| 8339 | @c The following paragraph is rewritten to avoid overfull hboxes
|
|---|
| 8340 | This tells @code{make} that the command @samp{bison -d @var{x}.y} will
|
|---|
| 8341 | make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
|
|---|
| 8342 | @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
|
|---|
| 8343 | and the file @file{scan.o} depends on the file @file{parse.tab.h},
|
|---|
| 8344 | when @file{parse.y} is changed, the command @samp{bison -d parse.y}
|
|---|
| 8345 | will be executed only once, and the prerequisites of both
|
|---|
| 8346 | @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
|
|---|
| 8347 | the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
|
|---|
| 8348 | and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
|
|---|
| 8349 | linked from @file{parse.tab.o}, @file{scan.o}, and its other
|
|---|
| 8350 | prerequisites, and it will execute happily ever after.)@refill
|
|---|
| 8351 |
|
|---|
| 8352 | @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
|
|---|
| 8353 | @subsection Automatic Variables
|
|---|
| 8354 | @cindex automatic variables
|
|---|
| 8355 | @cindex variables, automatic
|
|---|
| 8356 | @cindex variables, and implicit rule
|
|---|
| 8357 |
|
|---|
| 8358 | Suppose you are writing a pattern rule to compile a @samp{.c} file into a
|
|---|
| 8359 | @samp{.o} file: how do you write the @samp{cc} command so that it operates
|
|---|
| 8360 | on the right source file name? You cannot write the name in the command,
|
|---|
| 8361 | because the name is different each time the implicit rule is applied.
|
|---|
| 8362 |
|
|---|
| 8363 | What you do is use a special feature of @code{make}, the @dfn{automatic
|
|---|
| 8364 | variables}. These variables have values computed afresh for each rule that
|
|---|
| 8365 | is executed, based on the target and prerequisites of the rule. In this
|
|---|
| 8366 | example, you would use @samp{$@@} for the object file name and @samp{$<}
|
|---|
| 8367 | for the source file name.
|
|---|
| 8368 |
|
|---|
| 8369 | @cindex automatic variables in prerequisites
|
|---|
| 8370 | @cindex prerequisites, and automatic variables
|
|---|
| 8371 | It's very important that you recognize the limited scope in which
|
|---|
| 8372 | automatic variable values are available: they only have values within
|
|---|
| 8373 | the command script. In particular, you cannot use them anywhere
|
|---|
| 8374 | within the target or prerequisite lists of a rule; they have no value
|
|---|
| 8375 | there and will expand to the empty string. A common mistake is
|
|---|
| 8376 | attempting to use @code{$@@} within the prerequisites list in a rule;
|
|---|
| 8377 | this will not work. However, see below for information on the
|
|---|
| 8378 | SysV-style @code{$$@@} variables.
|
|---|
| 8379 |
|
|---|
| 8380 | Here is a table of automatic variables:
|
|---|
| 8381 |
|
|---|
| 8382 | @table @code
|
|---|
| 8383 | @vindex $@@
|
|---|
| 8384 | @vindex @@ @r{(automatic variable)}
|
|---|
| 8385 | @item $@@
|
|---|
| 8386 | The file name of the target of the rule. If the target is an archive
|
|---|
| 8387 | member, then @samp{$@@} is the name of the archive file. In a pattern
|
|---|
| 8388 | rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
|
|---|
| 8389 | Pattern Rules}), @samp{$@@} is the name of whichever target caused the
|
|---|
| 8390 | rule's commands to be run.
|
|---|
| 8391 |
|
|---|
| 8392 | @vindex $%
|
|---|
| 8393 | @vindex % @r{(automatic variable)}
|
|---|
| 8394 | @item $%
|
|---|
| 8395 | The target member name, when the target is an archive member.
|
|---|
| 8396 | @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
|
|---|
| 8397 | @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
|
|---|
| 8398 | empty when the target is not an archive member.
|
|---|
| 8399 |
|
|---|
| 8400 | @vindex $<
|
|---|
| 8401 | @vindex < @r{(automatic variable)}
|
|---|
| 8402 | @item $<
|
|---|
| 8403 | The name of the first prerequisite. If the target got its commands from
|
|---|
| 8404 | an implicit rule, this will be the first prerequisite added by the
|
|---|
| 8405 | implicit rule (@pxref{Implicit Rules}).
|
|---|
| 8406 |
|
|---|
| 8407 | @vindex $?
|
|---|
| 8408 | @vindex ? @r{(automatic variable)}
|
|---|
| 8409 | @item $?
|
|---|
| 8410 | The names of all the prerequisites that are newer than the target, with
|
|---|
| 8411 | spaces between them. For prerequisites which are archive members, only
|
|---|
| 8412 | the member named is used (@pxref{Archives}).
|
|---|
| 8413 | @cindex prerequisites, list of changed
|
|---|
| 8414 | @cindex list of changed prerequisites
|
|---|
| 8415 |
|
|---|
| 8416 | @vindex $^
|
|---|
| 8417 | @vindex ^ @r{(automatic variable)}
|
|---|
| 8418 | @item $^
|
|---|
| 8419 | The names of all the prerequisites, with spaces between them. For
|
|---|
| 8420 | prerequisites which are archive members, only the member named is used
|
|---|
| 8421 | (@pxref{Archives}). A target has only one prerequisite on each other file
|
|---|
| 8422 | it depends on, no matter how many times each file is listed as a
|
|---|
| 8423 | prerequisite. So if you list a prerequisite more than once for a target,
|
|---|
| 8424 | the value of @code{$^} contains just one copy of the name.
|
|---|
| 8425 | @cindex prerequisites, list of all
|
|---|
| 8426 | @cindex list of all prerequisites
|
|---|
| 8427 |
|
|---|
| 8428 | @vindex $+
|
|---|
| 8429 | @vindex + @r{(automatic variable)}
|
|---|
| 8430 | @item $+
|
|---|
| 8431 | This is like @samp{$^}, but prerequisites listed more than once are
|
|---|
| 8432 | duplicated in the order they were listed in the makefile. This is
|
|---|
| 8433 | primarily useful for use in linking commands where it is meaningful to
|
|---|
| 8434 | repeat library file names in a particular order.
|
|---|
| 8435 |
|
|---|
| 8436 | @vindex $*
|
|---|
| 8437 | @vindex * @r{(automatic variable)}
|
|---|
| 8438 | @item $*
|
|---|
| 8439 | The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
|
|---|
| 8440 | Patterns Match}). If the target is @file{dir/a.foo.b} and the target
|
|---|
| 8441 | pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
|
|---|
| 8442 | useful for constructing names of related files.@refill
|
|---|
| 8443 | @cindex stem, variable for
|
|---|
| 8444 |
|
|---|
| 8445 | In a static pattern rule, the stem is part of the file name that matched
|
|---|
| 8446 | the @samp{%} in the target pattern.
|
|---|
| 8447 |
|
|---|
| 8448 | In an explicit rule, there is no stem; so @samp{$*} cannot be determined
|
|---|
| 8449 | in that way. Instead, if the target name ends with a recognized suffix
|
|---|
| 8450 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
|
|---|
| 8451 | the target name minus the suffix. For example, if the target name is
|
|---|
| 8452 | @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
|
|---|
| 8453 | suffix. GNU @code{make} does this bizarre thing only for compatibility
|
|---|
| 8454 | with other implementations of @code{make}. You should generally avoid
|
|---|
| 8455 | using @samp{$*} except in implicit rules or static pattern rules.@refill
|
|---|
| 8456 |
|
|---|
| 8457 | If the target name in an explicit rule does not end with a recognized
|
|---|
| 8458 | suffix, @samp{$*} is set to the empty string for that rule.
|
|---|
| 8459 | @end table
|
|---|
| 8460 |
|
|---|
| 8461 | @samp{$?} is useful even in explicit rules when you wish to operate on only
|
|---|
| 8462 | the prerequisites that have changed. For example, suppose that an archive
|
|---|
| 8463 | named @file{lib} is supposed to contain copies of several object files.
|
|---|
| 8464 | This rule copies just the changed object files into the archive:
|
|---|
| 8465 |
|
|---|
| 8466 | @example
|
|---|
| 8467 | @group
|
|---|
| 8468 | lib: foo.o bar.o lose.o win.o
|
|---|
| 8469 | ar r lib $?
|
|---|
| 8470 | @end group
|
|---|
| 8471 | @end example
|
|---|
| 8472 |
|
|---|
| 8473 | Of the variables listed above, four have values that are single file
|
|---|
| 8474 | names, and three have values that are lists of file names. These seven
|
|---|
| 8475 | have variants that get just the file's directory name or just the file
|
|---|
| 8476 | name within the directory. The variant variables' names are formed by
|
|---|
| 8477 | appending @samp{D} or @samp{F}, respectively. These variants are
|
|---|
| 8478 | semi-obsolete in GNU @code{make} since the functions @code{dir} and
|
|---|
| 8479 | @code{notdir} can be used to get a similar effect (@pxref{File Name
|
|---|
| 8480 | Functions, , Functions for File Names}). Note, however, that the
|
|---|
| 8481 | @samp{D} variants all omit the trailing slash which always appears in
|
|---|
| 8482 | the output of the @code{dir} function. Here is a table of the variants:
|
|---|
| 8483 |
|
|---|
| 8484 | @table @samp
|
|---|
| 8485 | @vindex $(@@D)
|
|---|
| 8486 | @vindex @@D @r{(automatic variable)}
|
|---|
| 8487 | @item $(@@D)
|
|---|
| 8488 | The directory part of the file name of the target, with the trailing
|
|---|
| 8489 | slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
|
|---|
| 8490 | @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
|
|---|
| 8491 | not contain a slash.
|
|---|
| 8492 |
|
|---|
| 8493 | @vindex $(@@F)
|
|---|
| 8494 | @vindex @@F @r{(automatic variable)}
|
|---|
| 8495 | @item $(@@F)
|
|---|
| 8496 | The file-within-directory part of the file name of the target. If the
|
|---|
| 8497 | value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
|
|---|
| 8498 | @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
|
|---|
| 8499 |
|
|---|
| 8500 | @vindex $(*D)
|
|---|
| 8501 | @vindex *D @r{(automatic variable)}
|
|---|
| 8502 | @item $(*D)
|
|---|
| 8503 | @vindex $(*F)
|
|---|
| 8504 | @vindex *F @r{(automatic variable)}
|
|---|
| 8505 | @itemx $(*F)
|
|---|
| 8506 | The directory part and the file-within-directory
|
|---|
| 8507 | part of the stem; @file{dir} and @file{foo} in this example.
|
|---|
| 8508 |
|
|---|
| 8509 | @vindex $(%D)
|
|---|
| 8510 | @vindex %D @r{(automatic variable)}
|
|---|
| 8511 | @item $(%D)
|
|---|
| 8512 | @vindex $(%F)
|
|---|
| 8513 | @vindex %F @r{(automatic variable)}
|
|---|
| 8514 | @itemx $(%F)
|
|---|
| 8515 | The directory part and the file-within-directory part of the target
|
|---|
| 8516 | archive member name. This makes sense only for archive member targets
|
|---|
| 8517 | of the form @file{@var{archive}(@var{member})} and is useful only when
|
|---|
| 8518 | @var{member} may contain a directory name. (@xref{Archive Members,
|
|---|
| 8519 | ,Archive Members as Targets}.)
|
|---|
| 8520 |
|
|---|
| 8521 | @vindex $(<D)
|
|---|
| 8522 | @vindex <D @r{(automatic variable)}
|
|---|
| 8523 | @item $(<D)
|
|---|
| 8524 | @vindex $(<F)
|
|---|
| 8525 | @vindex <F @r{(automatic variable)}
|
|---|
| 8526 | @itemx $(<F)
|
|---|
| 8527 | The directory part and the file-within-directory
|
|---|
| 8528 | part of the first prerequisite.
|
|---|
| 8529 |
|
|---|
| 8530 | @vindex $(^D)
|
|---|
| 8531 | @vindex ^D @r{(automatic variable)}
|
|---|
| 8532 | @item $(^D)
|
|---|
| 8533 | @vindex $(^F)
|
|---|
| 8534 | @vindex ^F @r{(automatic variable)}
|
|---|
| 8535 | @itemx $(^F)
|
|---|
| 8536 | Lists of the directory parts and the file-within-directory
|
|---|
| 8537 | parts of all prerequisites.
|
|---|
| 8538 |
|
|---|
| 8539 | @vindex $(+D)
|
|---|
| 8540 | @vindex +D @r{(automatic variable)}
|
|---|
| 8541 | @item $(+D)
|
|---|
| 8542 | @vindex $(+F)
|
|---|
| 8543 | @vindex +F @r{(automatic variable)}
|
|---|
| 8544 | @itemx $(+F)
|
|---|
| 8545 | Lists of the directory parts and the file-within-directory
|
|---|
| 8546 | parts of all prerequisites, including multiple instances of duplicated
|
|---|
| 8547 | prerequisites.
|
|---|
| 8548 |
|
|---|
| 8549 | @vindex $(?D)
|
|---|
| 8550 | @vindex ?D @r{(automatic variable)}
|
|---|
| 8551 | @item $(?D)
|
|---|
| 8552 | @vindex $(?F)
|
|---|
| 8553 | @vindex ?F @r{(automatic variable)}
|
|---|
| 8554 | @itemx $(?F)
|
|---|
| 8555 | Lists of the directory parts and the file-within-directory parts of
|
|---|
| 8556 | all prerequisites that are newer than the target.
|
|---|
| 8557 | @end table
|
|---|
| 8558 |
|
|---|
| 8559 | Note that we use a special stylistic convention when we talk about these
|
|---|
| 8560 | automatic variables; we write ``the value of @samp{$<}'', rather than
|
|---|
| 8561 | @w{``the variable @code{<}''} as we would write for ordinary variables
|
|---|
| 8562 | such as @code{objects} and @code{CFLAGS}. We think this convention
|
|---|
| 8563 | looks more natural in this special case. Please do not assume it has a
|
|---|
| 8564 | deep significance; @samp{$<} refers to the variable named @code{<} just
|
|---|
| 8565 | as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
|
|---|
| 8566 | You could just as well use @samp{$(<)} in place of @samp{$<}.
|
|---|
| 8567 |
|
|---|
| 8568 | @vindex $$@@
|
|---|
| 8569 | @vindex $$(@@D)
|
|---|
| 8570 | @vindex $$(@@F)
|
|---|
| 8571 | @cindex $$@@, support for
|
|---|
| 8572 | GNU @code{make} provides support for the SysV @code{make} feature that
|
|---|
| 8573 | allows special variable references @code{$$@@}, @code{$$(@@D)}, and
|
|---|
| 8574 | @code{$$(@@F)} (note the required double-''$''!) to appear with the
|
|---|
| 8575 | @emph{prerequisites list} (normal automatic variables are available
|
|---|
| 8576 | only within a command script). When appearing in a prerequisites
|
|---|
| 8577 | list, these variables are expanded to the name of the target, the
|
|---|
| 8578 | directory component of the target, and the file component of the
|
|---|
| 8579 | target, respectively.
|
|---|
| 8580 |
|
|---|
| 8581 | Note that these variables are available only within explicit and
|
|---|
| 8582 | static pattern (@pxref{Static Pattern, ,Static Pattern Rules}) rules;
|
|---|
| 8583 | they have no special significance within implicit (suffix or pattern)
|
|---|
| 8584 | rules. Also note that while SysV @code{make} actually expands its
|
|---|
| 8585 | entire prerequisite list @emph{twice}, GNU @code{make} does not behave
|
|---|
| 8586 | this way: instead it simply expands these special variables without
|
|---|
| 8587 | re-expanding any other part of the prerequisites list.
|
|---|
| 8588 |
|
|---|
| 8589 | This somewhat bizarre feature is included only to provide some
|
|---|
| 8590 | compatibility with SysV makefiles. In a native GNU @code{make} file
|
|---|
| 8591 | there are other ways to accomplish the same results. This feature is
|
|---|
| 8592 | disabled if the special pseudo target @code{.POSIX} is defined.
|
|---|
| 8593 |
|
|---|
| 8594 | @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
|
|---|
| 8595 | @subsection How Patterns Match
|
|---|
| 8596 |
|
|---|
| 8597 | @cindex stem
|
|---|
| 8598 | A target pattern is composed of a @samp{%} between a prefix and a suffix,
|
|---|
| 8599 | either or both of which may be empty. The pattern matches a file name only
|
|---|
| 8600 | if the file name starts with the prefix and ends with the suffix, without
|
|---|
| 8601 | overlap. The text between the prefix and the suffix is called the
|
|---|
| 8602 | @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
|
|---|
| 8603 | @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
|
|---|
| 8604 | turned into actual file names by substituting the stem for the character
|
|---|
| 8605 | @samp{%}. Thus, if in the same example one of the prerequisites is written
|
|---|
| 8606 | as @samp{%.c}, it expands to @samp{test.c}.@refill
|
|---|
| 8607 |
|
|---|
| 8608 | When the target pattern does not contain a slash (and it usually does
|
|---|
| 8609 | not), directory names in the file names are removed from the file name
|
|---|
| 8610 | before it is compared with the target prefix and suffix. After the
|
|---|
| 8611 | comparison of the file name to the target pattern, the directory
|
|---|
| 8612 | names, along with the slash that ends them, are added on to the
|
|---|
| 8613 | prerequisite file names generated from the pattern rule's prerequisite
|
|---|
| 8614 | patterns and the file name. The directories are ignored only for the
|
|---|
| 8615 | purpose of finding an implicit rule to use, not in the application of
|
|---|
| 8616 | that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
|
|---|
| 8617 | with @samp{src/a} as the stem. When prerequisites are turned into file
|
|---|
| 8618 | names, the directories from the stem are added at the front, while the
|
|---|
| 8619 | rest of the stem is substituted for the @samp{%}. The stem
|
|---|
| 8620 | @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
|
|---|
| 8621 | @file{src/car}.@refill
|
|---|
| 8622 |
|
|---|
| 8623 | @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
|
|---|
| 8624 | @subsection Match-Anything Pattern Rules
|
|---|
| 8625 |
|
|---|
| 8626 | @cindex match-anything rule
|
|---|
| 8627 | @cindex terminal rule
|
|---|
| 8628 | When a pattern rule's target is just @samp{%}, it matches any file name
|
|---|
| 8629 | whatever. We call these rules @dfn{match-anything} rules. They are very
|
|---|
| 8630 | useful, but it can take a lot of time for @code{make} to think about them,
|
|---|
| 8631 | because it must consider every such rule for each file name listed either
|
|---|
| 8632 | as a target or as a prerequisite.
|
|---|
| 8633 |
|
|---|
| 8634 | Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
|
|---|
| 8635 | would have to consider making it by linking an object file @file{foo.c.o},
|
|---|
| 8636 | or by C compilation-and-linking in one step from @file{foo.c.c}, or by
|
|---|
| 8637 | Pascal compilation-and-linking from @file{foo.c.p}, and many other
|
|---|
| 8638 | possibilities.
|
|---|
| 8639 |
|
|---|
| 8640 | We know these possibilities are ridiculous since @file{foo.c} is a C source
|
|---|
| 8641 | file, not an executable. If @code{make} did consider these possibilities,
|
|---|
| 8642 | it would ultimately reject them, because files such as @file{foo.c.o} and
|
|---|
| 8643 | @file{foo.c.p} would not exist. But these possibilities are so
|
|---|
| 8644 | numerous that @code{make} would run very slowly if it had to consider
|
|---|
| 8645 | them.@refill
|
|---|
| 8646 |
|
|---|
| 8647 | To gain speed, we have put various constraints on the way @code{make}
|
|---|
| 8648 | considers match-anything rules. There are two different constraints that
|
|---|
| 8649 | can be applied, and each time you define a match-anything rule you must
|
|---|
| 8650 | choose one or the other for that rule.
|
|---|
| 8651 |
|
|---|
| 8652 | One choice is to mark the match-anything rule as @dfn{terminal} by defining
|
|---|
| 8653 | it with a double colon. When a rule is terminal, it does not apply unless
|
|---|
| 8654 | its prerequisites actually exist. Prerequisites that could be made with
|
|---|
| 8655 | other implicit rules are not good enough. In other words, no further
|
|---|
| 8656 | chaining is allowed beyond a terminal rule.
|
|---|
| 8657 |
|
|---|
| 8658 | For example, the built-in implicit rules for extracting sources from RCS
|
|---|
| 8659 | and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
|
|---|
| 8660 | not exist, @code{make} will not even consider trying to make it as an
|
|---|
| 8661 | intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
|
|---|
| 8662 | RCS and SCCS files are generally ultimate source files, which should not be
|
|---|
| 8663 | remade from any other files; therefore, @code{make} can save time by not
|
|---|
| 8664 | looking for ways to remake them.@refill
|
|---|
| 8665 |
|
|---|
| 8666 | If you do not mark the match-anything rule as terminal, then it is
|
|---|
| 8667 | nonterminal. A nonterminal match-anything rule cannot apply to a file name
|
|---|
| 8668 | that indicates a specific type of data. A file name indicates a specific
|
|---|
| 8669 | type of data if some non-match-anything implicit rule target matches it.
|
|---|
| 8670 |
|
|---|
| 8671 | For example, the file name @file{foo.c} matches the target for the pattern
|
|---|
| 8672 | rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
|
|---|
| 8673 | rule is actually applicable (which happens only if there is a file
|
|---|
| 8674 | @file{foo.y}), the fact that its target matches is enough to prevent
|
|---|
| 8675 | consideration of any nonterminal match-anything rules for the file
|
|---|
| 8676 | @file{foo.c}. Thus, @code{make} will not even consider trying to make
|
|---|
| 8677 | @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
|
|---|
| 8678 | @file{foo.c.p}, etc.@refill
|
|---|
| 8679 |
|
|---|
| 8680 | The motivation for this constraint is that nonterminal match-anything
|
|---|
| 8681 | rules are used for making files containing specific types of data (such as
|
|---|
| 8682 | executable files) and a file name with a recognized suffix indicates some
|
|---|
| 8683 | other specific type of data (such as a C source file).
|
|---|
| 8684 |
|
|---|
| 8685 | Special built-in dummy pattern rules are provided solely to recognize
|
|---|
| 8686 | certain file names so that nonterminal match-anything rules will not be
|
|---|
| 8687 | considered. These dummy rules have no prerequisites and no commands, and
|
|---|
| 8688 | they are ignored for all other purposes. For example, the built-in
|
|---|
| 8689 | implicit rule
|
|---|
| 8690 |
|
|---|
| 8691 | @example
|
|---|
| 8692 | %.p :
|
|---|
| 8693 | @end example
|
|---|
| 8694 |
|
|---|
| 8695 | @noindent
|
|---|
| 8696 | exists to make sure that Pascal source files such as @file{foo.p} match a
|
|---|
| 8697 | specific target pattern and thereby prevent time from being wasted looking
|
|---|
| 8698 | for @file{foo.p.o} or @file{foo.p.c}.
|
|---|
| 8699 |
|
|---|
| 8700 | Dummy pattern rules such as the one for @samp{%.p} are made for every
|
|---|
| 8701 | suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
|
|---|
| 8702 |
|
|---|
| 8703 | @node Canceling Rules, , Match-Anything Rules, Pattern Rules
|
|---|
| 8704 | @subsection Canceling Implicit Rules
|
|---|
| 8705 |
|
|---|
| 8706 | You can override a built-in implicit rule (or one you have defined
|
|---|
| 8707 | yourself) by defining a new pattern rule with the same target and
|
|---|
| 8708 | prerequisites, but different commands. When the new rule is defined, the
|
|---|
| 8709 | built-in one is replaced. The new rule's position in the sequence of
|
|---|
| 8710 | implicit rules is determined by where you write the new rule.
|
|---|
| 8711 |
|
|---|
| 8712 | You can cancel a built-in implicit rule by defining a pattern rule with the
|
|---|
| 8713 | same target and prerequisites, but no commands. For example, the following
|
|---|
| 8714 | would cancel the rule that runs the assembler:
|
|---|
| 8715 |
|
|---|
| 8716 | @example
|
|---|
| 8717 | %.o : %.s
|
|---|
| 8718 | @end example
|
|---|
| 8719 |
|
|---|
| 8720 | @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
|
|---|
| 8721 | @section Defining Last-Resort Default Rules
|
|---|
| 8722 | @cindex last-resort default rules
|
|---|
| 8723 | @cindex default rules, last-resort
|
|---|
| 8724 |
|
|---|
| 8725 | You can define a last-resort implicit rule by writing a terminal
|
|---|
| 8726 | match-anything pattern rule with no prerequisites (@pxref{Match-Anything
|
|---|
| 8727 | Rules}). This is just like any other pattern rule; the only thing
|
|---|
| 8728 | special about it is that it will match any target. So such a rule's
|
|---|
| 8729 | commands are used for all targets and prerequisites that have no commands
|
|---|
| 8730 | of their own and for which no other implicit rule applies.
|
|---|
| 8731 |
|
|---|
| 8732 | For example, when testing a makefile, you might not care if the source
|
|---|
| 8733 | files contain real data, only that they exist. Then you might do this:
|
|---|
| 8734 |
|
|---|
| 8735 | @example
|
|---|
| 8736 | %::
|
|---|
| 8737 | touch $@@
|
|---|
| 8738 | @end example
|
|---|
| 8739 |
|
|---|
| 8740 | @noindent
|
|---|
| 8741 | to cause all the source files needed (as prerequisites) to be created
|
|---|
| 8742 | automatically.
|
|---|
| 8743 |
|
|---|
| 8744 | @findex .DEFAULT
|
|---|
| 8745 | You can instead define commands to be used for targets for which there
|
|---|
| 8746 | are no rules at all, even ones which don't specify commands. You do
|
|---|
| 8747 | this by writing a rule for the target @code{.DEFAULT}. Such a rule's
|
|---|
| 8748 | commands are used for all prerequisites which do not appear as targets in
|
|---|
| 8749 | any explicit rule, and for which no implicit rule applies. Naturally,
|
|---|
| 8750 | there is no @code{.DEFAULT} rule unless you write one.
|
|---|
| 8751 |
|
|---|
| 8752 | If you use @code{.DEFAULT} with no commands or prerequisites:
|
|---|
| 8753 |
|
|---|
| 8754 | @example
|
|---|
| 8755 | .DEFAULT:
|
|---|
| 8756 | @end example
|
|---|
| 8757 |
|
|---|
| 8758 | @noindent
|
|---|
| 8759 | the commands previously stored for @code{.DEFAULT} are cleared.
|
|---|
| 8760 | Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
|
|---|
| 8761 |
|
|---|
| 8762 | If you do not want a target to get the commands from a match-anything
|
|---|
| 8763 | pattern rule or @code{.DEFAULT}, but you also do not want any commands
|
|---|
| 8764 | to be run for the target, you can give it empty commands (@pxref{Empty
|
|---|
| 8765 | Commands, ,Defining Empty Commands}).@refill
|
|---|
| 8766 |
|
|---|
| 8767 | You can use a last-resort rule to override part of another makefile.
|
|---|
| 8768 | @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
|
|---|
| 8769 |
|
|---|
| 8770 | @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
|
|---|
| 8771 | @section Old-Fashioned Suffix Rules
|
|---|
| 8772 | @cindex old-fashioned suffix rules
|
|---|
| 8773 | @cindex suffix rule
|
|---|
| 8774 |
|
|---|
| 8775 | @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
|
|---|
| 8776 | @code{make}. Suffix rules are obsolete because pattern rules are more
|
|---|
| 8777 | general and clearer. They are supported in GNU @code{make} for
|
|---|
| 8778 | compatibility with old makefiles. They come in two kinds:
|
|---|
| 8779 | @dfn{double-suffix} and @dfn{single-suffix}.@refill
|
|---|
| 8780 |
|
|---|
| 8781 | A double-suffix rule is defined by a pair of suffixes: the target suffix
|
|---|
| 8782 | and the source suffix. It matches any file whose name ends with the
|
|---|
| 8783 | target suffix. The corresponding implicit prerequisite is made by
|
|---|
| 8784 | replacing the target suffix with the source suffix in the file name. A
|
|---|
| 8785 | two-suffix rule whose target and source suffixes are @samp{.o} and
|
|---|
| 8786 | @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
|
|---|
| 8787 |
|
|---|
| 8788 | A single-suffix rule is defined by a single suffix, which is the source
|
|---|
| 8789 | suffix. It matches any file name, and the corresponding implicit
|
|---|
| 8790 | prerequisite name is made by appending the source suffix. A single-suffix
|
|---|
| 8791 | rule whose source suffix is @samp{.c} is equivalent to the pattern rule
|
|---|
| 8792 | @samp{% : %.c}.
|
|---|
| 8793 |
|
|---|
| 8794 | Suffix rule definitions are recognized by comparing each rule's target
|
|---|
| 8795 | against a defined list of known suffixes. When @code{make} sees a rule
|
|---|
| 8796 | whose target is a known suffix, this rule is considered a single-suffix
|
|---|
| 8797 | rule. When @code{make} sees a rule whose target is two known suffixes
|
|---|
| 8798 | concatenated, this rule is taken as a double-suffix rule.
|
|---|
| 8799 |
|
|---|
| 8800 | For example, @samp{.c} and @samp{.o} are both on the default list of
|
|---|
| 8801 | known suffixes. Therefore, if you define a rule whose target is
|
|---|
| 8802 | @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
|
|---|
| 8803 | suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
|
|---|
| 8804 | way to define the rule for compiling a C source file:@refill
|
|---|
| 8805 |
|
|---|
| 8806 | @example
|
|---|
| 8807 | .c.o:
|
|---|
| 8808 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 8809 | @end example
|
|---|
| 8810 |
|
|---|
| 8811 | Suffix rules cannot have any prerequisites of their own. If they have any,
|
|---|
| 8812 | they are treated as normal files with funny names, not as suffix rules.
|
|---|
| 8813 | Thus, the rule:
|
|---|
| 8814 |
|
|---|
| 8815 | @example
|
|---|
| 8816 | .c.o: foo.h
|
|---|
| 8817 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 8818 | @end example
|
|---|
| 8819 |
|
|---|
| 8820 | @noindent
|
|---|
| 8821 | tells how to make the file @file{.c.o} from the prerequisite file
|
|---|
| 8822 | @file{foo.h}, and is not at all like the pattern rule:
|
|---|
| 8823 |
|
|---|
| 8824 | @example
|
|---|
| 8825 | %.o: %.c foo.h
|
|---|
| 8826 | $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
|
|---|
| 8827 | @end example
|
|---|
| 8828 |
|
|---|
| 8829 | @noindent
|
|---|
| 8830 | which tells how to make @samp{.o} files from @samp{.c} files, and makes all
|
|---|
| 8831 | @samp{.o} files using this pattern rule also depend on @file{foo.h}.
|
|---|
| 8832 |
|
|---|
| 8833 | Suffix rules with no commands are also meaningless. They do not remove
|
|---|
| 8834 | previous rules as do pattern rules with no commands (@pxref{Canceling
|
|---|
| 8835 | Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
|
|---|
| 8836 | a target in the data base.@refill
|
|---|
| 8837 |
|
|---|
| 8838 | @findex .SUFFIXES
|
|---|
| 8839 | The known suffixes are simply the names of the prerequisites of the special
|
|---|
| 8840 | target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
|
|---|
| 8841 | for @code{.SUFFIXES} that adds more prerequisites, as in:
|
|---|
| 8842 |
|
|---|
| 8843 | @example
|
|---|
| 8844 | .SUFFIXES: .hack .win
|
|---|
| 8845 | @end example
|
|---|
| 8846 |
|
|---|
| 8847 | @noindent
|
|---|
| 8848 | which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
|
|---|
| 8849 |
|
|---|
| 8850 | If you wish to eliminate the default known suffixes instead of just adding
|
|---|
| 8851 | to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
|
|---|
| 8852 | special dispensation, this eliminates all existing prerequisites of
|
|---|
| 8853 | @code{.SUFFIXES}. You can then write another rule to add the suffixes you
|
|---|
| 8854 | want. For example,
|
|---|
| 8855 |
|
|---|
| 8856 | @example
|
|---|
| 8857 | @group
|
|---|
| 8858 | .SUFFIXES: # @r{Delete the default suffixes}
|
|---|
| 8859 | .SUFFIXES: .c .o .h # @r{Define our suffix list}
|
|---|
| 8860 | @end group
|
|---|
| 8861 | @end example
|
|---|
| 8862 |
|
|---|
| 8863 | The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
|
|---|
| 8864 | list of suffixes to be empty.
|
|---|
| 8865 |
|
|---|
| 8866 | @vindex SUFFIXES
|
|---|
| 8867 | The variable @code{SUFFIXES} is defined to the default list of suffixes
|
|---|
| 8868 | before @code{make} reads any makefiles. You can change the list of suffixes
|
|---|
| 8869 | with a rule for the special target @code{.SUFFIXES}, but that does not alter
|
|---|
| 8870 | this variable.
|
|---|
| 8871 |
|
|---|
| 8872 | @node Implicit Rule Search, , Suffix Rules, Implicit Rules
|
|---|
| 8873 | @section Implicit Rule Search Algorithm
|
|---|
| 8874 | @cindex implicit rule, search algorithm
|
|---|
| 8875 | @cindex search algorithm, implicit rule
|
|---|
| 8876 |
|
|---|
| 8877 | Here is the procedure @code{make} uses for searching for an implicit rule
|
|---|
| 8878 | for a target @var{t}. This procedure is followed for each double-colon
|
|---|
| 8879 | rule with no commands, for each target of ordinary rules none of which have
|
|---|
| 8880 | commands, and for each prerequisite that is not the target of any rule. It
|
|---|
| 8881 | is also followed recursively for prerequisites that come from implicit
|
|---|
| 8882 | rules, in the search for a chain of rules.
|
|---|
| 8883 |
|
|---|
| 8884 | Suffix rules are not mentioned in this algorithm because suffix rules are
|
|---|
| 8885 | converted to equivalent pattern rules once the makefiles have been read in.
|
|---|
| 8886 |
|
|---|
| 8887 | For an archive member target of the form
|
|---|
| 8888 | @samp{@var{archive}(@var{member})}, the following algorithm is run
|
|---|
| 8889 | twice, first using the entire target name @var{t}, and second using
|
|---|
| 8890 | @samp{(@var{member})} as the target @var{t} if the first run found no
|
|---|
| 8891 | rule.@refill
|
|---|
| 8892 |
|
|---|
| 8893 | @enumerate
|
|---|
| 8894 | @item
|
|---|
| 8895 | Split @var{t} into a directory part, called @var{d}, and the rest,
|
|---|
| 8896 | called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
|
|---|
| 8897 | @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
|
|---|
| 8898 |
|
|---|
| 8899 | @item
|
|---|
| 8900 | Make a list of all the pattern rules one of whose targets matches
|
|---|
| 8901 | @var{t} or @var{n}. If the target pattern contains a slash, it is
|
|---|
| 8902 | matched against @var{t}; otherwise, against @var{n}.
|
|---|
| 8903 |
|
|---|
| 8904 | @item
|
|---|
| 8905 | If any rule in that list is @emph{not} a match-anything rule, then
|
|---|
| 8906 | remove all nonterminal match-anything rules from the list.
|
|---|
| 8907 |
|
|---|
| 8908 | @item
|
|---|
| 8909 | Remove from the list all rules with no commands.
|
|---|
| 8910 |
|
|---|
| 8911 | @item
|
|---|
| 8912 | For each pattern rule in the list:
|
|---|
| 8913 |
|
|---|
| 8914 | @enumerate a
|
|---|
| 8915 | @item
|
|---|
| 8916 | Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
|
|---|
| 8917 | matched by the @samp{%} in the target pattern.@refill
|
|---|
| 8918 |
|
|---|
| 8919 | @item
|
|---|
| 8920 | Compute the prerequisite names by substituting @var{s} for @samp{%}; if
|
|---|
| 8921 | the target pattern does not contain a slash, append @var{d} to
|
|---|
| 8922 | the front of each prerequisite name.@refill
|
|---|
| 8923 |
|
|---|
| 8924 | @item
|
|---|
| 8925 | Test whether all the prerequisites exist or ought to exist. (If a
|
|---|
| 8926 | file name is mentioned in the makefile as a target or as an explicit
|
|---|
| 8927 | prerequisite, then we say it ought to exist.)
|
|---|
| 8928 |
|
|---|
| 8929 | If all prerequisites exist or ought to exist, or there are no prerequisites,
|
|---|
| 8930 | then this rule applies.
|
|---|
| 8931 | @end enumerate
|
|---|
| 8932 |
|
|---|
| 8933 | @item
|
|---|
| 8934 | If no pattern rule has been found so far, try harder.
|
|---|
| 8935 | For each pattern rule in the list:
|
|---|
| 8936 |
|
|---|
| 8937 | @enumerate a
|
|---|
| 8938 | @item
|
|---|
| 8939 | If the rule is terminal, ignore it and go on to the next rule.
|
|---|
| 8940 |
|
|---|
| 8941 | @item
|
|---|
| 8942 | Compute the prerequisite names as before.
|
|---|
| 8943 |
|
|---|
| 8944 | @item
|
|---|
| 8945 | Test whether all the prerequisites exist or ought to exist.
|
|---|
| 8946 |
|
|---|
| 8947 | @item
|
|---|
| 8948 | For each prerequisite that does not exist, follow this algorithm
|
|---|
| 8949 | recursively to see if the prerequisite can be made by an implicit
|
|---|
| 8950 | rule.
|
|---|
| 8951 |
|
|---|
| 8952 | @item
|
|---|
| 8953 | If all prerequisites exist, ought to exist, or can be
|
|---|
| 8954 | made by implicit rules, then this rule applies.
|
|---|
| 8955 | @end enumerate
|
|---|
| 8956 |
|
|---|
| 8957 | @item
|
|---|
| 8958 | If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
|
|---|
| 8959 | applies. In that case, give @var{t} the same commands that
|
|---|
| 8960 | @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
|
|---|
| 8961 | @end enumerate
|
|---|
| 8962 |
|
|---|
| 8963 | Once a rule that applies has been found, for each target pattern of the
|
|---|
| 8964 | rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
|
|---|
| 8965 | the pattern is replaced with @var{s} and the resultant file name is stored
|
|---|
| 8966 | until the commands to remake the target file @var{t} are executed. After
|
|---|
| 8967 | these commands are executed, each of these stored file names are entered
|
|---|
| 8968 | into the data base and marked as having been updated and having the same
|
|---|
| 8969 | update status as the file @var{t}.
|
|---|
| 8970 |
|
|---|
| 8971 | When the commands of a pattern rule are executed for @var{t}, the automatic
|
|---|
| 8972 | variables are set corresponding to the target and prerequisites.
|
|---|
| 8973 | @xref{Automatic Variables}.
|
|---|
| 8974 |
|
|---|
| 8975 | @node Archives, Features, Implicit Rules, Top
|
|---|
| 8976 | @chapter Using @code{make} to Update Archive Files
|
|---|
| 8977 | @cindex archive
|
|---|
| 8978 |
|
|---|
| 8979 | @dfn{Archive files} are files containing named subfiles called
|
|---|
| 8980 | @dfn{members}; they are maintained with the program @code{ar} and their
|
|---|
| 8981 | main use is as subroutine libraries for linking.
|
|---|
| 8982 |
|
|---|
| 8983 | @menu
|
|---|
| 8984 | * Archive Members:: Archive members as targets.
|
|---|
| 8985 | * Archive Update:: The implicit rule for archive member targets.
|
|---|
| 8986 | * Archive Pitfalls:: Dangers to watch out for when using archives.
|
|---|
| 8987 | * Archive Suffix Rules:: You can write a special kind of suffix rule
|
|---|
| 8988 | for updating archives.
|
|---|
| 8989 | @end menu
|
|---|
| 8990 |
|
|---|
| 8991 | @node Archive Members, Archive Update, Archives, Archives
|
|---|
| 8992 | @section Archive Members as Targets
|
|---|
| 8993 | @cindex archive member targets
|
|---|
| 8994 |
|
|---|
| 8995 | An individual member of an archive file can be used as a target or
|
|---|
| 8996 | prerequisite in @code{make}. You specify the member named @var{member} in
|
|---|
| 8997 | archive file @var{archive} as follows:
|
|---|
| 8998 |
|
|---|
| 8999 | @example
|
|---|
| 9000 | @var{archive}(@var{member})
|
|---|
| 9001 | @end example
|
|---|
| 9002 |
|
|---|
| 9003 | @noindent
|
|---|
| 9004 | This construct is available only in targets and prerequisites, not in
|
|---|
| 9005 | commands! Most programs that you might use in commands do not support this
|
|---|
| 9006 | syntax and cannot act directly on archive members. Only @code{ar} and
|
|---|
| 9007 | other programs specifically designed to operate on archives can do so.
|
|---|
| 9008 | Therefore, valid commands to update an archive member target probably must
|
|---|
| 9009 | use @code{ar}. For example, this rule says to create a member
|
|---|
| 9010 | @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
|
|---|
| 9011 |
|
|---|
| 9012 | @example
|
|---|
| 9013 | foolib(hack.o) : hack.o
|
|---|
| 9014 | ar cr foolib hack.o
|
|---|
| 9015 | @end example
|
|---|
| 9016 |
|
|---|
| 9017 | In fact, nearly all archive member targets are updated in just this way
|
|---|
| 9018 | and there is an implicit rule to do it for you. @strong{Note:} The
|
|---|
| 9019 | @samp{c} flag to @code{ar} is required if the archive file does not
|
|---|
| 9020 | already exist.
|
|---|
| 9021 |
|
|---|
| 9022 | To specify several members in the same archive, you can write all the
|
|---|
| 9023 | member names together between the parentheses. For example:
|
|---|
| 9024 |
|
|---|
| 9025 | @example
|
|---|
| 9026 | foolib(hack.o kludge.o)
|
|---|
| 9027 | @end example
|
|---|
| 9028 |
|
|---|
| 9029 | @noindent
|
|---|
| 9030 | is equivalent to:
|
|---|
| 9031 |
|
|---|
| 9032 | @example
|
|---|
| 9033 | foolib(hack.o) foolib(kludge.o)
|
|---|
| 9034 | @end example
|
|---|
| 9035 |
|
|---|
| 9036 | @cindex wildcard, in archive member
|
|---|
| 9037 | You can also use shell-style wildcards in an archive member reference.
|
|---|
| 9038 | @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
|
|---|
| 9039 | example, @w{@samp{foolib(*.o)}} expands to all existing members of the
|
|---|
| 9040 | @file{foolib} archive whose names end in @samp{.o}; perhaps
|
|---|
| 9041 | @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
|
|---|
| 9042 |
|
|---|
| 9043 | @node Archive Update, Archive Pitfalls, Archive Members, Archives
|
|---|
| 9044 | @section Implicit Rule for Archive Member Targets
|
|---|
| 9045 |
|
|---|
| 9046 | Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
|
|---|
| 9047 | member named @var{m} in the archive file @var{a}.
|
|---|
| 9048 |
|
|---|
| 9049 | When @code{make} looks for an implicit rule for such a target, as a special
|
|---|
| 9050 | feature it considers implicit rules that match @file{(@var{m})}, as well as
|
|---|
| 9051 | those that match the actual target @file{@var{a}(@var{m})}.
|
|---|
| 9052 |
|
|---|
| 9053 | This causes one special rule whose target is @file{(%)} to match. This
|
|---|
| 9054 | rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
|
|---|
| 9055 | into the archive. For example, it will update the archive member target
|
|---|
| 9056 | @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
|
|---|
| 9057 | archive @file{foo.a} as a @emph{member} named @file{bar.o}.
|
|---|
| 9058 |
|
|---|
| 9059 | When this rule is chained with others, the result is very powerful.
|
|---|
| 9060 | Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
|
|---|
| 9061 | @samp{(} and @samp{)} from being interpreted specially by the shell) in
|
|---|
| 9062 | the presence of a file @file{bar.c} is enough to cause the following
|
|---|
| 9063 | commands to be run, even without a makefile:
|
|---|
| 9064 |
|
|---|
| 9065 | @example
|
|---|
| 9066 | cc -c bar.c -o bar.o
|
|---|
| 9067 | ar r foo.a bar.o
|
|---|
| 9068 | rm -f bar.o
|
|---|
| 9069 | @end example
|
|---|
| 9070 |
|
|---|
| 9071 | @noindent
|
|---|
| 9072 | Here @code{make} has envisioned the file @file{bar.o} as an intermediate
|
|---|
| 9073 | file. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9074 |
|
|---|
| 9075 | Implicit rules such as this one are written using the automatic variable
|
|---|
| 9076 | @samp{$%}. @xref{Automatic Variables}.
|
|---|
| 9077 |
|
|---|
| 9078 | An archive member name in an archive cannot contain a directory name, but
|
|---|
| 9079 | it may be useful in a makefile to pretend that it does. If you write an
|
|---|
| 9080 | archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
|
|---|
| 9081 | automatic updating with this command:
|
|---|
| 9082 |
|
|---|
| 9083 | @example
|
|---|
| 9084 | ar r foo.a dir/file.o
|
|---|
| 9085 | @end example
|
|---|
| 9086 |
|
|---|
| 9087 | @noindent
|
|---|
| 9088 | which has the effect of copying the file @file{dir/file.o} into a member
|
|---|
| 9089 | named @file{file.o}. In connection with such usage, the automatic variables
|
|---|
| 9090 | @code{%D} and @code{%F} may be useful.
|
|---|
| 9091 |
|
|---|
| 9092 | @menu
|
|---|
| 9093 | * Archive Symbols:: How to update archive symbol directories.
|
|---|
| 9094 | @end menu
|
|---|
| 9095 |
|
|---|
| 9096 | @node Archive Symbols, , Archive Update, Archive Update
|
|---|
| 9097 | @subsection Updating Archive Symbol Directories
|
|---|
| 9098 | @cindex @code{__.SYMDEF}
|
|---|
| 9099 | @cindex updating archive symbol directories
|
|---|
| 9100 | @cindex archive symbol directory updating
|
|---|
| 9101 | @cindex symbol directories, updating archive
|
|---|
| 9102 | @cindex directories, updating archive symbol
|
|---|
| 9103 |
|
|---|
| 9104 | An archive file that is used as a library usually contains a special member
|
|---|
| 9105 | named @file{__.SYMDEF} that contains a directory of the external symbol
|
|---|
| 9106 | names defined by all the other members. After you update any other
|
|---|
| 9107 | members, you need to update @file{__.SYMDEF} so that it will summarize the
|
|---|
| 9108 | other members properly. This is done by running the @code{ranlib} program:
|
|---|
| 9109 |
|
|---|
| 9110 | @example
|
|---|
| 9111 | ranlib @var{archivefile}
|
|---|
| 9112 | @end example
|
|---|
| 9113 |
|
|---|
| 9114 | Normally you would put this command in the rule for the archive file,
|
|---|
| 9115 | and make all the members of the archive file prerequisites of that rule.
|
|---|
| 9116 | For example,
|
|---|
| 9117 |
|
|---|
| 9118 | @example
|
|---|
| 9119 | libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
|
|---|
| 9120 | ranlib libfoo.a
|
|---|
| 9121 | @end example
|
|---|
| 9122 |
|
|---|
| 9123 | @noindent
|
|---|
| 9124 | The effect of this is to update archive members @file{x.o}, @file{y.o},
|
|---|
| 9125 | etc., and then update the symbol directory member @file{__.SYMDEF} by
|
|---|
| 9126 | running @code{ranlib}. The rules for updating the members are not shown
|
|---|
| 9127 | here; most likely you can omit them and use the implicit rule which copies
|
|---|
| 9128 | files into the archive, as described in the preceding section.
|
|---|
| 9129 |
|
|---|
| 9130 | This is not necessary when using the GNU @code{ar} program, which
|
|---|
| 9131 | updates the @file{__.SYMDEF} member automatically.
|
|---|
| 9132 |
|
|---|
| 9133 | @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
|
|---|
| 9134 | @section Dangers When Using Archives
|
|---|
| 9135 | @cindex archive, and parallel execution
|
|---|
| 9136 | @cindex parallel execution, and archive update
|
|---|
| 9137 | @cindex archive, and @code{-j}
|
|---|
| 9138 | @cindex @code{-j}, and archive update
|
|---|
| 9139 |
|
|---|
| 9140 | It is important to be careful when using parallel execution (the
|
|---|
| 9141 | @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
|
|---|
| 9142 | If multiple @code{ar} commands run at the same time on the same archive
|
|---|
| 9143 | file, they will not know about each other and can corrupt the file.
|
|---|
| 9144 |
|
|---|
| 9145 | Possibly a future version of @code{make} will provide a mechanism to
|
|---|
| 9146 | circumvent this problem by serializing all commands that operate on the
|
|---|
| 9147 | same archive file. But for the time being, you must either write your
|
|---|
| 9148 | makefiles to avoid this problem in some other way, or not use @code{-j}.
|
|---|
| 9149 |
|
|---|
| 9150 | @node Archive Suffix Rules, , Archive Pitfalls, Archives
|
|---|
| 9151 | @section Suffix Rules for Archive Files
|
|---|
| 9152 | @cindex suffix rule, for archive
|
|---|
| 9153 | @cindex archive, suffix rule for
|
|---|
| 9154 | @cindex library archive, suffix rule for
|
|---|
| 9155 | @cindex @code{.a} (archives)
|
|---|
| 9156 |
|
|---|
| 9157 | You can write a special kind of suffix rule for dealing with archive
|
|---|
| 9158 | files. @xref{Suffix Rules}, for a full explanation of suffix rules.
|
|---|
| 9159 | Archive suffix rules are obsolete in GNU @code{make}, because pattern
|
|---|
| 9160 | rules for archives are a more general mechanism (@pxref{Archive
|
|---|
| 9161 | Update}). But they are retained for compatibility with other
|
|---|
| 9162 | @code{make}s.
|
|---|
| 9163 |
|
|---|
| 9164 | To write a suffix rule for archives, you simply write a suffix rule
|
|---|
| 9165 | using the target suffix @samp{.a} (the usual suffix for archive files).
|
|---|
| 9166 | For example, here is the old-fashioned suffix rule to update a library
|
|---|
| 9167 | archive from C source files:
|
|---|
| 9168 |
|
|---|
| 9169 | @example
|
|---|
| 9170 | @group
|
|---|
| 9171 | .c.a:
|
|---|
| 9172 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|---|
| 9173 | $(AR) r $@@ $*.o
|
|---|
| 9174 | $(RM) $*.o
|
|---|
| 9175 | @end group
|
|---|
| 9176 | @end example
|
|---|
| 9177 |
|
|---|
| 9178 | @noindent
|
|---|
| 9179 | This works just as if you had written the pattern rule:
|
|---|
| 9180 |
|
|---|
| 9181 | @example
|
|---|
| 9182 | @group
|
|---|
| 9183 | (%.o): %.c
|
|---|
| 9184 | $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
|
|---|
| 9185 | $(AR) r $@@ $*.o
|
|---|
| 9186 | $(RM) $*.o
|
|---|
| 9187 | @end group
|
|---|
| 9188 | @end example
|
|---|
| 9189 |
|
|---|
| 9190 | In fact, this is just what @code{make} does when it sees a suffix rule
|
|---|
| 9191 | with @samp{.a} as the target suffix. Any double-suffix rule
|
|---|
| 9192 | @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
|
|---|
| 9193 | pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
|
|---|
| 9194 |
|
|---|
| 9195 | Since you might want to use @samp{.a} as the suffix for some other kind
|
|---|
| 9196 | of file, @code{make} also converts archive suffix rules to pattern rules
|
|---|
| 9197 | in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
|
|---|
| 9198 | @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
|
|---|
| 9199 | @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
|
|---|
| 9200 |
|
|---|
| 9201 | @node Features, Missing, Archives, Top
|
|---|
| 9202 | @chapter Features of GNU @code{make}
|
|---|
| 9203 | @cindex features of GNU @code{make}
|
|---|
| 9204 | @cindex portability
|
|---|
| 9205 | @cindex compatibility
|
|---|
| 9206 |
|
|---|
| 9207 | Here is a summary of the features of GNU @code{make}, for comparison
|
|---|
| 9208 | with and credit to other versions of @code{make}. We consider the
|
|---|
| 9209 | features of @code{make} in 4.2 BSD systems as a baseline. If you are
|
|---|
| 9210 | concerned with writing portable makefiles, you should not use the
|
|---|
| 9211 | features of @code{make} listed here, nor the ones in @ref{Missing}.
|
|---|
| 9212 |
|
|---|
| 9213 | Many features come from the version of @code{make} in System V.
|
|---|
| 9214 |
|
|---|
| 9215 | @itemize @bullet
|
|---|
| 9216 | @item
|
|---|
| 9217 | The @code{VPATH} variable and its special meaning.
|
|---|
| 9218 | @xref{Directory Search, , Searching Directories for Prerequisites}.
|
|---|
| 9219 | This feature exists in System V @code{make}, but is undocumented.
|
|---|
| 9220 | It is documented in 4.3 BSD @code{make} (which says it mimics System V's
|
|---|
| 9221 | @code{VPATH} feature).@refill
|
|---|
| 9222 |
|
|---|
| 9223 | @item
|
|---|
| 9224 | Included makefiles. @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9225 | Allowing multiple files to be included with a single directive is a GNU
|
|---|
| 9226 | extension.
|
|---|
| 9227 |
|
|---|
| 9228 | @item
|
|---|
| 9229 | Variables are read from and communicated via the environment.
|
|---|
| 9230 | @xref{Environment, ,Variables from the Environment}.
|
|---|
| 9231 |
|
|---|
| 9232 | @item
|
|---|
| 9233 | Options passed through the variable @code{MAKEFLAGS} to recursive
|
|---|
| 9234 | invocations of @code{make}.
|
|---|
| 9235 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 9236 |
|
|---|
| 9237 | @item
|
|---|
| 9238 | The automatic variable @code{$%} is set to the member name
|
|---|
| 9239 | in an archive reference. @xref{Automatic Variables}.
|
|---|
| 9240 |
|
|---|
| 9241 | @item
|
|---|
| 9242 | The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
|
|---|
| 9243 | and @code{$?} have corresponding forms like @code{$(@@F)} and
|
|---|
| 9244 | @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
|
|---|
| 9245 | extension. @xref{Automatic Variables}.@refill
|
|---|
| 9246 |
|
|---|
| 9247 | @item
|
|---|
| 9248 | Substitution variable references.
|
|---|
| 9249 | @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9250 |
|
|---|
| 9251 | @item
|
|---|
| 9252 | The command-line options @samp{-b} and @samp{-m}, accepted and
|
|---|
| 9253 | ignored. In System V @code{make}, these options actually do something.
|
|---|
| 9254 |
|
|---|
| 9255 | @item
|
|---|
| 9256 | Execution of recursive commands to run @code{make} via the variable
|
|---|
| 9257 | @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
|
|---|
| 9258 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9259 |
|
|---|
| 9260 | @item
|
|---|
| 9261 | Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
|
|---|
| 9262 | Rules}. This feature is obsolete in GNU @code{make}, because the
|
|---|
| 9263 | general feature of rule chaining (@pxref{Chained Rules, ,Chains of
|
|---|
| 9264 | Implicit Rules}) allows one pattern rule for installing members in an
|
|---|
| 9265 | archive (@pxref{Archive Update}) to be sufficient.
|
|---|
| 9266 |
|
|---|
| 9267 | @item
|
|---|
| 9268 | The arrangement of lines and backslash-newline combinations in
|
|---|
| 9269 | commands is retained when the commands are printed, so they appear as
|
|---|
| 9270 | they do in the makefile, except for the stripping of initial
|
|---|
| 9271 | whitespace.
|
|---|
| 9272 | @end itemize
|
|---|
| 9273 |
|
|---|
| 9274 | The following features were inspired by various other versions of
|
|---|
| 9275 | @code{make}. In some cases it is unclear exactly which versions inspired
|
|---|
| 9276 | which others.
|
|---|
| 9277 |
|
|---|
| 9278 | @itemize @bullet
|
|---|
| 9279 | @item
|
|---|
| 9280 | Pattern rules using @samp{%}.
|
|---|
| 9281 | This has been implemented in several versions of @code{make}.
|
|---|
| 9282 | We're not sure who invented it first, but it's been spread around a bit.
|
|---|
| 9283 | @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
|
|---|
| 9284 |
|
|---|
| 9285 | @item
|
|---|
| 9286 | Rule chaining and implicit intermediate files.
|
|---|
| 9287 | This was implemented by Stu Feldman in his version of @code{make}
|
|---|
| 9288 | for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
|
|---|
| 9289 | AT&T Bell Labs in his @code{mk} program (where he terms it
|
|---|
| 9290 | ``transitive closure''). We do not really know if
|
|---|
| 9291 | we got this from either of them or thought it up ourselves at the
|
|---|
| 9292 | same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9293 |
|
|---|
| 9294 | @item
|
|---|
| 9295 | The automatic variable @code{$^} containing a list of all prerequisites
|
|---|
| 9296 | of the current target. We did not invent this, but we have no idea who
|
|---|
| 9297 | did. @xref{Automatic Variables}. The automatic variable
|
|---|
| 9298 | @code{$+} is a simple extension of @code{$^}.
|
|---|
| 9299 |
|
|---|
| 9300 | @item
|
|---|
| 9301 | The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
|
|---|
| 9302 | invented by Andrew Hume in @code{mk}.
|
|---|
| 9303 | @xref{Instead of Execution, ,Instead of Executing the Commands}.
|
|---|
| 9304 |
|
|---|
| 9305 | @item
|
|---|
| 9306 | The concept of doing several things at once (parallelism) exists in
|
|---|
| 9307 | many incarnations of @code{make} and similar programs, though not in the
|
|---|
| 9308 | System V or BSD implementations. @xref{Execution, ,Command Execution}.
|
|---|
| 9309 |
|
|---|
| 9310 | @item
|
|---|
| 9311 | Modified variable references using pattern substitution come from
|
|---|
| 9312 | SunOS 4. @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9313 | This functionality was provided in GNU @code{make} by the
|
|---|
| 9314 | @code{patsubst} function before the alternate syntax was implemented
|
|---|
| 9315 | for compatibility with SunOS 4. It is not altogether clear who
|
|---|
| 9316 | inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
|
|---|
| 9317 | 4 was released.@refill
|
|---|
| 9318 |
|
|---|
| 9319 | @item
|
|---|
| 9320 | The special significance of @samp{+} characters preceding command lines
|
|---|
| 9321 | (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
|
|---|
| 9322 | mandated by
|
|---|
| 9323 | @cite{IEEE Standard 1003.2-1992} (POSIX.2).
|
|---|
| 9324 |
|
|---|
| 9325 | @item
|
|---|
| 9326 | The @samp{+=} syntax to append to the value of a variable comes from SunOS
|
|---|
| 9327 | 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
|
|---|
| 9328 |
|
|---|
| 9329 | @item
|
|---|
| 9330 | The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
|
|---|
| 9331 | multiple members in a single archive file comes from SunOS 4 @code{make}.
|
|---|
| 9332 | @xref{Archive Members}.
|
|---|
| 9333 |
|
|---|
| 9334 | @item
|
|---|
| 9335 | The @code{-include} directive to include makefiles with no error for a
|
|---|
| 9336 | nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
|
|---|
| 9337 | @code{make} does not allow multiple makefiles to be specified in one
|
|---|
| 9338 | @code{-include} directive.) The same feature appears with the name
|
|---|
| 9339 | @code{sinclude} in SGI @code{make} and perhaps others.
|
|---|
| 9340 | @end itemize
|
|---|
| 9341 |
|
|---|
| 9342 | The remaining features are inventions new in GNU @code{make}:
|
|---|
| 9343 |
|
|---|
| 9344 | @itemize @bullet
|
|---|
| 9345 | @item
|
|---|
| 9346 | Use the @samp{-v} or @samp{--version} option to print version and
|
|---|
| 9347 | copyright information.
|
|---|
| 9348 |
|
|---|
| 9349 | @item
|
|---|
| 9350 | Use the @samp{-h} or @samp{--help} option to summarize the options to
|
|---|
| 9351 | @code{make}.
|
|---|
| 9352 |
|
|---|
| 9353 | @item
|
|---|
| 9354 | Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
|
|---|
| 9355 |
|
|---|
| 9356 | @item
|
|---|
| 9357 | Pass command-line variable assignments automatically through the
|
|---|
| 9358 | variable @code{MAKE} to recursive @code{make} invocations.
|
|---|
| 9359 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9360 |
|
|---|
| 9361 | @item
|
|---|
| 9362 | Use the @samp{-C} or @samp{--directory} command option to change
|
|---|
| 9363 | directory. @xref{Options Summary, ,Summary of Options}.
|
|---|
| 9364 |
|
|---|
| 9365 | @item
|
|---|
| 9366 | Make verbatim variable definitions with @code{define}.
|
|---|
| 9367 | @xref{Defining, ,Defining Variables Verbatim}.
|
|---|
| 9368 |
|
|---|
| 9369 | @item
|
|---|
| 9370 | Declare phony targets with the special target @code{.PHONY}.
|
|---|
| 9371 |
|
|---|
| 9372 | Andrew Hume of AT&T Bell Labs implemented a similar feature with a
|
|---|
| 9373 | different syntax in his @code{mk} program. This seems to be a case of
|
|---|
| 9374 | parallel discovery. @xref{Phony Targets, ,Phony Targets}.
|
|---|
| 9375 |
|
|---|
| 9376 | @item
|
|---|
| 9377 | Manipulate text by calling functions.
|
|---|
| 9378 | @xref{Functions, ,Functions for Transforming Text}.
|
|---|
| 9379 |
|
|---|
| 9380 | @item
|
|---|
| 9381 | Use the @samp{-o} or @samp{--old-file}
|
|---|
| 9382 | option to pretend a file's modification-time is old.
|
|---|
| 9383 | @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
|
|---|
| 9384 |
|
|---|
| 9385 | @item
|
|---|
| 9386 | Conditional execution.
|
|---|
| 9387 |
|
|---|
| 9388 | This feature has been implemented numerous times in various versions
|
|---|
| 9389 | of @code{make}; it seems a natural extension derived from the features
|
|---|
| 9390 | of the C preprocessor and similar macro languages and is not a
|
|---|
| 9391 | revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
|
|---|
| 9392 |
|
|---|
| 9393 | @item
|
|---|
| 9394 | Specify a search path for included makefiles.
|
|---|
| 9395 | @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9396 |
|
|---|
| 9397 | @item
|
|---|
| 9398 | Specify extra makefiles to read with an environment variable.
|
|---|
| 9399 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
|---|
| 9400 |
|
|---|
| 9401 | @item
|
|---|
| 9402 | Strip leading sequences of @samp{./} from file names, so that
|
|---|
| 9403 | @file{./@var{file}} and @file{@var{file}} are considered to be the
|
|---|
| 9404 | same file.@refill
|
|---|
| 9405 |
|
|---|
| 9406 | @item
|
|---|
| 9407 | Use a special search method for library prerequisites written in the
|
|---|
| 9408 | form @samp{-l@var{name}}.
|
|---|
| 9409 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
|---|
| 9410 |
|
|---|
| 9411 | @item
|
|---|
| 9412 | Allow suffixes for suffix rules
|
|---|
| 9413 | (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
|
|---|
| 9414 | characters. In other versions of @code{make}, they must begin with
|
|---|
| 9415 | @samp{.} and not contain any @samp{/} characters.
|
|---|
| 9416 |
|
|---|
| 9417 | @item
|
|---|
| 9418 | Keep track of the current level of @code{make} recursion using the
|
|---|
| 9419 | variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9420 |
|
|---|
| 9421 | @item
|
|---|
| 9422 | Provide any goals given on the command line in the variable
|
|---|
| 9423 | @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 9424 |
|
|---|
| 9425 | @item
|
|---|
| 9426 | Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
|
|---|
| 9427 |
|
|---|
| 9428 | @item
|
|---|
| 9429 | Provide selective @code{vpath} search.
|
|---|
| 9430 | @xref{Directory Search, ,Searching Directories for Prerequisites}.
|
|---|
| 9431 |
|
|---|
| 9432 | @item
|
|---|
| 9433 | Provide computed variable references.
|
|---|
| 9434 | @xref{Reference, ,Basics of Variable References}.
|
|---|
| 9435 |
|
|---|
| 9436 | @item
|
|---|
| 9437 | Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
|
|---|
| 9438 | System V @code{make} has a very, very limited form of this
|
|---|
| 9439 | functionality in that it will check out SCCS files for makefiles.
|
|---|
| 9440 |
|
|---|
| 9441 | @item
|
|---|
| 9442 | Various new built-in implicit rules.
|
|---|
| 9443 | @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
|
|---|
| 9444 |
|
|---|
| 9445 | @item
|
|---|
| 9446 | The built-in variable @samp{MAKE_VERSION} gives the version number of
|
|---|
| 9447 | @code{make}.
|
|---|
| 9448 | @end itemize
|
|---|
| 9449 |
|
|---|
| 9450 | @node Missing, Makefile Conventions, Features, Top
|
|---|
| 9451 | @chapter Incompatibilities and Missing Features
|
|---|
| 9452 | @cindex incompatibilities
|
|---|
| 9453 | @cindex missing features
|
|---|
| 9454 | @cindex features, missing
|
|---|
| 9455 |
|
|---|
| 9456 | The @code{make} programs in various other systems support a few features
|
|---|
| 9457 | that are not implemented in GNU @code{make}. The POSIX.2 standard
|
|---|
| 9458 | (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
|
|---|
| 9459 | require any of these features.@refill
|
|---|
| 9460 |
|
|---|
| 9461 | @itemize @bullet
|
|---|
| 9462 | @item
|
|---|
| 9463 | A target of the form @samp{@var{file}((@var{entry}))} stands for a member
|
|---|
| 9464 | of archive file @var{file}. The member is chosen, not by name, but by
|
|---|
| 9465 | being an object file which defines the linker symbol @var{entry}.@refill
|
|---|
| 9466 |
|
|---|
| 9467 | This feature was not put into GNU @code{make} because of the
|
|---|
| 9468 | nonmodularity of putting knowledge into @code{make} of the internal
|
|---|
| 9469 | format of archive file symbol tables.
|
|---|
| 9470 | @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
|
|---|
| 9471 |
|
|---|
| 9472 | @item
|
|---|
| 9473 | Suffixes (used in suffix rules) that end with the character @samp{~}
|
|---|
| 9474 | have a special meaning to System V @code{make};
|
|---|
| 9475 | they refer to the SCCS file that corresponds
|
|---|
| 9476 | to the file one would get without the @samp{~}. For example, the
|
|---|
| 9477 | suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
|
|---|
| 9478 | the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
|
|---|
| 9479 | series of such suffix rules is required.
|
|---|
| 9480 | @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
|
|---|
| 9481 |
|
|---|
| 9482 | In GNU @code{make}, this entire series of cases is handled by two
|
|---|
| 9483 | pattern rules for extraction from SCCS, in combination with the
|
|---|
| 9484 | general feature of rule chaining.
|
|---|
| 9485 | @xref{Chained Rules, ,Chains of Implicit Rules}.
|
|---|
| 9486 |
|
|---|
| 9487 | @item
|
|---|
| 9488 | In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
|
|---|
| 9489 | (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
|
|---|
| 9490 | strings. We feel it is much cleaner to always use automatic variables
|
|---|
| 9491 | and thus make this feature obsolete.@refill
|
|---|
| 9492 |
|
|---|
| 9493 | @item
|
|---|
| 9494 | In some Unix @code{make}s, the automatic variable @code{$*} appearing in
|
|---|
| 9495 | the prerequisites of a rule has the amazingly strange ``feature'' of
|
|---|
| 9496 | expanding to the full name of the @emph{target of that rule}. We cannot
|
|---|
| 9497 | imagine what went on in the minds of Unix @code{make} developers to do
|
|---|
| 9498 | this; it is utterly inconsistent with the normal definition of @code{$*}.
|
|---|
| 9499 | @vindex * @r{(automatic variable), unsupported bizarre usage}
|
|---|
| 9500 |
|
|---|
| 9501 | @item
|
|---|
| 9502 | In some Unix @code{make}s, implicit rule search
|
|---|
| 9503 | (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
|
|---|
| 9504 | @emph{all} targets, not just those without commands. This means you can
|
|---|
| 9505 | do:@refill
|
|---|
| 9506 |
|
|---|
| 9507 | @example
|
|---|
| 9508 | @group
|
|---|
| 9509 | foo.o:
|
|---|
| 9510 | cc -c foo.c
|
|---|
| 9511 | @end group
|
|---|
| 9512 | @end example
|
|---|
| 9513 |
|
|---|
| 9514 | @noindent
|
|---|
| 9515 | and Unix @code{make} will intuit that @file{foo.o} depends on
|
|---|
| 9516 | @file{foo.c}.@refill
|
|---|
| 9517 |
|
|---|
| 9518 | We feel that such usage is broken. The prerequisite properties of
|
|---|
| 9519 | @code{make} are well-defined (for GNU @code{make}, at least),
|
|---|
| 9520 | and doing such a thing simply does not fit the model.@refill
|
|---|
| 9521 |
|
|---|
| 9522 | @item
|
|---|
| 9523 | GNU @code{make} does not include any built-in implicit rules for
|
|---|
| 9524 | compiling or preprocessing EFL programs. If we hear of anyone who is
|
|---|
| 9525 | using EFL, we will gladly add them.
|
|---|
| 9526 |
|
|---|
| 9527 | @item
|
|---|
| 9528 | It appears that in SVR4 @code{make}, a suffix rule can be specified with
|
|---|
| 9529 | no commands, and it is treated as if it had empty commands
|
|---|
| 9530 | (@pxref{Empty Commands}). For example:
|
|---|
| 9531 |
|
|---|
| 9532 | @example
|
|---|
| 9533 | .c.a:
|
|---|
| 9534 | @end example
|
|---|
| 9535 |
|
|---|
| 9536 | @noindent
|
|---|
| 9537 | will override the built-in @file{.c.a} suffix rule.
|
|---|
| 9538 |
|
|---|
| 9539 | We feel that it is cleaner for a rule without commands to always simply
|
|---|
| 9540 | add to the prerequisite list for the target. The above example can be
|
|---|
| 9541 | easily rewritten to get the desired behavior in GNU @code{make}:
|
|---|
| 9542 |
|
|---|
| 9543 | @example
|
|---|
| 9544 | .c.a: ;
|
|---|
| 9545 | @end example
|
|---|
| 9546 |
|
|---|
| 9547 | @item
|
|---|
| 9548 | Some versions of @code{make} invoke the shell with the @samp{-e} flag,
|
|---|
| 9549 | except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
|
|---|
| 9550 | Program}). The @samp{-e} flag tells the shell to exit as soon as any
|
|---|
| 9551 | program it runs returns a nonzero status. We feel it is cleaner to
|
|---|
| 9552 | write each shell command line to stand on its own and not require this
|
|---|
| 9553 | special treatment.
|
|---|
| 9554 | @end itemize
|
|---|
| 9555 |
|
|---|
| 9556 | @comment The makefile standards are in a separate file that is also
|
|---|
| 9557 | @comment included by standards.texi.
|
|---|
| 9558 | @include make-stds.texi
|
|---|
| 9559 |
|
|---|
| 9560 | @node Quick Reference, Error Messages, Makefile Conventions, Top
|
|---|
| 9561 | @appendix Quick Reference
|
|---|
| 9562 |
|
|---|
| 9563 | This appendix summarizes the directives, text manipulation functions,
|
|---|
| 9564 | and special variables which GNU @code{make} understands.
|
|---|
| 9565 | @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
|
|---|
| 9566 | and @ref{Options Summary, ,Summary of Options},
|
|---|
| 9567 | for other summaries.
|
|---|
| 9568 |
|
|---|
| 9569 | Here is a summary of the directives GNU @code{make} recognizes:
|
|---|
| 9570 |
|
|---|
| 9571 | @table @code
|
|---|
| 9572 | @item define @var{variable}
|
|---|
| 9573 | @itemx endef
|
|---|
| 9574 |
|
|---|
| 9575 | Define a multi-line, recursively-expanded variable.@*
|
|---|
| 9576 | @xref{Sequences}.
|
|---|
| 9577 |
|
|---|
| 9578 | @item ifdef @var{variable}
|
|---|
| 9579 | @itemx ifndef @var{variable}
|
|---|
| 9580 | @itemx ifeq (@var{a},@var{b})
|
|---|
| 9581 | @itemx ifeq "@var{a}" "@var{b}"
|
|---|
| 9582 | @itemx ifeq '@var{a}' '@var{b}'
|
|---|
| 9583 | @itemx ifneq (@var{a},@var{b})
|
|---|
| 9584 | @itemx ifneq "@var{a}" "@var{b}"
|
|---|
| 9585 | @itemx ifneq '@var{a}' '@var{b}'
|
|---|
| 9586 | @itemx else
|
|---|
| 9587 | @itemx endif
|
|---|
| 9588 |
|
|---|
| 9589 | Conditionally evaluate part of the makefile.@*
|
|---|
| 9590 | @xref{Conditionals}.
|
|---|
| 9591 |
|
|---|
| 9592 | @item include @var{file}
|
|---|
| 9593 | @itemx -include @var{file}
|
|---|
| 9594 | @itemx sinclude @var{file}
|
|---|
| 9595 |
|
|---|
| 9596 | Include another makefile.@*
|
|---|
| 9597 | @xref{Include, ,Including Other Makefiles}.
|
|---|
| 9598 |
|
|---|
| 9599 | @item override @var{variable} = @var{value}
|
|---|
| 9600 | @itemx override @var{variable} := @var{value}
|
|---|
| 9601 | @itemx override @var{variable} += @var{value}
|
|---|
| 9602 | @itemx override @var{variable} ?= @var{value}
|
|---|
| 9603 | @itemx override define @var{variable}
|
|---|
| 9604 | @itemx endef
|
|---|
| 9605 |
|
|---|
| 9606 | Define a variable, overriding any previous definition, even one from
|
|---|
| 9607 | the command line.@*
|
|---|
| 9608 | @xref{Override Directive, ,The @code{override} Directive}.
|
|---|
| 9609 |
|
|---|
| 9610 | @item export
|
|---|
| 9611 |
|
|---|
| 9612 | Tell @code{make} to export all variables to child processes by default.@*
|
|---|
| 9613 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
|---|
| 9614 |
|
|---|
| 9615 | @item export @var{variable}
|
|---|
| 9616 | @itemx export @var{variable} = @var{value}
|
|---|
| 9617 | @itemx export @var{variable} := @var{value}
|
|---|
| 9618 | @itemx export @var{variable} += @var{value}
|
|---|
| 9619 | @itemx export @var{variable} ?= @var{value}
|
|---|
| 9620 | @itemx unexport @var{variable}
|
|---|
| 9621 | Tell @code{make} whether or not to export a particular variable to child
|
|---|
| 9622 | processes.@*
|
|---|
| 9623 | @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
|
|---|
| 9624 |
|
|---|
| 9625 | @item vpath @var{pattern} @var{path}
|
|---|
| 9626 | Specify a search path for files matching a @samp{%} pattern.@*
|
|---|
| 9627 | @xref{Selective Search, , The @code{vpath} Directive}.
|
|---|
| 9628 |
|
|---|
| 9629 | @item vpath @var{pattern}
|
|---|
| 9630 | Remove all search paths previously specified for @var{pattern}.
|
|---|
| 9631 |
|
|---|
| 9632 | @item vpath
|
|---|
| 9633 | Remove all search paths previously specified in any @code{vpath}
|
|---|
| 9634 | directive.
|
|---|
| 9635 | @end table
|
|---|
| 9636 |
|
|---|
| 9637 | Here is a summary of the text manipulation functions (@pxref{Functions}):
|
|---|
| 9638 |
|
|---|
| 9639 | @table @code
|
|---|
| 9640 | @item $(subst @var{from},@var{to},@var{text})
|
|---|
| 9641 | Replace @var{from} with @var{to} in @var{text}.@*
|
|---|
| 9642 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9643 |
|
|---|
| 9644 | @item $(patsubst @var{pattern},@var{replacement},@var{text})
|
|---|
| 9645 | Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
|
|---|
| 9646 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9647 |
|
|---|
| 9648 | @item $(strip @var{string})
|
|---|
| 9649 | Remove excess whitespace characters from @var{string}.@*
|
|---|
| 9650 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9651 |
|
|---|
| 9652 | @item $(findstring @var{find},@var{text})
|
|---|
| 9653 | Locate @var{find} in @var{text}.@*
|
|---|
| 9654 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9655 |
|
|---|
| 9656 | @item $(filter @var{pattern}@dots{},@var{text})
|
|---|
| 9657 | Select words in @var{text} that match one of the @var{pattern} words.@*
|
|---|
| 9658 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9659 |
|
|---|
| 9660 | @item $(filter-out @var{pattern}@dots{},@var{text})
|
|---|
| 9661 | Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
|
|---|
| 9662 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9663 |
|
|---|
| 9664 | @item $(sort @var{list})
|
|---|
| 9665 | Sort the words in @var{list} lexicographically, removing duplicates.@*
|
|---|
| 9666 | @xref{Text Functions, , Functions for String Substitution and Analysis}.
|
|---|
| 9667 |
|
|---|
| 9668 | @item $(dir @var{names}@dots{})
|
|---|
| 9669 | Extract the directory part of each file name.@*
|
|---|
| 9670 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9671 |
|
|---|
| 9672 | @item $(notdir @var{names}@dots{})
|
|---|
| 9673 | Extract the non-directory part of each file name.@*
|
|---|
| 9674 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9675 |
|
|---|
| 9676 | @item $(suffix @var{names}@dots{})
|
|---|
| 9677 | Extract the suffix (the last @samp{.} and following characters) of each file name.@*
|
|---|
| 9678 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9679 |
|
|---|
| 9680 | @item $(basename @var{names}@dots{})
|
|---|
| 9681 | Extract the base name (name without suffix) of each file name.@*
|
|---|
| 9682 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9683 |
|
|---|
| 9684 | @item $(addsuffix @var{suffix},@var{names}@dots{})
|
|---|
| 9685 | Append @var{suffix} to each word in @var{names}.@*
|
|---|
| 9686 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9687 |
|
|---|
| 9688 | @item $(addprefix @var{prefix},@var{names}@dots{})
|
|---|
| 9689 | Prepend @var{prefix} to each word in @var{names}.@*
|
|---|
| 9690 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9691 |
|
|---|
| 9692 | @item $(join @var{list1},@var{list2})
|
|---|
| 9693 | Join two parallel lists of words.@*
|
|---|
| 9694 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9695 |
|
|---|
| 9696 | @item $(word @var{n},@var{text})
|
|---|
| 9697 | Extract the @var{n}th word (one-origin) of @var{text}.@*
|
|---|
| 9698 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9699 |
|
|---|
| 9700 | @item $(words @var{text})
|
|---|
| 9701 | Count the number of words in @var{text}.@*
|
|---|
| 9702 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9703 |
|
|---|
| 9704 | @item $(wordlist @var{s},@var{e},@var{text})
|
|---|
| 9705 | Returns the list of words in @var{text} from @var{s} to @var{e}.@*
|
|---|
| 9706 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9707 |
|
|---|
| 9708 | @item $(firstword @var{names}@dots{})
|
|---|
| 9709 | Extract the first word of @var{names}.@*
|
|---|
| 9710 | @xref{File Name Functions, ,Functions for File Names}.
|
|---|
| 9711 |
|
|---|
| 9712 | @item $(wildcard @var{pattern}@dots{})
|
|---|
| 9713 | Find file names matching a shell file name pattern (@emph{not} a
|
|---|
| 9714 | @samp{%} pattern).@*
|
|---|
| 9715 | @xref{Wildcard Function, ,The Function @code{wildcard}}.
|
|---|
| 9716 |
|
|---|
| 9717 | @item $(error @var{text}@dots{})
|
|---|
| 9718 |
|
|---|
| 9719 | When this function is evaluated, @code{make} generates a fatal error
|
|---|
| 9720 | with the message @var{text}.@*
|
|---|
| 9721 | @xref{Make Control Functions, ,Functions That Control Make}.
|
|---|
| 9722 |
|
|---|
| 9723 | @item $(warning @var{text}@dots{})
|
|---|
| 9724 |
|
|---|
| 9725 | When this function is evaluated, @code{make} generates a warning with
|
|---|
| 9726 | the message @var{text}.@*
|
|---|
| 9727 | @xref{Make Control Functions, ,Functions That Control Make}.
|
|---|
| 9728 |
|
|---|
| 9729 | @item $(shell @var{command})
|
|---|
| 9730 |
|
|---|
| 9731 | Execute a shell command and return its output.@*
|
|---|
| 9732 | @xref{Shell Function, , The @code{shell} Function}.
|
|---|
| 9733 |
|
|---|
| 9734 | @item $(origin @var{variable})
|
|---|
| 9735 |
|
|---|
| 9736 | Return a string describing how the @code{make} variable @var{variable} was
|
|---|
| 9737 | defined.@*
|
|---|
| 9738 | @xref{Origin Function, , The @code{origin} Function}.
|
|---|
| 9739 |
|
|---|
| 9740 | @item $(foreach @var{var},@var{words},@var{text})
|
|---|
| 9741 |
|
|---|
| 9742 | Evaluate @var{text} with @var{var} bound to each word in @var{words},
|
|---|
| 9743 | and concatenate the results.@*
|
|---|
| 9744 | @xref{Foreach Function, ,The @code{foreach} Function}.
|
|---|
| 9745 |
|
|---|
| 9746 | @item $(call @var{var},@var{param},@dots{})
|
|---|
| 9747 |
|
|---|
| 9748 | Evaluate the variable @var{var} replacing any references to @code{$(1)},
|
|---|
| 9749 | @code{$(2)} with the first, second, etc. @var{param} values.@*
|
|---|
| 9750 | @xref{Call Function, ,The @code{call} Function}.
|
|---|
| 9751 |
|
|---|
| 9752 | @item $(eval @var{text})
|
|---|
| 9753 |
|
|---|
| 9754 | Evaluate @var{text} then read the results as makefile commands.
|
|---|
| 9755 | Expands to the empty string.@*
|
|---|
| 9756 | @xref{Eval Function, ,The @code{eval} Function}.
|
|---|
| 9757 |
|
|---|
| 9758 | @item $(value @var{var})
|
|---|
| 9759 |
|
|---|
| 9760 | Evaluates to the contents of the variable @var{var}, with no expansion
|
|---|
| 9761 | performed on it.@*
|
|---|
| 9762 | @xref{Value Function, ,The @code{value} Function}.
|
|---|
| 9763 | @end table
|
|---|
| 9764 |
|
|---|
| 9765 | Here is a summary of the automatic variables.
|
|---|
| 9766 | @xref{Automatic Variables},
|
|---|
| 9767 | for full information.
|
|---|
| 9768 |
|
|---|
| 9769 | @table @code
|
|---|
| 9770 | @item $@@
|
|---|
| 9771 | The file name of the target.
|
|---|
| 9772 |
|
|---|
| 9773 | @item $%
|
|---|
| 9774 | The target member name, when the target is an archive member.
|
|---|
| 9775 |
|
|---|
| 9776 | @item $<
|
|---|
| 9777 | The name of the first prerequisite.
|
|---|
| 9778 |
|
|---|
| 9779 | @item $?
|
|---|
| 9780 | The names of all the prerequisites that are
|
|---|
| 9781 | newer than the target, with spaces between them.
|
|---|
| 9782 | For prerequisites which are archive members, only
|
|---|
| 9783 | the member named is used (@pxref{Archives}).
|
|---|
| 9784 |
|
|---|
| 9785 | @item $^
|
|---|
| 9786 | @itemx $+
|
|---|
| 9787 | The names of all the prerequisites, with spaces between them. For
|
|---|
| 9788 | prerequisites which are archive members, only the member named is used
|
|---|
| 9789 | (@pxref{Archives}). The value of @code{$^} omits duplicate
|
|---|
| 9790 | prerequisites, while @code{$+} retains them and preserves their order.
|
|---|
| 9791 |
|
|---|
| 9792 | @item $*
|
|---|
| 9793 | The stem with which an implicit rule matches
|
|---|
| 9794 | (@pxref{Pattern Match, ,How Patterns Match}).
|
|---|
| 9795 |
|
|---|
| 9796 | @item $(@@D)
|
|---|
| 9797 | @itemx $(@@F)
|
|---|
| 9798 | The directory part and the file-within-directory part of @code{$@@}.
|
|---|
| 9799 |
|
|---|
| 9800 | @item $(*D)
|
|---|
| 9801 | @itemx $(*F)
|
|---|
| 9802 | The directory part and the file-within-directory part of @code{$*}.
|
|---|
| 9803 |
|
|---|
| 9804 | @item $(%D)
|
|---|
| 9805 | @itemx $(%F)
|
|---|
| 9806 | The directory part and the file-within-directory part of @code{$%}.
|
|---|
| 9807 |
|
|---|
| 9808 | @item $(<D)
|
|---|
| 9809 | @itemx $(<F)
|
|---|
| 9810 | The directory part and the file-within-directory part of @code{$<}.
|
|---|
| 9811 |
|
|---|
| 9812 | @item $(^D)
|
|---|
| 9813 | @itemx $(^F)
|
|---|
| 9814 | The directory part and the file-within-directory part of @code{$^}.
|
|---|
| 9815 |
|
|---|
| 9816 | @item $(+D)
|
|---|
| 9817 | @itemx $(+F)
|
|---|
| 9818 | The directory part and the file-within-directory part of @code{$+}.
|
|---|
| 9819 |
|
|---|
| 9820 | @item $(?D)
|
|---|
| 9821 | @itemx $(?F)
|
|---|
| 9822 | The directory part and the file-within-directory part of @code{$?}.
|
|---|
| 9823 | @end table
|
|---|
| 9824 |
|
|---|
| 9825 | These variables are used specially by GNU @code{make}:
|
|---|
| 9826 |
|
|---|
| 9827 | @table @code
|
|---|
| 9828 | @item MAKEFILES
|
|---|
| 9829 |
|
|---|
| 9830 | Makefiles to be read on every invocation of @code{make}.@*
|
|---|
| 9831 | @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
|
|---|
| 9832 |
|
|---|
| 9833 | @item VPATH
|
|---|
| 9834 |
|
|---|
| 9835 | Directory search path for files not found in the current directory.@*
|
|---|
| 9836 | @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
|
|---|
| 9837 |
|
|---|
| 9838 | @item SHELL
|
|---|
| 9839 |
|
|---|
| 9840 | The name of the system default command interpreter, usually @file{/bin/sh}.
|
|---|
| 9841 | You can set @code{SHELL} in the makefile to change the shell used to run
|
|---|
| 9842 | commands. @xref{Execution, ,Command Execution}.
|
|---|
| 9843 |
|
|---|
| 9844 | @item MAKESHELL
|
|---|
| 9845 |
|
|---|
| 9846 | On MS-DOS only, the name of the command interpreter that is to be used
|
|---|
| 9847 | by @code{make}. This value takes precedence over the value of
|
|---|
| 9848 | @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
|
|---|
| 9849 |
|
|---|
| 9850 | @item MAKE
|
|---|
| 9851 |
|
|---|
| 9852 | The name with which @code{make} was invoked.
|
|---|
| 9853 | Using this variable in commands has special meaning.
|
|---|
| 9854 | @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
|
|---|
| 9855 |
|
|---|
| 9856 | @item MAKELEVEL
|
|---|
| 9857 |
|
|---|
| 9858 | The number of levels of recursion (sub-@code{make}s).@*
|
|---|
| 9859 | @xref{Variables/Recursion}.
|
|---|
| 9860 |
|
|---|
| 9861 | @item MAKEFLAGS
|
|---|
| 9862 |
|
|---|
| 9863 | The flags given to @code{make}. You can set this in the environment or
|
|---|
| 9864 | a makefile to set flags.@*
|
|---|
| 9865 | @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
|
|---|
| 9866 |
|
|---|
| 9867 | It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
|
|---|
| 9868 | command line: its contents may not be quoted correctly for use in the
|
|---|
| 9869 | shell. Always allow recursive @code{make}'s to obtain these values
|
|---|
| 9870 | through the environment from its parent.
|
|---|
| 9871 |
|
|---|
| 9872 | @item MAKECMDGOALS
|
|---|
| 9873 |
|
|---|
| 9874 | The targets given to @code{make} on the command line. Setting this
|
|---|
| 9875 | variable has no effect on the operation of @code{make}.@*
|
|---|
| 9876 | @xref{Goals, ,Arguments to Specify the Goals}.
|
|---|
| 9877 |
|
|---|
| 9878 | @item CURDIR
|
|---|
| 9879 |
|
|---|
| 9880 | Set to the pathname of the current working directory (after all
|
|---|
| 9881 | @code{-C} options are processed, if any). Setting this variable has no
|
|---|
| 9882 | effect on the operation of @code{make}.@*
|
|---|
| 9883 | @xref{Recursion, ,Recursive Use of @code{make}}.
|
|---|
| 9884 |
|
|---|
| 9885 | @item SUFFIXES
|
|---|
| 9886 |
|
|---|
| 9887 | The default list of suffixes before @code{make} reads any makefiles.
|
|---|
| 9888 |
|
|---|
| 9889 | @item .LIBPATTERNS
|
|---|
| 9890 | Defines the naming of the libraries @code{make} searches for, and their
|
|---|
| 9891 | order.@*
|
|---|
| 9892 | @xref{Libraries/Search, ,Directory Search for Link Libraries}.
|
|---|
| 9893 | @end table
|
|---|
| 9894 |
|
|---|
| 9895 | @node Error Messages, Complex Makefile, Quick Reference, Top
|
|---|
| 9896 | @comment node-name, next, previous, up
|
|---|
| 9897 | @appendix Errors Generated by Make
|
|---|
| 9898 |
|
|---|
| 9899 | Here is a list of the more common errors you might see generated by
|
|---|
| 9900 | @code{make}, and some information about what they mean and how to fix
|
|---|
| 9901 | them.
|
|---|
| 9902 |
|
|---|
| 9903 | Sometimes @code{make} errors are not fatal, especially in the presence
|
|---|
| 9904 | of a @code{-} prefix on a command script line, or the @code{-k} command
|
|---|
| 9905 | line option. Errors that are fatal are prefixed with the string
|
|---|
| 9906 | @code{***}.
|
|---|
| 9907 |
|
|---|
| 9908 | Error messages are all either prefixed with the name of the program
|
|---|
| 9909 | (usually @samp{make}), or, if the error is found in a makefile, the name
|
|---|
| 9910 | of the file and linenumber containing the problem.
|
|---|
| 9911 |
|
|---|
| 9912 | In the table below, these common prefixes are left off.
|
|---|
| 9913 |
|
|---|
| 9914 | @table @samp
|
|---|
| 9915 |
|
|---|
| 9916 | @item [@var{foo}] Error @var{NN}
|
|---|
| 9917 | @itemx [@var{foo}] @var{signal description}
|
|---|
| 9918 | These errors are not really @code{make} errors at all. They mean that a
|
|---|
| 9919 | program that @code{make} invoked as part of a command script returned a
|
|---|
| 9920 | non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
|
|---|
| 9921 | as failure, or it exited in some other abnormal fashion (with a
|
|---|
| 9922 | signal of some type). @xref{Errors, ,Errors in Commands}.
|
|---|
| 9923 |
|
|---|
| 9924 | If no @code{***} is attached to the message, then the subprocess failed
|
|---|
| 9925 | but the rule in the makefile was prefixed with the @code{-} special
|
|---|
| 9926 | character, so @code{make} ignored the error.
|
|---|
| 9927 |
|
|---|
| 9928 | @item missing separator. Stop.
|
|---|
| 9929 | @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
|
|---|
| 9930 | This means that @code{make} could not understand much of anything about
|
|---|
| 9931 | the command line it just read. GNU @code{make} looks for various kinds
|
|---|
| 9932 | of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
|
|---|
| 9933 | decide what kind of commandline it's seeing. This means it couldn't
|
|---|
| 9934 | find a valid one.
|
|---|
| 9935 |
|
|---|
| 9936 | One of the most common reasons for this message is that you (or perhaps
|
|---|
| 9937 | your oh-so-helpful editor, as is the case with many MS-Windows editors)
|
|---|
| 9938 | have attempted to indent your command scripts with spaces instead of a
|
|---|
| 9939 | TAB character. In this case, @code{make} will use the second form of
|
|---|
| 9940 | the error above. Remember that every line in the command script must
|
|---|
| 9941 | begin with a TAB character. Eight spaces do not count. @xref{Rule
|
|---|
| 9942 | Syntax}.
|
|---|
| 9943 |
|
|---|
| 9944 | @item commands commence before first target. Stop.
|
|---|
| 9945 | @itemx missing rule before commands. Stop.
|
|---|
| 9946 | This means the first thing in the makefile seems to be part of a command
|
|---|
| 9947 | script: it begins with a TAB character and doesn't appear to be a legal
|
|---|
| 9948 | @code{make} command (such as a variable assignment). Command scripts
|
|---|
| 9949 | must always be associated with a target.
|
|---|
| 9950 |
|
|---|
| 9951 | The second form is generated if the line has a semicolon as the first
|
|---|
| 9952 | non-whitespace character; @code{make} interprets this to mean you left
|
|---|
| 9953 | out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
|
|---|
| 9954 |
|
|---|
| 9955 | @item No rule to make target `@var{xxx}'.
|
|---|
| 9956 | @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
|
|---|
| 9957 | This means that @code{make} decided it needed to build a target, but
|
|---|
| 9958 | then couldn't find any instructions in the makefile on how to do that,
|
|---|
| 9959 | either explicit or implicit (including in the default rules database).
|
|---|
| 9960 |
|
|---|
| 9961 | If you want that file to be built, you will need to add a rule to your
|
|---|
| 9962 | makefile describing how that target can be built. Other possible
|
|---|
| 9963 | sources of this problem are typos in the makefile (if that filename is
|
|---|
| 9964 | wrong) or a corrupted source tree (if that file is not supposed to be
|
|---|
| 9965 | built, but rather only a prerequisite).
|
|---|
| 9966 |
|
|---|
| 9967 | @item No targets specified and no makefile found. Stop.
|
|---|
| 9968 | @itemx No targets. Stop.
|
|---|
| 9969 | The former means that you didn't provide any targets to be built on the
|
|---|
| 9970 | command line, and @code{make} couldn't find any makefiles to read in.
|
|---|
| 9971 | The latter means that some makefile was found, but it didn't contain any
|
|---|
| 9972 | default target and none was given on the command line. GNU @code{make}
|
|---|
| 9973 | has nothing to do in these situations.
|
|---|
| 9974 | @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
|
|---|
| 9975 |
|
|---|
| 9976 | @item Makefile `@var{xxx}' was not found.
|
|---|
| 9977 | @itemx Included makefile `@var{xxx}' was not found.
|
|---|
| 9978 | A makefile specified on the command line (first form) or included
|
|---|
| 9979 | (second form) was not found.
|
|---|
| 9980 |
|
|---|
| 9981 | @item warning: overriding commands for target `@var{xxx}'
|
|---|
| 9982 | @itemx warning: ignoring old commands for target `@var{xxx}'
|
|---|
| 9983 | GNU @code{make} allows commands to be specified only once per target
|
|---|
| 9984 | (except for double-colon rules). If you give commands for a target
|
|---|
| 9985 | which already has been defined to have commands, this warning is issued
|
|---|
| 9986 | and the second set of commands will overwrite the first set.
|
|---|
| 9987 | @xref{Multiple Rules, ,Multiple Rules for One Target}.
|
|---|
| 9988 |
|
|---|
| 9989 | @item Circular @var{xxx} <- @var{yyy} dependency dropped.
|
|---|
| 9990 | This means that @code{make} detected a loop in the dependency graph:
|
|---|
| 9991 | after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
|
|---|
| 9992 | prerequisites, etc., one of them depended on @var{xxx} again.
|
|---|
| 9993 |
|
|---|
| 9994 | @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
|
|---|
| 9995 | This means you've defined a normal (recursive) @code{make} variable
|
|---|
| 9996 | @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
|
|---|
| 9997 | This is not allowed; either use simply-expanded variables (@code{:=}) or
|
|---|
| 9998 | use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
|
|---|
| 9999 | Variables}.
|
|---|
| 10000 |
|
|---|
| 10001 | @item Unterminated variable reference. Stop.
|
|---|
| 10002 | This means you forgot to provide the proper closing parenthesis
|
|---|
| 10003 | or brace in your variable or function reference.
|
|---|
| 10004 |
|
|---|
| 10005 | @item insufficient arguments to function `@var{xxx}'. Stop.
|
|---|
| 10006 | This means you haven't provided the requisite number of arguments for
|
|---|
| 10007 | this function. See the documentation of the function for a description
|
|---|
| 10008 | of its arguments. @xref{Functions, ,Functions for Transforming Text}.
|
|---|
| 10009 |
|
|---|
| 10010 | @item missing target pattern. Stop.
|
|---|
| 10011 | @itemx multiple target patterns. Stop.
|
|---|
| 10012 | @itemx target pattern contains no `%'. Stop.
|
|---|
| 10013 | @itemx mixed implicit and static pattern rules. Stop.
|
|---|
| 10014 | These are generated for malformed static pattern rules. The first means
|
|---|
| 10015 | there's no pattern in the target section of the rule; the second means
|
|---|
| 10016 | there are multiple patterns in the target section; the third means
|
|---|
| 10017 | the target doesn't contain a pattern character (@code{%}); and the
|
|---|
| 10018 | fourth means that all three parts of the static pattern rule contain
|
|---|
| 10019 | pattern characters (@code{%})--only the first two parts should.
|
|---|
| 10020 | @xref{Static Usage, ,Syntax of Static Pattern Rules}.
|
|---|
| 10021 |
|
|---|
| 10022 | @item warning: -jN forced in submake: disabling jobserver mode.
|
|---|
| 10023 | This warning and the next are generated if @code{make} detects error
|
|---|
| 10024 | conditions related to parallel processing on systems where
|
|---|
| 10025 | sub-@code{make}s can communicate (@pxref{Options/Recursion,
|
|---|
| 10026 | ,Communicating Options to a Sub-@code{make}}). This warning is
|
|---|
| 10027 | generated if a recursive invocation of a @code{make} process is forced
|
|---|
| 10028 | to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
|
|---|
| 10029 | than one). This could happen, for example, if you set the @code{MAKE}
|
|---|
| 10030 | environment variable to @samp{make -j2}. In this case, the
|
|---|
| 10031 | sub-@code{make} doesn't communicate with other @code{make} processes and
|
|---|
| 10032 | will simply pretend it has two jobs of its own.
|
|---|
| 10033 |
|
|---|
| 10034 | @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
|
|---|
| 10035 | In order for @code{make} processes to communicate, the parent will pass
|
|---|
| 10036 | information to the child. Since this could result in problems if the
|
|---|
| 10037 | child process isn't actually a @code{make}, the parent will only do this
|
|---|
| 10038 | if it thinks the child is a @code{make}. The parent uses the normal
|
|---|
| 10039 | algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
|
|---|
| 10040 | Variable Works}). If the makefile is constructed such that the parent
|
|---|
| 10041 | doesn't know the child is a @code{make} process, then the child will
|
|---|
| 10042 | receive only part of the information necessary. In this case, the child
|
|---|
| 10043 | will generate this warning message and proceed with its build in a
|
|---|
| 10044 | sequential manner.
|
|---|
| 10045 |
|
|---|
| 10046 | @end table
|
|---|
| 10047 |
|
|---|
| 10048 | @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
|
|---|
| 10049 | @appendix Complex Makefile Example
|
|---|
| 10050 |
|
|---|
| 10051 | Here is the makefile for the GNU @code{tar} program. This is a
|
|---|
| 10052 | moderately complex makefile.
|
|---|
| 10053 |
|
|---|
| 10054 | Because it is the first target, the default goal is @samp{all}. An
|
|---|
| 10055 | interesting feature of this makefile is that @file{testpad.h} is a
|
|---|
| 10056 | source file automatically created by the @code{testpad} program,
|
|---|
| 10057 | itself compiled from @file{testpad.c}.
|
|---|
| 10058 |
|
|---|
| 10059 | If you type @samp{make} or @samp{make all}, then @code{make} creates
|
|---|
| 10060 | the @file{tar} executable, the @file{rmt} daemon that provides
|
|---|
| 10061 | remote tape access, and the @file{tar.info} Info file.
|
|---|
| 10062 |
|
|---|
| 10063 | If you type @samp{make install}, then @code{make} not only creates
|
|---|
| 10064 | @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
|
|---|
| 10065 | them.
|
|---|
| 10066 |
|
|---|
| 10067 | If you type @samp{make clean}, then @code{make} removes the @samp{.o}
|
|---|
| 10068 | files, and the @file{tar}, @file{rmt}, @file{testpad},
|
|---|
| 10069 | @file{testpad.h}, and @file{core} files.
|
|---|
| 10070 |
|
|---|
| 10071 | If you type @samp{make distclean}, then @code{make} not only removes
|
|---|
| 10072 | the same files as does @samp{make clean} but also the
|
|---|
| 10073 | @file{TAGS}, @file{Makefile}, and @file{config.status} files.
|
|---|
| 10074 | (Although it is not evident, this makefile (and
|
|---|
| 10075 | @file{config.status}) is generated by the user with the
|
|---|
| 10076 | @code{configure} program, which is provided in the @code{tar}
|
|---|
| 10077 | distribution, but is not shown here.)
|
|---|
| 10078 |
|
|---|
| 10079 | If you type @samp{make realclean}, then @code{make} removes the same
|
|---|
| 10080 | files as does @samp{make distclean} and also removes the Info files
|
|---|
| 10081 | generated from @file{tar.texinfo}.
|
|---|
| 10082 |
|
|---|
| 10083 | In addition, there are targets @code{shar} and @code{dist} that create
|
|---|
| 10084 | distribution kits.
|
|---|
| 10085 |
|
|---|
| 10086 | @example
|
|---|
| 10087 | @group
|
|---|
| 10088 | # Generated automatically from Makefile.in by configure.
|
|---|
| 10089 | # Un*x Makefile for GNU tar program.
|
|---|
| 10090 | # Copyright (C) 1991 Free Software Foundation, Inc.
|
|---|
| 10091 | @end group
|
|---|
| 10092 |
|
|---|
| 10093 | @group
|
|---|
| 10094 | # This program is free software; you can redistribute
|
|---|
| 10095 | # it and/or modify it under the terms of the GNU
|
|---|
| 10096 | # General Public License @dots{}
|
|---|
| 10097 | @dots{}
|
|---|
| 10098 | @dots{}
|
|---|
| 10099 | @end group
|
|---|
| 10100 |
|
|---|
| 10101 | SHELL = /bin/sh
|
|---|
| 10102 |
|
|---|
| 10103 | #### Start of system configuration section. ####
|
|---|
| 10104 |
|
|---|
| 10105 | srcdir = .
|
|---|
| 10106 |
|
|---|
| 10107 | @group
|
|---|
| 10108 | # If you use gcc, you should either run the
|
|---|
| 10109 | # fixincludes script that comes with it or else use
|
|---|
| 10110 | # gcc with the -traditional option. Otherwise ioctl
|
|---|
| 10111 | # calls will be compiled incorrectly on some systems.
|
|---|
| 10112 | CC = gcc -O
|
|---|
| 10113 | YACC = bison -y
|
|---|
| 10114 | INSTALL = /usr/local/bin/install -c
|
|---|
| 10115 | INSTALLDATA = /usr/local/bin/install -c -m 644
|
|---|
| 10116 | @end group
|
|---|
| 10117 |
|
|---|
| 10118 | # Things you might add to DEFS:
|
|---|
| 10119 | # -DSTDC_HEADERS If you have ANSI C headers and
|
|---|
| 10120 | # libraries.
|
|---|
| 10121 | # -DPOSIX If you have POSIX.1 headers and
|
|---|
| 10122 | # libraries.
|
|---|
| 10123 | # -DBSD42 If you have sys/dir.h (unless
|
|---|
| 10124 | # you use -DPOSIX), sys/file.h,
|
|---|
| 10125 | # and st_blocks in `struct stat'.
|
|---|
| 10126 | # -DUSG If you have System V/ANSI C
|
|---|
| 10127 | # string and memory functions
|
|---|
| 10128 | # and headers, sys/sysmacros.h,
|
|---|
| 10129 | # fcntl.h, getcwd, no valloc,
|
|---|
| 10130 | # and ndir.h (unless
|
|---|
| 10131 | # you use -DDIRENT).
|
|---|
| 10132 | # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
|
|---|
| 10133 | # include memory.h.
|
|---|
| 10134 | # -DDIRENT If USG and you have dirent.h
|
|---|
| 10135 | # instead of ndir.h.
|
|---|
| 10136 | # -DSIGTYPE=int If your signal handlers
|
|---|
| 10137 | # return int, not void.
|
|---|
| 10138 | # -DNO_MTIO If you lack sys/mtio.h
|
|---|
| 10139 | # (magtape ioctls).
|
|---|
| 10140 | # -DNO_REMOTE If you do not have a remote shell
|
|---|
| 10141 | # or rexec.
|
|---|
| 10142 | # -DUSE_REXEC To use rexec for remote tape
|
|---|
| 10143 | # operations instead of
|
|---|
| 10144 | # forking rsh or remsh.
|
|---|
| 10145 | # -DVPRINTF_MISSING If you lack vprintf function
|
|---|
| 10146 | # (but have _doprnt).
|
|---|
| 10147 | # -DDOPRNT_MISSING If you lack _doprnt function.
|
|---|
| 10148 | # Also need to define
|
|---|
| 10149 | # -DVPRINTF_MISSING.
|
|---|
| 10150 | # -DFTIME_MISSING If you lack ftime system call.
|
|---|
| 10151 | # -DSTRSTR_MISSING If you lack strstr function.
|
|---|
| 10152 | # -DVALLOC_MISSING If you lack valloc function.
|
|---|
| 10153 | # -DMKDIR_MISSING If you lack mkdir and
|
|---|
| 10154 | # rmdir system calls.
|
|---|
| 10155 | # -DRENAME_MISSING If you lack rename system call.
|
|---|
| 10156 | # -DFTRUNCATE_MISSING If you lack ftruncate
|
|---|
| 10157 | # system call.
|
|---|
| 10158 | # -DV7 On Version 7 Unix (not
|
|---|
| 10159 | # tested in a long time).
|
|---|
| 10160 | # -DEMUL_OPEN3 If you lack a 3-argument version
|
|---|
| 10161 | # of open, and want to emulate it
|
|---|
| 10162 | # with system calls you do have.
|
|---|
| 10163 | # -DNO_OPEN3 If you lack the 3-argument open
|
|---|
| 10164 | # and want to disable the tar -k
|
|---|
| 10165 | # option instead of emulating open.
|
|---|
| 10166 | # -DXENIX If you have sys/inode.h
|
|---|
| 10167 | # and need it 94 to be included.
|
|---|
| 10168 |
|
|---|
| 10169 | DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
|
|---|
| 10170 | -DVPRINTF_MISSING -DBSD42
|
|---|
| 10171 | # Set this to rtapelib.o unless you defined NO_REMOTE,
|
|---|
| 10172 | # in which case make it empty.
|
|---|
| 10173 | RTAPELIB = rtapelib.o
|
|---|
| 10174 | LIBS =
|
|---|
| 10175 | DEF_AR_FILE = /dev/rmt8
|
|---|
| 10176 | DEFBLOCKING = 20
|
|---|
| 10177 |
|
|---|
| 10178 | @group
|
|---|
| 10179 | CDEBUG = -g
|
|---|
| 10180 | CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
|
|---|
| 10181 | -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
|
|---|
| 10182 | -DDEFBLOCKING=$(DEFBLOCKING)
|
|---|
| 10183 | LDFLAGS = -g
|
|---|
| 10184 | @end group
|
|---|
| 10185 |
|
|---|
| 10186 | @group
|
|---|
| 10187 | prefix = /usr/local
|
|---|
| 10188 | # Prefix for each installed program,
|
|---|
| 10189 | # normally empty or `g'.
|
|---|
| 10190 | binprefix =
|
|---|
| 10191 |
|
|---|
| 10192 | # The directory to install tar in.
|
|---|
| 10193 | bindir = $(prefix)/bin
|
|---|
| 10194 |
|
|---|
| 10195 | # The directory to install the info files in.
|
|---|
| 10196 | infodir = $(prefix)/info
|
|---|
| 10197 | @end group
|
|---|
| 10198 |
|
|---|
| 10199 | #### End of system configuration section. ####
|
|---|
| 10200 |
|
|---|
| 10201 | SRC1 = tar.c create.c extract.c buffer.c \
|
|---|
| 10202 | getoldopt.c update.c gnu.c mangle.c
|
|---|
| 10203 | SRC2 = version.c list.c names.c diffarch.c \
|
|---|
| 10204 | port.c wildmat.c getopt.c
|
|---|
| 10205 | SRC3 = getopt1.c regex.c getdate.y
|
|---|
| 10206 | SRCS = $(SRC1) $(SRC2) $(SRC3)
|
|---|
| 10207 | OBJ1 = tar.o create.o extract.o buffer.o \
|
|---|
| 10208 | getoldopt.o update.o gnu.o mangle.o
|
|---|
| 10209 | OBJ2 = version.o list.o names.o diffarch.o \
|
|---|
| 10210 | port.o wildmat.o getopt.o
|
|---|
| 10211 | OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
|
|---|
| 10212 | OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
|
|---|
| 10213 | @group
|
|---|
| 10214 | AUX = README COPYING ChangeLog Makefile.in \
|
|---|
| 10215 | makefile.pc configure configure.in \
|
|---|
| 10216 | tar.texinfo tar.info* texinfo.tex \
|
|---|
| 10217 | tar.h port.h open3.h getopt.h regex.h \
|
|---|
| 10218 | rmt.h rmt.c rtapelib.c alloca.c \
|
|---|
| 10219 | msd_dir.h msd_dir.c tcexparg.c \
|
|---|
| 10220 | level-0 level-1 backup-specs testpad.c
|
|---|
| 10221 | @end group
|
|---|
| 10222 |
|
|---|
| 10223 | all: tar rmt tar.info
|
|---|
| 10224 |
|
|---|
| 10225 | @group
|
|---|
| 10226 | tar: $(OBJS)
|
|---|
| 10227 | $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
|
|---|
| 10228 | @end group
|
|---|
| 10229 |
|
|---|
| 10230 | @group
|
|---|
| 10231 | rmt: rmt.c
|
|---|
| 10232 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
|
|---|
| 10233 | @end group
|
|---|
| 10234 |
|
|---|
| 10235 | @group
|
|---|
| 10236 | tar.info: tar.texinfo
|
|---|
| 10237 | makeinfo tar.texinfo
|
|---|
| 10238 | @end group
|
|---|
| 10239 |
|
|---|
| 10240 | @group
|
|---|
| 10241 | install: all
|
|---|
| 10242 | $(INSTALL) tar $(bindir)/$(binprefix)tar
|
|---|
| 10243 | -test ! -f rmt || $(INSTALL) rmt /etc/rmt
|
|---|
| 10244 | $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
|
|---|
| 10245 | @end group
|
|---|
| 10246 |
|
|---|
| 10247 | @group
|
|---|
| 10248 | $(OBJS): tar.h port.h testpad.h
|
|---|
| 10249 | regex.o buffer.o tar.o: regex.h
|
|---|
| 10250 | # getdate.y has 8 shift/reduce conflicts.
|
|---|
| 10251 | @end group
|
|---|
| 10252 |
|
|---|
| 10253 | @group
|
|---|
| 10254 | testpad.h: testpad
|
|---|
| 10255 | ./testpad
|
|---|
| 10256 | @end group
|
|---|
| 10257 |
|
|---|
| 10258 | @group
|
|---|
| 10259 | testpad: testpad.o
|
|---|
| 10260 | $(CC) -o $@@ testpad.o
|
|---|
| 10261 | @end group
|
|---|
| 10262 |
|
|---|
| 10263 | @group
|
|---|
| 10264 | TAGS: $(SRCS)
|
|---|
| 10265 | etags $(SRCS)
|
|---|
| 10266 | @end group
|
|---|
| 10267 |
|
|---|
| 10268 | @group
|
|---|
| 10269 | clean:
|
|---|
| 10270 | rm -f *.o tar rmt testpad testpad.h core
|
|---|
| 10271 | @end group
|
|---|
| 10272 |
|
|---|
| 10273 | @group
|
|---|
| 10274 | distclean: clean
|
|---|
| 10275 | rm -f TAGS Makefile config.status
|
|---|
| 10276 | @end group
|
|---|
| 10277 |
|
|---|
| 10278 | @group
|
|---|
| 10279 | realclean: distclean
|
|---|
| 10280 | rm -f tar.info*
|
|---|
| 10281 | @end group
|
|---|
| 10282 |
|
|---|
| 10283 | @group
|
|---|
| 10284 | shar: $(SRCS) $(AUX)
|
|---|
| 10285 | shar $(SRCS) $(AUX) | compress \
|
|---|
| 10286 | > tar-`sed -e '/version_string/!d' \
|
|---|
| 10287 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
|---|
| 10288 | -e q
|
|---|
| 10289 | version.c`.shar.Z
|
|---|
| 10290 | @end group
|
|---|
| 10291 |
|
|---|
| 10292 | @group
|
|---|
| 10293 | dist: $(SRCS) $(AUX)
|
|---|
| 10294 | echo tar-`sed \
|
|---|
| 10295 | -e '/version_string/!d' \
|
|---|
| 10296 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
|
|---|
| 10297 | -e q
|
|---|
| 10298 | version.c` > .fname
|
|---|
| 10299 | -rm -rf `cat .fname`
|
|---|
| 10300 | mkdir `cat .fname`
|
|---|
| 10301 | ln $(SRCS) $(AUX) `cat .fname`
|
|---|
| 10302 | tar chZf `cat .fname`.tar.Z `cat .fname`
|
|---|
| 10303 | -rm -rf `cat .fname` .fname
|
|---|
| 10304 | @end group
|
|---|
| 10305 |
|
|---|
| 10306 | @group
|
|---|
| 10307 | tar.zoo: $(SRCS) $(AUX)
|
|---|
| 10308 | -rm -rf tmp.dir
|
|---|
| 10309 | -mkdir tmp.dir
|
|---|
| 10310 | -rm tar.zoo
|
|---|
| 10311 | for X in $(SRCS) $(AUX) ; do \
|
|---|
| 10312 | echo $$X ; \
|
|---|
| 10313 | sed 's/$$/^M/' $$X \
|
|---|
| 10314 | > tmp.dir/$$X ; done
|
|---|
| 10315 | cd tmp.dir ; zoo aM ../tar.zoo *
|
|---|
| 10316 | -rm -rf tmp.dir
|
|---|
| 10317 | @end group
|
|---|
| 10318 | @end example
|
|---|
| 10319 |
|
|---|
| 10320 | @raisesections
|
|---|
| 10321 | @include fdl.texi
|
|---|
| 10322 | @lowersections
|
|---|
| 10323 |
|
|---|
| 10324 | @node Concept Index, Name Index, GNU Free Documentation License, Top
|
|---|
| 10325 | @unnumbered Index of Concepts
|
|---|
| 10326 |
|
|---|
| 10327 | @printindex cp
|
|---|
| 10328 |
|
|---|
| 10329 | @node Name Index, , Concept Index, Top
|
|---|
| 10330 | @unnumbered Index of Functions, Variables, & Directives
|
|---|
| 10331 |
|
|---|
| 10332 | @printindex fn
|
|---|
| 10333 |
|
|---|
| 10334 | @summarycontents
|
|---|
| 10335 | @contents
|
|---|
| 10336 | @bye
|
|---|