Skip to content

Commit

Permalink
chore: build and install the C bindings library with cargo-c.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Mar 27, 2024
1 parent f60fa64 commit 9d26f2b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
18 changes: 17 additions & 1 deletion capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ readme.workspace = true
license.workspace = true
homepage.workspace = true

[features]
# The `capi` feature is required by `cargo-c`.
capi = []
default = ["capi"]

[lib]
name = "yara_x_capi"
crate-type = ["staticlib", "cdylib"]
Expand All @@ -18,4 +23,15 @@ crate-type = ["staticlib", "cdylib"]
yara-x = { workspace = true }

[build-dependencies]
cbindgen = { workspace = true }
cbindgen = { workspace = true }


# This section is used by `cargo-c`, for generating the header file and
[package.metadata.capi.header]
# Name of the header file, without the `.h` extension.
name = "yara-x"
# Install the header into a subdirectory with the name of the crate. This
# is enabled by default, pass `false` or "" to disable it.
subdirectory = ""
# Generate the header file with `cbindgen`.
generation = true
2 changes: 0 additions & 2 deletions capi/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


language = "C"
cpp_compat = false

Expand Down
44 changes: 39 additions & 5 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
/*! C bindings for the YARA-X library.
This crate defines the C-compatible API that C/C++ programs can use for
interfacing with the YARA-X Rust library. When this crate is built, the header
file `capi/include/yara-x.h` is generated automatically using [`cbindgen`][1],
together with dynamic-linking and static-linking versions of a `libyara-x-capi`
that can be found in the `target` directory.
interfacing with the YARA-X Rust library. A header file for this library
(`yara-x.h`) will be automatically generated by [`cbindgen`][1], during
compilation, together with dynamic-linking and static-linking versions of
the library.
# How to build and install
You will need [`cargo-c`][2] for building this library:
```text
cargo install cargo-c
```
Build and install:
```text
cargo cinstall -p yara-x-capi --release
```
The command above will put the library and header files in the correct path
in your system (usually `/usr/local/lib` and `/usr/local/include`, respectively),
and will generate a `.pc` file so that `pkg-config` knows about the library.
You can check if everything went fine by compiling a simple test program, like
this:
```text
cat <<EOF > test.c
#include <yara-x.h>
int main() {
YRX_RULES* rules;
yrx_compile("rule dummy { condition: true }", &rules);
yrx_rules_destroy(rules);
}
EOF
```
```text
gcc `pkg-config --cflags yara_x_capi` `pkg-config --libs yara_x_capi` test.c
```
This crate is not intended to be used by other Rust programs.
[1]: https://github.com/mozilla/cbindgen
[2]: https://github.com/lu-zero/cargo-c
*/

#![allow(non_camel_case_types)]
Expand Down
5 changes: 2 additions & 3 deletions go/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Package yara_x provides Go bindings to the YARA-X library.
package yara_x

// #cgo CFLAGS: -I${SRCDIR}/../capi/include
// #cgo !static_link LDFLAGS: -L${SRCDIR}/../target/release -lyara_x_capi
// #cgo static_link LDFLAGS: ${SRCDIR}/../target/release/libyara_x_capi.a
// #cgo !static_link pkg-config: yara_x_capi
// #cgo static_link pkg-config: --static yara_x_capi
// #import <yara-x.h>
import "C"
import (
Expand Down

0 comments on commit 9d26f2b

Please sign in to comment.