Skip to content
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

Optimize openssl linkage #200

Open
wants to merge 3 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
52 changes: 20 additions & 32 deletions paho-mqtt-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,26 @@ mod build {
// but it only seems to set a variable for the path to the include files.
// We assume the directory above that one is the SSL root.
fn openssl_root_dir() -> Option<String> {
env::var("DEP_OPENSSL_INCLUDE").ok().and_then(|path| {
use std::ffi::OsString;

fn env_inner(name: &str) -> Option<OsString> {
let var = env::var_os(name);
println!("cargo:rerun-if-env-changed={}", name);

match var {
Some(ref v) => println!("{} = {}", name, v.to_string_lossy()),
None => println!("{} unset", name),
}

var
}
fn env(name: &str) -> Option<OsString> {
let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_");
let prefixed = format!("{}_{}", prefix, name);
env_inner(&prefixed).or_else(|| env_inner(name))
}

env("OPENSSL_INCLUDE_DIR").and_then(|path| {
Path::new(&path)
.parent()
.map(|path| path.display().to_string())
Expand Down Expand Up @@ -305,37 +324,6 @@ mod build {

bindings::place_bindings(&inc_dir);

// Link in the SSL libraries if configured for it.
if cfg!(feature = "ssl") {
if let Some(openssl_root_dir) = openssl_root_dir() {
println!("cargo:rustc-link-search={}/lib", openssl_root_dir);
}

// See if static SSL linkage was requested
let linkage = match env::var("OPENSSL_STATIC")
.as_ref()
.map(|s| s.as_str())
{
Ok("0") => "",
Ok(_) => "=static",
Err(_) => ""
};

let prefix = if is_msvc() { "lib" } else { "" };

println!("cargo:rustc-link-lib{}={}ssl", linkage, prefix);
println!("cargo:rustc-link-lib{}={}crypto", linkage, prefix);

if is_windows() {
if !is_msvc() {
// required for mingw builds
println!("cargo:rustc-link-lib{}=crypt32", linkage);
println!("cargo:rustc-link-lib{}=rpcrt4", linkage);
}
println!("cargo:rustc-link-lib=User32");
}
}

// we add the folder where all the libraries are built to the path search
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static={}", link_lib);
Expand Down
2 changes: 1 addition & 1 deletion paho-mqtt-sys/paho.mqtt.c
82 changes: 65 additions & 17 deletions paho-mqtt-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

// Weirdness in the bindgen bindings
#![allow(deref_nullptr)]

// Temporary
#![allow(dead_code)]

use std::ptr;
use std::fmt;
use std::os::raw::{c_char, c_int};
use std::ptr;

#[cfg(feature = "ssl")]
mod __make_openssl_linkage_work {
#[allow(unused_imports)]
use openssl_sys::*;
}

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

Expand All @@ -52,7 +56,12 @@ impl Default for MQTTAsync_createOptions {
/// Creates a client that can connect using MQTT v3.x or v5
fn default() -> Self {
Self {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'C' as c_char, b'O' as c_char],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'C' as c_char,
b'O' as c_char,
],
struct_version: CREATE_OPTIONS_STRUCT_VERSION,
sendWhileDisconnected: 0,
maxBufferedMessages: 100,
Expand Down Expand Up @@ -98,7 +107,12 @@ pub const CONNECT_OPTIONS_STRUCT_VERSION: i32 = 8;
impl Default for MQTTAsync_connectOptions {
fn default() -> Self {
Self {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'C' as c_char],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'C' as c_char,
],
struct_version: CONNECT_OPTIONS_STRUCT_VERSION,
keepAliveInterval: 60,
cleansession: 1,
Expand Down Expand Up @@ -181,7 +195,12 @@ pub const WILL_OPTIONS_STRUCT_VERSION: i32 = 1;
impl Default for MQTTAsync_willOptions {
fn default() -> MQTTAsync_willOptions {
MQTTAsync_willOptions {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'W' as c_char ],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'W' as c_char,
],
struct_version: WILL_OPTIONS_STRUCT_VERSION,
topicName: ptr::null(),
message: ptr::null(),
Expand All @@ -190,7 +209,7 @@ impl Default for MQTTAsync_willOptions {
payload: MQTTAsync_willOptions__bindgen_ty_1 {
len: 0,
data: ptr::null(),
}
},
}
}
}
Expand All @@ -201,7 +220,12 @@ pub const SSL_OPTIONS_STRUCT_VERSION: i32 = 5;
impl Default for MQTTAsync_SSLOptions {
fn default() -> MQTTAsync_SSLOptions {
MQTTAsync_SSLOptions {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'S' as c_char ],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'S' as c_char,
],
struct_version: SSL_OPTIONS_STRUCT_VERSION,
trustStore: ptr::null(),
keyStore: ptr::null(),
Expand Down Expand Up @@ -230,7 +254,12 @@ pub const SUBSCRIBE_OPTIONS_STRUCT_VERSION: i32 = 0;
impl Default for MQTTSubscribe_options {
fn default() -> MQTTSubscribe_options {
MQTTSubscribe_options {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'S' as c_char, b'O' as c_char ],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'S' as c_char,
b'O' as c_char,
],
struct_version: SUBSCRIBE_OPTIONS_STRUCT_VERSION,
noLocal: 0,
retainAsPublished: 0,
Expand All @@ -245,7 +274,12 @@ pub const RESPONSE_OPTIONS_STRUCT_VERSION: i32 = 1;
impl Default for MQTTAsync_responseOptions {
fn default() -> MQTTAsync_responseOptions {
MQTTAsync_responseOptions {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'R' as c_char ],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'R' as c_char,
],
struct_version: RESPONSE_OPTIONS_STRUCT_VERSION,
onSuccess: None,
onFailure: None,
Expand Down Expand Up @@ -284,7 +318,10 @@ impl Default for MQTTProperties {

impl Default for MQTTLenString {
fn default() -> MQTTLenString {
MQTTLenString { len: 0, data: ptr::null_mut(), }
MQTTLenString {
len: 0,
data: ptr::null_mut(),
}
}
}

Expand All @@ -297,7 +334,12 @@ pub const MESSAGE_STRUCT_VERSION: i32 = 1;
impl Default for MQTTAsync_message {
fn default() -> MQTTAsync_message {
MQTTAsync_message {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'M' as c_char ],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'M' as c_char,
],
struct_version: MESSAGE_STRUCT_VERSION,
payloadlen: 0,
payload: ptr::null_mut(),
Expand All @@ -319,7 +361,12 @@ pub const DISCONNECT_OPTIONS_STRUCT_VERSION: i32 = 1;
impl Default for MQTTAsync_disconnectOptions {
fn default() -> MQTTAsync_disconnectOptions {
MQTTAsync_disconnectOptions {
struct_id: [ b'M' as c_char, b'Q' as c_char, b'T' as c_char, b'D' as c_char],
struct_id: [
b'M' as c_char,
b'Q' as c_char,
b'T' as c_char,
b'D' as c_char,
],
struct_version: DISCONNECT_OPTIONS_STRUCT_VERSION,
timeout: 0,
onSuccess: None,
Expand Down Expand Up @@ -363,14 +410,15 @@ impl MQTTAsync_nameValue {

impl Default for MQTTAsync_nameValue {
fn default() -> Self {
Self { name: ptr::null(), value: ptr::null() }
Self {
name: ptr::null(),
value: ptr::null(),
}
}
}

/////////////////////////////////////////////////////////////////////////////
// Unit Tests

#[cfg(test)]
mod tests {
}

mod tests {}