Skip to content

Commit

Permalink
Merge tag 'v0.24.6'
Browse files Browse the repository at this point in the history
v0.24.6

      * Update MSRV to 1.57
      * Support variadic arguments (`...`) (mozilla#805)
      * Add --depfile option (mozilla#820)

# Conflicts:
#	Cargo.lock
#	src/bindgen/config.rs
  • Loading branch information
mhallin committed May 25, 2024
2 parents 80d46e9 + cbd3541 commit 7a0941c
Show file tree
Hide file tree
Showing 76 changed files with 785 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Specify the minimum supported Rust version
msrv = "1.54.0"
msrv = "1.57.0"
22 changes: 15 additions & 7 deletions .github/workflows/cbindgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ jobs:
- uses: actions/checkout@v3

- name: Install stable
uses: dtolnay/rust-toolchain@stable
uses: actions-rs/toolchain@v1
with:
components: "clippy, rustfmt"
toolchain: stable
components: clippy, rustfmt

- name: Run rustfmt
run: |
Expand All @@ -31,11 +32,14 @@ jobs:
- name: Install minimum supported Rust version
id: msrv
uses: dtolnay/rust-toolchain@1.54
uses: actions-rs/toolchain@v1
with:
toolchain: 1.57
override: true

- name: Build with minimum supported Rust version
run: |
cargo +${{steps.msrv.outputs.name}} test nonexistent-test --verbose
cargo test nonexistent-test --verbose
build:

Expand All @@ -44,8 +48,10 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Install stable Rust
uses: dtolnay/rust-toolchain@stable
- name: Install stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Install Python
uses: actions/setup-python@v4
Expand Down Expand Up @@ -73,7 +79,9 @@ jobs:
(cd target/package/cbindgen-$(cargo run -- --version | cut -d ' ' -f 2) && cargo test --verbose)
- name: Install nightly Rust
uses: dtolnay/rust-toolchain@nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly

- name: Test
env:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
- uses: actions/checkout@v3

- name: Install stable
uses: dtolnay/rust-toolchain@stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Build cbindgen
run: |
Expand Down
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.24.6

* Update MSRV to 1.57
* Support variadic arguments (`...`) (#805)
* Add --depfile option (#820)

## 0.24.5

* Don't enforce tempfile version.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cbindgen"
version = "0.24.5"
version = "0.24.6"
authors = [
"Emilio Cobos Álvarez <emilio@crisal.io>",
"Jeff Muizelaar <jmuizelaar@mozilla.com>",
Expand All @@ -13,6 +13,7 @@ keywords = ["bindings", "ffi", "code-generation"]
categories = ["external-ffi-bindings", "development-tools::ffi"]
repository = "https://github.com/eqrion/cbindgen"
edition = "2018"
rust-version = "1.57"
exclude = [
"tests/profile.rs", # Test relies in a sub-crate, see https://github.com/rust-lang/cargo/issues/9017
]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `cbindgen` &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![Api Rustdoc]][rustdoc] [![Rust](https://img.shields.io/badge/rust-1.54%2B-blue.svg?maxAge=3600)](https://github.com/eqrion/cbindgen)
# `cbindgen` &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![Api Rustdoc]][rustdoc] [![Rust](https://img.shields.io/badge/rust-1.57%2B-blue.svg?maxAge=3600)](https://github.com/eqrion/cbindgen)

[Build Status]: https://github.com/eqrion/cbindgen/workflows/cbindgen/badge.svg
[actions]: https://github.com/eqrion/cbindgen/actions
Expand Down
57 changes: 42 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ fn generate_tests() {

println!("cargo:rerun-if-changed={}", tests_dir.display());

// Try to make a decent guess at where our binary will end up in.
//
// TODO(emilio): Ideally running tests will just use the library-version of
// cbindgen instead of the built binary.
let cbindgen_path = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("cbindgen");

for entry in entries {
let path_segment = if entry.file_type().unwrap().is_file() {
match entry.path().extension().and_then(OsStr::to_str) {
Expand All @@ -53,8 +40,47 @@ fn generate_tests() {

writeln!(
dst,
"test_file!({:?}, test_{}, {:?}, {:?});",
cbindgen_path,
"test_file!(test_{}, {:?}, {:?});",
identifier,
path_segment,
entry.path(),
)
.unwrap();
}

dst.flush().unwrap();
}

fn generate_depfile_tests() {
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dst = File::create(Path::new(&out_dir).join("depfile_tests.rs")).unwrap();

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let tests_dir = manifest_dir.join("tests").join("depfile");
let tests = fs::read_dir(&tests_dir).unwrap();

let entries = tests.map(|t| t.expect("Couldn't read test file"));

println!("cargo:rerun-if-changed={}", tests_dir.display());

for entry in entries {
if entry.file_type().unwrap().is_file() {
continue;
};
let path_segment = entry.file_name().to_str().unwrap().to_owned();

let identifier = path_segment
.replace(|c| !char::is_alphanumeric(c), "_")
.replace("__", "_");

writeln!(
dst,
"test_file!(test_depfile_{}, {:?}, {:?});",
identifier,
path_segment,
entry.path(),
Expand All @@ -67,4 +93,5 @@ fn generate_tests() {

fn main() {
generate_tests();
generate_depfile_tests();
}
44 changes: 44 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Bindings {
constants: Vec<Constant>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
source_files: Vec<path::PathBuf>,
/// Bindings are generated by a recursive call to cbindgen
/// and shouldn't do anything when written anywhere.
noop: bool,
Expand All @@ -50,6 +51,7 @@ impl Bindings {
globals: Vec<Static>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
source_files: Vec<path::PathBuf>,
noop: bool,
) -> Bindings {
Bindings {
Expand All @@ -61,6 +63,7 @@ impl Bindings {
constants,
items,
functions,
source_files,
noop,
}
}
Expand Down Expand Up @@ -128,6 +131,47 @@ impl Bindings {
fields
}

pub fn generate_depfile<P: AsRef<path::Path>>(&self, header_path: P, depfile_path: P) {
if let Some(dir) = depfile_path.as_ref().parent() {
if !dir.exists() {
std::fs::create_dir_all(dir).unwrap()
}
}
let canon_header_path = header_path.as_ref().canonicalize().unwrap();
let mut canon_source_files: Vec<_> = self
.source_files
.iter()
.chain(self.config.config_path.as_ref().into_iter())
.map(|p| p.canonicalize().unwrap())
.collect();
// Sorting makes testing easier by ensuring the output is ordered.
canon_source_files.sort_unstable();

// When writing the depfile we must escape whitespace in paths to avoid it being interpreted
// as a seperator.
// It is not clear how to otherwise _correctly_ replace whitespace in a non-unicode
// compliant slice, without knowing the encoding, so we lossy convert such cases,
// to avoid panics.
let mut depfile = File::create(depfile_path).unwrap();
write!(
&mut depfile,
"{}:",
canon_header_path.to_string_lossy().replace(' ', "\\ ")
)
.expect("Writing header name to depfile failed");
canon_source_files.into_iter().for_each(|source_file| {
// Add line-continue and line-break and then indent with 4 spaces.
// This makes the output more human-readable.
depfile.write_all(b" \\\n ").unwrap();
let escaped_path = source_file.to_string_lossy().replace(' ', "\\ ");
depfile.write_all(escaped_path.as_bytes()).unwrap();
});

writeln!(&mut depfile).unwrap();

depfile.flush().unwrap();
}

pub fn write_to_file<P: AsRef<path::Path>>(&self, path: P) -> bool {
if self.noop {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ impl Builder {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
true,
));
}
Expand Down Expand Up @@ -391,6 +392,8 @@ impl Builder {
result.extend_with(&parser::parse_lib(cargo, &self.config)?);
}

result.source_files.extend_from_slice(self.srcs.as_slice());

Library::new(
self.config,
result.constants,
Expand All @@ -401,6 +404,7 @@ impl Builder {
result.opaque_items,
result.typedefs,
result.functions,
result.source_files,
)
.generate()
}
Expand Down
13 changes: 8 additions & 5 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::collections::{BTreeMap, HashMap};
use std::default::Default;
use std::str::FromStr;
use std::{fmt, fs, path::Path as StdPath};
use std::{fmt, fs, path::Path as StdPath, path::PathBuf as StdPathBuf};

use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
Expand Down Expand Up @@ -1036,6 +1036,8 @@ pub struct Config {
pub cython: CythonConfig,
/// Configuration options specific to C#
pub csharp: CsharpConfig,
#[serde(skip)]
pub(crate) config_path: Option<StdPathBuf>,
}

impl Default for Config {
Expand Down Expand Up @@ -1079,6 +1081,7 @@ impl Default for Config {
only_target_dependencies: false,
cython: CythonConfig::default(),
csharp: CsharpConfig::default(),
config_path: None,
}
}
}
Expand Down Expand Up @@ -1130,10 +1133,10 @@ impl Config {
)
})?;

match toml::from_str::<Config>(&config_text) {
Ok(x) => Ok(x),
Err(e) => Err(format!("Couldn't parse config file: {}.", e)),
}
let mut config = toml::from_str::<Config>(&config_text)
.map_err(|e| format!("Couldn't parse config file: {}.", e))?;
config.config_path = Some(StdPathBuf::from(file_name.as_ref()));
Ok(config)
}

pub fn from_root_or_default<P: AsRef<StdPath>>(root: P) -> Config {
Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ impl Type {
}
return Err("Tuples are not supported types.".to_owned());
}
syn::Type::Verbatim(ref tokens) if tokens.to_string() == "..." => {
Type::Primitive(PrimitiveType::VaList)
}
_ => return Err(format!("Unsupported type: {:?}", ty)),
};

Expand Down
5 changes: 5 additions & 0 deletions src/bindgen/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::HashMap;
use std::path::PathBuf;

use crate::bindgen::bindings::Bindings;
use crate::bindgen::config::{Config, Language, SortKey};
Expand All @@ -25,6 +26,7 @@ pub struct Library {
opaque_items: ItemMap<OpaqueItem>,
typedefs: ItemMap<Typedef>,
functions: Vec<Function>,
source_files: Vec<PathBuf>,
}

impl Library {
Expand All @@ -39,6 +41,7 @@ impl Library {
opaque_items: ItemMap<OpaqueItem>,
typedefs: ItemMap<Typedef>,
functions: Vec<Function>,
source_files: Vec<PathBuf>,
) -> Library {
Library {
config,
Expand All @@ -50,6 +53,7 @@ impl Library {
opaque_items,
typedefs,
functions,
source_files,
}
}

Expand Down Expand Up @@ -135,6 +139,7 @@ impl Library {
globals,
items,
functions,
self.source_files,
false,
))
}
Expand Down

0 comments on commit 7a0941c

Please sign in to comment.