Commit 8ef19c28 authored by ijones's avatar ijones
Browse files

basic preprocessing works! with some bugs

End-to-end preprocessing solution, including some test cases.
One test case fails:
  right now, preprocessors just stick files into the current
  directory, then copies them into the destination.  this is no good
  since we now won't be able to tell them from real source files the
  next time around.  clean is broken here...
parent 2a55e116
......@@ -168,6 +168,11 @@ tests currDir
assertBool "build did not create the executable: testA"
doesFileExist "dist/build/testB" >>=
assertBool "build did not create the executable: testB"
doesFileExist "dist/build/C.o" >>=
assertBool "C.testSuffix did not get compiled to C.o."
doesFileExist "C.hs" >>=
assertEqual "C.hs (a generated file) should not be in the top-level of the source tree"
False
assertCmd "./setup sdist"
"setup sdist returned error code"
doesFileExist "dist/test-1.0.tgz" >>=
......
......@@ -37,7 +37,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
module Distribution.PreProcess (preprocessSources, knownSuffixes,
module Distribution.PreProcess (preprocessSources, knownSuffixHandlers,
PPSuffixHandler, PreProcessor)
where
......@@ -45,9 +45,12 @@ import Distribution.PreProcess.Unlit(plain, unlit)
import Distribution.Package (PackageDescription(..), BuildInfo(..), Executable(..))
import Distribution.Simple.Configure (LocalBuildInfo(..))
import Distribution.Simple.Utils (setupMessage,moveSources, pathJoin,
withLib, rawSystemPath, splitFilePath)
withLib, rawSystemPath, splitFilePath,
joinFilenameDir, joinExt, moduleToFilePath)
import System.Exit (ExitCode(..))
import Data.Maybe(catMaybes)
-- |A preprocessor must fulfill this basic interface. It can be an
-- external program, or just a function.
type PreProcessor = FilePath -- ^Location of the source file in need of preprocessing
......@@ -71,19 +74,23 @@ preprocessSources :: PackageDescription
sources in -}
-> IO ()
preprocessSources pkg_descr _ _ pref =
preprocessSources pkg_descr _ handlers pref =
do
setupMessage "Preprocessing" pkg_descr
-- preprocess all sources before moving them
allSources <- findAllSourceFiles pkg_descr [a | (a, _, _) <- knownSuffixHandlers]
sequence [dispatchPP src handlers | src <- allSources] -- FIX: output errors?
-- move sources into place
withLib pkg_descr $ \lib ->
moveSources (hsSourceDir lib) (pathJoin [pref, hsSourceDir lib]) (modules lib) ["hs","lhs"]
sequence_ [ moveSources (hsSourceDir exeBi) (pathJoin [pref, hsSourceDir exeBi]) (modules exeBi) ["hs","lhs"]
| Executable _ _ exeBi <- executables pkg_descr]
dispatchPP :: FilePath -> [ PPSuffixHandler ] -> IO ()
dispatchPP :: FilePath -> [ PPSuffixHandler ] -> IO ExitCode
dispatchPP p handlers
= do let (dir, file, ext) = splitFilePath p
let (Just (lit, pp)) = findPP ext handlers
return ()
pp p (joinFilenameDir dir (joinExt file "hs"))
findPP :: String -- ^Extension
-> [PPSuffixHandler]
......@@ -94,6 +101,28 @@ findPP ext ((e2, lit, pp):t)
findPP _ [] = Nothing
-- |Locate the source files based on the module names, the search
-- pathes (both in PackageDescription) and the suffixes we might be
-- interested in.
findAllSourceFiles :: PackageDescription
-> [String] -- ^search suffixes
-> IO [FilePath]
findAllSourceFiles PackageDescription{executables=execs, library=lib} allSuffixes
= do exeFiles <- sequence [buildInfoSources (buildInfo e) allSuffixes | e <- execs]
libFiles <- case lib of
Just bi -> buildInfoSources bi allSuffixes
Nothing -> return []
return $ catMaybes ((concat exeFiles) ++ libFiles)
where buildInfoSources :: BuildInfo -> [String] -> IO [Maybe FilePath]
buildInfoSources BuildInfo{modules=mods, hsSourceDir=dir} allSuffixes
= sequence [moduleToFilePath dir mod allSuffixes | mod <- mods]
-- ------------------------------------------------------------
-- * known preprocessors
-- ------------------------------------------------------------
ppCpp, ppGreenCard, ppHsc2hs, ppC2hs, ppHappy, ppNone :: PreProcessor
ppCpp inFile outFile
......@@ -106,14 +135,19 @@ ppC2hs inFile outFile
ppHappy = standardPP "happy"
ppNone _ _ = return ExitSuccess
ppTestHandler inFile outFile
= do stuff <- readFile inFile
writeFile outFile ("-- this file has been preprocessed as a test\n\n" ++ stuff)
return ExitSuccess
standardPP :: String -> PreProcessor
standardPP eName inFile outFile
= rawSystemPath eName ["-o" ++ outFile, inFile]
-- |Leave in unlit since some preprocessors can't handle literated
-- source?
knownSuffixes :: [ PPSuffixHandler ]
knownSuffixes =
knownSuffixHandlers :: [ PPSuffixHandler ]
knownSuffixHandlers =
[ ("gc", plain, ppGreenCard)
, ("chs", plain, ppC2hs)
, ("hsc", plain, ppHsc2hs)
......@@ -123,4 +157,5 @@ knownSuffixes =
, ("gc", plain, ppNone) -- note, for nhc98 only
, ("hs", plain, ppNone)
, ("lhs", unlit, ppNone)
, ("testSuffix", plain, ppTestHandler)
]
......@@ -51,7 +51,7 @@ import Distribution.Misc (extensionsToGHCFlag, extensionsToNHCFlag)
import Distribution.Setup (Compiler(..), CompilerFlavor(..))
import Distribution.Package (PackageIdentifier(..), PackageDescription(..),
BuildInfo(..), showPackageId, Executable(..))
import Distribution.PreProcess (preprocessSources)
import Distribution.PreProcess (preprocessSources, knownSuffixHandlers)
import Distribution.Simple.Configure (LocalBuildInfo(..), compiler, exeDeps)
import Distribution.Simple.Utils (rawSystemExit, setupMessage,
die, rawSystemPathExit,
......@@ -79,7 +79,7 @@ build :: FilePath -- ^Build location
-> PackageDescription -> LocalBuildInfo -> IO ()
build pref pkg_descr lbi = do
createIfNotExists True pref
preprocessSources pkg_descr lbi [] pref
preprocessSources pkg_descr lbi knownSuffixHandlers pref
setupMessage "Building" pkg_descr
case compilerFlavor (compiler lbi) of
GHC -> buildGHC pref pkg_descr lbi
......
** integrate w/ GHC distro
** Preprocessing
- move files to output location
- integrate into preprocessSources function
- what other preprocessors can't unlit?
- right now, preprocessors just stick files into the current
directory, then copies them into the destination. this is no good
since we now won't be able to tell them from real source files the
next time around. clean is broken here...
** clarify description filename issues
- allow foo.hsproj?
......
Name: test
Version: 1.0
Modules: A, B.A
Modules: A, B.A, C
Exposed-Modules: A
C-Sources: hello.c
Extensions: ForeignFunctionInterface
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment