Skip to content

Commit

Permalink
Changes to build system to accomodate docking
Browse files Browse the repository at this point in the history
  • Loading branch information
dbr committed Oct 27, 2021
1 parent 86fa591 commit 08b7785
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 260 deletions.
1 change: 1 addition & 0 deletions imgui-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ pkg-config = { version="0.3", optional=true }
default = []
wasm = []
freetype = ["pkg-config"]
docking = []
34 changes: 19 additions & 15 deletions imgui-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::fs;
use std::io;
use std::path::Path;

const DEFINES: &[(&str, Option<&str>)] = &[
// Rust `char` is a unicode scalar value, e.g. 32 bits.
Expand All @@ -27,23 +26,18 @@ fn assert_file_exists(path: &str) -> io::Result<()> {
}

fn main() -> io::Result<()> {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
println!(
"cargo:THIRD_PARTY={}",
manifest_dir.join("third-party").display()
);
// Output define args for compiler
for (key, value) in DEFINES.iter() {
println!("cargo:DEFINE_{}={}", key, value.unwrap_or(""));
}
if std::env::var_os("CARGO_FEATURE_WASM").is_none() {
// 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")?;

// If we aren't building WASM output, bunch of extra stuff to do
if std::env::var_os("CARGO_FEATURE_WASM").is_none() {
// C++ compiler
let mut build = cc::Build::new();

build.cpp(true);

// Set defines for compiler
for (key, value) in DEFINES.iter() {
build.define(key, *value);
}
Expand All @@ -58,11 +52,21 @@ fn main() -> io::Result<()> {
build.define("IMGUI_ENABLE_FREETYPE", None);
println!("cargo:DEFINE_IMGUI_ENABLE_FREETYPE=");

// imgui_freetype.cpp needs access to imgui.h
build.include(manifest_dir.join("third-party/imgui/"));
// imgui_freetype.cpp needs access to `#include "imgui.h"`
let manifest_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
#[cfg(feature = "docking")]
build.include(manifest_dir.join("third-party/imgui-docking/imgui"));
#[cfg(not(feature = "docking"))]
build.include(manifest_dir.join("third-party/imgui-master/imgui"));
}

#[cfg(feature = "docking")]
let imgui_cpp = "include_imgui_docking.cpp";
#[cfg(not(feature = "docking"))]
let imgui_cpp = "include_imgui_master.cpp";

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() {
Expand All @@ -71,7 +75,7 @@ fn main() -> io::Result<()> {
// TODO: disable linking C++ stdlib? Not sure if it's allowed.
build
.warnings(false)
.file("include_all_imgui.cpp")
.file(imgui_cpp)
.compile("libcimgui.a");
}
Ok(())
Expand Down
16 changes: 0 additions & 16 deletions imgui-sys/include_all_imgui.cpp

This file was deleted.

16 changes: 16 additions & 0 deletions imgui-sys/include_imgui_docking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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-docking/imgui/imgui.cpp"
#include "./third-party/imgui-docking/imgui/imgui_demo.cpp"
#include "./third-party/imgui-docking/imgui/imgui_draw.cpp"
#include "./third-party/imgui-docking/imgui/imgui_widgets.cpp"
#include "./third-party/imgui-docking/imgui/imgui_tables.cpp"
#include "./third-party/imgui-docking/cimgui.cpp"

#ifdef IMGUI_ENABLE_FREETYPE
#include "./third-party/imgui-docking/imgui/misc/freetype/imgui_freetype.cpp"
#endif


16 changes: 16 additions & 0 deletions imgui-sys/include_imgui_master.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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-master/imgui/imgui.cpp"
#include "./third-party/imgui-master/imgui/imgui_demo.cpp"
#include "./third-party/imgui-master/imgui/imgui_draw.cpp"
#include "./third-party/imgui-master/imgui/imgui_widgets.cpp"
#include "./third-party/imgui-master/imgui/imgui_tables.cpp"
#include "./third-party/imgui-master/cimgui.cpp"

#ifdef IMGUI_ENABLE_FREETYPE
#include "./third-party/imgui-master/imgui/misc/freetype/imgui_freetype.cpp"
#endif


38 changes: 22 additions & 16 deletions xtask/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ use std::path::{Path, PathBuf};
impl Bindgen {
pub fn run(self) -> Result<()> {
let root = crate::project_root();
let bindings = self
.cimgui_path
.map(PathBuf::from)
.unwrap_or_else(|| root.join("imgui-sys/third-party"));

let output = self
.output_path
Expand All @@ -20,18 +16,28 @@ impl Bindgen {
.or_else(|| std::env::var("IMGUI_RS_WASM_IMPORT_NAME").ok())
.unwrap_or_else(|| "imgui-sys-v0".to_string());

let types = get_types(&bindings.join("structs_and_enums.json"))?;
let funcs = get_definitions(&bindings.join("definitions.json"))?;
let header = bindings.join("cimgui.h");
for variant in ["master", "docking"] {
let cimgui_output = root.join(&format!("imgui-sys/third-party/imgui-{}", variant));

let types = get_types(&cimgui_output.join("structs_and_enums.json"))?;
let funcs = get_definitions(&cimgui_output.join("definitions.json"))?;
let header = cimgui_output.join("cimgui.h");

generate_binding_file(&header, &output.join("bindings.rs"), &types, &funcs, None)?;
generate_binding_file(
&header,
&output.join("wasm_bindings.rs"),
&types,
&funcs,
Some(&wasm_name),
)?;
let output_name = if variant != "master" {
format!("{}_bindings.rs", variant)
} else {
"bindings.rs".into()
};

generate_binding_file(&header, &output.join(&output_name), &types, &funcs, None)?;
generate_binding_file(
&header,
&output.join(&format!("wasm_{}", &output_name)),
&types,
&funcs,
Some(&wasm_name),
)?;
}

Ok(())
}
Expand Down Expand Up @@ -104,7 +110,7 @@ fn generate_binding_file(
"--no-prepend-enum-name",
"--no-doc-comments",
// Layout tests aren't portable (they hardcode type sizes), and for
// our case they just serve to sanity check rustc's implementation of
// our case they just serve to sanity check rustc's implementation of
// `#[repr(C)]`. If we bind directly to C++ ever, we should reconsider this.
"--no-layout-tests",
"--with-derive-default",
Expand Down
10 changes: 2 additions & 8 deletions xtask/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ xflags::args_parser! {
/// Print help information.
optional -h, --help
}
/// Run lints the way we'd run it in CI
/// Run lints the way we run it in CI
cmd lint {}
/// Run tests the way we'd run them in CI
/// Run tests the way we run them in CI
cmd test {}
/// magically wrangle the submodules if needed
cmd modfix {}
/// produce bindings using installed `bindgen`.
cmd bindgen {
/// folder containing cimgui output (default: imgui-sys/third-party)
Expand All @@ -55,7 +53,6 @@ pub enum XtaskCmd {
Help(Help),
Lint(Lint),
Test(Test),
Modfix(Modfix),
Bindgen(Bindgen),
}

Expand All @@ -70,9 +67,6 @@ pub struct Lint {}
#[derive(Debug)]
pub struct Test {}

#[derive(Debug)]
pub struct Modfix {}

#[derive(Debug)]
pub struct Bindgen {
pub cimgui_path: Option<String>,
Expand Down
4 changes: 0 additions & 4 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
mod bindgen;
mod flags;
mod submodules;

use anyhow::Result;
use flags::XtaskCmd;
use std::path::{Path, PathBuf};

pub use submodules::autofix_submodules;

fn main() {
if let Err(e) = try_main() {
eprintln!("{}", e);
Expand All @@ -29,7 +26,6 @@ fn try_main() -> Result<()> {
XtaskCmd::Help(_) => eprintln!("{}", flags::Xtask::HELP),
XtaskCmd::Lint(_) => lint_all()?,
XtaskCmd::Test(_) => test_all()?,
XtaskCmd::Modfix(_) => submodules::fixup_submodules()?,
XtaskCmd::Bindgen(cmd) => cmd.run()?,
}
Ok(())
Expand Down
Loading

0 comments on commit 08b7785

Please sign in to comment.