Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Check fmt on whole workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jun 13, 2019
1 parent 9b61dfe commit cd372b4
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ before_cache:
before_install:
- if [ "$TRAVIS_OS_NAME" = linux ] && [ "$TRAVIS_RUST_VERSION" = stable ]; then
rustup component add rustfmt;
cargo fmt -- --check;
cargo fmt --all -- --check;
fi
115 changes: 86 additions & 29 deletions libunicorn-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use std::{
env,
ffi::{OsStr, OsString},
path::{Path, PathBuf},
process::Command,
env,
ffi::{OsString, OsStr},
};

use build_helper::rustc::{link_search, link_lib};
use build_helper::rustc::{link_lib, link_search};

fn get_vcvars_path_and_platform() -> (OsString, &'static str) {
let vswhere_output = Command::new(r"build_tools\vswhere.exe")
.args(&[
"-latest",
"-legacy",
"-property",
"installationPath"])
.args(&["-latest", "-legacy", "-property", "installationPath"])
.output()
.expect("failed to execute vswhere.exe");

Expand All @@ -22,39 +18,65 @@ fn get_vcvars_path_and_platform() -> (OsString, &'static str) {
}

let visual_studio_path = {
let vswhere_stdout = String::from_utf8(vswhere_output.stdout)
.expect("vswhere output is not valid UTF-8");
let vswhere_stdout =
String::from_utf8(vswhere_output.stdout).expect("vswhere output is not valid UTF-8");
String::from(vswhere_stdout.trim())
};

match build_helper::target::triple().arch() {
"i686" => {
let old_style_path = [&visual_studio_path, "VC", "bin", "vcvars32.bat"].iter().collect::<PathBuf>();
let old_style_path = [&visual_studio_path, "VC", "bin", "vcvars32.bat"]
.iter()
.collect::<PathBuf>();
if old_style_path.is_file() {
return (old_style_path.into_os_string(), "Win32");
}

let new_style_path = [&visual_studio_path, "VC", "Auxiliary", "Build", "vcvars32.bat"].iter().collect::<PathBuf>();
let new_style_path = [
&visual_studio_path,
"VC",
"Auxiliary",
"Build",
"vcvars32.bat",
]
.iter()
.collect::<PathBuf>();
if new_style_path.is_file() {
return (new_style_path.into_os_string(), "Win32");
}

panic!("failed to locate 'vcvars32.bat'");
},
}
"x86_64" => {
let old_style_path = [&visual_studio_path, "VC", "bin", "x86_amd64", "vcvarsx86_amd64.bat"].iter().collect::<PathBuf>();
let old_style_path = [
&visual_studio_path,
"VC",
"bin",
"x86_amd64",
"vcvarsx86_amd64.bat",
]
.iter()
.collect::<PathBuf>();
if old_style_path.is_file() {
return (old_style_path.into_os_string(), "x64");
}

let new_style_path = [&visual_studio_path, "VC", "Auxiliary", "Build", "vcvarsx86_amd64.bat"].iter().collect::<PathBuf>();
let new_style_path = [
&visual_studio_path,
"VC",
"Auxiliary",
"Build",
"vcvarsx86_amd64.bat",
]
.iter()
.collect::<PathBuf>();
if new_style_path.is_file() {
return (new_style_path.into_os_string(), "x64");
}

panic!("failed to locate 'vcvarsx86_amd64.bat'");
},
arch => panic!("'{}' is not a valid architecture for MSVC builds", arch)
}
arch => panic!("'{}' is not a valid architecture for MSVC builds", arch),
}
}

