[project @ 2002-10-29 10:53:42 by simonpj]
[ghc-hetmet.git] / ghc / utils / ghc-pkg / Main.hs
index 40a5d90..5bef564 100644 (file)
@@ -1,5 +1,7 @@
+{-# OPTIONS -fglasgow-exts #-}
+
 -----------------------------------------------------------------------------
--- $Id: Main.hs,v 1.22 2002/04/22 14:20:41 simonmar Exp $
+-- $Id: Main.hs,v 1.31 2002/10/29 10:53:42 simonpj Exp $
 --
 -- Package management tool
 -----------------------------------------------------------------------------
@@ -8,11 +10,16 @@ module Main where
 
 import Package
 
-#ifdef __GLASGOW_HASKELL__
-import qualified Exception
-#endif
+#if __GLASGOW_HASKELL__ >= 504
+import System.Console.GetOpt
+import Text.PrettyPrint
+import qualified Control.Exception as Exception
+#else
 import GetOpt
 import Pretty
+import qualified Exception
+#endif
+
 import Monad
 import Directory
 import System  ( getEnv, getArgs, 
@@ -22,10 +29,18 @@ import System       ( getEnv, getArgs,
 import IO
 import List ( isPrefixOf )
 
+import ParsePkgConfLite
+
 #include "../../includes/config.h"
 
-#ifdef mingw32_TARGET_OS
-import Win32DLL
+#ifdef mingw32_HOST_OS
+import Foreign
+
+#if __GLASGOW_HASKELL__ >= 504
+import Foreign.C.String
+#else
+import CString
+#endif
 #endif
 
 main = do
@@ -48,6 +63,7 @@ isAction (Config _)     = False
 isAction (Field _)      = False
 isAction (Input _)      = False
 isAction (AutoGHCiLibs) = False
+isAction (Force)       = False
 isAction _              = True
 
 usageHeader = "ghc-pkg [OPTION...]"
@@ -75,23 +91,16 @@ flags = [
        "Automatically build libs for GHCi (with -a)"
   ]
 
-#ifdef mingw32_TARGET_OS
-subst a b ls = map (\ x -> if x == a then b else x) ls
-
-unDosifyPath xs = subst '\\' '/' xs
-#endif
 
 runit clis = do
+  let err_msg = "missing -f option, location of package.conf unknown"
   conf_file <- 
      case [ f | Config f <- clis ] of
         fs@(_:_)  -> return (last fs)
-#ifndef mingw32_TARGET_OS
-       [] -> die "missing -f option, location of package.conf unknown"
-#else
-       [] -> do h <- getModuleHandle Nothing
-                n <- getModuleFileName h
-                return (reverse (drop (length "/bin/ghc-pkg.exe") (reverse (unDosifyPath n))) ++ "/package.conf")
-#endif
+       [] -> do mb_dir <- getExecDir "/bin/ghc-pkg.exe"
+                case mb_dir of
+                       Nothing  -> die err_msg
+                       Just dir -> return (dir ++ "/package.conf")
 
   let toField "import_dirs"     = return import_dirs
       toField "source_dirs"     = return source_dirs
@@ -104,12 +113,14 @@ runit clis = do
       toField "extra_ghc_opts"  = return extra_ghc_opts
       toField "extra_cc_opts"   = return extra_cc_opts
       toField "extra_ld_opts"   = return extra_ld_opts  
+      toField "framework_dirs"  = return framework_dirs  
+      toField "extra_frameworks"= return extra_frameworks  
       toField s                        = die ("unknown field: `" ++ s ++ "'")
 
   fields <- mapM toField [ f | Field f <- clis ]
 
   s <- readFile conf_file
-  let packages = read s :: [PackageConfig]
+  let packages = parsePackageConfig s
   eval_catch packages (\_ -> die "parse error in package config file")
 
   let auto_ghci_libs = any isAuto clis 
@@ -140,7 +151,7 @@ showPackage packages pkgconf pkg_name fields =
     []    -> die ("can't find package `" ++ pkg_name ++ "'")
     [pkg] | null fields -> hPutStrLn stdout (render (dumpPkgGuts pkg))
          | otherwise   -> hPutStrLn stdout (render (vcat 
-                               (map (vcat . map text) (map ($pkg) fields))))
+                               (map (vcat . map text) (map ($ pkg) fields))))
     _     -> die "showPackage: internal error"
 
 addPackage :: [PackageConfig] -> FilePath -> FilePath
@@ -155,7 +166,7 @@ addPackage packages pkgconf inputFile auto_ghci_libs updatePkg force = do
       f   -> do
         hPutStr stdout ("Reading package info from " ++ show f)
        readFile f
-  let new_pkg = read s :: PackageConfig
+  let new_pkg = parseOnePackageConfig s
   eval_catch new_pkg (\_ -> die "parse error in package info")
   hPutStrLn stdout "done."
   hPutStr stdout "Expanding embedded variables..."
@@ -293,8 +304,13 @@ autoBuildGHCiLib dir batch_file ghci_file = do
   let ghci_lib_file  = dir ++ '/':ghci_file
       batch_lib_file = dir ++ '/':batch_file
   hPutStr stderr ("building GHCi library `" ++ ghci_lib_file ++ "'...")
+#ifdef darwin_TARGET_OS
+  system("ld -r -x -o " ++ ghci_lib_file ++ 
+        " -all_load " ++ batch_lib_file)
+#else
   system("ld -r -x -o " ++ ghci_lib_file ++ 
         " --whole-archive " ++ batch_lib_file)
+#endif
   hPutStrLn stderr (" done.")
 
 -----------------------------------------------------------------------------
@@ -314,6 +330,8 @@ expandEnvVars pkg force = do
   e_g_opts <- expandStrings (extra_ghc_opts pkg)
   e_c_opts <- expandStrings (extra_cc_opts pkg)
   e_l_opts <- expandStrings (extra_ld_opts pkg)
+  f_dirs   <- expandStrings (framework_dirs pkg)
+  e_frames <- expandStrings (extra_frameworks pkg)
   return (pkg { name            = nm
              , import_dirs     = imp_dirs
              , source_dirs     = src_dirs
@@ -326,6 +344,8 @@ expandEnvVars pkg force = do
              , extra_ghc_opts  = e_g_opts
              , extra_cc_opts   = e_c_opts
              , extra_ld_opts   = e_l_opts
+             , framework_dirs  = f_dirs
+             , extra_frameworks= e_frames
              })
   where
    expandStrings = mapM expandString
@@ -380,3 +400,32 @@ my_catch = Exception.catchAllIO
 #endif
 
 #endif
+
+-----------------------------------------
+--     Cut and pasted from ghc/compiler/SysTools
+
+#if defined(mingw32_HOST_OS)
+subst a b ls = map (\ x -> if x == a then b else x) ls
+unDosifyPath xs = subst '\\' '/' xs
+
+getExecDir :: String -> IO (Maybe String)
+-- (getExecDir cmd) returns the directory in which the current
+--                 executable, which should be called 'cmd', is running
+-- So if the full path is /a/b/c/d/e, and you pass "d/e" as cmd,
+-- you'll get "/a/b/c" back as the result
+getExecDir cmd
+  = allocaArray len $ \buf -> do
+       ret <- getModuleFileName nullPtr buf len
+       if ret == 0 then return Nothing
+                   else do s <- peekCString buf
+                           return (Just (reverse (drop (length cmd) 
+                                                       (reverse (unDosifyPath s)))))
+  where
+    len = 2048::Int -- Plenty, PATH_MAX is 512 under Win32.
+
+foreign import stdcall "GetModuleFileNameA" unsafe 
+  getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
+#else
+getExecDir :: String -> IO (Maybe String) 
+getExecDir s = do return Nothing
+#endif