module Build.Drasil.Make.Helpers where
import Build.Drasil.Make.AST (Command(C), Rule(R))
import Build.Drasil.Make.MakeString (MakeString(Mc, Mr, Mv), MVar(Free, Implicit, Os))
import Data.List (nubBy)
import Text.PrettyPrint (Doc, empty, nest, text, vcat, ($+$))
($=) :: MVar -> String -> Doc
MVar
a $= :: MVar -> String -> Doc
$= String
b = String -> Doc
text forall a b. (a -> b) -> a -> b
$ MVar -> String
varName MVar
a forall a. [a] -> [a] -> [a]
++ String
"=" forall a. [a] -> [a] -> [a]
++ String
b
win :: MVar -> String
win :: MVar -> String
win (Os String
_ String
w String
_ String
_) = String
w
win MVar
_ = forall a. HasCallStack => String -> a
error String
"Expected Os Variable"
mac :: MVar -> String
mac :: MVar -> String
mac (Os String
_ String
_ String
m String
_) = String
m
mac MVar
_ = forall a. HasCallStack => String -> a
error String
"Expected Os Variable"
linux :: MVar -> String
linux :: MVar -> String
linux (Os String
_ String
_ String
_ String
l) = String
l
linux MVar
_ = forall a. HasCallStack => String -> a
error String
"Expected Os Variable"
defineOsVars :: (MVar -> String) -> [MVar] -> Doc
defineOsVars :: (MVar -> String) -> [MVar] -> Doc
defineOsVars MVar -> String
f [MVar]
m = Doc -> Doc
msIndent forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\MVar
x -> MVar
x MVar -> String -> Doc
$= MVar -> String
f MVar
x) [MVar]
m
osDefinitions :: [MVar] -> Doc
osDefinitions :: [MVar] -> Doc
osDefinitions [] = Doc
empty
osDefinitions [MVar]
m =
String -> Doc
text String
"ifeq \"$(OS)\" \"Windows_NT\"" Doc -> Doc -> Doc
$+$
(MVar -> String) -> [MVar] -> Doc
defineOsVars MVar -> String
win [MVar]
m Doc -> Doc -> Doc
$+$
String -> Doc
text String
"else" Doc -> Doc -> Doc
$+$
Doc -> Doc
msIndent ([Doc] -> Doc
vcat [String -> Doc
text String
"UNAME_S := $(shell uname -s)",
String -> Doc
text String
"ifeq ($(UNAME_S), Linux)",
(MVar -> String) -> [MVar] -> Doc
defineOsVars MVar -> String
linux [MVar]
m,
String -> Doc
text String
"endif",
String -> Doc
text String
"ifeq ($(UNAME_S), Darwin)",
(MVar -> String) -> [MVar] -> Doc
defineOsVars MVar -> String
mac [MVar]
m,
String -> Doc
text String
"endif"]) Doc -> Doc -> Doc
$+$
String -> Doc
text String
"endif" Doc -> Doc -> Doc
$+$
String -> Doc
text String
""
uniqueVars :: [MVar] -> [MVar]
uniqueVars :: [MVar] -> [MVar]
uniqueVars = forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (\MVar
x MVar
y -> MVar -> String
varName MVar
x forall a. Eq a => a -> a -> Bool
== MVar -> String
varName MVar
y Bool -> Bool -> Bool
&& (MVar
x forall a. Eq a => a -> a -> Bool
== MVar
y Bool -> Bool -> Bool
||
forall a. HasCallStack => String -> a
error (String
"Found disparate variable definitions for " forall a. [a] -> [a] -> [a]
++ MVar -> String
varName MVar
x)))
varName :: MVar -> String
varName :: MVar -> String
varName (Free String
s) = String
s
varName (Implicit String
s) = String
s
varName (Os String
s String
_ String
_ String
_) = String
s
extractVars :: Rule -> [MVar]
(R Annotation
_ Target
t Dependencies
d Type
_ [Command]
cs) = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Target -> [MVar]
getVars forall a b. (a -> b) -> a -> b
$ Target
t forall a. a -> [a] -> [a]
: Dependencies
d forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (\(C Target
s [CommandOpts]
_) -> Target
s) [Command]
cs
getVars :: MakeString -> [MVar]
getVars :: Target -> [MVar]
getVars (Mr String
_) = []
getVars (Mv MVar
v) = [MVar
v]
getVars (Mc Target
a Target
b) = Target -> [MVar]
getVars Target
a forall a. [a] -> [a] -> [a]
++ Target -> [MVar]
getVars Target
b
isOsVar :: MVar -> Bool
isOsVar :: MVar -> Bool
isOsVar Os{} = Bool
True
isOsVar MVar
_ = Bool
False
addCommonFeatures :: [Rule] -> Doc -> Doc
addCommonFeatures :: [Rule] -> Doc -> Doc
addCommonFeatures [Rule]
r Doc
m = [MVar] -> Doc
osDefinitions (forall a. (a -> Bool) -> [a] -> [a]
filter MVar -> Bool
isOsVar forall a b. (a -> b) -> a -> b
$ [MVar] -> [MVar]
uniqueVars forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Rule -> [MVar]
extractVars [Rule]
r) Doc -> Doc -> Doc
$+$ Doc
m
tab :: Doc
tab :: Doc
tab = String -> Doc
text String
"\t"
msIndent :: Doc -> Doc
msIndent :: Doc -> Doc
msIndent = Int -> Doc -> Doc
nest Int
4