1
1
mirror of https://github.com/nmattia/snack.git synced 2024-08-16 07:10:57 +03:00

Add mainModule in spec to allow -main-is compiling

This commit is contained in:
zhujinxuan 2019-11-12 16:25:58 -05:00 committed by Nicolas Mattia
parent 05d8cfe0b7
commit a76301a911
7 changed files with 52 additions and 5 deletions

View File

@ -18,11 +18,11 @@ rec {
# Returns an attribute set where the keys are all the built module names and
# the values are the paths to the object files.
# mainModSpec: a "main" module
buildMain = ghcWith: mainModSpec:
buildMain = ghcWith: mainModSpec: mainModName:
buildModulesRec ghcWith
# XXX: the main modules need special handling regarding the object name
{ "${mainModSpec.moduleName}" =
"${buildModule ghcWith mainModSpec}/Main.o";}
"${buildModule ghcWith mainModSpec}/${mainModName}.o";}
mainModSpec.moduleImports;
# returns a attrset where the keys are the module names and the values are
@ -34,9 +34,10 @@ rec {
{ ghcWith
, moduleSpec # The module to build
, name # The name to give the executable
, mainModName
}:
let
objAttrs = buildMain ghcWith moduleSpec;
objAttrs = buildMain ghcWith moduleSpec mainModName;
objList = lib.attrsets.mapAttrsToList (x: y: y) objAttrs;
deps = allTransitiveDeps [moduleSpec];
ghc = ghcWith deps;

View File

@ -46,7 +46,8 @@ with rec
let
moduleSpec = executableMainModSpec pkgSpec;
name = pkgSpec.packageName;
drv = linkMainModule { inherit moduleSpec name ghcWith; };
mainModName = pkgSpec.packageMainModule;
drv = linkMainModule { inherit moduleSpec name ghcWith mainModName; };
in
{ out = drv.out;
exe_path = "${drv.out}/${drv.relExePath}";

View File

@ -11,6 +11,7 @@ rec {
{ src ? []
, name ? null
, main ? null
, mainModule ? "Main"
, ghcOpts ? []
, dependencies ? []
, extensions ? []
@ -29,11 +30,20 @@ rec {
{ packageIsExe = ! builtins.isNull main;
packageName = pName;
packageMain = main;
packageMainModule = mainModule;
packageSourceDirs =
if builtins.isList src
then src
else [src];
packageGhcOpts = ghcOpts;
packageGhcOpts = let
addition =
if mainModule == "Main"
then
[]
else
["-main-is" mainModule];
in
ghcOpts ++ addition;
packageExtensions = extensions;
packageDependencies = mkPerModuleAttr dependencies;

View File

@ -0,0 +1,12 @@
{-|
Copyright:
© 2018 Nicolas Mattia
-}
module Spec
( main
) where
import Numeric.Natural (Natural)
main :: IO ()
main = putStrLn "hello, test!"

View File

@ -0,0 +1 @@
hello, test!

View File

@ -0,0 +1,4 @@
{ main = "Spec";
mainModule = "Spec";
src = ./.;
}

18
tests/main-is-different/test Executable file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
# vim: ft=sh sw=2 et
set -euo pipefail
test() {
$SNACK build
$SNACK run | diff golden -
TMP_FILE=$(mktemp)
capture_io "$TMP_FILE" main | $SNACK ghci
diff golden $TMP_FILE
rm $TMP_FILE
}
SNACK="snack --package-file ./package.nix" test