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

Feat: C Bindings #30

Merged
merged 24 commits into from
Sep 15, 2023
Merged
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
11 changes: 11 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ jobs:
run: npm install
- name: Run Tests
run: npm run test
c:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Update APT
run: sudo apt-get update -y
- name: Install Valgrind
run: sudo apt-get install valgrind -y
- name: Run Tests
run: make clib_test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ coverage
node_modules

dist/

*.h-e
88 changes: 88 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
VERSION=$(shell cat $(ROOT_DIR)/version/current_version)
VERSION_MAJOR=$(shell echo $(VERSION) | cut -c2)
VERSION_MINOR=$(shell echo $(VERSION) | cut -c4)
VERSION_MICRO=$(shell echo $(VERSION) | cut -c6)
CLIB_SO=libpolyglot
CLIB_SO_DEV=$(CLIB_SO).so
CLIB_SO_MAN=$(CLIB_SO_DEV).$(VERSION_MAJOR)
CLIB_SO_FULL=$(CLIB_SO_DEV).$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_MICRO)
CLIB_HEADER=polyglot.h
CLIB_SO_DEV_RELEASE=c_bindings/target/release/$(CLIB_SO_DEV)
CLIB_SO_DEV_DEBUG=c_bindings/target/debug/$(CLIB_SO_DEV)
CLIB_PKG_CONFIG=polyglot.pc
PREFIX ?= /usr/local

OS_NAME := $(shell uname -s | tr A-Z a-z)

CPU_BITS = $(shell getconf LONG_BIT)
ifeq ($(CPU_BITS), 32)
LIBDIR ?= $(PREFIX)/lib
else
LIBDIR ?= $(PREFIX)/lib$(CPU_BITS)
endif

INCLUDE_DIR ?= $(PREFIX)/include
PKG_CONFIG_LIBDIR ?= $(LIBDIR)/pkgconfig
MAN_DIR ?= $(PREFIX)/share/man

RELEASE ?=0

.PHONY: clib_debug
clib_debug:
cd c_bindings && cargo build --all && ([ -f ./target/debug/$(CLIB_SO).dylib ] && (echo "DYLIB exists, copying" && cp ./target/debug/$(CLIB_SO).dylib ./target/debug/$(CLIB_SO_DEV)) || echo "DYLIB does not exist, ignoring") && ln -sfv target/debug/$(CLIB_SO_DEV) target/debug/$(CLIB_SO_FULL) && ln -sfv target/debug/$(CLIB_SO_DEV) target/debug/$(CLIB_SO_MAN) && cd -

$(CLIB_SO_DEV_RELEASE):
cd c_bindings && cargo build --all --release && ([ -f ./target/release/$(CLIB_SO).dylib ] && (echo "DYLIB exists, copying" && cp ./target/release/$(CLIB_SO).dylib ./target/release/$(CLIB_SO_DEV)) || echo "DYLIB does not exist, ignoring") && cd -

$(CLIB_SO_DEV_DEBUG): clib_debug

clib: $(CLIB_HEADER) $(CLIB_SO_DEV_RELEASE) $(CLIB_PKG_CONFIG)

.PHONY: $(CLIB_HEADER)
$(CLIB_HEADER): $(CLIB_HEADER).in
cp $(CLIB_HEADER).in $(CLIB_HEADER)
sed -i -e 's/@_VERSION_MAJOR@/$(VERSION_MAJOR)/' \
$(CLIB_HEADER)
sed -i -e 's/@_VERSION_MINOR@/$(VERSION_MINOR)/' \
$(CLIB_HEADER)
sed -i -e 's/@_VERSION_MICRO@/$(VERSION_MICRO)/' \
$(CLIB_HEADER)

