Skip to content

support manual setup constants int type by name prefix #2396

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions openssl-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ cc = "1.0.61"
openssl-src = { version = "300.2.0", optional = true, features = ["legacy"] }
pkg-config = "0.3.9"
vcpkg = "0.2.8"
regex = "1"
once_cell = "1"

# We don't actually use metadeps for annoying reasons but this is still here for tooling
[package.metadata.pkg-config]
Expand Down
44 changes: 41 additions & 3 deletions openssl-sys/build/run_bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[cfg(feature = "bindgen")]
use bindgen::callbacks::IntKind;
#[cfg(feature = "bindgen")]
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
#[cfg(feature = "bindgen")]
use bindgen::{MacroTypeVariation, RustTarget};
#[cfg(feature = "bindgen")]
use once_cell::sync::OnceCell;
use std::io::Write;
use std::path::PathBuf;
#[cfg(not(feature = "bindgen"))]
Expand Down Expand Up @@ -74,10 +78,33 @@ const INCLUDES: &str = "
#endif
";

#[cfg(feature = "bindgen")]
static MACRO_PREFIX_KIND: OnceCell<Vec<(regex::Regex, IntKind)>> = OnceCell::new();

#[cfg(feature = "bindgen")]
fn setup_prefix_kind() {
let kinds = vec![
(regex::Regex::new(r"^OPENSSL_INIT_.+").unwrap(), IntKind::U64),
(regex::Regex::new(r"^(SSL_CTRL|CRYPTO_EX_INDEX|NID|EVP|X509_FILETYPE|SSL_FILETYPE|SSL3_AD|TLS1_AD)_.+").unwrap(), IntKind::Int),
(regex::Regex::new(r"^SSL_VERIFY_.+").unwrap(), IntKind::Int),
(regex::Regex::new(r"^OPENSSL_VERSION").unwrap(), IntKind::Int),
];
let _ = MACRO_PREFIX_KIND.get_or_init(|| kinds);
}
#[cfg(feature = "bindgen")]
fn match_prefix_kine(name: &str) -> Option<IntKind> {
let kinds = MACRO_PREFIX_KIND.get()?;

kinds
.iter()
.find(|&(re, _)| re.is_match(name))
.map(|(_, kind)| *kind)
}

#[cfg(feature = "bindgen")]
pub fn run(include_dirs: &[PathBuf]) {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

setup_prefix_kind();
let mut builder = bindgen::builder()
.parse_callbacks(Box::new(OpensslCallbacks))
.rust_target(RustTarget::Stable_1_47)
Expand Down Expand Up @@ -350,8 +377,19 @@ struct OpensslCallbacks;
#[cfg(feature = "bindgen")]
impl ParseCallbacks for OpensslCallbacks {
// for now we'll continue hand-writing constants
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
MacroParsingBehavior::Ignore
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
if let Some(_) = match_prefix_kine(name) {
MacroParsingBehavior::Default
} else {
MacroParsingBehavior::Ignore
}
}
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
if let Some(kind) = match_prefix_kine(name) {
return Some(kind);
}
// auto
None
}

fn item_name(&self, original_item_name: &str) -> Option<String> {
Expand Down
Loading