(cabal check) Add "No internal name clash" test
[cabal.git] / cabal-testsuite / PackageTests / ForeignLibs / setup.test.hs
bloba7d8ad0a0f1b2e693e67bf43e3cb0ab8df170f99
1 {-# LANGUAGE RecordWildCards #-}
2 {-# LANGUAGE CPP #-}
4 import Control.Exception
5 import Control.Monad.IO.Class
6 import System.Environment
7 import System.FilePath
8 import System.IO.Error
9 #ifndef mingw32_HOST_OS
10 import System.Posix (readSymbolicLink)
11 #endif /* mingw32_HOST_OS */
13 import Distribution.Simple.LocalBuildInfo
14 import Distribution.Simple.Program.Db
15 import Distribution.Simple.Program.Builtin
16 import Distribution.Simple.Program.Types
17 import Distribution.System
18 import Distribution.Verbosity
19 import Distribution.Version
21 import Test.Cabal.Prelude
23 -- Test that foreign libraries work
24 -- Recording is turned off because versionedlib will or will not
25 -- be installed depending on if we're on Linux or not.
26 main = setupAndCabalTest . recordMode DoNotRecord $ do
27 -- Foreign libraries don't work with GHC 7.6 and earlier
28 skipUnlessGhcVersion ">= 7.8"
29 osx <- isOSX
30 ghc80 <- isGhcVersion "== 8.0.2"
31 expectBrokenIf (osx && ghc80) 7989 $
32 withPackageDb $ do
33 setup_install []
34 setup "copy" [] -- regression test #4156
35 dist_dir <- fmap testDistDir getTestEnv
36 lbi <- getLocalBuildInfoM
37 let installDirs = absoluteInstallDirs (localPkgDescr lbi) lbi NoCopyDest
39 -- Link a C program against the library
40 _ <- runProgramM gccProgram
41 [ "-std=c11", "-Wall"
42 , "-o", "uselib"
43 , "UseLib.c"
44 , "-l", "myforeignlib"
45 , "-L", flibdir installDirs ]
46 Nothing
48 -- Run the C program
49 let ldPath = case hostPlatform lbi of
50 Platform _ OSX -> "DYLD_LIBRARY_PATH"
51 Platform _ Windows -> "PATH"
52 Platform _ _other -> "LD_LIBRARY_PATH"
53 oldLdPath <- liftIO $ getEnv' ldPath
54 withEnv [ (ldPath, Just $ flibdir installDirs ++ [searchPathSeparator] ++ oldLdPath) ] $ do
55 cwd <- fmap testCurrentDir getTestEnv
56 result <- runM (cwd </> "uselib") [] Nothing
57 assertOutputContains "5678" result
58 assertOutputContains "189" result
60 -- If we're on Linux, we should have built a library with a
61 -- version. We will now check that it was installed correctly.
62 #ifndef mingw32_HOST_OS
63 case hostPlatform lbi of
64 Platform _ Linux -> do
65 let libraryName = "libversionedlib.so.5.4.3"
66 libdir = flibdir installDirs
67 objdumpProgram = simpleProgram "objdump"
68 (objdump, _) <- liftIO $ requireProgram normal objdumpProgram (withPrograms lbi)
69 path1 <- liftIO $ readSymbolicLink $ libdir </> "libversionedlib.so"
70 path2 <- liftIO $ readSymbolicLink $ libdir </> "libversionedlib.so.5"
71 assertEqual "Symbolic link 'libversionedlib.so' incorrect"
72 path1 libraryName
73 assertEqual "Symbolic link 'libversionedlib.so.5' incorrect"
74 path2 libraryName
75 objInfo <- runM (programPath objdump) [
76 "-x"
77 , libdir </> libraryName
78 ] Nothing
79 assertBool "SONAME of 'libversionedlib.so.5.4.3' incorrect" $
80 elem "libversionedlib.so.5" $ words $ resultOutput objInfo
81 _ -> return ()
82 #endif /* mingw32_HOST_OS */
84 getEnv' :: String -> IO String
85 getEnv' = handle handler . getEnv
86 where
87 handler e = if isDoesNotExistError e
88 then return ""
89 else throw e