1
1
mirror of https://github.com/nmattia/snack.git synced 2024-10-11 02:47:39 +03:00

Merge pull request #16 from nmattia/nm-tmp

Remove moduleIsMain
This commit is contained in:
Nicolas Mattia 2018-06-23 00:22:36 +02:00 committed by GitHub
commit b40a083f32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 37 deletions

View File

@ -97,44 +97,34 @@ let
# Returns an attribute set where the keys are the module names and the values
# are the '.o's
flattenModuleObjects = ghcWith: mod0:
let
go = mod: attrs0:
let
objectName = x:
# TODO: can't justuse "moduleName.o" because some modules get
# renamed to "Main.o" :/ Also, hard coding the object file based on
# the module name feels icky
if x.moduleIsMain
then "Main.o"
else moduleToObject x.moduleName;
attrs1 = f attrs0 mod;
f = acc: elem:
if lib.attrsets.hasAttr elem.moduleName acc
then acc # breaks infinite recursion
else acc //
{ "${elem.moduleName}" =
"${buildModule ghcWith elem}/${objectName elem}";
lib.fix (f: acc0: mods:
let
insertMod = acc: mod:
if lib.attrsets.hasAttr mod.moduleName acc
then acc
else
let acc' = acc //
{ "${mod.moduleName}" =
"${buildModule ghcWith mod}/${moduleToObject mod.moduleName}";
};
in
lib.lists.foldl f attrs1 mod.moduleImports;
in go mod0 {};
in f acc' mod.moduleImports;
in
lib.foldl insertMod acc0 mods
)
# XXX: the main modules need special handling regarding the object name
{ "${mod0.moduleName}" = "${buildModule ghcWith mod0}/Main.o";}
mod0.moduleImports;
# TODO: this should be ghcWith
linkModuleObjects = ghcWith: mod: # main module
let
objAttrs = flattenModuleObjects ghcWith mod;
objList = lib.attrsets.mapAttrsToList (x: y: y) objAttrs;
# TODO: all recursive dependencies of "mod"
deps = allTransitiveDeps [mod];
ghc = ghcWith deps;
ghcOptsArgs = lib.strings.escapeShellArgs mod.moduleGhcOpts;
packageList = map (p: "-package ${p}") deps;
in stdenv.mkDerivation
{ name = "linker";
src = null;
builder = writeScript "linker-builder"
in runCommand "linker" {}
''
source $stdenv/setup
mkdir -p $out
${ghc}/bin/ghc \
${lib.strings.escapeShellArgs packageList} \
@ -142,7 +132,6 @@ let
${ghcOptsArgs} \
-o $out/out
'';
};
allModuleDirectories = modSpec:
lib.lists.concatLists

View File

@ -10,14 +10,12 @@ rec {
makeModuleSpec =
modName:
modImports:
isMain:
modFiles:
modDirs:
modBase:
modDeps:
modGhcOpts:
{ moduleName = modName;
moduleIsMain = isMain;
# local module imports, i.e. not part of an external dependency
moduleImports = modImports;
@ -34,6 +32,8 @@ rec {
# Create a module spec by following the dependencies. This assumes that the
# specified module is a "Main" module.
# TODO: pretty sure things will silently go wrong if several modules in the
# dependency tree share a common name
makeModuleSpecRec =
baseByModuleName:
filesByModuleName:
@ -41,19 +41,18 @@ rec {
depsByModuleName:
ghcOptsByModuleName:
lib.fix
(f: isMain: modName:
(f: modName:
makeModuleSpec
modName
(map (f false)
(map f
(lib.lists.filter (mn: baseByModuleName mn != null) (listModuleImports baseByModuleName modName))
)
isMain
(filesByModuleName modName)
(dirsByModuleName modName)
(baseByModuleName modName)
(depsByModuleName modName)
(ghcOptsByModuleName modName)
) true;
);
# Returns a list of all modules in the module spec graph
flattenModuleSpec = modSpec:

View File

@ -14,6 +14,7 @@ rec {
(lib.strings.replaceChars ["."] ["/"] mod) + ".hs";
# Turns a module name into the filepath of its object file
# TODO: bad name, this is module _name_ to object
moduleToObject = mod:
(lib.strings.replaceChars ["."] ["/"] mod) + ".o";

7
tests/library/app/Bar.hs Normal file
View File

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

View File

@ -1,5 +1,4 @@
import Conduit
import FooBar
import qualified Bar
main :: IO ()
main = runConduit $ spitOut .| takeC 5 .| digest
main = Bar.main