{- -----------------------------------------------------------------------------
Copyright 2020-2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

{-# LANGUAGE Safe #-}

module Cli.Programs (
  CompilerBackend(..),
  CxxCommand(..),
  TestCommand(..),
  TestCommandResult(..),
  VersionHash(..),
) where

import Control.Monad.IO.Class

import Base.CompilerError


class CompilerBackend b where
  runCxxCommand   :: (MonadIO m, CollectErrorsM m) => b -> CxxCommand -> m FilePath
  runTestCommand  :: (MonadIO m, CollectErrorsM m) => b -> TestCommand -> m TestCommandResult
  getCompilerHash :: (MonadIO m, CollectErrorsM m) => b -> m VersionHash

newtype VersionHash = VersionHash String deriving (VersionHash -> VersionHash -> Bool
(VersionHash -> VersionHash -> Bool)
-> (VersionHash -> VersionHash -> Bool) -> Eq VersionHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VersionHash -> VersionHash -> Bool
$c/= :: VersionHash -> VersionHash -> Bool
== :: VersionHash -> VersionHash -> Bool
$c== :: VersionHash -> VersionHash -> Bool
Eq)

instance Show VersionHash where
  show :: VersionHash -> String
show (VersionHash String
h) = String
h

data CxxCommand =
  CompileToObject {
    CxxCommand -> String
ctoSource :: FilePath,
    CxxCommand -> String
ctoPath :: FilePath,
    CxxCommand -> [(String, Maybe String)]
ctoMacros :: [(String,Maybe String)],
    CxxCommand -> [String]
ctoPaths :: [FilePath],
    CxxCommand -> Bool
ctoExtra :: Bool
  } |
  CompileToShared {
    CxxCommand -> [String]
ctsSources :: [FilePath],
    CxxCommand -> String
ctsOutput :: FilePath,
    CxxCommand -> [String]
ctsLinkFlags :: [String]
  } |
  CompileToBinary {
    CxxCommand -> String
ctbMain :: FilePath,
    CxxCommand -> [String]
ctbSources :: [FilePath],
    CxxCommand -> [(String, Maybe String)]
ctbMacros :: [(String,Maybe String)],
    CxxCommand -> String
ctbOutput :: FilePath,
    CxxCommand -> [String]
ctbPaths :: [FilePath],
    CxxCommand -> [String]
ctbLinkFlags :: [String]
  }
  deriving (Int -> CxxCommand -> ShowS
[CxxCommand] -> ShowS
CxxCommand -> String
(Int -> CxxCommand -> ShowS)
-> (CxxCommand -> String)
-> ([CxxCommand] -> ShowS)
-> Show CxxCommand
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CxxCommand] -> ShowS
$cshowList :: [CxxCommand] -> ShowS
show :: CxxCommand -> String
$cshow :: CxxCommand -> String
showsPrec :: Int -> CxxCommand -> ShowS
$cshowsPrec :: Int -> CxxCommand -> ShowS
Show)

data TestCommand =
  TestCommand {
    TestCommand -> String
tcBinary :: FilePath,
    TestCommand -> String
tcPath :: FilePath,
    TestCommand -> [String]
tcArgs :: [String]
  }
  deriving (Int -> TestCommand -> ShowS
[TestCommand] -> ShowS
TestCommand -> String
(Int -> TestCommand -> ShowS)
-> (TestCommand -> String)
-> ([TestCommand] -> ShowS)
-> Show TestCommand
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TestCommand] -> ShowS
$cshowList :: [TestCommand] -> ShowS
show :: TestCommand -> String
$cshow :: TestCommand -> String
showsPrec :: Int -> TestCommand -> ShowS
$cshowsPrec :: Int -> TestCommand -> ShowS
Show)

data TestCommandResult =
  TestCommandResult {
    TestCommandResult -> Bool
tcrSuccess :: Bool,
    TestCommandResult -> [String]
tcrOutput :: [String],
    TestCommandResult -> [String]
tcrError :: [String]
  }
  deriving (Int -> TestCommandResult -> ShowS
[TestCommandResult] -> ShowS
TestCommandResult -> String
(Int -> TestCommandResult -> ShowS)
-> (TestCommandResult -> String)
-> ([TestCommandResult] -> ShowS)
-> Show TestCommandResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TestCommandResult] -> ShowS
$cshowList :: [TestCommandResult] -> ShowS
show :: TestCommandResult -> String
$cshow :: TestCommandResult -> String
showsPrec :: Int -> TestCommandResult -> ShowS
$cshowsPrec :: Int -> TestCommandResult -> ShowS
Show)