|
| 1 | +-- | Contains an @sdist@ like function which computes the source files |
| 2 | +-- that we should track to determine if a rebuild is necessary. |
| 3 | +-- Unlike @sdist@, we can operate directly on the true |
| 4 | +-- 'PackageDescription' (not flattened). |
| 5 | +-- |
| 6 | +-- The naming convention, roughly, is that to declare we need the |
| 7 | +-- source for some type T, you use the function needT; some functions |
| 8 | +-- need auxiliary information. |
| 9 | +-- |
| 10 | +-- We can only use this code for non-Custom scripts; Custom scripts |
| 11 | +-- may have arbitrary extra dependencies (esp. new preprocessors) which |
| 12 | +-- we cannot "see" easily. |
| 13 | +module Distribution.Client.SourceFiles (needElaboratedConfiguredPackage) where |
| 14 | + |
| 15 | +import Distribution.Client.ProjectPlanning.Types |
| 16 | +import Distribution.Client.RebuildMonad |
| 17 | + |
| 18 | +import Distribution.Solver.Types.OptionalStanza |
| 19 | + |
| 20 | +import Distribution.Simple.PreProcess |
| 21 | + |
| 22 | +import Distribution.Types.PackageDescription |
| 23 | +import Distribution.Types.Component |
| 24 | +import Distribution.Types.ComponentRequestedSpec |
| 25 | +import Distribution.Types.Library |
| 26 | +import Distribution.Types.Executable |
| 27 | +import Distribution.Types.Benchmark |
| 28 | +import Distribution.Types.BenchmarkInterface |
| 29 | +import Distribution.Types.TestSuite |
| 30 | +import Distribution.Types.TestSuiteInterface |
| 31 | +import Distribution.Types.BuildInfo |
| 32 | + |
| 33 | +import Distribution.ModuleName |
| 34 | + |
| 35 | +import Prelude () |
| 36 | +import Distribution.Client.Compat.Prelude |
| 37 | + |
| 38 | +import System.FilePath |
| 39 | +import Control.Monad |
| 40 | +import qualified Data.Set as Set |
| 41 | + |
| 42 | +needElaboratedConfiguredPackage :: ElaboratedConfiguredPackage -> Rebuild () |
| 43 | +needElaboratedConfiguredPackage elab = |
| 44 | + case elabPkgOrComp elab of |
| 45 | + ElabComponent ecomp -> needElaboratedComponent elab ecomp |
| 46 | + ElabPackage epkg -> needElaboratedPackage elab epkg |
| 47 | + |
| 48 | +needElaboratedPackage :: ElaboratedConfiguredPackage -> ElaboratedPackage -> Rebuild () |
| 49 | +needElaboratedPackage elab epkg = |
| 50 | + mapM_ (needComponent pkg_descr) (enabledComponents pkg_descr enabled) |
| 51 | + where |
| 52 | + pkg_descr = elabPkgDescription elab |
| 53 | + enabled_stanzas = pkgStanzasEnabled epkg |
| 54 | + -- TODO: turn this into a helper function somewhere |
| 55 | + enabled = |
| 56 | + ComponentRequestedSpec { |
| 57 | + testsRequested = TestStanzas `Set.member` enabled_stanzas, |
| 58 | + benchmarksRequested = BenchStanzas `Set.member` enabled_stanzas |
| 59 | + } |
| 60 | + |
| 61 | +needElaboratedComponent :: ElaboratedConfiguredPackage -> ElaboratedComponent -> Rebuild () |
| 62 | +needElaboratedComponent elab ecomp = |
| 63 | + case mb_comp of |
| 64 | + Nothing -> needSetup |
| 65 | + Just comp -> needComponent pkg_descr comp |
| 66 | + where |
| 67 | + pkg_descr = elabPkgDescription elab |
| 68 | + mb_comp = fmap (getComponent pkg_descr) (compComponentName ecomp) |
| 69 | + |
| 70 | +needComponent :: PackageDescription -> Component -> Rebuild () |
| 71 | +needComponent pkg_descr comp = |
| 72 | + case comp of |
| 73 | + CLib lib -> needLibrary pkg_descr lib |
| 74 | + CExe exe -> needExecutable pkg_descr exe |
| 75 | + CTest test -> needTestSuite pkg_descr test |
| 76 | + CBench bench -> needBenchmark pkg_descr bench |
| 77 | + |
| 78 | +needSetup :: Rebuild () |
| 79 | +needSetup = findFirstFileMonitored id ["Setup.hs", "Setup.lhs"] >> return () |
| 80 | + |
| 81 | +needLibrary :: PackageDescription -> Library -> Rebuild () |
| 82 | +needLibrary pkg_descr (Library { exposedModules = modules |
| 83 | + , signatures = sigs |
| 84 | + , libBuildInfo = bi }) |
| 85 | + = needBuildInfo pkg_descr bi (modules ++ sigs) |
| 86 | + |
| 87 | +needExecutable :: PackageDescription -> Executable -> Rebuild () |
| 88 | +needExecutable pkg_descr (Executable { modulePath = mainPath |
| 89 | + , buildInfo = bi }) |
| 90 | + = do needBuildInfo pkg_descr bi [] |
| 91 | + needMainFile bi mainPath |
| 92 | + |
| 93 | +needTestSuite :: PackageDescription -> TestSuite -> Rebuild () |
| 94 | +needTestSuite pkg_descr t |
| 95 | + = case testInterface t of |
| 96 | + TestSuiteExeV10 _ mainPath -> do |
| 97 | + needBuildInfo pkg_descr bi [] |
| 98 | + needMainFile bi mainPath |
| 99 | + TestSuiteLibV09 _ m -> |
| 100 | + needBuildInfo pkg_descr bi [m] |
| 101 | + TestSuiteUnsupported _ -> return () -- soft fail |
| 102 | + where |
| 103 | + bi = testBuildInfo t |
| 104 | + |
| 105 | +needMainFile :: BuildInfo -> FilePath -> Rebuild () |
| 106 | +needMainFile bi mainPath = do |
| 107 | + -- The matter here is subtle. It might *seem* that we |
| 108 | + -- should just search for mainPath, but as per |
| 109 | + -- b61cb051f63ed5869b8f4a6af996ff7e833e4b39 'main-is' |
| 110 | + -- will actually be the source file AFTER preprocessing, |
| 111 | + -- whereas we need to get the file *prior* to preprocessing. |
| 112 | + ppFile <- findFileWithExtensionMonitored |
| 113 | + (ppSuffixes knownSuffixHandlers) |
| 114 | + (hsSourceDirs bi) |
| 115 | + (dropExtension mainPath) |
| 116 | + case ppFile of |
| 117 | + -- But check the original path in the end, because |
| 118 | + -- maybe it's a non-preprocessed file with a non-traditional |
| 119 | + -- extension. |
| 120 | + Nothing -> findFileMonitored (hsSourceDirs bi) mainPath |
| 121 | + >>= maybe (return ()) need |
| 122 | + Just pp -> need pp |
| 123 | + |
| 124 | +needBenchmark :: PackageDescription -> Benchmark -> Rebuild () |
| 125 | +needBenchmark pkg_descr bm |
| 126 | + = case benchmarkInterface bm of |
| 127 | + BenchmarkExeV10 _ mainPath -> do |
| 128 | + needBuildInfo pkg_descr bi [] |
| 129 | + needMainFile bi mainPath |
| 130 | + BenchmarkUnsupported _ -> return () -- soft fail |
| 131 | + where |
| 132 | + bi = benchmarkBuildInfo bm |
| 133 | + |
| 134 | +needBuildInfo :: PackageDescription -> BuildInfo -> [ModuleName] -> Rebuild () |
| 135 | +needBuildInfo pkg_descr bi modules = do |
| 136 | + -- NB: These are separate because there may be both A.hs and |
| 137 | + -- A.hs-boot; need to track both. |
| 138 | + findNeededModules ["hs", "lhs", "hsig", "lhsig"] |
| 139 | + findNeededModules ["hs-boot", "lhs-boot"] |
| 140 | + mapM_ needIfExists (cSources bi ++ jsSources bi) |
| 141 | + -- A MASSIVE HACK to (1) make sure we rebuild when header |
| 142 | + -- files change, but (2) not have to rebuild when anything |
| 143 | + -- in extra-src-files changes (most of these won't affect |
| 144 | + -- compilation). It would be even better if we knew on a |
| 145 | + -- per-component basis which headers would be used but that |
| 146 | + -- seems to be too difficult. |
| 147 | + mapM_ needIfExists (filter ((==".h").takeExtension) (extraSrcFiles pkg_descr)) |
| 148 | + forM_ (installIncludes bi) $ \f -> |
| 149 | + findFileMonitored ("." : includeDirs bi) f |
| 150 | + >>= maybe (return ()) need |
| 151 | + where |
| 152 | + findNeededModules exts = |
| 153 | + mapM_ (findNeededModule exts) |
| 154 | + (modules ++ otherModules bi) |
| 155 | + findNeededModule exts m = |
| 156 | + findFileWithExtensionMonitored |
| 157 | + (ppSuffixes knownSuffixHandlers ++ exts) |
| 158 | + (hsSourceDirs bi) |
| 159 | + (toFilePath m) |
| 160 | + >>= maybe (return ()) need |
0 commit comments