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 Jan 15, 2019
1 parent 004f385 commit d55ce2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cabal/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Add a `LibraryVisibility` field to `InstalledPackageInfo`
* 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
19 changes: 11 additions & 8 deletions Cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ import Control.Concurrent.MVar
( newEmptyMVar, putMVar, takeMVar )
import Data.Typeable
( cast )
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
import qualified Data.ByteString.Lazy as BS

import System.Directory
( Permissions(executable), getDirectoryContents, getPermissions
Expand Down 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.readFile path
_ <- evaluate (BS.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 d55ce2a

Please sign in to comment.