|
| 1 | +# Base API for constructing Rust packages given .lbf schemas |
| 2 | + |
| 3 | +# Nixpkgs |
| 4 | +pkgs: |
| 5 | +# LambdaBuffers Frontend |
| 6 | +lbf: |
| 7 | +# LambdaBuffers Rust Codegen |
| 8 | +lbg-rust: |
| 9 | +let |
| 10 | + lbfRustOpts = |
| 11 | + { |
| 12 | + # Source that is passed to `lbf` as the `--import-path` flag and used to find `files`. |
| 13 | + # Examples: src = ./api |
| 14 | + src |
| 15 | + , # Additional sources that are passed to `lbf` as the `--import-path` flag. |
| 16 | + # Examples: imports = [ lbf-prelude ] |
| 17 | + imports ? [ ] |
| 18 | + , # .lbf files in `src` to compile and codegen. |
| 19 | + # Examples: files = [ "Foo.lbf" "Foo/Bar.lbf" ] |
| 20 | + files |
| 21 | + # Classes for which to generate implementations for (default lbf-prelude classes). |
| 22 | + , classes ? [ ] |
| 23 | + , # Dependencies to include in the Cabal's `build-depends` stanza. |
| 24 | + # examples: dependencies = [ "lbf-prelude" ] |
| 25 | + dependencies ? [ ] |
| 26 | + , configs ? [ ] |
| 27 | + , # Name of the package and also the name of the Cabal package. |
| 28 | + # Examples: name = "lbf-myproject" |
| 29 | + name |
| 30 | + , # Version of the package and also the version of the Cabal package. |
| 31 | + # Examples: version = "0.1.0.0" |
| 32 | + version ? "0.1.0" |
| 33 | + }: { inherit src imports files classes dependencies configs name version; }; |
| 34 | + |
| 35 | + lbf-build = import ./lbf-build.nix pkgs lbf; |
| 36 | + |
| 37 | + lbfBuild = opts: with (lbfRustOpts opts); |
| 38 | + lbf-build.build |
| 39 | + { |
| 40 | + inherit src; |
| 41 | + opts = { |
| 42 | + inherit files; |
| 43 | + import-paths = imports; |
| 44 | + gen = lbg-rust; |
| 45 | + gen-classes = classes; |
| 46 | + gen-dir = "autogen"; |
| 47 | + gen-opts = builtins.map (c: "--config=${c}") configs; # WARN(bladyjoker): If I put quotes here everything breaks. |
| 48 | + work-dir = ".work"; |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + cargoTemplate = opts: with (lbfRustOpts opts); |
| 53 | + pkgs.writeTextFile { |
| 54 | + name = "lambda-buffers-cabal-template"; |
| 55 | + text = '' |
| 56 | + [package] |
| 57 | + name = "${name}" |
| 58 | + version = "${version}" |
| 59 | + edition = "2021" |
| 60 | +
|
| 61 | + [dependencies] |
| 62 | + ''; |
| 63 | + }; |
| 64 | + |
| 65 | + crateVersions = pkgs.writeTextFile { |
| 66 | + name = "lambda-buffers-crate-versions"; |
| 67 | + text = '' |
| 68 | + num-bigint = "0.4.4" |
| 69 | + serde_json = { version = "1.0.107", features = ["arbitrary_precision"] } |
| 70 | + plutus-ledger-api = { github = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = ["lbf"] } |
| 71 | + lbr-prelude = { path = "../lbr-prelude" } |
| 72 | + lbr-prelude-derive = { path = "../lbr-prelude-derive" } |
| 73 | + ''; |
| 74 | + }; |
| 75 | + |
| 76 | + build = opts: with (lbfRustOpts opts); |
| 77 | + let |
| 78 | + lbfBuilt = lbfBuild opts; |
| 79 | + in |
| 80 | + pkgs.stdenv.mkDerivation { |
| 81 | + inherit src version; |
| 82 | + pname = name; |
| 83 | + outputs = [ "out" "buildjson" ]; |
| 84 | + buildInputs = [ |
| 85 | + pkgs.jq |
| 86 | + ]; |
| 87 | + buildPhase = '' |
| 88 | + ln -s ${lbfBuilt} autogen; |
| 89 | + ln -s ${lbfBuilt.workdir} .work-dir; |
| 90 | + ln -s ${lbfBuilt.buildjson} build.json; |
| 91 | +
|
| 92 | + # Generating Cargo manifest file |
| 93 | + DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u)); |
| 94 | + echo "Gathered Cargo deps $DEPS"; |
| 95 | + cat ${cargoTemplate opts} > Cargo.toml; |
| 96 | + for DEP in $DEPS; do |
| 97 | + if [ $DEP != "std" ]; then |
| 98 | + echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml |
| 99 | + fi |
| 100 | + done |
| 101 | + ''; |
| 102 | + |
| 103 | + installPhase = '' |
| 104 | + cp build.json $buildjson; |
| 105 | + echo "Dependencies collected" |
| 106 | + cat $buildjson; |
| 107 | +
|
| 108 | + mkdir -p $out/src; |
| 109 | + cp -r autogen/* $out/src |
| 110 | + cp Cargo.toml $out/Cargo.toml; |
| 111 | +
|
| 112 | + # Generating module files |
| 113 | + chmod -R u+w $out/src |
| 114 | + pushd $out/src |
| 115 | +
|
| 116 | + MODS=$(find . -type f -name "*.rs") |
| 117 | + MODS+=" " |
| 118 | + MODS+=$(find . -type d) |
| 119 | +
|
| 120 | + for MOD in $MODS; do |
| 121 | + if [ "$MOD" != "." ]; then |
| 122 | + if [ $(dirname $MOD) = "." ]; |
| 123 | + then MODFILE="lib.rs"; |
| 124 | + else MODFILE=$(dirname $MOD).rs; |
| 125 | + fi |
| 126 | + DOC="pub mod $(basename $MOD .rs);" |
| 127 | +
|
| 128 | + if [ ! $(grep "$DOC" $MODFILE) ]; then |
| 129 | + echo $DOC >> $MODFILE; |
| 130 | + fi |
| 131 | + fi |
| 132 | + done |
| 133 | +
|
| 134 | + echo "Files generated" |
| 135 | + find $out/; |
| 136 | + ''; |
| 137 | + }; |
| 138 | +in |
| 139 | +build |
0 commit comments