Skip to content

Commit

Permalink
Merge pull request #468 from IntersectMBO/amesgen/fix-git-rev-embedding
Browse files Browse the repository at this point in the history
cardano-git-rev: Perform check for embedded git rev at run time
  • Loading branch information
erikd authored Mar 20, 2024
2 parents 30c6ce8 + 792ff3d commit 4b7a923
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions cardano-git-rev/src/Cardano/Git/Rev.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ForeignFunctionInterface #-}

#if __GLASGOW_HASKELL__ >= 900
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
-- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/2288
{-# LANGUAGE TemplateHaskell #-}
#endif

module Cardano.Git.Rev
( gitRev
) where
Expand Down Expand Up @@ -32,24 +38,23 @@ foreign import ccall "&_cardano_git_rev" c_gitrev :: CString
-- This must be a TH splice to ensure the git commit is captured at build time.
-- ie called as `$(gitRev)`.
gitRev :: Q Exp
gitRev
| gitRevEmbed /= zeroRev = textE gitRevEmbed
| otherwise =
#if defined(arm_HOST_ARCH)
-- cross compiling to arm fails; due to a linker bug
textE zeroRev
#else
textE =<< TH.runIO runGitRevParse
#endif
gitRev =
[| if
| gitRevEmbed /= zeroRev -> gitRevEmbed
| otherwise -> $(textE =<< TH.runIO runGitRevParse)
|]

-- Git revision embedded after compilation using
-- Data.FileEmbed.injectWith. If nothing has been injected,
-- this will be filled with 0 characters.
gitRevEmbed :: Text
gitRevEmbed = Text.pack $ drop 28 $ unsafeDupablePerformIO (peekCStringLen utf8 (c_gitrev, 68))

#if !defined(arm_HOST_ARCH)
runGitRevParse :: IO Text
#if defined(arm_HOST_ARCH)
-- cross compiling to arm fails; due to a linker bug
runGitRevParse = pure zeroRev
#else
runGitRevParse = do
(exitCode, output, errorMessage) <- readProcessWithExitCode_ "git" ["rev-parse", "--verify", "HEAD"] ""
case exitCode of
Expand Down

0 comments on commit 4b7a923

Please sign in to comment.