Commit f7be53ac authored by Christiaan Baaij's avatar Christiaan Baaij Committed by Austin Seipp

Fix inplace dynamic linking on OS X (#8266)

Signed-off-by: default avatarAustin Seipp <austin@well-typed.com>
parent 943f22a4
...@@ -1785,6 +1785,15 @@ linkBinary' staticLink dflags o_files dep_packages = do ...@@ -1785,6 +1785,15 @@ linkBinary' staticLink dflags o_files dep_packages = do
then [] then []
else ["-Wl,-rpath-link", "-Wl," ++ l] else ["-Wl,-rpath-link", "-Wl," ++ l]
in ["-L" ++ l] ++ rpathlink ++ rpath in ["-L" ++ l] ++ rpathlink ++ rpath
| osMachOTarget (platformOS platform) &&
dynLibLoader dflags == SystemDependent &&
not (gopt Opt_Static dflags) &&
gopt Opt_RPath dflags
= let libpath = if gopt Opt_RelativeDynlibPaths dflags
then "@loader_path" </>
(l `makeRelativeTo` full_output_fn)
else l
in ["-L" ++ l] ++ ["-Wl,-rpath", "-Wl," ++ libpath]
| otherwise = ["-L" ++ l] | otherwise = ["-L" ++ l]
let lib_paths = libraryPaths dflags let lib_paths = libraryPaths dflags
...@@ -1920,13 +1929,6 @@ linkBinary' staticLink dflags o_files dep_packages = do ...@@ -1920,13 +1929,6 @@ linkBinary' staticLink dflags o_files dep_packages = do
then ["-Wl,-read_only_relocs,suppress"] then ["-Wl,-read_only_relocs,suppress"]
else []) else [])
++ (if platformOS platform == OSDarwin &&
not staticLink &&
not (gopt Opt_Static dflags) &&
gopt Opt_RPath dflags
then ["-Wl,-rpath","-Wl," ++ topDir dflags]
else [])
++ o_files ++ o_files
++ lib_path_opts) ++ lib_path_opts)
++ extra_ld_inputs ++ extra_ld_inputs
......
...@@ -1293,7 +1293,8 @@ linkDynLib dflags0 o_files dep_packages ...@@ -1293,7 +1293,8 @@ linkDynLib dflags0 o_files dep_packages
let pkg_lib_paths = collectLibraryPaths pkgs let pkg_lib_paths = collectLibraryPaths pkgs
let pkg_lib_path_opts = concatMap get_pkg_lib_path_opts pkg_lib_paths let pkg_lib_path_opts = concatMap get_pkg_lib_path_opts pkg_lib_paths
get_pkg_lib_path_opts l get_pkg_lib_path_opts l
| osElfTarget (platformOS (targetPlatform dflags)) && | ( osElfTarget (platformOS (targetPlatform dflags)) ||
osMachOTarget (platformOS (targetPlatform dflags)) ) &&
dynLibLoader dflags == SystemDependent && dynLibLoader dflags == SystemDependent &&
not (gopt Opt_Static dflags) not (gopt Opt_Static dflags)
= ["-L" ++ l, "-Wl,-rpath", "-Wl," ++ l] = ["-L" ++ l, "-Wl,-rpath", "-Wl," ++ l]
...@@ -1390,9 +1391,7 @@ linkDynLib dflags0 o_files dep_packages ...@@ -1390,9 +1391,7 @@ linkDynLib dflags0 o_files dep_packages
instName <- case dylibInstallName dflags of instName <- case dylibInstallName dflags of
Just n -> return n Just n -> return n
Nothing -> do Nothing -> return $ "@rpath" `combine` (takeFileName output_fn)
pwd <- getCurrentDirectory
return $ pwd `combine` output_fn
runLink dflags ( runLink dflags (
map Option verbFlags map Option verbFlags
++ [ Option "-dynamiclib" ++ [ Option "-dynamiclib"
......
...@@ -12,6 +12,7 @@ module Platform ( ...@@ -12,6 +12,7 @@ module Platform (
target32Bit, target32Bit,
isARM, isARM,
osElfTarget, osElfTarget,
osMachOTarget,
platformUsesFrameworks, platformUsesFrameworks,
platformBinariesAreStaticLibs, platformBinariesAreStaticLibs,
) )
...@@ -129,6 +130,11 @@ osElfTarget OSUnknown = False ...@@ -129,6 +130,11 @@ osElfTarget OSUnknown = False
-- portability, otherwise we have to answer this question for every -- portability, otherwise we have to answer this question for every
-- new platform we compile on (even unreg). -- new platform we compile on (even unreg).
-- | This predicate tells us whether the OS support Mach-O shared libraries.
osMachOTarget :: OS -> Bool
osMachOTarget OSDarwin = True
osMachOTarget _ = False
osUsesFrameworks :: OS -> Bool osUsesFrameworks :: OS -> Bool
osUsesFrameworks OSDarwin = True osUsesFrameworks OSDarwin = True
osUsesFrameworks OSiOS = True osUsesFrameworks OSiOS = True
......
...@@ -282,11 +282,6 @@ include rules/dependencies.mk ...@@ -282,11 +282,6 @@ include rules/dependencies.mk
include rules/build-dependencies.mk include rules/build-dependencies.mk
include rules/include-dependencies.mk include rules/include-dependencies.mk
# -----------------------------------------------------------------------------
# Dynamic library references
include rules/relative-dynlib-references.mk
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Build package-data.mk files # Build package-data.mk files
......
...@@ -117,7 +117,7 @@ else ...@@ -117,7 +117,7 @@ else
rts/dist/build/lib$(LIBFFI_NAME)$(soext): libffi/build/inst/lib/lib$(LIBFFI_NAME)$(soext) rts/dist/build/lib$(LIBFFI_NAME)$(soext): libffi/build/inst/lib/lib$(LIBFFI_NAME)$(soext)
cp libffi/build/inst/lib/lib$(LIBFFI_NAME)$(soext)* rts/dist/build cp libffi/build/inst/lib/lib$(LIBFFI_NAME)$(soext)* rts/dist/build
ifeq "$(TargetOS_CPP)" "darwin" ifeq "$(TargetOS_CPP)" "darwin"
install_name_tool -id @rpath/rts-$(rts_VERSION)/lib$(LIBFFI_NAME)$(soext) rts/dist/build/lib$(LIBFFI_NAME)$(soext) install_name_tool -id @rpath/lib$(LIBFFI_NAME)$(soext) rts/dist/build/lib$(LIBFFI_NAME)$(soext)
endif endif
endif endif
endif endif
...@@ -197,6 +197,9 @@ LIBFFI_LIBS = -Lrts/dist/build -l$$(LIBFFI_NAME) ...@@ -197,6 +197,9 @@ LIBFFI_LIBS = -Lrts/dist/build -l$$(LIBFFI_NAME)
ifeq "$$(TargetElf)" "YES" ifeq "$$(TargetElf)" "YES"
LIBFFI_LIBS += -optl-Wl,-rpath -optl-Wl,'$$$$ORIGIN' -optl-Wl,-zorigin LIBFFI_LIBS += -optl-Wl,-rpath -optl-Wl,'$$$$ORIGIN' -optl-Wl,-zorigin
endif endif
ifeq "$(TargetOS_CPP)" "darwin"
LIBFFI_LIBS += -optl-Wl,-rpath -optl-Wl,@loader_path
endif
else else
# flags will be taken care of in rts/dist/libs.depend # flags will be taken care of in rts/dist/libs.depend
...@@ -207,7 +210,6 @@ $$(rts_$1_LIB) : $$(rts_$1_OBJS) $$(rts_$1_DTRACE_OBJS) rts/dist/libs.depend $$( ...@@ -207,7 +210,6 @@ $$(rts_$1_LIB) : $$(rts_$1_OBJS) $$(rts_$1_DTRACE_OBJS) rts/dist/libs.depend $$(
"$$(rts_dist_HC)" -package-name rts -shared -dynamic -dynload deploy \ "$$(rts_dist_HC)" -package-name rts -shared -dynamic -dynload deploy \
-no-auto-link-packages $$(LIBFFI_LIBS) `cat rts/dist/libs.depend` $$(rts_$1_OBJS) \ -no-auto-link-packages $$(LIBFFI_LIBS) `cat rts/dist/libs.depend` $$(rts_$1_OBJS) \
$$(rts_$1_DTRACE_OBJS) -o $$@ $$(rts_$1_DTRACE_OBJS) -o $$@
$(call relative-dynlib-references,rts,dist,1,$1)
endif endif
else else
$$(rts_$1_LIB) : $$(rts_$1_OBJS) $$(rts_$1_DTRACE_OBJS) $$(rts_$1_LIB) : $$(rts_$1_OBJS) $$(rts_$1_DTRACE_OBJS)
......
...@@ -91,7 +91,6 @@ $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS) ...@@ -91,7 +91,6 @@ $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
$$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \ $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
-no-auto-link-packages \ -no-auto-link-packages \
-o $$@ -o $$@
$(call relative-dynlib-references,$1,$2,$4)
endif endif
else else
# Build the ordinary .a library # Build the ordinary .a library
......
...@@ -261,10 +261,6 @@ ifeq "$$($1_$2_LINK_WITH_GCC)" "NO" ...@@ -261,10 +261,6 @@ ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
$1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/. $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
$$(call cmd,$1_$2_HC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_GHC_LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(call cmd,$1_$2_HC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_GHC_LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
ifeq "$$($1_$2_PROGRAM_WAY)" "dyn"
$(call relative-dynlib-references,$1,$2,$3)
$(call relative-dynlib-path,$3)
endif
else else
$1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/. $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
$$(call cmd,$1_$2_CC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_CC_OPTS) $$(LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_EXTRA_CC_OPTS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(call cmd,$1_$2_CC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_CC_OPTS) $$(LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_EXTRA_CC_OPTS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
......
...@@ -138,7 +138,9 @@ $1_$2_$3_GHC_LD_OPTS += \ ...@@ -138,7 +138,9 @@ $1_$2_$3_GHC_LD_OPTS += \
-fno-use-rpaths \ -fno-use-rpaths \
$$(foreach d,$$($1_$2_TRANSITIVE_DEPS),-optl-Wl$$(comma)-rpath -optl-Wl$$(comma)'$$$$ORIGIN/../$$d') -optl-Wl,-zorigin $$(foreach d,$$($1_$2_TRANSITIVE_DEPS),-optl-Wl$$(comma)-rpath -optl-Wl$$(comma)'$$$$ORIGIN/../$$d') -optl-Wl,-zorigin
else ifeq "$$(TargetOS_CPP)" "darwin" else ifeq "$$(TargetOS_CPP)" "darwin"
$1_$2_$3_GHC_LD_OPTS += -optl-Wl,-headerpad_max_install_names $1_$2_$3_GHC_LD_OPTS += \
-fno-use-rpaths \
$$(foreach d,$$($1_$2_TRANSITIVE_DEPS),-optl-Wl$$(comma)-rpath -optl-Wl$$(comma)'@loader_path/../$$d')
endif endif
endif endif
endif endif
......
# -----------------------------------------------------------------------------
#
# (c) 2009 The University of Glasgow
#
# This file is part of the GHC build system.
#
# To understand how the build system works and how to modify it, see
# http://ghc.haskell.org/trac/ghc/wiki/Building/Architecture
# http://ghc.haskell.org/trac/ghc/wiki/Building/Modifying
#
# -----------------------------------------------------------------------------
# Make dynlib references use relative paths, so that everything works
# without the build tree.
define relative-dynlib-references
# $1 = dir
# $2 = distdir
# $3 = GHC stage to use (0 == bootstrapping compiler)
# $4 = RTSway
ifeq "$$(TargetOS_CPP)" "darwin"
ifneq "$3" "0"
# Use relative paths for all the libraries
ifneq "$$($1_$2_TRANSITIVE_DEP_NAMES)" ""
install_name_tool $$(foreach d,$$($1_$2_TRANSITIVE_DEP_NAMES), -change $$(TOP)/$$($$($$d_INSTALL_INFO)_dyn_LIB) @rpath/$$d-$$($$($$d_INSTALL_INFO)_VERSION)/$$($$($$d_INSTALL_INFO)_dyn_LIB_NAME)) $$@
endif
# Change absolute library name/path to a relative name/path
ifeq "$$($1_$2_PROGNAME)" ""
ifeq "$1" "rts"
install_name_tool -id @rpath/rts-$$(rts_VERSION)/$$(rts_$4_LIB_NAME) $$@
else
install_name_tool -id @rpath/$$($1_PACKAGE)-$$($1_$2_VERSION)/$$($1_$2_dyn_LIB_NAME) $$@
endif
endif
# Use relative paths for the RTS. Rather than try to work out which RTS
# way is being linked, we just change it for all ways
install_name_tool $$(foreach w,$$(rts_WAYS), -change $$(TOP)/$$(rts_$$w_LIB) @rpath/rts-$$(rts_VERSION)/$$(rts_$$w_LIB_NAME)) $$@
install_name_tool -change $$(TOP)/$$(wildcard libffi/build/inst/lib/libffi.*.dylib) @rpath/rts-$$(rts_VERSION)/libffi.dylib $$@
endif
endif
endef
define relative-dynlib-path
# $1 = GHC stage to use (0 == bootstrapping compiler)
ifeq "$$(TargetOS_CPP)" "darwin"
ifneq "$1" "0"
install_name_tool -rpath $$(TOP)/inplace/lib @loader_path/.. $$@
endif
endif
endef
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment