Added cs to the list of languages
[midnight-commander.git] / src / depend.awk
blobec28b55fd9735b339768dea8fd1ffd3a03fc2413
1 #! /usr/bin/awk -f
2 # This is an awk script which does dependencies. We do NOT want it to
3 # recursively follow #include directives.
4 # We only add to dependencies those files which are inside of the rootdir
5 # tree :)
8 # Surely there is a more elegant way to see if a file exists. Anyone know
9 # what it is?
11 function fileExists(f, TMP, dummy, result) {
12 if(result=FILEHASH[f]) {
13 if(result=="Yes") {
14 return "Yes"
15 } else {return ""}
17 ERRNO = getline dummy < f
18 if(ERRNO >= 0) {
19 close(f)
20 return FILEHASH[f]="Yes"
21 } else {
22 FILEHASH[f]="No"
23 return ""
27 function Canonic(path) {
28 while (path ~ "/[^/]*/\\.\\./")
29 gsub("/[^/]*/\\.\\./","/",path)
30 return path
33 BEGIN{
34 hasdep=0
35 objprefix=""
36 USEDC=0
37 if(dolib) {
38 # dolib = "libdirectory libname"
39 split(dolib, dlib)
40 I=0
41 rootdir=srcdir
42 sub("/$","",rootdir)
43 sub("/[^/]*$","",rootdir)
44 while (getline > 0) {
45 if ($0 ~ "OBJS") {
46 objs=$0
47 } else if ($0 ~ "^/.*\\.h: \\\\$") {
48 sub(": \\\\$","",$0)
49 USED[USEDC]=$0
50 ++USEDC
53 sub("^OBJS=[ ]*\"[ ]*","",objs)
54 sub("\"[ ]*","",objs)
55 split(objs, obj)
56 printf "%s: ", dlib[2]
57 sub("/$","", dlib[1])
58 objprefix=dlib[1]"/"
59 for (fname in obj) {
60 fullname=dlib[1]"/"obj[fname]
61 printf " \\\n %s", fullname
62 sub("\\.o$",".c",obj[fname])
63 ARGV[ARGC]=obj[fname]
64 ++ARGC
66 printf "\n"
68 if(!hpath) {
69 print "hpath is not set"
70 exit 1
72 if(!srcdir) {
73 print "srcdir is not set"
74 exit 1
76 sub("[/ ]*$","",srcdir)
77 srcdir=srcdir"/"
78 sub("^\./$","",srcdir)
79 split(hpath, parray)
80 for(path in parray) {
81 sub("^-I","",parray[path])
82 sub("[/ ]*$","",parray[path])
83 parray[path]=Canonic(parray[path])
85 for(path in ARGV) {
86 USED[USEDC]=Canonic(srcdir""ARGV[path])
87 ++USEDC
91 /^#[ ]*include[ ]*[<"][^ ]*[>"]/{
92 found=0
93 if(LASTFILE!=FILENAME) {
94 if (hasdep) {
95 print cmd
96 hasdep=0
98 cmd=""
99 LASTFILE=FILENAME
100 depname=FILENAME
101 relpath=FILENAME
102 sub("\\.c$",".o: ",depname)
103 if (depname==FILENAME) {
104 depname=srcdir""depname
105 depname=Canonic(depname)
106 cmd="\n\t@touch "depname
107 } else
108 depname=objprefix""depname
109 sub("\\.h$",".h: ",depname)
110 if(relpath ~ "^\\." ) {
111 sub("[^/]*$","", relpath)
112 relpath=relpath"/"
113 sub("//","/", relpath)
114 } else {
115 relpath=""
118 fname=$0
119 sub("^#[ ]*include[ ]*[<\"]","",fname)
120 sub("[>\"].*","",fname)
121 if(fileExists(relpath""fname)) {
122 found=1
123 if (!hasdep) {
124 printf "%s", depname
125 hasdep=1
127 fullname=Canonic(srcdir""relpath""fname)
128 printf " \\\n %s", fullname
129 if(fname ~ "^\\." ) {
130 partname=relpath""fname
131 afound=0
132 for(name in USED) {
133 if (USED[name] == fullname) {
134 afound=1
135 break
138 if (!afound) {
139 ARGV[ARGC]=partname
140 ++ARGC
141 USED[USEDC]=fullname
142 ++USEDC
145 } else {
146 for(path in parray) {
147 if(fileExists(parray[path]"/"fname)) {
148 found=1
149 if (!hasdep) {
150 printf "%s", depname
151 hasdep=1
153 printf " \\\n %s", parray[path]"/"fname
159 END{
160 if (hasdep) {
161 print cmd