{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
module Distribution.Simple.Install (
install,
) where
import Prelude ()
import Distribution.Compat.Prelude
import Distribution.Types.TargetInfo
import Distribution.Types.LocalBuildInfo
import Distribution.Types.ForeignLib
import Distribution.Types.PackageDescription
import Distribution.Types.UnqualComponentName
import Distribution.Types.ExecutableScope
import Distribution.Package
import Distribution.PackageDescription
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.BuildPaths (haddockName, haddockPref)
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose
, installDirectoryContents, installOrdinaryFile, isInSearchPath
, die', info, noticeNoWrap, warn, matchDirFileGlob )
import Distribution.Simple.Compiler
( CompilerFlavor(..), compilerFlavor )
import Distribution.Simple.Setup
( CopyFlags(..), fromFlag, HaddockTarget(ForDevelopment) )
import Distribution.Simple.BuildTarget
import qualified Distribution.Simple.GHC as GHC
import qualified Distribution.Simple.GHCJS as GHCJS
import qualified Distribution.Simple.JHC as JHC
import qualified Distribution.Simple.LHC as LHC
import qualified Distribution.Simple.UHC as UHC
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
import Distribution.Compat.Graph (IsNode(..))
import System.Directory
( doesDirectoryExist, doesFileExist )
import System.FilePath
( takeFileName, takeDirectory, (</>), isRelative )
import Distribution.Verbosity
import Distribution.Text
( display )
install :: PackageDescription
-> LocalBuildInfo
-> CopyFlags
-> IO ()
install pkg_descr lbi flags = do
checkHasLibsOrExes
targets <- readTargetInfos verbosity pkg_descr lbi (copyArgs flags)
copyPackage verbosity pkg_descr lbi distPref copydest
withNeededTargetsInBuildOrder' pkg_descr lbi (map nodeKey targets) $ \target ->
let comp = targetComponent target
clbi = targetCLBI target
in copyComponent verbosity pkg_descr lbi comp clbi copydest
where
distPref = fromFlag (copyDistPref flags)
verbosity = fromFlag (copyVerbosity flags)
copydest = fromFlag (copyDest flags)
checkHasLibsOrExes =
unless (hasLibs pkg_descr || hasForeignLibs pkg_descr || hasExes pkg_descr) $
die' verbosity "No executables and no library found. Nothing to do."
copyPackage :: Verbosity -> PackageDescription
-> LocalBuildInfo -> FilePath -> CopyDest -> IO ()
copyPackage verbosity pkg_descr lbi distPref copydest = do
let
InstallDirs {
datadir = dataPref,
docdir = docPref,
htmldir = htmlPref,
haddockdir = interfacePref}
= absoluteInstallDirs pkg_descr lbi copydest
installDataFiles verbosity pkg_descr dataPref
docExists <- doesDirectoryExist $ haddockPref ForDevelopment distPref pkg_descr
info verbosity ("directory " ++ haddockPref ForDevelopment distPref pkg_descr ++
" does exist: " ++ show docExists)
when docExists $ do
createDirectoryIfMissingVerbose verbosity True htmlPref
installDirectoryContents verbosity
(haddockPref ForDevelopment distPref pkg_descr) htmlPref
let haddockInterfaceFileSrc = haddockPref ForDevelopment distPref pkg_descr
</> haddockName pkg_descr
haddockInterfaceFileDest = interfacePref </> haddockName pkg_descr
exists <- doesFileExist haddockInterfaceFileSrc
when exists $ do
createDirectoryIfMissingVerbose verbosity True interfacePref
installOrdinaryFile verbosity haddockInterfaceFileSrc
haddockInterfaceFileDest
let lfiles = licenseFiles pkg_descr
unless (null lfiles) $ do
createDirectoryIfMissingVerbose verbosity True docPref
sequence_
[ installOrdinaryFile verbosity lfile (docPref </> takeFileName lfile)
| lfile <- lfiles ]
copyComponent :: Verbosity -> PackageDescription
-> LocalBuildInfo -> Component -> ComponentLocalBuildInfo
-> CopyDest
-> IO ()
copyComponent verbosity pkg_descr lbi (CLib lib) clbi copydest = do
let InstallDirs{
libdir = libPref,
dynlibdir = dynlibPref,
includedir = incPref
} = absoluteComponentInstallDirs pkg_descr lbi (componentUnitId clbi) copydest
buildPref = componentBuildDir lbi clbi
case libName lib of
Nothing -> noticeNoWrap verbosity ("Installing library in " ++ libPref)
Just n -> noticeNoWrap verbosity ("Installing internal library " ++ display n ++ " in " ++ libPref)
installIncludeFiles verbosity lib incPref
case compilerFlavor (compiler lbi) of
GHC -> GHC.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
GHCJS -> GHCJS.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
LHC -> LHC.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
JHC -> JHC.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
UHC -> UHC.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
HaskellSuite _ -> HaskellSuite.installLib
verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
_ -> die' verbosity $ "installing with "
++ display (compilerFlavor (compiler lbi))
++ " is not implemented"
copyComponent verbosity pkg_descr lbi (CFLib flib) clbi copydest = do
let InstallDirs{
flibdir = flibPref
} = absoluteComponentInstallDirs pkg_descr lbi (componentUnitId clbi) copydest
buildPref = componentBuildDir lbi clbi
noticeNoWrap verbosity ("Installing foreign library " ++ unUnqualComponentName (foreignLibName flib) ++ " in " ++ flibPref)
case compilerFlavor (compiler lbi) of
GHC -> GHC.installFLib verbosity lbi flibPref buildPref pkg_descr flib
_ -> die' verbosity $ "installing foreign lib with "
++ display (compilerFlavor (compiler lbi))
++ " is not implemented"
copyComponent verbosity pkg_descr lbi (CExe exe) clbi copydest = do
let installDirs = absoluteComponentInstallDirs pkg_descr lbi (componentUnitId clbi) copydest
buildPref = buildDir lbi
uid = componentUnitId clbi
pkgid = packageId pkg_descr
binPref | ExecutablePrivate <- exeScope exe = libexecdir installDirs
| otherwise = bindir installDirs
progPrefixPref = substPathTemplate pkgid lbi uid (progPrefix lbi)
progSuffixPref = substPathTemplate pkgid lbi uid (progSuffix lbi)
progFix = (progPrefixPref, progSuffixPref)
noticeNoWrap verbosity ("Installing executable " ++ display (exeName exe)
++ " in " ++ binPref)
inPath <- isInSearchPath binPref
when (not inPath) $
warn verbosity ("The directory " ++ binPref
++ " is not in the system search path.")
case compilerFlavor (compiler lbi) of
GHC -> GHC.installExe verbosity lbi binPref buildPref progFix pkg_descr exe
GHCJS -> GHCJS.installExe verbosity lbi binPref buildPref progFix pkg_descr exe
LHC -> LHC.installExe verbosity lbi binPref buildPref progFix pkg_descr exe
JHC -> JHC.installExe verbosity binPref buildPref progFix pkg_descr exe
UHC -> return ()
HaskellSuite {} -> return ()
_ -> die' verbosity $ "installing with "
++ display (compilerFlavor (compiler lbi))
++ " is not implemented"
copyComponent _ _ _ (CBench _) _ _ = return ()
copyComponent _ _ _ (CTest _) _ _ = return ()
installDataFiles :: Verbosity -> PackageDescription -> FilePath -> IO ()
installDataFiles verbosity pkg_descr destDataDir =
flip traverse_ (dataFiles pkg_descr) $ \ file -> do
let srcDataDir = dataDir pkg_descr
files <- matchDirFileGlob srcDataDir file
let dir = takeDirectory file
createDirectoryIfMissingVerbose verbosity True (destDataDir </> dir)
sequence_ [ installOrdinaryFile verbosity (srcDataDir </> file')
(destDataDir </> file')
| file' <- files ]
installIncludeFiles :: Verbosity -> Library -> FilePath -> IO ()
installIncludeFiles verbosity lib destIncludeDir = do
let relincdirs = "." : filter isRelative (includeDirs lbi)
lbi = libBuildInfo lib
incs <- traverse (findInc relincdirs) (installIncludes lbi)
sequence_
[ do createDirectoryIfMissingVerbose verbosity True destDir
installOrdinaryFile verbosity srcFile destFile
| (relFile, srcFile) <- incs
, let destFile = destIncludeDir </> relFile
destDir = takeDirectory destFile ]
where
findInc [] file = die' verbosity ("can't find include file " ++ file)
findInc (dir:dirs) file = do
let path = dir </> file
exists <- doesFileExist path
if exists then return (file, path) else findInc dirs file