Skip to content

Commit

Permalink
Add support for depending on shared libraries on linux. (#61)
Browse files Browse the repository at this point in the history
* Add support for depending on shared libraries on linux.

* Use portable bash for TMPDIR check.

* Move example 'matrix' to 'ffi/rust_calling_c'.

* Use toolchain to get dylib/staticlib file extensions.

* Skip dylib test targets on osx.

* Simplify presubmit config.

* Fix skipped labels.

* Allow `rust_binary` to depend on `cc_library` (#83)

The error message and the documentation both say this is possible. Light
testing seems to show that this is the only change required.

* Skip just rpath setup on non-linux.

* Address review comments.

* Attempt to clarify _setup_deps

* Adjust provider test for field changes.

* Actually provide correct patterns in test

* Undo removing skipping of os check

* Add FreeBSD toolchain. (#85)

* Add os field to freebsd toolchain.
  • Loading branch information
mfarrugi authored and acmcarther committed May 23, 2018
1 parent 634a6f3 commit f4b5743
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 133 deletions.
11 changes: 7 additions & 4 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ platforms:
- "..."
- "@examples//..."
macos:
build_targets:
- "..."
- "@examples//..."
test_targets:
targets: &targets
- "--" # Hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "..."
# Skip tests for dylib support on osx, since we don't support it yet.
- "@examples//..."
- "-@examples//ffi/rust_calling_c:matrix_dylib_test"
- "-@examples//ffi/rust_calling_c:matrix_dynamically_linked"
build_targets: *targets
test_targets: *targets
41 changes: 41 additions & 0 deletions examples/ffi/rust_calling_c/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package(default_visibility = ["//ffi/rust_calling_c:__subpackages__"])

load("@//rust:rust.bzl", "rust_library", "rust_test", "rust_binary")

rust_library(
name = "matrix",
srcs = [
"src/ffi.rs",
"src/lib.rs",
"src/matrix.rs",
],
deps = [
"//ffi/rust_calling_c/c:native_matrix",
"@libc//:libc",
],
)

rust_test(
name = "matrix_test",
deps = [":matrix"],
)

## Do the same as above, but with a dynamic c library.

rust_library(
name = "matrix_dynamically_linked",
srcs = [
"src/ffi.rs",
"src/lib.rs",
"src/matrix.rs",
],
deps = [
"//ffi/rust_calling_c/c:native_matrix_so",
"@libc//:libc",
],
)

rust_test(
name = "matrix_dylib_test",
deps = [":matrix_dynamically_linked"],
)
35 changes: 35 additions & 0 deletions examples/ffi/rust_calling_c/c/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package(default_visibility = ["//ffi/rust_calling_c:__subpackages__"])

cc_library(
name = "native_matrix",
srcs = ["matrix.c"],
hdrs = ["matrix.h"],
copts = ["-std=c99"],
)

cc_test(
name = "native_matrix_test",
srcs = ["matrix_test.c"],
copts = ["-std=c99"],
deps = [
":native_matrix",
],
)

## Do the same as above, but with a dynamic c library.

cc_library(
name = "native_matrix_so",
srcs = [":libnative_matrix_so.so"],
hdrs = ["matrix.h"],
)

cc_binary(
name = "libnative_matrix_so.so",
srcs = [
"matrix.c",
"matrix.h",
],
copts = ["-std=c99"],
linkshared = True,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "matrix/src/matrix.h"
#include "ffi/rust_calling_c/c/matrix.h"

#include <string.h>
#include <stdio.h>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "matrix/src/matrix.h"
#include "ffi/rust_calling_c/c/matrix.h"

#include <assert.h>
#include <stdint.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Matrix {
pub data: *mut uint64_t,
}

#[link(name = "native_matrix")]
// #[link(name = "native_matrix")] // Don't need this, BUILD file manages linking already.
extern {
pub fn matrix_new(rows: size_t, cols: size_t, data: *const uint64_t) -> *mut Matrix;
pub fn matrix_at(matrix: *const Matrix, row: size_t, col: size_t, n: *mut uint64_t) -> c_int;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ extern crate libc;

mod ffi;

pub mod matrix;
pub mod matrix;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::ptr;

/// Wrapper around pointer to FFI Matrix struct.
pub struct Matrix {
pub matrix: *mut ffi::Matrix,
matrix: *mut ffi::Matrix,
}

/// Wrapper around low-level FFI Matrix API.
Expand All @@ -38,7 +38,7 @@ impl Matrix {
let mut data_copy: Vec<u64> = vec![0; data.len()];
data_copy.clone_from_slice(data);
unsafe {
let mut matrix: *mut ffi::Matrix = ffi::matrix_new(rows, cols, data_copy.as_ptr());
let matrix: *mut ffi::Matrix = ffi::matrix_new(rows, cols, data_copy.as_ptr());
if matrix.is_null() {
panic!("Failed to allocate Matrix.");
}
Expand Down
37 changes: 0 additions & 37 deletions examples/matrix/BUILD

This file was deleted.

3 changes: 3 additions & 0 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ rust_toolchain(
rustc_lib = ["@rust_linux_x86_64//:rustc_lib"],
staticlib_ext = ".a",
dylib_ext = ".so",
os = "linux",
visibility = ["//visibility:public"],
)
Expand All @@ -149,6 +150,7 @@ rust_toolchain(
rustc_lib = ["@rust_darwin_x86_64//:rustc_lib"],
staticlib_ext = ".a",
dylib_ext = ".dylib",
os = "mac os x",
visibility = ["//visibility:public"],
)
Expand All @@ -174,6 +176,7 @@ rust_toolchain(
rustc_lib = ["@rust_freebsd_x86_64//:rustc_lib"],
staticlib_ext = ".a",
dylib_ext = ".so",
os = "freebsd",
visibility = ["//visibility:public"],
)
"""
Expand Down
Loading

0 comments on commit f4b5743

Please sign in to comment.