-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Compiler
-- Copyright   :  Isaac Jones 2003-2004
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- This has an enumeration of the various compilers that Cabal knows about. It
-- also specifies the default compiler. Sadly you'll often see code that does
-- case analysis on this compiler flavour enumeration like:
--
-- > case compilerFlavor comp of
-- >   GHC -> GHC.getInstalledPackages verbosity packageDb progconf
-- >   JHC -> JHC.getInstalledPackages verbosity packageDb progconf
--
-- Obviously it would be better to use the proper 'Compiler' abstraction
-- because that would keep all the compiler-specific code together.
-- Unfortunately we cannot make this change yet without breaking the
-- 'UserHooks' api, which would break all custom @Setup.hs@ files, so for the
-- moment we just have to live with this deficiency. If you're interested, see
-- ticket #50.

{- All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.Compiler (
  -- * Compiler flavor
  CompilerFlavor(..),
  buildCompilerFlavor,
  defaultCompilerFlavor,
  parseCompilerFlavorCompat,

  -- * Compiler id
  CompilerId(..),
  ) where

import Distribution.Version (Version(..))

import qualified System.Info (compilerName)
import Distribution.Text (Text(..), display)
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import Text.PrettyPrint ((<>))

import qualified Data.Char as Char (toLower, isDigit, isAlphaNum)
import Control.Monad (when)

data CompilerFlavor = GHC | NHC | YHC | Hugs | HBC | Helium | JHC | LHC | UHC
                    | OtherCompiler String
  deriving (D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow, D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq, D:Ord ::
  Eq a =>
  (a -> a -> Ordering)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> a)
  -> (a -> a -> a)
  -> T:Ord aOrd)

knownCompilerFlavors :: [CompilerFlavor]
knownCompilerFlavors = [GHC :: CompilerFlavorGHC, NHC :: CompilerFlavorNHC, YHC :: CompilerFlavorYHC, Hugs :: CompilerFlavorHugs, HBC :: CompilerFlavorHBC, Helium :: CompilerFlavorHelium, JHC :: CompilerFlavorJHC, LHC :: CompilerFlavorLHC, UHC :: CompilerFlavorUHC]

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText CompilerFlavor where
  disp (OtherCompiler name) = text :: String -> DocDisp.text name :: Stringname
  disp NHC                  = text :: String -> DocDisp.text "nhc98"
  disp other                = text :: String -> DocDisp.text (lowercase :: String -> Stringlowercase (show :: Show a => a -> Stringshow other :: CompilerFlavorother))

  parse = do
    comp <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 isAlphaNum :: Char -> BoolChar.isAlphaNum
    when :: Monad m => Bool -> m () -> m ()when (all :: (a -> Bool) -> [a] -> Boolall isDigit :: Char -> BoolChar.isDigit comp :: Stringcomp) pfail :: ReadP r aParse.pfail
    return :: Monad m => forall a. a -> m areturn (classifyCompilerFlavor :: String -> CompilerFlavorclassifyCompilerFlavor comp :: Stringcomp)

classifyCompilerFlavor :: String -> CompilerFlavor
classifyCompilerFlavor s =
  case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (lowercase :: String -> Stringlowercase s :: Strings) compilerMap :: [(String, CompilerFlavor)]compilerMap of
    Just compiler -> compiler :: CompilerFlavorcompiler
    Nothing       -> OtherCompiler :: String -> CompilerFlavorOtherCompiler s :: Strings
  where
    compilerMap = [ (display :: Text a => a -> Stringdisplay compiler :: CompilerFlavorcompiler, compiler :: CompilerFlavorcompiler)
                  | compiler <- knownCompilerFlavors :: [CompilerFlavor]knownCompilerFlavors ]


--TODO: In some future release, remove 'parseCompilerFlavorCompat' and use
-- ordinary 'parse'. Also add ("nhc", NHC) to the above 'compilerMap'.

