Skip to content

Commit

Permalink
Read basedir from cabal-file, and thread it through apropriately.
Browse files Browse the repository at this point in the history
  • Loading branch information
angerman committed Nov 12, 2017
1 parent 09dd45e commit 25e6776
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 72 deletions.
110 changes: 76 additions & 34 deletions Cabal/Distribution/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ import Distribution.Text

-- Base
import System.Environment (getArgs, getProgName)
import System.Directory (removeFile, doesFileExist
import System.Directory (removeFile, doesFileExist, getCurrentDirectory
,doesDirectoryExist, removeDirectoryRecursive)
import System.Exit (exitWith,ExitCode(..))
import System.FilePath (searchPathSeparator)
import System.FilePath (searchPathSeparator, takeDirectory, (</>))
import Distribution.Compat.Environment (getEnvironment)
import Distribution.Compat.GetShortPathName (getShortPathName)

Expand Down Expand Up @@ -248,9 +248,10 @@ buildAction :: UserHooks -> BuildFlags -> Args -> IO ()
buildAction hooks flags args = do
distPref <- findDistPrefOrDefault (buildDistPref flags)
let verbosity = fromFlag $ buildVerbosity flags
flags' = flags { buildDistPref = toFlag distPref }

lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { buildDistPref = toFlag distPref
, buildCabalFilePath = maybeToFlag (cabalFilePath lbi)}

progs <- reconfigurePrograms verbosity
(buildProgramPaths flags')
(buildProgramArgs flags')
Expand Down Expand Up @@ -288,7 +289,10 @@ hscolourAction :: UserHooks -> HscolourFlags -> Args -> IO ()
hscolourAction hooks flags args = do
distPref <- findDistPrefOrDefault (hscolourDistPref flags)
let verbosity = fromFlag $ hscolourVerbosity flags
flags' = flags { hscolourDistPref = toFlag distPref }
lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { hscolourDistPref = toFlag distPref
, hscolourCabalFilePath = maybeToFlag (cabalFilePath lbi)}

hookedAction preHscolour hscolourHook postHscolour
(getBuildConfig hooks verbosity distPref)
hooks flags' args
Expand All @@ -313,9 +317,10 @@ haddockAction :: UserHooks -> HaddockFlags -> Args -> IO ()
haddockAction hooks flags args = do
distPref <- findDistPrefOrDefault (haddockDistPref flags)
let verbosity = fromFlag $ haddockVerbosity flags
flags' = flags { haddockDistPref = toFlag distPref }

lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { haddockDistPref = toFlag distPref
, haddockCabalFilePath = maybeToFlag (cabalFilePath lbi)}

progs <- reconfigurePrograms verbosity
(haddockProgramPaths flags')
(haddockProgramArgs flags')
Expand All @@ -328,7 +333,10 @@ haddockAction hooks flags args = do
cleanAction :: UserHooks -> CleanFlags -> Args -> IO ()
cleanAction hooks flags args = do
distPref <- findDistPrefOrDefault (cleanDistPref flags)
let flags' = flags { cleanDistPref = toFlag distPref }

lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { cleanDistPref = toFlag distPref
, cleanCabalFilePath = maybeToFlag (cabalFilePath lbi)}

pbi <- preClean hooks args flags'

Expand All @@ -354,7 +362,9 @@ copyAction :: UserHooks -> CopyFlags -> Args -> IO ()
copyAction hooks flags args = do
distPref <- findDistPrefOrDefault (copyDistPref flags)
let verbosity = fromFlag $ copyVerbosity flags
flags' = flags { copyDistPref = toFlag distPref }
lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { copyDistPref = toFlag distPref
, copyCabalFilePath = maybeToFlag (cabalFilePath lbi)}
hookedAction preCopy copyHook postCopy
(getBuildConfig hooks verbosity distPref)
hooks flags' { copyArgs = args } args
Expand All @@ -363,7 +373,9 @@ installAction :: UserHooks -> InstallFlags -> Args -> IO ()
installAction hooks flags args = do
distPref <- findDistPrefOrDefault (installDistPref flags)
let verbosity = fromFlag $ installVerbosity flags
flags' = flags { installDistPref = toFlag distPref }
lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { installDistPref = toFlag distPref
, installCabalFilePath = maybeToFlag (cabalFilePath lbi)}
hookedAction preInst instHook postInst
(getBuildConfig hooks verbosity distPref)
hooks flags' args
Expand Down Expand Up @@ -427,7 +439,9 @@ registerAction :: UserHooks -> RegisterFlags -> Args -> IO ()
registerAction hooks flags args = do
distPref <- findDistPrefOrDefault (regDistPref flags)
let verbosity = fromFlag $ regVerbosity flags
flags' = flags { regDistPref = toFlag distPref }
lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { regDistPref = toFlag distPref
, regCabalFilePath = maybeToFlag (cabalFilePath lbi)}
hookedAction preReg regHook postReg
(getBuildConfig hooks verbosity distPref)
hooks flags' { regArgs = args } args
Expand All @@ -436,7 +450,9 @@ unregisterAction :: UserHooks -> RegisterFlags -> Args -> IO ()
unregisterAction hooks flags args = do
distPref <- findDistPrefOrDefault (regDistPref flags)
let verbosity = fromFlag $ regVerbosity flags
flags' = flags { regDistPref = toFlag distPref }
lbi <- getBuildConfig hooks verbosity distPref
let flags' = flags { regDistPref = toFlag distPref
, regCabalFilePath = maybeToFlag (cabalFilePath lbi)}
hookedAction preUnreg unregHook postUnreg
(getBuildConfig hooks verbosity distPref)
hooks flags' args
Expand Down Expand Up @@ -618,62 +634,83 @@ defaultUserHooks = autoconfUserHooks {
-- https://github.com/haskell/cabal/issues/158
where oldCompatPostConf args flags pkg_descr lbi
= do let verbosity = fromFlag (configVerbosity flags)
confExists <- doesFileExist "configure"
baseDir lbi' = fromMaybe "" (takeDirectory <$> cabalFilePath lbi')

confExists <- doesFileExist $ (baseDir lbi) </> "configure"
when confExists $
runConfigureScript verbosity
backwardsCompatHack flags lbi

pbi <- getHookedBuildInfo verbosity
base_dir <- getBaseDir (configCabalFilePath flags)

pbi <- getHookedBuildInfo base_dir verbosity
sanityCheckHookedBuildInfo pkg_descr pbi
let pkg_descr' = updatePackageDescription pbi pkg_descr
lbi' = lbi { localPkgDescr = pkg_descr' }
postConf simpleUserHooks args flags pkg_descr' lbi'

backwardsCompatHack = True

getBaseDir :: Flag FilePath -> IO FilePath
getBaseDir flag = do
-- compute the base directory. This is the current
-- working directory unless a different one was provided
-- via --cabal-file-path.
pwd <- getCurrentDirectory
return $ fromMaybe pwd (takeDirectory <$> flagToMaybe flag)

autoconfUserHooks :: UserHooks
autoconfUserHooks
= simpleUserHooks
{
postConf = defaultPostConf,
preBuild = readHookWithArgs buildVerbosity,
preCopy = readHookWithArgs copyVerbosity,
preClean = readHook cleanVerbosity,
preInst = readHook installVerbosity,
preHscolour = readHook hscolourVerbosity,
preHaddock = readHook haddockVerbosity,
preReg = readHook regVerbosity,
preUnreg = readHook regVerbosity
preBuild = readHookWithArgs buildVerbosity buildCabalFilePath,
preCopy = readHookWithArgs copyVerbosity copyCabalFilePath,
preClean = readHook cleanVerbosity cleanCabalFilePath,
preInst = readHook installVerbosity installCabalFilePath,
preHscolour = readHook hscolourVerbosity hscolourCabalFilePath,
preHaddock = readHook haddockVerbosity haddockCabalFilePath,
preReg = readHook regVerbosity regCabalFilePath,
preUnreg = readHook regVerbosity regCabalFilePath
}
where defaultPostConf :: Args -> ConfigFlags -> PackageDescription
-> LocalBuildInfo -> IO ()
defaultPostConf args flags pkg_descr lbi
= do let verbosity = fromFlag (configVerbosity flags)
confExists <- doesFileExist "configure"
baseDir lbi' = fromMaybe "" (takeDirectory <$> cabalFilePath lbi')
confExists <- doesFileExist $ (baseDir lbi) </> "configure"
if confExists
then runConfigureScript verbosity
backwardsCompatHack flags lbi
else die "configure script not found."

pbi <- getHookedBuildInfo verbosity
base_dir <- getBaseDir (configCabalFilePath flags)

pbi <- getHookedBuildInfo base_dir verbosity
sanityCheckHookedBuildInfo pkg_descr pbi
let pkg_descr' = updatePackageDescription pbi pkg_descr
lbi' = lbi { localPkgDescr = pkg_descr' }
postConf simpleUserHooks args flags pkg_descr' lbi'

backwardsCompatHack = False

readHookWithArgs :: (a -> Flag Verbosity) -> Args -> a
readHookWithArgs :: (a -> Flag Verbosity)
-> (a -> Flag FilePath)
-> Args -> a
-> IO HookedBuildInfo
readHookWithArgs get_verbosity _ flags = do
getHookedBuildInfo verbosity
readHookWithArgs get_verbosity get_cabal_file_path _ flags = do
base_dir <- getBaseDir (get_cabal_file_path flags)
getHookedBuildInfo base_dir verbosity
where
verbosity = fromFlag (get_verbosity flags)

readHook :: (a -> Flag Verbosity) -> Args -> a -> IO HookedBuildInfo
readHook get_verbosity a flags = do
readHook :: (a -> Flag Verbosity)
-> (a -> Flag FilePath)
-> Args -> a -> IO HookedBuildInfo
readHook get_verbosity get_cabal_file_path a flags = do
noExtraFlags a
getHookedBuildInfo verbosity
base_dir <- getBaseDir (get_cabal_file_path flags)
getHookedBuildInfo base_dir verbosity
where
verbosity = fromFlag (get_verbosity flags)

Expand Down Expand Up @@ -705,8 +742,9 @@ runConfigureScript verbosity backwardsCompatHack flags lbi = do
shConfiguredProg <- lookupProgram shProg
`fmap` configureProgram verbosity shProg progDb
case shConfiguredProg of
Just sh -> runProgramInvocation verbosity
Just sh -> runProgramInvocation verbosity $
(programInvocation (sh {programOverrideEnv = overEnv}) args')
{ progInvokeCwd = takeDirectory <$> cabalFilePath lbi }
Nothing -> die notFoundMsg

where
Expand All @@ -718,9 +756,13 @@ runConfigureScript verbosity backwardsCompatHack flags lbi = do
++ "If you are not on Windows, ensure that an 'sh' command "
++ "is discoverable in your path."

getHookedBuildInfo :: Verbosity -> IO HookedBuildInfo
getHookedBuildInfo verbosity = do
maybe_infoFile <- defaultHookedPackageDesc
getHookedBuildInfo :: FilePath -> Verbosity -> IO HookedBuildInfo
getHookedBuildInfo baseDir verbosity = do
-- TODO: We should probably better generate this in the
-- build dir, rather then in the base dir? However
-- `configure` is run in the baseDir.

maybe_infoFile <- findHookedPackageDesc baseDir
case maybe_infoFile of
Nothing -> return emptyHookedBuildInfo
Just infoFile -> do
Expand Down
12 changes: 9 additions & 3 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ import qualified Data.Map as Map
import System.Directory
( doesFileExist, createDirectoryIfMissing, getTemporaryDirectory )
import System.FilePath
( (</>), isAbsolute )
( (</>), isAbsolute, takeDirectory )
import qualified System.Info
( compilerName, compilerVersion )
import System.IO
Expand Down Expand Up @@ -690,6 +690,7 @@ configure (pkg_descr0, pbi) cfg = do
compiler = comp,
hostPlatform = compPlatform,
buildDir = buildDir,
cabalFilePath = flagToMaybe (configCabalFilePath cfg),
componentGraph = Graph.fromDistinctList buildComponents,
componentNameMap = buildComponentsMap,
installedPkgs = packageDependsIndex,
Expand Down Expand Up @@ -1633,14 +1634,19 @@ checkForeignDeps pkg lbi verbosity =

libExists lib = builds (makeProgram []) (makeLdArgs [lib])

baseDir lbi' = fromMaybe "" (takeDirectory <$> cabalFilePath lbi')

commonCppArgs = platformDefines lbi
-- TODO: This is a massive hack, to work around the
-- fact that the test performed here should be
-- PER-component (c.f. the "I'm Feeling Lucky"; we
-- should NOT be glomming everything together.)
++ [ "-I" ++ buildDir lbi </> "autogen" ]
++ [ "-I" ++ dir | dir <- collectField PD.includeDirs ]
++ ["-I."]
++ [ "-I" ++ (baseDir lbi) </> dir | dir <- collectField PD.includeDirs
, not (isAbsolute dir)]
++ [ "-I" ++ dir | dir <- collectField PD.includeDirs
, isAbsolute dir]
++ ["-I" ++ (baseDir lbi)]
++ collectField PD.cppOptions
++ collectField PD.ccOptions
++ [ "-I" ++ dir
Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Simple/GHC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ configureToolchain _implInfo ghcProg ghcInfo =
}
where
compilerDir = takeDirectory (programPath ghcProg)
baseDir = takeDirectory compilerDir
mingwBinDir = baseDir </> "mingw" </> "bin"
base_dir = takeDirectory compilerDir
mingwBinDir = base_dir </> "mingw" </> "bin"
isWindows = case buildOS of Windows -> True; _ -> False
binPrefix = ""

Expand Down
3 changes: 2 additions & 1 deletion Cabal/Distribution/Simple/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ haddockToHscolour flags =
hscolourBenchmarks = haddockBenchmarks flags,
hscolourForeignLibs = haddockForeignLibs flags,
hscolourVerbosity = haddockVerbosity flags,
hscolourDistPref = haddockDistPref flags
hscolourDistPref = haddockDistPref flags,
hscolourCabalFilePath = haddockCabalFilePath flags
}

