Use check stacking on Windows.
[ghc.git] / compiler / main / SysTools.hs
1 {-
2 -----------------------------------------------------------------------------
3 --
4 -- (c) The University of Glasgow 2001-2003
5 --
6 -- Access to system tools: gcc, cp, rm etc
7 --
8 -----------------------------------------------------------------------------
9 -}
10
11 {-# LANGUAGE CPP, ScopedTypeVariables #-}
12
13 module SysTools (
14 -- Initialisation
15 initSysTools,
16
17 -- Interface to system tools
18 runUnlit, runCpp, runCc, -- [Option] -> IO ()
19 runPp, -- [Option] -> IO ()
20 runSplit, -- [Option] -> IO ()
21 runAs, runLink, runLibtool, -- [Option] -> IO ()
22 runMkDLL,
23 runWindres,
24 runLlvmOpt,
25 runLlvmLlc,
26 runClang,
27 figureLlvmVersion,
28
29 getLinkerInfo,
30 getCompilerInfo,
31
32 linkDynLib,
33
34 askCc,
35
36 touch, -- String -> String -> IO ()
37 copy,
38 copyWithHeader,
39
40 -- Temporary-file management
41 setTmpDir,
42 newTempName, newTempLibName,
43 cleanTempDirs, cleanTempFiles, cleanTempFilesExcept,
44 addFilesToClean,
45
46 Option(..),
47
48 -- frameworks
49 getPkgFrameworkOpts,
50 getFrameworkOpts
51
52
53 ) where
54
55 #include "HsVersions.h"
56
57 import DriverPhases
58 import Module
59 import Packages
60 import Config
61 import Outputable
62 import ErrUtils
63 import Panic
64 import Platform
65 import Util
66 import DynFlags
67 import Exception
68
69 import LlvmCodeGen.Base (llvmVersionStr, supportedLlvmVersion)
70
71 import Data.IORef
72 import Control.Monad
73 import System.Exit
74 import System.Environment
75 import System.FilePath
76 import System.IO
77 import System.IO.Error as IO
78 import System.Directory
79 import Data.Char
80 import Data.List
81 import qualified Data.Map as Map
82
83 #ifndef mingw32_HOST_OS
84 import qualified System.Posix.Internals
85 #else /* Must be Win32 */
86 import Foreign
87 import Foreign.C.String
88 import qualified System.Win32.Info as Info
89 import Control.Exception (finally)
90 import Foreign.Ptr (FunPtr, castPtrToFunPtr)
91 import System.Win32.Types (DWORD, LPTSTR, HANDLE)
92 import System.Win32.Types (failIfNull, failIf, iNVALID_HANDLE_VALUE)
93 import System.Win32.File (createFile,closeHandle, gENERIC_READ, fILE_SHARE_READ, oPEN_EXISTING, fILE_ATTRIBUTE_NORMAL, fILE_FLAG_BACKUP_SEMANTICS )
94 import System.Win32.DLL (loadLibrary, getProcAddress)
95 import Data.Bits((.|.))
96 #endif
97
98 import System.Process
99 import Control.Concurrent
100 import FastString
101 import SrcLoc ( SrcLoc, mkSrcLoc, noSrcSpan, mkSrcSpan )
102
103 #ifdef mingw32_HOST_OS
104 # if defined(i386_HOST_ARCH)
105 # define WINDOWS_CCONV stdcall
106 # elif defined(x86_64_HOST_ARCH)
107 # define WINDOWS_CCONV ccall
108 # else
109 # error Unknown mingw32 arch
110 # endif
111 #endif
112
113 {-
114 How GHC finds its files
115 ~~~~~~~~~~~~~~~~~~~~~~~
116
117 [Note topdir]
118
119 GHC needs various support files (library packages, RTS etc), plus
120 various auxiliary programs (cp, gcc, etc). It starts by finding topdir,
121 the root of GHC's support files
122
123 On Unix:
124 - ghc always has a shell wrapper that passes a -B<dir> option
125
126 On Windows:
127 - ghc never has a shell wrapper.
128 - we can find the location of the ghc binary, which is
129 $topdir/bin/<something>.exe
130 where <something> may be "ghc", "ghc-stage2", or similar
131 - we strip off the "bin/<something>.exe" to leave $topdir.
132
133 from topdir we can find package.conf, ghc-asm, etc.
134
135
136 SysTools.initSysProgs figures out exactly where all the auxiliary programs
137 are, and initialises mutable variables to make it easy to call them.
138 To to this, it makes use of definitions in Config.hs, which is a Haskell
139 file containing variables whose value is figured out by the build system.
140
141 Config.hs contains two sorts of things
142
143 cGCC, The *names* of the programs
144 cCPP e.g. cGCC = gcc
145 cUNLIT cCPP = gcc -E
146 etc They do *not* include paths
147
148
149 cUNLIT_DIR The *path* to the directory containing unlit, split etc
150 cSPLIT_DIR *relative* to the root of the build tree,
151 for use when running *in-place* in a build tree (only)
152
153
154
155 ---------------------------------------------
156 NOTES for an ALTERNATIVE scheme (i.e *not* what is currently implemented):
157
158 Another hair-brained scheme for simplifying the current tool location
159 nightmare in GHC: Simon originally suggested using another
160 configuration file along the lines of GCC's specs file - which is fine
161 except that it means adding code to read yet another configuration
162 file. What I didn't notice is that the current package.conf is
163 general enough to do this:
164
165 Package
166 {name = "tools", import_dirs = [], source_dirs = [],
167 library_dirs = [], hs_libraries = [], extra_libraries = [],
168 include_dirs = [], c_includes = [], package_deps = [],
169 extra_ghc_opts = ["-pgmc/usr/bin/gcc","-pgml${topdir}/bin/unlit", ... etc.],
170 extra_cc_opts = [], extra_ld_opts = []}
171
172 Which would have the advantage that we get to collect together in one
173 place the path-specific package stuff with the path-specific tool
174 stuff.
175 End of NOTES
176 ---------------------------------------------
177
178 ************************************************************************
179 * *
180 \subsection{Initialisation}
181 * *
182 ************************************************************************
183 -}
184
185 initSysTools :: Maybe String -- Maybe TopDir path (without the '-B' prefix)
186 -> IO Settings -- Set all the mutable variables above, holding
187 -- (a) the system programs
188 -- (b) the package-config file
189 -- (c) the GHC usage message
190 initSysTools mbMinusB
191 = do top_dir <- findTopDir mbMinusB
192 -- see [Note topdir]
193 -- NB: top_dir is assumed to be in standard Unix
194 -- format, '/' separated
195
196 let settingsFile = top_dir </> "settings"
197 platformConstantsFile = top_dir </> "platformConstants"
198 installed :: FilePath -> FilePath
199 installed file = top_dir </> file
200 libexec :: FilePath -> FilePath
201 libexec file = top_dir </> "bin" </> file
202
203 settingsStr <- readFile settingsFile
204 platformConstantsStr <- readFile platformConstantsFile
205 mySettings <- case maybeReadFuzzy settingsStr of
206 Just s ->
207 return s
208 Nothing ->
209 pgmError ("Can't parse " ++ show settingsFile)
210 platformConstants <- case maybeReadFuzzy platformConstantsStr of
211 Just s ->
212 return s
213 Nothing ->
214 pgmError ("Can't parse " ++
215 show platformConstantsFile)
216 let getSetting key = case lookup key mySettings of
217 Just xs ->
218 return $ case stripPrefix "$topdir" xs of
219 Just [] ->
220 top_dir
221 Just xs'@(c:_)
222 | isPathSeparator c ->
223 top_dir ++ xs'
224 _ ->
225 xs
226 Nothing -> pgmError ("No entry for " ++ show key ++ " in " ++ show settingsFile)
227 getBooleanSetting key = case lookup key mySettings of
228 Just "YES" -> return True
229 Just "NO" -> return False
230 Just xs -> pgmError ("Bad value for " ++ show key ++ ": " ++ show xs)
231 Nothing -> pgmError ("No entry for " ++ show key ++ " in " ++ show settingsFile)
232 readSetting key = case lookup key mySettings of
233 Just xs ->
234 case maybeRead xs of
235 Just v -> return v
236 Nothing -> pgmError ("Failed to read " ++ show key ++ " value " ++ show xs)
237 Nothing -> pgmError ("No entry for " ++ show key ++ " in " ++ show settingsFile)
238 crossCompiling <- getBooleanSetting "cross compiling"
239 targetArch <- readSetting "target arch"
240 targetOS <- readSetting "target os"
241 targetWordSize <- readSetting "target word size"
242 targetUnregisterised <- getBooleanSetting "Unregisterised"
243 targetHasGnuNonexecStack <- readSetting "target has GNU nonexec stack"
244 targetHasIdentDirective <- readSetting "target has .ident directive"
245 targetHasSubsectionsViaSymbols <- readSetting "target has subsections via symbols"
246 myExtraGccViaCFlags <- getSetting "GCC extra via C opts"
247 -- On Windows, mingw is distributed with GHC,
248 -- so we look in TopDir/../mingw/bin
249 -- It would perhaps be nice to be able to override this
250 -- with the settings file, but it would be a little fiddly
251 -- to make that possible, so for now you can't.
252 gcc_prog <- getSetting "C compiler command"
253 gcc_args_str <- getSetting "C compiler flags"
254 cpp_prog <- getSetting "Haskell CPP command"
255 cpp_args_str <- getSetting "Haskell CPP flags"
256 let unreg_gcc_args = if targetUnregisterised
257 then ["-DNO_REGS", "-DUSE_MINIINTERPRETER"]
258 else []
259 -- TABLES_NEXT_TO_CODE affects the info table layout.
260 tntc_gcc_args
261 | mkTablesNextToCode targetUnregisterised
262 = ["-DTABLES_NEXT_TO_CODE"]
263 | otherwise = []
264 cpp_args= map Option (words cpp_args_str)
265 gcc_args = map Option (words gcc_args_str
266 ++ unreg_gcc_args
267 ++ tntc_gcc_args)
268 ldSupportsCompactUnwind <- getBooleanSetting "ld supports compact unwind"
269 ldSupportsBuildId <- getBooleanSetting "ld supports build-id"
270 ldSupportsFilelist <- getBooleanSetting "ld supports filelist"
271 ldIsGnuLd <- getBooleanSetting "ld is GNU ld"
272 perl_path <- getSetting "perl command"
273
274 let pkgconfig_path = installed "package.conf.d"
275 ghc_usage_msg_path = installed "ghc-usage.txt"
276 ghci_usage_msg_path = installed "ghci-usage.txt"
277
278 -- For all systems, unlit, split, mangle are GHC utilities
279 -- architecture-specific stuff is done when building Config.hs
280 unlit_path = libexec cGHC_UNLIT_PGM
281
282 -- split is a Perl script
283 split_script = libexec cGHC_SPLIT_PGM
284
285 windres_path <- getSetting "windres command"
286 libtool_path <- getSetting "libtool command"
287
288 tmpdir <- getTemporaryDirectory
289
290 touch_path <- getSetting "touch command"
291
292 let -- On Win32 we don't want to rely on #!/bin/perl, so we prepend
293 -- a call to Perl to get the invocation of split.
294 -- On Unix, scripts are invoked using the '#!' method. Binary
295 -- installations of GHC on Unix place the correct line on the
296 -- front of the script at installation time, so we don't want
297 -- to wire-in our knowledge of $(PERL) on the host system here.
298 (split_prog, split_args)
299 | isWindowsHost = (perl_path, [Option split_script])
300 | otherwise = (split_script, [])
301 mkdll_prog <- getSetting "dllwrap command"
302 let mkdll_args = []
303
304 -- cpp is derived from gcc on all platforms
305 -- HACK, see setPgmP below. We keep 'words' here to remember to fix
306 -- Config.hs one day.
307
308
309 -- Other things being equal, as and ld are simply gcc
310 gcc_link_args_str <- getSetting "C compiler link flags"
311 let as_prog = gcc_prog
312 as_args = gcc_args
313 ld_prog = gcc_prog
314 ld_args = gcc_args ++ map Option (words gcc_link_args_str)
315
316 -- We just assume on command line
317 lc_prog <- getSetting "LLVM llc command"
318 lo_prog <- getSetting "LLVM opt command"
319
320 let iserv_prog = libexec "ghc-iserv"
321
322 let platform = Platform {
323 platformArch = targetArch,
324 platformOS = targetOS,
325 platformWordSize = targetWordSize,
326 platformUnregisterised = targetUnregisterised,
327 platformHasGnuNonexecStack = targetHasGnuNonexecStack,
328 platformHasIdentDirective = targetHasIdentDirective,
329 platformHasSubsectionsViaSymbols = targetHasSubsectionsViaSymbols,
330 platformIsCrossCompiling = crossCompiling
331 }
332
333 return $ Settings {
334 sTargetPlatform = platform,
335 sTmpDir = normalise tmpdir,
336 sGhcUsagePath = ghc_usage_msg_path,
337 sGhciUsagePath = ghci_usage_msg_path,
338 sTopDir = top_dir,
339 sRawSettings = mySettings,
340 sExtraGccViaCFlags = words myExtraGccViaCFlags,
341 sSystemPackageConfig = pkgconfig_path,
342 sLdSupportsCompactUnwind = ldSupportsCompactUnwind,
343 sLdSupportsBuildId = ldSupportsBuildId,
344 sLdSupportsFilelist = ldSupportsFilelist,
345 sLdIsGnuLd = ldIsGnuLd,
346 sProgramName = "ghc",
347 sProjectVersion = cProjectVersion,
348 sPgm_L = unlit_path,
349 sPgm_P = (cpp_prog, cpp_args),
350 sPgm_F = "",
351 sPgm_c = (gcc_prog, gcc_args),
352 sPgm_s = (split_prog,split_args),
353 sPgm_a = (as_prog, as_args),
354 sPgm_l = (ld_prog, ld_args),
355 sPgm_dll = (mkdll_prog,mkdll_args),
356 sPgm_T = touch_path,
357 sPgm_windres = windres_path,
358 sPgm_libtool = libtool_path,
359 sPgm_lo = (lo_prog,[]),
360 sPgm_lc = (lc_prog,[]),
361 sPgm_i = iserv_prog,
362 sOpt_L = [],
363 sOpt_P = [],
364 sOpt_F = [],
365 sOpt_c = [],
366 sOpt_a = [],
367 sOpt_l = [],
368 sOpt_windres = [],
369 sOpt_lo = [],
370 sOpt_lc = [],
371 sOpt_i = [],
372 sPlatformConstants = platformConstants
373 }
374
375 -- returns a Unix-format path (relying on getBaseDir to do so too)
376 findTopDir :: Maybe String -- Maybe TopDir path (without the '-B' prefix).
377 -> IO String -- TopDir (in Unix format '/' separated)
378 findTopDir (Just minusb) = return (normalise minusb)
379 findTopDir Nothing
380 = do -- Get directory of executable
381 maybe_exec_dir <- getBaseDir
382 case maybe_exec_dir of
383 -- "Just" on Windows, "Nothing" on unix
384 Nothing -> throwGhcExceptionIO (InstallationError "missing -B<dir> option")
385 Just dir -> return dir
386
387 {-
388 ************************************************************************
389 * *
390 \subsection{Running an external program}
391 * *
392 ************************************************************************
393 -}
394
395 runUnlit :: DynFlags -> [Option] -> IO ()
396 runUnlit dflags args = do
397 let prog = pgm_L dflags
398 opts = getOpts dflags opt_L
399 runSomething dflags "Literate pre-processor" prog
400 (map Option opts ++ args)
401
402 runCpp :: DynFlags -> [Option] -> IO ()
403 runCpp dflags args = do
404 let (p,args0) = pgm_P dflags
405 args1 = map Option (getOpts dflags opt_P)
406 args2 = if gopt Opt_WarnIsError dflags
407 then [Option "-Werror"]
408 else []
409 mb_env <- getGccEnv args2
410 runSomethingFiltered dflags id "C pre-processor" p
411 (args0 ++ args1 ++ args2 ++ args) mb_env
412
413 runPp :: DynFlags -> [Option] -> IO ()
414 runPp dflags args = do
415 let prog = pgm_F dflags
416 opts = map Option (getOpts dflags opt_F)
417 runSomething dflags "Haskell pre-processor" prog (args ++ opts)
418
419 runCc :: DynFlags -> [Option] -> IO ()
420 runCc dflags args = do
421 let (p,args0) = pgm_c dflags
422 args1 = map Option (getOpts dflags opt_c)
423 args2 = args0 ++ args1 ++ args
424 mb_env <- getGccEnv args2
425 runSomethingResponseFile dflags cc_filter "C Compiler" p args2 mb_env
426 where
427 -- discard some harmless warnings from gcc that we can't turn off
428 cc_filter = unlines . doFilter . lines
429
430 {-
431 gcc gives warnings in chunks like so:
432 In file included from /foo/bar/baz.h:11,
433 from /foo/bar/baz2.h:22,
434 from wibble.c:33:
435 /foo/flibble:14: global register variable ...
436 /foo/flibble:15: warning: call-clobbered r...
437 We break it up into its chunks, remove any call-clobbered register
438 warnings from each chunk, and then delete any chunks that we have
439 emptied of warnings.
440 -}
441 doFilter = unChunkWarnings . filterWarnings . chunkWarnings []
442 -- We can't assume that the output will start with an "In file inc..."
443 -- line, so we start off expecting a list of warnings rather than a
444 -- location stack.
445 chunkWarnings :: [String] -- The location stack to use for the next
446 -- list of warnings
447 -> [String] -- The remaining lines to look at
448 -> [([String], [String])]
449 chunkWarnings loc_stack [] = [(loc_stack, [])]
450 chunkWarnings loc_stack xs
451 = case break loc_stack_start xs of
452 (warnings, lss:xs') ->
453 case span loc_start_continuation xs' of
454 (lsc, xs'') ->
455 (loc_stack, warnings) : chunkWarnings (lss : lsc) xs''
456 _ -> [(loc_stack, xs)]
457
458 filterWarnings :: [([String], [String])] -> [([String], [String])]
459 filterWarnings [] = []
460 -- If the warnings are already empty then we are probably doing
461 -- something wrong, so don't delete anything
462 filterWarnings ((xs, []) : zs) = (xs, []) : filterWarnings zs
463 filterWarnings ((xs, ys) : zs) = case filter wantedWarning ys of
464 [] -> filterWarnings zs
465 ys' -> (xs, ys') : filterWarnings zs
466
467 unChunkWarnings :: [([String], [String])] -> [String]
468 unChunkWarnings [] = []
469 unChunkWarnings ((xs, ys) : zs) = xs ++ ys ++ unChunkWarnings zs
470
471 loc_stack_start s = "In file included from " `isPrefixOf` s
472 loc_start_continuation s = " from " `isPrefixOf` s
473 wantedWarning w
474 | "warning: call-clobbered register used" `isContainedIn` w = False
475 | otherwise = True
476
477 isContainedIn :: String -> String -> Bool
478 xs `isContainedIn` ys = any (xs `isPrefixOf`) (tails ys)
479
480 askCc :: DynFlags -> [Option] -> IO String
481 askCc dflags args = do
482 let (p,args0) = pgm_c dflags
483 args1 = map Option (getOpts dflags opt_c)
484 args2 = args0 ++ args1 ++ args
485 mb_env <- getGccEnv args2
486 runSomethingWith dflags "gcc" p args2 $ \real_args ->
487 readCreateProcessWithExitCode' (proc p real_args){ env = mb_env }
488
489 -- Similar to System.Process.readCreateProcessWithExitCode, but stderr is
490 -- inherited from the parent process, and output to stderr is not captured.
491 readCreateProcessWithExitCode'
492 :: CreateProcess
493 -> IO (ExitCode, String) -- ^ stdout
494 readCreateProcessWithExitCode' proc = do
495 (_, Just outh, _, pid) <-
496 createProcess proc{ std_out = CreatePipe }
497
498 -- fork off a thread to start consuming the output
499 output <- hGetContents outh
500 outMVar <- newEmptyMVar
501 _ <- forkIO $ evaluate (length output) >> putMVar outMVar ()
502
503 -- wait on the output
504 takeMVar outMVar
505 hClose outh
506
507 -- wait on the process
508 ex <- waitForProcess pid
509
510 return (ex, output)
511
512 replaceVar :: (String, String) -> [(String, String)] -> [(String, String)]
513 replaceVar (var, value) env =
514 (var, value) : filter (\(var',_) -> var /= var') env
515
516 -- | Version of @System.Process.readProcessWithExitCode@ that takes a
517 -- key-value tuple to insert into the environment.
518 readProcessEnvWithExitCode
519 :: String -- ^ program path
520 -> [String] -- ^ program args
521 -> (String, String) -- ^ addition to the environment
522 -> IO (ExitCode, String, String) -- ^ (exit_code, stdout, stderr)
523 readProcessEnvWithExitCode prog args env_update = do
524 current_env <- getEnvironment
525 readCreateProcessWithExitCode (proc prog args) {
526 env = Just (replaceVar env_update current_env) } ""
527
528 -- Don't let gcc localize version info string, #8825
529 c_locale_env :: (String, String)
530 c_locale_env = ("LANGUAGE", "C")
531
532 -- If the -B<dir> option is set, add <dir> to PATH. This works around
533 -- a bug in gcc on Windows Vista where it can't find its auxiliary
534 -- binaries (see bug #1110).
535 getGccEnv :: [Option] -> IO (Maybe [(String,String)])
536 getGccEnv opts =
537 if null b_dirs
538 then return Nothing
539 else do env <- getEnvironment
540 return (Just (map mangle_path env))
541 where
542 (b_dirs, _) = partitionWith get_b_opt opts
543
544 get_b_opt (Option ('-':'B':dir)) = Left dir
545 get_b_opt other = Right other
546
547 mangle_path (path,paths) | map toUpper path == "PATH"
548 = (path, '\"' : head b_dirs ++ "\";" ++ paths)
549 mangle_path other = other
550
551 runSplit :: DynFlags -> [Option] -> IO ()
552 runSplit dflags args = do
553 let (p,args0) = pgm_s dflags
554 runSomething dflags "Splitter" p (args0++args)
555
556 runAs :: DynFlags -> [Option] -> IO ()
557 runAs dflags args = do
558 let (p,args0) = pgm_a dflags
559 args1 = map Option (getOpts dflags opt_a)
560 args2 = args0 ++ args1 ++ args
561 mb_env <- getGccEnv args2
562 runSomethingFiltered dflags id "Assembler" p args2 mb_env
563
564 -- | Run the LLVM Optimiser
565 runLlvmOpt :: DynFlags -> [Option] -> IO ()
566 runLlvmOpt dflags args = do
567 let (p,args0) = pgm_lo dflags
568 args1 = map Option (getOpts dflags opt_lo)
569 runSomething dflags "LLVM Optimiser" p (args0 ++ args1 ++ args)
570
571 -- | Run the LLVM Compiler
572 runLlvmLlc :: DynFlags -> [Option] -> IO ()
573 runLlvmLlc dflags args = do
574 let (p,args0) = pgm_lc dflags
575 args1 = map Option (getOpts dflags opt_lc)
576 runSomething dflags "LLVM Compiler" p (args0 ++ args1 ++ args)
577
578 -- | Run the clang compiler (used as an assembler for the LLVM
579 -- backend on OS X as LLVM doesn't support the OS X system
580 -- assembler)
581 runClang :: DynFlags -> [Option] -> IO ()
582 runClang dflags args = do
583 -- we simply assume its available on the PATH
584 let clang = "clang"
585 -- be careful what options we call clang with
586 -- see #5903 and #7617 for bugs caused by this.
587 (_,args0) = pgm_a dflags
588 args1 = map Option (getOpts dflags opt_a)
589 args2 = args0 ++ args1 ++ args
590 mb_env <- getGccEnv args2
591 Exception.catch (do
592 runSomethingFiltered dflags id "Clang (Assembler)" clang args2 mb_env
593 )
594 (\(err :: SomeException) -> do
595 errorMsg dflags $
596 text ("Error running clang! you need clang installed to use the" ++
597 " LLVM backend") $+$
598 text "(or GHC tried to execute clang incorrectly)"
599 throwIO err
600 )
601
602 -- | Figure out which version of LLVM we are running this session
603 figureLlvmVersion :: DynFlags -> IO (Maybe (Int, Int))
604 figureLlvmVersion dflags = do
605 let (pgm,opts) = pgm_lc dflags
606 args = filter notNull (map showOpt opts)
607 -- we grab the args even though they should be useless just in
608 -- case the user is using a customised 'llc' that requires some
609 -- of the options they've specified. llc doesn't care what other
610 -- options are specified when '-version' is used.
611 args' = args ++ ["-version"]
612 ver <- catchIO (do
613 (pin, pout, perr, _) <- runInteractiveProcess pgm args'
614 Nothing Nothing
615 {- > llc -version
616 LLVM (http://llvm.org/):
617 LLVM version 3.5.2
618 ...
619 -}
620 hSetBinaryMode pout False
621 _ <- hGetLine pout
622 vline <- dropWhile (not . isDigit) `fmap` hGetLine pout
623 v <- case span (/= '.') vline of
624 ("",_) -> fail "no digits!"
625 (x,y) -> return (read x
626 , read $ takeWhile isDigit $ drop 1 y)
627
628 hClose pin
629 hClose pout
630 hClose perr
631 return $ Just v
632 )
633 (\err -> do
634 debugTraceMsg dflags 2
635 (text "Error (figuring out LLVM version):" <+>
636 text (show err))
637 errorMsg dflags $ vcat
638 [ text "Warning:", nest 9 $
639 text "Couldn't figure out LLVM version!" $$
640 text ("Make sure you have installed LLVM " ++
641 llvmVersionStr supportedLlvmVersion) ]
642 return Nothing)
643 return ver
644
645 {- Note [Windows stack usage]
646
647 See: Trac #8870 (and #8834 for related info) and #12186
648
649 On Windows, occasionally we need to grow the stack. In order to do
650 this, we would normally just bump the stack pointer - but there's a
651 catch on Windows.
652
653 If the stack pointer is bumped by more than a single page, then the
654 pages between the initial pointer and the resulting location must be
655 properly committed by the Windows virtual memory subsystem. This is
656 only needed in the event we bump by more than one page (i.e 4097 bytes
657 or more).
658
659 Windows compilers solve this by emitting a call to a special function
660 called _chkstk, which does this committing of the pages for you.
661
662 The reason this was causing a segfault was because due to the fact the
663 new code generator tends to generate larger functions, we needed more
664 stack space in GHC itself. In the x86 codegen, we needed approximately
665 ~12kb of stack space in one go, which caused the process to segfault,
666 as the intervening pages were not committed.
667
668 GCC can emit such a check for us automatically but only when the flag
669 -fstack-check is used.
670
671 See https://gcc.gnu.org/onlinedocs/gnat_ugn/Stack-Overflow-Checking.html
672 for more information.
673
674 -}
675
676 {- Note [Run-time linker info]
677
678 See also: Trac #5240, Trac #6063, Trac #10110
679
680 Before 'runLink', we need to be sure to get the relevant information
681 about the linker we're using at runtime to see if we need any extra
682 options. For example, GNU ld requires '--reduce-memory-overheads' and
683 '--hash-size=31' in order to use reasonable amounts of memory (see
684 trac #5240.) But this isn't supported in GNU gold.
685
686 Generally, the linker changing from what was detected at ./configure
687 time has always been possible using -pgml, but on Linux it can happen
688 'transparently' by installing packages like binutils-gold, which
689 change what /usr/bin/ld actually points to.
690
691 Clang vs GCC notes:
692
693 For gcc, 'gcc -Wl,--version' gives a bunch of output about how to
694 invoke the linker before the version information string. For 'clang',
695 the version information for 'ld' is all that's output. For this
696 reason, we typically need to slurp up all of the standard error output
697 and look through it.
698
699 Other notes:
700
701 We cache the LinkerInfo inside DynFlags, since clients may link
702 multiple times. The definition of LinkerInfo is there to avoid a
703 circular dependency.
704
705 -}
706
707 {- Note [ELF needed shared libs]
708
709 Some distributions change the link editor's default handling of
710 ELF DT_NEEDED tags to include only those shared objects that are
711 needed to resolve undefined symbols. For Template Haskell we need
712 the last temporary shared library also if it is not needed for the
713 currently linked temporary shared library. We specify --no-as-needed
714 to override the default. This flag exists in GNU ld and GNU gold.
715
716 The flag is only needed on ELF systems. On Windows (PE) and Mac OS X
717 (Mach-O) the flag is not needed.
718
719 -}
720
721 {- Note [Windows static libGCC]
722
723 The GCC versions being upgraded to in #10726 are configured with
724 dynamic linking of libgcc supported. This results in libgcc being
725 linked dynamically when a shared library is created.
726
727 This introduces thus an extra dependency on GCC dll that was not
728 needed before by shared libraries created with GHC. This is a particular
729 issue on Windows because you get a non-obvious error due to this missing
730 dependency. This dependent dll is also not commonly on your path.
731
732 For this reason using the static libgcc is preferred as it preserves
733 the same behaviour that existed before. There are however some very good
734 reasons to have the shared version as well as described on page 181 of
735 https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc.pdf :
736
737 "There are several situations in which an application should use the
738 shared ‘libgcc’ instead of the static version. The most common of these
739 is when the application wishes to throw and catch exceptions across different
740 shared libraries. In that case, each of the libraries as well as the application
741 itself should use the shared ‘libgcc’. "
742
743 -}
744
745 neededLinkArgs :: LinkerInfo -> [Option]
746 neededLinkArgs (GnuLD o) = o
747 neededLinkArgs (GnuGold o) = o
748 neededLinkArgs (DarwinLD o) = o
749 neededLinkArgs (SolarisLD o) = o
750 neededLinkArgs (AixLD o) = o
751 neededLinkArgs UnknownLD = []
752
753 -- Grab linker info and cache it in DynFlags.
754 getLinkerInfo :: DynFlags -> IO LinkerInfo
755 getLinkerInfo dflags = do
756 info <- readIORef (rtldInfo dflags)
757 case info of
758 Just v -> return v
759 Nothing -> do
760 v <- getLinkerInfo' dflags
761 writeIORef (rtldInfo dflags) (Just v)
762 return v
763
764 -- See Note [Run-time linker info].
765 getLinkerInfo' :: DynFlags -> IO LinkerInfo
766 getLinkerInfo' dflags = do
767 let platform = targetPlatform dflags
768 os = platformOS platform
769 (pgm,args0) = pgm_l dflags
770 args1 = map Option (getOpts dflags opt_l)
771 args2 = args0 ++ args1
772 args3 = filter notNull (map showOpt args2)
773
774 -- Try to grab the info from the process output.
775 parseLinkerInfo stdo _stde _exitc
776 | any ("GNU ld" `isPrefixOf`) stdo =
777 -- GNU ld specifically needs to use less memory. This especially
778 -- hurts on small object files. Trac #5240.
779 -- Set DT_NEEDED for all shared libraries. Trac #10110.
780 -- TODO: Investigate if these help or hurt when using split sections.
781 return (GnuLD $ map Option ["-Wl,--hash-size=31",
782 "-Wl,--reduce-memory-overheads",
783 -- ELF specific flag
784 -- see Note [ELF needed shared libs]
785 "-Wl,--no-as-needed"])
786
787 | any ("GNU gold" `isPrefixOf`) stdo =
788 -- GNU gold only needs --no-as-needed. Trac #10110.
789 -- ELF specific flag, see Note [ELF needed shared libs]
790 return (GnuGold [Option "-Wl,--no-as-needed"])
791
792 -- Unknown linker.
793 | otherwise = fail "invalid --version output, or linker is unsupported"
794
795 -- Process the executable call
796 info <- catchIO (do
797 case os of
798 OSSolaris2 ->
799 -- Solaris uses its own Solaris linker. Even all
800 -- GNU C are recommended to configure with Solaris
801 -- linker instead of using GNU binutils linker. Also
802 -- all GCC distributed with Solaris follows this rule
803 -- precisely so we assume here, the Solaris linker is
804 -- used.
805 return $ SolarisLD []
806 OSAIX ->
807 -- IBM AIX uses its own non-binutils linker as well
808 return $ AixLD []
809 OSDarwin ->
810 -- Darwin has neither GNU Gold or GNU LD, but a strange linker
811 -- that doesn't support --version. We can just assume that's
812 -- what we're using.
813 return $ DarwinLD []
814 OSiOS ->
815 -- Ditto for iOS
816 return $ DarwinLD []
817 OSMinGW32 ->
818 -- GHC doesn't support anything but GNU ld on Windows anyway.
819 -- Process creation is also fairly expensive on win32, so
820 -- we short-circuit here.
821 return $ GnuLD $ map Option
822 [ -- Reduce ld memory usage
823 "-Wl,--hash-size=31"
824 , "-Wl,--reduce-memory-overheads"
825 -- Emit gcc stack checks
826 -- Note [Windows stack usage]
827 , "-fstack-check"
828 -- Force static linking of libGCC
829 -- Note [Windows static libGCC]
830 , "-static-libgcc" ]
831 _ -> do
832 -- In practice, we use the compiler as the linker here. Pass
833 -- -Wl,--version to get linker version info.
834 (exitc, stdo, stde) <- readProcessEnvWithExitCode pgm
835 (["-Wl,--version"] ++ args3)
836 c_locale_env
837 -- Split the output by lines to make certain kinds
838 -- of processing easier. In particular, 'clang' and 'gcc'
839 -- have slightly different outputs for '-Wl,--version', but
840 -- it's still easy to figure out.
841 parseLinkerInfo (lines stdo) (lines stde) exitc
842 )
843 (\err -> do
844 debugTraceMsg dflags 2
845 (text "Error (figuring out linker information):" <+>
846 text (show err))
847 errorMsg dflags $ hang (text "Warning:") 9 $
848 text "Couldn't figure out linker information!" $$
849 text "Make sure you're using GNU ld, GNU gold" <+>
850 text "or the built in OS X linker, etc."
851 return UnknownLD)
852 return info
853
854 -- Grab compiler info and cache it in DynFlags.
855 getCompilerInfo :: DynFlags -> IO CompilerInfo
856 getCompilerInfo dflags = do
857 info <- readIORef (rtccInfo dflags)
858 case info of
859 Just v -> return v
860 Nothing -> do
861 v <- getCompilerInfo' dflags
862 writeIORef (rtccInfo dflags) (Just v)
863 return v
864
865 -- See Note [Run-time linker info].
866 getCompilerInfo' :: DynFlags -> IO CompilerInfo
867 getCompilerInfo' dflags = do
868 let (pgm,_) = pgm_c dflags
869 -- Try to grab the info from the process output.
870 parseCompilerInfo _stdo stde _exitc
871 -- Regular GCC
872 | any ("gcc version" `isInfixOf`) stde =
873 return GCC
874 -- Regular clang
875 | any ("clang version" `isInfixOf`) stde =
876 return Clang
877 -- XCode 5.1 clang
878 | any ("Apple LLVM version 5.1" `isPrefixOf`) stde =
879 return AppleClang51
880 -- XCode 5 clang
881 | any ("Apple LLVM version" `isPrefixOf`) stde =
882 return AppleClang
883 -- XCode 4.1 clang
884 | any ("Apple clang version" `isPrefixOf`) stde =
885 return AppleClang
886 -- Unknown linker.
887 | otherwise = fail "invalid -v output, or compiler is unsupported"
888
889 -- Process the executable call
890 info <- catchIO (do
891 (exitc, stdo, stde) <-
892 readProcessEnvWithExitCode pgm ["-v"] c_locale_env
893 -- Split the output by lines to make certain kinds
894 -- of processing easier.
895 parseCompilerInfo (lines stdo) (lines stde) exitc
896 )
897 (\err -> do
898 debugTraceMsg dflags 2
899 (text "Error (figuring out C compiler information):" <+>
900 text (show err))
901 errorMsg dflags $ hang (text "Warning:") 9 $
902 text "Couldn't figure out C compiler information!" $$
903 text "Make sure you're using GNU gcc, or clang"
904 return UnknownCC)
905 return info
906
907 runLink :: DynFlags -> [Option] -> IO ()
908 runLink dflags args = do
909 -- See Note [Run-time linker info]
910 linkargs <- neededLinkArgs `fmap` getLinkerInfo dflags
911 let (p,args0) = pgm_l dflags
912 args1 = map Option (getOpts dflags opt_l)
913 args2 = args0 ++ linkargs ++ args1 ++ args
914 args3 = argFixup args2 []
915 mb_env <- getGccEnv args3
916 runSomethingResponseFile dflags ld_filter "Linker" p args3 mb_env
917 where
918 testLib lib = "-l" `isPrefixOf` lib || ".a" `isSuffixOf` lib
919 {- GHC is just blindly appending linker arguments from libraries and
920 the commandline together. This results in very problematic link orders
921 which will cause incorrect linking. Since we're changing the link
922 arguments anyway, let's just make sure libraries are last.
923 This functions moves libraries on the link all the way back
924 but keeps the order amongst them the same. -}
925 argFixup [] r = [] ++ r
926 -- retain any lib in "-o" position.
927 argFixup (o@(Option "-o"):o'@(FileOption _ _):xs) r = o:o':argFixup xs r
928 argFixup (o@(Option opt):xs) r = if testLib opt
929 then argFixup xs (r ++ [o])
930 else o:argFixup xs r
931 argFixup (o@(FileOption _ opt):xs) r = if testLib opt
932 then argFixup xs (r ++ [o])
933 else o:argFixup xs r
934 ld_filter = case (platformOS (targetPlatform dflags)) of
935 OSSolaris2 -> sunos_ld_filter
936 _ -> id
937 {-
938 SunOS/Solaris ld emits harmless warning messages about unresolved
939 symbols in case of compiling into shared library when we do not
940 link against all the required libs. That is the case of GHC which
941 does not link against RTS library explicitly in order to be able to
942 choose the library later based on binary application linking
943 parameters. The warnings look like:
944
945 Undefined first referenced
946 symbol in file
947 stg_ap_n_fast ./T2386_Lib.o
948 stg_upd_frame_info ./T2386_Lib.o
949 templatezmhaskell_LanguageziHaskellziTHziLib_litE_closure ./T2386_Lib.o
950 templatezmhaskell_LanguageziHaskellziTHziLib_appE_closure ./T2386_Lib.o
951 templatezmhaskell_LanguageziHaskellziTHziLib_conE_closure ./T2386_Lib.o
952 templatezmhaskell_LanguageziHaskellziTHziSyntax_mkNameGzud_closure ./T2386_Lib.o
953 newCAF ./T2386_Lib.o
954 stg_bh_upd_frame_info ./T2386_Lib.o
955 stg_ap_ppp_fast ./T2386_Lib.o
956 templatezmhaskell_LanguageziHaskellziTHziLib_stringL_closure ./T2386_Lib.o
957 stg_ap_p_fast ./T2386_Lib.o
958 stg_ap_pp_fast ./T2386_Lib.o
959 ld: warning: symbol referencing errors
960
961 this is actually coming from T2386 testcase. The emitting of those
962 warnings is also a reason why so many TH testcases fail on Solaris.
963
964 Following filter code is SunOS/Solaris linker specific and should
965 filter out only linker warnings. Please note that the logic is a
966 little bit more complex due to the simple reason that we need to preserve
967 any other linker emitted messages. If there are any. Simply speaking
968 if we see "Undefined" and later "ld: warning:..." then we omit all
969 text between (including) the marks. Otherwise we copy the whole output.
970 -}
971 sunos_ld_filter :: String -> String
972 sunos_ld_filter = unlines . sunos_ld_filter' . lines
973 sunos_ld_filter' x = if (undefined_found x && ld_warning_found x)
974 then (ld_prefix x) ++ (ld_postfix x)
975 else x
976 breakStartsWith x y = break (isPrefixOf x) y
977 ld_prefix = fst . breakStartsWith "Undefined"
978 undefined_found = not . null . snd . breakStartsWith "Undefined"
979 ld_warn_break = breakStartsWith "ld: warning: symbol referencing errors"
980 ld_postfix = tail . snd . ld_warn_break
981 ld_warning_found = not . null . snd . ld_warn_break
982
983
984 runLibtool :: DynFlags -> [Option] -> IO ()
985 runLibtool dflags args = do
986 linkargs <- neededLinkArgs `fmap` getLinkerInfo dflags
987 let args1 = map Option (getOpts dflags opt_l)
988 args2 = [Option "-static"] ++ args1 ++ args ++ linkargs
989 libtool = pgm_libtool dflags
990 mb_env <- getGccEnv args2
991 runSomethingFiltered dflags id "Linker" libtool args2 mb_env
992
993 runMkDLL :: DynFlags -> [Option] -> IO ()
994 runMkDLL dflags args = do
995 let (p,args0) = pgm_dll dflags
996 args1 = args0 ++ args
997 mb_env <- getGccEnv (args0++args)
998 runSomethingFiltered dflags id "Make DLL" p args1 mb_env
999
1000 runWindres :: DynFlags -> [Option] -> IO ()
1001 runWindres dflags args = do
1002 let (gcc, gcc_args) = pgm_c dflags
1003 windres = pgm_windres dflags
1004 opts = map Option (getOpts dflags opt_windres)
1005 quote x = "\"" ++ x ++ "\""
1006 args' = -- If windres.exe and gcc.exe are in a directory containing
1007 -- spaces then windres fails to run gcc. We therefore need
1008 -- to tell it what command to use...
1009 Option ("--preprocessor=" ++
1010 unwords (map quote (gcc :
1011 map showOpt gcc_args ++
1012 map showOpt opts ++
1013 ["-E", "-xc", "-DRC_INVOKED"])))
1014 -- ...but if we do that then if windres calls popen then
1015 -- it can't understand the quoting, so we have to use
1016 -- --use-temp-file so that it interprets it correctly.
1017 -- See #1828.
1018 : Option "--use-temp-file"
1019 : args
1020 mb_env <- getGccEnv gcc_args
1021 runSomethingFiltered dflags id "Windres" windres args' mb_env
1022
1023 touch :: DynFlags -> String -> String -> IO ()
1024 touch dflags purpose arg =
1025 runSomething dflags purpose (pgm_T dflags) [FileOption "" arg]
1026
1027 copy :: DynFlags -> String -> FilePath -> FilePath -> IO ()
1028 copy dflags purpose from to = copyWithHeader dflags purpose Nothing from to
1029
1030 copyWithHeader :: DynFlags -> String -> Maybe String -> FilePath -> FilePath
1031 -> IO ()
1032 copyWithHeader dflags purpose maybe_header from to = do
1033 showPass dflags purpose
1034
1035 hout <- openBinaryFile to WriteMode
1036 hin <- openBinaryFile from ReadMode
1037 ls <- hGetContents hin -- inefficient, but it'll do for now. ToDo: speed up
1038 maybe (return ()) (header hout) maybe_header
1039 hPutStr hout ls
1040 hClose hout
1041 hClose hin
1042 where
1043 -- write the header string in UTF-8. The header is something like
1044 -- {-# LINE "foo.hs" #-}
1045 -- and we want to make sure a Unicode filename isn't mangled.
1046 header h str = do
1047 hSetEncoding h utf8
1048 hPutStr h str
1049 hSetBinaryMode h True
1050
1051
1052
1053 {-
1054 ************************************************************************
1055 * *
1056 \subsection{Managing temporary files
1057 * *
1058 ************************************************************************
1059 -}
1060
1061 cleanTempDirs :: DynFlags -> IO ()
1062 cleanTempDirs dflags
1063 = unless (gopt Opt_KeepTmpFiles dflags)
1064 $ mask_
1065 $ do let ref = dirsToClean dflags
1066 ds <- atomicModifyIORef' ref $ \ds -> (Map.empty, ds)
1067 removeTmpDirs dflags (Map.elems ds)
1068
1069 cleanTempFiles :: DynFlags -> IO ()
1070 cleanTempFiles dflags
1071 = unless (gopt Opt_KeepTmpFiles dflags)
1072 $ mask_
1073 $ do let ref = filesToClean dflags
1074 fs <- atomicModifyIORef' ref $ \fs -> ([],fs)
1075 removeTmpFiles dflags fs
1076
1077 cleanTempFilesExcept :: DynFlags -> [FilePath] -> IO ()
1078 cleanTempFilesExcept dflags dont_delete
1079 = unless (gopt Opt_KeepTmpFiles dflags)
1080 $ mask_
1081 $ do let ref = filesToClean dflags
1082 to_delete <- atomicModifyIORef' ref $ \files ->
1083 let (to_keep,to_delete) = partition (`elem` dont_delete) files
1084 in (to_keep,to_delete)
1085 removeTmpFiles dflags to_delete
1086
1087
1088 -- Return a unique numeric temp file suffix
1089 newTempSuffix :: DynFlags -> IO Int
1090 newTempSuffix dflags = atomicModifyIORef' (nextTempSuffix dflags) $ \n -> (n+1,n)
1091
1092 -- Find a temporary name that doesn't already exist.
1093 newTempName :: DynFlags -> Suffix -> IO FilePath
1094 newTempName dflags extn
1095 = do d <- getTempDir dflags
1096 findTempName (d </> "ghc_") -- See Note [Deterministic base name]
1097 where
1098 findTempName :: FilePath -> IO FilePath
1099 findTempName prefix
1100 = do n <- newTempSuffix dflags
1101 let filename = prefix ++ show n <.> extn
1102 b <- doesFileExist filename
1103 if b then findTempName prefix
1104 else do -- clean it up later
1105 consIORef (filesToClean dflags) filename
1106 return filename
1107
1108 newTempLibName :: DynFlags -> Suffix -> IO (FilePath, FilePath, String)
1109 newTempLibName dflags extn
1110 = do d <- getTempDir dflags
1111 findTempName d ("ghc_")
1112 where
1113 findTempName :: FilePath -> String -> IO (FilePath, FilePath, String)
1114 findTempName dir prefix
1115 = do n <- newTempSuffix dflags -- See Note [Deterministic base name]
1116 let libname = prefix ++ show n
1117 filename = dir </> "lib" ++ libname <.> extn
1118 b <- doesFileExist filename
1119 if b then findTempName dir prefix
1120 else do -- clean it up later
1121 consIORef (filesToClean dflags) filename
1122 return (filename, dir, libname)
1123
1124
1125 -- Return our temporary directory within tmp_dir, creating one if we
1126 -- don't have one yet.
1127 getTempDir :: DynFlags -> IO FilePath
1128 getTempDir dflags = do
1129 mapping <- readIORef dir_ref
1130 case Map.lookup tmp_dir mapping of
1131 Nothing -> do
1132 pid <- getProcessID
1133 let prefix = tmp_dir </> "ghc" ++ show pid ++ "_"
1134 mask_ $ mkTempDir prefix
1135 Just dir -> return dir
1136 where
1137 tmp_dir = tmpDir dflags
1138 dir_ref = dirsToClean dflags
1139
1140 mkTempDir :: FilePath -> IO FilePath
1141 mkTempDir prefix = do
1142 n <- newTempSuffix dflags
1143 let our_dir = prefix ++ show n
1144
1145 -- 1. Speculatively create our new directory.
1146 createDirectory our_dir
1147
1148 -- 2. Update the dirsToClean mapping unless an entry already exists
1149 -- (i.e. unless another thread beat us to it).
1150 their_dir <- atomicModifyIORef' dir_ref $ \mapping ->
1151 case Map.lookup tmp_dir mapping of
1152 Just dir -> (mapping, Just dir)
1153 Nothing -> (Map.insert tmp_dir our_dir mapping, Nothing)
1154
1155 -- 3. If there was an existing entry, return it and delete the
1156 -- directory we created. Otherwise return the directory we created.
1157 case their_dir of
1158 Nothing -> do
1159 debugTraceMsg dflags 2 $
1160 text "Created temporary directory:" <+> text our_dir
1161 return our_dir
1162 Just dir -> do
1163 removeDirectory our_dir
1164 return dir
1165 `catchIO` \e -> if isAlreadyExistsError e
1166 then mkTempDir prefix else ioError e
1167
1168 -- Note [Deterministic base name]
1169 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1170 --
1171 -- The filename of temporary files, especially the basename of C files, can end
1172 -- up in the output in some form, e.g. as part of linker debug information. In the
1173 -- interest of bit-wise exactly reproducible compilation (#4012), the basename of
1174 -- the temporary file no longer contains random information (it used to contain
1175 -- the process id).
1176 --
1177 -- This is ok, as the temporary directory used contains the pid (see getTempDir).
1178
1179 addFilesToClean :: DynFlags -> [FilePath] -> IO ()
1180 -- May include wildcards [used by DriverPipeline.run_phase SplitMangle]
1181 addFilesToClean dflags new_files
1182 = atomicModifyIORef' (filesToClean dflags) $ \files -> (new_files++files, ())
1183
1184 removeTmpDirs :: DynFlags -> [FilePath] -> IO ()
1185 removeTmpDirs dflags ds
1186 = traceCmd dflags "Deleting temp dirs"
1187 ("Deleting: " ++ unwords ds)
1188 (mapM_ (removeWith dflags removeDirectory) ds)
1189
1190 removeTmpFiles :: DynFlags -> [FilePath] -> IO ()
1191 removeTmpFiles dflags fs
1192 = warnNon $
1193 traceCmd dflags "Deleting temp files"
1194 ("Deleting: " ++ unwords deletees)
1195 (mapM_ (removeWith dflags removeFile) deletees)
1196 where
1197 -- Flat out refuse to delete files that are likely to be source input
1198 -- files (is there a worse bug than having a compiler delete your source
1199 -- files?)
1200 --
1201 -- Deleting source files is a sign of a bug elsewhere, so prominently flag
1202 -- the condition.
1203 warnNon act
1204 | null non_deletees = act
1205 | otherwise = do
1206 putMsg dflags (text "WARNING - NOT deleting source files:" <+> hsep (map text non_deletees))
1207 act
1208
1209 (non_deletees, deletees) = partition isHaskellUserSrcFilename fs
1210
1211 removeWith :: DynFlags -> (FilePath -> IO ()) -> FilePath -> IO ()
1212 removeWith dflags remover f = remover f `catchIO`
1213 (\e ->
1214 let msg = if isDoesNotExistError e
1215 then text "Warning: deleting non-existent" <+> text f
1216 else text "Warning: exception raised when deleting"
1217 <+> text f <> colon
1218 $$ text (show e)
1219 in debugTraceMsg dflags 2 msg
1220 )
1221
1222 -----------------------------------------------------------------------------
1223 -- Running an external program
1224
1225 runSomething :: DynFlags
1226 -> String -- For -v message
1227 -> String -- Command name (possibly a full path)
1228 -- assumed already dos-ified
1229 -> [Option] -- Arguments
1230 -- runSomething will dos-ify them
1231 -> IO ()
1232
1233 runSomething dflags phase_name pgm args =
1234 runSomethingFiltered dflags id phase_name pgm args Nothing
1235
1236 -- | Run a command, placing the arguments in an external response file.
1237 --
1238 -- This command is used in order to avoid overlong command line arguments on
1239 -- Windows. The command line arguments are first written to an external,
1240 -- temporary response file, and then passed to the linker via @filepath.
1241 -- response files for passing them in. See:
1242 --
1243 -- https://gcc.gnu.org/wiki/Response_Files
1244 -- https://ghc.haskell.org/trac/ghc/ticket/10777
1245 runSomethingResponseFile
1246 :: DynFlags -> (String->String) -> String -> String -> [Option]
1247 -> Maybe [(String,String)] -> IO ()
1248
1249 runSomethingResponseFile dflags filter_fn phase_name pgm args mb_env =
1250 runSomethingWith dflags phase_name pgm args $ \real_args -> do
1251 fp <- getResponseFile real_args
1252 let args = ['@':fp]
1253 r <- builderMainLoop dflags filter_fn pgm args mb_env
1254 return (r,())
1255 where
1256 getResponseFile args = do
1257 fp <- newTempName dflags "rsp"
1258 withFile fp WriteMode $ \h -> do
1259 hSetEncoding h utf8
1260 hPutStr h $ unlines $ map escape args
1261 return fp
1262
1263 -- Note: Response files have backslash-escaping, double quoting, and are
1264 -- whitespace separated (some implementations use newline, others any
1265 -- whitespace character). Therefore, escape any backslashes, newlines, and
1266 -- double quotes in the argument, and surround the content with double
1267 -- quotes.
1268 --
1269 -- Another possibility that could be considered would be to convert
1270 -- backslashes in the argument to forward slashes. This would generally do
1271 -- the right thing, since backslashes in general only appear in arguments
1272 -- as part of file paths on Windows, and the forward slash is accepted for
1273 -- those. However, escaping is more reliable, in case somehow a backslash
1274 -- appears in a non-file.
1275 escape x = concat
1276 [ "\""
1277 , concatMap
1278 (\c ->
1279 case c of
1280 '\\' -> "\\\\"
1281 '\n' -> "\\n"
1282 '\"' -> "\\\""
1283 _ -> [c])
1284 x
1285 , "\""
1286 ]
1287
1288 runSomethingFiltered
1289 :: DynFlags -> (String->String) -> String -> String -> [Option]
1290 -> Maybe [(String,String)] -> IO ()
1291
1292 runSomethingFiltered dflags filter_fn phase_name pgm args mb_env = do
1293 runSomethingWith dflags phase_name pgm args $ \real_args -> do
1294 r <- builderMainLoop dflags filter_fn pgm real_args mb_env
1295 return (r,())
1296
1297 runSomethingWith
1298 :: DynFlags -> String -> String -> [Option]
1299 -> ([String] -> IO (ExitCode, a))
1300 -> IO a
1301
1302 runSomethingWith dflags phase_name pgm args io = do
1303 let real_args = filter notNull (map showOpt args)
1304 cmdLine = showCommandForUser pgm real_args
1305 traceCmd dflags phase_name cmdLine $ handleProc pgm phase_name $ io real_args
1306
1307 handleProc :: String -> String -> IO (ExitCode, r) -> IO r
1308 handleProc pgm phase_name proc = do
1309 (rc, r) <- proc `catchIO` handler
1310 case rc of
1311 ExitSuccess{} -> return r
1312 ExitFailure n -> throwGhcExceptionIO (
1313 ProgramError ("`" ++ takeFileName pgm ++ "'" ++
1314 " failed in phase `" ++ phase_name ++ "'." ++
1315 " (Exit code: " ++ show n ++ ")"))
1316 where
1317 handler err =
1318 if IO.isDoesNotExistError err
1319 then does_not_exist
1320 else throwGhcExceptionIO (ProgramError $ show err)
1321
1322 does_not_exist = throwGhcExceptionIO (InstallationError ("could not execute: " ++ pgm))
1323
1324
1325 builderMainLoop :: DynFlags -> (String -> String) -> FilePath
1326 -> [String] -> Maybe [(String, String)]
1327 -> IO ExitCode
1328 builderMainLoop dflags filter_fn pgm real_args mb_env = do
1329 chan <- newChan
1330 (hStdIn, hStdOut, hStdErr, hProcess) <- runInteractiveProcess pgm real_args Nothing mb_env
1331
1332 -- and run a loop piping the output from the compiler to the log_action in DynFlags
1333 hSetBuffering hStdOut LineBuffering
1334 hSetBuffering hStdErr LineBuffering
1335 _ <- forkIO (readerProc chan hStdOut filter_fn)
1336 _ <- forkIO (readerProc chan hStdErr filter_fn)
1337 -- we don't want to finish until 2 streams have been completed
1338 -- (stdout and stderr)
1339 -- nor until 1 exit code has been retrieved.
1340 rc <- loop chan hProcess (2::Integer) (1::Integer) ExitSuccess
1341 -- after that, we're done here.
1342 hClose hStdIn
1343 hClose hStdOut
1344 hClose hStdErr
1345 return rc
1346 where
1347 -- status starts at zero, and increments each time either
1348 -- a reader process gets EOF, or the build proc exits. We wait
1349 -- for all of these to happen (status==3).
1350 -- ToDo: we should really have a contingency plan in case any of
1351 -- the threads dies, such as a timeout.
1352 loop _ _ 0 0 exitcode = return exitcode
1353 loop chan hProcess t p exitcode = do
1354 mb_code <- if p > 0
1355 then getProcessExitCode hProcess
1356 else return Nothing
1357 case mb_code of
1358 Just code -> loop chan hProcess t (p-1) code
1359 Nothing
1360 | t > 0 -> do
1361 msg <- readChan chan
1362 case msg of
1363 BuildMsg msg -> do
1364 log_action dflags dflags NoReason SevInfo noSrcSpan defaultUserStyle msg
1365 loop chan hProcess t p exitcode
1366 BuildError loc msg -> do
1367 log_action dflags dflags NoReason SevError (mkSrcSpan loc loc) defaultUserStyle msg
1368 loop chan hProcess t p exitcode
1369 EOF ->
1370 loop chan hProcess (t-1) p exitcode
1371 | otherwise -> loop chan hProcess t p exitcode
1372
1373 readerProc :: Chan BuildMessage -> Handle -> (String -> String) -> IO ()
1374 readerProc chan hdl filter_fn =
1375 (do str <- hGetContents hdl
1376 loop (linesPlatform (filter_fn str)) Nothing)
1377 `finally`
1378 writeChan chan EOF
1379 -- ToDo: check errors more carefully
1380 -- ToDo: in the future, the filter should be implemented as
1381 -- a stream transformer.
1382 where
1383 loop [] Nothing = return ()
1384 loop [] (Just err) = writeChan chan err
1385 loop (l:ls) in_err =
1386 case in_err of
1387 Just err@(BuildError srcLoc msg)
1388 | leading_whitespace l -> do
1389 loop ls (Just (BuildError srcLoc (msg $$ text l)))
1390 | otherwise -> do
1391 writeChan chan err
1392 checkError l ls
1393 Nothing -> do
1394 checkError l ls
1395 _ -> panic "readerProc/loop"
1396
1397 checkError l ls
1398 = case parseError l of
1399 Nothing -> do
1400 writeChan chan (BuildMsg (text l))
1401 loop ls Nothing
1402 Just (file, lineNum, colNum, msg) -> do
1403 let srcLoc = mkSrcLoc (mkFastString file) lineNum colNum
1404 loop ls (Just (BuildError srcLoc (text msg)))
1405
1406 leading_whitespace [] = False
1407 leading_whitespace (x:_) = isSpace x
1408
1409 parseError :: String -> Maybe (String, Int, Int, String)
1410 parseError s0 = case breakColon s0 of
1411 Just (filename, s1) ->
1412 case breakIntColon s1 of
1413 Just (lineNum, s2) ->
1414 case breakIntColon s2 of
1415 Just (columnNum, s3) ->
1416 Just (filename, lineNum, columnNum, s3)
1417 Nothing ->
1418 Just (filename, lineNum, 0, s2)
1419 Nothing -> Nothing
1420 Nothing -> Nothing
1421
1422 breakColon :: String -> Maybe (String, String)
1423 breakColon xs = case break (':' ==) xs of
1424 (ys, _:zs) -> Just (ys, zs)
1425 _ -> Nothing
1426
1427 breakIntColon :: String -> Maybe (Int, String)
1428 breakIntColon xs = case break (':' ==) xs of
1429 (ys, _:zs)
1430 | not (null ys) && all isAscii ys && all isDigit ys ->
1431 Just (read ys, zs)
1432 _ -> Nothing
1433
1434 data BuildMessage
1435 = BuildMsg !SDoc
1436 | BuildError !SrcLoc !SDoc
1437 | EOF
1438
1439 traceCmd :: DynFlags -> String -> String -> IO a -> IO a
1440 -- trace the command (at two levels of verbosity)
1441 traceCmd dflags phase_name cmd_line action
1442 = do { let verb = verbosity dflags
1443 ; showPass dflags phase_name
1444 ; debugTraceMsg dflags 3 (text cmd_line)
1445 ; case flushErr dflags of
1446 FlushErr io -> io
1447
1448 -- And run it!
1449 ; action `catchIO` handle_exn verb
1450 }
1451 where
1452 handle_exn _verb exn = do { debugTraceMsg dflags 2 (char '\n')
1453 ; debugTraceMsg dflags 2 (text "Failed:" <+> text cmd_line <+> text (show exn))
1454 ; throwGhcExceptionIO (ProgramError (show exn))}
1455
1456 {-
1457 ************************************************************************
1458 * *
1459 \subsection{Support code}
1460 * *
1461 ************************************************************************
1462 -}
1463
1464 -----------------------------------------------------------------------------
1465 -- Define getBaseDir :: IO (Maybe String)
1466
1467 getBaseDir :: IO (Maybe String)
1468 #if defined(mingw32_HOST_OS)
1469 -- Assuming we are running ghc, accessed by path $(stuff)/bin/ghc.exe,
1470 -- return the path $(stuff)/lib.
1471 getBaseDir = try_size 2048 -- plenty, PATH_MAX is 512 under Win32.
1472 where
1473 try_size size = allocaArray (fromIntegral size) $ \buf -> do
1474 ret <- c_GetModuleFileName nullPtr buf size
1475 case ret of
1476 0 -> return Nothing
1477 _ | ret < size -> do path <- peekCWString buf
1478 real <- getFinalPath path -- try to resolve symlinks paths
1479 return $ (Just . rootDir . sanitize . maybe path id) real
1480 | otherwise -> try_size (size * 2)
1481
1482 -- getFinalPath returns paths in full raw form.
1483 -- Unfortunately GHC isn't set up to handle these
1484 -- So if the call succeeded, we need to drop the
1485 -- \\?\ prefix.
1486 sanitize s = if "\\\\?\\" `isPrefixOf` s
1487 then drop 4 s
1488 else s
1489
1490 rootDir s = case splitFileName $ normalise s of
1491 (d, ghc_exe)
1492 | lower ghc_exe `elem` ["ghc.exe",
1493 "ghc-stage1.exe",
1494 "ghc-stage2.exe",
1495 "ghc-stage3.exe"] ->
1496 case splitFileName $ takeDirectory d of
1497 -- ghc is in $topdir/bin/ghc.exe
1498 (d', bin) | lower bin == "bin" -> takeDirectory d' </> "lib"
1499 _ -> fail
1500 _ -> fail
1501 where fail = panic ("can't decompose ghc.exe path: " ++ show s)
1502 lower = map toLower
1503
1504 foreign import WINDOWS_CCONV unsafe "windows.h GetModuleFileNameW"
1505 c_GetModuleFileName :: Ptr () -> CWString -> Word32 -> IO Word32
1506
1507 -- Attempt to resolve symlinks in order to find the actual location GHC
1508 -- is located at. See Trac #11759.
1509 getFinalPath :: FilePath -> IO (Maybe FilePath)
1510 getFinalPath name = do
1511 dllHwnd <- failIfNull "LoadLibray" $ loadLibrary "kernel32.dll"
1512 -- Note: The API GetFinalPathNameByHandleW is only available starting from Windows Vista.
1513 -- This means that we can't bind directly to it since it may be missing.
1514 -- Instead try to find it's address at runtime and if we don't succeed consider the
1515 -- function failed.
1516 addr_m <- (fmap Just $ failIfNull "getProcAddress" $ getProcAddress dllHwnd "GetFinalPathNameByHandleW")
1517 `catch` (\(_ :: SomeException) -> return Nothing)
1518 case addr_m of
1519 Nothing -> return Nothing
1520 Just addr -> do handle <- failIf (==iNVALID_HANDLE_VALUE) "CreateFile"
1521 $ createFile name
1522 gENERIC_READ
1523 fILE_SHARE_READ
1524 Nothing
1525 oPEN_EXISTING
1526 (fILE_ATTRIBUTE_NORMAL .|. fILE_FLAG_BACKUP_SEMANTICS)
1527 Nothing
1528 let fnPtr = makeGetFinalPathNameByHandle $ castPtrToFunPtr addr
1529 path <- Info.try "GetFinalPathName"
1530 (\buf len -> fnPtr handle buf len 0) 512
1531 `finally` closeHandle handle
1532 return $ Just path
1533
1534 type GetFinalPath = HANDLE -> LPTSTR -> DWORD -> DWORD -> IO DWORD
1535
1536 foreign import WINDOWS_CCONV unsafe "dynamic"
1537 makeGetFinalPathNameByHandle :: FunPtr GetFinalPath -> GetFinalPath
1538 #else
1539 getBaseDir = return Nothing
1540 #endif
1541
1542 #ifdef mingw32_HOST_OS
1543 foreign import ccall unsafe "_getpid" getProcessID :: IO Int -- relies on Int == Int32 on Windows
1544 #else
1545 getProcessID :: IO Int
1546 getProcessID = System.Posix.Internals.c_getpid >>= return . fromIntegral
1547 #endif
1548
1549 -- Divvy up text stream into lines, taking platform dependent
1550 -- line termination into account.
1551 linesPlatform :: String -> [String]
1552 #if !defined(mingw32_HOST_OS)
1553 linesPlatform ls = lines ls
1554 #else
1555 linesPlatform "" = []
1556 linesPlatform xs =
1557 case lineBreak xs of
1558 (as,xs1) -> as : linesPlatform xs1
1559 where
1560 lineBreak "" = ("","")
1561 lineBreak ('\r':'\n':xs) = ([],xs)
1562 lineBreak ('\n':xs) = ([],xs)
1563 lineBreak (x:xs) = let (as,bs) = lineBreak xs in (x:as,bs)
1564
1565 #endif
1566
1567 linkDynLib :: DynFlags -> [String] -> [UnitId] -> IO ()
1568 linkDynLib dflags0 o_files dep_packages
1569 = do
1570 let -- This is a rather ugly hack to fix dynamically linked
1571 -- GHC on Windows. If GHC is linked with -threaded, then
1572 -- it links against libHSrts_thr. But if base is linked
1573 -- against libHSrts, then both end up getting loaded,
1574 -- and things go wrong. We therefore link the libraries
1575 -- with the same RTS flags that we link GHC with.
1576 dflags1 = if cGhcThreaded then addWay' WayThreaded dflags0
1577 else dflags0
1578 dflags2 = if cGhcDebugged then addWay' WayDebug dflags1
1579 else dflags1
1580 dflags = updateWays dflags2
1581
1582 verbFlags = getVerbFlags dflags
1583 o_file = outputFile dflags
1584
1585 pkgs <- getPreloadPackagesAnd dflags dep_packages
1586
1587 let pkg_lib_paths = collectLibraryPaths pkgs
1588 let pkg_lib_path_opts = concatMap get_pkg_lib_path_opts pkg_lib_paths
1589 get_pkg_lib_path_opts l
1590 | ( osElfTarget (platformOS (targetPlatform dflags)) ||
1591 osMachOTarget (platformOS (targetPlatform dflags)) ) &&
1592 dynLibLoader dflags == SystemDependent &&
1593 WayDyn `elem` ways dflags
1594 = ["-L" ++ l, "-Wl,-rpath", "-Wl," ++ l]
1595 | otherwise = ["-L" ++ l]
1596
1597 let lib_paths = libraryPaths dflags
1598 let lib_path_opts = map ("-L"++) lib_paths
1599
1600 -- We don't want to link our dynamic libs against the RTS package,
1601 -- because the RTS lib comes in several flavours and we want to be
1602 -- able to pick the flavour when a binary is linked.
1603 -- On Windows we need to link the RTS import lib as Windows does
1604 -- not allow undefined symbols.
1605 -- The RTS library path is still added to the library search path
1606 -- above in case the RTS is being explicitly linked in (see #3807).
1607 let platform = targetPlatform dflags
1608 os = platformOS platform
1609 pkgs_no_rts = case os of
1610 OSMinGW32 ->
1611 pkgs
1612 _ ->
1613 filter ((/= rtsUnitId) . packageConfigId) pkgs
1614 let pkg_link_opts = let (package_hs_libs, extra_libs, other_flags) = collectLinkOpts dflags pkgs_no_rts
1615 in package_hs_libs ++ extra_libs ++ other_flags
1616
1617 -- probably _stub.o files
1618 -- and last temporary shared object file
1619 let extra_ld_inputs = ldInputs dflags
1620
1621 -- frameworks
1622 pkg_framework_opts <- getPkgFrameworkOpts dflags platform
1623 (map unitId pkgs)
1624 let framework_opts = getFrameworkOpts dflags platform
1625
1626 case os of
1627 OSMinGW32 -> do
1628 -------------------------------------------------------------
1629 -- Making a DLL
1630 -------------------------------------------------------------
1631 let output_fn = case o_file of
1632 Just s -> s
1633 Nothing -> "HSdll.dll"
1634
1635 runLink dflags (
1636 map Option verbFlags
1637 ++ [ Option "-o"
1638 , FileOption "" output_fn
1639 , Option "-shared"
1640 ] ++
1641 [ FileOption "-Wl,--out-implib=" (output_fn ++ ".a")
1642 | gopt Opt_SharedImplib dflags
1643 ]
1644 ++ map (FileOption "") o_files
1645
1646 -- Permit the linker to auto link _symbol to _imp_symbol
1647 -- This lets us link against DLLs without needing an "import library"
1648 ++ [Option "-Wl,--enable-auto-import"]
1649
1650 ++ extra_ld_inputs
1651 ++ map Option (
1652 lib_path_opts
1653 ++ pkg_lib_path_opts
1654 ++ pkg_link_opts
1655 ))
1656 OSDarwin -> do
1657 -------------------------------------------------------------------
1658 -- Making a darwin dylib
1659 -------------------------------------------------------------------
1660 -- About the options used for Darwin:
1661 -- -dynamiclib
1662 -- Apple's way of saying -shared
1663 -- -undefined dynamic_lookup:
1664 -- Without these options, we'd have to specify the correct
1665 -- dependencies for each of the dylibs. Note that we could
1666 -- (and should) do without this for all libraries except
1667 -- the RTS; all we need to do is to pass the correct
1668 -- HSfoo_dyn.dylib files to the link command.
1669 -- This feature requires Mac OS X 10.3 or later; there is
1670 -- a similar feature, -flat_namespace -undefined suppress,
1671 -- which works on earlier versions, but it has other
1672 -- disadvantages.
1673 -- -single_module
1674 -- Build the dynamic library as a single "module", i.e. no
1675 -- dynamic binding nonsense when referring to symbols from
1676 -- within the library. The NCG assumes that this option is
1677 -- specified (on i386, at least).
1678 -- -install_name
1679 -- Mac OS/X stores the path where a dynamic library is (to
1680 -- be) installed in the library itself. It's called the
1681 -- "install name" of the library. Then any library or
1682 -- executable that links against it before it's installed
1683 -- will search for it in its ultimate install location.
1684 -- By default we set the install name to the absolute path
1685 -- at build time, but it can be overridden by the
1686 -- -dylib-install-name option passed to ghc. Cabal does
1687 -- this.
1688 -------------------------------------------------------------------
1689
1690 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
1691
1692 instName <- case dylibInstallName dflags of
1693 Just n -> return n
1694 Nothing -> return $ "@rpath" `combine` (takeFileName output_fn)
1695 runLink dflags (
1696 map Option verbFlags
1697 ++ [ Option "-dynamiclib"
1698 , Option "-o"
1699 , FileOption "" output_fn
1700 ]
1701 ++ map Option o_files
1702 ++ [ Option "-undefined",
1703 Option "dynamic_lookup",
1704 Option "-single_module" ]
1705 ++ (if platformArch platform == ArchX86_64
1706 then [ ]
1707 else [ Option "-Wl,-read_only_relocs,suppress" ])
1708 ++ [ Option "-install_name", Option instName ]
1709 ++ map Option lib_path_opts
1710 ++ extra_ld_inputs
1711 ++ map Option framework_opts
1712 ++ map Option pkg_lib_path_opts
1713 ++ map Option pkg_link_opts
1714 ++ map Option pkg_framework_opts
1715 )
1716 OSiOS -> throwGhcExceptionIO (ProgramError "dynamic libraries are not supported on iOS target")
1717 _ -> do
1718 -------------------------------------------------------------------
1719 -- Making a DSO
1720 -------------------------------------------------------------------
1721
1722 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
1723 let bsymbolicFlag = -- we need symbolic linking to resolve
1724 -- non-PIC intra-package-relocations
1725 ["-Wl,-Bsymbolic"]
1726
1727 runLink dflags (
1728 map Option verbFlags
1729 ++ [ Option "-o"
1730 , FileOption "" output_fn
1731 ]
1732 ++ map Option o_files
1733 ++ [ Option "-shared" ]
1734 ++ map Option bsymbolicFlag
1735 -- Set the library soname. We use -h rather than -soname as
1736 -- Solaris 10 doesn't support the latter:
1737 ++ [ Option ("-Wl,-h," ++ takeFileName output_fn) ]
1738 ++ extra_ld_inputs
1739 ++ map Option lib_path_opts
1740 ++ map Option pkg_lib_path_opts
1741 ++ map Option pkg_link_opts
1742 )
1743
1744 getPkgFrameworkOpts :: DynFlags -> Platform -> [UnitId] -> IO [String]
1745 getPkgFrameworkOpts dflags platform dep_packages
1746 | platformUsesFrameworks platform = do
1747 pkg_framework_path_opts <- do
1748 pkg_framework_paths <- getPackageFrameworkPath dflags dep_packages
1749 return $ map ("-F" ++) pkg_framework_paths
1750
1751 pkg_framework_opts <- do
1752 pkg_frameworks <- getPackageFrameworks dflags dep_packages
1753 return $ concat [ ["-framework", fw] | fw <- pkg_frameworks ]
1754
1755 return (pkg_framework_path_opts ++ pkg_framework_opts)
1756
1757 | otherwise = return []
1758
1759 getFrameworkOpts :: DynFlags -> Platform -> [String]
1760 getFrameworkOpts dflags platform
1761 | platformUsesFrameworks platform = framework_path_opts ++ framework_opts
1762 | otherwise = []
1763 where
1764 framework_paths = frameworkPaths dflags
1765 framework_path_opts = map ("-F" ++) framework_paths
1766
1767 frameworks = cmdlineFrameworks dflags
1768 -- reverse because they're added in reverse order from the cmd line:
1769 framework_opts = concat [ ["-framework", fw]
1770 | fw <- reverse frameworks ]