2011-08-20 20:11:42 +00:00
|
|
|
{- Tests the system and generates Build.SysConfig.hs. -}
|
2011-01-20 00:02:48 +00:00
|
|
|
|
2015-05-10 20:38:49 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
2011-08-20 20:11:42 +00:00
|
|
|
module Build.TestConfig where
|
2011-01-20 00:02:48 +00:00
|
|
|
|
2012-12-14 19:52:44 +00:00
|
|
|
import Utility.Path
|
|
|
|
import Utility.Monad
|
2013-05-10 20:21:32 +00:00
|
|
|
import Utility.SafeCommand
|
2016-04-28 19:18:11 +00:00
|
|
|
import Utility.Directory
|
2012-12-14 19:52:44 +00:00
|
|
|
|
2011-01-20 00:02:48 +00:00
|
|
|
import System.IO
|
2012-12-14 19:52:44 +00:00
|
|
|
import System.FilePath
|
2011-01-20 00:02:48 +00:00
|
|
|
|
|
|
|
type ConfigKey = String
|
2011-04-08 01:47:56 +00:00
|
|
|
data ConfigValue =
|
|
|
|
BoolConfig Bool |
|
|
|
|
StringConfig String |
|
2012-03-22 01:21:20 +00:00
|
|
|
MaybeStringConfig (Maybe String) |
|
|
|
|
MaybeBoolConfig (Maybe Bool)
|
2011-01-20 00:02:48 +00:00
|
|
|
data Config = Config ConfigKey ConfigValue
|
|
|
|
|
|
|
|
type Test = IO Config
|
|
|
|
type TestName = String
|
|
|
|
data TestCase = TestCase TestName Test
|
|
|
|
|
|
|
|
instance Show ConfigValue where
|
|
|
|
show (BoolConfig b) = show b
|
|
|
|
show (StringConfig s) = show s
|
2011-04-08 01:47:56 +00:00
|
|
|
show (MaybeStringConfig s) = show s
|
2012-03-22 01:21:20 +00:00
|
|
|
show (MaybeBoolConfig s) = show s
|
2011-01-20 00:02:48 +00:00
|
|
|
|
|
|
|
instance Show Config where
|
2011-04-08 01:47:56 +00:00
|
|
|
show (Config key value) = unlines
|
2011-01-20 00:02:48 +00:00
|
|
|
[ key ++ " :: " ++ valuetype value
|
|
|
|
, key ++ " = " ++ show value
|
|
|
|
]
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
valuetype (BoolConfig _) = "Bool"
|
|
|
|
valuetype (StringConfig _) = "String"
|
|
|
|
valuetype (MaybeStringConfig _) = "Maybe String"
|
|
|
|
valuetype (MaybeBoolConfig _) = "Maybe Bool"
|
2011-01-20 00:02:48 +00:00
|
|
|
|
|
|
|
writeSysConfig :: [Config] -> IO ()
|
2012-03-23 16:43:21 +00:00
|
|
|
writeSysConfig config = writeFile "Build/SysConfig.hs" body
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
body = unlines $ header ++ map show config ++ footer
|
|
|
|
header = [
|
|
|
|
"{- Automatically generated. -}"
|
|
|
|
, "module Build.SysConfig where"
|
|
|
|
, ""
|
|
|
|
]
|
|
|
|
footer = []
|
2011-01-20 00:02:48 +00:00
|
|
|
|
|
|
|
runTests :: [TestCase] -> IO [Config]
|
|
|
|
runTests [] = return []
|
2011-07-15 07:12:05 +00:00
|
|
|
runTests (TestCase tname t : ts) = do
|
2011-01-20 00:02:48 +00:00
|
|
|
testStart tname
|
|
|
|
c <- t
|
|
|
|
testEnd c
|
|
|
|
rest <- runTests ts
|
|
|
|
return $ c:rest
|
|
|
|
|
|
|
|
{- Checks if a command is available by running a command line. -}
|
|
|
|
testCmd :: ConfigKey -> String -> Test
|
|
|
|
testCmd k cmdline = do
|
2013-05-10 20:21:32 +00:00
|
|
|
ok <- boolSystem "sh" [ Param "-c", Param $ quiet cmdline ]
|
|
|
|
return $ Config k (BoolConfig ok)
|
2011-01-20 00:02:48 +00:00
|
|
|
|
|
|
|
{- Ensures that one of a set of commands is available by running each in
|
|
|
|
- turn. The Config is set to the first one found. -}
|
configure: Check that checksum programs produce correct checksums. + bitter rant
So, it might be called sha1sum, or on some other OS, it might be called
sha1. It might be hidden away off of PATH on that OS. That's just expected
insanity; UNIX has been this way since 1980's. And these days, nobody even
gives the flying flip about standards that we briefly did in the 90's
after the first round of unix wars.
But it's the 2010's now, and we've certainly learned something.
So, let's make it so sometimes sha1 is a crazy program that wants to run as
root so it can lock memory while prompting for a passphrase, and outputting
binary garbage. Yes, that'd be wise. Let's package that in major Linux
distros, too, so users can stumble over it.
2012-10-25 04:05:12 +00:00
|
|
|
selectCmd :: ConfigKey -> [(String, String)] -> Test
|
2011-04-08 04:12:00 +00:00
|
|
|
selectCmd k = searchCmd
|
2011-07-15 07:12:05 +00:00
|
|
|
(return . Config k . StringConfig)
|
2011-04-08 04:12:00 +00:00
|
|
|
(\cmds -> do
|
|
|
|
testEnd $ Config k $ BoolConfig False
|
|
|
|
error $ "* need one of these commands, but none are available: " ++ show cmds
|
|
|
|
)
|
|
|
|
|
configure: Check that checksum programs produce correct checksums. + bitter rant
So, it might be called sha1sum, or on some other OS, it might be called
sha1. It might be hidden away off of PATH on that OS. That's just expected
insanity; UNIX has been this way since 1980's. And these days, nobody even
gives the flying flip about standards that we briefly did in the 90's
after the first round of unix wars.
But it's the 2010's now, and we've certainly learned something.
So, let's make it so sometimes sha1 is a crazy program that wants to run as
root so it can lock memory while prompting for a passphrase, and outputting
binary garbage. Yes, that'd be wise. Let's package that in major Linux
distros, too, so users can stumble over it.
2012-10-25 04:05:12 +00:00
|
|
|
maybeSelectCmd :: ConfigKey -> [(String, String)] -> Test
|
2011-04-08 04:12:00 +00:00
|
|
|
maybeSelectCmd k = searchCmd
|
2011-07-15 07:12:05 +00:00
|
|
|
(return . Config k . MaybeStringConfig . Just)
|
2011-04-08 04:12:00 +00:00
|
|
|
(\_ -> return $ Config k $ MaybeStringConfig Nothing)
|
|
|
|
|
configure: Check that checksum programs produce correct checksums. + bitter rant
So, it might be called sha1sum, or on some other OS, it might be called
sha1. It might be hidden away off of PATH on that OS. That's just expected
insanity; UNIX has been this way since 1980's. And these days, nobody even
gives the flying flip about standards that we briefly did in the 90's
after the first round of unix wars.
But it's the 2010's now, and we've certainly learned something.
So, let's make it so sometimes sha1 is a crazy program that wants to run as
root so it can lock memory while prompting for a passphrase, and outputting
binary garbage. Yes, that'd be wise. Let's package that in major Linux
distros, too, so users can stumble over it.
2012-10-25 04:05:12 +00:00
|
|
|
searchCmd :: (String -> Test) -> ([String] -> Test) -> [(String, String)] -> Test
|
|
|
|
searchCmd success failure cmdsparams = search cmdsparams
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
search [] = failure $ fst $ unzip cmdsparams
|
|
|
|
search ((c, params):cs) = do
|
2013-05-10 20:21:32 +00:00
|
|
|
ok <- boolSystem "sh" [ Param "-c", Param $ quiet $ c ++ " " ++ params ]
|
|
|
|
if ok
|
2012-11-11 04:51:07 +00:00
|
|
|
then success c
|
|
|
|
else search cs
|
2011-04-08 00:08:11 +00:00
|
|
|
|
2012-12-14 19:52:44 +00:00
|
|
|
{- Finds a command, either in PATH or perhaps in a sbin directory not in
|
|
|
|
- PATH. If it's in PATH the config is set to just the command name,
|
|
|
|
- but if it's found outside PATH, the config is set to the full path to
|
|
|
|
- the command. -}
|
|
|
|
findCmdPath :: ConfigKey -> String -> Test
|
|
|
|
findCmdPath k command = do
|
|
|
|
ifM (inPath command)
|
|
|
|
( return $ Config k $ MaybeStringConfig $ Just command
|
|
|
|
, do
|
|
|
|
r <- getM find ["/usr/sbin", "/sbin", "/usr/local/sbin"]
|
|
|
|
return $ Config k $ MaybeStringConfig r
|
|
|
|
)
|
|
|
|
where
|
|
|
|
find d =
|
|
|
|
let f = d </> command
|
|
|
|
in ifM (doesFileExist f) ( return (Just f), return Nothing )
|
|
|
|
|
2011-01-20 00:02:48 +00:00
|
|
|
quiet :: String -> String
|
|
|
|
quiet s = s ++ " >/dev/null 2>&1"
|
|
|
|
|
|
|
|
testStart :: TestName -> IO ()
|
|
|
|
testStart s = do
|
|
|
|
putStr $ " checking " ++ s ++ "..."
|
|
|
|
hFlush stdout
|
|
|
|
|
|
|
|
testEnd :: Config -> IO ()
|
2011-07-15 07:12:05 +00:00
|
|
|
testEnd (Config _ (BoolConfig True)) = status "yes"
|
|
|
|
testEnd (Config _ (BoolConfig False)) = status "no"
|
|
|
|
testEnd (Config _ (StringConfig s)) = status s
|
|
|
|
testEnd (Config _ (MaybeStringConfig (Just s))) = status s
|
|
|
|
testEnd (Config _ (MaybeStringConfig Nothing)) = status "not available"
|
2012-03-22 01:21:20 +00:00
|
|
|
testEnd (Config _ (MaybeBoolConfig (Just True))) = status "yes"
|
|
|
|
testEnd (Config _ (MaybeBoolConfig (Just False))) = status "no"
|
|
|
|
testEnd (Config _ (MaybeBoolConfig Nothing)) = status "unknown"
|
2011-07-15 07:12:05 +00:00
|
|
|
|
|
|
|
status :: String -> IO ()
|
|
|
|
status s = putStrLn $ ' ':s
|