From 7eabc7c93ca941f4fa7ddaafe71b7bb21ae6eead Mon Sep 17 00:00:00 2001
From: Daniel Gröber <dxld@darkboxed.org>
Date: Tue, 28 Apr 2015 21:55:05 +0200
Subject: Add `compiler-version` command

The compiler version in setup-config's header is the one Cabal was
compiled with not the one that was configured for.
---
 .travis.yml            |  2 +-
 CabalHelper/Main.hs    | 10 ++++++++--
 CabalHelper/Types.hs   |  2 ++
 CabalHelper/Wrapper.hs | 27 ++++++++++-----------------
 4 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 1448588..5d2f059 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -24,5 +24,5 @@ script:
   - if [ -n "$(ghc --version | awk '{ print $8 }' | sed -n '/^7.8/p')" ]; then export WERROR="--ghc-option=-Werror"; fi
   - cabal configure --enable-tests $WERROR
   - cabal build
-  - ./dist/build/cabal-helper-wrapper/cabal-helper-wrapper dist
+  - ./dist/build/cabal-helper-wrapper/cabal-helper-wrapper dist "compiler-version" "entrypoints" "source-dirs" "ghc-options" "ghc-src-options" "ghc-pkg-options" "ghc-lang-options"
 #  - cabal test
diff --git a/CabalHelper/Main.hs b/CabalHelper/Main.hs
index 62fe729..043e57c 100644
--- a/CabalHelper/Main.hs
+++ b/CabalHelper/Main.hs
@@ -52,8 +52,9 @@ import Distribution.Simple.Program.GHC (GhcOptions(..), renderGhcOptions)
 import Distribution.Simple.Setup (ConfigFlags(..),Flag(..))
 import Distribution.Simple.Build (initialBuildSteps)
 import Distribution.Simple.BuildPaths (autogenModuleName, cppHeaderName, exeExtension)
-import Distribution.Simple.Compiler (PackageDB(..))
+import Distribution.Simple.Compiler (PackageDB(..), compilerId)
 
+import Distribution.Compiler (CompilerId(..))
 import Distribution.ModuleName (components)
 import qualified Distribution.ModuleName as C (ModuleName)
 import Distribution.Text (display)
@@ -89,6 +90,7 @@ usage = do
      ++"DIST_DIR ( version\n"
      ++"         | print-lbi\n"
      ++"         | write-autogen-files\n"
+     ++"         | compiler-version"
      ++"         | ghc-options     [--with-inplace]\n"
      ++"         | ghc-src-options [--with-inplace]\n"
      ++"         | ghc-pkg-options [--with-inplace]\n"
@@ -100,7 +102,7 @@ usage = do
 commands :: [String]
 commands = [ "print-bli"
            , "write-autogen-files"
-           , "component-from-file"
+           , "compiler-version"
            , "ghc-options"
            , "ghc-src-options"
            , "ghc-pkg-options"
@@ -157,6 +159,10 @@ main = do
       initialBuildSteps distdir pd lbi v
       return Nothing
 
+    "compiler-version":[] -> do
+      let CompilerId comp ver = compilerId $ compiler lbi
+      return $ Just $ ChResponseVersion (show comp) ver
+
     "ghc-options":flags -> do
       res <- componentsMap lbi v distdir $ \c clbi bi -> let
            outdir = componentOutDir lbi c
diff --git a/CabalHelper/Types.hs b/CabalHelper/Types.hs
index add6dc1..0c1ebbe 100644
--- a/CabalHelper/Types.hs
+++ b/CabalHelper/Types.hs
@@ -18,6 +18,7 @@
 module CabalHelper.Types where
 
 import GHC.Generics
+import Data.Version
 
 newtype ChModuleName = ChModuleName String
     deriving (Eq, Ord, Read, Show, Generic)
@@ -33,6 +34,7 @@ data ChResponse
     = ChResponseStrings    [(ChComponentName, [String])]
     | ChResponseEntrypoints [(ChComponentName, ChEntrypoint)]
     | ChResponseLbi String
+    | ChResponseVersion String Version
   deriving (Eq, Ord, Read, Show, Generic)
 
 data ChEntrypoint = ChSetupEntrypoint -- ^ Almost like 'ChExeEntrypoint' but
diff --git a/CabalHelper/Wrapper.hs b/CabalHelper/Wrapper.hs
index 04a9971..f14ae6a 100644
--- a/CabalHelper/Wrapper.hs
+++ b/CabalHelper/Wrapper.hs
@@ -119,23 +119,16 @@ main = handlePanic $ do
 \Could not read Cabal's persistent setup configuration header\n\
 \- Check first line of: %s\n\
 \- Maybe try: $ cabal configure" cfgf
-        Just (hdrCabalVersion, (_compilerName, hdrCompilerVersion)) -> do
-          ghcVer <- ghcVersion opts
-          if not $ ghcVer `sameMajorVersionAs` hdrCompilerVersion
-            then panic $ printf "\
-\GHC major version changed! (was %s, now %s)\n\
-\- Reconfigure the project: $ cabal clean && cabal configure\
-\ " (showVersion hdrCompilerVersion) (showVersion ghcVer)
-            else do
-              eexe <- compileHelper opts hdrCabalVersion distdir
-              case eexe of
-                  Left e -> exitWith e
-                  Right exe ->
-                    case args' of
-                      "print-exe":_ -> putStrLn exe
-                      _ -> do
-                        (_,_,_,h) <- createProcess $ proc exe args
-                        exitWith =<< waitForProcess h
+        Just (hdrCabalVersion, _) -> do
+          eexe <- compileHelper opts hdrCabalVersion distdir
+          case eexe of
+              Left e -> exitWith e
+              Right exe ->
+                case args' of
+                  "print-exe":_ -> putStrLn exe
+                  _ -> do
+                    (_,_,_,h) <- createProcess $ proc exe args
+                    exitWith =<< waitForProcess h
 
 appDataDir :: IO FilePath
 appDataDir = (</> "cabal-helper") <$> getAppUserDataDirectory "ghc-mod"
-- 
cgit v1.2.3