Skip to content

Commit 9ea8e9f

Browse files
committed
Add new autogen-modules field to BuildInfo
1 parent eb5b9b7 commit 9ea8e9f

File tree

12 files changed

+128
-10
lines changed

12 files changed

+128
-10
lines changed

Cabal/Distribution/PackageDescription.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module Distribution.PackageDescription (
2828
defaultRenaming,
2929
lookupRenaming,
3030

31+
-- ** Module names
32+
allModuleNames,
33+
allModuleNamesAutogen,
34+
3135
-- ** Libraries
3236
Library(..),
3337
ModuleReexport(..),
@@ -36,13 +40,15 @@ module Distribution.PackageDescription (
3640
hasPublicLib,
3741
hasLibs,
3842
libModules,
43+
libModulesAutogen,
3944

4045
-- ** Executables
4146
Executable(..),
4247
emptyExecutable,
4348
withExe,
4449
hasExes,
4550
exeModules,
51+
exeModulesAutogen,
4652

4753
-- * Tests
4854
TestSuite(..),
@@ -54,6 +60,7 @@ module Distribution.PackageDescription (
5460
hasTests,
5561
withTest,
5662
testModules,
63+
testModulesAutogen,
5764

5865
-- * Benchmarks
5966
Benchmark(..),
@@ -65,6 +72,7 @@ module Distribution.PackageDescription (
6572
hasBenchmarks,
6673
withBenchmark,
6774
benchmarkModules,
75+
benchmarkModulesAutogen,
6876

6977
-- * Build information
7078
BuildInfo(..),

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Distribution.PackageDescription.Configuration
4141
import Distribution.Compiler
4242
import Distribution.System
4343
import Distribution.License
44+
import Distribution.Simple.BuildPaths (autogenModuleName)
4445
import Distribution.Simple.CCompiler
4546
import Distribution.Simple.Utils hiding (findPackageDesc, notice)
4647
import Distribution.Version
@@ -245,6 +246,12 @@ checkLibrary pkg lib =
245246
PackageDistInexcusable $
246247
"To use the 'required-signatures' field the package needs to specify "
247248
++ "at least 'cabal-version: >= 1.21'."
249+
250+
-- check that all autogen-modules appear on other-modules or exposed-modules
251+
, checkVersion [1,25] (not $ and $ map (flip elem (libModules lib)) (libModulesAutogen lib)) $
252+
PackageBuildWarning $
253+
"An 'autogen-module' is neither on 'exposed-modules' or 'other-modules'."
254+
248255
]
249256

250257
where
@@ -282,6 +289,14 @@ checkExecutable pkg exe =
282289
PackageBuildImpossible $
283290
"Duplicate modules in executable '" ++ exeName exe ++ "': "
284291
++ commaSep (map display moduleDuplicates)
292+
293+
-- check that all autogen-modules appear on other-modules
294+
, checkSpecVersion pkg [1,25]
295+
(not $ and $ map (flip elem (exeModules exe)) (exeModulesAutogen exe)) $
296+
PackageBuildWarning $
297+
"On executable '" ++ exeName exe ++ "' an 'autogen-module' is "
298+
++ "not on 'other-modules'"
299+
285300
]
286301
where
287302
moduleDuplicates = dups (exeModules exe)
@@ -319,6 +334,13 @@ checkTestSuite pkg test =
319334
PackageDistInexcusable $
320335
"The package uses a C/C++/obj-C source file for the 'main-is' field. "
321336
++ "To use this feature you must specify 'cabal-version: >= 1.18'."
337+
338+
-- check that all autogen-modules appear on other-modules
339+
, checkSpecVersion pkg [1,25]
340+
(not $ and $ map (flip elem (testModules test)) (testModulesAutogen test)) $
341+
PackageBuildWarning $
342+
"On test suite '" ++ testName test ++ "' an 'autogen-module' is "
343+
++ "not on 'other-modules'"
322344
]
323345
where
324346
moduleDuplicates = dups $ testModules test
@@ -332,7 +354,7 @@ checkTestSuite pkg test =
332354
_ -> False
333355

334356
checkBenchmark :: PackageDescription -> Benchmark -> [PackageCheck]
335-
checkBenchmark _pkg bm =
357+
checkBenchmark pkg bm =
336358
catMaybes [
337359

338360
case benchmarkInterface bm of
@@ -358,6 +380,13 @@ checkBenchmark _pkg bm =
358380
PackageBuildImpossible $
359381
"The 'main-is' field must specify a '.hs' or '.lhs' file "
360382
++ "(even if it is generated by a preprocessor)."
383+
384+
-- check that all autogen-modules appear on other-modules
385+
, checkSpecVersion pkg [1,25]
386+
(not $ and $ map (flip elem (benchmarkModules bm)) (benchmarkModulesAutogen bm)) $
387+
PackageBuildWarning $
388+
"On benchmark '" ++ benchmarkName bm ++ "' an 'autogen-module' is "
389+
++ "not on 'other-modules'"
361390
]
362391
where
363392
moduleDuplicates = dups $ benchmarkModules bm
@@ -1110,6 +1139,29 @@ checkCabalVersion pkg =
11101139
++ "that specifies the dependencies of the Setup.hs script itself. "
11111140
++ "The 'setup-depends' field uses the same syntax as 'build-depends', "
11121141
++ "so a simple example would be 'setup-depends: base, Cabal'."
1142+
1143+
, check (specVersion pkg >= Version [1,25] []
1144+
&& elem (autogenModuleName pkg) (allModuleNames pkg)
1145+
&& not (elem (autogenModuleName pkg) (allModuleNamesAutogen pkg)) ) $
1146+
PackageBuildWarning $
1147+
"Packages using 'cabal-version: >= 1.25' and the autogenerated "
1148+
++ "module Paths_* must include it also on the 'autoge-modules' field "
1149+
++ "besides 'exposed-modules' and 'other-modules'. This specifies that "
1150+
++ "the module does not come with the package and is generated on "
1151+
++ "setup. Modules built with a custom Setup.hs script also go here "
1152+
++ "to ensure that commands like sdist don't fail."
1153+
1154+
, check (specVersion pkg < Version [1,25] []
1155+
&& elem (autogenModuleName pkg) (allModuleNames pkg)
1156+
&& not (elem (autogenModuleName pkg) (allModuleNamesAutogen pkg)) ) $
1157+
PackageDistSuspiciousWarn $
1158+
"From version 1.25 autogenerated modules are included on the "
1159+
++ "'autoge-modules' field besides 'exposed-modules' and "
1160+
++ "'other-modules'. Consider using cabal-version >= 1.25 and putting "
1161+
++ "the module Paths_* on this new field. Modules built with a custom "
1162+
++ "Setup.hs script also go here to ensure that commands like sdist "
1163+
++ "don't fail."
1164+
11131165
]
11141166
where
11151167
-- Perform a check on packages that use a version of the spec less than

