diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java index 785a70c3fe9ce2..0775191a95a612 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java @@ -78,7 +78,6 @@ import net.starlark.java.eval.Starlark; import net.starlark.java.eval.StarlarkList; import net.starlark.java.eval.StarlarkValue; -import net.starlark.java.eval.Tuple; /** Common parts of the implementation of cc rules. */ public final class CcCommon implements StarlarkValue { @@ -230,11 +229,6 @@ public static Map> mergeOutputGroups( return mergedOutputGroups; } - @StarlarkMethod(name = "copts", structField = true, documented = false) - public Sequence getCoptsForStarlark() { - return StarlarkList.immutableCopyOf(getCopts()); - } - @StarlarkMethod(name = "linkopts", structField = true, documented = false) public Sequence getLinkoptsForStarlark() { return StarlarkList.immutableCopyOf(getLinkopts()); @@ -310,30 +304,6 @@ List> getPrivateHeaders() { return mapToListOfPairs(map); } - @StarlarkMethod(name = "srcs", documented = false, structField = true) - public Sequence getSourcesForStarlark() { - List> sources = getSources(); - ImmutableList tupleList = - sources.stream().map(p -> Tuple.pair(p.first, p.second)).collect(toImmutableList()); - return StarlarkList.immutableCopyOf(tupleList); - } - - @StarlarkMethod(name = "private_hdrs", documented = false, structField = true) - public Sequence getPrivateHeaderForStarlark() { - return convertListPairToTuple(getPrivateHeaders()); - } - - @StarlarkMethod(name = "public_hdrs", documented = false, structField = true) - public Sequence getPublicHeaderForStarlark() { - return convertListPairToTuple(getHeaders()); - } - - private Sequence convertListPairToTuple(List> listPair) { - ImmutableList tupleList = - listPair.stream().map(p -> Tuple.pair(p.first, p.second)).collect(toImmutableList()); - return StarlarkList.immutableCopyOf(tupleList); - } - /** * Returns a list of ({@link Artifact}, {@link Label}) pairs. Each pair represents an input source * file and the label of the rule that generates it (or the label of the source file itself if it @@ -418,7 +388,6 @@ public List> getHeaders() { } /** Returns the C++ toolchain provider. */ - @StarlarkMethod(name = "toolchain", documented = false, structField = true) public CcToolchainProvider getToolchain() { return ccToolchain; } diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl index ffaa2feb4e4fd5..4beca92ef650c1 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl @@ -641,10 +641,10 @@ def cc_binary_impl(ctx, additional_linkopts): local_defines = common.local_defines + cc_helper.get_local_defines_for_runfiles_lookup(ctx), loose_includes = common.loose_include_dirs, system_includes = common.system_include_dirs, - private_hdrs = common.private_hdrs, - public_hdrs = common.public_hdrs, + private_hdrs = cc_helper.get_private_hdrs(ctx), + public_hdrs = cc_helper.get_public_hdrs(ctx), copts_filter = common.copts_filter, - srcs = common.srcs, + srcs = cc_helper.get_srcs(ctx), compilation_contexts = compilation_context_deps, grep_includes = cc_helper.grep_includes_executable(ctx.attr._grep_includes), code_coverage_enabled = cc_helper.is_code_coverage_enabled(ctx = ctx), diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl index 91e629b0d8baaa..76a8c6aa7e7ded 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -364,6 +364,16 @@ CC_AND_OBJC.extend(CC_HEADER) CC_AND_OBJC.extend(ASSEMBLER) CC_AND_OBJC.extend(ASSESMBLER_WITH_C_PREPROCESSOR) +DISALLOWED_HDRS_FILES = [] +DISALLOWED_HDRS_FILES.extend(ARCHIVE) +DISALLOWED_HDRS_FILES.extend(PIC_ARCHIVE) +DISALLOWED_HDRS_FILES.extend(ALWAYSLINK_LIBRARY) +DISALLOWED_HDRS_FILES.extend(ALWAYSLINK_PIC_LIBRARY) +DISALLOWED_HDRS_FILES.extend(SHARED_LIBRARY) +DISALLOWED_HDRS_FILES.extend(INTERFACE_SHARED_LIBRARY) +DISALLOWED_HDRS_FILES.extend(OBJECT_FILE) +DISALLOWED_HDRS_FILES.extend(PIC_OBJECT_FILE) + extensions = struct( CC_SOURCE = CC_SOURCE, C_SOURCE = C_SOURCE, @@ -378,6 +388,7 @@ extensions = struct( OBJECT_FILE = OBJECT_FILE, PIC_OBJECT_FILE = PIC_OBJECT_FILE, CC_AND_OBJC = CC_AND_OBJC, + DISALLOWED_HDRS_FILES = DISALLOWED_HDRS_FILES, # Also includes VERSIONED_SHARED_LIBRARY files. ) def _collect_header_tokens( @@ -909,7 +920,7 @@ def _get_expanded_env(ctx, additional_make_variable_substitutions): return expanded_env def _has_target_constraints(ctx, constraints): - # Constraints is a label_list + # Constraints is a label_list. for constraint in constraints: constraint_value = constraint[platform_common.ConstraintValueInfo] if ctx.target_platform_has_constraint(constraint_value): @@ -935,6 +946,65 @@ def _is_stamping_enabled_for_aspect(ctx): def _get_local_defines_for_runfiles_lookup(ctx): return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)] +# This should be enough to assume if two labels are equal. +def _are_labels_equal(a, b): + return a.name == b.name and a.package == b.package + +def _map_to_list(m): + result = [] + for k, v in m.items(): + result.append((k, v)) + return result + +# Returns a list of (Artifact, Label) tuples. Each tuple represents an input source +# file and the label of the rule that generates it (or the label of the source file itself if it +# is an input file). +def _get_srcs(ctx): + if not hasattr(ctx.attr, "srcs"): + return [] + + # "srcs" attribute is a LABEL_LIST in cc_rules, which might also contain files. + artifact_label_map = {} + for src in ctx.attr.srcs: + if DefaultInfo in src: + for artifact in src[DefaultInfo].files.to_list(): + if "." + artifact.extension not in CC_HEADER: + old_label = artifact_label_map.get(artifact, None) + artifact_label_map[artifact] = src.label + if old_label != None and not _are_labels_equal(old_label, src.label) and "." + artifact.extension in CC_AND_OBJC: + fail( + "Artifact '{}' is duplicated (through '{}' and '{}')".format(artifact, old_label, src), + attr = "srcs", + ) + return _map_to_list(artifact_label_map) + +# Returns a list of (Artifact, Label) tuples. Each tuple represents an input source +# file and the label of the rule that generates it (or the label of the source file itself if it +# is an input file). +def _get_private_hdrs(ctx): + if not hasattr(ctx.attr, "srcs"): + return [] + artifact_label_map = {} + for src in ctx.attr.srcs: + if DefaultInfo in src: + for artifact in src[DefaultInfo].files.to_list(): + if "." + artifact.extension in CC_HEADER: + artifact_label_map[artifact] = src.label + return _map_to_list(artifact_label_map) + +# Returns the files from headers and does some checks. +def _get_public_hdrs(ctx): + if not hasattr(ctx.attr, "hdrs"): + return [] + artifact_label_map = {} + for hdr in ctx.attr.hdrs: + if DefaultInfo in hdr: + for artifact in hdr[DefaultInfo].files.to_list(): + if _check_src_extension(artifact, DISALLOWED_HDRS_FILES, True): + continue + artifact_label_map[artifact] = hdr.label + return _map_to_list(artifact_label_map) + cc_helper = struct( merge_cc_debug_contexts = _merge_cc_debug_contexts, is_code_coverage_enabled = _is_code_coverage_enabled, @@ -981,4 +1051,8 @@ cc_helper = struct( is_stamping_enabled = _is_stamping_enabled, is_stamping_enabled_for_aspect = _is_stamping_enabled_for_aspect, get_local_defines_for_runfiles_lookup = _get_local_defines_for_runfiles_lookup, + are_labels_equal = _are_labels_equal, + get_srcs = _get_srcs, + get_private_hdrs = _get_private_hdrs, + get_public_hdrs = _get_public_hdrs, ) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl index 268e7d39e6548e..45ad0c8925d36f 100755 --- a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl @@ -27,7 +27,7 @@ def _cc_library_impl(ctx): common = cc_internal.create_common(ctx = ctx) common.report_invalid_options(ctx = ctx) - cc_toolchain = common.toolchain + cc_toolchain = cc_helper.find_cpp_toolchain(ctx) feature_configuration = cc_common.configure_features( ctx = ctx, @@ -61,9 +61,9 @@ def _cc_library_impl(ctx): system_includes = common.system_include_dirs, copts_filter = common.copts_filter, purpose = "cc_library-compile", - srcs = common.srcs, - private_hdrs = common.private_hdrs, - public_hdrs = common.public_hdrs, + srcs = cc_helper.get_srcs(ctx), + private_hdrs = cc_helper.get_private_hdrs(ctx), + public_hdrs = cc_helper.get_public_hdrs(ctx), code_coverage_enabled = cc_helper.is_code_coverage_enabled(ctx), compilation_contexts = compilation_contexts, implementation_compilation_contexts = implementation_compilation_contexts,