Skip to content

Commit

Permalink
Add Starlark implementation of common.linkopts.
Browse files Browse the repository at this point in the history
Switch calls from common.linkopts to cc_helper.linkopts.

Fix buildifier warnings for cc_library.

PiperOrigin-RevId: 503924938
Change-Id: I93a9905463aaeb2f543f0d03db44b8f050349dcb
  • Loading branch information
buildbreaker2021 authored and copybara-github committed Jan 23, 2023
1 parent 2d1f37d commit ff92499
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,6 @@ public static Map<String, NestedSet<Artifact>> mergeOutputGroups(
return mergedOutputGroups;
}

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

/**
* Returns our own linkopts from the rule attribute. This determines linker options to use when
* building this target and anything that depends on it.
Expand Down
6 changes: 4 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ def _create_transitive_linking_actions(
link_target_type,
pdb_file,
win_def_file,
additional_linkopts):
additional_linkopts,
additional_make_variable_substitutions):
cc_compilation_outputs_with_only_objects = cc_common.create_compilation_outputs(objects = None, pic_objects = None)
deps_cc_info = CcInfo(linking_context = deps_cc_linking_context)
libraries_for_current_cc_linking_context = []
Expand Down Expand Up @@ -479,7 +480,7 @@ def _create_transitive_linking_actions(
linker_inputs = cc_common.create_linker_input(
owner = ctx.label,
libraries = depset(libraries_for_current_cc_linking_context),
user_link_flags = common.linkopts + additional_linkopts,
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain) + additional_linkopts,
additional_inputs = depset(common.linker_scripts + compilation_context.transitive_compilation_prerequisites().to_list()),
)
current_cc_linking_context = cc_common.create_linking_context(linker_inputs = depset([linker_inputs]))
Expand Down Expand Up @@ -752,6 +753,7 @@ def cc_binary_impl(ctx, additional_linkopts):
pdb_file,
win_def_file,
additional_linkopts,
additional_make_variable_substitutions,
)

cc_linking_outputs_binary_library = cc_linking_outputs_binary.library_to_link
Expand Down
46 changes: 41 additions & 5 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ linker_mode = struct(
LINKING_STATIC = "static_linking_mode",
)

ios_cpus = struct(
IOS_SIMULATOR_TARGET_CPUS = ["ios_x86_64", "ios_i386", "ios_sim_arm64"],
IOS_DEVICE_TARGET_CPUS = ["ios_armv6", "ios_arm64", "ios_armv7", "ios_armv7s", "ios_arm64e"],
WATCHOS_SIMULATOR_TARGET_CPUS = ["watchos_i386", "watchos_x86_64", "watchos_arm64"],
WATCHOS_DEVICE_TARGET_CPUS = ["watchos_armv7k", "watchos_arm64_32", "watchos_device_arm64", "watchos_device_arm64e"],
TVOS_SIMULATOR_TARGET_CPUS = ["tvos_x86_64", "tvos_sim_arm64"],
TVOS_DEVICE_TARGET_CPUS = ["tvos_arm64"],
CATALYST_TARGET_CPUS = ["catalyst_x86_64"],
MACOS_TARGET_CPUS = ["darwin_x86_64", "darwin_arm64", "darwin_arm64e", "darwin"],
)

SYSROOT_FLAG = "--sysroot="

def _build_linking_context_from_libraries(ctx, libraries):
Expand Down Expand Up @@ -740,16 +751,15 @@ def _get_cc_flags_make_variable(ctx, common, cc_toolchain):
cc_flags.extend(feature_config_cc_flags)
return {"CC_FLAGS": " ".join(cc_flags)}

def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True, targets = []):
# If make variable is predefined path variable(like $(location ...))
# we will expand it first.
if exp.find(" ") != -1:
if not execpath:
if exp.startswith("location"):
exp = exp.replace("location", "rootpath", 1)
targets = []
if ctx.attr.data != None:
targets = ctx.attr.data
targets.extend(ctx.attr.data)
return ctx.expand_location("$({})".format(exp), targets = targets)

# Recursively expand nested make variables, but since there is no recursion
Expand All @@ -773,7 +783,7 @@ def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
fail("potentially unbounded recursion during expansion of {}".format(exp))
return exp

def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True):
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True, targets = []):
idx = 0
last_make_var_end = 0
result = []
Expand Down Expand Up @@ -815,7 +825,7 @@ def _expand(ctx, expression, additional_make_variable_substitutions, execpath =
# last_make_var_end make_var_start make_var_end
result.append(expression[last_make_var_end:make_var_start - 1])
make_var = expression[make_var_start + 1:make_var_end]
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath)
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath, targets)
result.append(exp)

# Update indexes.
Expand Down Expand Up @@ -1221,6 +1231,31 @@ def _create_cc_instrumented_files_info(ctx, cc_config, cc_toolchain, metadata_fi
)
return info

def _is_apple_platform(cpu):
return cpu in ios_cpus.IOS_SIMULATOR_TARGET_CPUS or \
cpu in ios_cpus.IOS_DEVICE_TARGET_CPUS or \
cpu in ios_cpus.WATCHOS_SIMULATOR_TARGET_CPUS or \
cpu in ios_cpus.WATCHOS_DEVICE_TARGET_CPUS or \
cpu in ios_cpus.TVOS_SIMULATOR_TARGET_CPUS or \
cpu in ios_cpus.TVOS_DEVICE_TARGET_CPUS or \
cpu in ios_cpus.CATALYST_TARGET_CPUS or \
cpu in ios_cpus.MACOS_TARGET_CPUS

def _linkopts(ctx, additional_make_variable_substitutions, cc_toolchain):
linkopts = getattr(ctx.attr, "linkopts", [])
if len(linkopts) == 0:
return []
targets = []
for additional_linker_input in getattr(ctx.attr, "additional_linker_inputs", []):
targets.append(additional_linker_input)
tokens = []
for linkopt in linkopts:
expanded_linkopt = _expand(ctx, linkopt, additional_make_variable_substitutions, targets = targets)
_tokenize(tokens, expanded_linkopt)
if _is_apple_platform(cc_toolchain.cpu) and "-static" in tokens:
fail("in linkopts attribute of cc_library rule {}: Apple builds do not support statically linked binaries".format(ctx.label))
return tokens

cc_helper = struct(
merge_cc_debug_contexts = _merge_cc_debug_contexts,
is_code_coverage_enabled = _is_code_coverage_enabled,
Expand Down Expand Up @@ -1276,4 +1311,5 @@ cc_helper = struct(
system_include_dirs = _system_include_dirs,
get_coverage_environment = _get_coverage_environment,
create_cc_instrumented_files_info = _create_cc_instrumented_files_info,
linkopts = _linkopts,
)
16 changes: 8 additions & 8 deletions src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def _cc_library_impl(ctx):
has_compilation_outputs = not cc_helper.is_compilation_outputs_empty(compilation_outputs)
linking_context = CcInfo().linking_context
empty_archive_linking_context = CcInfo().linking_context
is_google = True

linking_contexts = cc_helper.get_linking_contexts_from_deps(ctx.attr.deps)
linking_contexts.extend(cc_helper.get_linking_contexts_from_deps(ctx.attr.implementation_deps))
Expand All @@ -125,7 +124,6 @@ def _cc_library_impl(ctx):
if has_compilation_outputs:
dll_name_suffix = ""
win_def_file = None
def_file = None
if cc_common.is_enabled(
feature_configuration = feature_configuration,
feature_name = "targets_windows",
Expand All @@ -152,7 +150,7 @@ def _cc_library_impl(ctx):
additional_inputs = _filter_linker_scripts(ctx.files.deps),
linking_contexts = linking_contexts,
grep_includes = ctx.executable._grep_includes,
user_link_flags = common.linkopts,
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain),
alwayslink = ctx.attr.alwayslink,
disallow_dynamic_library = not create_dynamic_library,
linked_dll_name_suffix = dll_name_suffix,
Expand Down Expand Up @@ -182,7 +180,7 @@ def _cc_library_impl(ctx):
else:
linking_outputs = struct(library_to_link = None)

_add_linker_artifacts_output_groups(ctx, output_group_builder, linking_outputs)
_add_linker_artifacts_output_groups(output_group_builder, linking_outputs)

precompiled_libraries = _convert_precompiled_libraries_to_library_to_link(
ctx,
Expand All @@ -205,12 +203,12 @@ def _cc_library_impl(ctx):
if has_compilation_outputs:
contexts_to_merge.append(linking_context)
else:
user_link_flags = common.linkopts
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain)
linker_scripts = _filter_linker_scripts(ctx.files.deps)
if len(common.linkopts) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive():
if len(user_link_flags) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive():
linker_input = cc_common.create_linker_input(
owner = ctx.label,
user_link_flags = common.linkopts,
user_link_flags = user_link_flags,
additional_inputs = depset(linker_scripts),
)
contexts_to_merge.append(cc_common.create_linking_context(linker_inputs = depset([linker_input])))
Expand Down Expand Up @@ -321,14 +319,15 @@ def _cc_library_impl(ctx):
providers.append(instrumented_files_info)

if ctx.fragments.cpp.enable_legacy_cc_provider():
# buildifier: disable=rule-impl-return
return struct(
cc = cc_internal.create_cc_provider(cc_info = cc_info),
providers = providers,
)
else:
return providers

def _add_linker_artifacts_output_groups(ctx, output_group_builder, linking_outputs):
def _add_linker_artifacts_output_groups(output_group_builder, linking_outputs):
archive_file = []
dynamic_library = []

Expand Down Expand Up @@ -595,6 +594,7 @@ attrs = {
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
),
"win_def_file": attr.label(allow_single_file = [".def"]),
# buildifier: disable=attr-license
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
"_stl": semantics.get_stl(),
"_grep_includes": attr.label(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public void testCcLibraryWithDashStaticOnDarwin() throws Exception {
"badlib",
"lib_with_dash_static",
// message:
"in linkopts attribute of cc_library rule //badlib:lib_with_dash_static: "
"in linkopts attribute of cc_library rule @//badlib:lib_with_dash_static: "
+ "Apple builds do not support statically linked binaries",
// build file:
"cc_library(name = 'lib_with_dash_static',",
Expand Down

0 comments on commit ff92499

Please sign in to comment.