Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor macro build #532

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}