diff --git a/Cabal/src/Distribution/Simple/GHC/BuildGeneric.hs b/Cabal/src/Distribution/Simple/GHC/BuildGeneric.hs index 7ff326aa9b3..330cc656d04 100644 --- a/Cabal/src/Distribution/Simple/GHC/BuildGeneric.hs +++ b/Cabal/src/Distribution/Simple/GHC/BuildGeneric.hs @@ -518,7 +518,7 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do } dynLinkerOpts = mempty - { ghcOptRPaths = rpaths + { ghcOptRPaths = rpaths <> toNubListR (extraLibDirs bnfo) , ghcOptInputFiles = toNubListR [tmpDir x | x <- cLikeObjs ++ cxxObjs ++ cmmObjs ++ asmObjs] diff --git a/Cabal/src/Distribution/Simple/GHC/BuildOrRepl.hs b/Cabal/src/Distribution/Simple/GHC/BuildOrRepl.hs index 8ae87642b51..64a8f0a6c40 100644 --- a/Cabal/src/Distribution/Simple/GHC/BuildOrRepl.hs +++ b/Cabal/src/Distribution/Simple/GHC/BuildOrRepl.hs @@ -474,7 +474,7 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do , ghcOptLinkFrameworks = toNubListR $ PD.frameworks libBi , ghcOptLinkFrameworkDirs = toNubListR $ PD.extraFrameworkDirs libBi - , ghcOptRPaths = rpaths + , ghcOptRPaths = rpaths <> toNubListR (extraLibDirs libBi) } ghcStaticLinkArgs = mempty diff --git a/Cabal/src/Distribution/Simple/Program/GHC.hs b/Cabal/src/Distribution/Simple/Program/GHC.hs index 537e008c17f..d94dd7c96f6 100644 --- a/Cabal/src/Distribution/Simple/Program/GHC.hs +++ b/Cabal/src/Distribution/Simple/Program/GHC.hs @@ -773,10 +773,7 @@ renderGhcOptions comp _platform@(Platform _arch os) opts else [] , ["-no-hs-main" | flagBool ghcOptLinkNoHsMain] , ["-dynload deploy" | not (null (flags ghcOptRPaths))] - , concat - [ ["-optl-Wl,-rpath," ++ dir] - | dir <- flags ghcOptRPaths - ] + , [ "-optl-Wl,-rpath," ++ dir | dir <- flags ghcOptRPaths ] , flags ghcOptLinkModDefFiles , ------------- -- Packages diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/T19350.script b/cabal-testsuite/PackageTests/LinkerOptions/T7339/T19350.script new file mode 100644 index 00000000000..d5b619f7bff --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/T19350.script @@ -0,0 +1,3 @@ +import Hello +hello +:q diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/clib/lib.c b/cabal-testsuite/PackageTests/LinkerOptions/T7339/clib/lib.c new file mode 100644 index 00000000000..556c1dcb748 --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/clib/lib.c @@ -0,0 +1,6 @@ +#include + +void hello_world(void) { + printf("hello world!"); +} + diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Hello.hs b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Hello.hs new file mode 100644 index 00000000000..0dd0de66f74 --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Hello.hs @@ -0,0 +1,3 @@ +module Hello (hello) where + +foreign import ccall "hello_world" hello :: IO () diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Setup.hs b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Setup.hs new file mode 100644 index 00000000000..9a994af677b --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/T7339.cabal b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/T7339.cabal new file mode 100644 index 00000000000..d4ea571a2c0 --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/lib/T7339.cabal @@ -0,0 +1,11 @@ +cabal-version: >=1.10 +name: T7339 +version: 1.0 +build-type: Simple + +library + build-depends: base + exposed-modules: Hello + default-language: Haskell2010 + extra-libraries: hello + diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.out b/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.out new file mode 100644 index 00000000000..45a71c209da --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.out @@ -0,0 +1,6 @@ +# Setup configure +# Setup build +Preprocessing library for T7339-1.0... +Building library for T7339-1.0... +# Setup register +Registering library for T7339-1.0... diff --git a/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.test.hs b/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.test.hs new file mode 100644 index 00000000000..ff9c1f60055 --- /dev/null +++ b/cabal-testsuite/PackageTests/LinkerOptions/T7339/setup.test.hs @@ -0,0 +1,63 @@ +-- Test for #19350, #7339 by @bgamari +-- ================================== +-- +-- The plan +-- --------- +-- We build a C shared library (`libhello`, contained in ./clib) and then build +-- a Haskell library (`T19350-lib`, in ./lib) which depends upon it via `foreign +-- import`. We make sure that the libhello shared object can only be found via +-- the extra-lib-dirs from the package database registration (which we do by +-- moving libhello.so from its original place). +-- +-- Finally, we enter GHCi, load the Haskell library, and try to use it to call +-- into libhello. + +import System.Directory +import Distribution.System +import Test.Cabal.Prelude + + +main = setupTest $ do + withPackageDb $ do + cwd <- takeDirectory . testCurrentDir <$> getTestEnv + plat <- testPlatform <$> getTestEnv + let libExt = case plat of + Platform _ OSX -> "dylib" + Platform _ Windows -> "dll" + Platform _ _other -> "so" + + + -- Link a C program against the library + _ <- runProgramM ghcProgram + [ "-fPIC", "-c", "clib/lib.c" + , "-o", "clib/lib.o" ] + Nothing + _ <- runProgramM ghcProgram + [ "-shared", "-no-hs-main", "clib/lib.o" + , "-o", "clib/libhello" <.> libExt ] + Nothing + + withDirectory "lib" $ do + setup "configure" ["-v0" + , "--extra-lib-dirs=" ++ (cwd "clib") + , "--extra-lib-dirs=" ++ (cwd "clib-install") + , "--disable-library-vanilla" + , "--enable-shared"] + setup "build" [] + setup "register" ["--inplace"] + + -- Move libhello from its original place to ensure it isn't found via RPATH + liftIO $ do + createDirectoryIfMissing False (cwd "clib-install") + copyFile (cwd "clib/libhello" <.> libExt) ( cwd "clib-install/libhello" <.> libExt) + removeFile (cwd "clib/libhello" <.> libExt) + + pkgDb <- testPackageDbDir <$> getTestEnv + ghciScript <- liftIO $ readFile (cwd "T19350.script") + _ <- runProgramM ghcProgram + [ "--interactive" + , "-package", "T7339" + , "-package-db", pkgDb + ] (Just ghciScript) + + return ()