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.

83 lines
2.9KB

  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. from .util import Pathish
  10. @contextmanager
  11. def fixup_toolchain(json_file: Pathish) -> Iterator[Path]:
  12. """
  13. Augment the toolchain at the given path by adding 'ccache' or -fuse-ld=lld,
  14. if those tools are available on the system. Yields a new toolchain file
  15. based on 'json_file'
  16. """
  17. json_file = Path(json_file)
  18. data = json5.loads(json_file.read_text())
  19. # Check if we can add ccache
  20. ccache = paths.find_exe('ccache')
  21. if ccache and data.get('compiler_id') in ('gnu', 'clang'):
  22. print('Found ccache:', ccache)
  23. data['compiler_launcher'] = [str(ccache)]
  24. # Check for lld for use with GCC/Clang
  25. if paths.find_exe('ld.lld') and data.get('compiler_id') in ('gnu', 'clang'):
  26. print('Linking with `-fuse-ld=lld`')
  27. data.setdefault('link_flags', []).append('-fuse-ld=lld')
  28. # Save the new toolchain data
  29. with paths.new_tempdir() as tdir:
  30. new_json = tdir / json_file.name
  31. new_json.write_text(json.dumps(data))
  32. yield new_json
  33. def get_default_audit_toolchain() -> Path:
  34. """
  35. Get the default toolchain that should be used for dev and test based on the
  36. host platform.
  37. """
  38. if sys.platform == 'win32':
  39. return paths.TOOLS_DIR / 'msvc-audit.jsonc'
  40. if sys.platform == 'linux':
  41. return paths.TOOLS_DIR / 'gcc-9-audit.jsonc'
  42. if sys.platform == 'darwin':
  43. return paths.TOOLS_DIR / 'gcc-9-audit-macos.jsonc'
  44. if sys.platform == 'freebsd11':
  45. return paths.TOOLS_DIR / 'freebsd-gcc-10.jsonc'
  46. raise RuntimeError(f'Unable to determine the default toolchain (sys.platform is {sys.platform!r})')
  47. def get_default_test_toolchain() -> Path:
  48. """
  49. Get the default toolchain that should be used by tests that need a toolchain
  50. to use for executing dds.
  51. """
  52. if sys.platform == 'win32':
  53. return paths.TESTS_DIR / 'msvc.tc.jsonc'
  54. if sys.platform in ('linux', 'darwin'):
  55. return paths.TESTS_DIR / 'gcc-9.tc.jsonc'
  56. if sys.platform == 'freebsd11':
  57. return paths.TOOLS_DIR / 'freebsd-gcc-10.jsonc'
  58. raise RuntimeError(f'Unable to determine the default toolchain (sys.platform is {sys.platform!r})')
  59. def get_default_toolchain() -> Path:
  60. """
  61. Get the default toolchain that should be used to generate the release executable
  62. based on the host platform.
  63. """
  64. if sys.platform == 'win32':
  65. return paths.TOOLS_DIR / 'msvc-rel.jsonc'
  66. if sys.platform == 'linux':
  67. return paths.TOOLS_DIR / 'gcc-9-rel.jsonc'
  68. if sys.platform == 'darwin':
  69. return paths.TOOLS_DIR / 'gcc-9-rel-macos.jsonc'
  70. if sys.platform == 'freebsd11':
  71. return paths.TOOLS_DIR / 'freebsd-gcc-10.jsonc'
  72. raise RuntimeError(f'Unable to determine the default toolchain (sys.platform is {sys.platform!r})')