summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2018-05-13 18:36:59 -0400
committerBen Gamari <ben@smart-cactus.org>2018-05-13 22:22:43 -0400
commitcf88c2b109a9f36d151af7fa0e542c48c98115fa (patch)
treed5062954d5a55e88a9a8808751dfc8ae8f4fbd9a /libraries
parent21e1a00c0ccf3072ccc04cd1acfc541c141189d2 (diff)
downloadhaskell-cf88c2b109a9f36d151af7fa0e542c48c98115fa.tar.gz
ghc-pkg: Configure handle encodings
This fixes #15021 using a the same approach as was used to fix the issue in ghc (#10762). Test Plan: Validate on Windows as user whose username contains non-ASCII characters Reviewers: simonmar Reviewed By: simonmar Subscribers: lehins, thomie, carter GHC Trac Issues: #15021 Differential Revision: https://phabricator.haskell.org/D4642
Diffstat (limited to 'libraries')
-rw-r--r--libraries/ghc-boot/GHC/HandleEncoding.hs31
-rw-r--r--libraries/ghc-boot/ghc-boot.cabal.in1
2 files changed, 32 insertions, 0 deletions
diff --git a/libraries/ghc-boot/GHC/HandleEncoding.hs b/libraries/ghc-boot/GHC/HandleEncoding.hs
new file mode 100644
index 0000000000..aaa1689782
--- /dev/null
+++ b/libraries/ghc-boot/GHC/HandleEncoding.hs
@@ -0,0 +1,31 @@
+-- | See GHC #10762 and #15021.
+module GHC.HandleEncoding (configureHandleEncoding) where
+
+import GHC.IO.Encoding (textEncodingName)
+import System.Environment
+import System.IO
+
+-- | Handle GHC-specific character encoding flags, allowing us to control how
+-- GHC produces output regardless of OS.
+configureHandleEncoding :: IO ()
+configureHandleEncoding = do
+ env <- getEnvironment
+ case lookup "GHC_CHARENC" env of
+ Just "UTF-8" -> do
+ hSetEncoding stdout utf8
+ hSetEncoding stderr utf8
+ _ -> do
+ -- Avoid GHC erroring out when trying to display unhandled characters
+ hSetTranslit stdout
+ hSetTranslit stderr
+
+-- | Change the character encoding of the given Handle to transliterate
+-- on unsupported characters instead of throwing an exception
+hSetTranslit :: Handle -> IO ()
+hSetTranslit h = do
+ menc <- hGetEncoding h
+ case fmap textEncodingName menc of
+ Just name | '/' `notElem` name -> do
+ enc' <- mkTextEncoding $ name ++ "//TRANSLIT"
+ hSetEncoding h enc'
+ _ -> return ()
diff --git a/libraries/ghc-boot/ghc-boot.cabal.in b/libraries/ghc-boot/ghc-boot.cabal.in
index 5c03a77043..0ca9c1ec73 100644
--- a/libraries/ghc-boot/ghc-boot.cabal.in
+++ b/libraries/ghc-boot/ghc-boot.cabal.in
@@ -40,6 +40,7 @@ Library
GHC.PackageDb
GHC.Serialized
GHC.ForeignSrcLang
+ GHC.HandleEncoding
build-depends: base >= 4.7 && < 4.13,
binary == 0.8.*,