Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Watermark #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion plover.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -83,6 +83,9 @@ library
, haskell-src-meta
, syb

, bytestring
, SHA

hs-source-dirs: src/
ghc-options:
-- -Wall -Werror
Expand Down
65 changes: 37 additions & 28 deletions src/Language/Plover/ModuleUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -210,34 +213,6 @@ doCodegenAll = do
(pair, imports) <- doCodegen opts (return $ fromRight bs)
liftIO $ writeFiles pair imports opts (Just mod)


splitStatic b | T.static b = Left b
splitStatic b = Right b

importName (T.ImportDef n) = n
both f (a, b) = (f a, f b)

writeFiles :: (String, String) -> [T.DefBinding] -> CompilerOpts -> Maybe String
-> IO (Maybe (FilePath, FilePath))
writeFiles (header, source) imports opts unitName =
let (staticIncludes, normalIncludes) =
both (map $ importName . T.definition) . partitionEithers . map splitStatic $ imports
in
case unitName of
Nothing -> do putStrLn "/* START HEADER */"
putStrLn (wrapHeader normalIncludes "DEFAULT" header)
putStrLn "/* START SOURCE */"
putStrLn source
return Nothing
Just name -> do
let cfile = joinPath [fromMaybe "" (cFilePrefix opts), name ++ ".c"]
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)
return $ Just (hfile, cfile)

makeHeaderName :: String -> String
makeHeaderName unitName = "PLOVER_GENERATED_" ++ clean' unitName
where clean' = map clean''
Expand All @@ -263,3 +238,37 @@ addIncludes moduleNames name body = unlines $
, "" ] ++
["#include \"" ++ mod ++ ".h\"" | mod <- moduleNames] ++
[ "" , body ]

getBinaryHash :: IO String
getBinaryHash = do
f <- getExecutablePath
file <- BS.readFile f
return $ "/* plover binary version: " ++ show (SHA.sha1 file) ++ " */\n"

splitStatic b | T.static b = Left b
splitStatic b = Right b

importName (T.ImportDef n) = n
both f (a, b) = (f a, f b)

writeFiles :: (String, String) -> [T.DefBinding] -> CompilerOpts -> Maybe String
-> IO (Maybe (FilePath, FilePath))
writeFiles (header, source) imports opts unitName =
let (staticIncludes, normalIncludes) =
both (map $ importName . T.definition) . partitionEithers . map splitStatic $ imports
in
case unitName of
Nothing -> do putStrLn "/* START HEADER */"
putStrLn (wrapHeader normalIncludes "DEFAULT" header)
putStrLn "/* START SOURCE */"
putStrLn source
return Nothing
Just name -> do
let cfile = joinPath [fromMaybe "" (cFilePrefix opts), name ++ ".c"]
let hfile = joinPath [fromMaybe "" (hFilePrefix opts), name ++ ".h"]
let addPrefix name = joinPath [fromMaybe "" (libPrefix opts), name]
let includeName = addPrefix name
hashHeader <- getBinaryHash
writeFile hfile $ hashHeader ++ (wrapHeader (map addPrefix normalIncludes) name header)
writeFile cfile $ hashHeader ++ (addIncludes (map addPrefix staticIncludes) includeName source)
return $ Just (hfile, cfile)