Rework the build system a bit
[ghc-hetmet.git] / mk / package.mk
1 # -----------------------------------------------------------------------------
2 # $Id: package.mk,v 1.55 2005/05/13 10:05:33 krasimir Exp $
3
4 ifneq "$(PACKAGE)" ""
5
6 # -----------------------------------------------------------------------------
7 # Directory layouts, installation etc.
8
9 # Here Windows & Unix differ.  On Windows, the value of $(prefix) is known
10 # to the compiler, and spliced into package.conf in place of $topdir at
11 # runtime.
12 #
13 # On Unix, we only use absolute paths in package.conf, except that when
14 # building a binary distribution we use $libdir and $datadir in package.conf
15 # which are then replaced by the correct values at install time.
16 #
17
18 ifeq "$(Windows)" "YES"
19
20 PKG_LIBDIR  = $$topdir
21 PKG_DATADIR = $$topdir
22
23 else
24
25 PKG_LIBDIR  = $$libdir
26 PKG_DATADIR = $$datadir
27
28 endif # Unix
29
30 IMPORT_DIR_INSTALLED = $(PKG_LIBDIR)/imports
31 IMPORT_DIR_INPLACE   = $(FPTOOLS_TOP_ABS)/libraries/$(PACKAGE)
32
33 INCLUDE_DIR_INSTALLED = $(PKG_LIBDIR)/include
34 INCLUDE_DIR_INPLACE   = $(FPTOOLS_TOP_ABS)/libraries/$(PACKAGE)/include
35
36 LIB_DIR_INSTALLED    = $(PKG_LIBDIR)
37 LIB_DIR_INPLACE      = $(FPTOOLS_TOP_ABS)/libraries/$(PACKAGE)
38
39 DATA_DIR_INSTALLED   = $(PKG_DATADIR)
40 DATA_DIR_INPLACE     = $(FPTOOLS_TOP_ABS)/libraries/$(PACKAGE)
41
42 HTML_DIR_INPLACE     = $(FPTOOLS_TOP_ABS)/libraries/$(PACKAGE)/html
43 HTML_DIR_INSTALLED   = $(PKG_DATADIR)/html/libraries/$(PACKAGE)
44
45 HADDOCK_IFACE_INPLACE   = $(HTML_DIR_INPLACE)/$(PACKAGE).haddock
46 HADDOCK_IFACE_INSTALLED = $(HTML_DIR_INSTALLED)/$(PACKAGE).haddock
47
48 # -----------------------------------------------------------------------------
49 # Build the package configuration file and tell the compiler about it.
50
51 # We want to build two versions of the package configuration: one for use
52 # in the 
53
54 ifeq "$(way)" ""
55
56 PACKAGE_CPP_OPTS += -I$(GHC_INCLUDE_DIR) -Iinclude
57
58 PACKAGE_CPP_OPTS += -DPACKAGE=${PACKAGE}
59 PACKAGE_CPP_OPTS += -DVERSION=${VERSION}
60
61 PACKAGE_CPP_OPTS += -DPKG_LIBDIR='"$(PKG_LIBDIR)"'
62 PACKAGE_CPP_OPTS += -DPKG_DATADIR='"$(PKG_DATADIR)"'
63
64 package.conf.inplace   : package.conf.in
65         $(CPP) $(RAWCPP_FLAGS) -P \
66                 -DIMPORT_DIR='"$(IMPORT_DIR_INPLACE)"' \
67                 -DLIB_DIR='"$(LIB_DIR_INPLACE)"' \
68                 -DINCLUDE_DIR='"$(INCLUDE_DIR_INPLACE)"' \
69                 -DDATA_DIR='"$(DATA_DIR_INPLACE)"' \
70                 -DHTML_DIR='"$(HTML_DIR_INPLACE)"' \
71                 -DHADDOCK_IFACE='"$(HADDOCK_IFACE_INPLACE)"' \
72                 -DFPTOOLS_TOP_ABS='"${FPTOOLS_TOP_ABS}"' \
73                 -x c $(PACKAGE_CPP_OPTS) $< | \
74         grep -v '^#pragma GCC' | \
75         sed -e 's/""//g' -e 's/:[       ]*,/: /g' >$@
76
77 package.conf.installed : package.conf.in
78         $(CPP) $(RAWCPP_FLAGS) -P -DINSTALLING \
79                 -DIMPORT_DIR='"$(IMPORT_DIR_INSTALLED)"' \
80                 -DLIB_DIR='"$(LIB_DIR_INSTALLED)"' \
81                 -DINCLUDE_DIR='"$(INCLUDE_DIR_INSTALLED)"' \
82                 -DDATA_DIR='"$(DATA_DIR_INSTALLED)"' \
83                 -DHTML_DIR='"$(HTML_DIR_INSTALLED)"' \
84                 -DHADDOCK_IFACE='"$(HADDOCK_IFACE_INSTALLED)"' \
85                  -x c $(PACKAGE_CPP_OPTS) $< | \
86         grep -v '^#pragma GCC' | \
87         sed -e 's/""//g' -e 's/:[       ]*,/: /g' >$@
88
89 # we could be more accurate here and add a dependency on
90 # driver/package.conf, but that doesn't work too well because of
91 # make's limited accuracy with modification times: when doing 'make
92 # boot' in multiple packages, make won't detect that the package
93 # configuration needs updating if it was updated already in the last
94 # second.
95 #
96 # The stamp file goes in $(GHC_DRIVER_DIR), so that if someone happens
97 # to 'make clean' in ghc without cleaning in libraries too, the packages
98 # will be correctly re-installed.
99 #
100
101 STAMP_PKG_CONF = $(GHC_DRIVER_DIR)/stamp-pkg-conf-$(PACKAGE)
102 CLEAN_FILES += $(STAMP_PKG_CONF)
103
104 ifneq "$(BootingFromHc)" "YES"
105 boot all :: $(STAMP_PKG_CONF)
106 endif
107
108 $(STAMP_PKG_CONF) : package.conf.inplace package.conf.installed
109         $(GHC_PKG_INPLACE) update - --force-files <package.conf.inplace
110         $(GHC_PKG_INPLACE) update - -f $(GHC_DRIVER_DIR)/package.conf --force-files <package.conf.installed
111         @touch $(STAMP_PKG_CONF)
112
113 CLEAN_FILES += package.conf.installed package.conf.inplace 
114
115 endif # $(way) == ""
116
117 # -----------------------------------------------------------------------------
118 # Building the static library libHS<pkg>.a
119
120 SRC_HSC2HS_OPTS += -I.
121
122 ifneq "$(NO_SET_HC)" "YES"
123 HC = $(GHC_INPLACE)
124 endif
125 IGNORE_PACKAGE_FLAG = -package-name  $(PACKAGE)-$(VERSION)
126
127 ifeq "$(NON_HS_PACKAGE)" ""
128 SRC_HC_OPTS     += $(IGNORE_PACKAGE_FLAG)
129 SRC_HC_OPTS     += $(GhcLibHcOpts)
130 SRC_HC_OPTS     += $(patsubst %, -package %, $(PACKAGE_DEPS))
131 endif
132
133 #       -fgenerics switches on generation of support code for 
134 #               derivable type classes.  This is now off by default,
135 #               but we switch it on for the libraries so that we generate
136 #               the code in case someone importing wants it.
137 ifeq "$(NON_HS_PACKAGE)" ""
138 SRC_HC_OPTS     += -fgenerics
139 endif
140
141 ifndef LIBRARY
142 LIBRARY         = libHS$(PACKAGE)$(_way).a
143 endif
144
145 ifeq "$(WAYS)" ""
146 WAYS = $(GhcLibWays)
147 endif
148
149 all :: $(LIBRARY)
150
151 # POSSIBLE alternative version using --make:
152 #
153 # lib : $(HS_SRCS)
154 #       $(GHC_INPLACE) $(HC_OPTS) --make $(HS_SRCS)
155
156 # $(LIBNAME) : lib
157 #       $(RM) $@
158 #       $(AR) $(AR_OPTS) $@ $(HS_OBJS)
159 #       $(RANLIB) $@
160
161 # %.o : %.hs
162 #       $(GHC_INPLACE) $(HC_OPTS) --make $<
163 # %.o : %.lhs
164 #       $(GHC_INPLACE) $(HC_OPTS) --make $<
165
166 # -----------------------------------------------------------------------------
167 # Installation; need to install .hi files as well as libraries
168
169 ifeq "$(DLLized)" "YES"
170 INSTALL_PROGS += $(DLL_NAME)
171 INSTALL_LIBS += $(patsubst %.a,%_imp.a, $(LIBRARY))
172 endif
173
174 # The interface files are put inside the $(libdir), since they
175 # might (potentially) be platform specific..
176
177 ifeq "$(HIERARCHICAL_LIB)" "YES"
178 ifacedir = $(libdir)/imports
179 else
180 ifacedir = $(libdir)/hslibs-imports/$(PACKAGE)
181 endif
182
183 # If the lib consists of a hierachy of modules, we must retain the directory
184 # structure when we install the interfaces.
185 ifeq "$(HIERARCHICAL_LIB)" "YES"
186 INSTALL_IFACES_WITH_DIRS += $(HS_IFACES)
187 ifneq "$(ALL_DIRS)" ""
188 install ::
189         @for i in $(ALL_DIRS); do \
190                 $(INSTALL_DIR) $(ifacedir)/$$i; \
191         done
192 endif
193 else
194 INSTALL_IFACES += $(HS_IFACES)
195 endif
196
197 # -----------------------------------------------------------------------------
198 # Dependencies
199
200 MKDEPENDHS = $(GHC_INPLACE)
201
202 SRC_MKDEPENDC_OPTS += $(addprefix -I,$(ALL_DIRS))
203 SRC_MKDEPENDC_OPTS += -I$(GHC_INCLUDE_DIR)
204
205 endif # $(PACKAGE) != ""
206
207 #--------------------------------------------------------------
208 # Installation
209
210 ifneq "$(NO_INSTALL_LIBRARY)" "YES"
211 INSTALL_LIBS  += $(LIBRARY) $(GHCI_LIBRARY)
212 endif
213
214 #--------------------------------------------------------------
215 # Building dynamically-linkable libraries for GHCi
216 #
217 # Build $(GHCI_LIBRARY) whenever we build $(LIBRARY)
218 #
219 # Why?  GHCi can only link .o files (at the moment), not .a files
220 # so we have to build libFoo.o as well as libFoo.a
221 #
222 # Furthermore, GHCi currently never loads 
223 # profiling libraries (or other non-std ways)
224
225 ifneq "$(LIBRARY)" ""
226
227 ifeq "$(way)" ""
228 ifeq "$(GhcWithInterpreter)" "YES"
229
230 ifndef GHCI_LIBRARY
231 GHCI_LIBRARY = $(patsubst lib%.a,%.o,$(LIBRARY))
232 endif
233
234 ifneq "$(NO_INSTALL_LIBRARY)" "YES"
235 INSTALL_LIBS += $(GHCI_LIBRARY)
236 endif
237
238 CLEAN_FILES  += $(GHCI_LIBRARY)
239
240 all :: $(GHCI_LIBRARY)
241
242 ifneq "$(DONT_WANT_STD_GHCI_LIB_RULE)" "YES"
243 # If you don't want to build GHCI_LIBRARY the 'standard' way,
244 # set DONT_WANT_STD_GHCI_LIB_RULE to YES. The Prelude and
245 # hslibs/Win32 uses this 'feature', which will go away soon
246 # when we can use a "fixed" ld.
247 #
248 $(GHCI_LIBRARY) : $(LIBOBJS)
249         $(LD) -r $(LD_X) -o $@ $(STUBOBJS) $(LIBOBJS)
250
251 endif # DONT_WANT_STD_GHCI_LIB_RULE
252 endif # GhcWithInterpreter
253 endif # way
254
255 ifeq "$(GhcBuildDylibs)" "YES"
256
257     # Build dynamic libraries.
258     # Currently, this is a hack. Anyone, PLEASE clean it up.
259
260     # For now, we pretend that there are two operating systems in the world;
261     # Darwin, and Everything Else. Furthermore, we pretend that Everything Else
262     # behaves like Linux.
263     
264 ifeq "$(darwin_TARGET_OS)" "1"
265     # Darwin: Shared libraries end in .dylib
266 DYLD_LIBRARY = $(patsubst %.a,%_dyn.dylib,$(LIBRARY))
267
268     # About the options used for Darwin:
269     # -dynamiclib
270     #   Apple's way of saying -shared
271     # -undefined dynamic_lookup:
272     #   Without these options, we'd have to specify the correct dependencies
273     #   for each of the dylibs. Note that we could (and should) do without this
274     #   for all libraries except the RTS; all we need to do is to pass the
275     #   correct HSfoo_dyn.dylib files to the link command.
276     #   This feature requires Mac OS X 10.3 or later; there is a similar feature,
277     #   -flat_namespace -undefined suppress, which works on earlier versions,
278     #   but it has other disadvantages.
279     # -single_module
280     #   Build the dynamic library as a single "module", i.e. no dynamic binding
281     #   nonsense when referring to symbols from within the library. The NCG
282     #   assumes that this option is specified (on i386, at least).
283     # -Wl,-macosx_version_min -Wl,10.3
284     #   Tell the linker its safe to assume that the library will run on 10.3 or
285     #   later, so that it will not complain about the use of the option
286     #   -undefined dynamic_lookup above.
287     # -install_name
288     #   Causes the dynamic linker to ignore the DYLD_LIBRARY_PATH when loading
289     #   this lib and instead look for it at its absolute path.
290     #   When installing the .dylibs (see target.mk), we'll change that path to
291     #   point to the place they are installed. Therefore, we won't have to set
292     #   up DYLD_LIBRARY_PATH specifically for ghc.
293
294 $(DYLD_LIBRARY) : $(LIBOBJS) $(STUBOBJS)
295         $(CC) -dynamiclib -o $@ $(STUBOBJS) $(LIBOBJS) \
296                 -undefined dynamic_lookup -single_module \
297                  -Wl,-macosx_version_min -Wl,10.3 \
298                 -install_name `pwd`/$@
299
300 else
301 DYLD_LIBRARY = $(patsubst %.a,%_dyn.so,$(LIBRARY))
302
303 $(DYLD_LIBRARY) : $(LIBOBJS) $(STUBOBJS)
304         $(CC) -shared -o $@ $(STUBOBJS) $(LIBOBJS)
305 endif
306
307 ifneq "$(NO_INSTALL_LIBRARY)" "YES"
308 INSTALL_LIBS += $(DYLD_LIBRARY)
309 endif
310
311 CLEAN_FILES += $(DYLD_LIBRARY)
312
313 all :: $(DYLD_LIBRARY)
314
315 endif # $(GhcBuildDylibs) == "YES"
316
317 endif # $(LIBRARY) /= ""
318
319 # -----------------------------------------------------------------------------
320 # Doc building with Haddock
321
322 ifneq "$(PACKAGE)" ""
323 ifneq "$(NO_HADDOCK_DOCS)" "YES"
324
325 HS_PPS = $(addsuffix .raw-hs, $(basename $(filter-out $(EXCLUDED_HADDOCK_SRCS), $(HS_SRCS)))) $(EXTRA_HADDOCK_SRCS)
326
327 HTML_DIR = ../html-docs/$(PACKAGE)
328 HTML_DOC = $(HTML_DIR)/haddock.css $(HTML_DIR)/haddock.js
329
330 ifneq "$(HS_PPS)" ""
331
332 CLEAN_FILES += $(HS_PPS) $(addsuffix .tmp, $(HS_SRCS))
333
334 ifeq "$(HADDOCK)" ""
335 html ::
336         @echo Haddock must be installed in order to build HTML library documentation.
337         @echo Please install Haddock and re-configure.
338         @exit 1
339 endif
340
341 html :: $(HTML_DOC)
342
343 extraclean :: 
344         $(RM) -rf $(HTML_DIR)
345
346 ifneq "$(findstring $(PACKAGE), $(CorePackages))" ""
347 HaddockSourceURL = $(CorePackageSourceURL)
348 else
349 HaddockSourceURL = $(ExtraPackageSourceURL)
350 endif
351
352 $(HTML_DOC) : $(HS_PPS)
353         @$(INSTALL_DIR) $(HTML_DIR)
354         $(HADDOCK) $(HADDOCK_OPTS) -h -o $(HTML_DIR) $(HS_PPS) \
355                 --package=$(PACKAGE) \
356                 --dump-interface=$(PACKAGE).haddock \
357                 --use-index=../doc-index.html --use-contents=../index.html \
358                 --source-module=$(HaddockSourceURL) \
359                 $(foreach pkg, $(PACKAGE_DEPS), \
360                    --read-interface=../$(pkg),../$(pkg)/$(pkg).haddock)
361
362 CLEAN_FILES += $(PACKAGE).haddock
363
364 %.raw-hs : %.lhs
365         $(HC) $(HC_OPTS) -D__HADDOCK__ -E $< -o $@
366
367 %.raw-hs : %.hs
368         $(HC) $(HC_OPTS) -D__HADDOCK__ -E $< -o $@
369
370 HTML_INSTALL_DIR = $(datadir)/html/libraries/$(PACKAGE)
371
372 install-docs :: $(HTML_DOC)
373         @$(INSTALL_DIR) $(HTML_INSTALL_DIR)
374         for i in $(HTML_DIR)/*; do \
375           echo $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(HTML_INSTALL_DIR); \
376           $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(HTML_INSTALL_DIR); \
377         done; \
378         $(INSTALL_DATA) $(INSTALL_OPTS) $(PACKAGE).haddock $(HTML_INSTALL_DIR); \
379
380 endif # HS_PPS
381 endif # NO_HADDOCK_DOCS
382 endif # $(PACKAGE) /= ""
383
384 # -----------------------------------------------------------------------------
385