Hannes Siebenhandl pushed to branch wip/fendor/dep-stdout at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • changelog.d/make-depends-streaming-output
    1
    +section: compiler
    
    2
    +synopsis: Allow streaming `ghc -M` output to stdout
    
    3
    +issues: #27937
    
    4
    +mrs: !16830
    
    5
    +description: {
    
    6
    +    Add option `-dep-makefile-stdout` and `-dep-json-stdout` which stream the dependency generation
    
    7
    +    output to stdout.
    
    8
    +
    
    9
    +    The Makefile and json dependency generation can stream to separate targets, e.g. `-dep-makefile target.Makefile -dep-json-stdout`
    
    10
    +    streams the JSON Lines output to stdout and the Makefile to `target.Makefile`.
    
    11
    +
    
    12
    +    If `-dep-makefile-stdout`, `-dep-json` or `-dep-json-stdout` is given, we no longer generate a Makefile by default, but only
    
    13
    +    if the user explicitly requests the Makefile dependency generation.
    
    14
    +
    
    15
    +    The output from a particular backend can be disabled via `-no-dep-json` and `-no-dep-makefile` respectively.
    
    16
    +}

  • compiler/GHC/Driver/DynFlags.hs
    ... ... @@ -73,6 +73,9 @@ module GHC.Driver.DynFlags (
    73 73
             IncludeSpecs(..), addGlobalInclude, addQuoteInclude, flattenIncludes,
    
    74 74
             addImplicitQuoteInclude,
    
    75 75
     
    
    76
    +        -- * `ghc -M` specification
    
    77
    +        MkDependOutput(..),
    
    78
    +
    
    76 79
             -- * SDoc
    
    77 80
             initSDocContext, initDefaultSDocContext,
    
    78 81
             initPromotionTickContext,
    
    ... ... @@ -350,8 +353,8 @@ data DynFlags = DynFlags {
    350 353
         -- ^ External plugins loaded from shared libraries
    
    351 354
     
    
    352 355
       --  For ghc -M
    
    353
    -  depMakefile           :: Maybe FilePath,
    
    354
    -  depJson               :: Maybe FilePath,
    
    356
    +  depMakefile           :: MkDependOutput,
    
    357
    +  depJson               :: MkDependOutput,
    
    355 358
       depIncludePkgDeps     :: Bool,
    
    356 359
       depIncludeCppDeps     :: Bool,
    
    357 360
       depExcludeMods        :: [ModuleName],
    
    ... ... @@ -694,8 +697,8 @@ defaultDynFlags mySettings =
    694 697
             llvmOptLevel            = 0,
    
    695 698
     
    
    696 699
             -- ghc -M values
    
    697
    -        depMakefile       = Just "Makefile",
    
    698
    -        depJson           = Nothing,
    
    700
    +        depMakefile       = MkDepend_Default "Makefile",
    
    701
    +        depJson           = MkDepend_None,
    
    699 702
             depIncludePkgDeps = False,
    
    700 703
             depIncludeCppDeps = False,
    
    701 704
             depExcludeMods    = [],
    
    ... ... @@ -801,6 +804,15 @@ flattenExtensionFlags ml = foldr g defaultExtensionFlags
    801 804
               g (Off f) flags = EnumSet.delete f flags
    
    802 805
               defaultExtensionFlags = EnumSet.fromList (languageExtensions ml)
    
    803 806
     
    
    807
    +-- -----------------------------------------------------------------------------
    
    808
    +-- -M -dep-*
    
    809
    +
    
    810
    +data MkDependOutput
    
    811
    +  = MkDepend_None
    
    812
    +  | MkDepend_Default !FilePath
    
    813
    +  | MkDepend_Explicit !FilePath
    
    814
    +  | MkDepend_StdOut
    
    815
    +
    
    804 816
     -- -----------------------------------------------------------------------------
    
    805 817
     -- -jN
    
    806 818
     
    

  • compiler/GHC/Driver/MakeFile.hs
    ... ... @@ -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
    

  • compiler/GHC/Driver/Session.hs
    ... ... @@ -790,16 +790,44 @@ addOptCmmP f = alterToolSettings $ \s -> s
    790 790
               }
    
    791 791
     
    
    792 792
     setDepMakefile :: FilePath -> DynFlags -> DynFlags
    
    793
    -setDepMakefile f d = d { depMakefile = Just f }
    
    793
    +setDepMakefile f d = d { depMakefile = MkDepend_Explicit f
    
    794
    +                       -- depJson has no default value, we add this for symmetry with `setDepJson`
    
    795
    +                       -- and if anything changes in the future, then the code is already here :)
    
    796
    +                       , depJson = unsetMkDependOutputIfDefault (depJson d)
    
    797
    +                       }
    
    798
    +
    
    799
    +setDepMakefileStdout :: DynFlags -> DynFlags
    
    800
    +setDepMakefileStdout d = d { depMakefile = MkDepend_StdOut
    
    801
    +                           -- depJson has no default value, we add this for symmetry with `setDepJson`
    
    802
    +                           -- and if anything changes in the future, then the code is already here :)
    
    803
    +                           , depJson = unsetMkDependOutputIfDefault (depJson d)
    
    804
    +                           }
    
    794 805
     
    
    795 806
     unsetDepMakefile :: DynFlags -> DynFlags
    
    796
    -unsetDepMakefile d = d { depMakefile = Nothing }
    
    807
    +unsetDepMakefile d = d { depMakefile = MkDepend_None }
    
    797 808
     
    
    798 809
     setDepJson :: FilePath -> DynFlags -> DynFlags
    
    799
    -setDepJson f d = d { depJson = Just f }
    
    810
    +setDepJson f d = d { depJson = MkDepend_Explicit f
    
    811
    +                   -- We generate a Makefile by default, but if a user requests json
    
    812
    +                   -- output, then they most likely don't need a Makefile.
    
    813
    +                   -- Thus, we only generate it if the user requests it.
    
    814
    +                   , depMakefile = unsetMkDependOutputIfDefault (depMakefile d)
    
    815
    +                   }
    
    816
    +
    
    817
    +setDepJsonStdout :: DynFlags -> DynFlags
    
    818
    +setDepJsonStdout d = d { depJson = MkDepend_StdOut
    
    819
    +                       -- We generate a Makefile by default, but if a user requests json
    
    820
    +                       -- output, then they most likely don't need a Makefile.
    
    821
    +                       -- Thus, we only generate it if the user requests it.
    
    822
    +                       , depMakefile = unsetMkDependOutputIfDefault (depMakefile d)
    
    823
    +                       }
    
    800 824
     
    
    801 825
     unsetDepJson :: DynFlags -> DynFlags
    
    802
    -unsetDepJson d = d { depJson = Nothing }
    
    826
    +unsetDepJson d = d { depJson = MkDepend_None }
    
    827
    +
    
    828
    +unsetMkDependOutputIfDefault :: MkDependOutput -> MkDependOutput
    
    829
    +unsetMkDependOutputIfDefault (MkDepend_Default _) = MkDepend_None
    
    830
    +unsetMkDependOutputIfDefault mkDepend_output      = mkDepend_output
    
    803 831
     
    
    804 832
     setDepIncludeCppDeps :: Bool -> DynFlags -> DynFlags
    
    805 833
     setDepIncludeCppDeps b d = d { depIncludeCppDeps = b }
    
    ... ... @@ -1272,8 +1300,10 @@ dynamic_flags_deps = [
    1272 1300
       , make_ord_flag defGhcFlag "dep-suffix"              (hasArg addDepSuffix)
    
    1273 1301
       , make_ord_flag defGhcFlag "dep-makefile"            (hasArg setDepMakefile)
    
    1274 1302
       , make_ord_flag defGhcFlag "no-dep-makefile"         (noArg unsetDepMakefile)
    
    1303
    +  , make_ord_flag defGhcFlag "dep-makefile-stdout"     (noArg setDepMakefileStdout)
    
    1275 1304
       , make_ord_flag defGhcFlag "dep-json"                (hasArg setDepJson)
    
    1276 1305
       , make_ord_flag defGhcFlag "no-dep-json"             (noArg unsetDepJson)
    
    1306
    +  , make_ord_flag defGhcFlag "dep-json-stdout"         (noArg setDepJsonStdout)
    
    1277 1307
       , make_ord_flag defGhcFlag "include-cpp-deps"
    
    1278 1308
             (noArg (setDepIncludeCppDeps True))
    
    1279 1309
       , make_ord_flag defGhcFlag "include-pkg-deps"
    

  • docs/users_guide/separate_compilation.rst
    ... ... @@ -1511,6 +1511,16 @@ generation are:
    1511 1511
     
    
    1512 1512
         Introduced in GHC 10.1, ``-no-dep-makefile`` disables the generation of a Makefile altogether.
    
    1513 1513
     
    
    1514
    +.. ghc-flag:: -dep-makefile-stdout
    
    1515
    +    :shortdesc: Write dependency generation to stdout using makefile format
    
    1516
    +    :type: dynamic
    
    1517
    +    :reverse: -no-dep-makefile
    
    1518
    +    :category: redirect-output
    
    1519
    +
    
    1520
    +    :since: 10.1
    
    1521
    +
    
    1522
    +    Stream the dependency generation of :ghc-flag:`-dep-makefile ⟨file⟩` to stdout instead of ⟨file⟩.
    
    1523
    +
    
    1514 1524
     .. ghc-flag:: -dep-json ⟨file⟩
    
    1515 1525
         :shortdesc: Write dependencies to ⟨file⟩ as a JSON Lines format
    
    1516 1526
         :type: dynamic
    
    ... ... @@ -1540,8 +1550,19 @@ generation are:
    1540 1550
     
    
    1541 1551
         ``-no-dep-json`` disables the generation of the JSON file.
    
    1542 1552
     
    
    1543
    -    By default, :ghc-flag:`-dep-makefile ⟨file⟩` is enabled even if :ghc-flag:`-dep-json ⟨file⟩` is given.
    
    1544
    -    Disable the generation of any Makefile via ``-no-dep-makefile``.
    
    1553
    +    :ghc-flag:`-dep-json ⟨file⟩` and :ghc-flag:`-dep-json-stdout` disable the default generation of a Makefile.
    
    1554
    +    To generate a Makefile for the dependency information, explicitly use :ghc-flag:`-dep-makefile ⟨file⟩` or
    
    1555
    +    :ghc-flag:`-dep-makefile-stdout`.
    
    1556
    +
    
    1557
    +.. ghc-flag:: -dep-json-stdout
    
    1558
    +    :shortdesc: Write dependency generation to stdout using JSON Lines format
    
    1559
    +    :type: dynamic
    
    1560
    +    :reverse: -no-dep-json
    
    1561
    +    :category: redirect-output
    
    1562
    +
    
    1563
    +    :since: 10.1
    
    1564
    +
    
    1565
    +    Stream the dependency generation of :ghc-flag:`-dep-json ⟨file⟩` to stdout instead of ⟨file⟩.
    
    1545 1566
     
    
    1546 1567
     .. ghc-flag:: -dep-suffix ⟨suffix⟩
    
    1547 1568
         :shortdesc: Make dependencies that declare that files with suffix