Skip to content

Commit

Permalink
bazel_to_cmake: prepare for enabling xds in gRPC
Browse files Browse the repository at this point in the history
This changes bazel_to_cmake to handle more edge cases.
* removes a bunch of generated target_include_directories with no associated hdrs.
* makes proto and cc strip_{include/import}_prefix consistent.
* improves variable substitution to add deps and handle cross-repo targets better.
* always uses the dict polyfill to change return type of keys()

PiperOrigin-RevId: 544229226
Change-Id: I4fa0b9c647ea98d9647194a5c0c38a6fe8e3d70d
  • Loading branch information
laramiel authored and copybara-github committed Jun 29, 2023
1 parent e23e45e commit 6a587e1
Show file tree
Hide file tree
Showing 27 changed files with 711 additions and 443 deletions.
12 changes: 10 additions & 2 deletions tools/cmake/bazel_to_cmake/bzl_library/rules_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""CMake implementation of "@rules_proto"."""
"""CMake implementation of "@rules_proto".
See: https://github.com/bazelbuild/rules_proto/blob/master/proto/defs.bzl
"""

# pylint: disable=relative-beyond-top-level

Expand All @@ -22,14 +25,19 @@


@register_bzl_library("@rules_proto//proto:defs.bzl", build=True)
class RulesCcDefsLibrary(BazelGlobals):
class RulesProtoDefsLibrary(BazelGlobals):

def bazel_proto_library(self, **kwargs):
return native_rules_proto.proto_library(self._context, **kwargs)

def bazel_proto_descriptor_set(self, **kwargs):
del kwargs
pass

@property
def bazel_proto_lang_toolchain(self):
return IgnoredObject()

def bazel_ProtoInfo(self, **kwargs):
del kwargs
return IgnoredObject()
6 changes: 3 additions & 3 deletions tools/cmake/bazel_to_cmake/cmake_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import collections
import json
import pathlib
from typing import Dict, List, Optional, Sequence, Set, Tuple, Union
from typing import Dict, List, Optional, Iterable, Set, Tuple, Union


def quote_string(x: str) -> str:
Expand All @@ -41,12 +41,12 @@ def quote_path(x: Union[str, pathlib.PurePath]) -> str:
return json.dumps(x.as_posix())


def quote_list(y: Sequence[str], separator: str = " ") -> str:
def quote_list(y: Iterable[str], separator: str = " ") -> str:
return separator.join(quote_string(x) for x in y)


def quote_path_list(
y: Union[Sequence[str], Sequence[pathlib.PurePosixPath]],
y: Union[Iterable[str], Iterable[pathlib.PurePosixPath]],
separator: str = " ",
) -> str:
return separator.join(quote_path(x) for x in y)
Expand Down
9 changes: 7 additions & 2 deletions tools/cmake/bazel_to_cmake/cmake_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pathlib
import re
from typing import Any, Dict, List, NamedTuple
from typing import Any, Dict, List, NamedTuple, Optional

from .cmake_target import CMakePackage
from .cmake_target import CMakeTarget
Expand Down Expand Up @@ -79,6 +79,12 @@ def set_persisted_canonical_name(
assert target_id.repository_id == self.repository_id
self.persisted_canonical_name[target_id] = cmake_target_pair

def get_persisted_canonical_name(
self, target_id: TargetId
) -> Optional[CMakeTargetPair]:
assert target_id.repository_id == self.repository_id
return self.persisted_canonical_name.get(target_id, None)


def make_repo_mapping(
repository_id: RepositoryId, repo_mapping: Any
Expand All @@ -100,7 +106,6 @@ def get_pairs():
y = str(y)
assert y.startswith("@")
y = RepositoryId(y[1:])
assert y != repository_id
output[x] = y
return output

Expand Down
98 changes: 44 additions & 54 deletions tools/cmake/bazel_to_cmake/cmake_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,24 @@
# limitations under the License.
"""CMake Provider types."""

from typing import List, NamedTuple, NewType, Optional
from typing import List, NamedTuple, NewType, Optional, Type, TypeVar

from .starlark.provider import Provider

CMakePackage = NewType("CMakePackage", str)
CMakeTarget = NewType("CMakeTarget", str)


class CMakeTargetPair(NamedTuple):
"""CMakeTargetPair identifies a cmake target, optionally with an alias."""

cmake_package: Optional[CMakePackage]
target: CMakeTarget
alias: Optional[CMakeTarget] = None

def with_alias(self, alias: Optional[CMakeTarget]) -> "CMakeTargetPair":
return self._replace(alias=alias)

@property
def dep(self) -> CMakeTarget:
return self.alias or self.target

def as_providers(self, is_binary: bool = False):
return (
CMakeTargetPairProvider(self),
CMakeExecutableTargetProvider(self.target)
if is_binary
else CMakeLibraryTargetProvider(self.target),
CMakeDepsProvider([self.dep]),
)

def __str__(self) -> str:
raise NotImplementedError


class CMakeTargetPairProvider(Provider):
"""CMakeTargetPair provider decribing a target."""
class CMakePackageDepsProvider(Provider):
"""CMake packages required by a Bazel target."""

__slots__ = ("target_pair",)
__slots__ = ("packages",)

def __init__(self, target_pair: CMakeTargetPair):
self.target_pair = target_pair
def __init__(self, packages: List[CMakePackage]):
self.packages = packages

def __repr__(self):
return f"{self.__class__.__name__}({repr(self.target_pair)})"

@property
def dep(self) -> CMakeTarget:
return self.target_pair.dep

@property
def target(self) -> CMakeTarget:
return self.target_pair.target
return f"{self.__class__.__name__}({repr(self.packages)})"


class CMakeDepsProvider(Provider):
Expand All @@ -80,18 +45,6 @@ def __repr__(self):
return f"{self.__class__.__name__}({repr(self.targets)})"


class CMakePackageDepsProvider(Provider):
"""CMake packages required by a Bazel target."""

__slots__ = ("packages",)

def __init__(self, packages: List[CMakePackage]):
self.packages = packages

def __repr__(self):
return f"{self.__class__.__name__}({repr(self.packages)})"


class CMakeLibraryTargetProvider(Provider):
"""CMake target corresponding to a Bazel library target."""

Expand All @@ -114,3 +67,40 @@ def __init__(self, target: CMakeTarget):

def __repr__(self):
return f"{self.__class__.__name__}({repr(self.target)})"


AnyCMakeTargetProvider = TypeVar(
"AnyCMakeTargetProvider",
CMakeLibraryTargetProvider,
CMakeExecutableTargetProvider,
)


class CMakeTargetPair(NamedTuple):
"""CMakeTargetPair identifies a cmake target, optionally with an alias."""

cmake_package: Optional[CMakePackage]
target: CMakeTarget
alias: Optional[CMakeTarget] = None

def with_alias(self, alias: Optional[CMakeTarget]) -> "CMakeTargetPair":
return self._replace(alias=alias)

@property
def dep(self) -> CMakeTarget:
return self.alias or self.target

def as_providers(
self,
provider: Optional[
Type[AnyCMakeTargetProvider]
] = CMakeLibraryTargetProvider,
):
a = (provider(self.target),) if provider is not None else tuple()
return (
CMakeDepsProvider([self.dep]),
CMakePackageDepsProvider([self.cmake_package]),
) + a

def __str__(self) -> str:
raise NotImplementedError
Loading

0 comments on commit 6a587e1

Please sign in to comment.