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

Refine ffi and python separation and feature flagging #1498

Merged
merged 9 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions nautilus_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions nautilus_core/accounting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ name = "nautilus_accounting"
crate-type = ["rlib", "cdylib"]

[dependencies]
nautilus-common = { path = "../common", features = ["stubs"] }
nautilus-model = { path = "../model", features = ["stubs"]}
nautilus-common = { path = "../common", features = ["stubs"]}
nautilus-model = { path = "../model" }
nautilus-core = { path = "../core" }
anyhow = { workspace = true }
pyo3 = { workspace = true, optional = true }
Expand All @@ -21,16 +21,17 @@ serde = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
rstest.workspace = true
rstest = { workspace = true }

[build-dependencies]
cbindgen = { workspace = true, optional = true }

[features]
extension-module = [
"pyo3/extension-module",
"nautilus-core/extension-module",
"nautilus-model/extension-module",
"nautilus-common/extension-module",
]
python = ["pyo3"]
default = ["python"]

[build-dependencies]
cbindgen = { workspace = true, optional = true }
default = []
1 change: 0 additions & 1 deletion nautilus_core/accounting/src/account/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use rust_decimal::prelude::ToPrimitive;
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
)]
pub struct BaseAccount {
#[pyo3(get)]
pub id: AccountId,
pub account_type: AccountType,
pub base_currency: Option<Currency>,
Expand Down
6 changes: 6 additions & 0 deletions nautilus_core/accounting/src/python/cash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl CashAccount {
)
}

#[getter]
#[pyo3(name = "id")]
fn py_id(&self) -> AccountId {
self.id
}

#[getter]
#[pyo3(name = "base_currency")]
fn py_base_currency(&self) -> Option<Currency> {
Expand Down
23 changes: 15 additions & 8 deletions nautilus_core/adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["rlib", "staticlib", "cdylib"]
[dependencies]
nautilus-common = { path = "../common" }
nautilus-core = { path = "../core" }
nautilus-model = { path = "../model", features = ["stubs"]}
nautilus-model = { path = "../model", features = ["stubs"] }
anyhow = { workspace = true }
chrono = { workspace = true }
indexmap = { workspace = true }
Expand All @@ -35,17 +35,24 @@ dbn = { version = "0.15.1", optional = true, features = ["python"] }
streaming-iterator = "0.1.9"
time = "0.3.31"

[dev-dependencies]
criterion = { workspace = true }
rstest = { workspace = true }

[features]
extension-module = [
"pyo3/extension-module",
"nautilus-common/extension-module",
"nautilus-core/extension-module",
"nautilus-model/extension-module",
]
databento = ["dep:databento", "dbn"]
python = ["pyo3", "pyo3-asyncio"]
default = ["databento", "python"]

[dev-dependencies]
criterion = { workspace = true }
rstest = { workspace = true }
databento = ["dep:databento", "dbn", "python"]
ffi = ["nautilus-core/ffi", "nautilus-model/ffi", "nautilus-common/ffi"]
python = [
"pyo3",
"pyo3-asyncio",
"nautilus-core/python",
"nautilus-model/python",
"nautilus-common/python",
]
default = ["ffi", "python"]
10 changes: 5 additions & 5 deletions nautilus_core/backtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ ustr = { workspace = true }
tempfile = { workspace = true }
rstest = { workspace = true}

[build-dependencies]
cbindgen = { workspace = true, optional = true }

[features]
extension-module = [
"pyo3/extension-module",
"nautilus-common/extension-module",
"nautilus-core/extension-module",
"nautilus-model/extension-module",
]
ffi = ["cbindgen"]
python = ["pyo3"]
ffi = ["cbindgen", "nautilus-core/ffi", "nautilus-common/ffi"]
python = ["pyo3", "nautilus-core/python", "nautilus-common/python"]
default = ["ffi", "python"]

[build-dependencies]
cbindgen = { workspace = true, optional = true }
41 changes: 23 additions & 18 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};
use std::env;

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

#[cfg(feature = "ffi")]
if !_is_ffi_feature_on {
extern crate cbindgen;
use std::path::PathBuf;
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: 0 additions & 1 deletion nautilus_core/backtest/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ impl Default for TimeEventAccumulator {
////////////////////////////////////////////////////////////////////////////////
// C API
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "ffi")]
#[repr(C)]
pub struct TimeEventAccumulatorAPI(Box<TimeEventAccumulator>);

Expand Down
17 changes: 9 additions & 8 deletions nautilus_core/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["rlib", "staticlib"]

[dependencies]
nautilus-core = { path = "../core" }
nautilus-model = { path = "../model", features = ["stubs"]}
nautilus-model = { path = "../model" }
anyhow = { workspace = true }
chrono = { workspace = true }
indexmap = { workspace = true }
Expand All @@ -32,19 +32,20 @@ sysinfo = "0.30.5"
tracing-subscriber = { version = "0.3.18", default-features = false, features = ["smallvec", "fmt", "ansi", "std", "env-filter"] }

[dev-dependencies]
rstest = { workspace = true }
tempfile = { workspace = true }

[build-dependencies]
cbindgen = { workspace = true, optional = true }

[features]
extension-module = [
"pyo3/extension-module",
"nautilus-core/extension-module",
"nautilus-model/extension-module",
]
ffi = ["cbindgen"]
python = ["pyo3", "pyo3-asyncio"]
stubs = ["rstest"]
ffi = ["cbindgen", "nautilus-core/ffi", "nautilus-model/ffi"]
python = ["pyo3", "pyo3-asyncio", "nautilus-core/python", "nautilus-model/python"]
stubs = ["rstest", "nautilus-model/stubs"]
redis = ["dep:redis"]
default = ["ffi", "python", "redis"]

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

extern crate cbindgen;

use std::{
env,
fs::File,
io::{Read, Write},
path::PathBuf,
};
use std::env;

#[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`");
let _is_ffi_feature_on = env::var("CARGO_FEATURE_FFI").is_ok();

#[cfg(feature = "ffi")]
if !_is_ffi_feature_on {
extern crate cbindgen;
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
};

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`");
}
}
13 changes: 7 additions & 6 deletions nautilus_core/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ ustr = { workspace = true }
uuid = { workspace = true }
heck = "0.4.1"

[features]
extension-module = ["pyo3/extension-module"]
ffi = ["cbindgen"]
python = ["pyo3"]
default = ["ffi", "python"]

[dev-dependencies]
criterion = { workspace = true }
iai = { workspace = true }
rstest = { workspace = true }

[build-dependencies]
cbindgen = { workspace = true, optional = true }

[features]
extension-module = ["pyo3/extension-module"]
ffi = ["cbindgen"]
python = ["pyo3"]
default = []

Loading