home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DMAKE37S.ZIP / DMAKE / MAKE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-06  |  26.5 KB  |  975 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/make.c,v 1.1 91/05/06 15:23:19 dvadura Exp $
  2. -- SYNOPSIS -- perform the update of all outdated targets.
  3. -- 
  4. -- DESCRIPTION
  5. --    This is where we traverse the make graph looking for targets that
  6. --    are out of date, and we try to infer how to make them if we can.
  7. --    The usual make macros are understood, as well as some new ones:
  8. --
  9. --        $$    - expands to $
  10. --        $@      - full target name
  11. --        $*      - target name with no suffix, same as $(@:db)
  12. --              or, the value of % in % meta rule recipes
  13. --        $?      - list of out of date prerequisites
  14. --        $<      - all prerequisites associated with rules line
  15. --        $&    - all prerequisites associated with target
  16. --        $>      - library name for target (if any)
  17. --        $^    - out of date prerequisites taken from value of $<
  18. --        {{    - expands to {
  19. --        }}    - expands to }
  20. --        \#    - expands to #
  21. -- 
  22. -- AUTHOR
  23. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  24. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  25. --
  26. -- COPYRIGHT
  27. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  28. -- 
  29. --      This program is free software; you can redistribute it and/or
  30. --      modify it under the terms of the GNU General Public License
  31. --      (version 1), as published by the Free Software Foundation, and
  32. --      found in the file 'LICENSE' included with this distribution.
  33. -- 
  34. --      This program is distributed in the hope that it will be useful,
  35. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  36. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37. --      GNU General Public License for more details.
  38. -- 
  39. --      You should have received a copy of the GNU General Public License
  40. --      along with this program;  if not, write to the Free Software
  41. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. --
  43. -- LOG
  44. --     $Log:    make.c,v $
  45.  * Revision 1.1  91/05/06  15:23:19  dvadura
  46.  * dmake Release Version 3.7
  47.  * 
  48. */
  49.  
  50. #include "extern.h"
  51.  
  52. static    void    _drop_mac ANSI((HASHPTR));
  53. static    void    _print_cmnd ANSI((char*, int, int));
  54. static    void    _set_recipe ANSI((char*, int));
  55. static    void    _set_tmd ANSI(());
  56. static    void    _append_file ANSI((STRINGPTR, FILE*, char*, int));
  57. static  LINKPTR _dup_prq ANSI((LINKPTR));
  58. static  char*   _prefix ANSI((char *, char *));
  59. static  char*   _pool_lookup ANSI((char *));
  60.  
  61. #define RP_GPPROLOG    0
  62. #define RP_RECIPE    1
  63. #define RP_GPEPILOG    2
  64. #define NUM_RECIPES    3
  65.  
  66. static STRINGPTR _recipes[ NUM_RECIPES ];
  67.  
  68.  
  69. PUBLIC int
  70. Make_targets()/*
  71. ================
  72.    Actually go and make the targets on the target list */
  73. {
  74.    LINKPTR lp;
  75.    int     done = 0;
  76.  
  77.    DB_ENTER( "Make_targets" );
  78.  
  79.    Read_state();
  80.    _set_recipe( ".GROUPPROLOG", RP_GPPROLOG );
  81.    _set_recipe( ".GROUPEPILOG", RP_GPEPILOG );
  82.  
  83.    Root->ce_flag  |= F_RULES|F_TARGET|F_STAT;
  84.    Root->ce_attr  |= A_NOSTATE;
  85.    Root->ce_time   = Do_time();
  86.    TALLOC( Root->ce_recipe, 1, STRING );
  87.    Root->ce_recipe->st_string = "";
  88.  
  89.    for( lp = Root->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
  90.       lp->cl_prq->ce_attr |= A_ROOT;
  91.  
  92.    while( !done ) {
  93.       int rval;
  94.  
  95.       if( (rval = Make(Root, NIL(LINK), NIL(CELL))) == -1 )
  96.      DB_RETURN(1);
  97.       else
  98.      done = Root->ce_flag & F_MADE;
  99.  
  100.       if( !rval && !done ) Wait_for_child( FALSE, -1 );
  101.    }
  102.  
  103.    for( lp = Root->ce_prq; lp != NIL(LINK); lp = lp->cl_next ) {
  104.       CELLPTR root = lp->cl_prq;
  105.       if( !(root->ce_attr & A_UPDATED) )
  106.      printf( "`%s' is up to date\n", root->CE_NAME );
  107.    }
  108.  
  109.    DB_RETURN( 0 );
  110. }
  111.  
  112.  
  113.  
  114. int
  115. Make( cp, parent, setdirroot )/*
  116. ================================  Make a specified target */
  117. CELLPTR cp;
  118. LINKPTR parent;
  119. CELLPTR setdirroot;
  120. {
  121.    register LINKPTR dp;
  122.    register CELLPTR tcp;
  123.    CELLPTR          nsetdirroot;
  124.    char            *name, *lib;
  125.    HASHPTR        m_at, m_q, m_b, m_g, m_l, m_bb, m_up;
  126.    char             *all    = NIL(char);
  127.    char             *inf    = NIL(char);
  128.    char             *outall = NIL(char);
  129.    char             *imm    = NIL(char);
  130.    int              rval    = 0;
  131.    int            push    = 0;
  132.    int            ignore;
  133.    time_t           otime   = (time_t) 1L;
  134.    time_t        ttime   = (time_t) 1L;
  135.    int            mark_made = FALSE;
  136.  
  137.    DB_ENTER( "Make" );
  138.    DB_PRINT( "mem", ("%s:-> mem %ld", cp->CE_NAME, (long) coreleft()) );
  139.  
  140.    m_q = m_b = m_g = m_l = m_bb = m_up = m_at = NIL(HASH);
  141.  
  142.    /* Check to see if we have made the node already.  If so then don't do
  143.     * it again, except if the cell's ce_setdir field is set to something other
  144.     * than the value of setdirroot.  If they differ then, and we have made it
  145.     * already, then make it again and set the cell's stat bit to off so that
  146.     * we do the stat again. */
  147.  
  148.    if( cp->ce_flag & F_VISITED ) {
  149.       /* we may return if we made it already from the same setdir location,
  150.        * or if it is not a library member whose lib field is non NULL.  (if
  151.        * it is such a member then we have a line of the form:
  152.        *    lib1 lib2 .LIBRARY : member_list...
  153.        * and we have to make sure all members are up to date in both libs. */
  154.  
  155.       if( cp->ce_setdir == setdirroot &&
  156.       !((cp->ce_attr & A_LIBRARYM) && (cp->ce_lib != NIL(char))) )
  157.      DB_RETURN( 0 );
  158.  
  159.  
  160.       /* We check to make sure that we are comming from a truly different
  161.        * directory, ie. ".SETDIR=joe : a.c b.c d.c" are all assumed to come
  162.        * from the same directory, even though setdirroot is different when
  163.        * making dependents of each of these targets. */
  164.  
  165.       if( cp->ce_setdir != NIL(CELL) && setdirroot != NIL(CELL) &&
  166.           !strcmp(cp->ce_dir, setdirroot->ce_dir) )
  167.          DB_RETURN( 0 );
  168.  
  169.       if( Max_proc > 1 ) {
  170.      if( parent == NIL(LINK) )
  171.         Fatal( "Internal Error:  NIL parent in Make()" );
  172.  
  173.      TALLOC(parent->cl_prq, 1, CELL);
  174.      *parent->cl_prq = *cp;
  175.      cp = parent->cl_prq;
  176.      cp->ce_prq = _dup_prq(cp->ce_prq);
  177.       }
  178.       cp->ce_flag  &= ~(F_STAT|F_VISITED|F_MADE);
  179.    }
  180.  
  181.  
  182.    /* If we are supposed to change directories for this target then do so.
  183.     * If we do change dir, then modify the setdirroot variable to reflect
  184.     * that fact for all of the prerequisites that we will be making. */
  185.  
  186.    nsetdirroot = setdirroot;
  187.    ignore = (((cp->ce_attr|Glob_attr)&A_IGNORE) != 0);
  188.  
  189.    if( cp->ce_attr & A_SETDIR ) {
  190.       /* Change directory only if the previous .SETDIR is a different
  191.       /* directory from the current one.  ie. all cells with the same .SETDIR
  192.       /* attribute are assumed to come from the same directory. */
  193.  
  194.       if( (setdirroot == NIL(CELL) || setdirroot->ce_dir != cp->ce_dir) &&
  195.           (push = Push_dir(cp->ce_dir,cp->CE_NAME,ignore)) )
  196.      setdirroot = cp;
  197.    }
  198.  
  199.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  200.    if( (cp->ce_recipe == NIL(STRING) && !(cp->ce_flag & F_INFER)) ) {
  201.       char *dir = cp->ce_dir;
  202.       
  203.       if( Verbose & V_MAKE )
  204.      printf( "%s:  Infering prerequisite(s) and recipe for [%s]\n", Pname,
  205.           cp->CE_NAME );
  206.  
  207.       Infer_recipe( cp, setdirroot );
  208.  
  209.       /* See if the directory has changed, if it has then make sure we
  210.        * push it. */
  211.       if( dir != cp->ce_dir ) {
  212.      if( push ) Pop_dir(FALSE);
  213.          push = Push_dir( cp->ce_dir, cp->CE_NAME, ignore );
  214.      setdirroot = cp;
  215.       }
  216.    }
  217.  
  218.    tcp = cp;
  219.    do {
  220.       if( push ) {
  221.      if( tcp->ce_dir ) FREE( tcp->ce_dir );
  222.      tcp->ce_dir   = _pool_lookup(Pwd);
  223.      tcp->ce_attr |= A_SETDIR;
  224.       }
  225.       tcp->ce_setdir = nsetdirroot;
  226.       tcp = tcp->ce_all;
  227.    }
  228.    while( tcp != NIL(CELL) && tcp != cp );
  229.  
  230.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  231.    /* If we have not yet statted the target then do so. */
  232.    if( !(cp->ce_flag & F_STAT) && !(cp->ce_attr&A_PHONY) ) {
  233.       time_t itime = cp->ce_time;
  234.       Stat_target( cp, TRUE );
  235.  
  236.       if( cp->ce_time == (time_t)0L ) {
  237.          if( cp->ce_flag & F_INFER )
  238.         cp->ce_time = itime;
  239.       }
  240.       else
  241.          cp->ce_attr |= A_PRECIOUS;  /* File exists so don't remove it later. */
  242.  
  243.       if( Verbose & V_MAKE )
  244.      printf("%s:  Time stamp of [%s] is %ld\n",Pname,cp->CE_NAME,
  245.             cp->ce_time);
  246.    }
  247.  
  248.    DB_PRINT( "make", ("(%s, %ld, 0x%08x, 0x%04x)", cp->CE_NAME,
  249.             cp->ce_time, cp->ce_attr, cp->ce_flag) );
  250.  
  251.    if( !(cp->ce_flag & F_TARGET) && (cp->ce_time == (time_t) 0L) )
  252.       if( Makemkf ) {
  253.      rval = -1;
  254.      goto stop_making_it;
  255.       }
  256.       else if(cp->ce_prq != NIL(LINK)||(Augmake && (cp->ce_flag&F_EXPLICIT)))
  257.      /* Assume an empty recipe for a target that we have run inference on
  258.       * but do not have a set of rules for but for which we have inferred
  259.       * a list of prerequisites. */
  260.      cp->ce_flag |= F_RULES;
  261.       else
  262.      Fatal( "`%s' not found, and can't be made", cp->CE_NAME );
  263.  
  264.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  265.  
  266.    /* set value of $* if we have not infered a recipe, in this case $* is
  267.     * the same as $(@:db), this allows us to be compatible with BSD make */
  268.    if( cp->ce_per == NIL(char) ) cp->ce_per = "$(@:db)";
  269.  
  270.    for( dp = cp->ce_prq; dp != NIL(LINK); dp = dp->cl_next ) {
  271.       int seq;
  272.  
  273.       /* Make the prerequisite, note that if the current target has the
  274.        * .LIBRARY attribute set we pass on to the prerequisite the .LIBRARYM
  275.        * attribute and pass on the name of the current target as the library
  276.        * name, and we take it away when we are done.  */
  277.  
  278.       tcp = dp->cl_prq;
  279.       seq = (((cp->ce_attr | Glob_attr) & A_SEQ) != 0);
  280.  
  281.       if( tcp->ce_flag & F_VISITED )
  282.      if( tcp->ce_flag & F_MADE ) {
  283.         if( tcp->ce_time > ttime ) ttime = tcp->ce_time;
  284.         continue;
  285.      }
  286.      else
  287.         goto stop_making_it;
  288.  
  289.       if( strchr(tcp->CE_NAME, '$') ) {
  290.      /* Make this prerequisite link point at the real prerequisite we
  291.       * are after, ie figure out what the dynamic one is and point at it. */
  292.  
  293.      m_at = Def_macro( "@", cp->ce_fname, M_MULTI );
  294.      name = Expand( tcp->CE_NAME );
  295.  
  296.      tcp = dp->cl_prq = Def_cell( name );
  297.      FREE( name );
  298.       }
  299.  
  300.       if( cp->ce_attr & A_LIBRARY ) {
  301.          tcp->ce_attr |= A_LIBRARYM;
  302.      tcp->ce_lib   = cp->ce_fname;
  303.       }
  304.  
  305.       if( (tcp->ce_flag & (F_INFER|F_STAT))==F_INFER && cp->ce_time >= ttime )
  306.      tcp->ce_time = cp->ce_time;
  307.  
  308.       /* Propagate parents F_REMOVE and F_INFER attribute to children.
  309.        * Make certain you do this AFTER propagating the time, since the
  310.        * time propagation test above uses the F_INFER flag to decide if
  311.        * it should do so. */
  312.       tcp->ce_flag |= cp->ce_flag & (F_REMOVE|F_INFER);
  313.  
  314.       rval |= Make(tcp, dp, setdirroot);
  315.  
  316.       if( cp->ce_attr & A_LIBRARY )
  317.          tcp->ce_attr ^= A_LIBRARYM;
  318.  
  319.       if( rval == -1 || (seq && (rval == 1)) )
  320.      goto stop_making_it;
  321.  
  322.       if( tcp->ce_time > ttime ) ttime = tcp->ce_time;
  323.    }
  324.  
  325.  
  326.    /* Do the loop again.  We are most definitely going to make the current
  327.     * cell now.  NOTE:  doing this loop here also results in a reduction
  328.     * in peak memory usage by the algorithm. */
  329.  
  330.    for( dp = cp->ce_prq; dp != NIL(LINK); dp = dp->cl_next ) {
  331.       int  tgflg;
  332.       tcp  = dp->cl_prq;
  333.       name = tcp->ce_fname;
  334.  
  335.       /* make certain that all prerequisites are made prior to advancing. */
  336.       if( !(tcp->ce_flag & F_MADE) ) goto stop_making_it;
  337.  
  338.       /* If the target is a library, then check to make certain that a member
  339.        * is newer than an object file sitting on disk.  If the disk version
  340.        * is newer then set the time stamps so that the archived member is
  341.        * replaced. */
  342.       if( cp->ce_attr & A_LIBRARY )
  343.      if( tcp->ce_time < cp->ce_time ) {
  344.         time_t mtime = Do_stat( name, tcp->ce_lib, NIL(char *) );
  345.         if( mtime < tcp->ce_time ) tcp->ce_time = cp->ce_time+1L;
  346.      }
  347.  
  348.       if( tcp->ce_time > otime ) otime = tcp->ce_time;
  349.  
  350.       all = _strapp( all, name );
  351.       if( tgflg = (dp->cl_flag & F_TARGET) ) inf = _strapp( inf, name );
  352.  
  353.       if((cp->ce_time<tcp->ce_time) || ((tcp->ce_flag & F_TARGET) && Force)) {
  354.          outall = _strapp( outall, name );
  355.          if( tgflg ) imm = _strapp( imm, name );
  356.       }
  357.    }
  358.  
  359.    DB_PRINT( "mem", ("%s:-C mem %ld", cp->CE_NAME, (long) coreleft()) );
  360.    DB_PRINT( "make", ("I make '%s' if %ld > %ld", cp->CE_NAME, otime,
  361.           cp->ce_time) );
  362.  
  363.    if( Verbose & V_MAKE )
  364.       printf( "%s:  >>>> Making [%s]\n", Pname, cp->CE_NAME );
  365.  
  366.    m_at = Def_macro( "@", cp->ce_fname, M_MULTI );
  367.    m_g  = Def_macro( ">", cp->ce_lib,   M_MULTI );
  368.    m_q  = Def_macro( "?", outall,       M_MULTI );
  369.    m_b  = Def_macro( "<", inf,          M_MULTI );
  370.    m_l  = Def_macro( "&", all,          M_MULTI );
  371.    m_bb = Def_macro( "*", cp->ce_per,   M_MULTI );
  372.    m_up = Def_macro( "^", imm,          M_MULTI );
  373.  
  374.    _recipes[ RP_RECIPE ] = cp->ce_recipe;
  375.  
  376.    /* We attempt to make the target if
  377.     *   1. it has a newer prerequisite
  378.     *   2. It is a target and Force is set
  379.     *   3. It's set of recipe lines has changed.
  380.     */
  381.    if(    Check_state(cp, _recipes, NUM_RECIPES )
  382.       || (cp->ce_time < otime)
  383.       || ((cp->ce_flag & F_TARGET) && Force)
  384.      ) {
  385.  
  386.       /* Only checking so stop as soon as we determine we will make something */
  387.       if( Check ) {
  388.      rval = -1;
  389.      goto stop_making_it;
  390.       }
  391.  
  392.       if( Verbose & V_MAKE )
  393.      printf( "%s:  Updating [%s], (%ld > %ld)\n", Pname,
  394.          cp->CE_NAME, otime, cp->ce_time );
  395.  
  396.       if( Touch ) {
  397.      name = cp->ce_fname;
  398.      lib  = cp->ce_lib;
  399.  
  400.      if( !(Glob_attr & A_SILENT) || !Trace )
  401.         if( lib == NIL(char) )
  402.            printf("touch(%s)", name );
  403.         else if( cp->ce_attr & A_SYMBOL )
  404.            printf("touch(%s((%s)))", lib, name );
  405.         else
  406.            printf("touch(%s(%s))", lib, name );
  407.  
  408.      if( !Trace )
  409.         if( Do_touch( name, lib,
  410.         (cp->ce_attr & A_SYMBOL) ? &name : NIL(char *) ) != 0 )
  411.            printf( "  not touched - non-existant" );
  412.  
  413.      printf( "\n" );
  414.      Update_time_stamp( cp );
  415.       }
  416.       else if( cp->ce_recipe != NIL(STRING) ) {
  417.      if( !(cp->ce_flag & F_SINGLE) )
  418.            rval = Exec_commands( cp );
  419.      else {
  420.         TKSTR tk;
  421.  
  422.         _drop_mac( m_q );
  423.         SET_TOKEN( &tk, outall );
  424.  
  425.         Doing_bang = TRUE;
  426.         name = Get_token( &tk, "", FALSE );
  427.         do {
  428.            m_q->ht_value = name;
  429.  
  430.            Wait_for_completion = TRUE;    /* Reset in Exec_commands */
  431.            rval = Exec_commands( cp );
  432.            Unlink_temp_files(cp);
  433.         }
  434.         while( *(name = Get_token( &tk, "", FALSE )) != '\0' );
  435.         Doing_bang = FALSE;
  436.         Update_time_stamp( cp );
  437.  
  438.         m_q->ht_value = NIL(char);
  439.      }
  440.       }
  441.       else if( !(cp->ce_flag & F_RULES) && !(cp->ce_flag & F_STAT) &&
  442.            (!(cp->ce_attr & A_ROOT) || !(cp->ce_flag & F_EXPLICIT)) )
  443.      Fatal( "Don't know how to make `%s'",cp->CE_NAME );
  444.       else {
  445.          /* Empty recipe, set the flag as MADE and update the time stamp */
  446.      Update_time_stamp( cp );
  447.       }
  448.    }
  449.    else
  450.       mark_made = TRUE;
  451.  
  452.    /* Make sure everyone gets remade if Force is set */
  453.    tcp = cp;
  454.    do {
  455.       if( !(tcp->ce_flag & F_TARGET) && Force ) tcp->ce_time = Do_time();
  456.       if( mark_made ) tcp->ce_flag |= F_MADE;
  457.  
  458.       tcp->ce_flag |= F_VISITED;
  459.  
  460.       /* Note:  If the prerequisite was made using a .SETDIR= attribute
  461.        *     directory then we will include the directory in the fname
  462.        *        of the target.  */
  463.       if( push ) {
  464.      char *nname = Build_path(_prefix(Makedir,tcp->ce_dir), tcp->ce_fname);
  465.  
  466.      if( (tcp->ce_attr & A_FFNAME) && (tcp->ce_fname != NIL(char)) )
  467.         FREE( tcp->ce_fname );
  468.  
  469.      tcp->ce_fname = _strdup(nname);
  470.      tcp->ce_attr |= A_FFNAME;
  471.       }
  472.  
  473.       tcp = tcp->ce_all;
  474.    }
  475.    while( tcp != NIL(CELL) && tcp != cp );
  476.  
  477. stop_making_it:
  478.    _drop_mac( m_g  );
  479.    _drop_mac( m_q  );
  480.    _drop_mac( m_b  );
  481.    _drop_mac( m_l  );
  482.    _drop_mac( m_bb );
  483.    _drop_mac( m_up );
  484.    _drop_mac( m_at );
  485.  
  486.    while( push-- )  Pop_dir(FALSE);
  487.  
  488.    if( inf    != NIL(char) ) FREE( inf    );
  489.    if( all    != NIL(char) ) FREE( all    );
  490.    if( imm    != NIL(char) ) FREE( imm    );
  491.    if( outall != NIL(char) ) FREE( outall );
  492.  
  493.    DB_PRINT( "mem", ("%s:-< mem %ld", cp->CE_NAME, (long) coreleft()) );
  494.    DB_RETURN( rval );
  495. }
  496.  
  497.  
  498. static char *
  499. _prefix( pfx, pat )
  500. char *pfx;
  501. char *pat;
  502. {
  503.    char *opat = pat;
  504.    while( *pfx && *pat && *pfx++ == *pat++ );
  505.  
  506.    return( !*pfx ? _strspn(pat,DirBrkStr) : opat );
  507. }
  508.  
  509.  
  510. static LINKPTR
  511. _dup_prq( lp )
  512. LINKPTR lp;
  513. {
  514.    LINKPTR tlp;
  515.  
  516.    if( lp == NIL(LINK) ) return(lp);
  517.  
  518.    TALLOC(tlp, 1, LINK);
  519.    *tlp = *lp;
  520.    tlp->cl_next = _dup_prq( lp->cl_next );
  521.  
  522.    return(tlp);
  523. }
  524.  
  525.  
  526. static void
  527. _drop_mac( hp )/*
  528. ================ set a macro value to zero. */
  529. HASHPTR hp;
  530. {
  531.    if( hp && hp->ht_value != NIL(char) ) {
  532.       FREE( hp->ht_value );
  533.       hp->ht_value = NIL(char);
  534.    }
  535. }
  536.  
  537.  
  538.  
  539. PUBLIC int
  540. Exec_commands( cp )/*
  541. =====================
  542.   Execute the commands one at a time that are pointed to by the rules pointer
  543.   of the target cp. If a group is indicated, then the ce_attr determines
  544.   .IGNORE and .SILENT treatment for the group.
  545.   
  546.   The function returns 0, if the command is executed and has successfully
  547.   returned, and returns 1 if the command is executing but has not yet
  548.   returned (for parallel makes).
  549.   
  550.   The F_MADE bit in the cell is guaranteed set when the command has
  551.   successfully completed.  */
  552. CELLPTR cp;
  553. {
  554.    static HASHPTR useshell = NIL(HASH);
  555.    static HASHPTR command  = NIL(HASH);
  556.    static         int   read_cmnd = 0;
  557.    register STRINGPTR    rp;
  558.    STRINGPTR            orp;
  559.    char            *cmnd;
  560.    char            *groupfile;
  561.    FILE            *tmpfile;
  562.    int            do_it;
  563.    t_attr        attr;
  564.    int            group;
  565.    int            trace;
  566.    int            rval  = 0;
  567.  
  568.    DB_ENTER( "Exec_commands" );
  569.  
  570.    Current_target = cp;
  571.    attr  = Glob_attr | cp->ce_attr;
  572.    trace = Trace || !(attr & A_SILENT);
  573.    group = cp->ce_flag & F_GROUP;
  574.  
  575.    /* Do it again here for those that call us from places other than Make()
  576.     * above. */
  577.    orp = _recipes[ RP_RECIPE ];
  578.    _recipes[ RP_RECIPE ] = cp->ce_recipe;
  579.  
  580.    if( group ) {
  581.       trace  = Trace || !(attr & A_SILENT);
  582.  
  583.       if( !Trace ) tmpfile = Start_temp( Grp_suff, cp, &groupfile );
  584.       if( trace )  fputs( "[\n", stdout );
  585.  
  586.       /* Emit group prolog */
  587.       if( attr & A_PROLOG )
  588.          _append_file( _recipes[RP_GPPROLOG], tmpfile, cp->CE_NAME, trace );
  589.    }
  590.  
  591.    if( !useshell  ) useshell=Def_macro("USESHELL",NIL(char),M_MULTI|M_EXPANDED);
  592.    if( !read_cmnd ) {command = GET_MACRO("COMMAND"); read_cmnd = 1;}
  593.  
  594.    /* Process commands in recipe. If in group, merely append to file.
  595.     * Otherwise, run them.  */
  596.    for( rp = _recipes[RP_RECIPE]; rp != NIL(STRING); rp=rp->st_next,FREE(cmnd)){
  597.       t_attr a_attr = A_DEFAULT;
  598.       char   *p;
  599.       int    new_attr = FALSE;
  600.       int    shell;
  601.  
  602.       /* Only check for +,-,%,@ if the recipe line begins with a '$' macro
  603.        * expansion.  Otherwise there is no way it is going to find these
  604.        * now. */
  605.       if( *rp->st_string == '$' && !group ) {
  606.          t_attr s_attr = Glob_attr;
  607.      Glob_attr |= A_SILENT;
  608.      Suppress_temp_file = TRUE;
  609.      cmnd = Expand(rp->st_string);
  610.      Suppress_temp_file = FALSE;
  611.      a_attr |= Rcp_attribute(cmnd);
  612.      FREE(cmnd);
  613.      ++new_attr;
  614.      Glob_attr = s_attr;
  615.       }
  616.  
  617.       attr |= a_attr|rp->st_attr;
  618.       shell = ((attr & A_SHELL) != 0);
  619.       useshell->ht_value = (group||shell)?"yes":"no";
  620.  
  621.       cmnd = Expand( rp->st_string );
  622.  
  623.       if( new_attr ) {
  624.      char *ecmnd = cmnd;
  625.      cmnd = _strdup(_strspn(cmnd, " \t\n+-@%"));
  626.      FREE(ecmnd);
  627.       }
  628.  
  629.       /* COMMAND macro is set to "$(CMNDNAME) $(CMNDARGS)" by default, it is
  630.        * possible for the user to reset it to, for example
  631.        *    COMMAND = $(CMNDNAME) @$(mktmp $(CMNDARGS))
  632.        * in order to get a different interface for his command execution. */
  633.       if( command != NIL(HASH) && !group && *(p = _strpbrk(cmnd, " \t\n")) ) {
  634.      char *cname = cmnd;
  635.  
  636.      *p = '\0';
  637.      (void) Def_macro("CMNDNAME",cname,M_MULTI|M_EXPANDED);
  638.      (void) Def_macro("CMNDARGS",p+1,M_MULTI|M_EXPANDED);
  639.  
  640.      cmnd = Expand("$(COMMAND)");
  641.      FREE(cname);             /* cname == cmnd at this point. */
  642.       }
  643.  
  644.       Swap_on_exec = ((attr & A_SWAP) != 0);      /* Swapping for DOS only */
  645.       do_it = !Trace;
  646.  
  647.       if( !group && Trace && _strstr(rp->st_string,"$(MAKE)") ) {
  648.      Wait_for_completion |= Trace;
  649.      do_it = TRUE;
  650.       }
  651.  
  652.       if( group )
  653.          Append_line( cmnd, TRUE, tmpfile, cp->CE_NAME, trace, 0 );
  654.       else {
  655.      if( *_strspn(cmnd, " \t") != '\0' )
  656.         _print_cmnd(cmnd, !(do_it && (attr & A_SILENT)), 0);
  657.      else
  658.         do_it = FALSE;
  659.  
  660.      rval=Do_cmnd(cmnd,FALSE,do_it,cp,(attr&A_IGNORE)!=0, shell,
  661.               rp->st_next == NIL(STRING) );
  662.       }
  663.    }
  664.  
  665.    /* If it is a group then output the EPILOG if required and possibly
  666.     * execute the command */
  667.    if( group ) {
  668.       if( attr & A_EPILOG )    /* emit epilog */
  669.      _append_file( _recipes[RP_GPEPILOG], tmpfile, cp->CE_NAME, trace );
  670.  
  671.       if( trace ) fputs("]\n", stdout);
  672.  
  673.       if( do_it = !Trace ) Close_temp( cp, tmpfile );
  674.       rval = Do_cmnd(groupfile, TRUE, do_it, cp, (attr & A_IGNORE)!=0,
  675.              TRUE, TRUE);
  676.    }
  677.  
  678.    Wait_for_completion = FALSE;
  679.    _recipes[ RP_RECIPE ] = orp;
  680.    DB_RETURN( rval );
  681. }
  682.  
  683.  
  684. static void
  685. _print_cmnd( cmnd, echo, map )/*
  686. ================================
  687.    This routine is called to print out the command to stdout.  If echo is
  688.    false the printing to stdout is supressed, but the new lines in the command
  689.    are still deleted. */
  690. char *cmnd;
  691. int  echo;
  692. int  map;
  693. {
  694.    register char *p;
  695.    register char *n;
  696.  
  697.    DB_ENTER( "_print_cmnd" );
  698.  
  699.    if( echo ) {
  700.       printf( "%s\n", cmnd  );
  701.       fflush(stdout);
  702.    }
  703.  
  704.    for( p=cmnd; (n = strchr(p, CONTINUATION_CHAR)) != NIL(char); )
  705.       if(n[1] == '\n') {
  706.      DB_PRINT( "make", ("fixing [%s]", p) );
  707.      strcpy( n, n+2 );
  708.      p = n;
  709.       }
  710.       else {
  711.      if( map ) Map_esc( n );
  712.      p = n+1;
  713.       }
  714.  
  715.    DB_VOID_RETURN;
  716. }
  717.  
  718.  
  719.  
  720. /* These routines are used to maintain a stack of directories when making
  721.  * the targets.  If a target cd's to the directory then it is assumed that
  722.  * it will undo it when it is finished making itself. */
  723.  
  724. static STRINGPTR dir_stack = NIL(STRING);
  725.  
  726. int
  727. Push_dir( dir, name, ignore )/*
  728. ===============================
  729.    Change the current working directory to dir and save the current
  730.    working directory on the stack so that we can come back.
  731.    
  732.    If ignore is TRUE then do not complain about _ch_dir if not possible.*/
  733. char *dir;
  734. char *name;
  735. int  ignore;
  736. {
  737.    STRINGPTR   new_dir;
  738.  
  739.    DB_ENTER( "Push_dir" );
  740.  
  741.    if( dir == NIL(char) ) dir = Pwd;
  742.    if( *dir == '\'' && dir[strlen(dir)-1] == '\'' ) {
  743.       dir = _strdup(dir+1);
  744.       dir[strlen(dir)-1]='\0';
  745.    }
  746.    else
  747.       dir = Expand(dir);
  748.  
  749.    if( Set_dir(dir) ) {
  750.       if( !ignore )
  751.          Fatal( "Unable to change to directory `%s', target is [%s]",
  752.             dir, name );
  753.       FREE(dir);
  754.       DB_RETURN( 0 );
  755.    }
  756.  
  757.    DB_PRINT( "dir", ("Push: [%s]", dir) );
  758.    if( Verbose & V_PRINT_DIR )
  759.       printf( "%s:  Changed to directory [%s]\n", Pname, dir  );
  760.  
  761.    FREE( dir );
  762.    TALLOC( new_dir, 1, STRING );
  763.    new_dir->st_next   = dir_stack;
  764.    dir_stack          = new_dir;
  765.    new_dir->st_string = _strdup( Pwd );
  766.  
  767.    Def_macro( "PWD", Get_current_dir(), M_MULTI | M_EXPANDED );
  768.    _set_tmd();
  769.  
  770.    DB_RETURN( 1 );
  771. }
  772.  
  773.  
  774.  
  775. PUBLIC void
  776. Pop_dir(ignore)/*
  777. =================
  778.    Change the current working directory to the previous saved dir. */
  779. int ignore;
  780. {
  781.    STRINGPTR old_dir;
  782.    char      *dir;
  783.  
  784.    DB_ENTER( "Pop_dir" );
  785.  
  786.    if( dir_stack == NIL(STRING) )
  787.       if( ignore ) {
  788.          DB_VOID_RETURN;
  789.       }
  790.       else
  791.      Error( "Directory stack empty for return from .SETDIR" );
  792.  
  793.    if( Set_dir(dir = dir_stack->st_string) )
  794.       Fatal( "Could not change to directory `%s'", dir );
  795.  
  796.    Def_macro( "PWD", dir, M_MULTI | M_EXPANDED );
  797.    DB_PRINT( "dir", ("Pop: [%s]", dir) );
  798.    if( Verbose & V_PRINT_DIR )
  799.       printf( "%s:  Changed back to directory [%s]\n", Pname, dir);
  800.  
  801.    old_dir   = dir_stack;
  802.    dir_stack = dir_stack->st_next;
  803.  
  804.    FREE( old_dir->st_string );
  805.    FREE( old_dir );
  806.    _set_tmd();
  807.  
  808.    DB_VOID_RETURN;
  809. }
  810.  
  811.  
  812.  
  813. static void
  814. _set_tmd()/*
  815. ============
  816.    Set the TWD Macro */
  817. {
  818.    TKSTR md, pd;
  819.    char  *m, *p;
  820.    char  *tmd;
  821.    int   is_sep;
  822.    int   first = 1;
  823.  
  824.    SET_TOKEN( &md, Makedir );
  825.    SET_TOKEN( &pd, Pwd );
  826.  
  827.    m = Get_token( &md, DirBrkStr, FALSE );
  828.    (void) Get_token( &pd, DirBrkStr, FALSE );
  829.    is_sep = (strchr(DirBrkStr, *m) != NIL(char));
  830.    tmd = _strdup( "" );
  831.  
  832.    do {
  833.       m = Get_token( &md, DirBrkStr, FALSE );
  834.       p = Get_token( &pd, DirBrkStr, FALSE );
  835.  
  836.       if( !is_sep && strcmp(m, p) ) {    /* they differ */
  837.      char *tmp;
  838.      if( first ) {        /* They differ in the first component    */
  839.         tmd = Makedir;    /* In this case use the full path    */
  840.         break;
  841.      }
  842.  
  843.      if( *p ) tmp = Build_path( "..", tmd );
  844.      if( *m ) tmp = Build_path( tmd, m );
  845.      FREE( tmd );
  846.      tmd = _strdup( tmp );
  847.       }
  848.  
  849.       is_sep = 1-is_sep;
  850.       first  = 0;
  851.    } while (*m || *p);
  852.  
  853.    CLEAR_TOKEN( &md );
  854.    CLEAR_TOKEN( &pd );
  855.  
  856.    Def_macro( "TMD", tmd, M_MULTI | M_EXPANDED );
  857.    if( tmd != Makedir ) FREE( tmd );
  858. }
  859.  
  860.  
  861. static void
  862. _set_recipe( target, ind )/*
  863. ============================
  864.    Set up the _recipes static variable so that the slot passed in points
  865.    at the rules corresponding to the target supplied. */
  866. char *target;
  867. int  ind;
  868. {
  869.    CELLPTR cp;
  870.    HASHPTR hp;
  871.  
  872.    if( (hp = Get_name(target, Defs, FALSE)) != NIL(HASH) ) {
  873.       cp = hp->CP_OWNR;
  874.       _recipes[ ind ] = cp->ce_recipe;
  875.    }
  876.    else
  877.       _recipes[ ind ] = NIL(STRING);
  878. }
  879.  
  880.  
  881.  
  882. PUBLIC void
  883. Append_line( cmnd, newline, tmpfile, name, printit, map )
  884. char *cmnd;
  885. int  newline;
  886. FILE *tmpfile;
  887. char *name;
  888. int  printit;
  889. int  map;
  890. {
  891.    _print_cmnd( cmnd, printit, map );
  892.  
  893.    if( Trace ) return;
  894.  
  895.    fputs(cmnd, tmpfile);
  896.    if( newline ) fputc('\n', tmpfile);
  897.    fflush(tmpfile);
  898.  
  899.    if( ferror(tmpfile) )
  900.       Fatal("Write error on temporary file, while processing `%s'", name);
  901. }
  902.  
  903.  
  904.  
  905. static void
  906. _append_file( rp, tmpfile, name, printit )
  907. register STRINGPTR rp;
  908. FILE            *tmpfile;
  909. char            *name;
  910. int            printit;
  911. {
  912.    char *cmnd;
  913.  
  914.    while( rp != NIL(STRING) ) {
  915.       Append_line(cmnd = Expand(rp->st_string), TRUE, tmpfile, name, printit,0);
  916.       FREE(cmnd);
  917.       rp = rp->st_next;
  918.    }
  919. }
  920.  
  921.  
  922. #define NUM_BUCKETS    20
  923.  
  924. typedef struct strpool {
  925.    char       *string;    /* a pointer to the string value */
  926.    uint32      keyval;    /* the strings hash value     */
  927.    struct strpool *next;    /* hash table link pointer     */
  928. } POOL, *POOLPTR;
  929.  
  930. static POOLPTR strings[ NUM_BUCKETS ];
  931.  
  932. static char *
  933. _pool_lookup( str )/*
  934. =====================
  935.    Scan down the list of chained strings and see if one of them matches
  936.    the string we are looking for. */
  937. char    *str;
  938. {
  939.    register POOLPTR key;
  940.    uint32   keyval;
  941.    uint16   hv;
  942.    uint16   keyindex;
  943.    char     *string;
  944.  
  945.    DB_ENTER( "_pool_lookup" );
  946.  
  947.    if( str == NIL(char) ) DB_RETURN("");
  948.  
  949.    hv  = Hash(str, &keyval);
  950.    key = strings[ keyindex = (hv % NUM_BUCKETS) ];
  951.  
  952.    while( key != NIL(POOL) )
  953.       if( (key->keyval != keyval) || strcmp(str, key->string) )
  954.      key = key->next;
  955.       else
  956.      break;
  957.  
  958.    if( key == NIL(POOL) ) {
  959.       DB_PRINT( "pool", ("Adding string [%s]", str) );
  960.       TALLOC( key, 1, POOL );            /* not found so add string */
  961.       
  962.       key->string = string = _strdup(str);
  963.       key->keyval = keyval;
  964.  
  965.       key->next           = strings[ keyindex ];
  966.       strings[ keyindex ] = key;
  967.    }
  968.    else {
  969.       DB_PRINT( "pool", ("Found string [%s], key->string") );
  970.       string = key->string;
  971.    }
  972.  
  973.    DB_RETURN( string );
  974. }
  975.