Skip to content

Commit de79216

Browse files
amesgenmrkkrp
andcommitted
Allow to disable bundling fixities
Should be reverted once the WASM backend supports TH. Co-authored-by: Mark Karpov <mark.karpov@tweag.io>
1 parent 5a2db24 commit de79216

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ormolu.cabal

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ flag dev
3232
default: False
3333
manual: True
3434

35+
flag internal-bundle-fixities
36+
description:
37+
An internal ad-hoc flag that is enabled by default, Ormolu Live disables
38+
it due to missing WASM TH support.
39+
40+
manual: True
41+
3542
library
3643
exposed-modules:
3744
Ormolu
@@ -116,6 +123,9 @@ library
116123
else
117124
ghc-options: -O2 -Wall
118125

126+
if flag(internal-bundle-fixities)
127+
cpp-options: -DBUNDLE_FIXITIES
128+
119129
executable ormolu
120130
main-is: Main.hs
121131
hs-source-dirs: app

src/Ormolu/Fixity.hs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE MultiWayIf #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -29,7 +30,6 @@ where
2930
import qualified Data.Binary as Binary
3031
import qualified Data.Binary.Get as Binary
3132
import qualified Data.ByteString.Lazy as BL
32-
import Data.FileEmbed (embedFile)
3333
import Data.Foldable (foldl')
3434
import Data.List.NonEmpty (NonEmpty ((:|)))
3535
import qualified Data.List.NonEmpty as NE
@@ -42,12 +42,32 @@ import Data.Set (Set)
4242
import qualified Data.Set as Set
4343
import Distribution.Types.PackageName (PackageName, mkPackageName, unPackageName)
4444
import Ormolu.Fixity.Internal
45+
#if BUNDLE_FIXITIES
46+
import Data.FileEmbed (embedFile)
47+
#else
48+
import qualified Data.ByteString.Unsafe as BU
49+
import Foreign.Ptr
50+
import System.Environment (getEnv)
51+
import System.IO.Unsafe (unsafePerformIO)
52+
#endif
4553

4654
packageToOps :: Map PackageName FixityMap
4755
packageToPopularity :: Map PackageName Int
56+
#if BUNDLE_FIXITIES
4857
HackageInfo packageToOps packageToPopularity =
4958
Binary.runGet Binary.get $
5059
BL.fromStrict $(embedFile "extract-hackage-info/hackage-info.bin")
60+
#else
61+
-- The GHC WASM backend does not yet support Template Haskell, so we instead
62+
-- pass in the encoded fixity DB at runtime by storing the pointer and length of
63+
-- the bytes in an environment variable.
64+
HackageInfo packageToOps packageToPopularity = unsafePerformIO $ do
65+
(ptr, len) <- read <$> getEnv "ORMOLU_HACKAGE_INFO"
66+
Binary.runGet Binary.get . BL.fromStrict
67+
<$> BU.unsafePackMallocCStringLen (intPtrToPtr $ IntPtr ptr, len)
68+
{-# NOINLINE packageToOps #-}
69+
{-# NOINLINE packageToPopularity #-}
70+
#endif
5171

5272
-- | List of packages shipped with GHC, for which the download count from
5373
-- Hackage does not reflect their high popularity.

0 commit comments

Comments
 (0)