Skip to content

Commit 168824f

Browse files
committed
Add new 'autogen-modules' field
Modules that are built automatically at setup, like Paths_PACKAGENAME or others created with a build-type custom, appear on 'other-modules' for the Library, Executable, Test-Suite or Benchmark stanzas or also on 'exposed-modules' for libraries but are not really on the package when distributed. This makes commands like sdist fail because the file is not found, so with this new field modules that appear there are treated the same way as Paths_PACKAGENAME was and there is no need to create complex build hooks. Just add the module names on 'other-modules' and 'exposed-modules' as always and on the new 'autogen-modules' besides.
1 parent 6309f82 commit 168824f

37 files changed

+516
-16
lines changed

Cabal/Cabal.cabal

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ extra-source-files:
3838
tests/PackageTests/AllowOlder/benchmarks/Bench.hs
3939
tests/PackageTests/AllowOlder/src/Foo.hs
4040
tests/PackageTests/AllowOlder/tests/Test.hs
41+
tests/PackageTests/AutogenModules/Package/Dummy.hs
42+
tests/PackageTests/AutogenModules/Package/MyBenchModule.hs
43+
tests/PackageTests/AutogenModules/Package/MyExeModule.hs
44+
tests/PackageTests/AutogenModules/Package/MyLibModule.hs
45+
tests/PackageTests/AutogenModules/Package/MyLibrary.hs
46+
tests/PackageTests/AutogenModules/Package/MyTestModule.hs
47+
tests/PackageTests/AutogenModules/Package/my.cabal
48+
tests/PackageTests/AutogenModules/SrcDist/Dummy.hs
49+
tests/PackageTests/AutogenModules/SrcDist/MyBenchModule.hs
50+
tests/PackageTests/AutogenModules/SrcDist/MyExeModule.hs
51+
tests/PackageTests/AutogenModules/SrcDist/MyLibModule.hs
52+
tests/PackageTests/AutogenModules/SrcDist/MyLibrary.hs
53+
tests/PackageTests/AutogenModules/SrcDist/MyTestModule.hs
54+
tests/PackageTests/AutogenModules/SrcDist/my.cabal
4155
tests/PackageTests/BenchmarkExeV10/Foo.hs
4256
tests/PackageTests/BenchmarkExeV10/benchmarks/bench-Foo.hs
4357
tests/PackageTests/BenchmarkExeV10/my.cabal
@@ -435,6 +449,8 @@ test-suite package-tests
435449
type: exitcode-stdio-1.0
436450
main-is: PackageTests.hs
437451
other-modules:
452+
PackageTests.AutogenModules.Package.Check
453+
PackageTests.AutogenModules.SrcDist.Check
438454
PackageTests.BenchmarkStanza.Check
439455
PackageTests.TestStanza.Check
440456
PackageTests.DeterministicAr.Check

