From db9432e3fea419229cfdfeefdac738ca6b7bb2b0 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Wed, 11 Sep 2024 15:37:27 -0700 Subject: [PATCH] bazel_to_cmake update: Emit include directories more like bazel. In emit_cc, when generating public includes, generate them more similarly to how bazel generates the includes. Also add additional golden tests and repair some tests. NOTE: This still cannot match bazel since bazel constructs virtual include directories and creates include symlinks between them, which bazel_to_cmake does not do. PiperOrigin-RevId: 673563783 Change-Id: I44fc90124608161fd1e15bd7af0d44e570598d6c --- tools/cmake/BUILD | 90 +++--- tools/cmake/bazel_to_cmake/emit_cc.py | 256 +++++++++++------- tools/cmake/bazel_to_cmake/emit_cc_test.py | 204 ++++++++++++++ tools/cmake/bazel_to_cmake/golden_test.py | 1 + .../bazel_to_cmake/native_rules_genrule.py | 4 +- .../bazel_to_cmake/native_rules_proto.py | 8 +- .../testdata/cc_includes/BUILD.bazel | 27 ++ .../testdata/cc_includes/WORKSPACE.bazel | 1 + .../bazel_to_cmake/testdata/cc_includes/b.cc | 3 + .../testdata/cc_includes/config.json | 19 ++ .../cc_includes/golden/build_rules.cmake | 220 +++++++++++++++ .../testdata/cc_includes/parent/BUILD.bazel | 80 ++++++ .../testdata/cc_includes/parent/child/a.cc | 3 + .../testdata/cc_includes/parent/child/a.h | 1 + .../testdata/cc_includes/parent/child/a.inc | 1 + .../grpc_generate_cc/golden/build_rules.cmake | 4 + .../local_mirror/golden/build_rules.cmake | 2 + .../testdata/native_rules/BUILD.bazel | 9 + .../testdata/native_rules/config.json | 10 +- .../native_rules/golden/build_rules.cmake | 218 +++------------ .../testdata/rules_proto/config.json | 5 +- .../golden/build_rules.cmake | 2 + .../testdata/upb_proto_library/config.json | 4 +- tools/cmake/bazel_to_cmake/util.py | 17 +- 24 files changed, 849 insertions(+), 340 deletions(-) create mode 100644 tools/cmake/bazel_to_cmake/emit_cc_test.py create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/BUILD.bazel create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/WORKSPACE.bazel create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/b.cc create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/config.json create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/golden/build_rules.cmake create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/BUILD.bazel create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.cc create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.h create mode 100644 tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.inc diff --git a/tools/cmake/BUILD b/tools/cmake/BUILD index 34426f141..35427fa11 100644 --- a/tools/cmake/BUILD +++ b/tools/cmake/BUILD @@ -26,6 +26,7 @@ SRCS = [ "bazel_to_cmake/cmake_repository.py", "bazel_to_cmake/cmake_target.py", "bazel_to_cmake/emit_cc.py", + "bazel_to_cmake/emit_cc_test.py", "bazel_to_cmake/evaluation.py", "bazel_to_cmake/golden_test.py", "bazel_to_cmake/main.py", @@ -73,72 +74,81 @@ SRCS = [ # glob(["bazel_to_cmake/testdata/**"]), TESTDATA = [ - "bazel_to_cmake/testdata/upb_proto_library/BUILD.bazel", - "bazel_to_cmake/testdata/upb_proto_library/a.cc", - "bazel_to_cmake/testdata/upb_proto_library/c.proto", - "bazel_to_cmake/testdata/upb_proto_library/config.json", - "bazel_to_cmake/testdata/upb_proto_library/golden/build_rules.cmake", - "bazel_to_cmake/testdata/upb_proto_library/d.proto", - "bazel_to_cmake/testdata/upb_proto_library/WORKSPACE.bazel", - "bazel_to_cmake/testdata/local_mirror/BUILD.bazel", + "bazel_to_cmake/testdata/bazel_skylib/BUILD.bazel", + "bazel_to_cmake/testdata/bazel_skylib/config.json", + "bazel_to_cmake/testdata/bazel_skylib/golden/_cmake_binary_dir_/config.h", + "bazel_to_cmake/testdata/bazel_skylib/golden/build_rules.cmake", + "bazel_to_cmake/testdata/bazel_skylib/WORKSPACE.bazel", + "bazel_to_cmake/testdata/cc_includes/BUILD.bazel", + "bazel_to_cmake/testdata/cc_includes/b.cc", + "bazel_to_cmake/testdata/cc_includes/config.json", + "bazel_to_cmake/testdata/cc_includes/golden/build_rules.cmake", + "bazel_to_cmake/testdata/cc_includes/parent/child/a.h", + "bazel_to_cmake/testdata/cc_includes/parent/child/a.cc", + "bazel_to_cmake/testdata/cc_includes/parent/child/a.inc", + "bazel_to_cmake/testdata/cc_includes/parent/BUILD.bazel", + "bazel_to_cmake/testdata/cc_includes/WORKSPACE.bazel", + "bazel_to_cmake/testdata/grpc_generate_cc/a.cc", + "bazel_to_cmake/testdata/grpc_generate_cc/BUILD.bazel", + "bazel_to_cmake/testdata/grpc_generate_cc/c.proto", + "bazel_to_cmake/testdata/grpc_generate_cc/config.json", + "bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake", + "bazel_to_cmake/testdata/grpc_generate_cc/WORKSPACE.bazel", "bazel_to_cmake/testdata/local_mirror/a.cc", + "bazel_to_cmake/testdata/local_mirror/BUILD.bazel", "bazel_to_cmake/testdata/local_mirror/config.json", - "bazel_to_cmake/testdata/local_mirror/golden/_find_pkg_redirects_/lpm-config.cmake", - "bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/_local_mirror/lpm-src/BUILD.bazel", "bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/_local_mirror/lpm-src/b.cc", "bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/_local_mirror/lpm-src/b.h", + "bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/_local_mirror/lpm-src/BUILD.bazel", "bazel_to_cmake/testdata/local_mirror/golden/_cmake_binary_dir_/_local_mirror/lpm-src/CMakeLists.txt", + "bazel_to_cmake/testdata/local_mirror/golden/_find_pkg_redirects_/lpm-config.cmake", "bazel_to_cmake/testdata/local_mirror/golden/build_rules.cmake", "bazel_to_cmake/testdata/local_mirror/WORKSPACE.bazel", - "bazel_to_cmake/testdata/rules_nasm/BUILD.bazel", - "bazel_to_cmake/testdata/rules_nasm/a.cc", - "bazel_to_cmake/testdata/rules_nasm/a.h", - "bazel_to_cmake/testdata/rules_nasm/a.asm", - "bazel_to_cmake/testdata/rules_nasm/config.json", - "bazel_to_cmake/testdata/rules_nasm/golden/build_rules.cmake", - "bazel_to_cmake/testdata/rules_nasm/include/b.inc", - "bazel_to_cmake/testdata/rules_nasm/WORKSPACE.bazel", - "bazel_to_cmake/testdata/bazel_skylib/BUILD.bazel", - "bazel_to_cmake/testdata/bazel_skylib/config.json", - "bazel_to_cmake/testdata/bazel_skylib/golden/_cmake_binary_dir_/config.h", - "bazel_to_cmake/testdata/bazel_skylib/golden/build_rules.cmake", - "bazel_to_cmake/testdata/bazel_skylib/WORKSPACE.bazel", - "bazel_to_cmake/testdata/native_rules/y.h", - "bazel_to_cmake/testdata/native_rules/BUILD.bazel", + "bazel_to_cmake/testdata/native_rules/.bazelrc", "bazel_to_cmake/testdata/native_rules/a.cc", + "bazel_to_cmake/testdata/native_rules/BUILD.bazel", "bazel_to_cmake/testdata/native_rules/c.proto", - "bazel_to_cmake/testdata/native_rules/.bazelrc", + "bazel_to_cmake/testdata/native_rules/config.json", + "bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake", "bazel_to_cmake/testdata/native_rules/subdir/BUILD.bazel", "bazel_to_cmake/testdata/native_rules/subdir/x.cc", "bazel_to_cmake/testdata/native_rules/subdir/z.proto", - "bazel_to_cmake/testdata/native_rules/config.json", - "bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake", - "bazel_to_cmake/testdata/native_rules/x.h", "bazel_to_cmake/testdata/native_rules/WORKSPACE.bazel", + "bazel_to_cmake/testdata/native_rules/x.h", + "bazel_to_cmake/testdata/native_rules/y.h", + "bazel_to_cmake/testdata/rules_nasm/a.asm", + "bazel_to_cmake/testdata/rules_nasm/a.cc", + "bazel_to_cmake/testdata/rules_nasm/a.h", + "bazel_to_cmake/testdata/rules_nasm/BUILD.bazel", + "bazel_to_cmake/testdata/rules_nasm/config.json", + "bazel_to_cmake/testdata/rules_nasm/golden/build_rules.cmake", + "bazel_to_cmake/testdata/rules_nasm/include/b.inc", + "bazel_to_cmake/testdata/rules_nasm/WORKSPACE.bazel", + "bazel_to_cmake/testdata/rules_proto/a.proto", + "bazel_to_cmake/testdata/rules_proto/b.proto", "bazel_to_cmake/testdata/rules_proto/BUILD.bazel", "bazel_to_cmake/testdata/rules_proto/c.proto", "bazel_to_cmake/testdata/rules_proto/config.json", "bazel_to_cmake/testdata/rules_proto/golden/build_rules.cmake", - "bazel_to_cmake/testdata/rules_proto/b.proto", - "bazel_to_cmake/testdata/rules_proto/a.proto", "bazel_to_cmake/testdata/rules_proto/WORKSPACE.bazel", "bazel_to_cmake/testdata/rules_proto/x.proto", - "bazel_to_cmake/testdata/grpc_generate_cc/BUILD.bazel", - "bazel_to_cmake/testdata/grpc_generate_cc/a.cc", - "bazel_to_cmake/testdata/grpc_generate_cc/c.proto", - "bazel_to_cmake/testdata/grpc_generate_cc/config.json", - "bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake", - "bazel_to_cmake/testdata/grpc_generate_cc/WORKSPACE.bazel", - "bazel_to_cmake/testdata/third_party_http_archive/BUILD.bazel", "bazel_to_cmake/testdata/third_party_http_archive/a.cc", + "bazel_to_cmake/testdata/third_party_http_archive/BUILD.bazel", "bazel_to_cmake/testdata/third_party_http_archive/config.json", - "bazel_to_cmake/testdata/third_party_http_archive/golden/_find_pkg_redirects_/whole-config.cmake", - "bazel_to_cmake/testdata/third_party_http_archive/golden/_find_pkg_redirects_/half-extra.cmake", "bazel_to_cmake/testdata/third_party_http_archive/golden/_cmake_binary_dir_/third_party/CMakeLists.txt", "bazel_to_cmake/testdata/third_party_http_archive/golden/_cmake_binary_dir_/third_party/half-proxy-CMakeLists.txt", + "bazel_to_cmake/testdata/third_party_http_archive/golden/_find_pkg_redirects_/half-extra.cmake", + "bazel_to_cmake/testdata/third_party_http_archive/golden/_find_pkg_redirects_/whole-config.cmake", "bazel_to_cmake/testdata/third_party_http_archive/golden/build_rules.cmake", "bazel_to_cmake/testdata/third_party_http_archive/half.BUILD.bazel", "bazel_to_cmake/testdata/third_party_http_archive/WORKSPACE.bazel", + "bazel_to_cmake/testdata/upb_proto_library/a.cc", + "bazel_to_cmake/testdata/upb_proto_library/BUILD.bazel", + "bazel_to_cmake/testdata/upb_proto_library/c.proto", + "bazel_to_cmake/testdata/upb_proto_library/config.json", + "bazel_to_cmake/testdata/upb_proto_library/d.proto", + "bazel_to_cmake/testdata/upb_proto_library/golden/build_rules.cmake", + "bazel_to_cmake/testdata/upb_proto_library/WORKSPACE.bazel", ] tensorstore_pytest_test( diff --git a/tools/cmake/bazel_to_cmake/emit_cc.py b/tools/cmake/bazel_to_cmake/emit_cc.py index d5c674870..6a5344067 100644 --- a/tools/cmake/bazel_to_cmake/emit_cc.py +++ b/tools/cmake/bazel_to_cmake/emit_cc.py @@ -15,7 +15,6 @@ # pylint: disable=relative-beyond-top-level,invalid-name,missing-function-docstring,g-long-lambda -import os import pathlib import re from typing import Any, Dict, Iterable, List, Optional, Set, cast @@ -25,12 +24,15 @@ from .cmake_target import CMakeTarget from .cmake_target import CMakeTargetPair from .evaluation import EvaluationState +from .starlark.bazel_target import PackageId from .starlark.invocation_context import InvocationContext from .starlark.label import RelativeLabel from .starlark.select import Configurable from .util import is_relative_to +from .util import PathSequence from .util import quote_list from .util import quote_path_list +from .util import quote_unescaped_list from .workspace import Workspace _SEP = "\n " @@ -55,6 +57,7 @@ def _emit_cc_common_options( defines: Optional[Iterable[str]] = None, local_defines: Optional[Iterable[str]] = None, includes: Optional[Iterable[str]] = None, + private_includes: Optional[Iterable[str]] = None, custom_target_deps: Optional[Iterable[str]] = None, extra_public_compile_options: Optional[Iterable[str]] = None, interface_only: bool = False, @@ -64,21 +67,15 @@ def _emit_cc_common_options( """Emits CMake rules for common C++ target options.""" del kwargs - # PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR should be in includes - assert includes is not None - include_dirs = [ - f"$" - for include_dir in sorted(set(includes)) - ] public_context = "INTERFACE" if interface_only else "PUBLIC" if local_defines is not None and local_defines and not interface_only: _builder.addtext( f"target_compile_definitions({target_name} PRIVATE" - f" {quote_list(local_defines)})\n" + f" {quote_unescaped_list(local_defines)})\n" ) if defines is not None and defines: _builder.addtext( - f"target_compile_definitions({target_name} {public_context} {quote_list(defines)})\n" + f"target_compile_definitions({target_name} {public_context} {quote_unescaped_list(defines)})\n" ) if copts is not None and copts and not interface_only: _builder.addtext( @@ -91,7 +88,7 @@ def _emit_cc_common_options( link_libs.extend(sorted(deps)) link_options: List[str] = [] - for x in (linkopts or []): + for x in linkopts or []: if x.startswith("-l") or x.startswith("-framework"): link_libs.append(x) else: @@ -105,10 +102,26 @@ def _emit_cc_common_options( f"target_link_options({target_name} {public_context}{_SEP}{quote_list(link_options, separator=_SEP)})\n" ) + include_dirs = [ + f"$" + for include_dir in sorted(set(includes)) + ] if include_dirs: _builder.addtext( - f"target_include_directories({target_name} {public_context}{_SEP}{quote_path_list(include_dirs, separator=_SEP)})\n" + f"target_include_directories({target_name} {public_context}" + f"{_SEP}{quote_path_list(include_dirs, separator=_SEP)})\n" ) + if not interface_only and private_includes: + include_dirs = [ + f"$" + for include_dir in sorted(set(private_includes)) + ] + if include_dirs: + _builder.addtext( + f"target_include_directories({target_name} PRIVATE" + f"{_SEP}{quote_path_list(include_dirs, separator=_SEP)})\n" + ) + _builder.addtext( f"target_compile_features({target_name} {public_context} cxx_std_17)\n" ) @@ -139,144 +152,180 @@ def _emit_cc_common_options( def replace_with_cmake_macro_dirs( - repo: CMakeRepository, paths: Iterable[str] + repo: CMakeRepository, paths: PathSequence ) -> List[str]: """Substitute reposotory path prefixes with CMake PROJECT_{*}_DIR macros.""" assert repo is not None - result: List[str] = [] + result: Set[str] = set() for x in paths: x_path = pathlib.PurePath(x) if is_relative_to(x_path, repo.cmake_binary_dir): relative_path = x_path.relative_to(repo.cmake_binary_dir).as_posix() if relative_path != ".": - result.append(f"${{PROJECT_BINARY_DIR}}/{relative_path}") + result.add(f"${{PROJECT_BINARY_DIR}}/{relative_path}") else: - result.append("${PROJECT_BINARY_DIR}") + result.add("${PROJECT_BINARY_DIR}") elif is_relative_to(x_path, repo.source_directory): relative_path = x_path.relative_to(repo.source_directory).as_posix() if relative_path != ".": - result.append(f"${{PROJECT_SOURCE_DIR}}/{relative_path}") + result.add(f"${{PROJECT_SOURCE_DIR}}/{relative_path}") else: - result.append("${PROJECT_SOURCE_DIR}") + result.add("${PROJECT_SOURCE_DIR}") else: - result.append(x) - return result + result.add(str(x)) + return list(sorted(result)) def construct_cc_includes( - _context: InvocationContext, + repo: CMakeRepository, + current_package_id: PackageId, *, includes: Optional[Configurable[List[str]]] = None, include_prefix: Optional[str] = None, strip_include_prefix: Optional[str] = None, - srcs_file_paths: Optional[Iterable[str]] = None, - hdrs_file_paths: Optional[Iterable[str]] = None, + known_include_files: Optional[Iterable[str]] = None, ) -> List[str]: - state = _context.access(EvaluationState) - repo = state.workspace.all_repositories.get( - _context.caller_package_id.repository_id - ) + """Returns the list of system includes for the configuration. + + By default Bazel generates private includes (-iquote) for the SRCDIR + and a few other directories. When `includes` is set, then bazel generates + public includes (-isystem) which are propagated to callers. + + Here we attempt to generate system includes for CMake based on the + bazel flags, however it is a best effort technique which, so far, has met + the needs of cmake translation. + """ assert repo is not None + if not known_include_files: + known_include_files = [] - # When constructing include dirs, first check the "header-like" files in srcs - # and make sure that they can be included. include_dirs: Set[str] = set() - add_bare: bool = strip_include_prefix is None and include_prefix is None - - def _try_add( - paths: Optional[Iterable[str]], repo_paths: List[pathlib.PurePath] - ): - if paths is None: - return - nonlocal include_dirs - nonlocal repo - for x in paths: - for y in repo_paths: - if is_relative_to(pathlib.PurePath(x), y): - include_dirs.add(str(y)) - - def _try_add_prefix(paths: Optional[Iterable[str]], prefix: str): - if not paths: - return - if not prefix or prefix == ".": - nonlocal add_bare - add_bare = True - return - nonlocal repo - _try_add( - paths, - [ - repo.cmake_binary_dir.joinpath(prefix), - repo.source_directory.joinpath(prefix), - ], - ) + + current_package_path = pathlib.PurePosixPath(current_package_id.package_name) # This include manipulation is a best effort that works for known cases. # https://bazel.build/reference/be/c-cpp#cc_library.includes # - current_package_name = _context.caller_package_id.package_name - relative_package_path = pathlib.PurePosixPath(current_package_name) - - for include in _context.evaluate_configurable_list(includes): + # List of include dirs to be added to the compile line. Subject to + # "Make variable" substitution. Each string is prepended with the package + # path and passed to the C++ toolchain for expansion via the "include_paths" + # CROSSTOOL feature. A toolchain running on a POSIX system with typical + # feature definitions will produce -isystem path_to_package/include_entry. + # + # Unlike COPTS, these flags are added for this rule and every rule that + # depends on it. + for include in includes or []: # HACK(gRPC): grpc build_system.bzl adds the following includes to # all targets; bazel currently requires them, however they interfere in # the CMake build, so remove them. if ( - _context.caller_package_id.repository_id.repository_name + current_package_id.repository_id.repository_name == "com_github_grpc_grpc" and include in ["src/core/ext/upb-generated", "src/core/ext/upbdefs-generated"] ): continue - include_path = str( - relative_package_path.joinpath(pathlib.PurePosixPath(include)) - ) - if include_path[0] == "/": - include_path = include_path[1:] + constructed = str(current_package_path.joinpath(include)) + if constructed[0] == "/": + constructed = constructed[1:] - _try_add_prefix(hdrs_file_paths, include_path) + include_dirs.add(str(repo.source_directory.joinpath(constructed))) + include_dirs.add(str(repo.cmake_binary_dir.joinpath(constructed))) - # Assuming a package with files, a/b/c.h: - # Default include path is "a/b/c.h" - # When `strip_include_prefix = "/a"` is specified, the include path is "b/c.h" - # When `include_prefix = "x"` is specified, the include path is "x/a/b/c.h", - # however if both are specified, then the path would be "x/b/c.h". + # For the package foo/bar + # - default include path is foo/bar/file.h + # - strip_include_prefix=/foo then the include path is bar/file.h + # - include_prefix=bar then the include path is bar/file.h # # bazel_to_cmake only supports composing strip_import_prefix and import_prefix # which happens to be a part of the path already, otherwise we'd need to # create symbolic links to the files. if strip_include_prefix is not None: - # Normalize to a relative prefix. + # The prefix to strip from the paths of the headers of this rule. + # + # When set, the headers in the hdrs attribute of this rule are accessible + # at their path with this prefix cut off. + # + # If it's a relative path, it's taken as a package-relative one. If it's an + # absolute one, it's understood as a repository-relative path. + # + # The prefix in the include_prefix attribute is added after this prefix is + # stripped. if strip_include_prefix[0] == "/": - _try_add_prefix(hdrs_file_paths, strip_include_prefix[1:]) + constructed = strip_include_prefix[1:] else: - _try_add_prefix( - hdrs_file_paths, - str(relative_package_path.joinpath(strip_include_prefix)), - ) + constructed = str(current_package_path.joinpath(strip_include_prefix)) + + src_path = repo.source_directory.joinpath(constructed) + bin_path = repo.cmake_binary_dir.joinpath(constructed) + # Only add if existing files are discoverable at the prefix + for x in known_include_files: + x_path = pathlib.PurePath(x) + if is_relative_to(x_path, src_path): + include_dirs.add(str(src_path)) + elif is_relative_to(x_path, bin_path): + include_dirs.add(str(bin_path)) if include_prefix is not None: + # The prefix to add to the paths of the headers of this rule. + # # "When set, the headers in the hdrs attribute of this rule are accessable # at is the value of this attribute prepended to their repository-relative # path." # - # Bazel may create a sandbox (symlink tree) to support the newly composed - # prefix, but bazel_to_cmake does not. - if current_package_name.endswith(include_prefix): - computed_prefix = str( - pathlib.PurePosixPath(current_package_name[: -len(include_prefix)]) - ) - _try_add_prefix(hdrs_file_paths, computed_prefix) - - _try_add(srcs_file_paths, [repo.cmake_binary_dir, repo.source_directory]) - if add_bare: - _try_add(hdrs_file_paths, [repo.cmake_binary_dir, repo.source_directory]) + # The prefix in the strip_include_prefix attribute is removed before this + # prefix is added. + if current_package_id.package_name.endswith(include_prefix): + # Bazel creates a sandbox (symlink tree) to support the newly composed + # prefix, but bazel_to_cmake does not, so mimic this behavior by looking + # at the current package and trying to remove the prefix of the path, + # thus omitting it from the C search path. + constructed = current_package_id.package_name[: -len(include_prefix)] + src_path = repo.source_directory.joinpath(constructed) + bin_path = repo.cmake_binary_dir.joinpath(constructed) + # Only add if existing files are discoverable at the prefix + for x in known_include_files: + x_path = pathlib.PurePath(x) + if is_relative_to(x_path, src_path): + include_dirs.add(str(src_path)) + elif is_relative_to(x_path, bin_path): + include_dirs.add(str(bin_path)) + + # HACK: Bazel does not add such a fallback, but since three are potential + # includes CMake needs to include SRC/BIN as interface includes. + if not include_dirs: + for x in known_include_files: + x_path = pathlib.PurePath(x) + if is_relative_to(x_path, repo.source_directory): + include_dirs.add(str(repo.source_directory)) + elif is_relative_to(x_path, repo.cmake_binary_dir): + include_dirs.add(str(repo.cmake_binary_dir)) return replace_with_cmake_macro_dirs(repo, include_dirs) +def construct_cc_private_includes( + repo: CMakeRepository, + *, + includes: Optional[List[str]] = None, + known_include_files: Optional[Iterable[str]] = None, +) -> List[str]: + if not includes: + includes = [] + result: List[str] = [] + if "${PROJECT_SOURCE_DIR}" not in includes: + result.append("${PROJECT_SOURCE_DIR}") + if "${PROJECT_BINARY_DIR}" not in includes: + for x in known_include_files: + x_path = pathlib.PurePath(x) + if is_relative_to(x_path, repo.cmake_binary_dir): + result.append("${PROJECT_BINARY_DIR}") + break + return result + + def handle_cc_common_options( _context: InvocationContext, src_required=False, @@ -349,18 +398,29 @@ def add_compile_options(lang: str, options: List[str]): f" include_prefix={include_prefix}." ) + known_include_files = ( + [x for x in srcs_file_paths if re.search(_HEADER_SRC_PATTERN, x)] + + (hdrs_file_paths or []) + + (textual_hdrs_file_paths or []) + ) + result["includes"] = construct_cc_includes( - _context, - includes=includes, + state.workspace.all_repositories.get( + _context.caller_package_id.repository_id + ), + _context.caller_package_id, + includes=_context.evaluate_configurable_list(includes), include_prefix=include_prefix, strip_include_prefix=strip_include_prefix, - srcs_file_paths=[ - x for x in srcs_file_paths if re.search(_HEADER_SRC_PATTERN, x) - ], - hdrs_file_paths=set( - [os.path.dirname(x) for x in (hdrs_file_paths or [])] - + [os.path.dirname(x) for x in (textual_hdrs_file_paths or [])] + known_include_files=known_include_files, + ) + + result["private_includes"] = construct_cc_private_includes( + state.workspace.all_repositories.get( + _context.caller_package_id.repository_id ), + includes=result["includes"], + known_include_files=known_include_files, ) return result diff --git a/tools/cmake/bazel_to_cmake/emit_cc_test.py b/tools/cmake/bazel_to_cmake/emit_cc_test.py new file mode 100644 index 000000000..2c758a027 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/emit_cc_test.py @@ -0,0 +1,204 @@ +# Copyright 2022 The TensorStore Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. +"""Tests for emit_cc functions.""" + +# pylint: disable=g-importing-member +import pathlib + +from .cmake_repository import CMakeRepository +from .cmake_target import CMakePackage +from .emit_cc import construct_cc_includes +from .emit_cc import replace_with_cmake_macro_dirs +from .starlark.bazel_target import PackageId +from .starlark.bazel_target import RepositoryId + +REPO = CMakeRepository( + RepositoryId("foo"), + CMakePackage("Foo"), + pathlib.PurePath("foo-srcdir"), + pathlib.PurePath("foo-bindir"), + {}, + {}, +) + + +def test_replace_with_cmake_macro_dirs(): + assert [ + "${PROJECT_BINARY_DIR}/b", + "${PROJECT_SOURCE_DIR}/c", + "a", + ] == replace_with_cmake_macro_dirs( + REPO, + [ + "a", + REPO.cmake_binary_dir.joinpath("b"), + REPO.source_directory.joinpath("c"), + ], + ) + + +def test_construct_cc_includes_bare(): + # No includes + assert not construct_cc_includes( + REPO, + PackageId("foo", "bar"), + ) + # Individual srcdir / bindir includes + assert ["${PROJECT_SOURCE_DIR}"] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + known_include_files=["foo-srcdir/bar/a.inc"], + ) + assert ["${PROJECT_BINARY_DIR}"] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + known_include_files=["foo-bindir/bar/b.inc"], + ) + # Other combinations + assert [ + "${PROJECT_BINARY_DIR}", + "${PROJECT_SOURCE_DIR}", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + known_include_files=[ + "foo-srcdir/bar/e.inc", + "foo-bindir/bar/ee.h", + ], + ) + + +def test_construct_cc_includes_includes(): + # includes does not test for file presence. + assert [ + "${PROJECT_BINARY_DIR}/bar", + "${PROJECT_SOURCE_DIR}/bar", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + includes=["."], + ) + # package-relative + assert [ + "${PROJECT_BINARY_DIR}/bar/includes", + "${PROJECT_SOURCE_DIR}/bar/includes", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + includes=["includes"], + known_include_files=["foo-srcdir/includes/b.h"], + ) + assert [ + "${PROJECT_BINARY_DIR}/bar/includes", + "${PROJECT_SOURCE_DIR}/bar/includes", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + includes=["includes"], + known_include_files=["foo-srcdir/bar/includes/c.h"], + ) + # reposnitory-relative + # bazel doesn't generate this one; it probably builds file symlinks. + assert [ + "${PROJECT_BINARY_DIR}/includes", + "${PROJECT_SOURCE_DIR}/includes", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + includes=["/includes"], + known_include_files=["foo-srcdir/includes/a.h"], + ) + + +def test_construct_cc_includes_include_prefix(): + # include_prefix really doesn't work for bazel_to_cmake. + assert [ + "${PROJECT_SOURCE_DIR}", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + include_prefix="bar", + known_include_files=["foo-srcdir/bar/x.h"], + ) + assert [ + "${PROJECT_SOURCE_DIR}", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + include_prefix="_mismatch_", + known_include_files=["foo-srcdir/bar/y.h"], + ) + + +def test_construct_cc_includes_strip_include_prefix(): + # mismatched + assert [ + "${PROJECT_SOURCE_DIR}", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + strip_include_prefix="xyz", + known_include_files=["foo-srcdir/bar/a.h"], + ) + assert [ + "${PROJECT_BINARY_DIR}", + ] == construct_cc_includes( + repo=REPO, + strip_include_prefix="xyz", + current_package_id=PackageId("foo", "bar"), + known_include_files=["foo-bindir/bar/b.h"], + ) + # Respoitory relative + assert [ + "${PROJECT_SOURCE_DIR}/bar", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + strip_include_prefix="/bar", + known_include_files=["foo-srcdir/bar/c.h"], + ) + assert [ + "${PROJECT_BINARY_DIR}/bar", + "${PROJECT_SOURCE_DIR}/bar", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + strip_include_prefix="/bar", + known_include_files=["foo-srcdir/bar/d.h", "foo-bindir/bar/dd.h"], + ) + assert [ + "${PROJECT_SOURCE_DIR}/bar", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + strip_include_prefix="/bar", + known_include_files=["foo-srcdir/bar/e.inc", "foo-srcdir/bar/e.h"], + ) + # Package includes + assert [ + "${PROJECT_SOURCE_DIR}", + ] == construct_cc_includes( + repo=REPO, + strip_include_prefix="bar", + current_package_id=PackageId("foo", "bar"), + known_include_files=["foo-srcdir/bar/f.h"], + ) + assert [ + "${PROJECT_SOURCE_DIR}/bar/includes", + ] == construct_cc_includes( + REPO, + PackageId("foo", "bar"), + strip_include_prefix="includes", + known_include_files=["foo-srcdir/bar/includes/g.h"], + ) diff --git a/tools/cmake/bazel_to_cmake/golden_test.py b/tools/cmake/bazel_to_cmake/golden_test.py index d63a26d76..521298350 100644 --- a/tools/cmake/bazel_to_cmake/golden_test.py +++ b/tools/cmake/bazel_to_cmake/golden_test.py @@ -64,6 +64,7 @@ def parameters() -> List[Tuple[str, Dict[str, Any]]]: """Returns config tuples by reading config.json from the 'testdata' subdir.""" if UPDATE_GOLDENS: + print('UPDATE_GOLDENS') testdata = pathlib.Path(__file__).resolve().with_name('testdata') else: testdata = pathlib.Path(__file__).with_name('testdata').resolve() diff --git a/tools/cmake/bazel_to_cmake/native_rules_genrule.py b/tools/cmake/bazel_to_cmake/native_rules_genrule.py index 2eceb3d3b..bd0133dce 100644 --- a/tools/cmake/bazel_to_cmake/native_rules_genrule.py +++ b/tools/cmake/bazel_to_cmake/native_rules_genrule.py @@ -321,10 +321,12 @@ def _emit_genrule( sep = "\n " quoted_outputs = quote_list(generated_files, sep) deps_str = quote_list(sorted(set(add_dependencies)), sep) + if deps_str: + deps_str = f"DEPENDS{sep}{deps_str}" out.write(f"""add_custom_command( OUTPUT{sep}{quoted_outputs} - DEPENDS{sep}{deps_str} + {deps_str} COMMAND {cmd_text} {optional_message_text}VERBATIM WORKING_DIRECTORY "${{CMAKE_CURRENT_SOURCE_DIR}}" diff --git a/tools/cmake/bazel_to_cmake/native_rules_proto.py b/tools/cmake/bazel_to_cmake/native_rules_proto.py index 6b7c00130..a3c22f366 100644 --- a/tools/cmake/bazel_to_cmake/native_rules_proto.py +++ b/tools/cmake/bazel_to_cmake/native_rules_proto.py @@ -165,12 +165,14 @@ def maybe_set_import_var(d: TargetId): # strip_import_prefix and import_prefix behave the same as for cc_library includes = construct_cc_includes( - _context, + state.workspace.all_repositories.get( + _context.caller_package_id.repository_id + ), + _context.caller_package_id, includes=None, include_prefix=import_prefix, strip_include_prefix=strip_import_prefix, - srcs_file_paths=None, - hdrs_file_paths=proto_src_files, + known_include_files=proto_src_files, ) # Sanity check; if there are sources, then there should be includes. diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/BUILD.bazel b/tools/cmake/bazel_to_cmake/testdata/cc_includes/BUILD.bazel new file mode 100644 index 000000000..ecbe76e16 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/BUILD.bazel @@ -0,0 +1,27 @@ +package(default_visibility = ["//visibility:public"]) + +COMMENT = """ +rm out.* +for x in a a_dot a_parent a_child a_strip_parent a_strip_child a_prefix_child; do + ../../../../../bazelisk.py aquery parent:$x >> out.$x + echo "--------------------------------------" >> out.$x + ../../../../../bazelisk.py aquery :$x >> out.$x +done +""" + +[ + cc_binary( + name = x, + srcs = ["b.cc"], + deps = ["//parent:" + x], + ) + for x in [ + "a", + "a_dot", + "a_child", + "a_parent", + "a_strip_child", + "a_strip_parent", + "a_prefix_child", + ] +] diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/WORKSPACE.bazel b/tools/cmake/bazel_to_cmake/testdata/cc_includes/WORKSPACE.bazel new file mode 100644 index 000000000..1c38dc215 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/WORKSPACE.bazel @@ -0,0 +1 @@ +workspace(name = "cc_includes_test_repo") diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/b.cc b/tools/cmake/bazel_to_cmake/testdata/cc_includes/b.cc new file mode 100644 index 000000000..f933fa415 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/b.cc @@ -0,0 +1,3 @@ +// b.cc + +int b_foo() { return 3; } diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/config.json b/tools/cmake/bazel_to_cmake/testdata/cc_includes/config.json new file mode 100644 index 000000000..d3dbb6d11 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/config.json @@ -0,0 +1,19 @@ +{ + "build_files": ["BUILD.bazel", "parent/BUILD.bazel"], + "targets": [ + "//parent:a", + "//parent:a_dot", + "//parent:a_child", + "//parent:a_parent", + "//parent:a_strip_child", + "//parent:a_strip_parent", + "//parent:a_prefix_child", + "//:a", + "//:a_dot", + "//:a_child", + "//:a_parent", + "//:a_strip_child", + "//:a_strip_parent", + "//:a_prefix_child" + ] +} diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/golden/build_rules.cmake b/tools/cmake/bazel_to_cmake/testdata/cc_includes/golden/build_rules.cmake new file mode 100644 index 000000000..66ba14763 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/golden/build_rules.cmake @@ -0,0 +1,220 @@ + +# genrule(@cc_includes_test_repo//parent:c_inc) +file(MAKE_DIRECTORY "_cmake_binary_dir_/parent/child") +add_custom_command( + OUTPUT + "_cmake_binary_dir_/parent/child/c.inc" + + COMMAND echo "// c.inc" > "_cmake_binary_dir_/parent/child/c.inc" + VERBATIM + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" +) +add_custom_target(genrule__CMakeProject_parent_c_inc DEPENDS + "_cmake_binary_dir_/parent/child/c.inc") +add_library(CMakeProject_parent_c_inc INTERFACE) +target_sources(CMakeProject_parent_c_inc INTERFACE + "_cmake_binary_dir_/parent/child/c.inc") +set_property(TARGET CMakeProject_parent_c_inc PROPERTY INTERFACE_INCLUDE_DIRECTORIES + "${PROJECT_BINARY_DIR}") +add_dependencies(CMakeProject_parent_c_inc genrule__CMakeProject_parent_c_inc) + +# cc_library(@cc_includes_test_repo//parent:a) +add_library(CMakeProject_parent_a) +set_property(TARGET CMakeProject_parent_a PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a PUBLIC + "$" + "$") +target_compile_features(CMakeProject_parent_a PUBLIC cxx_std_17) +add_dependencies(CMakeProject_parent_a "CMakeProject_parent_c_inc") +target_sources(CMakeProject_parent_a PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a ALIAS CMakeProject_parent_a) + +# cc_library(@cc_includes_test_repo//parent:a_dot) +add_library(CMakeProject_parent_a_dot) +set_property(TARGET CMakeProject_parent_a_dot PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_dot PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_dot PUBLIC + "$" + "$") +target_include_directories(CMakeProject_parent_a_dot PRIVATE + "$" + "$") +target_compile_features(CMakeProject_parent_a_dot PUBLIC cxx_std_17) +add_dependencies(CMakeProject_parent_a_dot "CMakeProject_parent_c_inc") +target_sources(CMakeProject_parent_a_dot PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_dot ALIAS CMakeProject_parent_a_dot) + +# cc_library(@cc_includes_test_repo//parent:a_child) +add_library(CMakeProject_parent_a_child) +set_property(TARGET CMakeProject_parent_a_child PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_child PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_child PUBLIC + "$" + "$") +target_include_directories(CMakeProject_parent_a_child PRIVATE + "$") +target_compile_features(CMakeProject_parent_a_child PUBLIC cxx_std_17) +target_sources(CMakeProject_parent_a_child PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_child ALIAS CMakeProject_parent_a_child) + +# cc_library(@cc_includes_test_repo//parent:a_parent) +add_library(CMakeProject_parent_a_parent) +set_property(TARGET CMakeProject_parent_a_parent PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_parent PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_parent PUBLIC + "$" + "$") +target_include_directories(CMakeProject_parent_a_parent PRIVATE + "$" + "$") +target_compile_features(CMakeProject_parent_a_parent PUBLIC cxx_std_17) +add_dependencies(CMakeProject_parent_a_parent "CMakeProject_parent_c_inc") +target_sources(CMakeProject_parent_a_parent PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_parent ALIAS CMakeProject_parent_a_parent) + +# cc_library(@cc_includes_test_repo//parent:a_strip_child) +add_library(CMakeProject_parent_a_strip_child) +set_property(TARGET CMakeProject_parent_a_strip_child PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_strip_child PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_strip_child PUBLIC + "$") +target_include_directories(CMakeProject_parent_a_strip_child PRIVATE + "$") +target_compile_features(CMakeProject_parent_a_strip_child PUBLIC cxx_std_17) +target_sources(CMakeProject_parent_a_strip_child PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_strip_child ALIAS CMakeProject_parent_a_strip_child) + +# cc_library(@cc_includes_test_repo//parent:a_strip_parent) +add_library(CMakeProject_parent_a_strip_parent) +set_property(TARGET CMakeProject_parent_a_strip_parent PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_strip_parent PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_strip_parent PUBLIC + "$" + "$") +target_compile_features(CMakeProject_parent_a_strip_parent PUBLIC cxx_std_17) +add_dependencies(CMakeProject_parent_a_strip_parent "CMakeProject_parent_c_inc") +target_sources(CMakeProject_parent_a_strip_parent PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_strip_parent ALIAS CMakeProject_parent_a_strip_parent) + +# cc_library(@cc_includes_test_repo//parent:a_prefix_child) +add_library(CMakeProject_parent_a_prefix_child) +set_property(TARGET CMakeProject_parent_a_prefix_child PROPERTY LINKER_LANGUAGE "CXX") +target_link_libraries(CMakeProject_parent_a_prefix_child PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_parent_a_prefix_child PUBLIC + "$") +target_compile_features(CMakeProject_parent_a_prefix_child PUBLIC cxx_std_17) +target_sources(CMakeProject_parent_a_prefix_child PRIVATE + "${TEST_DIRECTORY}/parent/child/a.cc") +add_library(CMakeProject::parent_a_prefix_child ALIAS CMakeProject_parent_a_prefix_child) + +# cc_binary(@cc_includes_test_repo//:a) +add_executable(CMakeProject_a "") +add_executable(CMakeProject::a ALIAS CMakeProject_a) +target_link_libraries(CMakeProject_a PUBLIC + "CMakeProject::parent_a" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a PRIVATE + "$") +target_compile_features(CMakeProject_a PUBLIC cxx_std_17) +target_sources(CMakeProject_a PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_dot) +add_executable(CMakeProject_a_dot "") +add_executable(CMakeProject::a_dot ALIAS CMakeProject_a_dot) +target_link_libraries(CMakeProject_a_dot PUBLIC + "CMakeProject::parent_a_dot" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_dot PRIVATE + "$") +target_compile_features(CMakeProject_a_dot PUBLIC cxx_std_17) +target_sources(CMakeProject_a_dot PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_child) +add_executable(CMakeProject_a_child "") +add_executable(CMakeProject::a_child ALIAS CMakeProject_a_child) +target_link_libraries(CMakeProject_a_child PUBLIC + "CMakeProject::parent_a_child" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_child PRIVATE + "$") +target_compile_features(CMakeProject_a_child PUBLIC cxx_std_17) +target_sources(CMakeProject_a_child PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_parent) +add_executable(CMakeProject_a_parent "") +add_executable(CMakeProject::a_parent ALIAS CMakeProject_a_parent) +target_link_libraries(CMakeProject_a_parent PUBLIC + "CMakeProject::parent_a_parent" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_parent PRIVATE + "$") +target_compile_features(CMakeProject_a_parent PUBLIC cxx_std_17) +target_sources(CMakeProject_a_parent PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_strip_child) +add_executable(CMakeProject_a_strip_child "") +add_executable(CMakeProject::a_strip_child ALIAS CMakeProject_a_strip_child) +target_link_libraries(CMakeProject_a_strip_child PUBLIC + "CMakeProject::parent_a_strip_child" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_strip_child PRIVATE + "$") +target_compile_features(CMakeProject_a_strip_child PUBLIC cxx_std_17) +target_sources(CMakeProject_a_strip_child PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_strip_parent) +add_executable(CMakeProject_a_strip_parent "") +add_executable(CMakeProject::a_strip_parent ALIAS CMakeProject_a_strip_parent) +target_link_libraries(CMakeProject_a_strip_parent PUBLIC + "CMakeProject::parent_a_strip_parent" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_strip_parent PRIVATE + "$") +target_compile_features(CMakeProject_a_strip_parent PUBLIC cxx_std_17) +target_sources(CMakeProject_a_strip_parent PRIVATE + "${TEST_DIRECTORY}/b.cc") + +# cc_binary(@cc_includes_test_repo//:a_prefix_child) +add_executable(CMakeProject_a_prefix_child "") +add_executable(CMakeProject::a_prefix_child ALIAS CMakeProject_a_prefix_child) +target_link_libraries(CMakeProject_a_prefix_child PUBLIC + "CMakeProject::parent_a_prefix_child" + "Threads::Threads" + "m") +target_include_directories(CMakeProject_a_prefix_child PRIVATE + "$") +target_compile_features(CMakeProject_a_prefix_child PUBLIC cxx_std_17) +target_sources(CMakeProject_a_prefix_child PRIVATE + "${TEST_DIRECTORY}/b.cc") diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/BUILD.bazel b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/BUILD.bazel new file mode 100644 index 000000000..16321a2b2 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/BUILD.bazel @@ -0,0 +1,80 @@ +package(default_visibility = ["//visibility:public"]) + +genrule( + name = "c_inc", + outs = ["child/c.inc"], + cmd = "echo \"// c.inc\" > $@", +) + +cc_library( + name = "a", + srcs = [ + "child/a.cc", + "child/a.inc", + ], + hdrs = ["child/a.h"], + textual_hdrs = ["child/c.inc"], +) + +cc_library( + name = "a_dot", + srcs = [ + "child/a.cc", + "child/a.inc", + ], + hdrs = ["child/a.h"], + includes = ["."], + textual_hdrs = ["child/c.inc"], +) + +cc_library( + name = "a_child", + srcs = [ + "child/a.cc", + "child/a.inc", + ], + hdrs = ["child/a.h"], + includes = ["child"], +) + +cc_library( + name = "a_parent", + srcs = [ + "child/a.cc", + "child/a.inc", + "child/c.inc", + ], + hdrs = ["child/a.h"], + includes = ["/parent"], +) + +cc_library( + name = "a_strip_parent", + srcs = [ + "child/a.cc", + "child/a.inc", + "child/c.inc", + ], + hdrs = ["child/a.h"], + strip_include_prefix = "parent", +) + +cc_library( + name = "a_strip_child", + srcs = [ + "child/a.cc", + "child/a.inc", + ], + hdrs = ["child/a.h"], + strip_include_prefix = "child", +) + +cc_library( + name = "a_prefix_child", + srcs = [ + "child/a.cc", + "child/a.inc", + ], + hdrs = ["child/a.h"], + include_prefix = "child", +) diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.cc b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.cc new file mode 100644 index 000000000..c42f044b1 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.cc @@ -0,0 +1,3 @@ +// a.cc + +int a_foo() { return 1; } diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.h b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.h new file mode 100644 index 000000000..98f27a6f6 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.h @@ -0,0 +1 @@ +// a.h \ No newline at end of file diff --git a/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.inc b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.inc new file mode 100644 index 000000000..962f77e24 --- /dev/null +++ b/tools/cmake/bazel_to_cmake/testdata/cc_includes/parent/child/a.inc @@ -0,0 +1 @@ +// a.inc \ No newline at end of file diff --git a/tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake b/tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake index a786f3496..1b2d002ba 100644 --- a/tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake +++ b/tools/cmake/bazel_to_cmake/testdata/grpc_generate_cc/golden/build_rules.cmake @@ -73,6 +73,8 @@ target_link_libraries(CMakeProject_cc_grpc PUBLIC "m") target_include_directories(CMakeProject_cc_grpc PUBLIC "$") +target_include_directories(CMakeProject_cc_grpc PRIVATE + "$") target_compile_features(CMakeProject_cc_grpc PUBLIC cxx_std_17) add_dependencies(CMakeProject_cc_grpc "CMakeProject_cc__grpc_codegen") target_sources(CMakeProject_cc_grpc PRIVATE @@ -86,6 +88,8 @@ target_link_libraries(CMakeProject_a PUBLIC "CMakeProject::cc_grpc" "Threads::Threads" "m") +target_include_directories(CMakeProject_a PRIVATE + "$") target_compile_features(CMakeProject_a PUBLIC cxx_std_17) target_sources(CMakeProject_a PRIVATE "${TEST_DIRECTORY}/a.cc") diff --git a/tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/build_rules.cmake b/tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/build_rules.cmake index 6e2d0781b..ea31df301 100644 --- a/tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/build_rules.cmake +++ b/tools/cmake/bazel_to_cmake/testdata/local_mirror/golden/build_rules.cmake @@ -10,6 +10,8 @@ target_link_libraries(CMakeProject_a PUBLIC "lpm::b" "lpm::validate_cc" "m") +target_include_directories(CMakeProject_a PRIVATE + "$") target_compile_features(CMakeProject_a PUBLIC cxx_std_17) target_sources(CMakeProject_a PRIVATE "${TEST_DIRECTORY}/a.cc") diff --git a/tools/cmake/bazel_to_cmake/testdata/native_rules/BUILD.bazel b/tools/cmake/bazel_to_cmake/testdata/native_rules/BUILD.bazel index 4abf1b483..0b82ccfa0 100644 --- a/tools/cmake/bazel_to_cmake/testdata/native_rules/BUILD.bazel +++ b/tools/cmake/bazel_to_cmake/testdata/native_rules/BUILD.bazel @@ -99,3 +99,12 @@ filegroup( name = "subdir_z", srcs = ["subdir/z.proto"], ) + +cc_library( + name = "defines", + srcs = ["a.cc"], + hdrs = ["a.h"], + defines = [ + "VERSION=\\\"" + (module_version() if module_version() != None else "") + "\\\"", + ], +) diff --git a/tools/cmake/bazel_to_cmake/testdata/native_rules/config.json b/tools/cmake/bazel_to_cmake/testdata/native_rules/config.json index 1756bb280..00f3810e6 100644 --- a/tools/cmake/bazel_to_cmake/testdata/native_rules/config.json +++ b/tools/cmake/bazel_to_cmake/testdata/native_rules/config.json @@ -8,13 +8,7 @@ "//:c_proto", "//:c_proto_2", "//:c_proto_2__cpp_library", - "//:c_proto_2__minitable_library", - "//:c_proto_2__upb_library", - "//:c_proto_2__upbdefs_library", "//:c_proto__cpp_library", - "//:c_proto__minitable_library", - "//:c_proto__upb_library", - "//:c_proto__upbdefs_library", "//:c_proto_alias", "//:c_proto_cc", "//:c_proto_cc_alias", @@ -27,8 +21,6 @@ "//subdir:y_includes", "//subdir:y_strip_include_prefix", "//subdir:z_proto__cpp_library", - "//subdir:z_proto__minitable_library", - "//subdir:z_proto__upb_library", - "//subdir:z_proto__upbdefs_library" + "//:defines" ] } diff --git a/tools/cmake/bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake b/tools/cmake/bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake index 2670cf468..78ef37632 100644 --- a/tools/cmake/bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake +++ b/tools/cmake/bazel_to_cmake/testdata/native_rules/golden/build_rules.cmake @@ -7,6 +7,8 @@ target_compile_definitions(CMakeProject_bb PUBLIC "BUILD_LINUX") target_link_libraries(CMakeProject_bb PUBLIC "Threads::Threads" "m") +target_include_directories(CMakeProject_bb PRIVATE + "$") target_compile_features(CMakeProject_bb PUBLIC cxx_std_17) target_sources(CMakeProject_bb PRIVATE "${TEST_DIRECTORY}/a.cc") @@ -47,6 +49,8 @@ target_link_libraries(CMakeProject_a PUBLIC "m") target_include_directories(CMakeProject_a PUBLIC "$") +target_include_directories(CMakeProject_a PRIVATE + "$") target_compile_features(CMakeProject_a PUBLIC cxx_std_17) add_dependencies(CMakeProject_a "CMakeProject_h_file") target_sources(CMakeProject_a PRIVATE @@ -102,6 +106,8 @@ target_link_libraries(CMakeProject_a_test PUBLIC "CMakeProject::c_proto_cc" "Threads::Threads" "m") +target_include_directories(CMakeProject_a_test PRIVATE + "$") target_compile_features(CMakeProject_a_test PUBLIC cxx_std_17) target_sources(CMakeProject_a_test PRIVATE "${TEST_DIRECTORY}/a.cc") @@ -133,129 +139,6 @@ btc_protobuf( DEPENDENCIES "protobuf::protoc" ) -# @native_rules_test_repo//:c_proto_2__minitable_library -add_library(CMakeProject_c_proto_2__minitable_library) -set_property(TARGET CMakeProject_c_proto_2__minitable_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto_2__minitable_library PUBLIC - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_c_proto_2__minitable_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto_2__minitable_library ALIAS CMakeProject_c_proto_2__minitable_library) - -btc_protobuf( - TARGET CMakeProject_c_proto_2__minitable_library - PROTO_TARGET CMakeProject_c_proto_2 - LANGUAGE upb_minitable - GENERATE_EXTENSIONS ".upb_minitable.h" ".upb_minitable.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/c5077a10 - PLUGIN protoc-gen-upb_minitable=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb_minitable_stage1" -) - -# @native_rules_test_repo//:c_proto_2__upb_library -add_library(CMakeProject_c_proto_2__upb_library) -set_property(TARGET CMakeProject_c_proto_2__upb_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto_2__upb_library PUBLIC - "CMakeProject::c_proto_2__minitable_library" - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_c_proto_2__upb_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto_2__upb_library ALIAS CMakeProject_c_proto_2__upb_library) - -btc_protobuf( - TARGET CMakeProject_c_proto_2__upb_library - PROTO_TARGET CMakeProject_c_proto_2 - LANGUAGE upb - GENERATE_EXTENSIONS ".upb.h" ".upb.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/c5077a10 - PLUGIN protoc-gen-upb=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb" -) - -# @native_rules_test_repo//:c_proto_2__upbdefs_library -add_library(CMakeProject_c_proto_2__upbdefs_library) -set_property(TARGET CMakeProject_c_proto_2__upbdefs_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto_2__upbdefs_library PUBLIC - "CMakeProject::c_proto_2__minitable_library" - "Protobuf::upb_generated_reflection_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me" - "Protobuf::upb_port") -target_compile_features(CMakeProject_c_proto_2__upbdefs_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto_2__upbdefs_library ALIAS CMakeProject_c_proto_2__upbdefs_library) - -btc_protobuf( - TARGET CMakeProject_c_proto_2__upbdefs_library - PROTO_TARGET CMakeProject_c_proto_2 - LANGUAGE upbdefs - GENERATE_EXTENSIONS ".upbdefs.h" ".upbdefs.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/c5077a10 - PLUGIN protoc-gen-upbdefs=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upbdefs" -) - -# @native_rules_test_repo//:c_proto__minitable_library -add_library(CMakeProject_c_proto__minitable_library) -set_property(TARGET CMakeProject_c_proto__minitable_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto__minitable_library PUBLIC - "Protobuf_timestamp_proto__minitable_library" - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_c_proto__minitable_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto__minitable_library ALIAS CMakeProject_c_proto__minitable_library) - -btc_protobuf( - TARGET CMakeProject_c_proto__minitable_library - PROTO_TARGET CMakeProject_c_proto - LANGUAGE upb_minitable - GENERATE_EXTENSIONS ".upb_minitable.h" ".upb_minitable.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/de6f8ca9 - PLUGIN protoc-gen-upb_minitable=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb_minitable_stage1" -) - -# @native_rules_test_repo//:c_proto__upb_library -add_library(CMakeProject_c_proto__upb_library) -set_property(TARGET CMakeProject_c_proto__upb_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto__upb_library PUBLIC - "CMakeProject::c_proto__minitable_library" - "Protobuf_timestamp_proto__upb_library" - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_c_proto__upb_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto__upb_library ALIAS CMakeProject_c_proto__upb_library) - -btc_protobuf( - TARGET CMakeProject_c_proto__upb_library - PROTO_TARGET CMakeProject_c_proto - LANGUAGE upb - GENERATE_EXTENSIONS ".upb.h" ".upb.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/de6f8ca9 - PLUGIN protoc-gen-upb=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb" -) - -# @native_rules_test_repo//:c_proto__upbdefs_library -add_library(CMakeProject_c_proto__upbdefs_library) -set_property(TARGET CMakeProject_c_proto__upbdefs_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_c_proto__upbdefs_library PUBLIC - "CMakeProject::c_proto__minitable_library" - "Protobuf::upb_generated_reflection_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me" - "Protobuf::upb_port" - "Protobuf_timestamp_proto__upbdefs_library") -target_compile_features(CMakeProject_c_proto__upbdefs_library PUBLIC cxx_std_17) -add_library(CMakeProject::c_proto__upbdefs_library ALIAS CMakeProject_c_proto__upbdefs_library) - -btc_protobuf( - TARGET CMakeProject_c_proto__upbdefs_library - PROTO_TARGET CMakeProject_c_proto - LANGUAGE upbdefs - GENERATE_EXTENSIONS ".upbdefs.h" ".upbdefs.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/de6f8ca9 - PLUGIN protoc-gen-upbdefs=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upbdefs" -) - # alias(@native_rules_test_repo//:c_proto_alias) add_library(CMakeProject_c_proto_alias ALIAS CMakeProject_c_proto) add_library(CMakeProject::c_proto_alias ALIAS CMakeProject_c_proto) @@ -271,6 +154,8 @@ target_compile_definitions(CMakeProject_subdir_x PUBLIC "BUILD_LINUX") target_link_libraries(CMakeProject_subdir_x PUBLIC "Threads::Threads" "m") +target_include_directories(CMakeProject_subdir_x PRIVATE + "$") target_compile_features(CMakeProject_subdir_x PUBLIC cxx_std_17) target_sources(CMakeProject_subdir_x PRIVATE "${TEST_DIRECTORY}/subdir/x.cc") @@ -325,6 +210,8 @@ target_link_libraries(CMakeProject_subdir_y PUBLIC "m") target_include_directories(CMakeProject_subdir_y PUBLIC "$") +target_include_directories(CMakeProject_subdir_y PRIVATE + "$") target_compile_features(CMakeProject_subdir_y PUBLIC cxx_std_17) add_dependencies(CMakeProject_subdir_y "CMakeProject_subdir_make_y" "CMakeProject_subdir_make_ycc") target_sources(CMakeProject_subdir_y PRIVATE @@ -344,6 +231,8 @@ target_link_libraries(CMakeProject_subdir_y_include_prefix PUBLIC "m") target_include_directories(CMakeProject_subdir_y_include_prefix PUBLIC "$") +target_include_directories(CMakeProject_subdir_y_include_prefix PRIVATE + "$") target_compile_features(CMakeProject_subdir_y_include_prefix PUBLIC cxx_std_17) add_dependencies(CMakeProject_subdir_y_include_prefix "CMakeProject_subdir_make_y" "CMakeProject_subdir_make_ycc") target_sources(CMakeProject_subdir_y_include_prefix PRIVATE @@ -358,8 +247,11 @@ target_link_libraries(CMakeProject_subdir_y_includes PUBLIC "Threads::Threads" "m") target_include_directories(CMakeProject_subdir_y_includes PUBLIC + "$" + "$") +target_include_directories(CMakeProject_subdir_y_includes PRIVATE "$" - "$") + "$") target_compile_features(CMakeProject_subdir_y_includes PUBLIC cxx_std_17) add_dependencies(CMakeProject_subdir_y_includes "CMakeProject_subdir_make_y" "CMakeProject_subdir_make_ycc") target_sources(CMakeProject_subdir_y_includes PRIVATE @@ -375,6 +267,9 @@ target_link_libraries(CMakeProject_subdir_y_strip_include_prefix PUBLIC "m") target_include_directories(CMakeProject_subdir_y_strip_include_prefix PUBLIC "$") +target_include_directories(CMakeProject_subdir_y_strip_include_prefix PRIVATE + "$" + "$") target_compile_features(CMakeProject_subdir_y_strip_include_prefix PUBLIC cxx_std_17) add_dependencies(CMakeProject_subdir_y_strip_include_prefix "CMakeProject_subdir_make_y" "CMakeProject_subdir_make_ycc") target_sources(CMakeProject_subdir_y_strip_include_prefix PRIVATE @@ -407,62 +302,19 @@ btc_protobuf( DEPENDENCIES "protobuf::protoc" ) -# @native_rules_test_repo//subdir:z_proto__minitable_library -add_library(CMakeProject_subdir_z_proto__minitable_library) -set_property(TARGET CMakeProject_subdir_z_proto__minitable_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_subdir_z_proto__minitable_library PUBLIC - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_subdir_z_proto__minitable_library PUBLIC cxx_std_17) -add_library(CMakeProject::subdir_z_proto__minitable_library ALIAS CMakeProject_subdir_z_proto__minitable_library) - -btc_protobuf( - TARGET CMakeProject_subdir_z_proto__minitable_library - PROTO_TARGET CMakeProject_subdir_z_proto - LANGUAGE upb_minitable - GENERATE_EXTENSIONS ".upb_minitable.h" ".upb_minitable.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/fb9b71f5 - PLUGIN protoc-gen-upb_minitable=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb_minitable_stage1" -) - -# @native_rules_test_repo//subdir:z_proto__upb_library -add_library(CMakeProject_subdir_z_proto__upb_library) -set_property(TARGET CMakeProject_subdir_z_proto__upb_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_subdir_z_proto__upb_library PUBLIC - "CMakeProject::subdir_z_proto__minitable_library" - "protobuf::upb_generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me") -target_compile_features(CMakeProject_subdir_z_proto__upb_library PUBLIC cxx_std_17) -add_library(CMakeProject::subdir_z_proto__upb_library ALIAS CMakeProject_subdir_z_proto__upb_library) - -btc_protobuf( - TARGET CMakeProject_subdir_z_proto__upb_library - PROTO_TARGET CMakeProject_subdir_z_proto - LANGUAGE upb - GENERATE_EXTENSIONS ".upb.h" ".upb.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/fb9b71f5 - PLUGIN protoc-gen-upb=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upb" -) - -# @native_rules_test_repo//subdir:z_proto__upbdefs_library -add_library(CMakeProject_subdir_z_proto__upbdefs_library) -set_property(TARGET CMakeProject_subdir_z_proto__upbdefs_library PROPERTY LINKER_LANGUAGE "CXX") -target_link_libraries(CMakeProject_subdir_z_proto__upbdefs_library PUBLIC - "CMakeProject::subdir_z_proto__minitable_library" - "Protobuf::upb_generated_reflection_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me" - "Protobuf::upb_port") -target_compile_features(CMakeProject_subdir_z_proto__upbdefs_library PUBLIC cxx_std_17) -add_library(CMakeProject::subdir_z_proto__upbdefs_library ALIAS CMakeProject_subdir_z_proto__upbdefs_library) - -btc_protobuf( - TARGET CMakeProject_subdir_z_proto__upbdefs_library - PROTO_TARGET CMakeProject_subdir_z_proto - LANGUAGE upbdefs - GENERATE_EXTENSIONS ".upbdefs.h" ".upbdefs.c" - PROTOC_OPTIONS --experimental_allow_proto3_optional - PROTOC_OUT_DIR ${PROJECT_BINARY_DIR}/fb9b71f5 - PLUGIN protoc-gen-upbdefs=$ - DEPENDENCIES "protobuf::protoc" "protobuf::protoc_gen_upbdefs" -) +# cc_library(@native_rules_test_repo//:defines) +add_library(CMakeProject_defines) +set_property(TARGET CMakeProject_defines PROPERTY LINKER_LANGUAGE "CXX") +target_compile_definitions(CMakeProject_defines PUBLIC "VERSION=\"\"" "BUILD_LINUX") +target_link_libraries(CMakeProject_defines PUBLIC + "Threads::Threads" + "m") +target_include_directories(CMakeProject_defines PUBLIC + "$") +target_include_directories(CMakeProject_defines PRIVATE + "$") +target_compile_features(CMakeProject_defines PUBLIC cxx_std_17) +add_dependencies(CMakeProject_defines "CMakeProject_h_file") +target_sources(CMakeProject_defines PRIVATE + "${TEST_DIRECTORY}/a.cc") +add_library(CMakeProject::defines ALIAS CMakeProject_defines) diff --git a/tools/cmake/bazel_to_cmake/testdata/rules_proto/config.json b/tools/cmake/bazel_to_cmake/testdata/rules_proto/config.json index 01d619b1e..cbd58ab09 100644 --- a/tools/cmake/bazel_to_cmake/testdata/rules_proto/config.json +++ b/tools/cmake/bazel_to_cmake/testdata/rules_proto/config.json @@ -1,5 +1,8 @@ { - "modules": [".bzl_library.rules_proto"], + "modules": [ + ".bzl_library.rules_proto", + ".bzl_library.upb_proto_library" + ], "targets": [ "//:a_proto", "//:a_proto__cpp_library", diff --git a/tools/cmake/bazel_to_cmake/testdata/third_party_http_archive/golden/build_rules.cmake b/tools/cmake/bazel_to_cmake/testdata/third_party_http_archive/golden/build_rules.cmake index 1b3bfa243..e69923ac8 100644 --- a/tools/cmake/bazel_to_cmake/testdata/third_party_http_archive/golden/build_rules.cmake +++ b/tools/cmake/bazel_to_cmake/testdata/third_party_http_archive/golden/build_rules.cmake @@ -16,6 +16,8 @@ target_link_libraries(CMakeProject_a PUBLIC "Threads::Threads" "half::half" "m") +target_include_directories(CMakeProject_a PRIVATE + "$") target_compile_features(CMakeProject_a PUBLIC cxx_std_17) target_sources(CMakeProject_a PRIVATE "${TEST_DIRECTORY}/a.cc") diff --git a/tools/cmake/bazel_to_cmake/testdata/upb_proto_library/config.json b/tools/cmake/bazel_to_cmake/testdata/upb_proto_library/config.json index 65bf6741b..e9bd73e7c 100644 --- a/tools/cmake/bazel_to_cmake/testdata/upb_proto_library/config.json +++ b/tools/cmake/bazel_to_cmake/testdata/upb_proto_library/config.json @@ -1,5 +1,7 @@ { - "modules": [".bzl_library.upb_proto_library"], + "modules": [ + ".bzl_library.upb_proto_library" + ], "targets": [ "//:c_upb_proto", "//:c_upb_proto_reflection", diff --git a/tools/cmake/bazel_to_cmake/util.py b/tools/cmake/bazel_to_cmake/util.py index 438d33ff9..3b919c10e 100644 --- a/tools/cmake/bazel_to_cmake/util.py +++ b/tools/cmake/bazel_to_cmake/util.py @@ -37,10 +37,6 @@ def quote_string(x: str) -> str: return json.dumps(x) -def quote_list(y: Iterable[str], separator: str = " ") -> str: - return separator.join(quote_string(x) for x in y) - - def quote_path(p: PathLike) -> str: """Quotes a path, converting backslashes to forward slashes. @@ -51,10 +47,23 @@ def quote_path(p: PathLike) -> str: return json.dumps(pathlib.PurePath(p).as_posix()) +def quote_unescaped(x: str) -> str: + unescaped = bytes(x, "utf-8").decode("unicode_escape") + return json.dumps(unescaped) + + +def quote_list(y: Iterable[str], separator: str = " ") -> str: + return separator.join(quote_string(x) for x in y) + + def quote_path_list(y: PathSequence, separator: str = " ") -> str: return separator.join(quote_path(x) for x in y if x) +def quote_unescaped_list(y: Iterable[str], separator: str = " ") -> str: + return separator.join(quote_unescaped(x) for x in y) + + # Unfortunately, pathlib.PurePath.is_relative_to is a python3.9 invention. def is_relative_to( leaf: pathlib.PurePath, root: pathlib.PurePath, _use_attr: bool = True