Skip to content

Commit ece45d1

Browse files
authoredOct 29, 2022
Merge pull request #382 from brianjjones/remove_build
Moving runtime bindings into their own crate
2 parents 015eb61 + cf5ab4f commit ece45d1

File tree

18 files changed

+115
-48
lines changed

18 files changed

+115
-48
lines changed
 

‎Cargo.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ features = ["private-docs-rs", "tensorflow_unstable", "ndarray", "eager"]
2323
libc = "0.2.132"
2424
num-complex = { version = "0.4.2", default-features = false }
2525
tensorflow-internal-macros = { version = "=0.0.3", path = "tensorflow-internal-macros" }
26-
tensorflow-sys = { version = "0.22.1", path = "tensorflow-sys" }
26+
tensorflow-sys = { version = "0.22.1", path = "tensorflow-sys", optional = true }
27+
tensorflow-sys-runtime = { version = "0.1.0", path = "tensorflow-sys-runtime", optional = true }
2728
byteorder = "1.4.3"
2829
crc = "3.0.0"
2930
half = "2.1.0"
@@ -38,9 +39,10 @@ random = "0.12.2"
3839
serial_test = "0.9.0"
3940

4041
[features]
42+
default = ["tensorflow-sys"]
4143
tensorflow_gpu = ["tensorflow-sys/tensorflow_gpu"]
4244
tensorflow_unstable = []
43-
tensorflow_runtime_linking = ["tensorflow-sys/runtime_linking"]
45+
tensorflow_runtime_linking = ["tensorflow-sys-runtime"]
4446
eager = ["tensorflow-sys/eager"]
4547
# This is for testing purposes; users should not use this.
4648
examples_system_alloc = ["tensorflow-sys/examples_system_alloc"]
@@ -69,4 +71,4 @@ name = "xor"
6971

7072
[[example]]
7173
name = "mobilenetv3"
72-
required-features = ["eager"]
74+
required-features = ["eager"]

