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

Nixify o1js bindings #291

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
534 changes: 534 additions & 0 deletions flake.lock

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11-small";
mina.url =
"git+https://github.com/minaprotocol/mina?submodules=1&ref=georgeee/granular-nix&shallow=1";
mina.inputs.nixpkgs.follows = "nixpkgs";
describe-dune.url = "github:o1-labs/describe-dune";
describe-dune.inputs.nixpkgs.follows = "nixpkgs";
describe-dune.inputs.flake-utils.follows = "flake-utils";
dune-nix.url = "github:o1-labs/dune-nix";
dune-nix.inputs.nixpkgs.follows = "nixpkgs";
dune-nix.inputs.flake-utils.follows = "flake-utils";
mina.inputs.dune-nix.follows = "dune-nix";
mina.inputs.describe-dune.follows = "describe-dune";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
dune-nix = inputs.dune-nix.lib.${system};
describe-dune = inputs.describe-dune.defaultPackage.${system};
dune-description = pkgs.stdenv.mkDerivation {
pname = "dune-description";
version = "dev";
src = with pkgs.lib.fileset;
(toSource {
root = ./.;
fileset = unions [
./ocaml/dune
./ocaml/lib/dune
./ocaml/dune-project
./ocaml/js/dune
];
});
phases = [ "unpackPhase" "buildPhase" ];
buildPhase = ''
${describe-dune}/bin/describe-dune > $out
'';
};
desc = builtins.fromJSON (builtins.readFile dune-description);
allOcamlDeps_ = pkgs.lib.concatMap (duneSpec:
pkgs.lib.concatMap (unitSpec: unitSpec.deps or [ ])
(duneSpec.units or [ ])) desc;
allOcamlDeps =
builtins.map (d: builtins.head (pkgs.lib.splitString "." d))
allOcamlDeps_;
mina = inputs.mina.packages."${system}";
minaDepsDrv = mina.info.deps;
minaDeps_ =
builtins.intersectAttrs (pkgs.lib.genAttrs allOcamlDeps (_: { }))
mina.info.raw.deps.units;
minaDeps = builtins.attrNames (builtins.foldl'
(acc: pkg: acc // dune-nix.deps.packageDeps minaDeps_ "pkgs" pkg)
minaDeps_ (builtins.attrNames minaDeps_));
commonOverrides = {
DUNE_PROFILE = "dev";
buildInputs = [ mina.base-libs ] ++ mina.external-libs
++ pkgs.lib.attrVals minaDeps mina.pkgs;
};
info = dune-nix.info desc;
allDeps = dune-nix.allDeps info;
noTestSkipping = _: false;
prj_ = dune-nix.outputs' commonOverrides (pkgs.lib.fileset.toSource {
root = ./.;
fileset = ./ocaml;
}) allDeps info noTestSkipping prj;
prj = prj_ // {
pkgs = prj_.pkgs // {
__ocaml-js__ = prj_.pkgs.__ocaml-js__.overrideAttrs {
PREBUILT_KIMCHI_BINDINGS_JS_WEB =
"${mina.files.src-lib-crypto-kimchi_bindings-js-web}/src/lib/crypto/kimchi_bindings/js/web";
PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS =
"${mina.files.src-lib-crypto-kimchi_bindings-js-node_js}/src/lib/crypto/kimchi_bindings/js/node_js";
};
};
};
in {
formatter = pkgs.nixfmt;
# TODO build from ./ocaml root, not ./. (after fixing a bug in dune-nix)
packages = {
inherit dune-description;
default = prj.pkgs.o1js_bindings;
js = prj.pkgs.__ocaml-js__;
};
});
}
52 changes: 4 additions & 48 deletions ocaml/dune
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
(env
(_
(js_of_ocaml
(compilation_mode whole_program))))

(data_only_dirs test_module)

(vendored_dirs o1js)

(executable
(name o1js_node)
(modules o1js_node)
(modes js)
(flags (-g))
(link_flags
(-noautolink -g))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty --source-map)
(link_flags --source-map)
(javascript_files overrides.js))
(libraries o1js_bindings_lib bindings_js.node_backend)
(link_deps
../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm.js
../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_web)
(modules o1js_web)
(modes js)
(link_flags (-noautolink))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty)
(javascript_files overrides.js))
(libraries o1js_bindings_lib bindings_js.web_backend)
(link_deps
../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm.js
../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_types)
(public_name o1js-types)
(package o1js_bindings)
(modules o1js_types)
(link_flags (-linkall))
(modes native)
@@ -66,6 +20,8 @@

(executable
(name o1js_constants)
(public_name o1js-constants)
(package o1js_bindings)
(modules o1js_constants)
(link_flags (-linkall))
(modes native)
3 changes: 3 additions & 0 deletions ocaml/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 3.3)

(package (name o1js_bindings))
81 changes: 81 additions & 0 deletions ocaml/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(env
(_
(js_of_ocaml
(compilation_mode whole_program))))

(rule
(enabled_if
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(deps (:d1 ../../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm.js) (:d2 ../../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm_bg.wasm) )
(action
(progn
(run cp %{d1} node_js_plonk_wasm.js)
(run cp %{d2} node_js_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(deps (:d1 ../../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm.js) (:d2 ../../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm_bg.wasm) )
(action
(progn
(run cp %{d1} web_plonk_wasm.js)
(run cp %{d2} web_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(action
(progn
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm.js node_js_plonk_wasm.js)
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm_bg.wasm node_js_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(action
(progn
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm.js web_plonk_wasm.js)
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm_bg.wasm web_plonk_wasm_bg.wasm)
)))

(executable
(name o1js_node)
(modules o1js_node)
(modes js)
(flags (-g))
(link_flags
(-noautolink -g))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty --source-map)
(link_flags --source-map)
(javascript_files overrides.js))
(libraries o1js_bindings.lib bindings_js.node_backend)
(link_deps node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_web)
(modules o1js_web)
(modes js)
(link_flags (-noautolink))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty)
(javascript_files overrides.js))
(libraries o1js_bindings.lib bindings_js.web_backend)
(link_deps web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions ocaml/lib/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(library
(public_name o1js_bindings.lib)
(name o1js_bindings_lib)
(libraries
;; opam libraries ;;
4 changes: 2 additions & 2 deletions scripts/build-o1js-node-artifacts.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"

2 changes: 1 addition & 1 deletion scripts/build-o1js-node.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

4 changes: 2 additions & 2 deletions scripts/build-o1js-web.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"
WEB_BINDINGS="src/bindings/compiled/web_bindings"
2 changes: 1 addition & 1 deletion scripts/prepare-dune-build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# use this script before you can run `dune build <something>`

4 changes: 2 additions & 2 deletions scripts/update-o1js-bindings.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
DIR_PATH=$(dirname "$0")
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"
4 changes: 2 additions & 2 deletions scripts/update-wasm-and-types.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/env bash
#!/usr/bin/env bash

# this script updates the wasm artifacts used for node,
# as well as auto-generated wasm types checked into git
@@ -24,4 +24,4 @@ mv -f $_NODE_BINDINGS/plonk_wasm.d.ts $_NODE_BINDINGS/plonk_wasm.d.cts
chmod 777 "$_NODE_BINDINGS"/*
node "src/build/fix-wasm-bindings-node.js" "$_NODE_BINDINGS/plonk_wasm.cjs"

cp $_NODE_BINDINGS/plonk_wasm.d.cts $NODE_BINDINGS/
cp $_NODE_BINDINGS/plonk_wasm.d.cts $NODE_BINDINGS/
Loading