2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2021 The ChromiumOS Authors
|
2021-09-02 23:00:07 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
use std::env;
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
use std::fs;
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
use std::path::Path;
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
use std::path::PathBuf;
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
2022-07-27 18:11:32 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
|
|
|
use anyhow::bail;
|
|
|
|
use anyhow::Result;
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
const MINIGBM_SRC: &str = "../third_party/minigbm";
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
const VIRGLRENDERER_SRC: &str = "../third_party/virglrenderer";
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
fn is_native_build() -> bool {
|
|
|
|
env::var("HOST").unwrap() == env::var("TARGET").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the target triplet prefix for gcc commands. No prefix is required
|
|
|
|
/// for native builds.
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
fn get_cross_compile_prefix() -> String {
|
|
|
|
if is_native_build() {
|
|
|
|
return String::from("");
|
|
|
|
}
|
|
|
|
|
|
|
|
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
|
|
|
|
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
|
|
|
|
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
|
2022-11-30 00:58:05 +00:00
|
|
|
format!("{}-{}-{}-", arch, os, env)
|
2021-09-02 23:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// For cross-compilation with meson, we need to pick a cross-file, which
|
|
|
|
/// live in /usr/local/share/meson/cross.
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
fn get_meson_cross_args() -> Vec<String> {
|
|
|
|
if is_native_build() {
|
|
|
|
Vec::new()
|
|
|
|
} else {
|
|
|
|
vec![
|
|
|
|
"--cross-file".to_string(),
|
|
|
|
env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
fn build_minigbm(out_dir: &Path) -> Result<()> {
|
|
|
|
let lib_path = out_dir.join("libgbm.a");
|
|
|
|
if lib_path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:38:11 +00:00
|
|
|
if !Path::new(MINIGBM_SRC).join(".git").exists() {
|
2021-09-02 23:00:07 +00:00
|
|
|
bail!(
|
2021-10-29 15:38:11 +00:00
|
|
|
"{} source does not exist, did you forget to \
|
|
|
|
`git submodule update --init`?",
|
|
|
|
MINIGBM_SRC
|
2021-09-02 23:00:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let make_flags = env::var("CARGO_MAKEFLAGS").unwrap();
|
|
|
|
let status = Command::new("make")
|
|
|
|
.env("MAKEFLAGS", make_flags)
|
|
|
|
.env("CROSS_COMPILE", get_cross_compile_prefix())
|
|
|
|
.arg(format!("OUT={}", out_dir.display()))
|
|
|
|
.arg("CC_STATIC_LIBRARY(libminigbm.pie.a)")
|
2021-10-29 15:38:11 +00:00
|
|
|
.current_dir(MINIGBM_SRC)
|
2021-09-02 23:00:07 +00:00
|
|
|
.status()?;
|
|
|
|
if !status.success() {
|
|
|
|
bail!("make failed with status: {}", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// minigbm will be linked using the name gbm, make sure it can be found.
|
|
|
|
fs::copy(out_dir.join("libminigbm.pie.a"), out_dir.join("libgbm.a"))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2021-09-02 23:00:07 +00:00
|
|
|
fn build_virglrenderer(out_dir: &Path) -> Result<()> {
|
|
|
|
let lib_path = out_dir.join("src/libvirglrenderer.a");
|
|
|
|
if lib_path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:38:11 +00:00
|
|
|
if !Path::new(VIRGLRENDERER_SRC).join(".git").exists() {
|
2021-09-02 23:00:07 +00:00
|
|
|
bail!(
|
2021-10-29 15:38:11 +00:00
|
|
|
"{} source does not exist, did you forget to \
|
|
|
|
`git submodule update --init`?",
|
|
|
|
VIRGLRENDERER_SRC
|
2021-09-02 23:00:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-22 04:36:27 +00:00
|
|
|
let platforms = [
|
|
|
|
"egl",
|
|
|
|
#[cfg(feature = "x")]
|
|
|
|
"glx",
|
|
|
|
];
|
|
|
|
|
2021-09-02 23:00:07 +00:00
|
|
|
let minigbm_src_abs = PathBuf::from(MINIGBM_SRC).canonicalize()?;
|
|
|
|
let status = Command::new("meson")
|
|
|
|
.env("PKG_CONFIG_PATH", &minigbm_src_abs)
|
|
|
|
.arg("setup")
|
2022-03-22 04:36:27 +00:00
|
|
|
.arg(format!("-Dplatforms={}", platforms.join(",")))
|
2021-09-02 23:00:07 +00:00
|
|
|
.arg("-Ddefault_library=static")
|
|
|
|
.args(get_meson_cross_args())
|
|
|
|
.arg(out_dir.as_os_str())
|
|
|
|
.current_dir(VIRGLRENDERER_SRC)
|
|
|
|
.status()?;
|
|
|
|
if !status.success() {
|
|
|
|
bail!("meson setup failed with status: {}", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add local minigbm paths to make sure virglrenderer can build against it.
|
|
|
|
let mut cmd = Command::new("meson");
|
|
|
|
cmd.env("CPATH", &minigbm_src_abs)
|
|
|
|
.arg("compile")
|
|
|
|
.arg("src/virglrenderer")
|
|
|
|
.current_dir(out_dir);
|
|
|
|
|
|
|
|
let status = cmd.status()?;
|
|
|
|
if !status.success() {
|
|
|
|
bail!("meson compile failed with status: {}", status);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:53:45 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
2022-09-20 18:17:04 +00:00
|
|
|
fn virglrenderer_deps() -> Result<()> {
|
2021-09-02 23:00:07 +00:00
|
|
|
// System provided runtime dependencies.
|
|
|
|
pkg_config::Config::new().probe("epoxy")?;
|
|
|
|
pkg_config::Config::new().probe("libdrm")?;
|
2022-09-20 18:17:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-02 23:00:07 +00:00
|
|
|
|
2022-09-20 18:17:04 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
|
|
|
fn virglrenderer() -> Result<()> {
|
2023-05-04 17:35:21 +00:00
|
|
|
// Use virglrenderer package from pkgconfig on ChromeOS builds
|
|
|
|
if std::env::var("CROSVM_BUILD_VARIANT").unwrap_or_default() == "chromeos" {
|
|
|
|
pkg_config::Config::new().probe("virglrenderer")?;
|
2022-09-20 18:17:04 +00:00
|
|
|
virglrenderer_deps()?;
|
2021-09-02 23:00:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise build from source.
|
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
|
|
|
|
let minigbm_out = out_dir.join("minigbm");
|
|
|
|
let virglrenderer_out = out_dir.join("virglrenderer");
|
|
|
|
build_minigbm(&minigbm_out)?;
|
|
|
|
build_virglrenderer(&virglrenderer_out)?;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search={}/src",
|
|
|
|
virglrenderer_out.display()
|
|
|
|
);
|
|
|
|
println!("cargo:rustc-link-search={}", minigbm_out.display());
|
|
|
|
println!("cargo:rustc-link-lib=static=virglrenderer");
|
|
|
|
println!("cargo:rustc-link-lib=static=gbm");
|
2022-09-20 18:17:04 +00:00
|
|
|
|
|
|
|
virglrenderer_deps()?;
|
|
|
|
|
2021-09-02 23:00:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-13 21:12:43 +00:00
|
|
|
|
2022-10-19 23:27:42 +00:00
|
|
|
#[cfg(all(feature = "gfxstream", not(feature = "gfxstream_stub")))]
|
2022-04-22 14:25:56 +00:00
|
|
|
fn gfxstream() -> Result<()> {
|
|
|
|
let gfxstream_path = std::env::var("GFXSTREAM_PATH")?;
|
2022-10-19 23:27:42 +00:00
|
|
|
println!("cargo:rustc-link-lib=gfxstream_backend");
|
2022-04-22 14:25:56 +00:00
|
|
|
println!("cargo:rustc-link-search={}", gfxstream_path);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:12:43 +00:00
|
|
|
fn main() -> Result<()> {
|
2022-04-08 10:51:32 +00:00
|
|
|
// Skip installing dependencies when generating documents.
|
|
|
|
if std::env::var("CARGO_DOC").is_ok() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:12:43 +00:00
|
|
|
#[cfg(feature = "virgl_renderer")]
|
|
|
|
virglrenderer()?;
|
2022-10-19 23:27:42 +00:00
|
|
|
#[cfg(all(feature = "gfxstream", not(feature = "gfxstream_stub")))]
|
2022-04-22 14:25:56 +00:00
|
|
|
gfxstream()?;
|
2021-10-13 21:12:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|