Skip to content

Commit

Permalink
Allow building as a .so/.dll, remove headers needed for bindgen support
Browse files Browse the repository at this point in the history
  • Loading branch information
redstrate committed Apr 28, 2024
1 parent 02521b0 commit 72ba83e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ publish = false

[lib]
name = "physis"
crate-type = ["staticlib"]
crate-type = ["cdylib", "staticlib"]

[profile.release]
lto = true
Expand All @@ -21,7 +21,8 @@ lto = true
cbindgen = { version = "0.26.0", default-features = false }

[features]
default = ["visual_data"]
default = ["visual_data", "logging"]
logging = []

game_install = ["physis/game_install"]
visual_data = ["physis/visual_data"]
Expand Down
26 changes: 26 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
extern crate cbindgen;

use std::env;
use std::fs;
use std::io::BufWriter;
use std::io::BufReader;
use std::fs::File;
use std::io::Write;
use std::io::BufRead;

use cbindgen::Language;

Expand All @@ -15,6 +21,7 @@ fn main() {
.with_parse_deps(true)
.with_parse_include(&["physis"])
.with_language(Language::C)
.with_define("feature", "logging", "ENABLE_LOGGING")
.generate()
.expect("Unable to generate C bindings")
.write_to_file("target/public/physis.h");
Expand All @@ -25,7 +32,26 @@ fn main() {
.with_parse_include(&["physis"])
.with_language(Language::Cxx)
.with_pragma_once(true)
.with_define("feature", "logging", "ENABLE_LOGGING")
.generate()
.expect("Unable to generate C++ bindings")
.write_to_file("target/public/physis.hpp");

// cbindgen always includes <ostream> and <new> even if they aren't used
// some downstream projects like PhysisSharp need to have a cleaner file
{
let mut file: File = File::open("target/public/physis.hpp").unwrap();
let mut out_file: File = File::create("target/public/physis.hpp.temp").unwrap();

let reader = BufReader::new(&file);
let mut writer = BufWriter::new(&out_file);

for (index, line) in reader.lines().enumerate() {
let line = line.as_ref().unwrap();
if !line.contains("#include <ostream>") && !line.contains("#include <new>") {
writeln!(writer, "{}", line);
}
}
}
fs::rename("target/public/physis.hpp.temp", "target/public/physis.hpp").unwrap();
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@ mod dic;

mod index;

#[cfg(feature = "logging")]
mod logging;

0 comments on commit 72ba83e

Please sign in to comment.