generate.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2019 The RE2 Authors. All Rights Reserved.
  2. # Use of this source code is governed by a BSD-style
  3. # license that can be found in the LICENSE file.
  4. import os
  5. import shutil
  6. import sys
  7. import sysconfig
  8. def generate():
  9. include = sysconfig.get_path('include')
  10. libs = os.path.join(include, '../libs')
  11. mydir = os.path.dirname(sys.argv[0]) or '.'
  12. shutil.copytree(include, f'{mydir}/include')
  13. try:
  14. shutil.copytree(libs, f'{mydir}/libs')
  15. except FileNotFoundError:
  16. # We must not be running on Windows. :)
  17. pass
  18. with open(f'{mydir}/BUILD.bazel', 'x') as file:
  19. file.write(
  20. """\
  21. load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
  22. load("@rules_python//python:py_runtime.bzl", "py_runtime")
  23. load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
  24. package(default_visibility = ["//visibility:public"])
  25. toolchain(
  26. name = "py",
  27. toolchain = ":py_toolchain",
  28. toolchain_type = "@rules_python//python:toolchain_type",
  29. )
  30. py_runtime_pair(
  31. name = "py_toolchain",
  32. py3_runtime = ":interpreter",
  33. )
  34. py_runtime(
  35. name = "interpreter",
  36. interpreter_path = "{interpreter_path}",
  37. interpreter_version_info = {{
  38. "major": "{major}",
  39. "minor": "{minor}",
  40. }},
  41. python_version = "PY3",
  42. )
  43. toolchain(
  44. name = "py_cc",
  45. toolchain = ":py_cc_toolchain",
  46. toolchain_type = "@rules_python//python/cc:toolchain_type",
  47. )
  48. py_cc_toolchain(
  49. name = "py_cc_toolchain",
  50. headers = ":headers",
  51. libs = ":libraries",
  52. python_version = "{major}.{minor}",
  53. )
  54. cc_library(
  55. name = "headers",
  56. hdrs = glob(["include/**/*.h"]),
  57. includes = ["include"],
  58. deps = select({{
  59. "@platforms//os:windows": [":interface_library"],
  60. "//conditions:default": [],
  61. }}),
  62. )
  63. cc_import(
  64. name = "interface_library",
  65. interface_library = select({{
  66. "@platforms//os:windows": "libs/python{major}{minor}.lib",
  67. "//conditions:default": None,
  68. }}),
  69. system_provided = True,
  70. )
  71. # Not actually necessary for our purposes. :)
  72. cc_library(
  73. name = "libraries",
  74. )
  75. """.format(
  76. interpreter_path=sys.executable.replace('\\', '/'),
  77. major=sys.version_info.major,
  78. minor=sys.version_info.minor,
  79. )
  80. )
  81. if __name__ == '__main__':
  82. generate()