forked from mozilla/mp4parse-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move mp4parse_capi's build_ffi_test to new test_ffi crate.
The existing process invoked a Makefile from a Rust test, building test.cc and a statically linkable mp4parse_capi, then running a series of C API tests. This wasn't very portable due the reliable on a Makefile, which also made assumptions about the layout of cargo's build artifacts. The new process introduces a new crate (test_ffi) and changes mp4parse_capi to additionally build as a cdylib. A build.rs in test_ffi builds test.cc (which must now be written with a simple library API, rather than as a binary with a main() due to cc-rs limitations) into a static libtest, which is then linked to test_ffi's Rust binary in main.rs. test.cc is now split into run_main for the binary dumping tool and test_main for the API tests. `cargo run -p test_ffi path/to/a.mp4` invokes run_main, and test_ffi's ffi_test invokes test_main. Fixes mozilla#197
- Loading branch information
Showing
11 changed files
with
107 additions
and
98 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"mp4parse", | ||
"mp4parse_capi", | ||
"test_ffi", | ||
] | ||
|
||
[profile.release] | ||
|
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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "test_ffi" | ||
version = "0.1.0" | ||
authors = ["Matthew Gregan <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
mp4parse_capi = { path = "../mp4parse_capi" } | ||
|
||
[build-dependencies] | ||
mp4parse_capi = { path = "../mp4parse_capi" } | ||
cc = "1.0" |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extern crate cc; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=src/main.rs"); | ||
println!("cargo:rerun-if-changed=src/test.cc"); | ||
|
||
cc::Build::new() | ||
.file("src/test.cc") | ||
.cpp(true) | ||
.flag_if_supported("-std=c++11") | ||
.include("../mp4parse_capi/include") | ||
.compile("libtest.a"); | ||
|
||
#[cfg(unix)] | ||
let suffix = ""; | ||
#[cfg(windows)] | ||
let suffix = ".dll"; | ||
println!("cargo:rustc-link-lib=dylib=mp4parse_capi{}", suffix); | ||
|
||
let profile = std::env::var("PROFILE").unwrap(); | ||
println!("cargo:rustc-link-search=native=target/{}", profile); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::os::raw::c_char; | ||
use std::ffi::CString; | ||
use std::convert::TryInto as _; | ||
|
||
fn main() { | ||
use std::os::raw::c_int; | ||
|
||
#[link(name="test", kind="static")] | ||
extern { fn run_main(npaths: c_int, paths: *const *const c_char) -> c_int; } | ||
|
||
|
||
let args: Vec<_> = std::env::args().map(|arg| CString::new(arg).unwrap()).collect(); | ||
let argv = { | ||
let mut a: Vec<_> = args.iter().map(|arg| arg.as_ptr()).collect(); | ||
a.push(std::ptr::null()); | ||
a | ||
}; | ||
|
||
let argc = argv.len() - 1; | ||
unsafe { run_main(argc.try_into().unwrap(), argv.as_ptr()); } | ||
} | ||
|
||
#[test] | ||
fn ffi_test() { | ||
use std::os::raw::c_void; | ||
|
||
extern { fn test_main(test_path: *const c_char) -> c_void; } | ||
|
||
let path = { | ||
let argv0 = std::env::args().next().unwrap(); | ||
let mut base = std::path::PathBuf::from(argv0); | ||
base.pop(); | ||
base.push("../../../mp4parse/tests/minimal.mp4"); | ||
let path = base.canonicalize().unwrap(); | ||
|
||
#[cfg(unix)] | ||
{ | ||
use std::os::unix::ffi::OsStrExt as _; | ||
CString::new(path.as_os_str().as_bytes()).unwrap() | ||
} | ||
#[cfg(windows)] | ||
{ | ||
CString::new(path.to_str().unwrap()).unwrap() | ||
} | ||
}; | ||
|
||
unsafe { test_main(path.as_ptr()); } | ||
} |
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