home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d523 / bmake.lha / BMake / source.lzh / make.c < prev    next >
C/C++ Source or Header  |  1991-07-07  |  6KB  |  217 lines

  1. /*    make.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <fcntl.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12.  
  13. #include "make.h"
  14. #include "depend.h"
  15.  
  16. static int make_implicit( char *goalname, int *remake_flag );
  17.  
  18. /*
  19.  *        Get the modification time of a file.  If the first
  20.  *        doesn't exist, it's modtime is set to 0.
  21.  */
  22.  
  23. time_t
  24. modtime( char *filename )
  25. {
  26.     struct FileInfoBlock *fib;
  27.     BPTR myLock;
  28.     long ioErr;
  29.     time_t mtime = 0L;
  30.  
  31.     fib = (struct FileInfoBlock *)malloc( sizeof(struct FileInfoBlock));
  32.     if( fib ) {
  33.         if( !(myLock = Lock( filename, ACCESS_READ ))) {
  34.             if( (ioErr = IoErr()) != ERROR_OBJECT_NOT_FOUND)
  35.                 logprintf( "Can't Lock '%s'; error %ld\n", filename, ioErr );
  36.         }
  37.         else if( !Examine( myLock, fib )) {
  38.             UnLock(myLock);
  39.             logprintf( "Can't Examine '%s'; error %ld", filename, IoErr() );
  40.         }
  41.         else {
  42.             mtime = fib->fib_Date.ds_Tick / TICKS_PER_SECOND +
  43.                  60*fib->fib_Date.ds_Minute + 86400 * fib->fib_Date.ds_Days;
  44.             UnLock( myLock );
  45.         }
  46.         free( fib );
  47.     }
  48.     return( mtime );
  49. }
  50.  
  51. /* return true if targ1 is newer than targ2 OR if either is missing */
  52. int
  53. isnewer( char *targ1, char *targ2 )
  54. {
  55.     time_t t1, t2;
  56.     long diff;
  57.     int retval;
  58.  
  59.     t1 = modtime( targ1 );
  60.     t2 = modtime( targ2 );
  61.     if( !t1 || !t2 ) retval = 1;
  62.     else {
  63.         diff = (long)(t1 - t2);
  64.         retval = (diff > 0L) ? 1 : 0;
  65.     }
  66.  
  67.     debugprintf( 5, ("isnewer %s=%ld,%s=%ld diff=%ld: %s\n", targ1, t1,
  68.         targ2, t2, diff, (retval) ? "yes" : "no" ));
  69.  
  70.     return( retval );
  71. }
  72.  
  73. /* recursively make a target filename */
  74. int
  75. make_filename( const char *goalname, int *made )
  76. {
  77.     int remake_flag = (Param.all_mode) ? 1 : 0;    
  78.     struct depend *dep;
  79.     struct List *cmdlist = NULL;
  80.     struct target *goal = find_target( goalname );
  81.     char *depname = NULL;
  82.  
  83.     Global.recursion_level++;
  84.     *made = (Param.all_mode) ? 1 : 0;
  85.  
  86.     if( Global.recursion_level == 1 ) {
  87.         logprintf( "\tmake( %s )\n", goalname );
  88.     }
  89.     else  {
  90.         debugprintf( 1, ( "\n\tmake %d ( %s )\n", Global.recursion_level,
  91.         goalname ));
  92.     }
  93.     if( goal ) {
  94.         if( (goal->flags & TF_NEVER) ||
  95.             ((goal->flags & (TF_ONCE | TF_MADE)) == (TF_ONCE | TF_MADE)) ) {
  96.             logprintf( "\tskipping %s\n", goalname );
  97.             return( 0 );
  98.         }
  99.         if( goal->dependlist.lh_Head->ln_Succ ) {
  100.             for( dep = (struct depend *)goal->dependlist.lh_Head; 
  101.                 dep->node.ln_Succ; dep = dep->node.ln_Succ ) {
  102.                 int made_flag = 0;
  103.  
  104.                 debugprintf( 1, ("%s depends on %s\n", goalname, dep->name ));
  105.                 if( make_filename( dep->name, &made_flag )) return( 1 );
  106.                 if( made_flag || (!(goal->flags & TF_PHONY) &&
  107.                     isnewer( dep->name, goalname ))) {
  108.                     depname = dep->name;
  109.                     remake_flag = 1;
  110.                     debugprintf( 4, ("%s was made\n", dep->name ));
  111.                 }
  112.             } /* for */
  113.         } 
  114.         else remake_flag = 1; /* has no dependencies */
  115.         cmdlist = (goal->flags & TF_OWNER ) ? &goal->commandlist :
  116.             goal->alternate;
  117.     } /* if */
  118.  
  119.     /* if no explicit rule for goal OR goal has no commands */
  120.     if( !goal || (!cmdlist->lh_Head->ln_Succ && !(goal->flags & TF_PHONY))) {
  121.         int retval = make_implicit( goalname, &remake_flag );
  122.         --Global.recursion_level;
  123.         if( retval == -1 ) {
  124.             if( !goal ) logprintf( "don't know how to make %s\n", goalname );
  125.             else retval = 0;
  126.         }
  127.         *made = remake_flag;
  128.         return( retval );
  129.     }
  130.     else {
  131.         --Global.recursion_level;
  132.         if( (remake_flag || goal->flags & TF_ALWAYS)
  133.             && cmdlist->lh_Head->ln_Succ ) {
  134.             int retval;
  135.  
  136.             set_target_macros( goalname, depname );
  137.             retval = recipe( goalname, cmdlist );
  138.             set_target_macros( NULL, NULL );
  139.             if( retval ) return( 1 );
  140.             goal->flags |= TF_MADE;
  141.         }
  142.         else if( !remake_flag ) {
  143.             debugprintf( 2,("%s is up to date\n", goal->name ));
  144.         }
  145.     }
  146.     if( !(goal->flags & TF_INVIS) ) *made = remake_flag;
  147.     debugprintf( 4, ("%s remake_flag = %d\n", goalname, remake_flag ));
  148.     return( 0 );
  149. }
  150.  
  151. /*    use this inference engine as a last resort
  152.  *    use suffix rules to determine dependencies and commands for goalname
  153.  */
  154. static int
  155. make_implicit( char *goalname, int *remake_flag )
  156. {
  157.     char *depfile = (char *)malloc( MAXPATHNAME );
  158.     struct patternrule *pr;
  159.     struct List *cmdlist;
  160.     struct target *goal;
  161.     int makeit_flag = (Param.all_mode) ? 1 : *remake_flag;
  162.  
  163.     debugprintf( 1, ("\tmake_implicit( %s,%d)\n", goalname, *remake_flag ));
  164.                 
  165.     if( !depfile ) goto death; /* no mem */
  166.  
  167.     *depfile = (char)0;
  168.     for( pr = &Global.patternlist.lh_Head; pr->node.ln_Succ;
  169.         pr = pr->node.ln_Succ ) {
  170.         if( pattern_match( pr->tar_pat, goalname )) {
  171.             debugprintf( 2, ("Matched Pattern rule %s: %s to %s\n",
  172.                 pr->tar_pat, pr->dep_pat, goalname ));
  173.             if( *pr->dep_pat ) {
  174.                 map_to_pattern( depfile, pr->tar_pat, pr->dep_pat, goalname );
  175.                 if( !access( depfile, 0 )) { /* found it */
  176.                     int made_flag;
  177.  
  178.                     debugprintf( 2, ("double pattern rule matches %s\n",
  179.                         depfile ));
  180.                     if( make_filename( depfile, &made_flag )) goto death;
  181.                     if( made_flag || isnewer( depfile, goalname ))
  182.                         makeit_flag = 1;
  183.                     break; /* found it */
  184.                 } /* else not the right rule, continue to find another */
  185.             }
  186.             else {
  187.                 debugprintf( 2, ("single suffix rule applies\n" ));
  188.                 makeit_flag = 1;
  189.                 break; /* found it */
  190.             }
  191.         } /* if */
  192.     } /* for each suffix rule */
  193.  
  194.     goal = ( pr->node.ln_Succ ) ? pr->targ : default_target;
  195.     if( !goal ) {
  196.         free( depfile );
  197.         debugprintf( 4, ("returning -1, remake_flag = %d\n", *remake_flag ));
  198.         return( -1 );
  199.     }
  200.     if( makeit_flag ) {
  201.         cmdlist = (goal->flags & TF_OWNER ) ?
  202.             &goal->commandlist : goal->alternate;
  203.         if( cmdlist && cmdlist->lh_Head->ln_Succ ) {
  204.             set_target_macros( goalname, depfile );
  205.             if( recipe( goalname, cmdlist )) goto death;
  206.             set_target_macros( NULL, NULL );
  207.             if( !(goal->flags & TF_INVIS) ) *remake_flag = 1;
  208.         }
  209.     }
  210.     free( depfile );
  211.     debugprintf( 4, ("implicit %s remake_flag = %d\n", goalname, *remake_flag ));
  212.     return( 0 );
  213. death:
  214.     if( depfile ) free( depfile );
  215.     return( 1 );
  216. }
  217.