Skip to content

Commit e29afa1

Browse files
committed
Make the Bazel CI workflow use the local Python environment.
This is setting up to run mypy as per google#496. Change-Id: I48e67387313ac01d493086be8481b34f26bafe82 Reviewed-on: https://code-review.googlesource.com/c/re2/+/63230 Reviewed-by: Alex Chernyakhovsky <[email protected]> Reviewed-by: Paul Wankadia <[email protected]>
1 parent e38a588 commit e29afa1

File tree

6 files changed

+123
-95
lines changed

6 files changed

+123
-95
lines changed

.github/bazel.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ export MSYS2_ARG_CONV_EXCL='*'
88
for compilation_mode in dbg opt
99
do
1010
bazel clean
11-
bazel build --compilation_mode=${compilation_mode} -- \
11+
bazel build \
12+
--extra_toolchains=//python/toolchains:all \
13+
--compilation_mode=${compilation_mode} -- \
1214
//:re2 \
1315
//python:re2
14-
bazel test --compilation_mode=${compilation_mode} -- \
16+
bazel test \
17+
--extra_toolchains=//python/toolchains:all \
18+
--compilation_mode=${compilation_mode} -- \
1519
//:all \
1620
-//:dfa_test \
1721
-//:exhaustive1_test \

.github/workflows/ci-bazel.yml

+10
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ jobs:
1818
- uses: bazel-contrib/[email protected]
1919
with:
2020
bazelisk-version: '1.x'
21+
- uses: actions/[email protected]
22+
with:
23+
python-version: '3.x'
24+
- name: Prepare Python 3.x environment
25+
run: |
26+
python -m pip install --upgrade pip
27+
python -m pip install --upgrade mypy
28+
python python/toolchains/generate.py
29+
shell: bash
2130
- run: .github/bazel.sh
2231
shell: bash
32+
# TODO(junyer): Run mypy as per https://github.com/google/re2/issues/496.

.github/workflows/python.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ jobs:
4646
- name: Prepare Python ${{ matrix.ver }} environment
4747
run: |
4848
"${PYTHON}" -m pip install --upgrade pip
49-
"${PYTHON}" -m pip install --upgrade build wheel auditwheel
49+
"${PYTHON}" -m pip install --upgrade setuptools build wheel auditwheel
5050
"${PYTHON}" -m pip install --upgrade absl-py
51+
"${PYTHON}" python/toolchains/generate.py
5152
shell: bash
5253
- name: Build wheel
5354
env:
@@ -105,8 +106,9 @@ jobs:
105106
- name: Prepare Python ${{ matrix.ver }} environment
106107
run: |
107108
python -m pip install --upgrade pip
108-
python -m pip install --upgrade build wheel delocate
109+
python -m pip install --upgrade setuptools build wheel delocate
109110
python -m pip install --upgrade absl-py
111+
python python/toolchains/generate.py
110112
shell: bash
111113
- name: Build wheel
112114
env:
@@ -162,8 +164,9 @@ jobs:
162164
- name: Prepare Python ${{ matrix.ver }} environment
163165
run: |
164166
python -m pip install --upgrade pip
165-
python -m pip install --upgrade build wheel delvewheel
167+
python -m pip install --upgrade setuptools build wheel delvewheel
166168
python -m pip install --upgrade absl-py
169+
python python/toolchains/generate.py
167170
shell: bash
168171
- name: Build wheel
169172
env:
@@ -205,7 +208,7 @@ jobs:
205208
- name: Prepare Python 3.x environment
206209
run: |
207210
python -m pip install --upgrade pip
208-
python -m pip install --upgrade build wheel
211+
python -m pip install --upgrade setuptools build wheel
209212
shell: bash
210213
- if: inputs.build == 1 || inputs.force-sdist == true
211214
name: Build source

app/build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ DSTDIR=$(mktemp --directory --tmpdir $(basename $0).XXXXXXXXXX)
77
cd ${SRCDIR}
88
# Emscripten doesn't support `-fstack-protector`.
99
AR=emar CC=emcc \
10-
bazel build --compilation_mode=opt \
10+
bazel build \
1111
--copt=-fno-stack-protector \
12-
-- :all
12+
--compilation_mode=opt -- :all
1313
cp ../bazel-bin/app/_re2.js ${DSTDIR}
1414
bazel clean --expunge
1515
cp app.ts index.html _re2.d.ts ${DSTDIR}

python/setup.py

-87
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import setuptools
77
import setuptools.command.build_ext
88
import shutil
9-
import sys
10-
import sysconfig
119

