File: ProbeSolvers.hs

package info (click to toggle)
haskell-what4 1.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,240 kB
  • sloc: haskell: 34,630; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 2,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}

module ProbeSolvers where

import           Control.Exception ( try, SomeException )
import           Data.Char ( toLower )
import qualified Data.List as L
import           Data.Maybe ( catMaybes )
import           System.Exit ( ExitCode(..) )
import           System.Process ( readProcessWithExitCode )


newtype TestLevel = TestLevel String deriving Eq
newtype SolverName = SolverName String deriving (Eq, Show)
newtype SolverVersion = SolverVersion String deriving (Eq, Show)

getSolverVersion :: SolverName -> IO (Either String SolverVersion)
getSolverVersion (SolverName solver) =
  let args = case toLower <$> solver of
               -- n.b. abc will return a non-zero exit code if asked
               -- for command usage.
               "abc" -> ["s", "-q", "version;quit"]
               _ -> ["--version"]
  in try (readProcessWithExitCode (toLower <$> solver) args "") >>= \case
    Right (r,o,e) ->
      if r == ExitSuccess
      then let ol = lines o in
             return $ Right $ SolverVersion
             $ if null ol then (solver <> " v??") else head ol
      else return $ Left $ solver <> " version error: " <> show r <> " /;/ " <> e
    Left (err :: SomeException) -> return $ Left $ solver <> " invocation error: " <> show err


reportSolverVersions :: TestLevel
                     -> (solverinfo -> SolverName)
                     -> [(solverinfo, Either String SolverVersion)]
                     -> IO [(solverinfo, SolverVersion)]
reportSolverVersions testLevel getSolverName versionedSolvers =
  do putStrLn "SOLVER SELF-REPORTED VERSIONS::"
     catMaybes <$> mapM (rep testLevel) versionedSolvers
  where rep lvl (testsolver, versionInfo) = let s = getSolverName testsolver
                                            in disp lvl testsolver s versionInfo
        disp lvl solver (SolverName sname) = \case
          Right v@(SolverVersion ver) ->
            do putStrLn $ "  Solver " <> sname <> " -> " <> ver
               return $ Just (solver, v)
          Left e -> if and [ "does not exist" `L.isInfixOf` e
                           , lvl == TestLevel "0"

                           ]
                    then do putStrLn $ "  Solver " <> sname <> " not found; skipping (would fail with CI_TEST_LEVEL=1)"
                            return Nothing
                    else do putStrLn $ "  Solver " <> sname <> " error: " <> e
                            return $ Just (solver, SolverVersion "v?")