-
Notifications
You must be signed in to change notification settings - Fork 20
/
Setup.hs
78 lines (65 loc) · 2.46 KB
/
Setup.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
69
70
71
72
73
74
75
76
77
78
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.UserHooks
import Distribution.Simple.Setup
import System.Info
import System.Directory
import System.Environment
import System.Process
import System.FilePath
import Data.Maybe
main = defaultMainWithHooks simpleUserHooks { confHook = odenConfHook
, postConf = odenPostConf }
addLibDirsToBuildInfo :: BuildInfo -> IO BuildInfo
addLibDirsToBuildInfo buildInfo = do
wd <- getCurrentDirectory
return $ buildInfo {
extraLibDirs = wd : extraLibDirs buildInfo
}
addLibDirsToOdenExe :: [Executable] -> IO [Executable]
addLibDirsToOdenExe = mapM addIfOden
where
addIfOden exe
| exeName exe == "oden-exe" = do
withLibDirs <- addLibDirsToBuildInfo (buildInfo exe)
return $ exe { buildInfo = withLibDirs }
| otherwise = return exe
addLibDirsToTests :: [TestSuite] -> IO [TestSuite]
addLibDirsToTests = mapM addLibDirs
where
addLibDirs suite = do
withLibDirs <- addLibDirsToBuildInfo (testBuildInfo suite)
return $ suite { testBuildInfo = withLibDirs }
odenConfHook :: (GenericPackageDescription, HookedBuildInfo)
-> ConfigFlags
-> IO LocalBuildInfo
odenConfHook (description, buildInfo) flags = do
localBuildInfo <- confHook simpleUserHooks (description, buildInfo) flags
let packageDescription = localPkgDescr localBuildInfo
lib = fromJust $ library packageDescription
libWithLibDirs <- addLibDirsToBuildInfo (libBuildInfo lib)
executablesWithLibDirs <- addLibDirsToOdenExe (executables packageDescription)
testSuitesWithLibDirs <- addLibDirsToTests (testSuites packageDescription)
return $ localBuildInfo {
localPkgDescr = packageDescription {
library = Just $ lib {
libBuildInfo = libWithLibDirs
},
executables = executablesWithLibDirs,
testSuites = testSuitesWithLibDirs
}
}
ext :: String
ext = case os of
"darwin" -> ".dylib"
"windows" -> ".dll"
_ -> ".so"
odenPostConf :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
odenPostConf _ _ _ _ = do
wd <- getCurrentDirectory
setEnv "GOPATH" (wd </> "go")
let dynamicPath = wd </> ("libimporter" ++ ext)
buildDynamic = shell ("go build -buildmode=c-shared -o " ++ dynamicPath ++ " oden/cmd/importer")
putStrLn $ "Compiling Go dynamic library to " ++ dynamicPath
readCreateProcess buildDynamic "" >>= putStr