-- | Like 'classifyCompilerFlavor' but compatible with the old ReadS parser.
--
-- It is compatible in the sense that it accepts only the same strings,
-- eg "GHC" but not "ghc". However other strings get mapped to 'OtherCompiler'.
-- The point of this is that we do not allow extra valid values that would
-- upset older Cabal versions that had a stricter parser however we cope with
-- new values more gracefully so that we'll be able to introduce new value in
-- future without breaking things so much.
--
parseCompilerFlavorCompat :: Parse.ReadP r CompilerFlavor
parseCompilerFlavorCompat = do
  comp <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 isAlphaNum :: Char -> BoolChar.isAlphaNum
  when :: Monad m => Bool -> m () -> m ()when (all :: (a -> Bool) -> [a] -> Boolall isDigit :: Char -> BoolChar.isDigit comp :: Stringcomp) pfail :: ReadP r aParse.pfail
  case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup comp :: Stringcomp compilerMap :: [(String, CompilerFlavor)]compilerMap of
    Just compiler -> return :: Monad m => forall a. a -> m areturn compiler :: CompilerFlavorcompiler
    Nothing       -> return :: Monad m => forall a. a -> m areturn (OtherCompiler :: String -> CompilerFlavorOtherCompiler comp :: Stringcomp)
  where
    compilerMap = [ (show :: Show a => a -> Stringshow compiler :: CompilerFlavorcompiler, compiler :: CompilerFlavorcompiler)
                  | compiler <- knownCompilerFlavors :: [CompilerFlavor]knownCompilerFlavors
                  , compiler :: CompilerFlavorcompiler (/=) :: Eq a => a -> a -> Bool/= YHC :: CompilerFlavorYHC ]

buildCompilerFlavor :: CompilerFlavor
buildCompilerFlavor = classifyCompilerFlavor :: String -> CompilerFlavorclassifyCompilerFlavor compilerName :: StringSystem.Info.compilerName

-- | The default compiler flavour to pick when compiling stuff. This defaults
-- to the compiler used to build the Cabal lib.
--
-- However if it's not a recognised compiler then it's 'Nothing' and the user
-- will have to specify which compiler they want.
--
defaultCompilerFlavor :: Maybe CompilerFlavor
defaultCompilerFlavor = case buildCompilerFlavor :: CompilerFlavorbuildCompilerFlavor of
  OtherCompiler _ -> Nothing :: Maybe aNothing
  _               -> Just :: a -> Maybe aJust buildCompilerFlavor :: CompilerFlavorbuildCompilerFlavor

-- ------------------------------------------------------------
-- * Compiler Id
-- ------------------------------------------------------------

data CompilerId = CompilerId CompilerFlavor Version
  deriving (D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq, D:Ord ::
  Eq a =>
  (a -> a -> Ordering)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> a)
  -> (a -> a -> a)
  -> T:Ord aOrd, D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow)

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText CompilerId where
  disp (CompilerId f (Version [] _)) = disp :: Text a => a -> Docdisp f :: CompilerFlavorf
  disp (CompilerId f v) = disp :: Text a => a -> Docdisp f :: CompilerFlavorf (<>) :: Doc -> Doc -> Doc<> char :: Char -> DocDisp.char '-' (<>) :: Doc -> Doc -> Doc<> disp :: Text a => a -> Docdisp v :: Versionv

  parse = do
    flavour <- parse :: Text a => forall r. ReadP r aparse
    version <- (char :: Char -> ReadP r CharParse.char '-' (>>) :: Monad m => forall a b. m a -> m b -> m b>> parse :: Text a => forall r. ReadP r aparse) (<++) :: ReadP a a -> ReadP r a -> ReadP r aParse.<++ return :: Monad m => forall a. a -> m areturn (Version :: [Int] -> [String] -> VersionVersion [] :: [a][] [] :: [a][])
    return :: Monad m => forall a. a -> m areturn (CompilerId :: CompilerFlavor -> Version -> CompilerIdCompilerId flavour :: CompilerFlavorflavour version :: Versionversion)

lowercase :: String -> String
lowercase = map :: (a -> b) -> [a] -> [b]map toLower :: Char -> CharChar.toLower