1210
long_description = r"""A drop-in replacement for the re module.
1311
@@ -67,10 +65,7 @@ def build_extension(self, ext):
6765
except KeyError:
6866
pass
6967
# Register the local Python toolchains with highest priority.
70-
self.generate_python_toolchains()
7168
cmd.append('--extra_toolchains=//python/toolchains:all')
72-
# Print debug information during toolchain resolution.
73-
cmd.append('--toolchain_resolution_debug=.*')
7469
cmd += ['--compilation_mode=opt', '--', ':all']
7570
self.spawn(cmd)
7671

@@ -82,88 +77,6 @@ def build_extension(self, ext):
8277
cmd = ['bazel', 'clean', '--expunge']
8378
self.spawn(cmd)
8479

85-
def generate_python_toolchains(self):
86-
include = sysconfig.get_path('include')
87-
libs = os.path.join(include, '../libs')
88-
89-
os.makedirs('toolchains')
90-
shutil.copytree(include, 'toolchains/include')
91-
try:
92-
shutil.copytree(libs, 'toolchains/libs')
93-
except FileNotFoundError:
94-
# We must not be running on Windows. :)
95-
pass
96-
97-
with open('toolchains/BUILD.bazel', 'x') as file:
98-
file.write(
99-
"""\
100-
load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
101-
load("@rules_python//python:py_runtime.bzl", "py_runtime")
102-
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
103-
104-
package(default_visibility = ["//visibility:public"])
105-
106-
toolchain(
107-
name = "py",
108-
toolchain = ":py_toolchain",
109-
toolchain_type = "@rules_python//python:toolchain_type",
110-
)
111-
112-
py_runtime_pair(
113-
name = "py_toolchain",
114-
py3_runtime = ":interpreter",
115-
)
116-
117-
py_runtime(
118-
name = "interpreter",
119-
interpreter_path = "{interpreter_path}",
120-
interpreter_version_info = {{
121-
"major": "{major}",
122-
"minor": "{minor}",
123-
}},
124-
python_version = "PY3",
125-
)
126-
127-
toolchain(
128-
name = "py_cc",
129-
toolchain = ":py_cc_toolchain",
130-
toolchain_type = "@rules_python//python/cc:toolchain_type",
131-
)
132-
133-
py_cc_toolchain(
134-
name = "py_cc_toolchain",
135-
headers = ":headers",
136-
libs = ":libraries",
137-
python_version = "{major}.{minor}",
138-
)
139-
140-
cc_library(
141-
name = "headers",
142-
hdrs = glob(["include/**/*.h"]),
143-
includes = ["include"],
144-
deps = select({{
145-
"@platforms//os:windows": [":interface_library"],
146-
"//conditions:default": [],
147-
}}),
148-
)
149-
150-
cc_import(
151-
name = "interface_library",
152-
interface_library = select({{
153-
"@platforms//os:windows": "libs/python{major}{minor}.lib",
154-
"//conditions:default": None,
155-
}}),
156-
system_provided = True,
157-
)
158-
159-
# Not actually necessary for our purposes. :)
160-
cc_library(
161-
name = "libraries",
162-
)
163-
""".format(interpreter_path=sys.executable.replace('\\', '/'),
164-
major=sys.version_info.major,
165-
minor=sys.version_info.minor))
166-
16780

16881
def options():
16982
bdist_wheel = {}

python/toolchains/generate.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
5+
import os
6+
import shutil
7+
import sys
8+
import sysconfig
9+
10+
11+
def 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+
"""\
26+
load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
27+
load("@rules_python//python:py_runtime.bzl", "py_runtime")
28+
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
29+
30+
package(default_visibility = ["//visibility:public"])
31+
32+
toolchain(
33+
name = "py",
34+
toolchain = ":py_toolchain",
35+
toolchain_type = "@rules_python//python:toolchain_type",
36+
)
37+
38+
py_runtime_pair(
39+
name = "py_toolchain",
40+
py3_runtime = ":interpreter",
41+
)
42+
43+
py_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+
53+
toolchain(
54+
name = "py_cc",
55+
toolchain = ":py_cc_toolchain",
56+
toolchain_type = "@rules_python//python/cc:toolchain_type",
57+
)
58+
59+
py_cc_toolchain(
60+
name = "py_cc_toolchain",
61+
headers = ":headers",
62+
libs = ":libraries",
63+
python_version = "{major}.{minor}",
64+
)
65+
66+
cc_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+
76+
cc_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. :)
86+
cc_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+
97+
if __name__ == '__main__':
98+
generate()

0 commit comments

Comments
 (0)