Ivory.hs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module Gidl.Backend.Ivory where
  2. import Ivory.Artifact
  3. import Ivory.Artifact.Template
  4. import Data.Char (isSpace)
  5. import Data.List (intercalate, nub)
  6. import qualified Paths_gidl as P
  7. import Gidl.Interface
  8. import Gidl.Schema
  9. import Gidl.Backend.Cabal
  10. import Gidl.Backend.Ivory.Types
  11. import Gidl.Backend.Ivory.Schema
  12. ivoryBackend :: [Interface] -> String -> String -> [Artifact]
  13. ivoryBackend iis pkgname namespace_raw =
  14. [ cabalFileArtifact cf
  15. , artifactPath "tests" $ codegenTest namespace
  16. , makefile
  17. ] ++ map (artifactPath "src") sources
  18. where
  19. sources = ivorySources iis namespace
  20. namespace = dotwords namespace_raw
  21. cf = (defaultCabalFile pkgname cabalmods deps) { executables = [ cg_exe ] }
  22. cg_exe = defaultCabalExe (pkgname ++ "-gen") "CodeGen.hs"
  23. (deps ++ (words "ivory-backend-c") ++ [pkgname])
  24. cabalmods = map (filePathToPackage . artifactFileName) sources
  25. deps = words "ivory ivory-stdlib ivory-serialize"
  26. ivorySources :: [Interface] -> [String] -> [Artifact]
  27. ivorySources iis namespace =
  28. tmods ++ concat smods ++ [ typeUmbrella namespace userDefinedTypes
  29. , unpackModule namespace
  30. ]
  31. where
  32. userDefinedTypes = nub [ t | i <- iis, t <- interfaceTypes i, isUserDefined t ]
  33. tmods = [ typeModule (namespace ++ ["Types"]) t
  34. | t <- userDefinedTypes ]
  35. smods = [ [ schemaModule (namespace ++ ["Interface"]) i (producerSchema i)
  36. , schemaModule (namespace ++ ["Interface"]) i (consumerSchema i) ]
  37. | i <- iis ]
  38. dotwords :: String -> [String]
  39. dotwords s = case dropWhile isDot s of
  40. "" -> []
  41. s' -> let (w, s'') = break isDot s' in w : dotwords s''
  42. where
  43. isDot c = (c == '.') || isSpace c
  44. makefile :: Artifact
  45. makefile = artifactCabalFile P.getDataDir "support/ivory/Makefile"
  46. codegenTest :: [String] -> Artifact
  47. codegenTest modulepath =
  48. artifactCabalFileTemplate P.getDataDir fname
  49. [("module_path", intercalate "." modulepath )]
  50. where
  51. fname = "support/ivory/CodeGen.hs.template"
  52. unpackModule :: [String] -> Artifact
  53. unpackModule modulepath =
  54. artifactPath (intercalate "/" modulepath) $
  55. artifactCabalFileTemplate P.getDataDir fname
  56. [("module_path", intercalate "." modulepath )]
  57. where
  58. fname = "support/ivory/Unpack.hs.template"