Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linking against versioned shared library #513

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions .bazelci/presubmit.yml
@@ -1,24 +1,28 @@
---
default_targets: &default_targets
default_linux_targets: &default_linux_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "..."
- "@examples//..."
# TODO: Switch manual tag to platform constraint after bazel 4.0.
- "//test/versioned_dylib:versioned_dylib_test"
# Bindgen currently only has a working toolchain for 18.04
- "-@examples//ffi/rust_calling_c/simple/..."
tasks:
ubuntu1604:
build_targets: *default_targets
test_targets: *default_targets
build_targets: *default_linux_targets
test_targets: *default_linux_targets
ubuntu1804:
build_targets: *default_targets
build_targets: *default_linux_targets
test_targets:
- "..."
- "@examples//..."
# TODO: Switch manual tag to platform constraint after bazel 4.0.
- "//test/versioned_dylib:versioned_dylib_test"
ubuntu2004:
name: "Minimum Supported Version"
bazel: "3.0.0"
build_targets: *default_targets
test_targets: *default_targets
build_targets: *default_linux_targets
test_targets: *default_linux_targets
macos:
osx_targets: &osx_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
Expand Down
3 changes: 2 additions & 1 deletion rust/private/rustc.bzl
Expand Up @@ -185,7 +185,8 @@ def collect_deps(label, deps, proc_macro_deps, aliases, toolchain):
transitive_dylibs.append(depset([
lib
for lib in libs.to_list()
if lib.basename.endswith(toolchain.dylib_ext)
# Dynamic libraries may have a version number nowhere, or before (linux) or after (macos) the extension.
djmarcin marked this conversation as resolved.
Show resolved Hide resolved
if lib.basename.endswith(toolchain.dylib_ext) or lib.basename.split(".", 2)[1] == toolchain.dylib_ext[1:]
]))
transitive_staticlibs.append(depset([
lib
Expand Down
3 changes: 2 additions & 1 deletion rust/private/utils.bzl
Expand Up @@ -77,7 +77,8 @@ def get_lib_name(lib):
Returns:
str: The name of the library
"""
libname, ext = lib.basename.split(".", 2)
# NB: The suffix may contain a version number like 'so.1.2.3'
libname = lib.basename.split(".", 1)[0]

if libname.startswith("lib"):
return libname[3:]
Expand Down
25 changes: 25 additions & 0 deletions test/versioned_dylib/BUILD
@@ -0,0 +1,25 @@
load(
"@io_bazel_rules_rust//rust:rust.bzl",
"rust_binary",
"rust_test",
)

package(default_visibility = ["//visibility:public"])

rust_binary(
name = "versioned_dylib",
srcs = [
"src/main.rs",
],
tags = ["manual"],
deps = [
"//test/versioned_dylib/c:libreturn_zero",
"@libc",
],
)

rust_test(
name = "versioned_dylib_test",
crate = "versioned_dylib",
tags = ["manual"],
)
23 changes: 23 additions & 0 deletions test/versioned_dylib/c/BUILD
@@ -0,0 +1,23 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

[cc_binary(
name = "libreturn_zero." + x,
srcs = [
"return_zero.c",
"return_zero.h",
],
copts = ["-std=c99"],
linkopts = ["-Wl,-soname,libreturn_zero.so.2"],
linkshared = True,
tags = ["manual"],
) for x in ("so", "so.2")]

cc_library(
name = "libreturn_zero",
srcs = [
":libreturn_zero.so",
":libreturn_zero.so.2",
],
tags = ["manual"],
visibility = ["//visibility:public"],
)
Binary file added test/versioned_dylib/c/libreturn_zero.so
Binary file not shown.
5 changes: 5 additions & 0 deletions test/versioned_dylib/c/return_zero.c
@@ -0,0 +1,5 @@
#include "return_zero.h"

int return_zero() {
return 0;
}
6 changes: 6 additions & 0 deletions test/versioned_dylib/c/return_zero.h
@@ -0,0 +1,6 @@
#ifndef versioned_lib_h__
#define versioned_lib_h__

extern int return_zero();

#endif // versioned_lib_h__
38 changes: 38 additions & 0 deletions test/versioned_dylib/src/main.rs
@@ -0,0 +1,38 @@
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate libc;

extern {
pub fn return_zero() -> libc::c_int;
}

fn main() {
let zero = unsafe { return_zero() };
println!("Got {} from our shared lib", zero);
}


#[cfg(test)]
mod test {
extern {
pub fn return_zero() -> libc::c_int;
}

#[test]
fn test_return_zero() {
assert_eq!(0, unsafe { return_zero() });
// If we make it past this call, it linked correctly, so the test passes.
}
}