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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, File};
use std::io::{self, BufRead, Write};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Output, Stdio};
use std::str::FromStr;
use time::macros::format_description;
use time::OffsetDateTime;
const DEFAULT_PREFIX: &str = "/usr/local";
fn get_version() -> String {
let mut version = env!("CARGO_PKG_VERSION").to_string();
if version.ends_with("-dev") {
let format = format_description!("[year][month][day]");
let date_str = OffsetDateTime::now_utc().format(&format).unwrap();
version.push_str(&format!("-{}", date_str));
}
version
}
#[derive(Clone)]
struct LibVersion {
maj: u16,
min: u16,
orig: String,
}
#[derive(Debug)]
struct ParseVersionError;
impl fmt::Display for ParseVersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(write!(f, "failed to parse version")?)
}
}
impl Error for ParseVersionError {}
impl FromStr for LibVersion {
type Err = ParseVersionError;
fn from_str(s: &str) -> Result<LibVersion, Self::Err> {
let parts: Vec<&str> = s.split('.').collect();
if parts.len() < 2 {
return Err(ParseVersionError);
}
let (maj, min): (u16, u16) = match (parts[0].parse(), parts[1].parse()) {
(Ok(maj), Ok(min)) => (maj, min),
_ => return Err(ParseVersionError),
};
Ok(LibVersion {
maj,
min,
orig: s.to_string(),
})
}
}
fn check_version(
pkg: &str,
found: LibVersion,
expect_maj: u16,
expect_min: u16,
) -> Result<(), Box<dyn Error>> {
if found.maj < expect_maj || (found.maj == expect_maj && found.min < expect_min) {
return Err(format!(
"{} version >={}.{} required, found: {}",
pkg, expect_maj, expect_min, found.orig,
)
.into());
}
Ok(())
}
fn prefixed_vars(prefix: &str) -> HashMap<String, String> {
let mut out = HashMap::new();
out.insert("BINDIR".into(), format!("{}/bin", prefix));
out.insert("CONFIGDIR".into(), format!("{}/etc", prefix));
out.insert("LIBDIR".into(), format!("{}/lib", prefix));
out.insert("LOGDIR".into(), "/var/log".into());
out.insert("RUNDIR".into(), "/var/run".into());
out
}
fn env_or_default(name: &str, defaults: &HashMap<String, String>) -> String {
match env::var(name) {
Ok(s) => s,
Err(_) => defaults.get(name).unwrap().to_string(),
}
}
fn write_if_different(dest: &Path, content: &[u8]) -> Result<(), Box<dyn Error>> {
let do_write = match fs::read(dest) {
Ok(v) => v != content,
Err(e) if e.kind() == io::ErrorKind::NotFound => true,
Err(e) => return Err(e.into()),
};
if do_write {
fs::write(dest, content)?;
}
Ok(())
}
fn write_cpp_conf_pri(
dest: &Path,
release: bool,
include_paths: &[&Path],
deny_warnings: bool,
) -> Result<(), Box<dyn Error>> {
let mut out = Vec::new();
writeln!(&mut out, "CONFIG -= debug_and_release")?;
if release {
writeln!(&mut out, "CONFIG += release")?;
} else {
writeln!(&mut out, "CONFIG += debug")?;
}
writeln!(&mut out)?;
for path in include_paths {
writeln!(&mut out, "INCLUDEPATH += {}", path.display())?;
}
writeln!(&mut out)?;
if deny_warnings {
writeln!(&mut out, "QMAKE_CXXFLAGS += \"-Werror\"")?;
}
write_if_different(dest, &out)
}
fn write_postbuild_conf_pri(
dest: &Path,
bin_dir: &str,
lib_dir: &str,
config_dir: &str,
run_dir: &str,
log_dir: &str,
) -> Result<(), Box<dyn Error>> {
let mut out = Vec::new();
writeln!(&mut out, "BINDIR = {}", bin_dir)?;
writeln!(&mut out, "LIBDIR = {}/pushpin", lib_dir)?;
writeln!(&mut out, "CONFIGDIR = {}/pushpin", config_dir)?;
writeln!(&mut out, "RUNDIR = {}/pushpin", run_dir)?;
writeln!(&mut out, "LOGDIR = {}/pushpin", log_dir)?;
writeln!(&mut out, "TARGET_TRIPLE = {}", env::var("TARGET").unwrap())?;
write_if_different(dest, &out)
}
// returned vec size guaranteed >= 1
fn get_args_lossy(command: &mut Command) -> Vec<String> {
let mut args = vec![command.get_program().to_string_lossy().into_owned()];
for s in command.get_args() {
args.push(s.to_string_lossy().into_owned());
}
args
}
// convert Result<Output> to Result<ExitStatus>, separating stdout
fn take_stdout(result: io::Result<Output>) -> (io::Result<ExitStatus>, Vec<u8>) {
match result {
Ok(output) => (Ok(output.status), output.stdout),
Err(e) => (Err(e), Vec::new()),
}
}
fn check_command_result(
program: &str,
result: io::Result<ExitStatus>,
) -> Result<(), Box<dyn Error>> {
let status = match result {
Ok(status) => status,
Err(e) => return Err(format!("{} failed: {}", program, e).into()),
};
if !status.success() {
return Err(format!("{} failed, {}", program, status).into());
}
Ok(())
}
fn check_command(command: &mut Command) -> Result<(), Box<dyn Error>> {
let args = get_args_lossy(command);
println!("{}", args.join(" "));
check_command_result(&args[0], command.status())
}
fn check_command_capture_stdout(command: &mut Command) -> Result<Vec<u8>, Box<dyn Error>> {
let args = get_args_lossy(command);
println!("{}", args.join(" "));
// don't capture stderr
let command = command.stderr(Stdio::inherit());
let (result, output) = take_stdout(command.output());
check_command_result(&args[0], result)?;
Ok(output)
}
fn check_qmake(qmake_path: &Path) -> Result<LibVersion, Box<dyn Error>> {
let version: LibVersion = {
let output =
check_command_capture_stdout(Command::new(qmake_path).args(["-query", "QT_VERSION"]))?;
let s = String::from_utf8(output)?;
let s = s.trim();
match s.parse() {
Ok(v) => v,
Err(_) => return Err(format!("unexpected qt version string: [{}]", s).into()),
}
};
check_version("qt", version.clone(), 5, 12)?;
Ok(version)
}
fn find_in_path(name: &str) -> Option<PathBuf> {
for d in env::var("PATH").unwrap_or_default().split(':') {
if d.is_empty() {
continue;
}
let path = Path::new(d).join(name);
if path.exists() {
return Some(path);
}
}
None
}
fn find_qmake() -> Result<(PathBuf, LibVersion), Box<dyn Error>> {
let mut errors = Vec::new();
// check for a usable qmake in PATH
let names = &["qmake", "qmake6", "qmake5"];
for name in names {
if let Some(p) = find_in_path(name) {
match check_qmake(&p) {
Ok(version) => return Ok((p, version)),
Err(e) => errors.push(format!("skipping {}: {}", p.display(), e)),
}
}
}
if errors.is_empty() {
errors.push(format!("none of ({}) found in PATH", names.join(", ")));
}
// check pkg-config
let pkg = "Qt5Core";
match pkg_config::get_variable(pkg, "host_bins") {
Ok(host_bins) if !host_bins.is_empty() => {
let host_bins = PathBuf::from(host_bins);
match fs::canonicalize(host_bins.join("qmake")) {
Ok(p) => match check_qmake(&p) {
Ok(version) => return Ok((p, version)),
Err(e) => errors.push(format!("skipping {}: {}", p.display(), e)),
},
Err(e) => errors.push(format!("qmake not found in {}: {}", host_bins.display(), e)),
}
}
Ok(_) => errors.push(format!(
"pkg-config variable host_bins does not exist for {}",
pkg
)),
Err(e) => errors.push(format!("pkg-config error for {}: {}", pkg, e)),
}
Err(format!("unable to find a usable qmake: {}", errors.join(", ")).into())
}
fn get_qmake() -> Result<(PathBuf, LibVersion), Box<dyn Error>> {
match env::var("QMAKE") {
Ok(s) => {
let path = PathBuf::from(s);
let version = check_qmake(&path)?;
Ok((path, version))
}
Err(env::VarError::NotPresent) => find_qmake(),
Err(env::VarError::NotUnicode(_)) => Err("QMAKE not unicode".into()),
}
}
fn contains_file_prefix(dir: &Path, prefix: &str) -> Result<bool, io::Error> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
if entry.file_name().as_bytes().starts_with(prefix.as_bytes()) {
return Ok(true);
}
}
Ok(false)
}
fn get_qt_lib_prefix(lib_dir: &Path, version_maj: u16) -> Result<String, Box<dyn Error>> {
let prefixes = if cfg!(target_os = "macos") {
[format!("Qt{}", version_maj), "Qt".to_string()]
} else {
[format!("libQt{}", version_maj), "libQt".to_string()]
};
for prefix in &prefixes {
if contains_file_prefix(lib_dir, prefix)? {
return Ok(prefix.strip_prefix("lib").unwrap_or(prefix).to_string());
}
}
Err(format!(
"no files in {} beginning with any of: {}",
lib_dir.display(),
prefixes.join(", ")
)
.into())
}
fn find_boost_include_dir() -> Result<PathBuf, Box<dyn Error>> {
let paths = [
"/opt/homebrew/include",
"/usr/local/include",
"/usr/include",
];
let version_filename = "boost/version.hpp";
for path in paths {
let path = PathBuf::from(path);
let full_path = path.join(version_filename);
if !full_path.exists() {
continue;
}
let file = File::open(&full_path)?;
let reader = io::BufReader::new(file);
let mut version_line = None;
for line in reader.lines() {
match line {
Ok(s) if s.contains("#define BOOST_LIB_VERSION") => version_line = Some(s),
Ok(_) => continue,
Err(e) => {
return Err(format!("failed to read {}: {}", full_path.display(), e).into())
}
}
}
let version_line = match version_line {
Some(s) => s,
None => return Err(format!("version line not found in {}", full_path.display()).into()),
};
let parts: Vec<&str> = version_line.split('"').collect();
if parts.len() < 2 {
return Err(format!("failed to parse version line in {}", full_path.display()).into());
}
let version = parts[1].replace('_', ".");
let version = match version.parse() {
Ok(v) => v,
Err(_) => return Err(format!("unexpected boost version string: {}", version).into()),
};
check_version("boost", version, 1, 71)?;
return Ok(path);
}
Err(format!(
"{} not found in any of: {}",
version_filename,
paths.join(", ")
)
.into())
}
fn contains_subslice<T: PartialEq>(haystack: &[T], needle: &[T]) -> bool {
haystack.windows(needle.len()).any(|w| w == needle)
}
fn main() -> Result<(), Box<dyn Error>> {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
cbindgen::generate(crate_dir).map_or_else(
|error| match error {
cbindgen::Error::ParseSyntaxError { .. } => {}
e => panic!("{:?}", e),
},
|bindings| {
bindings.write_to_file("target/include/rust/bindings.h");
},
);
let (qmake_path, qt_version) = get_qmake()?;
let qt_install_libs = {
let output = check_command_capture_stdout(
Command::new(&qmake_path).args(["-query", "QT_INSTALL_LIBS"]),
)?;
let libs_dir = PathBuf::from(String::from_utf8(output)?.trim());
fs::canonicalize(&libs_dir)
.map_err(|_| format!("QT_INSTALL_LIBS dir {} not found", libs_dir.display()))?
};
let qt_lib_prefix = get_qt_lib_prefix(&qt_install_libs, qt_version.maj)?;
let boost_include_dir = match env::var("BOOST_INCLUDE_DIR") {
Ok(s) => PathBuf::from(s),
Err(env::VarError::NotPresent) => find_boost_include_dir()?,
Err(env::VarError::NotUnicode(_)) => return Err("BOOST_INCLUDE_DIR not unicode".into()),
};
let default_vars = {
let prefix = match env::var("PREFIX") {
Ok(s) => Some(s),
Err(env::VarError::NotPresent) => None,
Err(env::VarError::NotUnicode(_)) => return Err("PREFIX not unicode".into()),
};
if let Some(prefix) = prefix {
prefixed_vars(&prefix)
} else {
prefixed_vars(DEFAULT_PREFIX)
}
};
let bin_dir = env_or_default("BINDIR", &default_vars);
let config_dir = env_or_default("CONFIGDIR", &default_vars);
let lib_dir = env_or_default("LIBDIR", &default_vars);
let log_dir = env_or_default("LOGDIR", &default_vars);
let run_dir = env_or_default("RUNDIR", &default_vars);
let root_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let profile = env::var("PROFILE")?;
let cpp_pro = root_dir.join("src/cpp.pro");
let cpp_tests_pro = root_dir.join("src/cpptests.pro");
for dir in ["moc", "obj", "test-moc", "test-obj", "test-work"] {
fs::create_dir_all(out_dir.join(dir))?;
}
let mut include_paths = Vec::new();
include_paths.push(out_dir.as_ref());
if boost_include_dir != Path::new("/usr/include") {
include_paths.push(boost_include_dir.as_ref());
}
let deny_warnings = match env::var("CARGO_ENCODED_RUSTFLAGS") {
Ok(s) => {
let flags: Vec<&str> = s.split('\x1f').collect();
contains_subslice(&flags, &["-D", "warnings"])
}
Err(env::VarError::NotPresent) => false,
Err(env::VarError::NotUnicode(_)) => {
return Err("CARGO_ENCODED_RUSTFLAGS not unicode".into())
}
};
write_cpp_conf_pri(
&out_dir.join("conf.pri"),
profile == "release",
&include_paths,
deny_warnings,
)?;
write_postbuild_conf_pri(
&Path::new("postbuild").join("conf.pri"),
&bin_dir,
&lib_dir,
&config_dir,
&run_dir,
&log_dir,
)?;
check_command(Command::new(&qmake_path).args([
OsStr::new("-o"),
out_dir.join("Makefile").as_os_str(),
cpp_pro.as_os_str(),
]))?;
check_command(Command::new(&qmake_path).args([
OsStr::new("-o"),
out_dir.join("Makefile.test").as_os_str(),
cpp_tests_pro.as_os_str(),
]))?;
check_command(
Command::new(&qmake_path)
.args(["-o", "Makefile", "postbuild.pro"])
.current_dir("postbuild"),
)?;
check_command(
Command::new("make")
.env("MAKEFLAGS", env::var("CARGO_MAKEFLAGS")?)
.args(["-f", "Makefile"])
.current_dir(&out_dir),
)?;
check_command(
Command::new("make")
.env("MAKEFLAGS", env::var("CARGO_MAKEFLAGS")?)
.args(["-f", "Makefile.test"])
.current_dir(&out_dir),
)?;
println!("cargo:rustc-env=APP_VERSION={}", get_version());
println!("cargo:rustc-env=CONFIG_DIR={}/pushpin", config_dir);
println!("cargo:rustc-env=LIB_DIR={}/pushpin", lib_dir);
println!("cargo:rustc-cfg=qt_lib_prefix=\"{}\"", qt_lib_prefix);
println!("cargo:rustc-link-search={}", out_dir.display());
if cfg!(target_os = "macos") {
println!(
"cargo:rustc-link-search=framework={}",
qt_install_libs.display()
);
} else {
println!("cargo:rustc-link-search={}", qt_install_libs.display());
}
println!("cargo:rerun-if-env-changed=RELEASE");
println!("cargo:rerun-if-env-changed=PREFIX");
println!("cargo:rerun-if-env-changed=BINDIR");
println!("cargo:rerun-if-env-changed=CONFIGDIR");
println!("cargo:rerun-if-env-changed=LIBDIR");
println!("cargo:rerun-if-env-changed=LOGDIR");
println!("cargo:rerun-if-env-changed=RUNDIR");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=cbindgen.toml");
Ok(())
}
|