[commit: ghc] master: Adds -ghc-version flag to ghc. (12a7444)

git at git.haskell.org git at git.haskell.org
Sat Nov 18 03:00:26 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/12a7444463184e9eddbe7b7251a0ee1e976f4d75/ghc

>---------------------------------------------------------------

commit 12a7444463184e9eddbe7b7251a0ee1e976f4d75
Author: Moritz Angermann <moritz.angermann at gmail.com>
Date:   Fri Nov 17 19:29:25 2017 +0800

    Adds -ghc-version flag to ghc.
    
    Summary:
    When building the rts with ghc (e.g. using ghc as a c compiler), ghc's
    "Value Add"[1] is, it includes adding `-include /path/to/ghcversion.h`.  For
    this it looksup the rts package in the package database, which--if
    empty--fails.  Thus to allow compiling C files with GHC, we add the
    `-ghc-version` flag, which takes the path to the `ghcversion.h` file.
    
    A `-no-ghc-version` flag was omitted, as at that point it becomes
    questionable why one would use ghc to compile c if one doesn't
    any of the added value.
    
    --
    
    [1] from `compiler/main/DriverPipeline.hs`
    >        -- add package include paths even if we're just compiling .c
    >        -- files; this is the Value Add(TM) that using ghc instead of
    >        -- gcc gives you :)
    
    Reviewers: bgamari, geekosaur, austin
    
    Reviewed By: bgamari
    
    Subscribers: rwbarton, thomie
    
    Differential Revision: https://phabricator.haskell.org/D4135


>---------------------------------------------------------------

12a7444463184e9eddbe7b7251a0ee1e976f4d75
 compiler/main/DriverPipeline.hs | 11 ++++++++---
 compiler/main/DynFlags.hs       |  7 +++++++
 docs/users_guide/using.rst      | 23 +++++++++++++++++++++++
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/compiler/main/DriverPipeline.hs b/compiler/main/DriverPipeline.hs
index fab7fad..4f7bfbd 100644
--- a/compiler/main/DriverPipeline.hs
+++ b/compiler/main/DriverPipeline.hs
@@ -2204,11 +2204,16 @@ touchObjectFile dflags path = do
 -- | Find out path to @ghcversion.h@ file
 getGhcVersionPathName :: DynFlags -> IO FilePath
 getGhcVersionPathName dflags = do
-  dirs <- getPackageIncludePath dflags [toInstalledUnitId rtsUnitId]
+  candidates <- case ghcVersion dflags of
+    Just path -> return [path]
+    Nothing -> (map (</> "ghcversion.h")) <$>
+               (getPackageIncludePath dflags [toInstalledUnitId rtsUnitId])
 
-  found <- filterM doesFileExist (map (</> "ghcversion.h") dirs)
+  found <- filterM doesFileExist candidates
   case found of
-      []    -> throwGhcExceptionIO (InstallationError ("ghcversion.h missing"))
+      []    -> throwGhcExceptionIO (InstallationError
+                                    ("ghcversion.h missing; tried: "
+                                      ++ intercalate ", " candidates))
       (x:_) -> return x
 
 -- Note [-fPIC for assembler]
diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs
index 5888acc..04ac635 100644
--- a/compiler/main/DynFlags.hs
+++ b/compiler/main/DynFlags.hs
@@ -534,6 +534,7 @@ data GeneralFlag
    | Opt_ExternalInterpreter
    | Opt_OptimalApplicativeDo
    | Opt_VersionMacros
+   | Opt_GhcVersion
    | Opt_WholeArchiveHsLibs
 
    -- PreInlining is on by default. The option is there just to see how
@@ -917,6 +918,7 @@ data DynFlags = DynFlags {
   flushOut              :: FlushOut,
   flushErr              :: FlushErr,
 
+  ghcVersion            :: Maybe FilePath,
   haddockOptions        :: Maybe String,
 
   -- | GHCi scripts specified by -ghci-script, in reverse order
@@ -1682,6 +1684,7 @@ defaultDynFlags mySettings myLlvmTargets =
         filesToClean   = panic "defaultDynFlags: No filesToClean",
         dirsToClean    = panic "defaultDynFlags: No dirsToClean",
         generatedDumps = panic "defaultDynFlags: No generatedDumps",
+        ghcVersion     = Nothing,
         haddockOptions = Nothing,
         dumpFlags = EnumSet.empty,
         generalFlags = EnumSet.fromList (defaultFlags mySettings),
@@ -2339,6 +2342,9 @@ addDepSuffix s d = d { depSuffixes = s : depSuffixes d }
 
 addCmdlineFramework f d = d { cmdlineFrameworks = f : cmdlineFrameworks d}
 
+addGhcVersion :: FilePath -> DynFlags -> DynFlags
+addGhcVersion f d = d { ghcVersion = Just f }
+
 addHaddockOpts f d = d { haddockOptions = Just f}
 
 addGhciScript f d = d { ghciScripts = f : ghciScripts d}
@@ -2866,6 +2872,7 @@ dynamic_flags_deps = [
   , make_ord_flag defGhcFlag "no-rtsopts-suggestions"
       (noArg (\d -> d {rtsOptsSuggestions = False}))
 
+  , make_ord_flag defGhcFlag "ghc-version"          (hasArg addGhcVersion)
   , make_ord_flag defGhcFlag "main-is"              (SepArg setMainIs)
   , make_ord_flag defGhcFlag "haddock"              (NoArg (setGeneralFlag Opt_Haddock))
   , make_ord_flag defGhcFlag "haddock-opts"         (hasArg addHaddockOpts)
diff --git a/docs/users_guide/using.rst b/docs/users_guide/using.rst
index a963ead..e3fa741 100644
--- a/docs/users_guide/using.rst
+++ b/docs/users_guide/using.rst
@@ -1064,3 +1064,26 @@ Some flags only make sense for particular target platforms.
     and later). The :ref:`LLVM backend <llvm-code-gen>` will also use
     SSE4.2 if your processor supports it but detects this automatically
     so no flag is required.
+
+Miscellaneous flags
+-------------------
+
+.. index::
+   single: miscellaneous flags
+
+Some flags only make sense for a particular use case.
+
+.. ghc-flag:: -ghc-version ⟨path to ghcversion.h⟩
+    :shortdesc: (GHC as a C compiler only) Use this ``ghcversion.h`` file
+    :type: dynamic
+    :category: misc
+
+    When GHC is used to compile C files, GHC adds package include paths and
+    includes ``ghcversion.h`` directly. The compiler will lookup the path for
+    the ``ghcversion.h`` file from the ``rts`` package in the package database.
+    In some cases, the compiler's package database does not contain the ``rts``
+    package, or one wants to specify a specific ``ghcversions.h`` to be
+    included. This option can be used to specify the path to the
+    ``ghcversions.h`` file to be included. This is primarily intended to be
+    used by GHC's build system.
+



More information about the ghc-commits mailing list