Skip to content

Commit

Permalink
rust native compile
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmacek committed Feb 27, 2024
1 parent b9bfe4a commit 895694e
Show file tree
Hide file tree
Showing 43 changed files with 1,429 additions and 561 deletions.
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.

6 changes: 3 additions & 3 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", optional = true , features = ["stubs"]}
nautilus-model = { path = "../model", optional = true }
nautilus-core = { path = "../core" }
anyhow = { workspace = true }
pyo3 = { workspace = true, optional = true }
Expand All @@ -30,7 +30,7 @@ extension-module = [
"nautilus-common/extension-module",
]
python = ["pyo3"]
default = ["python"]
default = ["nautilus-model", "nautilus-common"]

[build-dependencies]
cbindgen = { workspace = true, optional = true }
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
4 changes: 2 additions & 2 deletions nautilus_core/adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ extension-module = [
"nautilus-core/extension-module",
"nautilus-model/extension-module",
]
databento = ["dep:databento", "dbn"]
databento = ["dep:databento", "dbn", "python"]
python = ["pyo3", "pyo3-asyncio"]
default = ["databento", "python"]
default = []

[dev-dependencies]
criterion = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions nautilus_core/backtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ name = "nautilus_backtest"
crate-type = ["rlib", "staticlib"]

[dependencies]
nautilus-common = { path = "../common" }
nautilus-core = { path = "../core" }
nautilus-common = { path = "../common" , features = ["ffi","python"]}
nautilus-core = { path = "../core", features = ["ffi","python"] }
nautilus-model = { path = "../model" }
pyo3 = { workspace = true, optional = true }
ustr = { workspace = true }
Expand All @@ -30,7 +30,8 @@ extension-module = [
]
ffi = ["cbindgen"]
python = ["pyo3"]
default = ["ffi", "python"]
default = ["python"]


[build-dependencies]
cbindgen = { workspace = true, optional = true }
42 changes: 24 additions & 18 deletions nautilus_core/backtest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,35 @@
// 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"));
// Check if the 'native' feature is not enabled
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
12 changes: 6 additions & 6 deletions nautilus_core/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ name = "nautilus_common"
crate-type = ["rlib", "staticlib"]

[dependencies]
nautilus-core = { path = "../core" }
nautilus-model = { path = "../model", features = ["stubs"]}
nautilus-core = { path = "../core", optional = true }
nautilus-model = { path = "../model", optional = true}
anyhow = { workspace = true }
chrono = { workspace = true }
indexmap = { workspace = true }
Expand Down Expand Up @@ -40,11 +40,11 @@ extension-module = [
"nautilus-core/extension-module",
"nautilus-model/extension-module",
]
ffi = ["cbindgen"]
python = ["pyo3", "pyo3-asyncio"]
stubs = ["rstest"]
python = ["pyo3", "pyo3-asyncio", "nautilus-core/python", "nautilus-model/python"]
ffi = ["cbindgen", "nautilus-core/ffi", "nautilus-model/ffi"]
stubs = ["rstest", "nautilus-model/stubs"]
redis = ["dep:redis"]
default = ["ffi", "python", "redis"]
default = ["nautilus-core","nautilus-model"]

[build-dependencies]
cbindgen = { workspace = true, optional = true }
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`");
}
}
2 changes: 1 addition & 1 deletion nautilus_core/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ heck = "0.4.1"
extension-module = ["pyo3/extension-module"]
ffi = ["cbindgen"]
python = ["pyo3"]
default = ["ffi", "python"]
default = []

[dev-dependencies]
criterion = { workspace = true }
Expand Down
86 changes: 45 additions & 41 deletions nautilus_core/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,52 @@
// 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/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`");
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/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`");
}
}
2 changes: 1 addition & 1 deletion nautilus_core/indicators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ extension-module = [
"nautilus-model/extension-module",
]
python = ["pyo3"]
default = ["python"]
default = []
Loading

0 comments on commit 895694e

Please sign in to comment.