Setup.hs 3.96 KB
Newer Older
Bernd Brassel's avatar
Bernd Brassel committed
1
2
3
module Main where

import System.FilePath
4
5
import System.Directory
import Monad
Bernd Brassel's avatar
Bernd Brassel committed
6
7
8
import Data.List

import Distribution.Simple
9
import Distribution.Simple.Setup 
Bernd Brassel's avatar
Bernd Brassel committed
10
11
12
13
14
import Distribution.Simple.Program
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.PreProcess
import Distribution.PackageDescription 
import Distribution.Verbosity
15
import Distribution.ModuleName (ModuleName(..))
Bernd Brassel's avatar
Bernd Brassel committed
16
17
18
19

import Curry.Files.CymakePath
import Curry.Files.KiCSPath

20
21
kics, cymake :: IO Program
kics   = mkProg getKiCS
Bernd Brassel's avatar
Bernd Brassel committed
22
23
24
25
26
27
28
cymake = mkProg getCymake

mkProg getProg = do
  call <- getProg
  return (simpleProgram (takeBaseName call))
            {programFindLocation = \_-> return (Just call)}

29
30
31
32
prophecy, mkstrict :: Program
prophecy = simpleProgram "prophecy"
mkstrict = simpleProgram "mkstrict"

Bernd Brassel's avatar
Bernd Brassel committed
33
34
35
36
37
main :: IO ()
main = do
  cymakeProg <- cymake
  kicsProg   <- kics
  defaultMainWithHooks simpleUserHooks
38
39
    {hookedPrograms=kicsProg:cymakeProg:prophecy:hookedPrograms simpleUserHooks
    ,confHook=myConfHook
Bernd Brassel's avatar
Bernd Brassel committed
40
    ,postConf=myPostConf
41
    ,hookedPreProcessors=[("curry",mkModule),("lcurry",mkModule)]
Bernd Brassel's avatar
Bernd Brassel committed
42
43
    }

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- what a hack! something was forgotten in Distribution.ModuleName
modName :: [String] -> ModuleName
modName xs = read $ "ModuleName " ++ show xs

myConfHook :: (Either GenericPackageDescription PackageDescription, HookedBuildInfo) 
           -> ConfigFlags 
           -> IO LocalBuildInfo
myConfHook info flags = do
  lbi <- confHook simpleUserHooks info flags
  libPath <- getKiCSLibDir
  allFiles <- getDirectoryContents libPath
  let stdlibs  = map takeBaseName (filter (isSuffixOf ".fcy") allFiles)
      libs     = concatMap 
                     (\ lib -> [--modName ("Curry":"Module":["Oracle" ++ lib])
                               {- , -}modName ("Curry":"DebugModule":[lib])]) 
                     stdlibs
      package  = localPkgDescr lbi
      Just lib = library package
      lib' = Just lib{exposedModules=exposedModules lib ++ libs}
  let require p = requireProg flags lbi (return p)
  prop <- require prophecy
  stri <- require mkstrict  
  mapM (mkOracleLib (unflag $ configVerbosity flags) prop stri) stdlibs
  return lbi{localPkgDescr=package{library=lib'}}

Bernd Brassel's avatar
Bernd Brassel committed
69
70
unflag = fromFlagOrDefault silent

71
72
requireProg :: ConfigFlags -> LocalBuildInfo -> IO Program 
            -> IO ConfiguredProgram
Bernd Brassel's avatar
Bernd Brassel committed
73
74
requireProg verb lbi prog = do
  p <- prog
75
76
77
  (cp,_) <- requireProgram (unflag $ configVerbosity verb) p AnyVersion 
                           (withPrograms lbi)
  return cp
Bernd Brassel's avatar
Bernd Brassel committed
78
79
80
81
82
83
84
85
 
callProg :: Verbosity -> LocalBuildInfo -> IO Program -> Args -> IO ()
callProg verb lbi prog args = do
  p <- prog
  rawSystemProgramConf verb p (withPrograms lbi) args
  
myPostConf :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
myPostConf args confFlags descrs lbi = do
86
  let require = requireProg confFlags lbi
Bernd Brassel's avatar
Bernd Brassel committed
87
88
89
90
  require cymake
  require kics
  postConf simpleUserHooks args confFlags descrs lbi

91
92
93
94
95
96
97
mkOracleLib :: Verbosity -> ConfiguredProgram -> ConfiguredProgram -> FilePath -> IO ()
mkOracleLib verb prop strict stdlib = do
  rawSystemProgram verb prop   ["-o","Curry/Module/",stdlib]
  when (not (elem stdlib []))
       (rawSystemProgram verb strict ["-o","Curry/DebugModule/",stdlib])
  return ()

Bernd Brassel's avatar
Bernd Brassel committed
98
99
100
101
102
103
104
105
106
mkModule :: BuildInfo -> LocalBuildInfo -> PreProcessor
mkModule buildInfo lbi = PreProcessor
  {platformIndependent = True
  ,runPreProcessor     = mkSimplePreProcessor (runKics lbi)}

runKics :: LocalBuildInfo -> FilePath -> FilePath -> Verbosity -> IO ()
runKics lbi infile outfile verb = do
  let call    = callProg verb lbi
  datadir <- getKiCSLibDir
107
108
109
110
111
112
113
114
115
116
  call cymake ["-e","--flat","-i"++datadir
              ,"-iprophecy/Curry/Module"
              ,"-ibiosphere/src/Curry/Module"
              ,infile]
  let callKics = call kics . (["-nomake","-userlibpath","prophecy/Curry/Module","-o",outfile]++)
  if    isSuffixOf "Transform.curry" infile
   then callKics ["-executable","prophecy",infile] else
   if   isSuffixOf "TransformationDependencies.lcurry" infile
   then callKics ["-executable","mkstrict",infile]
   else callKics [infile]
Bernd Brassel's avatar
Bernd Brassel committed
117