summaryrefslogtreecommitdiffstats
path: root/scripts/depend.awk
blob: 93cb0ebbce3af7856c513e3b2a6eabf28f0a6c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This is an awk script which does dependencies. We do NOT want it to
# recursively follow #include directives.
#
# The HPATH environment variable should be set to indicate where to look
# for include files.  The -I in front of the path is optional.

#
# Surely there is a more elegant way to see if a file exists.  Anyone know
# what it is?
#
function fileExists(f,    TMP, dummy, result) {
	if(result=FILEHASH[f]) {
		if(result=="Yes") {
			return "Yes"
		} else {return ""}
	}
	ERRNO = getline dummy < f
	if(ERRNO >= 0) {
		close(f)
		return FILEHASH[f]="Yes"
	} else {
		FILEHASH[f]="No"
		return ""
	}
}

function endfile(f) {
	if (hasconfig && !needsconfig) {
		printf "%s doesn't need config\n",f > "/dev/stderr"
	}
	if (hasdep) {
		print cmd
	}
}

BEGIN{
	hasdep=0
	hasconfig=0
	needsconfig=0
	incomment=0
	if(!(TOPDIR=ENVIRON["TOPDIR"])) {
		print "Environment variable TOPDIR is not set"
		exit 1
	}
	split(ENVIRON["HPATH"],parray," ")
	for(path in parray) {
	    sub("^-I","",parray[path])
	    sub("[/ ]*$","",parray[path])
	}
}

# eliminate comments
{
    # remove all comments fully contained on a single line
	gsub("\\/\\*.*\\*\\/", "")
	if (incomment) {
		if ($0 ~ /\*\//) {
			incomment = 0;
			gsub(".*\\*\\/", "")
		} else {
			next
		}
	} else {
		# start of multi-line comment
		if ($0 ~ /\/\*/)
		{
			incomment = 1;
			sub("\\/\\*.*", "")
		} else if ($0 ~ /\*\//) {
			incomment = 0;
			sub(".*\\*\\/", "")
		}
	}
}

/^[ 	]*#[ 	]*if.*[^A-Za-z_]CONFIG_/ {
	needsconfig=1
	if (!hasconfig) {
		printf "%s needs config but has not included config file\n",FILENAME > "/dev/stderr"
		# only say it once per file..
		hasconfig = 1
	}
}

/^[ 	]*#[ 	]*include[ 	]*[<"][^ 	]*[>"]/{
	found=0
	if(LASTFILE!=FILENAME) {
		endfile(LASTFILE)
		hasdep=0
		hasconfig=0
		needsconfig=0
		incomment=0
		cmd=""
		LASTFILE=FILENAME
		depname=FILENAME
		relpath=FILENAME
		sub("\\.c$",".o: ",depname)
		sub("\\.S$",".o: ",depname)
		if (depname==FILENAME) {
			cmd="\n\t@touch "depname
		}
		sub("\\.h$",".h: ",depname)
		if(relpath ~ "^\\." ) {
			sub("[^/]*$","",  relpath)
			relpath=relpath"/"
			sub("//","/",  relpath)
		} else {
			relpath=""
		}
	}
	fname=$0
	sub("^#[ 	]*include[ 	]*[<\"]","",fname)
	sub("[>\"].*","",fname)
	if (fname=="linux/config.h") {
		hasconfig=1
	}
	rfname=relpath""fname
	if(fileExists(rfname)) {
		found=1
		if (!hasdep) {
			printf "%s", depname
		}
		hasdep=1
		printf " \\\n   %s", rfname
		if(fname ~ "^\\." ) {
			fnd=0;
			for(i in ARGV) {
				if(ARGV[i]==rfname) {
					fnd=1
				}
			}
			if(fnd==0) {
				ARGV[ARGC]=rfname
				++ARGC
			}
		}
	} else {
		for(path in parray) {
			if(fileExists(parray[path]"/"fname)) {
				shortp=parray[path]
				found=1
				if (!hasdep) {
					printf "%s", depname
				}
				hasdep=1
				printf " \\\n   %s", parray[path]"/"fname
			}
		}
	}
}

END{
	endfile(FILENAME)
}