Make.hs 6.93 KB
Newer Older
1
2
3
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}

md9ms's avatar
md9ms committed
4
5
6
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Make
7
-- Copyright   :  Martin Sjögren 2004
8
-- License     :  BSD3
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
11
-- Portability :  portable
md9ms's avatar
md9ms committed
12
--
Duncan Coutts's avatar
Duncan Coutts committed
13
14
15
16
17
18
19
20
21
-- This is an alternative build system that delegates everything to the @make@
-- program. All the commands just end up calling @make@ with appropriate
-- arguments. The intention was to allow preexisting packages that used
-- makefiles to be wrapped into Cabal packages. In practice essentially all
-- such packages were converted over to the \"Simple\" build system instead.
-- Consequently this module is not used much and it certainly only sees cursory
-- maintenance and no testing. Perhaps at some point we should stop pretending
-- that it works.
--
Duncan Coutts's avatar
Duncan Coutts committed
22
-- Uses the parsed command-line from "Distribution.Simple.Setup" in order to build
Ian D. Bollinger's avatar
Ian D. Bollinger committed
23
-- Haskell tools using a back-end build system based on make. Obviously we
24
25
-- assume that there is a configure script, and that after the ConfigCmd has
-- been run, there is a Makefile. Further assumptions:
26
--
27
-- [ConfigCmd] We assume the configure script accepts
28
29
30
31
32
33
34
35
--              @--with-hc@,
--              @--with-hc-pkg@,
--              @--prefix@,
--              @--bindir@,
--              @--libdir@,
--              @--libexecdir@,
--              @--datadir@.
--
36
-- [BuildCmd] We assume that the default Makefile target will build everything.
37
--
38
39
-- [InstallCmd] We assume there is an @install@ target. Note that we assume that
-- this does *not* register the package!
40
41
42
43
44
45
46
--
-- [CopyCmd]    We assume there is a @copy@ target, and a variable @$(destdir)@.
--              The @copy@ target should probably just invoke @make install@
--              recursively (e.g. @$(MAKE) install prefix=$(destdir)\/$(prefix)
--              bindir=$(destdir)\/$(bindir)@. The reason we can\'t invoke @make
--              install@ directly here is that we don\'t know the value of @$(prefix)@.
--
47
-- [SDistCmd] We assume there is a @dist@ target.
48
--
49
-- [RegisterCmd] We assume there is a @register@ target and a variable @$(user)@.
50
--
51
-- [UnregisterCmd] We assume there is an @unregister@ target.
52
--
53
54
-- [HaddockCmd] We assume there is a @docs@ or @doc@ target.

55

56
57
58
--                      copy :
--                              $(MAKE) install prefix=$(destdir)/$(prefix) \
--                                              bindir=$(destdir)/$(bindir) \
md9ms's avatar
md9ms committed
59
60

module Distribution.Make (
61
62
63
        module Distribution.Package,
        License(..), Version(..),
        defaultMain, defaultMainArgs, defaultMainNoRead
md9ms's avatar
md9ms committed
64
65
  ) where

66
67
68
import Prelude ()
import Distribution.Compat.Prelude

md9ms's avatar
md9ms committed
69
-- local
70
import Distribution.Compat.Exception
71
72
import Distribution.Package
import Distribution.Simple.Program
simonmar's avatar
simonmar committed
73
import Distribution.PackageDescription
74
import Distribution.Simple.Setup
75
import Distribution.Simple.Command
md9ms's avatar
md9ms committed
76

77
import Distribution.Simple.Utils
md9ms's avatar
md9ms committed
78

79
import Distribution.License
80
81
import Distribution.Version
import Distribution.Text
md9ms's avatar
md9ms committed
82

83
import System.Environment (getArgs, getProgName)
md9ms's avatar
md9ms committed
84
85
86
import System.Exit

defaultMain :: IO ()
87
88
89
defaultMain = getArgs >>= defaultMainArgs

defaultMainArgs :: [String] -> IO ()
90
defaultMainArgs = defaultMainHelper
md9ms's avatar
md9ms committed
91

92
{-# DEPRECATED defaultMainNoRead "it ignores its PackageDescription arg" #-}
md9ms's avatar
md9ms committed
93
defaultMainNoRead :: PackageDescription -> IO ()
94
95
96
97
defaultMainNoRead = const defaultMain

defaultMainHelper :: [String] -> IO ()
defaultMainHelper args =
98
  case commandsRun (globalCommand commands) commands args of
99
    CommandHelp   help                 -> printHelp help
100
    CommandList   opts                 -> printOptionsList opts
101
102
103
    CommandErrors errs                 -> printErrors errs
    CommandReadyToGo (flags, commandParse)  ->
      case commandParse of
104
105
        _ | fromFlag (globalVersion flags)        -> printVersion
          | fromFlag (globalNumericVersion flags) -> printNumericVersion
106
        CommandHelp     help           -> printHelp help
107
        CommandList     opts           -> printOptionsList opts
108
109
110
111
112
        CommandErrors   errs           -> printErrors errs
        CommandReadyToGo action        -> action

  where
    printHelp help = getProgName >>= putStr . help
113
    printOptionsList = putStr . unlines
114
    printErrors errs = do
EyalLotem's avatar
EyalLotem committed
115
      putStr (intercalate "\n" errs)
116
      exitWith (ExitFailure 1)
117
    printNumericVersion = putStrLn $ display cabalVersion
118
    printVersion        = putStrLn $ "Cabal library version "
119
                                  ++ display cabalVersion
120

121
    progs = defaultProgramDb
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    commands =
      [configureCommand progs `commandAddAction` configureAction
      ,buildCommand     progs `commandAddAction` buildAction
      ,installCommand         `commandAddAction` installAction
      ,copyCommand            `commandAddAction` copyAction
      ,haddockCommand         `commandAddAction` haddockAction
      ,cleanCommand           `commandAddAction` cleanAction
      ,sdistCommand           `commandAddAction` sdistAction
      ,registerCommand        `commandAddAction` registerAction
      ,unregisterCommand      `commandAddAction` unregisterAction
      ]

configureAction :: ConfigFlags -> [String] -> IO ()
configureAction flags args = do
136
  noExtraFlags args
137
  let verbosity = fromFlag (configVerbosity flags)
138
139
140
  rawSystemExit verbosity "sh" $
    "configure"
    : configureArgs backwardsCompatHack flags
141
  where backwardsCompatHack = True
142
143
144

copyAction :: CopyFlags -> [String] -> IO ()
copyAction flags args = do
145
  noExtraFlags args
146
  let destArgs = case fromFlag $ copyDest flags of
147
148
        NoCopyDest      -> ["install"]
        CopyTo path     -> ["copy", "destdir=" ++ path]
149
  rawSystemExit (fromFlag $ copyVerbosity flags) "make" destArgs
150
151
152

installAction :: InstallFlags -> [String] -> IO ()
installAction flags args = do
153
  noExtraFlags args
154
155
  rawSystemExit (fromFlag $ installVerbosity flags) "make" ["install"]
  rawSystemExit (fromFlag $ installVerbosity flags) "make" ["register"]
156
157

haddockAction :: HaddockFlags -> [String] -> IO ()
158
haddockAction flags args = do
159
  noExtraFlags args
160
  rawSystemExit (fromFlag $ haddockVerbosity flags) "make" ["docs"]
161
    `catchIO` \_ ->
162
    rawSystemExit (fromFlag $ haddockVerbosity flags) "make" ["doc"]
163
164
165

buildAction :: BuildFlags -> [String] -> IO ()
buildAction flags args = do
166
  noExtraFlags args
167
  rawSystemExit (fromFlag $ buildVerbosity flags) "make" []
168
169
170

cleanAction :: CleanFlags -> [String] -> IO ()
cleanAction flags args = do
171
  noExtraFlags args
172
  rawSystemExit (fromFlag $ cleanVerbosity flags) "make" ["clean"]
173
174
175

sdistAction :: SDistFlags -> [String] -> IO ()
sdistAction flags args = do
176
  noExtraFlags args
177
  rawSystemExit (fromFlag $ sDistVerbosity flags) "make" ["dist"]
178
179
180

registerAction :: RegisterFlags -> [String] -> IO ()
registerAction  flags args = do
181
  noExtraFlags args
182
  rawSystemExit (fromFlag $ regVerbosity flags) "make" ["register"]
183
184
185

unregisterAction :: RegisterFlags -> [String] -> IO ()
unregisterAction flags args = do
186
  noExtraFlags args
187
  rawSystemExit (fromFlag $ regVerbosity flags) "make" ["unregister"]