b83dd8eb3cc1d6aabea606e941afc59e60199ef2
[ghc-hetmet.git] / ghc / utils / ghc-pkg / Main.hs
1 {-# OPTIONS -fglasgow-exts #-}
2 -----------------------------------------------------------------------------
3 --
4 -- (c) The University of Glasgow 2004.
5 --
6 -- Package management tool
7 --
8 -----------------------------------------------------------------------------
9
10 -- TODO:
11 --      - validate modules
12 --      - expose/hide
13 --      - expanding of variables in new-style package conf
14 --      - version manipulation (checking whether old version exists,
15 --        hiding old version?)
16
17 module Main (main) where
18
19 import Version  ( version, targetOS, targetARCH )
20 import Distribution.InstalledPackageInfo
21 import Distribution.Compat.ReadP
22 import Distribution.Package
23 import Distribution.License
24 import Distribution.Version
25 import Compat.Directory         ( getAppUserDataDirectory )
26 import Control.Exception        ( evaluate )
27 import qualified Control.Exception as Exception
28
29 import Prelude
30
31 import Package -- the old package config type
32
33 #if __GLASGOW_HASKELL__ < 603
34 #include "config.h"
35 #endif
36
37 #if __GLASGOW_HASKELL__ >= 504
38 import System.Console.GetOpt
39 import Text.PrettyPrint
40 import qualified Control.Exception as Exception
41 #else
42 import GetOpt
43 import Pretty
44 import qualified Exception
45 #endif
46
47 import Data.Char        ( isSpace )
48 import Monad
49 import Directory
50 import System   ( getEnv, getArgs, getProgName,
51                   system, exitWith,
52                   ExitCode(..)
53                 )
54 import IO
55 import List ( isPrefixOf, isSuffixOf )
56
57 import ParsePkgConfLite
58
59 #include "../../includes/ghcconfig.h"
60
61 #ifdef mingw32_HOST_OS
62 import Foreign
63
64 #if __GLASGOW_HASKELL__ >= 504
65 import Foreign.C.String
66 #else
67 import CString
68 #endif
69 #endif
70
71 -- -----------------------------------------------------------------------------
72 -- Entry point
73
74 main :: IO ()
75 main = do
76   args <- getArgs
77
78   case getOpt Permute flags args of
79         (cli,_,[]) | FlagHelp `elem` cli -> do
80            prog <- getProgramName
81            bye (usageInfo (usageHeader prog) flags)
82         (cli,_,[]) | FlagVersion `elem` cli ->
83            bye ourCopyright
84         (cli@(_:_),nonopts,[]) ->
85            runit cli nonopts
86         (_,_,errors) -> tryOldCmdLine errors args
87
88 -- If the new command-line syntax fails, then we try the old.  If that
89 -- fails too, then we output the original errors and the new syntax
90 -- (so the old syntax is still available, but hidden).
91 tryOldCmdLine :: [String] -> [String] -> IO ()
92 tryOldCmdLine errors args = do
93   case getOpt Permute oldFlags args of
94         (cli@(_:_),[],[]) -> 
95            oldRunit cli
96         _failed -> do
97            prog <- getProgramName
98            die (concat errors ++ usageInfo (usageHeader prog) flags)
99
100 -- -----------------------------------------------------------------------------
101 -- Command-line syntax
102
103 data Flag
104   = FlagUser
105   | FlagGlobal
106   | FlagHelp
107   | FlagVersion
108   | FlagConfig  FilePath
109   | FlagGlobalConfig FilePath
110   | FlagForce
111   deriving Eq
112
113 flags :: [OptDescr Flag]
114 flags = [
115   Option [] ["user"] (NoArg FlagUser)
116         "use the current user's package database",
117   Option [] ["global"] (NoArg FlagGlobal)
118         "(default) use the global package database",
119   Option ['f'] ["package-conf"] (ReqArg FlagConfig "FILE")
120         "act upon specified package config file (only)",
121   Option [] ["global-conf"] (ReqArg FlagGlobalConfig "FILE")
122         "location of the global package config",
123   Option [] ["force"] (NoArg FlagForce)
124         "ignore missing dependencies, directories, and libraries",
125   Option ['?'] ["help"] (NoArg FlagHelp)
126         "display this help and exit",
127    Option ['V'] ["version"] (NoArg FlagVersion)
128         "output version information and exit"
129   ]
130
131 ourCopyright :: String
132 ourCopyright = "GHC package manager version " ++ version ++ "\n"
133
134 usageHeader :: String -> String
135 usageHeader prog = substProg prog $
136   "Usage:\n" ++
137   "  $p {--help | -?}\n" ++
138   "    Produce this usage message.\n" ++
139   "\n" ++
140   "  $p register {filename | -} [--user | --global]\n" ++
141   "    Register the package using the specified installed package\n" ++
142   "    description. The syntax for the latter is given in the $p\n" ++
143   "    documentation.\n" ++
144   "\n" ++
145   "  $p unregister {pkg-id}\n" ++
146   "    Unregister the specified package.\n" ++
147   "\n" ++
148   "  $p expose {pkg-id}\n" ++
149   "    Expose the specified package.\n" ++
150   "\n" ++
151   "  $p hide {pkg-id}\n" ++
152   "    Hide the specified package.\n" ++
153   "\n" ++
154   "  $p list [--global | --user]\n" ++
155   "    List all registered packages, both global and user (unless either\n" ++
156   "    --global or --user is specified), and both hidden and exposed.\n" ++
157   "\n" ++
158   "  $p describe {pkg-id}\n" ++
159   "    Give the registered description for the specified package. The\n" ++
160   "    description is returned in precisely the syntax required by $p\n" ++
161   "    register.\n" ++
162   "\n" ++
163   "  $p field {pkg-id} {field}\n" ++
164   "    Extract the specified field of the package description for the\n" ++
165   "    specified package.\n"
166
167 substProg :: String -> String -> String
168 substProg _ [] = []
169 substProg prog ('$':'p':xs) = prog ++ substProg prog xs
170 substProg prog (c:xs) = c : substProg prog xs
171
172 -- -----------------------------------------------------------------------------
173 -- Do the business
174
175 runit :: [Flag] -> [String] -> IO ()
176 runit cli nonopts = do
177   prog <- getProgramName
178   dbs <- getPkgDatabases cli
179   db_stack <- mapM readParseDatabase dbs
180   let
181         force = FlagForce `elem` cli
182   --
183   -- first, parse the command
184   case nonopts of
185     ["register", filename] -> 
186         registerPackage filename [] db_stack False False force
187     ["update", filename] -> 
188         registerPackage filename [] db_stack False True force
189     ["unregister", pkgid_str] -> do
190         pkgid <- readPkgId pkgid_str
191         unregisterPackage db_stack pkgid
192     ["expose", pkgid_str] -> do
193         pkgid <- readPkgId pkgid_str
194         exposePackage pkgid db_stack
195     ["hide",   pkgid_str] -> do
196         pkgid <- readPkgId pkgid_str
197         hidePackage pkgid db_stack
198     ["list"] -> do
199         listPackages db_stack
200     ["describe", pkgid_str] -> do
201         pkgid <- readPkgId pkgid_str
202         describePackage db_stack pkgid
203     ["field", pkgid_str, field] -> do
204         pkgid <- readPkgId pkgid_str
205         describeField db_stack pkgid field
206     [] -> do
207         die ("missing command\n" ++ 
208                 usageInfo (usageHeader prog) flags)
209     (_cmd:_) -> do
210         die ("command-line syntax error\n" ++ 
211                 usageInfo (usageHeader prog) flags)
212
213 parseCheck :: ReadP a a -> String -> String -> IO a
214 parseCheck parser str what = 
215   case readP_to_S parser str of
216     [(x,ys)] | all isSpace ys -> return x
217     _ -> die ("cannot parse \'" ++ str ++ "\' as a " ++ what)
218
219 readPkgId :: String -> IO PackageIdentifier
220 readPkgId str = parseCheck parsePackageId str "package identifier"
221
222 -- -----------------------------------------------------------------------------
223 -- Package databases
224
225 -- Some commands operate on a single database:
226 --      register, unregister, expose, hide
227 -- however these commands also check the union of the available databases
228 -- in order to check consistency.  For example, register will check that
229 -- dependencies exist before registering a package.
230 --
231 -- Some commands operate  on multiple databases, with overlapping semantics:
232 --      list, describe, field
233
234 type PackageDBName  = FilePath
235 type PackageDB      = [InstalledPackageInfo]
236
237 type PackageDBStack = [(PackageDBName,PackageDB)]
238         -- A stack of package databases.  Convention: head is the topmost
239         -- in the stack.  Earlier entries override later one.
240
241 -- The output of this function is the list of databases to act upon, with
242 -- the "topmost" overlapped database last.  The commands which operate on a
243 -- single database will use the last one.  Commands which operate on multiple
244 -- databases will interpret the databases as overlapping.
245 getPkgDatabases :: [Flag] -> IO [PackageDBName]
246 getPkgDatabases flags = do
247   -- first we determine the location of the global package config.  On Windows,
248   -- this is found relative to the ghc-pkg.exe binary, whereas on Unix the
249   -- location is passed to the binary using the --global-config flag by the
250   -- wrapper script.
251   let err_msg = "missing --global-conf option, location of global package.conf unknown\n"
252   global_conf <- 
253      case [ f | FlagGlobalConfig f <- flags ] of
254         [] -> do mb_dir <- getExecDir "/bin/ghc-pkg.exe"
255                  case mb_dir of
256                         Nothing  -> die err_msg
257                         Just dir -> return (dir `joinFileName` "package.conf")
258         fs -> return (last fs)
259
260   -- get the location of the user package database, and create it if necessary
261   appdir <- getAppUserDataDirectory "ghc"
262
263   let
264         subdir = targetARCH ++ '-':targetOS ++ '-':version
265         user_conf = appdir `joinFileName` subdir `joinFileName` "package.conf"
266   b <- doesFileExist user_conf
267   when (not b) $ do
268         putStrLn ("Creating user package database in " ++ user_conf)
269         createParents user_conf
270         writeFile user_conf emptyPackageConfig
271
272   let
273         databases = foldl addDB [global_conf] flags
274
275         -- implement the following rules:
276         --      global database is the default
277         --      --user means overlap with the user database
278         --      --global means reset to just the global database
279         --      -f <file> means overlap with <file>
280         addDB dbs FlagUser       = user_conf : dbs
281         addDB dbs FlagGlobal     = [global_conf]
282         addDB dbs (FlagConfig f) = f : dbs
283         addDB dbs _              = dbs
284
285   return databases
286
287 readParseDatabase :: PackageDBName -> IO (PackageDBName,PackageDB)
288 readParseDatabase filename = do
289   str <- readFile filename
290   let packages = read str
291   evaluate packages
292     `Exception.catch` \_ -> 
293         die (filename ++ ": parse error in package config file\n")
294   return (filename,packages)
295
296 emptyPackageConfig :: String
297 emptyPackageConfig = "[]"
298
299 -- -----------------------------------------------------------------------------
300 -- Registering
301
302 registerPackage :: FilePath
303                 -> [(String,String)] --  defines, ToDo: maybe remove?
304                 -> PackageDBStack
305                 -> Bool         -- auto_ghci_libs
306                 -> Bool         -- update
307                 -> Bool         -- force
308                 -> IO ()
309 registerPackage input defines db_stack auto_ghci_libs update force = do
310   let
311         db_to_operate_on = head db_stack
312         db_filename      = fst db_to_operate_on
313   --
314   checkConfigAccess db_filename
315
316   s <-
317     case input of
318       "-" -> do
319         putStr "Reading package info from stdin... "
320         getContents
321       f   -> do
322         putStr ("Reading package info from " ++ show f)
323         readFile f
324
325   pkg <- parsePackageInfo s defines force
326   putStrLn "done."
327
328   validatePackageConfig pkg db_stack auto_ghci_libs update force
329   new_details <- updatePackageDB (snd db_to_operate_on) pkg
330   savePackageConfig db_filename
331   maybeRestoreOldConfig db_filename $
332     writeNewConfig db_filename new_details
333
334 parsePackageInfo
335         :: String
336         -> [(String,String)]
337         -> Bool
338         -> IO InstalledPackageInfo
339 parsePackageInfo str defines force =
340   case parseInstalledPackageInfo str of
341     Right ok -> return ok
342     Left err -> do
343         old_pkg <- evaluate (parseOnePackageConfig str)
344                             `Exception.catch` \_ -> parse_failed
345         putStr "Expanding embedded variables... "
346         new_old_pkg <- expandEnvVars old_pkg defines force
347         return (convertOldPackage old_pkg)
348  where
349    parse_failed = die "parse error in package info\n"
350
351 convertOldPackage :: PackageConfig -> InstalledPackageInfo
352 convertOldPackage
353    Package {
354         name            = name,
355         auto            = auto,
356         import_dirs     = import_dirs,
357         source_dirs     = source_dirs,
358         library_dirs    = library_dirs,
359         hs_libraries    = hs_libraries,
360         extra_libraries = extra_libraries,
361         include_dirs    = include_dirs,
362         c_includes      = c_includes,
363         package_deps    = package_deps,
364         extra_ghc_opts  = extra_ghc_opts,
365         extra_cc_opts   = extra_cc_opts,
366         extra_ld_opts   = extra_ld_opts,
367         framework_dirs  = framework_dirs,
368         extra_frameworks= extra_frameworks
369     }
370    = InstalledPackageInfo {
371         package          = pkgNameToId name,
372         license          = AllRightsReserved,
373         copyright        = "",
374         maintainer       = "",
375         author           = "",
376         stability        = "",
377         homepage         = "",
378         pkgUrl           = "",
379         description      = "",
380         category         = "",
381         exposed          = auto,
382         exposedModules   = [],
383         hiddenModules    = [],
384         importDirs       = import_dirs,
385         libraryDirs      = library_dirs,
386         hsLibraries      = hs_libraries,
387         extraLibraries   = extra_libraries,
388         includeDirs      = include_dirs,
389         includes         = c_includes,
390         depends          = map pkgNameToId package_deps,
391         extraHugsOpts    = [],
392         extraCcOpts      = extra_cc_opts,
393         extraLdOpts      = extra_ld_opts,
394         frameworkDirs    = framework_dirs,
395         extraFrameworks  = extra_frameworks,
396         haddockInterfaces = [],
397         haddockHTMLs      = []
398     }
399
400
401 -- Used for converting old versionless package names to new PackageIdentifiers.
402 -- "Version [] []" is special: it means "no version" or "any version"
403 pkgNameToId :: String -> PackageIdentifier
404 pkgNameToId name = PackageIdentifier name (Version [] [])
405
406 -- -----------------------------------------------------------------------------
407 -- Unregistering
408
409 unregisterPackage :: PackageDBStack -> PackageIdentifier -> IO ()
410 unregisterPackage [] _ = error "unregisterPackage"
411 unregisterPackage ((db_name, pkgs) : _) pkgid = do  
412   checkConfigAccess db_name
413   when (pkgid `notElem` map package pkgs)
414        (die (db_name ++ ": package '" ++ showPackageId pkgid
415                  ++ "' not found\n"))
416   savePackageConfig db_name
417   maybeRestoreOldConfig db_name $
418     writeNewConfig db_name (filter ((/= pkgid) . package) pkgs)
419
420 -- -----------------------------------------------------------------------------
421 -- Exposing
422
423 exposePackage :: PackageIdentifier ->  PackageDBStack -> IO ()
424 exposePackage = error "TODO"
425
426 -- -----------------------------------------------------------------------------
427 -- Hiding
428
429 hidePackage :: PackageIdentifier ->  PackageDBStack -> IO ()
430 hidePackage = error "TODO"
431
432 -- -----------------------------------------------------------------------------
433 -- Listing packages
434
435 listPackages ::  PackageDBStack -> IO ()
436 listPackages db_confs = do
437   mapM_ show_pkgconf (reverse db_confs)
438   where show_pkgconf (db_name,pkg_confs) =
439           hPutStrLn stdout (render $
440                 text (db_name ++ ":") $$ nest 4 packages
441                 )
442            where packages = fsep (punctuate comma (map pp_pkg pkg_confs))
443                  pp_pkg = text . showPackageId . package
444
445
446 -- -----------------------------------------------------------------------------
447 -- Describe
448
449 describePackage :: PackageDBStack -> PackageIdentifier -> IO ()
450 describePackage db_stack pkgid = do
451   p <- findPackage db_stack pkgid
452   putStrLn (showInstalledPackageInfo p)
453
454 findPackage :: PackageDBStack -> PackageIdentifier -> IO InstalledPackageInfo
455 findPackage db_stack pkgid
456   = case [ p | p <- all_pkgs, pkgid == package p ] of
457         [] -> die ("cannot find package " ++ showPackageId pkgid)
458         (p:ps) -> return p
459   where
460         all_pkgs = concat (map snd db_stack)
461
462 -- -----------------------------------------------------------------------------
463 -- Field
464
465 describeField :: PackageDBStack -> PackageIdentifier -> String -> IO ()
466 describeField db_stack pkgid field = do
467   case toField field of
468     Nothing -> die ("unknown field: " ++ field)
469     Just fn -> do
470         p <- findPackage db_stack pkgid 
471         putStrLn (fn p)
472
473 toField :: String -> Maybe (InstalledPackageInfo -> String)
474 -- backwards compatibility:
475 toField "import_dirs"     = Just $ strList . importDirs
476 toField "source_dirs"     = Just $ strList . importDirs
477 toField "library_dirs"    = Just $ strList . libraryDirs
478 toField "hs_libraries"    = Just $ strList . hsLibraries
479 toField "extra_libraries" = Just $ strList . extraLibraries
480 toField "include_dirs"    = Just $ strList . includeDirs
481 toField "c_includes"      = Just $ strList . includes
482 toField "package_deps"    = Just $ strList . map showPackageId. depends
483 toField "extra_cc_opts"   = Just $ strList . extraCcOpts
484 toField "extra_ld_opts"   = Just $ strList . extraLdOpts  
485 toField "framework_dirs"  = Just $ strList . frameworkDirs  
486 toField "extra_frameworks"= Just $ strList . extraFrameworks  
487 toField s                 = showInstalledPackageInfoField s
488
489 strList :: [String] -> String
490 strList = show
491
492 -- -----------------------------------------------------------------------------
493 -- Manipulating package.conf files
494
495 checkConfigAccess :: FilePath -> IO ()
496 checkConfigAccess filename = do
497   access <- getPermissions filename
498   when (not (writable access))
499       (die (filename ++ ": you don't have permission to modify this file\n"))
500
501 maybeRestoreOldConfig :: FilePath -> IO () -> IO ()
502 maybeRestoreOldConfig filename io
503   = io `catch` \e -> do
504         hPutStrLn stderr (show e)
505         hPutStr stdout ("\nWARNING: an error was encountered while the new \n"++
506                           "configuration was being written.  Attempting to \n"++
507                           "restore the old configuration... ")
508         renameFile (filename ++ ".old")  filename
509         hPutStrLn stdout "done."
510         ioError e
511
512 writeNewConfig :: FilePath -> [InstalledPackageInfo] -> IO ()
513 writeNewConfig filename packages = do
514   hPutStr stdout "Writing new package config file... "
515   h <- openFile filename WriteMode
516   hPutStrLn h (show packages)
517   hClose h
518   hPutStrLn stdout "done."
519
520 savePackageConfig :: FilePath -> IO ()
521 savePackageConfig filename = do
522   hPutStr stdout "Saving old package config file... "
523     -- mv rather than cp because we've already done an hGetContents
524     -- on this file so we won't be able to open it for writing
525     -- unless we move the old one out of the way...
526   let oldFile = filename ++ ".old"
527   doesExist <- doesFileExist oldFile  `catch` (\ _ -> return False)
528   when doesExist (removeFile oldFile `catch` (const $ return ()))
529   catch (renameFile filename oldFile)
530         (\ err -> do
531                 hPutStrLn stderr (unwords [ "Unable to rename "
532                                           , show filename
533                                           , " to "
534                                           , show oldFile
535                                           ])
536                 ioError err)
537   hPutStrLn stdout "done."
538
539 -----------------------------------------------------------------------------
540 -- Sanity-check a new package config, and automatically build GHCi libs
541 -- if requested.
542
543 validatePackageConfig :: InstalledPackageInfo
544                       -> PackageDBStack
545                       -> Bool   -- auto-ghc-libs
546                       -> Bool   -- update
547                       -> Bool   -- force
548                       -> IO ()
549 validatePackageConfig pkg db_stack auto_ghci_libs update force = do
550   checkDuplicates db_stack pkg update
551   mapM_ (checkDep db_stack force) (depends pkg)
552   mapM_ (checkDir force) (importDirs pkg)
553   mapM_ (checkDir force) (libraryDirs pkg)
554   mapM_ (checkDir force) (includeDirs pkg)
555   mapM_ (checkHSLib (libraryDirs pkg) auto_ghci_libs force) (hsLibraries pkg)
556   -- ToDo: check these somehow?
557   --    extra_libraries :: [String],
558   --    c_includes      :: [String],
559
560
561 checkDuplicates :: PackageDBStack -> InstalledPackageInfo -> Bool -> IO ()
562 checkDuplicates db_stack pkg update = do
563   let
564         pkgid = package pkg
565
566         (_top_db_name, pkgs) : _  = db_stack
567
568         pkgs_with_same_name = 
569                 [ p | p <- pkgs, pkgName (package p) == pkgName pkgid]
570         exposed_pkgs_with_same_name =
571                 filter exposed pkgs_with_same_name
572   --
573   -- Check whether this package id already exists in this DB
574   --
575   when (not update && (package pkg `elem` map package pkgs)) $
576        die ("package " ++ showPackageId pkgid ++ " is already installed\n")
577   --
578   -- if we are exposing this new package, then check that
579   -- there are no other exposed packages with the same name.
580   --
581   when (not update && exposed pkg && not (null exposed_pkgs_with_same_name)) $
582         die ("trying to register " ++ showPackageId pkgid 
583                   ++ " as exposed, but "
584                   ++ showPackageId (package (head exposed_pkgs_with_same_name))
585                   ++ " is also exposed.")
586
587
588 checkDir :: Bool -> String -> IO ()
589 checkDir force d
590  | "$libdir" `isPrefixOf` d = return ()
591         -- can't check this, because we don't know what $libdir is
592  | otherwise = do
593    there <- doesDirectoryExist d
594    when (not there)
595        (dieOrForce force (d ++ " doesn't exist or isn't a directory\n"))
596
597 checkDep :: PackageDBStack -> Bool -> PackageIdentifier -> IO ()
598 checkDep db_stack force pkgid
599   | real_version && pkgid `elem` pkgids = return ()
600   | not real_version && pkgName pkgid `elem` pkg_names = return ()
601   | otherwise = dieOrForce force ("dependency " ++ showPackageId pkgid
602                                         ++ " doesn't exist\n")
603   where
604         -- for backwards compat, we treat 0.0 as a special version,
605         -- and don't check that it actually exists.
606         real_version = versionBranch (pkgVersion pkgid) /= []
607         
608         all_pkgs = concat (map snd db_stack)
609         pkgids = map package all_pkgs
610         pkg_names = map pkgName pkgids
611
612 checkHSLib :: [String] -> Bool -> Bool -> String -> IO ()
613 checkHSLib dirs auto_ghci_libs force lib = do
614   let batch_lib_file = "lib" ++ lib ++ ".a"
615   bs <- mapM (doesLibExistIn batch_lib_file) dirs
616   case [ dir | (exists,dir) <- zip bs dirs, exists ] of
617         [] -> dieOrForce force ("cannot find `" ++ batch_lib_file ++
618                                  "' on library path") 
619         (dir:_) -> checkGHCiLib dirs dir batch_lib_file lib auto_ghci_libs
620
621 doesLibExistIn :: String -> String -> IO Bool
622 doesLibExistIn lib d
623  | "$libdir" `isPrefixOf` d = return True
624  | otherwise                = doesFileExist (d ++ '/':lib)
625
626 checkGHCiLib :: [String] -> String -> String -> String -> Bool -> IO ()
627 checkGHCiLib dirs batch_lib_dir batch_lib_file lib auto_build
628   | auto_build = autoBuildGHCiLib batch_lib_dir batch_lib_file ghci_lib_file
629   | otherwise  = do
630       bs <- mapM (doesLibExistIn ghci_lib_file) dirs
631       case [dir | (exists,dir) <- zip bs dirs, exists] of
632         []    -> hPutStrLn stderr ("warning: can't find GHCi lib `" ++ ghci_lib_file ++ "'")
633         (_:_) -> return ()
634   where
635     ghci_lib_file = lib ++ ".o"
636
637 -- automatically build the GHCi version of a batch lib, 
638 -- using ld --whole-archive.
639
640 autoBuildGHCiLib :: String -> String -> String -> IO ()
641 autoBuildGHCiLib dir batch_file ghci_file = do
642   let ghci_lib_file  = dir ++ '/':ghci_file
643       batch_lib_file = dir ++ '/':batch_file
644   hPutStr stderr ("building GHCi library `" ++ ghci_lib_file ++ "'...")
645 #if defined(darwin_TARGET_OS)
646   r <- system("ld -r -x -o " ++ ghci_lib_file ++ 
647                  " -all_load " ++ batch_lib_file)
648 #elif defined(mingw32_HOST_OS)
649   execDir <- getExecDir "/bin/ghc-pkg.exe"
650   r <- system (maybe "" (++"/gcc-lib/") execDir++"ld -r -x -o " ++ 
651                 ghci_lib_file ++ " --whole-archive " ++ batch_lib_file)
652 #else
653   r <- system("ld -r -x -o " ++ ghci_lib_file ++ 
654                  " --whole-archive " ++ batch_lib_file)
655 #endif
656   when (r /= ExitSuccess) $ exitWith r
657   hPutStrLn stderr (" done.")
658
659 -- -----------------------------------------------------------------------------
660 -- Updating the DB with the new package.
661
662 updatePackageDB
663         :: [InstalledPackageInfo]
664         -> InstalledPackageInfo
665         -> IO [InstalledPackageInfo]
666 updatePackageDB pkgs new_pkg = do
667   let
668         is_exposed = exposed new_pkg
669         pkgid      = package new_pkg
670         name       = pkgName pkgid
671
672         pkgs' = [ maybe_hide p | p <- pkgs, package p /= pkgid ]
673         
674         -- When update is on, and we're exposing the new package,
675         -- we hide any packages with the same name (different versions)
676         -- in the current DB.  Earlier checks will have failed if
677         -- update isn't on.
678         maybe_hide p
679           | is_exposed && pkgName (package p) == name = p{ exposed = False }
680           | otherwise = p
681   --
682   return (pkgs'++[new_pkg])
683
684 -- -----------------------------------------------------------------------------
685 -- The old command-line syntax, supported for backwards compatibility
686
687 data OldFlag 
688   = OF_Config FilePath
689   | OF_Input FilePath
690   | OF_List
691   | OF_ListLocal
692   | OF_Add Bool {- True => replace existing info -}
693   | OF_Remove String | OF_Show String 
694   | OF_Field String | OF_AutoGHCiLibs | OF_Force
695   | OF_DefinedName String String
696   | OF_GlobalConfig FilePath
697   deriving (Eq)
698
699 isAction :: OldFlag -> Bool
700 isAction OF_Config{}        = False
701 isAction OF_Field{}         = False
702 isAction OF_Input{}         = False
703 isAction OF_AutoGHCiLibs{}  = False
704 isAction OF_Force{}         = False
705 isAction OF_DefinedName{}   = False
706 isAction OF_GlobalConfig{}  = False
707 isAction _                  = True
708
709 oldFlags :: [OptDescr OldFlag]
710 oldFlags = [
711   Option ['f'] ["config-file"] (ReqArg OF_Config "FILE")
712         "use the specified package config file",
713   Option ['l'] ["list-packages"] (NoArg OF_List)
714         "list packages in all config files",
715   Option ['L'] ["list-local-packages"] (NoArg OF_ListLocal)
716         "list packages in the specified config file",
717   Option ['a'] ["add-package"] (NoArg (OF_Add False))
718         "add a new package",
719   Option ['u'] ["update-package"] (NoArg (OF_Add True))
720         "update package with new configuration",
721   Option ['i'] ["input-file"] (ReqArg OF_Input "FILE")
722         "read new package info from specified file",
723   Option ['s'] ["show-package"] (ReqArg OF_Show "NAME")
724         "show the configuration for package NAME",
725   Option [] ["field"] (ReqArg OF_Field "FIELD")
726         "(with --show-package) Show field FIELD only",
727   Option [] ["force"] (NoArg OF_Force)
728         "ignore missing directories/libraries",
729   Option ['r'] ["remove-package"] (ReqArg OF_Remove "NAME")
730         "remove an installed package",
731   Option ['g'] ["auto-ghci-libs"] (NoArg OF_AutoGHCiLibs)
732         "automatically build libs for GHCi (with -a)",
733   Option ['D'] ["define-name"] (ReqArg toDefined "NAME=VALUE")
734         "define NAME as VALUE",
735   Option [] ["global-conf"] (ReqArg OF_GlobalConfig "FILE")
736         "location of the global package config"
737   ]
738  where
739   toDefined str = 
740     case break (=='=') str of
741       (nm,[]) -> OF_DefinedName nm []
742       (nm,_:val) -> OF_DefinedName nm val
743
744 oldRunit :: [OldFlag] -> IO ()
745 oldRunit clis = do
746   let config_flags = [ f | Just f <- map conv clis ]
747
748       conv (OF_GlobalConfig f) = Just (FlagGlobalConfig f)
749       conv (OF_Config f)       = Just (FlagConfig f)
750       conv _                   = Nothing
751
752   db_names <- getPkgDatabases config_flags
753   db_stack <- mapM readParseDatabase db_names
754
755   let fields = [ f | OF_Field f <- clis ]
756
757   let auto_ghci_libs = any isAuto clis 
758          where isAuto OF_AutoGHCiLibs = True; isAuto _ = False
759       input_file = head ([ f | (OF_Input f) <- clis] ++ ["-"])
760
761       force = OF_Force `elem` clis
762       
763       defines = [ (nm,val) | OF_DefinedName nm val <- clis ]
764
765   case [ c | c <- clis, isAction c ] of
766     [ OF_List ]      -> listPackages db_stack
767     [ OF_ListLocal ] -> listPackages db_stack
768     [ OF_Add upd ]   -> registerPackage input_file defines db_stack
769                                 auto_ghci_libs upd force
770     [ OF_Remove p ]  -> unregisterPackage db_stack (pkgNameToId p)
771     [ OF_Show p ]
772         | null fields -> describePackage db_stack (pkgNameToId p)
773         | otherwise   -> mapM_ (describeField db_stack (pkgNameToId p)) fields
774     _            -> do prog <- getProgramName
775                        die (usageInfo (usageHeader prog) flags)
776
777 -- ---------------------------------------------------------------------------
778
779 expandEnvVars :: PackageConfig -> [(String, String)]
780         -> Bool -> IO PackageConfig
781 expandEnvVars pkg defines force = do
782    -- permit _all_ strings to contain ${..} environment variable references,
783    -- arguably too flexible.
784   nm       <- expandString  (name pkg)
785   imp_dirs <- expandStrings (import_dirs pkg) 
786   src_dirs <- expandStrings (source_dirs pkg) 
787   lib_dirs <- expandStrings (library_dirs pkg) 
788   hs_libs  <- expandStrings (hs_libraries pkg)
789   ex_libs  <- expandStrings (extra_libraries pkg)
790   inc_dirs <- expandStrings (include_dirs pkg)
791   c_incs   <- expandStrings (c_includes pkg)
792   p_deps   <- expandStrings (package_deps pkg)
793   e_g_opts <- expandStrings (extra_ghc_opts pkg)
794   e_c_opts <- expandStrings (extra_cc_opts pkg)
795   e_l_opts <- expandStrings (extra_ld_opts pkg)
796   f_dirs   <- expandStrings (framework_dirs pkg)
797   e_frames <- expandStrings (extra_frameworks pkg)
798   return (pkg { name            = nm
799               , import_dirs     = imp_dirs
800               , source_dirs     = src_dirs
801               , library_dirs    = lib_dirs
802               , hs_libraries    = hs_libs
803               , extra_libraries = ex_libs
804               , include_dirs    = inc_dirs
805               , c_includes      = c_incs
806               , package_deps    = p_deps
807               , extra_ghc_opts  = e_g_opts
808               , extra_cc_opts   = e_c_opts
809               , extra_ld_opts   = e_l_opts
810               , framework_dirs  = f_dirs
811               , extra_frameworks= e_frames
812               })
813   where
814    expandStrings :: [String] -> IO [String]
815    expandStrings = liftM concat . mapM expandSpecial
816
817    -- Permit substitutions for list-valued variables (but only when
818    -- they occur alone), e.g., package_deps["${deps}"] where env var
819    -- (say) 'deps' is "base,haskell98,network"
820    expandSpecial :: String -> IO [String]
821    expandSpecial str =
822       let expand f = liftM f $ expandString str
823       in case splitString str of
824          [Var _] -> expand (wordsBy (== ','))
825          _ -> expand (\x -> [x])
826
827    expandString :: String -> IO String
828    expandString = liftM concat . mapM expandElem . splitString
829
830    expandElem :: Elem -> IO String
831    expandElem (String s) = return s
832    expandElem (Var v)    = lookupEnvVar v
833
834    lookupEnvVar :: String -> IO String
835    lookupEnvVar nm = 
836      case lookup nm defines of
837        Just x | not (null x) -> return x
838        _      -> 
839         catch (System.getEnv nm)
840            (\ _ -> do dieOrForce force ("Unable to expand variable " ++ 
841                                         show nm)
842                       return "")
843
844 data Elem = String String | Var String
845
846 splitString :: String -> [Elem]
847 splitString "" = []
848 splitString str =
849    case break (== '$') str of
850       (pre, _:'{':xs) ->
851          case span (/= '}') xs of
852             (var, _:suf) ->
853                (if null pre then id else (String pre :)) (Var var : splitString suf)
854             _ -> [String str]   -- no closing brace
855       _ -> [String str]   -- no dollar/opening brace combo
856
857 -- wordsBy isSpace == words
858 wordsBy :: (Char -> Bool) -> String -> [String]
859 wordsBy p s = case dropWhile p s of
860   "" -> []
861   s' -> w : wordsBy p s'' where (w,s'') = break p s'
862
863 -----------------------------------------------------------------------------
864
865 getProgramName :: IO String
866 getProgramName = liftM (`withoutSuffix` ".bin") getProgName
867    where str `withoutSuffix` suff
868             | suff `isSuffixOf` str = take (length str - length suff) str
869             | otherwise             = str
870
871 bye :: String -> IO a
872 bye s = putStr s >> exitWith ExitSuccess
873
874 die :: String -> IO a
875 die s = do 
876   hFlush stdout
877   prog <- getProgramName
878   hPutStr stderr (prog ++ ": " ++ s)
879   exitWith (ExitFailure 1)
880
881 dieOrForce :: Bool -> String -> IO ()
882 dieOrForce force s 
883   | force     = do hFlush stdout; hPutStrLn stderr (s ++ " (ignoring)")
884   | otherwise = die (s ++ "\n")
885
886
887 -----------------------------------------------------------------------------
888 -- Create a hierarchy of directories
889
890 createParents :: FilePath -> IO ()
891 createParents dir = do
892   let parent = directoryOf dir
893   b <- doesDirectoryExist parent
894   when (not b) $ do
895         createParents parent
896         createDirectory parent
897
898 -----------------------------------------
899 --      Cut and pasted from ghc/compiler/SysTools
900
901 #if defined(mingw32_HOST_OS)
902 subst a b ls = map (\ x -> if x == a then b else x) ls
903 unDosifyPath xs = subst '\\' '/' xs
904
905 getExecDir :: String -> IO (Maybe String)
906 -- (getExecDir cmd) returns the directory in which the current
907 --                  executable, which should be called 'cmd', is running
908 -- So if the full path is /a/b/c/d/e, and you pass "d/e" as cmd,
909 -- you'll get "/a/b/c" back as the result
910 getExecDir cmd
911   = allocaArray len $ \buf -> do
912         ret <- getModuleFileName nullPtr buf len
913         if ret == 0 then return Nothing
914                     else do s <- peekCString buf
915                             return (Just (reverse (drop (length cmd) 
916                                                         (reverse (unDosifyPath s)))))
917   where
918     len = 2048::Int -- Plenty, PATH_MAX is 512 under Win32.
919
920 foreign import stdcall unsafe  "GetModuleFileNameA"
921   getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
922 #else
923 getExecDir :: String -> IO (Maybe String) 
924 getExecDir _ = return Nothing
925 #endif
926
927 -- -----------------------------------------------------------------------------
928 -- Utils from Krasimir's FilePath library, copied here for now
929
930 directoryOf :: FilePath -> FilePath
931 directoryOf = fst.splitFileName
932
933 splitFileName :: FilePath -> (String, String)
934 splitFileName p = (reverse (path2++drive), reverse fname)
935   where
936 #ifdef mingw32_TARGET_OS
937     (path,drive) = break (== ':') (reverse p)
938 #else
939     (path,drive) = (reverse p,"")
940 #endif
941     (fname,path1) = break isPathSeparator path
942     path2 = case path1 of
943       []                           -> "."
944       [_]                          -> path1   -- don't remove the trailing slash if 
945                                               -- there is only one character
946       (c:path) | isPathSeparator c -> path
947       _                            -> path1
948
949 joinFileName :: String -> String -> FilePath
950 joinFileName ""  fname = fname
951 joinFileName "." fname = fname
952 joinFileName dir fname
953   | isPathSeparator (last dir) = dir++fname
954   | otherwise                  = dir++pathSeparator:fname
955
956 isPathSeparator :: Char -> Bool
957 isPathSeparator ch =
958 #ifdef mingw32_TARGET_OS
959   ch == '/' || ch == '\\'
960 #else
961   ch == '/'
962 #endif
963
964 pathSeparator :: Char
965 #ifdef mingw32_TARGET_OS
966 pathSeparator = '\\'
967 #else
968 pathSeparator = '/'
969 #endif