+
+----------------------------------------------------------------------
+-- haskeline-specific hacks
+
+-- Sigh, haskeline proper uses stuff in Setup.hs to handle whether
+-- or not -liconv is used. We don't use Setup.hs, so we replicate
+-- what it does here. We should do this better somehow.
+
+-- Test whether compiling a c program that links against libiconv needs -liconv.
+maybeSetLibiconv :: Verbosity -> BuildInfo -> LocalBuildInfo -> IO ()
+maybeSetLibiconv verb bi lbi = do
+ let biWithIconv = addIconv bi
+ worksWithout <- tryCompile iconv_prog bi lbi verb
+ if worksWithout
+ then writeBuildInfo ""
+ else do
+ worksWith <- tryCompile iconv_prog biWithIconv lbi verb
+ if worksWith
+ then do
+ writeBuildInfo "iconv"
+ else fail "Unable to link against the iconv library."
+ where
+ -- Cabal (at least 1.6.0.1) won't parse an empty buildinfo file.
+ writeBuildInfo libs = writeFile "haskeline.buildinfo"
+ $ unlines ["extra-libraries: " ++ libs]
+
+tryCompile :: String -> BuildInfo -> LocalBuildInfo -> Verbosity -> IO Bool
+tryCompile program bi lbi verb = flip catchIO processException $ flip catchExit processExit $ do
+ tempDir <- getTemporaryDirectory
+ withTempFile tempDir ".c" $ \fname h -> do
+ hPutStr h program
+ hClose h
+ -- TODO take verbosity from the args.
+ rawSystemProgramStdoutConf verb gccProgram (withPrograms lbi) (fname : args)
+ return True
+ where
+ processException :: IOException -> IO Bool
+ processException _ = return False
+ processExit = return . (==ExitSuccess)
+ -- Mimicing Distribution.Simple.Configure
+ deps = topologicalOrder (installedPkgs lbi)
+ args = concat
+ [ ccOptions bi
+ , cppOptions bi
+ , ldOptions bi
+ -- --extra-include-dirs and --extra-lib-dirs are included
+ -- in the below fields.
+ -- Also sometimes a dependency like rts points to a nonstandard
+ -- include/lib directory where iconv can be found.
+ , map ("-I" ++) (includeDirs bi ++ concatMap Installed.includeDirs deps)
+ , map ("-L" ++) (extraLibDirs bi ++ concatMap Installed.libraryDirs deps)
+ , map ("-l" ++) (extraLibs bi)
+ ]
+
+addIconv :: BuildInfo -> BuildInfo
+addIconv bi = bi {extraLibs = "iconv" : extraLibs bi}
+
+iconv_prog :: String
+iconv_prog = unlines $
+ [ "#include <iconv.h>"
+ , "int main(void) {"
+ , " iconv_t t = iconv_open(\"UTF-8\", \"UTF-8\");"
+ , " return 0;"
+ , "}"
+ ]
+