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