Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 31 additions & 8 deletions cc/private/cc_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ _UNBOUND = _UnboundValueProviderDoNotUse()

_OLD_STARLARK_API_ALLOWLISTED_PACKAGES = [("", "tools/build_defs/cc"), ("_builtins", ""), ("", "third_party/gloop/tools/build_defs/cc")]

def _ctx_from_ctx_or_actions(ctx, actions):
if ctx != None and actions != None:
fail("exactly one of ctx or actions must be specified")
if ctx == None:
if actions == None:
fail("exactly one of ctx or actions must be specified")
ctx = actions
if type(ctx) == "actions":
return _cc_internal.actions2ctx_cheat(ctx)
return ctx

def _check_all_sources_contain_tuples_or_none_of_them(files):
no_tuple = False
has_tuple = False
Expand All @@ -63,7 +74,8 @@ def _check_all_sources_contain_tuples_or_none_of_them(files):

def _link(
*,
actions,
ctx = None,
actions = None,
name,
feature_configuration,
cc_toolchain,
Expand All @@ -88,6 +100,8 @@ def _link(
use_shareable_artifact_factory = _UNBOUND,
build_config = _UNBOUND,
emit_interface_shared_library = _UNBOUND):
ctx = _ctx_from_ctx_or_actions(ctx, actions)

if output_type == "archive":
_cc_internal.check_private_api(allowlist = _PRIVATE_STARLARKIFICATION_ALLOWLIST)

Expand Down Expand Up @@ -125,7 +139,7 @@ def _link(
emit_interface_shared_library = False

return link(
actions = actions,
ctx = ctx,
name = name,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
Expand Down Expand Up @@ -346,7 +360,8 @@ def _is_cc_toolchain_resolution_enabled_do_not_use(*, ctx):

def _create_linking_context_from_compilation_outputs(
*,
actions,
ctx = None,
actions = None,
name,
feature_configuration,
cc_toolchain,
Expand All @@ -362,6 +377,8 @@ def _create_linking_context_from_compilation_outputs(
stamp = _UNBOUND,
linked_dll_name_suffix = _UNBOUND,
test_only_target = _UNBOUND):
ctx = _ctx_from_ctx_or_actions(ctx, actions)

if stamp != _UNBOUND or \
linked_dll_name_suffix != _UNBOUND or \
test_only_target != _UNBOUND:
Expand All @@ -375,7 +392,7 @@ def _create_linking_context_from_compilation_outputs(
test_only_target = False

return create_linking_context_from_compilation_outputs(
actions = actions,
ctx = ctx,
name = name,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
Expand Down Expand Up @@ -415,7 +432,8 @@ def _create_extra_link_time_library(*, build_library_func, **kwargs):

def _register_linkstamp_compile_action(
*,
actions,
ctx = None,
actions = None,
cc_toolchain,
feature_configuration,
source_file,
Expand All @@ -426,6 +444,8 @@ def _register_linkstamp_compile_action(
build_target = None,
label_replacement = None, # deprecated, use target_name
output_replacement = None): # deprecated, use build_target
ctx = _ctx_from_ctx_or_actions(ctx, actions)

_cc_internal.check_private_api(allowlist = _PRIVATE_STARLARKIFICATION_ALLOWLIST)

resolved_target_name = target_name if target_name != None else label_replacement
Expand All @@ -437,7 +457,7 @@ def _register_linkstamp_compile_action(
fail("build_target must be specified.")

return register_linkstamp_compile_action(
actions = actions,
ctx = ctx,
cc_toolchain = cc_toolchain,
feature_configuration = feature_configuration,
source_file = source_file,
Expand All @@ -450,7 +470,8 @@ def _register_linkstamp_compile_action(

def _compile(
*,
actions,
ctx = None,
actions = None,
feature_configuration,
cc_toolchain,
name,
Expand Down Expand Up @@ -490,6 +511,8 @@ def _compile(
separate_module_headers = _UNBOUND,
module_interfaces = _UNBOUND,
non_compilation_additional_inputs = _UNBOUND):
ctx = _ctx_from_ctx_or_actions(ctx, actions)

if module_map != _UNBOUND or \
additional_module_maps != _UNBOUND or \
additional_exported_hdrs != _UNBOUND or \
Expand Down Expand Up @@ -532,7 +555,7 @@ def _compile(
_cc_internal.check_private_api(allowlist = _PRIVATE_STARLARKIFICATION_ALLOWLIST)

return compile(
actions = actions,
ctx = ctx,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
name = name,
Expand Down
9 changes: 4 additions & 5 deletions cc/private/compile/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ LTO_SOURCE_EXTENSIONS = set(extensions.CC_SOURCE + extensions.C_SOURCE)
# LINT.IfChange(compile_api)
def compile(
*,
actions,
ctx,
feature_configuration,
cc_toolchain,
srcs = [],
Expand Down Expand Up @@ -140,7 +140,7 @@ def compile(
"""Should be used for C++ compilation.

Args:
actions: <code>actions</code> object.
ctx: The rule context.
feature_configuration: <code>feature_configuration</code> to be queried."),
cc_toolchain: <code>CcToolchainInfo</code> provider to be used."),
srcs: The list of source files to be compiled.",
Expand Down Expand Up @@ -213,7 +213,7 @@ def compile(
"""

# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcModuleApi.java:compile_api)
ctx = _cc_internal.actions2ctx_cheat(actions)
actions = ctx.actions
_starlark_cc_semantics.validate_cc_compile_call(
label = ctx.label,
include_prefix = include_prefix,
Expand All @@ -225,7 +225,7 @@ def compile(
if additional_module_maps == None:
additional_module_maps = []

label = _cc_internal.actions2ctx_cheat(actions).label.same_package_label(name)
label = ctx.label.same_package_label(name)
fdo_context = cc_toolchain._fdo_context

use_pic_for_dynamic_libraries = _use_pic_for_dynamic_libs(cpp_configuration, feature_configuration)
Expand All @@ -248,7 +248,6 @@ def compile(
language_normalized = "c++" if language == None else language
language_normalized = language_normalized.replace("+", "p").upper()
source_category = SOURCE_CATEGORY_CC if language_normalized == "CPP" else SOURCE_CATEGORY_CC_AND_OBJC
ctx = _cc_internal.actions2ctx_cheat(actions)
if type(includes) == "depset":
includes = includes.to_list()
textual_hdrs_list = textual_hdrs.to_list() if type(textual_hdrs) == "depset" else textual_hdrs
Expand Down
6 changes: 2 additions & 4 deletions cc/private/compile/linkstamp_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ load(

def register_linkstamp_compile_action(
*,
actions,
ctx,
cc_toolchain,
feature_configuration,
source_file,
Expand All @@ -47,7 +47,7 @@ def register_linkstamp_compile_action(
"""Registers a C++ compile action for linkstamps.

Args:
actions: `actions` object.
ctx: The rule context.
cc_toolchain: `CcToolchainInfo` provider to be used.
feature_configuration: `feature_configuration` to be queried.
source_file: The linkstamp source file to be compiled.
Expand All @@ -60,8 +60,6 @@ def register_linkstamp_compile_action(
stamping: Whether stamping is enabled. If None, it's computed based on configuration.
additional_linkstamp_defines: A list of additional defines for linkstamp compilation.
"""
ctx = _cc_internal.actions2ctx_cheat(actions)

if stamping == None:
stamping = should_stamp(ctx)

Expand Down
13 changes: 8 additions & 5 deletions cc/private/link/cc_linking_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ load("//cc/private/link:target_types.bzl", "LINKING_MODE", "LINK_TARGET_TYPE", "
# this can be done after removing Objc archives from cc_common.link.

def create_cc_link_actions(
actions,
ctx,
link_actions,
name,
static_link_type,
dynamic_link_type,
Expand Down Expand Up @@ -80,7 +81,8 @@ def create_cc_link_actions(
behavior.

Args:
actions: (Actions) `actions` object.
ctx: The rule context.
link_actions: The wrapped action factory used to declare link artifacts.
name: (str) This is used for naming the output artifacts of actions created by this method.
static_link_type: (None|LINK_TARGET_TYPE) Type of static libraries to create.
dynamic_link_type: (None|LINK_TARGET_TYPE) Type of dynamic libraries to create.
Expand Down Expand Up @@ -151,7 +153,8 @@ def create_cc_link_actions(
use_pic_for_dynamic_libs = _use_pic_for_dynamic_libs(cpp_config, feature_configuration)

link_action_kwargs = dict(
actions = actions,
ctx = ctx,
link_actions = link_actions,
stamping = stamping,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
Expand All @@ -176,7 +179,7 @@ def create_cc_link_actions(
static_library = {}
if static_link_type:
static_library = _create_no_pic_and_pic_static_libs_actions(
actions,
link_actions,
name,
static_link_type,
cc_toolchain,
Expand All @@ -203,7 +206,7 @@ def create_cc_link_actions(

dynamic_library, all_lto_artifacts, linker_output_artifact = \
_create_dynamic_link_actions(
actions,
link_actions,
name,
dynamic_link_type,
linking_mode,
Expand Down
20 changes: 11 additions & 9 deletions cc/private/link/cpp_link_action.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ load("//cc/private/rules_impl:native_cc_common.bzl", _cc_common_internal = "nati

def link_action(
*,
actions,
ctx,
link_actions,
mnemonic,
library_identifier, # TODO(b/331164666): Set in the callee and remove
link_type,
Expand Down Expand Up @@ -76,7 +77,8 @@ def link_action(
and returned.

Args:
actions: (Actions) `actions` object.
ctx: The rule context.
link_actions: The wrapped action factory used to declare link artifacts.
mnemonic: (str) The mnemonic used in the action.
library_identifier: (str) Identifier of the library.
link_type: (LINK_TARGET_TYPE) Type of libraries to create.
Expand Down Expand Up @@ -141,7 +143,7 @@ def link_action(
if thinlto_param_file:
non_code_inputs.append(thinlto_param_file)

linkstamp_map = _map_linkstamps_to_outputs(actions, linkstamps, output)
linkstamp_map = _map_linkstamps_to_outputs(link_actions, linkstamps, output)
linkstamp_object_file_inputs = linkstamp_map.values()
object_file_inputs = object_files

Expand All @@ -161,7 +163,7 @@ def link_action(
object_files = combined_object_artifacts if use_archiver else [],
lto_compilation_context = lto_compilation_context if use_archiver else [],
shared_non_lto_backends = create_shared_non_lto_artifacts(
actions,
link_actions,
lto_compilation_context,
link_type.linker_or_archiver == USE_LINKER,
feature_configuration,
Expand Down Expand Up @@ -191,7 +193,7 @@ def link_action(
feature_configuration,
output,
_cc_internal.dynamic_library_soname(
actions,
link_actions,
# Must match https://github.com/bazelbuild/bazel/blob/795af54db5c348af5ca8b2961a982b399206ea20/src/main/java/com/google/devtools/build/lib/rules/cpp/SolibSymlinkAction.java#L169.
root_relative_path(output),
link_type != LINK_TARGET_TYPE.NODEPS_DYNAMIC_LIBRARY,
Expand All @@ -203,7 +205,7 @@ def link_action(
user_link_flags = linkopts + cc_toolchain._cpp_configuration.linkopts

finalize_link_action(
actions,
ctx,
# TODO(b/331164666): use default value instead of an if
mnemonic if mnemonic else "CppLink",
link_type.action_name,
Expand Down Expand Up @@ -241,14 +243,14 @@ def link_action(

return output_library, interface_output_library

def _map_linkstamps_to_outputs(actions, linkstamps, output):
def _map_linkstamps_to_outputs(link_actions, linkstamps, output):
""" Translates a collection of Linkstamp instances to an immutable mapping from linkstamp to object files.

In other words, given a set of source files, this method determines the output
path to which each file should be compiled.

Args:
actions: (Actions) `actions` object.
link_actions: The wrapped action factory used to declare link artifacts.
linkstamps: (list[Linkstamp])
output: the binary output path for this link
Returns:
Expand All @@ -269,6 +271,6 @@ def _map_linkstamps_to_outputs(actions, linkstamps, output):
stamp_output_dir,
paths.replace_extension(linkstamp_file.short_path, ".o"),
)
stamp_output_file = actions.declare_shareable_artifact(stamp_output_path)
stamp_output_file = link_actions.declare_shareable_artifact(stamp_output_path)
map[linkstamp] = stamp_output_file
return map
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ load("//cc/private/link:target_types.bzl", "LINKING_MODE", "LINK_TARGET_TYPE")
# IMPORTANT: This function is public API exposed on cc_common module!
def create_linking_context_from_compilation_outputs(
*,
actions,
ctx,
name,
feature_configuration,
cc_toolchain,
Expand Down Expand Up @@ -68,7 +68,7 @@ def create_linking_context_from_compilation_outputs(
those parameters need to be eventually removed or made public.

Args:
actions: (Actions) `actions` object.
ctx: The rule context.
name: (str) This is used for naming the output artifacts of actions created by this method.
feature_configuration: (FeatureConfiguration) `feature_configuration` to be queried.
language: ("cpp") Only C++ supported for now. Do not use this parameter.
Expand All @@ -94,15 +94,16 @@ def create_linking_context_from_compilation_outputs(

# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcModuleApi.java)
# TODO(b/202252560): Fix for swift_library's implicit output, remove rule_kind_cheat.
if alwayslink and _cc_internal.rule_class(_cc_internal.actions2ctx_cheat(actions)) != "swift_library":
if alwayslink and _cc_internal.rule_class(ctx) != "swift_library":
static_link_type = LINK_TARGET_TYPE.ALWAYS_LINK_STATIC_LIBRARY
else:
static_link_type = LINK_TARGET_TYPE.STATIC_LIBRARY
if type(additional_inputs) == type([]):
additional_inputs = depset(additional_inputs)

cc_linking_outputs = create_cc_link_actions(
_cc_internal.wrap_link_actions(actions),
ctx,
_cc_internal.wrap_link_actions(ctx.actions),
name,
None if disallow_static_libraries else static_link_type,
None if disallow_dynamic_library else LINK_TARGET_TYPE.NODEPS_DYNAMIC_LIBRARY,
Expand All @@ -122,8 +123,7 @@ def create_linking_context_from_compilation_outputs(
)

linker_input = create_linker_input(
# TODO(b/331164666): remove cheat, we always produce a file, file.owner gives us a label
owner = _cc_internal.actions2ctx_cheat(actions).label.same_package_label(name),
owner = ctx.label.same_package_label(name),
libraries = depset([cc_linking_outputs.library_to_link] if cc_linking_outputs.library_to_link else []),
user_link_flags = user_link_flags,
additional_inputs = additional_inputs,
Expand Down
Loading