Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.3.0] Add changes to cc_shared_library from head to 6.3 #18606

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.Structure;
import net.starlark.java.eval.Tuple;
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.docgen.annot.StarlarkConstructor;
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
import net.starlark.java.eval.Starlark;

/**
* A module that contains Starlark utilities for C++ support.
Expand Down Expand Up @@ -139,11 +151,120 @@ public abstract class CcModule

public abstract CppSemantics getSemantics(Language language);

/**
* This class exists only for a 6.3 cherrypick. For 7.0 this will be in Starlark.
*/
@StarlarkBuiltin(
name = "CcSharedLibraryHintInfo",
category = DocCategory.PROVIDER,
documented = false
)
@Immutable
private static final class CcSharedLibraryHintInfo extends NativeInfo implements StructApi {
private static final Provider PROVIDER = new Provider();

private final List<String> attributes;
private final List<Label> owners;

public CcSharedLibraryHintInfo(
List<String> attributes,
List<Label> owners) {
this.attributes = attributes;
this.owners = owners;
}

@StarlarkMethod(
name = "attributes",
structField = true,
allowReturnNones = true,
documented = false)
@Nullable
public Sequence<String> getAttributes() {
return attributes == null ? null : StarlarkList.immutableCopyOf(attributes);
}

@StarlarkMethod(
name = "owners",
structField = true,
allowReturnNones = true,
documented = false)
@Nullable
public Sequence<Label> getOwners() {
return owners == null ? null : StarlarkList.immutableCopyOf(owners);
}

@Override
public Provider getProvider() {
return PROVIDER;
}

public static class Provider extends BuiltinProvider<CcSharedLibraryHintInfo>
implements ProviderApi {
private Provider() {
super("CcSharedLibraryHintInfo", CcSharedLibraryHintInfo.class);
}
}
}

@StarlarkMethod(
name = "CcSharedLibraryHintInfo_6_X_constructor_do_not_use",
documented = false,
parameters = {
@Param(
name = "attributes",
documented = false,
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {
@ParamType(type = Sequence.class),
@ParamType(type = NoneType.class)
}),
@Param(
name = "owners",
documented = false,
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {
@ParamType(type = Sequence.class),
@ParamType(type = NoneType.class)
}),
}
)
public CcSharedLibraryHintInfo ccSharedLibraryHintInfoConstructor(
Object starlarkAttributesObject,
Object starlarkOwnersObject) throws EvalException {
List<String> attributes = null;
if (starlarkAttributesObject != Starlark.NONE) {
attributes = convertFromNoneable(
starlarkAttributesObject, /* defaultValue= */ (Sequence<String>) null).getImmutableList();
}
List<Label> owners = null;
if (starlarkOwnersObject != Starlark.NONE) {
owners = convertFromNoneable(
starlarkOwnersObject, /* defaultValue= */ (Sequence<Label>) null).getImmutableList();
}
return new CcSharedLibraryHintInfo(
attributes,
owners
);
}

@StarlarkMethod(
name = "CcSharedLibraryHintInfo_6_X_getter_do_not_use",
documented = false,
structField = true)
public Provider getCcSharedLibraryHintInfo() {
return CcSharedLibraryHintInfo.PROVIDER;
}

@Override
public Provider getCcToolchainProvider() {
return CcToolchainProvider.PROVIDER;
}


