Tizen 2.0 Release
[external/mawk.git] / examples / deps.awk
1 #!/usr/bin/mawk -f
2
3 # find include dependencies in C source
4 #
5 # mawk -f deps.awk  C_source_files
6 #         -- prints a dependency list suitable for make
7 #         -- ignores   #include <   >
8 #
9
10
11 BEGIN {  stack_index = 0 # stack[] holds the input files
12
13   for(i = 1 ; i < ARGC ; i++)
14   { 
15     file = ARGV[i]
16     if ( file !~ /\.[cC]$/ )  continue  # skip it
17     outfile = substr(file, 1, length(file)-2) ".o"
18
19     # INCLUDED[] stores the set of included files
20     # -- start with the empty set
21     for( j in INCLUDED ) delete INCLUDED[j]
22
23     while ( 1 )
24     {
25         if ( getline line < file <= 0 )  # no open or EOF
26         { close(file)
27           if ( stack_index == 0 )  break # empty stack
28           else  
29           { file = stack[ stack_index-- ]
30             continue
31           }
32         }
33
34         if ( line ~ /^#include[ \t]+".*"/ )
35         {
36           split(line, X, "\"")  # filename is in X[2]
37
38           if ( X[2] in INCLUDED ) # we've already included it
39                 continue
40
41           #push current file 
42           stack[ ++stack_index ] = file
43           INCLUDED[ file = X[2] ] = ""
44         }
45     }  # end of while
46     
47    # test if INCLUDED is empty
48    flag = 0 # on once the front is printed 
49    for( j in INCLUDED )
50       if ( ! flag )  
51       { printf "%s : %s" , outfile, j ; flag = 1 }
52       else  printf " %s" , j
53
54    if ( flag )  print ""
55
56   }# end of loop over files in ARGV[i]
57
58 }