Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / build / linux-merge-config.awk
1 #!/bin/awk -f
2 BEGIN {
3         nsects = 0
4 }
5 {
6         ARCH = $1
7         ARCHES[ARCH] = 1
8         TYPE = $2
9         TYPES[TYPE] = 1
10         NTOTAL++
11         ARCHTYPES[ARCH ":" TYPE] = 1
12         NARCHES[TYPE]++
13         if (NARCHES[TYPE] == 1)
14             NTOTALTYPES++
15         NTYPES[ARCH]++
16         if (NTYPES[ARCH] == 1)
17             NTOTALARCHES++
18         FILE = $3
19         cursects = nsects
20         while ((getline < FILE) > 0) {
21                 if ($0 ~ /^\/\*/ || $0 ~ /^ \*\// || $0 ~ /^[   ]*$/)
22                         continue
23                 if ($0 ~ /^ * /) {
24                         SECTION = gensub(/^ \* /,"",$0)
25                         if (!(SECTION in sectno)) {
26                                 sectno[SECTION] = nsects
27                                 counts[SECTION] = 0
28                                 nsects++
29                         } else if (cursects && cursects != nsects) {
30                                 no = sectno[SECTION]
31                                 diff = nsects - cursects
32                                 for (s in sectno) {
33                                         if (sectno[s] >= cursects)
34                                                 sectno[s] = sectno[s] - cursects + no
35                                         else if (sectno[s] >= no)
36                                                 sectno[s] += diff
37                                 }
38                         }
39                         cursects = nsect
40                         cursym[SECTION] = counts[SECTION]
41                         continue
42                 }
43                 if ($1 != "#define" && $1 != "#undef")
44                         exit 1
45                 SYMBOL = $2
46                 n = index($0,SYMBOL)+length(SYMBOL)
47                 if ($1 == "#define") {
48                         n = index($0,SYMBOL)+length(SYMBOL)
49                         VALUE = gensub(/^[      ]*/,"","",substr($0,n))
50                         if (VALUE == "") VALUE = "__novalue__"
51                 } else
52                         VALUE = "__undefined__"
53                 if (values[SYMBOL]) {
54                         if (present[SYMBOL,ARCH,TYPE]) continue
55                         present[SYMBOL,ARCH,TYPE] = 1
56                         values[SYMBOL] = values[SYMBOL] SUBSEP ARCH ":" TYPE ":" VALUE
57                         if (SECTION == sections[SYMBOL] && cursym[SECTION] && cursym[SECTION] != counts[SECTION]) {
58                                 no = pos[SYMBOL]
59                                 diff = counts[SECTION]-cursym[SECTION]
60                                 for (s in pos)
61                                         if (sections[s] == SECTION) {
62                                                 if (pos[s] >= cursym[SECTION])
63                                                         pos[s] = pos[s] - cursym[SECTION] + no
64                                                 else if (pos[s] >= no)
65                                                         pos[s] += diff
66                                         }
67                                 cursym[SECTION] = counts[SECTION]
68                         }
69                 } else {
70                         present[SYMBOL,ARCH,TYPE] = 1
71                         values[SYMBOL] = ARCH ":" TYPE ":" VALUE
72                         sections[SYMBOL] = SECTION
73                         pos[SYMBOL] = counts[SECTION]
74                         counts[SECTION]++
75                 }
76         }
77         close(FILE)
78 }
79 END {
80         for (SECTION in sectno)
81                 x[sectno[SECTION]] = SECTION
82         for (i = 0; i < nsects; i++) {
83                 SECTION = x[i]
84                 if (i > 0)
85                         printf "\n"
86                 printf "/*\n * %s\n */\n", SECTION
87                 split("",lines)
88                 lastelse = ""
89                 for (SYMBOL in sections)
90                         if (sections[SYMBOL] == SECTION)
91                                 y[pos[SYMBOL]] = SYMBOL
92                 for (j = 0; j < counts[SECTION]; j++) {
93                         SYMBOL = y[j]
94                         split("",ntype)
95                         split("",total)
96                         split(values[SYMBOL],z,SUBSEP)
97                         split("",val)
98                         totalsum = 0
99                         for (k in z) {
100                                 split(z[k],l,":")
101                                 ARCH = l[1]
102                                 TYPE = l[2]
103                                 VALUE = substr(z[k],length(ARCH)+length(TYPE)+3)
104                                 if (val[VALUE])
105                                         val[VALUE] = val[VALUE] " "
106                                 val[VALUE] = val[VALUE] ARCH ":" TYPE
107                                 ntype[VALUE,TYPE] += 1
108                                 total[VALUE] += 1
109                                 totalsum += 1
110                         }
111                         split("",curlines)
112                         append = 1
113                         for (VALUE in val) {
114                             if (total[VALUE] == NTOTAL) {
115                                 if (VALUE == "__undefined__")
116                                     curlines["1"] = "#undef  " SYMBOL "\n"
117                                 else if (VALUE == "__novalue__")
118                                     curlines["1"] = "#define " SYMBOL "\n"
119                                 else
120                                     curlines["1"] = "#define " SYMBOL " " VALUE "\n"
121                                 if (!lines["1"])
122                                     append = 0
123                                 break
124                             }
125                             shorteststr = ""
126                             curcount = 0
127                             for (m = 0; m < 4; m++) {
128                                 str = ""
129                                 split(val[VALUE],yy)
130                                 if (total[VALUE] > 1 && total[VALUE] == NTOTAL - 1) {
131                                     found = 0
132                                     for (arch in ARCHES) {
133                                         for (type in TYPES) {
134                                             archtype = arch ":" type
135                                             if (ARCHTYPES [archtype] == 1) {
136                                                 for (n in yy)
137                                                     if (yy[n] == archtype)
138                                                         break
139                                                 if (yy[n] != archtype) {
140                                                     found = 1
141                                                     break
142                                                 }
143                                             }
144                                         }
145                                         if (found)
146                                             break
147                                     }
148                                     if (NARCHES[type] > 1 && NTYPES[arch] > 1) {
149                                         str = "!defined(__module__" arch "_" type ")"
150                                         shorteststr = str
151                                         break
152                                     }
153                                 }
154                                 if (m == 0 || m == 2) {
155                                     nfull = 0
156                                     split("",yysave)
157                                     for (type in TYPES)
158                                         if (ntype[VALUE,type] == NARCHES[type]) {
159                                             if (str) str = str " || "
160                                             str = str "defined(__module__" type ")"
161                                             for (k in yy) {
162                                                 split(yy[k], z, ":")
163                                                 if (z[2] == type) {
164                                                     yysave[k] = yy[k]
165                                                     delete yy[k]
166                                                 }
167                                             }
168                                             nfull++
169                                         } else
170                                             NOTYPE = type
171                                     if (m < 2 && nfull > 1 && nfull == NTOTALTYPES - 1) {
172                                         str = "!defined(__module__" NOTYPE ")"
173                                         for (k in yysave)
174                                             yy[k] = yysave[k]
175                                         for (k in yy) {
176                                             split(yy[k], z, ":")
177                                             if (z[2] != NOTYPE)
178                                                 delete yy[k]
179                                         }
180                                     }
181                                 }
182                                 savestr = str
183                                 nfull = 0
184                                 split("",yysave)
185                                 for (arch in ARCHES) {
186                                     narch = 0
187                                     for (k in yy) {
188                                         split(yy[k], z, ":")
189                                         if (z[1] == arch)
190                                             narch++
191                                     }
192                                     if (narch == NTYPES[arch]) {
193                                         if (str) str = str " || "
194                                         str = str "defined(__module__" arch ")"
195                                         for (k in yy) {
196                                             split(yy[k], z, ":")
197                                             if (z[1] == arch) {
198                                                 yysave[k] = yy[k]
199                                                 delete yy[k]
200                                             }
201                                         }
202                                         nfull++
203                                     } else
204                                         NOARCH = arch
205                                 }
206                                 if (m < 2 && nfull > 1 && nfull == NTOTALARCHES - 1) {
207                                     str = savestr
208                                     for (k in yysave)
209                                         yy[k] = yysave[k]
210                                     if (str) str = str " || "
211                                     str = str "!defined(__module__" NOARCH ")"
212                                     for (k in yy) {
213                                         split(yy[k], z, ":")
214                                         if (z[1] != NOARCH)
215                                             delete yy[k]
216                                     }
217                                 }
218                                 if (m == 1 || m == 3) {
219                                     savestr = str
220                                     nfull = 0
221                                     split("",yysave)
222                                     for (type in TYPES) {
223                                         ntypex = 0
224                                         for (k in yy) {
225                                             split(yy[k], z, ":")
226                                             if (z[2] == type)
227                                                 ntypex++
228                                         }
229                                         if (ntypex == NARCHES[type]) {
230                                             if (str) str = str " || "
231                                             str = str "defined(__module__" type ")"
232                                             for (k in yy) {
233                                                 split(yy[k], z, ":")
234                                                 if (z[2] == type) {
235                                                     yysave[k] = yy[k]
236                                                     delete yy[k]
237                                                 }
238                                             }
239                                             nfull++
240                                         } else
241                                             NOTYPE = type
242                                     }
243                                     if (m < 2 && nfull > 1 && nfull == NTOTALTYPES - 1) {
244                                         str = savestr
245                                         for (k in yysave)
246                                             yy[k] = yysave[k]
247                                         if (str) str = str " || "
248                                         str = "!defined(__module__" NOTYPE ")"
249                                         for (k in yy) {
250                                             split(yy[k], z, ":")
251                                             if (z[2] != NOTYPE)
252                                                 delete yy[k]
253                                         }
254                                     }
255                                 }
256                                 for (k in yy) {
257                                     split(yy[k], z, ":")
258                                     if (str) str = str " || "
259                                     str = str "defined(__module__" z[1] "_" z[2] ")"
260                                 }
261                                 if (m == 0 || length(str) < length(shorteststr))
262                                     shorteststr = str
263                             }
264                             str = shorteststr
265                             if (VALUE == "__undefined__")
266                                 curlines[str] = "#undef  " SYMBOL "\n"
267                             else if (VALUE == "__novalue__")
268                                 curlines[str] = "#define " SYMBOL "\n"
269                             else
270                                 curlines[str] = "#define " SYMBOL " " VALUE "\n"
271                             if (!lines[str])
272                                 append = 0
273                         }
274                         if (append) {
275                             for (str in curlines)
276                                 if (curlines[str])
277                                     lines[str] = lines[str] curlines[str]
278                         } else {
279                             if (lines["1"])
280                                 printf "%s", lines["1"]
281                             else if (j > 0) {
282                                 ifstr = "#if "
283                                 for (str in lines)
284                                     if (lines[str] && str != lastelse) {
285                                         printf "%s %s\n%s", ifstr, str, lines[str]
286                                         ifstr = "#elif "
287                                     }
288                                 if (lastelse != "")
289                                     printf "#else\n%s", lines[lastelse]
290                                 printf "#endif\n"
291                             }
292                             split("",lines)
293                             lastelse = ""
294                             for (str in curlines)
295                                 if (curlines[str]) {
296                                     lines[str] = curlines[str]
297                                     if (totalsum == NTOTAL && length(str) > length(lastelse)) {
298                                         lastelse = str
299                                     }
300                                 }
301                         }
302                 }
303                 if (lines["1"])
304                     printf "%s", lines["1"]
305                 else if (j > 0) {
306                     ifstr = "#if "
307                     for (str in lines)
308                         if (lines[str] && str != lastelse) {
309                             printf "%s %s\n%s", ifstr, str, lines[str]
310                             ifstr = "#elif "
311                         }
312                     if (lastelse != "")
313                         printf "#else\n%s", lines[lastelse]
314                     printf "#endif\n"
315                 }
316         }
317 }