mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
The ultimate effect of this change will be that both the native flags and Starlark flags will continue to work with future versions of Bazel that lack the proto fragment, until such time as we deprecate or remove the flags in a Protobuf release. This decouples Bazel releases from Protobuf behavior changes. 1. Builds on the Bazel 8-compatible abstraction layer we submitted in cl/955468205 by setting `HAS_NATIVE_PROTO_FLAGS` properly in Bazel. We do this by adding a `rules.has_proto_fragment` setting to the `proto_bazel_features.bzl` mechanism: we know that the fragment no longer needs to be read in Bazel 9+, because we can use `flag_alias()` instead. 2. Adds `flag_alias()` declarations in MODULE.bazel for all compat layer flags. This will work in Bazel 9, but is ignored in Bazel 8. This leads to a slight inconsistency. Consider the following set of flags set by a user: ``` --@protobuf//bazel/flags:protocopt=foo --protocopt=bar ``` In Bazel 9+, where the flag alias is respected, the final value of the flag will be `bar`. In Bazel 8, where we emulate `flag_alias()` by reconciling the two different values at analysis time, we always prefer the Starlark version (and cannot tell the relative order), so the final value of the flag will be `foo`. To avoid this, prefer setting the Starlark version of the flag whenever possible. PiperOrigin-RevId: 970834999
161 lines
3.5 KiB
Python
161 lines
3.5 KiB
Python
#!/usr/bin/python
|
|
#
|
|
# Protocol Buffers - Google's data interchange format
|
|
# Copyright 2023 Google LLC. All rights reserved.
|
|
#
|
|
# Use of this source code is governed by a BSD-style
|
|
# license that can be found in the LICENSE file or at
|
|
# https://developers.google.com/open-source/licenses/bsd
|
|
|
|
"""A tool to convert MODULE.bazel -> CMakeLists.txt.
|
|
|
|
This tool is very protobuf-specific at the moment, and should not be seen as a
|
|
generic Bazel -> CMake converter.
|
|
"""
|
|
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import textwrap
|
|
|
|
|
|
class ExtensionFunctions(object):
|
|
"""A fake extension that we can use to get the functions we need."""
|
|
|
|
def defaults(self, *args, **kwargs):
|
|
pass
|
|
|
|
def toolchain(self, *args, **kwargs):
|
|
pass
|
|
|
|
def parse(self, *args, **kwargs):
|
|
pass
|
|
|
|
def spec(self, *args, **kwargs):
|
|
pass
|
|
|
|
def from_specs(self, *args, **kwargs):
|
|
pass
|
|
|
|
def install(self, *args, **kwargs):
|
|
pass
|
|
|
|
def bundle_fetch(self, *args, **kwargs):
|
|
pass
|
|
|
|
def find(self, *args, **kwargs):
|
|
pass
|
|
|
|
def source_archive(self, *args, **kwargs):
|
|
pass
|
|
|
|
def nuget_package(self, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
def empty_func(*args, **kwargs):
|
|
pass
|
|
|
|
|
|
class ModuleFileFunctions(object):
|
|
"""A fake MODULE file that we can exec() to get the functions we need."""
|
|
|
|
def __init__(self, converter):
|
|
self.converter = converter
|
|
|
|
def module(self, *args, **kwargs):
|
|
pass
|
|
|
|
def bazel_dep(self, name, version, **kwargs):
|
|
# Strip BCR-specific version suffixes (e.g. 1.17.0.bcr.2 -> 1.17.0)
|
|
# since they don't correspond to upstream git tags.
|
|
version = re.sub(r"\.bcr\.\d+", "", version)
|
|
self.converter.toplevel += textwrap.dedent(
|
|
"""\
|
|
set(%(name)s-version "%(version)s")
|
|
"""
|
|
% {
|
|
"name": name,
|
|
"version": version,
|
|
}
|
|
)
|
|
|
|
def register_toolchains(self, *args, **kwargs):
|
|
pass
|
|
|
|
def flag_alias(self, *args, **kwargs):
|
|
pass
|
|
|
|
def use_repo(self, *args, **kwargs):
|
|
pass
|
|
|
|
def use_repo_rule(self, *args, **kwargs):
|
|
return empty_func
|
|
|
|
def single_version_override(self, *args, **kwargs):
|
|
pass
|
|
|
|
def use_extension(self, *args, **kwargs):
|
|
return ExtensionFunctions()
|
|
|
|
def local_path_override(self, *args, **kwargs):
|
|
pass
|
|
|
|
def git_override(self, *args, **kwargs):
|
|
pass
|
|
|
|
def archive_override(self, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
class Converter(object):
|
|
|
|
def __init__(self):
|
|
self.toplevel = ""
|
|
self.if_lua = ""
|
|
|
|
def convert(self):
|
|
return self.template % {
|
|
"toplevel": converter.toplevel,
|
|
}
|
|
|
|
template = textwrap.dedent("""\
|
|
# Auto-generated by @//cmake:make_dependencies
|
|
#
|
|
# This file contains lists of external dependencies based on our Bazel
|
|
# config. It should be included from a hand-written CMake file that uses
|
|
# them.
|
|
#
|
|
# Changes to this file will be overwritten based on Bazel definitions.
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER 3.16 OR ${CMAKE_VERSION} VERSION_EQUAL 3.16)
|
|
include_guard()
|
|
endif()
|
|
|
|
%(toplevel)s
|
|
|
|
""")
|
|
|
|
|
|
data = {}
|
|
converter = Converter()
|
|
|
|
|
|
def GetDict(obj):
|
|
ret = {}
|
|
for k in dir(obj):
|
|
if not k.startswith("_"):
|
|
ret[k] = getattr(obj, k)
|
|
return ret
|
|
|
|
|
|
# We take the MODULE path as a command-line argument to ensure that we can find
|
|
# it regardless of how exactly Bazel was invoked.
|
|
exec(open(sys.argv[1]).read(), GetDict(ModuleFileFunctions(converter)))
|
|
|
|
with open(sys.argv[2], "w") as f:
|
|
f.write(converter.convert())
|