[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / scripts / gen-tunables.awk
1 # Generate dl-tunable-list.h from dl-tunables.list
2
3 BEGIN {
4   min_of["STRING"]="0"
5   max_of["STRING"]="0"
6   min_of["INT_32"]="INT32_MIN"
7   max_of["INT_32"]="INT32_MAX"
8   min_of["UINT_64"]="0"
9   max_of["UINT_64"]="UINT64_MAX"
10   min_of["SIZE_T"]="0"
11   max_of["SIZE_T"]="SIZE_MAX"
12   tunable=""
13   ns=""
14   top_ns=""
15   max_name_len=0
16   max_alias_len=0
17 }
18
19 # Skip over blank lines and comments.
20 /^#/ {
21   next
22 }
23
24 /^[ \t]*$/ {
25   next
26 }
27
28 # Beginning of either a top namespace, tunable namespace or a tunable, decided
29 # on the current value of TUNABLE, NS or TOP_NS.
30 $2 == "{" {
31   if (top_ns == "") {
32     top_ns = $1
33   }
34   else if (ns == "") {
35     ns = $1
36   }
37   else if (tunable == "") {
38     tunable = $1
39   }
40   else {
41     printf ("Unexpected occurrence of '{': %s:%d\n", FILENAME, FNR)
42     exit 1
43   }
44
45   next
46 }
47
48 # End of either a top namespace, tunable namespace or a tunable.
49 $1 == "}" {
50   if (tunable != "") {
51     # Tunables definition ended, now fill in default attributes.
52     if (!types[top_ns,ns,tunable]) {
53       types[top_ns,ns,tunable] = "STRING"
54     }
55     if (!minvals[top_ns,ns,tunable]) {
56       minvals[top_ns,ns,tunable] = min_of[types[top_ns,ns,tunable]]
57     }
58     if (!maxvals[top_ns,ns,tunable]) {
59       maxvals[top_ns,ns,tunable] = max_of[types[top_ns,ns,tunable]]
60     }
61     if (!env_alias[top_ns,ns,tunable]) {
62       env_alias[top_ns,ns,tunable] = "{0}"
63     }
64     len = length(top_ns"."ns"."tunable)
65     if (len > max_name_len)
66       max_name_len = len
67
68     tunable = ""
69   }
70   else if (ns != "") {
71     ns = ""
72   }
73   else if (top_ns != "") {
74     top_ns = ""
75   }
76   else {
77     printf ("syntax error: extra }: %s:%d\n", FILENAME, FNR)
78     exit 1
79   }
80   next
81 }
82
83 # Everything else, which could either be a tunable without any attributes or a
84 # tunable attribute.
85 {
86   if (ns == "") {
87     printf("Line %d: Invalid tunable outside a namespace: %s\n", NR, $0)
88     exit 1
89   }
90
91   if (tunable == "") {
92     # We encountered a tunable without any attributes, so note it with a
93     # default.
94     types[top_ns,ns,$1] = "STRING"
95     next
96   }
97
98   # Otherwise, we have encountered a tunable attribute.
99   split($0, arr, ":")
100   attr = gensub(/^[ \t]+|[ \t]+$/, "", "g", arr[1])
101   val = gensub(/^[ \t]+|[ \t]+$/, "", "g", arr[2])
102
103   if (attr == "type") {
104     types[top_ns,ns,tunable] = val
105   }
106   else if (attr == "minval") {
107     minvals[top_ns,ns,tunable] = val
108   }
109   else if (attr == "maxval") {
110     maxvals[top_ns,ns,tunable] = val
111   }
112   else if (attr == "env_alias") {
113     env_alias[top_ns,ns,tunable] = sprintf("\"%s\"", val)
114     len = length(val)
115     if (len > max_alias_len)
116       max_alias_len = len
117   }
118   else if (attr == "default") {
119     if (types[top_ns,ns,tunable] == "STRING") {
120       default_val[top_ns,ns,tunable] = sprintf(".strval = \"%s\"", val);
121     }
122     else {
123       default_val[top_ns,ns,tunable] = sprintf(".numval = %s", val)
124     }
125   }
126 }
127
128 END {
129   if (ns != "") {
130     print "Unterminated namespace.  Is a closing brace missing?"
131     exit 1
132   }
133
134   print "/* AUTOGENERATED by gen-tunables.awk.  */"
135   print "#ifndef _TUNABLES_H_"
136   print "# error \"Do not include this file directly.\""
137   print "# error \"Include tunables.h instead.\""
138   print "#endif"
139   print "#include <dl-procinfo.h>\n"
140
141   # Now, the enum names
142   print "\ntypedef enum"
143   print "{"
144   for (tnm in types) {
145     split (tnm, indices, SUBSEP);
146     t = indices[1];
147     n = indices[2];
148     m = indices[3];
149     printf ("  TUNABLE_ENUM_NAME(%s, %s, %s),\n", t, n, m);
150   }
151   print "} tunable_id_t;\n"
152
153   print "\n#ifdef TUNABLES_INTERNAL"
154   # Internal definitions.
155   print "# define TUNABLE_NAME_MAX " (max_name_len + 1)
156   print "# define TUNABLE_ALIAS_MAX " (max_alias_len + 1)
157   print "# include \"dl-tunable-types.h\""
158   # Finally, the tunable list.
159   print "static tunable_t tunable_list[] attribute_relro = {"
160   for (tnm in types) {
161     split (tnm, indices, SUBSEP);
162     t = indices[1];
163     n = indices[2];
164     m = indices[3];
165     printf ("  {TUNABLE_NAME_S(%s, %s, %s)", t, n, m)
166     printf (", {TUNABLE_TYPE_%s, %s, %s}, {%s}, {%s}, false, %s},\n",
167             types[t,n,m], minvals[t,n,m], maxvals[t,n,m], default_val[t,n,m],
168             default_val[t,n,m], env_alias[t,n,m]);
169   }
170   print "};"
171   print "#endif"
172 }