GF.CompileOne: use random numbers to avoid temporary file name clashes

When using make -j to compile examples/app or examples/phrasebook, since
the dependencies are not completely specified in the Makefiles, it can
happen that the same file is compiled at the same by more than one process,
resulting in an error when they try to write the same .gfo.tmp file. Adding a
random number to the temporary file name avoids this problem.
This commit is contained in:
hallgren
2015-05-05 13:51:35 +00:00
parent 3649412ce2
commit b0e8a49b9a

View File

@@ -25,6 +25,7 @@ import GF.Data.Operations(ErrorMonad,liftErr,(+++),done)
import GF.System.Directory(doesFileExist,getCurrentDirectory,renameFile)
import System.FilePath(makeRelative)
import System.Random(randomIO)
import qualified Data.Map as Map
import GF.Text.Pretty(render,(<+>),($$)) --Doc,
import GF.System.Console(TermColors(..),getTermColors)
@@ -142,11 +143,12 @@ compileSourceModule opts cwd mb_gfFile gr =
--writeGFO :: Options -> InitPath -> FilePath -> SourceModule -> IOE ()
writeGFO opts cwd file mo =
putPointE Normal opts (" write file" +++ rfile) $
do encodeModule tmp mo2
do n <- liftIO randomIO --avoid name clashes when compiling with 'make -j'
let tmp = file++".tmp" ++show (n::Int)
encodeModule tmp mo2
renameFile tmp file
where
rfile = makeRelative cwd file
tmp = file++".tmp"
mo2 = (m,mi{jments=Map.filter notAnyInd (jments mi)})
(m,mi) = subexpModule mo