Skip to content

Commit 90cba53

Browse files
laramielcopybara-github
authored andcommitted
Improve some bazel-to-cmake translation affecting AOM.
This mainly affects proto generation; if a strip_import_prefix path is used the output files should be generated in a path which does not require the strip_import_prefix to be proppagated in the target includes. Also fixed an issue where the empty source was not added to alwayslink targets. Basically, alwayslink cannot be INTERFACE targets in CMake. Finally, changed includes to look at public/private header paths when choosing whether to add includes as public/private. PiperOrigin-RevId: 895497540 Change-Id: Ic679b5de863bab74281fb0186ae69a362741e598
1 parent 357afc1 commit 90cba53

20 files changed

Lines changed: 747 additions & 463 deletions

File tree

tools/cmake/bazel_to_cmake/bzl_library/grpc_generate_cc.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
# Copyright 2022 The TensorStore Authors
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-
"""CMake implementation of "@tensorstore//bazel:cc_grpc_library.bzl"."""
15-
161
# pylint: disable=invalid-name,missing-function-docstring,relative-beyond-top-level,g-long-lambda
2+
3+
from collections.abc import Sequence
174
import io
18-
from typing import Any, List, Optional, cast
5+
from typing import Any, cast
196

207
from ..cmake_builder import CMakeBuilder
218
from ..cmake_provider import CMakeAddDependenciesProvider
@@ -25,26 +12,41 @@
2512
from ..native_aspect_proto import btc_protobuf
2613
from ..native_aspect_proto import plugin_generated_files
2714
from ..native_aspect_proto import PluginSettings
28-
from ..native_aspect_proto import maybe_augment_output_dir
29-
from ..starlark.scope_common import ScopeCommon
3015
from ..starlark.bazel_target import RepositoryId
3116
from ..starlark.bazel_target import TargetId
3217
from ..starlark.common_providers import FilesProvider
3318
from ..starlark.common_providers import ProtoLibraryProvider
3419
from ..starlark.invocation_context import InvocationContext
3520
from ..starlark.invocation_context import RelativeLabel
3621
from ..starlark.provider import TargetInfo
22+
from ..starlark.scope_common import ScopeCommon
3723
from ..starlark.select import Configurable
3824
from ..util import quote_path
3925
from .register import register_bzl_library
4026
from .upb_proto_library import UPB_PLUGIN # pylint: disable=unused-import
4127

28+
29+
# Copyright 2022 The TensorStore Authors
30+
#
31+
# Licensed under the Apache License, Version 2.0 (the "License");
32+
# you may not use this file except in compliance with the License.
33+
# You may obtain a copy of the License at
34+
#
35+
# http://www.apache.org/licenses/LICENSE-2.0
36+
#
37+
# Unless required by applicable law or agreed to in writing, software
38+
# distributed under the License is distributed on an "AS IS" BASIS,
39+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+
# See the License for the specific language governing permissions and
41+
# limitations under the License.
42+
"""CMake implementation of "@tensorstore//bazel:cc_grpc_library.bzl"."""
43+
4244
GRPC_REPO = RepositoryId("grpc")
4345

4446
_SEP = "\n "
4547

4648

47-
def _empty_target_list(t: TargetId) -> List[TargetId]:
49+
def _empty_target_list(t: TargetId) -> list[TargetId]:
4850
del t
4951
return []
5052

