From 02fc719a27d28899d492be7b83dc868335fbb963 Mon Sep 17 00:00:00 2001 From: Herbert Valerio Riedel Date: Mon, 17 Dec 2018 20:23:59 +0100 Subject: [PATCH] Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding This takes care of knock-off effects of #2557 Specifically, the `Paths_*.hs` and `cabal_macros.h` files would result being incorrectly by a `rewriteFileEx` which isn't UTF-8 capable. Now the `cabal_macros.h` file is written out exactly like the `.h` file generated internally by `ghc` is generated; note however that standard CPP doesn't support non-ASCII characters in CPP symbols and will thus not work with a standard CPP preprocessor. --- Cabal/ChangeLog.md | 1 + Cabal/Distribution/Simple/Utils.hs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cabal/ChangeLog.md b/Cabal/ChangeLog.md index 607fb6aa6c2..4c0fe691bdb 100644 --- a/Cabal/ChangeLog.md +++ b/Cabal/ChangeLog.md @@ -9,6 +9,7 @@ * `autoconfUserHooks` now passes `--host=$HOST` when cross-compiling * Fix corrupted config file header for non-ASCII package names ([2557](https://github.com/haskell/cabal/issues/2557)). + * Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding. ---- diff --git a/Cabal/Distribution/Simple/Utils.hs b/Cabal/Distribution/Simple/Utils.hs index 7acd49befa1..0b6e0886410 100644 --- a/Cabal/Distribution/Simple/Utils.hs +++ b/Cabal/Distribution/Simple/Utils.hs @@ -1401,19 +1401,22 @@ rewriteFile = rewriteFileEx normal -- the same as the existing content then leave the file as is so that we do not -- update the file's modification time. -- --- NB: the file is assumed to be ASCII-encoded. +-- NB: Before Cabal-3.0 the file content was assumed to be +-- ASCII-representable. Since Cabal-3.0 the file is assumed to be +-- UTF-8 encoded. rewriteFileEx :: Verbosity -> FilePath -> String -> IO () rewriteFileEx verbosity path newContent = flip catchIO mightNotExist $ do - existingContent <- annotateIO verbosity $ readFile path - _ <- evaluate (length existingContent) - unless (existingContent == newContent) $ + existingContent <- annotateIO verbosity $ BS.Char8.readFile path + _ <- evaluate (BS.Char8.length existingContent) + unless (existingContent == newContent') $ annotateIO verbosity $ - writeFileAtomic path (BS.Char8.pack newContent) + writeFileAtomic path newContent' where + newContent' = toUTF8LBS newContent + mightNotExist e | isDoesNotExistError e - = annotateIO verbosity $ writeFileAtomic path - (BS.Char8.pack newContent) + = annotateIO verbosity $ writeFileAtomic path newContent' | otherwise = ioError e