Cabal/Distribution/PackageDescription/Parse.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ binfoFieldDescrs =
443443
, listFieldWithSep vcat "other-modules"
444444
disp parseModuleNameQ
445445
otherModules (\val binfo -> binfo{otherModules=val})
446+
, listFieldWithSep vcat "autogen-modules"
447+
disp parseModuleNameQ
448+
autogenModules (\val binfo -> binfo{autogenModules=val})
446449
, optsField "ghc-prof-options" GHC
447450
profOptions (\val binfo -> binfo{profOptions=val})
448451
, optsField "ghcjs-prof-options" GHCJS

Cabal/Distribution/ParseUtils.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ ppField name fielddoc
288288
, "includes"
289289
, "install-includes"
290290
, "other-modules"
291+
, "autogen-modules"
291292
, "depends"
292293
]
293294

Cabal/Distribution/Simple/SrcDist.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ listPackageSources verbosity pkg_descr0 pps = do
136136
maybeExecutable <- listPackageSourcesMaybeExecutable pkg_descr
137137
return (ordinary, maybeExecutable)
138138
where
139-
pkg_descr = filterAutogenModule pkg_descr0
139+
pkg_descr = filterAutogenModules pkg_descr0
140140

141141
-- | List those source files that may be executable (e.g. the configure script).
142142
listPackageSourcesMaybeExecutable :: PackageDescription -> IO [FilePath]
@@ -259,7 +259,7 @@ prepareTree verbosity pkg_descr0 mb_lbi targetDir pps = do
259259
maybeCreateDefaultSetupScript targetDir
260260

261261
where
262-
pkg_descr = filterAutogenModule pkg_descr0
262+
pkg_descr = filterAutogenModules pkg_descr0
263263

