Skip to content

Commit

Permalink
Add Starlark implementation for several CcCommon methods. (#19076)
Browse files Browse the repository at this point in the history
Also deletes some of the implemented methods (the ones not used natively) and methods which are obsolete.

PiperOrigin-RevId: 483673510
Change-Id: Ifb0179f7ba6de2be19af6563f7e94ddca42b844f

Co-authored-by: Googler <gnish@google.com>
  • Loading branch information
iancha1992 and buildbreaker2021 authored Aug 16, 2023
1 parent 783a50e commit 485a26f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -230,11 +229,6 @@ public static Map<String, NestedSet<Artifact>> mergeOutputGroups(
return mergedOutputGroups;
}

@StarlarkMethod(name = "copts", structField = true, documented = false)
public Sequence<String> getCoptsForStarlark() {
return StarlarkList.immutableCopyOf(getCopts());
}

@StarlarkMethod(name = "linkopts", structField = true, documented = false)
public Sequence<String> getLinkoptsForStarlark() {
return StarlarkList.immutableCopyOf(getLinkopts());
Expand Down Expand Up @@ -310,30 +304,6 @@ List<Pair<Artifact, Label>> getPrivateHeaders() {
return mapToListOfPairs(map);
}

@StarlarkMethod(name = "srcs", documented = false, structField = true)
public Sequence<Tuple> getSourcesForStarlark() {
List<Pair<Artifact, Label>> sources = getSources();
ImmutableList<Tuple> 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<Tuple> getPrivateHeaderForStarlark() {
return convertListPairToTuple(getPrivateHeaders());
}

@StarlarkMethod(name = "public_hdrs", documented = false, structField = true)
public Sequence<Tuple> getPublicHeaderForStarlark() {
return convertListPairToTuple(getHeaders());
}

private Sequence<Tuple> convertListPairToTuple(List<Pair<Artifact, Label>> listPair) {
ImmutableList<Tuple> 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
Expand Down Expand Up @@ -418,7 +388,6 @@ public List<Pair<Artifact, Label>> getHeaders() {
}

/** Returns the C++ toolchain provider. */
@StarlarkMethod(name = "toolchain", documented = false, structField = true)
public CcToolchainProvider getToolchain() {
return ccToolchain;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
76 changes: 75 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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,
)
8 changes: 4 additions & 4 deletions src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 485a26f

Please sign in to comment.