try to fix the way we find $topdir
[ghc-hetmet.git] / compiler / main / SysTools.lhs
index 9d53b81..11e31b8 100644 (file)
@@ -163,16 +163,11 @@ initSysTools mbMinusB dflags0
         ; let installed, installed_bin :: FilePath -> FilePath
               installed_bin pgm  = top_dir </> pgm
               installed     file = top_dir </> file
-              inplace dir   pgm  = let real_top_dir = foldr (</>) ""
-                                                    $ reverse
-                                                    $ drop 4
-                                                    $ reverse
-                                                    $ splitDirectories top_dir
-                                   in real_top_dir </> dir </> pgm
+              inplace dir   pgm  = top_dir </> dir </> pgm
 
         ; let pkgconfig_path
                 | am_installed = installed "package.conf"
-                | otherwise    = inplace cGHC_DRIVER_DIR_REL "package.conf.inplace"
+                | otherwise    = inplace "inplace-datadir" "package.conf"
 
               ghc_usage_msg_path
                 | am_installed = installed "ghc-usage.txt"
@@ -327,11 +322,16 @@ findTopDir :: Maybe String   -- Maybe TopDir path (without the '-B' prefix).
 
 findTopDir mbMinusB
   = do { top_dir <- get_proto
-        -- Discover whether we're running in a build tree or in an installation,
-        -- by looking for the package configuration file.
-       ; am_installed <- doesFileExist (top_dir </> "package.conf")
+       ; exists1 <- doesFileExist (top_dir </> "package.conf")
+       ; exists2 <- doesFileExist (top_dir </> "inplace")
+       ; let amInplace = not exists1 -- On Windows, package.conf doesn't exist
+                                     -- when we are inplace
+                      || exists2 -- On Linux, the presence of inplace signals
+                                 -- that we are inplace
 
-       ; return (am_installed, top_dir)
+       ; let real_top = if exists2 then top_dir </> ".." else top_dir
+
+       ; return (not amInplace, real_top)
        }
   where
     -- get_proto returns a Unix-format path (relying on getBaseDir to do so too)
@@ -856,9 +856,14 @@ getBaseDir = do let len = (2048::Int) -- plenty, PATH_MAX is 512 under Win32.
     rootDir s = case splitFileName $ normalise s of
                 (d, "ghc.exe") ->
                     case splitFileName $ takeDirectory d of
+                    -- installed ghc.exe is in $topdir/bin/ghc.exe
                     (d', "bin") -> takeDirectory d'
-                    _ -> panic ("Expected \"bin\" in " ++ show s)
-                _ -> panic ("Expected \"ghc.exe\" in " ++ show s)
+                    -- inplace ghc.exe is in $topdir/ghc/stage1-inplace/ghc.exe
+                    (d', x) | "-inplace" `isSuffixOf` x -> 
+                        takeDirectory d' </> ".."
+                    _ -> fail
+                _ -> fail
+        where fail = panic ("can't decompose ghc.exe path: " ++ show s)
 
 foreign import stdcall unsafe "GetModuleFileNameA"
   getModuleFileName :: Ptr () -> CString -> Int -> IO Int32