1# This is an awk script which does dependencies. We do NOT want it to 2# recursively follow #include directives. 3# 4# The HPATH environment variable should be set to indicate where to look 5# for include files. The -I in front of the path is optional. 6 7# 8# Surely there is a more elegant way to see if a file exists. Anyone know 9# what it is? 10# 11function fileExists(f, TMP, dummy, result) { 12 if(result=FILEHASH[f]) { 13 if(result=="Yes") { 14 return "Yes" 15 } else {return ""} 16 } 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 "" 24 } 25} 26 27function endfile(f) { 28 if (hasconfig && !needsconfig) { 29 printf "%s doesn't need config\n",f > "/dev/stderr" 30 } 31 if (hasdep) { 32 print cmd 33 } 34} 35 36BEGIN{ 37 hasdep=0 38 hasconfig=0 39 needsconfig=0 40 incomment=0 41 if(!(TOPDIR=ENVIRON["TOPDIR"])) { 42 print "Environment variable TOPDIR is not set" 43 exit 1 44 } 45 split(ENVIRON["HPATH"],parray," ") 46 for(path in parray) { 47 sub("^-I","",parray[path]) 48 sub("[/ ]*$","",parray[path]) 49 } 50} 51 52# eliminate comments 53{ 54 # remove all comments fully contained on a single line 55 gsub("\\/\\*.*\\*\\/", "") 56 if (incomment) { 57 if ($0 ~ /\*\//) { 58 incomment = 0; 59 gsub(".*\\*\\/", "") 60 } else { 61 next 62 } 63 } else { 64 # start of multi-line comment 65 if ($0 ~ /\/\*/) 66 { 67 incomment = 1; 68 sub("\\/\\*.*", "") 69 } else if ($0 ~ /\*\//) { 70 incomment = 0; 71 sub(".*\\*\\/", "") 72 } 73 } 74} 75 76/^[ ]*#[ ]*if.*[^A-Za-z_]CONFIG_/ { 77 needsconfig=1 78 if (!hasconfig) { 79 printf "%s needs config but has not included config file\n",FILENAME > "/dev/stderr" 80 # only say it once per file.. 81 hasconfig = 1 82 } 83} 84 85/^[ ]*#[ ]*include[ ]*[<"][^ ]*[>"]/{ 86 found=0 87 if(LASTFILE!=FILENAME) { 88 endfile(LASTFILE) 89 hasdep=0 90 hasconfig=0 91 needsconfig=0 92 incomment=0 93 cmd="" 94 LASTFILE=FILENAME 95 depname=FILENAME 96 relpath=FILENAME 97 sub("\\.c$",".o: ",depname) 98 sub("\\.S$",".o: ",depname) 99 if (depname==FILENAME) { 100 cmd="\n\t@touch "depname 101 } 102 sub("\\.h$",".h: ",depname) 103 if(relpath ~ "^\\." ) { 104 sub("[^/]*$","", relpath) 105 relpath=relpath"/" 106 sub("//","/", relpath) 107 } else { 108 relpath="" 109 } 110 } 111 fname=$0 112 sub("^#[ ]*include[ ]*[<\"]","",fname) 113 sub("[>\"].*","",fname) 114 if (fname=="linux/config.h") { 115 hasconfig=1 116 } 117 rfname=relpath""fname 118 if(fileExists(rfname)) { 119 found=1 120 if (!hasdep) { 121 printf "%s", depname 122 } 123 hasdep=1 124 printf " \\\n %s", rfname 125 if(fname ~ "^\\." ) { 126 fnd=0; 127 for(i in ARGV) { 128 if(ARGV[i]==rfname) { 129 fnd=1 130 } 131 } 132 if(fnd==0) { 133 ARGV[ARGC]=rfname 134 ++ARGC 135 } 136 } 137 } else { 138 for(path in parray) { 139 if(fileExists(parray[path]"/"fname)) { 140 shortp=parray[path] 141 found=1 142 if (!hasdep) { 143 printf "%s", depname 144 } 145 hasdep=1 146 printf " \\\n %s", parray[path]"/"fname 147 } 148 } 149 } 150} 151 152END{ 153 endfile(FILENAME) 154} 155

