Skip to content

Instantly share code, notes, and snippets.

@vadixidav
Created July 4, 2018 22:24
Show Gist options
  • Save vadixidav/ffe9f36469faecc95aeeef67c739b3c9 to your computer and use it in GitHub Desktop.
Save vadixidav/ffe9f36469faecc95aeeef67c739b3c9 to your computer and use it in GitHub Desktop.
extern crate cbindgen;
use std::env;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_namespace("cbindgen_test")
.with_language(cbindgen::Language::Cxx)
.generate()
.expect("Unable to generate bindings")
.write_to_file("cbindgen_test.h");
}
[package]
name = "cbindgen_test"
version = "0.2.0"
authors = ["Geordon Worley <vadixidav@gmail.com>"]
[lib]
crate-type = ["staticlib"]
[profile]
panic = "abort"
[dependencies]
[build-dependencies]
cbindgen = "0.6.0"
from conans import ConanFile, CMake, tools
class CbindgentestConan(ConanFile):
name = "cbindgen_test"
version = "0.2.0"
description = "Test generated C++ bindings for Rust"
settings = "os", "compiler", "build_type", "arch"
no_copy_source = True
def cargo_target(self):
return "i686-pc-windows-msvc" if self.settings.arch == "x86" else "x86_64-pc-windows-msvc"
def source(self):
self.run(
"git clone git@github.com:vadixidav/cbindgen_test.git . || git fetch")
self.run("git checkout v{}".format(self.version))
def build(self):
flags = "-C target-feature={}crt-static".format(
"+" if self.settings.compiler.runtime in ["MT", "MTd"] else "-")
with tools.environment_append({"CARGO_TARGET_DIR": self.build_folder, "RUSTFLAGS": flags}):
with tools.chdir(self.source_folder):
debrel = "--release" if self.settings.build_type == "Release" else ""
self.run(
"cargo build {} --target={}".format(debrel, self.cargo_target()))
def package(self):
mode = "debug" if self.settings.build_type == "Debug" else "release"
out_dir = "{}/{}".format(self.cargo_target(), mode)
self.copy("cbindgen_test.h", dst="include")
self.copy("cbindgen_test.dll", dst="bin", src=out_dir, keep_path=False)
self.copy("cbindgen_test.lib", dst="lib", src=out_dir, keep_path=False)
self.copy("cbindgen_test.so", dst="lib", src=out_dir, keep_path=False)
self.copy("cbindgen_test.dylib", dst="lib",
src=out_dir, keep_path=False)
self.copy("cbindgen_test.a", dst="lib", src=out_dir, keep_path=False)
def package_info(self):
self.cpp_info.libs = tools.collect_libs(self)
self.cpp_info.libs += ["Ws2_32", "Userenv"]
#[no_mangle]
pub extern "C" fn hello(a: i32) {
println!("Hello, {}!", a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment