1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
--------------------------------------------------------------------------
--- Main module of a tool to generate a makefile for a Curry application.
---
--- @author Michael Hanus
--- @version December 2018
--------------------------------------------------------------------------

module Main where

import Distribution     ( installDir )
import GetOpt
import ReadNumeric      (readNat)
import System           ( exitWith, getArgs )

import System.CurryPath ( stripCurrySuffix )

import GenerateMakeFile

main :: IO ()
main = do
  argv <- getArgs
  let (funopts, args, opterrors) = getOpt Permute options argv
      opts = foldl (flip id) defaultOptions funopts
  unless (null opterrors || length args /= 1)
         (putStr (unlines opterrors) >> putStrLn usageText >> exitWith 1)
  when (optVerb opts > 0) $ putStr banner
  when (null args || optHelp opts) (putStrLn usageText >> exitWith 1)
  generateMakeForApplication (optVerb opts) (unwords argv)
    (stripCurrySuffix (head args)) (optOutput opts)
    (if null (optRoot opts) then installDir else optRoot opts)
    (optTool opts)

version :: String
version = "Version of 09/05/2017"

-- Banner of this tool:
banner :: String
 = unlines [bannerLine,bannerText,bannerLine]
 where
  bannerText =
    "curry-genmake: Generate a makefile for a Curry application ("++version++")"
  bannerLine = take (length bannerText) (repeat '-')

-- Help text
usageText :: String
usageText =
  usageInfo "Usage: curry-genmake [options] <main module name>\n" options

--------------------------------------------------------------------------
-- Processing options:

-- Representation of command line options
data Options = Options
  { optHelp     :: Bool
  , optVerb     :: Int
  , optOutput   :: String   -- output file (or empty)
  , optRoot     :: String   -- root directory of the Curry system
  , optTool     :: String   -- name of the tool binary (or empty)
  }

-- Default command line options.
defaultOptions :: Options
defaultOptions  = Options
  { optHelp     = False
  , optVerb     = 1
  , optOutput   = ""
  , optRoot     = ""
  , optTool     = ""
  }

-- Definition of actual command line options.
options :: [OptDescr (Options -> Options)]
options =
  [ Option "h?" ["help"]  (NoArg (\opts -> opts { optHelp = True }))
           "print help and exit"
  , Option "q" ["quiet"] (NoArg (\opts -> opts { optVerb = 0 }))
           "run quietly (no output, only exit code)"
  , Option "v" ["verbosity"]
            (OptArg (maybe (checkVerb 2) (safeReadNat checkVerb)) "<n>")
            "verbosity level:\n0: quiet (same as `-q')\n1: show progress (default)\n2: show generated output (same as `-v')"
  , Option "o" ["output"]
           (ReqArg (\n opts -> opts { optOutput = n }) "<o>")
           "name of output file (e.g., Makefile)"
  , Option "r" ["root"]
           (ReqArg (\n opts -> opts { optRoot = n }) "<r>")
           "root directory of the Curry system in Makefile"
  , Option "t" ["tool"]
           (ReqArg (\n opts -> opts { optTool = n }) "<t>")
           "name of the tool binary"
  ]
 where
  safeReadNat opttrans s opts =
   let numError = error "Illegal number argument (try `-h' for help)" in
    maybe numError
          (\ (n,rs) -> if null rs then opttrans n opts else numError)
          (readNat s)

  checkVerb n opts = if n>=0 && n<3
                     then opts { optVerb = n }
                     else error "Illegal verbosity level (try `-h' for help)"


--------------------------------------------------------------------------