2018-04-12 05:19:59 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Nix.Cache where
|
|
|
|
|
2019-03-17 22:47:38 +01:00
|
|
|
import qualified Data.ByteString.Lazy as BS
|
2018-04-12 05:19:59 +02:00
|
|
|
import Nix.Expr.Types.Annotated
|
|
|
|
|
2019-10-21 23:44:09 +02:00
|
|
|
#if defined (__linux__)
|
2018-04-12 05:19:59 +02:00
|
|
|
#define USE_COMPACT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_COMPACT
|
|
|
|
import qualified Data.Compact as C
|
|
|
|
import qualified Data.Compact.Serialize as C
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2019-03-17 22:47:38 +01:00
|
|
|
import qualified Codec.Serialise as S
|
2018-04-12 05:19:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
readCache :: FilePath -> IO NExprLoc
|
|
|
|
readCache path = do
|
2018-05-09 01:40:56 +02:00
|
|
|
#if USE_COMPACT
|
2018-04-12 05:19:59 +02:00
|
|
|
eres <- C.unsafeReadCompact path
|
|
|
|
case eres of
|
|
|
|
Left err -> error $ "Error reading cache file: " ++ err
|
|
|
|
Right expr -> return $ C.getCompact expr
|
|
|
|
#else
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2019-03-17 22:47:38 +01:00
|
|
|
eres <- S.deserialiseOrFail <$> BS.readFile path
|
|
|
|
case eres of
|
|
|
|
Left err -> error $ "Error reading cache file: " ++ show err
|
|
|
|
Right expr -> return expr
|
2018-05-09 01:40:56 +02:00
|
|
|
#else
|
|
|
|
error "readCache not implemented for this platform"
|
|
|
|
#endif
|
2018-04-12 05:19:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
writeCache :: FilePath -> NExprLoc -> IO ()
|
|
|
|
writeCache path expr =
|
|
|
|
#ifdef USE_COMPACT
|
|
|
|
C.writeCompact path =<< C.compact expr
|
|
|
|
#else
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2019-03-17 22:47:38 +01:00
|
|
|
BS.writeFile path (S.serialise expr)
|
2018-05-09 01:40:56 +02:00
|
|
|
#else
|
|
|
|
error "writeCache not implemented for this platform"
|
|
|
|
#endif
|
2020-09-14 19:27:51 +02:00
|
|
|
#endif
|