Improve online docs for `includes:` field
[cabal.git] / cabal-testsuite / PackageTests / ForeignLibs / setup.test.hs
blob1dcf918eaed36338f2d2d0b4fcdba44025fecd9e
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
20 import System.Directory
22 import Test.Cabal.Prelude
24 -- Test that foreign libraries work
25 -- Recording is turned off because versionedlib will or will not
26 -- be installed depending on if we're on Linux or not.
27 main = setupAndCabalTest . recordMode DoNotRecord $ do
28 -- Foreign libraries don't work with GHC 7.6 and earlier
29 skipUnlessGhcVersion ">= 7.8"
30 win <- isWindows
31 ghc94 <- isGhcVersion ">= 9.4.1"
32 expectBrokenIf (win && ghc94) 8451 $
33 withPackageDb $ do
34 setup_install []
35 setup "copy" [] -- regression test #4156
36 dist_dir <- fmap testDistDir getTestEnv
37 lbi <- getLocalBuildInfoM
38 let installDirs = absoluteInstallDirs (localPkgDescr lbi) lbi NoCopyDest
40 -- Link a C program against the library
41 _ <- runProgramM gccProgram
42 [ "-std=c11", "-Wall"
43 , "-o", "uselib"
44 , "UseLib.c"
45 , "-l", "myforeignlib"
46 , "-L", flibdir installDirs ]
47 Nothing
49 -- Run the C program
50 let ldPath = case hostPlatform lbi of
51 Platform _ OSX -> "DYLD_LIBRARY_PATH"
52 Platform _ Windows -> "PATH"
53 Platform _ _other -> "LD_LIBRARY_PATH"
54 oldLdPath <- liftIO $ getEnv' ldPath
55 withEnv [ (ldPath, Just $ flibdir installDirs ++ [searchPathSeparator] ++ oldLdPath) ] $ do
56 cwd <- fmap testCurrentDir getTestEnv
57 result <- runM (cwd </> "uselib") [] Nothing
58 assertOutputContains "5678" result
59 assertOutputContains "189" result
61 -- If we're on Linux, we should have built a library with a
62 -- version. We will now check that it was installed correctly.
63 #ifndef mingw32_HOST_OS
64 case hostPlatform lbi of
65 Platform _ Linux -> do
66 let libraryName = "libversionedlib.so.5.4.3"
67 libdir = flibdir installDirs
68 objdumpProgram = simpleProgram "objdump"
69 (objdump, _) <- liftIO $ requireProgram normal objdumpProgram (withPrograms lbi)
70 path1 <- liftIO $ readSymbolicLink $ libdir </> "libversionedlib.so"
71 path2 <- liftIO $ readSymbolicLink $ libdir </> "libversionedlib.so.5"
72 assertEqual "Symbolic link 'libversionedlib.so' incorrect"
73 path1 libraryName
74 assertEqual "Symbolic link 'libversionedlib.so.5' incorrect"
75 path2 libraryName
76 objInfo <- runM (programPath objdump) [
77 "-x"
78 , libdir </> libraryName
79 ] Nothing
80 assertBool "SONAME of 'libversionedlib.so.5.4.3' incorrect" $
81 elem "libversionedlib.so.5" $ words $ resultOutput objInfo
82 _ -> return ()
83 #endif /* mingw32_HOST_OS */
85 getEnv' :: String -> IO String
86 getEnv' = handle handler . getEnv
87 where
88 handler e = if isDoesNotExistError e
89 then return ""
90 else throw e