-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.hs
159 lines (121 loc) · 5.63 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Data.List
import Data.Traversable (for)
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.BuildPaths
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import Distribution.Simple.Program
import qualified Distribution.Verbosity as Verbosity
import System.Directory
import System.FilePath
import System.Info
main :: IO ()
main = defaultMainWithHooks simpleUserHooks
{ confHook = customConfHook
, buildHook = customBuildHook
, copyHook = customCopyHook
, cleanHook = customCleanHook
, hookedPrograms = hookedPrograms simpleUserHooks
++ [ makeProgram ]
}
customConfHook :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags
-> IO LocalBuildInfo
customConfHook (pkg, pbi) flags = do
(_, includeDir, _) <- libvoyeurPaths
let addIncludeDirs = (onLocalLibBuildInfo . onIncludeDirs) (++ [".", includeDir])
addLibs = if os == "darwin"
then id
else (onLocalLibBuildInfo . onLdOptions) (++ ["-lbsd"])
lbi <- confHook simpleUserHooks (pkg, pbi) flags
return $ (addLibs . addIncludeDirs) lbi
customBuildHook :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
customBuildHook pkg lbi usrHooks flags = do
putStrLn "Building libvoyeur..."
(libvoyeurDir, _, libDir) <- libvoyeurPaths
let verbosity = fromFlag (buildVerbosity flags)
runMake = runDbProgram verbosity makeProgram (withPrograms lbi)
inDir libvoyeurDir $
runMake []
buildHook simpleUserHooks pkg lbi usrHooks flags
notice verbosity "Relinking libvoyeur.."
let libObjs = map (libObjPath libDir) [ "voyeur"
, "net"
, "env"
, "event"
, "util"
]
componentLibs = concatMap componentLibNames $ componentsConfigs lbi
addStaticObjectFile libName objName = runAr ["r", libName, objName]
runAr = runDbProgram verbosity arProgram (withPrograms lbi)
forM_ componentLibs $ \componentLib -> do
when (withVanillaLib lbi) $
let libName = buildDir lbi </> mkLibName componentLib
in mapM_ (addStaticObjectFile libName) libObjs
when (withProfLib lbi) $
let libName = buildDir lbi </> mkProfLibName componentLib
in mapM_ (addStaticObjectFile libName) libObjs
when (withSharedLib lbi) $
let libName = buildDir lbi </> mkSharedLibName buildCompilerId componentLib
in mapM_ (addStaticObjectFile libName) libObjs
customCopyHook :: PackageDescription -> LocalBuildInfo -> UserHooks -> CopyFlags -> IO ()
customCopyHook pkg lbi hooks flags = do
let verb = fromFlagOrDefault Verbosity.normal $ copyVerbosity flags
copyHook simpleUserHooks pkg lbi hooks flags
putStrLn "Installing libvoyeur helper libraries..."
let helperLibs = [ "exec", "exit", "open", "close" ]
helperLibFiles = map (("libvoyeur-" ++) . (<.> dllExtension)) helperLibs
helperLibDir = datadir (absoluteInstallDirs pkg lbi NoCopyDest)
(_, _, libDir) <- libvoyeurPaths
copyFiles verb helperLibDir $ map (libDir,) helperLibFiles
customCleanHook :: PackageDescription -> () -> UserHooks -> CleanFlags -> IO ()
customCleanHook pkg v hooks flags = do
putStrLn "Cleaning libvoyeur..."
let verb = fromFlagOrDefault Verbosity.normal $ cleanVerbosity flags
pgmConf <- configureProgram verb (simpleProgram "make") defaultProgramConfiguration
(libvoyeurDir, _, _) <- libvoyeurPaths
inDir libvoyeurDir $
runDbProgram verb makeProgram pgmConf ["clean"]
cleanHook simpleUserHooks pkg v hooks flags
libvoyeurPaths :: IO (FilePath, FilePath, FilePath)
libvoyeurPaths = do
curDir <- getCurrentDirectory
return (curDir </> "libvoyeur",
curDir </> "libvoyeur" </> "include",
curDir </> "libvoyeur" </> "build")
componentLibNames :: (ComponentName, ComponentLocalBuildInfo, [ComponentName]) -> [LibraryName]
componentLibNames (_, LibComponentLocalBuildInfo {..}, _) = componentLibraries
componentLibNames _ = []
makeProgram :: Program
makeProgram = simpleProgram "make"
libObjPath :: FilePath -> FilePath -> FilePath
libObjPath dir name = dir </> name <.> objExtension
inDir :: FilePath -> IO a -> IO a
inDir dir act = do
curDir <- getCurrentDirectory
bracket_ (setCurrentDirectory dir)
(setCurrentDirectory curDir)
act
type Lifter a b = (a -> a) -> b -> b
onLocalPkgDescr :: Lifter PackageDescription LocalBuildInfo
onLocalPkgDescr f lbi = lbi { localPkgDescr = f (localPkgDescr lbi) }
onLibrary :: Lifter Library PackageDescription
onLibrary f lpd = lpd { library = f <$> library lpd }
onLibBuildInfo :: Lifter BuildInfo Library
onLibBuildInfo f lib = lib { libBuildInfo = f (libBuildInfo lib) }
onLocalLibBuildInfo :: Lifter BuildInfo LocalBuildInfo
onLocalLibBuildInfo = onLocalPkgDescr . onLibrary . onLibBuildInfo
onIncludeDirs :: Lifter [FilePath] BuildInfo
onIncludeDirs f libbi = libbi { includeDirs = f (includeDirs libbi) }
onLdOptions :: Lifter [FilePath] BuildInfo
onLdOptions f libbi = libbi { ldOptions = f (ldOptions libbi) }
onPkgDescr :: Lifter PackageDescription GenericPackageDescription
onPkgDescr f gpd = gpd { packageDescription = f (packageDescription gpd) }
onExtraSrcFiles :: Lifter [FilePath] PackageDescription
onExtraSrcFiles f pd = pd { extraSrcFiles = f (extraSrcFiles pd) }