-- ------------------------------------------------------------------------------
Expand Down
17 changes: 9 additions & 8 deletions Cabal/Distribution/Simple/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ copyComponent verbosity pkg_descr lbi (CLib lib) clbi copydest = do

-- install include files for all compilers - they may be needed to compile
-- haskell files (using the CPP extension)
installIncludeFiles verbosity lib buildPref incPref
installIncludeFiles verbosity lib lbi buildPref incPref

case compilerFlavor (compiler lbi) of
GHC -> GHC.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
Expand Down Expand Up @@ -247,20 +247,21 @@ installDataFiles verbosity pkg_descr destDataDir =

-- | Install the files listed in install-includes for a library
--
installIncludeFiles :: Verbosity -> Library -> FilePath -> FilePath -> IO ()
installIncludeFiles verbosity lib buildPref destIncludeDir = do
let relincdirs = "." : filter isRelative (includeDirs lbi)
lbi = libBuildInfo lib
incdirs = relincdirs ++ [ buildPref </> dir | dir <- relincdirs ]
incs <- traverse (findInc incdirs) (installIncludes lbi)
installIncludeFiles :: Verbosity -> Library -> LocalBuildInfo -> FilePath -> FilePath -> IO ()
installIncludeFiles verbosity lib lbi buildPref destIncludeDir = do
let relincdirs = "." : filter isRelative (includeDirs libBi)
libBi = libBuildInfo lib
incdirs = [ baseDir lbi </> dir | dir <- relincdirs ]
++ [ buildPref </> dir | dir <- relincdirs ]
incs <- traverse (findInc incdirs) (installIncludes libBi)
sequence_
[ do createDirectoryIfMissingVerbose verbosity True destDir
installOrdinaryFile verbosity srcFile destFile
| (relFile, srcFile) <- incs
, let destFile = destIncludeDir </> relFile
destDir = takeDirectory destFile ]
where