.PHONY: $(CLIB_PKG_CONFIG)
$(CLIB_PKG_CONFIG): $(CLIB_PKG_CONFIG).in
cp $(CLIB_PKG_CONFIG).in $(CLIB_PKG_CONFIG)
sed -i -e 's|@_VERSION_MAJOR@|$(VERSION_MAJOR)|' $(CLIB_PKG_CONFIG)
sed -i -e 's|@_VERSION_MINOR@|$(VERSION_MINOR)|' $(CLIB_PKG_CONFIG)
sed -i -e 's|@_VERSION_MICRO@|$(VERSION_MICRO)|' $(CLIB_PKG_CONFIG)
sed -i -e 's|@PREFIX@|$(PREFIX)|' $(CLIB_PKG_CONFIG)
sed -i -e 's|@LIBDIR@|$(LIBDIR)|' $(CLIB_PKG_CONFIG)
sed -i -e 's|@INCLUDE_DIR@|$(INCLUDE_DIR)|' $(CLIB_PKG_CONFIG)

.PHONY: clib_test
clib_test: $(CLIB_SO_DEV_DEBUG) $(CLIB_HEADER)
$(eval TMPDIR := $(shell mktemp -d))
cp $(CLIB_SO_DEV_DEBUG) $(TMPDIR)/$(CLIB_SO_FULL)
ln -sfv $(TMPDIR)/$(CLIB_SO_FULL) $(TMPDIR)/$(CLIB_SO_MAN)
ln -sfv $(TMPDIR)/$(CLIB_SO_FULL) $(TMPDIR)/$(CLIB_SO_DEV)
cp $(CLIB_HEADER) $(TMPDIR)/$(shell basename $(CLIB_HEADER))
gcc -g -Wall -Wextra -L$(TMPDIR) -I$(TMPDIR) -o $(TMPDIR)/polyglot_test c_bindings/tests/polyglot_test.c -lpolyglot
if [ $(OS_NAME) = "darwin" ]; then (echo "Using Leaks on Darwin" && LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(TMPDIR) leaks --atExit -- $(TMPDIR)/polyglot_test); else (echo "Using Valgrind on Unix" && LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(TMPDIR) valgrind --trace-children=yes --leak-check=full --error-exitcode=1 $(TMPDIR)/polyglot_test); fi
rm -rf $(TMPDIR)

install: clib
mkdir -p $(DESTDIR)$(LIBDIR)/$(CLIB_SO_FULL)
install -p -m755 $(CLIB_SO_DEV_RELEASE) $(DESTDIR)$(LIBDIR)/$(CLIB_SO_FULL)
ln -sfv $(CLIB_SO_FULL) $(DESTDIR)$(LIBDIR)/$(CLIB_SO_MAN)
ln -sfv $(CLIB_SO_FULL) $(DESTDIR)$(LIBDIR)/$(CLIB_SO_DEV)
mkdir -p $(DESTDIR)$(INCLUDE_DIR)/$(shell basename $(CLIB_HEADER))
install -p -v -m644 $(CLIB_HEADER) $(DESTDIR)$(INCLUDE_DIR)/$(shell basename $(CLIB_HEADER))
mkdir -p $(DESTDIR)$(PKG_CONFIG_LIBDIR)/$(shell basename $(CLIB_PKG_CONFIG))
install -p -v -m644 $(CLIB_PKG_CONFIG) $(DESTDIR)$(PKG_CONFIG_LIBDIR)/$(shell basename $(CLIB_PKG_CONFIG))

uninstall:
- rm -rfv $(DESTDIR)$(LIBDIR)/$(CLIB_SO_DEV)
- rm -rfv $(DESTDIR)$(LIBDIR)/$(CLIB_SO_MAN)
- rm -rfv $(DESTDIR)$(LIBDIR)/$(CLIB_SO_FULL)
- rm -rfv $(DESTDIR)$(INCLUDE_DIR)/$(shell basename $(CLIB_HEADER))
- rm -rfv $(DESTDIR)$(INCLUDE_DIR)/$(shell basename $(CLIB_PKG_CONFIG))
22 changes: 22 additions & 0 deletions c_bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "c_bindings"
version = "1.1.3"
edition = "2021"
description="A high-performance serialization framework used for encoding and decoding arbitrary datastructures across languages."
license = "Apache-2.0"
repository = "https://github.com/loopholelabs/polyglot"
readme = "README.md"
keywords = ["polyglot", "serialization", "deserialization", "encoding", "decoding"]

[lib]
path = "lib.rs"
name = "polyglot"
crate-type = ["cdylib"]

[dependencies]
polyglot_rs = { path = "../" }

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
Loading