Skip to content

Commit

Permalink
Revise tests for bazel_to_cmake
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laramiel authored and copybara-github committed Dec 1, 2022
1 parent 618169c commit d717eb0
Show file tree
Hide file tree
Showing 57 changed files with 908 additions and 716 deletions.
15 changes: 15 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Ignore .git folders
.git

# See: https://github.com/bazelbuild/bazel/issues/7093
tools/cmake/bazel_to_cmake
tools/cmake/bazel_to_cmake/bzl_library
tools/cmake/bazel_to_cmake/starlark
tools/cmake/bazel_to_cmake/testdata
tools/cmake/bazel_to_cmake/testdata/native_rules
tools/cmake/bazel_to_cmake/testdata/rules_nasm
tools/cmake/bazel_to_cmake/testdata/bazel_skylib
tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc
tools/cmake/bazel_to_cmake/testdata/local_mirror
tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/local_mirror/lpm
tools/cmake/bazel_to_cmake/testdata/third_party_http_archive
17 changes: 12 additions & 5 deletions tools/cmake/bazel_to_cmake/bzl_library/bazel_skylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from ..starlark.provider import TargetInfo
from ..starlark.select import Configurable
from ..util import cmake_is_true
from ..util import cmake_is_windows
from ..util import write_file_if_not_already_equal


Expand Down Expand Up @@ -142,11 +143,12 @@ def _expand_template_impl(
CMakeDepsProvider([cmake_target_pair.dep]),
FilesProvider([out_file])))

template_target = _context.resolve_target_or_label(
resolved_template = _context.resolve_target_or_label(
cast(RelativeLabel, _context.evaluate_configurable(template)))

deps: List[CMakeTarget] = []
template_paths = state.get_file_paths(template_target, deps)
template_paths = state.get_file_paths(resolved_template, deps)

assert len(template_paths) == 1
template_path = template_paths[0]
script_path = os.path.join(os.path.dirname(__file__), "expand_template.py")
Expand Down Expand Up @@ -224,16 +226,21 @@ def _write_file_impl(
_target: TargetId,
_out_target: TargetId,
content: Configurable[List[str]],
newline: str,
newline: Configurable[str],
**kwargs,
):
del kwargs
out_file = _context.get_generated_file_path(_out_target)
_context.add_analyzed_target(_out_target,
TargetInfo(FilesProvider([out_file])))
_context.add_analyzed_target(_target, TargetInfo())
if newline == "unix" or (newline == "auto" and _context.access(
EvaluationState).workspace.cmake_vars["CMAKE_SYSTEM_NAME"] != "Windows"):

resolved_newline = _context.evaluate_configurable(newline)

if resolved_newline == "unix" or (
resolved_newline == "auto" and not cmake_is_windows(
_context.access(
EvaluationState).workspace.cmake_vars["CMAKE_SYSTEM_NAME"])):
nl = "\n"
else:
nl = "\r\n"
Expand Down
87 changes: 0 additions & 87 deletions tools/cmake/bazel_to_cmake/bzl_library/bazel_skylib_test.py

This file was deleted.

2 changes: 1 addition & 1 deletion tools/cmake/bazel_to_cmake/bzl_library/local_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _local_mirror_impl(_context: InvocationContext, **kwargs):
if not sha256:
raise ValueError(
f"local_mirror requires SHA256 for downloaded file: {file}")
out.write(f"\n EXPECTED_HASH SHA256={sha256})\n\n")
out.write(f"""\n EXPECTED_HASH "SHA256={sha256}")\n\n""")

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

Expand Down
7 changes: 3 additions & 4 deletions tools/cmake/bazel_to_cmake/bzl_library/rules_nasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,12 @@ def _emit_nasm_library(
if use_builtin_rule:
_builder.addtext(
f"""target_sources({target_name} PRIVATE {quote_list(all_srcs + dummy_sources)})
target_include_directories({target_name} PRIVATE {quote_list(sorted(includes))})
set_source_files_properties(
target_include_directories({target_name} PRIVATE {quote_list(sorted(includes))})
set_source_files_properties(
{quote_list(all_srcs)}
PROPERTIES
LANGUAGE ASM_NASM
COMPILE_OPTIONS {quote_string(";".join(flags))})
""")
COMPILE_OPTIONS {quote_string(";".join(flags))})\n""")
if cmake_deps:
_builder.addtext(
f"add_dependencies({target_name} {quote_list(sorted(cmake_deps))})\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ def _get_fetch_content_invocation(
out = io.StringIO()
out.write(f"FetchContent_Declare({cmake_name}")
if urls:
out.write(f" URL {quote_string(urls[0])}")
out.write(f"\n URL {quote_string(urls[0])}")
if sha256:
hash_str = f"SHA256={sha256}"
out.write(f" URL_HASH {quote_string(hash_str)}")
out.write(f"\n URL_HASH {quote_string(hash_str)}")

patch_commands = []
for patch in patches or ():
Expand Down Expand Up @@ -306,8 +306,8 @@ def _get_fetch_content_invocation(
f"""{quote_path(cmake_command)} -E copy {quote_path(new_cmakelists_path)} CMakeLists.txt"""
)
patch_command = " && ".join(patch_commands)
out.write(f" PATCH_COMMAND {patch_command}")
out.write(" OVERRIDE_FIND_PACKAGE)\n")
out.write(f"\n PATCH_COMMAND {patch_command}")
out.write("\n OVERRIDE_FIND_PACKAGE)\n")
return out.getvalue()


Expand Down
5 changes: 4 additions & 1 deletion tools/cmake/bazel_to_cmake/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ class Phase(enum.Enum):
def _get_kind(currentframe) -> Optional[str]:
if not currentframe:
return None
return currentframe.f_back.f_code.co_name
kind = currentframe.f_back.f_back.f_code.co_name
if kind.startswith("bazel_"):
kind = kind[len("bazel_"):]
return kind


class EvaluationState:
Expand Down
Loading

0 comments on commit d717eb0

Please sign in to comment.