Skip to content

Commit

Permalink
Refactor macro build (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored May 3, 2024
1 parent b8307eb commit 97141bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
41 changes: 41 additions & 0 deletions macro/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{env, error::Error, path::Path, process::Command, str};

const LLVM_MAJOR_VERSION: usize = 18;

fn main() -> Result<(), Box<dyn Error>> {
let version_variable = format!("MLIR_SYS_{}0_PREFIX", LLVM_MAJOR_VERSION);

println!("cargo:rerun-if-env-changed={version_variable}");
println!(
"cargo:rustc-env=LLVM_INCLUDE_DIRECTORY={}",
// spell-checker: disable-next-line
llvm_config("--includedir", &version_variable)?
);

Ok(())
}

fn llvm_config(
argument: &str,
version_variable: &str,
) -> Result<String, Box<dyn std::error::Error>> {
let prefix = env::var(version_variable)
.map(|path| Path::new(&path).join("bin"))
.unwrap_or_default();
let call = format!(
"{} --link-static {}",
prefix.join("llvm-config").display(),
argument
);

Ok(str::from_utf8(
&if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", &call]).output()?
} else {
Command::new("sh").arg("-c").arg(&call).output()?
}
.stdout,
)?
.trim()
.to_string())
}
33 changes: 2 additions & 31 deletions macro/src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use operation::Operation;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use std::{env, fmt::Display, path::Path, process::Command, str};
use std::{env, fmt::Display, str};
use tblgen::{record::Record, record_keeper::RecordKeeper, TableGenParser};

const LLVM_MAJOR_VERSION: usize = 18;

pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std::error::Error>> {
let mut parser = TableGenParser::new();

Expand All @@ -32,12 +30,9 @@ pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std:
parser = parser.add_source_file(file).map_err(create_syn_error)?;
}

// spell-checker: disable-next-line
let llvm_include_directory = llvm_config("--includedir")?;

for path in input
.include_directories()
.chain([llvm_include_directory.as_str()])
.chain([env!("LLVM_INCLUDE_DIRECTORY")])
{
parser = parser.add_include_path(path);
}
Expand Down Expand Up @@ -86,30 +81,6 @@ fn generate_dialect_module(
})
}

// TODO Move this into a `build.rs` script and pass down configuration values
// (e.g. `include_directories`) via environment variables.
fn llvm_config(argument: &str) -> Result<String, Box<dyn std::error::Error>> {
let prefix = env::var(format!("MLIR_SYS_{}0_PREFIX", LLVM_MAJOR_VERSION))
.map(|path| Path::new(&path).join("bin"))
.unwrap_or_default();
let call = format!(
"{} --link-static {}",
prefix.join("llvm-config").display(),
argument
);

Ok(str::from_utf8(
&if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", &call]).output()?
} else {
Command::new("sh").arg("-c").arg(&call).output()?
}
.stdout,
)?
.trim()
.to_string())
}

fn create_syn_error(error: impl Display) -> syn::Error {
syn::Error::new(Span::call_site(), format!("{}", error))
}

0 comments on commit 97141bd

Please sign in to comment.