{-# LANGUAGE TemplateHaskell #-}

-- | This module exports version information about the Futhark
-- compiler.
module Futhark.Version
  ( version,
    versionString,
  )
where

import qualified Data.ByteString.Char8 as BS
import Data.FileEmbed
import Data.Version
import Futhark.Util (trim)
import GitHash
import qualified Paths_futhark

{-# NOINLINE version #-}

-- | The version of Futhark that we are using.  This is equivalent to
-- the version defined in the .cabal file.
version :: Version
version :: Version
version = Version
Paths_futhark.version

{-# NOINLINE versionString #-}

-- | The version of Futhark that we are using, as a 'String'.
versionString :: String
versionString :: String
versionString =
  Version -> String
showVersion Version
version
    String -> String -> String
forall a. [a] -> [a] -> [a]
++ Either String GitInfo -> String
forall a. Either a GitInfo -> String
gitversion Bool
Int
String
[String]
String
-> String
-> Bool
-> String
-> Int
-> [String]
-> String
-> String
-> String
-> GitInfo
GitInfo -> Either String GitInfo
forall a b. b -> Either a b
$$tGitInfoCwdTry
  where
    gitversion :: Either a GitInfo -> String
gitversion (Left a
_) =
      case Maybe String
commitIdFromFile of
        Maybe String
Nothing -> String
""
        Just String
commit -> String
"\ngit: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
commit
    gitversion (Right GitInfo
gi) =
      [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
        [ String
"\n",
          String
"git: ",
          String
branch,
          Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
7 (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ GitInfo -> String
giHash GitInfo
gi,
          String
" (",
          GitInfo -> String
giCommitDate GitInfo
gi,
          String
")",
          String
dirty
        ]
      where
        branch :: String
branch
          | GitInfo -> String
giBranch GitInfo
gi String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"master" = String
""
          | Bool
otherwise = GitInfo -> String
giBranch GitInfo
gi String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" @ "
        dirty :: String
dirty = if GitInfo -> Bool
giDirty GitInfo
gi then String
" [modified]" else String
""

commitIdFromFile :: Maybe String
commitIdFromFile :: Maybe String
commitIdFromFile = String -> String
trim (String -> String)
-> (ByteString -> String) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BS.unpack (ByteString -> String) -> Maybe ByteString -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> $(embedFileIfExists "./commit-id")