• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import os
6import shutil
7import sys
8import sysconfig
9
10
11def generate():
12  include = sysconfig.get_path('include')
13  libs = os.path.join(include, '../libs')
14
15  mydir = os.path.dirname(sys.argv[0]) or '.'
16  shutil.copytree(include, f'{mydir}/include')
17  try:
18    shutil.copytree(libs, f'{mydir}/libs')
19  except FileNotFoundError:
20    # We must not be running on Windows. :)
21    pass
22
23  with open(f'{mydir}/BUILD.bazel', 'x') as file:
24    file.write(
25        """\
26load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
27load("@rules_python//python:py_runtime.bzl", "py_runtime")
28load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
29
30package(default_visibility = ["//visibility:public"])
31
32toolchain(
33    name = "py",
34    toolchain = ":py_toolchain",
35    toolchain_type = "@rules_python//python:toolchain_type",
36)
37
38py_runtime_pair(
39    name = "py_toolchain",
40    py3_runtime = ":interpreter",
41)
42
43py_runtime(
44    name = "interpreter",
45    interpreter_path = "{interpreter_path}",
46    interpreter_version_info = {{
47        "major": "{major}",
48        "minor": "{minor}",
49    }},
50    python_version = "PY3",
51)
52
53toolchain(
54    name = "py_cc",
55    toolchain = ":py_cc_toolchain",
56    toolchain_type = "@rules_python//python/cc:toolchain_type",
57)
58
59py_cc_toolchain(
60    name = "py_cc_toolchain",
61    headers = ":headers",
62    libs = ":libraries",
63    python_version = "{major}.{minor}",
64)
65
66cc_library(
67    name = "headers",
68    hdrs = glob(["include/**/*.h"]),
69    includes = ["include"],
70    deps = select({{
71        "@platforms//os:windows": [":interface_library"],
72        "//conditions:default": [],
73    }}),
74)
75
76cc_import(
77    name = "interface_library",
78    interface_library = select({{
79        "@platforms//os:windows": "libs/python{major}{minor}.lib",
80        "//conditions:default": None,
81    }}),
82    system_provided = True,
83)
84
85# Not actually necessary for our purposes. :)
86cc_library(
87    name = "libraries",
88)
89""".format(
90            interpreter_path=sys.executable.replace('\\', '/'),
91            major=sys.version_info.major,
92            minor=sys.version_info.minor,
93        )
94    )
95
96
97if __name__ == '__main__':
98  generate()
99