Embedding version info in executables

What are existing solutions for embedding version info (git revision, build date/time, versions of dependencies) in Haskell programs? Roman

Hi Roman,
I am using this solution:
http://www.hyperedsoftware.com/blog/entries/build-info-gen.html
Cheers,
Thu
2014-07-11 11:26 GMT+02:00 Roman Cheplyaka
What are existing solutions for embedding version info (git revision, build date/time, versions of dependencies) in Haskell programs?
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

We use template haskell to embed the git revision into the executable.
You could probably do the same for the build time. Be aware that if no
sources have changed, cabal won't recompile the module so you'd end up
with the old git revision/build time.
Regards,
Erik
On Fri, Jul 11, 2014 at 11:28 AM, Vo Minh Thu
Hi Roman,
I am using this solution:
http://www.hyperedsoftware.com/blog/entries/build-info-gen.html
Cheers, Thu
2014-07-11 11:26 GMT+02:00 Roman Cheplyaka
: What are existing solutions for embedding version info (git revision, build date/time, versions of dependencies) in Haskell programs?
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I've used the same technique as Erik in the past. More recently, we've had
Jenkins output a minimal Haskell file with a string literal containing the
Jenkins build ID and timestamp.
On Fri, Jul 11, 2014 at 12:38 PM, Erik Hesselink
We use template haskell to embed the git revision into the executable. You could probably do the same for the build time. Be aware that if no sources have changed, cabal won't recompile the module so you'd end up with the old git revision/build time.
Regards,
Erik
On Fri, Jul 11, 2014 at 11:28 AM, Vo Minh Thu
wrote: Hi Roman,
I am using this solution:
http://www.hyperedsoftware.com/blog/entries/build-info-gen.html
Cheers, Thu
2014-07-11 11:26 GMT+02:00 Roman Cheplyaka
: What are existing solutions for embedding version info (git revision, build date/time, versions of dependencies) in Haskell programs?
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

It's possible to use a SimpelUserHook setup type, and then in Setup.hs
add preBuild hook that will generate a file in ./dist/autobuild/, then you
can import this file in Main.hs and use this information.
I have used such Setup.hs (have not reviewed it for years), I think it may
be improved.
```
import Distribution.Simple
import Distribution.Simple.Setup
import Data.Time.LocalTime
import Distribution.PackageDescription (emptyHookedBuildInfo)
import System.Directory (createDirectoryIfMissing, doesDirectoryExist)
import System.Process (readProcess)
main = defaultMainWithHooks (simpleUserHooks{ preBuild=addGitVersion })
addGitVersion _ buildFlags = do
let Flag dir = buildDistPref buildFlags
buildFilePath = dir ++ "/build/autogen"
putStrLn $ "Generating " ++ buildFilePath ++ "..."
createDirectoryIfMissing True buildFilePath
exists <- doesDirectoryExist "git"
desc <- if exists
then readProcess "git" ["describe", "--all", "--long",
"--dirty=-modified"] ""
else return "detached version"
now <- return . show =<< getZonedTime
writeFile (buildFilePath ++ "/Build_hvmm.hs") $ unlines
[ "module Build_hvmm where "
, "gitDescribe::String"
, "gitDescribe = " ++ show desc
, "buildTime:: String"
, "buildTime = " ++ show now
]
return emptyHookedBuildInfo
```
On 11 July 2014 13:26, Roman Cheplyaka
What are existing solutions for embedding version info (git revision, build date/time, versions of dependencies) in Haskell programs?
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Alexander

On 2014-07-11 at 11:46:36 +0200, Alexander V Vershilov wrote:
It's possible to use a SimpelUserHook setup type, and then in Setup.hs add preBuild hook that will generate a file in ./dist/autobuild/, then you can import this file in Main.hs and use this information.
I have used such Setup.hs (have not reviewed it for years), I think it may be improved.
Btw, the main problem with that variant is that it breaks down if you want need `cabal sdist` to work. In order to have `cabal sdist` do something sensible I had to hack up an abomination of a `Setup.hs` which would rewrite the .cabal file and replace the `Setup.hs` by a `build-type: Simple` compliant one during `sdist` creation. It's really a pity Cabal makes this use-case so hard to achieve, as it would just need to provide a hook to determine the version-field dynamically... Cheers, hvr

Yes, this is a problem.
To be honest, the project where I used this approach was never public, and
we didn't use sdist. I think that it's possible to check if you have
`git` executable,
and if the the project is in repository, otherwise there is no sense
in this information,
and information from a cabal file that is given to the hook can be used.
On 11 July 2014 16:09, Herbert Valerio Riedel
On 2014-07-11 at 11:46:36 +0200, Alexander V Vershilov wrote:
It's possible to use a SimpelUserHook setup type, and then in Setup.hs add preBuild hook that will generate a file in ./dist/autobuild/, then you can import this file in Main.hs and use this information.
I have used such Setup.hs (have not reviewed it for years), I think it may be improved.
Btw, the main problem with that variant is that it breaks down if you want need `cabal sdist` to work.
In order to have `cabal sdist` do something sensible I had to hack up an abomination of a `Setup.hs` which would rewrite the .cabal file and replace the `Setup.hs` by a `build-type: Simple` compliant one during `sdist` creation. It's really a pity Cabal makes this use-case so hard to achieve, as it would just need to provide a hook to determine the version-field dynamically...
Cheers, hvr
-- Alexander
participants (6)
-
Alexander V Vershilov
-
Erik Hesselink
-
Herbert Valerio Riedel
-
Michael Snoyman
-
Roman Cheplyaka
-
Vo Minh Thu