1 -----------------------------------------------------------------------------
4 -- (c) Simon Marlow 2000
6 -----------------------------------------------------------------------------
8 module Main (main) where
28 #define GLOBAL_VAR(name,value,ty) \
29 name = global (value) :: IORef (ty); \
32 -----------------------------------------------------------------------------
35 -- time commands when run with -v
41 -- make sure OPTIONS in .hs file propogate to .hc file if -C or -keep-hc-file-too
42 -- reading the package configuration file is too slow
44 -----------------------------------------------------------------------------
45 -- Differences vs. old driver:
47 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
48 -- consistency checking removed (may do this properly later)
50 -- no hi diffs (could be added later)
53 -----------------------------------------------------------------------------
54 -- non-configured things
56 cHaskell1Version = "5" -- i.e., Haskell 98
58 -----------------------------------------------------------------------------
62 hPutStr stderr "\nUsage: For basic information, try the `-help' option.\n"
66 let usage_filename = "ghc-usage.txt"
67 usage_dir = findFile usage_filename cGHC_DRIVER_DIR
68 usage <- readFile (usage_dir ++ "/" ++ usage_filename)
73 dump ('$':'$':s) = hPutStr stderr get_prog_name >> dump s
74 dump (c:s) = hPutChar stderr c >> dump s
76 version_str = cProjectVersion ++
77 ( if cProjectPatchLevel /= "0" && cProjectPatchLevel /= ""
78 then '.':cProjectPatchLevel
81 -----------------------------------------------------------------------------
85 Phase of the | Suffix saying | Flag saying | (suffix of)
86 compilation system | ``start here''| ``stop after''| output file
88 literate pre-processor | .lhs | - | -
89 C pre-processor (opt.) | - | -E | -
90 Haskell compiler | .hs | -C, -S | .hc, .s
91 C compiler (opt.) | .hc or .c | -S | .s
92 assembler | .s or .S | -c | .o
93 linker | other | - | a.out
97 = MkDependHS -- haskell dependency generation
102 | HCc -- Haskellised C (as opposed to vanilla C) compilation
103 | Mangle -- assembly mangling, now done by a separate script.
104 | SplitMangle -- after mangler if splitting
108 deriving (Eq,Ord,Enum,Ix,Show,Bounded)
110 initial_phase = Unlit
112 -----------------------------------------------------------------------------
116 = UnknownFileType String
119 | MultipleSrcsOneOutput
120 | UnknownPackage String
121 | WayCombinationNotSupported [WayName]
122 | PhaseFailed String ExitCode
128 GLOBAL_VAR(prog_name, "ghc", String)
130 get_prog_name = unsafePerformIO (readIORef prog_name) -- urk!
132 instance Show BarfKind where
134 = showString get_prog_name . showString ": " . showBarf e
136 showBarf AmbiguousPhase
137 = showString "only one of the flags -M, -E, -C, -S, -c is allowed"
138 showBarf (UnknownFileType s)
139 = showString "unknown file type, and linking not done: " . showString s
140 showBarf (UnknownFlag s)
141 = showString "unrecognised flag: " . showString s
142 showBarf MultipleSrcsOneOutput
143 = showString "can't apply -o option to multiple source files"
144 showBarf (UnknownPackage s)
145 = showString "unknown package name: " . showString s
146 showBarf (WayCombinationNotSupported ws)
147 = showString "combination not supported: "
148 . foldr1 (\a b -> a . showChar '/' . b)
149 (map (showString . wayName . lkupWay) ws)
150 showBarf (NoInputFiles)
151 = showString "no input files"
152 showBarf (OtherError str)
155 barfKindTc = mkTyCon "BarfKind"
157 instance Typeable BarfKind where
158 typeOf _ = mkAppTy barfKindTc []
160 -----------------------------------------------------------------------------
163 GLOBAL_VAR(files_to_clean, [], [String])
165 cleanTempFiles :: IO ()
167 fs <- readIORef files_to_clean
168 verb <- readIORef verbose
171 (do on verb (hPutStrLn stderr ("removing: " ++ f))
172 if '*' `elem` f then system ("rm -f " ++ f) >> return ()
175 (\e -> on verb (hPutStrLn stderr
176 ("warning: can't remove tmp file" ++ f)))
179 -----------------------------------------------------------------------------
180 -- Which phase to stop at
182 GLOBAL_VAR(stop_after, Ln, Phase)
184 end_phase_flag :: String -> Maybe Phase
185 end_phase_flag "-M" = Just MkDependHS
186 end_phase_flag "-E" = Just Cpp
187 end_phase_flag "-C" = Just Hsc
188 end_phase_flag "-S" = Just Mangle
189 end_phase_flag "-c" = Just As
190 end_phase_flag _ = Nothing
192 getStopAfter :: [String]
193 -> IO ( [String] -- rest of command line
194 , Phase -- stop after phase
195 , Bool -- do linking?
198 = case my_partition end_phase_flag flags of
199 ([] , rest) -> return (rest, As, True)
200 ([one], rest) -> return (rest, one, False)
201 (_ , rest) -> throwDyn AmbiguousPhase
203 -----------------------------------------------------------------------------
204 -- Global compilation flags
207 GLOBAL_VAR(cpp_flag, False, Bool)
208 hs_source_cpp_opts = global
209 [ "-D__HASKELL1__="++cHaskell1Version
210 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
212 , "-D__CONCURRENT_HASKELL__"
215 -- Keep output from intermediate phases
216 GLOBAL_VAR(keep_hi_diffs, False, Bool)
217 GLOBAL_VAR(keep_hc_files, False, Bool)
218 GLOBAL_VAR(keep_s_files, False, Bool)
219 GLOBAL_VAR(keep_raw_s_files, False, Bool)
221 -- Compiler RTS options
222 GLOBAL_VAR(specific_heap_size, 6 * 1000 * 1000, Integer)
223 GLOBAL_VAR(specific_stack_size, 1000 * 1000, Integer)
224 GLOBAL_VAR(scale_sizes_by, 1.0, Double)
227 GLOBAL_VAR(verbose, False, Bool)
228 is_verbose = do v <- readIORef verbose; if v then return "-v" else return ""
231 GLOBAL_VAR(dry_run, False, Bool)
232 GLOBAL_VAR(recomp, True, Bool)
233 GLOBAL_VAR(tmp_prefix, cTMPDIR, String)
234 GLOBAL_VAR(stolen_x86_regs, 4, Int)
235 GLOBAL_VAR(static, True, Bool) -- ToDo: not for mingw32
236 GLOBAL_VAR(collect_ghc_timing, False, Bool)
237 GLOBAL_VAR(do_asm_mangling, True, Bool)
239 -----------------------------------------------------------------------------
240 -- Splitting object files (for libraries)
242 GLOBAL_VAR(split_object_files, False, Bool)
243 GLOBAL_VAR(split_prefix, "", String)
244 GLOBAL_VAR(n_split_files, 0, Int)
247 can_split = prefixMatch "i386" cTARGETPLATFORM
248 || prefixMatch "alpha" cTARGETPLATFORM
249 || prefixMatch "hppa" cTARGETPLATFORM
250 || prefixMatch "m68k" cTARGETPLATFORM
251 || prefixMatch "mips" cTARGETPLATFORM
252 || prefixMatch "powerpc" cTARGETPLATFORM
253 || prefixMatch "rs6000" cTARGETPLATFORM
254 || prefixMatch "sparc" cTARGETPLATFORM
256 -----------------------------------------------------------------------------
257 -- Compiler output options
264 GLOBAL_VAR(hsc_lang, if cGhcWithNativeCodeGen == "YES" &&
265 (prefixMatch "i386" cTARGETPLATFORM ||
266 prefixMatch "sparc" cTARGETPLATFORM)
271 GLOBAL_VAR(output_dir, Nothing, Maybe String)
272 GLOBAL_VAR(output_suf, Nothing, Maybe String)
273 GLOBAL_VAR(output_file, Nothing, Maybe String)
274 GLOBAL_VAR(output_hi, Nothing, Maybe String)
276 GLOBAL_VAR(ld_inputs, [], [String])
278 odir_ify :: String -> IO String
280 odir_opt <- readIORef output_dir
283 Just d -> return (newdir d f)
285 osuf_ify :: String -> IO String
287 osuf_opt <- readIORef output_suf
290 Just s -> return (newsuf s f)
292 -----------------------------------------------------------------------------
295 GLOBAL_VAR(produceHi, True, Bool)
296 GLOBAL_VAR(hi_on_stdout, False, Bool)
297 GLOBAL_VAR(hi_with, "", String)
298 GLOBAL_VAR(hi_suf, "hi", String)
300 data HiDiffFlag = NormalHiDiffs | UsageHiDiffs | NoHiDiffs
301 GLOBAL_VAR(hi_diffs, NoHiDiffs, HiDiffFlag)
303 -----------------------------------------------------------------------------
304 -- Warnings & sanity checking
306 -- Warning packages that are controlled by -W and -Wall. The 'standard'
307 -- warnings that you get all the time are
309 -- -fwarn-overlapping-patterns
310 -- -fwarn-missing-methods
311 -- -fwarn-missing-fields
312 -- -fwarn-deprecations
313 -- -fwarn-duplicate-exports
315 -- these are turned off by -Wnot.
317 standardWarnings = [ "-fwarn-overlapping-patterns"
318 , "-fwarn-missing-methods"
319 , "-fwarn-missing-fields"
320 , "-fwarn-deprecations"
321 , "-fwarn-duplicate-exports"
323 minusWOpts = standardWarnings ++
324 [ "-fwarn-unused-binds"
325 , "-fwarn-unused-matches"
326 , "-fwarn-incomplete-patterns"
327 , "-fwarn-unused-imports"
329 minusWallOpts = minusWOpts ++
330 [ "-fwarn-type-defaults"
331 , "-fwarn-name-shadowing"
332 , "-fwarn-missing-signatures"
335 data WarningState = W_default | W_ | W_all | W_not
337 GLOBAL_VAR(warning_opt, W_default, WarningState)
339 -----------------------------------------------------------------------------
340 -- Compiler optimisation options
342 GLOBAL_VAR(opt_level, 0, Int)
344 setOptLevel :: String -> IO ()
345 setOptLevel "" = do { writeIORef opt_level 1; go_via_C }
346 setOptLevel "not" = writeIORef opt_level 0
347 setOptLevel [c] | isDigit c = do
348 let level = ord c - ord '0'
349 writeIORef opt_level level
350 on (level >= 1) go_via_C
351 setOptLevel s = throwDyn (UnknownFlag ("-O"++s))
354 l <- readIORef hsc_lang
355 case l of { HscAsm -> writeIORef hsc_lang HscC;
356 _other -> return () }
358 GLOBAL_VAR(opt_minus_o2_for_C, False, Bool)
360 GLOBAL_VAR(opt_MaxSimplifierIterations, 4, Int)
361 GLOBAL_VAR(opt_StgStats, False, Bool)
362 GLOBAL_VAR(opt_UsageSPInf, False, Bool) -- Off by default
364 hsc_minusO2_flags = hsc_minusO_flags -- for now
366 hsc_minusNoO_flags = do
367 iter <- readIORef opt_MaxSimplifierIterations
369 "-fignore-interface-pragmas",
370 "-fomit-interface-pragmas",
373 "-fmax-simplifier-iterations" ++ show iter,
377 hsc_minusO_flags = do
378 iter <- readIORef opt_MaxSimplifierIterations
379 usageSP <- readIORef opt_UsageSPInf
380 stgstats <- readIORef opt_StgStats
385 "-fdo-eta-reduction",
386 "-fdo-lambda-eta-expansion",
391 -- initial simplify: mk specialiser happy: minimum effort please
396 -- Don't inline anything till full laziness has bitten
397 -- In particular, inlining wrappers inhibits floating
398 -- e.g. ...(case f x of ...)...
399 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
400 -- ==> ...(case x of I# x# -> case fw x# of ...)...
401 -- and now the redex (f x) isn't floatable any more
404 -- Similarly, don't apply any rules until after full
405 -- laziness. Notably, list fusion can prevent floating.
408 -- Don't do case-of-case transformations.
409 -- This makes full laziness work better
411 "-fmax-simplifier-iterations2",
414 -- Specialisation is best done before full laziness
415 -- so that overloaded functions have all their dictionary lambdas manifest
424 -- Want to run with inline phase 1 after the specialiser to give
425 -- maximum chance for fusion to work before we inline build/augment
426 -- in phase 2. This made a difference in 'ansi' where an
427 -- overloaded function wasn't inlined till too late.
428 "-fmax-simplifier-iterations" ++ show iter,
431 -- infer usage information here in case we need it later.
432 -- (add more of these where you need them --KSW 1999-04)
433 if usageSP then "-fusagesp" else "",
437 -- Need inline-phase2 here so that build/augment get
438 -- inlined. I found that spectral/hartel/genfft lost some useful
439 -- strictness in the function sumcode' if augment is not inlined
440 -- before strictness analysis runs
443 "-fmax-simplifier-iterations2",
449 "-fmax-simplifier-iterations2",
450 -- No -finline-phase: allow all Ids to be inlined now
451 -- This gets foldr inlined before strictness analysis
460 "-fmax-simplifier-iterations" ++ show iter,
461 -- No -finline-phase: allow all Ids to be inlined now
465 -- nofib/spectral/hartel/wang doubles in speed if you
466 -- do full laziness late in the day. It only happens
467 -- after fusion and other stuff, so the early pass doesn't
468 -- catch it. For the record, the redex is
469 -- f_el22 (f_el21 r_midblock)
471 -- Leave out lambda lifting for now
472 -- "-fsimplify", -- Tidy up results of full laziness
474 -- "-fmax-simplifier-iterations2",
476 -- "-ffloat-outwards-full",
478 -- We want CSE to follow the final full-laziness pass, because it may
479 -- succeed in commoning up things floated out by full laziness.
481 -- CSE must immediately follow a simplification pass, because it relies
482 -- on the no-shadowing invariant. See comments at the top of CSE.lhs
483 -- So it must NOT follow float-inwards, which can give rise to shadowing,
484 -- even if its input doesn't have shadows. Hence putting it between
491 -- Case-liberation for -O2. This should be after
492 -- strictness analysis and the simplification which follows it.
494 -- ( ($OptLevel != 2)
496 -- : "-fliberate-case -fsimplify [ $Oopt_FB_Support -ffloat-lets-exposing-whnf -ffloat-primops-ok -fcase-of-case -fdo-case-elim -fcase-merge -fdo-lambda-eta-expansion -freuse-con -flet-to-case $Oopt_PedanticBottoms $Oopt_MaxSimplifierIterations $Oopt_ShowSimplifierProgress ]" ),
498 -- "-fliberate-case",
500 -- Final clean-up simplification:
503 "-fmax-simplifier-iterations" ++ show iter,
504 -- No -finline-phase: allow all Ids to be inlined now
509 -----------------------------------------------------------------------------
512 split_marker = ':' -- not configurable
514 import_paths, include_paths, library_paths :: IORef [String]
515 GLOBAL_VAR(import_paths, ["."], [String])
516 GLOBAL_VAR(include_paths, ["."], [String])
517 GLOBAL_VAR(library_paths, [], [String])
519 GLOBAL_VAR(cmdline_libraries, [], [String])
520 GLOBAL_VAR(cmdline_hc_includes, [], [String])
522 augment_import_paths :: String -> IO ()
523 augment_import_paths "" = writeIORef import_paths []
524 augment_import_paths path
525 = do paths <- readIORef import_paths
526 writeIORef import_paths (paths ++ dirs)
527 where dirs = split split_marker path
529 augment_include_paths :: String -> IO ()
530 augment_include_paths path
531 = do paths <- readIORef include_paths
532 writeIORef include_paths (paths ++ split split_marker path)
534 augment_library_paths :: String -> IO ()
535 augment_library_paths path
536 = do paths <- readIORef library_paths
537 writeIORef library_paths (paths ++ split split_marker path)
539 -----------------------------------------------------------------------------
542 GLOBAL_VAR(package_config, (findFile "package.conf" (cGHC_DRIVER_DIR++"/package.conf.inplace")), String)
544 listPackages :: IO ()
546 details <- readIORef package_details
547 hPutStr stdout (listPkgs details)
554 details <- readIORef package_details
555 hPutStr stdout "Reading package info from stdin... "
557 let new_pkg = read stuff :: (String,Package)
559 (\e -> throwDyn (OtherError "parse error in package info"))
560 hPutStrLn stdout "done."
561 if (fst new_pkg `elem` map fst details)
562 then throwDyn (OtherError ("package `" ++ fst new_pkg ++
563 "' already installed"))
565 conf_file <- readIORef package_config
566 savePackageConfig conf_file
567 maybeRestoreOldConfig conf_file $ do
568 writeNewConfig conf_file ( ++ [new_pkg])
571 deletePackage :: String -> IO ()
572 deletePackage pkg = do
574 details <- readIORef package_details
575 if (pkg `notElem` map fst details)
576 then throwDyn (OtherError ("package `" ++ pkg ++ "' not installed"))
578 conf_file <- readIORef package_config
579 savePackageConfig conf_file
580 maybeRestoreOldConfig conf_file $ do
581 writeNewConfig conf_file (filter ((/= pkg) . fst))
584 checkConfigAccess :: IO ()
585 checkConfigAccess = do
586 conf_file <- readIORef package_config
587 access <- fileAccess conf_file True True False
589 then throwDyn (OtherError "you don't have permission to modify the package configuration file")
592 maybeRestoreOldConfig :: String -> IO () -> IO ()
593 maybeRestoreOldConfig conf_file io
594 = catchAllIO io (\e -> do
595 hPutStr stdout "\nWARNING: an error was encountered while the new \n\
596 \configuration was being written. Attempting to \n\
597 \restore the old configuration... "
598 system ("cp " ++ conf_file ++ ".old " ++ conf_file)
599 hPutStrLn stdout "done."
603 writeNewConfig :: String -> ([(String,Package)] -> [(String,Package)]) -> IO ()
604 writeNewConfig conf_file fn = do
605 hPutStr stdout "Writing new package config file... "
606 old_details <- readIORef package_details
607 h <- openFile conf_file WriteMode
608 hPutStr h (dumpPackages (fn old_details))
610 hPutStrLn stdout "done."
612 savePackageConfig :: String -> IO ()
613 savePackageConfig conf_file = do
614 hPutStr stdout "Saving old package config file... "
615 -- mv rather than cp because we've already done an hGetContents
616 -- on this file so we won't be able to open it for writing
617 -- unless we move the old one out of the way...
618 system ("mv " ++ conf_file ++ " " ++ conf_file ++ ".old")
619 hPutStrLn stdout "done."
621 -- package list is maintained in dependency order
622 packages = global ["std", "rts", "gmp"] :: IORef [String]
623 -- comma in value, so can't use macro, grrr
624 {-# NOINLINE packages #-}
626 addPackage :: String -> IO ()
628 = do pkg_details <- readIORef package_details
629 case lookup package pkg_details of
630 Nothing -> throwDyn (UnknownPackage package)
632 ps <- readIORef packages
635 else do mapM_ addPackage (package_deps details)
636 ps <- readIORef packages
637 writeIORef packages (package:ps)
639 getPackageImportPath :: IO [String]
640 getPackageImportPath = do
641 ps <- readIORef packages
642 ps' <- getPackageDetails ps
643 return (nub (concat (map import_dirs ps')))
645 getPackageIncludePath :: IO [String]
646 getPackageIncludePath = do
647 ps <- readIORef packages
648 ps' <- getPackageDetails ps
649 return (nub (filter (not.null) (concatMap include_dirs ps')))
651 -- includes are in reverse dependency order (i.e. rts first)
652 getPackageCIncludes :: IO [String]
653 getPackageCIncludes = do
654 ps <- readIORef packages
655 ps' <- getPackageDetails ps
656 return (reverse (nub (filter (not.null) (concatMap c_includes ps'))))
658 getPackageLibraryPath :: IO [String]
659 getPackageLibraryPath = do
660 ps <- readIORef packages
661 ps' <- getPackageDetails ps
662 return (nub (concat (map library_dirs ps')))
664 getPackageLibraries :: IO [String]
665 getPackageLibraries = do
666 ps <- readIORef packages
667 ps' <- getPackageDetails ps
668 tag <- readIORef build_tag
669 let suffix = if null tag then "" else '_':tag
670 return (concat (map libraries ps'))
672 getPackageExtraGhcOpts :: IO [String]
673 getPackageExtraGhcOpts = do
674 ps <- readIORef packages
675 ps' <- getPackageDetails ps
676 return (concatMap extra_ghc_opts ps')
678 getPackageExtraCcOpts :: IO [String]
679 getPackageExtraCcOpts = do
680 ps <- readIORef packages
681 ps' <- getPackageDetails ps
682 return (concatMap extra_cc_opts ps')
684 getPackageExtraLdOpts :: IO [String]
685 getPackageExtraLdOpts = do
686 ps <- readIORef packages
687 ps' <- getPackageDetails ps
688 return (concatMap extra_ld_opts ps')
690 getPackageDetails :: [String] -> IO [Package]
691 getPackageDetails ps = do
692 pkg_details <- readIORef package_details
693 return [ pkg | p <- ps, Just pkg <- [ lookup p pkg_details ] ]
695 GLOBAL_VAR(package_details, (error "package_details"), [(String,Package)])
697 -----------------------------------------------------------------------------
700 -- The central concept of a "way" is that all objects in a given
701 -- program must be compiled in the same "way". Certain options change
702 -- parameters of the virtual machine, eg. profiling adds an extra word
703 -- to the object header, so profiling objects cannot be linked with
704 -- non-profiling objects.
706 -- After parsing the command-line options, we determine which "way" we
707 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
709 -- We then find the "build-tag" associated with this way, and this
710 -- becomes the suffix used to find .hi files and libraries used in
713 GLOBAL_VAR(build_tag, "", String)
742 GLOBAL_VAR(ways, [] ,[WayName])
744 allowed_combinations =
745 [ [WayProf,WayUnreg],
746 [WayProf,WaySMP] -- works???
749 findBuildTag :: IO [String] -- new options
751 way_names <- readIORef ways
752 case sort way_names of
753 [] -> do writeIORef build_tag ""
756 [w] -> do let details = lkupWay w
757 writeIORef build_tag (wayTag details)
758 return (wayOpts details)
760 ws -> if ws `notElem` allowed_combinations
761 then throwDyn (WayCombinationNotSupported ws)
762 else let stuff = map lkupWay ws
763 tag = concat (map wayTag stuff)
764 flags = map wayOpts stuff
766 writeIORef build_tag tag
767 return (concat flags)
770 case lookup w way_details of
771 Nothing -> error "findBuildTag"
772 Just details -> details
780 way_details :: [ (WayName, Way) ]
782 [ (WayProf, Way "p" "Profiling"
785 , "-optc-DPROFILING" ]),
787 (WayTicky, Way "t" "Ticky-ticky Profiling"
790 , "-optc-DTICKY_TICKY" ]),
792 (WayUnreg, Way "u" "Unregisterised"
794 , "-optc-DUSE_MINIINTERPRETER"
795 , "-fno-asm-mangling"
796 , "-funregisterised" ]),
798 (WayPar, Way "mp" "Parallel"
801 , "-D__PARALLEL_HASKELL__"
803 , "-package concurrent" ]),
805 (WayGran, Way "mg" "Gransim"
810 , "-package concurrent" ]),
812 (WaySMP, Way "s" "SMP"
818 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
819 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
820 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
821 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
822 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
823 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
824 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
825 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
826 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
827 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
828 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
829 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
830 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
831 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
832 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
833 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
834 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
837 -----------------------------------------------------------------------------
838 -- Programs for particular phases
840 GLOBAL_VAR(pgm_dep, findFile "mkdependHS" cGHC_MKDEPENDHS, String)
841 GLOBAL_VAR(pgm_L, findFile "unlit" cGHC_UNLIT, String)
842 GLOBAL_VAR(pgm_P, cRAWCPP, String)
843 GLOBAL_VAR(pgm_C, findFile "hsc" cGHC_HSC, String)
844 GLOBAL_VAR(pgm_c, cGCC, String)
845 GLOBAL_VAR(pgm_m, findFile "ghc-asm" cGHC_MANGLER, String)
846 GLOBAL_VAR(pgm_s, findFile "ghc-split" cGHC_SPLIT, String)
847 GLOBAL_VAR(pgm_a, cGCC, String)
848 GLOBAL_VAR(pgm_l, cGCC, String)
850 -----------------------------------------------------------------------------
851 -- Options for particular phases
853 GLOBAL_VAR(opt_dep, [], [String])
854 GLOBAL_VAR(opt_L, [], [String])
855 GLOBAL_VAR(opt_P, [], [String])
856 GLOBAL_VAR(opt_C, [], [String])
857 GLOBAL_VAR(opt_Crts, [], [String])
858 GLOBAL_VAR(opt_c, [], [String])
859 GLOBAL_VAR(opt_a, [], [String])
860 GLOBAL_VAR(opt_m, [], [String])
861 GLOBAL_VAR(opt_l, [], [String])
862 GLOBAL_VAR(opt_dll, [], [String])
864 -- we add to the options from the front, so we need to reverse the list
865 getOpts :: IORef [String] -> IO [String]
866 getOpts opts = readIORef opts >>= return . reverse
868 GLOBAL_VAR(anti_opt_C, [], [String])
870 -----------------------------------------------------------------------------
871 -- Via-C compilation stuff
873 -- flags returned are: ( all C compilations
874 -- , registerised HC compilations
878 | prefixMatch "alpha" cTARGETPLATFORM
879 = return ( ["-static"], [] )
881 | prefixMatch "hppa" cTARGETPLATFORM
882 -- ___HPUX_SOURCE, not _HPUX_SOURCE, is #defined if -ansi!
883 -- (very nice, but too bad the HP /usr/include files don't agree.)
884 = return ( ["-static", "-D_HPUX_SOURCE"], [] )
886 | prefixMatch "m68k" cTARGETPLATFORM
887 -- -fno-defer-pop : for the .hc files, we want all the pushing/
888 -- popping of args to routines to be explicit; if we let things
889 -- be deferred 'til after an STGJUMP, imminent death is certain!
891 -- -fomit-frame-pointer : *don't*
892 -- It's better to have a6 completely tied up being a frame pointer
893 -- rather than let GCC pick random things to do with it.
894 -- (If we want to steal a6, then we would try to do things
895 -- as on iX86, where we *do* steal the frame pointer [%ebp].)
896 = return ( [], ["-fno-defer-pop", "-fno-omit-frame-pointer"] )
898 | prefixMatch "i386" cTARGETPLATFORM
899 -- -fno-defer-pop : basically the same game as for m68k
901 -- -fomit-frame-pointer : *must* in .hc files; because we're stealing
902 -- the fp (%ebp) for our register maps.
903 = do n_regs <- readIORef stolen_x86_regs
904 sta <- readIORef static
905 return ( [ if sta then "-DDONT_WANT_WIN32_DLL_SUPPORT" else "" ],
906 [ "-fno-defer-pop", "-fomit-frame-pointer",
907 "-DSTOLEN_X86_REGS="++show n_regs ]
910 | prefixMatch "mips" cTARGETPLATFORM
911 = return ( ["static"], [] )
913 | prefixMatch "powerpc" cTARGETPLATFORM || prefixMatch "rs6000" cTARGETPLATFORM
914 = return ( ["static"], ["-finhibit-size-directive"] )
919 -----------------------------------------------------------------------------
920 -- Build the Hsc command line
922 build_hsc_opts :: IO [String]
924 opt_C_ <- getOpts opt_C -- misc hsc opts
927 warn_level <- readIORef warning_opt
928 let warn_opts = case warn_level of
929 W_default -> standardWarnings
931 W_all -> minusWallOpts
935 minus_o <- readIORef opt_level
938 0 -> hsc_minusNoO_flags
939 1 -> hsc_minusO_flags
940 2 -> hsc_minusO2_flags
944 ways_ <- readIORef ways
945 let stg_massage | WayProf `elem` ways_ = "-fmassage-stg-for-profiling"
948 stg_stats <- readIORef opt_StgStats
949 let stg_stats_flag | stg_stats = "-dstg-stats"
952 let stg_opts = [ stg_massage, stg_stats_flag, "-flet-no-escape" ]
953 -- let-no-escape always on for now
956 let hi_vers = "-fhi-version="++cProjectVersionInt
957 static <- (do s <- readIORef static; if s then return "-static" else return "")
959 l <- readIORef hsc_lang
962 HscAsm -> "-olang=asm"
963 HscJava -> "-olang=java"
965 -- get hi-file suffix
966 hisuf <- readIORef hi_suf
968 -- hi-suffix for packages depends on the build tag.
970 do tag <- readIORef build_tag
973 else return (tag ++ "_hi")
975 import_dirs <- readIORef import_paths
976 package_import_dirs <- getPackageImportPath
978 let hi_map = "-himap=" ++
979 makeHiMap import_dirs hisuf
980 package_import_dirs package_hisuf
983 hi_map_sep = "-himap-sep=" ++ [split_marker]
985 scale <- readIORef scale_sizes_by
986 heap <- readIORef specific_heap_size
987 stack <- readIORef specific_stack_size
988 cmdline_rts_opts <- getOpts opt_Crts
989 let heap' = truncate (fromIntegral heap * scale) :: Integer
990 stack' = truncate (fromIntegral stack * scale) :: Integer
991 rts_opts = [ "+RTS", "-H"++show heap', "-K"++show stack' ]
992 ++ cmdline_rts_opts ++ [ "-RTS" ]
994 -- take into account -fno-* flags by removing the equivalent -f*
995 -- flag from our list.
996 anti_flags <- getOpts anti_opt_C
997 let basic_opts = opt_C_ ++ warn_opts ++ optimisation_opts ++ stg_opts
998 filtered_opts = filter (`notElem` anti_flags) basic_opts
1003 -- ToDo: C stub files
1004 ++ [ hi_vers, static, verb, lang, hi_map, hi_map_sep ]
1009 (import_dirs :: [String])
1010 (hi_suffix :: String)
1011 (package_import_dirs :: [String])
1012 (package_hi_suffix :: String)
1013 (split_marker :: Char)
1014 = foldr (add_dir hi_suffix)
1015 (foldr (add_dir package_hi_suffix) "" package_import_dirs)
1018 add_dir hisuf dir str = dir ++ "%." ++ hisuf ++ split_marker : str
1021 getOptionsFromSource
1022 :: String -- input file
1023 -> IO [String] -- options, if any
1024 getOptionsFromSource file
1025 = do h <- openFile file ReadMode
1031 () | null l -> look h
1032 | prefixMatch "#" l -> look h
1033 | prefixMatch "{-# LINE" l -> look h
1034 | Just (opts:_) <- matchRegex optionRegex l
1035 -> return (words opts)
1036 | otherwise -> return []
1038 optionRegex = mkRegex "{-#[ \t]+OPTIONS[ \t]+(.*)#-}"
1040 -----------------------------------------------------------------------------
1043 get_source_files :: [String] -> ([String],[String])
1044 get_source_files = partition (('-' /=) . head)
1046 suffixes :: [(String,Phase)]
1058 phase_input_ext Unlit = "lhs"
1059 phase_input_ext Cpp = "lpp"
1060 phase_input_ext Hsc = "cpp"
1061 phase_input_ext HCc = "hc"
1062 phase_input_ext Cc = "c"
1063 phase_input_ext Mangle = "raw_s"
1064 phase_input_ext SplitMangle = "split_s" -- not really generated
1065 phase_input_ext As = "s"
1066 phase_input_ext SplitAs = "split_s" -- not really generated
1067 phase_input_ext Ln = "o"
1069 find_phase :: String -> ([(Phase,String)], [String])
1070 -> ([(Phase,String)], [String])
1071 find_phase f (phase_srcs, unknown_srcs)
1072 = case lookup ext suffixes of
1073 Just the_phase -> ((the_phase,f):phase_srcs, unknown_srcs)
1074 Nothing -> (phase_srcs, f:unknown_srcs)
1075 where (basename,ext) = split_filename f
1078 find_phases srcs = (phase_srcs, unknown_srcs)
1079 where (phase_srcs, unknown_srcs) = foldr find_phase ([],[]) srcs
1082 -- all error messages are propagated as exceptions
1083 my_catchDyn (\dyn -> case dyn of
1084 PhaseFailed phase code -> exitWith code
1085 Interrupted -> exitWith (ExitFailure 1)
1086 _ -> do hPutStrLn stderr (show (dyn :: BarfKind))
1087 exitWith (ExitFailure 1)) $
1089 later cleanTempFiles $
1090 -- exceptions will be blocked while we clean the temporary files,
1091 -- so there shouldn't be any difficulty if we receive further
1095 -- install signal handlers
1096 main_thread <- myThreadId
1097 let sig_handler = Catch (raiseInThread main_thread
1098 (DynException (toDyn Interrupted)))
1099 installHandler sigQUIT sig_handler Nothing
1100 installHandler sigINT sig_handler Nothing
1103 writeIORef prog_name pgm
1107 -- grab any -B options from the command line first
1108 argv' <- setTopDir argv
1110 -- read the package configuration
1111 conf_file <- readIORef package_config
1112 contents <- readFile conf_file
1113 writeIORef package_details (read contents)
1115 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
1116 (flags2, stop_phase, do_linking) <- getStopAfter argv'
1118 -- process all the other arguments, and get the source files
1119 srcs <- processArgs flags2 []
1121 -- find the build tag, and re-process the build-specific options
1122 more_opts <- findBuildTag
1123 _ <- processArgs more_opts []
1126 verb <- readIORef verbose
1128 when verb (hPutStrLn stderr ("Using package config file: " ++ conf_file))
1130 if stop_phase == MkDependHS -- mkdependHS is special
1131 then do_mkdependHS flags2 srcs
1134 -- for each source file, find which phase to start at
1135 let (phase_srcs, unknown_srcs) = find_phases srcs
1137 o_file <- readIORef output_file
1138 if isJust o_file && not do_linking && length phase_srcs > 1
1139 then throwDyn MultipleSrcsOneOutput
1142 if null unknown_srcs && null phase_srcs
1143 then throwDyn NoInputFiles
1146 -- if we have unknown files, and we're not doing linking, complain
1147 -- (otherwise pass them through to the linker).
1148 if not (null unknown_srcs) && not do_linking
1149 then throwDyn (UnknownFileType (head unknown_srcs))
1152 let compileFile :: (Phase, String) -> IO String
1153 compileFile (phase, src) = do
1154 let (orig_base, _) = split_filename src
1155 if phase < Ln -- anything to do?
1156 then run_pipeline stop_phase do_linking True orig_base (phase,src)
1159 o_files <- mapM compileFile phase_srcs
1162 then do_link o_files unknown_srcs
1166 -- The following compilation pipeline algorithm is fairly hacky. A
1167 -- better way to do this would be to express the whole comilation as a
1168 -- data flow DAG, where the nodes are the intermediate files and the
1169 -- edges are the compilation phases. This framework would also work
1170 -- nicely if a haskell dependency generator was included in the
1173 -- It would also deal much more cleanly with compilation phases that
1174 -- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
1175 -- possibly stub files), where some of the output files need to be
1176 -- processed further (eg. the stub files need to be compiled by the C
1179 -- A cool thing to do would then be to execute the data flow graph
1180 -- concurrently, automatically taking advantage of extra processors on
1181 -- the host machine. For example, when compiling two Haskell files
1182 -- where one depends on the other, the data flow graph would determine
1183 -- that the C compiler from the first comilation can be overlapped
1184 -- with the hsc comilation for the second file.
1187 :: Phase -- phase to end on (never Linker)
1188 -> Bool -- doing linking afterward?
1189 -> Bool -- take into account -o when generating output?
1190 -> String -- original basename (eg. Main)
1191 -> (Phase, String) -- phase to run, input file
1192 -> IO String -- return final filename
1194 run_pipeline last_phase do_linking use_ofile orig_basename (phase, input_fn)
1195 | phase > last_phase = return input_fn
1199 let (basename,ext) = split_filename input_fn
1201 split <- readIORef split_object_files
1202 mangle <- readIORef do_asm_mangling
1203 lang <- readIORef hsc_lang
1205 -- figure out what the next phase is. This is
1206 -- straightforward, apart from the fact that hsc can generate
1207 -- either C or assembler direct, and assembly mangling is
1208 -- optional, and splitting involves one extra phase and an alternate
1214 HscAsm | split -> SplitMangle
1217 HCc | mangle -> Mangle
1222 Mangle | not split -> As
1223 SplitMangle -> SplitAs
1229 -- filename extension for the output, determined by next_phase
1230 let new_ext = phase_input_ext next_phase
1232 -- Figure out what the output from this pass should be called.
1234 -- If we're keeping the output from this phase, then we just save
1235 -- it in the current directory, otherwise we generate a new temp file.
1236 keep_s <- readIORef keep_s_files
1237 keep_raw_s <- readIORef keep_raw_s_files
1238 keep_hc <- readIORef keep_hc_files
1239 let keep_this_output =
1242 Mangle | keep_raw_s -> True -- first enhancement :)
1244 HCc | keep_hc -> True
1248 (if next_phase > last_phase && not do_linking && use_ofile
1249 then do o_file <- readIORef output_file
1253 f <- odir_ify (orig_basename ++ '.':new_ext)
1256 -- .o files are always kept. .s files and .hc file may be kept.
1257 else if keep_this_output
1258 then odir_ify (orig_basename ++ '.':new_ext)
1259 else do filename <- newTempName new_ext
1260 add files_to_clean filename
1264 run_phase phase orig_basename input_fn output_fn
1266 -- sadly, ghc -E is supposed to write the file to stdout. We
1267 -- generate <file>.cpp, so we also have to cat the file here.
1268 if (next_phase > last_phase && last_phase == Cpp)
1269 then run_something "Dump pre-processed file to stdout"
1270 ("cat " ++ output_fn)
1273 run_pipeline last_phase do_linking use_ofile
1274 orig_basename (next_phase, output_fn)
1277 -- find a temporary name that doesn't already exist.
1278 newTempName :: String -> IO String
1279 newTempName extn = do
1281 tmp_dir <- readIORef tmp_prefix
1282 findTempName tmp_dir x
1283 where findTempName tmp_dir x = do
1284 let filename = tmp_dir ++ "/ghc" ++ show x ++ '.':extn
1285 b <- fileExist filename
1286 if b then findTempName tmp_dir (x+1)
1287 else return filename
1289 -------------------------------------------------------------------------------
1292 do_mkdependHS :: [String] -> [String] -> IO ()
1293 do_mkdependHS cmd_opts srcs = do
1295 let quote_include_opt o | prefixMatch "-#include" o = "'" ++ o ++ "'"
1298 mkdependHS <- readIORef pgm_dep
1299 mkdependHS_opts <- getOpts opt_dep
1300 hs_src_cpp_opts <- readIORef hs_source_cpp_opts
1302 run_something "Dependency generation"
1303 (unwords (mkdependHS :
1306 ++ ("--" : map quote_include_opt cmd_opts )
1310 -------------------------------------------------------------------------------
1313 run_phase Unlit basename input_fn output_fn
1314 = do unlit <- readIORef pgm_L
1315 unlit_flags <- getOpts opt_L
1316 run_something "Literate pre-processor"
1317 ("echo '# 1 \"" ++input_fn++"\"' > "++output_fn++" && "
1318 ++ unlit ++ ' ':input_fn ++ " - >> " ++ output_fn)
1320 -------------------------------------------------------------------------------
1323 run_phase Cpp basename input_fn output_fn
1324 = do src_opts <- getOptionsFromSource input_fn
1325 processArgs src_opts []
1327 do_cpp <- readIORef cpp_flag
1330 cpp <- readIORef pgm_P
1331 hscpp_opts <- getOpts opt_P
1332 hs_src_cpp_opts <- readIORef hs_source_cpp_opts
1334 cmdline_include_paths <- readIORef include_paths
1335 pkg_include_dirs <- getPackageIncludePath
1336 let include_paths = map (\p -> "-I"++p) (cmdline_include_paths
1337 ++ pkg_include_dirs)
1340 run_something "C pre-processor"
1342 (["echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}'", ">", output_fn, "&&",
1347 ++ [ "-x", "c", input_fn, ">>", output_fn ]
1350 run_something "Inefective C pre-processor"
1351 ("echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}' > "
1352 ++ output_fn ++ " && cat " ++ input_fn
1353 ++ " >> " ++ output_fn)
1355 -----------------------------------------------------------------------------
1358 run_phase Hsc basename input_fn output_fn
1359 = do hsc <- readIORef pgm_C
1361 -- we add the current directory (i.e. the directory in which
1362 -- the .hs files resides) to the import path, since this is
1363 -- what gcc does, and it's probably what you want.
1364 let current_dir = getdir basename
1366 paths <- readIORef include_paths
1367 writeIORef include_paths (current_dir : paths)
1369 -- build the hsc command line
1370 hsc_opts <- build_hsc_opts
1372 doing_hi <- readIORef produceHi
1373 tmp_hi_file <- if doing_hi
1374 then do fn <- newTempName "hi"
1375 add files_to_clean fn
1379 let hi_flag = if doing_hi then "-hifile=" ++ tmp_hi_file
1382 -- deal with -Rghc-timing
1383 timing <- readIORef collect_ghc_timing
1384 stat_file <- newTempName "stat"
1385 add files_to_clean stat_file
1386 let stat_opts | timing = [ "+RTS", "-S"++stat_file, "-RTS" ]
1389 -- tmp files for foreign export stub code
1390 tmp_stub_h <- newTempName "stub_h"
1391 tmp_stub_c <- newTempName "stub_c"
1392 add files_to_clean tmp_stub_h
1393 add files_to_clean tmp_stub_c
1395 -- figure out where to put the .hi file
1396 ohi <- readIORef output_hi
1397 hisuf <- readIORef hi_suf
1398 let hi_flags = case ohi of
1399 Nothing -> [ "-hidir="++current_dir, "-hisuf="++hisuf ]
1400 Just fn -> [ "-hifile="++fn ]
1402 -- run the compiler!
1403 run_something "Haskell Compiler"
1404 (unwords (hsc : input_fn : (
1408 "-ofile="++output_fn,
1415 -- Generate -Rghc-timing info
1417 run_something "Generate timing stats"
1418 (findFile "ghc-stats" cGHC_STATS ++ ' ':stat_file)
1422 let stub_h = basename ++ "_stub.h"
1423 let stub_c = basename ++ "_stub.c"
1425 -- copy .h_stub file into current dir if present
1426 b <- fileExist tmp_stub_h
1428 run_something "Copy stub .h file"
1429 ("cp " ++ tmp_stub_h ++ ' ':stub_h)
1431 -- #include <..._stub.h> in .hc file
1432 add cmdline_hc_includes tmp_stub_h -- hack
1434 -- copy the _stub.c file into the current dir
1435 run_something "Copy stub .c file"
1437 "rm -f", stub_c, "&&",
1438 "echo \'#include \""++stub_h++"\"\' >"++stub_c, " &&",
1439 "cat", tmp_stub_c, ">> ", stub_c
1442 -- compile the _stub.c file w/ gcc
1443 run_pipeline As False{-no linking-}
1444 False{-no -o option-}
1448 add ld_inputs (basename++"_stub.o")
1451 -----------------------------------------------------------------------------
1454 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
1455 -- way too many hacks, and I can't say I've ever used it anyway.
1457 run_phase cc_phase basename input_fn output_fn
1458 | cc_phase == Cc || cc_phase == HCc
1459 = do cc <- readIORef pgm_c
1460 cc_opts <- getOpts opt_c
1461 cmdline_include_dirs <- readIORef include_paths
1462 -- ToDo: $c_flags .= " -mno-cygwin" if ( $TargetPlatform =~ /-mingw32$/ );
1464 let hcc = cc_phase == HCc
1466 -- add package include paths even if we're just compiling
1467 -- .c files; this is the Value Add(TM) that using
1468 -- ghc instead of gcc gives you :)
1469 pkg_include_dirs <- getPackageIncludePath
1470 let include_paths = map (\p -> "-I"++p) (cmdline_include_dirs
1471 ++ pkg_include_dirs)
1473 c_includes <- getPackageCIncludes
1474 cmdline_includes <- readIORef cmdline_hc_includes -- -#include options
1476 let cc_injects | hcc = unlines (map mk_include
1477 (c_includes ++ reverse cmdline_includes))
1481 '"':_{-"-} -> "#include "++h_file
1482 '<':_ -> "#include "++h_file
1483 _ -> "#include \""++h_file++"\""
1485 cc_help <- newTempName "c"
1486 add files_to_clean cc_help
1487 h <- openFile cc_help WriteMode
1488 hPutStr h cc_injects
1489 hPutStrLn h ("#include \"" ++ input_fn ++ "\"\n")
1492 ccout <- newTempName "ccout"
1493 add files_to_clean ccout
1495 mangle <- readIORef do_asm_mangling
1496 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
1500 o2 <- readIORef opt_minus_o2_for_C
1501 let opt_flag | o2 = "-O2"
1504 pkg_extra_cc_opts <- getPackageExtraCcOpts
1506 run_something "C Compiler"
1507 (unwords ([ cc, "-x", "c", cc_help, "-o", output_fn ]
1509 ++ (if cc_phase == HCc && mangle
1510 then md_regd_c_flags
1512 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
1513 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
1516 ++ pkg_extra_cc_opts
1520 -- ToDo: postprocess the output from gcc
1522 -----------------------------------------------------------------------------
1525 run_phase Mangle basename input_fn output_fn
1526 = do mangler <- readIORef pgm_m
1527 mangler_opts <- getOpts opt_m
1529 if (prefixMatch "i386" cTARGETPLATFORM)
1530 then do n_regs <- readIORef stolen_x86_regs
1531 return [ show n_regs ]
1533 run_something "Assembly Mangler"
1536 ++ [ input_fn, output_fn ]
1540 -----------------------------------------------------------------------------
1543 run_phase SplitMangle basename input_fn outputfn
1544 = do splitter <- readIORef pgm_s
1546 -- this is the prefix used for the split .s files
1547 tmp_pfx <- readIORef tmp_prefix
1549 let split_s_prefix = tmp_pfx ++ "/ghc" ++ show x
1550 writeIORef split_prefix split_s_prefix
1551 add files_to_clean (split_s_prefix ++ "__*") -- d:-)
1553 -- allocate a tmp file to put the no. of split .s files in (sigh)
1554 n_files <- newTempName "n_files"
1555 add files_to_clean n_files
1557 run_something "Split Assembly File"
1564 -- save the number of split files for future references
1565 s <- readFile n_files
1566 let n = read s :: Int
1567 writeIORef n_split_files n
1569 -----------------------------------------------------------------------------
1572 run_phase As basename input_fn output_fn
1573 = do as <- readIORef pgm_a
1574 as_opts <- getOpts opt_a
1576 cmdline_include_paths <- readIORef include_paths
1577 let cmdline_include_flags = map (\p -> "-I"++p) cmdline_include_paths
1578 run_something "Assembler"
1579 (unwords (as : as_opts
1580 ++ cmdline_include_flags
1581 ++ [ "-c", input_fn, "-o", output_fn ]
1584 run_phase SplitAs basename input_fn output_fn
1585 = do as <- readIORef pgm_a
1586 as_opts <- getOpts opt_a
1588 odir_opt <- readIORef output_dir
1589 let odir | Just s <- odir_opt = s
1590 | otherwise = basename
1592 split_s_prefix <- readIORef split_prefix
1593 n <- readIORef n_split_files
1595 odir <- readIORef output_dir
1596 let real_odir = case odir of
1600 let assemble_file n = do
1601 let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
1602 let output_o = newdir real_odir
1603 (basename ++ "__" ++ show n ++ ".o")
1604 real_o <- osuf_ify output_o
1605 run_something "Assembler"
1606 (unwords (as : as_opts
1607 ++ [ "-c", "-o", real_o, input_s ]
1610 mapM_ assemble_file [1..n]
1612 -----------------------------------------------------------------------------
1615 do_link :: [String] -> [String] -> IO ()
1616 do_link o_files unknown_srcs = do
1617 ln <- readIORef pgm_l
1619 o_file <- readIORef output_file
1620 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
1622 pkg_lib_paths <- getPackageLibraryPath
1623 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
1625 lib_paths <- readIORef library_paths
1626 let lib_path_opts = map ("-L"++) lib_paths
1628 pkg_libs <- getPackageLibraries
1629 let pkg_lib_opts = map ("-l"++) pkg_libs
1631 libs <- readIORef cmdline_libraries
1632 let lib_opts = map ("-l"++) (reverse libs)
1633 -- reverse because they're added in reverse order from the cmd line
1635 pkg_extra_ld_opts <- getPackageExtraLdOpts
1637 -- probably _stub.o files
1638 extra_ld_inputs <- readIORef ld_inputs
1640 -- opts from -optl-<blah>
1641 extra_ld_opts <- getOpts opt_l
1643 run_something "Linker"
1645 ([ ln, verb, "-o", output_fn ]
1651 ++ pkg_lib_path_opts
1653 ++ pkg_extra_ld_opts
1658 -----------------------------------------------------------------------------
1659 -- Running an external program
1661 run_something phase_name cmd
1663 verb <- readIORef verbose
1672 n <- readIORef dry_run
1673 if n then return () else do
1676 exit_code <- system cmd `catchAllIO`
1677 (\e -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
1679 if exit_code /= ExitSuccess
1680 then throwDyn (PhaseFailed phase_name exit_code)
1681 else do on verb (putStr "\n")
1684 -----------------------------------------------------------------------------
1688 = NoArg (IO ()) -- flag with no argument
1689 | HasArg (String -> IO ()) -- flag has an argument (maybe prefix)
1690 | SepArg (String -> IO ()) -- flag has a separate argument
1691 | Prefix (String -> IO ()) -- flag is a prefix only
1692 | OptPrefix (String -> IO ()) -- flag may be a prefix
1693 | AnySuffix (String -> IO ()) -- flag is a prefix, pass whole arg to fn
1694 | PassFlag (String -> IO ()) -- flag with no arg, pass flag to fn
1696 -- note that ordering is important in the following list: any flag which
1697 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
1698 -- flags further down the list with the same prefix.
1701 [ ------- help -------------------------------------------------------
1702 ( "?" , NoArg long_usage)
1703 , ( "-help" , NoArg long_usage)
1706 ------- version ----------------------------------------------------
1707 , ( "-version" , NoArg (do hPutStrLn stderr (cProjectName
1708 ++ ", version " ++ version_str)
1709 exitWith ExitSuccess))
1710 , ( "-numeric-version", NoArg (do hPutStrLn stderr version_str
1711 exitWith ExitSuccess))
1713 ------- verbosity ----------------------------------------------------
1714 , ( "v" , NoArg (writeIORef verbose True) )
1715 , ( "n" , NoArg (writeIORef dry_run True) )
1717 ------- recompilation checker --------------------------------------
1718 , ( "recomp" , NoArg (writeIORef recomp True) )
1719 , ( "no-recomp" , NoArg (writeIORef recomp False) )
1721 ------- ways --------------------------------------------------------
1722 , ( "prof" , NoArg (addNoDups ways WayProf) )
1723 , ( "unreg" , NoArg (addNoDups ways WayUnreg) )
1724 , ( "ticky" , NoArg (addNoDups ways WayTicky) )
1725 , ( "parallel" , NoArg (addNoDups ways WayPar) )
1726 , ( "gransim" , NoArg (addNoDups ways WayGran) )
1727 , ( "smp" , NoArg (addNoDups ways WaySMP) )
1728 , ( "debug" , NoArg (addNoDups ways WayDebug) )
1731 ------- Interface files ---------------------------------------------
1732 , ( "hi" , NoArg (writeIORef produceHi True) )
1733 , ( "nohi" , NoArg (writeIORef produceHi False) )
1734 , ( "hi-diffs" , NoArg (writeIORef hi_diffs NormalHiDiffs) )
1735 , ( "no-hi-diffs" , NoArg (writeIORef hi_diffs NoHiDiffs) )
1736 , ( "hi-diffs-with-usages" , NoArg (writeIORef hi_diffs UsageHiDiffs) )
1737 , ( "keep-hi-diffs" , NoArg (writeIORef keep_hi_diffs True) )
1738 --"hi-with-*" -> hiw <- readIORef hi_with (ToDo)
1740 --------- Profiling --------------------------------------------------
1741 , ( "auto-dicts" , NoArg (add opt_C "-fauto-sccs-on-dicts") )
1742 , ( "auto-all" , NoArg (add opt_C "-fauto-sccs-on-all-toplevs") )
1743 , ( "auto" , NoArg (add opt_C "-fauto-sccs-on-exported-toplevs") )
1744 , ( "caf-all" , NoArg (add opt_C "-fauto-sccs-on-individual-cafs") )
1745 -- "ignore-sccs" doesn't work (ToDo)
1747 ------- Miscellaneous -----------------------------------------------
1748 , ( "cpp" , NoArg (writeIORef cpp_flag True) )
1749 , ( "#include" , HasArg (add cmdline_hc_includes) )
1750 , ( "no-link-chk" , NoArg (return ()) ) -- ignored for backwards compat
1752 ------- Output Redirection ------------------------------------------
1753 , ( "odir" , HasArg (writeIORef output_dir . Just) )
1754 , ( "o" , SepArg (writeIORef output_file . Just) )
1755 , ( "osuf" , HasArg (writeIORef output_suf . Just) )
1756 , ( "hisuf" , HasArg (writeIORef hi_suf) )
1757 , ( "tmpdir" , HasArg (writeIORef tmp_prefix . (++ "/")) )
1758 , ( "ohi" , HasArg (\s -> case s of
1759 "-" -> writeIORef hi_on_stdout True
1760 _ -> writeIORef output_hi (Just s)) )
1763 , ( "keep-hc-file" , AnySuffix (\_ -> writeIORef keep_hc_files True) )
1764 , ( "keep-s-file" , AnySuffix (\_ -> writeIORef keep_s_files True) )
1765 , ( "keep-raw-s-file", AnySuffix (\_ -> writeIORef keep_raw_s_files True) )
1767 , ( "split-objs" , NoArg (if can_split
1768 then do writeIORef split_object_files True
1769 add opt_C "-fglobalise-toplev-names"
1770 add opt_c "-DUSE_SPLIT_MARKERS"
1771 else hPutStrLn stderr
1772 "warning: don't know how to split \
1773 \object files on this architecture"
1776 ------- Include/Import Paths ----------------------------------------
1777 , ( "i" , OptPrefix augment_import_paths )
1778 , ( "I" , Prefix augment_include_paths )
1780 ------- Libraries ---------------------------------------------------
1781 , ( "L" , Prefix augment_library_paths )
1782 , ( "l" , Prefix (add cmdline_libraries) )
1784 ------- Packages ----------------------------------------------------
1785 , ( "package-name" , HasArg (\s -> add opt_C ("-inpackage="++s)) )
1787 , ( "package" , HasArg (addPackage) )
1788 , ( "syslib" , HasArg (addPackage) ) -- for compatibility w/ old vsns
1790 , ( "-list-packages" , NoArg (listPackages) )
1791 , ( "-add-package" , NoArg (newPackage) )
1792 , ( "-delete-package" , SepArg (deletePackage) )
1794 ------- Specific phases --------------------------------------------
1795 , ( "pgmdep" , HasArg (writeIORef pgm_dep) )
1796 , ( "pgmL" , HasArg (writeIORef pgm_L) )
1797 , ( "pgmP" , HasArg (writeIORef pgm_P) )
1798 , ( "pgmC" , HasArg (writeIORef pgm_C) )
1799 , ( "pgmc" , HasArg (writeIORef pgm_c) )
1800 , ( "pgmm" , HasArg (writeIORef pgm_m) )
1801 , ( "pgms" , HasArg (writeIORef pgm_s) )
1802 , ( "pgma" , HasArg (writeIORef pgm_a) )
1803 , ( "pgml" , HasArg (writeIORef pgm_l) )
1805 , ( "optdep" , HasArg (add opt_dep) )
1806 , ( "optL" , HasArg (add opt_L) )
1807 , ( "optP" , HasArg (add opt_P) )
1808 , ( "optCrts" , HasArg (add opt_Crts) )
1809 , ( "optC" , HasArg (add opt_C) )
1810 , ( "optc" , HasArg (add opt_c) )
1811 , ( "optm" , HasArg (add opt_m) )
1812 , ( "opta" , HasArg (add opt_a) )
1813 , ( "optl" , HasArg (add opt_l) )
1814 , ( "optdll" , HasArg (add opt_dll) )
1816 ------ HsCpp opts ---------------------------------------------------
1817 , ( "D" , Prefix (\s -> add opt_P ("-D'"++s++"'") ) )
1818 , ( "U" , Prefix (\s -> add opt_P ("-U'"++s++"'") ) )
1820 ------ Warning opts -------------------------------------------------
1821 , ( "W" , NoArg (writeIORef warning_opt W_))
1822 , ( "Wall" , NoArg (writeIORef warning_opt W_all))
1823 , ( "Wnot" , NoArg (writeIORef warning_opt W_not))
1824 , ( "w" , NoArg (writeIORef warning_opt W_not))
1826 ----- Linker --------------------------------------------------------
1827 , ( "static" , NoArg (writeIORef static True) )
1829 ------ Compiler RTS options -----------------------------------------
1830 , ( "H" , HasArg (sizeOpt specific_heap_size) )
1831 , ( "K" , HasArg (sizeOpt specific_stack_size) )
1832 , ( "Rscale-sizes" , HasArg (floatOpt scale_sizes_by) )
1833 , ( "Rghc-timing" , NoArg (writeIORef collect_ghc_timing True) )
1835 ------ Debugging ----------------------------------------------------
1836 , ( "dstg-stats" , NoArg (writeIORef opt_StgStats True) )
1838 , ( "dno-" , Prefix (\s -> add anti_opt_C ("-d"++s)) )
1839 , ( "d" , AnySuffix (add opt_C) )
1841 ------ Machine dependant (-m<blah>) stuff ---------------------------
1843 , ( "monly-2-regs", NoArg (writeIORef stolen_x86_regs 2) )
1844 , ( "monly-3-regs", NoArg (writeIORef stolen_x86_regs 3) )
1845 , ( "monly-4-regs", NoArg (writeIORef stolen_x86_regs 4) )
1847 ------ Compiler flags -----------------------------------------------
1848 , ( "O2-for-C" , NoArg (writeIORef opt_minus_o2_for_C True) )
1849 , ( "O" , OptPrefix (setOptLevel) )
1851 , ( "fglasgow-exts-no-lang", NoArg ( do add opt_C "-fglasgow-exts") )
1853 , ( "fglasgow-exts" , NoArg (do add opt_C "-fglasgow-exts"
1856 , ( "fasm" , OptPrefix (\_ -> writeIORef hsc_lang HscAsm) )
1858 , ( "fvia-C" , NoArg (writeIORef hsc_lang HscC) )
1860 , ( "fno-asm-mangling" , NoArg (writeIORef do_asm_mangling False) )
1862 , ( "fmax-simplifier-iterations",
1863 Prefix (writeIORef opt_MaxSimplifierIterations . read) )
1865 , ( "fusagesp", NoArg (do writeIORef opt_UsageSPInf True
1866 add opt_C "-fusagesp-on") )
1868 -- flags that are "active negatives"
1869 , ( "fno-implicit-prelude" , PassFlag (add opt_C) )
1870 , ( "fno-prune-tydecls" , PassFlag (add opt_C) )
1871 , ( "fno-prune-instdecls" , PassFlag (add opt_C) )
1872 , ( "fno-pre-inlining" , PassFlag (add opt_C) )
1874 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
1875 , ( "fno-", Prefix (\s -> add anti_opt_C ("-f"++s)) )
1877 -- Pass all remaining "-f<blah>" options to hsc
1878 , ( "f", AnySuffix (add opt_C) )
1881 -----------------------------------------------------------------------------
1882 -- Process command-line
1884 processArgs :: [String] -> [String] -> IO [String] -- returns spare args
1885 processArgs [] spare = return (reverse spare)
1886 processArgs args@(('-':_):_) spare = do
1887 args' <- processOneArg args
1888 processArgs args' spare
1889 processArgs (arg:args) spare =
1890 processArgs args (arg:spare)
1892 processOneArg :: [String] -> IO [String]
1893 processOneArg (('-':arg):args) = do
1894 let (rest,action) = findArg arg
1900 then io >> return args
1901 else throwDyn (UnknownFlag dash_arg)
1905 then fio rest >> return args
1907 [] -> throwDyn (UnknownFlag dash_arg)
1908 (arg1:args1) -> fio arg1 >> return args1
1912 [] -> throwDyn (UnknownFlag dash_arg)
1913 (arg1:args1) -> fio arg1 >> return args1
1917 then fio rest >> return args
1918 else throwDyn (UnknownFlag dash_arg)
1920 OptPrefix fio -> fio rest >> return args
1922 AnySuffix fio -> fio ('-':arg) >> return args
1926 then throwDyn (UnknownFlag dash_arg)
1927 else fio ('-':arg) >> return args
1929 findArg :: String -> (String,OptKind)
1931 = case [ (remove_spaces rest, k) | (pat,k) <- opts,
1932 Just rest <- [my_prefix_match pat arg],
1933 is_prefix k || null rest ] of
1934 [] -> throwDyn (UnknownFlag ('-':arg))
1937 is_prefix (NoArg _) = False
1938 is_prefix (SepArg _) = False
1939 is_prefix (PassFlag _) = False
1942 -----------------------------------------------------------------------------
1943 -- convert sizes like "3.5M" into integers
1945 sizeOpt :: IORef Integer -> String -> IO ()
1947 | c == "" = writeSizeOpt ref (truncate n)
1948 | c == "K" || c == "k" = writeSizeOpt ref (truncate (n * 1000))
1949 | c == "M" || c == "m" = writeSizeOpt ref (truncate (n * 1000 * 1000))
1950 | c == "G" || c == "g" = writeSizeOpt ref (truncate (n * 1000 * 1000 * 1000))
1951 | otherwise = throwDyn (UnknownFlag str)
1952 where (m, c) = span pred str
1953 n = read m :: Double
1954 pred c = isDigit c || c == '.'
1956 writeSizeOpt :: IORef Integer -> Integer -> IO ()
1957 writeSizeOpt ref new = do
1958 current <- readIORef ref
1960 then writeIORef ref new
1963 floatOpt :: IORef Double -> String -> IO ()
1965 = writeIORef ref (read str :: Double)
1967 -----------------------------------------------------------------------------
1968 -- Finding files in the installation
1970 GLOBAL_VAR(topDir, clibdir, String)
1972 -- grab the last -B option on the command line, and
1973 -- set topDir to its value.
1974 setTopDir :: [String] -> IO [String]
1976 let (minusbs, others) = partition (prefixMatch "-B") args
1978 [] -> writeIORef topDir clibdir
1979 some -> writeIORef topDir (drop 2 (last some)))
1982 findFile name alt_path = unsafePerformIO (do
1983 top_dir <- readIORef topDir
1984 let installed_file = top_dir ++ '/':name
1985 let inplace_file = top_dir ++ '/':cCURRENT_DIR ++ '/':alt_path
1986 b <- fileExist inplace_file
1987 if b then return inplace_file
1988 else return installed_file
1991 -----------------------------------------------------------------------------
1994 my_partition :: (a -> Maybe b) -> [a] -> ([b],[a])
1995 my_partition p [] = ([],[])
1996 my_partition p (a:as)
1997 = let (bs,cs) = my_partition p as in
1999 Nothing -> (bs,a:cs)
2002 my_prefix_match :: String -> String -> Maybe String
2003 my_prefix_match [] rest = Just rest
2004 my_prefix_match (p:pat) [] = Nothing
2005 my_prefix_match (p:pat) (r:rest)
2006 | p == r = my_prefix_match pat rest
2007 | otherwise = Nothing
2009 prefixMatch :: Eq a => [a] -> [a] -> Bool
2010 prefixMatch [] str = True
2011 prefixMatch pat [] = False
2012 prefixMatch (p:ps) (s:ss) | p == s = prefixMatch ps ss
2015 postfixMatch :: String -> String -> Bool
2016 postfixMatch pat str = prefixMatch (reverse pat) (reverse str)
2018 later = flip finally
2020 on b io = if b then io >> return (error "on") else return (error "on")
2022 my_catch = flip catchAllIO
2023 my_catchDyn = flip catchDyn
2025 global :: a -> IORef a
2026 global a = unsafePerformIO (newIORef a)
2028 split_filename :: String -> (String,String)
2029 split_filename f = (reverse rev_basename, reverse rev_ext)
2030 where (rev_ext, '.':rev_basename) = span ('.' /=) (reverse f)
2032 split :: Char -> String -> [String]
2033 split c s = case rest of
2035 _:rest -> chunk : split c rest
2036 where (chunk, rest) = break (==c) s
2038 add :: IORef [a] -> a -> IO ()
2041 writeIORef var (x:xs)
2043 addNoDups :: Eq a => IORef [a] -> a -> IO ()
2044 addNoDups var x = do
2046 if x `elem` xs then return () else writeIORef var (x:xs)
2048 remove_suffix :: String -> Char -> String
2050 | null pre = reverse suf
2051 | otherwise = reverse pre
2052 where (suf,pre) = break (==c) (reverse s)
2054 drop_longest_prefix :: String -> Char -> String
2055 drop_longest_prefix s c = reverse suf
2056 where (suf,pre) = break (==c) (reverse s)
2058 take_longest_prefix :: String -> Char -> String
2059 take_longest_prefix s c = reverse pre
2060 where (suf,pre) = break (==c) (reverse s)
2062 newsuf :: String -> String -> String
2063 newsuf suf s = remove_suffix s '.' ++ suf
2065 -- getdir strips the filename off the input string, returning the directory.
2066 getdir :: String -> String
2067 getdir s = if null dir then "." else init dir
2068 where dir = take_longest_prefix s '/'
2070 newdir :: String -> String -> String
2071 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
2073 remove_spaces :: String -> String
2074 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace