feat: add SWIG support for generating multi-language bindings#53
feat: add SWIG support for generating multi-language bindings#53
Conversation
Co-authored-by: fzipi <[email protected]>
|
@copilot add GitHub action tests for Java and python wrappers, add the examples to examples directory and keep tests consistent and covering all exported functions. |
Co-authored-by: jptosso <[email protected]>
Co-authored-by: jptosso <[email protected]>
Done in commit 2a8ac8c:
Functions not covered by the SWIG examples are |
@copilot add the language specific examples, we need a directory for Java and a directory for python following a similar example |
…use them Co-authored-by: jptosso <[email protected]>
Co-authored-by: jptosso <[email protected]>
Done in commit 0c80cc1. Each language directory is now self-contained:
The CI workflow now delegates to these Makefiles ( |
|
@copilot update the gha version releases for all actions to latest, and include always the sha of the release. |
Co-authored-by: fzipi <[email protected]>
Done in commit 2aa4462. All actions in both
|
There was a problem hiding this comment.
Pull request overview
Adds first-class SWIG support to libcoraza so connector authors can generate multi-language bindings from the existing C API, along with CI-verified Python and Java example bindings.
Changes:
- Introduces
coraza.iSWIG interface with language-specific typemaps (errors, byte buffers) and exclusions for callback APIs. - Adds optional SWIG detection + a conditional
make swigtarget in the autotools build. - Adds Python/Java SWIG example projects and a CI workflow that builds/runs them.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
coraza.i |
Defines the SWIG module, mirrors C API types, and adds typemaps for error handling + byte buffers. |
configure.ac |
Detects SWIG optionally and reports it in the configure summary. |
Makefile.am |
Adds a conditional swig make target controlled by HAVE_SWIG. |
README.md |
Documents SWIG prerequisites/build steps and fixes an LD_LIBRARY_PATH typo. |
examples/python/simple_get.py |
Python smoke-test script exercising SWIG-exported APIs (no callbacks). |
examples/python/README.md |
Python example build/run docs and function coverage table. |
examples/python/Makefile |
Builds the Python SWIG wrapper/extension against libcoraza. |
examples/java/SimpleGet.java |
Java smoke-test exercising SWIG-exported APIs (no callbacks). |
examples/java/README.md |
Java example build/run docs and function coverage table. |
examples/java/Makefile |
Builds the Java SWIG wrapper + JNI library and runs the example. |
.github/workflows/swig.yml |
CI jobs to generate/build/run Python and Java SWIG examples. |
.gitignore |
Ignores generated SWIG artifacts and un-ignores the example Makefiles. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: fzipi <[email protected]>
Done in commit ca2033c:
|
|
@copilot Looks like functions defined in the Go code can lose sync with the defined in the coraza.i file. Can this be solved automatically. E.g. adding something into the Makefile for autogeneration? If not, then add clear warnings in the origin Golang file to update the dependent swig interface file. |
Co-authored-by: fzipi <[email protected]>
Full auto-generation of
This can also be wired into CI by adding a |
|
@jptosso Can you review this one? |
libcoraza needs SWIG compatibility to enable connector authors to generate bindings for Python, Ruby, Java, PHP, Perl, and other languages from the existing C API.
Changes
coraza.i— SWIG interface file:_GoString_,GoInt, etc.) to SWIGchar **eroutput parameters hidden from target language via typemaps; raises a runtime exception with the actual error message on failure (language-specific:SWIG_JavaThrowExceptionfor Java,SWIG_Error+free+SWIG_failfor others) — C error string is freed after capture to prevent memory leaks%newobjectoncoraza_matched_rule_get_error_logso target language runtime owns and frees the returned stringcoraza_add_debug_log_callback/coraza_add_error_callbackexcluded via%ignore— function pointer handling is inherently language-specific and must be addressed per-bindingbytes/bytearraytypemap: collapses(const unsigned char *data, int length)into a single native bytes argumentbyte[]typemap: same pair mapped to a singlebyte[]argumentSystem.loadLibrary("coraza_jni")automatically embedded in the generatedcorazaJNIclasslibcoraza/coraza.goandmake check-swig-synclibcoraza/coraza.go— Sync warning comment added before the first//exportannotation, directing developers to updatecoraza.iand runmake check-swig-syncwhen adding, removing, or changing exported functionsconfigure.ac— Optional SWIG detection viaAC_PATH_PROG+AM_CONDITIONAL([HAVE_SWIG]); shown in configure summaryMakefile.am— Conditionalswigtarget (present only when SWIG is found); target language and output directory configurable viaSWIG_LANG(default:python) andSWIG_OUTDIR(default:.):New
check-swig-synctarget verifies every//exportfunction inlibcoraza/coraza.gois accounted for incoraza.i(as anexterndeclaration or%ignore); exits non-zero if any are missing — safe to run in CI.examples/python/— Self-contained Python SWIG example directory:simple_get.py— covers all exported functions testable without a native callback, includingcoraza_append_request_body/coraza_append_response_bodyvia the native bytes typemapMakefile—makebuilds the SWIG extension;make runexecutes the example;make cleanremoves generated filesREADME.md— prerequisites, build/run instructions, and function coverage tableexamples/java/— Self-contained Java SWIG example directory:SimpleGet.java— covers all exported functions testable without a native callback usingbyte[]for body functionsMakefile—makegenerates the JNI library and compiles Java classes;make runexecutes the example;make cleanremoves generated artifactsREADME.md— prerequisites, build/run instructions, and function coverage table.github/workflows/swig.yml— CI workflow with two jobs (swig-python,swig-java), each tested against Go 1.24.x and 1.25.x on Ubuntu: installs dependencies, builds libcoraza, then delegates tomake -C examples/python run/make -C examples/java run; all actions pinned to latest release SHA.github/workflows/make-install.yml— Updated to pin all actions to latest release SHAREADME.md— Documents SWIG prerequisites, build steps, and a complete Python compilation example; also fixes pre-existing$LID_LIBRARY_PATHtypo.gitignore— Excludes generated SWIG wrapper sources, language modules,examples/java/gen/, and other per-directory build artifacts; tracksexamples/python/Makefileandexamples/java/Makefilevia negation rulesAll GitHub Actions across all workflows are pinned by full commit SHA alongside a version comment (e.g.
actions/setup-go@<sha> # v6.3.0,actions/checkout@<sha> # v6.0.2) for supply-chain security.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.