, hscRecompile = genericHscRecompile hscOneShotCompiler
- , hscBackend = genericHscBackend hscOneShotCompiler
+ , hscBackend = \ tc_result mod_summary mb_old_hash -> do
+ hsc_env <- getSession
+ case hscTarget (hsc_dflags hsc_env) of
+ HscNothing -> return (HscRecomp False ())
+ _otherw -> genericHscBackend hscOneShotCompiler
+ tc_result mod_summary mb_old_hash
, hscGenBootOutput = \tc_result mod_summary mb_old_iface -> do
(iface, changed, _) <- hscSimpleIface tc_result mb_old_iface
details <- genModDetails iface
return (HscNoRecomp, iface, details)
- , hscRecompile = \mod_summary mb_old_hash ->
- case ms_hsc_src mod_summary of
- ExtCoreFile ->
- panic "hscCompileNothing: cannot do external core"
- _otherwise -> do
- tc_result <- hscFileFrontEnd mod_summary
- hscBackend hscNothingCompiler tc_result mod_summary mb_old_hash
+ , hscRecompile = genericHscRecompile hscNothingCompiler
, hscBackend = \tc_result _mod_summary mb_old_iface -> do
(iface, _changed, details) <- hscSimpleIface tc_result mb_old_iface
<indexterm><primary>using GHC</primary></indexterm>
<sect1>
+ <title>Getting started: compiling programs</title>
+
+ <para>
+ In this chapter you'll find a complete reference to the GHC
+ command-line syntax, including all 400+ flags. It's a large and
+ complex system, and there are lots of details, so it can be
+ quite hard to figure out how to get started. With that in mind,
+ this introductory section provides a quick introduction to the
+ basic usage of GHC for compiling a Haskell program, before the
+ following sections dive into the full syntax.
+ </para>
+
+ <para>
+ Let's create a Hello World program, and compile and run it.
+ First, create a file <filename>hello.hs</filename> containing
+ the Haskell code:
+ </para>
+
+<programlisting>
+main = putStrLn "Hello, World!"
+</programlisting>
+
+ <para>To compile the program, use GHC like this:</para>
+
+<screen>
+$ ghc hello.hs</screen>
+
+ <para>(where <literal>$</literal> represents the prompt: don't
+ type it). GHC will compile the source
+ file <filename>hello.hs</filename>, producing
+ an <firstterm>object
+ file</firstterm> <filename>hello.o</filename> and
+ an <firstterm>interface
+ file</firstterm> <filename>hello.hi</filename>, and then it
+ will link the object file to the libraries that come with GHC
+ to produce an executable called <filename>hello</filename> on
+ Unix/Linux/Mac, or <filename>hello.exe</filename> on
+ Windows.</para>
+
+ <para>
+ By default GHC will be very quiet about what it is doing, only
+ printing error messages. If you want to see in more detail
+ what's going on behind the scenes, add <option>-v</option> to
+ the command line.
+ </para>
+
+ <para>
+ Then we can run the program like this:
+ </para>
+
+<screen>
+$ ./hello
+Hello World!</screen>
+
+ <para>
+ If your program contains multiple modules, then you only need to
+ tell GHC the name of the source file containing
+ the <filename>Main</filename> module, and GHC will examine
+ the <literal>import</literal> declarations to find the other
+ modules that make up the program and find their source files.
+ This means that, with the exception of
+ the <literal>Main</literal> module, every source file should be
+ named after the module name that it contains (with dots replaced
+ by directory separators). For example, the
+ module <literal>Data.Person</literal> would be in the
+ file <filename>Data/Person.hs</filename> on Unix/Linux/Mac,
+ or <filename>Data\Person.hs</filename> on Windows.
+ </para>
+ </sect1>
+
+ <sect1>
<title>Options overview</title>
<para>GHC's behaviour is controlled by
<varlistentry>
<term>Mode flags</term>
<listitem>
- <para>For example, <option>--make</option> or <option>-E</option>.
+ <para>For example, <option>––make</option> or <option>-E</option>.
There may only be a single mode flag on the command line. The
available modes are listed in <xref linkend="modes"/>.</para>
</listitem>
<sect1 id="modes">
<title>Modes of operation</title>
- <para>GHC's behaviour is firstly controlled by a mode flag. Only
- one of these flags may be given, but it does not necessarily need
- to be the first option on the command-line. The available modes
- are:</para>
+ <para>
+ GHC's behaviour is firstly controlled by a mode flag. Only one
+ of these flags may be given, but it does not necessarily need to
+ be the first option on the command-line.
+ </para>
+
+ <para>
+ If no mode flag is present, then GHC will enter make mode
+ (<xref linkend="make-mode" />) if there are any Haskell source
+ files given on the command line, or else it will link the
+ objects named on the command line to produce an executable.
+ </para>
+
+ <para>The available mode flags are:</para>
<variablelist>
<varlistentry>
<varlistentry>
<term>
- <cmdsynopsis><command>ghc --make</command>
+ <cmdsynopsis><command>ghc ––make</command>
</cmdsynopsis>
<indexterm><primary>make mode</primary></indexterm>
<indexterm><primary><option>––make</option></primary></indexterm>
likely to be much easier, and faster, than using
<command>make</command>. Make mode is described in <xref
linkend="make-mode"/>.</para>
+
+ <para>
+ This mode is the default if there are any Haskell
+ source files mentioned on the command line, and in this case
+ the <option>––make</option> option can be omitted.
+ </para>
</listitem>
</varlistentry>
<indexterm><primary><option>––make</option></primary></indexterm>
<indexterm><primary>separate compilation</primary></indexterm>
- <para>When given the <option>––make</option> option,
- GHC will build a multi-module Haskell program by following
+ <para>In this mode, GHC will build a multi-module Haskell program by following
dependencies from one or more root modules (usually just
<literal>Main</literal>). For example, if your
<literal>Main</literal> module is in a file called
ghc ––make Main.hs
</screen>
- <para>The command line may contain any number of source file
- names or module names; GHC will figure out all the modules in
- the program by following the imports from these initial modules.
- It will then attempt to compile each module which is out of
- date, and finally, if there is a <literal>Main</literal> module,
- the program will also be linked into an executable.</para>
+ <para>
+ In fact, GHC enters make mode automatically if there are any
+ Haskell source files on the command line and no other mode is
+ specified, so in this case we could just type
+ </para>
+
+<screen>
+ghc Main.hs
+</screen>
+
+ <para>Any number of source file names or module names may be
+ specified; GHC will figure out all the modules in the program by
+ following the imports from these initial modules. It will then
+ attempt to compile each module which is out of date, and
+ finally, if there is a <literal>Main</literal> module, the
+ program will also be linked into an executable.</para>
<para>The main advantages to using <literal>ghc
––make</literal> over traditional
isDoInteractiveMode (Right (Right DoInteractive)) = True
isDoInteractiveMode _ = False
+isStopLnMode :: Mode -> Bool
+isStopLnMode (Right (Right (StopBefore StopLn))) = True
+isStopLnMode _ = False
+
+isDoMakeMode :: Mode -> Bool
+isDoMakeMode (Right (Right DoMake)) = True
+isDoMakeMode _ = False
+
#ifdef GHCI
isInteractiveMode :: PostLoadMode -> Bool
isInteractiveMode DoInteractive = True
isCompManagerMode (DoEval _) = True
isCompManagerMode _ = False
-
-- -----------------------------------------------------------------------------
-- Parsing the mode flag
runCmdLine (processArgs mode_flags args)
(Nothing, [], [])
mode = case mModeFlag of
- Nothing -> stopBeforeMode StopLn
+ Nothing -> doMakeMode
Just (m, _) -> m
errs = errs1 ++ map (mkGeneralLocated "on the commandline") errs2
when (not (null errs)) $ ghcError $ errorsToGhcException errs
Supported
------- primary modes ------------------------------------------------
+ , Flag "c" (PassFlag (\f -> do setMode (stopBeforeMode StopLn) f
+ addFlag "-no-link" f))
+ Supported
, Flag "M" (PassFlag (setMode doMkDependHSMode))
Supported
, Flag "E" (PassFlag (setMode (stopBeforeMode anyHsc)))
Supported
, Flag "e" (SepArg (\s -> setMode (doEvalMode s) "-e"))
Supported
-
- -- -fno-code says to stop after Hsc but don't generate any code.
- , Flag "fno-code" (PassFlag (\f -> do setMode (stopBeforeMode HCc) f
- addFlag "-fno-code" f
- addFlag "-fforce-recomp" f))
- Supported
]
setMode :: Mode -> String -> ModeM ()
Nothing -> ((newMode, newFlag), errs)
Just (oldMode, oldFlag) ->
case (oldMode, newMode) of
+ -- -c/--make are allowed together, and mean --make -no-link
+ _ | isStopLnMode oldMode && isDoMakeMode newMode
+ || isStopLnMode newMode && isDoMakeMode oldMode ->
+ ((doMakeMode, "--make"), [])
+
-- If we have both --help and --interactive then we
-- want showGhciUsage
_ | isShowGhcUsageMode oldMode &&
-- Run --make mode
doMake :: [(String,Maybe Phase)] -> Ghc ()
-doMake [] = ghcError (UsageError "no input files")
doMake srcs = do
let (hs_srcs, non_hs_srcs) = partition haskellish srcs
phase `notElem` [As, Cc, CmmCpp, Cmm, StopLn]
hsc_env <- GHC.getSession
+
+ -- if we have no haskell sources from which to do a dependency
+ -- analysis, then just do one-shot compilation and/or linking.
+ -- This means that "ghc Foo.o Bar.o -o baz" links the program as
+ -- we expect.
+ if (null hs_srcs)
+ then oneShot hsc_env StopLn srcs >> GHC.printWarnings
+ else do
+
o_files <- mapM (\x -> do
f <- compileFile hsc_env StopLn x
GHC.printWarnings