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