Rework how temporary files are named.

The temporary files used during compilation are now just the output name with
an appropriate extension added (so when compiling to cgtest02, it'll produce
cgtest02.cpp, etc.). This is what GCC and KRoC do; it keeps the temp files with
the binaries that were produced from them, and avoids having to pick a sensible
directory to put randomly-named temp files in.
This commit is contained in:
Adam Sampson 2008-02-29 22:53:45 +00:00
parent d048a0ef71
commit 168628576d
3 changed files with 46 additions and 42 deletions

74
Main.hs
View File

@ -192,38 +192,51 @@ instance Die (StateT [FilePath] PassM) where
lift $ dieReport err lift $ dieReport err
compileFull :: String -> StateT [FilePath] PassM () compileFull :: String -> StateT [FilePath] PassM ()
compileFull fn compileFull inputFile
= do optsPS <- lift get = do optsPS <- lift get
destBin <- case csOutputFile optsPS of outputFile <- case csOutputFile optsPS of
"-" -> dieReport (Nothing, "Must specify an output file when using full-compile mode") "-" -> dieReport (Nothing, "Must specify an output file when using full-compile mode")
file -> return file file -> return file
-- First, compile the code into C/C++: let extension = case csBackend optsPS of
tempCPath <- execWithTempFile "tock-temp-c" (compile ModeCompile fn) BackendC -> ".c"
BackendCPPCSP -> ".cpp"
_ -> ""
-- Translate input file to C/C++
let cFile = outputFile ++ extension
withOutputFile cFile $ compile ModeCompile inputFile
noteFile cFile
-- Then, compile the C/C++:
case csBackend optsPS of case csBackend optsPS of
BackendC -> BackendC ->
do -- Compile the C into an object file: let sFile = outputFile ++ ".s"
exec $ cCommand tempCPath (tempCPath ++ ".o") oFile = outputFile ++ ".o"
noteFile (tempCPath ++ ".o") postCFile = outputFile ++ "_post.c"
-- Compile the same C into assembly: postOFile = outputFile ++ "_post.o"
exec $ cAsmCommand tempCPath (tempCPath ++ ".s") occFile = outputFile ++ "_wrapper.occ"
noteFile (tempCPath ++ ".s") in
do sequence_ $ map noteFile [sFile, oFile, postCFile, postOFile, occFile]
-- Compile the C into assembly, and assembly into an object file
exec $ cAsmCommand cFile sFile
exec $ cCommand sFile oFile
-- Analyse the assembly for stack sizes, and output a -- Analyse the assembly for stack sizes, and output a
-- "post" C file: -- "post" C file
tempCPathPost <- execWithTempFile "tock-temp-post-c" (postCAnalyse (tempCPath ++ ".s")) withOutputFile postCFile $ postCAnalyse sFile
-- Compile this new "post" C file into an object file: -- Compile this new "post" C file into an object file
exec $ cCommand tempCPathPost (tempCPathPost ++ ".o") exec $ cCommand postCFile postOFile
noteFile (tempCPathPost ++ ".o")
-- Create a temporary occam file, and write the standard -- Create a temporary occam file, and write the standard
-- occam wrapper into it: -- occam wrapper into it
tempPathOcc <- execWithTempFile "tock-temp-occ.occ" (liftIO . writeOccamWrapper) withOutputFile occFile $ liftIO . writeOccamWrapper
-- Use kroc to compile and link the occam file with the two -- Use kroc to compile and link the occam file with the two
-- object files from the C compilation: -- object files from the C compilation
exec $ krocLinkCommand tempPathOcc [(tempCPath ++ ".o"), (tempCPathPost ++ ".o")] destBin exec $ krocLinkCommand occFile [oFile, postOFile] outputFile
-- For C++, just compile the source file directly into a binary:
BackendCPPCSP -> exec $ cxxCommand tempCPath destBin -- For C++, just compile the source file directly into a binary
BackendCPPCSP ->
exec $ cxxCommand cFile outputFile
_ -> dieReport (Nothing, "Cannot use specified backend: " _ -> dieReport (Nothing, "Cannot use specified backend: "
++ show (csBackend optsPS) ++ show (csBackend optsPS)
++ " with full-compile mode") ++ " with full-compile mode")
@ -237,20 +250,11 @@ compileFull fn
noteFile :: Monad m => FilePath -> StateT [FilePath] m () noteFile :: Monad m => FilePath -> StateT [FilePath] m ()
noteFile fp = modify (\fps -> (fp:fps)) noteFile fp = modify (\fps -> (fp:fps))
-- Takes a temporary file pattern, a function to do something with that withOutputFile :: FilePath -> (Handle -> PassM ()) -> StateT [FilePath] PassM ()
-- file, and returns the path of the now-closed temporary file withOutputFile path func
execWithTempFile' :: String -> (Handle -> PassM ()) -> PassM FilePath = do handle <- liftIO $ openFile path WriteMode
execWithTempFile' pat func lift $ func handle
= do (path,handle) <- liftIO $ openTempFile "." pat
func handle
liftIO $ hClose handle liftIO $ hClose handle
return path
execWithTempFile :: String -> (Handle -> PassM ()) -> StateT [FilePath] PassM FilePath
execWithTempFile pat func
= do file <- lift $ execWithTempFile' pat func
noteFile file
return file
exec :: String -> StateT [FilePath] PassM () exec :: String -> StateT [FilePath] PassM ()
exec cmd = do lift $ progress $ "Executing command: " ++ cmd exec cmd = do lift $ progress $ "Executing command: " ++ cmd

