""" This script asserts that Python config vars with compiler options including one or more -O flags have the last specified -O flag equal to the script's first argument. We use it to check that the debug build (as well as extension modules) was built with a desired optimization level (usually -Og or -O0). About -O flags: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html "If you use multiple -O options, with or without level numbers, the last such option is the one that is effective." """ import sys import sysconfig # We will check all flags if none were requested KEYS_TO_CHECK = sys.argv[2:] or list(sysconfig.get_config_vars().keys()) # For backwards compatibility, if no flags were provided, we assume flags without -O are to be skipped # But when we provide explicit list of flags, we assert they get the options, # so we can assert things like "CFLAGS has -O3" vs. "CFLAGS has no -O at all" NO_FLAG_FAILS = bool(sys.argv[2:]) # The flags that currently don't have the -Og flag on the debug build # and we consider it OK, because we don't know any better :) SKIP = [ 'CONFIGURE_CFLAGS', 'CONFIGURE_CFLAGS_NODIST', 'CONFIG_ARGS', 'OPT', ] print('Expecting that {} is the last -O flag:\n'.format(sys.argv[1])) ret = 0 for key in KEYS_TO_CHECK: if key in SKIP: continue flags = sysconfig.get_config_vars()[key] if isinstance(flags, str): oflags = [f for f in flags.split(' ') if f.startswith('-O')] if (oflags and oflags[-1] != sys.argv[1]) or (not oflags and NO_FLAG_FAILS): print('Problem in {} -O flags: {}'.format(key, ' '.join(oflags) or '')) ret = 1 elif oflags: print('{} are OK'.format(key)) sys.exit(ret)