From e339fa975e21c1b589a8dfc6ac083aba2b0a6394 Mon Sep 17 00:00:00 2001 From: Marcos Magueta Date: Wed, 16 Oct 2024 01:31:05 -0300 Subject: dune environment vars with flake and C++ interop with stubs --- .gitignore | 2 ++ bin/dune | 1 + bin/main.ml | 4 +++- deps/libressl.nix | 13 +++++++++++++ deps/merklecpp.nix | 22 ++++++++++++++++++++++ flake.lock | 16 ++++++++++------ flake.nix | 45 +++++++++++++++++++++++++++++---------------- lib/dune | 10 +++++++++- lib/hello.ml | 1 + lib/merklecpp_stubs.cpp | 15 +++++++++++++++ 10 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 .gitignore create mode 100644 deps/libressl.nix create mode 100644 deps/merklecpp.nix create mode 100644 lib/hello.ml create mode 100644 lib/merklecpp_stubs.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2790b52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +_build \ No newline at end of file diff --git a/bin/dune b/bin/dune index 0888311..f0cdc01 100644 --- a/bin/dune +++ b/bin/dune @@ -2,3 +2,4 @@ (public_name relational_engine) (name main) (libraries relational_engine)) + diff --git a/bin/main.ml b/bin/main.ml index 7bf6048..6fe3d69 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1 +1,3 @@ -let () = print_endline "Hello, World!" +open Relational_engine.Hello + +let _ = hello () diff --git a/deps/libressl.nix b/deps/libressl.nix new file mode 100644 index 0000000..ad33870 --- /dev/null +++ b/deps/libressl.nix @@ -0,0 +1,13 @@ +{ pkgs, ... }: +pkgs.stdenv.mkDerivation { + name = "libressl-lib"; + src = ./.; + buildInputs = with pkgs; [libressl.dev pkg-config]; + installPhase = '' + mkdir -p $out/lib + mkdir -p $out/include + libressl_path=$(${pkgs.pkg-config}/bin/pkg-config --libs ${pkgs.libressl.dev}/lib/pkgconfig/libssl.pc | awk '{sub(/^-L/, ""); sub(/ -lssl$/, ""); print}') + cp -r $libressl_path/* $out/lib + cp -r ${pkgs.libressl.dev}/include/* $out/include + ''; +} diff --git a/deps/merklecpp.nix b/deps/merklecpp.nix new file mode 100644 index 0000000..459a957 --- /dev/null +++ b/deps/merklecpp.nix @@ -0,0 +1,22 @@ +{ pkgs, ... }: +pkgs.stdenv.mkDerivation rec { + name = "merklecpp"; + + src = pkgs.fetchFromGitHub { + owner = "microsoft"; + repo = "merklecpp"; + rev = "master"; + sha256 = "jJJ6FEn+AkMknqun0GkXh5DSXsJnFbdE7U8BQ7Dd/Fw="; + }; + + prePatch = '' + ''; + configurePhase = '' + ''; + buildPhase = '' + ''; + installPhase = '' + mkdir -p $out/include + mv merklecpp.h $out/include/merklecpp.h + ''; +} diff --git a/flake.lock b/flake.lock index 735ecfb..622ed64 100644 --- a/flake.lock +++ b/flake.lock @@ -35,14 +35,18 @@ }, "nixpkgs": { "locked": { - "lastModified": 0, - "narHash": "sha256-v3rIhsJBOMLR8e/RNWxr828tB+WywYIoajrZKFM+0Gg=", - "path": "/nix/store/5w3dp0m37794iffsbm9vd9f1xmmhda6i-source", - "type": "path" + "lastModified": 1728888510, + "narHash": "sha256-nsNdSldaAyu6PE3YUA+YQLqUDJh+gRbBooMMekZJwvI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a3c0b3b21515f74fd2665903d4ce6bc4dc81c77c", + "type": "github" }, "original": { - "id": "nixpkgs", - "type": "indirect" + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" } }, "root": { diff --git a/flake.nix b/flake.nix index 2d50b55..d48082d 100644 --- a/flake.nix +++ b/flake.nix @@ -12,6 +12,7 @@ # $ nix flake lock --update-input ... --commit-lockfile # inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; # Convenience functions for writing flakes flake-utils.url = "github:numtide/flake-utils"; # Precisely filter files copied to the nix store @@ -28,6 +29,10 @@ ocamlPackages = legacyPackages.ocamlPackages; # Library functions from nixpkgs lib = legacyPackages.lib; + systems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + forAllSystems = f: builtins.listToAttrs (map (name: { inherit name; value = f name; }) systems); + merklecpp = legacyPackages.callPackage ./deps/merklecpp.nix {}; + libressl = legacyPackages.callPackage ./deps/libressl.nix {}; # Filtered sources (prevents unecessary rebuilds) sources = { @@ -63,26 +68,33 @@ # $ nix build # $ nix run -- # - default = self.packages.${system}.relational_engine; + default = self.packages.${system}.relational_engine; + + relational_engine = ocamlPackages.buildDunePackage { + pname = "relational_engine"; + version = "0.1.0"; + duneVersion = "3"; + src = sources.ocaml; - relational_engine = ocamlPackages.buildDunePackage { - pname = "relational_engine"; - version = "0.1.0"; - duneVersion = "3"; - src = sources.ocaml; + env = { + MERKLECPP_INCLUDE_PATH="${merklecpp}/include"; + LIBRESSL_INCLUDE_PATH="${libressl}/include"; + LIBRESSL_LIB_PATH="${libressl}/lib"; + }; - buildInputs = [ - # Ocaml package dependencies needed to build go here. - ]; + nativeInputs = []; - strictDeps = true; + buildInputs = [ + # Ocaml package dependencies needed to build go here. + ]; - preBuild = '' - dune build relational_engine.opam - ''; - }; - }; + strictDeps = true; + preBuild = '' + dune build relational_engine.opam + ''; + }; + }; # Flake checks # # $ nix flake check @@ -204,7 +216,8 @@ ocamlPackages.utop # Libraries ocamlPackages.menhir - ocamlPackages.batteries + ocamlPackages.ctypes + ocamlPackages.ctypes-foreign ]; # Tools from packages diff --git a/lib/dune b/lib/dune index 5d95cc5..3bc492f 100644 --- a/lib/dune +++ b/lib/dune @@ -1,2 +1,10 @@ (library - (name relational_engine)) + (name relational_engine) + (foreign_stubs + (language cxx) + (names merklecpp_stubs) + (flags + (:standard -lssl -lcrypto) + (-L %{env:LIBRESSL_LIB_PATH=}) + (-I %{env:LIBRESSL_INCLUDE_PATH=}) + (-I %{env:MERKLECPP_INCLUDE_PATH=})))) diff --git a/lib/hello.ml b/lib/hello.ml new file mode 100644 index 0000000..1a3a138 --- /dev/null +++ b/lib/hello.ml @@ -0,0 +1 @@ +external hello : unit -> int = "hello_ocaml" diff --git a/lib/merklecpp_stubs.cpp b/lib/merklecpp_stubs.cpp new file mode 100644 index 0000000..4ce427e --- /dev/null +++ b/lib/merklecpp_stubs.cpp @@ -0,0 +1,15 @@ +#include +#include + +void cpp_normal_function() { + std::cout << "HELLO OCAML! :)" << std::endl; +} + +extern "C" { +#include +#include + extern value hello_ocaml(value unit){ + cpp_normal_function(); + return 0; + } +} -- cgit v1.2.3