Switch to SCU build (aka 'Single File' or 'Unity build') for imgui/cimgui code

This commit is contained in:
Thom Chiovoloni 2020-12-06 02:00:34 -08:00
parent 63a4ae45a9
commit 5bf5c54447
3 changed files with 33 additions and 15 deletions

View File

@ -10,6 +10,8 @@ license = "MIT/Apache-2.0"
categories = ["gui", "external-ffi-bindings"]
build = "build.rs"
links = "imgui"
# exclude json, lua, and the imgui subdirs (imgui/examples, imgui/docs, etc)
exclude = ["third-party/*.json", "third-party/*.lua", "third-party/imgui/*/"]
[build-dependencies]
cc = "1.0"

View File

@ -4,14 +4,6 @@ use std::fs;
use std::io;
use std::path::Path;
const CPP_FILES: [&str; 5] = [
"third-party/cimgui.cpp",
"third-party/imgui/imgui.cpp",
"third-party/imgui/imgui_demo.cpp",
"third-party/imgui/imgui_draw.cpp",
"third-party/imgui/imgui_widgets.cpp",
];
const DEFINES: &[(&str, Option<&str>)] = &[
// Disabled due to linking issues
("CIMGUI_NO_EXPORT", None),
@ -41,20 +33,33 @@ fn main() -> io::Result<()> {
for (key, value) in DEFINES.iter() {
println!("cargo:DEFINE_{}={}", key, value.unwrap_or(""));
}
#[cfg(not(feature = "wasm"))]
{
if !std::env::var_os("CARGO_FEATURE_WASM").is_some() {
// Check submodule status. (Anything else should be a compile error in
// the C code).
assert_file_exists("third-party/cimgui.cpp")?;
assert_file_exists("third-party/imgui/imgui.cpp")?;
let mut build = cc::Build::new();
build.cpp(true);
for (key, value) in DEFINES.iter() {
build.define(key, *value);
}
build.flag_if_supported("-Wno-return-type-c-linkage");
for path in &CPP_FILES {
assert_file_exists(path)?;
build.file(path);
let compiler = build.get_compiler();
// Avoid the if-supported flag functions for easy cases, as they're
// kinda costly.
if compiler.is_like_gnu() || compiler.is_like_clang() {
build
.flag("-fno-exceptions")
.flag("-fno-rtti");
}
build.compile("libcimgui.a");
// TODO: disable linking C++ stdlib? Not sure if it's allowed.
build
.warnings(false)
.file("include_all_imgui.cpp")
.compile("libcimgui.a")
;
}
Ok(())
}

View File

@ -0,0 +1,11 @@
// This improves build speed by only compiling a single file, and performance by
// allowing the optimizer to inline across separate object files (note that even
// when rust is built with LTO, unless the steps are taken to allow cross-lang
// LTO (tricky), the C/C++ code won't be LTOed).
#include "./third-party/imgui/imgui.cpp"
#include "./third-party/imgui/imgui_demo.cpp"
#include "./third-party/imgui/imgui_draw.cpp"
#include "./third-party/imgui/imgui_widgets.cpp"
#include "./third-party/cimgui.cpp"