Skip to content

Commit 2aa8fda

Browse files
authored
Fix build and linting (#19)
This PR fixes build for Linux + Homebrew environment and lining for the new version of Rust.
1 parent 3d94eef commit 2aa8fda

File tree

8 files changed

+70
-56
lines changed

8 files changed

+70
-56
lines changed

.github/workflows/test.yaml

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ on:
55
- master
66
pull_request:
77
jobs:
8+
build:
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
os:
13+
- ubuntu-latest
14+
- macos-latest
15+
runs-on: ${{ matrix.os }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: swatinem/rust-cache@v2
19+
- uses: homebrew/actions/setup-homebrew@master
20+
- run: tools/setup.sh
21+
- run: cargo build
822
test:
923
strategy:
1024
fail-fast: false
@@ -15,16 +29,16 @@ jobs:
1529
runs-on: ${{ matrix.os }}
1630
steps:
1731
- uses: actions/checkout@v4
18-
- uses: Swatinem/rust-cache@v2
19-
- uses: Homebrew/actions/setup-homebrew@master
32+
- uses: swatinem/rust-cache@v2
33+
- uses: homebrew/actions/setup-homebrew@master
2034
- run: tools/setup.sh
2135
- run: cargo test
2236
lint:
2337
runs-on: ubuntu-latest
2438
steps:
2539
- uses: actions/checkout@v4
26-
- uses: Swatinem/rust-cache@v2
27-
- uses: Homebrew/actions/setup-homebrew@master
40+
- uses: swatinem/rust-cache@v2
41+
- uses: homebrew/actions/setup-homebrew@master
2842
- run: tools/setup.sh
2943
- run: cargo clippy -- -D warnings
3044
format:

build.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
env,
33
error::Error,
4+
ffi::OsStr,
5+
fs::read_dir,
46
path::Path,
57
process::{exit, Command},
68
str,
@@ -18,18 +20,17 @@ const LLVM_MAJOR_VERSION: usize = if cfg!(feature = "llvm16-0") {
1820

1921
fn main() {
2022
if let Err(error) = run() {
21-
eprintln!("{}", error);
23+
eprintln!("{error}");
2224
exit(1);
2325
}
2426
}
2527

2628
fn run() -> Result<(), Box<dyn Error>> {
2729
let version = llvm_config("--version")?;
2830

29-
if !version.starts_with(&format!("{}.", LLVM_MAJOR_VERSION)) {
31+
if !version.starts_with(&format!("{LLVM_MAJOR_VERSION}.")) {
3032
return Err(format!(
31-
"failed to find correct version ({}.x.x) of llvm-config (found {})",
32-
LLVM_MAJOR_VERSION, version
33+
"failed to find correct version ({LLVM_MAJOR_VERSION}.x.x) of llvm-config (found {version})",
3334
)
3435
.into());
3536
}
@@ -38,6 +39,8 @@ fn run() -> Result<(), Box<dyn Error>> {
3839
println!("cargo:rerun-if-changed=cc");
3940
println!("cargo:rustc-link-search={}", llvm_config("--libdir")?);
4041

42+
build_c_library()?;
43+
4144
for name in llvm_config("--libnames")?.trim().split(' ') {
4245
println!("cargo:rustc-link-lib=static={}", parse_library_name(name)?);
4346
}
@@ -62,43 +65,41 @@ fn run() -> Result<(), Box<dyn Error>> {
6265
}
6366
}
6467

65-
println!("cargo:rustc-link-lib=ffi");
66-
6768
if let Some(name) = get_system_libcpp() {
68-
println!("cargo:rustc-link-lib={}", name);
69+
println!("cargo:rustc-link-lib={name}");
6970
}
7071

71-
std::env::set_var("CXXFLAGS", llvm_config("--cxxflags")?);
72-
std::env::set_var("CFLAGS", llvm_config("--cflags")?);
73-
println!("cargo:rustc-link-search={}", &env::var("OUT_DIR")?);
72+
bindgen::builder()
73+
.header("wrapper.h")
74+
.clang_arg("-Icc/include")
75+
.clang_arg(format!("-I{}", llvm_config("--includedir")?))
76+
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
77+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
78+
.generate()?
79+
.write_to_file(Path::new(&env::var("OUT_DIR")?).join("bindings.rs"))?;
80+
81+
Ok(())
82+
}
83+
84+
fn build_c_library() -> Result<(), Box<dyn Error>> {
85+
env::set_var("CXXFLAGS", llvm_config("--cxxflags")?);
86+
env::set_var("CFLAGS", llvm_config("--cflags")?);
7487

7588
cc::Build::new()
89+
.cpp(true)
7690
.files(
77-
std::fs::read_dir("cc/lib")?
78-
.filter(|r| r.is_ok())
79-
.map(|r| r.unwrap().path())
80-
.filter(|r| r.is_file() && r.extension().unwrap() == "cpp"),
91+
read_dir("cc/lib")?
92+
.collect::<Result<Vec<_>, _>>()?
93+
.into_iter()
94+
.map(|entry| entry.path())
95+
.filter(|path| path.is_file() && path.extension() == Some(OsStr::new("cpp"))),
8196
)
82-
.cpp(true)
8397
.include("cc/include")
8498
.include(llvm_config("--includedir")?)
85-
.flag(&llvm_config("--cxxflags")?)
86-
.flag("-Wno-unused-parameter")
99+
.flag("-Werror")
87100
.std("c++17")
88101
.compile("CTableGen");
89102

90-
println!("cargo:rustc-link-lib=static=CTableGen");
91-
92-
bindgen::builder()
93-
.header("wrapper.h")
94-
.clang_arg("-Icc/include")
95-
.clang_arg(format!("-I{}", llvm_config("--includedir")?))
96-
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
97-
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
98-
.generate()
99-
.unwrap()
100-
.write_to_file(Path::new(&env::var("OUT_DIR")?).join("bindings.rs"))?;
101-
102103
Ok(())
103104
}
104105

@@ -117,9 +118,8 @@ fn llvm_config(argument: &str) -> Result<String, Box<dyn Error>> {
117118
.map(|path| Path::new(&path).join("bin"))
118119
.unwrap_or_default();
119120
let call = format!(
120-
"{} --link-static {}",
121-
prefix.join("llvm-config").display(),
122-
argument
121+
"{} --link-static {argument}",
122+
prefix.join("llvm-config").display()
123123
);
124124

125125
Ok(str::from_utf8(
@@ -137,5 +137,5 @@ fn llvm_config(argument: &str) -> Result<String, Box<dyn Error>> {
137137
fn parse_library_name(name: &str) -> Result<&str, String> {
138138
name.strip_prefix("lib")
139139
.and_then(|name| name.split('.').next())
140-
.ok_or_else(|| format!("failed to parse library name: {}", name))
140+
.ok_or_else(|| format!("failed to parse library name: {name}"))
141141
}

src/init.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub enum TypedInit<'a> {
5151
Invalid,
5252
}
5353

54-
impl<'a> TypedInit<'a> {
54+
impl TypedInit<'_> {
5555
fn variant_name(&self) -> &'static str {
5656
match self {
5757
TypedInit::Bit(_) => "Bit",
@@ -67,7 +67,7 @@ impl<'a> TypedInit<'a> {
6767
}
6868
}
6969

70-
impl<'a> Display for TypedInit<'a> {
70+
impl Display for TypedInit<'_> {
7171
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
7272
match self {
7373
Self::Bit(init) => write!(f, "{}", &init),
@@ -83,7 +83,7 @@ impl<'a> Display for TypedInit<'a> {
8383
}
8484
}
8585

86-
impl<'a> Debug for TypedInit<'a> {
86+
impl Debug for TypedInit<'_> {
8787
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
8888
write!(f, "TypedInit(")?;
8989
let name = self.variant_name();

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct TableGenParser<'s> {
127127
_source_ref: PhantomData<&'s str>,
128128
}
129129

130-
impl<'s> Default for TableGenParser<'s> {
130+
impl Default for TableGenParser<'_> {
131131
fn default() -> Self {
132132
Self::new()
133133
}
@@ -212,7 +212,7 @@ impl<'s> TableGenParser<'s> {
212212
}
213213
}
214214

215-
impl<'s> Drop for TableGenParser<'s> {
215+
impl Drop for TableGenParser<'_> {
216216
fn drop(&mut self) {
217217
unsafe {
218218
tableGenFree(self.raw);

src/record.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Record<'a> {
3737
_reference: PhantomData<&'a TableGenRecordRef>,
3838
}
3939

40-
impl<'a> Display for Record<'a> {
40+
impl Display for Record<'_> {
4141
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
4242
let mut data = (formatter, Ok(()));
4343

@@ -53,7 +53,7 @@ impl<'a> Display for Record<'a> {
5353
}
5454
}
5555

56-
impl<'a> Debug for Record<'a> {
56+
impl Debug for Record<'_> {
5757
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
5858
writeln!(formatter, "Record(")?;
5959
Display::fmt(self, formatter)?;
@@ -191,7 +191,7 @@ impl<'a> Record<'a> {
191191
}
192192
}
193193

194-
impl<'a> SourceLoc for Record<'a> {
194+
impl SourceLoc for Record<'_> {
195195
fn source_location(self) -> SourceLocation {
196196
unsafe { SourceLocation::from_raw(tableGenRecordGetLoc(self.raw)) }
197197
}
@@ -236,7 +236,7 @@ pub struct RecordValue<'a> {
236236
_reference: PhantomData<&'a TableGenRecordRef>,
237237
}
238238

239-
impl<'a> Display for RecordValue<'a> {
239+
impl Display for RecordValue<'_> {
240240
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
241241
let mut data = (formatter, Ok(()));
242242

@@ -252,7 +252,7 @@ impl<'a> Display for RecordValue<'a> {
252252
}
253253
}
254254

255-
impl<'a> RecordValue<'a> {
255+
impl RecordValue<'_> {
256256
/// Creates a record from a raw object.
257257
///
258258
/// # Safety
@@ -270,7 +270,7 @@ impl<'a> RecordValue<'a> {
270270
}
271271
}
272272

273-
impl<'a> SourceLoc for RecordValue<'a> {
273+
impl SourceLoc for RecordValue<'_> {
274274
fn source_location(self) -> SourceLocation {
275275
unsafe { SourceLocation::from_raw(tableGenRecordValGetLoc(self.raw)) }
276276
}

src/record_keeper.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'s> RecordKeeper<'s> {
9696
}
9797
}
9898

99-
impl<'s> Drop for RecordKeeper<'s> {
99+
impl Drop for RecordKeeper<'_> {
100100
fn drop(&mut self) {
101101
unsafe {
102102
tableGenRecordKeeperFree(self.raw);
@@ -131,7 +131,7 @@ pub struct NamedRecordIter<'a, T> {
131131
_kind: PhantomData<&'a T>,
132132
}
133133

134-
impl<'a, T> NamedRecordIter<'a, T> {
134+
impl<T> NamedRecordIter<'_, T> {
135135
unsafe fn from_raw(raw: TableGenRecordKeeperIteratorRef) -> Self {
136136
NamedRecordIter {
137137
raw,
@@ -159,13 +159,13 @@ impl<'a, T: NextRecord> Iterator for NamedRecordIter<'a, T> {
159159
}
160160
}
161161

162-
impl<'a, T> Clone for NamedRecordIter<'a, T> {
162+
impl<T> Clone for NamedRecordIter<'_, T> {
163163
fn clone(&self) -> Self {
164164
unsafe { Self::from_raw(tableGenRecordKeeperIteratorClone(self.raw)) }
165165
}
166166
}
167167

168-
impl<'a, T> Drop for NamedRecordIter<'a, T> {
168+
impl<T> Drop for NamedRecordIter<'_, T> {
169169
fn drop(&mut self) {
170170
unsafe { tableGenRecordKeeperIteratorFree(self.raw) }
171171
}
@@ -201,7 +201,7 @@ impl<'a> Iterator for RecordIter<'a> {
201201
}
202202
}
203203

204-
impl<'a> Drop for RecordIter<'a> {
204+
impl Drop for RecordIter<'_> {
205205
fn drop(&mut self) {
206206
unsafe { tableGenRecordVectorFree(self.raw) }
207207
}

src/string_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct StringRef<'a> {
88
_reference: PhantomData<&'a TableGenStringRef>,
99
}
1010

11-
impl<'a> StringRef<'a> {
11+
impl StringRef<'_> {
1212
pub unsafe fn to_raw(self) -> TableGenStringRef {
1313
self.raw
1414
}

tools/setup.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ llvm_prefix=$(brew --prefix llvm@$llvm_version)
1111

1212
echo TABLEGEN_190_PREFIX=$llvm_prefix >>$GITHUB_ENV
1313
echo PATH=$llvm_prefix/bin:$PATH >>$GITHUB_ENV
14-
echo LIBRARY_PATH=$(brew --prefix)/lib:$LIBRARY_PATH >>$GITHUB_ENV
15-
echo LD_LIBRARY_PATH=$(brew --prefix)/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV
14+
echo LIBRARY_PATH=$llvm_prefix/lib:$LIBRARY_PATH >>$GITHUB_ENV
15+
echo LD_LIBRARY_PATH=$llvm_prefix/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV

0 commit comments

Comments
 (0)