From d9b5bf87e724bfbbad78fe254e902048f8d7e534 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Mon, 1 Jun 2020 14:55:52 -0700 Subject: [PATCH 6/7] Fixes and clean up of sizeof test --- src/libevent/SConscript | 115 ++++++++++------------------------------ 1 file changed, 28 insertions(+), 87 deletions(-) diff --git src/libevent/SConscript src/libevent/SConscript index ab1f72ad..db9884a6 100644 --- src/libevent/SConscript +++ src/libevent/SConscript @@ -17,88 +17,25 @@ def CheckSYS_TIMERFD_H(ctx): conf.AddTest('CheckSYS_TIMERFD_H', CheckSYS_TIMERFD_H) -# sizeof -# https://github.com/scons/scons/wiki/AutoconfRecipes#ac_check_sizeof -# Sensible default for common types on common platforms. -_DEFAULTS_SIZEOF = { - 'short' : [2], - 'int' : [4, 2], - 'long' : [4, 8], - 'long long' : [8, 4], - 'unsigned short' : [2], - 'unsigned int' : [4, 2], - 'unsigned long' : [4, 8], - 'unsigned long long' : [8, 4], - 'float' : [4], - 'double' : [8], - 'long double' : [12], - 'size_t' : [4, 8], -} - -def CheckSizeOf(ctx, type, includes = None): - """This check can be used to get the size of a given type, - or to check whether the type is of expected size. - - Arguments: - - type : str - the type to check - - includes : sequence - list of headers to include in the test code before testing the type - Returns: - status : int - 0 if the check failed, or the found size of the type - if the check succeeded.""" - minsz = 0 - maxsz = 16 - +def CheckSizeOf(ctx, type, defaults, includes = None): src = ''' - #include - #include - #include - #include + int main() { + int a[1 - 2 * (sizeof(%s) != %d)]; + return a[0] = 0; + } ''' - if includes: - src += "\n".join("#include <%s>\n" % i for i in includes) - - ext = '.c' - # test code taken from autoconf: this is a pretty clever hack to find that - # a type is of a given size using only compilation. This speeds things up - # quite a bit compared to straightforward code using TryRun - src += r""" -typedef %s scons_check_type; - -int main() -{ - static int test_array[1 - 2 * ((long int) sizeof(scons_check_type) > %d)]; - test_array[0] = 0; - return 0; -} -""" + if includes: + src = '\n'.join('#include <%s>' % i for i in includes) + '\n' + src - # Only check if the given size is the right one ctx.Message('Checking size of %s ... ' % type) - # Try sensible defaults first - try: - szrange = _DEFAULTS_SIZEOF[type] - except KeyError: - szrange = [] - szrange.extend(range(minsz, maxsz)) - st = 0 - - # Actual test - for sz in szrange: - st = ctx.TryCompile(src % (type, sz), ext) - if st: - break - - if st: - ctx.Result('%d' % sz) - return sz - else: - ctx.Result('Failed !') - return 0 + for sz in defaults + range(1, 16): + if ctx.TryCompile(src % (type, sz), '.c'): + ctx.Result(str(sz)) + return sz + + raise Exception('Failed to get the sizeof of ' + type) conf.AddTest('CheckSizeOf', CheckSizeOf) @@ -209,7 +146,17 @@ headers = '''arpa/inet.h fcntl.h ifaddrs.h inttypes.h mach/mach_time.h ''' decls = '''CTL_KERN KERN_ARND KERN_RANDOM RANDOM_UUID''' structs = 'addrinfo in6_addr sockaddr_in6 sockaddr_storage' -sizeof = ['int', 'long', 'long long', 'off_t', 'short', 'size_t', 'void *'] + +sizeof = [ + ('short', [2], None), + ('int', [4, 2], None), + ('long', [8, 4], None), + ('long long', [8, 4], None), + ('void *', [8, 4], None), + ('size_t', [8, 4], ['sys/types.h']), + ('off_t', [8, 4], ['sys/types.h']), + ('pthread_t', [8, 4], ['pthread.h']), +] gethostbyname_r_3_arg_src = ''' #ifndef __cplusplus @@ -284,13 +231,9 @@ def get_event_config_defs(): # Sizes - for type in sizeof: - size = conf.CheckSizeOf(type) - if size: - defs['SIZEOF_' + to_def(type)] = size - - size = conf.CheckSizeOf('pthread_t', ['pthread.h']) - if size: defs['SIZEOF_PTHREAD_T'] = size + for type, defaults, includes in sizeof: + size = conf.CheckSizeOf(type, defaults, includes) + defs['SIZEOF_' + to_def(type)] = size # Decls @@ -380,13 +323,11 @@ def build_function(target, source, env): f = open(target, 'w') - f.write('#ifndef EVENT2_EVENT_CONFIG_H_INCLUDED_\n') - f.write('#define EVENT2_EVENT_CONFIG_H_INCLUDED_\n') + f.write('#pragma once\n') for name, value in sorted(defs.items()): f.write('#define EVENT__%s %s\n' % (name, value)) - f.write('#endif /* EVENT2_EVENT_CONFIG_H_INCLUDED_ */\n') f.close() -- 2.27.0.rc1.5.gae92ac8ae3