Skip to content
Closed
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
6 changes: 0 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ jobs:
command: |
source "$HOME/.cargo/env"
eval $(opam env)
OCAMLLIB=$(ocamlopt.opt -config | grep standard_library: | awk '{ print $2 }');export OCAMLLIB
cargo build
(cd ocamlpool/test&& ./ocamlpool_test.sh)
(cd rust/ocamlrep/test&& ./ocamlrep_test.sh)
cargo test

macos-build-and-test:
Expand All @@ -67,10 +64,7 @@ jobs:
command: |
source "$HOME/.cargo/env"
eval $(opam env)
OCAMLLIB=$(ocamlopt.opt -config | grep standard_library: | awk '{ print $2 }');export OCAMLLIB
cargo build
(cd ocamlpool/test&& ./ocamlpool_test.sh)
(cd rust/ocamlrep/test&& ./ocamlrep_test.sh)
cargo test

workflows:
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This project is stand-alone and requires or works with:
## Building ocamlrep
Assume an opam installation on a `4.14.0` like switch. From the repository root,
```bash
OCAMLLIB=$(ocamlopt.opt -config | grep standard_library: | awk '{ print $2 }'); export OCAMLLIB
cargo build
```

Expand Down
15 changes: 14 additions & 1 deletion ocamlpool/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
// --set-switch)"`) then to find the prevailing standard library caml
// headers, `OCAMLLIB=$(ocamlopt.opt -config | grep standard_library:
// | awk '{ print $2 }')`.
fn ocamllib_dir() -> std::path::PathBuf {
let mut sh = std::process::Command::new("sh");
sh.args([
"-c",
"ocamlopt.opt -config | grep standard_library: | awk '{ print $2 }'",
]);
std::path::Path::new(
std::str::from_utf8(&sh.output().unwrap().stdout)
.unwrap()
.trim(),
)
.to_path_buf()
}

fn main() {
cc::Build::new()
.include(env!("OCAMLLIB"))
.include(ocamllib_dir().as_path().to_str().unwrap())
.file("ocamlpool.c")
.compile("ocamlpool");
}
2 changes: 1 addition & 1 deletion ocamlpool/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[lib]
path = "ocamlpool_test.rs"
test = false
test = true # Don't forget to update the buck TARGETS file!
doctest = false
crate-type = ["lib", "staticlib"]

Expand Down
6 changes: 5 additions & 1 deletion ocamlpool/test/ocamlpool_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ external test : unit -> unit = "test"
* in order to allocate memory for ocaml. Calling rust from ocaml
* is a good way of ensuring this dependecy is built.
*)
let () = test ()
let () = begin
print_endline "[ocamlpool_test][info]: start";
test ();
print_endline "[ocamlpool_test][info]: finish"
end
79 changes: 79 additions & 0 deletions ocamlpool/test/ocamlpool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#![feature(exit_status_error)]

use ocamlrep_ocamlpool::ocaml_ffi;

extern "C" {
Expand Down Expand Up @@ -31,3 +33,80 @@ ocaml_ffi! {
}
}
}

#[cfg(test)]
mod tests {
use std::process::{Command, ExitStatusError};

fn cmd(prog: &str, args: &[&str]) -> Command {
let mut prog_cmd = Command::new(prog);
prog_cmd.args(args);
prog_cmd
}

fn ocamlopt_cmd(args: &[&str]) -> Command {
cmd("ocamlopt.opt", args)
}

fn sh_cmd(args: &[&str]) -> Command {
cmd("sh", args)
}

fn cargo_cmd(args: &[&str]) -> Command {
cmd("cargo", args)
}

fn workspace_dir(ds: &[&str]) -> std::path::PathBuf {
let mut cargo_cmd = cargo_cmd(&["locate-project", "--workspace", "--message-format=plain"]);
let output = cargo_cmd.output().unwrap().stdout;
let root_cargo_toml = std::path::Path::new(std::str::from_utf8(&output).unwrap().trim());
let mut p = root_cargo_toml.parent().unwrap().to_path_buf();
for d in ds {
p.push(d);
}
p
}

fn run(mut cmd: Command) -> Result<(), ExitStatusError> {
cmd.spawn().unwrap().wait().ok().unwrap().exit_ok()
}

fn fmt_exit_status_err(err: ExitStatusError) -> String {
format!("error status: {err}")
}

fn build_flavor() -> &'static str {
if cfg!(debug_assertions) {
"debug"
} else {
"release"
}
}

