How to get haskell compiler version within code without a long list of CPP if/else conditionals?

Viewed 105

Something along the lines of:

import GHC.Version qualified

main :: IO ()
main = print $ GHC.Version.current
-- would print Version 8 10 7

A roundabout way is to execute ghc --version from shell:

import System.Process

main = system "ghc --version"

but this may be incorrect when the running GHC isn't the same as the ghc in PATH.

1 Answers

Yes, in two ways. The first using System.Info.compilerVersion

import System.Info

main :: IO ()
main = print compilerVersion
-- prints "Version {versionBranch = [8,6], versionTags = []}"

Second via the CPP language extension:

{-# LANGUAGE CPP #-}

main :: IO ()
main = print __GLASGOW_HASKELL__
-- prints "806" (of type Integer)

Docs on how that number has been come up with.

Related