Tower.hs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module Gidl.Backend.Tower where
  2. import Data.List (intercalate)
  3. import Ivory.Artifact
  4. import Ivory.Artifact.Template
  5. import qualified Paths_gidl as P
  6. import Gidl.Interface
  7. import Gidl.Schema
  8. import Gidl.Backend.Cabal
  9. import Gidl.Backend.Ivory (dotwords, ivorySources)
  10. import Gidl.Backend.Ivory.Schema (ifModuleName)
  11. import Gidl.Backend.Tower.Schema
  12. import Gidl.Backend.Tower.Server
  13. towerBackend :: [Interface] -> String -> String -> [Artifact]
  14. towerBackend iis pkgname namespace_raw =
  15. [ cabalFileArtifact cf
  16. , makefile
  17. , defaultconf
  18. , artifactPath "tests" (codegenTest iis namespace)
  19. ] ++ map (artifactPath "src") sources
  20. where
  21. namespace = dotwords namespace_raw
  22. sources = isources ++ [ attrModule (namespace ++ ["Tower"]) ] ++ tsources
  23. tsources = towerSources iis (namespace ++ ["Tower"])
  24. isources = ivorySources iis (namespace ++ ["Ivory"])
  25. cf = (defaultCabalFile pkgname cabalmods deps) { executables = [ cg_exe ] }
  26. cabalmods = map (filePathToPackage . artifactFileName) sources
  27. deps = words "ivory ivory-stdlib ivory-serialize tower"
  28. cg_exe = defaultCabalExe (pkgname ++ "-gen") "CodeGen.hs"
  29. (deps ++ (words "tower-config tower-freertos-stm32") ++ [pkgname])
  30. towerSources :: [Interface] -> [String] -> [Artifact]
  31. towerSources iis namespace = towerInterfaces
  32. where
  33. towerInterfaces = concat
  34. [ [ schemaModule ifnamespace i (producerSchema i)
  35. , schemaModule ifnamespace i (consumerSchema i)
  36. , serverModule ifnamespace i
  37. , umbrellaModule ifnamespace i
  38. ]
  39. | i <- iis ]
  40. ifnamespace = namespace ++ ["Interface"]
  41. makefile :: Artifact
  42. makefile = artifactCabalFile P.getDataDir "support/tower/Makefile"
  43. defaultconf :: Artifact
  44. defaultconf = artifactCabalFile P.getDataDir "support/tower/default.conf"
  45. codegenTest :: [Interface] -> [String] -> Artifact
  46. codegenTest iis modulepath =
  47. artifactCabalFileTemplate P.getDataDir fname
  48. [("module_path",intercalate "." modulepath)
  49. ,("imports", intercalate "\n"
  50. [ "import "
  51. ++ interfaceImport (ifModuleName i) "Producer"
  52. ++ "\n"
  53. ++ "import "
  54. ++ interfaceImport (ifModuleName i) "Consumer"
  55. | i <- iis
  56. ])
  57. ,("app_body", intercalate "\n " (concat [ interfaceTest i | i <- iis ]))
  58. ]
  59. where
  60. fname = "support/tower/CodeGen.hs.template"
  61. interfaceImport i j = intercalate "." (modulepath ++ ["Tower", "Interface", i, j])
  62. interfaceTest :: Interface -> [String]
  63. interfaceTest i = [ schemaTest (producerSchema i)
  64. , schemaTest (consumerSchema i)
  65. ]
  66. where
  67. schemaTest :: Schema -> String
  68. schemaTest (Schema _ []) = []
  69. schemaTest (Schema schemaName _)
  70. = (inputFuncName ((ifModuleName i) ++ schemaName))
  71. ++ " (snd c) >>= \\i -> "
  72. ++ (outputFuncName ((ifModuleName i) ++ schemaName))
  73. ++ " i >>= \\(_ :: ChanOutput (Array 80 (Stored Uint8))) -> return ()"
  74. attrModule :: [String] -> Artifact
  75. attrModule modulepath =
  76. artifactPath (intercalate "/" modulepath) $
  77. artifactCabalFileTemplate P.getDataDir fname
  78. [("module_path", intercalate "." modulepath )]
  79. where
  80. fname = "support/tower/Attr.hs.template"