Expand All @@ -63,7 +85,11 @@ fn main() {

if !build_helper::windows() {
if Command::new("pkg-config").output().is_ok()
&& pkg_config::Config::new().atleast_version("1.0.0").probe("unicorn").is_ok() {
&& pkg_config::Config::new()
.atleast_version("1.0.0")
.probe("unicorn")
.is_ok()
{
return;
}
}
Expand All @@ -80,11 +106,20 @@ fn main() {
_ => [""],
};

if build_helper::target::triple().env().unwrap_or("").contains("msvc") {
if build_helper::target::triple()
.env()
.unwrap_or("")
.contains("msvc")
{
let build_cmd_path: PathBuf = [
env::current_dir().expect("failed to retrieve current directory").as_path(),
env::current_dir()
.expect("failed to retrieve current directory")
.as_path(),
Path::new("build_tools"),
Path::new("msvc_build.bat")].iter().collect();
Path::new("msvc_build.bat"),
]
.iter()
.collect();

let platform_toolset = match env::var("PLATFORMTOOLSET") {
Ok(x) => x,
Expand All @@ -93,26 +128,48 @@ fn main() {

let (vcvars_path, platform) = get_vcvars_path_and_platform();
let status = match Command::new(build_cmd_path)
.args(&[&vcvars_path, OsStr::new(&out_dir), OsStr::new(&platform_toolset), OsStr::new(platform)])
.args(&[
&vcvars_path,
OsStr::new(&out_dir),
OsStr::new(&platform_toolset),
OsStr::new(platform),
])
.current_dir("unicorn")
.status() {
.status()
{
Ok(status) => status,
Err(e) => fail(&format!("failed to execute command: {}", e)),
};
if !status.success() {
fail(&format!("command did not execute successfully, got {}", status));
fail(&format!(
"command did not execute successfully, got {}",
status
));
}

link_search(Some(build_helper::SearchKind::Native), build_helper::out_dir());
link_search(
Some(build_helper::SearchKind::Native),
build_helper::out_dir(),
);
link_lib(Some(build_helper::LibKind::Static), "unicorn_static");
} else {
// TODO(sduquette): the package build should fail if this command fails.
let _ = Command::new("./make.sh").args(&make_args).current_dir("unicorn").status();
let _ = Command::new("./make.sh")
.args(&make_args)
.current_dir("unicorn")
.status();

let unicorn = "libunicorn.a";
let _ = Command::new("cp").current_dir("unicorn").arg(&unicorn).arg(&out_dir).status();

link_search(Some(build_helper::SearchKind::Native), build_helper::out_dir());
let _ = Command::new("cp")
.current_dir("unicorn")
.arg(&unicorn)
.arg(&out_dir)
.status();

link_search(
Some(build_helper::SearchKind::Native),
build_helper::out_dir(),
);
link_lib(Some(build_helper::LibKind::Static), "unicorn");
}
}
Expand Down
107 changes: 57 additions & 50 deletions libunicorn-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pub mod unicorn_const;

use crate::unicorn_const::{Arch, Error, HookType, MemRegion, Mode, Query};
use core::{fmt, slice};
use libc::{c_char, c_int, c_void};
use crate::unicorn_const::{Arch, MemRegion, Mode, Error, HookType, Query};

#[allow(non_camel_case_types)]
pub type uc_handle = libc::size_t;
Expand All @@ -21,62 +21,61 @@ extern "C" {
pub fn uc_free(mem: libc::size_t) -> Error;
pub fn uc_errno(engine: uc_handle) -> Error;
pub fn uc_strerror(error_code: Error) -> *const c_char;
pub fn uc_reg_write(engine: uc_handle,
regid: c_int,
value: *const c_void)
-> Error;
pub fn uc_reg_write(engine: uc_handle, regid: c_int, value: *const c_void) -> Error;
pub fn uc_reg_read(engine: uc_handle, regid: c_int, value: *mut c_void) -> Error;
pub fn uc_mem_write(engine: uc_handle,
address: u64,
bytes: *const u8,
size: libc::size_t)
-> Error;
pub fn uc_mem_read(engine: uc_handle,
address: u64,
bytes: *mut u8,
size: libc::size_t)
-> Error;
pub fn uc_mem_write(
engine: uc_handle,
address: u64,
bytes: *const u8,
size: libc::size_t,
) -> Error;
pub fn uc_mem_read(
engine: uc_handle,
address: u64,
bytes: *mut u8,
size: libc::size_t,
) -> Error;
pub fn uc_mem_map(engine: uc_handle, address: u64, size: libc::size_t, perms: u32) -> Error;
pub fn uc_mem_map_ptr(engine: uc_handle,
address: u64,
size: libc::size_t,
perms: u32,
ptr: *mut c_void)
-> Error;
pub fn uc_mem_map_ptr(
engine: uc_handle,
address: u64,
size: libc::size_t,
perms: u32,
ptr: *mut c_void,
) -> Error;
pub fn uc_mem_unmap(engine: uc_handle, address: u64, size: libc::size_t) -> Error;
pub fn uc_mem_protect(engine: uc_handle,
address: u64,
size: libc::size_t,
perms: u32)
-> Error;
pub fn uc_mem_regions(engine: uc_handle,
regions: *const *const MemRegion,
count: *mut u32)
-> Error;
pub fn uc_emu_start(engine: uc_handle,
begin: u64,
until: u64,
timeout: u64,
count: libc::size_t)
-> Error;
pub fn uc_mem_protect(engine: uc_handle, address: u64, size: libc::size_t, perms: u32)
-> Error;
pub fn uc_mem_regions(
engine: uc_handle,
regions: *const *const MemRegion,
count: *mut u32,
) -> Error;
pub fn uc_emu_start(
engine: uc_handle,
begin: u64,
until: u64,
timeout: u64,
count: libc::size_t,
) -> Error;
pub fn uc_emu_stop(engine: uc_handle) -> Error;
pub fn uc_hook_add(engine: uc_handle,
hook: *mut uc_hook,
hook_type: HookType,
callback: libc::size_t,
user_data: *mut libc::size_t,
begin: u64,
end: u64,
...)
-> Error;
pub fn uc_hook_add(
engine: uc_handle,
hook: *mut uc_hook,
hook_type: HookType,
callback: libc::size_t,
user_data: *mut libc::size_t,
begin: u64,
end: u64,
...
) -> Error;
pub fn uc_hook_del(engine: uc_handle, hook: uc_hook) -> Error;
pub fn uc_query(engine: uc_handle, query_type: Query, result: *mut libc::size_t) -> Error;
pub fn uc_context_alloc(engine: uc_handle, context: *mut uc_context) -> Error;
pub fn uc_context_save(engine: uc_handle, context: uc_context) -> Error;
pub fn uc_context_restore(engine: uc_handle, context: uc_context) -> Error;
}


impl Error {
pub fn msg(self) -> &'static str {
unsafe {
Expand All @@ -88,17 +87,25 @@ impl Error {

unsafe fn cstr_len(s: *const u8) -> usize {
let mut p = s;
while 0 != *p { p = p.add(1); }
while 0 != *p {
p = p.add(1);
}
p as usize - s as usize
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.msg().fmt(fmt) }
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.msg().fmt(fmt)
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &str { self.msg().as_bytes() }
fn description(&self) -> &str {
self.msg().as_bytes()
}

fn cause(&self) -> Option<&std::error::Error> { None }
fn cause(&self) -> Option<&std::error::Error> {
None
}
}
Loading

0 comments on commit cd372b4

Please sign in to comment.