Skip to content

Commit 501b2c5

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Fix compilation and test issues on Windows
PiperOrigin-RevId: 908247644
1 parent 45bdaad commit 501b2c5

10 files changed

Lines changed: 83 additions & 24 deletions

File tree

.bazelrc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
# Enable platform-specific configuration.
2+
# This allows Bazel to automatically pick up the `windows` config on Windows.
3+
common --enable_platform_specific_config
4+
15
# Disable automatic creation of `__init__.py` files, which prevent multiple
26
# workspaces from providing files with the same package prefix (e.g., "cel").
37
# See https://github.com/bazelbuild/rules_python/issues/330.
4-
build --incompatible_default_to_explicit_init_py
8+
build --incompatible_default_to_explicit_init_py
9+
10+
# MSVC: Use the C++20 standard in order to support C++20 features that were introduced in clang and gcc
11+
build:windows --copt="/std:c++20" --cxxopt="/std:c++20"
12+
# MSVC: Enable modern standards-compatible preprocessor
13+
build:windows --cxxopt="/Zc:preprocessor" --host_cxxopt="/Zc:preprocessor"
14+
# MSVC: Link ANTLR statically to avoid Windows DLL linking errors
15+
build:windows --cxxopt="-DANTLR4CPP_STATIC" --host_cxxopt="-DANTLR4CPP_STATIC"

MODULE.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ _CEL_CPP_SHA256 = "d1b9b2c2e9745826c1c8540a49f972ea4b93dffd5878c6a67756a2627b011
2121

