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

Commits:

1 changed file:

Changes:

  • compiler/GHC/Driver/MakeFile.hs
    ... ... @@ -196,36 +196,37 @@ doMkDependModuleGraph dflags module_graph = do
    196 196
     -- | A 'DepNode' is a direct translation of a 'ModuleGraphNode' that
    
    197 197
     -- can be serialised to a Makefile or json lines format.
    
    198 198
     data DepNode =
    
    199
    -  ModuleDepNode DepModuleNode [Dep]
    
    199
    +  ModuleDepNode HomeModuleNode [Dep]
    
    200 200
       -- ^ A dependency node of a home module with its dependencies
    
    201 201
     
    
    202 202
     -- | A home module node.
    
    203 203
     -- Everything we know about home modules that can be serialised.
    
    204
    -data DepModuleNode =
    
    205
    -  DepModuleNode
    
    206
    -    { dn_unitId :: UnitId
    
    204
    +data HomeModuleNode =
    
    205
    +  HomeModuleNode
    
    206
    +    { hmn_unitId :: UnitId
    
    207 207
         -- ^ 'UnitId' of the 'HomeUnitEnv' this home module belongs to.
    
    208
    -    , dn_packageName :: Maybe String
    
    208
    +    , hmn_packageName :: Maybe String
    
    209 209
         -- ^ Optional package name, used for @-XPackageImports@.
    
    210
    -    , dn_mod :: ModuleWithIsBoot
    
    210
    +    , hmn_mod :: ModuleWithIsBoot
    
    211 211
         -- ^ 'Module' of this home module with 'IsBootInterface' information.
    
    212
    -    , dn_src :: OsPath
    
    212
    +    , hmn_src :: OsPath
    
    213 213
         -- ^ Where is the source for this home module located.
    
    214
    -    , dn_obj :: OsPath
    
    214
    +    , hmn_obj :: OsPath
    
    215 215
         -- ^ Where is the object file for this home module located if it exists.
    
    216
    -    , dn_hi :: OsPath
    
    216
    +    , hmn_hi :: OsPath
    
    217 217
         -- ^ Where is the interface file for this home module located if it exists.
    
    218
    -    , dn_preprocessing :: PreprocessingNode
    
    218
    +    , hmn_preprocessing :: Maybe PreprocessingNode
    
    219 219
         -- ^ Is there a preprocessor that needs to be run for this module before compilation.
    
    220
    -    , dn_includePkgDeps :: Bool
    
    220
    +    , hmn_includePkgDeps :: Bool
    
    221 221
         -- ^ When serialising this home module, should we include external package dependencies
    
    222 222
         -- (such as importing @Data.List@ from @base@) in the output?
    
    223 223
         }
    
    224 224
     
    
    225 225
     -- | Preprocessor that needs to be run before a module can be compiled.
    
    226 226
     data PreprocessingNode = PreprocessingNode
    
    227
    -  { pn_preprocessor :: Maybe String
    
    227
    +  { pn_preprocessor :: String
    
    228 228
       -- ^ The preprocessor.
    
    229
    +  , pn_arguments :: [String]
    
    229 230
       }
    
    230 231
     
    
    231 232
     -- | A dependency edge on another module, or cpp path.
    
    ... ... @@ -249,7 +250,7 @@ data Dep
    249 250
         -- as the 'DepModuleNode'.
    
    250 251
         }
    
    251 252
       | DepCpp
    
    252
    -    -- ^ A dependency on a CPP @#include@.
    
    253
    +    -- ^ A dependency on a CPP @#include@ in the Haskell source file, identified by the preprocessor.
    
    253 254
         { dep_path :: OsPath
    
    254 255
         -- ^ Path to the @#include@d path.
    
    255 256
         }
    
    ... ... @@ -307,7 +308,6 @@ processDeps _ _ (AcyclicSCC (ModuleNode _ (ModuleNodeFixed {})))
    307 308
       = return Nothing
    
    308 309
     
    
    309 310
     processDeps hsc_env0 excl_mods (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node))) = do
    
    310
    -  pp <- preprocessor
    
    311 311
       let
    
    312 312
         find_deps imps = do
    
    313 313
           cpp_deps <- find_cpp_deps
    
    ... ... @@ -318,7 +318,7 @@ processDeps hsc_env0 excl_mods (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node
    318 318
     
    
    319 319
       if null missing_dep_errs
    
    320 320
         then do
    
    321
    -      pure $ Just $ ModuleDepNode (mkDepNode pp) deps
    
    321
    +      pure $ Just $ ModuleDepNode mkDepNode deps
    
    322 322
         else do
    
    323 323
           let sec = initSourceErrorContext dflags
    
    324 324
           throwErrors sec (mkMessages (listToBag missing_dep_errs))
    
    ... ... @@ -328,25 +328,29 @@ processDeps hsc_env0 excl_mods (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node
    328 328
         -- separate dependencies and options.
    
    329 329
         -- Thus, we need to set the active unit id to the one of the module we are processing.
    
    330 330
         local_hsc_env = hscSetActiveUnitId (ms_unitid node) hsc_env0
    
    331
    -    unit_dflags = hsc_dflags local_hsc_env
    
    332 331
         dflags = ms_hspp_opts node
    
    333 332
         src_file = msHsFileOsPath node
    
    334
    -    mkDepNode preproc =
    
    335
    -      DepModuleNode
    
    336
    -        { dn_unitId = ms_unitid node
    
    337
    -        , dn_packageName = thisPackageName dflags
    
    338
    -        , dn_mod = GWIB (ms_mod node) (isBootSummary node)
    
    339
    -        , dn_src = src_file
    
    340
    -        , dn_obj = msObjFileOsPath node
    
    341
    -        , dn_hi = msHiFileOsPath node
    
    342
    -        , dn_preprocessing = preproc
    
    343
    -        , dn_includePkgDeps = depIncludePkgDeps unit_dflags
    
    333
    +    mkDepNode =
    
    334
    +      HomeModuleNode
    
    335
    +        { hmn_unitId = ms_unitid node
    
    336
    +        , hmn_packageName = thisPackageName dflags
    
    337
    +        , hmn_mod = GWIB (ms_mod node) (isBootSummary node)
    
    338
    +        , hmn_src = src_file
    
    339
    +        , hmn_obj = msObjFileOsPath node
    
    340
    +        , hmn_hi = msHiFileOsPath node
    
    341
    +        , hmn_preprocessing = preprocessor
    
    342
    +        , hmn_includePkgDeps = depIncludePkgDeps dflags
    
    344 343
             }
    
    345 344
     
    
    346
    -    preprocessor :: IO PreprocessingNode
    
    345
    +    preprocessor :: Maybe PreprocessingNode
    
    347 346
         preprocessor = do
    
    348
    -      pure PreprocessingNode
    
    349
    -        { pn_preprocessor = find_preprocessor dflags
    
    347
    +      guard (gopt Opt_Pp dflags)
    
    348
    +      let pp = pgm_F dflags
    
    349
    +      guard (not $ null pp)
    
    350
    +      let args = opt_F dflags
    
    351
    +      pure $ PreprocessingNode
    
    352
    +        { pn_preprocessor = pp
    
    353
    +        , pn_arguments = args
    
    350 354
             }
    
    351 355
     
    
    352 356
         -- Emit a dependency for each CPP import
    
    ... ... @@ -356,7 +360,7 @@ processDeps hsc_env0 excl_mods (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node
    356 360
         -- fails to parse, which may not be desirable (see #16616).
    
    357 361
         find_cpp_deps :: IO [Dep]
    
    358 362
         find_cpp_deps =
    
    359
    -      if depIncludeCppDeps unit_dflags
    
    363
    +      if depIncludeCppDeps dflags
    
    360 364
             then do
    
    361 365
               session <- Session <$> newIORef local_hsc_env
    
    362 366
               parsedMod <- reflectGhc (GHC.parseModule node) session
    
    ... ... @@ -402,13 +406,6 @@ findDependency hsc_env (L srcloc imp) = do
    402 406
         mod_name = ui_mod_name imp
    
    403 407
         is_boot  = ui_boot imp
    
    404 408
     
    
    405
    -find_preprocessor :: DynFlags -> Maybe String
    
    406
    -find_preprocessor d = do
    
    407
    -  guard (gopt Opt_Pp d)
    
    408
    -  let pp = pgm_F d
    
    409
    -  guard (not $ null pp)
    
    410
    -  Just pp
    
    411
    -
    
    412 409
     -----------------------------------------------------------------
    
    413 410
     --
    
    414 411
     --              beginMkDependHs
    
    ... ... @@ -486,7 +483,7 @@ writeDependencies ::
    486 483
       Handle ->
    
    487 484
       [OsString] ->
    
    488 485
       -- ^ Suffixes
    
    489
    -  DepModuleNode ->
    
    486
    +  HomeModuleNode ->
    
    490 487
       [Dep] ->
    
    491 488
       IO ()
    
    492 489
     writeDependencies root hdl suffixes node deps =
    
    ... ... @@ -496,12 +493,12 @@ writeDependencies root hdl suffixes node deps =
    496 493
     
    
    497 494
         -- Emit std dependency of the object(s) on the source file
    
    498 495
         -- Something like       A.o : A.hs
    
    499
    -    source_dep = (obj_files, dn_src)
    
    496
    +    source_dep = (obj_files, hmn_src)
    
    500 497
     
    
    501 498
         -- add dependency between objects and their corresponding .hi-boot
    
    502 499
         -- files if the module has a corresponding .hs-boot file (#14482)
    
    503
    -    boot_dep = case gwib_isBoot dn_mod of
    
    504
    -      IsBoot -> [([obj], hi) | (obj, hi) <- zip (suffixed (removeBootSuffix dn_obj)) (suffixed dn_hi)]
    
    500
    +    boot_dep = case gwib_isBoot hmn_mod of
    
    501
    +      IsBoot -> [([obj], hi) | (obj, hi) <- zip (suffixed (removeBootSuffix hmn_obj)) (suffixed hmn_hi)]
    
    505 502
           NotBoot -> []
    
    506 503
     
    
    507 504
         -- Add one dependency for each suffix;
    
    ... ... @@ -509,7 +506,7 @@ writeDependencies root hdl suffixes node deps =
    509 506
         --              A.x_o : B.x_hi
    
    510 507
         import_dep = \case
    
    511 508
           DepHi {dep_path, dep_local}
    
    512
    -        | dep_local || dn_includePkgDeps
    
    509
    +        | dep_local || hmn_includePkgDeps
    
    513 510
             -> [([obj], hi) | (obj, hi) <- zip obj_files (suffixed dep_path)]
    
    514 511
     
    
    515 512
             | otherwise
    
    ... ... @@ -519,11 +516,11 @@ writeDependencies root hdl suffixes node deps =
    519 516
     
    
    520 517
         write (from, to) = writeDependency root hdl from to
    
    521 518
     
    
    522
    -    obj_files = suffixed dn_obj
    
    519
    +    obj_files = suffixed hmn_obj
    
    523 520
     
    
    524 521
         suffixed f = insertSuffixes f suffixes
    
    525 522
     
    
    526
    -    DepModuleNode {dn_src, dn_obj, dn_hi, dn_mod, dn_includePkgDeps} = node
    
    523
    +    HomeModuleNode {hmn_src, hmn_obj, hmn_hi, hmn_mod, hmn_includePkgDeps} = node
    
    527 524
     
    
    528 525
     -----------------------------
    
    529 526
     writeDependency :: OsPath -> Handle -> [OsPath] -> OsPath -> IO ()
    
    ... ... @@ -684,7 +681,7 @@ data HomeUnitDep = HomeUnitDep
    684 681
       , source :: OsPath
    
    685 682
       , imports :: ImportDeps
    
    686 683
       , cpp :: Set.Set OsPath
    
    687
    -  , preprocessor :: Maybe FilePath
    
    684
    +  , preprocessor :: Maybe PreprocessingNode
    
    688 685
       }
    
    689 686
     
    
    690 687
     addImport :: ImportDep -> HomeUnitDep -> HomeUnitDep
    
    ... ... @@ -759,8 +756,8 @@ instance ToJson HomeUnitDep where
    759 756
             , mkPair      "imports"      (importsObj imports)
    
    760 757
             , omitIfNull  "package-name" (maybe JSNull JSString packageName)
    
    761 758
             , mkPair      "source"       (JSString . showOsPath $ normalise source)
    
    762
    -        , omitIfEmpty "includes"     (strArray cpp showOsPath)
    
    763
    -        , omitIfNull  "preprocessor" (maybe JSNull JSString preprocessor)
    
    759
    +        , omitIfEmpty "includes"     (strArray (Set.toList cpp) showOsPath)
    
    760
    +        , omitIfNull  "preprocessor" (maybe JSNull preprocessorObj preprocessor)
    
    764 761
             ]
    
    765 762
        where
    
    766 763
         moduleKey :: Module -> String
    
    ... ... @@ -796,8 +793,16 @@ instance ToJson HomeUnitDep where
    796 793
             SpliceLevel -> "splice"
    
    797 794
             QuoteLevel -> "quote"
    
    798 795
     
    
    796
    +    preprocessorObj :: PreprocessingNode -> JsonDoc
    
    797
    +    preprocessorObj pp =
    
    798
    +      JSObject $
    
    799
    +        concat
    
    800
    +          [ mkPair      "command"   (JSString $ pn_preprocessor pp)
    
    801
    +          , omitIfEmpty "arguments" (strArray (pn_arguments pp) id)
    
    802
    +          ]
    
    803
    +
    
    799 804
         array values render = JSArray (fmap render (Set.toList values))
    
    800
    -    strArray values render = fmap (JSString . render) (Set.toList values)
    
    805
    +    strArray values render = fmap (JSString . render) values
    
    801 806
     
    
    802 807
         omitIfDefault :: String -> a -> (a -> Bool) -> (a -> JsonDoc) -> [(String, JsonDoc)]
    
    803 808
         omitIfDefault k v p toJson
    
    ... ... @@ -836,8 +841,8 @@ cacheExternalDepsJson extUnits (DepJson e0) =
    836 841
             Set.union e0 extUnits
    
    837 842
         }
    
    838 843
     
    
    839
    -writeHomeUnitDeps :: JsonOutput -> DepModuleNode -> [Dep] -> IO ()
    
    840
    -writeHomeUnitDeps deps_json DepModuleNode {..} deps = do
    
    844
    +writeHomeUnitDeps :: JsonOutput -> HomeModuleNode -> [Dep] -> IO ()
    
    845
    +writeHomeUnitDeps deps_json HomeModuleNode {..} deps = do
    
    841 846
       extUnits <- readJson deps_json
    
    842 847
       let newExtUnits = Set.difference extUnitsDeps (externalDeps extUnits)
    
    843 848
       -- Write new external unit deps before the module that uses them
    
    ... ... @@ -851,11 +856,11 @@ writeHomeUnitDeps deps_json DepModuleNode {..} deps = do
    851 856
     
    
    852 857
         initial_node_data =
    
    853 858
           HomeUnitDep
    
    854
    -        { unitId = dn_unitId
    
    855
    -        , packageName = dn_packageName
    
    856
    -        , modWithIsBoot = dn_mod
    
    857
    -        , source = dn_src
    
    858
    -        , preprocessor = pn_preprocessor dn_preprocessing
    
    859
    +        { unitId = hmn_unitId
    
    860
    +        , packageName = hmn_packageName
    
    861
    +        , modWithIsBoot = hmn_mod
    
    862
    +        , source = hmn_src
    
    863
    +        , preprocessor = hmn_preprocessing
    
    859 864
             , cpp = Set.empty
    
    860 865
             , imports = ImportDeps Map.empty
    
    861 866
             }
    
    ... ... @@ -865,7 +870,7 @@ writeHomeUnitDeps deps_json DepModuleNode {..} deps = do
    865 870
             | dep_local
    
    866 871
             -> (extDeps, addImport (mkImportDep dep_mod dep_level dep_boot) node_data)
    
    867 872
     
    
    868
    -        | dn_includePkgDeps
    
    873
    +        | hmn_includePkgDeps
    
    869 874
             , Just unit <- dep_unit
    
    870 875
             , let PackageName nameFS = unitPackageName unit
    
    871 876
                   name = unpackFS nameFS