Skip to content

Commit

Permalink
Extract a util method cc_helper.should_use_pic
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 627630498
Change-Id: Ica5cc689b9c96dc4621d03c287c7e6feb7892a02
  • Loading branch information
hvadehra authored and Copybara-Service committed Apr 24, 2024
1 parent e26ab62 commit 45836d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,10 @@ def _create_transitive_linking_actions(
cc_launcher_info = cc_internal.create_cc_launcher_info(cc_info = cc_info_without_extra_link_time_libraries, compilation_outputs = cc_compilation_outputs_with_only_objects)
return (cc_linking_outputs, cc_launcher_info, cc_linking_context)

def _use_pic(ctx, cc_toolchain, cpp_config, feature_configuration):
def _use_pic(ctx, cc_toolchain, feature_configuration):
if _is_link_shared(ctx):
return cc_toolchain.needs_pic_for_dynamic_libraries(feature_configuration = feature_configuration)
return cpp_config.force_pic() or (
cc_toolchain.needs_pic_for_dynamic_libraries(feature_configuration = feature_configuration) and (
ctx.var["COMPILATION_MODE"] != "opt" or
cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "prefer_pic_for_opt_binaries")
)
)
return cc_helper.should_use_pic(ctx, cc_toolchain, feature_configuration)

def _collect_linking_context(ctx):
cc_infos = _get_providers(ctx)
Expand Down Expand Up @@ -581,7 +576,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
link_variables["def_file_path"] = win_def_file.path
additional_linker_inputs.append(win_def_file)

use_pic = _use_pic(ctx, cc_toolchain, cpp_config, feature_configuration)
use_pic = _use_pic(ctx, cc_toolchain, feature_configuration)

# On Windows, if GENERATE_PDB_FILE feature is enabled
# then a pdb file will be built along with the executable.
Expand Down
19 changes: 19 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,24 @@ def _proto_output_root(proto_root, bin_dir_path):
else:
return bin_dir_path + "/" + proto_root

def _should_use_pic(ctx, cc_toolchain, feature_configuration):
"""Whether to use pic files
Args:
ctx: (RuleContext)
cc_toolchain: (CcToolchainInfo)
feature_configuration: (FeatureConfiguration)
Returns:
(bool)
"""
return ctx.fragments.cpp.force_pic() or (
cc_toolchain.needs_pic_for_dynamic_libraries(feature_configuration = feature_configuration) and (
ctx.var["COMPILATION_MODE"] != "opt" or
cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "prefer_pic_for_opt_binaries")
)
)

cc_helper = struct(
CPP_TOOLCHAIN_TYPE = _CPP_TOOLCHAIN_TYPE,
merge_cc_debug_contexts = _merge_cc_debug_contexts,
Expand Down Expand Up @@ -1259,4 +1277,5 @@ cc_helper = struct(
proto_output_root = _proto_output_root,
package_source_root = _package_source_root,
tokenize = _tokenize,
should_use_pic = _should_use_pic,
)

0 comments on commit 45836d0

Please sign in to comment.