Wibble to InstEnv.instanceHead
[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-07-05
40 # Passing
41 #     as_ln_s='cp -p'
42 # isn't sufficient to stop cygwin using symlinks the mingw gcc can't
43 # follow, as it isn't used consistently. Instead we put an ln.bat in
44 # path that always fails.
45
46 ifeq "$(BuildSharedLibs)" "YES"
47 libffi_STAMP_CONFIGURE = libffi/stamp.ffi.configure-shared
48 libffi_STAMP_BUILD     = libffi/stamp.ffi.build-shared
49 else
50 libffi_STAMP_CONFIGURE = libffi/stamp.ffi.configure
51 libffi_STAMP_BUILD     = libffi/stamp.ffi.build
52 endif
53
54 BINDIST_STAMPS = libffi/stamp.ffi.build libfii/stamp.ffi.configure
55
56 INSTALL_HEADERS   += libffi/dist-install/build/ffi.h \
57                      libffi/dist-install/build/ffitarget.h
58 libffi_STATIC_LIB  = libffi/dist-install/build/libffi.a
59 INSTALL_LIBS      += libffi/dist-install/build/libHSffi.a \
60                      libffi/dist-install/build/libHSffi_p.a \
61                      libffi/dist-install/build/HSffi.o
62
63 # We have to add the GHC version to the name of our dynamic libs, because
64 # they will be residing in the system location along with dynamic libs from
65 # other GHC installations.
66
67 libffi_HS_DYN_LIB_NAME = libHSffi$(dyn_libsuf)
68 libffi_HS_DYN_LIB      = libffi/dist-install/build/$(libffi_HS_DYN_LIB_NAME)
69
70 ifeq "$(Windows)" "YES"
71 libffi_DYNAMIC_PROG = $(libffi_HS_DYN_LIB).a
72 libffi_DYNAMIC_LIBS = $(libffi_HS_DYN_LIB)
73 else
74 libffi_DYNAMIC_PROG =
75 ifeq "$(darwin_TARGET_OS)" "1"
76 libffi_DYNAMIC_LIBS = libffi/dist-install/build/libffi$(soext) \
77                       libffi/dist-install/build/libffi.5$(soext)
78 else ifeq "$(openbsd_TARGET_OS)" "1"
79 libffi_DYNAMIC_LIBS = libffi/dist-install/build/libffi.so.5.10
80 else
81 libffi_DYNAMIC_LIBS = libffi/dist-install/build/libffi.so \
82                       libffi/dist-install/build/libffi.so.5
83 endif
84 endif
85
86 ifeq "$(BuildSharedLibs)" "YES"
87 libffi_EnableShared=yes
88 else
89 libffi_EnableShared=no
90 endif
91
92 ifeq "$(BuildSharedLibs)" "YES"
93 INSTALL_LIBS  += $(libffi_HS_DYN_LIB)
94 ifeq "$(Windows)" "YES"
95 INSTALL_PROGS += $(libffi_HS_DYN_LIB).a
96 endif
97 endif
98
99 # We have to fake a non-working ln for configure, so that the fallback
100 # option (cp -p) gets used instead.  Otherwise the libffi build system
101 # will use cygwin symbolic linkks which cannot be read by mingw gcc.
102 # The same trick is played by the GMP build in ../gmp.
103
104 ifneq "$(BINDIST)" "YES"
105 $(libffi_STAMP_CONFIGURE):
106         "$(RM)" $(RM_OPTS_REC) $(LIBFFI_DIR) libffi/build
107         cat ghc-tarballs/libffi/libffi*.tar.gz | $(GZIP_CMD) -d | { cd libffi && $(TAR_CMD) -xf - ; }
108         mv libffi/libffi-* libffi/build
109         chmod +x libffi/ln
110
111 # Because -Werror may be in SRC_CC_OPTS/SRC_LD_OPTS, we need to turn
112 # warnings off or the compilation of libffi might fail due to warnings
113         cd libffi && \
114             PATH=`pwd`:$$PATH; \
115             export PATH; \
116             cd build && \
117             CC=$(WhatGccIsCalled) \
118             LD=$(LD) \
119             AR=$(AR) \
120             NM=$(NM) \
121         CFLAGS="$(SRC_CC_OPTS) $(CONF_CC_OPTS_STAGE1) -w" \
122         LDFLAGS="$(SRC_LD_OPTS) $(CONF_LD_OPTS_STAGE1) -w" \
123         "$(SHELL)" configure \
124                   --enable-static=yes \
125                   --enable-shared=$(libffi_EnableShared) \
126                   --host=$(PLATFORM) --build=$(PLATFORM)
127
128         # libffi.so needs to be built with the correct soname.
129         # NOTE: this builds libffi_convience.so with the incorrect
130         # soname, but we don't need that anyway!
131         cd libffi && \
132           "$(CP)" build/libtool build/libtool.orig; \
133           sed -e s/soname_spec=.*/soname_spec="$(libffi_HS_DYN_LIB_NAME)"/ build/libtool.orig > build/libtool
134
135         # We don't want libtool's cygwin hacks
136         cd libffi && \
137           "$(CP)" build/libtool build/libtool.orig; \
138           sed -e s/dlname=\'\$$tdlname\'/dlname=\'\$$dlname\'/ build/libtool.orig > build/libtool
139
140         touch $@
141
142 libffi/dist-install/build/ffi.h: $(libffi_STAMP_CONFIGURE) libffi/dist-install/build/ffitarget.h | $$(dir $$@)/.
143         "$(CP)" libffi/build/include/ffi.h $@
144
145 libffi/dist-install/build/ffitarget.h: $(libffi_STAMP_CONFIGURE) | $$(dir $$@)/.
146         "$(CP)" libffi/build/include/ffitarget.h $@
147
148 $(libffi_STAMP_BUILD): $(libffi_STAMP_CONFIGURE) | libffi/dist-install/build/.
149         $(MAKE) -C libffi/build MAKEFLAGS=
150         cd libffi/build && ./libtool --mode=install cp libffi.la $(TOP)/libffi/dist-install/build
151
152         # We actually want both static and dllized libraries, because we build
153         #   the runtime system both ways. libffi_convenience.a is the static version.
154 ifeq "$(Windows)" "YES"
155         cp libffi/build/.libs/libffi_convenience.a $(libffi_STATIC_LIB)
156 endif
157
158         touch $@
159
160 $(libffi_STATIC_LIB): $(libffi_STAMP_BUILD)
161         @test -f $@ || { echo "$< exits, but $@ does not."; echo "Suggest removing $<."; exit 1; }
162
163 # Rename libffi.a to libHSffi.a
164 libffi/dist-install/build/libHSffi.a libffi/dist-install/build/libHSffi_p.a: $(libffi_STATIC_LIB)
165         "$(CP)" $(libffi_STATIC_LIB) libffi/dist-install/build/libHSffi.a
166         "$(CP)" $(libffi_STATIC_LIB) libffi/dist-install/build/libHSffi_p.a
167
168 $(eval $(call all-target,libffi,$(INSTALL_HEADERS) $(INSTALL_LIBS)))
169
170 # The GHCi import lib isn't needed as compiler/ghci/Linker.lhs + rts/Linker.c
171 # link the interpreted references to FFI to the compiled FFI.
172 # Instead of adding libffi to the list preloaded packages (see
173 # compiler/ghci/Linker.lhs:emptyPLS) we generate an empty HSffi.o
174
175 libffi/dist-install/build/HSffi.o: libffi/dist-install/build/libHSffi.a
176         cd libffi/dist-install/build && \
177           touch empty.c && \
178           "$(CC)" $(SRC_CC_OPTS) $(CONF_CC_OPTS_STAGE1) -c empty.c -o HSffi.o
179
180 $(eval $(call all-target,libffi,libffi/dist-install/build/HSffi.o))
181
182 ifeq "$(BuildSharedLibs)" "YES"
183 ifeq "$(Windows)" "YES"
184 libffi/libffi.dll.a $(libffi_HS_DYN_LIB): $(libffi_STAMP_BUILD)
185         @test -f $@ || { echo "$< exits, but $@ does not."; echo "Suggest removing $<."; exit 1; }
186
187 # Windows libtool creates <soname>.dll, and as we already patched that
188 # there is no need to copy from libffi.dll to libHSffi...dll.
189 # However, the renaming is still required for the import library
190 # libffi.dll.a.
191 $(libffi_HS_DYN_LIB).a: libffi/dist-install/build/libffi.dll.a | $$(dir $$@)/.
192         "$(CP)" $< $@
193
194 $(eval $(call all-target,libffi,$(libffi_HS_DYN_LIB).a))
195
196 else
197 $(libffi_DYNAMIC_LIBS): $(libffi_STAMP_BUILD)
198         @test -f $@ || { echo "$< exits, but $@ does not."; echo "Suggest removing $<."; exit 1; }
199
200 # Rename libffi.so to libHSffi...so
201 $(libffi_HS_DYN_LIB): $(libffi_DYNAMIC_LIBS) | $$(dir $$@)/.
202         "$(CP)" $(word 1,$(libffi_DYNAMIC_LIBS)) $(libffi_HS_DYN_LIB)
203 ifeq "$(darwin_TARGET_OS)" "1"
204         # Ensure library's install name is correct before anyone links with it.
205         install_name_tool -id $(ghclibdir)/$(libffi_HS_DYN_LIB_NAME) $(libffi_HS_DYN_LIB)
206 endif
207
208 $(eval $(call all-target,libffi,$(libffi_HS_DYN_LIB)))
209 endif
210 endif
211
212 $(eval $(call clean-target,libffi,, \
213    libffi/build libffi/stamp.ffi.* libffi/dist-install))
214 endif
215
216 #-----------------------------------------------------------------------------
217 # Do the package config
218
219 $(eval $(call manual-package-config,libffi))
220
221 #-----------------------------------------------------------------------------
222 #
223 # binary-dist
224
225 BINDIST_EXTRAS += libffi/package.conf.in
226