@@ -66,7 +68,7 @@ def bazel_generate_cc(
6668
self,
6769
well_known_protos: Any,
6870
name: str,
69-
visibility: Optional[List[RelativeLabel]] = None,
71+
visibility: Sequence[RelativeLabel] | None = None,
7072
**kwargs,
7173
):
7274
context = self._context.snapshot()
@@ -86,9 +88,9 @@ def bazel_generate_cc(
8688
def _generate_grpc_cc_impl(
8789
_context: InvocationContext,
8890
_target: TargetId,
89-
srcs: Optional[Configurable[List[RelativeLabel]]] = None,
90-
plugin: Optional[Configurable[RelativeLabel]] = None,
91-
flags: Optional[List[str]] = None,
91+
srcs: Configurable[Sequence[RelativeLabel]] | None = None,
92+
plugin: Configurable[RelativeLabel] | None = None,
93+
flags: Sequence[str] | None = None,
9294
**kwargs,
9395
):
9496
del kwargs
@@ -153,6 +155,7 @@ def _generate_grpc_cc_impl(
153155
src,
154156
plugin_settings,
155157
repo.cmake_binary_dir,
158+
proto_library_provider,
156159
):
157160
_context.add_analyzed_target(
158161
t[0],
@@ -177,12 +180,9 @@ def _generate_grpc_cc_impl(
177180
src_collector = state.collect_targets(srcs_with_service)
178181
state.collect_deps(UPB_PLUGIN.aspectdeps(srcs_with_service[0]))
179182

180-
# Augment output with strip import prefix
181-
output_dir = repo.replace_with_cmake_macro_dirs([
182-
maybe_augment_output_dir(
183-
_context, proto_library_provider, repo.cmake_binary_dir
184-
)
185-
])[0]
183+
# The generated files should be in a directory with the strip_import_prefix
184+
# already removed.
185+
output_dir = repo.replace_with_cmake_macro_dirs([repo.cmake_binary_dir])[0]
186186

187187
# Emit.
188188
out = io.StringIO()

tools/cmake/bazel_to_cmake/bzl_library/rules_nasm.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
# pylint: disable=relative-beyond-top-level,invalid-name,missing-function-docstring,missing-class-docstring
1717

18+
from collections.abc import Callable, Collection
1819
import hashlib
1920
import io
2021
import itertools
2122
import os
2223
import pathlib
23-
from typing import Callable, Collection, List, Optional, cast
24+
from typing import cast
2425

2526
from .. import native_rules_cc
2627
from ..cmake_builder import CMakeBuilder
@@ -40,6 +41,7 @@
4041
from ..starlark.scope_common import ScopeCommon
4142
from ..starlark.select import Configurable
4243
from ..util import cmake_is_true
44+
from ..util import make_relative_path
4345
from ..util import partition_by
4446
from ..util import quote_list
4547
from ..util import quote_path
@@ -68,7 +70,7 @@ class RulesNasmLibrary(ScopeCommon):
6870
def bazel_nasm_library(
6971
self,
7072
name: str,
71-
visibility: Optional[List[RelativeLabel]] = None,
73+
visibility: list[RelativeLabel] | None = None,
7274
**kwargs,
7375
):
7476
context = self._context.snapshot()
@@ -89,7 +91,7 @@ class RulesNasmCcLibrary(ScopeCommon):
8991
def bazel_nasm_cc_library(
9092
self,
9193
name: str,
92-
visibility: Optional[List[RelativeLabel]] = None,
94+
visibility: list[RelativeLabel] | None = None,
9395
**kwargs,
9496
):
9597
context = self._context.snapshot()
@@ -145,11 +147,11 @@ def _common_nasm_resolve(
145147
_next: Callable[..., None],
146148
_context: InvocationContext,
147149
_target: TargetId,
148-
srcs: Optional[Configurable[List[RelativeLabel]]] = None,
149-
hdrs: Optional[Configurable[List[RelativeLabel]]] = None,
150-
preincs: Optional[Configurable[List[RelativeLabel]]] = None,
151-
copts: Optional[Configurable[List[str]]] = None,
152-
includes: Optional[Configurable[List[str]]] = None,
150+
srcs: Configurable[list[RelativeLabel]] | None = None,
151+
hdrs: Configurable[list[RelativeLabel]] | None = None,
152+
preincs: Configurable[list[RelativeLabel]] | None = None,
153+
copts: Configurable[list[str]] | None = None,
154+
includes: Configurable[list[str]] | None = None,
153155
**kwargs,
154156
):
155157
"""Applies evaluate_configurable to common (and uncommon) nasm arguments."""
@@ -175,7 +177,7 @@ def _construct_nasm_includes(
175177
current_package_id: PackageId,
176178
source_directory: pathlib.PurePath,
177179
cmake_binary_dir: pathlib.PurePath,
178-
includes: Optional[Collection[str]] = None,
180+
includes: Collection[str] | None = None,
179181
):
180182
"""Returns the set of includes for the configuration."""
181183

@@ -212,11 +214,11 @@ def _nasm_library_impl(
212214
_context: InvocationContext,
213215
_target: TargetId,
214216
*,
215-
srcs: List[TargetId],
216-
hdrs: List[TargetId],
217-
preincs: List[TargetId],
218-
copts: List[str],
219-
includes: List[str],
217+
srcs: list[TargetId],
218+
hdrs: list[TargetId],
219+
preincs: list[TargetId],
220+
copts: list[str],
221+
includes: list[str],
220222
**kwargs,
221223
):
222224
del kwargs
@@ -229,7 +231,7 @@ def _nasm_library_impl(
229231
srcs_collector = state.collect_targets(srcs)
230232
preincs_collector = state.collect_targets(preincs)
231233

232-
cmake_deps: List[CMakeTarget] = list(
234+
cmake_deps: list[CMakeTarget] = list(
233235
itertools.chain(
234236
srcs_collector.add_dependencies(),
235237
preincs_collector.add_dependencies(),
@@ -259,12 +261,18 @@ def _nasm_library_impl(
259261
o_ext = _get_obj_suffix(state)
260262

261263
def _output_file(src: pathlib.PurePath):
264+
# Use relative path for hashing to ensure stability across workspaces.
265+
_, relative_src = make_relative_path(
266+
src,
267+
(None, repo.source_directory),
268+
(None, repo.cmake_binary_dir),
269+
)
262270
return os.path.join(
263271
repo.cmake_binary_dir,
264272
"_nasm",
265273
_target.target_name,
266274
hashlib.sha256(
267-
(_target.as_label() + str(src)).encode("utf-8")
275+
f"{_target.as_label()}{relative_src.as_posix()}".encode("utf-8")
268276
).hexdigest()[:8],
269277
os.path.basename(src) + o_ext,
270278
)
@@ -304,13 +312,13 @@ def _emit_nasm_assemble(
304312
out: io.StringIO,
305313
src: pathlib.PurePath,
306314
generated_obj: pathlib.PurePath,
307-
copts: List[str],
308-
cmake_deps: List[CMakeTarget],
315+
copts: list[str],
316+
cmake_deps: list[CMakeTarget],
309317
):
310318
"""Generates an NASM library target."""
311319
out.write(f"""add_custom_command(
312320
OUTPUT {quote_path(generated_obj)}
313-
DEPENDS {quote_path_list([src] + cast(List[str], cmake_deps))}
321+
DEPENDS {quote_path_list([src] + cast(list[str], cmake_deps))}
314322
COMMAND ${{CMAKE_ASM_NASM_COMPILER}}
315323
-f ${{CMAKE_ASM_NASM_OBJECT_FORMAT}}
316324
${{CMAKE_ASM_NASM_FLAGS}}
@@ -329,11 +337,11 @@ def _nasm_cc_library_builtin_impl(
329337
_context: InvocationContext,
330338
_target: TargetId,
331339
*,
332-
srcs: List[TargetId],
333-
hdrs: List[TargetId],
334-
preincs: List[TargetId],
335-
copts: List[str],
336-
includes: List[str],
340+
srcs: list[TargetId],
341+
hdrs: list[TargetId],
342+
preincs: list[TargetId],
343+
copts: list[str],
344+
includes: list[str],
337345
alwayslink: bool = False,
338346
linkstatic: bool = False,
339347
**kwargs,
@@ -348,7 +356,7 @@ def _nasm_cc_library_builtin_impl(
348356
srcs_collector = state.collect_targets(srcs)
349357
preincs_collector = state.collect_targets(preincs)
350358

351-
cmake_deps: List[CMakeTarget] = list(
359+
cmake_deps: list[CMakeTarget] = list(
352360
itertools.chain(
353361
srcs_collector.add_dependencies(),
354362
preincs_collector.add_dependencies(),
@@ -430,9 +438,9 @@ def _emit_nasm_sources(
430438
out: io.StringIO,
431439
target_name: CMakeTarget,
432440
srcs: set[pathlib.PurePath],
433-
includes: List[pathlib.PurePath],
434-
copts: List[str],
435-
cmake_deps: List[CMakeTarget],
441+
includes: list[pathlib.PurePath],
442+
copts: list[str],
443+
cmake_deps: list[CMakeTarget],
436444
):
437445
"""Generates an NASM library target."""
438446
_sep = "\n "

0 commit comments

Comments
 (0)