Skip to content

Commit

Permalink
Make mbedtls compile on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijs Kwakkel committed Mar 20, 2023
1 parent 4029adb commit 745c2a4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
3 changes: 3 additions & 0 deletions mbedtls-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ libc = { version = "0.2.0", optional = true }
[target.'cfg(unix)'.dependencies]
libc = { version = "0.2.0" }

[target.'cfg(windows)'.dependencies]
libc = { version = "0.2.0" }

[build-dependencies]
bindgen = "0.58.0"
cmake = "0.1.17"
Expand Down
15 changes: 9 additions & 6 deletions mbedtls-sys/build/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ impl super::BuildConfig {
}

let mut cc = cc::Build::new();
cc.include(&self.mbedtls_include)
.flag(&format!(
"-DMBEDTLS_CONFIG_FILE=\"{}\"",
self.config_h.to_str().expect("config.h UTF-8 error")
));

if cc.get_compiler().is_like_msvc() {
cc.flag("--driver-mode=cl");
}
cc.include(&self.mbedtls_include).define(
"MBEDTLS_CONFIG_FILE",
Some(format!(r#""{}""#, self.config_h.to_str().expect("config.h UTF-8 error")).as_str()),
);
for cflag in &self.cflags {
cc.flag(cflag);
}
Expand Down Expand Up @@ -125,6 +126,8 @@ impl super::BuildConfig {
.allowlist_var("^(?i)mbedtls_.*")
.allowlist_recursively(false)
.blocklist_type("^mbedtls_time_t$")
.blocklist_function("^mbedtls_platform_set_vsnprintf$")
.blocklist_item("^mbedtls_vsnprintf$")
.use_core()
.ctypes_prefix("::types::raw_types")
.parse_callbacks(Box::new(MbedtlsParseCallbacks))
Expand Down
13 changes: 2 additions & 11 deletions mbedtls-sys/build/cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl super::BuildConfig {
))
.define("ENABLE_PROGRAMS", "OFF")
.define("ENABLE_TESTING", "OFF")
.build_target("lib");
.build_target("install");
for cflag in &self.cflags {
cmk.cflag(cflag);
}
Expand All @@ -40,16 +40,7 @@ impl super::BuildConfig {

let mut dst = cmk.build();

dst.push("build");
dst.push("library");
println!(
"cargo:rustc-link-search=native={}",
dst.to_str().expect("link-search UTF-8 error")
);

assert!(dst.pop());
dst.push("crypto");
dst.push("library");
dst.push("lib");
println!(
"cargo:rustc-link-search=native={}",
dst.to_str().expect("link-search UTF-8 error")
Expand Down
4 changes: 2 additions & 2 deletions mbedtls-sys/build/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ impl Features {
}
}
if let Some(components) = self.with_feature("std") {
if env_have_target_cfg("family", "unix") {
if env_have_target_cfg("family", "unix") || env_have_target_cfg("family", "windows") {
components.insert("net");
components.insert("fs");
components.insert("entropy");
}
}
if let Some(components) = self.with_feature("time") {
if !have_custom_gmtime_r && env_have_target_cfg("family", "unix") {
if !have_custom_gmtime_r && (env_have_target_cfg("family", "unix") || env_have_target_cfg("family", "windows")) {
components.insert("libc");
} else {
components.insert("custom");
Expand Down
2 changes: 1 addition & 1 deletion mbedtls-sys/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod raw_types {
}
}

#[cfg(unix)]
#[cfg(any(unix, windows))]
extern crate libc;

#[cfg(std_component = "fs")]
Expand Down
7 changes: 4 additions & 3 deletions mbedtls/src/pk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use crate::alloc_prelude::*;
use mbedtls_sys::*;

pub use mbedtls_sys::ecp_group_id;
use mbedtls_sys::types::raw_types::c_void;

use core::ptr;
Expand Down Expand Up @@ -41,9 +42,9 @@ pub use crate::ecp::EcGroup;
pub use dhparam::Dhm;

// SHA-256("Fortanix")[:4]
const CUSTOM_PK_TYPE: pk_type_t = 0x8b205408 as pk_type_t;
const CUSTOM_PK_TYPE: pk_type_t = 0x8b205408u32 as pk_type_t;

const RAW_RSA_DECRYPT : i32 = 1040451858;
const RAW_RSA_DECRYPT: i32 = 1040451858;

define!(
#[c_ty(pk_type_t)]
Expand Down Expand Up @@ -116,7 +117,7 @@ unsafe extern "C" fn free_custom_pk_ctx(p: *mut c_void) {
let _ = Box::from_raw(p as *mut CustomPkContext);
}

extern "C" fn custom_pk_can_do(_t: u32) -> i32 {
extern "C" fn custom_pk_can_do(_t: pk_type_t) -> i32 {
0
}

Expand Down
8 changes: 7 additions & 1 deletion mbedtls/src/rust_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#include <stdio.h>
#include <stdarg.h>
#ifdef _WIN32
#define alloca _alloca
#include <malloc.h>
#else
#include <alloca.h>
#endif

extern void mbedtls_log(const char* msg);

Expand All @@ -22,7 +28,7 @@ extern int mbedtls_printf(const char *fmt, ...) {
return -1;

n++;
char p[n];
char *p = alloca(n);

va_start(ap,fmt);
n=vsnprintf(p,n,fmt,ap);
Expand Down

0 comments on commit 745c2a4

Please sign in to comment.