|
| 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 = ./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 | + let |
| 39 | + findModules = root: map |
| 40 | + (path: builtins.replaceStrings [ "/" ] [ "." ] |
| 41 | + (pkgs.lib.strings.removePrefix "./" (pkgs.lib.strings.removeSuffix ".lbf" |
| 42 | + (pkgs.lib.path.removePrefix root path)))) |
| 43 | + (builtins.filter (pkgs.lib.hasSuffix ".lbf") |
| 44 | + (pkgs.lib.filesystem.listFilesRecursive root)); |
| 45 | + packageSet = |
| 46 | + pkgs.writeTextFile { |
| 47 | + name = "lb-packages"; |
| 48 | + text = |
| 49 | + builtins.toJSON |
| 50 | + ({ crate = findModules src; } // builtins.mapAttrs (_: findModules) imports); |
| 51 | + }; |
| 52 | + |
| 53 | + in |
| 54 | + lbf-build.build |
| 55 | + { |
| 56 | + inherit src; |
| 57 | + opts = { |
| 58 | + inherit files; |
| 59 | + import-paths = pkgs.lib.attrsets.attrValues imports; |
| 60 | + gen = lbg-rust; |
| 61 | + gen-classes = classes; |
| 62 | + gen-dir = "autogen"; |
| 63 | + gen-opts = [ "--packages=${packageSet}" ] ++ builtins.map (c: "--config=${c}") configs; # WARN(bladyjoker): If I put quotes here everything breaks. |
| 64 | + work-dir = ".work"; |
| 65 | + }; |
| 66 | + }; |
| 67 | + |
| 68 | + cargoTemplate = opts: with (lbfRustOpts opts); |
| 69 | + pkgs.writeTextFile { |
| 70 | + name = "lambda-buffers-cabal-template"; |
| 71 | + text = '' |
| 72 | + [package] |
| 73 | + name = "${name}" |
| 74 | + version = "${version}" |
| 75 | + edition = "2021" |
| 76 | +
|
| 77 | + [dependencies] |
| 78 | + ''; |
| 79 | + }; |
| 80 | + |
| 81 | + # |
| 82 | + crateVersions = pkgs.writeTextFile { |
| 83 | + name = "lambda-buffers-crate-versions"; |
| 84 | + text = '' |
| 85 | + num-bigint = "0.4.4" |
| 86 | + serde_json = { version = "1.0.107", features = ["arbitrary_precision"] } |
| 87 | + plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [ "lbf", ], rev = "fb93fa590908580eb40368369bf6614d42ce9a95" } |
| 88 | + ''; |
| 89 | + }; |
| 90 | + |
| 91 | + build = opts: with (lbfRustOpts opts); |
| 92 | + let |
| 93 | + lbfBuilt = lbfBuild opts; |
| 94 | + in |
| 95 | + pkgs.stdenv.mkDerivation { |
| 96 | + inherit src version; |
| 97 | + pname = name; |
| 98 | + outputs = [ "out" "buildjson" ]; |
| 99 | + buildInputs = [ |
| 100 | + pkgs.jq |
| 101 | + ]; |
| 102 | + buildPhase = '' |
| 103 | + ln -s ${lbfBuilt} autogen; |
| 104 | + ln -s ${lbfBuilt.workdir} .work-dir; |
| 105 | + ln -s ${lbfBuilt.buildjson} build.json; |
| 106 | +
|
| 107 | + # Generating Cargo manifest file |
| 108 | + DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u)); |
| 109 | + echo "Gathered Cargo deps $DEPS"; |
| 110 | + cat ${cargoTemplate opts} > Cargo.toml; |
| 111 | + for DEP in $DEPS; do |
| 112 | + if [ $DEP != "std" ]; then |
| 113 | + echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml |
| 114 | + fi |
| 115 | + done |
| 116 | + ''; |
| 117 | + |
| 118 | + installPhase = '' |
| 119 | + cp build.json $buildjson; |
| 120 | + echo "Dependencies collected" |
| 121 | + cat $buildjson; |
| 122 | +
|
| 123 | + mkdir -p $out/src; |
| 124 | + cp -r autogen/* $out/src |
| 125 | + cp Cargo.toml $out/Cargo.toml; |
| 126 | +
|
| 127 | + # Generating module files |
| 128 | + chmod -R u+w $out/src |
| 129 | + pushd $out/src |
| 130 | +
|
| 131 | + MODS=$(find . -type f -name "*.rs") |
| 132 | + MODS+=" " |
| 133 | + MODS+=$(find . -type d) |
| 134 | +
|
| 135 | + for MOD in $MODS; do |
| 136 | + if [ "$MOD" != "." ]; then |
| 137 | + if [ $(dirname $MOD) = "." ]; |
| 138 | + then MODFILE="lib.rs"; |
| 139 | + else MODFILE=$(dirname $MOD).rs; |
| 140 | + fi |
| 141 | + DOC="pub mod $(basename $MOD .rs);" |
| 142 | +
|
| 143 | + if [ ! $(grep "$DOC" $MODFILE) ]; then |
| 144 | + echo $DOC >> $MODFILE; |
| 145 | + fi |
| 146 | + fi |
| 147 | + done |
| 148 | +
|
| 149 | + echo "Files generated" |
| 150 | + find $out/; |
| 151 | + ''; |
| 152 | + }; |
| 153 | +in |
| 154 | +build |
0 commit comments