-- Copyright (c) 2019 The DAML Authors. All rights reserved.

-- SPDX-License-Identifier: Apache-2.0


{-# LANGUAGE CPP                      #-}
{-# LANGUAGE NondecreasingIndentation #-}

-----------------------------------------------------------------------------

--

-- GHC Driver

--

-- (c) The University of Glasgow 2005

--

-----------------------------------------------------------------------------


module Development.IDE.GHC.CPP(doCpp, addOptP)
where

import           Development.IDE.GHC.Compat      as Compat
import           GHC
#if !MIN_VERSION_ghc(8,10,0)
import qualified Development.IDE.GHC.Compat.CPP  as CPP
#else
import           Development.IDE.GHC.Compat.Util
#endif

#if MIN_VERSION_ghc(9,0,0)
import qualified GHC.Driver.Pipeline             as Pipeline
import           GHC.Settings
#else
#if MIN_VERSION_ghc (8,10,0)
import qualified DriverPipeline                  as Pipeline
import           ToolSettings
#else
import           DynFlags
#endif
#endif

addOptP :: String -> DynFlags -> DynFlags
#if MIN_VERSION_ghc (8,10,0)
addOptP :: String -> DynFlags -> DynFlags
addOptP String
f = (ToolSettings -> ToolSettings) -> DynFlags -> DynFlags
alterToolSettings ((ToolSettings -> ToolSettings) -> DynFlags -> DynFlags)
-> (ToolSettings -> ToolSettings) -> DynFlags -> DynFlags
forall a b. (a -> b) -> a -> b
$ \ToolSettings
s -> ToolSettings
s
          { toolSettings_opt_P :: [String]
toolSettings_opt_P             = String
f String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ToolSettings -> [String]
toolSettings_opt_P ToolSettings
s
          , toolSettings_opt_P_fingerprint :: Fingerprint
toolSettings_opt_P_fingerprint = [String] -> Fingerprint
fingerprintStrings (String
f String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ToolSettings -> [String]
toolSettings_opt_P ToolSettings
s)
          }
  where
    fingerprintStrings :: [String] -> Fingerprint
fingerprintStrings [String]
ss = [Fingerprint] -> Fingerprint
fingerprintFingerprints ([Fingerprint] -> Fingerprint) -> [Fingerprint] -> Fingerprint
forall a b. (a -> b) -> a -> b
$ (String -> Fingerprint) -> [String] -> [Fingerprint]
forall a b. (a -> b) -> [a] -> [b]
map String -> Fingerprint
fingerprintString [String]
ss
    alterToolSettings :: (ToolSettings -> ToolSettings) -> DynFlags -> DynFlags
alterToolSettings ToolSettings -> ToolSettings
f DynFlags
dynFlags = DynFlags
dynFlags { toolSettings :: ToolSettings
toolSettings = ToolSettings -> ToolSettings
f (DynFlags -> ToolSettings
toolSettings DynFlags
dynFlags) }
#else
addOptP opt = onSettings (onOptP (opt:))
  where
    onSettings f x = x{settings = f $ settings x}
    onOptP f x = x{sOpt_P = f $ sOpt_P x}
#endif

doCpp :: HscEnv -> Bool -> FilePath -> FilePath -> IO ()
doCpp :: HscEnv -> Bool -> String -> String -> IO ()
doCpp HscEnv
env Bool
raw String
input_fn String
output_fn =
#if MIN_VERSION_ghc (9,2,0)
    Pipeline.doCpp (hsc_logger env) (hsc_tmpfs env) (hsc_dflags env) (hsc_unit_env env) raw input_fn output_fn
#elif MIN_VERSION_ghc (8,10,0)
    DynFlags -> Bool -> String -> String -> IO ()
Pipeline.doCpp (HscEnv -> DynFlags
hsc_dflags HscEnv
env) Bool
raw String
input_fn String
output_fn
#else
    CPP.doCpp (hsc_dflags env) raw input_fn output_fn
#endif