Hadrian: support dynamically linking ghc
[ghc.git] / hadrian / src / Context.hs
1 module Context (
2 -- * Context
3 Context (..), vanillaContext, stageContext,
4
5 -- * Expressions
6 getStage, getPackage, getWay, getStagedSettingList, getBuildPath,
7
8 -- * Paths
9 contextDir, buildPath, buildDir, pkgInplaceConfig, pkgSetupConfigFile,
10 pkgHaddockFile, pkgLibraryFile, pkgGhciLibraryFile, pkgConfFile, objectPath,
11 contextPath, getContextPath, libDir, libPath, distDir
12 ) where
13
14 import Base
15 import Context.Path
16 import Context.Type
17 import Hadrian.Expression
18 import Hadrian.Haskell.Cabal
19 import Oracles.Setting
20
21 -- | Most targets are built only one way, hence the notion of 'vanillaContext'.
22 vanillaContext :: Stage -> Package -> Context
23 vanillaContext s p = Context s p vanilla
24
25 -- | Partial context with undefined 'Package' field. Useful for 'Packages'
26 -- expressions that only read the environment and current 'Stage'.
27 stageContext :: Stage -> Context
28 stageContext s = vanillaContext s $ error "stageContext: package not set"
29
30 -- | Get the 'Stage' of the current 'Context'.
31 getStage :: Expr Context b Stage
32 getStage = stage <$> getContext
33
34 -- | Get the 'Package' of the current 'Context'.
35 getPackage :: Expr Context b Package
36 getPackage = package <$> getContext
37
38 -- | Get the 'Way' of the current 'Context'.
39 getWay :: Expr Context b Way
40 getWay = way <$> getContext
41
42 -- | Get a list of configuration settings for the current stage.
43 getStagedSettingList :: (Stage -> SettingList) -> Args Context b
44 getStagedSettingList f = getSettingList . f =<< getStage
45
46 libDir :: Context -> FilePath
47 libDir Context {..} = stageString stage -/- "lib"
48
49 -- | Path to the directory containg the final artifact in a given 'Context'.
50 libPath :: Context -> Action FilePath
51 libPath context = buildRoot <&> (-/- libDir context)
52
53 -- | Get the directory name for binary distribution files
54 -- <arch>-<os>-ghc-<version>.
55 distDir :: Action FilePath
56 distDir = do
57 version <- setting ProjectVersion
58 hostOs <- setting BuildOs
59 hostArch <- setting BuildArch
60 return $ hostArch ++ "-" ++ hostOs ++ "-ghc-" ++ version
61
62 pkgFile :: Context -> String -> String -> Action FilePath
63 pkgFile context@Context {..} prefix suffix = do
64 path <- buildPath context
65 pid <- pkgIdentifier package
66 return $ path -/- prefix ++ pid ++ suffix
67
68 -- | Path to inplace package configuration file of a given 'Context'.
69 pkgInplaceConfig :: Context -> Action FilePath
70 pkgInplaceConfig context = do
71 path <- contextPath context
72 return $ path -/- "inplace-pkg-config"
73
74 -- TODO: Add a @Rules FilePath@ alternative.
75 -- | Path to the @setup-config@ of a given 'Context'.
76 pkgSetupConfigFile :: Context -> Action FilePath
77 pkgSetupConfigFile context = do
78 path <- contextPath context
79 return $ path -/- "setup-config"
80
81 -- | Path to the haddock file of a given 'Context', e.g.:
82 -- @_build/stage1/libraries/array/doc/html/array/array.haddock@.
83 pkgHaddockFile :: Context -> Action FilePath
84 pkgHaddockFile Context {..} = do
85 root <- buildRoot
86 let name = pkgName package
87 return $ root -/- "docs/html/libraries" -/- name -/- name <.> "haddock"
88
89 -- | Path to the library file of a given 'Context', e.g.:
90 -- @_build/stage1/libraries/array/build/libHSarray-0.5.1.0.a@.
91 pkgLibraryFile :: Context -> Action FilePath
92 pkgLibraryFile context@Context {..} = do
93 extension <- libsuf way
94 pkgFile context "libHS" extension
95
96 -- | Path to the GHCi library file of a given 'Context', e.g.:
97 -- @_build/stage1/libraries/array/build/HSarray-0.5.1.0.o@.
98 pkgGhciLibraryFile :: Context -> Action FilePath
99 pkgGhciLibraryFile context = pkgFile context "HS" ".o"
100
101 -- | Path to the configuration file of a given 'Context'.
102 pkgConfFile :: Context -> Action FilePath
103 pkgConfFile Context {..} = do
104 root <- buildRoot
105 pid <- pkgIdentifier package
106 return $ root -/- relativePackageDbPath stage -/- pid <.> "conf"
107
108 -- | Given a 'Context' and a 'FilePath' to a source file, compute the 'FilePath'
109 -- to its object file. For example:
110 -- * "Task.c" -> "_build/stage1/rts/Task.thr_o"
111 -- * "_build/stage1/rts/cmm/AutoApply.cmm" -> "_build/stage1/rts/cmm/AutoApply.o"
112 objectPath :: Context -> FilePath -> Action FilePath
113 objectPath context@Context {..} src = do
114 isGenerated <- isGeneratedSource src
115 path <- buildPath context
116 let extension = drop 1 $ takeExtension src
117 obj = src -<.> osuf way
118 result | isGenerated = obj
119 | "*hs*" ?== extension = path -/- obj
120 | otherwise = path -/- extension -/- obj
121 return result