GNU Make a Program for Directing Recompilation Edition 0.48, for Make Version 3.73 Beta
Total Page:16
File Type:pdf, Size:1020Kb
GNU Make A Program for Directing Recompilation Edition 0.48, for make Version 3.73 Beta. April 1995 Richard M. Stallman and Roland McGrath Copyright c 1988, '89, '90, '91, '92, '93, '94, '95 Free Software Founda- tion, Inc. Published by the Free Software Foundation 675 Massachusetts Avenue, Cambridge, MA 02139 USA Printed copies are available for $20 each. ISBN 1-882114-50-7 Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modi®ed versions of this manual under the conditions for verbatim copying, provided that the en- tire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modi®ed versions, except that this permission notice may be stated in a translation ap- proved by the Free Software Foundation. Cover art by Etienne Suvasa. GNU Make 1 Overview of make ............................. 517 1.1 How to Read This Manual ............................. 517 1.2 Problems and Bugs .................................... 518 2 An Introduction to Make®les ............... 519 2.1 What a Rule Looks Like ............................... 519 2.2 A Simple Make®le ..................................... 520 2.3 How make Processes a Make®le ........................ 521 2.4 Variables Make Make®les Simpler ..................... 522 2.5 Letting make Deduce the Commands ................... 523 2.6 Another Style of Make®le .............................. 524 2.7 Rules for Cleaning the Directory ....................... 525 3 Writing Make®les ............................ 527 3.1 What Make®les Contain ............................... 527 3.2 What Name to Give Your Make®le ..................... 528 3.3 Including Other Make®les ............................. 528 3.4 The Variable MAKEFILES................................ 530 3.5 How Make®les Are Remade ............................ 530 3.6 Overriding Part of Another Make®le ................... 531 4 Writing Rules ................................ 533 4.1 Rule Syntax ............................................ 533 4.2 Using Wildcard Characters in File Names ............. 534 4.2.1 Wildcard Examples ........................... 534 4.2.2 Pitfalls of Using Wildcards ................... 535 4.2.3 The Function wildcard ....................... 536 4.3 Searching Directories for Dependencies................ 536 4.3.1 VPATH: Search Path for All Dependencies ..... 537 make 4.3.2 The vpath Directive .......................... 537 4.3.3 Writing Shell Commands with Directory Search .................................................. 539 4.3.4 Directory Search and Implicit Rules .......... 539 4.3.5 Directory Search for Link Libraries .......... 539 4.4 Phony Targets ......................................... 540 4.5 Rules without Commands or Dependencies ............ 542 4.6 Empty Target Files to Record Events .................. 542 4.7 Special Built-in Target Names ......................... 543 4.8 Multiple Targets in a Rule ............................. 544 4.9 Multiple Rules for One Target ......................... 545 cygnus support 513 GNU make 4.10 Static Pattern Rules .................................. 546 4.10.1 Syntax of Static Pattern Rules .............. 546 4.10.2 Static Pattern Rules versus Implicit Rules .. 548 4.11 Double-Colon Rules ................................... 548 4.12 Generating Dependencies Automatically ............. 549 5 Writing the Commands in Rules ............ 553 5.1 Command Echoing ..................................... 553 5.2 Command Execution ................................... 554 5.3 Parallel Execution ..................................... 554 5.4 Errors in Commands ................................... 556 5.5 Interrupting or Killing make ........................... 557 5.6 Recursive Use of make .................................. 557 5.6.1 How the MAKE Variable Works ................ 558 5.6.2 Communicating Variables to a Sub-make ..... 559 5.6.3 Communicating Options to a Sub-make ....... 561 5.6.4 The `--print-directory' Option ............. 563 5.7 De®ning Canned Command Sequences ................ 563 5.8 Using Empty Commands .............................. 564 6 How to Use Variables ........................ 567 6.1 Basics of Variable References .......................... 567 6.2 The Two Flavors of Variables .......................... 568 6.3 Advanced Features for Reference to Variables ......... 570 6.3.1 Substitution References ...................... 571 6.3.2 Computed Variable Names ................... 571 6.4 How Variables Get Their Values ....................... 574 6.5 Setting Variables....................................... 574 6.6 Appending More Text to Variables ..................... 575 6.7 The override Directive ................................ 577 6.8 De®ning Variables Verbatim ........................... 577 6.9 Variables from the Environment ....................... 578 7 Conditional Parts of Make®les.............. 581 7.1 Example of a Conditional .............................. 581 7.2 Syntax of Conditionals ................................. 582 7.3 Conditionals that Test Flags ........................... 585 8 Functions for Transforming Text ........... 587 8.1 Function Call Syntax .................................. 587 8.2 Functions for String Substitution and Analysis ........ 588 8.3 Functions for File Names .............................. 591 8.4 The foreach Function ................................. 594 514 5 March 1997 8.5 The origin Function................................... 595 8.6 The shell Function .................................... 597 9 How to Run make ............................. 599 9.1 Arguments to Specify the Make®le..................... 599 9.2 Arguments to Specify the Goals ........................ 599 9.3 Instead of Executing the Commands ................... 601 9.4 Avoiding Recompilation of Some Files ................. 603 9.5 Overriding Variables ................................... 603 9.6 Testing the Compilation of a Program ................. 604 9.7 Summary of Options ................................... 605 10 Using Implicit Rules........................ 611 10.1 Using Implicit Rules .................................. 611 10.2 Catalogue of Implicit Rules ........................... 612 10.3 Variables Used by Implicit Rules ..................... 616 10.4 Chains of Implicit Rules .............................. 618 10.5 De®ning and Rede®ning Pattern Rules ............... 619 10.5.1 Introduction to Pattern Rules ............... 620 10.5.2 Pattern Rule Examples ...................... 621 10.5.3 Automatic Variables ......................... 622 10.5.4 How Patterns Match ........................ 624 10.5.5 Match-Anything Pattern Rules .............. 625 10.5.6 Canceling Implicit Rules .................... 626 10.6 De®ning Last-Resort Default Rules................... 626 10.7 Old-Fashioned Suf®x Rules ........................... 627 10.8 Implicit Rule Search Algorithm....................... 629 11 Using make to Update Archive Files ....... 631 11.1 Archive Members as Targets.......................... 631 11.2 Implicit Rule for Archive Member Targets ............ 631 11.2.1 Updating Archive Symbol Directories ....... 632 11.3 Dangers When Using Archives ....................... 633 make 11.4 Suf®x Rules for Archive Files ......................... 633 12 Features of GNU make ...................... 635 13 Incompatibilities and Missing Features .. 639 cygnus support 515 GNU make 14 Make®le Conventions ...................... 641 14.1 General Conventions for Make®les ................... 641 14.2 Utilities in Make®les ................................. 642 14.3 Standard Targets for Users ........................... 642 14.4 Variables for Specifying Commands .................. 646 14.5 Variables for Installation Directories ................. 647 Appendix A Quick Reference ................. 653 Appendix B Complex Make®le Example ..... 659 Index of Concepts ............................... 665 Index of Functions, Variables, & Directives ... 673 516 5 March 1997 Chapter 1: Overview of make 1 Overview of make The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them. This manual describes gnu make, which was implemented by Richard Stallman and Roland McGrath. gnu make conforms to section 6.2 of IEEE Standard 1003.2-1992 (POSIX.2). Our examples show C programs, since they are most common, but you can use make with any programming language whose compiler can be run with a shell command. Indeed, make is not limited to programs. You can use it to describe any task where some ®les must be updated automatically from others whenever the others change. To prepare to use make, you must write a ®le called the make®le that describes the relationships among ®les in your program and provides commands for updating each ®le. In a program, typically, the executable ®le is updated from object ®les, which are in turn made by compiling source ®les. Once a suitable make®le exists, each time you change some source ®les, this simple shell command: make suf®ces to perform all necessary recompilations. The make program uses the make®le data base and the last-modi®cation times of the ®les to decide which of the ®les need to be updated. For each of those ®les, it issues the commands recorded