From 4d23db9b9dab42bf6f9555c410fc432cc1bf2d61 Mon Sep 17 00:00:00 2001 From: Scott Kovach Date: Wed, 6 Apr 2016 15:13:17 -0700 Subject: [PATCH 1/3] add executable's hash to generated files --- plover.cabal | 3 +++ src/Language/Plover/ModuleUtils.hs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/plover.cabal b/plover.cabal index 3a0509e..fd488c2 100644 --- a/plover.cabal +++ b/plover.cabal @@ -83,6 +83,9 @@ library , haskell-src-meta , syb + , bytestring + , SHA + hs-source-dirs: src/ ghc-options: -- -Wall -Werror diff --git a/src/Language/Plover/ModuleUtils.hs b/src/Language/Plover/ModuleUtils.hs index 259d6f9..261e552 100644 --- a/src/Language/Plover/ModuleUtils.hs +++ b/src/Language/Plover/ModuleUtils.hs @@ -25,6 +25,9 @@ import Control.Arrow (first, second) import System.Directory import System.FilePath import Debug.Trace +import System.Environment (getExecutablePath) +import qualified Data.ByteString.Lazy as BS +import qualified Data.Digest.Pure.SHA as SHA type Error = String type Action = EitherT Error (StateT ModuleState IO) @@ -210,6 +213,11 @@ doCodegenAll = do (pair, imports) <- doCodegen opts (return $ fromRight bs) liftIO $ writeFiles pair imports opts (Just mod) +getBinaryHash :: IO String +getBinaryHash = do + f <- getExecutablePath + file <- BS.readFile f + return $ "/* " ++ show (SHA.sha1 file) ++ " */\n" splitStatic b | T.static b = Left b splitStatic b = Right b @@ -234,8 +242,9 @@ writeFiles (header, source) imports opts unitName = let hfile = joinPath [fromMaybe "" (hFilePrefix opts), name ++ ".h"] let addPrefix name = joinPath [fromMaybe "" (libPrefix opts), name] let includeName = addPrefix name - writeFile hfile (wrapHeader (map addPrefix normalIncludes) name header) - writeFile cfile (addIncludes (map addPrefix staticIncludes) includeName source) + hashHeader <- getBinaryHash + writeFile hfile $ hashHeader ++ (wrapHeader (map addPrefix normalIncludes) name header) + writeFile cfile $ hashHeader ++ (addIncludes (map addPrefix staticIncludes) includeName source) return $ Just (hfile, cfile) makeHeaderName :: String -> String From 5dc47341e6303438f5b1e13abd1051e12b1f19da Mon Sep 17 00:00:00 2001 From: Scott Kovach Date: Wed, 6 Apr 2016 15:25:36 -0700 Subject: [PATCH 2/3] add some words to header, reorder functions --- src/Language/Plover/ModuleUtils.hs | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Language/Plover/ModuleUtils.hs b/src/Language/Plover/ModuleUtils.hs index 261e552..47555ed 100644 --- a/src/Language/Plover/ModuleUtils.hs +++ b/src/Language/Plover/ModuleUtils.hs @@ -213,11 +213,37 @@ doCodegenAll = do (pair, imports) <- doCodegen opts (return $ fromRight bs) liftIO $ writeFiles pair imports opts (Just mod) +makeHeaderName :: String -> String +makeHeaderName unitName = "PLOVER_GENERATED_" ++ clean' unitName + where clean' = map clean'' + clean'' c | c `elem` (['A'..'Z'] ++ ['a' .. 'z'] ++ ['0'..'9']) = c + | otherwise = '_' + +wrapHeader :: [String] -> String -> String -> String +wrapHeader moduleNames unitName body = unlines $ + [ "#ifndef " ++ headername + , "#define " ++ headername + , "" ] ++ + ["#include \"" ++ mod ++ ".h\"" | mod <- moduleNames] ++ + [ "" + , body + , "" + , "#endif /* " ++ headername ++ " */" + ] + where headername = makeHeaderName unitName + +addIncludes :: [String] -> String -> String -> String +addIncludes moduleNames name body = unlines $ + [ "#include \"" ++ name ++ ".h\"" + , "" ] ++ + ["#include \"" ++ mod ++ ".h\"" | mod <- moduleNames] ++ + [ "" , body ] + getBinaryHash :: IO String getBinaryHash = do f <- getExecutablePath file <- BS.readFile f - return $ "/* " ++ show (SHA.sha1 file) ++ " */\n" + return $ "/* plover binary version: " ++ show (SHA.sha1 file) ++ " */\n" splitStatic b | T.static b = Left b splitStatic b = Right b @@ -246,29 +272,3 @@ writeFiles (header, source) imports opts unitName = writeFile hfile $ hashHeader ++ (wrapHeader (map addPrefix normalIncludes) name header) writeFile cfile $ hashHeader ++ (addIncludes (map addPrefix staticIncludes) includeName source) return $ Just (hfile, cfile) - -makeHeaderName :: String -> String -makeHeaderName unitName = "PLOVER_GENERATED_" ++ clean' unitName - where clean' = map clean'' - clean'' c | c `elem` (['A'..'Z'] ++ ['a' .. 'z'] ++ ['0'..'9']) = c - | otherwise = '_' - -wrapHeader :: [String] -> String -> String -> String -wrapHeader moduleNames unitName body = unlines $ - [ "#ifndef " ++ headername - , "#define " ++ headername - , "" ] ++ - ["#include \"" ++ mod ++ ".h\"" | mod <- moduleNames] ++ - [ "" - , body - , "" - , "#endif /* " ++ headername ++ " */" - ] - where headername = makeHeaderName unitName - -addIncludes :: [String] -> String -> String -> String -addIncludes moduleNames name body = unlines $ - [ "#include \"" ++ name ++ ".h\"" - , "" ] ++ - ["#include \"" ++ mod ++ ".h\"" | mod <- moduleNames] ++ - [ "" , body ] From a4f69d6a91e0449f76b5c70ddb3f930ddc3643b0 Mon Sep 17 00:00:00 2001 From: Scott Kovach Date: Wed, 6 Apr 2016 15:40:33 -0700 Subject: [PATCH 3/3] version increase --- plover.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plover.cabal b/plover.cabal index fd488c2..a1dee9d 100644 --- a/plover.cabal +++ b/plover.cabal @@ -6,7 +6,7 @@ name: plover -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change -version: 0.1.0.0 +version: 0.2.0.0 synopsis: An embedded DSL for compiling linear algebra into C for embedded systems