Skip to content

Commit

Permalink
Fix haskell#96 Write files with utf8 encoding, not, effectively, char8
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Dec 14, 2024
1 parent 2d6e7a3 commit 0a92dfa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ default_compiler = "cc"
------------------------------------------------------------------------
-- Write the output files.

writeBinaryFile :: FilePath -> String -> IO ()
writeBinaryFile fp str = withBinaryFile fp WriteMode $ \h -> hPutStr h str
writeUtf8File :: FilePath -> String -> IO ()
writeUtf8File fp str = withBinaryFile fp WriteMode $ \h -> do
hSetEncoding h utf8
hPutStr h str

rawSystemL :: FilePath -> FilePath -> String -> Bool -> FilePath -> [String] -> IO ()
rawSystemL outDir outBase action flg prog args = withResponseFile outDir outBase args $ \rspFile -> do
Expand Down
6 changes: 3 additions & 3 deletions src/CrossCodegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ checkValidity input = do
concatMap (uncurry (outValidityCheck (cViaAsm config))) (zip input [0..])
testLog ("checking for compilation errors") $ do
success <- makeTest2 (".c",".o") $ \(cFile,oFile) -> do
liftTestIO $ writeBinaryFile cFile test
liftTestIO $ writeUtf8File cFile test
compiler <- testGetCompiler
runCompiler compiler
(["-S" | cViaAsm config ]++
Expand Down Expand Up @@ -619,7 +619,7 @@ runCompileAsmIntegerTest _ = error "runCompileAsmIntegerTestargument isn't a Spe
runCompileExtract :: String -> String -> TestMonad Integer
runCompileExtract k testStr = do
makeTest3 (".c", ".s", ".txt") $ \(cFile, sFile, stdout) -> do
liftTestIO $ writeBinaryFile cFile testStr
liftTestIO $ writeUtf8File cFile testStr
flags <- testGetFlags
compiler <- testGetCompiler
_ <- runCompiler compiler
Expand All @@ -631,7 +631,7 @@ runCompileExtract k testStr = do
runCompileTest :: String -> TestMonad Bool
runCompileTest testStr = do
makeTest3 (".c", ".o",".txt") $ \(cFile,oFile,stdout) -> do
liftTestIO $ writeBinaryFile cFile testStr
liftTestIO $ writeUtf8File cFile testStr
flags <- testGetFlags
compiler <- testGetCompiler
runCompiler compiler
Expand Down
6 changes: 3 additions & 3 deletions src/DirectCodegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ outputDirect config outName outDir outBase name toks = do
"if","ifdef","ifndef", "elif","else","endif"]) $
die (file ++ ":" ++ show line ++ " directive \"" ++ key ++ "\" is not safe for cross-compilation"))

writeBinaryFile cProgName $
writeUtf8File cProgName $
outTemplateHeaderCProg (cTemplate config)++
concatMap outFlagHeaderCProg flags++
concatMap outHeaderCProg specials++
Expand Down Expand Up @@ -99,7 +99,7 @@ outputDirect config outName outDir outBase name toks = do
rawSystemWithStdOutL outDir outBase ("running " ++ execProgName) beVerbose execProgName [] outName
possiblyRemove progName $ do

when needsH $ writeBinaryFile outHName $
when needsH $ writeUtf8File outHName $
"#ifndef "++includeGuard++"\n" ++
"#define "++includeGuard++"\n" ++
"#include <HsFFI.h>\n" ++
Expand All @@ -111,7 +111,7 @@ outputDirect config outName outDir outBase name toks = do
concatMap outTokenH specials++
"#endif\n"

when needsC $ writeBinaryFile outCName $
when needsC $ writeUtf8File outCName $
"#include \""++outHFile++"\"\n"++
concatMap outTokenC specials
-- NB. outHFile not outHName; works better when processed
Expand Down
2 changes: 1 addition & 1 deletion src/UtilsCodegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ withUtilsObject config outDir outBase f = do
oUtilsName = outDir ++ outBase ++ "_hsc_utils.o"

possiblyRemove cUtilsName $ do
writeBinaryFile cUtilsName $ unlines $
writeUtf8File cUtilsName $ unlines $
-- These header will cause a mismatch with any mingw-w64 header by
-- including system headers before user headers in the hsc file.
-- We *MUST* include user headers *BEFORE* automatic ones. */
Expand Down

0 comments on commit 0a92dfa

Please sign in to comment.