From b0e8a49b9aa0764a83bfe88afc55fd29eb915f9e Mon Sep 17 00:00:00 2001 From: hallgren Date: Tue, 5 May 2015 13:51:35 +0000 Subject: [PATCH] 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. --- src/compiler/GF/CompileOne.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/compiler/GF/CompileOne.hs b/src/compiler/GF/CompileOne.hs index 8443ec932..318d0d3a3 100644 --- a/src/compiler/GF/CompileOne.hs +++ b/src/compiler/GF/CompileOne.hs @@ -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