| ... |
... |
@@ -132,7 +132,7 @@ doMkDepend = do |
|
132
|
132
|
module_graph <- GHC.depanal excl_mods True {- Allow dup roots -}
|
|
133
|
133
|
doMkDependModuleGraph dflags module_graph
|
|
134
|
134
|
|
|
135
|
|
-doMkDependModuleGraph :: GhcMonad m => DynFlags -> ModuleGraph -> m ()
|
|
|
135
|
+doMkDependModuleGraph :: GhcMonad m => DynFlags -> ModuleGraph -> m ()
|
|
136
|
136
|
doMkDependModuleGraph dflags module_graph = do
|
|
137
|
137
|
hsc_env <- getSession
|
|
138
|
138
|
logger <- getLogger
|
| ... |
... |
@@ -143,12 +143,14 @@ doMkDependModuleGraph dflags module_graph = do |
|
143
|
143
|
root <- liftIO Directory.getCurrentDirectory
|
|
144
|
144
|
|
|
145
|
145
|
let
|
|
|
146
|
+ suffixes = fmap unsafeEncodeUtf (depSuffixes dflags)
|
|
|
147
|
+ tmpdir = tmpDir dflags
|
|
146
|
148
|
backends = concat
|
|
147
|
|
- [ [ initFileDepWriter logger root tmpfs dflags makefile_output
|
|
148
|
|
- | Just makefile_output <- [depMakefile dflags]
|
|
|
149
|
+ [ [ initFileDepWriter logger root tmpfs tmpdir suffixes makefile_output
|
|
|
150
|
+ | Just makefile_output <- [initDepOutput (depMakefile dflags)]
|
|
149
|
151
|
]
|
|
150
|
|
- , [ initJsonDepWriter logger tmpfs dflags json_output
|
|
151
|
|
- | Just json_output <- [depJson dflags]
|
|
|
152
|
+ , [ initJsonDepWriter logger tmpfs tmpdir json_output
|
|
|
153
|
+ | Just json_output <- [initDepOutput (depJson dflags)]
|
|
152
|
154
|
]
|
|
153
|
155
|
]
|
|
154
|
156
|
|
| ... |
... |
@@ -158,24 +160,14 @@ doMkDependModuleGraph dflags module_graph = do |
|
158
|
160
|
liftIO $ debugTraceMsg logger 2 (text "Module dependencies" $$ ppr sorted)
|
|
159
|
161
|
|
|
160
|
162
|
liftIO $ do
|
|
161
|
|
-
|
|
162
|
|
- -- Setup the writers
|
|
163
|
|
- writers <- sequenceA backends
|
|
164
|
|
- sinks <- traverse dw_beginWriter writers
|
|
165
|
|
-
|
|
166
|
|
- -- Do the actual work
|
|
167
|
|
- do
|
|
|
163
|
+ withWriters backends $ \ depSink -> do
|
|
|
164
|
+ -- Do the actual work
|
|
168
|
165
|
-- Process them one by one, dumping results and complaining about cycles
|
|
169
|
|
- mapM_ (processDeps hsc_env (UniqSet.mkUniqSet excl_mods) sinks) sorted
|
|
|
166
|
+ mapM_ (processDeps hsc_env (UniqSet.mkUniqSet excl_mods) depSink) sorted
|
|
170
|
167
|
|
|
171
|
168
|
-- If -ddump-mod-cycles, show cycles in the module graph
|
|
172
|
169
|
liftIO $ dumpModCycles logger module_graph
|
|
173
|
170
|
|
|
174
|
|
- -- Tidy up
|
|
175
|
|
- do
|
|
176
|
|
- traverse_ dw_endWriter writers
|
|
177
|
|
-
|
|
178
|
|
-
|
|
179
|
171
|
-- Unconditional exiting is a bad idea. If an error occurs we'll get an
|
|
180
|
172
|
--exception; if that is not caught it's fine, but at least we have a
|
|
181
|
173
|
--chance to find out exactly what went wrong. Uncomment the following
|
| ... |
... |
@@ -216,15 +208,118 @@ data Dep |
|
216
|
208
|
{ dep_path :: OsPath
|
|
217
|
209
|
}
|
|
218
|
210
|
|
|
|
211
|
+-- | Consumer of the dependency information generated from the 'ModuleGraph'.
|
|
|
212
|
+--
|
|
|
213
|
+-- Use with 'withWriter'/'withWriters' to initialise the consumers of the dependency information
|
|
|
214
|
+-- generation.
|
|
219
|
215
|
data DependencyWriter = DependencyWriter
|
|
220
|
|
- { dw_beginWriter :: IO DepSink
|
|
|
216
|
+ { dw_sink :: DepSink
|
|
|
217
|
+ -- ^ Sink for dependency information
|
|
221
|
218
|
, dw_endWriter :: IO ()
|
|
|
219
|
+ -- ^ We finished consuming the dependency information, perform final
|
|
|
220
|
+ -- tidy up operations. This may be a no-op, but could also
|
|
|
221
|
+ -- copy temporary files, close open handlers, etc...
|
|
|
222
|
+ --
|
|
|
223
|
+ -- If 'dw_endWriter' was called, calling 'dw_onError' is unnecessary.
|
|
|
224
|
+ , dw_onError :: IO ()
|
|
|
225
|
+ -- ^ Something crashed! Clean up any resources opened by this 'DependencyWriter'.
|
|
222
|
226
|
}
|
|
223
|
227
|
|
|
224
|
|
-data DepSink = DepSink
|
|
|
228
|
+-- | Initialise multiple backends and combine them into one output function.
|
|
|
229
|
+--
|
|
|
230
|
+-- Combines the 'DepSink' of each 'DependencyWriter' into one single consumer.
|
|
|
231
|
+withWriters :: [IO DependencyWriter] -> (DepSink -> IO a) -> IO a
|
|
|
232
|
+withWriters [] k = k noopSink
|
|
|
233
|
+withWriters (dw : dws) k =
|
|
|
234
|
+ withWriter
|
|
|
235
|
+ dw
|
|
|
236
|
+ $ \s -> withWriters dws (\s' -> k (s `mappendSink` s'))
|
|
|
237
|
+
|
|
|
238
|
+-- | Initialise a single 'DependencyWriter', making sure any allocated resource
|
|
|
239
|
+-- get's tidied up again.
|
|
|
240
|
+--
|
|
|
241
|
+-- Accepts 'IO' 'DependencyWriter' since the initialisation may perform setup operations,
|
|
|
242
|
+-- such as opening file handles, or intermediate data types.
|
|
|
243
|
+withWriter :: IO DependencyWriter -> (DepSink -> IO r) -> IO r
|
|
|
244
|
+withWriter dwIO k =
|
|
|
245
|
+ bracketOnError dwIO dw_onError $ \ dw -> do
|
|
|
246
|
+ r <- k (dw_sink dw)
|
|
|
247
|
+ -- Clean up open resources
|
|
|
248
|
+ dw_endWriter dw
|
|
|
249
|
+ pure r
|
|
|
250
|
+
|
|
|
251
|
+-- | Process the dependency information for one node in the 'ModuleGraph'.
|
|
|
252
|
+newtype DepSink = DepSink
|
|
225
|
253
|
{ ds_writeDependency :: DepNode -> [Dep] -> IO ()
|
|
226
|
254
|
}
|
|
227
|
255
|
|
|
|
256
|
+noopSink :: DepSink
|
|
|
257
|
+noopSink = DepSink $ \ _ _ -> pure ()
|
|
|
258
|
+
|
|
|
259
|
+mappendSink :: DepSink -> DepSink -> DepSink
|
|
|
260
|
+mappendSink (DepSink s1) (DepSink s2) = DepSink (\ node deps -> s1 node deps >> s2 node deps)
|
|
|
261
|
+
|
|
|
262
|
+-- | Where should the dependency output end up?
|
|
|
263
|
+data DepOutput
|
|
|
264
|
+ = DepStreamHandle Handle
|
|
|
265
|
+ -- | The dependency information should be written to this 'Handle'.
|
|
|
266
|
+ | DepFileOutput FilePath
|
|
|
267
|
+ -- | The dependency information should be written to this 'FilePath'.
|
|
|
268
|
+
|
|
|
269
|
+-- | A 'DepStream' is an abstraction over how to output single elements
|
|
|
270
|
+-- of the dependency generation, e.g., a json object or one makefile rule.
|
|
|
271
|
+data DepStream = DepStream
|
|
|
272
|
+ { depStream_write :: String -> IO ()
|
|
|
273
|
+ , depStream_close :: IO ()
|
|
|
274
|
+ , depStream_onError :: IO ()
|
|
|
275
|
+ }
|
|
|
276
|
+
|
|
|
277
|
+initDepOutput :: MkDependOutput -> Maybe DepOutput
|
|
|
278
|
+initDepOutput = \ case
|
|
|
279
|
+ MkDepend_None -> Nothing
|
|
|
280
|
+ MkDepend_Default fp -> Just $ DepFileOutput fp
|
|
|
281
|
+ MkDepend_Explicit fp -> Just $ DepFileOutput fp
|
|
|
282
|
+ MkDepend_StdOut -> Just $ DepStreamHandle stdout
|
|
|
283
|
+
|
|
|
284
|
+initMakefileDepStream :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DepStream
|
|
|
285
|
+initMakefileDepStream _ _ _ (DepStreamHandle h) =
|
|
|
286
|
+ pure $ depStreamHandle h
|
|
|
287
|
+initMakefileDepStream logger tmpfs tmpdir (DepFileOutput fp) = do
|
|
|
288
|
+ files <- beginMkDependHS logger tmpfs tmpdir fp
|
|
|
289
|
+ pure $
|
|
|
290
|
+ DepStream
|
|
|
291
|
+ { depStream_write = hPutStrLn (mkd_tmp_hdl files)
|
|
|
292
|
+ , depStream_onError = do
|
|
|
293
|
+ hClose $ mkd_tmp_hdl files
|
|
|
294
|
+ case mkd_make_hdl files of
|
|
|
295
|
+ Nothing -> return ()
|
|
|
296
|
+ Just hdl -> hClose hdl
|
|
|
297
|
+ , depStream_close = endMkDependHS logger files
|
|
|
298
|
+ }
|
|
|
299
|
+
|
|
|
300
|
+initJsonDepStream :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DepStream
|
|
|
301
|
+initJsonDepStream _ _ _ (DepStreamHandle h) =
|
|
|
302
|
+ pure $ depStreamHandle h
|
|
|
303
|
+initJsonDepStream logger tmpfs tmpdir (DepFileOutput fp) = do
|
|
|
304
|
+ files <- initJsonDepFiles logger tmpfs tmpdir fp
|
|
|
305
|
+ pure $
|
|
|
306
|
+ DepStream
|
|
|
307
|
+ { depStream_write = hPutStrLn (json_tmp_hdl files)
|
|
|
308
|
+ , depStream_onError = hClose (json_tmp_hdl files)
|
|
|
309
|
+ , depStream_close = endJsonDepFiles logger files
|
|
|
310
|
+ }
|
|
|
311
|
+
|
|
|
312
|
+depStreamHandle :: Handle -> DepStream
|
|
|
313
|
+depStreamHandle h =
|
|
|
314
|
+ DepStream
|
|
|
315
|
+ { depStream_write =
|
|
|
316
|
+ \output -> do
|
|
|
317
|
+ hPutStrLn h output
|
|
|
318
|
+ hFlush h -- We flush to ensure the output is streamed per line.
|
|
|
319
|
+ , depStream_close = pure ()
|
|
|
320
|
+ , depStream_onError = pure ()
|
|
|
321
|
+ }
|
|
|
322
|
+
|
|
228
|
323
|
-----------------------------------------------------------------
|
|
229
|
324
|
--
|
|
230
|
325
|
-- processDeps
|
| ... |
... |
@@ -233,7 +328,7 @@ data DepSink = DepSink |
|
233
|
328
|
|
|
234
|
329
|
processDeps :: HscEnv
|
|
235
|
330
|
-> UniqSet ModuleName -- ^ Excludes
|
|
236
|
|
- -> [DepSink]
|
|
|
331
|
+ -> DepSink
|
|
237
|
332
|
-> SCC ModuleGraphNode
|
|
238
|
333
|
-> IO ()
|
|
239
|
334
|
-- Write suitable dependencies to handle
|
| ... |
... |
@@ -267,7 +362,7 @@ processDeps _ _ _ (AcyclicSCC (ModuleNode _ (ModuleNodeFixed {}))) |
|
267
|
362
|
-- No dependencies needed for fixed modules (already compiled)
|
|
268
|
363
|
= return ()
|
|
269
|
364
|
|
|
270
|
|
-processDeps hsc_env0 excl_mods sinks (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node))) = do
|
|
|
365
|
+processDeps hsc_env0 excl_mods sink (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node))) = do
|
|
271
|
366
|
pp <- preprocessor
|
|
272
|
367
|
let
|
|
273
|
368
|
dep_node = mkDepNode pp
|
| ... |
... |
@@ -279,7 +374,7 @@ processDeps hsc_env0 excl_mods sinks (AcyclicSCC (ModuleNode _ (ModuleNodeCompil |
|
279
|
374
|
|
|
280
|
375
|
if null missing_dep_errs
|
|
281
|
376
|
then do
|
|
282
|
|
- traverse_ (\ sink -> ds_writeDependency sink dep_node deps) sinks
|
|
|
377
|
+ ds_writeDependency sink dep_node deps
|
|
283
|
378
|
else do
|
|
284
|
379
|
let sec = initSourceErrorContext (hsc_dflags local_hsc_env)
|
|
285
|
380
|
throwErrors sec (mkMessages (listToBag missing_dep_errs))
|
| ... |
... |
@@ -380,17 +475,16 @@ find_preprocessor d = do |
|
380
|
475
|
--
|
|
381
|
476
|
-----------------------------------------------------------------
|
|
382
|
477
|
|
|
383
|
|
-initFileDepWriter :: Logger -> OsPath -> TmpFs -> DynFlags -> FilePath -> IO DependencyWriter
|
|
384
|
|
-initFileDepWriter logger root tmpfs dflags makefile = do
|
|
385
|
|
- files <- beginMkDependHS logger tmpfs dflags makefile
|
|
|
478
|
+initFileDepWriter :: Logger -> OsPath -> TmpFs -> TempDir -> [OsPath] -> DepOutput -> IO DependencyWriter
|
|
|
479
|
+initFileDepWriter logger root tmpfs tmpdir suffixes output = do
|
|
|
480
|
+ stream <- initMakefileDepStream logger tmpfs tmpdir output
|
|
386
|
481
|
pure DependencyWriter
|
|
387
|
|
- { dw_beginWriter = do
|
|
388
|
|
- pure $ DepSink $ \ node deps -> do
|
|
389
|
|
- writeDependencies root (mkd_tmp_hdl files) suffixes node deps
|
|
390
|
|
- , dw_endWriter = endMkDependHS logger files
|
|
|
482
|
+ { dw_sink =
|
|
|
483
|
+ DepSink $ \ node deps -> do
|
|
|
484
|
+ traverse_ (depStream_write stream) (dependencyRules root suffixes node deps)
|
|
|
485
|
+ , dw_endWriter = depStream_close stream
|
|
|
486
|
+ , dw_onError = depStream_onError stream
|
|
391
|
487
|
}
|
|
392
|
|
- where
|
|
393
|
|
- suffixes = map unsafeEncodeUtf (depSuffixes dflags)
|
|
394
|
488
|
|
|
395
|
489
|
data MkDepFiles
|
|
396
|
490
|
= MkDep { mkd_make_file :: FilePath, -- Name of the makefile
|
| ... |
... |
@@ -398,11 +492,11 @@ data MkDepFiles |
|
398
|
492
|
mkd_tmp_file :: FilePath, -- Name of the temporary file
|
|
399
|
493
|
mkd_tmp_hdl :: Handle } -- Handle of the open temporary file
|
|
400
|
494
|
|
|
401
|
|
-beginMkDependHS :: Logger -> TmpFs -> DynFlags -> FilePath -> IO MkDepFiles
|
|
402
|
|
-beginMkDependHS logger tmpfs dflags makefile = do
|
|
|
495
|
+beginMkDependHS :: Logger -> TmpFs -> TempDir -> FilePath -> IO MkDepFiles
|
|
|
496
|
+beginMkDependHS logger tmpfs tmpdir makefile = do
|
|
403
|
497
|
-- open a new temp file in which to stuff the dependency info
|
|
404
|
498
|
-- as we go along.
|
|
405
|
|
- tmp_file <- newTempName logger tmpfs (tmpDir dflags) TFL_CurrentModule "dep"
|
|
|
499
|
+ tmp_file <- newTempName logger tmpfs tmpdir TFL_CurrentModule "dep"
|
|
406
|
500
|
tmp_hdl <- openFile tmp_file WriteMode
|
|
407
|
501
|
|
|
408
|
502
|
-- open the makefile
|
| ... |
... |
@@ -443,16 +537,15 @@ beginMkDependHS logger tmpfs dflags makefile = do |
|
443
|
537
|
return (MkDep { mkd_make_file = makefile, mkd_make_hdl = mb_make_hdl,
|
|
444
|
538
|
mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl})
|
|
445
|
539
|
|
|
446
|
|
-writeDependencies ::
|
|
|
540
|
+dependencyRules ::
|
|
447
|
541
|
OsPath ->
|
|
448
|
|
- Handle ->
|
|
449
|
542
|
[OsString] ->
|
|
450
|
543
|
-- ^ Suffixes
|
|
451
|
544
|
DepNode ->
|
|
452
|
545
|
[Dep] ->
|
|
453
|
|
- IO ()
|
|
454
|
|
-writeDependencies root hdl suffixes node deps =
|
|
455
|
|
- traverse_ write tasks
|
|
|
546
|
+ [String]
|
|
|
547
|
+dependencyRules root suffixes node deps =
|
|
|
548
|
+ map rule tasks
|
|
456
|
549
|
where
|
|
457
|
550
|
tasks = source_dep : boot_dep ++ concatMap import_dep deps
|
|
458
|
551
|
|
| ... |
... |
@@ -479,7 +572,7 @@ writeDependencies root hdl suffixes node deps = |
|
479
|
572
|
|
|
480
|
573
|
DepCpp {dep_path} -> [(obj_files, dep_path)]
|
|
481
|
574
|
|
|
482
|
|
- write (from, to) = writeDependency root hdl from to
|
|
|
575
|
+ rule (from, to) = dependencyRule root from to
|
|
483
|
576
|
|
|
484
|
577
|
obj_files = suffixed dn_obj
|
|
485
|
578
|
|
| ... |
... |
@@ -488,18 +581,18 @@ writeDependencies root hdl suffixes node deps = |
|
488
|
581
|
DepNode {dn_src, dn_obj, dn_hi, dn_mod} = node
|
|
489
|
582
|
|
|
490
|
583
|
-----------------------------
|
|
491
|
|
-writeDependency :: OsPath -> Handle -> [OsPath] -> OsPath -> IO ()
|
|
|
584
|
+dependencyRule :: OsPath -> [OsPath] -> OsPath -> String
|
|
492
|
585
|
-- (writeDependency r h [t1,t2] dep) writes to handle h the dependency
|
|
493
|
586
|
-- t1 t2 : dep
|
|
494
|
|
-writeDependency root hdl targets dep
|
|
495
|
|
- = do let -- We need to avoid making deps on
|
|
496
|
|
- -- c:/foo/...
|
|
497
|
|
- -- on Windows as make gets confused by the :
|
|
498
|
|
- -- Making relative deps avoids some instances of this.
|
|
499
|
|
- dep' = OsPath.makeRelative root dep
|
|
500
|
|
- forOutput = escapeSpaces . reslash Forwards . unsafeDecodeUtf . OsPath.normalise
|
|
501
|
|
- output = unwords (map forOutput targets) ++ " : " ++ forOutput dep'
|
|
502
|
|
- hPutStrLn hdl output
|
|
|
587
|
+dependencyRule root targets dep
|
|
|
588
|
+ = let -- We need to avoid making deps on
|
|
|
589
|
+ -- c:/foo/...
|
|
|
590
|
+ -- on Windows as make gets confused by the :
|
|
|
591
|
+ -- Making relative deps avoids some instances of this.
|
|
|
592
|
+ dep' = OsPath.makeRelative root dep
|
|
|
593
|
+ forOutput = escapeSpaces . reslash Forwards . unsafeDecodeUtf . OsPath.normalise
|
|
|
594
|
+ in
|
|
|
595
|
+ unwords (map forOutput targets) ++ " : " ++ forOutput dep'
|
|
503
|
596
|
|
|
504
|
597
|
-----------------------------
|
|
505
|
598
|
insertSuffixes
|
| ... |
... |
@@ -554,16 +647,27 @@ endMkDependHS logger |
|
554
|
647
|
--
|
|
555
|
648
|
-----------------------------------------------------------------
|
|
556
|
649
|
|
|
557
|
|
-initJsonDepWriter :: Logger -> TmpFs -> DynFlags -> FilePath -> IO DependencyWriter
|
|
558
|
|
-initJsonDepWriter logger tmpfs dflags output = do
|
|
559
|
|
- json_var <- mkJsonOutput logger tmpfs dflags output initDepJson
|
|
560
|
|
- pure DependencyWriter
|
|
561
|
|
- { dw_beginWriter =
|
|
562
|
|
- pure $ DepSink $ \ node deps ->
|
|
563
|
|
- writeHomeUnitDeps json_var node deps
|
|
564
|
|
- , dw_endWriter = do
|
|
565
|
|
- writeJsonOutput logger json_var
|
|
566
|
|
- }
|
|
|
650
|
+initJsonDepWriter :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DependencyWriter
|
|
|
651
|
+initJsonDepWriter logger tmpfs tmpdir output = do
|
|
|
652
|
+ stream <- initJsonDepStream logger tmpfs tmpdir output
|
|
|
653
|
+ json_var <- mkJsonOutput stream
|
|
|
654
|
+ pure
|
|
|
655
|
+ DependencyWriter
|
|
|
656
|
+ { dw_sink =
|
|
|
657
|
+ DepSink $ \node deps -> do
|
|
|
658
|
+ extUnits <- readJson json_var
|
|
|
659
|
+ let
|
|
|
660
|
+ (extUnitsDeps, payload) = homeUnitDeps node deps
|
|
|
661
|
+ newExtUnits = Set.difference extUnitsDeps (externalDeps extUnits)
|
|
|
662
|
+ -- Write new external unit deps before the module that uses them
|
|
|
663
|
+ for_ newExtUnits $ \extUnit -> do
|
|
|
664
|
+ depStream_write stream (renderJsonl extUnit)
|
|
|
665
|
+ depStream_write stream (renderJsonl payload)
|
|
|
666
|
+ -- Cache external deps we already reported in the past
|
|
|
667
|
+ updateJson json_var $ cacheExternalDepsJson newExtUnits
|
|
|
668
|
+ , dw_endWriter = depStream_close stream
|
|
|
669
|
+ , dw_onError = depStream_onError stream
|
|
|
670
|
+ }
|
|
567
|
671
|
|
|
568
|
672
|
--------------------------------------------------------------------------------
|
|
569
|
673
|
-- Output interface for json dumps
|
| ... |
... |
@@ -572,65 +676,64 @@ initJsonDepWriter logger tmpfs dflags output = do |
|
572
|
676
|
-- The flag @-dep-json@ add an additional output target for dependency
|
|
573
|
677
|
-- diagnostics.
|
|
574
|
678
|
--
|
|
575
|
|
--- Contains temporary file locations to stream output to.
|
|
576
|
|
-data JsonOutput =
|
|
577
|
|
- JsonOutput {
|
|
578
|
|
- -- | This ref is updated in @processDeps@ incrementally, using a
|
|
579
|
|
- -- flag-specific type.
|
|
580
|
|
- json_ref :: IORef DepJson,
|
|
581
|
|
-
|
|
582
|
|
- -- | The output file path specified as argument to the flag.
|
|
583
|
|
- json_path :: FilePath,
|
|
584
|
|
- -- | Name of the temporary file
|
|
585
|
|
- json_tmp_file :: FilePath,
|
|
586
|
|
- -- | Handle of the open temporary file
|
|
587
|
|
- json_tmp_hdl :: Handle
|
|
|
679
|
+data JsonOutput
|
|
|
680
|
+ = JsonOutput
|
|
|
681
|
+ { json_ref :: IORef DepJson
|
|
|
682
|
+ -- ^ This ref is updated in @processDeps@ incrementally, using a
|
|
|
683
|
+ -- flag-specific type.
|
|
|
684
|
+ , json_dep_stream :: DepStream
|
|
588
|
685
|
}
|
|
589
|
686
|
|
|
590
|
|
--- | Set up json generation for `ghc -M`.
|
|
591
|
|
---
|
|
592
|
|
--- Creates a temporary file that we write the dependency information to.
|
|
593
|
|
--- The temporary file is written to the final output file path in 'writeJsonOutput'.
|
|
594
|
|
-mkJsonOutput ::
|
|
595
|
|
- Logger ->
|
|
596
|
|
- TmpFs ->
|
|
597
|
|
- DynFlags ->
|
|
598
|
|
- FilePath ->
|
|
599
|
|
- IO (IORef DepJson) ->
|
|
600
|
|
- IO JsonOutput
|
|
601
|
|
-mkJsonOutput logger tmpfs dflags json_path mk_ref = do
|
|
602
|
|
- json_ref <- mk_ref
|
|
603
|
|
- -- open a new temp file in which to stuff the dependency info
|
|
|
687
|
+data JsonDepFiles = JsonDepFiles
|
|
|
688
|
+ { json_path :: FilePath
|
|
|
689
|
+ -- ^ The output file path specified as argument to the flag.
|
|
|
690
|
+ , json_tmp_file :: FilePath
|
|
|
691
|
+ -- ^ Name of the temporary file
|
|
|
692
|
+ , json_tmp_hdl :: Handle
|
|
|
693
|
+ -- ^ Handle of the open temporary file
|
|
|
694
|
+ }
|
|
|
695
|
+
|
|
|
696
|
+initJsonDepFiles :: Logger -> TmpFs -> TempDir -> FilePath -> IO JsonDepFiles
|
|
|
697
|
+initJsonDepFiles logger tmpfs tmpdir path = do
|
|
|
698
|
+ -- open a new temp file in which to stuff the dependency info into
|
|
604
|
699
|
-- as we go along.
|
|
605
|
|
- json_tmp_file <- newTempName logger tmpfs (tmpDir dflags) TFL_CurrentModule "dep"
|
|
|
700
|
+ json_tmp_file <- newTempName logger tmpfs tmpdir TFL_CurrentModule "dep-json"
|
|
606
|
701
|
json_tmp_hdl <- openFile json_tmp_file WriteMode
|
|
607
|
|
-
|
|
608
|
|
- pure JsonOutput {json_ref, json_path, json_tmp_file, json_tmp_hdl}
|
|
609
|
|
-
|
|
610
|
|
-updateJson :: JsonOutput -> (DepJson -> DepJson) -> IO ()
|
|
611
|
|
-updateJson JsonOutput {json_ref} f = modifyIORef' json_ref f
|
|
612
|
|
-
|
|
613
|
|
-readJson :: JsonOutput -> IO DepJson
|
|
614
|
|
-readJson JsonOutput {json_ref} = readIORef json_ref
|
|
|
702
|
+ pure $ JsonDepFiles
|
|
|
703
|
+ { json_path = path
|
|
|
704
|
+ , json_tmp_file = json_tmp_file
|
|
|
705
|
+ , json_tmp_hdl = json_tmp_hdl
|
|
|
706
|
+ }
|
|
615
|
707
|
|
|
616
|
708
|
-- | Finalise the json generation by copying the temporary file to the final destination.
|
|
617
|
709
|
-- Avoids partial writes in the case of an error.
|
|
618
|
|
-writeJsonOutput ::
|
|
619
|
|
- Logger ->
|
|
620
|
|
- JsonOutput ->
|
|
621
|
|
- IO ()
|
|
622
|
|
-writeJsonOutput logger JsonOutput {json_path, json_tmp_file, json_tmp_hdl} = do
|
|
|
710
|
+endJsonDepFiles :: Logger -> JsonDepFiles -> IO ()
|
|
|
711
|
+endJsonDepFiles logger JsonDepFiles {json_path, json_tmp_file, json_tmp_hdl} = do
|
|
623
|
712
|
hClose json_tmp_hdl
|
|
624
|
713
|
-- Copy the new jsonl in place
|
|
625
|
714
|
showPass logger "Installing new json output"
|
|
626
|
715
|
SysTools.copyFile json_tmp_file json_path
|
|
627
|
716
|
|
|
|
717
|
+-- | Set up json generation for `ghc -M`.
|
|
|
718
|
+mkJsonOutput ::
|
|
|
719
|
+ DepStream ->
|
|
|
720
|
+ IO JsonOutput
|
|
|
721
|
+mkJsonOutput dep_stream = do
|
|
|
722
|
+ json_ref <- newIORef (DepJson Set.empty)
|
|
|
723
|
+ pure JsonOutput {json_ref, json_dep_stream = dep_stream }
|
|
|
724
|
+
|
|
|
725
|
+updateJson :: JsonOutput -> (DepJson -> DepJson) -> IO ()
|
|
|
726
|
+updateJson JsonOutput {json_ref} f = modifyIORef' json_ref f
|
|
|
727
|
+
|
|
|
728
|
+readJson :: JsonOutput -> IO DepJson
|
|
|
729
|
+readJson JsonOutput {json_ref} = readIORef json_ref
|
|
|
730
|
+
|
|
628
|
731
|
--------------------------------------------------------------------------------
|
|
629
|
732
|
-- Output helpers
|
|
630
|
733
|
|
|
631
|
|
-writeJsonl :: ToJson a => JsonOutput -> a -> IO ()
|
|
632
|
|
-writeJsonl JsonOutput {json_tmp_hdl} output = do
|
|
633
|
|
- hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON $ json output)
|
|
|
734
|
+renderJsonl :: ToJson a => a -> String
|
|
|
735
|
+renderJsonl output = do
|
|
|
736
|
+ showSDocUnsafe $ renderJSON $ json output
|
|
634
|
737
|
|
|
635
|
738
|
--------------------------------------------------------------------------------
|
|
636
|
739
|
-- Payload for -dep-json
|
| ... |
... |
@@ -788,9 +891,6 @@ instance ToJson ExtUnitDep where |
|
788
|
891
|
where
|
|
789
|
892
|
showPackageId (DPackageId (PackageId pid)) = unpackFS pid
|
|
790
|
893
|
|
|
791
|
|
-initDepJson :: IO (IORef DepJson)
|
|
792
|
|
-initDepJson = newIORef $ DepJson Set.empty
|
|
793
|
|
-
|
|
794
|
894
|
cacheExternalDepsJson :: Set.Set ExtUnitDep -> DepJson -> DepJson
|
|
795
|
895
|
cacheExternalDepsJson extUnits (DepJson e0) =
|
|
796
|
896
|
DepJson
|
| ... |
... |
@@ -798,19 +898,10 @@ cacheExternalDepsJson extUnits (DepJson e0) = |
|
798
|
898
|
Set.union e0 extUnits
|
|
799
|
899
|
}
|
|
800
|
900
|
|
|
801
|
|
-writeHomeUnitDeps :: JsonOutput -> DepNode -> [Dep] -> IO ()
|
|
802
|
|
-writeHomeUnitDeps deps_json DepNode {..} deps = do
|
|
803
|
|
- extUnits <- readJson deps_json
|
|
804
|
|
- let newExtUnits = Set.difference extUnitsDeps (externalDeps extUnits)
|
|
805
|
|
- -- Write new external unit deps before the module that uses them
|
|
806
|
|
- for_ newExtUnits $ \ extUnit -> do
|
|
807
|
|
- writeJsonl deps_json extUnit
|
|
808
|
|
- writeJsonl deps_json payload
|
|
809
|
|
- -- Cache external deps we already reported in the past
|
|
810
|
|
- updateJson deps_json $ cacheExternalDepsJson newExtUnits
|
|
|
901
|
+homeUnitDeps :: Foldable t => DepNode -> t Dep -> (Set.Set ExtUnitDep, HomeUnitDep)
|
|
|
902
|
+homeUnitDeps DepNode {..} deps =
|
|
|
903
|
+ foldl' go (Set.empty, initial_node_data) deps
|
|
811
|
904
|
where
|
|
812
|
|
- (extUnitsDeps, payload) = foldl' go (Set.empty, initial_node_data) deps
|
|
813
|
|
-
|
|
814
|
905
|
initial_node_data =
|
|
815
|
906
|
HomeUnitDep
|
|
816
|
907
|
{ unitId = dn_unitId
|