2024-02-25 21:22:53 +00:00
|
|
|
import Control.Exception (throw)
|
|
|
|
import Control.Monad (forM_, when)
|
2024-02-21 16:51:03 +00:00
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Distribution.PackageDescription
|
|
|
|
import Distribution.Simple
|
|
|
|
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo(..), localPkgDescr)
|
|
|
|
import Distribution.Simple.PreProcess
|
|
|
|
import Distribution.Simple.Program.Find
|
|
|
|
( defaultProgramSearchPath
|
|
|
|
, findProgramOnSearchPath
|
|
|
|
)
|
|
|
|
import Distribution.Simple.Setup
|
|
|
|
import Distribution.Simple.Utils
|
|
|
|
( IODataMode(IODataModeBinary)
|
|
|
|
, maybeExit
|
|
|
|
, rawSystemStdInOut
|
|
|
|
)
|
|
|
|
import Distribution.Verbosity (Verbosity)
|
|
|
|
import qualified Distribution.Verbosity as Verbosity
|
2024-02-25 21:22:53 +00:00
|
|
|
import GHC.Generics
|
|
|
|
import System.Directory
|
|
|
|
( XdgDirectory(..)
|
|
|
|
, copyFile
|
|
|
|
, createDirectory
|
|
|
|
, createDirectoryIfMissing
|
|
|
|
, doesDirectoryExist
|
|
|
|
, doesFileExist
|
|
|
|
, getCurrentDirectory
|
|
|
|
, getDirectoryContents
|
|
|
|
, getHomeDirectory
|
|
|
|
, getXdgDirectory
|
|
|
|
)
|
|
|
|
import System.Environment
|
2024-02-21 16:51:03 +00:00
|
|
|
import System.FilePath ((</>))
|
2024-02-25 21:22:53 +00:00
|
|
|
import Text.Regex
|
|
|
|
import Text.Regex.Base
|
2024-02-21 16:51:03 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = defaultMainWithHooks hooks
|
|
|
|
where
|
|
|
|
hooks =
|
|
|
|
simpleUserHooks
|
|
|
|
{ preConf =
|
|
|
|
\_ flags -> do
|
|
|
|
rsMake (fromFlag $ configVerbosity flags)
|
|
|
|
pure emptyHookedBuildInfo
|
|
|
|
, hookedPreProcessors = knownSuffixHandlers
|
|
|
|
, confHook = \a flags -> confHook simpleUserHooks a flags >>= rsAddDirs
|
|
|
|
, postClean = \_ flags _ _ -> rsClean (fromFlag $ cleanVerbosity flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
rsFolder :: FilePath
|
|
|
|
rsFolder = "librustzcash-wrapper"
|
|
|
|
|
|
|
|
execCargo :: Verbosity -> String -> [String] -> IO ()
|
|
|
|
execCargo verbosity command args = do
|
|
|
|
cargoPath <-
|
2024-03-03 21:29:50 +00:00
|
|
|
findProgramOnSearchPath Verbosity.normal defaultProgramSearchPath "cargo"
|
2024-02-21 16:51:03 +00:00
|
|
|
dir <- getCurrentDirectory
|
|
|
|
let cargoExec =
|
|
|
|
case cargoPath of
|
|
|
|
Just (p, _) -> p
|
|
|
|
Nothing -> "cargo"
|
|
|
|
cargoArgs = command : args
|
|
|
|
workingDir = Just (dir </> rsFolder)
|
|
|
|
thirdComponent (_, _, c) = c
|
|
|
|
maybeExit . fmap thirdComponent $
|
|
|
|
rawSystemStdInOut
|
|
|
|
verbosity
|
|
|
|
cargoExec
|
|
|
|
cargoArgs
|
|
|
|
workingDir
|
|
|
|
Nothing
|
|
|
|
Nothing
|
|
|
|
IODataModeBinary
|
|
|
|
|
|
|
|
rsMake :: Verbosity -> IO ()
|
2024-02-25 21:22:53 +00:00
|
|
|
rsMake verbosity = do
|
|
|
|
execCargo verbosity "cbuild" []
|
2024-02-21 16:51:03 +00:00
|
|
|
|
|
|
|
rsAddDirs :: LocalBuildInfo -> IO LocalBuildInfo
|
|
|
|
rsAddDirs lbi' = do
|
2024-02-25 21:22:53 +00:00
|
|
|
localData <- getXdgDirectory XdgData "zcash-haskell"
|
|
|
|
createDirectoryIfMissing True localData
|
2024-02-21 16:51:03 +00:00
|
|
|
dir <- getCurrentDirectory
|
|
|
|
let rustIncludeDir =
|
|
|
|
dir </> rsFolder </> "target/x86_64-unknown-linux-gnu/debug"
|
|
|
|
rustLibDir = dir </> rsFolder </> "target/x86_64-unknown-linux-gnu/debug"
|
|
|
|
updateLbi lbi = lbi {localPkgDescr = updatePkgDescr (localPkgDescr lbi)}
|
|
|
|
updatePkgDescr pkgDescr =
|
|
|
|
pkgDescr {library = updateLib <$> library pkgDescr}
|
|
|
|
updateLib lib = lib {libBuildInfo = updateLibBi (libBuildInfo lib)}
|
|
|
|
updateLibBi libBuild =
|
|
|
|
libBuild
|
|
|
|
{ includeDirs = rustIncludeDir : includeDirs libBuild
|
|
|
|
, extraLibDirs = rustLibDir : extraLibDirs libBuild
|
|
|
|
}
|
2024-02-25 21:22:53 +00:00
|
|
|
copyDir rustLibDir localData
|
2024-02-21 16:51:03 +00:00
|
|
|
pure $ updateLbi lbi'
|
|
|
|
|
|
|
|
rsClean :: Verbosity -> IO ()
|
|
|
|
rsClean verbosity = execCargo verbosity "clean" []
|
|
|
|
|
|
|
|
cabalFlag :: FlagName -> ConfigFlags -> Bool
|
|
|
|
cabalFlag name =
|
|
|
|
fromMaybe False . lookupFlagAssignment name . configConfigurationsFlags
|
|
|
|
|
|
|
|
unlessFlagM :: FlagName -> ConfigFlags -> IO () -> IO ()
|
|
|
|
unlessFlagM name flags action
|
|
|
|
| cabalFlag name flags = pure ()
|
|
|
|
| otherwise = action
|
|
|
|
|
|
|
|
applyUnlessM :: FlagName -> ConfigFlags -> (a -> IO a) -> a -> IO a
|
|
|
|
applyUnlessM name flags apply a
|
|
|
|
| cabalFlag name flags = pure a
|
|
|
|
| otherwise = apply a
|
2024-02-25 21:22:53 +00:00
|
|
|
|
|
|
|
copyDir :: FilePath -> FilePath -> IO ()
|
|
|
|
copyDir src dst = do
|
|
|
|
whenM (not <$> doesDirectoryExist src) $
|
|
|
|
throw (userError "source does not exist")
|
|
|
|
--whenM (doesFileOrDirectoryExist dst) $
|
|
|
|
--throw (userError "destination already exists")
|
|
|
|
createDirectoryIfMissing True dst
|
|
|
|
content <- getDirectoryContents src
|
|
|
|
let xs = filter (`notElem` [".", ".."]) content
|
|
|
|
forM_ xs $ \name -> do
|
|
|
|
let srcPath = src </> name
|
|
|
|
let dstPath = dst </> name
|
|
|
|
isDirectory <- doesDirectoryExist srcPath
|
|
|
|
if isDirectory
|
|
|
|
then copyDir srcPath dstPath
|
|
|
|
else copyFile srcPath dstPath
|
|
|
|
where
|
|
|
|
doesFileOrDirectoryExist x = orM [doesDirectoryExist x, doesFileExist x]
|
|
|
|
orM xs = or <$> sequence xs
|
|
|
|
whenM s r = s >>= flip when r
|