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