Skip to content

Commit ff5b4dd

Browse files
Internal change
PiperOrigin-RevId: 917337474
1 parent b60b825 commit ff5b4dd

8 files changed

Lines changed: 368 additions & 14 deletions

File tree

.bazelrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@ build --incompatible_default_to_explicit_init_py
99

1010
# MSVC: Use the C++20 standard in order to support C++20 features that were introduced in clang and gcc
1111
build:windows --copt="/std:c++20" --cxxopt="/std:c++20"
12+
# MSVC: Corrects the __cplusplus macro to report the true standard
13+
build:windows --copt="/Zc:__cplusplus" --cxxopt="/Zc:__cplusplus"
1214
# MSVC: Enable modern standards-compatible preprocessor
1315
build:windows --cxxopt="/Zc:preprocessor" --host_cxxopt="/Zc:preprocessor"
1416
# MSVC: Link ANTLR statically to avoid Windows DLL linking errors
1517
build:windows --cxxopt="-DANTLR4CPP_STATIC" --host_cxxopt="-DANTLR4CPP_STATIC"
18+
19+
# Repo environment bypasses for corporate proxy (Windows-only)
20+
common:windows --repo_env=no_proxy=bcr.bazel.build,bazel.build
21+
common:windows --repo_env=NO_PROXY=bcr.bazel.build,bazel.build
22+
# Configure autoconfigured toolchain defaults to C++20 and modern __cplusplus macro (Windows-only)
23+
common:windows --repo_env=BAZEL_CXXOPTS="/std:c++20 /Zc:__cplusplus"
24+
25+
# Downloader attempts and retries for BCR network stability (Windows-only)
26+
common:windows --http_connector_attempts=10
27+
common:windows --experimental_repository_downloader_retries=10
28+
29+
# Build verbosity and output options (Windows-only)
30+
build:windows --verbose_failures
31+
test:windows --test_output=errors
32+
33+
# GCS remote caching config (Windows-only, active by default on Windows!)
34+
build:windows --remote_cache=https://storage.googleapis.com/windows-cel-python-remote-cache
35+
build:windows --google_default_credentials=true

MODULE.bazel

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ module(
33
)
44

55
# https://registry.bazel.build/modules/abseil-cpp
6-
bazel_dep(name = "abseil-cpp", version = "20250814.1", repo_name = "com_google_absl")
6+
bazel_dep(name = "abseil-cpp", version = "20260107.0", repo_name = "com_google_absl")
77

88
# https://registry.bazel.build/modules/abseil-py
9-
bazel_dep(name = "abseil-py", version = "2.1.0", repo_name = "com_google_absl_py")
9+
bazel_dep(name = "abseil-py", version = "2.4.0", repo_name = "com_google_absl_py")
1010

1111
# https://github.com/bazelbuild/bazel-skylib
12-
bazel_dep(name = "bazel_skylib", version = "1.8.2")
12+
bazel_dep(name = "bazel_skylib", version = "1.9.0")
1313