#[test]
fn ocamlpool_test() {
let compile_cmd = ocamlopt_cmd(&[
"-verbose",
"-c",
"ocamlpool_test.ml",
"-o",
"ocamlpool_test_ml.cmx",
]);
assert_eq!(run(compile_cmd).map_err(fmt_exit_status_err), Ok(()));
let link_cmd = ocamlopt_cmd(&[
"-verbose",
"-o",
"ocamlpool_test",
"ocamlpool_test_ml.cmx",
"-ccopt",
&("-L".to_owned() + workspace_dir(&["target", build_flavor()]).to_str().unwrap()),
"-cclib",
"-locamlpool_test",
"-cclib",
"-locamlpool",
]);
assert_eq!(run(link_cmd).map_err(fmt_exit_status_err), Ok(()));
let ocamlpool_test_cmd = sh_cmd(&["-c", "./ocamlpool_test"]);
assert_eq!(run(ocamlpool_test_cmd).map_err(fmt_exit_status_err), Ok(()));
}
}
13 changes: 0 additions & 13 deletions ocamlpool/test/ocamlpool_test.sh

This file was deleted.

12 changes: 0 additions & 12 deletions rust/ocamlrep/test/ocamlrep_test.sh

This file was deleted.

80 changes: 80 additions & 0 deletions rust/ocamlrep/test/test_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#![feature(exit_status_error)]

use std::cell::RefCell;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -286,3 +287,82 @@ pub extern "C" fn roundtrip_int64(value: usize) -> usize {
let i = unsafe { ocamlrep_caml_builtins::Int64::from_ocaml(value).unwrap() };
val(i)
}

#[cfg(test)]
mod tests {
use std::process::{Command, ExitStatusError};

fn cmd(prog: &str, args: &[&str]) -> Command {
let mut prog_cmd = Command::new(prog);
prog_cmd.args(args);
prog_cmd
}

fn ocamlopt_cmd(args: &[&str]) -> Command {
cmd("ocamlopt.opt", args)
}

fn sh_cmd(args: &[&str]) -> Command {
cmd("sh", args)
}

fn cargo_cmd(args: &[&str]) -> Command {
cmd("cargo", args)
}

fn workspace_dir(ds: &[&str]) -> std::path::PathBuf {
let mut cargo_cmd = cargo_cmd(&["locate-project", "--workspace", "--message-format=plain"]);
let output = cargo_cmd.output().unwrap().stdout;
let root_cargo_toml = std::path::Path::new(std::str::from_utf8(&output).unwrap().trim());
let mut p = root_cargo_toml.parent().unwrap().to_path_buf();
for d in ds {
p.push(d);
}
p
}

fn run(mut cmd: Command) -> Result<(), ExitStatusError> {
cmd.spawn().unwrap().wait().ok().unwrap().exit_ok()
}

fn fmt_exit_status_err(err: ExitStatusError) -> String {
format!("error status: {err}")
}

fn build_flavor() -> &'static str {
if cfg!(debug_assertions) {
"debug"
} else {
"release"
}
}
#[test]
fn ocamlrep_test() {
let mut compile_cmd = ocamlopt_cmd(&[
"-verbose",
"-c",
"test_ocamlrep.ml",
"-o",
"test_ocamlrep_ml.cmx",
]);
compile_cmd.current_dir("..");
assert_eq!(run(compile_cmd).map_err(fmt_exit_status_err), Ok(()));
let mut link_cmd = ocamlopt_cmd(&[
"-verbose",
"-o",
"ocamlrep_test",
"test_ocamlrep_ml.cmx",
"-ccopt",
&("-L".to_owned() + workspace_dir(&["target", build_flavor()]).to_str().unwrap()),
"-cclib",
"-ltest_bindings",
"-cclib",
"-locamlpool",
]);
link_cmd.current_dir("..");
assert_eq!(run(link_cmd).map_err(fmt_exit_status_err), Ok(()));
let mut ocamlpool_test_cmd = sh_cmd(&["-c", "./ocamlrep_test"]);
ocamlpool_test_cmd.current_dir("..");
assert_eq!(run(ocamlpool_test_cmd).map_err(fmt_exit_status_err), Ok(()));
}
}
2 changes: 1 addition & 1 deletion rust/ocamlrep/test/test_bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[lib]
path = "../test_bindings.rs"
test = false
test = true # Don't forget to update the buck TARGETS file!
doctest = false
crate-type = ["lib", "staticlib"]

Expand Down
6 changes: 5 additions & 1 deletion rust/ocamlrep/test/test_ocamlrep.ml
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,8 @@ let test_cases =

let main () = List.iter (fun test -> test ()) test_cases

let () = main ()
let () = begin
print_endline "[ocamlrep_test][info]: start";
main ();
print_endline "[ocamlrep_test][info]: finish"
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[lib]
path = "../../ocamlrep_marshal_ffi_bindings.rs"
test = false
#test = true Remember to update the buck TARGETS file!
doctest = false
crate-type = ["lib", "staticlib"]

Expand Down
Loading