Skip to content

Commit 89cf822

Browse files
Do not use tmp dirs for Haddock, add --no-haddock-version-cpp, and more
Haddock no longer writes compilation files by default, so we do not need to pass tmp dirs for `-hidir`, `-stubdir`, and `-odir` via `--optghc`. Indeed, we do not *want* to do so, since it results in recompilation for every invocation of Haddock via Cabal. This commit stops this from happening. This commit also introduces the `--no-haddock-version-cpp` flag and `no-haddock-version-cpp:` cabal.project field, which prevent the definition of the `__HADDOCK_VERSION__` macro when invoking GHC through Haddock. This is essentially required in order for Haddock to be able to use existing compilation results and avoid recompilation, since the macro definition will trigger recompilation. This commit also renames the `--haddock-lib` flag to `--haddock-resources-dir` (and `haddock-lib:` cabal.project field to `haddock-resources-dir:`), and adds this flag to the users guide since it was missing an entry. This also allows us to add this field to `cabal-install:test:integration-tests2`, since it is no longer ambiguous with the `--lib` flag. This commit also causes `documentation: true` or `--enable-documentation` to imply `-haddock` for GHC. Also, since Haddock >= 2.29 is renaming `--lib` to `--resources-dir`, this commit switches the flag provided to Haddock using a backwards compatible condition based on the Haddock version.
1 parent e77f139 commit 89cf822

File tree

19 files changed

+247
-167
lines changed

19 files changed

+247
-167
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ bench.html
8383

8484
# Emacs
8585
.projectile
86+
87+
# direnv
88+
.envrc

Cabal/src/Distribution/Simple/Haddock.hs

+118-115
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ data HaddockArgs = HaddockArgs
137137
-- ^ Re-exported modules
138138
, argTargets :: [FilePath]
139139
-- ^ Modules to process.
140-
, argLib :: Flag String
140+
, argResourcesDir :: Flag String
141141
-- ^ haddock's static \/ auxiliary files.
142142
}
143143
deriving (Generic)
@@ -287,10 +287,18 @@ haddock pkg_descr lbi suffixes flags' = do
287287
[] -> allTargetsInBuildOrder' pkg_descr lbi
288288
_ -> targets
289289

290+
version' =
291+
if flag haddockNoVersionCPP then
292+
Nothing
293+
else
294+
Just version
295+
296+
index = installedPkgs lbi
297+
290298
internalPackageDB <-
291299
createInternalPackageDB verbosity lbi (flag haddockDistPref)
292300

