|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +module WPC.GhcStgApp where |
| 4 | + |
| 5 | +import GHC.Prelude |
| 6 | + |
| 7 | +import GHC.Data.Maybe |
| 8 | +import GHC.Platform.ArchOS |
| 9 | +import GHC.Utils.Outputable |
| 10 | +import GHC.Unit.Info |
| 11 | +import GHC.Unit.Home.ModInfo |
| 12 | +import GHC.Unit.Module.Deps |
| 13 | +import GHC.Linker.Static.Utils |
| 14 | +import GHC.Linker.Types |
| 15 | +import GHC.Unit.Module.ModIface |
| 16 | + |
| 17 | +import GHC.Driver.Ppr |
| 18 | +import GHC.Driver.Session |
| 19 | + |
| 20 | +import qualified GHC.Data.ShortText as ST |
| 21 | + |
| 22 | +import GHC.Utils.Json |
| 23 | + |
| 24 | +import Data.List ( isPrefixOf ) |
| 25 | +import Data.Containers.ListUtils ( nubOrd ) |
| 26 | +import qualified Data.Set as Set |
| 27 | +import Data.Version |
| 28 | +import GHC.Unit.State |
| 29 | +import GHC.Unit.Env |
| 30 | +import GHC.Unit.Types |
| 31 | +import GHC.Platform |
| 32 | + |
| 33 | +import System.FilePath |
| 34 | +import System.Directory |
| 35 | + |
| 36 | +import WPC.Yaml |
| 37 | + |
| 38 | +{- |
| 39 | +TODO: |
| 40 | + list app modules |
| 41 | + list app cbits |
| 42 | +-} |
| 43 | + |
| 44 | +writeGhcStgApp :: DynFlags -> UnitEnv -> HomePackageTable -> IO () |
| 45 | +writeGhcStgApp dflags unit_env hpt = do |
| 46 | + let home_mod_infos = eltsHpt hpt |
| 47 | + |
| 48 | + -- the packages we depend on |
| 49 | + dep_units = Set.toList |
| 50 | + $ Set.unions |
| 51 | + $ fmap (dep_direct_pkgs . mi_deps . hm_iface) |
| 52 | + $ home_mod_infos |
| 53 | + |
| 54 | + platform = targetPlatform dflags |
| 55 | + arch_os = platformArchOS platform |
| 56 | + staticLink = False |
| 57 | + output_fn = exeFileName arch_os staticLink (outputFile_ dflags) |
| 58 | + |
| 59 | + root <- getCurrentDirectory |
| 60 | + dep_unit_infos <- mayThrowUnitErr (preloadUnitsInfo' unit_env dep_units) |
| 61 | + let pp :: Outputable a => a -> String |
| 62 | + pp = showSDoc dflags . ppr |
| 63 | + toAbsPath p |
| 64 | + | isAbsolute p = p |
| 65 | + | otherwise = root </> p |
| 66 | + arrOfAbsPathST = arrOfAbsPath . map ST.unpack |
| 67 | + arrOfAbsPath = JSArray . map JSString . nubOrd . map toAbsPath |
| 68 | + app_deps = JSArray |
| 69 | + [ JSObject |
| 70 | + [ ("name", JSString $ pp unitPackageName) |
| 71 | + , ("version", JSString (showVersion unitPackageVersion)) |
| 72 | + , ("id", JSString $ pp unitId) |
| 73 | + , ("unit-import-dirs", arrOfAbsPathST unitImportDirs) |
| 74 | + , ("unit-libraries", JSArray $ map (JSString . ST.unpack) unitLibraries) |
| 75 | + , ("library-dirs", arrOfAbsPathST unitLibraryDirs) |
| 76 | + , ("extra-libraries", JSArray $ map (JSString . ST.unpack) unitExtDepLibsSys) |
| 77 | + , ("framework-dirs", arrOfAbsPathST unitExtDepFrameworkDirs) |
| 78 | + , ("extra-frameworks", JSArray $ map (JSString . ST.unpack) unitExtDepFrameworks) |
| 79 | + , ("ld-options", JSArray $ map (JSString . ST.unpack) unitLinkerOptions) |
| 80 | + , ("exposed-modules", JSArray $ map (JSString . pp) [mod | (mod, Nothing) <- unitExposedModules]) |
| 81 | + , ("hidden-modules", JSArray $ map (JSString . pp) unitHiddenModules) |
| 82 | + ] |
| 83 | + | GenericUnitInfo{..} <- dep_unit_infos |
| 84 | + ] |
| 85 | + |
| 86 | + let arrOfStr = JSArray . map JSString . nubOrd |
| 87 | + appLdOptions = [ o |
| 88 | + | Option o <- ldInputs dflags |
| 89 | + , not $ isPrefixOf "-l" o |
| 90 | + ] |
| 91 | + writeFile (output_fn ++ "." ++ objectSuf dflags ++ "_ghc_stgapp") $ showSDoc dflags $ renderYAML $ JSObject |
| 92 | + [ ("ghc-name", JSString . ghcNameVersion_programName $ ue_namever unit_env) |
| 93 | + , ("ghc-version", JSString . ghcNameVersion_projectVersion $ ue_namever unit_env) |
| 94 | + , ("platform-os", JSString . stringEncodeOS . platformOS $ targetPlatform dflags) |
| 95 | + , ("no-hs-main", JSBool $ gopt Opt_NoHsMain dflags) |
| 96 | + , ("o-suffix", JSString $ objectSuf dflags) |
| 97 | + , ("ways", arrOfStr $ map show . Set.toList $ ways dflags) |
| 98 | + , ("object-dir", JSString . toAbsPath $ fromMaybe root (objectDir dflags)) |
| 99 | + , ("app-unit-id", JSString . pp $ ue_current_unit unit_env) |
| 100 | + , ("app-modules", JSArray $ map (JSString . pp) [moduleName . mi_module $ hm_iface | HomeModInfo{..} <- home_mod_infos]) |
| 101 | + , ("extra-ld-inputs", arrOfAbsPath [f | FileOption _ f <- ldInputs dflags]) |
| 102 | + , ("library-dirs", arrOfAbsPath $ libraryPaths dflags) |
| 103 | + , ("extra-libraries", arrOfStr [lib | Option ('-':'l':lib) <- ldInputs dflags]) |
| 104 | + , ("framework-dirs", arrOfAbsPath $ frameworkPaths dflags) |
| 105 | + , ("extra-frameworks", JSArray $ map JSString $ cmdlineFrameworks dflags) |
| 106 | + , ("ld-options", arrOfStr appLdOptions) |
| 107 | + , ("unit-db-paths", arrOfAbsPath $ maybe [] (map unitDatabasePath) $ ue_unit_dbs unit_env) |
| 108 | + , ("app-deps", app_deps) |
| 109 | + ] |
| 110 | + |
| 111 | +{- |
| 112 | +eltsUDFM :: UniqDFM key elt -> [elt] |
| 113 | +eltsHpt :: HomePackageTable -> [HomeModInfo] |
| 114 | +type HomePackageTable = DModuleNameEnv HomeModInfo |
| 115 | +
|
| 116 | +data HomeModInfo = HomeModInfo |
| 117 | + { hm_iface :: !ModIface |
| 118 | + , hm_details :: ModDetails |
| 119 | + , hm_linkable :: !HomeModLinkable |
| 120 | + } |
| 121 | +
|
| 122 | +type ModIface = ModIface_ 'ModIfaceFinal |
| 123 | + mi_module :: !Module, -- ^ Name of the module we are for |
| 124 | +
|
| 125 | +data GenModule unit = Module |
| 126 | + { moduleUnit :: !unit -- ^ Unit the module belongs to |
| 127 | + , moduleName :: !ModuleName -- ^ Module name (e.g. A.B.C) |
| 128 | + } |
| 129 | + deriving (Eq,Ord,Data,Functor) |
| 130 | +
|
| 131 | +-- | A Module is a pair of a 'Unit' and a 'ModuleName'. |
| 132 | +type Module = GenModule Unit |
| 133 | +
|
| 134 | +moduleUnitId :: Module -> UnitId |
| 135 | +moduleUnitId = toUnitId . moduleUnit |
| 136 | +-} |
0 commit comments