Skip to content

Commit

Permalink
Bindings "filesystem" (#225)
Browse files Browse the repository at this point in the history
* bindings structure

* missed some renaming

* add back the headers

* path fixes

* need to sleep at night

* windows path mystery is unfathomable
  • Loading branch information
mratsim authored Mar 1, 2023
1 parent 1cb6c3d commit 4dc2610
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
7 changes: 7 additions & 0 deletions bindings/README.md
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 19 additions & 19 deletions constantine.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,12 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) =
# In the future, Constantine might use:
# - heap-allocated sequences and objects manually managed or managed by destructors for multithreading.
# - 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 " &
" --panics:on " & # Defects are not catchable
" --verbosity:0 --hints:off --warnings:off " &
" --nimMainPrefix:" & prefixNimMain &
" --out:" & libName & " --outdir:bindings/generated " &
" --out:" & libName & " --outdir:lib " &
" --nimcache:nimcache/bindings/" & bindingsName &
" bindings/" & bindingsName & ".nim"

Expand All @@ -483,9 +483,9 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) =
elif defined(macosx):
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'"
exec "lipo bindings/generated/lib" & bindingsName & ".dylib.arm " &
" bindings/generated/lib" & bindingsName & ".dylib.x64 " &
" -output bindings/generated/lib" & bindingsName & ".dylib -create"
exec "lipo lib/lib" & bindingsName & ".dylib.arm " &
" lib/lib" & bindingsName & ".dylib.x64 " &
" -output lib/lib" & bindingsName & ".dylib -create"

else:
compile "lib" & bindingsName & ".so"
Expand All @@ -499,12 +499,12 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) =
# In the future, Constantine might use:
# - heap-allocated sequences and objects manually managed or managed by destructors for multithreading.
# - 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 " &
" --panics:on " & # Defects are not catchable
" --verbosity:0 --hints:off --warnings:off " &
" --nimMainPrefix:" & prefixNimMain &
" --out:" & libName & " --outdir:bindings/generated " &
" --out:" & libName & " --outdir:lib " &
" --nimcache:nimcache/bindings/" & bindingsName &
" bindings/" & bindingsName & ".nim"

Expand All @@ -514,21 +514,21 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) =
elif defined(macosx):
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'"
exec "lipo bindings/generated/lib" & bindingsName & ".a.arm " &
" bindings/generated/lib" & bindingsName & ".a.x64 " &
" -output bindings/generated/lib" & bindingsName & ".a -create"
exec "lipo lib/lib" & bindingsName & ".a.arm " &
" lib/lib" & bindingsName & ".a.x64 " &
" -output lib/lib" & bindingsName & ".a -create"

else:
compile "lib" & bindingsName & ".a"

proc genHeaders(bindingsName: string) =
echo "Generating header: bindings/generated/" & bindingsName & ".h"
echo "Generating header: include/" & bindingsName & ".h"
exec "nim c -d:release -d:CttGenerateHeaders " &
" --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" &
" bindings/" & bindingsName & ".nim"
exec "bindings/generated/" & bindingsName & "_gen_header.exe bindings/generated"
exec "build/" & bindingsName & "_gen_header.exe include"

proc genParallelCmdRunner() =
exec "nim c --verbosity:0 --hints:off --warnings:off -d:release --out:build/pararun --nimcache:nimcache/pararun helpers/pararun.nim"
Expand All @@ -549,23 +549,23 @@ task test_bindings, "Test C bindings":
exec "mkdir -p build/testsuite"
echo "--> Testing dynamically linked library"
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 "LD_LIBRARY_PATH=bindings/generated ./build/testsuite/t_libctt_bls12_381_dl"
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=lib ./build/testsuite/t_libctt_bls12_381_dl"
else:
# 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"

echo "--> Testing statically linked library"
when not defined(windows):
# Beware MacOS annoying linker with regards to static libraries
# 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"
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"

task test, "Run all tests":
Expand Down
5 changes: 5 additions & 0 deletions examples_c/README.md
Original file line number Diff line number Diff line change
@@ -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/`
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// * 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.

// 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 <gmp.h>
#include <constantine_bls12_381.h>
Expand Down Expand Up @@ -34,7 +37,7 @@ void prologue(
mpz_ptr p,
bls12381_fp* a_ctt, bls12381_fp* b_ctt,
byte a_buf[ByteLength], byte b_buf[ByteLength]) {

// Generate random value in the range 0 ..< 2^(bits-1)
mpz_urandomb(a, gmp_rng, BitLength);
mpz_urandomb(b, gmp_rng, BitLength);
Expand Down Expand Up @@ -65,7 +68,7 @@ void epilogue(
mpz_ptr r, mpz_ptr a, mpz_ptr b,
bls12381_fp* r_ctt, bls12381_fp* a_ctt, bls12381_fp* b_ctt,
char* operation) {

byte r_raw_gmp[ByteLength];
byte r_raw_ctt[ByteLength];

Expand Down Expand Up @@ -98,10 +101,10 @@ void epilogue(
}

int main(){

// Initialize the runtime. For Constantine, it populates CPU runtime detection dispatch.
ctt_bls12381_init_NimMain();

gmp_randstate_t gmpRng;
gmp_randinit_mt(gmpRng);
// The GMP seed varies between run so that
Expand Down
5 changes: 5 additions & 0 deletions include/README.md
Original file line number Diff line number Diff line change
@@ -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`.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 4dc2610

Please sign in to comment.