1414
# https://registry.bazel.build/modules/cel-cpp
1515
bazel_dep(name = "cel-cpp", version = "0.15.0", repo_name = "com_google_cel_cpp")
16-
single_version_override(
16+
git_override(
1717
module_name = "cel-cpp",
18+
commit = "2e6e9ff4493bfbe0baf883107f3fb7ce6f675d88",
1819
patch_cmds = [
1920
# ABSL_CONST_INIT is incompatible with MSVC-CL with the /std:c++20 option
2021
"sed -i 's/ABSL_CONST_INIT //g' common/values/optional_value.cc",
2122
],
22-
version = "0.15.0",
23+
patch_cmds_win = [
24+
"python -c \"import sys; path='common/values/optional_value.cc'; content=open(path).read(); open(path,'w').write(content.replace('ABSL_CONST_INIT ',''))\"",
25+
],
26+
remote = "https://github.com/google/cel-cpp",
2327
)
2428

2529
# https://registry.bazel.build/modules/cel-spec
@@ -32,7 +36,7 @@ bazel_dep(name = "googletest", version = "1.17.0.bcr.2", repo_name = "com_google
3236
bazel_dep(name = "platforms", version = "1.0.0")
3337

3438
# https://registry.bazel.build/modules/protobuf
35-
bazel_dep(name = "protobuf", version = "33.1", repo_name = "com_google_protobuf")
39+
bazel_dep(name = "protobuf", version = "33.4", repo_name = "com_google_protobuf")
3640

3741
# https://registry.bazel.build/modules/pybind11_abseil
3842
bazel_dep(name = "pybind11_abseil", version = "202402.0")
@@ -41,25 +45,34 @@ bazel_dep(name = "pybind11_abseil", version = "202402.0")
4145
bazel_dep(name = "pybind11_bazel", version = "3.0.0")
4246

4347
# https://registry.bazel.build/modules/rules_cc
44-
bazel_dep(name = "rules_cc", version = "0.2.14")
48+
bazel_dep(name = "rules_cc", version = "0.2.16")
4549

4650
# https://registry.bazel.build/modules/rules_proto
4751
bazel_dep(name = "rules_proto", version = "7.1.0")
4852

4953
# https://registry.bazel.build/modules/rules_python
50-
bazel_dep(name = "rules_python", version = "1.7.0")
54+
bazel_dep(name = "rules_python", version = "1.9.0")
5155

5256
# On Windows the file system is case-insensitive, which creates a collision between
5357
# antlr4-cpp-runtime/VERSION and the system `#include <version>`
5458
single_version_override(
5559
module_name = "antlr4-cpp-runtime",
5660
patch_cmds = [
57-
"mv VERSION VERSION.txt",
61+
"python -c \"import os; os.rename('VERSION', 'VERSION.txt') if os.path.exists('VERSION') else None\"",
62+
"python -c \"import os; os.rename('version', 'version.txt') if os.path.exists('version') else None\"",
5863
],
5964
)
6065

61-
python_rules = use_extension("@rules_python//python/extensions:python.bzl", "python")
62-
python_rules.toolchain(
63-
is_default = True,
64-
python_version = "3.11",
66+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
67+
python.defaults(python_version = "3.11")
68+
python.toolchain(python_version = "3.11")
69+
use_repo(python, "pythons_hub")
70+
71+
pybind11_internal_configure = use_extension(
72+
"@pybind11_bazel//:internal_configure.bzl",
73+
"internal_configure_extension",
6574
)
75+
use_repo(pybind11_internal_configure, "pybind11")
76+
77+
local_repo_ext = use_extension("//cel_expr_python:local_repo_extension.bzl", "local_repo_ext")
78+
use_repo(local_repo_ext, "python_headers_custom")

cel_expr_python/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pybind_library(
125125
"@com_google_cel_cpp//compiler",
126126
"@com_google_cel_cpp//runtime:runtime_builder",
127127
"@com_google_cel_cpp//runtime:runtime_options",
128+
"@python_headers_custom//:headers",
128129
],
129130
)
130131

cel_expr_python/ext/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pybind_extension(
1616
"//cel_expr_python:cel_extension",
1717
"@com_google_cel_cpp//compiler",
1818
"@com_google_cel_cpp//extensions:bindings_ext",
19+
"@python_headers_custom//:headers",
1920
],
2021
)
2122

@@ -36,6 +37,7 @@ pybind_extension(
3637
"@com_google_cel_cpp//extensions:encoders",
3738
"@com_google_cel_cpp//runtime:runtime_builder",
3839
"@com_google_cel_cpp//runtime:runtime_options",
40+
"@python_headers_custom//:headers",
3941
],
4042
)
4143

@@ -58,6 +60,7 @@ pybind_extension(
5860
"@com_google_cel_cpp//extensions:math_ext_decls",
5961
"@com_google_cel_cpp//runtime:runtime_builder",
6062
"@com_google_cel_cpp//runtime:runtime_options",
63+
"@python_headers_custom//:headers",
6164
],
6265
)
6366

@@ -81,6 +84,7 @@ pybind_extension(
8184
"@com_google_cel_cpp//runtime:optional_types",
8285
"@com_google_cel_cpp//runtime:runtime_builder",
8386
"@com_google_cel_cpp//runtime:runtime_options",
87+
"@python_headers_custom//:headers",
8488
],
8589
)
8690

@@ -98,6 +102,7 @@ pybind_extension(
98102
"//cel_expr_python:cel_extension",
99103
"@com_google_cel_cpp//compiler",
100104
"@com_google_cel_cpp//extensions:proto_ext",
105+
"@python_headers_custom//:headers",
101106
],
102107
)
103108

