Cover image for Managing Projects with GNU Make, 3rd Edition

Book description

The utility simply known as make is one of the most enduring features of both Unix and other operating systems. First invented in the 1970s, make still turns up to this day as the central engine in most programming projects; it even builds the Linux kernel. In the third edition of the classic Managing Projects with GNU make, readers will learn why this utility continues to hold its top position in project build software, despite many younger competitors. The premise behind make is simple: after you change source files and want to rebuild your program or other output files, make checks timestamps to see what has changed and rebuilds just what you need, without wasting time rebuilding other files. But on top of this simple principle, make layers a rich collection of options that lets you manipulate multiple directories, build different versions of programs for different platforms, and customize your builds in other ways. This edition focuses on the GNU version of make, which has deservedly become the industry standard. GNU make contains powerful extensions that are explored in this book. It is also popular because it is free software and provides a version for almost every platform, including a version for Microsoft Windows as part of the free Cygwin project. Managing Projects with GNU make, 3rd Edition provides guidelines on meeting the needs of large, modern projects. Also added are a number of interesting advanced topics such as portability, parallelism, and use with Java. Robert Mecklenburg, author of the third edition, has used make for decades with a variety of platforms and languages. In this book he zealously lays forth how to get your builds to be as efficient as possible, reduce maintenance, avoid errors, and thoroughly understand what make is doing. Chapters on C++ and Java provide makefile entries optimized for projects in those languages. The author even includes a discussion of the makefile used to build the book.

