Commit 02ac5ebf authored by Duncan Coutts's avatar Duncan Coutts
Browse files

Rewrite the Register module

It was getting increasingly convoluted and incomprehensible.
Now uses the Program.HcPkg and Program.Scripts modules.
parent af3da760
......@@ -95,7 +95,7 @@ import Distribution.Simple.Command
import Distribution.Simple.Build ( build )
import Distribution.Simple.SrcDist ( sdist )
import Distribution.Simple.Register
( register, unregister, removeRegScripts )
( register, unregister )
import Distribution.Simple.Configure
( getPersistBuildConfig, maybeGetPersistBuildConfig
......@@ -385,9 +385,6 @@ clean pkg_descr flags = do
exists <- doesDirectoryExist distPref
when exists (removeDirectoryRecursive distPref)
-- these live in the top level dir so must be removed separately
removeRegScripts
-- Any extra files the user wants to remove
mapM_ removeFileOrDirectory (extraTmpFiles pkg_descr)
......
......@@ -107,8 +107,6 @@ import Distribution.Simple.Utils
, intercalate, comparing, cabalVersion, cabalBootstrapping
, withFileContents, writeFileAtomic
, withTempFile )
import Distribution.Simple.Register
( removeInstalledConfig )
import Distribution.System
( OS(..), buildOS, buildArch )
import Distribution.Version
......@@ -403,8 +401,6 @@ configure (pkg_descr0, pbi) cfg
| (name, uses) <- inconsistencies
, (pkg, ver) <- uses ]
removeInstalledConfig distPref
-- installation directories
defaultDirs <- defaultInstallDirs flavor userInstall (hasLibs pkg_descr)
let installDirs = combineInstallDirs fromFlagOrDefault
......
......@@ -54,11 +54,13 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
module Distribution.Simple.Register (
register,
unregister,
writeInstalledConfig,
removeInstalledConfig,
removeRegScripts,
register,
unregister,
registerPackage,
inplaceInstalledPackageInfo,
absoluteInstalledPackageInfo,
generalInstalledPackageInfo,
) where
import Distribution.Simple.LocalBuildInfo
......@@ -68,14 +70,17 @@ import Distribution.Simple.BuildPaths (haddockName)
import Distribution.Simple.Compiler
( CompilerFlavor(..), compilerFlavor
, PackageDB(..), registrationPackageDB )
import Distribution.Simple.Program (ConfiguredProgram, programPath,
programArgs, rawSystemProgram,
lookupProgram, ghcPkgProgram, lhcPkgProgram)
import Distribution.Simple.Program
( ConfiguredProgram, runProgramInvocation
, requireProgram, lookupProgram, ghcPkgProgram, lhcPkgProgram )
import Distribution.Simple.Program.Script
( invocationAsSystemScript )
import qualified Distribution.Simple.Program.HcPkg as HcPkg
import Distribution.Simple.Setup
( RegisterFlags(..), CopyDest(..)
, fromFlag, fromFlagOrDefault, flagToMaybe )
import Distribution.PackageDescription (PackageDescription(..),
BuildInfo(..), Library(..))
import Distribution.PackageDescription
( PackageDescription(..), Library(..), BuildInfo(..), hcOptions )
import Distribution.Package
( Package(..), packageName )
import Distribution.InstalledPackageInfo
......@@ -83,34 +88,27 @@ import Distribution.InstalledPackageInfo
, showInstalledPackageInfo )
import qualified Distribution.InstalledPackageInfo as IPI
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose, installOrdinaryFile, writeFileAtomic
, die, info, notice, setupMessage )
( createDirectoryIfMissingVerbose, writeFileAtomic
, die, notice, setupMessage )
import Distribution.System
( OS(..), buildOS )
import Distribution.Text
( display )
import Distribution.Verbosity ( normal, deafening )
import Distribution.Verbosity as Verbosity
( Verbosity, normal )
import Distribution.Compat.CopyFile
( setFileExecutable )
import System.FilePath ((</>), (<.>), isAbsolute)
import System.Directory (removeFile, getCurrentDirectory,
removeDirectoryRecursive,
setPermissions, getPermissions,
Permissions(executable))
import System.Directory
( getCurrentDirectory, removeDirectoryRecursive )
import System.IO.Error (try)
import Control.Monad (when)
import Data.Maybe (isNothing, isJust, fromJust, fromMaybe)
import Data.Maybe
( isJust, fromMaybe )
import Data.List (partition)
regScriptLocation :: FilePath
regScriptLocation = case buildOS of
Windows -> "register.bat"
_ -> "register.sh"
unregScriptLocation :: FilePath
unregScriptLocation = case buildOS of
Windows -> "unregister.bat"
_ -> "unregister.sh"
-- -----------------------------------------------------------------------------
-- Registration
......@@ -118,248 +116,278 @@ unregScriptLocation = case buildOS of
register :: PackageDescription -> LocalBuildInfo
-> RegisterFlags -- ^Install in the user's database?; verbose
-> IO ()
register pkg_descr lbi regFlags
| isNothing (library pkg_descr) = do
setupMessage (fromFlag $ regVerbosity regFlags) "No package to register" (packageId pkg_descr)
return ()
| otherwise = do
let distPref = fromFlag $ regDistPref regFlags
isWindows = case buildOS of Windows -> True; _ -> False
genScript = fromFlag (regGenScript regFlags)
genPkgConf = isJust (flagToMaybe (regGenPkgConf regFlags))
genPkgConfigDefault = display (packageId pkg_descr) <.> "conf"
genPkgConfigFile = fromMaybe genPkgConfigDefault
(fromFlag (regGenPkgConf regFlags))
verbosity = fromFlag (regVerbosity regFlags)
packageDB = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
(regPackageDB regFlags)
inplace = fromFlag (regInPlace regFlags)
message | genPkgConf = "Writing package registration file: "
++ genPkgConfigFile ++ " for"
| genScript = "Writing registration script: "
++ regScriptLocation ++ " for"
| otherwise = "Registering"
setupMessage verbosity message (packageId pkg_descr)
case compilerFlavor (compiler lbi) of
GHC -> do
config_flags <- case packageDB of
GlobalPackageDB -> return []
UserPackageDB -> return ["--user"]
SpecificPackageDB db -> return ["--package-conf=" ++ db]
let instConf | genPkgConf = genPkgConfigFile
| inplace = inplacePkgConfigFile distPref
| otherwise = installedPkgConfigFile distPref
when (genPkgConf || not genScript) $ do
info verbosity ("create " ++ instConf)
writeInstalledConfig distPref pkg_descr lbi inplace instConf
let register_flags = let conf = if genScript && not isWindows
then ["-"]
else [instConf]
in "update" : conf
let verbosity_flags = if verbosity >= deafening
then ["-v2"]
else if verbosity >= normal
then []
else ["-v0"]
let allFlags = config_flags ++ register_flags ++ verbosity_flags
let Just pkgTool = lookupProgram ghcPkgProgram (withPrograms lbi)
case () of
_ | genPkgConf -> return ()
| genScript ->
do cfg <- showInstalledConfig distPref pkg_descr lbi inplace
rawSystemPipe pkgTool regScriptLocation cfg allFlags
_ -> rawSystemProgram verbosity pkgTool allFlags
LHC -> do
config_flags <- case packageDB of
GlobalPackageDB -> return []
UserPackageDB -> return ["--user"]
SpecificPackageDB db -> return ["--package-conf=" ++ db]
let instConf | genPkgConf = genPkgConfigFile
| inplace = inplacePkgConfigFile distPref
| otherwise = installedPkgConfigFile distPref
when (genPkgConf || not genScript) $ do
info verbosity ("create " ++ instConf)
writeInstalledConfig distPref pkg_descr lbi inplace instConf
let register_flags = let conf = if genScript && not isWindows
then ["-"]
else [instConf]
in "update" : conf
let allFlags = config_flags ++ register_flags
let Just pkgTool = lookupProgram lhcPkgProgram (withPrograms lbi)
case () of
_ | genPkgConf -> return ()
| genScript ->
do cfg <- showInstalledConfig distPref pkg_descr lbi inplace
rawSystemPipe pkgTool regScriptLocation cfg allFlags
_ -> rawSystemProgram verbosity pkgTool allFlags
Hugs -> do
when inplace $ die "--inplace is not supported with Hugs"
let installDirs = absoluteInstallDirs (packageId pkg_descr) lbi NoCopyDest
createDirectoryIfMissingVerbose verbosity True (libdir installDirs)
installOrdinaryFile verbosity (installedPkgConfigFile distPref)
(libdir installDirs </> "package.conf")
JHC -> notice verbosity "registering for JHC (nothing to do)"
NHC -> notice verbosity "registering nhc98 (nothing to do)"
_ -> die "only registering with GHC/Hugs/jhc/nhc98 is implemented"
register pkg@PackageDescription { library = Just lib }
lbi@LocalBuildInfo { libraryConfig = Just clbi } regFlags
-- Three different modes:
| modeGenerateRegFile = writeRegistrationFile
| modeGenerateRegScript = writeRegisterScript
| otherwise = registerPackage verbosity
pkg lib lbi clbi distPref inplace packageDb
where
modeGenerateRegFile = isJust (flagToMaybe (regGenPkgConf regFlags))
regFile = fromMaybe (display (packageId pkg) <.> "conf")
(fromFlag (regGenPkgConf regFlags))
modeGenerateRegScript = fromFlag (regGenScript regFlags)
inplace = fromFlag (regInPlace regFlags)
packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
(regPackageDB regFlags)
distPref = fromFlag (regDistPref regFlags)
verbosity = fromFlag (regVerbosity regFlags)
writeRegistrationFile = do
installedPkgInfo <- generateRegistrationInfo
pkg lib lbi clbi inplace distPref
notice verbosity ("Creating package registration file: " ++ regFile)
writeFileAtomic regFile (showInstalledPackageInfo installedPkgInfo ++ "\n")
writeRegisterScript =
case compilerFlavor (compiler lbi) of
GHC -> do (ghcPkg, _) <- requireProgram verbosity ghcPkgProgram (withPrograms lbi)
writeHcPkgRegisterScript verbosity ghcPkg pkg lib lbi clbi distPref inplace packageDb
LHC -> do (lhcPkg, _) <- requireProgram verbosity lhcPkgProgram (withPrograms lbi)
writeHcPkgRegisterScript verbosity lhcPkg pkg lib lbi clbi distPref inplace packageDb
Hugs -> notice verbosity "Registration scripts not needed for hugs"
JHC -> notice verbosity "Registration scripts not needed for jhc"
NHC -> notice verbosity "Registration scripts not needed for nhc98"
_ -> die "Registration scripts are not implemented for this compiler"
register _ _ regFlags = notice verbosity "No package to register"
where
verbosity = fromFlag (regVerbosity regFlags)
generateRegistrationInfo :: PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> Bool
-> FilePath
-> IO InstalledPackageInfo
generateRegistrationInfo pkg lib lbi clbi inplace distPref = do
--TODO: eliminate pwd!
pwd <- getCurrentDirectory
let installedPkgInfo
| inplace = inplaceInstalledPackageInfo pwd distPref
pkg lib lbi clbi
| otherwise = absoluteInstalledPackageInfo
pkg lib lbi clbi
return installedPkgInfo
registerPackage :: Verbosity
-> PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> FilePath
-> Bool
-> PackageDB
-> IO ()
registerPackage verbosity pkg lib lbi clbi distPref inplace packageDb = do
setupMessage verbosity "Registering" (packageId pkg)
case compilerFlavor (compiler lbi) of
GHC -> registerPackageGHC verbosity pkg lib lbi clbi distPref inplace packageDb
LHC -> registerPackageLHC verbosity pkg lib lbi clbi distPref inplace packageDb
Hugs -> registerPackageHugs verbosity pkg lib lbi clbi distPref inplace packageDb
JHC -> notice verbosity "Registering for jhc (nothing to do)"
NHC -> notice verbosity "Registering for nhc98 (nothing to do)"
_ -> die "Registering is not implemented for this compiler"
registerPackageGHC, registerPackageLHC, registerPackageHugs
:: Verbosity
-> PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> FilePath
-> Bool
-> PackageDB
-> IO ()
registerPackageGHC verbosity pkg lib lbi clbi distPref inplace packageDb = do
installedPkgInfo <- generateRegistrationInfo
pkg lib lbi clbi inplace distPref
let Just ghcPkg = lookupProgram ghcPkgProgram (withPrograms lbi)
HcPkg.reregister verbosity ghcPkg packageDb (Right installedPkgInfo)
registerPackageLHC verbosity pkg lib lbi clbi distPref inplace packageDb = do
installedPkgInfo <- generateRegistrationInfo
pkg lib lbi clbi inplace distPref
let Just lhcPkg = lookupProgram lhcPkgProgram (withPrograms lbi)
HcPkg.reregister verbosity lhcPkg packageDb (Right installedPkgInfo)
registerPackageHugs verbosity pkg lib lbi clbi distPref inplace _packageDb = do
when inplace $ die "--inplace is not supported with Hugs"
installedPkgInfo <- generateRegistrationInfo
pkg lib lbi clbi inplace distPref
let installDirs = absoluteInstallDirs (packageId pkg) lbi NoCopyDest
createDirectoryIfMissingVerbose verbosity True (libdir installDirs)
writeFileAtomic (libdir installDirs </> "package.conf")
(showInstalledPackageInfo installedPkgInfo ++ "\n")
writeHcPkgRegisterScript :: Verbosity
-> ConfiguredProgram
-> PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> FilePath
-> Bool
-> PackageDB
-> IO ()
writeHcPkgRegisterScript verbosity hcPkg pkg lib lbi clbi distPref inplace packageDb = do
installedPkgInfo <- generateRegistrationInfo
pkg lib lbi clbi inplace distPref
let invocation = HcPkg.reregisterInvocation hcPkg Verbosity.normal
packageDb (Right installedPkgInfo)
regScript = invocationAsSystemScript buildOS invocation
notice verbosity ("Creating package registration script: " ++ regScriptFileName)
writeFileAtomic regScriptFileName regScript
setFileExecutable regScriptFileName
regScriptFileName :: FilePath
regScriptFileName = case buildOS of
Windows -> "register.bat"
_ -> "register.sh"
-- -----------------------------------------------------------------------------
-- The installed package config
-- |Register doesn't drop the register info file, it must be done in a
-- separate step.
writeInstalledConfig :: FilePath -> PackageDescription -> LocalBuildInfo
-> Bool -> FilePath -> IO ()
writeInstalledConfig distPref pkg_descr lbi inplace instConf = do
pkg_config <- showInstalledConfig distPref pkg_descr lbi inplace
writeFileAtomic instConf (pkg_config ++ "\n")
-- |Create a string suitable for writing out to the package config file
showInstalledConfig :: FilePath -> PackageDescription -> LocalBuildInfo -> Bool
-> IO String
showInstalledConfig distPref pkg_descr lbi inplace
= do cfg <- mkInstalledPackageInfo distPref pkg_descr lbi inplace
return (showInstalledPackageInfo cfg)
removeInstalledConfig :: FilePath -> IO ()
removeInstalledConfig distPref = do
try $ removeFile $ installedPkgConfigFile distPref
try $ removeFile $ inplacePkgConfigFile distPref
return ()
removeRegScripts :: IO ()
removeRegScripts = do
try $ removeFile regScriptLocation
try $ removeFile unregScriptLocation
return ()
installedPkgConfigFile :: FilePath -> FilePath
installedPkgConfigFile distPref = distPref </> "installed-pkg-config"
inplacePkgConfigFile :: FilePath -> FilePath
inplacePkgConfigFile distPref = distPref </> "inplace-pkg-config"
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo
mkInstalledPackageInfo
:: FilePath
-> PackageDescription
-> LocalBuildInfo
-> Bool
-> IO InstalledPackageInfo
mkInstalledPackageInfo distPref pkg_descr lbi inplace = do
--TODO: get rid of getCurrentDirectory here, make this function pure
pwd <- getCurrentDirectory
let
lib = fromJust (library pkg_descr) -- checked for Nothing earlier
clbi = fromJust (libraryConfig lbi)
--TODO: ^^ pass explicitly rather than using fromJust
bi = libBuildInfo lib
build_dir = pwd </> buildDir lbi
installDirs = absoluteInstallDirs (packageId pkg_descr) lbi NoCopyDest
inplaceDirs = (absoluteInstallDirs (packageId pkg_descr) lbi NoCopyDest) {
datadir = pwd,
datasubdir = distPref,
docdir = inplaceDocdir,
htmldir = inplaceHtmldir,
haddockdir = inplaceHtmldir
}
where inplaceDocdir = pwd </> distPref </> "doc"
inplaceHtmldir = inplaceDocdir </> "html"
</> display (packageName pkg_descr)
(absinc,relinc) = partition isAbsolute (includeDirs bi)
installIncludeDir | null (installIncludes bi) = []
| otherwise = [includedir installDirs]
haddockInterfaceDir
| inplace = haddockdir inplaceDirs
| otherwise = haddockdir installDirs
haddockHtmlDir
| inplace = htmldir inplaceDirs
| otherwise = htmldir installDirs
libraryDir
| inplace = build_dir
| otherwise = libdir installDirs
hasModules = not $ null (exposedModules lib)
&& null (otherModules bi)
hasLibrary = hasModules || not (null (cSources bi))
in
return InstalledPackageInfo {
IPI.package = packageId pkg_descr,
IPI.license = license pkg_descr,
IPI.copyright = copyright pkg_descr,
IPI.maintainer = maintainer pkg_descr,
IPI.author = author pkg_descr,
IPI.stability = stability pkg_descr,
IPI.homepage = homepage pkg_descr,
IPI.pkgUrl = pkgUrl pkg_descr,
IPI.description = description pkg_descr,
IPI.category = category pkg_descr,
IPI.exposed = libExposed lib,
IPI.exposedModules = exposedModules lib,
IPI.hiddenModules = otherModules bi,
IPI.importDirs = [ libraryDir | hasModules ],
IPI.libraryDirs = if hasLibrary
then libraryDir : extraLibDirs bi
else extraLibDirs bi,
IPI.hsLibraries = ["HS" ++ display (packageId pkg_descr)
| hasLibrary ],
IPI.extraLibraries = extraLibs bi,
IPI.extraGHCiLibraries= [],
IPI.includeDirs = absinc ++ if inplace
then map (pwd </>) relinc
else installIncludeDir,
IPI.includes = includes bi,
IPI.depends = componentPackageDeps clbi,
IPI.hugsOptions = concat [opts | (Hugs,opts) <- options bi],
IPI.ccOptions = [], -- NB. NOT ccOptions bi!
-- We don't want cc-options to be
-- propagated to C ompilations in other
-- packages.
IPI.ldOptions = ldOptions bi,
IPI.frameworkDirs = [],
IPI.frameworks = frameworks bi,
IPI.haddockInterfaces = [haddockInterfaceDir </> haddockName pkg_descr],
IPI.haddockHTMLs = [haddockHtmlDir]
}
-- | Construct 'InstalledPackageInfo' for a library in a package, given a set
-- of installation directories.
--
generalInstalledPackageInfo
:: ([FilePath] -> [FilePath]) -- ^ Translate relative include dir paths to
-- absolute paths.
-> PackageDescription
-> Library
-> ComponentLocalBuildInfo
-> InstallDirs FilePath
-> InstalledPackageInfo
generalInstalledPackageInfo adjustRelIncDirs pkg lib clbi installDirs =
InstalledPackageInfo {
IPI.package = packageId pkg,
IPI.license = license pkg,
IPI.copyright = copyright pkg,
IPI.maintainer = maintainer pkg,
IPI.author = author pkg,
IPI.stability = stability pkg,
IPI.homepage = homepage pkg,
IPI.pkgUrl = pkgUrl pkg,
IPI.description = description pkg,
IPI.category = category pkg,
IPI.exposed = libExposed lib,
IPI.exposedModules = exposedModules lib,
IPI.hiddenModules = otherModules bi,
IPI.importDirs = [ libdir installDirs | hasModules ],
IPI.libraryDirs = if hasLibrary
then libdir installDirs : extraLibDirs bi
else extraLibDirs bi,
IPI.hsLibraries = [ "HS" ++ display (packageId pkg) | hasLibrary ],
IPI.extraLibraries = extraLibs bi,
IPI.extraGHCiLibraries = [],
IPI.includeDirs = absinc ++ adjustRelIncDirs relinc,
IPI.includes = includes bi,
IPI.depends = componentPackageDeps clbi,
IPI.hugsOptions = hcOptions Hugs bi,
IPI.ccOptions = [], -- Note. NOT ccOptions bi!
-- We don't want cc-options to be propagated
-- to C compilations in other packages.
IPI.ldOptions = ldOptions bi,
IPI.frameworkDirs = [],
IPI.frameworks = frameworks bi,
IPI.haddockInterfaces = [haddockdir installDirs </> haddockName pkg],
IPI.haddockHTMLs = [htmldir installDirs]
}
where
bi = libBuildInfo lib
(absinc, relinc) = partition isAbsolute (includeDirs bi)
hasModules = not $ null (exposedModules lib)
&& null (otherModules bi)
hasLibrary = hasModules || not (null (cSources bi))
-- | Construct 'InstalledPackageInfo' for a library that is inplace in the
-- build tree.
--
-- This function knows about the layout of inplace packages.
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
-> FilePath -- ^ location of the dist tree
-> PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> InstalledPackageInfo
inplaceInstalledPackageInfo inplaceDir distPref pkg lib lbi clbi =
generalInstalledPackageInfo adjustReativeIncludeDirs pkg lib clbi installDirs
where
adjustReativeIncludeDirs = map (inplaceDir </>)
installDirs =
(absoluteInstallDirs (packageId pkg) lbi NoCopyDest) {
libdir = inplaceDir </> buildDir lbi,
datadir = inplaceDir,
datasubdir = distPref,
docdir = inplaceDocdir,
htmldir = inplaceHtmldir,
haddockdir = inplaceHtmldir
}
inplaceDocdir = inplaceDir </> distPref </> "doc"
inplaceHtmldir = inplaceDocdir </> "html" </> display (packageName pkg)
-- | Construct 'InstalledPackageInfo' for the final install location of a
-- library package.
--
-- This function knows about the layout of installed packages.
--
absoluteInstalledPackageInfo :: PackageDescription
-> Library
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> InstalledPackageInfo
absoluteInstalledPackageInfo pkg lib lbi clbi =
generalInstalledPackageInfo adjustReativeIncludeDirs pkg lib clbi installDirs
where
-- For installed packages we install all include files into one dir,
-- whereas in the build tree they may live in multiple local dirs.
adjustReativeIncludeDirs _
| null (installIncludes bi) = []
| otherwise = [includedir installDirs]
bi = libBuildInfo lib
installDirs = absoluteInstallDirs (packageId pkg) lbi NoCopyDest
-- -----------------------------------------------------------------------------
-- Unregistration
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
unregister pkg_descr lbi regFlags = do
let genScript = fromFlag (regGenScript regFlags)
unregister pkg lbi regFlags = do
let pkgid = packageId pkg
genScript = fromFlag (regGenScript regFlags)
verbosity = fromFlag (regVerbosity regFlags)
packageDB = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
(regPackageDB regFlags)
installDirs = absoluteInstallDirs (packageId pkg_descr) lbi NoCopyDest