From ba997ae0943f6c672fcb15f4f8ce0657ce101359 Mon Sep 17 00:00:00 2001
From: Sebastien Bourdeauducq <sb@m-labs.hk>
Date: Sun, 23 Jan 2022 11:32:32 +0800
Subject: [PATCH] flake: run nac3standalone demo checks

also keep auxiliary projects in separate Nix outputs
---
 flake.nix                       | 26 +++++++++++++++++++++-----
 nac3standalone/demo/run_demo.sh |  9 ++++++++-
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/flake.nix b/flake.nix
index 7a533d7a..28442566 100644
--- a/flake.nix
+++ b/flake.nix
@@ -52,17 +52,33 @@
         nac3artiq = pkgs.python3Packages.toPythonModule (
           pkgs.rustPlatform.buildRustPackage {
             name = "nac3artiq";
+            outputs = [ "out" "runkernel" "standalone" ];
             src = self;
             cargoLock = { lockFile = ./Cargo.lock; };
             nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_13.clang-unwrapped llvm-nac3 ];
             buildInputs = [ pkgs.python3 llvm-nac3 ];
-            cargoBuildFlags = [ "--package" "nac3artiq" ];
-            cargoTestFlags = [ "--package" "nac3ast" "--package" "nac3parser" "--package" "nac3core" "--package" "nac3artiq" ];
+            checkInputs = [ (pkgs.python3.withPackages(ps: [ ps.numpy ])) ];
+            checkPhase =
+              ''
+              echo "Checking nac3standalone demos..."
+              pushd nac3standalone/demo
+              patchShebangs .
+              ./check_demos.sh
+              popd
+              echo "Running Cargo tests..."
+              cargoCheckHook
+              '';
             installPhase =
               ''
-              TARGET_DIR=$out/${pkgs.python3Packages.python.sitePackages}
-              mkdir -p $TARGET_DIR
-              cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $TARGET_DIR/nac3artiq.so
+              PYTHON_SITEPACKAGES=$out/${pkgs.python3Packages.python.sitePackages}
+              mkdir -p $PYTHON_SITEPACKAGES
+              cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $PYTHON_SITEPACKAGES/nac3artiq.so
+
+              mkdir -p $runkernel/bin
+              cp target/x86_64-unknown-linux-gnu/release/runkernel $runkernel/bin
+
+              mkdir -p $standalone/bin
+              cp target/x86_64-unknown-linux-gnu/release/nac3standalone $standalone/bin
               '';
           }
         );
diff --git a/nac3standalone/demo/run_demo.sh b/nac3standalone/demo/run_demo.sh
index d51d5784..e57ca019 100755
--- a/nac3standalone/demo/run_demo.sh
+++ b/nac3standalone/demo/run_demo.sh
@@ -7,7 +7,14 @@ if [ -z "$1" ]; then
     exit 1
 fi
 
+if [ -e ../../target/release/nac3standalone ]; then
+    nac3standalone=../../target/release/nac3standalone
+else
+    # used by Nix builds
+    nac3standalone=../../target/x86_64-unknown-linux-gnu/release/nac3standalone
+fi
+
 rm -f *.o
-../../target/release/nac3standalone $1
+$nac3standalone $1
 rustc -o demo demo.rs -Clink-arg=./module.o
 ./demo