Skip to content

Commit f2f9a03

Browse files
authored
Merge pull request #9554 from alt-romes/wip/romes/7339
Add extraLibDirs to runtime lib search paths of library
2 parents 1a8b93c + addbcbf commit f2f9a03

File tree

10 files changed

+101
-6
lines changed

10 files changed

+101
-6
lines changed

Cabal/src/Distribution/Simple/GHC/BuildGeneric.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do
518518
}
519519
dynLinkerOpts =
520520
mempty
521-
{ ghcOptRPaths = rpaths
521+
{ ghcOptRPaths = rpaths <> toNubListR (extraLibDirs bnfo)
522522
, ghcOptInputFiles =
523523
toNubListR
524524
[tmpDir </> x | x <- cLikeObjs ++ cxxObjs ++ cmmObjs ++ asmObjs]

Cabal/src/Distribution/Simple/GHC/BuildOrRepl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
474474
, ghcOptLinkFrameworks = toNubListR $ PD.frameworks libBi
475475
, ghcOptLinkFrameworkDirs =
476476
toNubListR $ PD.extraFrameworkDirs libBi
477-
, ghcOptRPaths = rpaths
477+
, ghcOptRPaths = rpaths <> toNubListR (extraLibDirs libBi)
478478
}
479479
ghcStaticLinkArgs =
480480
mempty

Cabal/src/Distribution/Simple/Program/GHC.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,7 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
778778
else []
779779
, ["-no-hs-main" | flagBool ghcOptLinkNoHsMain]
780780
, ["-dynload deploy" | not (null (flags ghcOptRPaths))]
781-
, concat
782-
[ ["-optl-Wl,-rpath," ++ dir]
783-
| dir <- flags ghcOptRPaths
784-
]
781+
, ["-optl-Wl,-rpath," ++ dir | dir <- flags ghcOptRPaths]
785782
, flags ghcOptLinkModDefFiles
786783
, -------------
787784
-- Packages
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Hello
2+
hello
3+
:q
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
void hello_world(void) {
4+
printf("hello world!");
5+
}
6+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Hello (hello) where
2+
3+
foreign import ccall "hello_world" hello :: IO ()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cabal-version: >=1.10
2+
name: T7339
3+
version: 1.0
4+
build-type: Simple
5+
6+
library
7+
build-depends: base
8+
exposed-modules: Hello
9+
default-language: Haskell2010
10+
extra-libraries: hello
11+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Setup configure
2+
# Setup build
3+
Preprocessing library for T7339-1.0...
4+
Building library for T7339-1.0...
5+
# Setup register
6+
Registering library for T7339-1.0...
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- Test for #19350, #7339 originally by @bgamari
2+
-- =============================================
3+
--
4+
-- The plan
5+
-- ---------
6+
-- We build a C shared library (`libhello`, contained in ./clib) and then build
7+
-- a Haskell library (`T19350-lib`, in ./lib) which depends upon it via `foreign
8+
-- import`. We make sure that the libhello shared object can only be found via
9+
-- the extra-lib-dirs from the package database registration (which we do by
10+
-- moving libhello.so from its original place).
11+
--
12+
-- Finally, we enter GHCi, load the Haskell library, and try to use it to call
13+
-- into libhello.
14+
15+
import System.Directory
16+
import Distribution.System
17+
import Test.Cabal.Prelude
18+
19+
20+
main = setupTest $ do
21+
22+
skipIfWindows
23+
skipUnlessGhcVersion ">= 8.4"
24+
25+
withPackageDb $ do
26+
cwd <- takeDirectory . testCurrentDir <$> getTestEnv
27+
plat <- testPlatform <$> getTestEnv
28+
let libExt = case plat of
29+
Platform _ OSX -> "dylib"
30+
Platform _ Windows -> "dll"
31+
Platform _ _other -> "so"
32+
33+
34+
-- Link a C program against the library
35+
_ <- runProgramM ghcProgram
36+
[ "-fPIC", "-c", "clib/lib.c"
37+
, "-o", "clib/lib.o" ]
38+
Nothing
39+
_ <- runProgramM ghcProgram
40+
[ "-shared", "-no-hs-main", "clib/lib.o"
41+
, "-o", "clib/libhello" <.> libExt ]
42+
Nothing
43+
44+
withDirectory "lib" $ do
45+
setup "configure" ["-v0"
46+
, "--extra-lib-dirs=" ++ (cwd </> "clib")
47+
, "--extra-lib-dirs=" ++ (cwd </> "clib-install")
48+
, "--disable-library-vanilla"
49+
, "--enable-shared"]
50+
setup "build" []
51+
setup "register" ["--inplace"]
52+
53+
-- Move libhello from its original place to ensure it isn't found via RPATH
54+
liftIO $ do
55+
createDirectoryIfMissing False (cwd </> "clib-install")
56+
copyFile (cwd </> "clib/libhello" <.> libExt) ( cwd </> "clib-install/libhello" <.> libExt)
57+
removeFile (cwd </> "clib/libhello" <.> libExt)
58+
59+
pkgDb <- testPackageDbDir <$> getTestEnv
60+
ghciScript <- liftIO $ readFile (cwd </> "T19350.script")
61+
_ <- runProgramM ghcProgram
62+
[ "--interactive"
63+
, "-package", "T7339"
64+
, "-package-db", pkgDb
65+
] (Just ghciScript)
66+
67+
return ()

0 commit comments

Comments
 (0)