1
1
mirror of https://github.com/nmattia/snack.git synced 2024-09-11 11:55:36 +03:00

Enable support for library build

This commit is contained in:
Nicolas Mattia 2018-06-23 18:16:15 +02:00
parent 312a262c2a
commit 9a22c3df46
9 changed files with 87 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#!nix-shell -I nixpkgs=./nix
#!nix-shell -p snack-exe
#!nix-shell -p shfmt
#!nix-shell -p jq
#!nix-shell --pure
# vim: ft=sh sw=2 et
@ -74,6 +75,11 @@ pushd tests/library
./test
popd
banner "Test library-2"
pushd tests/library-2
./test
popd
banner "Test stack-exe formatting"
list=$(shfmt -i 2 -l bin/snack)
if [[ -n "$list" ]]; then

View File

@ -7,6 +7,7 @@
, stdenv
, symlinkJoin
, writeScript
, writeText
, runCommand
, callPackage
}:
@ -104,6 +105,8 @@ let
"${buildModule ghcWith mainModSpec}/Main.o";}
mainModSpec.moduleImports;
# returns a attrset where the keys are the module names and the values are
# the modules' object file path
buildLibrary = ghcWith: modSpecs:
buildModulesRec ghcWith {} modSpecs;
@ -148,8 +151,9 @@ let
# module spec
ghciWithMain = ghcWith: mainModSpec:
let
ghcOpts = allTransitiveGhcOpts [mainModSpec];
ghc = ghcWith (allTransitiveDeps [mainModSpec]);
modSpecs = [mainModSpec];
ghcOpts = allTransitiveGhcOpts modSpecs;
ghc = ghcWith (allTransitiveDeps modSpecs);
ghciArgs = lib.strings.escapeShellArgs
(ghcOpts ++ absoluteModuleFiles);
absoluteModuleFiles =
@ -221,6 +225,7 @@ let
ghcOptsByModuleName = ghcOptsByModuleName;
};
# TODO: "executable" is a bad name
executable = pkgDescr:
let
moduleSpecFold' = modSpecFoldFromPackageSpec topPkgSpec;
@ -230,7 +235,19 @@ let
mainModName = topPkgSpec.packageMain;
in
if builtins.isNull topPkgSpec.packageMain
then abort "Snack does not support building libraries!"
then
let
modNames = listModulesInDir topPkgSpec.packageBase;
fld = moduleSpecFold' modSpecs;
modSpecs = foldDAG fld modNames;
in
{
build =
writeText
"library-build"
(builtins.toJSON (buildLibrary ghcWith (builtins.attrValues modSpecs)));
ghci = abort "No GHCi support for libraries!";
}
else
let
mainModName = topPkgSpec.packageMain;
@ -240,10 +257,9 @@ let
modSpecs = foldDAG fld [mainModName];
in modSpecs.${mainModName};
in
{ build = linkMainModule ghcWith mainModSpec;
ghci = ghciWithMain ghcWith mainModSpec;
};
{ build = linkMainModule ghcWith mainModSpec;
ghci = ghciWithMain ghcWith mainModSpec;
};
in
{
inherit

5
tests/library-2/golden Normal file
View File

@ -0,0 +1,5 @@
1
2
3
4
5

View File

@ -0,0 +1,5 @@
[
"Bar",
"Foo",
"FooBar"
]

View File

@ -0,0 +1,7 @@
module Bar where
import Conduit
import FooBar
main :: IO ()
main = runConduit $ spitOut .| takeC 5 .| digest

View File

@ -0,0 +1,6 @@
module Foo where
import qualified Bar
someFunc :: IO ()
someFunc = Bar.main

10
tests/library-2/snack.nix Normal file
View File

@ -0,0 +1,10 @@
let
my-lib =
{ src = ./src;
dependencies = [ "conduit" ];
};
in
{ src = ./lib;
dependencies = [ "conduit" ];
packages = [ my-lib ];
}

View File

@ -0,0 +1,9 @@
module FooBar where
import Conduit
spitOut :: ConduitT () Int IO ()
spitOut = yieldMany [ 1 ..]
digest :: ConduitT Int Void IO ()
digest = mapM_C print

16
tests/library-2/test Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
# vim: ft=sh sw=2 et
set -euo pipefail
TMP_FILE=$(mktemp)
cat $($SNACK build) | jq -M 'keys' > $TMP_FILE
diff golden.jq $TMP_FILE
# TODO: enable this once supported
#capture_io "$TMP_FILE" someFunc | $SNACK ghci
#diff golden $TMP_FILE
rm $TMP_FILE