Permalink
Browse files

Stop using template haskell for config finder

The local config finder was generating the imports and a module list
and then we were using template haskell to actually create the list
at compile time. Now we just directly generate the correct haskell
source and include the generated file with the preprocessor.

The template haskell approach was not working well when the config
files were changed and recompiled. The compiled names would
sometimes change causing a link error in the previously generated
template haskell code.
  • Loading branch information...
1 parent fed1050 commit ae708348c862ecfe29d1a3d895269f299f4273e2 @dmpots committed Aug 26, 2010
Showing with 49 additions and 63 deletions.
  1. +1 −1 FindBench.hs
  2. +38 −0 FindConfig.hs
  3. +6 −33 Setup.hs
  4. +4 −6 tools/fibon-run/Fibon/Run/Config/Local.hs
  5. +0 −23 tools/fibon-run/Fibon/Run/Config/LocalConfigFinder.hs
View
@@ -29,7 +29,7 @@ findLocalBenchmarks baseDir = do
concat $ zipWith (\g bs -> map ((,)g) (sort bs)) allGroups bms
outFile = searchPath ++ ".hs"
putStrLn $ "... found ("++ (show.length$ allBms)++")"
- putStrLn $ "writing benchmark manifest to "++outFile
+ putStrLn $ " writing benchmark manifest to "++outFile
h <- openFile outFile WriteMode
hPutStrLn h moduleHeader
hPutStrLn h $ moduleImports (join "." benchmarksModule) qualifiedBms
View
@@ -0,0 +1,38 @@
+module FindConfig(findLocalConfigs, localConfigsFileName) where
+import Data.List
+import System.Directory
+import System.FilePath
+import System.IO
+
+findLocalConfigs :: FilePath -> IO ()
+findLocalConfigs cDir = do
+ fs <- getDirectoryContents cDir
+ putStr "\nLooking for local configuration modules... "
+ let modNames = map dropExtension $ filter (".hs" `isSuffixOf`) fs
+ let imports = map importStmt $ modNames
+ let modules = map importAs $ modNames
+ putStrLn $ "found ("++ (show.length$ modNames)++")"
+ writeToFile (localConfigsFileName cDir) (imports ++ [modulesList modules])
+ where
+ importStmt m = "import qualified "++m++" as " ++importAs m
+ writeToFile fName contents = do
+ h <- openFile fName WriteMode
+ putStrLn $ " writing config manifest to " ++ fName
+ --putStrLn $ unlines contents
+ hPutStr h (unlines contents)
+ hClose h
+
+modulesList :: [String] -> String
+modulesList mods = typ ++ lst
+ where
+ typ = "localConfigs :: [RunConfig]\n"
+ lst = "localConfigs = [" ++ (join ", " cfgs) ++ "]"
+ cfgs = map (++".config") mods
+ join s = concat . intersperse s
+
+importAs :: String -> String
+importAs modName = modName++"_Config"
+
+localConfigsFileName :: FilePath -> FilePath
+localConfigsFileName baseDir = baseDir </> "LocalConfigs.txt"
+
View
@@ -1,52 +1,25 @@
#!/usr/bin/env runhaskell
import Distribution.Simple
import Control.Monad
-import Data.List
import System.Directory
-import System.FilePath
-import System.IO
import FindBench
+import FindConfig
main = defaultMainWithHooks simpleUserHooks {postConf = writeLocalConf, postClean = deleteLocalConf}
writeLocalConf _ _ _ _ = do
- findLocalConfigModules configDir
- findLocalBenchmarks benchmarkDir
+ findLocalConfigs configDir
+ findLocalBenchmarks benchmarkDir
deleteLocalConf _ _ _ _ = do
- safeDelete (importsFileName configDir)
- safeDelete (modulesFileName configDir)
+ deleteIfExists (localConfigsFileName configDir)
-safeDelete :: FilePath -> IO ()
-safeDelete f = do
+deleteIfExists :: FilePath -> IO ()
+deleteIfExists f = do
e <- doesFileExist f
when e (removeFile f)
-findLocalConfigModules :: FilePath -> IO ()
-findLocalConfigModules cDir = do
- fs <- getDirectoryContents cDir
- putStr "\nLooking for local configuration modules... "
- let modNames = map dropExtension $ filter (".hs" `isSuffixOf`) fs
- let imports = map importStmt $ modNames
- let modules = map importAs $ modNames
- putStrLn $ "found ("++ (show.length$ modNames)++")"
- writeToFile (importsFileName cDir) imports
- writeToFile (modulesFileName cDir) modules
- where
- importStmt m = "import qualified "++m++" as " ++importAs m
- writeToFile fName contents = do
- h <- openFile fName WriteMode
- putStrLn $ "writing " ++ fName
- putStrLn $ unlines contents
- hPutStr h (unlines contents)
- hClose h
-
-importAs :: String -> String
-importAs modName = modName++"_Config"
configDir, benchmarkDir :: FilePath
configDir = "config"
benchmarkDir = "benchmarks"
-importsFileName, modulesFileName :: FilePath -> FilePath
-importsFileName baseDir = baseDir </> "LocalConfigImports.txt"
-modulesFileName baseDir = baseDir </> "LocalConfigModules.txt"
@@ -1,16 +1,14 @@
-{-# LANGUAGE TemplateHaskell, CPP #-}
+{-# LANGUAGE CPP #-}
module Fibon.Run.Config.Local(
configs
)
where
import Fibon.Run.Config
-import Fibon.Run.Config.LocalConfigFinder as LocalConfigFinder
-#include "LocalConfigImports.txt"
+-- This includes the conifg module imports and defines the localConfigs list
+#include "LocalConfigs.txt"
configs :: [(ConfigId, RunConfig)]
-configs = map (\x -> (configId x,x)) ms
- where
- ms = $(findLocalConfigModules "config")
+configs = map (\x -> (configId x,x)) localConfigs
@@ -1,23 +0,0 @@
-{-# LANGUAGE TemplateHaskell #-}
-module Fibon.Run.Config.LocalConfigFinder (
- findLocalConfigModules
-)
-where
-
-import Language.Haskell.TH
-import System.FilePath
-
-modulesFileName :: FilePath
-modulesFileName = "LocalConfigModules.txt"
-
-readLocalConfigModules :: FilePath -> IO [String]
-readLocalConfigModules baseDir = do
- ms <- readFile (baseDir </> modulesFileName)
- return (lines ms)
- where
-
-findLocalConfigModules :: FilePath -> Q Exp
-findLocalConfigModules baseDir = do
- ms <- runIO $ readLocalConfigModules baseDir
- return $ ListE $ map (\m -> (VarE . mkName) (m ++ ".config")) ms
-

0 comments on commit ae70834

Please sign in to comment.