-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSetup.hs
165 lines (150 loc) · 6.74 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
160
161
162
163
164
165
{-# LANGUAGE CPP #-}
import Control.Monad (unless, when)
import Data.Char (toLower)
import Data.Maybe (fromJust, fromMaybe)
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo (InstallDirs (..),
LocalBuildInfo (..),
absoluteInstallDirs,
localPkgDescr)
import Distribution.Simple.Program.Find (defaultProgramSearchPath,
findProgramOnSearchPath)
import Distribution.Simple.Setup
import Distribution.Simple.Utils (cabalVersion,
installExecutableFile,
rawSystemExit,
rawSystemStdout)
import Distribution.System
import qualified Distribution.Verbosity as Verbosity
import System.Directory (doesFileExist,
getCurrentDirectory)
#if MIN_VERSION_Cabal(2, 0, 0)
import Distribution.Version (mkVersion)
#else
mkVersion :: [Int] -> Version
mkVersion = flip Version []
mkFlagName :: String -> FlagName
mkFlagName = FlagName
#endif
main = defaultMainWithHooks hooksFix
where
hooks = simpleUserHooks {
preConf = makeLibsass
, confHook = \a f -> confHook simpleUserHooks a f >>= updateExtraLibDirs
, postConf = disablePostConfHooks
, preBuild = updateLibDirs
, postCopy = copyLibsass
, postClean = cleanLibsass
}
-- Fix for Cabal-1.18 - it does not `copy` on `install`, so we `copy` on
-- `install` manually. ;)
hooksFix = if cabalVersion < mkVersion [1, 20, 0]
then hooks { postInst = installLibsass }
else hooks
execMake :: Verbosity.Verbosity -> String -> String -> IO ()
execMake verbosity build_target target = do
gmakePath <- findProgramOnSearchPath Verbosity.silent defaultProgramSearchPath "gmake"
let makeExec = case gmakePath of
#if MIN_VERSION_Cabal(1, 24, 0)
Just (p, _) -> p
#else
Just p -> p
#endif
Nothing -> "make"
baseArgs = if null build_target
then [makeExec, "--directory=libsass"]
else ["BUILD=" ++ build_target, makeExec, "--directory=libsass"]
makeArgs = if null target
then baseArgs
else baseArgs ++ [target]
rawSystemExit verbosity "env" makeArgs
updateLibsassVersion :: ConfigFlags -> IO ()
updateLibsassVersion flags = do
let verbosity = fromFlag $ configVerbosity flags
exists <- doesFileExist "libsass/VERSION"
unless exists $ do
ver <- rawSystemStdout verbosity "env" [ "git", "-C", "libsass", "describe",
"--abbrev=4", "--dirty", "--always", "--tags" ]
writeFile "libsass/VERSION" ver
makeLibsass :: Args -> ConfigFlags -> IO HookedBuildInfo
makeLibsass _ f = do
let verbosity = fromFlag $ configVerbosity f
external = getCabalFlag "externalLibsass" f
target = if getCabalFlag "sharedLibsass" f then "shared" else "static"
unless external $ updateLibsassVersion f
unless external $ execMake verbosity target ""
return emptyHookedBuildInfo
disablePostConfHooks :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
disablePostConfHooks args flags pd lbi
| getCabalFlag "externalLibsass" flags = postConf simpleUserHooks args flags pd lbi
| otherwise = return ()
updateLibDirs :: Args -> BuildFlags -> IO HookedBuildInfo
updateLibDirs _ _ = do
dir <- getCurrentDirectory
let libsassDir = dir ++ "/libsass/lib"
bi = emptyBuildInfo { extraLibDirs = [ libsassDir ] }
return (Just bi, [])
updateExtraLibDirs :: LocalBuildInfo -> IO LocalBuildInfo
updateExtraLibDirs lbi
| getCabalFlag "externalLibsass" $ configFlags lbi = return lbi
| otherwise = do
let pkg_descr = localPkgDescr lbi
lib = fromJust $ library pkg_descr
libBuild = libBuildInfo lib
libPref = libdir $ absoluteInstallDirs pkg_descr lbi NoCopyDest
return lbi {
localPkgDescr = pkg_descr {
library = Just $ lib {
libBuildInfo = libBuild {
extraLibDirs = libPref : extraLibDirs libBuild
}
}
}
}
copyLib :: ConfigFlags -> LocalBuildInfo -> FilePath -> IO ()
copyLib fl lbi libPref =
let verb = fromFlag $ configVerbosity fl
external = getCabalFlag "externalLibsass" fl
Platform _ os = hostPlatform lbi
shared = getCabalFlag "sharedLibsass" fl
ext = if shared then "so" else "a"
in unless external $
if os == Windows
then do
installExecutableFile verb
"libsass/lib/libsass.a"
(libPref ++ "/libsass.a")
when shared $ installExecutableFile verb
"libsass/lib/libsass.dll"
(libPref ++ "/libsass.dll")
else
installExecutableFile verb
("libsass/lib/libsass." ++ ext)
(libPref ++ "/libsass." ++ ext)
copyLibsass :: Args -> CopyFlags -> PackageDescription -> LocalBuildInfo -> IO ()
copyLibsass _ flags pkg_descr lbi =
let libPref = libdir . absoluteInstallDirs pkg_descr lbi
. fromFlag . copyDest
$ flags
config = configFlags lbi
in copyLib config lbi libPref
installLibsass :: Args -> InstallFlags -> PackageDescription -> LocalBuildInfo -> IO ()
installLibsass _ flags pkg_descr lbi =
let libPref = libdir $ absoluteInstallDirs pkg_descr lbi NoCopyDest
config = configFlags lbi
in copyLib config lbi libPref
cleanLibsass :: Args -> CleanFlags -> PackageDescription -> () -> IO ()
cleanLibsass _ flags _ _ =
execMake (fromFlag $ cleanVerbosity flags) "" "clean"
#if MIN_VERSION_Cabal(2, 2, 0)
getCabalFlag :: String -> ConfigFlags -> Bool
getCabalFlag name flags = fromMaybe False (lookupFlagAssignment (mkFlagName name') allFlags)
where allFlags = configConfigurationsFlags flags
name' = map toLower name
#else
getCabalFlag :: String -> ConfigFlags -> Bool
getCabalFlag name flags = fromMaybe False (lookup (mkFlagName name') allFlags)
where allFlags = configConfigurationsFlags flags
name' = map toLower name
#endif