Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / build / linux-merge-modules.awk
1 #!/bin/awk -f
2 {
3         # lines in input look like ARCH TYPE path/to/TYPE/ARCH/modules/foo.ver
4         ARCH=$1
5         ARCHES[ARCH]=1
6         TYPE=$2
7         TYPES[TYPE]=1
8         NTOTAL++
9         NARCHES[TYPE]++
10         NTYPES[ARCH]++
11         FILE=$3
12
13         # read files that look like pairs of repeating
14         # #define __ver_foo hexstring
15         # #define foo _set_ver(foo)
16         while ((getline < FILE) > 0) {
17                 if ($0 ~ /^[    ]*$/)
18                         continue
19                 if ($1 != "#define" || $2 !~ /^__ver_/)
20                         exit 1
21
22                 # this is a "#define __ver_foo somehex" line
23                 SYMBOL=gensub(/^__ver_/,"","",$2)
24                 VALUE=gensub(/^(smp_|2gig_|smp2gig_)/,"","",$3)
25                 VALUE=gensub(/^(smp|2gig|smp2gig)/,"","",VALUE)
26                 values[SYMBOL,ARCH,TYPE]=VALUE
27
28                 # skip the "#define foo _set_ver(foo)" line
29                 if ((getline < FILE) <= 0)
30                         exit 2
31                 if ($1 != "#define" || $2 != SYMBOL || $3 != "_set_ver(" SYMBOL ")")
32                         exit 3
33         }
34         close(FILE)
35 }
36 END {
37         count=0
38         for (key in values)
39                 if (values[key]) {
40                         count++
41                         split(key,x,SUBSEP)
42                         SYMBOL=x[1]
43                         ARCH=x[2]
44                         TYPE=x[3]
45
46                         # (re)initialize a few arrays to have no elements
47                         split("",x)
48                         split("",ntype)
49                         split("",total)
50
51                         totalsum=0
52                         for (arch in ARCHES)
53                             for (type in TYPES)
54                                 if (values[SYMBOL,arch,type]) {
55                                     VALUE = values[SYMBOL,arch,type]
56                                     values[SYMBOL,arch,type] = ""
57                                     ntype[VALUE,type] += 1
58                                     total[VALUE] += 1
59                                     if (x[VALUE])
60                                         x[VALUE] = x[VALUE] " "
61                                     x[VALUE] = x[VALUE] arch ":" type
62                                 }
63                         ifstr="#if "
64                         for (VALUE in x) {
65                             if (total[VALUE] == NTOTAL) {
66                                 # there is only one checksum for this symbol
67                                 printf "#define __ver_%s\t_ver_str(%s)\n", SYMBOL, VALUE
68                                 printf "#define %s _set_ver(%s)\n", SYMBOL, SYMBOL
69                                 break
70                             }
71
72                             totalsum += total[VALUE]
73                             if (totalsum == NTOTAL && ifstr == "#elif") {
74                                 # this is the last unique checksum for this symbol
75                                 printf "#else\n#define __ver_%s\t_ver_str(%s)\n", SYMBOL, VALUE
76                                 printf "#define %s _set_ver(%s)\n", SYMBOL, SYMBOL
77                                 break
78                             }
79
80                             # there must be more than one checksum still to
81                             # print for this symbol
82                             str=""
83                             split(x[VALUE],y)
84                             for (type in TYPES)
85                                 if (ntype[VALUE,type] == NARCHES[type]) {
86                                     if (str) str = str " || "
87                                     str = str "defined(__module__" type ")"
88                                     for (k in y) {
89                                         split(y[k], z, ":")
90                                         if (z[2] == type)
91                                             delete y[k]
92                                     }
93                                 }
94                             for (arch in ARCHES) {
95                                 narch=0
96                                 for (k in y) {
97                                     split(y[k], z, ":")
98                                     if (z[1] == arch)
99                                         narch++
100                                 }
101                                 if (narch == NTYPES[arch]) {
102                                     if (str) str = str " || "
103                                     str = str "defined(__module__" arch ")"
104                                     for (k in y) {
105                                         split(y[k], z, ":")
106                                         if (z[1] == arch)
107                                             delete y[k]
108                                     }
109                                 }
110                             }
111                             for (k in y) {
112                                 split(y[k], z, ":")
113                                 if (str) str = str " || "
114                                 str = str "defined(__module__" z[1] "_" z[2] ")"
115                             }
116                             printf "%s %s\n#define __ver_%s\t_ver_str(%s)\n", ifstr, str, SYMBOL, VALUE
117                             printf "#define %s _set_ver(%s)\n", SYMBOL, SYMBOL
118                             ifstr="#elif "
119                         }
120                         if (ifstr == "#elif ")
121                             printf "#endif\n"
122                 }
123         if (!count)
124                 printf "\n"
125 }