module Build.Drasil.Make.Print where
import Prelude hiding ((<>))
import Text.PrettyPrint (Doc, empty, text, (<>), (<+>), ($+$), ($$), hsep, vcat)
import Build.Drasil.Make.AST (Command(C), CommandOpts(IgnoreReturnCode),
Dependencies, Makefile(M), Rule(R), Target, Type(Abstract))
import Build.Drasil.Make.Helpers (addCommonFeatures, tab)
import Build.Drasil.Make.Import (RuleTransformer, toMake)
import Build.Drasil.Make.MakeString (renderMS)
genMake :: RuleTransformer c => [c] -> Doc
genMake :: [c] -> Doc
genMake = Makefile -> Doc
build (Makefile -> Doc) -> ([c] -> Makefile) -> [c] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [c] -> Makefile
forall c. RuleTransformer c => [c] -> Makefile
toMake
build :: Makefile -> Doc
build :: Makefile -> Doc
build (M rules :: [Rule]
rules) = [Rule] -> Doc -> Doc
addCommonFeatures [Rule]
rules (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
[Doc] -> Doc
vcat ((Rule -> Doc) -> [Rule] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\x :: Rule
x -> Rule -> Doc
printRule Rule
x Doc -> Doc -> Doc
$+$ String -> Doc
text "") [Rule]
rules) Doc -> Doc -> Doc
$$ [Rule] -> Doc
printPhony [Rule]
rules
printRule :: Rule -> Doc
printRule :: Rule -> Doc
printRule (R t :: Target
t d :: Dependencies
d _ c :: [Command]
c) = Target -> Dependencies -> Doc
printTarget Target
t Dependencies
d Doc -> Doc -> Doc
$+$ [Command] -> Doc
printCmds [Command]
c
printPhony :: [Rule] -> Doc
printPhony :: [Rule] -> Doc
printPhony = Doc -> Doc -> Doc
(<+>) (String -> Doc
text ".PHONY:") (Doc -> Doc) -> ([Rule] -> Doc) -> [Rule] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
hsep ([Doc] -> Doc) -> ([Rule] -> [Doc]) -> [Rule] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rule -> Doc) -> [Rule] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(R t :: Target
t _ _ _) -> String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Target -> String
renderMS Target
t) ([Rule] -> [Doc]) -> ([Rule] -> [Rule]) -> [Rule] -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(Rule -> Bool) -> [Rule] -> [Rule]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(R _ _ t :: Type
t _) -> Type
t Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
Abstract)
printTarget :: Target -> Dependencies -> Doc
printTarget :: Target -> Dependencies -> Doc
printTarget nameLb :: Target
nameLb deps :: Dependencies
deps = String -> Doc
text (Target -> String
renderMS Target
nameLb String -> String -> String
forall a. [a] -> [a] -> [a]
++ ":") Doc -> Doc -> Doc
<+> [Doc] -> Doc
hsep ((Target -> Doc) -> Dependencies -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Doc
text (String -> Doc) -> (Target -> String) -> Target -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Target -> String
renderMS) Dependencies
deps)
printCmd :: Command -> Doc
printCmd :: Command -> Doc
printCmd (C c :: Target
c opts :: [CommandOpts]
opts) = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ (if CommandOpts
IgnoreReturnCode CommandOpts -> [CommandOpts] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CommandOpts]
opts then "-" else "") String -> String -> String
forall a. [a] -> [a] -> [a]
++ Target -> String
renderMS Target
c
printCmds :: [Command] -> Doc
printCmds :: [Command] -> Doc
printCmds = (Command -> Doc -> Doc) -> Doc -> [Command] -> Doc
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Doc -> Doc -> Doc
($+$) (Doc -> Doc -> Doc) -> (Command -> Doc) -> Command -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc -> Doc
(<>) Doc
tab (Doc -> Doc) -> (Command -> Doc) -> Command -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Command -> Doc
printCmd) Doc
empty