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

Add support for depending on shared libraries on linux. #61

Merged
merged 19 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 it's not yet supported.
- "@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.

Loading