-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jethro Beekman
committed
Oct 30, 2018
1 parent
83b2cc2
commit a148760
Showing
50 changed files
with
4,583 additions
and
3,816 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,85 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
* | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
use bindgen; | ||
|
||
use std::fs::File; | ||
use std::io::{stderr,Write}; | ||
use std::io::{stderr, Write}; | ||
|
||
use headers; | ||
|
||
#[derive(Debug)] | ||
struct StderrLogger; | ||
|
||
impl bindgen::Logger for StderrLogger { | ||
fn error(&self, msg: &str) { let _=writeln!(stderr(),"Bindgen ERROR: {}",msg); } | ||
fn warn(&self, msg: &str) { let _=writeln!(stderr(),"Bindgen WARNING: {}",msg); } | ||
fn error(&self, msg: &str) { | ||
let _ = writeln!(stderr(), "Bindgen ERROR: {}", msg); | ||
} | ||
fn warn(&self, msg: &str) { | ||
let _ = writeln!(stderr(), "Bindgen WARNING: {}", msg); | ||
} | ||
} | ||
|
||
impl super::BuildConfig { | ||
pub fn bindgen(&self) { | ||
let header=self.out_dir.join("bindgen-input.h"); | ||
File::create(&header).and_then(|mut f|Ok( | ||
for h in headers::enabled_ordered() { | ||
try!(writeln!(f,"#include <mbedtls/{}>",h)); | ||
} | ||
)).expect("bindgen-input.h I/O error"); | ||
pub fn bindgen(&self) { | ||
let header = self.out_dir.join("bindgen-input.h"); | ||
File::create(&header) | ||
.and_then(|mut f| { | ||
Ok(for h in headers::enabled_ordered() { | ||
try!(writeln!(f, "#include <mbedtls/{}>", h)); | ||
}) | ||
}).expect("bindgen-input.h I/O error"); | ||
|
||
let include=self.mbedtls_src.join("include"); | ||
|
||
let logger=StderrLogger; | ||
let mut bindgen=bindgen::Builder::new(header.into_os_string().into_string().unwrap()); | ||
let bindings=bindgen | ||
.log(&logger) | ||
.clang_arg("-Dmbedtls_t_udbl=mbedtls_t_udbl;") // bindgen can't handle unused uint128 | ||
.clang_arg(format!("-DMBEDTLS_CONFIG_FILE=<{}>",self.config_h.to_str().expect("config.h UTF-8 error"))) | ||
.clang_arg(format!("-I{}",include.to_str().expect("include/ UTF-8 error"))) | ||
.match_pat(include.to_str().expect("include/ UTF-8 error")) | ||
.match_pat(self.config_h.to_str().expect("config.h UTF-8 error")) | ||
.use_core(true) | ||
.derive_debug(false) // buggy :( | ||
.ctypes_prefix(vec!["types".to_owned(),"raw_types".to_owned()]) | ||
.remove_prefix("mbedtls_") | ||
.rust_enums(false) | ||
.convert_macros(true) | ||
.macro_int_types(vec!["sint","sint","sint","slonglong","sint","sint","sint","slonglong"].into_iter()) | ||
.generate().expect("bindgen error"); | ||
let include = self.mbedtls_src.join("include"); | ||
|
||
let bindings_rs=self.out_dir.join("bindings.rs"); | ||
File::create(&bindings_rs).and_then(|mut f|{ | ||
try!(bindings.write(Box::new(&mut f))); | ||
f.write_all(b"use ::types::*;\n") // for FILE, time_t, etc. | ||
}).expect("bindings.rs I/O error"); | ||
let logger = StderrLogger; | ||
let mut bindgen = bindgen::Builder::new(header.into_os_string().into_string().unwrap()); | ||
let bindings = bindgen | ||
.log(&logger) | ||
.clang_arg("-Dmbedtls_t_udbl=mbedtls_t_udbl;") // bindgen can't handle unused uint128 | ||
.clang_arg(format!( | ||
"-DMBEDTLS_CONFIG_FILE=<{}>", | ||
self.config_h.to_str().expect("config.h UTF-8 error") | ||
)).clang_arg(format!( | ||
"-I{}", | ||
include.to_str().expect("include/ UTF-8 error") | ||
)).match_pat(include.to_str().expect("include/ UTF-8 error")) | ||
.match_pat(self.config_h.to_str().expect("config.h UTF-8 error")) | ||
.use_core(true) | ||
.derive_debug(false) // buggy :( | ||
.ctypes_prefix(vec!["types".to_owned(), "raw_types".to_owned()]) | ||
.remove_prefix("mbedtls_") | ||
.rust_enums(false) | ||
.convert_macros(true) | ||
.macro_int_types( | ||
vec![ | ||
"sint", | ||
"sint", | ||
"sint", | ||
"slonglong", | ||
"sint", | ||
"sint", | ||
"sint", | ||
"slonglong", | ||
].into_iter(), | ||
).generate() | ||
.expect("bindgen error"); | ||
|
||
let mod_bindings=self.out_dir.join("mod-bindings.rs"); | ||
File::create(&mod_bindings).and_then(|mut f| | ||
f.write_all(b"mod bindings;\n") | ||
).expect("mod-bindings.rs I/O error"); | ||
} | ||
let bindings_rs = self.out_dir.join("bindings.rs"); | ||
File::create(&bindings_rs) | ||
.and_then(|mut f| { | ||
try!(bindings.write(Box::new(&mut f))); | ||
f.write_all(b"use ::types::*;\n") // for FILE, time_t, etc. | ||
}).expect("bindings.rs I/O error"); | ||
|
||
let mod_bindings = self.out_dir.join("mod-bindings.rs"); | ||
File::create(&mod_bindings) | ||
.and_then(|mut f| f.write_all(b"mod bindings;\n")) | ||
.expect("mod-bindings.rs I/O error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,114 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
* | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
extern crate bindgen; | ||
extern crate cmake; | ||
|
||
mod config; | ||
mod headers; | ||
#[path="cmake.rs"] | ||
mod mod_cmake; | ||
#[path="bindgen.rs"] | ||
#[path = "bindgen.rs"] | ||
mod mod_bindgen; | ||
#[path = "cmake.rs"] | ||
mod mod_cmake; | ||
|
||
use std::collections::HashMap; | ||
use std::path::{Path,PathBuf}; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::{Path, PathBuf}; | ||
|
||
pub fn have_feature(feature: &'static str) -> bool { | ||
env::var_os(format!("CARGO_FEATURE_{}",feature).to_uppercase().replace("-","_")).is_some() | ||
env::var_os( | ||
format!("CARGO_FEATURE_{}", feature) | ||
.to_uppercase() | ||
.replace("-", "_"), | ||
).is_some() | ||
} | ||
|
||
struct BuildConfig { | ||
out_dir: PathBuf, | ||
mbedtls_src: PathBuf, | ||
config_h: PathBuf, | ||
out_dir: PathBuf, | ||
mbedtls_src: PathBuf, | ||
config_h: PathBuf, | ||
} | ||
|
||
impl BuildConfig { | ||
fn create_config_h(&self) { | ||
let mut defines=config::DEFAULT_DEFINES.iter().cloned().collect::<HashMap<_,_>>(); | ||
for &(feat,def) in config::FEATURE_DEFINES { | ||
if have_feature(feat) { | ||
defines.insert(def.0,def.1); | ||
} | ||
} | ||
|
||
File::create(&self.config_h).and_then(|mut f|{ | ||
try!(f.write_all(config::PREFIX.as_bytes())); | ||
for (name,def) in defines { | ||
try!(f.write_all(def.define(name).as_bytes())); | ||
} | ||
if have_feature("custom_printf") { | ||
try!(writeln!(f,"int mbedtls_printf(const char *format, ...);")); | ||
} | ||
if have_feature("custom_has_support") { | ||
try!(writeln!(f,"int mbedtls_aesni_has_support( unsigned int what ) __attribute__((weak));")); | ||
try!(writeln!(f,"int mbedtls_padlock_has_support( int feature ) __attribute__((weak));")); | ||
} | ||
if have_feature("custom_threading") { | ||
try!(writeln!(f,"typedef void* mbedtls_threading_mutex_t;")); | ||
} | ||
f.write_all(config::SUFFIX.as_bytes()) | ||
}).expect("config.h I/O error"); | ||
} | ||
fn create_config_h(&self) { | ||
let mut defines = config::DEFAULT_DEFINES | ||
.iter() | ||
.cloned() | ||
.collect::<HashMap<_, _>>(); | ||
for &(feat, def) in config::FEATURE_DEFINES { | ||
if have_feature(feat) { | ||
defines.insert(def.0, def.1); | ||
} | ||
} | ||
|
||
File::create(&self.config_h) | ||
.and_then(|mut f| { | ||
try!(f.write_all(config::PREFIX.as_bytes())); | ||
for (name, def) in defines { | ||
try!(f.write_all(def.define(name).as_bytes())); | ||
} | ||
if have_feature("custom_printf") { | ||
try!(writeln!(f, "int mbedtls_printf(const char *format, ...);")); | ||
} | ||
if have_feature("custom_has_support") { | ||
try!(writeln!( | ||
f, | ||
"int mbedtls_aesni_has_support( unsigned int what ) __attribute__((weak));" | ||
)); | ||
try!(writeln!( | ||
f, | ||
"int mbedtls_padlock_has_support( int feature ) __attribute__((weak));" | ||
)); | ||
} | ||
if have_feature("custom_threading") { | ||
try!(writeln!(f, "typedef void* mbedtls_threading_mutex_t;")); | ||
} | ||
f.write_all(config::SUFFIX.as_bytes()) | ||
}).expect("config.h I/O error"); | ||
} | ||
|
||
fn print_rerun_files(&self) { | ||
fn print_rerun_files(&self) { | ||
println!("cargo:rerun-if-env-changed=RUST_MBEDTLS_SYS_SOURCE"); | ||
println!("cargo:rerun-if-changed={}",self.mbedtls_src.join("CMakeLists.txt").display()); | ||
let include=self.mbedtls_src.join(Path::new("include").join("mbedtls")); | ||
println!( | ||
"cargo:rerun-if-changed={}", | ||
self.mbedtls_src.join("CMakeLists.txt").display() | ||
); | ||
let include = self.mbedtls_src.join(Path::new("include").join("mbedtls")); | ||
for h in headers::enabled_ordered() { | ||
println!("cargo:rerun-if-changed={}",include.join(h).display()); | ||
} | ||
for f in self.mbedtls_src.join("library").read_dir().expect("read_dir failed") { | ||
println!("cargo:rerun-if-changed={}",f.expect("DirEntry failed").path().display()); | ||
} | ||
} | ||
println!("cargo:rerun-if-changed={}", include.join(h).display()); | ||
} | ||
for f in self | ||
.mbedtls_src | ||
.join("library") | ||
.read_dir() | ||
.expect("read_dir failed") | ||
{ | ||
println!( | ||
"cargo:rerun-if-changed={}", | ||
f.expect("DirEntry failed").path().display() | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let out_dir=PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR environment not set?")); | ||
let src=PathBuf::from(env::var("RUST_MBEDTLS_SYS_SOURCE").unwrap_or("vendor".to_owned())); | ||
let cfg=BuildConfig { | ||
config_h: out_dir.join("config.h"), | ||
out_dir: out_dir, | ||
mbedtls_src: src, | ||
}; | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR environment not set?")); | ||
let src = PathBuf::from(env::var("RUST_MBEDTLS_SYS_SOURCE").unwrap_or("vendor".to_owned())); | ||
let cfg = BuildConfig { | ||
config_h: out_dir.join("config.h"), | ||
out_dir: out_dir, | ||
mbedtls_src: src, | ||
}; | ||
|
||
cfg.create_config_h(); | ||
cfg.print_rerun_files(); | ||
cfg.cmake(); | ||
cfg.bindgen(); | ||
cfg.create_config_h(); | ||
cfg.print_rerun_files(); | ||
cfg.cmake(); | ||
cfg.bindgen(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,41 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
* | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
use cmake; | ||
|
||
use have_feature; | ||
|
||
impl super::BuildConfig { | ||
pub fn cmake(&self) { | ||
let mut cmk=cmake::Config::new(&self.mbedtls_src); | ||
cmk | ||
.cflag(format!(r#"-DMBEDTLS_CONFIG_FILE="<{}>""#,self.config_h.to_str().expect("config.h UTF-8 error"))) | ||
.define("ENABLE_PROGRAMS","OFF") | ||
.define("ENABLE_TESTING","OFF") | ||
.build_target("lib"); | ||
if !have_feature("std") || ::std::env::var("TARGET").map(|s|s=="x86_64-unknown-none-gnu")==Ok(true) { | ||
println!("cargo:rustc-link-lib=gcc"); | ||
cmk.cflag("-fno-builtin").cflag("-D_FORTIFY_SOURCE=0").cflag("-fno-stack-protector"); | ||
} | ||
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")); | ||
pub fn cmake(&self) { | ||
let mut cmk = cmake::Config::new(&self.mbedtls_src); | ||
cmk.cflag(format!( | ||
r#"-DMBEDTLS_CONFIG_FILE="<{}>""#, | ||
self.config_h.to_str().expect("config.h UTF-8 error") | ||
)).define("ENABLE_PROGRAMS", "OFF") | ||
.define("ENABLE_TESTING", "OFF") | ||
.build_target("lib"); | ||
if !have_feature("std") | ||
|| ::std::env::var("TARGET").map(|s| s == "x86_64-unknown-none-gnu") == Ok(true) | ||
{ | ||
println!("cargo:rustc-link-lib=gcc"); | ||
cmk.cflag("-fno-builtin") | ||
.cflag("-D_FORTIFY_SOURCE=0") | ||
.cflag("-fno-stack-protector"); | ||
} | ||
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") | ||
); | ||
println!("cargo:rustc-link-lib=mbedtls"); | ||
println!("cargo:rustc-link-lib=mbedx509"); | ||
println!("cargo:rustc-link-lib=mbedx509"); | ||
println!("cargo:rustc-link-lib=mbedcrypto"); | ||
} | ||
} | ||
} |
Oops, something went wrong.