View File

@ -47,13 +47,13 @@ CompilerCommands.hs: Makefile
echo -e '--This file is auto-generated by Makefile.am\n' >> CompilerCommands.hs echo -e '--This file is auto-generated by Makefile.am\n' >> CompilerCommands.hs
echo -e 'import Data.List\n' >> CompilerCommands.hs echo -e 'import Data.List\n' >> CompilerCommands.hs
echo -e 'cCommand :: String -> String -> String\n' >> CompilerCommands.hs echo -e 'cCommand :: String -> String -> String\n' >> CompilerCommands.hs
echo -e 'cCommand inp out = "$(CC) $(TOCK_CFLAGS) -x c -c -o " ++ out ++ " " ++ inp\n' >> CompilerCommands.hs echo -e 'cCommand inp out = "$(CC) $(TOCK_CFLAGS) -c -o " ++ out ++ " " ++ inp\n' >> CompilerCommands.hs
echo -e 'cAsmCommand :: String -> String -> String\n' >> CompilerCommands.hs echo -e 'cAsmCommand :: String -> String -> String\n' >> CompilerCommands.hs
echo -e 'cAsmCommand inp out = "$(CC) $(TOCK_CFLAGS) -x c -S -o " ++ out ++ " " ++ inp\n' >> CompilerCommands.hs echo -e 'cAsmCommand inp out = "$(CC) $(TOCK_CFLAGS) -S -o " ++ out ++ " " ++ inp\n' >> CompilerCommands.hs
echo -e 'krocLinkCommand :: String -> [String] -> String -> String\n' >> CompilerCommands.hs echo -e 'krocLinkCommand :: String -> [String] -> String -> String\n' >> CompilerCommands.hs
echo -e 'krocLinkCommand wrapper files out = "kroc -o " ++ out ++ " " ++ wrapper ++ " " ++ (concat (intersperse " " files)) ++ " -lcif"\n' >> CompilerCommands.hs echo -e 'krocLinkCommand wrapper files out = "kroc -o " ++ out ++ " " ++ wrapper ++ " " ++ (concat (intersperse " " files)) ++ " -lcif"\n' >> CompilerCommands.hs
echo -e 'cxxCommand :: String -> String -> String\n' >> CompilerCommands.hs echo -e 'cxxCommand :: String -> String -> String\n' >> CompilerCommands.hs
echo -e 'cxxCommand inp out = "$(CXX) $(TOCK_CXXFLAGS) -x c++ -o " ++ out ++ " " ++ inp ++ " $(TOCK_CXXLDFLAGS)"\n' >> CompilerCommands.hs echo -e 'cxxCommand inp out = "$(CXX) $(TOCK_CXXFLAGS) -o " ++ out ++ " " ++ inp ++ " $(TOCK_CXXLDFLAGS)"\n' >> CompilerCommands.hs
# Both these results are near-identical. The -g flag to alex tells it to generate # Both these results are near-identical. The -g flag to alex tells it to generate
# a lexer optimised for GHC. The other part of the rule inserts the # a lexer optimised for GHC. The other part of the rule inserts the

View File

@ -8,7 +8,7 @@ checkout:
svn co http://projects.cs.kent.ac.uk/projects/kroc/svn/kroc/trunk/tests/cgtests svn co http://projects.cs.kent.ac.uk/projects/kroc/svn/kroc/trunk/tests/cgtests
cgtests/%: cgtests/%.occ cgtests/%: cgtests/%.occ
./tock -v --backend=cppcsp -o $@ $< ./tock -vk --backend=cppcsp -o $@ $<
run-all: $(addprefix run-,$(tests)) run-all: $(addprefix run-,$(tests))