Skip to content

Commit

Permalink
Build JoltC and Jolt using CMake instead of cc directly
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Apr 20, 2024
1 parent aaff39c commit 58972fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 60 deletions.
2 changes: 1 addition & 1 deletion crates/jolt-sys/JoltC
Submodule JoltC updated 1 files
+12 −9 CMakeLists.txt
83 changes: 24 additions & 59 deletions crates/jolt-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,58 @@ use std::env;
use std::path::Path;

use anyhow::Context;
use walkdir::WalkDir;

fn main() {
let flags = build_flags();

build_jolt(&flags);
build_joltc(&flags);
build_joltc();

link();

generate_bindings(&flags).unwrap();
}

fn build_flags() -> Vec<(&'static str, &'static str)> {
let mut flags = Vec::new();
fn build_joltc() {
let mut config = cmake::Config::new("JoltC");

flags.push(("JPH_DEBUG_RENDERER", "ON"));
if cfg!(windows) {
config.cxxflag("/EHsc");
}

if cfg!(feature = "double-precision") {
flags.push(("JPC_DOUBLE_PRECISION", "ON"));
flags.push(("JPH_DOUBLE_PRECISION", "ON"));
config.configure_arg("-DDOUBLE_PRECISION=ON");
}

if cfg!(feature = "object-layer-u32") {
flags.push(("JPC_OBJECT_LAYER_BITS", "32"));
flags.push(("JPH_OBJECT_LAYER_BITS", "32"));
config.configure_arg("-DOBJECT_LAYER_BITS=32");
}

flags
}

fn build_joltc(flags: &[(&'static str, &'static str)]) {
let mut build = cc::Build::new();

for entry in WalkDir::new("JoltC/JoltC") {
let entry = entry.unwrap();
let file_name = entry
.file_name()
.to_str()
.expect("file was not valid UTF-8");

if file_name.ends_with(".cpp") {
build.file(entry.path());
}
}
let dst = config.build();

for (key, value) in flags {
build.define(key, *value);
}
println!("cargo:rustc-link-search=native={}", dst.display());
}

build
.std("c++17")
.include("JoltC")
.include("JoltC/JoltPhysics")
.cpp(true)
.compile("JoltC");
fn link() {
println!("cargo:rustc-link-lib=Jolt");
println!("cargo:rustc-link-lib=joltc");
}

fn build_jolt(flags: &[(&'static str, &'static str)]) {
let mut build = cc::Build::new();
fn build_flags() -> Vec<(&'static str, &'static str)> {
let mut flags = Vec::new();

for entry in WalkDir::new("JoltC/JoltPhysics/Jolt") {
let entry = entry.unwrap();
let file_name = entry
.file_name()
.to_str()
.expect("file was not valid UTF-8");
flags.push(("JPH_DEBUG_RENDERER", "ON"));

if file_name.ends_with(".cpp") {
build.file(entry.path());
}
if cfg!(feature = "double-precision") {
flags.push(("JPC_DOUBLE_PRECISION", "ON"));
flags.push(("JPH_DOUBLE_PRECISION", "ON"));
}

for (key, value) in flags {
build.define(key, *value);
if cfg!(feature = "object-layer-u32") {
flags.push(("JPC_OBJECT_LAYER_BITS", "32"));
flags.push(("JPH_OBJECT_LAYER_BITS", "32"));
}

build
.std("c++17")
.include("JoltC/JoltPhysics")
.cpp(true)
.compile("Jolt");
}

fn link() {
println!("cargo:rustc-link-lib=Jolt");
println!("cargo:rustc-link-lib=JoltC");
flags
}

fn generate_bindings(flags: &[(&'static str, &'static str)]) -> anyhow::Result<()> {
Expand Down

0 comments on commit 58972fb

Please sign in to comment.