-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
93 lines (86 loc) · 2.25 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::path::PathBuf;
#[allow(dead_code, unused_macros, unused_imports)]
use std::{env,
io};
#[cfg(target_os = "windows")]
use winres::WindowsResource;
#[allow(unused_macros)]
macro_rules! print {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}
pub const OVERRIDE_ICON_LOCATION: Option<&'static str> = option_env!("ICON_LOCATION");
pub const RUST_FLAGS: Option<&'static str> = option_env!("RUSTFLAGS");
fn main()
{
windows_icon().expect("Failed to embed icon");
fltk().expect("Failed to build fltk files");
}
/// generate files for fltk ui stuff
fn fltk() -> Result<(), BuildError>
{
println!("cargo:rerun-if-changed=src/gui/shared_ui.fl");
let g = fl2rust::Generator::default();
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
if let Err(e) = g.in_out(
"src/gui/shared_ui.fl",
out_path.join("shared_ui.rs").to_str().unwrap()
)
{
return Err(BuildError::FLTK(format!(
"Failed to build shared_ui.fl {:#?}",
e
)));
}
Ok(())
}
/// check if a location exists
#[allow(dead_code)]
fn path_exists(path: String) -> bool
{
std::path::Path::new(path.as_str()).exists()
}
/// set the icon to `icon.ico` when building for windows
#[cfg(target_os = "windows")]
fn windows_icon() -> Result<(), BuildError>
{
let icon_location = OVERRIDE_ICON_LOCATION.unwrap_or("icon.ico");
if env::var_os("CARGO_CFG_WINDOWS").is_some()
{
if !path_exists(icon_location.to_string())
{
print!("icon.ico not found. Not embedding icon");
return Ok(());
}
WindowsResource::new()
// This path can be absolute, or relative to your crate root.
.set_icon(icon_location)
.compile()?;
print!("successfully set icon");
}
else
{
print!("not on windows, can't embed icon");
}
Ok(())
}
/// ignored since icon handling is done by fltk on non-windows
#[cfg(not(target_os = "windows"))]
fn windows_icon() -> Result<(), BuildError>
{
Ok(())
}
#[derive(Debug)]
pub enum BuildError
{
IO(io::Error),
FLTK(String)
}
impl From<io::Error> for BuildError
{
fn from(e: io::Error) -> Self
{
BuildError::IO(e)
}
}