2222
archive_override(
2323
module_name = "cel-cpp",
24+
patch_cmds = [
25+
# ABSL_CONST_INIT is incompatible with MSVC-CL with the /std:c++20 option
26+
"sed -i 's/ABSL_CONST_INIT //g' common/values/optional_value.cc",
27+
],
2428
sha256 = _CEL_CPP_SHA256,
2529
strip_prefix = "cel-cpp-" + _CEL_CPP_COMMIT,
2630
url = "https://github.com/google/cel-cpp/archive/" + _CEL_CPP_COMMIT + ".tar.gz",
@@ -53,6 +57,15 @@ bazel_dep(name = "rules_proto", version = "7.1.0")
5357
# https://registry.bazel.build/modules/rules_python
5458
bazel_dep(name = "rules_python", version = "1.7.0")
5559

60+
# On Windows the file system is case-insensitive, which creates a collision between
61+
# antlr4-cpp-runtime/VERSION and the system `#include <version>`
62+
single_version_override(
63+
module_name = "antlr4-cpp-runtime",
64+
patch_cmds = [
65+
"mv VERSION VERSION.txt",
66+
],
67+
)
68+
5669
python_rules = use_extension("@rules_python//python/extensions:python.bzl", "python")
5770
python_rules.toolchain(
5871
is_default = True,

cel_expr_python/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ cc_library(
142142
py_test(
143143
name = "cel_test",
144144
srcs = ["cel_test.py"],
145+
data = [
146+
":cel",
147+
],
145148
deps = [
146149
":cel",
147150
"//testing:proto2_test_all_types_py_pb2",
@@ -153,6 +156,13 @@ py_test(
153156
py_test(
154157
name = "cel_env_test",
155158
srcs = ["cel_env_test.py"],
159+
data = [
160+
":cel",
161+
"//cel_expr_python/ext:ext_bindings",
162+
"//cel_expr_python/ext:ext_math",
163+
"//cel_expr_python/ext:ext_optional",
164+
"//cel_expr_python/ext:ext_strings",
165+
],
156166
deps = [
157167
":cel",
158168
"//cel_expr_python/ext:ext_bindings",

cel_expr_python/cel_test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def testEvalInt64(self):
160160
self.assertEqual(res.value(), 42)
161161
res = self._eval("var_int", {"var_int": 1000000000000000000000000})
162162
self.assertEqual(res.type(), cel.Type.ERROR)
163-
self.assertIn("int too large", res.value())
163+
self.assertTrue(
164+
"int too large" in res.value() or "int too big" in res.value(),
165+
msg=f"Unexpected error message: {res.value()}",
166+
)
164167
res = self._eval("var_int - 2", {"var_int": 3.14})
165168
self.assertEqual(res.type(), cel.Type.ERROR)
166169
self.assertIn(
@@ -197,7 +200,11 @@ def testEvalUint64(self):
197200
self.assertEqual(res.value(), 43)
198201
res = self._eval("var_uint + 1u", {"var_uint": -42})
199202
self.assertEqual(res.type(), cel.Type.ERROR)
200-
self.assertIn("can't convert negative value to unsigned int", res.value())
203+
self.assertTrue(
204+
"can't convert negative value to unsigned int" in res.value()
205+
or "can't convert negative int to unsigned" in res.value(),
206+
msg=f"Unexpected error message: {res.value()}",
207+
)
201208

202209
def testEvalDouble(self):
203210
res: cel.Value = self._eval("3.14", expected_return_type=cel.Type.DOUBLE)

cel_expr_python/py_cel_type.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ absl::StatusOr<PyCelType> PyCelType::ForPyObject(
404404

405405
if (PyNumber_Check(py_object)) {
406406
// Attempt to convert to a signed 64-bit integer.
407-
PyLong_AsLong(py_object);
407+
PyLong_AsLongLong(py_object);
408408
if (!PyErr_Occurred()) {
409409
return PyCelType::Int();
410410
}
411411
PyErr_Clear();
412412

413413
// If the value is too large for a signed 64-bit integer, try converting
414414
// to an unsigned 64-bit integer. If successful, return a UINT type.
415-
PyLong_AsUnsignedLong(py_object);
415+
PyLong_AsUnsignedLongLong(py_object);
416416
if (!PyErr_Occurred()) {
417417
return PyCelType::Uint();
418418
}

cel_expr_python/py_cel_value.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
483483
}
484484
case cel::Kind::kInt: {
485485
if (bypass_type_check || PyNumber_Check(py_object)) {
486-
int64_t value = PyLong_AsLong(py_object);
486+
int64_t value = PyLong_AsLongLong(py_object);
487487
if (PyErr_Occurred()) {
488488
return cel::ErrorValue(PyErr_toStatus());
489489
}
@@ -494,7 +494,7 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
494494
}
495495
case cel::Kind::kUint: {
496496
if (bypass_type_check || PyNumber_Check(py_object)) {
497-
uint64_t value = PyLong_AsUnsignedLong(py_object);
497+
uint64_t value = PyLong_AsUnsignedLongLong(py_object);
498498
if (PyErr_Occurred()) {
499499
return cel::ErrorValue(PyErr_toStatus());
500500
}

cel_expr_python/py_error_status.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static absl::Status PyErrorToStatus(PyObject* py_type, PyObject* py_error) {
4242
{PyExc_SyntaxError, absl::StatusCode::kInternal},
4343
{PyExc_TypeError, absl::StatusCode::kInvalidArgument},
4444
{PyExc_ValueError, absl::StatusCode::kOutOfRange},
45-
{PyExc_LookupError, absl::StatusCode::kNotFound}});
45+
{PyExc_LookupError, absl::StatusCode::kNotFound},
46+
{PyExc_OverflowError, absl::StatusCode::kOutOfRange}});
4647
PyObject* pystr = PyObject_Str(py_error);
4748
const char* message = pystr ? PyUnicode_AsUTF8(pystr) : "Unknown error";
4849
Py_XDECREF(pystr);

conformance/BUILD

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,29 @@ _ALL_TESTS = [
3535
py_test(
3636
name = "conformance_test",
3737
srcs = ["conformance_test.py"],
38-
data = _ALL_TESTS,
38+
data = _ALL_TESTS + [
39+
"//cel_expr_python:cel",
40+
"//cel_expr_python/ext:ext_bindings",
41+
"//cel_expr_python/ext:ext_encoders",
42+
"//cel_expr_python/ext:ext_math",
43+
"//cel_expr_python/ext:ext_optional",
44+
"//cel_expr_python/ext:ext_proto",
45+
"//cel_expr_python/ext:ext_strings",
46+
],
47+
env = {
48+
"TEST_FILES": ",".join(["$(rlocationpath %s)" % t for t in _ALL_TESTS]),
49+
},
3950
deps = [
4051
"@com_google_cel_spec//proto/cel/expr:checked_py_pb2",
4152
"@com_google_cel_spec//proto/cel/expr:value_py_pb2",
42-
"//cel_expr_python:cel",
53+
"@com_google_absl_py//absl/testing:absltest",
4354
"//cel_expr_python/ext:ext_bindings",
4455
"//cel_expr_python/ext:ext_encoders",
4556
"//cel_expr_python/ext:ext_math",
4657
"//cel_expr_python/ext:ext_optional",
4758
"//cel_expr_python/ext:ext_proto",
4859
"//cel_expr_python/ext:ext_strings",
49-
"@com_google_absl_py//absl/testing:absltest",
60+
"//cel_expr_python:cel",
5061
"//testing:proto2_test_all_types_py_pb2",
5162
"//testing:proto3_test_all_types_py_pb2",
5263
"//testing:simple_py_pb2",

conformance/conformance_test.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class ConformanceTestSuite(unittest.TestSuite):
7373
"optionals/optionals/optional_struct_optindex_index_value",
7474
"optionals/optionals/optional_struct_optindex_value",
7575
"optionals/optionals/struct_optindex_value",
76+
# TODO(b/507568865): Timezone support on Windows.
77+
"timestamps/timestamp_selectors_tz/.*",
7678
]
7779

7880
def __init__(self):
@@ -96,9 +98,7 @@ def _load_tests(self):
9698
This function names tests as "file_name/section_name/test_name", and adds
9799
them to the test suite.
98100
"""
99-
testfiles = self._all_test_files(
100-
"cel-spec+/tests/simple/testdata"
101-
)
101+
testfiles = self._all_test_files()
102102

103103
for testfile in testfiles:
104104
with open(testfile, "rb", encoding=None, errors=None) as data_file:
@@ -117,17 +117,18 @@ def _load_tests(self):
117117
if not should_skip:
118118
self.addTest(ConformanceTest(test_name, test))
119119

120-
def _all_test_files(self, path: str) -> list[str]:
120+
def _all_test_files(self) -> list[str]:
121121
r = runfiles.Create()
122-
location = r.Rlocation(path)
123-
if not location:
124-
raise ValueError("Could not resolve path: " + path)
125-
126122
test_files = []
127-
for root, _, files in os.walk(location):
128-
for f in files:
129-
if f.endswith(".textproto"):
130-
test_files.append(os.path.join(root, f))
123+
env_test_files = os.environ.get("TEST_FILES")
124+
if env_test_files:
125+
for f in env_test_files.split(","):
126+
location = r.Rlocation(f)
127+
if location:
128+
test_files.append(location)
129+
else:
130+
raise ValueError(f"Could not resolve path: {f}")
131+
131132
return sorted(test_files)
132133

133134

custom_ext/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ pybind_extension(
3333
py_library(
3434
name = "sample_cel_ext_py",
3535
srcs = ["sample_cel_ext.py"],
36-
deps = [
36+
data = [
3737
"//cel_expr_python:cel",
3838
],
39+
deps = ["//cel_expr_python:cel"],
3940
)
4041

4142
py_test(
4243
name = "custom_ext_test",
4344
srcs = ["custom_ext_test.py"],
45+
data = [
46+
":sample_cel_ext_cc",
47+
"//cel_expr_python:cel",
48+
],
4449
deps = [
4550
":sample_cel_ext_cc",
4651
":sample_cel_ext_py",

0 commit comments

Comments
 (0)