Change alias to the native style depending on the platform

Fixes #3431
This commit is contained in:
Olivier Goffart 2023-09-29 14:27:10 +02:00 committed by Olivier Goffart
parent 92f14c54f0
commit d688f37ee8
7 changed files with 95 additions and 75 deletions

View file

@ -300,21 +300,6 @@ pub fn compile_with_config(
let mut compiler_config = config.config;
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
let mut rerun_if_changed = String::new();
if std::env::var_os("SLINT_STYLE").is_none() && compiler_config.style.is_none() {
compiler_config.style = std::env::var_os("OUT_DIR").and_then(|path| {
// Same logic as in i-slint-backend-selector's build script to get the path
let path = Path::new(&path).parent()?.parent()?.join("SLINT_DEFAULT_STYLE.txt");
// unfortunately, if for some reason the file is changed by the i-slint-backend-selector's build script,
// it is changed after cargo decide to re-run this build script or not. So that means one will need two build
// to settle the right thing.
rerun_if_changed = format!("cargo:rerun-if-changed={}", path.display());
let style = std::fs::read_to_string(path).ok()?;
Some(style.trim().into())
});
}
let syntax_node = syntax_node.expect("diags contained no compilation errors");
// 'spin_on' is ok here because the compiler in single threaded and does not block if there is no blocking future
@ -353,7 +338,7 @@ pub fn compile_with_config(
});
write!(code_formatter, "{}", generated).map_err(CompileError::SaveError)?;
println!("{}\ncargo:rerun-if-changed={}", rerun_if_changed, path.display());
println!("cargo:rerun-if-changed={}", path.display());
for resource in doc.root_component.embedded_file_resources.borrow().keys() {
if !resource.starts_with("builtin:") {

View file

@ -7,7 +7,6 @@
#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
extern crate proc_macro;
use std::path::Path;
use i_slint_compiler::diagnostics::BuildDiagnostics;
use i_slint_compiler::parser::SyntaxKind;
@ -338,41 +337,6 @@ pub fn slint(stream: TokenStream) -> TokenStream {
let mut compiler_config =
CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Rust);
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
if std::env::var_os("SLINT_STYLE").is_none() {
// This file is written by the i-slint-backend-selector's built script.
// It is in the target/xxx/build directory
let target_path = match std::env::var_os("OUT_DIR") {
Some(out_dir) => Some(
Path::new(&out_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.join("SLINT_DEFAULT_STYLE.txt"),
),
None => {
// OUT_DIR is only defined when the crate having the macro has a build.rs script
// as a fallback, try to parse the rustc arguments
// https://stackoverflow.com/questions/60264534/getting-the-target-folder-from-inside-a-rust-proc-macro
let mut args = std::env::args();
let mut out_dir = None;
while let Some(arg) = args.next() {
if arg == "--out-dir" {
out_dir = args.next();
}
}
out_dir.map(|out_dir| {
Path::new(&out_dir).parent().unwrap().join("build/SLINT_DEFAULT_STYLE.txt")
})
}
};
if let Some(target_path) = target_path {
compiler_config.style =
std::fs::read_to_string(target_path).map(|style| style.trim().into()).ok()
}
}
compiler_config.include_paths = include_paths;
let (root_component, diag) =
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));