git.iko.soy/typeable/entropy
on a8b0d89 help sign in
49 lines 2.4 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 let hcProg = Program "ghc" undefined undefined undefined
18 mConf = lookupProgram hcProg (withPrograms lbi)
19 err = error "Could not determine C compiler"
20 cc = locationPath . programLocation . maybe err id $ mConf
21 b <- canUseRDRAND cc
22 let newWithPrograms1 = userSpecifyArgs "gcc" cArgs (withPrograms lbi)
23 newWithPrograms = userSpecifyArgs "ghc" cArgsHC newWithPrograms1
24 lbiNew = if b then (lbi {withPrograms = newWithPrograms }) else lbi
25 buildHook simpleUserHooks pd lbiNew uh bf
26 }
28cArgs :: [String]
29cArgs = ["-DHAVE_RDRAND"]
31cArgsHC :: [String]
32cArgsHC = map ("-optc" ++) cArgs
34canUseRDRAND :: FilePath -> IO Bool
35canUseRDRAND cc = do
36 withTempDirectory normal "" "testRDRAND" $ \tmpDir -> do
37 writeFile (tmpDir ++ "/testRDRAND.c")
38 (unlines [ "#include <stdint.h>"
39 , "int main() {"
40 , " uint64_t therand;"
41 , " unsigned char err;"
42 , " asm volatile(\"rdrand %0 ; setc %1\""
43 , " : \"=r\" (therand), \"=qm\" (err));"
44 , " return (!err);"
45 , "}"
46 ])
47 ec <- rawSystemExitCode normal cc [tmpDir </> "testRDRAND.c", "-o", tmpDir ++ "/a.o","-c"]
48 notice normal $ "Result of RDRAND Test: " ++ show (ec == ExitSuccess)
49 return (ec == ExitSuccess)