Skip to content

Commit

Permalink
Fix build and linting (#19)
Browse files Browse the repository at this point in the history
This PR fixes build for Linux + Homebrew environment and lining for the
new version of Rust.
  • Loading branch information
raviqqe authored Jan 7, 2025
1 parent 3d94eef commit 2aa8fda
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 56 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ on:
- master
pull_request:
jobs:
build:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: swatinem/rust-cache@v2
- uses: homebrew/actions/setup-homebrew@master
- run: tools/setup.sh
- run: cargo build
test:
strategy:
fail-fast: false
Expand All @@ -15,16 +29,16 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: Homebrew/actions/setup-homebrew@master
- uses: swatinem/rust-cache@v2
- uses: homebrew/actions/setup-homebrew@master
- run: tools/setup.sh
- run: cargo test
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: Homebrew/actions/setup-homebrew@master
- uses: swatinem/rust-cache@v2
- uses: homebrew/actions/setup-homebrew@master
- run: tools/setup.sh
- run: cargo clippy -- -D warnings
format:
Expand Down
66 changes: 33 additions & 33 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{
env,
error::Error,
ffi::OsStr,
fs::read_dir,
path::Path,
process::{exit, Command},
str,
Expand All @@ -18,18 +20,17 @@ const LLVM_MAJOR_VERSION: usize = if cfg!(feature = "llvm16-0") {

fn main() {
if let Err(error) = run() {
eprintln!("{}", error);
eprintln!("{error}");
exit(1);
}
}

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

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

build_c_library()?;

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

println!("cargo:rustc-link-lib=ffi");

if let Some(name) = get_system_libcpp() {
println!("cargo:rustc-link-lib={}", name);
println!("cargo:rustc-link-lib={name}");
}

std::env::set_var("CXXFLAGS", llvm_config("--cxxflags")?);
std::env::set_var("CFLAGS", llvm_config("--cflags")?);
println!("cargo:rustc-link-search={}", &env::var("OUT_DIR")?);
bindgen::builder()
.header("wrapper.h")
.clang_arg("-Icc/include")
.clang_arg(format!("-I{}", llvm_config("--includedir")?))
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()?
.write_to_file(Path::new(&env::var("OUT_DIR")?).join("bindings.rs"))?;

Ok(())
}

fn build_c_library() -> Result<(), Box<dyn Error>> {
env::set_var("CXXFLAGS", llvm_config("--cxxflags")?);
env::set_var("CFLAGS", llvm_config("--cflags")?);

cc::Build::new()
.cpp(true)
.files(
std::fs::read_dir("cc/lib")?
.filter(|r| r.is_ok())
.map(|r| r.unwrap().path())
.filter(|r| r.is_file() && r.extension().unwrap() == "cpp"),
read_dir("cc/lib")?
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.map(|entry| entry.path())
.filter(|path| path.is_file() && path.extension() == Some(OsStr::new("cpp"))),
)
.cpp(true)
.include("cc/include")
.include(llvm_config("--includedir")?)
.flag(&llvm_config("--cxxflags")?)
.flag("-Wno-unused-parameter")
.flag("-Werror")
.std("c++17")
.compile("CTableGen");

println!("cargo:rustc-link-lib=static=CTableGen");

bindgen::builder()
.header("wrapper.h")
.clang_arg("-Icc/include")
.clang_arg(format!("-I{}", llvm_config("--includedir")?))
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.unwrap()
.write_to_file(Path::new(&env::var("OUT_DIR")?).join("bindings.rs"))?;

Ok(())
}

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

Ok(str::from_utf8(
Expand All @@ -137,5 +137,5 @@ fn llvm_config(argument: &str) -> Result<String, Box<dyn Error>> {
fn parse_library_name(name: &str) -> Result<&str, String> {
name.strip_prefix("lib")
.and_then(|name| name.split('.').next())
.ok_or_else(|| format!("failed to parse library name: {}", name))
.ok_or_else(|| format!("failed to parse library name: {name}"))
}
6 changes: 3 additions & 3 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum TypedInit<'a> {
Invalid,
}

impl<'a> TypedInit<'a> {
impl TypedInit<'_> {
fn variant_name(&self) -> &'static str {
match self {
TypedInit::Bit(_) => "Bit",
Expand All @@ -67,7 +67,7 @@ impl<'a> TypedInit<'a> {
}
}

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

impl<'a> Debug for TypedInit<'a> {
impl Debug for TypedInit<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "TypedInit(")?;
let name = self.variant_name();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct TableGenParser<'s> {
_source_ref: PhantomData<&'s str>,
}

impl<'s> Default for TableGenParser<'s> {
impl Default for TableGenParser<'_> {
fn default() -> Self {
Self::new()
}
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'s> TableGenParser<'s> {
}
}

impl<'s> Drop for TableGenParser<'s> {
impl Drop for TableGenParser<'_> {
fn drop(&mut self) {
unsafe {
tableGenFree(self.raw);
Expand Down
12 changes: 6 additions & 6 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Record<'a> {
_reference: PhantomData<&'a TableGenRecordRef>,
}

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

Expand All @@ -53,7 +53,7 @@ impl<'a> Display for Record<'a> {
}
}

impl<'a> Debug for Record<'a> {
impl Debug for Record<'_> {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
writeln!(formatter, "Record(")?;
Display::fmt(self, formatter)?;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'a> Record<'a> {
}
}

impl<'a> SourceLoc for Record<'a> {
impl SourceLoc for Record<'_> {
fn source_location(self) -> SourceLocation {
unsafe { SourceLocation::from_raw(tableGenRecordGetLoc(self.raw)) }
}
Expand Down Expand Up @@ -236,7 +236,7 @@ pub struct RecordValue<'a> {
_reference: PhantomData<&'a TableGenRecordRef>,
}

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

Expand All @@ -252,7 +252,7 @@ impl<'a> Display for RecordValue<'a> {
}
}

impl<'a> RecordValue<'a> {
impl RecordValue<'_> {
/// Creates a record from a raw object.
///
/// # Safety
Expand All @@ -270,7 +270,7 @@ impl<'a> RecordValue<'a> {
}
}

impl<'a> SourceLoc for RecordValue<'a> {
impl SourceLoc for RecordValue<'_> {
fn source_location(self) -> SourceLocation {
unsafe { SourceLocation::from_raw(tableGenRecordValGetLoc(self.raw)) }
}
Expand Down
10 changes: 5 additions & 5 deletions src/record_keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'s> RecordKeeper<'s> {
}
}

impl<'s> Drop for RecordKeeper<'s> {
impl Drop for RecordKeeper<'_> {
fn drop(&mut self) {
unsafe {
tableGenRecordKeeperFree(self.raw);
Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct NamedRecordIter<'a, T> {
_kind: PhantomData<&'a T>,
}

impl<'a, T> NamedRecordIter<'a, T> {
impl<T> NamedRecordIter<'_, T> {
unsafe fn from_raw(raw: TableGenRecordKeeperIteratorRef) -> Self {
NamedRecordIter {
raw,
Expand Down Expand Up @@ -159,13 +159,13 @@ impl<'a, T: NextRecord> Iterator for NamedRecordIter<'a, T> {
}
}

impl<'a, T> Clone for NamedRecordIter<'a, T> {
impl<T> Clone for NamedRecordIter<'_, T> {
fn clone(&self) -> Self {
unsafe { Self::from_raw(tableGenRecordKeeperIteratorClone(self.raw)) }
}
}

impl<'a, T> Drop for NamedRecordIter<'a, T> {
impl<T> Drop for NamedRecordIter<'_, T> {
fn drop(&mut self) {
unsafe { tableGenRecordKeeperIteratorFree(self.raw) }
}
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'a> Iterator for RecordIter<'a> {
}
}

impl<'a> Drop for RecordIter<'a> {
impl Drop for RecordIter<'_> {
fn drop(&mut self) {
unsafe { tableGenRecordVectorFree(self.raw) }
}
Expand Down
2 changes: 1 addition & 1 deletion src/string_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct StringRef<'a> {
_reference: PhantomData<&'a TableGenStringRef>,
}

impl<'a> StringRef<'a> {
impl StringRef<'_> {
pub unsafe fn to_raw(self) -> TableGenStringRef {
self.raw
}
Expand Down
4 changes: 2 additions & 2 deletions tools/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ llvm_prefix=$(brew --prefix llvm@$llvm_version)

echo TABLEGEN_190_PREFIX=$llvm_prefix >>$GITHUB_ENV
echo PATH=$llvm_prefix/bin:$PATH >>$GITHUB_ENV
echo LIBRARY_PATH=$(brew --prefix)/lib:$LIBRARY_PATH >>$GITHUB_ENV
echo LD_LIBRARY_PATH=$(brew --prefix)/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV
echo LIBRARY_PATH=$llvm_prefix/lib:$LIBRARY_PATH >>$GITHUB_ENV
echo LD_LIBRARY_PATH=$llvm_prefix/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV

0 comments on commit 2aa8fda

Please sign in to comment.