-
Notifications
You must be signed in to change notification settings - Fork 3
/
Setup.hs
100 lines (90 loc) · 4.23 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
{-# LANGUAGE CPP #-}
module Main (main) where
import Control.Monad
import Distribution.Pretty
import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.Simple.LocalBuildInfo
import Distribution.Verbosity
import Distribution.Simple.Utils
import Distribution.Simple.Program
import Distribution.Simple.Program.Db
import Distribution.System
import Distribution.Utils.NubList
import Distribution.Types.Library
import Distribution.Types.BuildInfo
import Distribution.Types.LocalBuildInfo
import Distribution.Types.PackageDescription
import Data.Maybe
import Data.List
import System.Directory
import System.FilePath
import qualified System.Environment as System
main :: IO ()
main = do
mainArgs <- System.getArgs
if head mainArgs == "configure"
then defaultMainWithHooksArgs simpleUserHooks {
postConf = \ args flags pkg_descr lbi -> do
let verbosity = fromFlag (configVerbosity flags)
baseDir lbi' = fromMaybe "" (takeDirectory <$> cabalFilePath lbi')
configFile = baseDir lbi </> "third_party" </> "botan" </> "configure.py"
configFolder = baseDir lbi </> "third_party" </> "botan"
placeholder = "build" </> "include" </> "external" </> "cabal.placeholder"
confExists <- doesFileExist configFile
if confExists
then do
runConfigureScript configFolder configFile verbosity flags lbi
runTouch configFolder placeholder verbosity flags
else die' verbosity "botan configure script not found."
postConf simpleUserHooks args flags pkg_descr lbi
, regHook = \ _ _ _ _ -> return ()
#if !MIN_VERSION_Cabal(3,2,1)
} mainArgs
#else
} ("--ghc-options":"-optcxx-std=c++11":mainArgs)
#endif
else defaultMain
runConfigureScript :: FilePath -> FilePath -> Verbosity -> ConfigFlags -> LocalBuildInfo -> IO ()
runConfigureScript configFolder configFile verbosity flags lbi = do
env <- System.getEnvironment
configureFile <- makeAbsolute configFile
let extraPath = fromNubList $ configProgramPathExtra flags
spSep = [searchPathSeparator]
pathEnv = maybe (intercalate spSep extraPath)
((intercalate spSep extraPath ++ spSep)++) $ lookup "PATH" env
pyProgs = simpleProgram <$> ["python", "python2", "python3"]
progDb = modifyProgramSearchPath
(\p -> map ProgramSearchPathDir extraPath ++ p) emptyProgramDb
overEnv = [("PATH", Just pathEnv) | not (null extraPath)]
hp@(Platform arch os) = hostPlatform lbi
-- use gcc/mingw bunlded with GHC
osStr = if os == Windows then "mingw" else (show (pretty os))
hostFlag = [ "--cpu=" ++ show (pretty arch), "--os=" ++ osStr]
-- pass amalgamation to produce botan_all.cpp
args = configureFile:"--amalgamation":"--disable-shared":hostFlag
pyConfiguredProg <- forM pyProgs $ \ pyProg ->
lookupProgram pyProg <$> configureProgram verbosity pyProg progDb
case msum (pyConfiguredProg) of
Just py -> runProgramInvocation verbosity $
(programInvocation (py {programOverrideEnv = overEnv}) args)
{ progInvokeCwd = Just configFolder }
Nothing -> die' verbosity notFoundMsg
where
notFoundMsg = "The package's dep(botan) has a 'configure.py' script. "
++ "This requires python is discoverable in your path."
runTouch :: FilePath -> FilePath -> Verbosity -> ConfigFlags -> IO ()
runTouch configFolder placeholder verbosity flags = do
let extraPath = fromNubList $ configProgramPathExtra flags
progDb = modifyProgramSearchPath
(\p -> map ProgramSearchPathDir extraPath ++ p) emptyProgramDb
touchPG = simpleProgram "touch"
touch <- lookupProgram touchPG <$> configureProgram verbosity touchPG progDb
case touch of
Just touch' ->
runProgramInvocation verbosity $
(programInvocation touch' [placeholder])
{ progInvokeCwd = Just configFolder }
Nothing -> warn verbosity notFoundMsg
where
notFoundMsg = "touch is required to keep a cabal placeholder file, otherwise cabal sdist may produce an unbuildable tarball"