@@ -119,5 +124,6 @@ pybind_extension(
119124
"@com_google_cel_cpp//extensions:strings",
120125
"@com_google_cel_cpp//runtime:runtime_builder",
121126
"@com_google_cel_cpp//runtime:runtime_options",
127+
"@python_headers_custom//:headers",
122128
],
123129
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Module extension to create a local repository for custom headers."""
2+
3+
# A repository rule that dynamically constructs a platform-independent
4+
# `python_headers_custom` repository.
5+
#
6+
# Why this is needed:
7+
# 1. In the Google internal repository, Python headers are implicitly provided by internal rules.
8+
# In the OSS export, `pybind_extension` and `pybind_library` require explicit access to C++ Python headers.
9+
# 2. Instead of hardcoding a platform-specific repository (like the Linux-only x86_64-unknown-linux-gnu),
10+
# we dynamically resolve the compiler headers from the host platform's registered toolchain alias
11+
# (`@@rules_python++python+python_3_11_host`).
12+
# 3. The directory layout of Python include headers varies by platform:
13+
# - POSIX (Linux/macOS) Python distributions nest headers under `include/python3.11/Python.h`.
14+
# - Windows MSVC Python distributions place headers directly under `include/Python.h`.
15+
# 4. This implementation symlinks the host's `include` directory, checks if the nested `python3.11`
16+
# folder exists, and dynamically configures the `cc_library` glob and `includes` path accordingly.
17+
# This guarantees a seamless, warning-free C++20 compilation on Windows, Linux, and macOS.
18+
def _custom_headers_repo_impl(ctx):
19+
# Resolve label to path
20+
build_file_path = ctx.path(ctx.attr.build_file_label)
21+
repo_dir = build_file_path.dirname
22+
23+
# Symlink the include directory
24+
ctx.symlink(repo_dir.get_child("include"), "include")
25+
26+
# Check if include/python3.11 exists (dynamic pathing for POSIX vs Windows)
27+
if ctx.path("include/python3.11").exists:
28+
include_path = "include/python3.11"
29+
else:
30+
include_path = "include"
31+
32+
# Create BUILD file with dynamic includes
33+
ctx.file("BUILD.bazel", """
34+
cc_library(
35+
name = "headers",
36+
hdrs = glob(["{include_path}/**"]),
37+
includes = ["{include_path}"],
38+
visibility = ["//visibility:public"],
39+
)
40+
""".format(include_path = include_path))
41+
42+
custom_headers_repo = repository_rule(
43+
implementation = _custom_headers_repo_impl,
44+
attrs = {
45+
"build_file_label": attr.label(mandatory = True),
46+
},
47+
)
48+
49+
def _local_repo_extension_impl(_ctx):
50+
custom_headers_repo(
51+
name = "python_headers_custom",
52+
# Use the platform-independent host repository alias!
53+
build_file_label = "@@rules_python++python+python_3_11_host//:BUILD.bazel",
54+
)
55+
56+
local_repo_ext = module_extension(
57+
implementation = _local_repo_extension_impl,
58+
)

release/kokoro/build_windows.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# proto-message: BuildConfig
33

44
build_file: "cel-python/release/kokoro/presubmit_windows.bat"
5-
timeout_mins: 30
5+
timeout_mins: 120
66

77
container_properties {
88
docker_image: "us-central1-docker.pkg.dev/kokoro-container-bakery/kokoro/windows/windows2019/full@sha256:c9995cdda2206803409164f9682f47e0027b8836076591a960373e3cdb1847e1"

release/kokoro/download_patches.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Dynamic BCR Patches Downloader Engine.
16+
17+
This script resolves external dependencies dynamically on the target Windows VM.
18+
It parses the JSON Bzlmod dependency graph computed on-the-fly by `bazel mod graph`,
19+
recursively queries the Bazel Central Registry (BCR), extracts registry metadata
20+
and source files, downloads all transitive registry patches (which must be applied
21+
locally), isolates colliding patch names into separate directories, and outputs
22+
the dynamically computed `--distdir` flags to be loaded directly by the Windows batch script.
23+
"""
24+
25+
import json
26+
import os
27+
import sys
28+
import time
29+
import urllib.error
30+
import urllib.request
31+
32+
def find_modules(node, modules=None):
33+
"""Recursively parses Bzlmod JSON module graph to extract all module names/versions."""
34+
if modules is None:
35+
modules = set()
36+
37+
if isinstance(node, dict):
38+
name = node.get("name")
39+
version = node.get("version")
40+
if name and version and name != "cel-python" and name != "cel-cpp":
41+
modules.add((name, version))
42+
43+
for val in node.values():
44+
find_modules(val, modules)
45+
elif isinstance(node, list):
46+
for item in node:
47+
find_modules(item, modules)
48+
49+
return modules
50+
51+
def urlopen_with_retry(
52+
url, headers=None, timeout=30, max_retries=5, backoff_factor=2
53+
):
54+
"""Executes urlopen with exponential backoff retry logic to absorb VM drops."""
55+
if headers is None:
56+
headers = {"User-Agent": "Mozilla/5.0"}
57+
58+
req = urllib.request.Request(url, headers=headers)
59+
60+
retries = 0
61+
delay = 1
62+
while True:
63+
try:
64+
# Use standard urlopen (respects proxy settings)
65+
return urllib.request.urlopen(req, timeout=timeout)
66+
except (urllib.error.URLError, ConnectionError, TimeoutError, Exception) as e:
67+
retries += 1
68+
if retries > max_retries:
69+
print(f"Failed to fetch {url} after {max_retries} attempts.", file=sys.stderr)
70+
raise e
71+
print(f"Error fetching {url} (attempt {retries}/{max_retries}): {e}. Retrying in {delay}s...", file=sys.stderr)
72+
time.sleep(delay)
73+
delay *= backoff_factor
74+
75+
def main():
76+
if len(sys.argv) < 3:
77+
print("Usage: python download_patches.py <graph.json> <distdir_base_path>", file=sys.stderr)
78+
sys.exit(1)
79+
80+
graph_path = sys.argv[1]
81+
distdir_base = sys.argv[2]
82+
83+
if not os.path.exists(graph_path):
84+
print(f"Error: {graph_path} not found.", file=sys.stderr)
85+
sys.exit(1)
86+
87+
with open(graph_path, "r") as f:
88+
graph = json.load(f)
89+
90+
modules = find_modules(graph)
91+
print(f"Found {len(modules)} unique transitive modules.", file=sys.stderr)
92+
93+
patches_found = []
94+
95+
for name, version in sorted(modules):
96+
url = f"https://bcr.bazel.build/modules/{name}/{version}/source.json"
97+
try:
98+
with urlopen_with_retry(url, timeout=30) as response:
99+
source_data = json.loads(response.read().decode())
100+
patches = source_data.get("patches")
101+
if patches:
102+
print(f"Module {name}@{version} has patches: {list(patches.keys())}", file=sys.stderr)
103+
for patch_name in patches.keys():
104+
patch_url = f"https://bcr.bazel.build/modules/{name}/{version}/patches/{patch_name}"
105+
patches_found.append({
106+
"module": name,
107+
"version": version,
108+
"patch_name": patch_name,
109+
"url": patch_url
110+
})
111+
except urllib.error.HTTPError as e:
112+
if e.code != 404:
113+
print(f"HTTP Error for {name}@{version}: {e.code}", file=sys.stderr)
114+
except Exception as e:
115+
print(f"Error fetching {name}@{version}: {e}", file=sys.stderr)
116+
117+
print(f"Found {len(patches_found)} patches to download.", file=sys.stderr)
118+
119+
distdir_dirs = set()
120+
121+
for patch in patches_found:
122+
module_clean = patch["module"].replace("-", "_")
123+
dir_name = os.path.join(distdir_base, module_clean)
124+
distdir_dirs.add(dir_name)
125+
126+
os.makedirs(dir_name, exist_ok=True)
127+
dest_file = os.path.join(dir_name, patch["patch_name"])
128+
129+
print(f"Downloading {patch['url']} to {dest_file}...", file=sys.stderr)
130+
try:
131+
with (
132+
urlopen_with_retry(patch["url"], timeout=30) as response,
133+
open(dest_file, "wb") as out,
134+
):
135+
out.write(response.read())
136+
except Exception as e:
137+
print(f"Failed to download {patch['url']}: {e}", file=sys.stderr)
138+
sys.exit(1)
139+
140+
# Generate the env setting command for batch
141+
distdir_flags = []
142+
for d in sorted(distdir_dirs):
143+
d_win = d.replace("/", "\\")
144+
distdir_flags.append(f"--distdir={d_win}")
145+
146+
flags_str = " ".join(distdir_flags)
147+
print(f'@set "DISTDIR_FLAGS={flags_str}"')
148+
149+
if __name__ == "__main__":
150+
main()

0 commit comments

Comments
 (0)