Bindings "filesystem" (#225)

* bindings structure

* missed some renaming

* add back the headers

* path fixes

* need to sleep at night

* windows path mystery is unfathomable
This commit is contained in:
Mamy Ratsimbazafy 2023-03-01 12:59:06 +01:00 committed by GitHub
parent 1cb6c3d9e1
commit 4dc2610557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 23 deletions

7
bindings/README.md Normal file
View File

@ -0,0 +1,7 @@
# Bindings generator
This folder holds the bindings generators for Constantine.
To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`.
Headers will be placed in "include" folder and static and dynamic libraries in "lib"

View File

@ -468,12 +468,12 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) =
# In the future, Constantine might use: # In the future, Constantine might use:
# - heap-allocated sequences and objects manually managed or managed by destructors for multithreading. # - heap-allocated sequences and objects manually managed or managed by destructors for multithreading.
# - heap-allocated strings for hex-string or decimal strings # - heap-allocated strings for hex-string or decimal strings
echo "Compiling dynamic library: bindings/generated/" & libName echo "Compiling dynamic library: lib/" & libName
exec "nim c -f " & flags & " --noMain -d:danger --app:lib --gc:arc " & exec "nim c -f " & flags & " --noMain -d:danger --app:lib --gc:arc " &
" --panics:on " & # Defects are not catchable " --panics:on " & # Defects are not catchable
" --verbosity:0 --hints:off --warnings:off " & " --verbosity:0 --hints:off --warnings:off " &
" --nimMainPrefix:" & prefixNimMain & " --nimMainPrefix:" & prefixNimMain &
" --out:" & libName & " --outdir:bindings/generated " & " --out:" & libName & " --outdir:lib " &
" --nimcache:nimcache/bindings/" & bindingsName & " --nimcache:nimcache/bindings/" & bindingsName &
" bindings/" & bindingsName & ".nim" " bindings/" & bindingsName & ".nim"
@ -483,9 +483,9 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) =
elif defined(macosx): elif defined(macosx):
compile "lib" & bindingsName & ".dylib.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'" compile "lib" & bindingsName & ".dylib.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'"
compile "lib" & bindingsName & ".dylib.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'" compile "lib" & bindingsName & ".dylib.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'"
exec "lipo bindings/generated/lib" & bindingsName & ".dylib.arm " & exec "lipo lib/lib" & bindingsName & ".dylib.arm " &
" bindings/generated/lib" & bindingsName & ".dylib.x64 " & " lib/lib" & bindingsName & ".dylib.x64 " &
" -output bindings/generated/lib" & bindingsName & ".dylib -create" " -output lib/lib" & bindingsName & ".dylib -create"
else: else:
compile "lib" & bindingsName & ".so" compile "lib" & bindingsName & ".so"
@ -499,12 +499,12 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) =
# In the future, Constantine might use: # In the future, Constantine might use:
# - heap-allocated sequences and objects manually managed or managed by destructors for multithreading. # - heap-allocated sequences and objects manually managed or managed by destructors for multithreading.
# - heap-allocated strings for hex-string or decimal strings # - heap-allocated strings for hex-string or decimal strings
echo "Compiling static library: bindings/generated/" & libName echo "Compiling static library: lib/" & libName
exec "nim c -f " & flags & " --noMain -d:danger --app:staticLib --gc:arc " & exec "nim c -f " & flags & " --noMain -d:danger --app:staticLib --gc:arc " &
" --panics:on " & # Defects are not catchable " --panics:on " & # Defects are not catchable
" --verbosity:0 --hints:off --warnings:off " & " --verbosity:0 --hints:off --warnings:off " &
" --nimMainPrefix:" & prefixNimMain & " --nimMainPrefix:" & prefixNimMain &
" --out:" & libName & " --outdir:bindings/generated " & " --out:" & libName & " --outdir:lib " &
" --nimcache:nimcache/bindings/" & bindingsName & " --nimcache:nimcache/bindings/" & bindingsName &
" bindings/" & bindingsName & ".nim" " bindings/" & bindingsName & ".nim"
@ -514,21 +514,21 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) =
elif defined(macosx): elif defined(macosx):
compile "lib" & bindingsName & ".a.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'" compile "lib" & bindingsName & ".a.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'"
compile "lib" & bindingsName & ".a.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'" compile "lib" & bindingsName & ".a.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'"
exec "lipo bindings/generated/lib" & bindingsName & ".a.arm " & exec "lipo lib/lib" & bindingsName & ".a.arm " &
" bindings/generated/lib" & bindingsName & ".a.x64 " & " lib/lib" & bindingsName & ".a.x64 " &
" -output bindings/generated/lib" & bindingsName & ".a -create" " -output lib/lib" & bindingsName & ".a -create"
else: else:
compile "lib" & bindingsName & ".a" compile "lib" & bindingsName & ".a"
proc genHeaders(bindingsName: string) = proc genHeaders(bindingsName: string) =
echo "Generating header: bindings/generated/" & bindingsName & ".h" echo "Generating header: include/" & bindingsName & ".h"
exec "nim c -d:release -d:CttGenerateHeaders " & exec "nim c -d:release -d:CttGenerateHeaders " &
" --verbosity:0 --hints:off --warnings:off " & " --verbosity:0 --hints:off --warnings:off " &
" --out:" & bindingsName & "_gen_header.exe --outdir:bindings/generated " & " --out:" & bindingsName & "_gen_header.exe --outdir:build " &
" --nimcache:nimcache/bindings/" & bindingsName & "_header" & " --nimcache:nimcache/bindings/" & bindingsName & "_header" &
" bindings/" & bindingsName & ".nim" " bindings/" & bindingsName & ".nim"
exec "bindings/generated/" & bindingsName & "_gen_header.exe bindings/generated" exec "build/" & bindingsName & "_gen_header.exe include"
proc genParallelCmdRunner() = proc genParallelCmdRunner() =
exec "nim c --verbosity:0 --hints:off --warnings:off -d:release --out:build/pararun --nimcache:nimcache/pararun helpers/pararun.nim" exec "nim c --verbosity:0 --hints:off --warnings:off -d:release --out:build/pararun --nimcache:nimcache/pararun helpers/pararun.nim"
@ -549,23 +549,23 @@ task test_bindings, "Test C bindings":
exec "mkdir -p build/testsuite" exec "mkdir -p build/testsuite"
echo "--> Testing dynamically linked library" echo "--> Testing dynamically linked library"
when not defined(windows): when not defined(windows):
exec "gcc -Ibindings/generated -Lbindings/generated -o build/testsuite/t_libctt_bls12_381_dl tests/bindings/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" exec "gcc -Iinclude -Llib -o build/testsuite/t_libctt_bls12_381_dl examples_c/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381"
exec "LD_LIBRARY_PATH=bindings/generated ./build/testsuite/t_libctt_bls12_381_dl" exec "LD_LIBRARY_PATH=lib ./build/testsuite/t_libctt_bls12_381_dl"
else: else:
# Put DLL near the exe as LD_LIBRARY_PATH doesn't work even in an POSIX compatible shell # Put DLL near the exe as LD_LIBRARY_PATH doesn't work even in an POSIX compatible shell
exec "gcc -Ibindings/generated -Lbindings/generated -o build/testsuite/t_libctt_bls12_381_dl.exe tests/bindings/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" exec "gcc -Iinclude -Llib -o build/testsuite/t_libctt_bls12_381_dl.exe examples_c/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381"
exec "./build/testsuite/t_libctt_bls12_381_dl.exe" exec "./build/testsuite/t_libctt_bls12_381_dl.exe"
echo "--> Testing statically linked library" echo "--> Testing statically linked library"
when not defined(windows): when not defined(windows):
# Beware MacOS annoying linker with regards to static libraries # Beware MacOS annoying linker with regards to static libraries
# The following standard way cannot be used on MacOS # The following standard way cannot be used on MacOS
# exec "gcc -Ibindings/generated -Lbindings/generated -o build/t_libctt_bls12_381_sl.exe tests/bindings/t_libctt_bls12_381.c -lgmp -Wl,-Bstatic -lconstantine_bls12_381 -Wl,-Bdynamic" # exec "gcc -Iinclude -Llib -o build/t_libctt_bls12_381_sl.exe examples_c/t_libctt_bls12_381.c -lgmp -Wl,-Bstatic -lconstantine_bls12_381 -Wl,-Bdynamic"
exec "gcc -Ibindings/generated -o build/testsuite/t_libctt_bls12_381_sl tests/bindings/t_libctt_bls12_381.c bindings/generated/libconstantine_bls12_381.a -lgmp" exec "gcc -Iinclude -o build/testsuite/t_libctt_bls12_381_sl examples_c/t_libctt_bls12_381.c lib/libconstantine_bls12_381.a -lgmp"
exec "./build/testsuite/t_libctt_bls12_381_sl" exec "./build/testsuite/t_libctt_bls12_381_sl"
else: else:
exec "gcc -Ibindings/generated -o build/testsuite/t_libctt_bls12_381_sl.exe tests/bindings/t_libctt_bls12_381.c bindings/generated/constantine_bls12_381.lib -lgmp" exec "gcc -Iinclude -o build/testsuite/t_libctt_bls12_381_sl.exe examples_c/t_libctt_bls12_381.c lib/constantine_bls12_381.lib -lgmp"
exec "./build/testsuite/t_libctt_bls12_381_sl.exe" exec "./build/testsuite/t_libctt_bls12_381_sl.exe"
task test, "Run all tests": task test, "Run all tests":

5
examples_c/README.md Normal file
View File

@ -0,0 +1,5 @@
# C bindings examples
This folder holds tests (prefixed with `t_`) and examples for the C bindings.
Headers are located in `include/` and the static and dynamic libraries in `lib/`

View File

@ -6,6 +6,9 @@
// * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). // * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
// at your option. This file may not be copied, modified, or distributed except according to those terms. // at your option. This file may not be copied, modified, or distributed except according to those terms.
// This is a test to ensure Constantine's modular arithmetic is consistent with GMP.
// While not intended as a tutorial, it showcases serialization, deserialization and computation.
#include <assert.h> #include <assert.h>
#include <gmp.h> #include <gmp.h>
#include <constantine_bls12_381.h> #include <constantine_bls12_381.h>
@ -34,7 +37,7 @@ void prologue(
mpz_ptr p, mpz_ptr p,
bls12381_fp* a_ctt, bls12381_fp* b_ctt, bls12381_fp* a_ctt, bls12381_fp* b_ctt,
byte a_buf[ByteLength], byte b_buf[ByteLength]) { byte a_buf[ByteLength], byte b_buf[ByteLength]) {
// Generate random value in the range 0 ..< 2^(bits-1) // Generate random value in the range 0 ..< 2^(bits-1)
mpz_urandomb(a, gmp_rng, BitLength); mpz_urandomb(a, gmp_rng, BitLength);
mpz_urandomb(b, gmp_rng, BitLength); mpz_urandomb(b, gmp_rng, BitLength);
@ -65,7 +68,7 @@ void epilogue(
mpz_ptr r, mpz_ptr a, mpz_ptr b, mpz_ptr r, mpz_ptr a, mpz_ptr b,
bls12381_fp* r_ctt, bls12381_fp* a_ctt, bls12381_fp* b_ctt, bls12381_fp* r_ctt, bls12381_fp* a_ctt, bls12381_fp* b_ctt,
char* operation) { char* operation) {
byte r_raw_gmp[ByteLength]; byte r_raw_gmp[ByteLength];
byte r_raw_ctt[ByteLength]; byte r_raw_ctt[ByteLength];
@ -98,10 +101,10 @@ void epilogue(
} }
int main(){ int main(){
// Initialize the runtime. For Constantine, it populates CPU runtime detection dispatch. // Initialize the runtime. For Constantine, it populates CPU runtime detection dispatch.
ctt_bls12381_init_NimMain(); ctt_bls12381_init_NimMain();
gmp_randstate_t gmpRng; gmp_randstate_t gmpRng;
gmp_randinit_mt(gmpRng); gmp_randinit_mt(gmpRng);
// The GMP seed varies between run so that // The GMP seed varies between run so that

5
include/README.md Normal file
View File

@ -0,0 +1,5 @@
# Headers
This folder holds the generated C headers for Constantine.
To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`.

5
lib/README.md Normal file
View File

@ -0,0 +1,5 @@
# Libraries
This folder holds the generated dynamic libraries (.so on Linux, .dylib on MacOS, .dll on Windows) and static libraries (.a on Linux and Mac, .dll on Windows).
To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`.