‎RELEASING.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Note that any crate not mentioned here (e.g. tensorflow-proto-codegen, tensorflo
2525
1. Bump the version in `tensorflow-sys/Cargo.toml`
2626
1. Bump the version in `tensorflow-sys/README.md`
2727
1. Bump the version for `tensorflow-sys` in the root `Cargo.toml`
28+
1. Bump version number of `tensorflow-sys-runtime` if necessary
29+
1. Run `git log v${PREVIOUS_VERSION?}..HEAD tensorflow-sys-runtime` and see if there were any changes. If not, skip.
30+
1. Bump the version in `tensorflow-sys-runtime/Cargo.toml`
31+
1. Bump the version in `tensorflow-sys-runtime/README.md`
32+
1. Bump the version for `tensorflow-sys-runtime` in the root `Cargo.toml`
2833
1. Bump version number of `tensorflow-internal-macros` if necessary
2934
1. Run `git log v${PREVIOUS_VERSION?}..HEAD tensorflow-internal-macros` and see if there were any changes. If not, skip.
3035
1. Bump the version in `tensorflow-internal-macros/Cargo.toml`
@@ -36,6 +41,7 @@ Note that any crate not mentioned here (e.g. tensorflow-proto-codegen, tensorflo
3641
1. Run `./run-valgrind`
3742
1. Commit and push the changes. (Push before publishing to ensure that the changes being published are up to date.)
3843
1. If the version of tensorflow-sys was bumped, run `cargo publish` for tensorflow-sys. (Publish before tagging in case there are problems publishing and we need to add commits to fix them.)
44+
1. If the version of tensorflow-sys-runtime was bumped, run `cargo publish` for tensorflow-sys-runtime. (Publish before tagging in case there are problems publishing and we need to add commits to fix them.)
3945
1. If the version of tensorflow-internal-macros was bumped, run `cargo publish` for tensorflow-internal-macros. (Publish before tagging in case there are problems publishing and we need to add commits to fix them.)
4046
1. Run `cargo publish`. (Publish before tagging in case there are problems publishing and we need to add commits to fix them.)
4147
1. Add a `v${VERSION?}` tag and push it

‎src/buffer.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use std::ops::RangeTo;
1616
use std::os::raw::c_void as std_c_void;
1717
use std::process;
1818
use std::slice;
19+
#[cfg(feature = "default")]
1920
use tensorflow_sys as tf;
21+
#[cfg(feature = "tensorflow_runtime_linking")]
22+
use tensorflow_sys_runtime as tf;
2023

2124
/// Fixed-length heap-allocated vector.
2225
/// This is basically a `Box<[T]>`, except that that type can't actually be constructed.

‎src/graph.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use std::slice;
2828
use std::str::FromStr;
2929
use std::str::Utf8Error;
3030
use std::sync::Arc;
31+
#[cfg(feature = "default")]
3132
use tensorflow_sys as tf;
33+
#[cfg(feature = "tensorflow_runtime_linking")]
34+
use tensorflow_sys_runtime as tf;
3235

3336
#[derive(Debug)]
3437
struct GraphImpl {

‎src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ use std::process;
4949
use std::ptr;
5050
use std::slice;
5151
use std::str::Utf8Error;
52+
#[cfg(feature = "default")]
5253
use tensorflow_sys as tf;
54+
#[cfg(feature = "tensorflow_runtime_linking")]
55+
use tensorflow_sys_runtime as tf;
5356

5457
////////////////////////
5558

‎src/while_loop.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::os::raw::c_int;
99
use std::ptr;
1010
use std::result;
1111
use std::slice;
12+
#[cfg(feature = "default")]
1213
use tensorflow_sys as tf;
14+
#[cfg(feature = "tensorflow_runtime_linking")]
15+
use tensorflow_sys_runtime as tf;
1316

1417
// This exists purely to ensure TF_AbortWhile gets called properly, even on panic.
1518
#[derive(Debug)]

‎tensorflow-sys-runtime/Cargo.toml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "tensorflow-sys-runtime"
3+
version = "0.1.0"
4+
authors = ["Brian Jones <brian.j.jones@intel.com>"]
5+
license = "Apache-2.0"
6+
keywords = ["TensorFlow", "runtime bindings"]
7+
description = "The package provides runtime bindings to TensorFlow."
8+
documentation = "https://tensorflow.github.io/rust"
9+
homepage = "https://github.com/tensorflow/rust"
10+
repository = "https://github.com/tensorflow/rust"
11+
edition = "2018"
12+
13+
[dependencies]
14+
libc = "0.2.132"
15+
lazy_static = "1.4.0"
16+
libloading = "0.7.3"
17+
cfg-if = "1.0.0"
18+
log = "0.4.17"

‎tensorflow-sys-runtime/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# tensorflow-sys-runtime [![Version][version-icon]][version-page]
2+
3+
The crate provides runtime bindings to [TensorFlow][tensorflow]. Using runtime bindings allows you to avoid
4+
pulling in additional package dependencies into your project. Users will need to call tensorflow::library::load()
5+
before any other calls so that the linking is completed before use.
6+
7+
## NOTE
8+
This crate is meant to be used by [Rust language bindings for Tensorflow][crates-tf]. It is not meant to be used on it's own.
9+
To use it you will need to disable the default features so you don't also include the tensorflow-sys crate.
10+
11+
```
12+
[dependencies]
13+
tensorflow = { version = "0.19.1", default-features = false, features = ["tensorflow_runtime_linking"]}
14+
```
15+
16+
## Requirements
17+
18+
To use these bindings you must have the Tensorflow C libraries installed. See [install steps][tensorflow-setup]
19+
for detailed instructions.
20+
21+
[tensorflow]: https://www.tensorflow.org
22+
[tensorflow-setup]: https://www.tensorflow.org/install/lang_c
23+
[crates-tf]: https://crates.io/crates/tensorflow
24+
[version-icon]: https://img.shields.io/crates/v/tensorflow-sys-runtime.svg
25+
[version-page]: https://crates.io/crates/tensorflow-sys-runtime
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
if ! which bindgen > /dev/null; then
4+
echo "ERROR: Please install 'bindgen' using cargo:"
5+
echo " cargo install bindgen"
6+
echo "See https://github.com/servo/rust-bindgen for more information."
7+
exit 1
8+
fi
9+
10+
include_dir="$HOME/git/tensorflow"
11+
12+
bindgen_options_runtime_functions="--allowlist-function TF_.+ --blocklist-type .+ --size_t-is-usize --default-enum-style=rust --generate-inline-functions"
13+
cmd="bindgen ${bindgen_options_runtime_functions} ${include_dir}/tensorflow/c/c_api.h --output src/runtime_linking/c_api.rs -- -I ${include_dir}"
14+
echo ${cmd}
15+
${cmd}
16+
17+
bindgen_options_runtime_types="--allowlist-type TF_.+ --blocklist-function .+ --size_t-is-usize --default-enum-style=rust --generate-inline-functions"
18+
cmd="bindgen ${bindgen_options_runtime_types} ${include_dir}/tensorflow/c/c_api.h --output src/runtime_linking/types.rs -- -I ${include_dir}"
19+
echo ${cmd}
20+
${cmd}
21+
22+
echo "link! {\n$(cat src/runtime_linking/c_api.rs)" > src/runtime_linking/c_api.rs
23+
echo } >> src/runtime_linking/c_api.rs

‎tensorflow-sys/src/runtime_linking/c_api.rs‎ renamed to ‎tensorflow-sys-runtime/src/c_api.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include!("runtime.rs");
12
link! {
23
/* automatically generated by rust-bindgen 0.59.2 */
34

File renamed without changes.

‎tensorflow-sys-runtime/src/lib.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(non_camel_case_types)]
2+
#![allow(non_snake_case)]
3+
#![allow(non_upper_case_globals)]
4+
include!("c_api.rs");
5+
include!("types.rs");
6+
include!("finder.rs");
7+
pub use crate::TF_AttrType::*;
8+
pub use crate::TF_Code::*;
9+
pub use crate::TF_DataType::*;
10+
11+
pub mod library {
12+
use std::path::PathBuf;
13+
14+
// Include the definition of `load` here. This allows hiding all of the "extra" linking-related
15+
// functions in the same place, without polluting the top-level namespace (which should only
16+
// contain foreign functions and types).
17+
#[doc(inline)]
18+
pub use super::load;
19+
20+
pub fn find() -> Option<PathBuf> {
21+
super::find("tensorflow")
22+
}
23+
}

‎tensorflow-sys/src/runtime_linking/runtime.rs‎ renamed to ‎tensorflow-sys-runtime/src/runtime.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ macro_rules! link {
1616
// Wrap the loaded functions.
1717
pub(crate) struct SharedLibrary {
1818
library: libloading::Library,
19-
path: PathBuf,
2019
pub functions: Functions,
2120
}
2221
impl SharedLibrary {
23-
fn new(library: libloading::Library, path: PathBuf) -> Self {
22+
fn new(library: libloading::Library) -> Self {
2423
Self {
2524
library,
26-
path,
2725
functions: Functions::default(),
2826
}
2927
}
@@ -95,7 +93,7 @@ macro_rules! link {
9593
)
9694
});
9795

98-
let mut library = SharedLibrary::new(library?, path);
96+
let mut library = SharedLibrary::new(library?);
9997
$(load::$name(&mut library);)+
10098
Ok(library)
10199
}
File renamed without changes.

‎tensorflow-sys/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ zip = "0.6.2"
3636
[features]
3737
tensorflow_gpu = []
3838
eager = []
39-
runtime_linking = ["lazy_static", "libloading", "cfg-if", "log"]
4039
# This is for testing purposes; users should not use this.
4140
examples_system_alloc = []
4241
private-docs-rs = [] # DO NOT RELY ON THIS

‎tensorflow-sys/generate_bindgen_rs.sh‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,3 @@ bindgen_options_eager="--allowlist-function TFE_.+ --allowlist-type TFE_.+ --all
1818
cmd="bindgen ${bindgen_options_eager} ${include_dir}/tensorflow/c/eager/c_api.h --output src/eager/c_api.rs -- -I ${include_dir}"
1919
echo ${cmd}
2020
${cmd}
21-
22-
bindgen_options_runtime_functions="--allowlist-function TF_.+ --blocklist-type .+ --size_t-is-usize --default-enum-style=rust --generate-inline-functions"
23-
cmd="bindgen ${bindgen_options_runtime_functions} ${include_dir}/tensorflow/c/c_api.h --output src/runtime_linking/c_api.rs -- -I ${include_dir}"
24-
echo ${cmd}
25-
${cmd}
26-
27-
bindgen_options_runtime_types="--allowlist-type TF_.+ --blocklist-function .+ --size_t-is-usize --default-enum-style=rust --generate-inline-functions"
28-
cmd="bindgen ${bindgen_options_runtime_types} ${include_dir}/tensorflow/c/c_api.h --output src/runtime_linking/types.rs -- -I ${include_dir}"
29-
echo ${cmd}
30-
${cmd}
31-
32-
echo "link! {\n$(cat src/runtime_linking/c_api.rs)" > src/runtime_linking/c_api.rs
33-
echo } >> src/runtime_linking/c_api.rs

‎tensorflow-sys/src/lib.rs‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,8 @@
66
mod eager;
77
#[cfg(feature = "eager")]
88
pub use eager::*;
9-
10-
#[cfg(feature = "runtime_linking")]
11-
mod runtime_linking;
12-
#[cfg(feature = "runtime_linking")]
13-
pub use runtime_linking::*;
14-
15-
#[cfg(not(feature = "runtime_linking"))]
169
include!("c_api.rs");
1710

18-
#[cfg(not(feature = "runtime_linking"))]
1911
pub use crate::TF_AttrType::*;
2012
pub use crate::TF_Code::*;
2113
pub use crate::TF_DataType::*;
22-
23-
#[cfg(feature = "runtime_linking")]
24-
pub mod library {
25-
use std::path::PathBuf;
26-
27-
// Include the definition of `load` here. This allows hiding all of the "extra" linking-related
28-
// functions in the same place, without polluting the top-level namespace (which should only
29-
// contain foreign functions and types).
30-
#[doc(inline)]
31-
pub use super::runtime_linking::load;
32-
33-
pub fn find() -> Option<PathBuf> {
34-
super::runtime_linking::find("tensorflow")
35-
}
36-
}

‎tensorflow-sys/src/runtime_linking/mod.rs‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.