Cabal/Distribution/PackageDescription.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ module Distribution.PackageDescription (
3636
hasPublicLib,
3737
hasLibs,
3838
libModules,
39+
libModulesAutogen,
3940

4041
-- ** Executables
4142
Executable(..),
4243
emptyExecutable,
4344
withExe,
4445
hasExes,
4546
exeModules,
47+
exeModulesAutogen,
4648

4749
-- * Tests
4850
TestSuite(..),
@@ -54,6 +56,7 @@ module Distribution.PackageDescription (
5456
hasTests,
5557
withTest,
5658
testModules,
59+
testModulesAutogen,
5760

5861
-- * Benchmarks
5962
Benchmark(..),
@@ -65,6 +68,7 @@ module Distribution.PackageDescription (
6568
hasBenchmarks,
6669
withBenchmark,
6770
benchmarkModules,
71+
benchmarkModulesAutogen,
6872

6973
-- * Build information
7074
BuildInfo(..),

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 58 additions & 0 deletions
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 (autogenPathsModuleName)
4445
import Distribution.Simple.CCompiler
4546
import Distribution.Simple.Utils hiding (findPackageDesc, notice)
4647
import Distribution.Version
@@ -245,6 +246,14 @@ 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+
, check
252+
(not $ and $ map (flip elem (libModules lib)) (libModulesAutogen lib)) $
253+
PackageBuildImpossible $
254+
"An 'autogen-module' is neither on 'exposed-modules' or "
255+
++ "'other-modules'."
256+
248257
]
249258

250259
where
@@ -282,6 +291,14 @@ checkExecutable pkg exe =
282291
PackageBuildImpossible $
283292
"Duplicate modules in executable '" ++ exeName exe ++ "': "
284293
++ commaSep (map display moduleDuplicates)
294+
295+
-- check that all autogen-modules appear on other-modules
296+
, check
297+
(not $ and $ map (flip elem (exeModules exe)) (exeModulesAutogen exe)) $
298+
PackageBuildImpossible $
299+
"On executable '" ++ exeName exe ++ "' an 'autogen-module' is not "
300+
++ "on 'other-modules'"
301+
285302
]
286303
where
287304
moduleDuplicates = dups (exeModules exe)
@@ -319,6 +336,16 @@ checkTestSuite pkg test =
319336
PackageDistInexcusable $
320337
"The package uses a C/C++/obj-C source file for the 'main-is' field. "
321338
++ "To use this feature you must specify 'cabal-version: >= 1.18'."
339+
340+
-- check that all autogen-modules appear on other-modules
341+
, check
342+
(not $ and $ map
343+
(flip elem (testModules test))
344+
(testModulesAutogen test)
345+
) $
346+
PackageBuildImpossible $
347+
"On test suite '" ++ testName test ++ "' an 'autogen-module' is not "
348+
++ "on 'other-modules'"
322349
]
323350
where
324351
moduleDuplicates = dups $ testModules test
@@ -358,6 +385,16 @@ checkBenchmark _pkg bm =
358385
PackageBuildImpossible $
359386
"The 'main-is' field must specify a '.hs' or '.lhs' file "
360387
++ "(even if it is generated by a preprocessor)."
388+
389+
-- check that all autogen-modules appear on other-modules
390+
, check
391+
(not $ and $ map
392+
(flip elem (benchmarkModules bm))
393+
(benchmarkModulesAutogen bm)
394+
) $
395+
PackageBuildImpossible $
396+
"On benchmark '" ++ benchmarkName bm ++ "' an 'autogen-module' is "
397+
++ "not on 'other-modules'"
361398
]
362399
where
363400
moduleDuplicates = dups $ benchmarkModules bm
@@ -1110,6 +1147,18 @@ checkCabalVersion pkg =
11101147
++ "that specifies the dependencies of the Setup.hs script itself. "
11111148
++ "The 'setup-depends' field uses the same syntax as 'build-depends', "
11121149
++ "so a simple example would be 'setup-depends: base, Cabal'."
1150+
1151+
, check (specVersion pkg >= Version [1,25] []
1152+
&& elem (autogenPathsModuleName pkg) allModuleNames
1153+
&& not (elem (autogenPathsModuleName pkg) allModuleNamesAutogen) ) $
1154+
PackageDistInexcusable $
1155+
"Packages using 'cabal-version: >= 1.25' and the autogenerated "
1156+
++ "module Paths_* must include it also on the 'autogen-modules' field "
1157+
++ "besides 'exposed-modules' and 'other-modules'. This specifies that "
1158+
++ "the module does not come with the package and is generated on "
1159+
++ "setup. Modules built with a custom Setup.hs script also go here "
1160+
++ "to ensure that commands like sdist don't fail."
1161+
11131162
]
11141163
where
11151164
-- Perform a check on packages that use a version of the spec less than
@@ -1244,6 +1293,15 @@ checkCabalVersion pkg =
12441293
map DisableExtension
12451294
[MonoPatBinds]
12461295

1296+
allModuleNames =
1297+
(case library pkg of
1298+
Nothing -> []
1299+
(Just lib) -> libModules lib
1300+
)
1301+
++ concatMap otherModules (allBuildInfo pkg)
1302+
1303+
allModuleNamesAutogen = concatMap autogenModules (allBuildInfo pkg)
1304+
12471305
-- | A variation on the normal 'Text' instance, shows any ()'s in the original
12481306
-- textual syntax. We need to show these otherwise it's confusing to users when
12491307
-- we complain of their presence but do not pretty print them!

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/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ writeAutogenFiles verbosity pkg lbi clbi = do
583583
createDirectoryIfMissingVerbose verbosity True (autogenComponentModulesDir lbi clbi)
584584

