AC-BuildPlatform 1.0.0 → 1.1.0
raw patch · 7 files changed
+203/−98 lines, 7 filessetup-changedPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- System.Platform: compiler_name :: Maybe String
+ System.Platform: compiler_name :: String
- System.Platform: compiler_version :: Maybe String
+ System.Platform: compiler_version :: String
- System.Platform: os_type :: Maybe OS_Type
+ System.Platform: os_type :: OS_Type
Files
- AC-BuildPlatform.cabal +15/−10
- Configure/Detect.hs +64/−0
- Configure/Write.hs +19/−0
- Manual.hs +70/−0
- ReadMe.xhtml +13/−3
- Setup.hs +16/−78
- System/Platform.hs +6/−7
AC-BuildPlatform.cabal view
@@ -1,6 +1,6 @@ Cabal-version: >=1.6 Name: AC-BuildPlatform -Version: 1.0.0 +Version: 1.1.0 Synopsis: Detect which OS you're running on. Description: @@ -13,18 +13,19 @@ the build environment. Once built, other packages can query this information without needing to do complex trickery themselves. . - Design assumptions: + See also the included @ReadMe.xhtml@ file. . - * You are using GHC, version 6.8.1 or newer. + Changes: . - * You build this package using the Cabal-install tool. (Just doing - @runhaskell Setup@ won't work properly.) + * If the configure step fails to detect the OS type, the configure + step now /fails/. . - If these conditions are not met, the package should still /build/, - it's just that all the compile-time constants will come back as - 'Nothing'. + * A new file @Manual.hs@ allows you to override the + auto-detected platform values with whatever you want. (See + @ReadMe.xhtml@ for details.) . - See also the included @ReadMe.xhtml@ file. + * All the platform queries no longer return @Maybe@ values. (This + breaks API compatibility, hence the version bump.) License: BSD3 License-file: License.txt @@ -33,7 +34,11 @@ Category: System Build-type: Custom Extra-tmp-files: Include/Platform.h -Extra-source-files: ReadMe.xhtml +Extra-source-files: + Configure/Detect.hs + Configure/Write.hs + Manual.hs + ReadMe.xhtml Library Exposed-modules:
+ Configure/Detect.hs view
@@ -0,0 +1,64 @@+module Configure.Detect (detect) where + +import Prelude hiding (catch) +import Control.Exception (catch, IOException) +import System.IO +import System.Process +import System.Exit (ExitCode (..)) + +detect :: IO (Maybe String, Maybe String, Maybe String) +detect = do + putStrLn "-| Attempting to detect platform data..." + m_txt <- gather + case m_txt of + Nothing -> return (Nothing, Nothing, Nothing) + Just txt -> parse txt + +gather :: IO (Maybe String) +gather = do + putStrLn "---| Attempting to invoke Setup.hs compiled program..." + let cmd0 = proc "./dist/setup/setup" ["+RTS", "--info"] + let cmd1 = cmd0 {std_out = CreatePipe} + + catch + (do + (Nothing, Just hout, Nothing, pid) <- createProcess cmd1 + txt <- hGetContents hout + exit <- waitForProcess pid + + case exit of + ExitFailure n -> do + putStrLn $ "---| Compiled Setup.hs program failed with return code " ++ show n + return Nothing + ExitSuccess -> return (Just txt) + ) + (\ e -> do + let f = e `asTypeOf` (undefined :: IOException) + putStrLn "---| Could not run compiled Setup.hs program." + return Nothing + ) + +parse :: String -> IO (Maybe String, Maybe String, Maybe String) +parse txt = do + putStrLn "---| OK. Parsing program result..." + case txt of + "" -> do + putStrLn "---| Empty result." + return (Nothing, Nothing, Nothing) + _ -> + case readsPrec 0 txt of + [] -> do + putStrLn "---| Parser error." + return (Nothing, Nothing, Nothing) + (info, _):_ -> + let + os = + case lookup "Target OS" info of + Nothing -> Nothing + Just "mingw32" -> Just "MS_Windows" + Just _ -> Just "Unix" + (name, ver) = + case lookup "GHC version" info of + Nothing -> (Nothing , Nothing) + Just v -> (Just "GHC", Just v ) + in return (os, name, ver)
+ Configure/Write.hs view
@@ -0,0 +1,19 @@+module Configure.Write (write) where + +import System.IO + +write :: (String, String, String) -> IO () +write (os, cn, cv) = do + putStrLn $ "-| Final data values:" + putStrLn $ "---| Operating System = " ++ os + putStrLn $ "---| Compiler name = " ++ show cn + putStrLn $ "---| Compiler version = " ++ show cv + + putStrLn $ "-| Saving data..." + writeFile "Include/Platform.h" $ unlines $ + [ + "#define HS_PNAME " ++ os, + "#define HS_CNAME " ++ show cn, + "#define HS_CVERS " ++ show cv + ] + putStrLn $ "-| Done."
+ Manual.hs view
@@ -0,0 +1,70 @@+module Main (main) where + +import System.IO +import System.Directory + +import Configure.Write (write) + +main = do + putStrLn "Manual.hs revision #01, 08 Jun 2011" + putStrLn "" + + x1 <- doesDirectoryExist "Include" + if x1 then return () else createDirectory "Include" + x2 <- doesFileExist "Include/Platform.h" + if x2 then msg2 else msg1 + +msg1 = do + putStrLn "This program allows you to manually specify the build platform" + putStrLn "values that will be returned from the System.Platform module." + putStrLn "Usually the configure script will auto-detect these; by specifying" + putStrLn "these values manually, you will disable auto-detection. Are you" + putStrLn "sure you want to do that?" + yes_no prompt_OS (putStrLn "Exit.") + +msg2 = do + putStrLn "This program allows you to manually specify the build platform" + putStrLn "values that will be returned from the System.Platform module." + putStrLn "The data in question has already been set; if you proceed, this" + putStrLn "data will be overwritten. Are you sure you want to do that?" + yes_no prompt_OS (putStrLn "Exit.") + +yes_no yes no = do + putStr "(Y/N)? " + hFlush stdout + l <- getLine + case l of + ('y':_) -> yes + ('Y':_) -> yes + _ -> no + +prompt_OS = do + putStrLn "" + putStrLn "Select OS type:" + putStrLn "1 = Unix" + putStrLn "2 = Microsoft Windows" + putStr "(1/2)? " + l <- getLine + case l of + "1" -> prompt_CName ("Unix") + "2" -> prompt_CName ("MS_Windows") + _ -> do + putStrLn "Invalid response." + prompt_OS + +prompt_CName os = do + putStrLn "" + putStrLn "Enter compiler name string (e.g., \"GHC\", \"JHC\", etc.)" + putStrLn "Your text will be used verbatim." + putStr "Name> " + cname <- getLine + prompt_CVer os cname + +prompt_CVer os cname = do + putStrLn "" + putStrLn "Enter compiler version string (e.g., \"6.6\", \"7.0.2\", etc.)" + putStrLn "Your text will be used verbatim." + putStrLn "Version> " + cver <- getLine + putStrLn "" + write (os, cname, cver)
ReadMe.xhtml view
@@ -29,8 +29,18 @@ This package allows you to query at run-time what operating system and compiler are in use. This information is not easy to discover — hence why this package exists in the first place. The package runs some custom code during the “configure” stage. This code attempts to detect what platform it's running under. Note that the script <em>prints out</em> the values detected, so you don't have to actually build and install the package just to find out if platform detection is working right. </p> <p> -The platform detection code makes a number of assumptions, which are documented here. From the Haddock documentation, you can see that all the queries provided by this package return <code>Maybe</code> something. If part of the detection algorithm fails, the corresponding value is just reported as <code>Nothing</code> rather than a value. The package should <em>build</em> under all circumstances; it's just that you will only get useful information if the assumptions below are respected. +The platform detection code makes a number of assumptions, which are documented here. If these assumptions fail, it may not be possible to detect some or all of the platform values. Note that if the OS type cannot be detected, the configure step <em>will fail</em>, preventing the package from being built and installed. </p> +<h2>Manual override</h2> +<p> +There is a small utility called <code>Manual.hs</code>. If you run this, it will let you set the platform values to whatever you like. This kludge lets you specify that your compiler is <em>Fred's Haskell Compiler</em> or whatever. +</p> +<p> + You must run this before the “build” and/or “haddock” stages, but you can run it before or after the “configure” stage. So if you run a “configure” and it fails, and you can't correct the failure, you can use a manual override to specify the correct values and then run “configure” again. +</p> +<p> +Note that performing a manual override turns off the auto-detection logic in “configure”. To turn it back on again, just perform a “clean” (which removes any manual values you specified). See the notes above about performing a “clean” operation though. +</p> <h2>Platform detection assumptions</h2> <p> @@ -39,11 +49,11 @@ <ol> <li> <p><b>You configure the package using “<code>cabal configure</code>”, not “<code>runhaskell Setup configure</code>”.</b></p> - <p>If this is not the case, no information is detected.</p> + <p>If this is not the case, no information is detected [and therefore configure fails].</p> </li> <li> <p><b>Your compiler is GHC, version 6.8.1 or newer.</b></p> - <p>If this is not the case, most likely no information will be detected.</p> + <p>If this is not the case, most likely no information will be detected [and therefore configure will fail].</p> <p>I realise that being able to query the compiler is a bit pointless when only one possible compiler is supported. Note though that if the compiler is <em>not</em> GHC, it will <em>not</em> be reported as GHC. So there is some small information content there.</p> </li> <li>
Setup.hs view
@@ -1,87 +1,25 @@ module Main where -import Prelude hiding (catch) import Distribution.Simple -import Control.Exception (catch, IOException) -import System.IO import System.Directory -import System.Process -import System.Exit (ExitCode (..)) -main = defaultMainWithHooks hooks - -hooks = simpleUserHooks {postConf = gather} - -gather args cfg pkg bld = do - putStrLn "-| Gathering platform data..." - - putStrLn "---| Attempting to invoke Setup.hs compiled program..." - let cmd0 = proc "./dist/setup/setup" ["+RTS", "--info"] - let cmd1 = cmd0 {std_out = CreatePipe} - - catch - (do - (Nothing, Just hout, Nothing, pid) <- createProcess cmd1 - txt <- hGetContents hout - exit <- waitForProcess pid - - case exit of - ExitFailure n -> do - putStrLn $ "---| Compiled Setup.hs program failed with return code " ++ show n - save Nothing Nothing Nothing - ExitSuccess -> do - (os, cn, cv) <- parse txt - save os cn cv - ) - (\ e -> do - let f = e `asTypeOf` (undefined :: IOException) - putStrLn "---| Could not run." - save Nothing Nothing Nothing - ) +import Configure.Detect (detect) +import Configure.Write (write) -parse :: String -> IO (Maybe String, Maybe String, Maybe String) -parse txt = do - putStrLn "---| OK. Parsing program result..." - case txt of - "" -> do - putStrLn "---| Empty result." - return (Nothing, Nothing, Nothing) - _ -> - case readsPrec 0 txt of - [] -> do - putStrLn "---| Parser error." - return (Nothing, Nothing, Nothing) - (info, _):_ -> - let - os = - case lookup "Target OS" info of - Nothing -> Nothing - Just "mingw32" -> Just "MS_Windows" - Just _ -> Just "Unix" - (name, ver) = - case lookup "GHC version" info of - Nothing -> (Nothing , Nothing) - Just v -> (Just "GHC", Just v ) - in return (os, name, ver) +main = defaultMainWithHooks hooks -save :: Maybe String -> Maybe String -> Maybe String -> IO () -save os cn cv = do - putStrLn $ "-----| Operating System = " ++ show os - putStrLn $ "-----| Compiler name = " ++ show cn - putStrLn $ "-----| Compiler version = " ++ show cv +hooks = simpleUserHooks {postConf = check} - x <- doesDirectoryExist "Include" - if x - then return () +check _ _ _ _ = do + putStrLn "-| Checking for platform values..." + x1 <- doesDirectoryExist "Include" + if x1 then return () else createDirectory "Include" + x2 <- doesFileExist "Include/Platform.h" + if x2 + then putStrLn "-| Platform values have already been set." else do - putStrLn "-| Create Include folder..." - createDirectory "Include" - - putStrLn $ "-| Saving results..." - writeFile "Include/Platform.h" $ unlines $ - [ - "#define HS_PNAME " ++ (case os of Nothing -> "Nothing"; Just t -> "Just " ++ t), - "#define HS_CNAME " ++ show cn, - "#define HS_CVERS " ++ show cv - ] - putStrLn $ "-| Done." + putStrLn "-| Platform values are not yet set. Attempting to guess them..." + (m_os, m_cname, m_cver) <- detect + case m_os of + Nothing -> putStrLn "-| FATAL: Cannot detect OS type." + Just os -> write (os, maybe "" id m_cname, maybe "" id m_cver)
System/Platform.hs view
@@ -44,24 +44,23 @@ deriving (Eq, Show) {- | - The type of operating system under which we are running (or - 'Nothing' if this could not be detected). + The type of operating system under which we are running. -} -os_type :: Maybe OS_Type +os_type :: OS_Type os_type = HS_PNAME {- | - The name of the compiler (e.g., @\"GHC\"@), or 'Nothing' if + The name of the compiler (e.g., @\"GHC\"@), or \"Unknown\" if this could not be detected. -} -compiler_name :: Maybe String +compiler_name :: String compiler_name = HS_CNAME {- | The numerical version string for the compiler - (e.g., @\"6.10.2\"@), or 'Nothing' if this could not be detected. + (e.g., @\"6.10.2\"@), or \"\" if this could not be detected. -} -compiler_version :: Maybe String +compiler_version :: String compiler_version = HS_CVERS {- $local