Skip to content

Commit

Permalink
Remove dependency on Cargo variables in a helper function for the Sli…
Browse files Browse the repository at this point in the history
…nt to Rust compiler (#6430)

Co-authored-by: Willem Vanhulle <willemvanhulle@gmail.com>
  • Loading branch information
otiv-willem-vanhulle and wvhulle authored Oct 4, 2024
1 parent 43b0507 commit 595221f
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions api/rs/build/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,50 @@ pub fn compile(path: impl AsRef<std::path::Path>) -> Result<(), CompileError> {
/// slint_build::compile_with_config("ui/hello.slint", config).unwrap();
/// ```
pub fn compile_with_config(
path: impl AsRef<std::path::Path>,
relative_slint_file_path: impl AsRef<std::path::Path>,
config: CompilerConfiguration,
) -> Result<(), CompileError> {
let path = Path::new(&env::var_os("CARGO_MANIFEST_DIR").ok_or(CompileError::NotRunViaCargo)?)
.join(path.as_ref());
.join(relative_slint_file_path.as_ref());

let absolute_rust_output_file_path =
Path::new(&env::var_os("OUT_DIR").ok_or(CompileError::NotRunViaCargo)?).join(
path.file_stem()
.map(Path::new)
.unwrap_or_else(|| Path::new("slint_out"))
.with_extension("rs"),
);

let paths_dependencies =
compile_with_output(path, absolute_rust_output_file_path.clone(), config)?;

for path_dependency in paths_dependencies {
println!("cargo:rerun-if-changed={}", path_dependency.display());
}

println!("cargo:rerun-if-env-changed=SLINT_STYLE");
println!("cargo:rerun-if-env-changed=SLINT_FONT_SIZES");
println!("cargo:rerun-if-env-changed=SLINT_SCALE_FACTOR");
println!("cargo:rerun-if-env-changed=SLINT_ASSET_SECTION");
println!("cargo:rerun-if-env-changed=SLINT_EMBED_RESOURCES");
println!("cargo:rerun-if-env-changed=SLINT_EMIT_DEBUG_INFO");

println!(
"cargo:rustc-env=SLINT_INCLUDE_GENERATED={}",
absolute_rust_output_file_path.display()
);

Ok(())
}

/// Compile the input file to an output file and list dependencies.
pub fn compile_with_output(
input_slint_file_path: impl AsRef<std::path::Path>,
output_rust_file_path: impl AsRef<std::path::Path>,
config: CompilerConfiguration,
) -> Result<Vec<std::path::PathBuf>, CompileError> {
let mut diag = BuildDiagnostics::default();
let syntax_node = i_slint_compiler::parser::parse_file(&path, &mut diag);
let syntax_node = i_slint_compiler::parser::parse_file(&input_slint_file_path, &mut diag);

if diag.has_errors() {
let vec = diag.to_string_vec();
Expand All @@ -394,21 +430,16 @@ pub fn compile_with_config(
return Err(CompileError::CompileError(vec));
}

let output_file_path = Path::new(&env::var_os("OUT_DIR").ok_or(CompileError::NotRunViaCargo)?)
.join(
path.file_stem()
.map(Path::new)
.unwrap_or_else(|| Path::new("slint_out"))
.with_extension("rs"),
);

let file = std::fs::File::create(&output_file_path).map_err(CompileError::SaveError)?;
let mut code_formatter = CodeFormatter::new(BufWriter::new(file));
let output_file =
std::fs::File::create(&output_rust_file_path).map_err(CompileError::SaveError)?;
let mut code_formatter = CodeFormatter::new(BufWriter::new(output_file));
let generated = i_slint_compiler::generator::rust::generate(&doc, &loader.compiler_config);

let mut dependencies: Vec<std::path::PathBuf> = Vec::new();

for x in &diag.all_loaded_files {
if x.is_absolute() {
println!("cargo:rerun-if-changed={}", x.display());
dependencies.push(x.clone());
}
}

Expand All @@ -420,23 +451,15 @@ pub fn compile_with_config(
});

write!(code_formatter, "{}", generated).map_err(CompileError::SaveError)?;
println!("cargo:rerun-if-changed={}", path.display());
dependencies.push(input_slint_file_path.as_ref().to_path_buf());

for resource in doc.embedded_file_resources.borrow().keys() {
if !resource.starts_with("builtin:") {
println!("cargo:rerun-if-changed={}", resource);
dependencies.push(Path::new(resource).to_path_buf());
}
}
println!("cargo:rerun-if-env-changed=SLINT_STYLE");
println!("cargo:rerun-if-env-changed=SLINT_FONT_SIZES");
println!("cargo:rerun-if-env-changed=SLINT_SCALE_FACTOR");
println!("cargo:rerun-if-env-changed=SLINT_ASSET_SECTION");
println!("cargo:rerun-if-env-changed=SLINT_EMBED_RESOURCES");
println!("cargo:rerun-if-env-changed=SLINT_EMIT_DEBUG_INFO");

println!("cargo:rustc-env=SLINT_INCLUDE_GENERATED={}", output_file_path.display());

Ok(())
Ok(dependencies)
}

/// This function is for use the application's build script, in order to print any device specific
Expand Down

0 comments on commit 595221f

Please sign in to comment.