From 4a0370290839a6ffe1907229de35b0d28f7fb4f7 Mon Sep 17 00:00:00 2001 From: dbr Date: Tue, 2 Nov 2021 14:24:32 +1100 Subject: [PATCH] Use CARGO_FEATURE_x instead of #[cfg(...)] Avoids unnecessary rebuilds of build script. Also a few somewhat unrelated tidy-ups --- imgui-sys/build.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/imgui-sys/build.rs b/imgui-sys/build.rs index e3a56fe..9e01939 100644 --- a/imgui-sys/build.rs +++ b/imgui-sys/build.rs @@ -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"); }