forked from scrive/hpqtypes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.hs
67 lines (60 loc) · 2.19 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
{-# OPTIONS_GHC -Wall #-}
import Control.Monad
import Data.Char
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import Distribution.Verbosity
import System.Directory
import System.Exit
import System.FilePath
main :: IO ()
main = defaultMainWithHooks simpleUserHooks {
hookedPrograms = [pgconfigProgram]
, confHook = \pkg flags -> do
lbi <- confHook simpleUserHooks pkg flags
let verbosity = fromFlag $ configVerbosity flags
pqTypesConfigure verbosity
bi <- psqlBuildInfo verbosity lbi
return lbi {
localPkgDescr = updatePackageDescription (Just bi, []) (localPkgDescr lbi)
}
, cleanHook = \desc unit userHooks flags -> do
cleanHook simpleUserHooks desc unit userHooks flags
pqTypesDistclean (fromFlag $ cleanVerbosity flags)
}
pgconfigProgram :: Program
pgconfigProgram = (simpleProgram "pgconfig") {
programFindLocation = \verbosity psp -> do
pgconfig <- findProgramOnSearchPath verbosity psp "pgconfig"
pg_config <- findProgramOnSearchPath verbosity psp "pg_config"
return $ pgconfig `mplus` pg_config
}
psqlBuildInfo :: Verbosity -> LocalBuildInfo -> IO BuildInfo
psqlBuildInfo verbosity lbi = do
(pgconfigProg, _) <- requireProgram verbosity pgconfigProgram (withPrograms lbi)
let pgconfig = getProgramOutput verbosity pgconfigProg
incDir <- pgconfig ["--includedir"]
libDir <- pgconfig ["--libdir"]
return emptyBuildInfo {
extraLibDirs = [trim libDir]
, includeDirs = [trim incDir]
}
where
trim = let f = reverse . dropWhile isSpace in f . f
pqTypesConfigure :: Verbosity -> IO ()
pqTypesConfigure verbosity = do
dir <- getCurrentDirectory
setCurrentDirectory $ dir </> "libpqtypes"
-- run configure to create appropriate pqt_config.h
res <- rawSystemExitCode verbosity "env" ["./configure"]
case res of
ExitFailure _ -> putStrLn "WARNING: configure failed, using static pqt_config.h"
_ -> return ()
setCurrentDirectory dir
pqTypesDistclean :: Verbosity -> IO ()
pqTypesDistclean verbosity =
rawSystemExit verbosity "env" ["make", "--directory=libpqtypes", "distclean"]