Link all dynamic libraries with the correct install_name on Mac OS/X.
[ghc-hetmet.git] / libffi / ghc.mk
1 # -----------------------------------------------------------------------------
2 #
3 # (c) 2009 The University of Glasgow
4 #
5 # This file is part of the GHC build system.
6 #
7 # To understand how the build system works and how to modify it, see
8 #      http://hackage.haskell.org/trac/ghc/wiki/Building/Architecture
9 #      http://hackage.haskell.org/trac/ghc/wiki/Building/Modifying
10 #
11 # -----------------------------------------------------------------------------
12
13
14 # We package libffi as Haskell package for two reasons: 
15
16 # 1) GHC uses different names for shared and static libs, so it can
17 #    choose the lib variant to link with on its own. With regular
18 #    libtool styled shared lib names, the linker would interfer and
19 #    link against the shared lib variant even when GHC runs in -static
20 #    mode.
21 # 2) The first issue isn't a problem when a shared lib of libffi would
22 #    be installed in system locations, but we do not assume that. So,
23 #    when running in -dynamic mode, we must either install libffi to
24 #    system locations ourselves, or we must add its location to
25 #    respective environment variable, (DY)LD_LIBRARY_PATH etc...before
26 #    we call dynamically linked binaries. Especially, the latter is
27 #    necessary as GHC calls binary it produced before its installation
28 #    phase. However, both mechanism, installing to system locations or
29 #    modifying (DY)LD_LIBRARY_PATH, are already in place for Haskell
30 #    packages so with packaging libffi as Haskell package we reuse
31 #    them naturally.
32
33 # -----------------------------------------------------------------------------
34 #
35 # We use libffi's own configuration stuff.
36
37 PLATFORM := $(shell echo $(HOSTPLATFORM) | sed 's/i[567]86/i486/g')
38
39 # 2007-09-26
40 #     set -o igncr 
41 # is not a valid command on non-Cygwin-systems.
42 # Let it fail silently instead of aborting the build.
43 #
44 # 2007-07-05
45 # We do
46 #     set -o igncr; export SHELLOPTS
47 # here as otherwise checking the size of limbs
48 # makes the build fall over on Cygwin. See the thread
49 # http://www.cygwin.com/ml/cygwin/2006-12/msg00011.html
50 # for more details.
51
52 # 2007-07-05
53 # Passing
54 #     as_ln_s='cp -p'
55 # isn't sufficient to stop cygwin using symlinks the mingw gcc can't
56 # follow, as it isn't used consistently. Instead we put an ln.bat in
57 # path that always fails.
58
59 ifeq "$(BuildSharedLibs)" "YES"
60 libffi_STAMP_CONFIGURE = libffi/stamp.ffi.configure-shared
61 libffi_STAMP_BUILD     = libffi/stamp.ffi.build-shared
62 else
63 libffi_STAMP_CONFIGURE = libffi/stamp.ffi.configure
64 libffi_STAMP_BUILD     = libffi/stamp.ffi.build
65 endif
66
67 BINDIST_STAMPS = libffi/stamp.ffi.build libfii/stamp.ffi.configure
68
69 INSTALL_HEADERS   += libffi/dist-install/build/ffi.h
70 libffi_STATIC_LIB  = libffi/dist-install/build/libffi.a
71 INSTALL_LIBS      += libffi/dist-install/build/libHSffi.a \
72                      libffi/dist-install/build/libHSffi_p.a \
73                      libffi/dist-install/build/HSffi.o
74
75 # We have to add the GHC version to the name of our dynamic libs, because
76 # they will be residing in the system location along with dynamic libs from
77 # other GHC installations.
78
79 libffi_HS_DYN_LIB_NAME = libHSffi$(dyn_libsuf)
80 libffi_HS_DYN_LIB      = libffi/dist-install/build/$(libffi_HS_DYN_LIB_NAME)
81
82 ifeq "$(Windows)" "YES"
83 libffi_DYNAMIC_PROG = $(libffi_HS_DYN_LIB).a
84 libffi_DYNAMIC_LIBS = $(libffi_HS_DYN_LIB)
85 else
86 libffi_DYNAMIC_PROG =
87 ifeq "$(darwin_TARGET_OS)" "1"
88 libffi_DYNAMIC_LIBS = libffi/libffi$(soext) libffi/libffi.5$(soext) libffi/libffi.5.0.9$(soext)
89 else
90 libffi_DYNAMIC_LIBS = libffi/dist-install/build/libffi.so \
91                       libffi/dist-install/build/libffi.so.5 \
92                       libffi/dist-install/build/libffi.so.5.0.9
93 endif
94 endif
95
96 ifeq "$(BuildSharedLibs)" "YES"
97 libffi_EnableShared=yes
98 else
99 libffi_EnableShared=no
100 endif
101
102 ifeq "$(BuildSharedLibs)" "YES"
103 INSTALL_LIBS  += $(libffi_HS_DYN_LIB)
104 ifeq "$(Windows)" "YES"
105 INSTALL_PROGS += $(libffi_HS_DYN_LIB).a
106 endif
107 endif
108
109 # We have to fake a non-working ln for configure, so that the fallback
110 # option (cp -p) gets used instead.  Otherwise the libffi build system
111 # will use cygwin symbolic linkks which cannot be read by mingw gcc.
112 # The same trick is played by the GMP build in ../gmp.
113
114 ifneq "$(BINDIST)" "YES"
115 $(libffi_STAMP_CONFIGURE):
116         "$(RM)" $(RM_OPTS) -r $(LIBFFI_DIR) libffi/build
117         cd libffi && $(TAR) -zxf ../ghc-tarballs/libffi/libffi*.tar.gz
118         mv libffi/libffi-* libffi/build
119         chmod +x libffi/ln
120         cd libffi/build && "$(PATCH)" -p1 < ../libffi.dllize-3.0.8.patch
121         # This patch is just the resulting delta from running
122         # automake && autoreconf && libtoolize --force --copy
123         cd libffi/build && "$(PATCH)" -p1 < ../libffi.autotools-update-3.0.8.patch
124
125 # Because -Werror may be in SRC_CC_OPTS/SRC_LD_OPTS, we need to turn
126 # warnings off or the compilation of libffi might fail due to warnings
127         cd libffi && \
128           (set -o igncr 2>/dev/null) && set -o igncr; export SHELLOPTS; \
129             PATH=`pwd`:$$PATH; \
130             export PATH; \
131             cd build && \
132             CC=$(WhatGccIsCalled) \
133         CFLAGS="$(SRC_CC_OPTS) $(CONF_CC_OPTS) -w" \
134         LDFLAGS="$(SRC_LD_OPTS) $(CONF_LD_OPTS) -w" \
135         "$(SHELL)" configure \
136                   --enable-static=yes \
137                   --enable-shared=$(libffi_EnableShared) \
138                   --host=$(PLATFORM) --build=$(PLATFORM)
139
140         # libffi.so needs to be built with the correct soname.
141         # NOTE: this builds libffi_convience.so with the incorrect
142         # soname, but we don't need that anyway!
143         cd libffi && \
144           "$(CP)" build/libtool build/libtool.orig; \
145           sed -e s/soname_spec=.*/soname_spec="$(libffi_HS_DYN_LIB_NAME)"/ build/libtool.orig > build/libtool
146
147         # We don't want libtool's cygwin hacks
148         cd libffi && \
149           "$(CP)" build/libtool build/libtool.orig; \
150           sed -e s/dlname=\'\$$tdlname\'/dlname=\'\$$dlname\'/ build/libtool.orig > build/libtool
151
152         touch $@
153
154 libffi/dist-install/build/ffi.h: $(libffi_STAMP_CONFIGURE) | $$(dir $$@)/.
155         "$(CP)" libffi/build/include/ffi.h $@
156
157
158 $(libffi_STAMP_BUILD): $(libffi_STAMP_CONFIGURE) | libffi/dist-install/build/.
159         $(MAKE) -C libffi/build MAKEFLAGS=
160         cd libffi/build && ./libtool --mode=install cp libffi.la $(TOP)/libffi/dist-install/build
161
162         # We actually want both static and dllized libraries, because we build
163         #   the runtime system both ways. libffi_convenience.a is the static version.
164 ifeq "$(Windows)" "YES"
165         cp libffi/build/.libs/libffi_convenience.a $(libffi_STATIC_LIB)
166 endif
167
168         touch $@
169
170 $(libffi_STATIC_LIB): $(libffi_STAMP_BUILD)
171 # Rename libffi.a to libHSffi.a
172 libffi/dist-install/build/libHSffi.a libffi/dist-install/build/libHSffi_p.a: $(libffi_STATIC_LIB)
173         "$(CP)" $(libffi_STATIC_LIB) libffi/dist-install/build/libHSffi.a
174         "$(CP)" $(libffi_STATIC_LIB) libffi/dist-install/build/libHSffi_p.a
175
176 $(eval $(call all-target,libffi,$(INSTALL_HEADERS) $(INSTALL_LIBS)))
177
178 # The GHCi import lib isn't needed as compiler/ghci/Linker.lhs + rts/Linker.c
179 # link the interpreted references to FFI to the compiled FFI.
180 # Instead of adding libffi to the list preloaded packages (see
181 # compiler/ghci/Linker.lhs:emptyPLS) we generate an empty HSffi.o
182
183 libffi/dist-install/build/HSffi.o: libffi/dist-install/build/libHSffi.a
184         cd libffi/dist-install/build && \
185           touch empty.c && \
186           "$(CC)" $(SRC_CC_OPTS) $(CONF_CC_OPTS) -c empty.c -o HSffi.o
187
188 $(eval $(call all-target,libffi,libffi/dist-install/build/HSffi.o))
189
190 ifeq "$(BuildSharedLibs)" "YES"
191 ifeq "$(Windows)" "YES"
192 libffi/libffi.dll.a $(libffi_HS_DYN_LIB): $(libffi_STAMP_BUILD)
193 # Windows libtool creates <soname>.dll, and as we already patched that
194 # there is no need to copy from libffi.dll to libHSffi...dll.
195 # However, the renaming is still required for the import library
196 # libffi.dll.a.
197 $(libffi_HS_DYN_LIB).a: libffi/dist-install/build/libffi.dll.a | $$(dir $$@)/.
198         "$(CP)" $< $@
199
200 $(eval $(call all-target,libffi,$(libffi_HS_DYN_LIB).a))
201
202 else
203 $(libffi_DYNAMIC_LIBS): $(libffi_STAMP_BUILD)
204 # Rename libffi.so to libHSffi...so
205 $(libffi_HS_DYN_LIB): $(libffi_DYNAMIC_LIBS) | $$(dir $$@)/.
206         "$(CP)" $(word 1,$(libffi_DYNAMIC_LIBS)) $(libffi_HS_DYN_LIB)
207 ifeq "$(darwin_TARGET_OS)" "1"
208        # Ensure library's install name is correct before anyone links with it.
209        install_name_tool -id $(ghclibdir)/$(libffi_HS_DYN_LIB_NAME) $(libffi_HS_DYN_LIB)
210 endif
211
212 $(eval $(call all-target,libffi,$(libffi_HS_DYN_LIB)))
213 endif
214 endif
215
216 $(eval $(call clean-target,libffi,, \
217    libffi/build libffi/stamp.ffi.* libffi/dist-install))
218 endif
219
220 #-----------------------------------------------------------------------------
221 # Do the package config
222
223 $(eval $(call manual-package-config,libffi))
224
225 #-----------------------------------------------------------------------------
226 #
227 # binary-dist
228
229 BINDIST_EXTRAS += libffi/package.conf.in
230