585585
let pathsModulePath = autogenComponentModulesDir lbi clbi
586-
</> ModuleName.toFilePath (autogenModuleName pkg) <.> "hs"
586+
</> ModuleName.toFilePath (autogenPathsModuleName pkg) <.> "hs"
587587
rewriteFile pathsModulePath (Build.PathsModule.generate pkg lbi clbi)
588588

589589
let cppHeaderPath = autogenComponentModulesDir lbi clbi </> cppHeaderName

Cabal/Distribution/Simple/Build/PathsModule.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ generate pkg_descr lbi clbi =
226226
_ -> False
227227
supportsRelocatableProgs _ = False
228228

229-
paths_modulename = autogenModuleName pkg_descr
229+
paths_modulename = autogenPathsModuleName pkg_descr
230230

231231
get_prefix_stuff = get_prefix_win32 buildArch
232232

Cabal/Distribution/Simple/BuildPaths.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Distribution.Simple.BuildPaths (
1919
autogenComponentModulesDir,
2020

2121
autogenModuleName,
22+
autogenPathsModuleName,
2223
cppHeaderName,
2324
haddockName,
2425

@@ -87,9 +88,14 @@ autogenComponentModulesDir lbi clbi = componentBuildDir lbi clbi </> "autogen"
8788
cppHeaderName :: String
8889
cppHeaderName = "cabal_macros.h"
8990

91+
{-# DEPRECATED autogenModuleName "Use autogenPathsModuleName instead" #-}
9092
-- |The name of the auto-generated module associated with a package
9193
autogenModuleName :: PackageDescription -> ModuleName
92-
autogenModuleName pkg_descr =
94+
autogenModuleName = autogenPathsModuleName
95+
96+
-- | The name of the auto-generated Paths_* module associated with a package
97+
autogenPathsModuleName :: PackageDescription -> ModuleName
98+
autogenPathsModuleName pkg_descr =
9399
ModuleName.fromString $
94100
"Paths_" ++ map fixchar (display (packageName pkg_descr))
95101
where fixchar '-' = '_'

Cabal/Distribution/Simple/SrcDist.hs

Lines changed: 14 additions & 10 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 = autogenPathsModuleName 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
@@ -437,7 +440,8 @@ allSourcesBuildInfo bi pps modules = do
437440
nonEmpty _ f xs = f xs
438441
suffixes = ppSuffixes pps ++ ["hs", "lhs"]
439442
notFound m = die $ "Error: Could not find module: " ++ display m
440-
++ " with any suffix: " ++ show suffixes
443+
++ " with any suffix: " ++ show suffixes ++ ". If the module "
444+
++ "is autogenerated it should be added to 'autogen-modules'."
441445

442446

443447
printPackageProblems :: Verbosity -> PackageDescription -> IO ()

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/TestSuite.hs

Lines changed: 6 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,8 @@ 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 = autogenModules (testBuildInfo test)

Cabal/changelog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050
* 'getComponentLocalBuildInfo', 'withComponentsInBuildOrder'
5151
and 'componentsInBuildOrder' are deprecated in favor of a
5252
new interface in "Distribution.Types.LocalBuildInfo".
53+
* New 'autogen-modules' field. Modules that are built automatically at
54+
setup, like Paths_PACKAGENAME or others created with a build-type
55+
custom, appear on 'other-modules' for the Library, Executable,
56+
Test-Suite or Benchmark stanzas or also on 'exposed-modules' for
57+
libraries but are not really on the package when distributed. This
58+
makes commands like sdist fail because the file is not found, so with
59+
this new field modules that appear there are treated the same way as
60+
Paths_PACKAGENAME was and there is no need to create complex build
61+
hooks. Just add the module names on 'other-modules' and
62+
'exposed-modules' as always and on the new 'autogen-modules' besides.
63+
(#3656).
5364

5465
1.24.0.0 Ryan Thomas <[email protected]> March 2016
5566
* Support GHC 8.

0 commit comments

Comments
 (0)