Skip to content

Commit 5d65670

Browse files
capickettfacebook-github-bot
authored andcommitted
Expose soname field on rust_library
Summary: Requested by tmcguire-meta on https://fb.workplace.com/groups/rustonmobile/posts/3723752194537762 in order to allow including rust shared objects into gradle builds in arvr land. Reviewed By: dtolnay Differential Revision: D58200175 fbshipit-source-id: 27d6e8ed37a8c661475ca1c88db8d4269aed33d2
1 parent b952b54 commit 5d65670

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

prelude/decls/cxx_rules.bzl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,12 @@ cxx_library = prelude_rule(
519519
cxx_common.exported_post_platform_linker_flags_arg() |
520520
native_common.link_style() |
521521
native_common.link_whole(link_whole_type = attrs.option(attrs.bool(), default = None)) |
522+
native_common.soname() |
522523
cxx_common.raw_headers_arg() |
523524
cxx_common.include_directories_arg() |
524525
cxx_common.public_include_directories_arg() |
525526
cxx_common.public_system_include_directories_arg() |
526527
{
527-
"soname": attrs.option(attrs.string(), default = None, doc = """
528-
Sets the soname ("shared object name") of any shared library produced from this rule.
529-
The default value is based on the full rule name.
530-
The macro `$(ext)` will be replaced with a platform-appropriate extension.
531-
An argument can be provided, which is a library version.
532-
For example `soname = 'libfoo.$(ext 2.3)'` will be `libfoo.2.3.dylib` on Mac and `libfoo.so.2.3` on Linux.
533-
"""),
534528
"used_by_wrap_script": attrs.bool(default = False, doc = """
535529
When using an exopackage
536530
Android, if this parameter is set to `True`, then the library is

prelude/decls/native_common.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,22 @@ def _link_group_public_deps_label():
4646
"""),
4747
}
4848

49+
def _soname():
50+
return {
51+
"soname": attrs.option(attrs.string(), default = None, doc = """
52+
Sets the soname ("shared object name") of any shared library produced from this rule.
53+
The default value is based on the full rule name.
54+
The macro `$(ext)` will be replaced with a platform-appropriate extension.
55+
An argument can be provided, which is a library version.
56+
For example `soname = 'libfoo.$(ext 2.3)'` will be `libfoo.2.3.dylib` on Mac and `libfoo.so.2.3` on Linux.
57+
"""),
58+
}
59+
4960
native_common = struct(
5061
link_group_deps = _link_group_deps,
5162
link_group_public_deps_label = _link_group_public_deps_label,
5263
link_style = _link_style,
5364
link_whole = _link_whole,
5465
preferred_linkage = _preferred_linkage,
66+
soname = _soname,
5567
)

prelude/decls/rust_rules.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ rust_library = prelude_rule(
186186
rust_common.crate(crate_type = attrs.option(attrs.string(), default = None)) |
187187
rust_common.crate_root() |
188188
native_common.preferred_linkage(preferred_linkage_type = attrs.enum(Linkage.values(), default = "any")) |
189+
native_common.soname() |
189190
_rust_common_attributes(is_binary = False) |
190191
{
191192
"crate_dynamic": attrs.option(attrs.dep(), default = None),

prelude/rust/build.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ load("@prelude//cxx:debug.bzl", "SplitDebugMode")
2424
load("@prelude//cxx:dwp.bzl", "dwp", "dwp_available")
2525
load(
2626
"@prelude//cxx:linker.bzl",
27-
"get_default_shared_library_name",
2827
"get_shared_library_name_linker_flags",
2928
)
3029
load(
@@ -75,6 +74,7 @@ load(
7574
"RustLinkInfo",
7675
"attr_crate",
7776
"attr_simple_crate_for_filenames",
77+
"attr_soname",
7878
"get_available_proc_macros",
7979
"inherited_external_debug_info",
8080
"inherited_merged_link_infos",
@@ -895,7 +895,7 @@ def _compute_common_args(
895895

896896
if crate_type in [CrateType("cdylib"), CrateType("dylib")] and not is_check:
897897
linker_info = compile_ctx.cxx_toolchain_info.linker_info
898-
shlib_name = get_default_shared_library_name(linker_info, ctx.label)
898+
shlib_name = attr_soname(ctx)
899899
dep_args.add(cmd_args(
900900
get_shared_library_name_linker_flags(linker_info.type, shlib_name),
901901
format = "-Clink-arg={}",

prelude/rust/link_info.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ load(
1616
"@prelude//cxx:cxx.bzl",
1717
"get_auto_link_group_specs",
1818
)
19+
load("@prelude//cxx:cxx_context.bzl", "get_cxx_toolchain_info")
1920
load(
2021
"@prelude//cxx:cxx_library_utility.bzl",
2122
"cxx_is_gnu",
@@ -37,6 +38,11 @@ load(
3738
"@prelude//cxx:link_groups_types.bzl",
3839
"LinkGroupInfo", # @unused Used as a type
3940
)
41+
load(
42+
"@prelude//cxx:linker.bzl",
43+
"get_default_shared_library_name",
44+
"get_shared_library_name_for_param",
45+
)
4046
load(
4147
"@prelude//linking:link_groups.bzl",
4248
"LinkGroupLib", # @unused Used as a type
@@ -584,3 +590,12 @@ def attr_crate(ctx: AnalysisContext) -> CrateName:
584590
simple = normalize_crate(ctx.attrs.crate or ctx.label.name),
585591
dynamic = dynamic,
586592
)
593+
594+
def attr_soname(ctx: AnalysisContext) -> str:
595+
"""
596+
Get the shared library name to set for the given rust library.
597+
"""
598+
linker_info = get_cxx_toolchain_info(ctx).linker_info
599+
if ctx.attrs.soname != None:
600+
return get_shared_library_name_for_param(linker_info, ctx.attrs.soname)
601+
return get_default_shared_library_name(linker_info, ctx.label)

prelude/rust/rust_library.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ load(
1818
load(
1919
"@prelude//cxx:linker.bzl",
2020
"PDB_SUB_TARGET",
21-
"get_default_shared_library_name",
2221
"get_pdb_providers",
2322
)
2423
load(
@@ -92,6 +91,7 @@ load(
9291
"RustLinkStrategyInfo",
9392
"RustProcMacroMarker", # @unused Used as a type
9493
"attr_crate",
94+
"attr_soname",
9595
"inherited_exported_link_deps",
9696
"inherited_link_group_lib_infos",
9797
"inherited_linkable_graphs",
@@ -599,7 +599,7 @@ def _advanced_unstable_link_providers(
599599
solibs = {}
600600

601601
# Add the shared library to the list of shared libs.
602-
shlib_name = get_default_shared_library_name(linker_info, ctx.label)
602+
shlib_name = attr_soname(ctx)
603603

604604
shared_lib_params = lang_style_param[(LinkageLang("native-unbundled"), LibOutputStyle("shared_lib"))]
605605
shared_lib_output = native_param_artifact[shared_lib_params].output
@@ -786,7 +786,7 @@ def _native_link_providers(
786786
solibs = {}
787787

788788
# Add the shared library to the list of shared libs.
789-
shlib_name = get_default_shared_library_name(linker_info, ctx.label)
789+
shlib_name = attr_soname(ctx)
790790

791791
# Only add a shared library if we generated one.
792792
# TODO(cjhopman): This is strange. Normally (like in c++) the link_infos passed to create_merged_link_info above would only have

0 commit comments

Comments
 (0)