Skip to content

Commit

Permalink
Make build script errors more explicit
Browse files Browse the repository at this point in the history
Signed-off-by: Andrej Orsula <[email protected]>
  • Loading branch information
AndrejOrsula committed Jan 8, 2024
1 parent 8245230 commit 1692b7c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 44 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ version = "0.1.0"
pxr_build = { path = "pxr_build", version = "0.1.0" }
pxr_sys = { path = "pxr_sys", version = "0.1.0" }

anyhow = { version = "1.0" }
autocxx = { version = "0.26" }
autocxx-build = { version = "0.26" }
built_different = { version = "0.1" }
Expand Down
1 change: 0 additions & 1 deletion pxr_build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
anyhow = { workspace = true }
regex = { workspace = true }
which = { workspace = true }

Expand Down
4 changes: 2 additions & 2 deletions pxr_build/src/codegen/vt/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ macro_rules! gen_vt_value {
};
}

pub fn codegen(out: impl AsRef<std::path::Path>) -> anyhow::Result<()> {
pub fn codegen(out: impl AsRef<std::path::Path>) {
let code = gen_vt_value![
bool as "bool",
u8 as "uint8_t" => "unsigned char",
Expand Down Expand Up @@ -140,5 +140,5 @@ pub fn codegen(out: impl AsRef<std::path::Path>) -> anyhow::Result<()> {
crate::pxr::GfVec4i as "pxr::GfVec4i" => "GfVec4i",
[i32; 4] as "pxr::GfVec4i" => "GfVec4i",
];
crate::codegen_write(&code, out)
crate::codegen_write(&code, out);
}
2 changes: 1 addition & 1 deletion pxr_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod codegen;
mod utils;

pub use codegen::*;
pub(crate) use utils::codegen::codegen_write;
pub(crate) use utils::codegen_write;
14 changes: 6 additions & 8 deletions pxr_build/src/utils/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub const HEADER: &str = "\
//! It is not intended for manual editing.
";

pub fn codegen_write(code: &str, out: impl AsRef<Path>) -> anyhow::Result<()> {
pub fn codegen_write(code: &str, out: impl AsRef<Path>) {
// Try formatting the generated code if rustfmt is available.
let code = rustfmt(code).unwrap_or_else(|_| code.to_string());

Expand All @@ -16,20 +16,18 @@ pub fn codegen_write(code: &str, out: impl AsRef<Path>) -> anyhow::Result<()> {
// Don't overwrite the file if it hasn't changed (ignoring whitespace differences)
let out = out.as_ref();
if out.exists() {
let original_code = std::fs::read_to_string(out)?;
let original_code = std::fs::read_to_string(out).unwrap();
if remove_redundant_whitespace(&original_code) == remove_redundant_whitespace(&code) {
return Ok(());
return;
}
}

// Ensure the parent directory exists
if let Some(parent) = out.parent() {
std::fs::create_dir_all(parent)?;
std::fs::create_dir_all(parent).unwrap();
}

// Write the generated code to a file.
let mut file = std::fs::File::create(out)?;
file.write_all(code.as_bytes())?;

Ok(())
let mut file = std::fs::File::create(out).unwrap();
file.write_all(code.as_bytes()).unwrap();
}
1 change: 0 additions & 1 deletion pxr_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ half = { workspace = true }
thiserror = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
autocxx-build = { workspace = true }
built_different = { workspace = true }
cpp_build = { workspace = true }
Expand Down
53 changes: 25 additions & 28 deletions pxr_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn main() {

// If the `vendored` feature is enabled, download and compile OpenUSD (or use the previously cached installation)
#[cfg(feature = "vendored")]
vendored::download_and_compile_openusd().unwrap()
vendored::download_and_compile_openusd()
}
};
// Verify that the OpenUSD installation contains the required subdirectories
Expand Down Expand Up @@ -294,21 +294,19 @@ fn main() {
.collect::<Vec<_>>();

// Expand macros to generate Rust code with `cpp` macros (workaround | `cpp!` macros cannot be nested)
bindings::expand_macros_cpp().unwrap();
bindings::expand_macros_cpp();

// Compile C++ code with `cpp`
bindings::compile_cpp(&include_paths).unwrap();
bindings::compile_cpp(&include_paths);

// Generate bindings with `autocxx`
bindings::generate_autocxx(&include_paths).unwrap();
bindings::generate_autocxx(&include_paths);
}

/// Module that contains functions for generating bindings.
mod bindings {
pub fn generate_autocxx(
include_paths: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
) -> anyhow::Result<()> {
autocxx_build::Builder::new("src/ffi/bindings.rs", include_paths.into_iter())
pub fn generate_autocxx(include_paths: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>) {
autocxx_build::Builder::new("src/ffi/bindings.rs", include_paths)
.extra_clang_args(&[
"-x",
"c++",
Expand All @@ -318,7 +316,8 @@ mod bindings {
&format!("-D_GLIBCXX_USE_CXX11_ABI={}", super::USE_CXX11_ABI),
"-Wno-everything",
])
.build()?
.build()
.unwrap()
.compiler("clang")
.flag("-x")
.flag("c++")
Expand All @@ -328,23 +327,19 @@ mod bindings {
.define("_GLIBCXX_USE_CXX11_ABI", Some(super::USE_CXX11_ABI))
.flag("-Wno-everything")
.compile("openusd_sys_autocxx");
Ok(())
}

pub fn expand_macros_cpp() -> anyhow::Result<()> {
pub fn expand_macros_cpp() {
pxr_build::codegen::codegen_vt_value(
std::path::PathBuf::from("src")
.join("ffi")
.join("pxr")
.join("vt")
.join("value.rs"),
)?;
Ok(())
);
}

pub fn compile_cpp(
include_paths: impl IntoIterator<Item = impl AsRef<std::path::Path>>,
) -> anyhow::Result<()> {
pub fn compile_cpp(include_paths: impl IntoIterator<Item = impl AsRef<std::path::Path>>) {
// Configure rebuild triggers based on file changes
walkdir::WalkDir::new("src/ffi")
.into_iter()
Expand Down Expand Up @@ -375,13 +370,12 @@ mod bindings {
cpp_builder = cpp_builder.include(path);
}
cpp_builder.build("src/lib.rs");
Ok(())
}
}

#[cfg(feature = "vendored")]
mod vendored {
pub fn download_and_compile_openusd() -> anyhow::Result<std::path::PathBuf> {
pub fn download_and_compile_openusd() -> std::path::PathBuf {
// ENV: Determine if the download should be forced regardless of the cache validity
println!("cargo:rerun-if-env-changed=OPENUSD_DOWNLOAD_FORCE");
let force_download = built_different::parse_bool_env("OPENUSD_DOWNLOAD_FORCE", false);
Expand Down Expand Up @@ -413,7 +407,7 @@ mod vendored {

// Skip download if the cache is valid and download is not forced
if !force_download && cache_path.exists() {
return Ok(cache_path);
return cache_path;
}

// Determine the path where the OpenUSD source will be extracted
Expand All @@ -430,13 +424,15 @@ mod vendored {
&download_url,
&openusd_extract_path,
force_download,
)?;
)
.unwrap();

// Locate the root of the OpenUSD source
let openusd_src_path = openusd_extract_path.join(format!("OpenUSD-{openusd_version}"));

// Apply compile-time patches
built_different::apply_file_patches_in_place("patches/src", &openusd_src_path, true, true)?;
built_different::apply_file_patches_in_place("patches/src", &openusd_src_path, true, true)
.unwrap();

// Locate the build script
let build_script_path = openusd_src_path.join("build_scripts").join("build_usd.py");
Expand Down Expand Up @@ -534,7 +530,8 @@ mod vendored {
.arg(&openusd_install_path)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()?;
.output()
.unwrap();
let status = output.status;
if !status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Expand All @@ -543,20 +540,20 @@ mod vendored {
}

// Remove the source now that it is no longer needed
std::fs::remove_dir_all(&openusd_extract_path)?;
std::fs::remove_dir_all(openusd_install_path.join("src"))?;
std::fs::remove_dir_all(&openusd_extract_path).unwrap();
std::fs::remove_dir_all(openusd_install_path.join("src")).unwrap();

// Move OpenUSD to the cache or create a symlink
if symlink_cache && !cache_path.is_dir() {
built_different::create_symlink(&openusd_install_path, &cache_path, true)?;
built_different::create_symlink(&openusd_install_path, &cache_path, true).unwrap();
} else {
if let Some(parent) = cache_path.parent() {
std::fs::create_dir_all(parent)?;
std::fs::create_dir_all(parent).unwrap();
}
std::fs::rename(&openusd_install_path, &cache_path)?;
std::fs::rename(&openusd_install_path, &cache_path).unwrap();
}

Ok(cache_path)
cache_path
}

fn determine_cache_path_openusd(version: &str) -> std::path::PathBuf {
Expand Down

0 comments on commit 1692b7c

Please sign in to comment.