Skip to content

Commit df13db0

Browse files
committed
Add support for include directory env vars.
1 parent 3ed1e35 commit df13db0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

macro/src/dialect.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
mod error;
27
mod generation;
38
mod input;
@@ -31,7 +36,7 @@ pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std:
3136

3237
parser = parser.add_include_directory(LLVM_INCLUDE_DIRECTORY);
3338

34-
for path in input.directories() {
39+
let get_path = |path: &str| {
3540
let path = if matches!(
3641
Path::new(path).components().next(),
3742
Some(Component::CurDir | Component::ParentDir)
@@ -40,8 +45,19 @@ pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std:
4045
} else {
4146
Path::new(LLVM_INCLUDE_DIRECTORY).join(path)
4247
};
48+
path.display().to_string()
49+
};
50+
51+
for path in input.directories() {
52+
let path = get_path(path);
53+
parser = parser.add_include_directory(&path);
54+
}
4355

44-
parser = parser.add_include_directory(&path.display().to_string());
56+
for env_var in input.directory_env_vars() {
57+
if let Ok(path) = env::var(env_var) {
58+
let path = get_path(&path);
59+
parser = parser.add_include_directory(&path);
60+
}
4561
}
4662

4763
if input.files().count() > 0 {

macro/src/dialect/input.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
mod input_field;
27

38
use self::input_field::InputField;
@@ -8,6 +13,7 @@ pub struct DialectInput {
813
name: String,
914
files: Vec<String>,
1015
directories: Vec<String>,
16+
directory_env_vars: Vec<String>,
1117
}
1218

1319
impl DialectInput {
@@ -22,13 +28,18 @@ impl DialectInput {
2228
pub fn directories(&self) -> impl Iterator<Item = &str> {
2329
self.directories.iter().map(Deref::deref)
2430
}
31+
32+
pub fn directory_env_vars(&self) -> impl Iterator<Item = &str> {
33+
self.directory_env_vars.iter().map(Deref::deref)
34+
}
2535
}
2636

2737
impl Parse for DialectInput {
2838
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
2939
let mut name = None;
3040
let mut files = vec![];
3141
let mut directories = vec![];
42+
let mut directory_env_vars = vec![];
3243

3344
for item in Punctuated::<InputField, Token![,]>::parse_terminated(input)? {
3445
match item {
@@ -39,13 +50,17 @@ impl Parse for DialectInput {
3950
InputField::Directories(field) => {
4051
directories = field.into_iter().map(|literal| literal.value()).collect()
4152
}
53+
InputField::DirectoryEnvVars(field) => {
54+
directory_env_vars = field.into_iter().map(|literal| literal.value()).collect()
55+
}
4256
}
4357
}
4458

4559
Ok(Self {
4660
name: name.ok_or_else(|| input.error("dialect name required"))?,
4761
files,
4862
directories,
63+
directory_env_vars
4964
})
5065
}
5166
}

macro/src/dialect/input/input_field.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
use proc_macro2::Ident;
27
use quote::format_ident;
38
use syn::{LitStr, Token, bracketed, parse::Parse, punctuated::Punctuated};
@@ -6,6 +11,7 @@ pub enum InputField {
611
Name(LitStr),
712
Files(Punctuated<LitStr, Token![,]>),
813
Directories(Punctuated<LitStr, Token![,]>),
14+
DirectoryEnvVars(Punctuated<LitStr, Token![,]>),
915
}
1016

1117
impl Parse for InputField {
@@ -28,6 +34,12 @@ impl Parse for InputField {
2834
Ok(Self::Directories(
2935
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
3036
))
37+
} else if ident == format_ident!("include_directory_env_vars") {
38+
let content;
39+
bracketed!(content in input);
40+
Ok(Self::DirectoryEnvVars(
41+
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
42+
))
3143
} else {
3244
Err(input.error(format!("invalid field {ident}")))
3345
}

0 commit comments

Comments
 (0)