Skip to content

Commit d717eb0

Browse files
laramielcopybara-github
authored andcommitted
Revise tests for bazel_to_cmake
Introduces golden_test.py which validates cmake file generation. Minor CMake format updates in a few cases. Adds example testdata for the following: * native_rules * bazel_skylib * grpc_generate_cc * local_mirror * third_party_http_archive * rules_nasm PiperOrigin-RevId: 492229343 Change-Id: Id8e47ea2b54427958639073acfc3e42ffabd9019
1 parent 618169c commit d717eb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+908
-716
lines changed

.bazelignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Ignore .git folders
2+
.git
3+
4+
# See: https://github.com/bazelbuild/bazel/issues/7093
5+
tools/cmake/bazel_to_cmake
6+
tools/cmake/bazel_to_cmake/bzl_library
7+
tools/cmake/bazel_to_cmake/starlark
8+
tools/cmake/bazel_to_cmake/testdata
9+
tools/cmake/bazel_to_cmake/testdata/native_rules
10+
tools/cmake/bazel_to_cmake/testdata/rules_nasm
11+
tools/cmake/bazel_to_cmake/testdata/bazel_skylib
12+
tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc
13+
tools/cmake/bazel_to_cmake/testdata/local_mirror
14+
tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/local_mirror/lpm
15+
tools/cmake/bazel_to_cmake/testdata/third_party_http_archive

tools/cmake/bazel_to_cmake/bzl_library/bazel_skylib.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from ..starlark.provider import TargetInfo
4040
from ..starlark.select import Configurable
4141
from ..util import cmake_is_true
42+
from ..util import cmake_is_windows
4243
from ..util import write_file_if_not_already_equal
4344

4445

@@ -142,11 +143,12 @@ def _expand_template_impl(
142143
CMakeDepsProvider([cmake_target_pair.dep]),
143144
FilesProvider([out_file])))
144145

145-
template_target = _context.resolve_target_or_label(
146+
resolved_template = _context.resolve_target_or_label(
146147
cast(RelativeLabel, _context.evaluate_configurable(template)))
147148

148149
deps: List[CMakeTarget] = []
149-
template_paths = state.get_file_paths(template_target, deps)
150+
template_paths = state.get_file_paths(resolved_template, deps)
151+
150152
assert len(template_paths) == 1
151153
template_path = template_paths[0]
152154
script_path = os.path.join(os.path.dirname(__file__), "expand_template.py")
@@ -224,16 +226,21 @@ def _write_file_impl(
224226
_target: TargetId,
225227
_out_target: TargetId,
226228
content: Configurable[List[str]],
227-
newline: str,
229+
newline: Configurable[str],
228230
**kwargs,
229231
):
230232
del kwargs
231233
out_file = _context.get_generated_file_path(_out_target)
232234
_context.add_analyzed_target(_out_target,
233235
TargetInfo(FilesProvider([out_file])))
234236
_context.add_analyzed_target(_target, TargetInfo())
235-
if newline == "unix" or (newline == "auto" and _context.access(
236-
EvaluationState).workspace.cmake_vars["CMAKE_SYSTEM_NAME"] != "Windows"):
237+
238+
resolved_newline = _context.evaluate_configurable(newline)
239+
240+
if resolved_newline == "unix" or (
241+
resolved_newline == "auto" and not cmake_is_windows(
242+
_context.access(
243+
EvaluationState).workspace.cmake_vars["CMAKE_SYSTEM_NAME"])):
237244
nl = "\n"
238245
else:
239246
nl = "\r\n"

tools/cmake/bazel_to_cmake/bzl_library/bazel_skylib_test.py

-87
This file was deleted.

tools/cmake/bazel_to_cmake/bzl_library/local_mirror.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _local_mirror_impl(_context: InvocationContext, **kwargs):
9595
if not sha256:
9696
raise ValueError(
9797
f"local_mirror requires SHA256 for downloaded file: {file}")
98-
out.write(f"\n EXPECTED_HASH SHA256={sha256})\n\n")
98+
out.write(f"""\n EXPECTED_HASH "SHA256={sha256}")\n\n""")
9999

100100
cmaketxt_path = pathlib.Path(os.path.join(local_mirror_dir, "CMakeLists.txt"))
101101

tools/cmake/bazel_to_cmake/bzl_library/rules_nasm.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ def _emit_nasm_library(
154154
if use_builtin_rule:
155155
_builder.addtext(
156156
f"""target_sources({target_name} PRIVATE {quote_list(all_srcs + dummy_sources)})
157-
target_include_directories({target_name} PRIVATE {quote_list(sorted(includes))})
158-
set_source_files_properties(
157+
target_include_directories({target_name} PRIVATE {quote_list(sorted(includes))})
158+
set_source_files_properties(
159159
{quote_list(all_srcs)}
160160
PROPERTIES
161161
LANGUAGE ASM_NASM
162-
COMPILE_OPTIONS {quote_string(";".join(flags))})
163-
""")
162+
COMPILE_OPTIONS {quote_string(";".join(flags))})\n""")
164163
if cmake_deps:
165164
_builder.addtext(
166165
f"add_dependencies({target_name} {quote_list(sorted(cmake_deps))})\n")

tools/cmake/bazel_to_cmake/bzl_library/third_party_http_archive.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@ def _get_fetch_content_invocation(
267267
out = io.StringIO()
268268
out.write(f"FetchContent_Declare({cmake_name}")
269269
if urls:
270-
out.write(f" URL {quote_string(urls[0])}")
270+
out.write(f"\n URL {quote_string(urls[0])}")
271271
if sha256:
272272
hash_str = f"SHA256={sha256}"
273-
out.write(f" URL_HASH {quote_string(hash_str)}")
273+
out.write(f"\n URL_HASH {quote_string(hash_str)}")
274274

275275
patch_commands = []
276276
for patch in patches or ():
@@ -306,8 +306,8 @@ def _get_fetch_content_invocation(
306306
f"""{quote_path(cmake_command)} -E copy {quote_path(new_cmakelists_path)} CMakeLists.txt"""
307307
)
308308
patch_command = " && ".join(patch_commands)
309-
out.write(f" PATCH_COMMAND {patch_command}")
310-
out.write(" OVERRIDE_FIND_PACKAGE)\n")
309+
out.write(f"\n PATCH_COMMAND {patch_command}")
310+
out.write("\n OVERRIDE_FIND_PACKAGE)\n")
311311
return out.getvalue()
312312

313313

tools/cmake/bazel_to_cmake/evaluation.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ class Phase(enum.Enum):
129129
def _get_kind(currentframe) -> Optional[str]:
130130
if not currentframe:
131131
return None
132-
return currentframe.f_back.f_code.co_name
132+
kind = currentframe.f_back.f_back.f_code.co_name
133+
if kind.startswith("bazel_"):
134+
kind = kind[len("bazel_"):]
135+
return kind
133136

134137

135138
class EvaluationState:

0 commit comments

Comments
 (0)