ebook img

The GNU Make Book PDF

256 Pages·2.701 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The GNU Make Book

TT HHEE GGNNUU MMAAKKEE,, Technical review PPRROOBBLLEE MM by Paul Smith, SSOOLLVVEEDD.. maintainer of GNU make GG NNUU MM AA KKEE TT HH BB OOOO KK EE GNU make is the most widely used build automation • Handle automatic dependency generation, tool, but it can be intimidating for new users and rebuilding, and non-recursive make GG its terse language can be tough to parse for even • Modify the GNU make source and take advantage experienced programmers. Those who run into dif- of the GNU Make Standard Library NN ficulties face a long, involved struggle, often leaving J O H N G R A H A M - C U M MI NG unsolved problems behind and GNU make’s vast • Create makefile assertions and debug makefiles UU potential untapped. GNU make is known for being tricky to use, but it The GNU Make Book demystifies GNU make and doesn’t have to be. Seasoned users and newbies alike shows you how to use its best features. You’ll find will find The GNU Make Book to be an indispensable MM a fast, thorough rundown of the basics of variables, guide to this indispensable tool. rules, targets, and makefiles. Learn how to fix waste- AA fully long build times and other common problems, ABOUT THE AUTHOR and gain insight into more advanced capabilities, KK John Graham-Cumming is a longtime GNU make such as complex pattern rules. With this utterly prag- expert. He wrote the acclaimed machine learning– matic manual and cookbook, you’ll make rapid based POPFile email filter and successfully petitioned EE progress toward becoming a more effective user. the British government to apologize for its treatment of You’ll also learn how to: Alan Turing. He holds a doctorate in computer security BB from Oxford University and works at CloudFlare. • Master user-defined functions, variables, and path handling OO • Weigh the pitfalls and advantages of GNU make parallelization OO KK THE FINEST IN GEEK ENTERTAINMENT™ CG www.nostarch.com UMRA $34.95 ($40.95 CDN) MIHA NM COMPUTERS/PROSHELVE IN: G- G RAM M IN G Safety Area: All Text, Logos & Barcode should remain inside the Pink Dotted Lines Bleed Area: All Backgrounds should extend to, but not past, the Blue Dotted Lines The GNU Make Book T H e G N U M a k e B o o k by John Graham-Cumming San Francisco The GNU Make Book. Copyright © 2015 by John Graham-Cumming. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed on demand in USA ISBN-10: 1-59327-649-4 ISBN-13: 978-1-59327-649-2 Publisher: William Pollock Production Editor: Alison Law Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editors: Greg Poulos and Leslie Shen Technical Reviewer: Paul Smith Copyeditor: Anne Marie Walker Compositor: Susan Glinert Stevens Proofreader: James Fraleigh Indexer: Nancy Guenther For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; [email protected] www.nostarch.com Library of Congress Cataloging-in-Publication Data: Graham-Cumming, John. The GNU make book / by John Graham-Cumming. -- 1st edition. pages cm Includes index. Summary: "Covers GNU Make basics through advanced topics, including: user-defined functions, macros, and path handling; creating makefile assertions and debugging makefiles; parallelization; automatic dependency generation, rebuilding targets, and non-recursive Make; and using the GNU Make Standard Library"-- Provided by publisher. ISBN 978-1-59327-649-2 -- ISBN 1-59327-649-4 1. GNU Emacs. 2. Text editors (Computer programs) 3. Make (Computer file) I. Title. QA76.76.T49G725 2015 005.13--dc23 2015007254 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. about the author John Graham-Cumming is a longtime GNU make expert. He wrote the acclaimed machine learning–based POPFile email filter and successfully petitioned the British government to apologize for its treatment of Alan Turing. He holds a doctorate in computer security from Oxford University and works at CloudFlare. about the Technical Reviewer Paul Smith has been the Free Software Foundation’s GNU make project maintainer since 1996. He’s been using and contributing to free software since the 1980s and to GNU/Linux since 1993. Professionally, he writes networking and database system software. Personally, he enjoys biking and scuba diving with his wife and kids. Brief Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Chapter 1: The Basics Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 2: Makefile Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Chapter 3: Building and Rebuilding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Chapter 4: Pitfalls and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Chapter 5: Pushing the Envelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Chapter 6: The GNU Make Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Contents in De tail Preface xv 1 The Basics revisiTed 1 Getting Environment Variables into GNU make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Setting Variables from Outside the Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Environment Used by Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The $(shell) Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Target-Specific and Pattern-Specific Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Target-Specific Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Pattern-Specific Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Version Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 MAKE_VERSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 .FEATURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Detecting $(eval) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Using Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Undefined Variables in Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Consistent Truth Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Logical Operations Using Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 User-Defined Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Built-in Logical Operators (GNU make 3 .81 and Later) . . . . . . . . . . . . . . . . . 20 Command Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Delayed Variable Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Simple List Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Argument-Handling Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Calling Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Recent GNU make Versions: 3 .81, 3 .82, and 4 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 What’s New in GNU make 3 .81 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 What’s New in GNU make 3 .82 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 What’s New in GNU make 4 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 What’s New in GNU make 4 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 Makefile deBugging 43 Printing the Value of a Makefile Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Dumping Every Makefile Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Tracing Variable Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Tracing Variable Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 How the Variable Tracer Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Tracing Rule Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 The SHELL Hack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 An Even Smarter SHELL Hack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 GNU make 4 .0 Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Makefile Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 assert_exists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 assert_target_directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 An Interactive GNU make Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Debugger in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Breakpoints in Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Breakpoints in Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Debugger Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Dynamic Breakpoints in the GNU make Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Dynamic Breakpoints in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Easy Part . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 The Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Rocket Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 An Introduction to remake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Just Print and Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Targets, Macro Values, and Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3 Building and reBuilding 77 Rebuilding When CPPFLAGS Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 An Example Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Changing Our Example Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 How Signature Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Rebuilding When a File’s Checksum Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 An Example Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Digesting File Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The Modified Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The Hack in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Improving the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Automatic Dependency Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 An Example Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 makedepend and make depend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Automating makedepend and Removing make depend . . . . . . . . . . . . . . . . . 89 Making Deleted Files Disappear from Dependencies . . . . . . . . . . . . . . . . . . . 90 Doing Away with makedepend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Using gcc -MP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Atomic Rules in GNU make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 What Not to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Using Pattern Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Using a Sentinel File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Painless Non-recursive make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 A Simple Recursive Make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 A Flexible Non-recursive make System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Using the Non-recursive make System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 What About Submodules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 x Contents in Detail

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.