Use CARGO_FEATURE_x instead of #[cfg(...)]

Avoids unnecessary rebuilds of build script.

Also a few somewhat unrelated tidy-ups
This commit is contained in:
dbr 2021-11-02 14:24:32 +11:00
parent a350e8dcd8
commit 4a03702908

View File

@ -12,27 +12,19 @@ const DEFINES: &[(&str, Option<&str>)] = &[
("IMGUI_DISABLE_OSX_FUNCTIONS", None),
];
fn assert_file_exists(path: &str) -> io::Result<()> {
match fs::metadata(path) {
Ok(_) => Ok(()),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
panic!(
"Can't access {}. Did you forget to fetch git submodules?",
path
);
}
Err(e) => Err(e),
}
}
fn main() -> io::Result<()> {
// Output define args for compiler
for (key, value) in DEFINES.iter() {
println!("cargo:DEFINE_{}={}", key, value.unwrap_or(""));
}
// Feature flags - no extra dependencies, so these are queried as
// env-vars to avoid recompilation of build.rs
let docking_enabled = std::env::var_os("CARGO_FEATURE_DOCKING").is_some();
let wasm_enabled = std::env::var_os("CARGO_FEATURE_WASM").is_none();
// If we aren't building WASM output, bunch of extra stuff to do
if std::env::var_os("CARGO_FEATURE_WASM").is_none() {
if !wasm_enabled {
// C++ compiler
let mut build = cc::Build::new();
build.cpp(true);
@ -45,26 +37,32 @@ fn main() -> io::Result<()> {
// Freetype font rasterizer feature
#[cfg(feature = "freetype")]
{
// Find library
let freetype = pkg_config::Config::new().find("freetype2").unwrap();
for include in freetype.include_paths.iter() {
build.include(include);
}
// Set flag for dear imgui
build.define("IMGUI_ENABLE_FREETYPE", None);
println!("cargo:DEFINE_IMGUI_ENABLE_FREETYPE=");
// 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"));
if docking_enabled {
build.include(manifest_dir.join("third-party/imgui-docking/imgui"));
} else {
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";
// Which "all imgui" file to use
let imgui_cpp = if docking_enabled {
"include_imgui_docking.cpp"
} else {
"include_imgui_master.cpp"
};
// Set up compiler
let compiler = build.get_compiler();
// Avoid the if-supported flag functions for easy cases, as they're
@ -72,6 +70,8 @@ fn main() -> io::Result<()> {
if compiler.is_like_gnu() || compiler.is_like_clang() {
build.flag("-fno-exceptions").flag("-fno-rtti");
}
// Build imgui lib, suppressing warnings.
// TODO: disable linking C++ stdlib? Not sure if it's allowed.
build.warnings(false).file(imgui_cpp).compile("libcimgui.a");
}