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

WIP: use cargo-c #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/target/
**/*.rs.bk
Cargo.lock
Ex-0.1.gir
Ex-0.1.typelib
Ex-0.1.vapi
test-c
test-vala
/include/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "cbindgen"]
path = cbindgen
url = https://github.com/elmarco/cbindgen.git
branch = gbindgen
26 changes: 23 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,28 @@ libc = "0.2"
glib = { git = "https://github.com/gtk-rs/gtk-rs-core" }
once_cell = "1.0"

[lib]
crate-type = ["cdylib"]

[features]
bindings = []
capi = [] # needed by cargo-c

[build-dependencies]
cargo_metadata = "0.13.1"
cbindgen = { path = "cbindgen", features = ["gobject"] }

[package.metadata.capi.library]
rustflags = "-Cpanic=abort"

[package.metadata.capi.header]
name = "ex" # let's make it match the namespace
subdirectory = "gobject-example-0.1"
generation = false # gobject cbindgen support is missing atm

[package.metadata.capi.pkg_config]
filename = "gobject-example-0.1"
requires = "gobject-2.0"

[workspace]
members = [
"cbindgen",
".",
]
6 changes: 0 additions & 6 deletions Ex-0.1.pc

This file was deleted.

83 changes: 42 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
HEADERS = \
include/ex/ex.h \
include/ex/flags.h \
include/ex/color.h \
include/ex/foo.h \
include/ex/bar.h \
include/ex/nameable.h \
include/ex/rstring.h \
include/ex/shared-rstring.h \
HEADER = inst/usr/include/gobject-example-0.1/ex.h
GIR = inst/usr/share/gir-1.0/Ex-0.1.gir
TYPELIB = inst/usr/lib64/girepository-1.0/Ex-0.1.typelib
VAPI = inst/usr/share/vala/vapi/gobject-example-0.1.vapi

RUST_SOURCES = \
src/lib.rs \
src/color/ffi.rs \
src/color/imp.rs \
src/color/mod.rs \
src/error/ffi.rs \
src/error/imp.rs \
src/error/mod.rs \
src/flags/ffi.rs \
src/flags/imp.rs \
src/flags/mod.rs \
Expand All @@ -32,66 +30,69 @@ RUST_SOURCES = \
src/shared_rstring/imp.rs \
src/shared_rstring/mod.rs

all: Ex-0.1.gir Ex-0.1.typelib Ex-0.1.vapi
all: $(GIR) $(TYPELIB) $(VAPI)

export PKG_CONFIG_PATH=$(PWD)
export GI_TYPELIB_PATH=$(PWD)
export LD_LIBRARY_PATH=$(PWD)/target/debug
export PKG_CONFIG_PATH=$(PWD)/inst/usr/lib64/pkgconfig
export GI_TYPELIB_PATH=$(PWD)/inst/usr/lib64/girepository-1.0
export LD_LIBRARY_PATH=$(PWD)/inst/usr/lib64

target/debug/libgobject_example.so: $(RUST_SOURCES)
cargo build
$(HEADER): $(RUST_SOURCES)
cargo cinstall --release --destdir=inst --prefix=/usr --libdir=/usr/lib64

Ex-0.1.gir: target/debug/libgobject_example.so $(HEADERS)
$(GIR): $(HEADER)
mkdir -p $(@D)
g-ir-scanner -v --warn-all \
--namespace Ex --nsversion=0.1 \
-Iinclude --c-include "ex/ex.h" \
--library=gobject_example --library-path=target/debug \
-Iinst/include --c-include "ex.h" \
--library=gobject_example --library-path=inst/usr/lib64 \
--include=GObject-2.0 -pkg gobject-2.0 \
--output $@ \
$(HEADERS)
$<

Ex-0.1.typelib: Ex-0.1.gir
g-ir-compiler \
--includedir=include \
$< -o $@
$(TYPELIB): $(GIR)
mkdir -p $(@D)
g-ir-compiler $< -o $@

Ex-0.1.vapi: Ex-0.1.gir
$(VAPI): $(GIR)
mkdir -p $(@D)
vapigen \
--library Ex-0.1 \
$<
--library gobject-example-0.1 \
$< -d $(@D)

clean:
rm -f Ex-0.1.typelib
rm -f Ex-0.1.gir
rm -f Ex-0.1.vapi test-vala
rm -rf test-c
cargo clean

run-python: Ex-0.1.typelib
run-python: $(TYPELIB)
python3 test.py

run-gjs: Ex-0.1.typelib
run-gjs: $(TYPELIB)
gjs test.js

test-vala: test.vala Ex-0.1.vapi
test-vala: test.vala $(VAPI)
valac -v \
--vapidir=$(PWD) \
--pkg=Ex-0.1 \
--vapidir=inst/usr/share/vala/vapi \
-X -Iinst/usr/include/gobject-example-0.1 \
-X -Linst/usr/lib64 \
--pkg=gobject-example-0.1 \
$< -o $@

run-vala: test-vala
./test-vala

test-c: test.c target/debug/libgobject_example.so Ex-0.1.pc $(HEADERS)
$(CC) -Wall $< `pkg-config --cflags --libs Ex-0.1` -o $@
test-c: test.c $(HEADER)
$(CC) -Wall \
$(shell pkg-config --cflags --libs gobject-example-0.1) \
-Iinst/usr/include/gobject-example-0.1 \
-Linst/usr/lib64 \
$< -o $@

run-c: test-c
./test-c

check:
cargo test

check-bindings: target/debug/libgobject_example.so
check-bindings: $(HEADER)
cargo test --features=bindings

install: $(HEADER)
sudo cp -r inst/* $(DESTDIR)/

check-all: check check-bindings run-c run-python run-gjs run-vala
58 changes: 55 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
use cargo_metadata::*;
use std::env;
use std::path::*;

use cbindgen::Builder;

fn main() {
let path = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let meta = MetadataCommand::new()
.manifest_path("./Cargo.toml")
.current_dir(&path)
.exec()
.unwrap();

println!("{:?}", &meta);

if cfg!(feature = "bindings") {
// assuming target dir isn't tweaked
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let profile = env::var("PROFILE").unwrap();
println!(
"cargo:rustc-link-search=native={}/target/{}",
manifest_dir, profile
"cargo:rustc-link-search=native={}/inst/usr/lib64",
manifest_dir,
);
println!("cargo:rustc-link-lib=dylib=gobject_example");
}

if cfg!(feature = "capi") {
let version = &meta.root_package().unwrap().version;
let name = &meta.root_package().unwrap().metadata["capi"]["header"]["name"]
.as_str()
.unwrap();
let out = std::env::var("OUT_DIR").unwrap();
let out = Path::new(&out);
let out_include = out.join("capi/include/");
std::fs::create_dir_all(&out_include).unwrap();

let mut config = cbindgen::Config::default();
let warning = config.autogen_warning.unwrap_or_default();
let version_info = format!(
r"
#define {0}_MAJOR_VERSION {1}
#define {0}_MINOR_VERSION {2}
#define {0}_PATCH_VERSION {3}

#define {0}_CHECK_VERSION(major,minor,path) \
({0}_MAJOR_VERSION > (major) || \
({0}_MAJOR_VERSION == (major) && {0}_MINOR_VERSION > (minor)) || \
({0}_MAJOR_VERSION == (major) && {0}_MINOR_VERSION == (minor) && \
{0}_PATCH_VERSION >= (patch)))
",
name.to_uppercase(),
version.major,
version.minor,
version.patch
);
config.autogen_warning = Some(warning + &version_info);

Builder::new()
.with_crate(&path)
.with_config(config)
.with_gobject(true)
.generate()
.unwrap()
.write_to_file(out_include.join(format!("{}.h", name)));
}
}
1 change: 1 addition & 0 deletions cbindgen
Submodule cbindgen added at 5443be
37 changes: 0 additions & 37 deletions include/ex/bar.h

This file was deleted.

21 changes: 0 additions & 21 deletions include/ex/color.h

This file was deleted.

12 changes: 0 additions & 12 deletions include/ex/ex.h

This file was deleted.

21 changes: 0 additions & 21 deletions include/ex/flags.h

This file was deleted.

39 changes: 0 additions & 39 deletions include/ex/foo.h

This file was deleted.

Loading