Table of Contents

  1. Managing Projects with GNU Make, 3rd Edition
  2. A Note Regarding Supplemental Files
  3. Foreword
  4. Dedication
  5. Preface
    1. The Road to the Third Edition
    2. What's New in This Edition
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Comments and Questions
    6. Acknowledgments
  6. I. Basic Concepts
    1. 1. How to Write a Simple Makefile
      1. 1.1. Targets and Prerequisites
      2. 1.2. Dependency Checking
      3. 1.3. Minimizing Rebuilds
      4. 1.4. Invoking make
      5. 1.5. Basic Makefile Syntax
    2. 2. Rules
      1. 2.1. Explicit Rules
        1. 2.1.1. Wildcards
        2. 2.1.2. Phony Targets
        3. 2.1.3. Empty Targets
      2. 2.2. Variables
        1. 2.2.1. Automatic Variables
      3. 2.3. Finding Files with VPATH and vpath
      4. 2.4. Pattern Rules
        1. 2.4.1. The Patterns
        2. 2.4.2. Static Pattern Rules
        3. 2.4.3. Suffix Rules
      5. 2.5. The Implicit Rules Database
        1. 2.5.1. Working with Implicit Rules
        2. 2.5.2. Rule Structure
        3. 2.5.3. Implicit Rules for Source Control
        4. 2.5.4. A Simple Help Command
      6. 2.6. Special Targets
      7. 2.7. Automatic Dependency Generation
      8. 2.8. Managing Libraries
        1. 2.8.1. Creating and Updating Libraries
        2. 2.8.2. Using Libraries as Prerequisites
        3. 2.8.3. Double-Colon Rules
    3. 3. Variables and Macros
      1. 3.1. What Variables Are Used For
      2. 3.2. Variable Types
        1. 3.2.1. Other Types of Assignment
      3. 3.3. Macros
      4. 3.4. When Variables Are Expanded
      5. 3.5. Target- and Pattern-Specific Variables
      6. 3.6. Where Variables Come From
      7. 3.7. Conditional and include Processing
        1. 3.7.1. The include Directive
        2. 3.7.2. include and Dependencies
      8. 3.8. Standard make Variables
    4. 4. Functions
      1. 4.1. User-Defined Functions
      2. 4.2. Built-in Functions
        1. 4.2.1. String Functions
        2. 4.2.2. Important Miscellaneous Functions
        3. 4.2.3. Filename Functions
        4. 4.2.4. Flow Control
          1. 4.2.4.1. Style note concerning variables and parentheses
        5. 4.2.5. Less Important Miscellaneous Functions
      3. 4.3. Advanced User-Defined Functions
        1. 4.3.1. eval and value
        2. 4.3.2. Hooking Functions
        3. 4.3.3. Passing Parameters
    5. 5. Commands
      1. 5.1. Parsing Commands
        1. 5.1.1. Continuing Long Commands
        2. 5.1.2. Command Modifiers
        3. 5.1.3. Errors and Interrupts
          1. 5.1.3.1. Deleting and preserving target files
      2. 5.2. Which Shell to Use
      3. 5.3. Empty Commands
      4. 5.4. Command Environment
      5. 5.5. Evaluating Commands
      6. 5.6. Command-Line Limits
  7. II. Advanced and Specialized Topics
    1. 6. Managing Large Projects
      1. 6.1. Recursive make
        1. 6.1.1. Command-Line Options
        2. 6.1.2. Passing Variables
        3. 6.1.3. Error Handling
        4. 6.1.4. Building Other Targets
        5. 6.1.5. Cross-Makefile Dependencies
        6. 6.1.6. Avoiding Duplicate Code
      2. 6.2. Nonrecursive make
      3. 6.3. Components of Large Systems
        1. 6.3.1. Requirements
      4. 6.4. Filesystem Layout
      5. 6.5. Automating Builds and Testing
    2. 7. Portable Makefiles
      1. 7.1. Portability Issues
      2. 7.2. Cygwin
        1. 7.2.1. Line Termination
        2. 7.2.2. Filesystem
        3. 7.2.3. Program Conflicts
      3. 7.3. Managing Programs and Files
        1. 7.3.1. Source Tree Layout
      4. 7.4. Working with Nonportable Tools
        1. 7.4.1. A Standard Shell
      5. 7.5. Automake
    3. 8. C and C++
      1. 8.1. Separating Source and Binary
        1. 8.1.1. The Easy Way
        2. 8.1.2. The Hard Way
      2. 8.2. Read-Only Source
      3. 8.3. Dependency Generation
        1. 8.3.1. Tromey's Way
        2. 8.3.2. makedepend Programs
      4. 8.4. Supporting Multiple Binary Trees
      5. 8.5. Partial Source Trees
      6. 8.6. Reference Builds, Libraries, and Installers
    4. 9. Java
      1. 9.1. Alternatives to make
        1. 9.1.1. Ant
        2. 9.1.2. IDEs
      2. 9.2. A Generic Java Makefile
      3. 9.3. Compiling Java
        1. 9.3.1. The Fast Approach: All-in-One Compile
        2. 9.3.2. Compiling with Dependencies
        3. 9.3.3. Setting CLASSPATH
      4. 9.4. Managing Jars
      5. 9.5. Reference Trees and Third-Party Jars
      6. 9.6. Enterprise JavaBeans
    5. 10. Improving the Performance of make
      1. 10.1. Benchmarking
      2. 10.2. Identifying and Handling Bottlenecks
        1. 10.2.1. Simple Variables Versus Recursive
        2. 10.2.2. Disabling @
        3. 10.2.3. Lazy Initialization
      3. 10.3. Parallel make
      4. 10.4. Distributed make
    6. 11. Example Makefiles
      1. 11.1. The Book Makefile
        1. 11.1.1. Managing Examples
        2. 11.1.2. XML Preprocessing
        3. 11.1.3. Generating Output
        4. 11.1.4. Validating the Source
      2. 11.2. The Linux Kernel Makefile
        1. 11.2.1. Command-Line Options
        2. 11.2.2. Configuration Versus Building
        3. 11.2.3. Managing Command Echo
        4. 11.2.4. User-Defined Functions
    7. 12. Debugging Makefiles
      1. 12.1. Debugging Features of make
        1. 12.1.1. Command-Line Options
          1. 12.1.1.1. —just-print
          2. 12.1.1.2. —print-data-base
          3. 12.1.1.3. —warn-undefined-variables
        2. 12.1.2. The —debug Option
      2. 12.2. Writing Code for Debugging
        1. 12.2.1. Good Coding Practices
        2. 12.2.2. Defensive Coding
        3. 12.2.3. Debugging Techniques
      3. 12.3. Common Error Messages
        1. 12.3.1. Syntax Errors
          1. 12.3.1.1. missing separator
          2. 12.3.1.2. commands commence before first target
          3. 12.3.1.3. unterminated variable reference
        2. 12.3.2. Errors in Command Scripts
        3. 12.3.3. No Rule to Make Target
        4. 12.3.4. Overriding Commands for Target
  8. III. Appendixes
    1. A. Running make
    2. B. The Outer Limits
      1. B.1. Data Structures
      2. B.2. Arithmetic
    3. C. GNU Free Documentation License—GNU Project—Free Software Foundation (FSF)
      1. C.1. 0. PREAMBLE
      2. C.2. 1. APPLICABILITY AND DEFINITIONS
      3. C.3. 2. VERBATIM COPYING
      4. C.4. 3. COPYING IN QUANTITY
      5. C.5. 4. MODIFICATIONS
      6. C.6. 5. COMBINING DOCUMENTS
      7. C.7. 6. COLLECTIONS OF DOCUMENTS
      8. C.8. 7. AGGREGATION WITH INDEPENDENT WORKS
      9. C.9. 8. TRANSLATION
      10. C.10. 9. TERMINATION
      11. C.11. 10. FUTURE REVISIONS OF THIS LICENSE
  9. Index
  10. About the Author
  11. Colophon
  12. Copyright