264264
-- | Find the setup script file, if it exists.
265265
findSetupFile :: FilePath -> IO (Maybe FilePath)
@@ -305,21 +305,24 @@ findIncludeFile (d:ds) f = do
305305
b <- doesFileExist path
306306
if b then return (f,path) else findIncludeFile ds f
307307

308-
-- | Remove the auto-generated module ('Paths_*') from 'exposed-modules' and
309-
-- 'other-modules'.
310-
filterAutogenModule :: PackageDescription -> PackageDescription
311-
filterAutogenModule pkg_descr0 = mapLib filterAutogenModuleLib $
308+
-- | Remove the auto-generated modules (like 'Paths_*') from 'exposed-modules'
309+
-- and 'other-modules'.
310+
filterAutogenModules :: PackageDescription -> PackageDescription
311+
filterAutogenModules pkg_descr0 = mapLib filterAutogenModuleLib $
312312
mapAllBuildInfo filterAutogenModuleBI pkg_descr0
313313
where
314314
mapLib f pkg = pkg { library = fmap f (library pkg)
315315
, subLibraries = map f (subLibraries pkg) }
316316
filterAutogenModuleLib lib = lib {
317-
exposedModules = filter (/=autogenModule) (exposedModules lib)
317+
exposedModules = filter (filterFunction (libBuildInfo lib)) (exposedModules lib)
318318
}
319319
filterAutogenModuleBI bi = bi {
320-
otherModules = filter (/=autogenModule) (otherModules bi)
320+
otherModules = filter (filterFunction bi) (otherModules bi)
321321
}
322-
autogenModule = autogenModuleName pkg_descr0
322+
pathsModule = autogenModuleName pkg_descr0
323+
filterFunction bi = \mn ->
324+
mn /= pathsModule
325+
&& not (elem mn (autogenModules bi))
323326

324327
-- | Prepare a directory tree of source files for a snapshot version.
325328
-- It is expected that the appropriate snapshot version has already been set

Cabal/Distribution/Types/Benchmark.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Distribution.Types.Benchmark (
66
emptyBenchmark,
77
benchmarkType,
88
benchmarkModules,
9+
benchmarkModulesAutogen
910
) where
1011

1112
import Prelude ()
@@ -60,3 +61,8 @@ benchmarkType benchmark = case benchmarkInterface benchmark of
6061
-- | Get all the module names from a benchmark.
6162
benchmarkModules :: Benchmark -> [ModuleName]
6263
benchmarkModules benchmark = otherModules (benchmarkBuildInfo benchmark)
64+
65+
-- | Get all the auto generated module names from a benchmark.
66+
-- This are a subset of 'benchmarkModules'.
67+
benchmarkModulesAutogen :: Benchmark -> [ModuleName]
68+
benchmarkModulesAutogen benchmark = autogenModules (benchmarkBuildInfo benchmark)

