Commit 93d8ddb2 authored by Francesco Gazzetta's avatar Francesco Gazzetta Committed by GitHub
Browse files

Merge pull request #4641 from fgaz/new-run/datafiles-2

Use package root as data-file base path, not cwd
parents 8356832e 3d0f9be3
......@@ -37,7 +37,7 @@ import Distribution.Verbosity
import Distribution.Simple.Utils
( wrapText, die', ordNub, info )
import Distribution.Client.ProjectPlanning
( ElaboratedConfiguredPackage(..)
( ElaboratedConfiguredPackage(..), BuildStyle(..)
, ElaboratedInstallPlan, binDirectoryFor )
import Distribution.Client.InstallPlan
( toList, foldPlanPackage )
......@@ -52,13 +52,13 @@ import Distribution.Simple.Build.PathsModule
( pkgPathEnvVar )
import Distribution.Types.UnitId
( UnitId )
import Distribution.Client.Types
( PackageLocation(..) )
import qualified Data.Map as Map
import qualified Data.Set as Set
import System.FilePath
( (</>) )
import System.Directory
( getCurrentDirectory )
runCommand :: CommandUI (ConfigFlags, ConfigExFlags, InstallFlags, HaddockFlags)
......@@ -208,16 +208,13 @@ runAction (applyFlagDefaults -> (configFlags, configExFlags, installFlags, haddo
pkg
exeName
</> exeName
curDir <- getCurrentDirectory
let dataDirEnvVar = (pkgPathEnvVar (elabPkgDescription pkg) "datadir",
Just $ curDir </> dataDir (elabPkgDescription pkg))
args = drop 1 targetStrings
let args = drop 1 targetStrings
runProgramInvocation
verbosity
emptyProgramInvocation {
progInvokePath = exePath,
progInvokeArgs = args,
progInvokeEnv = [dataDirEnvVar]
progInvokeEnv = dataDirsEnvironmentForPlan elaboratedPlan
}
where
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
......@@ -225,6 +222,39 @@ runAction (applyFlagDefaults -> (configFlags, configExFlags, installFlags, haddo
globalFlags configFlags configExFlags
installFlags haddockFlags
-- | Construct the environment needed for the data files to work.
-- This consists of a separate @*_datadir@ variable for each
-- inplace package in the plan.
dataDirsEnvironmentForPlan :: ElaboratedInstallPlan
-> [(String, Maybe FilePath)]
dataDirsEnvironmentForPlan = catMaybes
. fmap (foldPlanPackage
(const Nothing)
dataDirEnvVarForPackage)
. toList
-- | Construct an environment variable that points
-- the package's datadir to its correct location.
-- This might be:
-- * 'Just' the package's source directory plus the data subdirectory
-- for inplace packages.
-- * 'Nothing' for packages installed in the store (the path was
-- already included in the package at install/build time).
-- * The other cases are not handled yet. See below.
dataDirEnvVarForPackage :: ElaboratedConfiguredPackage
-> Maybe (String, Maybe FilePath)
dataDirEnvVarForPackage pkg =
case (elabBuildStyle pkg, elabPkgSourceLocation pkg)
of (BuildAndInstall, _) -> Nothing
(BuildInplaceOnly, LocalUnpackedPackage path) -> Just
(pkgPathEnvVar (elabPkgDescription pkg) "datadir",
Just $ path </> dataDir (elabPkgDescription pkg))
-- TODO: handle the other cases for PackageLocation.
-- We will only need this when we add support for
-- remote/local tarballs.
(BuildInplaceOnly, _) -> Nothing
singleExeOrElse :: IO (UnitId, UnqualComponentName) -> TargetsMap -> IO (UnitId, UnqualComponentName)
singleExeOrElse action targetsMap =
case Set.toList . distinctTargetComponents $ targetsMap
......
import LibFoo
main = putStrLn =<< LibFoo.getData
name: bar
version: 1.0
build-type: Simple
cabal-version: >= 1.10
executable bar
main-is: Main.hs
build-depends: base, foo
default-language: Haskell2010
......@@ -2,7 +2,18 @@
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- Datafiles-1.0 (exe:foo) (first run)
Configuring executable 'foo' for Datafiles-1.0..
Preprocessing executable 'foo' for Datafiles-1.0..
Building executable 'foo' for Datafiles-1.0..
- foo-1.0 (exe:foo) (first run)
Configuring executable 'foo' for foo-1.0..
Preprocessing executable 'foo' for foo-1.0..
Building executable 'foo' for foo-1.0..
# cabal new-run
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- foo-1.0 (lib) (first run)
- bar-1.0 (exe:bar) (first run)
Configuring library for foo-1.0..
Preprocessing library for foo-1.0..
Building library for foo-1.0..
Configuring executable 'bar' for bar-1.0..
Preprocessing executable 'bar' for bar-1.0..
Building executable 'bar' for bar-1.0..
import Test.Cabal.Prelude
main = cabalTest $
main = cabalTest $ do
cabal' "new-run" ["foo"] >>= assertOutputContains "Hello World"
cabal' "new-run" ["bar"] >>= assertOutputContains "Hello World"
module LibFoo where
import Paths_foo
getData = readFile =<< getDataFileName "hello.txt"
name: Datafiles
name: foo
version: 1.0
build-type: Simple
cabal-version: >= 1.10
......@@ -9,3 +9,11 @@ executable foo
main-is: Main.hs
build-depends: base
default-language: Haskell2010
library
exposed-modules: LibFoo
other-modules: Paths_foo
build-depends: base
default-language: Haskell2010
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment