{-# LANGUAGE OverloadedStrings #-}

module BNFC.Backend.Txt2Tags.Makefile where

import BNFC.Backend.CommonInterface.Backend

import BNFC.Backend.Txt2Tags.Options
import BNFC.Backend.Txt2Tags.State

import BNFC.CF

import BNFC.Options.GlobalOptions

import BNFC.Prelude

import Control.Monad.State

import Data.String (fromString)

import System.FilePath (takeBaseName)

import Prettyprinter
import Prettyprinter.Render.String

txt2tagsmakefile :: LBNF -> State Txt2TagsBackendState Result
txt2tagsmakefile :: LBNF -> State Txt2TagsBackendState Result
txt2tagsmakefile LBNF
_ = do
  Txt2TagsBackendState
st <- StateT Txt2TagsBackendState Identity Txt2TagsBackendState
forall s (m :: * -> *). MonadState s m => m s
get
  let
    cfName :: String
cfName = String -> String
takeBaseName (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ GlobalOptions -> String
optInput (GlobalOptions -> String) -> GlobalOptions -> String
forall a b. (a -> b) -> a -> b
$ Txt2TagsBackendState -> GlobalOptions
globalOpt Txt2TagsBackendState
st
    t :: String
t = Txt2TagsBackendOptions -> String
target (Txt2TagsBackendOptions -> String)
-> Txt2TagsBackendOptions -> String
forall a b. (a -> b) -> a -> b
$ Txt2TagsBackendState -> Txt2TagsBackendOptions
txtOpts Txt2TagsBackendState
st
  Result -> State Txt2TagsBackendState Result
forall (m :: * -> *) a. Monad m => a -> m a
return [(String
"Makefile", String -> String -> String
makefileString String
cfName String
t)]

makefileString :: String -> String -> String
makefileString :: String -> String -> String
makefileString String
cfName String
target =
  SimpleDocStream () -> String
forall ann. SimpleDocStream ann -> String
renderString (SimpleDocStream () -> String)
-> (Doc () -> SimpleDocStream ()) -> Doc () -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc () -> SimpleDocStream ()
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart LayoutOptions
defaultLayoutOptions (Doc () -> String) -> Doc () -> String
forall a b. (a -> b) -> a -> b
$
    String -> String -> Doc ()
makefileDoc String
cfName String
target

makefileDoc :: String -> String -> Doc ()
makefileDoc :: String -> String -> Doc ()
makefileDoc String
cfName String
t = [Doc ()] -> Doc ()
forall ann. [Doc ann] -> Doc ann
vsep
  [ Doc ()
"TARGET=" Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
target
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"$(TARGET)" Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
colon Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+>
    Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"t2t"
  , Doc ()
"\ttxt2tags -t" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
"$(TARGET)" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"t2t"
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
".PHONY: clean"
  , Doc ()
"clean:"
  , Doc ()
"\t-rm" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"$(TARGET)"
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
"cleanall : clean"
  , Doc ()
"\t-rm" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
"Makefile" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"t2t"
  ]
  where
    grammar :: Doc ()
grammar = String -> Doc ()
forall a. IsString a => String -> a
fromString String
cfName
    target :: Doc ()
target  = String -> Doc ()
forall a. IsString a => String -> a
fromString String
t