mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-22 17:29:28 +00:00
A native C++ binding generator (`cpp_native.nim`), the C++ counterpart of the C
and Go native paths and companion to the CBOR `cpp.nim`. It emits
`<lib>_native.hpp`: an idiomatic C++ struct + `toC`/`fromC` per `{.ffi.}` type,
and a `<Lib>Node` class whose methods marshal typed args into / read typed
struct returns out of the native ABI (`<name>` entry points + flat C structs in
`<lib>.h`) — zero serialization. Wired into genBindings under
`targetLang=cpp` + `-d:ffiMode=native`; emits the native C header alongside so
the binding is self-contained. Task: `genbindings_cpp_native`.
First cut covers scalar/string/bool/nested-struct fields (create/version/echo);
seq/Option params are `// SKIPPED`, and native typed events are next. Filename
is `_native.hpp` for now to coexist with the CBOR `.hpp` (rename is a follow-up).
Verified end-to-end: the generated example builds and round-trips a typed
EchoResponse (`make run`).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
42 lines
1.1 KiB
Makefile
42 lines
1.1 KiB
Makefile
# Build + run the GENERATED native C++ example.
|
|
#
|
|
# make run # build the Nim dylib + the C++ driver against the generated hpp
|
|
# make clean
|
|
#
|
|
# Regenerate the bindings with `nimble genbindings_cpp_native` (from the repo
|
|
# root). The Nim library is compiled from the repo root so its vendored Nimble
|
|
# dependencies resolve.
|
|
|
|
REPO_ROOT := $(abspath ../../..)
|
|
NIM_SRC := $(REPO_ROOT)/examples/timer/timer.nim
|
|
|
|
UNAME_S := $(shell uname -s)
|
|
ifeq ($(UNAME_S),Darwin)
|
|
LIBNAME := libmy_timer.dylib
|
|
RPATH := -Wl,-rpath,.
|
|
else
|
|
LIBNAME := libmy_timer.so
|
|
RPATH := -Wl,-rpath,'$$ORIGIN'
|
|
endif
|
|
|
|
CXX ?= c++
|
|
CXXFLAGS ?= -std=c++17 -Wall -Wextra -O2 -I.
|
|
NIMFLAGS := --mm:orc -d:chronicles_log_level=WARN --app:lib --noMain \
|
|
--nimMainPrefix:libmy_timer
|
|
|
|
.PHONY: all run clean
|
|
|
|
all: example
|
|
|
|
$(LIBNAME):
|
|
cd $(REPO_ROOT) && nim c $(NIMFLAGS) -o:$(CURDIR)/$(LIBNAME) $(NIM_SRC)
|
|
|
|
example: main.cpp my_timer_native.hpp my_timer.h $(LIBNAME)
|
|
$(CXX) $(CXXFLAGS) main.cpp -L. -lmy_timer $(RPATH) -o example
|
|
|
|
run: example
|
|
./example
|
|
|
|
clean:
|
|
rm -f example $(LIBNAME)
|