@@ -100,14 +100,31 @@ import System.Environment (getArgs, getProgName)
100100import System.Directory (removeFile , doesFileExist
101101 ,doesDirectoryExist , removeDirectoryRecursive )
102102import System.Exit (exitWith ,ExitCode (.. ))
103- import System.FilePath (searchPathSeparator )
103+ import System.FilePath (searchPathSeparator , takeDirectory , (</>) )
104104import Distribution.Compat.Environment (getEnvironment )
105105import Distribution.Compat.GetShortPathName (getShortPathName )
106106
107107import Data.List (unionBy , (\\) )
108108
109109import Distribution.PackageDescription.Parsec
110110
111+ #if MIN_VERSION_directory(1,2,2)
112+ import System.Directory
113+ (makeAbsolute )
114+ #else
115+ import System.Directory
116+ (getCurrentDirectory )
117+ import System.FilePath
118+ (isAbsolute )
119+
120+ makeAbsolute :: FilePath -> IO FilePath
121+ makeAbsolute p | isAbsolute p = return p
122+ | otherwise = do
123+ cwd <- getCurrentDirectory
124+ return $ cwd </> p
125+ #endif
126+
127+
111128-- | A simple implementation of @main@ for a Cabal setup script.
112129-- It reads the package description file using IO, and performs the
113130-- action specified on the command line.
@@ -249,9 +266,10 @@ buildAction :: UserHooks -> BuildFlags -> Args -> IO ()
249266buildAction hooks flags args = do
250267 distPref <- findDistPrefOrDefault (buildDistPref flags)
251268 let verbosity = fromFlag $ buildVerbosity flags
252- flags' = flags { buildDistPref = toFlag distPref }
253-
254269 lbi <- getBuildConfig hooks verbosity distPref
270+ let flags' = flags { buildDistPref = toFlag distPref
271+ , buildCabalFilePath = maybeToFlag (cabalFilePath lbi)}
272+
255273 progs <- reconfigurePrograms verbosity
256274 (buildProgramPaths flags')
257275 (buildProgramArgs flags')
@@ -289,7 +307,10 @@ hscolourAction :: UserHooks -> HscolourFlags -> Args -> IO ()
289307hscolourAction hooks flags args = do
290308 distPref <- findDistPrefOrDefault (hscolourDistPref flags)
291309 let verbosity = fromFlag $ hscolourVerbosity flags
292- flags' = flags { hscolourDistPref = toFlag distPref }
310+ lbi <- getBuildConfig hooks verbosity distPref
311+ let flags' = flags { hscolourDistPref = toFlag distPref
312+ , hscolourCabalFilePath = maybeToFlag (cabalFilePath lbi)}
313+
293314 hookedAction preHscolour hscolourHook postHscolour
294315 (getBuildConfig hooks verbosity distPref)
295316 hooks flags' args
@@ -314,9 +335,10 @@ haddockAction :: UserHooks -> HaddockFlags -> Args -> IO ()
314335haddockAction hooks flags args = do
315336 distPref <- findDistPrefOrDefault (haddockDistPref flags)
316337 let verbosity = fromFlag $ haddockVerbosity flags
317- flags' = flags { haddockDistPref = toFlag distPref }
318-
319338 lbi <- getBuildConfig hooks verbosity distPref
339+ let flags' = flags { haddockDistPref = toFlag distPref
340+ , haddockCabalFilePath = maybeToFlag (cabalFilePath lbi)}
341+
320342 progs <- reconfigurePrograms verbosity
321343 (haddockProgramPaths flags')
322344 (haddockProgramArgs flags')
@@ -360,7 +382,9 @@ copyAction :: UserHooks -> CopyFlags -> Args -> IO ()
360382copyAction hooks flags args = do
361383 distPref <- findDistPrefOrDefault (copyDistPref flags)
362384 let verbosity = fromFlag $ copyVerbosity flags
363- flags' = flags { copyDistPref = toFlag distPref }
385+ lbi <- getBuildConfig hooks verbosity distPref
386+ let flags' = flags { copyDistPref = toFlag distPref
387+ , copyCabalFilePath = maybeToFlag (cabalFilePath lbi)}
364388 hookedAction preCopy copyHook postCopy
365389 (getBuildConfig hooks verbosity distPref)
366390 hooks flags' { copyArgs = args } args
@@ -369,7 +393,9 @@ installAction :: UserHooks -> InstallFlags -> Args -> IO ()
369393installAction hooks flags args = do
370394 distPref <- findDistPrefOrDefault (installDistPref flags)
371395 let verbosity = fromFlag $ installVerbosity flags
372- flags' = flags { installDistPref = toFlag distPref }
396+ lbi <- getBuildConfig hooks verbosity distPref
397+ let flags' = flags { installDistPref = toFlag distPref
398+ , installCabalFilePath = maybeToFlag (cabalFilePath lbi)}
373399 hookedAction preInst instHook postInst
374400 (getBuildConfig hooks verbosity distPref)
375401 hooks flags' args
@@ -433,7 +459,9 @@ registerAction :: UserHooks -> RegisterFlags -> Args -> IO ()
433459registerAction hooks flags args = do
434460 distPref <- findDistPrefOrDefault (regDistPref flags)
435461 let verbosity = fromFlag $ regVerbosity flags
436- flags' = flags { regDistPref = toFlag distPref }
462+ lbi <- getBuildConfig hooks verbosity distPref
463+ let flags' = flags { regDistPref = toFlag distPref
464+ , regCabalFilePath = maybeToFlag (cabalFilePath lbi)}
437465 hookedAction preReg regHook postReg
438466 (getBuildConfig hooks verbosity distPref)
439467 hooks flags' { regArgs = args } args
@@ -442,7 +470,9 @@ unregisterAction :: UserHooks -> RegisterFlags -> Args -> IO ()
442470unregisterAction hooks flags args = do
443471 distPref <- findDistPrefOrDefault (regDistPref flags)
444472 let verbosity = fromFlag $ regVerbosity flags
445- flags' = flags { regDistPref = toFlag distPref }
473+ lbi <- getBuildConfig hooks verbosity distPref
474+ let flags' = flags { regDistPref = toFlag distPref
475+ , regCabalFilePath = maybeToFlag (cabalFilePath lbi)}
446476 hookedAction preUnreg unregHook postUnreg
447477 (getBuildConfig hooks verbosity distPref)
448478 hooks flags' args
@@ -630,12 +660,14 @@ defaultUserHooks = autoconfUserHooks {
630660 -- https://github.com/haskell/cabal/issues/158
631661 where oldCompatPostConf args flags pkg_descr lbi
632662 = do let verbosity = fromFlag (configVerbosity flags)
633- confExists <- doesFileExist " configure"
663+ baseDir lbi' = fromMaybe " " (takeDirectory <$> cabalFilePath lbi')
664+
665+ confExists <- doesFileExist $ (baseDir lbi) </> " configure"
634666 when confExists $
635667 runConfigureScript verbosity
636668 backwardsCompatHack flags lbi
637669
638- pbi <- getHookedBuildInfo verbosity
670+ pbi <- getHookedBuildInfo (buildDir lbi) verbosity
639671 sanityCheckHookedBuildInfo pkg_descr pbi
640672 let pkg_descr' = updatePackageDescription pbi pkg_descr
641673 lbi' = lbi { localPkgDescr = pkg_descr' }
@@ -648,44 +680,51 @@ autoconfUserHooks
648680 = simpleUserHooks
649681 {
650682 postConf = defaultPostConf,
651- preBuild = readHookWithArgs buildVerbosity,
652- preCopy = readHookWithArgs copyVerbosity,
653- preClean = readHook cleanVerbosity,
654- preInst = readHook installVerbosity,
655- preHscolour = readHook hscolourVerbosity,
656- preHaddock = readHook haddockVerbosity,
657- preReg = readHook regVerbosity,
658- preUnreg = readHook regVerbosity
683+ preBuild = readHookWithArgs buildVerbosity buildDistPref, -- buildCabalFilePath ,
684+ preCopy = readHookWithArgs copyVerbosity copyDistPref ,
685+ preClean = readHook cleanVerbosity cleanDistPref ,
686+ preInst = readHook installVerbosity installDistPref ,
687+ preHscolour = readHook hscolourVerbosity hscolourDistPref ,
688+ preHaddock = readHook haddockVerbosity haddockDistPref ,
689+ preReg = readHook regVerbosity regDistPref ,
690+ preUnreg = readHook regVerbosity regDistPref
659691 }
660692 where defaultPostConf :: Args -> ConfigFlags -> PackageDescription
661693 -> LocalBuildInfo -> IO ()
662694 defaultPostConf args flags pkg_descr lbi
663695 = do let verbosity = fromFlag (configVerbosity flags)
664- confExists <- doesFileExist " configure"
696+ baseDir lbi' = fromMaybe " " (takeDirectory <$> cabalFilePath lbi')
697+ confExists <- doesFileExist $ (baseDir lbi) </> " configure"
665698 if confExists
666699 then runConfigureScript verbosity
667700 backwardsCompatHack flags lbi
668701 else die " configure script not found."
669702
670- pbi <- getHookedBuildInfo verbosity
703+ pbi <- getHookedBuildInfo (buildDir lbi) verbosity
671704 sanityCheckHookedBuildInfo pkg_descr pbi
672705 let pkg_descr' = updatePackageDescription pbi pkg_descr
673706 lbi' = lbi { localPkgDescr = pkg_descr' }
674707 postConf simpleUserHooks args flags pkg_descr' lbi'
675708
676709 backwardsCompatHack = False
677710
678- readHookWithArgs :: (a -> Flag Verbosity ) -> Args -> a
711+ readHookWithArgs :: (a -> Flag Verbosity )
712+ -> (a -> Flag FilePath )
713+ -> Args -> a
679714 -> IO HookedBuildInfo
680- readHookWithArgs get_verbosity _ flags = do
681- getHookedBuildInfo verbosity
715+ readHookWithArgs get_verbosity get_dist_pref _ flags = do
716+ dist_dir <- findDistPrefOrDefault (get_dist_pref flags)
717+ getHookedBuildInfo (dist_dir </> " build" ) verbosity
682718 where
683719 verbosity = fromFlag (get_verbosity flags)
684720
685- readHook :: (a -> Flag Verbosity ) -> Args -> a -> IO HookedBuildInfo
686- readHook get_verbosity a flags = do
721+ readHook :: (a -> Flag Verbosity )
722+ -> (a -> Flag FilePath )
723+ -> Args -> a -> IO HookedBuildInfo
724+ readHook get_verbosity get_dist_pref a flags = do
687725 noExtraFlags a
688- getHookedBuildInfo verbosity
726+ dist_dir <- findDistPrefOrDefault (get_dist_pref flags)
727+ getHookedBuildInfo (dist_dir </> " build" ) verbosity
689728 where
690729 verbosity = fromFlag (get_verbosity flags)
691730
@@ -702,6 +741,8 @@ runConfigureScript verbosity backwardsCompatHack flags lbi = do
702741 -- to ccFlags
703742 -- We don't try and tell configure which ld to use, as we don't have
704743 -- a way to pass its flags too
744+ configureFile <- makeAbsolute $
745+ fromMaybe " ." (takeDirectory <$> cabalFilePath lbi) </> " configure"
705746 let extraPath = fromNubList $ configProgramPathExtra flags
706747 let cflagsEnv = maybe (unwords ccFlags) (++ (" " ++ unwords ccFlags))
707748 $ lookup " CFLAGS" env
@@ -710,29 +751,30 @@ runConfigureScript verbosity backwardsCompatHack flags lbi = do
710751 ((intercalate spSep extraPath ++ spSep)++ ) $ lookup " PATH" env
711752 overEnv = (" CFLAGS" , Just cflagsEnv) :
712753 [(" PATH" , Just pathEnv) | not (null extraPath)]
713- args' = args ++ [" CC=" ++ ccProgShort]
754+ args' = configureFile : args ++ [" CC=" ++ ccProgShort]
714755 shProg = simpleProgram " sh"
715756 progDb = modifyProgramSearchPath
716757 (\ p -> map ProgramSearchPathDir extraPath ++ p) emptyProgramDb
717758 shConfiguredProg <- lookupProgram shProg
718759 `fmap` configureProgram verbosity shProg progDb
719760 case shConfiguredProg of
720- Just sh -> runProgramInvocation verbosity
761+ Just sh -> runProgramInvocation verbosity $
721762 (programInvocation (sh {programOverrideEnv = overEnv}) args')
763+ { progInvokeCwd = Just (buildDir lbi) }
722764 Nothing -> die notFoundMsg
723765
724766 where
725- args = " ./configure " : configureArgs backwardsCompatHack flags
767+ args = configureArgs backwardsCompatHack flags
726768
727769 notFoundMsg = " The package has a './configure' script. "
728770 ++ " If you are on Windows, This requires a "
729771 ++ " Unix compatibility toolchain such as MinGW+MSYS or Cygwin. "
730772 ++ " If you are not on Windows, ensure that an 'sh' command "
731773 ++ " is discoverable in your path."
732774
733- getHookedBuildInfo :: Verbosity -> IO HookedBuildInfo
734- getHookedBuildInfo verbosity = do
735- maybe_infoFile <- defaultHookedPackageDesc
775+ getHookedBuildInfo :: FilePath -> Verbosity -> IO HookedBuildInfo
776+ getHookedBuildInfo build_dir verbosity = do
777+ maybe_infoFile <- findHookedPackageDesc build_dir
736778 case maybe_infoFile of
737779 Nothing -> return emptyHookedBuildInfo
738780 Just infoFile -> do
0 commit comments