#![allow(dead_code)]
use std::{env, path::PathBuf};
struct Environment {
host: String,
emit_debug_info: bool,
target_compiler: Option<String>,
target_os: String,
target_env: Option<String>,
mode: String,
static_crt: bool,
}
struct Context {
root: PathBuf,
builder: cc::Build,
env: Environment,
includes: Vec<PathBuf>,
}
impl Context {
fn add_includes(&mut self, rel_root: &str, includes: &[&str]) -> &mut Self {
let root = self.root.join(rel_root);
self.includes
.extend(includes.iter().map(|inc| root.join(inc)));
self
}
fn add_sources(&mut self, rel_root: &str, files: &[&str]) -> &mut Self {
let root = self.root.join(rel_root);
self.builder.files(files.iter().map(|src| {
let mut p = root.join(src);
p.set_extension("cpp");
p
}));
self.includes.push(root);
self
}
fn add_component(&mut self, name: &str, sources: &[&str]) -> &mut Self {
let src_dir = format!("source/{}/src", name);
self.add_sources(&src_dir, sources);
let mut comproot = self.root.join("source");
comproot.push(name);
self.includes.push(comproot.join("include"));
self
}
}
macro_rules! component {
($name:ident) => {
fn $name(ctx: &mut Context) {
let sources = include!(concat!("sources/", stringify!($name)));
ctx.add_component(stringify!($name), &sources);
}
};
}
component! {common}
component! {fastxml}
component! {lowlevelaabb}
component! {lowleveldynamics}
component! {physxcharacterkinematic}
component! {pvd}
component! {scenequery}
component! {simulationcontroller}
component! {task}
fn foundation(ctx: &mut Context) {
let sources = include!("sources/foundation");
ctx.add_component("foundation", &sources);
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("TARGET_FAMILY not specified");
let sources = match target_family.as_str() {
"unix" => &include!("sources/foundation_unix"),
"windows" => &include!("sources/foundation_windows"),
other => panic!("unknown TARGET_FAMILY '{}'", other),
};
ctx.add_sources(&format!("source/foundation/src/{}", target_family), sources);
}
fn lowlevel(ctx: &mut Context) {
ctx.builder
.file(ctx.root.join("source/lowlevel/api/src/px_globals.cpp"));
{
let sources = include!("sources/lowlevel_pipeline");
ctx.add_sources("source/lowlevel/common/src/pipeline", &sources);
}
{
let sources = include!("sources/lowlevel_software");
ctx.add_sources("source/lowlevel/software/src", &sources);
}
ctx.add_includes(
"source/lowlevel",
&[
"api/include",
"common/include/collision",
"common/include/pipeline",
"common/include/utils",
"software/include",
],
);
}
fn vehicle(ctx: &mut Context) {
let sources = include!("sources/vehicle");
ctx.add_component("physxvehicle", &sources);
let sources = include!("sources/vehicle_metadata");
ctx.add_sources("source/physxvehicle/src/physxmetadata/src", &sources);
ctx.add_includes("source/physxvehicle/src/physxmetadata", &["include"]);
}
fn extensions(ctx: &mut Context) {
let sources = include!("sources/extensions");
ctx.add_component("physxextensions", &sources);
ctx.add_sources(
"source/physxmetadata/extensions/src",
&["PxExtensionAutoGeneratedMetaDataObjects"],
);
let sources = include!("sources/extensions_serialization");
ctx.add_sources("source/physxextensions/src/serialization", &sources);
ctx.add_includes("source/physxextensions/src/serialization", &["File"]);
let sources = include!("sources/extensions_xml");
ctx.add_sources("source/physxextensions/src/serialization/Xml", &sources);
let sources = include!("sources/extensions_binary");
ctx.add_sources("source/physxextensions/src/serialization/Binary", &sources);
ctx.add_includes("source/physxmetadata/extensions", &["include"]);
}
fn geomutils(ctx: &mut Context) {
let sources = include!("sources/geomutils");
ctx.add_sources("source/geomutils/src", &sources);
ctx.add_includes("source/geomutils", &["include"]);
let sources = include!("sources/geomutils_ccd");
ctx.add_sources("source/geomutils/src/ccd", &sources);
let sources = include!("sources/geomutils_common");
ctx.add_sources("source/geomutils/src/common", &sources);
let sources = include!("sources/geomutils_contact");
ctx.add_sources("source/geomutils/src/contact", &sources);
let sources = include!("sources/geomutils_convex");
ctx.add_sources("source/geomutils/src/convex", &sources);
let sources = include!("sources/geomutils_distance");
ctx.add_sources("source/geomutils/src/distance", &sources);
let sources = include!("sources/geomutils_gjk");
ctx.add_sources("source/geomutils/src/gjk", &sources);
let sources = include!("sources/geomutils_hf");
ctx.add_sources("source/geomutils/src/hf", &sources);
let sources = include!("sources/geomutils_intersection");
ctx.add_sources("source/geomutils/src/intersection", &sources);
let sources = include!("sources/geomutils_mesh");
ctx.add_sources("source/geomutils/src/mesh", &sources);
let sources = include!("sources/geomutils_pcm");
ctx.add_sources("source/geomutils/src/pcm", &sources);
let sources = include!("sources/geomutils_sweep");
ctx.add_sources("source/geomutils/src/sweep", &sources);
}
fn cooking(ctx: &mut Context) {
let sources = include!("sources/cooking");
ctx.add_sources("source/physxcooking/src", &sources);
ctx.add_includes("source/include", &["cooking"]);
let sources = include!("sources/cooking_mesh");
ctx.add_sources("source/physxcooking/src/mesh", &sources);
let sources = include!("sources/cooking_convex");
ctx.add_sources("source/physxcooking/src/convex", &sources);
ctx.builder.define("PX_COOKING", None);
}
fn physx(ctx: &mut Context) {
{
let sources = ["PxAutoGeneratedMetaDataObjects", "PxMetaDataObjects"];
ctx.add_sources("source/physxmetadata/core/src", &sources);
ctx.add_includes("source", &["physxmetadata/core/include"]);
}
ctx.builder.file(
ctx.root
.join("source/immediatemode/src/NpImmediateMode.cpp"),
);
let sources = include!("sources/buffering");
ctx.add_sources("source/physx/src/buffering", &sources);
let sources = include!("sources/core");
ctx.add_sources("source/physx/src", &sources);
}
fn add_common(ctx: &mut Context) {
let shared_root = ctx.root.parent().unwrap().join("pxshared");
let builder = &mut ctx.builder;
let ccenv = &ctx.env;
let root = &ctx.root;
builder.cpp(true);
if ccenv.target_os == "android" {
builder.define("ANDROID", None);
let ndk_path = PathBuf::from(
env::var("ANDROID_NDK_ROOT")
.expect("environment variable \"ANDROID_NDK_ROOT\" has not been set"),
);
let host_str = ccenv.host.as_str();
let ndk_toolchain = match host_str {
"x86_64-pc-windows-msvc" => "windows-x86_64",
"x86_64-unknown-linux-gnu" => "linux-x86_64",
"x86_64-apple-darwin" => "darwin-x86_64",
_ => panic!(
"Host triple {} is unsupported for cross-compilation to Android",
host_str
),
};
let sysroot_path = ndk_path
.join("toolchains/llvm/prebuilt")
.join(ndk_toolchain)
.join("sysroot");
if !sysroot_path.exists() {
panic!(
"Can't find Android NDK sysroot path \"{}\"",
sysroot_path.to_str().unwrap()
);
}
builder.flag(&format!("--sysroot={}", &sysroot_path.to_str().unwrap()));
builder.cpp_link_stdlib("c++");
}
ctx.includes.push(shared_root.join("include"));
ctx.includes.extend(
[
"include",
"source/foundation/include",
"source/common/src",
"source/filebuf/include", ]
.iter()
.map(|inc| root.join(inc)),
);
if ccenv.target_env.as_deref() == Some("msvc") {
builder
.define("_CRT_SECURE_NO_WARNINGS", None)
.define("_WINSOCK_DEPRECATED_NO_WARNINGS", None)
.define("_ITERATOR_DEBUG_LEVEL", "0");
}
builder.define("PX_PHYSX_STATIC_LIB", None);
builder.define("DISABLE_CUDA_PHYSX", None);
if ccenv.emit_debug_info {
builder.define("PX_DEBUG", None).define("PX_CHECKED", None);
}
builder.define("PX_SUPPORT_PVD", "1");
if cfg!(feature = "profile") {
builder.define("PX_PROFILE", "1");
}
if (ccenv.host.contains("-linux-") || ccenv.host == "x86_64-apple-darwin")
&& ccenv.target_compiler.is_none()
{
builder.compiler("clang++");
}
let flags = if builder.get_compiler().is_like_clang() {
vec![
"-std=c++14",
"-ferror-limit=0",
"-Wall",
"-Wextra",
"-Wstrict-aliasing=2",
"-Wno-everything",
]
} else if builder.get_compiler().is_like_gnu() {
vec![
"-std=c++14",
"-Wall",
"-Wextra",
"-Wstrict-aliasing=2",
"-w",
]
} else if builder.get_compiler().is_like_msvc() {
builder.no_default_flags(true);
let mut flags = vec!["-nologo", "/MP"];
if ccenv.static_crt {
flags.push("/MT");
} else {
flags.push("/MD");
}
if ccenv.emit_debug_info {
flags.push("/Z7");
}
if ccenv.mode.as_str() == "profile" {
flags.push("/O2");
}
flags.push("/std:c++14");
flags.extend(["/W4", "/GF", "/GS-", "/GR-", "/Gd"].iter());
flags.extend(
[
"/wd4514", "/wd4820", "/wd4127", "/wd4710", "/wd4711", "/wd4577", "/wd4996",
"/wd4723", ]
.iter(),
);
flags
} else {
vec![]
};
for flag in flags {
builder.flag(flag);
}
builder.define("NDEBUG", "1");
if builder.get_compiler().is_like_clang() && ccenv.target_os == "windows" {
builder.pic(false);
}
}
fn cc_compile(target_env: Environment) {
let root = env::current_dir().unwrap().join("PhysX/physx");
let ccenv = target_env;
let mut ctx = Context {
builder: cc::Build::new(),
root,
env: ccenv,
includes: Vec::with_capacity(1000),
};
add_common(&mut ctx);
fastxml(&mut ctx);
task(&mut ctx);
foundation(&mut ctx);
lowlevel(&mut ctx);
lowlevelaabb(&mut ctx);
lowleveldynamics(&mut ctx);
vehicle(&mut ctx);
extensions(&mut ctx);
physxcharacterkinematic(&mut ctx);
common(&mut ctx);
geomutils(&mut ctx);
cooking(&mut ctx);
pvd(&mut ctx);
physx(&mut ctx);
scenequery(&mut ctx);
simulationcontroller(&mut ctx);
ctx.includes.push(ctx.root.join("source/pvd/include"));
ctx.includes.sort();
ctx.includes.dedup();
for dir in ctx.includes {
ctx.builder.include(dir);
}
ctx.builder.compile("physx");
}
fn main() {
let build_mode = match env::var("OPT_LEVEL")
.ok()
.and_then(|s| s.parse::<i32>().ok())
.unwrap_or(1)
{
0 => "debug",
_ => "profile",
};
let target = env::var("TARGET").expect("TARGET not specified");
let host = env::var("HOST").expect("HOST not specified");
let compiler = {
env::var(format!("CXX_{}", target))
.or_else(|_| {
let target_under = target.replace('-', "_");
env::var(format!("CXX_{}", target_under))
})
.or_else(|_| env::var("TARGET_CXX"))
.or_else(|_| env::var("CXX"))
.ok()
};
{
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target os not specified");
let target_env = env::var("CARGO_CFG_TARGET_ENV").ok();
let static_crt = env::var("CARGO_CFG_TARGET_FEATURE")
.unwrap_or_default()
.contains("crt-static");
let environment = Environment {
emit_debug_info: env::var("DEBUG")
.ok()
.and_then(|s| s.parse::<bool>().ok())
.unwrap_or(false),
target_compiler: compiler.clone(),
target_os,
target_env,
mode: build_mode.to_owned(),
host: host.clone(),
static_crt,
};
cc_compile(environment);
}
let mut cc_builder = cc::Build::new();
let physx_cc = cc_builder
.cpp(true)
.opt_level(3)
.debug(false)
.use_plt(false)
.warnings(false)
.extra_warnings(false)
.define("NDEBUG", None)
.define("PX_PHYSX_STATIC_LIB", None)
.include("PhysX/physx/include")
.include("PhysX/pxshared/include")
.include("PhysX/physx/source/foundation/include");
if cfg!(feature = "profile") {
physx_cc.define("PX_PROFILE", Some("1"));
}
if compiler.is_none() && host.contains("-linux-") {
physx_cc.compiler("clang++");
}
physx_cc.flag(if physx_cc.get_compiler().is_like_msvc() {
"/std:c++14"
} else {
"-std=c++14"
});
use std::ffi::OsString;
let output_dir_path =
PathBuf::from(env::var("OUT_DIR").expect("output directory not specified"));
let include_path = if env::var("CARGO_FEATURE_STRUCTGEN").is_ok() {
let mut structgen_path = output_dir_path.join("structgen");
let is_cross_compiling_aarch64 = target != host && target.starts_with("aarch64-");
let structgen_compiler = physx_cc.get_compiler();
let mut cmd = structgen_compiler.to_command();
if structgen_compiler.is_like_msvc() {
let mut s = OsString::from("/Fe");
s.push(&structgen_path);
cmd.arg(s);
let mut s = OsString::from("/Fo");
s.push(&structgen_path);
s.push(".obj");
cmd.arg(s);
} else {
if is_cross_compiling_aarch64 {
cmd.arg("-static");
}
cmd.arg("-o").arg(&structgen_path);
}
cmd.arg("src/structgen/structgen.cpp");
cmd.status().expect("c++ compiler failed to execute");
if host.contains("-windows-") {
structgen_path.set_extension("exe");
}
std::fs::metadata(&structgen_path)
.expect("failed to compile structgen even though compiler reported no failures");
let mut structgen = if is_cross_compiling_aarch64 {
let mut structgen = std::process::Command::new("qemu-aarch64");
structgen.arg(&structgen_path);
structgen
} else {
std::process::Command::new(&structgen_path)
};
structgen.current_dir(&output_dir_path);
structgen.status().expect("structgen failed to execute, if you are cross compiling to aarch64 you need to have qemu-aarch64 installed");
output_dir_path
} else {
let mut include = PathBuf::from("src/generated");
match target.as_str() {
"x86_64-apple-darwin"
| "x86_64-pc-windows-msvc"
| "aarch64-linux-android"
| "aarch64-unknown-linux-gnu"
| "aarch64-apple-darwin" => {
include.push(target);
}
nix if nix.starts_with("x86_64-unknown-linux") => {
include.push("x86_64-unknown-linux");
}
_ => panic!("unknown TARGET triple '{}'", target),
}
include
};
if physx_cc.get_compiler().is_like_gnu() {
physx_cc.flag("-Wno-stringop-overflow");
}
physx_cc
.include(include_path)
.file("src/physx_api.cpp")
.compile("physx_api");
println!("cargo:rerun-if-changed=src/structgen/structgen.cpp");
println!("cargo:rerun-if-changed=src/structgen/structgen.hpp");
println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=src/physx_generated.hpp");
println!("cargo:rerun-if-changed=src/physx_generated.rs");
println!("cargo:rerun-if-changed=src/physx_api.cpp");
println!("cargo:rerun-if-changed=PhysX/physx/include/PxPhysicsVersion.h");
}