|
1 | 1 | import math
|
2 | 2 | import os.path
|
3 | 3 | import sys
|
| 4 | +import sysconfig |
4 | 5 | import textwrap
|
5 | 6 | from test import support
|
6 | 7 |
|
@@ -208,3 +209,87 @@ def clear_caches():
|
208 | 209 | pass
|
209 | 210 | else:
|
210 | 211 | fractions._hash_algorithm.cache_clear()
|
| 212 | + |
| 213 | + |
| 214 | +def get_build_info(): |
| 215 | + # Get most important configure and build options as a list of strings. |
| 216 | + # Example: ['debug', 'ASAN+MSAN'] or ['release', 'LTO+PGO']. |
| 217 | + |
| 218 | + config_args = sysconfig.get_config_var('CONFIG_ARGS') or '' |
| 219 | + cflags = sysconfig.get_config_var('PY_CFLAGS') or '' |
| 220 | + cflags_nodist = sysconfig.get_config_var('PY_CFLAGS_NODIST') or '' |
| 221 | + ldflags_nodist = sysconfig.get_config_var('PY_LDFLAGS_NODIST') or '' |
| 222 | + |
| 223 | + build = [] |
| 224 | + if hasattr(sys, 'gettotalrefcount'): |
| 225 | + # --with-pydebug |
| 226 | + build.append('debug') |
| 227 | + |
| 228 | + if '-DNDEBUG' in (cflags + cflags_nodist): |
| 229 | + build.append('without_assert') |
| 230 | + else: |
| 231 | + build.append('release') |
| 232 | + |
| 233 | + if '--with-assertions' in config_args: |
| 234 | + build.append('with_assert') |
| 235 | + elif '-DNDEBUG' not in (cflags + cflags_nodist): |
| 236 | + build.append('with_assert') |
| 237 | + |
| 238 | + # --enable-framework=name |
| 239 | + framework = sysconfig.get_config_var('PYTHONFRAMEWORK') |
| 240 | + if framework: |
| 241 | + build.append(f'framework={framework}') |
| 242 | + |
| 243 | + # --enable-shared |
| 244 | + shared = int(sysconfig.get_config_var('PY_ENABLE_SHARED') or '0') |
| 245 | + if shared: |
| 246 | + build.append('shared') |
| 247 | + |
| 248 | + # --with-lto |
| 249 | + optimizations = [] |
| 250 | + if '-flto=thin' in ldflags_nodist: |
| 251 | + optimizations.append('ThinLTO') |
| 252 | + elif '-flto' in ldflags_nodist: |
| 253 | + optimizations.append('LTO') |
| 254 | + |
| 255 | + # --enable-optimizations |
| 256 | + pgo_options = ( |
| 257 | + # GCC |
| 258 | + '-fprofile-use', |
| 259 | + # clang: -fprofile-instr-use=code.profclangd |
| 260 | + '-fprofile-instr-use', |
| 261 | + # ICC |
| 262 | + "-prof-use", |
| 263 | + ) |
| 264 | + if any(option in cflags_nodist for option in pgo_options): |
| 265 | + optimizations.append('PGO') |
| 266 | + if optimizations: |
| 267 | + build.append('+'.join(optimizations)) |
| 268 | + |
| 269 | + # --with-address-sanitizer |
| 270 | + sanitizers = [] |
| 271 | + if support.check_sanitizer(address=True): |
| 272 | + sanitizers.append("ASAN") |
| 273 | + # --with-memory-sanitizer |
| 274 | + if support.check_sanitizer(memory=True): |
| 275 | + sanitizers.append("MSAN") |
| 276 | + # --with-undefined-behavior-sanitizer |
| 277 | + if support.check_sanitizer(ub=True): |
| 278 | + sanitizers.append("UBSAN") |
| 279 | + if sanitizers: |
| 280 | + build.append('+'.join(sanitizers)) |
| 281 | + |
| 282 | + # --with-trace-refs |
| 283 | + if hasattr(sys, 'getobjects'): |
| 284 | + build.append("TraceRefs") |
| 285 | + # --enable-pystats |
| 286 | + if hasattr(sys, '_stats_on'): |
| 287 | + build.append("pystats") |
| 288 | + # --with-valgrind |
| 289 | + if sysconfig.get_config_var('WITH_VALGRIND'): |
| 290 | + build.append("valgrind") |
| 291 | + # --with-dtrace |
| 292 | + if sysconfig.get_config_var('WITH_DTRACE'): |
| 293 | + build.append("dtrace") |
| 294 | + |
| 295 | + return build |
0 commit comments