Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add way to exclude generated modules from sdists #1046

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Cabal/Distribution/Simple/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ import Distribution.Simple.InstallDirs
( InstallDirs(..), CopyDest(..),
PathTemplate, toPathTemplate, fromPathTemplate )
import Distribution.Verbosity
import Distribution.ModuleName
( ModuleName )

import Data.List ( sort )
import Data.Char ( isSpace, isAlpha )
Expand Down Expand Up @@ -785,7 +787,8 @@ data SDistFlags = SDistFlags {
sDistSnapshot :: Flag Bool,
sDistDirectory :: Flag FilePath,
sDistDistPref :: Flag FilePath,
sDistVerbosity :: Flag Verbosity
sDistVerbosity :: Flag Verbosity,
sDistGeneratedModules :: [ModuleName]
}
deriving Show

Expand All @@ -794,7 +797,8 @@ defaultSDistFlags = SDistFlags {
sDistSnapshot = Flag False,
sDistDirectory = mempty,
sDistDistPref = Flag defaultDistPref,
sDistVerbosity = Flag normal
sDistVerbosity = Flag normal,
sDistGeneratedModules = mempty
}

sdistCommand :: CommandUI SDistFlags
Expand All @@ -818,6 +822,13 @@ sdistCommand = makeCommand name shortDesc longDesc defaultSDistFlags options
"Generate a source distribution in the given directory"
sDistDirectory (\v flags -> flags { sDistDirectory = v })
(reqArgFlag "DIR")

,option "" ["generated-module"]
"Name a generated module to exclude from the tarball"
sDistGeneratedModules (\v flags -> flags { sDistGeneratedModules = v })
(reqArg "MODULE"
(readP_to_E (const "module name expected") ((\x -> [x]) `fmap` parse))
(map (\x -> display x)))
]

emptySDistFlags :: SDistFlags
Expand All @@ -828,13 +839,15 @@ instance Monoid SDistFlags where
sDistSnapshot = mempty,
sDistDirectory = mempty,
sDistDistPref = mempty,
sDistVerbosity = mempty
sDistVerbosity = mempty,
sDistGeneratedModules = mempty
}
mappend a b = SDistFlags {
sDistSnapshot = combine sDistSnapshot,
sDistDirectory = combine sDistDirectory,
sDistDistPref = combine sDistDistPref,
sDistVerbosity = combine sDistVerbosity
sDistVerbosity = combine sDistVerbosity,
sDistGeneratedModules = combine sDistGeneratedModules
}
where combine field = field a `mappend` field b

