Skip to content

Commit

Permalink
Include all necessary sources in prebuild test data.
Browse files Browse the repository at this point in the history
For cc compilation, we need access to a number of sources from the workspace, from both the target dependencies and from the cc toolchain. Add these to the `sources` directory created by the `test_data` rule.

To achieve this, add some explicit `test_mode` support to the existing cc rules in `build_dependencies.bzl` and export all the required sourcs to `test_projects.bzl` which can then add them to the `sources` directory.

Also add the cc related output groups to the `OutputInfo` returned by `PrebuiltProjectFakeDependencyBuilder` so that it behaves more like the real implementation.

PiperOrigin-RevId: 578519803
  • Loading branch information
Googler authored and copybara-github committed Nov 1, 2023
1 parent dc59ab9 commit fe0df78
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions aspect/build_dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ DependenciesInfo = provider(
"target_to_artifacts": "a map between a target and all its artifacts",
"aars": "a list of aars with resource files",
"gensrcs": "a list of sources generated by project targets",
"test_mode_own_files": "a structure describing artifacts required when the target is requested within the project scope",
"cc_info": "a structure containing info required to compile cc sources",
"cc_headers": "a list of generated headers required to compile cc sources",
"cc_toolchain_info": "struct containing cc toolchain info, with keys file (the output file) and id (unique ID for the toolchain info, referred to from elsewhere)",
"test_mode_own_files": "a structure describing Java artifacts required when the target is requested within the project scope",
"test_mode_cc_src_deps": "a list of sources (e.g. headers) required to compile cc sources in integratrion tests",
},
)

Expand All @@ -69,20 +70,22 @@ def create_dependencies_info(
target_to_artifacts = {},
aars = depset(),
gensrcs = depset(),
test_mode_own_files = None,
cc_info = None,
cc_headers = depset(),
cc_toolchain_info = None):
cc_toolchain_info = None,
test_mode_own_files = None,
test_mode_cc_src_deps = depset()):
"""A helper function to create a DependenciesInfo provider instance."""
return DependenciesInfo(
compile_time_jars = compile_time_jars,
target_to_artifacts = target_to_artifacts,
aars = aars,
gensrcs = gensrcs,
test_mode_own_files = test_mode_own_files,
cc_info = cc_info,
cc_headers = cc_headers,
cc_toolchain_info = cc_toolchain_info,
test_mode_own_files = test_mode_own_files,
test_mode_cc_src_deps = test_mode_cc_src_deps,
)

def _encode_target_info_proto(target_to_artifacts):
Expand Down Expand Up @@ -367,17 +370,26 @@ def _get_followed_cc_dependency_info(rule):
return cc_toolchain_target[DependenciesInfo]
return None

def _collect_own_and_dependency_cc_info(target, dependency_info):
def _collect_own_and_dependency_cc_info(target, dependency_info, test_mode):
compilation_context = target[CcInfo].compilation_context
cc_toolchain_info = None
tets_mode_cc_src_deps = depset()
if dependency_info:
cc_toolchain_info = dependency_info.cc_toolchain_info
if test_mode:
tets_mode_cc_src_deps = dependency_info.test_mode_cc_src_deps

gen_headers = depset()
compilation_info = None
if compilation_context:
gen_headers = depset([f for f in compilation_context.headers.to_list() if not f.is_source])

if test_mode:
tets_mode_cc_src_deps = depset(
[f for f in compilation_context.headers.to_list() if f.is_source],
transitive = [tets_mode_cc_src_deps],
)

compilation_info = struct(
transitive_defines = compilation_context.defines.to_list(),
transitive_include_directory = compilation_context.includes.to_list(),
Expand All @@ -390,6 +402,7 @@ def _collect_own_and_dependency_cc_info(target, dependency_info):
return struct(
compilation_info = compilation_info,
gen_headers = gen_headers,
tets_mode_cc_src_deps = tets_mode_cc_src_deps,
cc_toolchain_info = cc_toolchain_info,
)

Expand All @@ -411,7 +424,7 @@ def _collect_dependencies_core_impl(
test_mode,
)
if CcInfo in target:
dep_infos.append(_collect_cc_dependencies_core_impl(target, ctx))
dep_infos.append(_collect_cc_dependencies_core_impl(target, ctx, test_mode))
if cc_common.CcToolchainInfo in target:
dep_infos.append(_collect_cc_toolchain_info(target, ctx))
return dep_infos
Expand Down Expand Up @@ -462,15 +475,16 @@ def _collect_java_dependencies_core_impl(
),
]

def _collect_cc_dependencies_core_impl(target, ctx):
def _collect_cc_dependencies_core_impl(target, ctx, test_mode):
dependency_info = _get_followed_cc_dependency_info(ctx.rule)

cc_info = _collect_own_and_dependency_cc_info(target, dependency_info)
cc_info = _collect_own_and_dependency_cc_info(target, dependency_info, test_mode)

return create_dependencies_info(
cc_info = cc_info.compilation_info,
cc_headers = cc_info.gen_headers,
cc_toolchain_info = cc_info.cc_toolchain_info,
test_mode_cc_src_deps = cc_info.tets_mode_cc_src_deps,
)

def _collect_cc_toolchain_info(target, ctx):
Expand Down Expand Up @@ -540,6 +554,7 @@ def _collect_cc_toolchain_info(target, ctx):

return create_dependencies_info(
cc_toolchain_info = struct(file = cc_toolchain_file, id = toolchain_id),
test_mode_cc_src_deps = depset([f for f in toolchain_info.all_files.to_list() if f.is_source]),
)

def _get_ide_aar_file(target, ctx):
Expand Down

0 comments on commit fe0df78

Please sign in to comment.