Skip to content

Commit ff92499

Browse files
buildbreaker2021copybara-github
authored andcommitted
Add Starlark implementation of common.linkopts.
Switch calls from common.linkopts to cc_helper.linkopts. Fix buildifier warnings for cc_library. PiperOrigin-RevId: 503924938 Change-Id: I93a9905463aaeb2f543f0d03db44b8f050349dcb
1 parent 2d1f37d commit ff92499

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,6 @@ public static Map<String, NestedSet<Artifact>> mergeOutputGroups(
227227
return mergedOutputGroups;
228228
}
229229

230-
@StarlarkMethod(name = "linkopts", structField = true, documented = false)
231-
public Sequence<String> getLinkoptsForStarlark() {
232-
return StarlarkList.immutableCopyOf(getLinkopts());
233-
}
234-
235230
/**
236231
* Returns our own linkopts from the rule attribute. This determines linker options to use when
237232
* building this target and anything that depends on it.

src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ def _create_transitive_linking_actions(
430430
link_target_type,
431431
pdb_file,
432432
win_def_file,
433-
additional_linkopts):
433+
additional_linkopts,
434+
additional_make_variable_substitutions):
434435
cc_compilation_outputs_with_only_objects = cc_common.create_compilation_outputs(objects = None, pic_objects = None)
435436
deps_cc_info = CcInfo(linking_context = deps_cc_linking_context)
436437
libraries_for_current_cc_linking_context = []
@@ -479,7 +480,7 @@ def _create_transitive_linking_actions(
479480
linker_inputs = cc_common.create_linker_input(
480481
owner = ctx.label,
481482
libraries = depset(libraries_for_current_cc_linking_context),
482-
user_link_flags = common.linkopts + additional_linkopts,
483+
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain) + additional_linkopts,
483484
additional_inputs = depset(common.linker_scripts + compilation_context.transitive_compilation_prerequisites().to_list()),
484485
)
485486
current_cc_linking_context = cc_common.create_linking_context(linker_inputs = depset([linker_inputs]))
@@ -752,6 +753,7 @@ def cc_binary_impl(ctx, additional_linkopts):
752753
pdb_file,
753754
win_def_file,
754755
additional_linkopts,
756+
additional_make_variable_substitutions,
755757
)
756758

757759
cc_linking_outputs_binary_library = cc_linking_outputs_binary.library_to_link

src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ linker_mode = struct(
5050
LINKING_STATIC = "static_linking_mode",
5151
)
5252

53+
ios_cpus = struct(
54+
IOS_SIMULATOR_TARGET_CPUS = ["ios_x86_64", "ios_i386", "ios_sim_arm64"],
55+
IOS_DEVICE_TARGET_CPUS = ["ios_armv6", "ios_arm64", "ios_armv7", "ios_armv7s", "ios_arm64e"],
56+
WATCHOS_SIMULATOR_TARGET_CPUS = ["watchos_i386", "watchos_x86_64", "watchos_arm64"],
57+
WATCHOS_DEVICE_TARGET_CPUS = ["watchos_armv7k", "watchos_arm64_32", "watchos_device_arm64", "watchos_device_arm64e"],
58+
TVOS_SIMULATOR_TARGET_CPUS = ["tvos_x86_64", "tvos_sim_arm64"],
59+
TVOS_DEVICE_TARGET_CPUS = ["tvos_arm64"],
60+
CATALYST_TARGET_CPUS = ["catalyst_x86_64"],
61+
MACOS_TARGET_CPUS = ["darwin_x86_64", "darwin_arm64", "darwin_arm64e", "darwin"],
62+
)
63+
5364
SYSROOT_FLAG = "--sysroot="
5465

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

743-
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
754+
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True, targets = []):
744755
# If make variable is predefined path variable(like $(location ...))
745756
# we will expand it first.
746757
if exp.find(" ") != -1:
747758
if not execpath:
748759
if exp.startswith("location"):
749760
exp = exp.replace("location", "rootpath", 1)
750-
targets = []
751761
if ctx.attr.data != None:
752-
targets = ctx.attr.data
762+
targets.extend(ctx.attr.data)
753763
return ctx.expand_location("$({})".format(exp), targets = targets)
754764

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

776-
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True):
786+
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True, targets = []):
777787
idx = 0
778788
last_make_var_end = 0
779789
result = []
@@ -815,7 +825,7 @@ def _expand(ctx, expression, additional_make_variable_substitutions, execpath =
815825
# last_make_var_end make_var_start make_var_end
816826
result.append(expression[last_make_var_end:make_var_start - 1])
817827
make_var = expression[make_var_start + 1:make_var_end]
818-
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath)
828+
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath, targets)
819829
result.append(exp)
820830

821831
# Update indexes.
@@ -1221,6 +1231,31 @@ def _create_cc_instrumented_files_info(ctx, cc_config, cc_toolchain, metadata_fi
12211231
)
12221232
return info
12231233

1234+
def _is_apple_platform(cpu):
1235+
return cpu in ios_cpus.IOS_SIMULATOR_TARGET_CPUS or \
1236+
cpu in ios_cpus.IOS_DEVICE_TARGET_CPUS or \
1237+
cpu in ios_cpus.WATCHOS_SIMULATOR_TARGET_CPUS or \
1238+
cpu in ios_cpus.WATCHOS_DEVICE_TARGET_CPUS or \
1239+
cpu in ios_cpus.TVOS_SIMULATOR_TARGET_CPUS or \
1240+
cpu in ios_cpus.TVOS_DEVICE_TARGET_CPUS or \
1241+
cpu in ios_cpus.CATALYST_TARGET_CPUS or \
1242+
cpu in ios_cpus.MACOS_TARGET_CPUS
1243+
1244+
def _linkopts(ctx, additional_make_variable_substitutions, cc_toolchain):
1245+
linkopts = getattr(ctx.attr, "linkopts", [])
1246+
if len(linkopts) == 0:
1247+
return []
1248+
targets = []
1249+
for additional_linker_input in getattr(ctx.attr, "additional_linker_inputs", []):
1250+
targets.append(additional_linker_input)
1251+
tokens = []
1252+
for linkopt in linkopts:
1253+
expanded_linkopt = _expand(ctx, linkopt, additional_make_variable_substitutions, targets = targets)
1254+
_tokenize(tokens, expanded_linkopt)
1255+
if _is_apple_platform(cc_toolchain.cpu) and "-static" in tokens:
1256+
fail("in linkopts attribute of cc_library rule {}: Apple builds do not support statically linked binaries".format(ctx.label))
1257+
return tokens
1258+
12241259
cc_helper = struct(
12251260
merge_cc_debug_contexts = _merge_cc_debug_contexts,
12261261
is_code_coverage_enabled = _is_code_coverage_enabled,
@@ -1276,4 +1311,5 @@ cc_helper = struct(
12761311
system_include_dirs = _system_include_dirs,
12771312
get_coverage_environment = _get_coverage_environment,
12781313
create_cc_instrumented_files_info = _create_cc_instrumented_files_info,
1314+
linkopts = _linkopts,
12791315
)

src/main/starlark/builtins_bzl/common/cc/cc_library.bzl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def _cc_library_impl(ctx):
102102
has_compilation_outputs = not cc_helper.is_compilation_outputs_empty(compilation_outputs)
103103
linking_context = CcInfo().linking_context
104104
empty_archive_linking_context = CcInfo().linking_context
105-
is_google = True
106105

107106
linking_contexts = cc_helper.get_linking_contexts_from_deps(ctx.attr.deps)
108107
linking_contexts.extend(cc_helper.get_linking_contexts_from_deps(ctx.attr.implementation_deps))
@@ -125,7 +124,6 @@ def _cc_library_impl(ctx):
125124
if has_compilation_outputs:
126125
dll_name_suffix = ""
127126
win_def_file = None
128-
def_file = None
129127
if cc_common.is_enabled(
130128
feature_configuration = feature_configuration,
131129
feature_name = "targets_windows",
@@ -152,7 +150,7 @@ def _cc_library_impl(ctx):
152150
additional_inputs = _filter_linker_scripts(ctx.files.deps),
153151
linking_contexts = linking_contexts,
154152
grep_includes = ctx.executable._grep_includes,
155-
user_link_flags = common.linkopts,
153+
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain),
156154
alwayslink = ctx.attr.alwayslink,
157155
disallow_dynamic_library = not create_dynamic_library,
158156
linked_dll_name_suffix = dll_name_suffix,
@@ -182,7 +180,7 @@ def _cc_library_impl(ctx):
182180
else:
183181
linking_outputs = struct(library_to_link = None)
184182

185-
_add_linker_artifacts_output_groups(ctx, output_group_builder, linking_outputs)
183+
_add_linker_artifacts_output_groups(output_group_builder, linking_outputs)
186184

187185
precompiled_libraries = _convert_precompiled_libraries_to_library_to_link(
188186
ctx,
@@ -205,12 +203,12 @@ def _cc_library_impl(ctx):
205203
if has_compilation_outputs:
206204
contexts_to_merge.append(linking_context)
207205
else:
208-
user_link_flags = common.linkopts
206+
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain)
209207
linker_scripts = _filter_linker_scripts(ctx.files.deps)
210-
if len(common.linkopts) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive():
208+
if len(user_link_flags) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive():
211209
linker_input = cc_common.create_linker_input(
212210
owner = ctx.label,
213-
user_link_flags = common.linkopts,
211+
user_link_flags = user_link_flags,
214212
additional_inputs = depset(linker_scripts),
215213
)
216214
contexts_to_merge.append(cc_common.create_linking_context(linker_inputs = depset([linker_input])))
@@ -321,14 +319,15 @@ def _cc_library_impl(ctx):
321319
providers.append(instrumented_files_info)
322320

323321
if ctx.fragments.cpp.enable_legacy_cc_provider():
322+
# buildifier: disable=rule-impl-return
324323
return struct(
325324
cc = cc_internal.create_cc_provider(cc_info = cc_info),
326325
providers = providers,
327326
)
328327
else:
329328
return providers
330329

331-
def _add_linker_artifacts_output_groups(ctx, output_group_builder, linking_outputs):
330+
def _add_linker_artifacts_output_groups(output_group_builder, linking_outputs):
332331
archive_file = []
333332
dynamic_library = []
334333

@@ -595,6 +594,7 @@ attrs = {
595594
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
596595
),
597596
"win_def_file": attr.label(allow_single_file = [".def"]),
597+
# buildifier: disable=attr-license
598598
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
599599
"_stl": semantics.get_stl(),
600600
"_grep_includes": attr.label(

src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ public void testCcLibraryWithDashStaticOnDarwin() throws Exception {
696696
"badlib",
697697
"lib_with_dash_static",
698698
// message:
699-
"in linkopts attribute of cc_library rule //badlib:lib_with_dash_static: "
699+
"in linkopts attribute of cc_library rule @//badlib:lib_with_dash_static: "
700700
+ "Apple builds do not support statically linked binaries",
701701
// build file:
702702
"cc_library(name = 'lib_with_dash_static',",

0 commit comments

Comments
 (0)