baseDir lbi' = fromMaybe "" (takeDirectory <$> cabalFilePath lbi')
findInc [] file = die' verbosity ("can't find include file " ++ file)
findInc (dir:dirs) file = do
let path = dir </> file
Expand Down
8 changes: 4 additions & 4 deletions Cabal/Distribution/Simple/LHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ configureToolchain :: ConfiguredProgram -> ProgramDb
-> ProgramDb
configureToolchain lhcProg =
addKnownProgram gccProgram {
programFindLocation = findProg gccProgram (baseDir </> "gcc.exe"),
programFindLocation = findProg gccProgram (base_dir </> "gcc.exe"),
programPostConf = configureGcc
}
. addKnownProgram ldProgram {
Expand All @@ -127,9 +127,9 @@ configureToolchain lhcProg =
}
where
compilerDir = takeDirectory (programPath lhcProg)
baseDir = takeDirectory compilerDir
gccLibDir = baseDir </> "gcc-lib"
includeDir = baseDir </> "include" </> "mingw"
base_dir = takeDirectory compilerDir
gccLibDir = base_dir </> "gcc-lib"
includeDir = base_dir </> "include" </> "mingw"
isWindows = case buildOS of Windows -> True; _ -> False

-- on Windows finding and configuring ghc's gcc and ld is a bit special
Expand Down
Loading

0 comments on commit 25e6776

Please sign in to comment.