Skip to content

Commit

Permalink
Extend Distribution.Simple.Utils.rewriteFileEx from ASCII to UTF-8 …
Browse files Browse the repository at this point in the history
…encoding

This takes care of knock-off effects of haskell#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.
  • Loading branch information
hvr committed Dec 17, 2018
1 parent 623d868 commit 02fc719
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cabal/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

----

Expand Down
17 changes: 10 additions & 7 deletions Cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 02fc719

Please sign in to comment.