Skip to content

Commit

Permalink
rust native feature refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmacek committed Feb 13, 2024
1 parent 74e3533 commit f18fc81
Show file tree
Hide file tree
Showing 74 changed files with 1,449 additions and 653 deletions.
1 change: 1 addition & 0 deletions nautilus_core/accounting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension-module = [
]
python = ["pyo3"]
default = ["python"]
native = []

[build-dependencies]
cbindgen = { workspace = true, optional = true }
1 change: 1 addition & 0 deletions nautilus_core/adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extension-module = [
databento = ["dep:databento", "dbn"]
python = ["pyo3", "pyo3-asyncio"]
default = ["databento", "python"]
native = []

[dev-dependencies]
criterion = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions nautilus_core/backtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extension-module = [
ffi = ["cbindgen"]
python = ["pyo3"]
default = ["ffi", "python"]
native = []


[build-dependencies]
cbindgen = { workspace = true, optional = true }
39 changes: 22 additions & 17 deletions nautilus_core/backtest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,34 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

extern crate cbindgen;

use std::{env, path::PathBuf};

#[allow(clippy::expect_used)] // OK in build script
fn main() {
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
// Check if the 'native' feature is not enabled
let is_native_feature_on = env::var("CARGO_FEATURE_NATIVE").is_ok();

#[cfg(feature = "ffi")]
if !is_native_feature_on {
extern crate cbindgen;
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");
// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/backtest.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);
let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/backtest.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");
// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/backtest.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path);
let cython_path = crate_dir.join("../../nautilus_trader/core/rust/backtest.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path);
}
}
1 change: 1 addition & 0 deletions nautilus_core/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ python = ["pyo3"]
stubs = ["rstest"]
redis = ["dep:redis"]
default = ["ffi", "python", "redis"]
native = []

[build-dependencies]
cbindgen = { workspace = true, optional = true }
76 changes: 41 additions & 35 deletions nautilus_core/common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

extern crate cbindgen;

use std::{
env,
fs::File,
Expand All @@ -24,37 +22,45 @@ use std::{

#[allow(clippy::expect_used)] // OK in build script
fn main() {
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/common.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/common.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
// Check if the 'native' feature is not enabled
let is_native_feature_on = env::var("CARGO_FEATURE_NATIVE").is_ok();

#[cfg(feature = "ffi")]
if !is_native_feature_on {
extern crate cbindgen;

let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/common.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/common.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
}
}
1 change: 1 addition & 0 deletions nautilus_core/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension-module = ["pyo3/extension-module"]
ffi = ["cbindgen"]
python = ["pyo3"]
default = ["ffi", "python"]
native = []

[dev-dependencies]
criterion = { workspace = true }
Expand Down
75 changes: 40 additions & 35 deletions nautilus_core/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

extern crate cbindgen;

use std::{
env,
fs::File,
Expand All @@ -24,37 +22,44 @@ use std::{

#[allow(clippy::expect_used)] // OK in build script
fn main() {
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/core.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/core.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
// Check if the 'native' feature is not enabled
let is_native_feature_on = env::var("CARGO_FEATURE_NATIVE").is_ok();

#[cfg(feature = "ffi")]
if !is_native_feature_on {
extern crate cbindgen;
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/core.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/core.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
}
}
1 change: 1 addition & 0 deletions nautilus_core/indicators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ extension-module = [
]
python = ["pyo3"]
default = ["python"]
native = []
1 change: 1 addition & 0 deletions nautilus_core/infrastructure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ extension-module = [
python = ["pyo3"]
redis = ["dep:redis"]
default = ["python", "redis"]
native = []
7 changes: 4 additions & 3 deletions nautilus_core/model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "nautilus_model"
crate-type = ["rlib", "staticlib"]

[dependencies]
nautilus-core = { path = "../core" }
nautilus-core = { path = "../core" , optional = true}
anyhow = { workspace = true }
indexmap = { workspace = true }
once_cell = { workspace = true }
Expand All @@ -35,11 +35,12 @@ extension-module = [
"pyo3/extension-module",
"nautilus-core/extension-module",
]
ffi = ["cbindgen"]
python = ["pyo3"]
ffi = ["cbindgen", "nautilus-core/ffi"]
python = ["pyo3", "pyo3/multiple-pymethods","nautilus-core/python"]
stubs = ["rstest"]
trivial_copy = [] # Enables deriving the `Copy` trait for data types (should be included in default)
default = ["ffi", "python", "stubs", "trivial_copy"]
native = ["nautilus-core/native","trivial_copy"]

[dev-dependencies]
criterion = { workspace = true }
Expand Down
79 changes: 43 additions & 36 deletions nautilus_core/model/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,55 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

extern crate cbindgen;

use std::env;
#[cfg(feature = "ffi")]
use std::{
env,
fs::File,
io::{Read, Write},
path::PathBuf,
};

#[allow(clippy::expect_used)] // OK in build script
fn main() {
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/model.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/model.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
// Check if the 'native' feature is not enabled
let _is_native_feature_on = env::var("CARGO_FEATURE_NATIVE").is_ok();

#[cfg(feature = "ffi")]
if !_is_native_feature_on {
extern crate cbindgen;

let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Generate C headers
let config_c = cbindgen::Config::from_file("cbindgen.toml")
.expect("unable to find cbindgen.toml configuration file");

let c_header_path = crate_dir.join("../../nautilus_trader/core/includes/model.h");
cbindgen::generate_with_config(&crate_dir, config_c)
.expect("unable to generate bindings")
.write_to_file(c_header_path);

// Generate Cython definitions
let config_cython = cbindgen::Config::from_file("cbindgen_cython.toml")
.expect("unable to find cbindgen_cython.toml configuration file");

let cython_path = crate_dir.join("../../nautilus_trader/core/rust/model.pxd");
cbindgen::generate_with_config(&crate_dir, config_cython)
.expect("unable to generate bindings")
.write_to_file(cython_path.clone());

// Open and read the file entirely
let mut src = File::open(cython_path.clone()).expect("`File::open` failed");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("invalid UTF-8 in stream");

// Run the replace operation in memory
let new_data = data.replace("cdef enum", "cpdef enum");

// Recreate the file and dump the processed contents to it
let mut dst = File::create(cython_path).expect("`File::create` failed");
dst.write_all(new_data.as_bytes())
.expect("I/O error on `dist.write`");
}
}
Loading

0 comments on commit f18fc81

Please sign in to comment.