Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C link rust #3

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ edition = "2021"

[dependencies]
libc = "0.2"

[build-dependencies]
bindgen = "0.59.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to have the generated Rust interface included in the project, so consumers of the library don't have to depend on bindgen + its dependencies.

54 changes: 54 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
extern crate bindgen;

use std::path::PathBuf;
use std::{env, path::Path};
use std::process::Command;

fn main() {

let opentimeline_dir =
env::var("OPENTIMELINE_DIR").expect("'OPENTIMELINE_DIR' envvar is not defined");
let opentimeline_lib_path = Path::new(&opentimeline_dir).join("lib");
let opentimeline_include_path =
Path::new(&opentimeline_dir).join("include").join("opentimelineio");

let copentime_dir =
env::var("COPENTIME_DIR").expect("'COPENTIME_DIR' envvar is not defined");
let copentime_lib_path = Path::new(&copentime_dir).join("lib");
let copentime_include_path =
Path::new(&copentime_dir).join("include").join("copentime");

let copentimeline_dir =
env::var("COPENTIMELINE_DIR").expect("'COPENTIMELINE_DIR' envvar is not defined");
let copentimeline_lib_path = Path::new(&copentimeline_dir).join("lib");
let copentimeline_include_path =
Path::new(&copentimeline_dir).join("include").join("copentimelineio");

println!("cargo:rerun-if-changed=build.rs");

println!("cargo:rustc-link-search=native={}", opentimeline_lib_path.display()); // the "-L" flag
println!("cargo:rustc-link-lib=dylib=opentimelineio"); // the "-l" flag

println!("cargo:rustc-link-search=native={}", copentime_lib_path.display());
println!("cargo:rustc-link-lib=static=copentime");

println!("cargo:rustc-link-search=native={}", copentimeline_lib_path.display());
println!("cargo:rustc-link-lib=static=copentimelineio");

let bindings = bindgen::Builder::default()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the bindgen note.

.header("wrapper.h")
.allowlist_function("*")
.allowlist_type("*")
.allowlist_var("*")
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");


// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
51 changes: 51 additions & 0 deletions wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <copentime/errorStatus.h>
#include <copentime/optionalOpenTime.h>
#include <copentime/rationalTime.h>
#include <copentime/timeRange.h>
#include <copentime/timeTransform.h>

#include <copentimelineio/any.h>
#include <copentimelineio/anyDictionary.h>
#include <copentimelineio/anyVector.h>
#include <copentimelineio/clip.h>
#include <copentimelineio/composable.h>
#include <copentimelineio/composableRetainerVector.h>
#include <copentimelineio/composableVector.h>
#include <copentimelineio/composition.h>
#include <copentimelineio/deserialization.h>
#include <copentimelineio/effect.h>
#include <copentimelineio/effectRetainerVector.h>
#include <copentimelineio/effectVector.h>
#include <copentimelineio/errorStatus.h>
#include <copentimelineio/externalReference.h>
#include <copentimelineio/freezeFrame.h>
#include <copentimelineio/gap.h>
#include <copentimelineio/generatorReference.h>
#include <copentimelineio/item.h>
#include <copentimelineio/linearTimeWarp.h>
#include <copentimelineio/mapComposableTimeRange.h>
#include <copentimelineio/marker.h>
#include <copentimelineio/markerRetainerVector.h>
#include <copentimelineio/markerVector.h>
#include <copentimelineio/mediaReference.h>
#include <copentimelineio/missingReference.h>
#include <copentimelineio/optionalPairRationalTime.h>
#include <copentimelineio/retainerPairComposable.h>
#include <copentimelineio/safely_typed_any.h>
#include <copentimelineio/serializableCollection.h>
#include <copentimelineio/serializableObject.h>
#include <copentimelineio/serializableObjectRetainerVector.h>
#include <copentimelineio/serializableObjectVector.h>
#include <copentimelineio/serializableObjectWithMetadata.h>
#include <copentimelineio/serialization.h>
#include <copentimelineio/stack.h>
#include <copentimelineio/stackAlgorithm.h>
#include <copentimelineio/timeEffect.h>
#include <copentimelineio/timeline.h>
#include <copentimelineio/track.h>
#include <copentimelineio/trackAlgorithm.h>
#include <copentimelineio/trackVector.h>
#include <copentimelineio/transition.h>
#include <copentimelineio/typeInfo.h>
#include <copentimelineio/typeRegistry.h>
#include <copentimelineio/unknownSchema.h>