-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
300 lines (262 loc) · 10.8 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
module Main where
import GHC
import DynFlags
import MonadUtils
import Digraph (flattenSCC)
import HscTypes (handleFlagWarnings)
import HscTypes (dep_pkgs)
import PackageConfig
import Packages
import Module
import StaticFlags
import ErrUtils
import FastString (unpackFS)
import qualified Outputable as O
import qualified Pretty as GHCPretty
import Config (cProjectName, cProjectVersion)
import GHC.Paths ( libdir, ghc )
import Paths_ghc_xcode (version)
import Data.Version (showVersion)
import System.Environment
import System.Exit
import System.FilePath
import System.Directory (doesDirectoryExist, removeFile)
import System.Posix.Files (getSymbolicLinkStatus, isSymbolicLink, createSymbolicLink)
import qualified Data.Set as Set
import Control.Exception as E
import System.IO
import System.Environment.Executable
import Text.PrettyPrint.HughesPJ
import qualified Data.ByteString.Char8 as B
import qualified Data.Text as Text
import Data.Text.Encoding (encodeUtf8)
import Numeric (showHex)
import Data.Char (isPrint)
import Control.Monad (when)
main = do
args <- getArgs
if "--version" `elem` args then printVersion else do
runGHCWithArgsForTargets args $ do
setCustomLogger
modules <- compileAndLoadModules
targetFiles <- getTargetFiles
let isTarget m = case ml_hs_file (ms_location m) of
Just f | f `elem` targetFiles -> True
_ -> False
let targetModules = filter isTarget modules
execName <- liftIO getExecutableName
liftIO $ writeFile moduleInitFile $ moduleInitFileContents execName targetModules
packageIds <- getUniquePackageIds modules
-- Create a symbolic link to the GHC RTS include folder.
getIncludePaths >>= liftIO . createIncludeLink rtsIncludeLink
-- Try to get the path to the "link file" from environmental variables
-- which XCode sets during a build.
-- If we didn't find them, we're running in a shell.
maybeLinkFilePath <- liftIO getLinkFilePath
case maybeLinkFilePath of
Nothing -> printInstructions args modules packageIds
Just f -> writeObjectFiles f modules packageIds
printVersion :: IO ()
printVersion = do
putStrLn $ "ghc-xcode, version " ++ showVersion version
putStrLn $ cProjectName ++ ", version " ++ cProjectVersion
rtsIncludeLink = "_ghc_rts_include"
printInstructions :: [String] -> [ModSummary] -> [PackageId] -> Ghc ()
printInstructions args modules packageIds = do
progName <- liftIO $ getExecutablePath
stubHeaders <- getStubHeaders
let progInvocation = progName ++ " " ++ unwords args
let instructionList =
text "* Under Build Settings, add Header Search Paths:"
$$ nest 4 (text rtsIncludeLink)
$$ text "* Under Build Settings, add Other Linker Flags:"
$$ nest 4 (text "-liconv")
$$ nest 4 (text "-Wl,-no_compact_unwind,-no_pie")
$$ text "* Add the following files to your XCode project:"
$$ nest 4 (vcat $ map text $ [ moduleInitFile ] ++ stubHeaders)
$$ text "* Add a \"Run Script\" build phase which occurs before the"
$$ text " \"Compile Sources\" phase and calls: "
$$ nest 4 (text progInvocation)
liftIO $ print $
text "You will need to make the following changes in XCode:"
$$ nest 2 instructionList
writeObjectFiles linkFilePath modules packageIds = do
objectFiles <- moduleObjectFiles modules
packageLibFiles <- mapM getPackageLibraryFile packageIds
let objFiles = objectFiles ++ packageLibFiles
liftIO $ do
hPutStrLn stderr $ "Adding object files:"
mapM_ (hPutStrLn stderr) objFiles
appendFile linkFilePath
$ unlines $ objFiles
createIncludeLink linkPath targetPath = do
-- Delete the symbolic link, if it already exists.
existingFolder <- doesDirectoryExist linkPath
when existingFolder $ do
stat <- getSymbolicLinkStatus linkPath
if isSymbolicLink stat
then removeFile linkPath
else error $ "Existing file " ++ show linkPath
++ " doesn't look like a symbolic link"
-- Add a new symbolic link to the target folder
createSymbolicLink targetPath linkPath
getLinkFilePath = E.catch (do
arch <- getEnv "CURRENT_ARCH"
variant <- getEnv "CURRENT_VARIANT"
fmap Just $ getEnv $ "LINK_FILE_LIST_" ++ variant ++ "_" ++ arch)
(\(e::IOException) -> return Nothing)
compileAndLoadModules = do
liftIO $ hPutStrLn stderr "Compiling..."
success <- handleSourceError (\e -> printException e >> return Failed)
$ load LoadAllTargets
liftIO $ case success of
Succeeded -> hPutStrLn stderr "Succeeded."
Failed -> do
hPutStrLn stderr "Failed."
exitWith (ExitFailure 1)
g <- depanal [] False
let sccs = topSortModuleGraph False g Nothing
return $ concatMap flattenSCC sccs
getUniquePackageIds modules = do
all_deps <- flip mapM modules $ \theModule -> do
let m = ms_mod theModule
Just info <- getModuleInfo (ms_mod theModule)
let Just iface = modInfoIface info
return $ map fst $ dep_pkgs $ mi_deps iface
return $ Set.toList $ Set.fromList
$ [rtsPackageId] ++ concat all_deps
getPackageLibraryFile packageId = do
dflags <- getSessionDynFlags
let conf = getPackageDetails (pkgState dflags) packageId
-- packagehsLibs takes -threaded, etc. into account
-- for the rts package's libs
let [libname] = packageHsLibs dflags conf
let libdir:_ = libraryDirs conf
return $ libdir </> "lib" ++ (libname :: String) <.> "a"
runGHCWithArgsForTargets args action = do
let locatedArgs = map (mkGeneralLocated "on the command line")
args
-- Static flags are necessary in particular to pick up "-threaded".
(args', staticFlagWarnings) <- parseStaticFlags locatedArgs
defaultErrorHandler defaultLogAction $
runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
defaultCleanupHandler dflags $ do
dflags <- getSessionDynFlags
(dflags2, fileish_args, dynamicFlagWarnings) <- parseDynamicFlags dflags args'
handleSourceError (\e -> do
GHC.printException e
liftIO $ exitWith (ExitFailure 1)) $
liftIO $ handleFlagWarnings dflags2
$ staticFlagWarnings ++ dynamicFlagWarnings
setSessionDynFlags dflags2
let fileArgs = map unLoc fileish_args
targets <- mapM (flip guessTarget Nothing) fileArgs
setTargets targets
action
moduleObjectFiles moduleSummaries = do
targetFiles <- getTargetFiles
return $
map (ml_obj_file . ms_location) moduleSummaries
-- stub files
-- Apparently not needed in ghc-7.2.
-- ++ map ((++ "_stub.o") . dropExtension) targetFiles
getTargetFiles = do
targets <- getTargets
return [f | TargetFile f _ <- map targetId targets]
getIncludePaths = do
dflags <- getSessionDynFlags
let [rtsIncludeDir] = includeDirs
$ getPackageDetails (pkgState dflags)
rtsPackageId
return rtsIncludeDir
getStubHeaders = fmap (map ((++"_stub.h") . dropExtension))
getTargetFiles
---------
moduleInitFile = "_hs_module_init.c"
moduleInitFileContents execName moduleSummaries = unlines $
[ "/* This file has been generated automatically by ghc-xcode."
, " You should not edit it directly. */"
, "#include <HsFFI.h>"
]
++ ["#include \"" ++ f ++ "\"" | f <- map headerStub moduleSummaries ]
++ ["extern void " ++ s ++ "(void);" | s <- stginits] ++
[ "static void library_init(void) __attribute__((constructor));"
, "static void library_init(void)"
, "{"
, "static char *argv[] = { " ++ quotedCString execName ++ ", 0 }, **argv_ = argv;"
, "static int argc = 1;"
, ""
, "hs_init(&argc, &argv_);"
]
++ ["hs_add_root(" ++ s ++ ");" | s <- stginits] ++
[ "}"
, ""
, "static void library_exit(void) __attribute__((destructor));"
, "static void library_exit(void)"
, "{"
, "hs_exit();"
, "}"
]
where
stginits = map stginit moduleSummaries
stginit m
= "__stginit_" ++ (fixZEncoding $ moduleNameString $ moduleName $ ms_mod m)
headerStub m
= dropExtension (ml_obj_file $ ms_location m) ++ "_stub.h"
fixZEncoding = concatMap $ \c -> case c of
'.' -> "zi"
_ -> [c]
-- Return the value of ${EXECUTABLE_NAME}.
-- Return a default value if it's not set (most likely, because we're not running in XCode).
getExecutableName = E.catch (getEnv "EXECUTABLE_NAME")
(\(e::IOException) -> return "PROGNAME")
-- Takes the input and turns it into a C expression representation of the string.
-- which is encoded in UTF-8 and escapes problem characters.
quotedCString :: String -> String
quotedCString = cRep . cvtToUTF8
where
cvtToUTF8 = B.unpack . encodeUtf8 . Text.pack
cRep s = "\"" ++ concatMap cRepChar s ++ "\""
cRepChar '"' = "\\\""
cRepChar c
| isPrint c && c < '\128' = [c]
| otherwise = "\\x" ++ hexRep (fromEnum c)
hexRep c
| c < 16 = "0" ++ showHex c "" -- probably won't happen in practice
| otherwise = showHex c ""
------------
-- Custom logger.
setCustomLogger :: Ghc ()
setCustomLogger = do
-- Don't change the logger if we're running outside of XCode.
execName <- liftIO $ E.try $ getEnv "EXECUTABLE_NAME"
case execName of
Left (e::IOException) -> return () -- not XCode
Right _ -> do
dflags <- getSessionDynFlags
setSessionDynFlags dflags {log_action = myLogger}
return ()
-- XCode parses error messages of the forms:
-- Foo.hs:10: error: message
-- Foo.hs:10: warning: message
-- also possibly "note".
-- Error messages might span multiple lines; for now, we just
-- put it all on one line and prepend it with "error:", "warning:", etc.
myLogger :: LogAction
myLogger sev span style msg = do
let fullMsg = gccLoc (srcSpanStart span)
O.<+> O.text (errType sev)
O.<> O.colon O.<+> msg
GHCPretty.printDoc GHCPretty.OneLineMode stderr
$ O.runSDoc fullMsg $ O.initSDocContext style
hFlush stderr
gccLoc :: SrcLoc -> Message
gccLoc (UnhelpfulLoc s) = O.text "::"
gccLoc (RealSrcLoc loc) = O.ftext (srcLocFile loc) O.<> O.colon
O.<> O.ppr (srcLocLine loc) O.<> O.colon
errType :: Severity -> String
errType SevError = "error"
errType SevWarning = "warning"
errType SevFatal = "error"
errType _ = "note"