|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | +{-# LANGUAGE CPP #-} |
1 | 3 | -- |
|
2 | 4 |
|
3 | 5 | module Main (main) where
|
4 | 6 |
|
| 7 | +import qualified Control.Monad as Monad |
5 | 8 | import qualified Data.ByteString.Lazy as BL
|
6 | 9 | import qualified Data.ByteString.Lazy.UTF8 as BL8
|
| 10 | +import Data.Maybe (isNothing) |
| 11 | +import qualified Data.List as L |
| 12 | +import Paths_ouroboros_consensus_cardano |
| 13 | +import qualified System.Directory as D |
| 14 | +import qualified System.Environment as E |
| 15 | +import System.Exit (exitFailure) |
| 16 | +import qualified System.FilePath as F |
| 17 | +import qualified System.Process.ByteString.Lazy as P |
| 18 | +import qualified Test.Cardano.Chain.Binary.Cddl as Byron |
7 | 19 | import qualified Test.Cardano.Ledger.Allegra.Binary.Cddl as Allegra
|
8 | 20 | import qualified Test.Cardano.Ledger.Alonzo.Binary.Cddl as Alonzo
|
9 | 21 | import qualified Test.Cardano.Ledger.Babbage.Binary.Cddl as Babbage
|
10 |
| -import qualified Paths_cardano_ledger_byron as Byron |
11 | 22 | import qualified Test.Cardano.Ledger.Conway.Binary.Cddl as Conway
|
12 | 23 | import qualified Test.Cardano.Ledger.Mary.Binary.Cddl as Mary
|
13 | 24 | import qualified Test.Cardano.Ledger.Shelley.Binary.Cddl as Shelley
|
14 |
| -import qualified Data.List as L |
15 |
| -import qualified System.Process.ByteString.Lazy as P |
16 |
| -import qualified System.Environment as E |
17 |
| -import qualified System.FilePath as F |
18 | 25 |
|
19 | 26 | main :: IO ()
|
20 | 27 | main = do
|
21 |
| - byron <- forwardize . (:[]) <$> Byron.getDataFileName "cddl-spec/byron.cddl" |
22 |
| - shelley <- forwardize <$> Shelley.readShelleyCddlFileNames |
23 |
| - allegra <- forwardize <$> Allegra.readAllegraCddlFileNames |
24 |
| - mary <- forwardize <$> Mary.readMaryCddlFileNames |
25 |
| - alonzo <- forwardize <$> Alonzo.readAlonzoCddlFileNames |
26 |
| - babbage <- forwardize <$> Babbage.readBabbageCddlFileNames |
27 |
| - conway <- forwardize <$> Conway.readConwayCddlFileNames |
28 |
| - |
29 |
| - let include_path = mconcat $ L.intersperse ":" $ [byron, shelley, allegra, mary, alonzo, babbage, conway] |
| 28 | + probeTools |
| 29 | + setupEnv |
| 30 | + -- For now I just print this. |
| 31 | + print =<< getCDDLs |
| 32 | + |
| 33 | +setupEnv :: IO () |
| 34 | +setupEnv = do |
| 35 | + byron <- map takePath <$> Byron.readByronCddlFileNames |
| 36 | + shelley <- map takePath <$> Shelley.readShelleyCddlFileNames |
| 37 | + allegra <- map takePath <$> Allegra.readAllegraCddlFileNames |
| 38 | + mary <- map takePath <$> Mary.readMaryCddlFileNames |
| 39 | + alonzo <- map takePath <$> Alonzo.readAlonzoCddlFileNames |
| 40 | + babbage <- map takePath <$> Babbage.readBabbageCddlFileNames |
| 41 | + conway <- map takePath <$> Conway.readConwayCddlFileNames |
| 42 | + |
| 43 | + localDataDir <- takePath <$> getDataDir |
| 44 | + let local_paths = map (localDataDir F.</>) [ |
| 45 | + "cddl" |
| 46 | + , "cddl/disk" |
| 47 | + , "cddl/disk/snapshot" |
| 48 | + , "cddl/node-to-client/localstatequery/byron" |
| 49 | + , "cddl/node-to-client/localstatequery/consensus" |
| 50 | + , "cddl/node-to-client/localstatequery/shelley" |
| 51 | + , "cddl/node-to-client/txmonitor" |
| 52 | + ] |
| 53 | + |
| 54 | + include_path = |
| 55 | + mconcat |
| 56 | + $ L.intersperse ":" |
| 57 | + $ map (mconcat . L.intersperse ":") [byron, shelley, allegra, mary, alonzo, babbage, conway] <> local_paths |
| 58 | + |
30 | 59 | E.setEnv "CDDL_INCLUDE_PATH" (include_path <> ":")
|
31 | 60 |
|
32 |
| - putStrLn . BL8.toString =<< cddlc "ouroboros-consensus-cardano/cddl/disk/block.cddl" |
| 61 | +newtype CDDLSpec = CDDLSpec BL.ByteString deriving Show |
| 62 | + |
| 63 | +data CDDLs = CDDLs { |
| 64 | + diskBlockCDDL :: CDDLSpec |
| 65 | + , diskSnapshotCDDL :: CDDLSpec |
| 66 | + |
| 67 | + , ntnBlockFetchBlockCDDL :: CDDLSpec |
| 68 | + , ntnBlockFetchPointCDDL :: CDDLSpec |
| 69 | + |
| 70 | + , ntnChainSyncHeaderCDDL :: CDDLSpec |
| 71 | + , ntnChainSyncPointCDDL :: CDDLSpec |
| 72 | + , ntnChainSyncTipCDDL :: CDDLSpec |
| 73 | + |
| 74 | + , ntnTxSubmissionTicketNoCDDL :: CDDLSpec |
| 75 | + , ntnTxSubmissionTxCDDL :: CDDLSpec |
| 76 | + , ntnTxSubmissionTxIdCDDL :: CDDLSpec |
| 77 | + |
| 78 | + , ntcLocalStateQueryQueryCDDL :: CDDLSpec |
| 79 | + , ntcLocalStateQueryResultCDDL :: CDDLSpec |
| 80 | + |
| 81 | + , ntcTxMonitorTxCDDL :: CDDLSpec |
| 82 | + , ntcTxMonitorTxIdCDDL :: CDDLSpec |
| 83 | + , ntcTxMonitorSlotNoCDDL :: CDDLSpec |
| 84 | + } deriving Show |
| 85 | + |
| 86 | +getCDDLs :: IO CDDLs |
| 87 | +getCDDLs = CDDLs |
| 88 | + -- Disk |
| 89 | + <$> cddlc "cddl/disk/block.cddl" |
| 90 | + <*> cddlc "cddl/disk/snapshot.cddl" |
| 91 | + |
| 92 | + -- Node to node |
| 93 | + -- -- BlockFetch |
| 94 | + <*> cddlc "cddl/node-to-node/blockfetch/block.cddl" |
| 95 | + <*> cddlc "cddl/node-to-node/blockfetch/point.cddl" |
| 96 | + |
| 97 | + -- -- ChainSync |
| 98 | + <*> cddlc "cddl/node-to-node/chainsync/header.cddl" |
| 99 | + <*> cddlc "cddl/node-to-node/chainsync/point.cddl" |
| 100 | + <*> cddlc "cddl/node-to-node/chainsync/tip.cddl" |
| 101 | + |
| 102 | + -- -- TxSubmission2 |
| 103 | + <*> cddlc "cddl/node-to-node/txsubmission2/ticketno.cddl" |
| 104 | + <*> cddlc "cddl/node-to-node/txsubmission2/tx.cddl" |
| 105 | + <*> cddlc "cddl/node-to-node/txsubmission2/txid.cddl" |
| 106 | + |
| 107 | + -- Node to client |
| 108 | + -- -- LocalStateQuery |
| 109 | + <*> cddlc "cddl/node-to-client/localstatequery/query.cddl" |
| 110 | + <*> cddlc "cddl/node-to-client/localstatequery/result.cddl" |
| 111 | + |
| 112 | + -- -- TxMonitor |
| 113 | + <*> cddlc "cddl/node-to-client/txmonitor/tx.cddl" |
| 114 | + <*> cddlc "cddl/node-to-client/txmonitor/txid.cddl" |
| 115 | + <*> cddlc "cddl/node-to-client/txmonitor/slotno.cddl" |
| 116 | + |
| 117 | +cddlc :: FilePath -> IO CDDLSpec |
| 118 | +cddlc dataFile = do |
| 119 | + putStrLn $ "Generating: " <> dataFile |
| 120 | + path <- getDataFileName dataFile |
| 121 | + (_, cddl, err) <- |
| 122 | +#ifdef POSIX |
| 123 | + P.readProcessWithExitCode "cddlc" ["-u", "-2", "-t", "cddl", path] mempty |
| 124 | +#else |
| 125 | + -- we cannot call @cddlc@ directly because it is not an executable in |
| 126 | + -- Haskell eyes, but we can call @ruby@ and pass the @cddlc@ script path as |
| 127 | + -- an argument |
| 128 | + do |
| 129 | + prefix <- E.getEnv "MSYSTEM_PREFIX" |
| 130 | + P.readProcessWithExitCode "ruby" [prefix F.</> "bin/cddlc", "-u", "-2", "-t", "cddl", path] mempty |
| 131 | +#endif |
| 132 | + Monad.unless (BL.null err) $ red $ BL8.toString err |
| 133 | + return $ CDDLSpec cddl |
33 | 134 | where
|
34 |
| - forwardize [x] = |
35 |
| - let f = [ if c /= '\\' then c else '/' | c <- F.takeDirectory x ] |
36 |
| - in if "C:" `L.isPrefixOf` f |
37 |
| - then drop 2 f |
38 |
| - else f |
39 |
| - forwardize x = error $ "match: " <> show x |
40 |
| - |
41 |
| - |
42 |
| --- | A 'CDDL' specifcation for a protocol 'ps'. |
43 |
| --- |
44 |
| --- newtype CDDLSpec ps = CDDLSpec BL.ByteString |
45 |
| - |
46 |
| -cddlc :: FilePath -> IO BL.ByteString |
47 |
| -cddlc path = do |
48 |
| - (_, cddl, err) <- P.readProcessWithExitCode "ruby" ["C:/msys64/clang64/bin/cddlc", "-u", "-2", "-t", "cddl", path] mempty |
49 |
| - putStrLn $ BL8.toString err |
50 |
| - return cddl |
| 135 | + red s = putStrLn $ "\ESC[31m" <> s <> "\ESC[0m" |
| 136 | + |
| 137 | +takePath :: FilePath -> FilePath |
| 138 | +takePath x = |
| 139 | +#ifdef POSIX |
| 140 | + F.takeDirectory x |
| 141 | +#else |
| 142 | + -- @cddlc@ is not capable of using backlashes |
| 143 | + -- |
| 144 | + -- @cddlc@ mixes @C:@ with the separator in @CDDL_INCLUDE_PATH@, and it |
| 145 | + -- doesn't understand @;@ as a separator. It works if we remove @C:@ and we |
| 146 | + -- are running in the same drive as the cddl files. |
| 147 | + let f = [ if c /= '\\' then c else '/' | c <- F.takeDirectory x ] |
| 148 | + in if "C:" `L.isPrefixOf` f |
| 149 | + then drop 2 f |
| 150 | + else f |
| 151 | +#endif |
| 152 | + |
| 153 | +probeTools :: IO () |
| 154 | +probeTools = do |
| 155 | + putStrLn "Probing tools:" |
| 156 | +#ifdef POSIX |
| 157 | + posixProbeTool "cddl" "install the `cddl` ruby gem" |
| 158 | + posixProbeTool "cddlc" "install the `cddlc` ruby gem" |
| 159 | + where |
| 160 | + posixProbeTool :: String -> Sring -> IO () |
| 161 | + posixProbeTool tool suggestion = do |
| 162 | + putStr $ "- " <> tool <> " " |
| 163 | + exe <- D.findExecutable tool |
| 164 | + if isNothing exe |
| 165 | + then do |
| 166 | + putStrLn "not found!" |
| 167 | + putStrLn $ "Please " <> suggestion |
| 168 | + exitFailure |
| 169 | + else |
| 170 | + putStrLn "found" |
| 171 | +#else |
| 172 | + -- On Windows, the cddl and cddlc files are POSIX scripts and therefore not |
| 173 | + -- recognized as executables by @findExecutable@, so we need to do some dirty |
| 174 | + -- tricks here. We check that ruby executable exists and then that there are |
| 175 | + -- cddl and cddlc files in the binary folder of the MSYS2 installation. |
| 176 | + putStr "- ruby " |
| 177 | + rubyExe <- D.findExecutable "ruby" |
| 178 | + if (isNothing rubyExe) |
| 179 | + then do |
| 180 | + putStrLn "not found!\nPlease install ruby and the `cddl` and `cddlc` gems" |
| 181 | + exitFailure |
| 182 | + else |
| 183 | + putStrLn "found" |
| 184 | + |
| 185 | + putStr "- cddl " |
| 186 | + cddlExe <- D.doesFileExist . (F.</> "bin/cddlc") =<< E.getEnv "MSYSTEM_PREFIX" |
| 187 | + if cddlExe |
| 188 | + then putStrLn "found" |
| 189 | + else do |
| 190 | + putStrLn "not found!\nPlease install the `cddl` ruby gem" |
| 191 | + exitFailure |
| 192 | + |
| 193 | + putStr "- cddlc " |
| 194 | + cddlcExe <- D.doesFileExist . (F.</> "bin/cddlc") =<< E.getEnv "MSYSTEM_PREFIX" |
| 195 | + if cddlcExe |
| 196 | + then putStrLn "found" |
| 197 | + else do |
| 198 | + putStrLn "not found!\nPlease install the `cddlc` ruby gem" |
| 199 | + exitFailure |
| 200 | + pure () |
| 201 | +#endif |
0 commit comments