+-- Sanity-check a new package config, and automatically build GHCi libs
+-- if requested.
+
+validatePackageConfig :: PackageConfig
+ -> [[PackageConfig]]
+ -> Bool
+ -> Bool
+ -> Bool
+ -> IO [PackageConfig]
+validatePackageConfig pkg pkg_confs@(pkgs:_) auto_ghci_libs updatePkg force = do
+ when (not updatePkg && (name pkg `elem` map name pkgs))
+ (die ("package `" ++ name pkg ++ "' is already installed\n"))
+ mapM_ (checkDep pkg_confs force) (package_deps pkg)
+ mapM_ (checkDir force) (import_dirs pkg)
+ mapM_ (checkDir force) (source_dirs pkg)
+ mapM_ (checkDir force) (library_dirs pkg)
+ mapM_ (checkDir force) (include_dirs pkg)
+ mapM_ (checkHSLib (library_dirs pkg) auto_ghci_libs force) (hs_libraries pkg)
+ -- ToDo: check these somehow?
+ -- extra_libraries :: [String],
+ -- c_includes :: [String],
+ let existing_pkgs
+ | updatePkg = filter ((/=(name pkg)).name) pkgs
+ | otherwise = pkgs
+ return (existing_pkgs ++ [pkg])
+
+checkDir :: Bool -> String -> IO ()
+checkDir force d
+ | "$libdir" `isPrefixOf` d = return ()
+ -- can't check this, because we don't know what $libdir is
+ | otherwise = do
+ there <- doesDirectoryExist d
+ when (not there)
+ (dieOrForce force ("`" ++ d ++ "' doesn't exist or isn't a directory\n"))
+
+checkDep :: [[PackageConfig]] -> Bool -> String -> IO ()
+checkDep pkgs force n
+ | n `elem` pkg_names = return ()
+ | otherwise = dieOrForce force ("dependency `" ++ n ++ "' doesn't exist\n")
+ where
+ pkg_names = concat (map (map name) pkgs)
+
+checkHSLib :: [String] -> Bool -> Bool -> String -> IO ()
+checkHSLib dirs auto_ghci_libs force lib = do
+ let batch_lib_file = "lib" ++ lib ++ ".a"
+ bs <- mapM (doesLibExistIn batch_lib_file) dirs
+ case [ dir | (exists,dir) <- zip bs dirs, exists ] of
+ [] -> dieOrForce force ("cannot find `" ++ batch_lib_file ++
+ "' on library path")
+ (dir:_) -> checkGHCiLib dirs dir batch_lib_file lib auto_ghci_libs
+
+doesLibExistIn :: String -> String -> IO Bool
+doesLibExistIn lib d
+ | "$libdir" `isPrefixOf` d = return True
+ | otherwise = doesFileExist (d ++ '/':lib)
+
+checkGHCiLib :: [String] -> String -> String -> String -> Bool -> IO ()
+checkGHCiLib dirs batch_lib_dir batch_lib_file lib auto_build
+ | auto_build = autoBuildGHCiLib batch_lib_dir batch_lib_file ghci_lib_file
+ | otherwise = do
+ bs <- mapM (doesLibExistIn ghci_lib_file) dirs
+ case [dir | (exists,dir) <- zip bs dirs, exists] of
+ [] -> hPutStrLn stderr ("warning: can't find GHCi lib `" ++ ghci_lib_file ++ "'")
+ (_:_) -> return ()
+ where
+ ghci_lib_file = lib ++ ".o"
+
+-- automatically build the GHCi version of a batch lib,
+-- using ld --whole-archive.
+
+autoBuildGHCiLib :: String -> String -> String -> IO ()
+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
+#ifdef mingw32_HOST_OS
+ execDir <- getExecDir "/bin/ghc-pkg.exe"
+ system (maybe "" (++"/gcc-lib/") execDir++"ld -r -x -o " ++ ghci_lib_file ++
+ " --whole-archive " ++ batch_lib_file)
+#else
+ system("ld -r -x -o " ++ ghci_lib_file ++
+ " --whole-archive " ++ batch_lib_file)
+#endif
+#endif
+ hPutStrLn stderr (" done.")
+
+-----------------------------------------------------------------------------
+expandEnvVars :: PackageConfig -> [(String, String)] -> Bool -> IO PackageConfig
+expandEnvVars pkg defines force = do
+ -- permit _all_ strings to contain ${..} environment variable references,
+ -- arguably too flexible.
+ nm <- expandString (name pkg)
+ imp_dirs <- expandStrings (import_dirs pkg)
+ src_dirs <- expandStrings (source_dirs pkg)
+ lib_dirs <- expandStrings (library_dirs pkg)
+ hs_libs <- expandStrings (hs_libraries pkg)
+ ex_libs <- expandStrings (extra_libraries pkg)
+ inc_dirs <- expandStrings (include_dirs pkg)
+ c_incs <- expandStrings (c_includes pkg)
+ p_deps <- expandStrings (package_deps pkg)
+ 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
+ , library_dirs = lib_dirs
+ , hs_libraries = hs_libs
+ , extra_libraries = ex_libs
+ , include_dirs = inc_dirs
+ , c_includes = c_incs
+ , package_deps = p_deps
+ , 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 :: [String] -> IO [String]
+ expandStrings = liftM concat . mapM expandSpecial
+
+ -- Permit substitutions for list-valued variables (but only when
+ -- they occur alone), e.g., package_deps["${deps}"] where env var
+ -- (say) 'deps' is "base,haskell98,network"
+ expandSpecial :: String -> IO [String]
+ expandSpecial str =
+ let expand f = liftM f $ expandString str
+ in case splitString str of
+ [Var _] -> expand (wordsBy (== ','))
+ _ -> expand (\x -> [x])
+
+ expandString :: String -> IO String
+ expandString = liftM concat . mapM expandElem . splitString
+
+ expandElem :: Elem -> IO String
+ expandElem (String s) = return s
+ expandElem (Var v) = lookupEnvVar v
+
+ lookupEnvVar :: String -> IO String
+ lookupEnvVar nm =
+ case lookup nm defines of
+ Just x | not (null x) -> return x
+ _ ->
+ catch (System.getEnv nm)
+ (\ _ -> do dieOrForce force ("Unable to expand variable " ++
+ show nm)
+ return "")
+
+data Elem = String String | Var String
+
+splitString :: String -> [Elem]
+splitString "" = []
+splitString str =
+ case break (== '$') str of
+ (pre, _:'{':xs) ->
+ case span (/= '}') xs of
+ (var, _:suf) ->
+ (if null pre then id else (String pre :)) (Var var : splitString suf)
+ _ -> [String str] -- no closing brace
+ _ -> [String str] -- no dollar/opening brace combo
+
+-- wordsBy isSpace == words
+wordsBy :: (Char -> Bool) -> String -> [String]
+wordsBy p s = case dropWhile p s of
+ "" -> []
+ s' -> w : wordsBy p s'' where (w,s'') = break p s'
+
+-----------------------------------------------------------------------------
+
+getProgramName :: IO String
+getProgramName = liftM (`withoutSuffix` ".bin") getProgName
+ where str `withoutSuffix` suff
+ | suff `isSuffixOf` str = take (length str - length suff) str
+ | otherwise = str
+
+bye :: String -> IO a
+bye s = putStr s >> exitWith ExitSuccess