You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.3KB

  1. import json
  2. import sys
  3. from contextlib import contextmanager
  4. from pathlib import Path
  5. from typing import Iterator
  6. import distro
  7. import json5
  8. from . import paths
  9. @contextmanager
  10. def fixup_toolchain(json_file: Path) -> Iterator[Path]:
  11. """
  12. Augment the toolchain at the given path by adding 'ccache' or -fuse-ld=lld,
  13. if those tools are available on the system. Yields a new toolchain file
  14. based on 'json_file'
  15. """
  16. data = json5.loads(json_file.read_text())
  17. # Check if we can add ccache
  18. ccache = paths.find_exe('ccache')
  19. if ccache:
  20. print('Found ccache:', ccache)
  21. data['compiler_launcher'] = [str(ccache)]
  22. # Check for lld for use with GCC/Clang
  23. if paths.find_exe('ld.lld') and data.get('compiler_id') in ('gnu', 'clang'):
  24. print('Linking with `-fuse-ld=lld`')
  25. data.setdefault('link_flags', []).append('-fuse-ld=lld')
  26. # Save the new toolchain data
  27. with paths.new_tempdir() as tdir:
  28. new_json = tdir / json_file.name
  29. new_json.write_text(json.dumps(data))
  30. yield new_json
  31. def get_default_test_toolchain() -> Path:
  32. """
  33. Get the default toolchain that should be used for dev and test based on the
  34. host platform.
  35. """
  36. if distro.id() == 'alpine':
  37. # Alpine Linux cannot use the full audit mode, as asan and ubsan do not
  38. # work with musl
  39. return paths.TOOLS_DIR / 'gcc-9-test.jsonc'
  40. if sys.platform == 'win32':
  41. return paths.TOOLS_DIR / 'msvc-audit.jsonc'
  42. if sys.platform in 'linux':
  43. return paths.TOOLS_DIR / 'gcc-9-audit.jsonc'
  44. if sys.platform == 'darwin':
  45. return paths.TOOLS_DIR / 'gcc-9-audit-macos.jsonc'
  46. raise RuntimeError(f'Unable to determine the default toolchain (sys.platform is {sys.platform!r})')
  47. def get_default_toolchain() -> Path:
  48. """
  49. Get the default toolchain that should be used to generate the release executable
  50. based on the host platform.
  51. """
  52. if sys.platform == 'win32':
  53. return paths.TOOLS_DIR / 'msvc-rel.jsonc'
  54. if sys.platform == 'linux':
  55. return paths.TOOLS_DIR / 'gcc-9-rel.jsonc'
  56. if sys.platform == 'darwin':
  57. return paths.TOOLS_DIR / 'gcc-9-rel-macos.jsonc'
  58. raise RuntimeError(f'Unable to determine the default toolchain (sys.platform is {sys.platform!r})')