home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 581b.lha / genmake_v1.03 / build.c next >
C/C++ Source or Header  |  1991-11-10  |  4KB  |  157 lines

  1. /* build.c */
  2.  
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/dir.h>
  6. #include "types.h"
  7.  
  8. DNODE     *add_dep_node();
  9. FNODE    *add_file_node();
  10.  
  11. /* goes through the current directory build the dep list for each file */
  12.  
  13. build_depends(make_list,opts,mname)
  14. DNODE        **make_list;
  15. OPTS        *opts;
  16. char        *mname;
  17. {
  18.     DIR        *dirp;
  19.     struct direct    *dp;
  20.     DNODE        *new;
  21.     int        is_dep;
  22.     char        ask[BUFSIZE];
  23.  
  24.     if ((dirp = opendir( CURRENT_DIR )) == NULL) {
  25.         fprintf(stderr,"build_depends(): can't open directory\n");
  26.         return(0);
  27.     }
  28.     /* for each file in the directory */
  29.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
  30.         /* is it a recognisable source file */
  31.         if (!valid_source_file(dp->d_name))
  32.             continue;
  33.         if ((new = add_dep_node(make_list,dp->d_name)) == NULL) {
  34.             closedir(dirp);
  35.             return(0);
  36.         }
  37.         /* a file depends on itself */
  38.         add_file_node(new,dp->d_name);
  39.         /* create the dependency list for the current file */
  40.         if ((is_dep = build_dep_list(new,opts)) == 0) {
  41.             closedir(dirp);
  42.             return(0);
  43.         }
  44.         if (opts->exhaustive == 1 && is_dep == 1 && strcmp(new->name,mname)) {
  45.             fprintf(stderr,"Warning:  no one else uses file %s\n",new->name);
  46.             if (opts->interactive) {
  47.                 printf("Include file in Makefile(Y/N)? ");
  48.                 gets(ask);
  49.                 if (ask[0] != 'Y' && ask[0] != 'y')
  50.                     new->use_it = 0;
  51.             }
  52.             else
  53.                 new->use_it = 0;
  54.         }
  55.     }
  56.     closedir(dirp);
  57.     return(1);
  58. }
  59.  
  60. build_dep_list(dnode,opts)
  61. DNODE        *dnode;
  62. OPTS        *opts;
  63. {
  64.     FILE        *fp;
  65.     int        count = 0;
  66.     int        cp = 0;
  67.     int        stype;
  68.     int        cont;
  69.     char        buf[BUFSIZE];
  70.     char        func_name[BUFSIZE];
  71.     int        aok;
  72.     int        is_dep = 0;
  73.     CPPN        **cpp_stack;
  74.     SYMENT        **symtab;
  75.  
  76.     stype = valid_source_file(dnode->name);    
  77.     if (opts->exhaustive == 0 && stype == CSOURCE)
  78.         return(1);
  79.     if ((fp = fopen(dnode->name,"r")) == NULL) {
  80.         fprintf(stderr,"build_dep_list: can't open %s\n",dnode->name);
  81.         return(0);
  82.     }
  83.     cpp_stack = (CPPN **)malloc(sizeof(CPPN *));
  84.     *cpp_stack = NULL;
  85.     symtab = (SYMENT **)malloc(sizeof(SYMENT *));
  86.     *symtab = NULL;
  87.     buf[0] = '\0';
  88.     func_name[0] = '\0';
  89.     /* forever */
  90.     for (;;) {
  91.         cont = 1;
  92.         switch (stype) {
  93.             /* get the next function definition */    
  94.             case CSOURCE:    cont = parse_file(fp,func_name,&count,buf,&cp,cpp_stack,symtab);
  95.                     break;
  96.             default:    ;
  97.         }
  98.         /* if we are done with the file, stop */
  99.         if (!cont)
  100.             break;
  101.         /* add the dependencies */
  102.         if ((aok = depend_on_it(dnode,func_name)) == 0) {
  103.             cpp_rel(cpp_stack,symtab);
  104.             fclose(fp);
  105.             return(0);
  106.         }
  107.         if (aok == 2)
  108.             is_dep = 1;
  109.         if (stype == CHEADER)
  110.             break;
  111.     }
  112.     cpp_rel(cpp_stack,symtab);
  113.     fclose(fp);
  114.     return(is_dep+1);
  115. }
  116.  
  117. depend_on_it(dnode,func_name)
  118. DNODE        *dnode;
  119. char        *func_name;
  120. {
  121.     DIR        *dirp;
  122.     struct direct    *dp;
  123.     int        stype;
  124.     int        aok;
  125.     int        is_dep = 0;
  126.  
  127. #ifdef DEBUG
  128.     printf("depend_on_it():  dnode->name= '%s'\nfunc_name= '%s'\n",dnode->name,func_name);
  129. #endif
  130.     stype = valid_source_file(dnode->name);    
  131.     if ((dirp = opendir( CURRENT_DIR )) == NULL) {
  132.         fprintf(stderr,"depend_on_it():  Can't open directory\n");
  133.         return(0);
  134.     }
  135.     /* for each file in the directory */
  136.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
  137.         if (!valid_source_file(dp->d_name) || in_file_list(dnode,dp->d_name))
  138.             continue;
  139.         switch (stype) {
  140.             case CSOURCE:    aok = depends(dp->d_name,func_name);
  141.                     break;
  142.             case CHEADER:    aok = hdepends(dp->d_name,dnode->name);
  143.                     break;
  144.         }
  145.         /* if we found a dependency, add the current file to list */
  146.         if (aok) {
  147.             is_dep = 1;
  148.             if (add_file_node(dnode,dp->d_name) == NULL) {
  149.                 closedir(dirp);
  150.                 return(0);
  151.             }
  152.         }
  153.     }
  154.     closedir(dirp);
  155.     return(is_dep+1);
  156. }
  157.