attic/timestamp-info: Fix a few warnings on recent Fedora.
[ntpsec.git] / wafhelpers / check_sizeof.py
blob3343abe2bda2cdeaad27a05d2a3539a72fa8c645
1 # Copyright the NTPsec project contributors
3 # SPDX-License-Identifier: BSD-2-Clause
5 from waflib.Configure import conf
6 from waflib import Errors
8 SIZE_FRAG = """
9 %s
10 #include <stdio.h>
11 int main(void) {
12 printf("%%lu", sizeof(%s));
13 return 0;
15 """
18 def check_sizeof_host(ctx, header, sizeof, mandatory=True):
19 sizeof_ns = sizeof.replace(" ", "_")
20 name = "NTP_SIZEOF_%s" % sizeof_ns.upper()
22 header_snippet = ""
23 if header:
24 ctx.start_msg("Checking sizeof %s (%s)" % (sizeof, header))
25 header_snippet = "#include <%s>" % header
26 else:
27 ctx.start_msg("Checking sizeof %s" % (sizeof))
29 ctx.check_cc(
30 fragment=SIZE_FRAG % (header_snippet, sizeof),
31 define_name=name,
32 execute=True,
33 define_ret=True,
34 quote=False,
35 mandatory=mandatory,
36 comment="Size of %s from <%s>" % (sizeof, header)
38 ctx.end_msg(ctx.get_define(name))
41 # Cross compile check. Much slower so we do not run it all the time.
43 SIZE_FRAG_CROSS = """
45 #include <sys/stat.h>
46 int main(void) {
47 static int test_array [1 - 2 * !(((long int) (sizeof (%s))) <= %d)];
48 test_array [0] = 0;
49 return test_array[0];
51 """
54 def check_sizeof_cross(ctx, header, sizeof, mandatory=True):
55 sizeof_ns = sizeof.replace(" ", "_")
56 name = "NTP_SIZEOF_%s" % sizeof_ns.upper()
58 header_snippet = ""
59 if header:
60 ctx.start_msg("Checking sizeof %s (%s)" % (sizeof, header))
61 header_snippet = "#include <%s>" % header
62 else:
63 ctx.start_msg("Checking sizeof %s" % (sizeof))
65 for size in range(2, 99):
67 try:
68 ctx.check_cc(
69 fragment=SIZE_FRAG_CROSS % (header_snippet, sizeof, size),
70 features="c",
71 execute=False,
72 mandatory=mandatory,
74 ctx.define(name, size, comment="Size of %s from <%s>"
75 % (sizeof, header))
76 ctx.end_msg(ctx.get_define(name))
77 return
78 except Errors.ConfigurationError:
79 pass
81 raise # never reached.
84 @conf
85 def check_sizeof(*kwargs):
86 if kwargs[0].env.ENABLE_CROSS:
87 check_sizeof_cross(*kwargs)
88 else:
89 check_sizeof_host(*kwargs)
92 # timex slots are documented as long
93 # #if (__TIMESIZE == 64 && __WORDSIZE == 32)
94 # they turn into long long
95 # This fails to build in the normal case.
96 # So we set NTP_TIMEX_LONG_LONG to 0
97 SIZE_FRAG_TIMEX = """
98 #include <sys/time.h> /* for NetBSD */
99 #include <sys/timex.h>
100 #include <stdio.h>
101 int main(void) {
102 struct timex dummy;
103 long long *foo = &dummy.jitter;
104 *foo = 1; /* supress unused warning */
105 if (*foo) printf("1");
106 return 0;
110 def check_timex(ctx):
111 name = "NTP_TIMEX_LONG_LONG"
112 ctx.start_msg("Checking for long long in struct timex" )
113 ctx.check_cc(
114 cflags="-Werror",
115 fragment=SIZE_FRAG_TIMEX,
116 define_name=name,
117 execute=not ctx.env.ENABLE_CROSS,
118 define_ret=True,
119 quote=False,
120 mandatory=False,
121 comment="struct timex has long long"
123 ctx.end_msg(ctx.get_define(name))