home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / gnumake / info / make.i01
Text File  |  1992-03-14  |  51KB  |  1,062 lines

  1. Info file make.info, produced by Makeinfo, -*- Text -*- from input
  2. file make.texinfo.
  3.  
  4.    This file documents the GNU Make utility.
  5.  
  6.    Copyright (C) 1988-1991 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of
  9. this manual provided the copyright notice and this permission notice
  10. are preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the section entitled "GNU General Public License" is included
  15. exactly as in the original, and provided that the entire resulting
  16. derived work is distributed under the terms of a permission notice
  17. identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the text of the translations of the section
  22. entitled "GNU General Public License" must be approved for accuracy by
  23. the Foundation.
  24.  
  25. 
  26. File: make.info,  Node: Top,  Next: Overview,  Up: (dir)
  27.  
  28.    The GNU `make' utility determines automatically which pieces of a
  29. large program need to be recompiled, and issues the commands to
  30. recompile them.  This manual describes it and contains the following
  31. chapters:
  32.  
  33. * Menu:
  34.  
  35. * Overview::    Introducing `make'.
  36. * Copying::     Copying conditions for GNU `make'.
  37. * Bugs::        If you have problems, or think you've found a bug.
  38. * Simple::      A simple example explained.
  39. * Makefiles::   The data base contains rules and variable definitions.
  40. * Rules::       A rule says how and when to remake one file.
  41. * Commands::    A rule contains shell commands that say how to remake.
  42. * Variables::   A variable holds a text string for substitution into rules.
  43. * Conditionals::Makefiles that do one thing or another depending on
  44.                  variable values.
  45. * Functions::   Functions can do text-processing within `make'.
  46.  
  47. * Running::     How to run `make'; how you can adjust the way
  48.                  `make' uses the makefile.
  49.  
  50. * Implicit::    Implicit rules take over if the makefile doesn't say
  51.                  how a file is to be remade.
  52. * Archives::    How to use `make' to update archive files.
  53.  
  54. * Features::    GNU `make''s advanced features and how GNU `make'
  55.                  relates to other versions of `make'.
  56. * Missing::     Features of other `make's not supported
  57.                  by GNU `make'.
  58.  
  59. * Concept Index::Index of cross-references to where concepts are discussed.
  60. * Name Index::  Index of cross-references for names of `make''s
  61.                  variables, functions, special targets and directives.
  62.  
  63. 
  64. File: make.info,  Node: Overview,  Next: Copying,  Prev: Top,  Up: Top
  65.  
  66. Overview of `make'
  67. ******************
  68.  
  69.    The purpose of the `make' utility is to determine automatically
  70. which pieces of a large program need to be recompiled, and issue the
  71. commands to recompile them.  This manual describes the GNU
  72. implementation of `make', which was implemented by Richard Stallman
  73. and Roland McGrath.
  74.  
  75.    Our examples show C programs, since they are most common, but you
  76. can use `make' with any programming language whose compiler can be run
  77. with a shell command.  In fact, `make' is not limited to programs. 
  78. You can use it to describe any task where some files must be updated
  79. automatically from others whenever the others change.
  80.  
  81.    To prepare to use `make', you must write a file called the
  82. "makefile" that describes the relationships among files in your
  83. program, and the states the commands for updating each file.  In a
  84. program, typically the executable file is updated from object files,
  85. which are in turn made by compiling source files.
  86.  
  87.    Once a suitable makefile exists, each time you change some source
  88. files, this simple shell command:
  89.  
  90.      make
  91.  
  92. suffices to perform all necessary recompilations.  The `make' program
  93. uses the makefile data base and the last-modification times of the
  94. files to decide which of the files need to be updated.  For each of
  95. those files, it issues the commands recorded in the data base.
  96.  
  97. 
  98. File: make.info,  Node: Copying,  Next: Bugs,  Prev: Overview,  Up: Top
  99.  
  100. GNU GENERAL PUBLIC LICENSE
  101. **************************
  102.  
  103.                              Version 2, June 1991
  104.  
  105.      Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  106.      675 Mass Ave, Cambridge, MA 02139, USA
  107.      
  108.      Everyone is permitted to copy and distribute verbatim copies
  109.      of this license document, but changing it is not allowed.
  110.  
  111. Preamble
  112. ========
  113.  
  114.    The licenses for most software are designed to take away your
  115. freedom to share and change it.  By contrast, the GNU General Public
  116. License is intended to guarantee your freedom to share and change free
  117. software--to make sure the software is free for all its users.  This
  118. General Public License applies to most of the Free Software
  119. Foundation's software and to any other program whose authors commit to
  120. using it.  (Some other Free Software Foundation software is covered by
  121. the GNU Library General Public License instead.)  You can apply it to
  122. your programs, too.
  123.  
  124.    When we speak of free software, we are referring to freedom, not
  125. price.  Our General Public Licenses are designed to make sure that you
  126. have the freedom to distribute copies of free software (and charge for
  127. this service if you wish), that you receive source code or can get it
  128. if you want it, that you can change the software or use pieces of it
  129. in new free programs; and that you know you can do these things.
  130.  
  131.    To protect your rights, we need to make restrictions that forbid
  132. anyone to deny you these rights or to ask you to surrender the rights. 
  133. These restrictions translate to certain responsibilities for you if you
  134. distribute copies of the software, or if you modify it.
  135.  
  136.    For example, if you distribute copies of such a program, whether
  137. gratis or for a fee, you must give the recipients all the rights that
  138. you have.  You must make sure that they, too, receive or can get the
  139. source code.  And you must show them these terms so they know their
  140. rights.
  141.  
  142.    We protect your rights with two steps: (1) copyright the software,
  143. and (2) offer you this license which gives you legal permission to
  144. copy, distribute and/or modify the software.
  145.  
  146.    Also, for each author's protection and ours, we want to make certain
  147. that everyone understands that there is no warranty for this free
  148. software.  If the software is modified by someone else and passed on,
  149. we want its recipients to know that what they have is not the
  150. original, so that any problems introduced by others will not reflect
  151. on the original authors' reputations.
  152.  
  153.    Finally, any free program is threatened constantly by software
  154. patents.  We wish to avoid the danger that redistributors of a free
  155. program will individually obtain patent licenses, in effect making the
  156. program proprietary.  To prevent this, we have made it clear that any
  157. patent must be licensed for everyone's free use or not licensed at all.
  158.  
  159.    The precise terms and conditions for copying, distribution and
  160. modification follow.
  161.  
  162.        TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  163.  
  164.   1. This License applies to any program or other work which contains
  165.      a notice placed by the copyright holder saying it may be
  166.      distributed under the terms of this General Public License.  The
  167.      "Program", below, refers to any such program or work, and a "work
  168.      based on the Program" means either the Program or any derivative
  169.      work under copyright law: that is to say, a work containing the
  170.      Program or a portion of it, either verbatim or with modifications
  171.      and/or translated into another language.  (Hereinafter,
  172.      translation is included without limitation in the term
  173.      "modification".)  Each licensee is addressed as "you".
  174.  
  175.         Activities other than copying, distribution and modification
  176.      are not covered by this License; they are outside its scope.  The
  177.      act of running the Program is not restricted, and the output from
  178.      the Program is covered only if its contents constitute a work
  179.      based on the Program (independent of having been made by running
  180.      the Program).  Whether that is true depends on what the Program
  181.      does.
  182.  
  183.   2. You may copy and distribute verbatim copies of the Program's
  184.      source code as you receive it, in any medium, provided that you
  185.      conspicuously and appropriately publish on each copy an
  186.      appropriate copyright notice and disclaimer of warranty; keep
  187.      intact all the notices that refer to this License and to the
  188.      absence of any warranty; and give any other recipients of the
  189.      Program a copy of this License along with the Program.
  190.  
  191.         You may charge a fee for the physical act of transferring a
  192.      copy, and you may at your option offer warranty protection in
  193.      exchange for a fee.
  194.  
  195.   3. You may modify your copy or copies of the Program or any portion
  196.      of it, thus forming a work based on the Program, and copy and
  197.      distribute such modifications or work under the terms of Section 1
  198.      above, provided that you also meet all of these conditions:
  199.  
  200.        a) You must cause the modified files to carry prominent notices
  201.           stating that you changed the files and the date of any
  202.           change.
  203.  
  204.        b) You must cause any work that you distribute or publish, that
  205.           in whole or in part contains or is derived from the Program
  206.           or any part thereof, to be licensed as a whole at no charge
  207.           to all third parties under the terms of this License.
  208.  
  209.        c) If the modified program normally reads commands interactively
  210.           when run, you must cause it, when started running for such
  211.           interactive use in the most ordinary way, to print or
  212.           display an announcement including an appropriate copyright
  213.           notice and a notice that there is no warranty (or else,
  214.           saying that you provide a warranty) and that users may
  215.           redistribute the program under these conditions, and telling
  216.           the user how to view a copy of this License.  (Exception: if
  217.           the Program itself is interactive but does not normally
  218.           print such an announcement, your work based on the Program
  219.           is not required to print an announcement.)
  220.  
  221.              These requirements apply to the modified work as a whole.  If
  222.      identifiable sections of that work are not derived from the
  223.      Program, and can be reasonably considered independent and
  224.      separate works in themselves, then this License, and its terms,
  225.      do not apply to those sections when you distribute them as
  226.      separate works.  But when you distribute the same sections as
  227.      part of a whole which is a work based on the Program, the
  228.      distribution of the whole must be on the terms of this License,
  229.      whose permissions for other licensees extend to the entire whole,
  230.      and thus to each and every part regardless of who wrote it.
  231.  
  232.         Thus, it is not the intent of this section to claim rights or
  233.      contest your rights to work written entirely by you; rather, the
  234.      intent is to exercise the right to control the distribution of
  235.      derivative or collective works based on the Program.
  236.  
  237.         In addition, mere aggregation of another work not based on the
  238.      Program with the Program (or with a work based on the Program) on
  239.      a volume of a storage or distribution medium does not bring the
  240.      other work under the scope of this License.
  241.  
  242.   4. You may copy and distribute the Program (or a work based on it,
  243.      under Section 2) in object code or executable form under the
  244.      terms of Sections 1 and 2 above provided that you also do one of
  245.      the following:
  246.  
  247.        a) Accompany it with the complete corresponding machine-readable
  248.           source code, which must be distributed under the terms of
  249.           Sections 1 and 2 above on a medium customarily used for
  250.           software interchange; or,
  251.  
  252.        b) Accompany it with a written offer, valid for at least three
  253.           years, to give any third party, for a charge no more than
  254.           your cost of physically performing source distribution, a
  255.           complete machine-readable copy of the corresponding source
  256.           code, to be distributed under the terms of Sections 1 and 2
  257.           above on a medium customarily used for software interchange;
  258.           or,
  259.  
  260.        c) Accompany it with the information you received as to the
  261.           offer to distribute corresponding source code.  (This
  262.           alternative is allowed only for noncommercial distribution
  263.           and only if you received the program in object code or
  264.           executable form with such an offer, in accord with
  265.           Subsection b above.)
  266.  
  267.              The source code for a work means the preferred form of the
  268.      work for making modifications to it.  For an executable work,
  269.      complete source code means all the source code for all modules it
  270.      contains, plus any associated interface definition files, plus
  271.      the scripts used to control compilation and installation of the
  272.      executable.  However, as a special exception, the source code
  273.      distributed need not include anything that is normally
  274.      distributed (in either source or binary form) with the major
  275.      components (compiler, kernel, and so on) of the operating system
  276.      on which the executable runs, unless that component itself
  277.      accompanies the executable.
  278.  
  279.         If distribution of executable or object code is made by
  280.      offering access to copy from a designated place, then offering
  281.      equivalent access to copy the source code from the same place
  282.      counts as distribution of the source code, even though third
  283.      parties are not compelled to copy the source along with the
  284.      object code.
  285.  
  286.   5. You may not copy, modify, sublicense, or distribute the Program
  287.      except as expressly provided under this License.  Any attempt
  288.      otherwise to copy, modify, sublicense or distribute the Program is
  289.      void, and will automatically terminate your rights under this
  290.      License.  However, parties who have received copies, or rights,
  291.      from you under this License will not have their licenses
  292.      terminated so long as such parties remain in full compliance.
  293.  
  294.   6. You are not required to accept this License, since you have not
  295.      signed it.  However, nothing else grants you permission to modify
  296.      or distribute the Program or its derivative works.  These actions
  297.      are prohibited by law if you do not accept this License. 
  298.      Therefore, by modifying or distributing the Program (or any work
  299.      based on the Program), you indicate your acceptance of this
  300.      License to do so, and all its terms and conditions for copying,
  301.      distributing or modifying the Program or works based on it.
  302.  
  303.   7. Each time you redistribute the Program (or any work based on the
  304.      Program), the recipient automatically receives a license from the
  305.      original licensor to copy, distribute or modify the Program
  306.      subject to these terms and conditions.  You may not impose any
  307.      further restrictions on the recipients' exercise of the rights
  308.      granted herein.  You are not responsible for enforcing compliance
  309.      by third parties to this License.
  310.  
  311.   8. If, as a consequence of a court judgment or allegation of patent
  312.      infringement or for any other reason (not limited to patent
  313.      issues), conditions are imposed on you (whether by court order,
  314.      agreement or otherwise) that contradict the conditions of this
  315.      License, they do not excuse you from the conditions of this
  316.      License.  If you cannot distribute so as to satisfy
  317.      simultaneously your obligations under this License and any other
  318.      pertinent obligations, then as a consequence you may not
  319.      distribute the Program at all.  For example, if a patent license
  320.      would not permit royalty-free redistribution of the Program by
  321.      all those who receive copies directly or indirectly through you,
  322.      then the only way you could satisfy both it and this License
  323.      would be to refrain entirely from distribution of the Program.
  324.  
  325.         If any portion of this section is held invalid or
  326.      unenforceable under any particular circumstance, the balance of
  327.      the section is intended to apply and the section as a whole is
  328.      intended to apply in other circumstances.
  329.  
  330.         It is not the purpose of this section to induce you to
  331.      infringe any patents or other property right claims or to contest
  332.      validity of any such claims; this section has the sole purpose of
  333.      protecting the integrity of the free software distribution
  334.      system, which is implemented by public license practices.  Many
  335.      people have made generous contributions to the wide range of
  336.      software distributed through that system in reliance on
  337.      consistent application of that system; it is up to the
  338.      author/donor to decide if he or she is willing to distribute
  339.      software through any other system and a licensee cannot impose
  340.      that choice.
  341.  
  342.         This section is intended to make thoroughly clear what is
  343.      believed to be a consequence of the rest of this License.
  344.  
  345.   9. If the distribution and/or use of the Program is restricted in
  346.      certain countries either by patents or by copyrighted interfaces,
  347.      the original copyright holder who places the Program under this
  348.      License may add an explicit geographical distribution limitation
  349.      excluding those countries, so that distribution is permitted only
  350.      in or among countries not thus excluded.  In such case, this
  351.      License incorporates the limitation as if written in the body of
  352.      this License.
  353.  
  354.  10. The Free Software Foundation may publish revised and/or new
  355.      versions of the General Public License from time to time.  Such
  356.      new versions will be similar in spirit to the present version,
  357.      but may differ in detail to address new problems or concerns.
  358.  
  359.         Each version is given a distinguishing version number.  If the
  360.      Program specifies a version number of this License which applies
  361.      to it and "any later version", you have the option of following
  362.      the terms and conditions either of that version or of any later
  363.      version published by the Free Software Foundation.  If the
  364.      Program does not specify a version number of this License, you
  365.      may choose any version ever published by the Free Software
  366.      Foundation.
  367.  
  368.  11. If you wish to incorporate parts of the Program into other free
  369.      programs whose distribution conditions are different, write to
  370.      the author to ask for permission.  For software which is
  371.      copyrighted by the Free Software Foundation, write to the Free
  372.      Software Foundation; we sometimes make exceptions for this.  Our
  373.      decision will be guided by the two goals of preserving the free
  374.      status of all derivatives of our free software and of promoting
  375.      the sharing and reuse of software generally.
  376.  
  377.                                       NO WARRANTY
  378.  
  379.  12. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  380.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  381.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  382.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
  383.      WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
  384.      NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  385.      FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE
  386.      QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  387.      PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  388.      SERVICING, REPAIR OR CORRECTION.
  389.  
  390.  13. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  391.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  392.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  393.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  394.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  395.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS
  396.      OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  397.      YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH
  398.      ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  399.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  400.  
  401.                          END OF TERMS AND CONDITIONS
  402.  
  403. How to Apply These Terms to Your New Programs
  404. =============================================
  405.  
  406.    If you develop a new program, and you want it to be of the greatest
  407. possible use to the public, the best way to achieve this is to make it
  408. free software which everyone can redistribute and change under these
  409. terms.
  410.  
  411.    To do so, attach the following notices to the program.  It is safest
  412. to attach them to the start of each source file to most effectively
  413. convey the exclusion of warranty; and each file should have at least
  414. the "copyright" line and a pointer to where the full notice is found.
  415.  
  416.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  417.      Copyright (C) 19YY  NAME OF AUTHOR
  418.      
  419.      This program is free software; you can redistribute it and/or modify
  420.      it under the terms of the GNU General Public License as published by
  421.      the Free Software Foundation; either version 2 of the License, or
  422.      (at your option) any later version.
  423.      
  424.      This program is distributed in the hope that it will be useful,
  425.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  426.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  427.      GNU General Public License for more details.
  428.      
  429.      You should have received a copy of the GNU General Public License
  430.      along with this program; if not, write to the Free Software
  431.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  432.  
  433.    Also add information on how to contact you by electronic and paper
  434. mail.
  435.  
  436.    If the program is interactive, make it output a short notice like
  437. this when it starts in an interactive mode:
  438.  
  439.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  440.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  441.      This is free software, and you are welcome to redistribute it
  442.      under certain conditions; type `show c' for details.
  443.  
  444.    The hypothetical commands `show w' and `show c' should show the
  445. appropriate parts of the General Public License.  Of course, the
  446. commands you use may be called something other than `show w' and `show
  447. c'; they could even be mouse-clicks or menu items--whatever suits your
  448. program.
  449.  
  450.    You should also get your employer (if you work as a programmer) or
  451. your school, if any, to sign a "copyright disclaimer" for the program,
  452. if necessary.  Here is a sample; alter the names:
  453.  
  454.      Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  455.      `Gnomovision' (which makes passes at compilers) written by James Hacker.
  456.      
  457.      SIGNATURE OF TY COON, 1 April 1989
  458.      Ty Coon, President of Vice
  459.  
  460.    This General Public License does not permit incorporating your
  461. program into proprietary programs.  If your program is a subroutine
  462. library, you may consider it more useful to permit linking proprietary
  463. applications with the library.  If this is what you want to do, use
  464. the GNU Library General Public License instead of this License.
  465.  
  466. 
  467. File: make.info,  Node: Bugs,  Next: Simple,  Prev: Copying,  Up: Top
  468.  
  469. Problems and Bugs
  470. *****************
  471.  
  472.    If you have problems with GNU `make' or think you've found a bug,
  473. please report it to Roland McGrath; he doesn't promise to do anything
  474. but he might well want to fix it.
  475.  
  476.    Before reporting a bug, make sure you've actually found a real bug. 
  477. Carefully reread the documentation and see if it really says you can do
  478. what you're trying to do.  If it's not clear whether you should be able
  479. to do something or not, report that too; it's a bug in the
  480. documentation!
  481.  
  482.    Before reporting a bug or trying to fix it yourself, try to isolate
  483. it to the smallest possible makefile that reproduces the problem. 
  484. Then send us the makefile and the exact results `make' gave you.  Also
  485. say what you expected to occur; this will help us decide whether the
  486. problem was really in the documentation.
  487.  
  488.    Once you've got a precise problem, send email to (Internet)
  489. `bug-gnu-utils@prep.ai.mit.edu' or (UUCP)
  490. `mit-eddie!prep.ai.mit.edu!bug-gnu-utils'.  Please include the version
  491. number of `make' you are using.  You can get this information with the
  492. command `make -v -f /dev/null'.
  493.  
  494.    Non-bug suggestions are always welcome as well.  If you have
  495. questions about things that are unclear in the documentation or are
  496. just obscure features, ask Roland McGrath; he'll be happy to help you
  497. out (but no promises).  You can send him electronic mail at Internet
  498. address `roland@prep.ai.mit.edu' or UUCP path
  499. `mit-eddie!prep.ai.mit.edu!roland'.
  500.  
  501. 
  502. File: make.info,  Node: Simple,  Next: Makefiles,  Prev: Bugs,  Up: Top
  503.  
  504. Simple Example of `make'
  505. ************************
  506.  
  507.    Suppose we have a text editor consisting of eight C source files
  508. and three header files.  We need a makefile to tell `make' how to
  509. compile and link the editor.  Assume that all the C files include
  510. `defs.h', but only those defining editing commands include
  511. `commands.h' and only low level files that change the editor buffer
  512. include `buffer.h'.
  513.  
  514.    To recompile the editor, each changed C source file must be
  515. recompiled.  If a header file has changed, to be safe each C source
  516. file that includes the header file must be recompiled.  Each
  517. compilation produces an object file corresponding to the source file. 
  518. Finally, if any source file has been recompiled, all the object files,
  519. whether newly made or saved from previous compilations, must be linked
  520. together to produce the new executable editor.
  521.  
  522.    Here is a straightforward makefile that describes these criteria
  523. and says how to compile and link when the time comes:
  524.  
  525.      edit : main.o kbd.o commands.o display.o \
  526.             insert.o search.o files.o utils.o
  527.              cc -o edit main.o kbd.o commands.o display.o \
  528.                         insert.o search.o files.o utils.o
  529.      
  530.      main.o : main.c defs.h
  531.              cc -c main.c
  532.      kbd.o : kbd.c defs.h command.h
  533.              cc -c kbd.c
  534.      commands.o : command.c defs.h command.h
  535.              cc -c commands.c
  536.      display.o : display.c defs.h buffer.h
  537.              cc -c display.c
  538.      insert.o : insert.c defs.h buffer.h
  539.              cc -c insert.c
  540.      search.o : search.c defs.h buffer.h
  541.              cc -c search.c
  542.      files.o : files.c defs.h buffer.h command.h
  543.              cc -c files.c
  544.      utils.o : utils.c defs.h
  545.              cc -c utils.c
  546.  
  547.    We split each long line into two lines using a backslash-newline;
  548. this is like using one long line, but is easier to read.
  549.  
  550.    Each file that is generated by a program--that is to say, each file
  551. except for source files--is the "target" of a "rule" (*note Rules::.). 
  552. (In this example, these are the object files such as `main.o',
  553. `kbd.o', etc., and the executable file `edit'.)  The target appears at
  554. the beginning of a line, followed by a colon.
  555.  
  556.    After the colon come the target's "dependencies": all the files that
  557. are used as input when the target file is updated.  A target file
  558. needs to be recompiled or relinked if any of its dependencies changes.
  559.  In addition, any dependencies that are themselves automatically
  560. generated should be updated first.  In this example, `edit' depends on
  561. each of the eight object files; the object file `main.o' depends on
  562. the source file `main.c' and on the header file `defs.h'.
  563.  
  564.    By default, `make' starts with the first rule (not counting rules
  565. whose target names start with `.').  This is called the "default
  566. goal".  Therefore, we put the rule for the executable program `edit'
  567. first.  The other rules are processed because their targets appear as
  568. dependencies of the goal.
  569.  
  570.    After each line containing a target and dependencies come one or
  571. more lines of shell commands that say how to update the target file. 
  572. These lines start with a tab to tell `make' that they are command
  573. lines.  But `make' does not know anything about how the commands work.
  574.  It is up to you to supply commands that will update the target file
  575. properly.  All `make' does is execute the commands you have specified
  576. when the target file needs to be updated.
  577.  
  578. How `make' Processes This Makefile
  579. ==================================
  580.  
  581.    After reading the makefile, `make' begins its real work by
  582. processing the first rule, the one for relinking `edit'; but before it
  583. can fully process this rule, it must process the rules for the files
  584. `edit' depends on: all the object files.  Each of these files is
  585. processed according to its own rule.  These rules say to update the
  586. `.o' file by compiling its source file.  The recompilation must be
  587. done if the source file, or any of the header files named as
  588. dependencies, is more recent than the object file, or if the object
  589. file does not exist.
  590.  
  591.    Before recompiling an object file, `make' considers updating its
  592. dependencies, the source file and header files.  This makefile does not
  593. specify anything to be done for them--the `.c' and `.h' files are not
  594. the targets of any rules--so nothing needs to be done.  But
  595. automatically generated C programs, such as made by Bison or Yacc,
  596. would be updated by their own rules at this time.
  597.  
  598.    After recompiling whichever object files need it, `make' can now
  599. decide whether to relink `edit'.  This must be done if the file `edit'
  600. does not exist, or if any of the object files are newer than it.  If
  601. an object file was just recompiled, it is now newer than `edit', so
  602. `edit' will be relinked.
  603.  
  604.    Thus, if we change the file `insert.c' and run `make', `make' will
  605. compile that file to update `insert.o', and then link `edit'.  If we
  606. change the file `command.h' and run `make', `make' will recompile the
  607. object files `kbd.o', `commands.o' and `files.o' and then link file
  608. `edit'.
  609.  
  610. Variables Make Makefiles Simpler
  611. ================================
  612.  
  613.    In our example, we had to list all the object files twice in the
  614. rule for `edit' (repeated here):
  615.  
  616.      edit : main.o kbd.o commands.o display.o \
  617.                    insert.o search.o files.o utils.o
  618.              cc -o edit main.o kbd.o commands.o display.o \
  619.                         insert.o search.o files.o utils.o
  620.  
  621.    Such duplication is error-prone; if a new object file is added to
  622. the system, we might add it to one list and forget the other.  We can
  623. eliminate the risk and simplify the makefile by using a "variable". 
  624. Variables allow a text string to be defined once and substituted in
  625. multiple places later (*note Variables::.).
  626.  
  627.    It is standard practice for every makefile to have a variable named
  628. `objects', `OBJECTS', `objs', `OBJS', `obj' or `OBJ' which is a list
  629. of all object file names.  We would define such a variable `objects'
  630. with a line like this in the makefile:
  631.  
  632.      objects = main.o kbd.o commands.o display.o \
  633.                    insert.o search.o files.o utils.o
  634.  
  635. Then, each place we want to put a list of the object file names, we can
  636. substitute the variable's value by writing `$(objects)' (*note
  637. Variables::.).  Here is how the rule for `edit' looks as a result:
  638.  
  639.      edit : $(objects)
  640.              cc -o edit $(objects)
  641.  
  642. Letting `make' Deduce the Commands
  643. ==================================
  644.  
  645.    It is not necessary to spell out the commands for compiling the
  646. individual C source files, because `make' can figure them out: it has
  647. an "implicit rule" for updating a `.o' file from a correspondingly
  648. named `.c' file using a `cc -c' command.  For example, it will use the
  649. command `cc -c main.c -o main.o' to compile `main.c' into `main.o'. 
  650. We can therefore omit the commands from the rules for the object
  651. files.  *Note Implicit::.
  652.  
  653.    When a `.c' file is used automatically in this way, it is also
  654. automatically added to the list of dependencies.  We can therefore omit
  655. the `.c' files from the dependencies, provided we omit the commands.
  656.  
  657.    Here is the entire example, with both of these changes, and a
  658. variable `objects' as suggested above:
  659.  
  660.      objects =  main.o kbd.o commands.o display.o \
  661.       insert.o search.o files.o utils.o
  662.      
  663.      edit : $(objects)
  664.              cc -o edit $(objects)
  665.      
  666.      main.o : defs.h
  667.      kbd.o : defs.h command.h
  668.      commands.o : defs.h command.h
  669.      display.o : defs.h buffer.h
  670.      insert.o : defs.h buffer.h
  671.      search.o : defs.h buffer.h
  672.      files.o : defs.h buffer.h command.h
  673.      utils.o : defs.h
  674.  
  675. This is how we would write the makefile in actual practice.
  676.  
  677. Another Style of Makefile
  678. =========================
  679.  
  680.    Since the rules for the object files specify only dependencies, no
  681. commands, one can alternatively combine them by dependency instead of
  682. by target.  Here is what it looks like:
  683.  
  684.      objects =  main.o kbd.o commands.o display.o \
  685.       insert.o search.o files.o utils.o
  686.      
  687.      edit : $(objects)
  688.              cc -o edit $(objects)
  689.      
  690.      $(objects) : defs.h
  691.      kbd.o commands.o files.o : command.h
  692.      display.o insert.o search.o files.o : buffer.h
  693.  
  694. Here `defs.h' is given as a dependency of all the object files;
  695. `commands.h' and `buffer.h' are dependencies of the specific object
  696. files listed for them.
  697.  
  698.    Whether this is better is a matter of taste: it is more compact,
  699. but some people dislike it because they find it clearer to put all the
  700. information about each target in one place.
  701.  
  702. Rules for Cleaning the Directory
  703. ================================
  704.  
  705.    Compiling a program isn't the only thing you might want to write
  706. rules for.  Makefiles commonly tell how to do a few other things
  707. besides compiling the program: for example, how to delete all the
  708. object files and executables so that the directory is "clean".  Here
  709. is how we would write a `make' rule for cleaning our example editor:
  710.  
  711.      clean:
  712.              rm edit $(objects)
  713.  
  714. This rule would be added at the end of the makefile, because we don't
  715. want it to run by default!  We want the rule for `edit', which
  716. recompiles the editor, to remain the default goal.
  717.  
  718.    Since `clean' is not a dependency of `edit', this rule won't run at
  719. all if we give the command `make' with no arguments.  In order to make
  720. the rule run, we have to type `make clean'.
  721.  
  722. 
  723. File: make.info,  Node: Makefiles,  Next: Rules,  Prev: Simple,  Up: Top
  724.  
  725. Writing Makefiles
  726. *****************
  727.  
  728.    The information that tells `make' how to recompile a system comes
  729. from reading a data base called the "makefile".
  730.  
  731. * Menu:
  732.  
  733. * Contents: Makefile Contents.   Overview of what you put in a makefile.
  734. * Names: Makefile Names.         Where `make' finds the makefile.
  735. * Include::                      How one makefile can use another makefile.
  736. * MAKEFILES Variable::           The environment can specify extra makefiles.
  737. * Remaking Makefiles::           How makefiles get remade.
  738. * Overriding Makefiles::         How to override part of one makefile
  739.                                  with another makefile.
  740.  
  741. 
  742. File: make.info,  Node: Makefile Contents,  Next: Makefile Names,  Prev: Makefiles,  Up: Makefiles
  743.  
  744. What Makefiles Contain
  745. ======================
  746.  
  747.    Makefiles contain four kinds of things: "rules", "variable
  748. definitions", "directives" and "comments".  Rules, variables and
  749. directives are described at length in later chapters.
  750.  
  751.    * A rule says when and how to remake one or more files, called the
  752.      rule's "targets".  It lists the other files that the targets
  753.      "depend on", and may also give commands to use to create or
  754.      update the targets.  *Note Rules::.
  755.  
  756.    * A variable definition is a line that specifies a text string value
  757.      for a "variable" that can be substituted into the text later. 
  758.      The simple makefile example (*note Simple::.) shows a variable
  759.      definition for `objects' as a list of all object files.  *Note
  760.      Variables::, for full details.
  761.  
  762.    * A directive is a command for `make' to do something special while
  763.      reading the makefile.  These include:
  764.  
  765.         * Reading another makefile (*note Include::.).
  766.  
  767.         * Deciding (based on the values of variables) whether to use or
  768.           ignore a part of the makefile (*note Conditionals::.).
  769.  
  770.         * Defining a variable from a verbatim string containing
  771.           multiple lines (*note Defining::.).
  772.  
  773.    * `#' in a line of a makefile starts a comment.  It and the rest of
  774.      the line are ignored, except that a trailing backslash not
  775.      escaped by another backslash will continue the comment across
  776.      multiple lines.  Comments may appear on any of the lines in the
  777.      makefile, except within a `define' directive, and perhaps within
  778.      commands (where the shell decides what is a comment).  A line
  779.      containing just a comment (with perhaps spaces before it) is
  780.      effectively blank, and is ignored.
  781.  
  782. 
  783. File: make.info,  Node: Makefile Names,  Next: Include,  Prev: Makefile Contents,  Up: Makefiles
  784.  
  785. What Name to Give Your Makefile
  786. ===============================
  787.  
  788.    By default, when `make' looks for the makefile, it tries the names
  789. `GNUmakefile', `makefile' and `Makefile', in that order.
  790.  
  791.    Normally you should call your makefile either `makefile' or
  792. `Makefile'.  (We recommend `Makefile' because it appears prominently
  793. near the beginning of a directory listing, right near other important
  794. files such as `README'.)  The first name checked, `GNUmakefile', is
  795. not recommended for most makefiles.  You should use this name if you
  796. have a makefile that is specific to GNU `make', and will not be
  797. understood by other versions of `make'.
  798.  
  799.    If `make' finds none of these names, it does not use any makefile. 
  800. Then you must specify a goal with a command argument, and `make' will
  801. attempt to figure out how to remake it using only its built-in
  802. implicit rules.  *Note Implicit::.
  803.  
  804.    If you want to use a nonstandard name for your makefile, you can
  805. specify the makefile name with the `-f' option.  The arguments `-f
  806. NAME' tell `make' to read the file NAME as the makefile.  If you use
  807. more than one `-f' option, you can specify several makefiles.  All the
  808. makefiles are effectively concatenated in the order specified.  The
  809. default makefile names `GNUmakefile', `makefile' and `Makefile' are
  810. not checked automatically if you specify `-f'.
  811.  
  812. 
  813. File: make.info,  Node: Include,  Next: MAKEFILES Variable,  Prev: Makefile Names,  Up: Makefiles
  814.  
  815. Including Other Makefiles
  816. =========================
  817.  
  818.    The `include' directive tells `make' to suspend reading the current
  819. makefile and read another makefile before continuing.  The directive
  820. is a line in the makefile that looks like this:
  821.  
  822.      include FILENAME
  823.  
  824.    Extra spaces are allowed and ignored at the beginning of the line,
  825. but a tab is not allowed.  (If the line begins with a tab, it will be
  826. considered a command line.)  Whitespace is required between `include'
  827. and FILENAME; extra whitespace is ignored there and at the end of the
  828. directive.  A comment starting with `#' is allowed at the end of the
  829. line.  If FILENAME contains any variable or function references, they
  830. are expanded.  (*Note Variables::.)
  831.  
  832.    When `make' processes an `include' directive, it suspends reading
  833. of the containing makefile and reads from FILENAME instead.  When that
  834. is finished, `make' resumes reading the makefile in which the
  835. directive appears.
  836.  
  837.    One occasion for using `include' directives is when several
  838. programs, handled by individual makefiles in various directories, need
  839. to use a common set of variable definitions (*note Setting::.) or
  840. pattern rules (*note Pattern Rules::.).
  841.  
  842.    Another such occasion is when you want to automatically generate
  843. dependencies from source files; the dependencies can be put in a file
  844. that is included by the main makefile.  This practice is generally
  845. cleaner than that of somehow appending the dependencies to the end of
  846. the main makefile as has been traditionally done with other versions
  847. of `make'.
  848.  
  849.    If the specified name does not start with a slash, and the file is
  850. not found in the current directory, several other directories are
  851. searched.  First, any directories you have specified with the `-I'
  852. option are searched (*note Options::.).  Then the following
  853. directories (if they exist) are searched, in this order:
  854. `/usr/gnu/include', `/usr/local/include', `/usr/include'.  If an
  855. included makefile cannot be found in any of these directories, a
  856. warning message is generated, but it is not a fatal error; processing
  857. of the makefile containing the `include' continues.
  858.  
  859. 
  860. File: make.info,  Node: MAKEFILES Variable,  Next: Remaking Makefiles,  Prev: Include,  Up: Makefiles
  861.  
  862. The Variable `MAKEFILES'
  863. ========================
  864.  
  865.    If the environment variable `MAKEFILES' is defined, `make'
  866. considers its value as a list of names (separated by whitespace) of
  867. additional makefiles to be read before the others.  This works much
  868. like the `include' directive: various directories are searched for
  869. those files (*note Include::.).  In addition, the default goal is
  870. never taken from one of these makefiles and it is not an error if the
  871. files listed in `MAKEFILES' are not found.
  872.  
  873.    The main use of `MAKEFILES' is in communication between recursive
  874. invocations of `make' (*note Recursion::.).  It usually isn't
  875. desirable to set the environment variable before a top-level invocation
  876. of `make', because it is usually better not to mess with a makefile
  877. from outside.  However, if you are running `make' without a specific
  878. makefile, a makefile in `MAKEFILES' can do useful things to help the
  879. built-in implicit rules work better, such as defining search paths.
  880.  
  881.    Some users are tempted to set `MAKEFILES' in the environment
  882. automatically on login, and program makefiles to expect this to be
  883. done.  This is a very bad idea, because such makefiles will fail to
  884. work if run by anyone else.  It is much better to write explicit
  885. `include' directives in the makefiles.
  886.  
  887. 
  888. File: make.info,  Node: Remaking Makefiles,  Next: Overriding Makefiles,  Prev: MAKEFILES Variable,  Up: Makefiles
  889.  
  890. How Makefiles Are Remade
  891. ========================
  892.  
  893.    Sometimes makefiles can be remade from other files, such as RCS or
  894. SCCS files.  If a makefile can be remade from other files, you
  895. probably want `make' to get an up-to-date version of the makefile to
  896. read in.
  897.  
  898.    To this end, after reading in all makefiles, `make' will consider
  899. each as a goal target and attempt to update it.  If a makefile has a
  900. rule which says how to update it (found either in that very makefile or
  901. in another one) or if an implicit rule applies to it (*note
  902. Implicit::.), it will be updated if necessary.  After all makefiles
  903. have been checked, if any have actually been changed, `make' starts
  904. with a clean slate and reads all the makefiles over again.  (It will
  905. also attempt to update each of them over again, but normally this will
  906. not change them again, since they are already up to date.)
  907.  
  908.    If the makefiles specify commands to remake a file but no
  909. dependencies, the file will always be remade.  In the case of
  910. makefiles, a makefile that has commands but no dependencies will be
  911. remade every time `make' is run, and then again after `make' starts
  912. over and reads the makefiles in again.  This would cause an infinite
  913. loop; `make' would constantly remake the makefile, and never do
  914. anything else.  So, to avoid this, `make' will *not* attempt to remake
  915. makefiles which are specified as targets but have no dependencies.
  916.  
  917.    If you do not specify any makefiles to be read with `-f' options,
  918. `make' will try the default makefile names; *note Makefile Names::.. 
  919. Unlike makefiles explicitly requested with `-f' options, `make' is not
  920. certain that these makefiles should exist.  However, if a default
  921. makefile does not exist but can be created by running `make' rules,
  922. you probably want the rules to be run so that the makefile can be used.
  923.  
  924.    Therefore, if none of the default makefiles exists, `make' will try
  925. to make each of them in the same order in which they are searched for
  926. (*note Makefile Names::.) until it succeeds in making one, or it runs
  927. out of names to try.  Note that it is not an error if `make' cannot
  928. find or make any makefile; a makefile is not always necessary.
  929.  
  930.    When you use the `-t' option (touch targets), you would not want to
  931. use an out-of-date makefile to decide which targets to touch.  So the
  932. `-t' option has no effect on updating makefiles; they are really
  933. updated even if `-t' is specified.  Likewise, `-q' and `-n' do not
  934. prevent updating of makefiles, because an out-of-date makefile would
  935. result in the wrong output for other targets.  Thus, `make -f mfile -n
  936. foo' will update `mfile', read it in, and then print the commands to
  937. update `foo' and its dependencies without running them.  The commands
  938. printed for `foo' will be those specified in the updated contents of
  939. `mfile'.
  940.  
  941.    However, on occasion you might actually wish to prevent updating of
  942. even the makefiles.  You can do this by specifying the makefiles as
  943. goals in the command line as well as specifying them as makefiles. 
  944. When the makefile name is specified explicitly as a goal, the options
  945. `-t' and so on do apply to them.
  946.  
  947.    Thus, `make -f mfile -n mfile foo' would read the makefile `mfile',
  948. print the commands needed to update it without actually running them,
  949. and then print the commands needed to update `foo' without running
  950. them.  The commands for `foo' will be those specified by the existing
  951. contents of `mfile'.
  952.  
  953. 
  954. File: make.info,  Node: Overriding Makefiles,  Prev: Remaking Makefiles,  Up: Makefiles
  955.  
  956. Overriding Part of One Makefile with Another Makefile
  957. =====================================================
  958.  
  959.    Sometimes it is useful to have a makefile that is mostly just like
  960. another makefile.  You can often use the `include' directive to
  961. include one in the other, and add more targets or variable definitions. 
  962. However, if the two makefiles give different commands for the same
  963. target, `make' will not let you just do this.  But there is another
  964. way.
  965.  
  966.    In the containing makefile (the one that wants to include the
  967. other), you can use the `.DEFAULT' special target to say that to
  968. remake any target that cannot be made from the information in the
  969. containing makefile, `make' should look in another makefile.  *Note
  970. Last Resort::, for more information on `.DEFAULT'.
  971.  
  972.    For example, if you have a makefile called `Makefile' that says how
  973. to make the target `foo' (and other targets), you can write a makefile
  974. called `GNUmakefile' that contains:
  975.  
  976.      foo:
  977.              frobnicate > foo
  978.      
  979.      .DEFAULT:
  980.              @$(MAKE) -f Makefile $@
  981.  
  982.    If you say `make foo', `make' will find `GNUmakefile', read it, and
  983. see that to make `foo', it needs to run the command `frobnicate >
  984. foo'.  If you say `make bar', `make' will find no way to make `bar' in
  985. `GNUmakefile', so it will use the commands from `.DEFAULT': `make -f
  986. Makefile bar'.  If `Makefile' provides a rule for updating `bar',
  987. `make' will apply the rule.  And likewise for any other target that
  988. `GNUmakefile' does not say how to make.
  989.  
  990. 
  991. File: make.info,  Node: Rules,  Next: Commands,  Prev: Makefiles,  Up: Top
  992.  
  993. Writing Rules
  994. *************
  995.  
  996.    A "rule" appears in the makefile and says when and how to remake
  997. certain files, called the rule's "targets" (usually only one per rule). 
  998. It lists the other files that are the "dependencies" of the target, and
  999. "commands" to use to create or update the target.
  1000.  
  1001.    The order of rules is not significant, except for determining the
  1002. "default goal": the target for `make' to consider, if you do not
  1003. otherwise specify one.  The default goal is the target of the first
  1004. rule in the first makefile, except that targets starting with a period
  1005. do not count unless they contain slashes as well; also, a target that
  1006. defines a pattern rule (*note Pattern Rules::.) or a suffix rule
  1007. (*note Suffix Rules::.) has no effect on the default goal.
  1008.  
  1009.    Therefore, we usually write the makefile so that the first rule is
  1010. the one for compiling the entire program or all the programs described
  1011. by the makefile.  *Note Goals::.
  1012.  
  1013. * Menu:
  1014.  
  1015. * Rule Example::        An explained example of a rule.
  1016. * Rule Syntax::         General syntax of rules, with explanation.
  1017.  
  1018. * Wildcards::           Using wildcard characters like `*' in file names.
  1019. * Directory Search::    Searching other directories for source files.
  1020.  
  1021. * Phony Targets::       Using a target that isn't a real file's name.
  1022. * Force Targets::       A target without commands or dependencies can
  1023.                          be used to mark other targets as phony.
  1024. * Special Targets::     Targets with special built-in meanings.
  1025. * Empty Targets::       Real files that are empty--only the date matters.
  1026. * Multiple Targets::    When it is useful to have several targets in a rule.
  1027. * Static Pattern::      Static pattern rules apply to multiple targets
  1028.                          and can vary the dependencies according to the
  1029.                          target name.
  1030. * Multiple Rules::      Using several rules with the same target.
  1031. * Double-Colon::        Special kind of rule allowing
  1032.                           several independent rules for one target.
  1033. * Commands::            Special features and details of how commands
  1034.                          in a rule are executed.
  1035.  
  1036. 
  1037. File: make.info,  Node: Rule Example,  Next: Rule Syntax,  Prev: Rules,  Up: Rules
  1038.  
  1039. Rule Example
  1040. ============
  1041.  
  1042.    Here is an example of a rule:
  1043.  
  1044.      foo.o : foo.c defs.h       # module for twiddling the frobs
  1045.              cc -c -g foo.c
  1046.  
  1047.    Its target is `foo.o' and its dependencies are `foo.c' and
  1048. `defs.h'.  It has one command, which is `cc -c -g foo.c'.  The command
  1049. line starts with a tab to identify it as a command.
  1050.  
  1051.    This rule says two things:
  1052.  
  1053.    * How to decide whether `foo.o' is out of date: it is out of date
  1054.      if it does not exist, or if either `foo.c' or `defs.h' is more
  1055.      recent than it.
  1056.  
  1057.    * How to update the file `foo.o': by running `cc' as stated.  The
  1058.      command does not explicitly mention `defs.h', but we presume that
  1059.      `foo.c' includes it, and that that is why `defs.h' was added to
  1060.      the dependencies.
  1061.  
  1062.