From fbb1438cda6e53695a3ba182e5b8932836dce645 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Wed, 18 Apr 2018 14:29:42 -0600 Subject: [PATCH] static linking --- Chapter05/.gitignore | 1 + Chapter05/Cargo.toml | 2 ++ Chapter05/README.md | 3 +++ Chapter05/build.rs | 33 ++++++++++++++++++++++++++++++ Chapter05/src/lib.rs | 11 +++++----- Chapter05/src/motor1.c | 6 ++++++ Chapter05/src/motor2.c | 6 ++++++ Chapter05/src/motor3.c | 6 ++++++ Chapter05/src/motor_controllers.rs | 16 +++++++++++++++ Chapter05/src/simulate_trip.rs | 12 +++++++++++ 10 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 Chapter05/.gitignore create mode 100644 Chapter05/README.md create mode 100644 Chapter05/build.rs create mode 100644 Chapter05/src/motor1.c create mode 100644 Chapter05/src/motor2.c create mode 100644 Chapter05/src/motor3.c diff --git a/Chapter05/.gitignore b/Chapter05/.gitignore new file mode 100644 index 0000000..2dd8f0d --- /dev/null +++ b/Chapter05/.gitignore @@ -0,0 +1 @@ +src/*.o diff --git a/Chapter05/Cargo.toml b/Chapter05/Cargo.toml index bc93506..90fa5e0 100644 --- a/Chapter05/Cargo.toml +++ b/Chapter05/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "elevator" version = "1.0.0" +build = "build.rs" [dependencies] floating-duration = "0.1.2" @@ -9,6 +10,7 @@ timebomb = "0.1" serde = "1.0" serde_json = "1.0" serde_derive = "1.0" +libc = "0.2" [[bin]] name = "simulate_trip" diff --git a/Chapter05/README.md b/Chapter05/README.md new file mode 100644 index 0000000..58fdfc9 --- /dev/null +++ b/Chapter05/README.md @@ -0,0 +1,3 @@ +Linked object files are platform dependent. The C files and headers are provided, and a build/link script is provided. +The build script is somewhat platform dependent and requires GCC to be installed. It may be necessary to adjust build.rs +to compile this Chapter's code. diff --git a/Chapter05/build.rs b/Chapter05/build.rs new file mode 100644 index 0000000..3ef607f --- /dev/null +++ b/Chapter05/build.rs @@ -0,0 +1,33 @@ +use std::process::Command; +use std::env; +use std::path::Path; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + + Command::new("gcc").args(&["src/motor1.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/motor1.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libmotor1.a", "motor1.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + + Command::new("gcc").args(&["src/motor2.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/motor2.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libmotor2.a", "motor2.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + + Command::new("gcc").args(&["src/motor3.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/motor3.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libmotor3.a", "motor3.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + + println!("cargo:rustc-link-search=native={}", out_dir); + println!("cargo:rustc-link-lib=static=motor1"); + println!("cargo:rustc-link-lib=static=motor2"); + println!("cargo:rustc-link-lib=static=motor3"); +} diff --git a/Chapter05/src/lib.rs b/Chapter05/src/lib.rs index 1e97bfe..ff2a4ec 100644 --- a/Chapter05/src/lib.rs +++ b/Chapter05/src/lib.rs @@ -1,6 +1,7 @@ +extern crate libc; -mod motor_controllers; -mod elevator_drivers; -mod buildings; -mod physics; -mod trip_planning; +pub mod motor_controllers; +pub mod elevator_drivers; +pub mod buildings; +pub mod physics; +pub mod trip_planning; diff --git a/Chapter05/src/motor1.c b/Chapter05/src/motor1.c new file mode 100644 index 0000000..2e974d7 --- /dev/null +++ b/Chapter05/src/motor1.c @@ -0,0 +1,6 @@ + +int motor1_adjust_motor(int target_force){ + //real driver would do something here + //interface with physical components + return 0; +} diff --git a/Chapter05/src/motor2.c b/Chapter05/src/motor2.c new file mode 100644 index 0000000..1a15ae2 --- /dev/null +++ b/Chapter05/src/motor2.c @@ -0,0 +1,6 @@ + +int motor2_adjust_motor(int target_force){ + //real driver would do something here + //interface with physical components + return 0; +} diff --git a/Chapter05/src/motor3.c b/Chapter05/src/motor3.c new file mode 100644 index 0000000..69611d6 --- /dev/null +++ b/Chapter05/src/motor3.c @@ -0,0 +1,6 @@ + +int motor3_adjust_motor(int target_force){ + //real driver would do something here + //interface with physical components + return 0; +} diff --git a/Chapter05/src/motor_controllers.rs b/Chapter05/src/motor_controllers.rs index e69de29..6d6a2a5 100644 --- a/Chapter05/src/motor_controllers.rs +++ b/Chapter05/src/motor_controllers.rs @@ -0,0 +1,16 @@ +use libc::c_int; + +#[link(name = "motor1")] +extern { + pub fn motor1_adjust_motor(target_force: c_int) -> c_int; +} + +#[link(name = "motor2")] +extern { + pub fn motor2_adjust_motor(target_force: c_int) -> c_int; +} + +#[link(name = "motor3")] +extern { + pub fn motor3_adjust_motor(target_force: c_int) -> c_int; +} diff --git a/Chapter05/src/simulate_trip.rs b/Chapter05/src/simulate_trip.rs index ea0a26d..39a929c 100644 --- a/Chapter05/src/simulate_trip.rs +++ b/Chapter05/src/simulate_trip.rs @@ -1,3 +1,15 @@ +extern crate elevator; +use elevator::motor_controllers::{motor1_adjust_motor,motor2_adjust_motor,motor3_adjust_motor}; fn main(){ + unsafe { + println!("motor1 adjust"); + motor1_adjust_motor(0); + + println!("motor2 adjust"); + motor2_adjust_motor(0); + + println!("motor3 adjust"); + motor3_adjust_motor(0); + } }