-- 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           Development.IDE.GHC.Compat.Util
import           GHC

-- See Note [Guidelines For Using CPP In GHCIDE Import Statements]

import           GHC.Settings

#if !MIN_VERSION_ghc(9,3,0)
import qualified GHC.Driver.Pipeline             as Pipeline
#endif

#if MIN_VERSION_ghc(9,3,0) && !MIN_VERSION_ghc(9,5,0)
import qualified GHC.Driver.Pipeline.Execute     as Pipeline
#endif

#if MIN_VERSION_ghc(9,5,0)
import qualified GHC.SysTools.Cpp                as Pipeline
#endif

addOptP :: String -> DynFlags -> DynFlags
addOptP :: String -> DynFlags -> DynFlags
addOptP String
f = (ToolSettings -> ToolSettings) -> DynFlags -> DynFlags
alterToolSettings forall a b. (a -> b) -> a -> b
$ \ToolSettings
s -> ToolSettings
s
          { toolSettings_opt_P :: [String]
toolSettings_opt_P             = String
f 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 forall a. a -> [a] -> [a]
: ToolSettings -> [String]
toolSettings_opt_P ToolSettings
s)
          }
  where
    fingerprintStrings :: [String] -> Fingerprint
fingerprintStrings [String]
ss = [Fingerprint] -> Fingerprint
fingerprintFingerprints forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map String -> Fingerprint
fingerprintString [String]
ss
    alterToolSettings :: (ToolSettings -> ToolSettings) -> DynFlags -> DynFlags
alterToolSettings ToolSettings -> ToolSettings
g DynFlags
dynFlags = DynFlags
dynFlags { toolSettings :: ToolSettings
toolSettings = ToolSettings -> ToolSettings
g (DynFlags -> ToolSettings
toolSettings DynFlags
dynFlags) }

doCpp :: HscEnv -> FilePath -> FilePath -> IO ()
doCpp :: HscEnv -> String -> String -> IO ()
doCpp HscEnv
env String
input_fn String
output_fn =
        -- See GHC commit a2f53ac8d968723417baadfab5be36a020ea6850
        -- this function/Pipeline.doCpp previously had a raw parameter
        -- always set to True that corresponded to these settings

#if MIN_VERSION_ghc(9,5,0)
    let cpp_opts = Pipeline.CppOpts
                 { cppLinePragmas = True
# if MIN_VERSION_ghc(9,9,0)
                 , useHsCpp = True
# else
                 , cppUseCc = False
# endif
                 } in
#else
    let cpp_opts :: Bool
cpp_opts = Bool
True in
#endif

    Logger
-> TmpFs
-> DynFlags
-> UnitEnv
-> Bool
-> String
-> String
-> IO ()
Pipeline.doCpp (HscEnv -> Logger
hsc_logger HscEnv
env) (HscEnv -> TmpFs
hsc_tmpfs HscEnv
env) (HscEnv -> DynFlags
hsc_dflags HscEnv
env) (HscEnv -> UnitEnv
hsc_unit_env HscEnv
env) Bool
cpp_opts String
input_fn String
output_fn