Tower.hs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. , artifactCabalFile P.getDataDir "Makefile.sandbox"
  18. , depsfile
  19. , defaultconf
  20. , artifactPath "tests" (codegenTest iis namespace)
  21. ] ++ map (artifactPath "src") sources
  22. where
  23. namespace = dotwords namespace_raw
  24. sources = isources ++ tsources
  25. ++ [ attrModule (namespace ++ ["Tower"])
  26. (namespace ++ ["Ivory","Types"]) ]
  27. tsources = towerSources iis (namespace ++ ["Tower"])
  28. isources = ivorySources iis (namespace ++ ["Ivory"])
  29. cf = (defaultCabalFile pkgname cabalmods cabalDeps) { executables = [ cg_exe ] }
  30. cabalmods = map (filePathToPackage . artifactFileName) sources
  31. (makeDeps, cabalDeps) = unzip towerDeps
  32. (makeExeDeps, cabalExeDeps) = unzip towerTestDeps
  33. cg_exe = defaultCabalExe (pkgname ++ "-gen") "CodeGen.hs"
  34. (cabalDeps ++ cabalExeDeps ++ [pkgname])
  35. sandwich a b c = a ++ c ++ b
  36. depsfile = artifactString "Makefile.deps" $ unlines $
  37. sandwich ["$(call add-cabal-package-source, \\"] [")"] $
  38. map (sandwich " " " \\") $
  39. makeDeps ++ makeExeDeps
  40. towerDeps :: [(String, String)]
  41. towerDeps =
  42. [ ("$(IVORY_REPO)/ivory", "ivory")
  43. , ("$(IVORY_REPO)/ivory-serialize", "ivory-serialize")
  44. , ("$(IVORY_REPO)/ivory-stdlib", "ivory-stdlib")
  45. , ("$(TOWER_REPO)/tower", "tower")
  46. ]
  47. towerTestDeps :: [(String, String)]
  48. towerTestDeps =
  49. [ ("$(TOWER_REPO)/tower-config", "tower-config")
  50. , ("$(BSP_REPO)/tower-freertos-stm32", "tower-freertos-stm32")
  51. ]
  52. towerSources :: [Interface] -> [String] -> [Artifact]
  53. towerSources iis namespace = towerInterfaces
  54. where
  55. towerInterfaces = concat
  56. [ [ schemaModule ifnamespace i (producerSchema i)
  57. , schemaModule ifnamespace i (consumerSchema i)
  58. , serverModule ifnamespace i
  59. , umbrellaModule ifnamespace i
  60. ]
  61. | i <- iis ]
  62. ifnamespace = namespace ++ ["Interface"]
  63. makefile :: Artifact
  64. makefile = artifactCabalFile P.getDataDir "support/tower/Makefile"
  65. defaultconf :: Artifact
  66. defaultconf = artifactCabalFile P.getDataDir "support/tower/default.conf"
  67. codegenTest :: [Interface] -> [String] -> Artifact
  68. codegenTest iis modulepath =
  69. artifactCabalFileTemplate P.getDataDir fname
  70. [("module_path",intercalate "." modulepath)
  71. ,("imports", intercalate "\n"
  72. [ "import "
  73. ++ interfaceImport (ifModuleName i) "Producer"
  74. ++ "\n"
  75. ++ "import "
  76. ++ interfaceImport (ifModuleName i) "Consumer"
  77. | i <- iis
  78. ])
  79. ,("app_body", intercalate "\n " (concat [ interfaceTest i | i <- iis ]))
  80. ]
  81. where
  82. fname = "support/tower/CodeGen.hs.template"
  83. interfaceImport i j = intercalate "." (modulepath ++ ["Tower", "Interface", i, j])
  84. interfaceTest :: Interface -> [String]
  85. interfaceTest i = [ schemaTest (producerSchema i)
  86. , schemaTest (consumerSchema i)
  87. ]
  88. where
  89. schemaTest :: Schema -> String
  90. schemaTest (Schema _ []) = []
  91. schemaTest (Schema schemaName _)
  92. = (inputFuncName ((ifModuleName i) ++ schemaName))
  93. ++ " (snd c) >>= \\i -> "
  94. ++ (outputFuncName ((ifModuleName i) ++ schemaName))
  95. ++ " i >>= \\(_ :: ChanOutput (Array 80 (Stored Uint8))) -> return ()"
  96. attrModule :: [String] -> [String] -> Artifact
  97. attrModule modulepath typespath =
  98. artifactPath (intercalate "/" modulepath) $
  99. artifactCabalFileTemplate P.getDataDir fname
  100. [("module_path", intercalate "." modulepath )
  101. ,("types_path", intercalate "." typespath)
  102. ]
  103. where
  104. fname = "support/tower/Attr.hs.template"