Expand Down
58 changes: 49 additions & 9 deletions Cabal/Distribution/Simple/SrcDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ module Distribution.Simple.SrcDist (

-- ** Parts of 'sdist'
printPackageProblems,
prepareTreeWithoutGeneratedModules,
prepareTree,
createArchive,

-- ** Snaphots
prepareSnapshotTree,
prepareSnapshotTreeWithoutGeneratedModules,
snapshotPackage,
snapshotVersion,
dateToSnapshotNumber,
Expand Down Expand Up @@ -140,18 +142,18 @@ sdist pkg mb_lbi flags mkTmpDir pps = do
generateSourceDir targetDir pkg' = do

setupMessage verbosity "Building source dist for" (packageId pkg')
prepareTree verbosity pkg' mb_lbi distPref targetDir pps
prepareTreeWithoutGeneratedModules verbosity pkg' mb_lbi distPref targetDir pps generatedModules
when snapshot $
overwriteSnapshotPackageDesc verbosity pkg' targetDir

verbosity = fromFlag (sDistVerbosity flags)
snapshot = fromFlag (sDistSnapshot flags)
generatedModules = sDistGeneratedModules flags

distPref = fromFlag $ sDistDistPref flags
targetPref = distPref
tmpTargetDir = mkTmpDir distPref


-- |Prepare a directory tree of source files.
prepareTree :: Verbosity -- ^verbosity
-> PackageDescription -- ^info from the cabal file
Expand All @@ -160,7 +162,21 @@ prepareTree :: Verbosity -- ^verbosity
-> FilePath -- ^source tree to populate
-> [PPSuffixHandler] -- ^extra preprocessors (includes suffixes)
-> IO ()
prepareTree verbosity pkg_descr0 mb_lbi distPref targetDir pps = do
prepareTree verbosity pkg_descr mb_lbi distPref targetDir pps =
prepareTreeWithoutGeneratedModules verbosity pkg_descr mb_lbi distPref targetDir pps []

-- |Prepare a directory tree of source files, except for generated modules.
-- Note that this automatically ignores the Paths_<pkg>.hs module.
prepareTreeWithoutGeneratedModules
:: Verbosity -- ^verbosity
-> PackageDescription -- ^info from the cabal file
-> Maybe LocalBuildInfo
-> FilePath -- ^dist dir
-> FilePath -- ^source tree to populate
-> [PPSuffixHandler] -- ^extra preprocessors (includes suffixes)
-> [ModuleName] -- ^generated modules to ignore
-> IO ()
prepareTreeWithoutGeneratedModules verbosity pkg_descr0 mb_lbi distPref targetDir pps generatedModules0 = do
createDirectoryIfMissingVerbose verbosity True targetDir

-- maybe move the library files into place
Expand Down Expand Up @@ -262,11 +278,19 @@ prepareTree verbosity pkg_descr0 mb_lbi distPref targetDir pps = do
installOrdinaryFile verbosity descFile (targetDir </> descFile)

where
pkg_descr = mapAllBuildInfo filterAutogenModule pkg_descr0
filterAutogenModule bi = bi {
otherModules = filter (/=autogenModule) (otherModules bi)
pkg_descr1 = mapAllBuildInfo filterGeneratedOtherModules pkg_descr0
pkg_descr = pkg_descr1 {
library = fmap filterGeneratedExposedModules (library pkg_descr1)
}
filterGeneratedOtherModules bi = bi {
otherModules = filterGeneratedModules (otherModules bi)
}
filterGeneratedExposedModules libr = libr {
exposedModules = filterGeneratedModules (exposedModules libr)
}
autogenModule = autogenModuleName pkg_descr0
filterGeneratedModules = filter (flip notElem generatedModules)
-- Paths_<pkg>.hs, as well as the modules specified by the caller.
generatedModules = autogenModuleName pkg_descr0 : generatedModules0

findInc [] f = die ("can't find include file " ++ f)
findInc (d:ds) f = do
Expand All @@ -292,8 +316,24 @@ prepareSnapshotTree :: Verbosity -- ^verbosity
-> FilePath -- ^source tree to populate
-> [PPSuffixHandler] -- ^extra preprocessors (includes suffixes)
-> IO ()
prepareSnapshotTree verbosity pkg mb_lbi distPref targetDir pps = do
prepareTree verbosity pkg mb_lbi distPref targetDir pps
prepareSnapshotTree verbosity pkg mb_lbi distPref targetDir pps =
prepareSnapshotTreeWithoutGeneratedModules verbosity pkg mb_lbi distPref targetDir pps []

-- | Prepare a directory tree of source files for a snapshot version, without
-- the specified generated modules (as well as Paths_<pkg>.hs).
-- It is expected that the appropriate snapshot version has already been set
-- in the package description, eg using 'snapshotPackage' or 'snapshotVersion'.
prepareSnapshotTreeWithoutGeneratedModules
:: Verbosity -- ^verbosity
-> PackageDescription -- ^info from the cabal file
-> Maybe LocalBuildInfo
-> FilePath -- ^dist dir
-> FilePath -- ^source tree to populate
-> [PPSuffixHandler] -- ^extra preprocessors (includes suffixes)
-> [ModuleName] -- ^generated modules to ignore
-> IO ()
prepareSnapshotTreeWithoutGeneratedModules verbosity pkg mb_lbi distPref targetDir pps generatedModules = do
prepareTreeWithoutGeneratedModules verbosity pkg mb_lbi distPref targetDir pps generatedModules
overwriteSnapshotPackageDesc verbosity pkg targetDir

overwriteSnapshotPackageDesc :: Verbosity -- ^verbosity
Expand Down
11 changes: 11 additions & 0 deletions Cabal/doc/installing-packages.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,23 @@ the setup script, the sources of the modules named in the package
description file, and files named in the `license-file`, `main-is`,
`c-sources`, `data-files` and `extra-source-files` fields.

If the package includes generated Haskell source files, they can be
excluded from the distribution on the command line. `Paths_`_pkgname_
does not need to be so specified; Cabal automatically knows to exclude it.
All the necessary scripts and data to re-generate the modules should be listed
in the `extra-source-files` field of the `.cabal` file.

This command takes the following option:

`--snapshot`
: Append today's date (in "YYYYMMDD" format) to the version number for
the generated source package. The original package is unaffected.

`--generated-module=`_module_
: Specify that the named module is machine-generated and not to be included
it in the distribution. Note that this takes a module name, not a
path name (that is, `Distribution.GeneratedModule` rather than
`Distribution/GeneratedModule.hs`).

[dist-simple]: ../libraries/Cabal/Distribution-Simple.html
[dist-make]: ../libraries/Cabal/Distribution-Make.html
Expand Down