@Override
public FeatureConfigurationForStarlark configureFeatures(
Object ruleContextOrNone,
Expand Down
67 changes: 10 additions & 57 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""cc_binary Starlark implementation replacing native"""

load(":common/cc/semantics.bzl", "semantics")
load(":common/cc/experimental_cc_shared_library.bzl", "CcSharedLibraryInfo", "GraphNodeInfo", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "merge_cc_shared_library_infos", "throw_linked_but_not_exported_errors")
load(":common/cc/experimental_cc_shared_library.bzl", "GraphNodeInfo", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "throw_linked_but_not_exported_errors")
load(":common/cc/cc_helper.bzl", "cc_helper")

CcInfo = _builtins.toplevel.CcInfo
Expand Down Expand Up @@ -333,42 +333,8 @@ def _collect_transitive_dwo_artifacts(cc_compilation_outputs, cc_debug_context,
transitive_dwo_files = cc_debug_context.files
return depset(dwo_files, transitive = [transitive_dwo_files])

def _separate_static_and_dynamic_link_libraries(direct_children, can_be_linked_dynamically):
link_statically_labels = {}
link_dynamically_labels = {}
all_children = list(direct_children)
seen_labels = {}

# Some of the logic here is a duplicate from cc_shared_library.
# But some parts are different hence rewriting.
for i in range(2147483647):
if i == len(all_children):
break
node = all_children[i]
node_label = str(node.label)
if node_label in seen_labels:
continue
seen_labels[node_label] = True
if node_label in can_be_linked_dynamically:
link_dynamically_labels[node_label] = True
else:
link_statically_labels[node_label] = True
all_children.extend(node.children)
return (link_statically_labels, link_dynamically_labels)

def _get_preloaded_deps_from_dynamic_deps(ctx):
cc_infos = []
for dep in ctx.attr.dynamic_deps:
cc_shared_library_info = dep[CcSharedLibraryInfo]
preloaded_deps_field = cc_shared_library_info.preloaded_deps
if preloaded_deps_field != None:
cc_infos.append(preloaded_deps_field)

return cc_common.merge_cc_infos(direct_cc_infos = cc_infos, cc_infos = cc_infos).linking_context.linker_inputs.to_list()

def _filter_libraries_that_are_linked_dynamically(ctx, cc_linking_context, cpp_config):
merged_cc_shared_library_infos = merge_cc_shared_library_infos(ctx)
preloaded_deps = _get_preloaded_deps_from_dynamic_deps(ctx)
link_once_static_libs_map = build_link_once_static_libs_map(merged_cc_shared_library_infos)
exports_map = build_exports_map_from_only_dynamic_deps(merged_cc_shared_library_infos)
static_linker_inputs = []
Expand All @@ -383,7 +349,7 @@ def _filter_libraries_that_are_linked_dynamically(ctx, cc_linking_context, cpp_c
if owner in exports_map:
can_be_linked_dynamically[owner] = True

(link_statically_labels, link_dynamically_labels) = _separate_static_and_dynamic_link_libraries(graph_structure_aspect_nodes, can_be_linked_dynamically)
(link_statically_labels, link_dynamically_labels) = separate_static_and_dynamic_link_libraries(graph_structure_aspect_nodes, can_be_linked_dynamically)

linker_inputs_seen = {}
linked_statically_but_not_exported = {}
Expand All @@ -401,19 +367,7 @@ def _filter_libraries_that_are_linked_dynamically(ctx, cc_linking_context, cpp_c

throw_linked_but_not_exported_errors(linked_statically_but_not_exported)

rule_impl_debug_files = None
if cpp_config.experimental_cc_shared_library_debug():
debug_linker_inputs_file = ["Owner: " + str(ctx.label)]
static_linker_inputs_and_preloaded_deps = static_linker_inputs + preloaded_deps
for linker_input in static_linker_inputs_and_preloaded_deps:
debug_linker_inputs_file.append(str(linker_input.owner))
link_once_static_libs_debug_file = ctx.actions.declare_file(ctx.label.name + "_link_once_static_libs.txt")
ctx.actions.write(link_once_static_libs_debug_file, "\n".join(debug_linker_inputs_file), False)
transitive_debug_files_list = []
for dep in ctx.attr.dynamic_deps:
transitive_debug_files_list.append(dep[OutputGroupInfo].rule_impl_debug_files)
rule_impl_debug_files = depset([link_once_static_libs_debug_file], transitive = transitive_debug_files_list)
return (cc_common.create_linking_context(linker_inputs = depset(exports_map.values() + static_linker_inputs + preloaded_deps, order = "topological")), rule_impl_debug_files)
return cc_common.create_linking_context(linker_inputs = depset(exports_map.values() + static_linker_inputs, order = "topological"))

def _create_transitive_linking_actions(
ctx,
Expand Down Expand Up @@ -493,9 +447,8 @@ def _create_transitive_linking_actions(
cc_info = cc_common.merge_cc_infos(cc_infos = [cc_info_without_extra_link_time_libraries, extra_link_time_libraries_cc_info])
cc_linking_context = cc_info.linking_context

rule_impl_debug_files = None
if len(ctx.attr.dynamic_deps) > 0:
cc_linking_context, rule_impl_debug_files = _filter_libraries_that_are_linked_dynamically(ctx, cc_linking_context, cpp_config)
cc_linking_context = _filter_libraries_that_are_linked_dynamically(ctx, cc_linking_context, cpp_config)
link_deps_statically = True
if linking_mode == _LINKING_DYNAMIC:
link_deps_statically = False
Expand Down Expand Up @@ -526,7 +479,7 @@ def _create_transitive_linking_actions(
win_def_file = win_def_file,
)
cc_launcher_info = cc_internal.create_cc_launcher_info(cc_info = cc_info_without_extra_link_time_libraries, compilation_outputs = cc_compilation_outputs_with_only_objects)
return (cc_linking_outputs, cc_launcher_info, rule_impl_debug_files, cc_linking_context)
return (cc_linking_outputs, cc_launcher_info, cc_linking_context)

def _use_pic(ctx, cc_toolchain, cpp_config, feature_configuration):
if _is_link_shared(ctx):
Expand Down Expand Up @@ -671,6 +624,8 @@ def cc_binary_impl(ctx, additional_linkopts):
# Allows the dynamic library generated for code of test targets to be linked separately.
link_compile_output_separately = ctx.attr._is_test and linking_mode == _LINKING_DYNAMIC and cpp_config.dynamic_mode() == "DEFAULT" and ("dynamic_link_test_srcs" in ctx.features)

is_windows_enabled = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "targets_windows")

# When linking the object files directly into the resulting binary, we do not need
# library-level link outputs; thus, we do not let CcCompilationHelper produce link outputs
# (either shared object files or archives) for a non-library link type [*], and add
Expand All @@ -695,14 +650,15 @@ def cc_binary_impl(ctx, additional_linkopts):
linking_contexts = cc_helper.get_linking_contexts_from_deps(all_deps),
stamp = cc_helper.is_stamping_enabled(ctx),
alwayslink = True,
disallow_dynamic_library = is_windows_enabled,
)

is_static_mode = linking_mode != _LINKING_DYNAMIC
deps_cc_linking_context = _collect_linking_context(ctx, cpp_config)
generated_def_file = None
win_def_file = None
if _is_link_shared(ctx):
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "targets_windows"):
if is_windows_enabled:
# Make copy of a list, to avoid mutating frozen values.
object_files = list(cc_compilation_outputs.objects)
for linker_input in deps_cc_linking_context.linker_inputs.to_list():
Expand Down Expand Up @@ -735,7 +691,7 @@ def cc_binary_impl(ctx, additional_linkopts):
if extra_link_time_libraries != None:
linker_inputs_extra, runtime_libraries_extra = extra_link_time_libraries.build_libraries(ctx = ctx, static_mode = linking_mode != _LINKING_DYNAMIC, for_dynamic_library = _is_link_shared(ctx))

cc_linking_outputs_binary, cc_launcher_info, rule_impl_debug_files, deps_cc_linking_context = _create_transitive_linking_actions(
cc_linking_outputs_binary, cc_launcher_info, deps_cc_linking_context = _create_transitive_linking_actions(
ctx,
cc_toolchain,
feature_configuration,
Expand Down Expand Up @@ -850,9 +806,6 @@ def cc_binary_impl(ctx, additional_linkopts):
if generated_def_file != None:
output_groups["def_file"] = depset([generated_def_file])

if rule_impl_debug_files != None:
output_groups["rule_impl_debug_files"] = rule_impl_debug_files

if cc_linking_outputs_binary_library != None:
# For consistency and readability.
library_to_link = cc_linking_outputs_binary_library
Expand Down
Loading