From 4d5e9f147931d442e8f3551175067b0f2cdea3e3 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Wed, 13 Mar 2024 12:05:49 +1100 Subject: [PATCH] Fix gitRev TH splice Previously we had a TH quotation calling a TH splce, but that meant that the TH splice was, under certain curcumstances, being cached in the object file in `~/.cabal/store`. Solution is to pull the TH splice into the TH quotation. This should force the running of the `git rev-parse` command whenever the TH quotation is evaluated. --- cardano-git-rev/cardano-git-rev.cabal | 1 - cardano-git-rev/src/Cardano/Git/Rev.hs | 39 +++++++++++++++---- cardano-git-rev/src/Cardano/Git/RevFromGit.hs | 33 ---------------- 3 files changed, 31 insertions(+), 42 deletions(-) delete mode 100644 cardano-git-rev/src/Cardano/Git/RevFromGit.hs diff --git a/cardano-git-rev/cardano-git-rev.cabal b/cardano-git-rev/cardano-git-rev.cabal index 5f96a7e31..273402fbb 100644 --- a/cardano-git-rev/cardano-git-rev.cabal +++ b/cardano-git-rev/cardano-git-rev.cabal @@ -34,7 +34,6 @@ library c-sources: cbits/rev.c exposed-modules: Cardano.Git.Rev - Cardano.Git.RevFromGit build-depends: process , template-haskell diff --git a/cardano-git-rev/src/Cardano/Git/Rev.hs b/cardano-git-rev/src/Cardano/Git/Rev.hs index 3e55c159e..1b0dde4e6 100644 --- a/cardano-git-rev/src/Cardano/Git/Rev.hs +++ b/cardano-git-rev/src/Cardano/Git/Rev.hs @@ -9,17 +9,22 @@ module Cardano.Git.Rev ) where import Data.Text (Text) -import qualified Data.Text as T +import qualified Data.Text as Text -#if !defined(arm_HOST_ARCH) -import Cardano.Git.RevFromGit (gitRevFromGit) -#endif import Foreign.C.String (CString) import GHC.Foreign (peekCStringLen) import Language.Haskell.TH (Exp, Q) import System.IO (utf8) import System.IO.Unsafe (unsafeDupablePerformIO) +#if !defined(arm_HOST_ARCH) +import Control.Exception (catch) +import System.Exit (ExitCode (..)) +import qualified System.IO as IO +import System.IO.Error (isDoesNotExistError) +import System.Process (readProcessWithExitCode) +#endif + 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. @@ -27,8 +32,8 @@ foreign import ccall "&_cardano_git_rev" c_gitrev :: CString gitRev :: Q Exp gitRev = [| if - | gitRevEmbed /= zeroRev -> gitRevEmbed - | T.null fromGit -> zeroRev + | gitRevEmbed /= zeroRev -> fromGit + | Text.null fromGit -> zeroRev | otherwise -> fromGit |] @@ -36,7 +41,7 @@ gitRev = -- Data.FileEmbed.injectWith. If nothing has been injected, -- this will be filled with 0 characters. gitRevEmbed :: Text -gitRevEmbed = T.pack $ drop 28 $ unsafeDupablePerformIO (peekCStringLen utf8 (c_gitrev, 68)) +gitRevEmbed = Text.pack $ drop 28 $ unsafeDupablePerformIO (peekCStringLen utf8 (c_gitrev, 68)) -- Git revision found during compilation by running git. If -- git could not be run, then this will be empty. @@ -45,8 +50,26 @@ fromGit :: Text -- cross compiling to arm fails; due to a linker bug fromGit = "" #else -fromGit = T.strip (T.pack $(gitRevFromGit)) +fromGit = Text.strip $ Text.pack (unsafeDupablePerformIO runGitRevParse) #endif zeroRev :: Text zeroRev = "0000000000000000000000000000000000000000" + +#if !defined(arm_HOST_ARCH) +runGitRevParse :: IO String +runGitRevParse = do + (exitCode, output, errorMessage) <- readProcessWithExitCode_ "git" ["rev-parse", "--verify", "HEAD"] "" + case exitCode of + ExitSuccess -> pure output + ExitFailure _ -> do + IO.hPutStrLn IO.stderr $ "WARNING: " ++ errorMessage + pure "" + where + readProcessWithExitCode_ :: FilePath -> [String] -> String -> IO (ExitCode, String, String) + readProcessWithExitCode_ cmd args input = + catch (readProcessWithExitCode cmd args input) $ \e -> + if isDoesNotExistError e + then pure (ExitFailure 127, "", show e) + else pure (ExitFailure 999, "", show e) +#endif diff --git a/cardano-git-rev/src/Cardano/Git/RevFromGit.hs b/cardano-git-rev/src/Cardano/Git/RevFromGit.hs deleted file mode 100644 index b60314ff2..000000000 --- a/cardano-git-rev/src/Cardano/Git/RevFromGit.hs +++ /dev/null @@ -1,33 +0,0 @@ -module Cardano.Git.RevFromGit - ( gitRevFromGit - ) where - -import Control.Exception (catch) -import System.Exit (ExitCode (..)) -import qualified System.IO as IO -import System.IO.Error (isDoesNotExistError) -import System.Process (readProcessWithExitCode) - -import qualified Language.Haskell.TH as TH - --- | Git revision found by running git rev-parse. If git could not be --- executed, then this will be an empty string. -gitRevFromGit :: TH.Q TH.Exp -gitRevFromGit = - TH.LitE . TH.StringL <$> TH.runIO runGitRevParse - where - runGitRevParse :: IO String - runGitRevParse = do - (exitCode, output, errorMessage) <- readProcessWithExitCode_ "git" ["rev-parse", "--verify", "HEAD"] "" - case exitCode of - ExitSuccess -> pure output - ExitFailure _ -> do - IO.hPutStrLn IO.stderr $ "WARNING: " ++ errorMessage - pure "" - - readProcessWithExitCode_ :: FilePath -> [String] -> String -> IO (ExitCode, String, String) - readProcessWithExitCode_ cmd args input = - catch (readProcessWithExitCode cmd args input) $ \e -> - if isDoesNotExistError e - then return (ExitFailure 127, "", show e) - else return (ExitFailure 999, "", show e)