Skip to content

Commit 80b4e1a

Browse files
hvadehracopybara-github
authored andcommitted
Extract the cc_shared_library rule from @_builtins to @rules_cc
PiperOrigin-RevId: 791188872 Change-Id: I3c61917386f7754028462ad460cd738fb0d66c81
1 parent 4b1c73e commit 80b4e1a

File tree

3 files changed

+1214
-4
lines changed

3 files changed

+1214
-4
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module(
77
bazel_dep(name = "bazel_features", version = "1.28.0")
88
bazel_dep(name = "bazel_skylib", version = "1.7.1")
99
bazel_dep(name = "platforms", version = "0.0.10")
10+
bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf")
1011

1112
cc_configure = use_extension("//cc:extensions.bzl", "cc_configure_extension")
1213
use_repo(cc_configure, "local_config_cc", "local_config_cc_toolchains")

cc/common/cc_helper.bzl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,78 @@ linker_mode = struct(
4040

4141
artifact_category = _artifact_category
4242

43+
def _is_non_empty_list_or_select(value, attr):
44+
if type(value) == "list":
45+
return len(value) > 0
46+
elif type(value) == "select":
47+
return True
48+
else:
49+
fail("Only select or list is valid for {} attr".format(attr))
50+
51+
def _gen_empty_def_file(ctx):
52+
trivial_def_file = ctx.actions.declare_file(ctx.label.name + ".gen.empty.def")
53+
ctx.actions.write(trivial_def_file, "", False)
54+
return trivial_def_file
55+
56+
def _get_windows_def_file_for_linking(ctx, custom_def_file, generated_def_file, feature_configuration):
57+
# 1. If a custom DEF file is specified in win_def_file attribute, use it.
58+
# 2. If a generated DEF file is available and should be used, use it.
59+
# 3. Otherwise, we use an empty DEF file to ensure the import library will be generated.
60+
if custom_def_file != None:
61+
return custom_def_file
62+
elif generated_def_file != None and _should_generate_def_file(ctx, feature_configuration) == True:
63+
return generated_def_file
64+
else:
65+
return _gen_empty_def_file(ctx)
66+
67+
def _should_generate_def_file(ctx, feature_configuration):
68+
windows_export_all_symbols_enabled = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "windows_export_all_symbols")
69+
no_windows_export_all_symbols_enabled = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "no_windows_export_all_symbols")
70+
return windows_export_all_symbols_enabled and (not no_windows_export_all_symbols_enabled) and (ctx.attr.win_def_file == None)
71+
72+
def _generate_def_file(ctx, def_parser, object_files, dll_name):
73+
def_file = ctx.actions.declare_file(ctx.label.name + ".gen.def")
74+
args = ctx.actions.args()
75+
args.add(def_file)
76+
args.add(dll_name)
77+
argv = ctx.actions.args()
78+
argv.use_param_file("@%s", use_always = True)
79+
argv.set_param_file_format("shell")
80+
for object_file in object_files:
81+
argv.add(object_file.path)
82+
83+
ctx.actions.run(
84+
mnemonic = "DefParser",
85+
executable = def_parser,
86+
toolchain = None,
87+
arguments = [args, argv],
88+
inputs = object_files,
89+
outputs = [def_file],
90+
use_default_shell_env = True,
91+
)
92+
return def_file
93+
94+
def _stringify_linker_input(linker_input):
95+
parts = []
96+
parts.append(str(linker_input.owner))
97+
for library in linker_input.libraries:
98+
if library.static_library != None:
99+
parts.append(library.static_library.path)
100+
if library.pic_static_library != None:
101+
parts.append(library.pic_static_library.path)
102+
if library.dynamic_library != None:
103+
parts.append(library.dynamic_library.path)
104+
if library.interface_library != None:
105+
parts.append(library.interface_library.path)
106+
107+
for additional_input in linker_input.additional_inputs:
108+
parts.append(additional_input.path)
109+
110+
for linkstamp in linker_input.linkstamps:
111+
parts.append(linkstamp.file().path)
112+
113+
return "".join(parts)
114+
43115
def _get_compilation_contexts_from_deps(deps):
44116
compilation_contexts = []
45117
for dep in deps:
@@ -427,5 +499,9 @@ cc_helper = struct(
427499
get_cc_flags_make_variable = _get_cc_flags_make_variable,
428500
get_compilation_contexts_from_deps = _get_compilation_contexts_from_deps,
429501
system_include_dirs = _system_include_dirs,
502+
stringify_linker_input = _stringify_linker_input,
503+
generate_def_file = _generate_def_file,
504+
get_windows_def_file_for_linking = _get_windows_def_file_for_linking,
505+
is_non_empty_list_or_select = _is_non_empty_list_or_select,
430506
)
431507
# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl:forked_exports)

0 commit comments

Comments
 (0)