293-
(\f -> foldM_ f (installedPkgs lbi) targets') $ \index target -> do
301+
forM_ targets' $ \target -> do
294302
let component = targetComponent target
295303
clbi = targetCLBI target
296304

@@ -305,28 +313,25 @@ haddock pkg_descr lbi suffixes flags' = do
305313

306314
preprocessComponent pkg_descr component lbi' clbi False verbosity suffixes
307315
let
308-
doExe com = case (compToExe com) of
316+
doExe com = case compToExe com of
309317
Just exe -> do
310-
withTempDirectoryEx verbosity tmpFileOpts (buildDir lbi') "tmp" $
311-
\tmp -> do
312-
exeArgs <-
313-
fromExecutable
314-
verbosity
315-
tmp
316-
lbi'
317-
clbi
318-
htmlTemplate
319-
version
320-
exe
321-
let exeArgs' = commonArgs `mappend` exeArgs
322-
runHaddock
323-
verbosity
324-
tmpFileOpts
325-
comp
326-
platform
327-
haddockProg
328-
True
329-
exeArgs'
318+
exeArgs <-
319+
fromExecutable
320+
verbosity
321+
lbi'
322+
clbi
323+
htmlTemplate
324+
version'
325+
exe
326+
let exeArgs' = commonArgs `mappend` exeArgs
327+
runHaddock
328+
verbosity
329+
tmpFileOpts
330+
comp
331+
platform
332+
haddockProg
333+
True
334+
exeArgs'
330335
Nothing -> do
331336
warn
332337
(fromFlag $ haddockVerbosity flags)
@@ -345,67 +350,62 @@ haddock pkg_descr lbi suffixes flags' = do
345350
(maybeComponentInstantiatedWith clbi)
346351
case component of
347352
CLib lib -> do
348-
withTempDirectoryEx verbosity tmpFileOpts (buildDir lbi) "tmp" $
349-
\tmp -> do
350-
smsg
351-
libArgs <-
352-
fromLibrary
353-
verbosity
354-
tmp
355-
lbi'
356-
clbi
357-
htmlTemplate
358-
version
359-
lib
360-
let libArgs' = commonArgs `mappend` libArgs
361-
runHaddock verbosity tmpFileOpts comp platform haddockProg True libArgs'
362-
363-
pwd <- getCurrentDirectory
364-
365-
let
366-
ipi =
367-
inplaceInstalledPackageInfo
368-
pwd
369-
(flag haddockDistPref)
370-
pkg_descr
371-
(mkAbiHash "inplace")
372-
lib
373-
lbi'
374-
clbi
353+
smsg
354+
libArgs <-
355+
fromLibrary
356+
verbosity
357+
lbi'
358+
clbi
359+
htmlTemplate
360+
version'
361+
lib
362+
let libArgs' = commonArgs `mappend` libArgs
363+
runHaddock verbosity tmpFileOpts comp platform haddockProg True libArgs'
375364

376-
debug verbosity $
377-
"Registering inplace:\n"
378-
++ (InstalledPackageInfo.showInstalledPackageInfo ipi)
365+
pwd <- getCurrentDirectory
366+
367+
let
368+
ipi =
369+
inplaceInstalledPackageInfo
370+
pwd
371+
(flag haddockDistPref)
372+
pkg_descr
373+
(mkAbiHash "inplace")
374+
lib
375+
lbi'
376+
clbi
377+
378+
debug verbosity $
379+
"Registering inplace:\n"
380+
++ (InstalledPackageInfo.showInstalledPackageInfo ipi)
381+
382+
registerPackage
383+
verbosity
384+
(compiler lbi')
385+
(withPrograms lbi')
386+
(withPackageDB lbi')
387+
ipi
388+
HcPkg.defaultRegisterOptions
389+
{ HcPkg.registerMultiInstance = True
390+
}
391+
392+
return $ PackageIndex.insert ipi index
379393

380-
registerPackage
381-
verbosity
382-
(compiler lbi')
383-
(withPrograms lbi')
384-
(withPackageDB lbi')
385-
ipi
386-
HcPkg.defaultRegisterOptions
387-
{ HcPkg.registerMultiInstance = True
388-
}
389-
390-
return $ PackageIndex.insert ipi index
391394
CFLib flib ->
392395
when
393396
(flag haddockForeignLibs)
394397
( do
395-
withTempDirectoryEx verbosity tmpFileOpts (buildDir lbi') "tmp" $
396-
\tmp -> do
397-
smsg
398-
flibArgs <-
399-
fromForeignLib
400-
verbosity
401-
tmp
402-
lbi'
403-
clbi
404-
htmlTemplate
405-
version
406-
flib
407-
let libArgs' = commonArgs `mappend` flibArgs
408-
runHaddock verbosity tmpFileOpts comp platform haddockProg True libArgs'
398+
smsg
399+
flibArgs <-
400+
fromForeignLib
401+
verbosity
402+
lbi'
403+
clbi
404+
htmlTemplate
405+
version'
406+
flib
407+
let libArgs' = commonArgs `mappend` flibArgs
408+
runHaddock verbosity tmpFileOpts comp platform haddockProg True libArgs'
409409
)
410410
>> return index
411411
CExe _ -> when (flag haddockExecutables) (smsg >> doExe component) >> return index
@@ -465,7 +465,7 @@ fromFlags env flags =
465465
(haddockIndex flags)
466466
, argGenIndex = Flag False
467467
, argBaseUrl = haddockBaseUrl flags
468-
, argLib = haddockLib flags
468+
, argResourcesDir = haddockResourcesDir flags
469469
, argVerbose =
470470
maybe mempty (Any . (>= deafening))
471471
. flagToMaybe
@@ -491,7 +491,7 @@ fromHaddockProjectFlags flags =
491491
, argPrologueFile = haddockProjectPrologue flags
492492
, argInterfaces = fromFlagOrDefault [] (haddockProjectInterfaces flags)
493493
, argLinkedSource = Flag True
494-
, argLib = haddockProjectLib flags
494+
, argResourcesDir = haddockProjectResourcesDir flags
495495
}
496496

497497
fromPackageDescription :: HaddockTarget -> PackageDescription -> HaddockArgs
@@ -536,27 +536,20 @@ componentGhcOptions verbosity lbi bi clbi odir =
536536

537537
mkHaddockArgs
538538
:: Verbosity
539-
-> FilePath
540539
-> LocalBuildInfo
541540
-> ComponentLocalBuildInfo
542541
-> Maybe PathTemplate
543542
-- ^ template for HTML location
544-
-> Version
543+
-> Maybe Version
544+
-- ^ 'Nothing' if the user requested not to define the __HADDOCK_VERSION__
545+
-- macro
545546
-> [FilePath]
546547
-> BuildInfo
547548
-> IO HaddockArgs
548-
mkHaddockArgs verbosity tmp lbi clbi htmlTemplate haddockVersion inFiles bi = do
549+
mkHaddockArgs verbosity lbi clbi htmlTemplate haddockVersion inFiles bi = do
549550
ifaceArgs <- getInterfaces verbosity lbi clbi htmlTemplate
550551
let vanillaOpts =
551-
(componentGhcOptions normal lbi bi clbi (buildDir lbi))
552-
{ -- Noooooooooo!!!!!111
553-
-- haddock stomps on our precious .hi
554-
-- and .o files. Workaround by telling
555-
-- haddock to write them elsewhere.
556-
ghcOptObjDir = toFlag tmp
557-
, ghcOptHiDir = toFlag tmp
558-
, ghcOptStubDir = toFlag tmp
559-
}
552+
componentGhcOptions normal lbi bi clbi (buildDir lbi)
560553
`mappend` getGhcCppOpts haddockVersion bi
561554
sharedOpts =
562555
vanillaOpts
@@ -583,20 +576,20 @@ mkHaddockArgs verbosity tmp lbi clbi htmlTemplate haddockVersion inFiles bi = do
583576

584577
fromLibrary
585578
:: Verbosity
586-
-> FilePath
587579
-> LocalBuildInfo
588580
-> ComponentLocalBuildInfo
589581
-> Maybe PathTemplate
590582
-- ^ template for HTML location
591-
-> Version
583+
-> Maybe Version
584+
-- ^ 'Nothing' if the user requested not to define the __HADDOCK_VERSION__
585+
-- macro
592586
-> Library
593587
-> IO HaddockArgs
594-
fromLibrary verbosity tmp lbi clbi htmlTemplate haddockVersion lib = do
588+
fromLibrary verbosity lbi clbi htmlTemplate haddockVersion lib = do
595589
inFiles <- map snd `fmap` getLibSourceFiles verbosity lbi lib clbi
596590
args <-
597591
mkHaddockArgs
598592
verbosity
599-
tmp
600593
lbi
601594
clbi
602595
htmlTemplate
@@ -610,20 +603,20 @@ fromLibrary verbosity tmp lbi clbi htmlTemplate haddockVersion lib = do
610603

611604
fromExecutable
612605
:: Verbosity
613-
-> FilePath
614606
-> LocalBuildInfo
615607
-> ComponentLocalBuildInfo
616608
-> Maybe PathTemplate
617609
-- ^ template for HTML location
618-
-> Version
610+
-> Maybe Version
611+
-- ^ 'Nothing' if the user requested not to define the __HADDOCK_VERSION__
612+
-- macro
619613
-> Executable
620614
-> IO HaddockArgs
621-
fromExecutable verbosity tmp lbi clbi htmlTemplate haddockVersion exe = do
615+
fromExecutable verbosity lbi clbi htmlTemplate haddockVersion exe = do
622616
inFiles <- map snd `fmap` getExeSourceFiles verbosity lbi exe clbi
623617
args <-
624618
mkHaddockArgs
625619
verbosity
626-
tmp
627620
lbi
628621
clbi
629622
htmlTemplate
@@ -638,20 +631,20 @@ fromExecutable verbosity tmp lbi clbi htmlTemplate haddockVersion exe = do
638631

639632
fromForeignLib
640633
:: Verbosity
641-
-> FilePath
642634
-> LocalBuildInfo
643635
-> ComponentLocalBuildInfo
644636
-> Maybe PathTemplate
645637
-- ^ template for HTML location
646-
-> Version
638+
-> Maybe Version
639+
-- ^ 'Nothing' if the user requested not to define the __HADDOCK_VERSION__
640+
-- macro
647641
-> ForeignLib
648642
-> IO HaddockArgs
649-
fromForeignLib verbosity tmp lbi clbi htmlTemplate haddockVersion flib = do
643+
fromForeignLib verbosity lbi clbi htmlTemplate haddockVersion flib = do
650644
inFiles <- map snd `fmap` getFLibSourceFiles verbosity lbi flib clbi
651645
args <-
652646
mkHaddockArgs
653647
verbosity
654-
tmp
655648
lbi
656649
clbi
657650
htmlTemplate
@@ -707,7 +700,9 @@ getReexports LibComponentLocalBuildInfo{componentExposedModules = mods} =
707700
getReexports _ = []
708701

709702
getGhcCppOpts
710-
:: Version
703+
:: Maybe Version
704+
-- ^ 'Nothing' if the user requested not to define the __HADDOCK_VERSION__
705+
-- macro
711706
-> BuildInfo
712707
-> GhcOptions
713708
getGhcCppOpts haddockVersion bi =
@@ -717,16 +712,21 @@ getGhcCppOpts haddockVersion bi =
717712
}
718713
where
719714
needsCpp = EnableExtension CPP `elem` usedExtensions bi
720-
defines = [haddockVersionMacro]
721-
haddockVersionMacro =
722-
"-D__HADDOCK_VERSION__="
723-
++ show (v1 * 1000 + v2 * 10 + v3)
715+
defines =
716+
[ "-D__HADDOCK_VERSION__=" ++ show vn
717+
| Just vn <- [versionInt . versionNumbers <$> haddockVersion]
718+
]
724719
where
725-
(v1, v2, v3) = case versionNumbers haddockVersion of
726-
[] -> (0, 0, 0)
727-
[x] -> (x, 0, 0)
728-
[x, y] -> (x, y, 0)
729-
(x : y : z : _) -> (x, y, z)
720+
-- For some list xs = [x, y, z ...], versionInt xs results in
721+
-- x * 1000 + y * 10 + z. E.g.:
722+
-- >>> versionInt [2, 29, 0]
723+
-- 2290
724+
-- >>> versionInt [3, 4]
725+
-- 3040
726+
-- >>> versionInt []
727+
-- 0
728+
versionInt :: [Int] -> Int
729+
versionInt = foldr ((+) . uncurry (*)) 0 . zip [1000,10,1]
730730

731731
getGhcLibDir
732732
:: Verbosity
@@ -922,7 +922,7 @@ renderPureArgs version comp platform args =
922922
, isVersion 2 19
923923
]
924924
, argTargets $ args
925-
, maybe [] ((: []) . ("--lib=" ++)) . flagToMaybe . argLib $ args
925+
, maybe [] ((: []) . (resourcesDirFlag ++)) . flagToMaybe . argResourcesDir $ args
926926
]
927927
where
928928
renderInterfaces = map renderInterface
@@ -964,6 +964,9 @@ renderPureArgs version comp platform args =
964964
verbosityFlag
965965
| isVersion 2 5 = "--verbosity=1"
966966
| otherwise = "--verbose"
967+
resourcesDirFlag
968+
| isVersion 2 29 = "--resources-dir="
969+
| otherwise = "--lib="
967970
haddockSupportsVisibility = version >= mkVersion [2, 26, 1]
968971
haddockSupportsPackageName = version > mkVersion [2, 16]
969972

0 commit comments

Comments
 (0)