gpu_display: rework build.rs for cross compilation

The #[cfg(unix)] and #[cfg(windows)] checks in build.rs are testing
properties of the build system, not the target system, which is
inappropriate for cross compilation scenarios. This will matter when
cross compiling from Linux for Windows, as an example.

Use the appropriate Cargo environment variables to test target
properties instead:
https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

BUG=None
TEST=cargo build
TEST=tools/run_tests --target=host --build-target=mingw64 --build-only

Change-Id: If1ac479ae519fe69168cead68b9ee44bac56f851
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3840955
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Daniel Verkamp 2022-08-18 17:24:15 -07:00 committed by crosvm LUCI
parent 05737b5b42
commit 583a2963d3

View file

@ -2,17 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cfg_if::cfg_if! {
if #[cfg(unix)] {
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
// Performs a recursive search for a file with name under path and returns the full path if such a
// file is found.
fn scan_path<P: AsRef<Path>, O: AsRef<OsStr>>(path: P, name: O) -> Option<PathBuf> {
// Performs a recursive search for a file with name under path and returns the full path if such a
// file is found.
fn scan_path<P: AsRef<Path>, O: AsRef<OsStr>>(path: P, name: O) -> Option<PathBuf> {
for entry in (fs::read_dir(path).ok()?).flatten() {
let file_type = match entry.file_type() {
Ok(t) => t,
@ -28,10 +27,10 @@ cfg_if::cfg_if! {
}
}
None
}
}
// Searches for the given protocol in both the system wide and bundles protocols path.
fn find_protocol(name: &str) -> PathBuf {
// Searches for the given protocol in both the system wide and bundles protocols path.
fn find_protocol(name: &str) -> PathBuf {
let protocol_file_name = PathBuf::from(format!("{}.xml", name));
// Prioritize the systems wayland protocols before using the bundled ones.
if let Ok(protocols_path) = pkg_config::get_variable("wayland-protocols", "pkgdatadir") {
@ -51,9 +50,9 @@ cfg_if::cfg_if! {
name
);
protocol_path
}
}
fn compile_protocol<P: AsRef<Path>>(name: &str, out: P) -> PathBuf {
fn compile_protocol<P: AsRef<Path>>(name: &str, out: P) -> PathBuf {
let in_protocol = find_protocol(name);
println!("cargo:rerun-if-changed={}", in_protocol.display());
let out_code = out.as_ref().join(format!("{}.c", name));
@ -72,9 +71,14 @@ cfg_if::cfg_if! {
.output()
.expect("wayland-scanner client-header failed");
out_code
}
fn main() {
// Do nothing on Windows.
if std::env::var("CARGO_CFG_WINDOWS").is_ok() {
return;
}
fn main() {
// Skip installing dependencies when generating documents.
if std::env::var("CARGO_DOC").is_ok() {
return;
@ -103,9 +107,4 @@ cfg_if::cfg_if! {
build.compile("display_wl");
println!("cargo:rustc-link-lib=dylib=wayland-client");
}
} else if #[cfg(windows)] {
fn main() {}
}
}