git.iko.soy/typeable/entropy
on f0044cc help sign in
53 lines 2.6 kB history raw
1import Control.Monad
2import Distribution.Simple
3import Distribution.Simple.LocalBuildInfo
4import Distribution.Simple.Setup
5import Distribution.PackageDescription
6import Distribution.Simple.Utils
7import Distribution.Simple.Program
8import Distribution.Verbosity
9import System.Process
10import System.Directory
11import System.FilePath
12import System.Exit
14main = defaultMainWithHooks hk
15 where
16 hk = simpleUserHooks { buildHook = \pd lbi uh bf -> do
17 let ccProg0 = Program "gcc" undefined undefined undefined
18 ccProg1 = Program "clang" undefined undefined undefined
19 f x = lookupProgram x (withPrograms lbi)
20 mConf = f ccProg0 `mplus` f ccProg1
21 err = error "Could not determine C compiler"
22 cc = locationPath . programLocation . maybe err id $ mConf
23 print (withPrograms lbi)
24 b <- canUseRDRAND cc
25 let newWithPrograms1 = userSpecifyArgs "gcc" cArgs (withPrograms lbi)
26 newWithPrograms = userSpecifyArgs "ghc" cArgsHC newWithPrograms1
27 lbiNew = if b then (lbi {withPrograms = newWithPrograms }) else lbi
28 buildHook simpleUserHooks pd lbiNew uh bf
29 }
31cArgs :: [String]
32cArgs = ["-DHAVE_RDRAND"]
34cArgsHC :: [String]
35cArgsHC = map ("-optc" ++) cArgs
37canUseRDRAND :: FilePath -> IO Bool
38canUseRDRAND cc = do
39 withTempDirectory normal "" "testRDRAND" $ \tmpDir -> do
40 -- withTempDirectory normal False "" "testRDRAND" $ \tmpDir -> do
41 writeFile (tmpDir ++ "/testRDRAND.c")
42 (unlines [ "#include <stdint.h>"
43 , "int main() {"
44 , " uint64_t therand;"
45 , " unsigned char err;"
46 , " asm volatile(\"rdrand %0 ; setc %1\""
47 , " : \"=r\" (therand), \"=qm\" (err));"
48 , " return (!err);"
49 , "}"
50 ])
51 ec <- rawSystemExitCode normal cc [tmpDir </> "testRDRAND.c", "-o" ++ tmpDir ++ "/a.out"]
52 notice normal $ "Result of RDRAND Test: " ++ show (ec == ExitSuccess)
53 return (ec == ExitSuccess)