Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC Commits: 175beaef by fendor at 2026-10-22T09:55:26+02:00 `ghc -M`: Add support for link nodes to json output `LinkNode`s are part of the module graph and indicate that a certain linking artefact is to be produced from other module graph nodes. We expose `LinkNode`s in the output of `ghc -M -dep-json`, allowing consumers to see when a linking artefact is to be produced. Only the JSON output format of `ghc -M` supports `LinkNode` as it is unclear how the Makefile output should be retrofitted to express the dependencies, and whether it would require a feature guard. Closes #28007 - - - - - 24 changed files: - + changelog.d/make-depends-link-nodes - compiler/GHC/Driver/MakeFile.hs - docs/users_guide/make-depends-json-schema-1_0.json - testsuite/tests/driver/mkdepend/all.T - + testsuite/tests/driver/mkdepend/mhu04/app/Main.hs - + testsuite/tests/driver/mkdepend/mhu04/app/Utils.hs - + testsuite/tests/driver/mkdepend/mhu04/lib/Lib.hs - + testsuite/tests/driver/mkdepend/mhu04/lib/Lib/Internal.hs - + testsuite/tests/driver/mkdepend/mhu04/unitApp - + testsuite/tests/driver/mkdepend/mhu04/unitLib - + testsuite/tests/driver/mkdepend/mhu05/app/Main.hs - + testsuite/tests/driver/mkdepend/mhu05/lib/Lib.hs - + testsuite/tests/driver/mkdepend/mhu05/lib/Lib/Internal.hs - + testsuite/tests/driver/mkdepend/mhu05/test/Spec.hs - + testsuite/tests/driver/mkdepend/mhu05/unitApp - + testsuite/tests/driver/mkdepend/mhu05/unitLib - + testsuite/tests/driver/mkdepend/mhu05/unitTest - + testsuite/tests/driver/mkdepend/mkdepend02.dep-json - + testsuite/tests/driver/mkdepend/mkdepend02/Main.hs - + testsuite/tests/driver/mkdepend/mkdepend_bkp_01/bkp01.bkp - + testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-json - + testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-makefile - + testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-json - + testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-makefile Changes: ===================================== changelog.d/make-depends-link-nodes ===================================== @@ -0,0 +1,12 @@ +section: compiler +synopsis: Expose link nodes in `ghc -M -dep-json` output +issues: #24384 +mrs: !16717 +description: { + Adds the option to output the dependency information of `ghc -M` in + JSON Lines format via the new flag `-dep-json <FILE>`. + + The output is richer than the `Makefile`, since it can encode additional information + such as `quote` and `splice` imports (see `-XExplicitLevelImports`). + By using JSON Lines, the output of `-dep-json` can be streamed efficiently. +} ===================================== compiler/GHC/Driver/MakeFile.hs ===================================== @@ -77,6 +77,7 @@ import System.IO import System.IO.Error (isEOFError) import System.OsPath as OsPath import System.OsString qualified as OsString +import qualified GHC.Unit.Env as UnitEnv ----------------------------------------------------------------- -- @@ -216,6 +217,19 @@ data Dep { dep_path :: OsPath } +data DepLink + = DepLink + { link_output :: !UnitId + , link_nodes :: ![DepLinkNode] + , link_type :: GhcLink + } + +data DepLinkNode + = DepLink_Unit !UnitId + | DepLink_Module !ModuleNameWithIsBoot !UnitId + | DepLink_HomeUnit {-# UNPACK #-} !UnitId + | DepLink_ExtUnit {-# UNPACK #-} !UnitId + data DependencyWriter = DependencyWriter { dw_beginWriter :: IO DepSink , dw_endWriter :: IO () @@ -223,6 +237,7 @@ data DependencyWriter = DependencyWriter data DepSink = DepSink { ds_writeDependency :: DepNode -> [Dep] -> IO () + , ds_writeLinkDependency :: DepLink -> IO () } ----------------------------------------------------------------- @@ -261,7 +276,22 @@ processDeps hsc_env _ _ (AcyclicSCC (InstantiationNode _uid node)) mkPlainErrorMsgEnvelope noSrcSpan $ GhcDriverMessage $ DriverInstantiationNodeInDependencyGeneration node -processDeps _ _ _ (AcyclicSCC (LinkNode {})) = return () +processDeps hsc_env _ sinks (AcyclicSCC (LinkNode keys unitId)) = do + let + toLinkNodeDep = \ case + NodeKey_Unit iu -> DepLink_Unit $ virtualUnitId iu + NodeKey_Module node -> DepLink_Module (mnkModuleName node) (mnkUnitId node) + NodeKey_Link uid -> DepLink_HomeUnit uid + NodeKey_ExternalUnit uid -> DepLink_ExtUnit uid + + linkNode = DepLink + { link_nodes = map toLinkNodeDep keys + , link_output = unitId + , link_type = ghcLink $ UnitEnv.ue_unitFlags unitId (hsc_unit_env hsc_env) + } + + traverse_ (\ sink -> ds_writeLinkDependency sink linkNode) sinks + processDeps _ _ _ (AcyclicSCC (UnitNode {})) = return () processDeps _ _ _ (AcyclicSCC (ModuleNode _ (ModuleNodeFixed {}))) -- No dependencies needed for fixed modules (already compiled) @@ -385,8 +415,13 @@ initFileDepWriter logger root tmpfs dflags makefile = do files <- beginMkDependHS logger tmpfs dflags makefile pure DependencyWriter { dw_beginWriter = do - pure $ DepSink $ \ node deps -> do - writeDependencies root (mkd_tmp_hdl files) suffixes node deps + pure DepSink + { ds_writeDependency = \ node deps -> do + writeDependencies root (mkd_tmp_hdl files) suffixes node deps + , ds_writeLinkDependency = \ _linkNode -> do + -- Link nodes are not supported + pure () + } , dw_endWriter = endMkDependHS logger files } where @@ -559,8 +594,13 @@ initJsonDepWriter logger tmpfs dflags output = do json_var <- mkJsonOutput logger tmpfs dflags output initDepJson pure DependencyWriter { dw_beginWriter = - pure $ DepSink $ \ node deps -> - writeHomeUnitDeps json_var node deps + pure DepSink + { ds_writeDependency = \ node deps -> + writeHomeUnitDeps json_var node deps + , ds_writeLinkDependency = \ link -> + -- Link nodes are not supported + writeLinkNode json_var link + } , dw_endWriter = do writeJsonOutput logger json_var } @@ -629,8 +669,12 @@ writeJsonOutput logger JsonOutput {json_path, json_tmp_file, json_tmp_hdl} = do -- Output helpers writeJsonl :: ToJson a => JsonOutput -> a -> IO () -writeJsonl JsonOutput {json_tmp_hdl} output = do - hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON $ json output) +writeJsonl deps_json output = do + writeJsonl' deps_json (json output) + +writeJsonl' :: JsonOutput -> JsonDoc -> IO () +writeJsonl' JsonOutput {json_tmp_hdl} output = do + hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON output) -------------------------------------------------------------------------------- -- Payload for -dep-json @@ -849,6 +893,65 @@ writeHomeUnitDeps deps_json DepNode {..} deps = do DepCpp {dep_path} -> (extDeps, addCpp dep_path node_data) +writeLinkNode :: JsonOutput -> DepLink -> IO () +writeLinkNode dep_json (DepLink uid deps linkType) = do + writeJsonl' dep_json $ + JSObject + [ ("unit-id", JSString $ unitIdString uid) + , ("link-type", linkTypeJson linkType) + , ("deps", JSArray $ concat + [ map renderMods (Map.toList allMods) + , map (JSString . unitIdString) (Set.toList units) + , map (JSString . unitIdString) (Set.toList homeUids) + , map (JSString . unitIdString) (Set.toList extUids) + ]) + ] + where + (extUids, homeUids, units, allMods) = foldl' go (Set.empty, Set.empty, Set.empty, Map.empty) deps + + renderMods (u, ms) = + JSObject + [ ("unit-id", JSString $ unitIdString u) + , ("modules", JSArray $ map moduleKey (Set.toList ms)) + ] + + + go (ext, homeUid, unit, mods) = \ case + DepLink_Module modName u -> + let + newMods = Map.insertWith (Set.union) u (Set.singleton modName) mods + in + (ext, homeUid, unit, newMods) + DepLink_Unit uid -> + (ext, homeUid, Set.insert uid unit, mods) + DepLink_HomeUnit uid -> + (ext, Set.insert uid homeUid, unit, mods) + DepLink_ExtUnit uid -> + (Set.insert uid ext, homeUid, unit, mods) + + moduleNameJson m = JSString $ moduleNameString m + + moduleKey :: ModuleNameWithIsBoot -> JsonDoc + moduleKey t = + case gwib_isBoot t of + NotBoot -> moduleNameJson (gwib_mod t) + IsBoot -> JSObject + [ ("module", moduleNameJson (gwib_mod t)) + -- Redundant check, but the compiler will fix it and + -- this feels like it might avoid bugs in the future. + , ("boot", JSBool $ gwib_isBoot t == IsBoot) + ] + + linkTypeJson :: GhcLink -> JsonDoc + linkTypeJson = JSString . \ case + NoLink{} -> panic "" + LinkExecutable{} -> "executable" + LinkInMemory{} -> "in-memory" + LinkDynLib{} -> "dynamic-library" + LinkBytecodeLib{} -> "bytecode-library" + LinkStaticLib{} -> "static-library" + LinkMergedObj{} -> "merged-object" + ----------------------------------------------------------------- -- Module cycles ----------------------------------------------------------------- ===================================== docs/users_guide/make-depends-json-schema-1_0.json ===================================== @@ -3,14 +3,15 @@ "title": "GHC dependencies", "description": "The dependency graph of a Haskell program.", "oneOf": [ - { "$ref": "#/definitions/home-module" }, - { "$ref": "#/definitions/external-unit" } + { "$ref": "#/definitions/external-unit" }, + { "$ref": "#/definitions/module" }, + { "$ref": "#/definitions/link" } ], "definitions": { "external-unit": { "type": "object", "properties": { - "unit-id": { "$ref": "#/definitions/unit-name" }, + "unit-id": { "$ref": "#/definitions/unit-id" }, "package-name": { "$ref": "#/definitions/package-name" }, "package-id": { "$ref": "#/definitions/package-id" } }, @@ -29,17 +30,17 @@ "module-name": { "type": "string" }, - "home-module": { + "module": { "type": "object", "properties": { "module": { "$ref": "#/definitions/module-name" }, "unit-id": { "$ref": "#/definitions/unit-id" }, "package-name": { "$ref": "#/definitions/package-name" }, - "source": { "$ref": "#/definitions/home-module-source" }, - "boot": { "type": "boolean" }, + "source": { "$ref": "#/definitions/module-source" }, + "boot": { "$ref": "#/definitions/boot" }, "includes": { "type": "array", - "items": { "$ref": "#/definitions/home-module-include" } + "items": { "$ref": "#/definitions/module-include" } }, "imports": { "type": "object", @@ -48,18 +49,18 @@ "items": { "$ref": "#/definitions/import" } } }, - "preprocessor": { "$ref": "#/definitions/home-module-preprocessor" } + "preprocessor": { "$ref": "#/definitions/module-preprocessor" } }, "required": ["module", "unit-id", "source"], "unevaluatedProperties": false }, - "home-module-source": { + "module-source": { "type": "string" }, - "home-module-include": { + "module-include": { "type": "string" }, - "home-module-preprocessor": { + "module-preprocessor": { "type": "string" }, "import": { @@ -70,17 +71,66 @@ "properties": { "module": { "$ref": "#/definitions/module-name" }, "level": { "$ref": "#/definitions/import-level" }, - "boot": { "type": "boolean" } + "boot": { "$ref": "#/definitions/boot" } }, "required": ["module"], "unevaluatedProperties": false } ] }, + "boot": { + "type": "boolean", + "default": false + }, "import-level": { "type": "string", "enum": ["normal", "quote", "splice"], "default": "normal" + }, + "link": { + "type": "object", + "link-type": { "$ref": "#/definitions/link-type" }, + "properties": { + "links": { + "type": "array", + "items": { "$ref": "#/definitions/link-unit" } + } + } + }, + "link-type": { + "type": "string", + "enum": [ + "executable", + "in-memory", + "dynamic-library", + "bytecode-library", + "static-library", + "merged-object" + ] + }, + "link-unit": { + "type": "object", + "properties": { + "unit-id": { "$ref": "#/definitions/unit-id" }, + "modules": { + "type": "array", + "items": { "$ref": "#/definitions/link-module" } + } + }, + "required": ["unit-id"] + }, + "link-module": { + "oneOf": [ + { "$ref": "#/definitions/module-name" }, + { + "type": "object", + "properties": { + "module": { "$ref": "#/definitions/module-name" }, + "boot": { "$ref": "#/definitions/boot" } + }, + "required": ["module"] + } + ] } } } ===================================== testsuite/tests/driver/mkdepend/all.T ===================================== @@ -30,9 +30,23 @@ test('mkdepend_mhu03', , when(opsys('mingw32'), skip) # shell script preprocessor ], make_depend_multiunit, [['mhu03/unitA'], '']) +# Link nodes +test('mkdepend_mhu04', + [ extra_files(['mhu04/']), dep_makefile, dep_json ], + make_depend_multiunit, [['mhu04/unitLib', 'mhu04/unitApp'], '']) +test('mkdepend_mhu05', + [ extra_files(['mhu05/']), dep_makefile, dep_json ], + make_depend_multiunit, [['mhu05/unitLib', 'mhu05/unitApp', 'mhu05/unitTest'], '']) # Honour -F test('mkdepend01', [ extra_files(['mkdepend01/']), dep_makefile, dep_json ], make_depend, [['mkdepend01/A.hs'], '-pgmF pp']) - +# Link nodes +test('mkdepend02', + [ extra_files(['mkdepend02/']), dep_json ], + make_depend, [['mkdepend02/Main.hs'], '']) +# Backpack +test('mkdepend_bkp_01', + [ extra_files(['mkdepend_bkp_01/']), dep_json, expect_fail ], + make_depend, [['mkdepend_bkp_01/bkp01.bkp'], '']) ===================================== testsuite/tests/driver/mkdepend/mhu04/app/Main.hs ===================================== @@ -0,0 +1,7 @@ +module Main (main) where + +import Lib (greeting) +import Utils (shout) + +main :: IO () +main = putStrLn (shout greeting) ===================================== testsuite/tests/driver/mkdepend/mhu04/app/Utils.hs ===================================== @@ -0,0 +1,6 @@ +module Utils (shout) where + +import Data.Char (toUpper) + +shout :: String -> String +shout = map toUpper ===================================== testsuite/tests/driver/mkdepend/mhu04/lib/Lib.hs ===================================== @@ -0,0 +1,6 @@ +module Lib (greeting) where + +import Lib.Internal (name) + +greeting :: String +greeting = "Hello, " ++ name ===================================== testsuite/tests/driver/mkdepend/mhu04/lib/Lib/Internal.hs ===================================== @@ -0,0 +1,4 @@ +module Lib.Internal (name) where + +name :: String +name = "world" ===================================== testsuite/tests/driver/mkdepend/mhu04/unitApp ===================================== @@ -0,0 +1 @@ +-i -i. -working-dir=mhu04/app -this-unit-id app -this-package-name app -package-id lib -o app Main ===================================== testsuite/tests/driver/mkdepend/mhu04/unitLib ===================================== @@ -0,0 +1 @@ +-i -i. -working-dir=mhu04/lib -this-unit-id lib -this-package-name lib Lib Lib.Internal ===================================== testsuite/tests/driver/mkdepend/mhu05/app/Main.hs ===================================== @@ -0,0 +1,6 @@ +module Main (main) where + +import Lib (greeting) + +main :: IO () +main = putStrLn greeting ===================================== testsuite/tests/driver/mkdepend/mhu05/lib/Lib.hs ===================================== @@ -0,0 +1,6 @@ +module Lib (greeting) where + +import Lib.Internal (name) + +greeting :: String +greeting = "Hello, " ++ name ===================================== testsuite/tests/driver/mkdepend/mhu05/lib/Lib/Internal.hs ===================================== @@ -0,0 +1,4 @@ +module Lib.Internal (name) where + +name :: String +name = "world" ===================================== testsuite/tests/driver/mkdepend/mhu05/test/Spec.hs ===================================== @@ -0,0 +1,9 @@ +module Spec (main) where + +import Lib (greeting) +import Lib.Internal (name) + +main :: IO () +main + | greeting == "Hello, " ++ name = putStrLn "OK" + | otherwise = error "unexpected greeting" ===================================== testsuite/tests/driver/mkdepend/mhu05/unitApp ===================================== @@ -0,0 +1 @@ +-i -i. -working-dir=mhu05/app -this-unit-id app -this-package-name app -package-id lib -o app Main ===================================== testsuite/tests/driver/mkdepend/mhu05/unitLib ===================================== @@ -0,0 +1 @@ +-i -i. -working-dir=mhu05/lib -this-unit-id lib -this-package-name lib -staticlib Lib Lib.Internal ===================================== testsuite/tests/driver/mkdepend/mhu05/unitTest ===================================== @@ -0,0 +1 @@ +-i -i. -working-dir=mhu05/test -this-unit-id test -this-package-name test -package-id lib -main-is Spec -o test Spec ===================================== testsuite/tests/driver/mkdepend/mkdepend02.dep-json ===================================== @@ -0,0 +1,2 @@ +{"module":"Main","unit-id":"main","imports":{},"source":"mkdepend02/Main.hs"} +{"unit-id":"main","link-type":"executable","deps":[{"unit-id":"main","modules":["Main"]}]} ===================================== testsuite/tests/driver/mkdepend/mkdepend02/Main.hs ===================================== @@ -0,0 +1,3 @@ +module Main where + +main = putStrLn "Hello, World" ===================================== testsuite/tests/driver/mkdepend/mkdepend_bkp_01/bkp01.bkp ===================================== @@ -0,0 +1,20 @@ +unit p where + signature H where + data T + x :: Bool + module A where + import H + data A = MkA T + y = x + +unit q where + dependency p[H=<H>] + +unit h where + module H where + data T = T + x = True + +unit r where + dependency h + dependency q[H=h:H] ===================================== testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-json ===================================== @@ -0,0 +1,5 @@ +{"module":"Lib.Internal","unit-id":"lib","imports":{},"package-name":"lib","source":"mhu04/lib/Lib/Internal.hs"} +{"module":"Lib","unit-id":"lib","imports":{"lib":["Lib.Internal"]},"package-name":"lib","source":"mhu04/lib/Lib.hs"} +{"module":"Utils","unit-id":"app","imports":{},"package-name":"app","source":"mhu04/app/Utils.hs"} +{"module":"Main","unit-id":"app","imports":{"app":["Utils"],"lib":["Lib"]},"package-name":"app","source":"mhu04/app/Main.hs"} +{"unit-id":"app","link-type":"executable","deps":[{"unit-id":"app","modules":["Main","Utils"]}]} ===================================== testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-makefile ===================================== @@ -0,0 +1,9 @@ +# DO NOT DELETE: Beginning of Haskell dependencies +mhu04/lib/Lib/Internal.o : mhu04/lib/Lib/Internal.hs +mhu04/lib/Lib.o : mhu04/lib/Lib.hs +mhu04/lib/Lib.o : mhu04/lib/Lib/Internal.hi +mhu04/app/Utils.o : mhu04/app/Utils.hs +mhu04/app/Main.o : mhu04/app/Main.hs +mhu04/app/Main.o : mhu04/app/Utils.hi +mhu04/app/Main.o : mhu04/lib/Lib.hi +# DO NOT DELETE: End of Haskell dependencies ===================================== testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-json ===================================== @@ -0,0 +1,7 @@ +{"module":"Lib.Internal","unit-id":"lib","imports":{},"package-name":"lib","source":"mhu05/lib/Lib/Internal.hs"} +{"module":"Lib","unit-id":"lib","imports":{"lib":["Lib.Internal"]},"package-name":"lib","source":"mhu05/lib/Lib.hs"} +{"unit-id":"lib","link-type":"static-library","deps":[{"unit-id":"lib","modules":["Lib","Lib.Internal"]}]} +{"module":"Main","unit-id":"app","imports":{"lib":["Lib"]},"package-name":"app","source":"mhu05/app/Main.hs"} +{"unit-id":"app","link-type":"executable","deps":[{"unit-id":"app","modules":["Main"]}]} +{"module":"Spec","unit-id":"test","imports":{"lib":["Lib","Lib.Internal"]},"package-name":"test","source":"mhu05/test/Spec.hs"} +{"unit-id":"test","link-type":"executable","deps":[{"unit-id":"test","modules":["Spec"]}]} ===================================== testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-makefile ===================================== @@ -0,0 +1,10 @@ +# DO NOT DELETE: Beginning of Haskell dependencies +mhu05/lib/Lib/Internal.o : mhu05/lib/Lib/Internal.hs +mhu05/lib/Lib.o : mhu05/lib/Lib.hs +mhu05/lib/Lib.o : mhu05/lib/Lib/Internal.hi +mhu05/app/Main.o : mhu05/app/Main.hs +mhu05/app/Main.o : mhu05/lib/Lib.hi +mhu05/test/Spec.o : mhu05/test/Spec.hs +mhu05/test/Spec.o : mhu05/lib/Lib/Internal.hi +mhu05/test/Spec.o : mhu05/lib/Lib.hi +# DO NOT DELETE: End of Haskell dependencies View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/175beaefb7d913049e63f2ff99620401... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/175beaefb7d913049e63f2ff99620401... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help