Cabal/Distribution/Types/BuildInfo.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ data BuildInfo = BuildInfo {
4040
jsSources :: [FilePath],
4141
hsSourceDirs :: [FilePath], -- ^ where to look for the Haskell module hierarchy
4242
otherModules :: [ModuleName], -- ^ non-exposed or non-main modules
43+
autogenModules :: [ModuleName], -- ^ not present on sdist, Paths_* or user-generated with a custom Setup.hs
4344

4445
defaultLanguage :: Maybe Language,-- ^ language used when not explicitly specified
4546
otherLanguages :: [Language], -- ^ other languages used within the package
@@ -80,6 +81,7 @@ instance Monoid BuildInfo where
8081
jsSources = [],
8182
hsSourceDirs = [],
8283
otherModules = [],
84+
autogenModules = [],
8385
defaultLanguage = Nothing,
8486
otherLanguages = [],
8587
defaultExtensions = [],
@@ -114,6 +116,7 @@ instance Semigroup BuildInfo where
114116
jsSources = combineNub jsSources,
115117
hsSourceDirs = combineNub hsSourceDirs,
116118
otherModules = combineNub otherModules,
119+
autogenModules = combineNub autogenModules,
117120
defaultLanguage = combineMby defaultLanguage,
118121
otherLanguages = combineNub otherLanguages,
119122
defaultExtensions = combineNub defaultExtensions,

Cabal/Distribution/Types/Executable.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Distribution.Types.Executable (
55
Executable(..),
66
emptyExecutable,
77
exeModules,
8+
exeModulesAutogen
89
) where
910

1011
import Prelude ()
@@ -46,3 +47,8 @@ emptyExecutable = mempty
4647
-- | Get all the module names from an exe
4748
exeModules :: Executable -> [ModuleName]
4849
exeModules exe = otherModules (buildInfo exe)
50+
51+
-- | Get all the auto generated module names from an exe
52+
-- This are a subset of 'exeModules'.
53+
exeModulesAutogen :: Executable -> [ModuleName]
54+
exeModulesAutogen exe = autogenModules (buildInfo exe)

Cabal/Distribution/Types/Library.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Distribution.Types.Library (
55
Library(..),
66
emptyLibrary,
77
libModules,
8+
libModulesAutogen
89
) where
910

1011
import Prelude ()
@@ -58,3 +59,8 @@ libModules :: Library -> [ModuleName]
5859
libModules lib = exposedModules lib
5960
++ otherModules (libBuildInfo lib)
6061
++ requiredSignatures lib
62+
63+
-- | Get all the auto generated module names from the library, exposed or not.
64+
-- This are a subset of 'libModules'.
65+
libModulesAutogen :: Library -> [ModuleName]
66+
libModulesAutogen lib = autogenModules (libBuildInfo lib)

Cabal/Distribution/Types/PackageDescription.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ module Distribution.Types.PackageDescription (
4040
hasBenchmarks,
4141
withBenchmark,
4242
allBuildInfo,
43+
allModuleNames,
44+
allModuleNamesAutogen,
4345
updatePackageDescription,
4446
pkgComponents,
4547
pkgBuildableComponents,
@@ -65,6 +67,7 @@ import Distribution.Types.HookedBuildInfo
6567

6668
import Distribution.Package
6769
import Distribution.Version
70+
import Distribution.ModuleName
6871
import Distribution.License
6972
import Distribution.Compiler
7073

@@ -296,6 +299,18 @@ allBuildInfo pkg_descr = [ bi | lib <- allLibraries pkg_descr
296299
--FIXME: many of the places where this is used, we actually want to look at
297300
-- unbuildable bits too, probably need separate functions
298301

302+
-- | As they appear on the cabal file, Paths_* module has no special treatment.
303+
-- It is not exluded if it already appears here.
304+
allModuleNames :: PackageDescription -> [ModuleName]
305+
allModuleNames pkg_descr = (case library pkg_descr of
306+
Nothing -> []
307+
(Just lib) -> libModules lib)
308+
++ concatMap otherModules (allBuildInfo pkg_descr)
309+
310+
-- | As they appear on the cabal file, Paths_* module has no special treatment.
311+
-- It is not added if it doesn't already appears here.
312+
allModuleNamesAutogen :: PackageDescription -> [ModuleName]
313+
allModuleNamesAutogen pkg_descr = concatMap autogenModules (allBuildInfo pkg_descr)
299314

300315
-- ------------------------------------------------------------
301316
-- * Utils

Cabal/Distribution/Types/TestSuite.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Distribution.Types.TestSuite (
66
emptyTestSuite,
77
testType,
88
testModules,
9+
testModulesAutogen
910
) where
1011

1112
import Prelude ()
@@ -65,3 +66,11 @@ testModules test = (case testInterface test of
6566
TestSuiteLibV09 _ m -> [m]
6667
_ -> [])
6768
++ otherModules (testBuildInfo test)
69+
70+
-- | Get all the auto generated module names from a test suite.
71+
-- This are a subset of 'testModules'.
72+
testModulesAutogen :: TestSuite -> [ModuleName]
73+
testModulesAutogen test = (case testInterface test of
74+
TestSuiteLibV09 _ m -> [m]
75+
_ -> [])
76+
++ autogenModules (testBuildInfo test)

cabal-install/Distribution/Client/Init.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,12 @@ generateCabalFile fileName c =
889889
Executable -> "Modules included in this executable, other than Main.")
890890
True
891891

892+
, fieldS "autogen-modules" (listField (otherModules c'))
893+
(Just $ case pkgtype of
894+
Library -> "Auto generated modules in this library."
895+
Executable -> "Auto generated modules in this executable.")
896+
True
897+
892898
, fieldS "other-extensions" (listField (otherExts c'))
893899
(Just "LANGUAGE extensions used by modules in this package.")
894900
True

0 commit comments

Comments
 (0)