[project @ 2001-05-24 10:46:08 by simonmar]
[ghc-hetmet.git] / mk / target.mk
1 #################################################################################
2 #
3 #                       target.mk
4 #
5 #               Standard targets for fptools
6 #
7 #################################################################################
8
9 #
10 # This file contain three groups of target rules:
11 #
12 # 1.  FPtools targets
13 #       depend*
14 #       runtests*
15 #
16 # 2.  GNU standard targets
17 #       all*
18 #       install* uninstall installcheck installdirs
19 #       clean* distclean* mostlyclean* maintainer-clean*
20 #       tags*
21 #       dvi ps (no info) FPTOOLS adds: pdf rtf html
22 #       check
23 #
24 # 3. Some of the above targets have a version that
25 #    recursively invokes that target in sub-directories.
26 #    This relies on the importing Makefile setting SUBDIRS
27 #
28 #    The recursive targets are marked with a * above
29 #
30
31
32
33 #
34
35
36 ##################################################################
37 #
38 #               Recursive stuff
39 #
40 # At the top of the file so that recursive makes happen before
41 # makes in the main directory. This is needed for some targets,
42 # e.g. when building DLLs in hslibs.
43 #
44 ##################################################################
45
46 # Here are the diabolically clever rules that
47
48 # (a) for each "recursive target" <t>
49 #     propagates "make <t>" to directories in SUBDIRS
50 #
51 # (b) when SUBDIRS is empty,
52 #     for each "multi-way-target" <t>
53 #     calls "make way=w <t>" for each w in $(WAYS)
54 #
55 #     This has the effect of making the standard target
56 #     in each of the specified ways (as well as in the normal way
57
58 # Controlling variables
59 #       WAYS    = extra (beyond the normal way) ways to build things in
60 #       SUBDIRS = subdirectories to recurse into
61
62 # No ways, so iterate over the SUBDIRS
63
64 # note about recursively invoking make: we'd like make to drop all the
65 # way back to the top level if it fails in any of the
66 # sub(sub-...)directories.  This is done by setting the -e flag to the
67 # shell during the loop, which causes an immediate failure if any of
68 # the shell commands fail.
69
70 # One exception: if the user gave the -i or -k flag to make in the
71 # first place, we'd like to reverse this behaviour.  So we check for
72 # these flags, and set the -e flag appropriately.  NOTE: watch out for
73 # the --no-print-directory flag which is passed to recursive
74 # invocations of make.
75 #
76 # NOTE: Truly weird use of exit below to stop the for loop dead in
77 # its tracks should any of the sub-makes fail. By my reckoning, 
78 #  "cmd || exit $?" should be equivalent to "cmd"
79
80 ifneq "$(SUBDIRS)" ""
81
82 # we override the boot & all targets in the top level Makefile
83 ifneq "$(NO_ALL_TARGETS)" "YES"
84 ALL_TARGET     = all
85 BOOT_TARGET    = boot
86 INSTALL_TARGET = install
87 endif
88
89 $(ALL_TARGET) docs runtests $(BOOT_TARGET) TAGS clean distclean mostlyclean maintainer-clean $(INSTALL_TARGET) html ps dvi txt::
90         @echo "------------------------------------------------------------------------"
91         @echo "===fptools== Recursively making \`$@' in $(SUBDIRS) ..."
92         @echo "PWD = $(shell pwd)"
93         @echo "------------------------------------------------------------------------"
94 # Don't rely on -e working, instead we check exit return codes from sub-makes.
95         @case '${MFLAGS}' in *-[ik]*) x_on_err=0;; *-r*[ik]*) x_on_err=0;; *) x_on_err=1;; esac; \
96         for i in $(SUBDIRS); do \
97           echo "------------------------------------------------------------------------"; \
98           echo "==fptools== $(MAKE) $@ $(MFLAGS);"; \
99           echo " in $(shell pwd)/$$i"; \
100           echo "------------------------------------------------------------------------"; \
101           $(MAKE) --no-print-directory -C $$i $(MFLAGS) $@; \
102           if [ $$? -eq 0 -o $$x_on_err -eq 0 ] ;  then true; else exit 1; fi; \
103         done
104         @echo "------------------------------------------------------------------------"
105         @echo "===fptools== Finished making \`$@' in $(SUBDIRS) ..."
106         @echo "PWD = $(shell pwd)"
107         @echo "------------------------------------------------------------------------"
108
109 endif
110
111 #
112 # Selectively building subdirectories.
113 #
114 #
115 ifneq "$(SUBDIRS)" ""
116 $(SUBDIRS) ::
117           $(MAKE) -C $@ $(MFLAGS)
118 endif
119
120 ##################################################################
121 #               FPtools standard targets
122 #
123 # depend:
124 #
125 #  The depend target has to cope with a set of files that may have
126 #  different ways of computing their dependencies, i.e., a Haskell
127 #  module's dependencies are computed differently from C files.
128 #
129 # Note that we don't compute dependencies automatically, i.e., have the
130 # .depend file be a target that is dependent on the Haskell+C sources,
131 # and then have the `depend' target depend on `.depend'. The reason for
132 # this is that when GNU make is processing the `include .depend' statement
133 # it records .depend as being a Makefile. Before doing any other processing,
134 # `make' will try to check to see if the Makefiles are up-to-date. And,
135 # surprisingly enough, .depend has a rule for it, so if any of the source
136 # files change, it will be invoked, *regardless* of what target you're making.
137 #
138 # So, for now, the dependencies has to be re-computed manually via `make depend'
139 # whenever a module changes its set of imports. Doing what was outlined above
140 # is only a small optimisation anyway, it would avoid the recomputation of
141 # dependencies if the .depend file was newer than any of the source modules.
142 #
143 .PHONY: depend
144
145 # Compiler produced files that are targets of the source's imports.
146 MKDEPENDHS_OBJ_SUFFICES=o
147
148 depend :: $(MKDEPENDHS_SRCS) $(MKDEPENDC_SRCS)
149         @$(RM) .depend
150         @touch .depend
151 ifneq "$(DOC_SRCS)" ""
152         $(MKDEPENDLIT) -o .depend $(MKDEPENDLIT_OPTS) $(filter %.lit,$(DOC_SRCS))
153 endif
154 ifneq "$(MKDEPENDC_SRCS)" ""
155         $(MKDEPENDC) -f .depend $(MKDEPENDC_OPTS) $(foreach way,$(WAYS),-s $(way)) -- $(CC_OPTS) -- $(MKDEPENDC_SRCS) 
156 endif
157 ifneq "$(MKDEPENDHS_SRCS)" ""
158         $(MKDEPENDHS) -M -optdep-f -optdep.depend $(foreach way,$(WAYS),-optdep-s -optdep$(way)) $(foreach obj,$(MKDEPENDHS_OBJ_SUFFICES),-osuf $(obj)) $(MKDEPENDHS_OPTS) $(HC_OPTS) $(MKDEPENDHS_SRCS)
159 endif
160
161
162 ##################################################################
163 #                       boot
164 #
165 #  The boot target, at a minimum generates dependency information
166
167 .PHONY: boot
168 boot :: depend
169
170
171 ##################################################################
172 #               GNU Standard targets
173 #
174 #       Every Makefile should define the following targets
175
176 # `all'
177 #      Compile the entire program. This should be the default target.
178 #      This target need not rebuild any documentation files
179
180 # `install'
181 #      Compile the program and copy the executables, libraries, and so on
182 #      to the file names where they should reside for actual use. If
183 #      there is a simple test to verify that a program is properly
184 #      installed, this target should run that test.
185
186 #      The commands should create all the directories in which files are
187 #      to be installed, if they don't already exist. This includes the
188 #      directories specified as the values of the variables prefix and
189 #      exec_prefix , as well as all subdirectories that are needed. One
190 #      way to do this is by means of an installdirs target as described
191 #      below.
192
193 #      Use `-' before any command for installing a man page, so that make
194 #      will ignore any errors.  This is in case there are systems that
195 #      don't have the Unix man page documentation system installed.
196
197 # `uninstall'
198 #      Delete all the installed files that the `install' target would
199 #      create (but not the noninstalled files such as `make all' would
200 #      create).
201
202 # `clean'
203
204 #      Delete all files from the current directory that are normally
205 #      created by building the program.  Don't delete the files that
206 #      record the configuration. Also preserve files that could be made
207 #      by building, but normally aren't because the distribution comes
208 #      with them.
209
210 #      Delete `.dvi' files here if they are not part of the
211 #      distribution.
212
213 # `distclean'
214 #      Delete all files from the current directory that are created by
215 #      configuring or building the program. If you have unpacked the
216 #      source and built the program without creating any other files,
217 #      `make distclean' should leave only the files that were in the
218 #      distribution.
219
220 # `mostlyclean'
221 #      Like `clean', but may refrain from deleting a few files that
222 #      people normally don't want to recompile. For example, the
223 #      `mostlyclean' target for GCC does not delete `libgcc.a', because
224 #      recompiling it is rarely necessary and takes a lot of time.
225
226 # `maintainer-clean'
227 #      Delete everything from the current directory that can be
228 #      reconstructed with this Makefile.  This typically includes
229 #      everything deleted by distclean , plus more: C source files
230 #      produced by Bison, tags tables, and so on.
231
232 #      One exception, however: `make maintainer-clean' should not delete
233 #      `configure' even if `configure' can be remade using a rule in the
234 #      Makefile. More generally, `make maintainer-clean' should not delete
235 #      anything that needs to exist in order to run `configure' and then
236 #      begin to build the program.
237
238 # `TAGS'
239 #      Update a tags table for this program.
240
241 # `dvi' `ps' `pdf' `html' `pdf'
242 #      Generate DVI/PS/PDF files for LaTeX/DocBook docs. Not everything is
243 #      supported everywhere, but the intention is to standardise on DocBook
244 #      producing all formats.
245 #
246 # `check'
247 #      Perform self-tests (if any). The user must build the program
248 #      before running the tests, but need not install the program; you
249 #      should write the self-tests so that they work when the program is
250 #      built but not installed.
251
252 # The following targets are suggested as conventional names, for programs
253 # in which they are useful.
254
255 # installcheck
256 #      Perform installation tests (if any). The user must build and
257 #      install the program before running the tests. You should not
258 #      assume that `$(bindir)' is in the search path.
259
260 # installdirs
261 #      It's useful to add a target named `installdirs' to create the
262 #      directories where files are installed, and their parent
263 #      directories. There is a script called `mkinstalldirs' which is
264 #      convenient for this; find it in the Texinfo package.
265 #      (FPTOOLS: we use a close relative of the suggested script, situated
266 #       in glafp-utils/mkdirhier -- SOF)
267
268
269
270
271 ###########################################
272 #
273 #       Targets: "all"
274 #
275 ###########################################
276
277 # For each of these variables that is defined
278 # we generate one "all" rule and one rule for the variable itself:
279 #
280 #       HS_PROG         Haskell program
281 #       C_PROG          C program
282 #       LIBRARY         Library
283 #       SCRIPT_PROG     Script (e.g. Perl script)
284 #
285 # For details of exactly what rule is generated, see the
286 # relevant section below
287
288 .PHONY: all
289
290 #----------------------------------------
291 #       Haskell programs
292
293 ifneq "$(HS_PROG)" ""
294 all :: $(HS_PROG)
295
296 ifneq "$(BootingFromHc)" "YES"
297 $(HS_PROG) :: $(HS_OBJS)
298         $(HC) -o $@ $(HC_OPTS) $(LD_OPTS) $(HS_OBJS)
299 else
300 # see bootstrp.mk
301 $(HS_PROG) :: $(HS_OBJS)
302         $(CC) -o $@ $(HC_BOOT_CC_OPTS) $(HC_BOOT_LD_OPTS) $(HS_OBJS) $(HC_BOOT_LIBS)
303 endif
304 endif
305
306 # Object and interface files have suffixes tagged with their ways
307 ifneq "$(way)" ""
308 SRC_HC_OPTS += -hisuf $(way_)hi -hcsuf $(way_)hc -osuf $(way_)o
309 endif
310
311 #----------------------------------------
312 #       C programs
313
314 ifneq "$(C_PROG)" ""
315 all :: $(C_PROG)
316
317 $(C_PROG) :: $(C_OBJS)
318         $(CC) -o $@ $(CC_OPTS) $(LD_OPTS) $(C_OBJS) $(LIBS)
319 endif
320
321
322 #----------------------------------------
323 #       Building HsLibs libraries.
324
325 ifneq "$(HSLIB)" ""
326
327 ifeq "$(IS_CBITS_LIB)" "YES"
328 _cbits := _cbits
329 endif
330
331 LIBRARY      = libHS$(HSLIB)$(_cbits)$(_way).a
332 GHCI_LIBRARY = HS$(HSLIB)$(_cbits)$(_way).o
333
334 ifneq "$(IS_CBITS_LIB)" "YES"
335 WAYS=$(GhcLibWays)
336 endif
337
338 ifeq "$(LIBOBJS)" ""
339   ifeq "$(IS_CBITS_LIB)" "YES"
340   LIBOBJS = $(C_OBJS)
341   else
342   LIBOBJS = $(HS_OBJS)
343   endif
344 endif
345
346 STUBOBJS += $(HSC_C_OBJS)
347 SRC_CC_OPTS += -I$(GHC_INCLUDE_DIR) -I$(GHC_RUNTIME_DIR)
348
349 ifeq "$(IS_CBITS_LIB)" "YES"
350 override datadir:=$(libdir)/includes
351 INSTALL_DATAS += Hs$(shell perl -e 'print ucfirst "$(HSLIB)"').h
352 else
353 SRC_CC_OPTS += -Icbits
354 endif
355
356 endif # HSLIB
357
358 #----------------------------------------
359 #       Libraries/archives
360
361 ifneq "$(LIBRARY)" ""
362 all :: $(LIBRARY)
363
364 define BUILD_LIB
365 $(RM) $@
366 $(AR) $(AR_OPTS) $@ $(STUBOBJS) $(LIBOBJS)
367 $(RANLIB) $@
368 endef
369
370 #
371 # For Haskell object files, we might have chosen to split
372 # up the object files. Test for whether the library being
373 # built is consisting of Haskell files by (hackily) checking
374 # whether HS_SRCS is empty or not.
375 #
376
377 ifneq "$(HS_SRCS)" ""
378 ifeq "$(SplitObjs)" "YES"
379
380 # can't split objs in way 'u', so we disable it here
381 ifneq "$(way)" "u"
382
383 SRC_HC_OPTS += -split-objs
384
385 define BUILD_LIB
386 $(RM) $@
387 (echo $(STUBOBJS); $(FIND) $(patsubst %.$(way_)o,%,$(LIBOBJS)) -name '*.$(way_)o') | xargs ar q $@
388 $(RANLIB) $@
389 endef
390
391 # Extra stuff for compiling Haskell files with $(SplitObjs):
392
393 HC_SPLIT_PRE = \
394     $(RM) $@; if [ ! -d $(basename $@) ]; then mkdir $(basename $@); else \
395     $(FIND) $(basename $@) -name '*.$(way_)o' | xargs $(RM) __rm_food; fi
396 ifeq "$(GhcWithInterpreter)" "YES"
397 HC_SPLIT_POST = ld -r -x -o $@ $(basename $@)/*.$(way_)o
398 else
399 HC_SPLIT_POST = touch $@
400 endif
401
402 SRC_HC_PRE_OPTS  += $(HC_SPLIT_PRE);
403 SRC_HC_POST_OPTS += $(HC_SPLIT_POST);
404
405 #
406 # If (Haskell) object files are split, cleaning up 
407 # consist of descending into the directories where
408 # the myriads of object files have been put.
409 #
410
411 extraclean ::
412         $(FIND) $(patsubst %.$(way_)o,%,$(HS_OBJS)) -name '*.$(way_)o' -print | xargs $(RM) __rm_food
413         -rmdir $(patsubst %.$(way_)o,%,$(HS_OBJS)) > /dev/null 2>&1
414
415 endif # $(way) == u
416 endif # $(SplitObjs)
417 endif # $(HS_SRCS)
418
419 #
420 # Remove local symbols from library objects if requested.
421 #
422
423 ifeq "$(StripLibraries)" "YES"
424 ifeq "$(SplitObjs)" "YES"
425 SRC_HC_POST_OPTS += \
426   for i in $(basename $@)/*; do \
427         ld -r -x -o $$i.tmp $$i; \
428         $(MV) $$i.tmp $$i; \
429   done
430 else
431 SRC_HC_POST_OPTS += \
432   ld -r -x -o $@.tmp $@; $(MV) $@.tmp $@
433 endif
434 endif
435
436 $(LIBRARY) :: $(STUBOBJS) $(LIBOBJS)
437         $(BUILD_LIB)
438 endif
439
440 #--------------------------------------------------------------
441 #       Build dynamically-linkable libraries for GHCi
442 #
443
444 ifneq "$(GHCI_LIBRARY)" ""
445 ifeq "$(way)" ""
446 ifeq "$(GhcWithInterpreter)" "YES"
447
448 all :: $(GHCI_LIBRARY)
449
450 ifeq "$(GHCI_LIBOBJS)" ""
451 GHCI_LIBOBJS = $(LIBOBJS)
452 endif
453
454 $(GHCI_LIBRARY) :: $(GHCI_LIBOBJS)
455         ld -r -x -o $@ $(GHCI_LIBOBJS) $(STUBOBJS)
456
457 INSTALL_LIBS += $(GHCI_LIBRARY)
458 CLEAN_FILES += $(GHCI_LIBRARY)
459 endif
460 endif
461 endif
462
463 #----------------------------------------
464 #       Building Win32 DLLs
465 #
466
467 ifeq "$(DLLized)" "YES"
468
469 ifneq "$(HSLIB)" ""
470
471 SRC_BLD_DLL_OPTS += --export-all --output-def=HS$(HSLIB)$(_cbits)$(_way).def DllVersionInfo.$(way_)o
472 ifneq "$(HSLIB)" "rts"
473 SRC_BLD_DLL_OPTS += -lHSstd_cbits_imp -L$(GHC_LIB_DIR)/std/cbits
474 SRC_BLD_DLL_OPTS += -lHSrts_$(way_)imp -L$(GHC_RUNTIME_DIR)
475 ifneq "$(HSLIB)" "std"
476   ifeq "$(IS_CBITS_LIB)" ""
477   SRC_BLD_DLL_OPTS += -lHSstd_$(way_)imp -L$(GHC_LIB_DIR)/std 
478   endif
479 endif
480 endif
481 SRC_BLD_DLL_OPTS += -lgmp -L. -L$(GHC_RUNTIME_DIR)/gmp
482 ifeq "$(IS_CBITS_LIB)" ""
483 SRC_BLD_DLL_OPTS += $(patsubst %,-lHS%_$(way_)imp, $(HSLIB_DEPS))
484 SRC_BLD_DLL_OPTS += $(patsubst %,-L../%, $(HSLIB_DEPS))
485 endif
486 ifneq "$(HAS_CBITS)" ""
487 SRC_BLD_DLL_OPTS += -lHS$(HSLIB)_cbits_imp -Lcbits
488 endif
489 SRC_BLD_DLL_OPTS += -lwsock32 -lwinmm
490
491 endif # HSLIB != ""
492
493 SplitObjs = NO 
494
495 ifneq "$(LIBRARY)" ""
496
497 all :: DllVersionInfo.$(way_)o
498
499 ifeq "$(DLL_NAME)" ""
500 DLL_NAME = $(patsubst %.a,%.dll,$(subst lib,,$(LIBRARY)))
501 endif
502
503 ifneq "$(DLL_NAME)" ""
504 DLL_NAME := $(DLL_PEN)/$(DLL_NAME)
505 endif
506
507 all :: $(DLL_NAME)
508
509 ifeq "$(DLL_IMPLIB_NAME)" ""
510 DLL_IMPLIB_NAME = $(patsubst %.a,%_imp.a,$(LIBRARY))
511 endif
512
513 $(DLL_NAME) :: $(LIBRARY)
514         $(BLD_DLL) --output-lib $(DLL_IMPLIB_NAME) -o $(DLL_NAME) $(LIBRARY) $(BLD_DLL_OPTS)
515 endif # LIBRARY != ""
516
517 endif # DLLized
518
519 #
520 # Version information is baked into a DLL by having the DLL include DllVersionInfo.o.
521 # The version info contains two user tweakables: DLL_VERSION and DLL_VERSION_NAME.
522 # (both are given sensible defaults though.)
523 #
524 # Note: this will not work as expected with Cygwin B20.1; you need a more recent
525 #       version of binutils (to pick up windres bugfixes.)
526
527 ifndef DLL_VERSION
528 DLL_VERSION=$(ProjectVersion)
529 endif
530
531 ifndef DLL_VERSION_NAME
532 DLL_VERSION_NAME="http://www.haskell.org/ghc"
533 endif
534
535 ifndef DLL_DESCRIPTION
536 DLL_DESCRIPTION="A GHC-compiled DLL"
537 endif
538
539 ifndef EXE_VERSION
540 EXE_VERSION=$(ProjectVersion)
541 endif
542
543 ifndef EXE_VERSION_NAME
544 EXE_VERSION_NAME="http://www.haskell.org/ghc"
545 endif
546
547 ifndef EXE_DESCRIPTION
548 EXE_DESCRIPTION="A GHC-compiled binary"
549 endif
550
551 #
552 # Little bit of lo-fi mangling to get at the right set of settings depending
553 # on whether we're generating the VERSIONINFO for a DLL or EXE
554
555 DLL_OR_EXE=$(subst VersionInfo.$(way_)rc,,$@)
556 VERSION_FT=$(subst Dll, 0x2L, $(subst Exe, 0x1L, $(DLL_OR_EXE)))
557 VERSION_RES_NAME=$(subst Exe,$(EXE_VERSION_NAME), $(subst Dll, $(DLL_VERSION_NAME),$(DLL_OR_EXE)))
558 VERSION_RES=$(subst Exe,$(EXE_VERSION), $(subst Dll, $(DLL_VERSION),$(DLL_OR_EXE)))
559 VERSION_DESC=$(subst Exe,$(EXE_DESCRIPTION), $(subst Dll, $(DLL_DESCRIPTION),$(DLL_OR_EXE)))
560
561 DllVersionInfo.$(way_)rc ExeVersionInfo.$(way_)rc:
562         $(RM) DllVersionInfo.$(way_)rc
563         echo "1 VERSIONINFO"                > $@
564         echo "FILEVERSION 1,0,0,1"         >> $@
565         echo "PRODUCTVERSION 1,0,0,1"      >> $@
566         echo "FILEFLAGSMASK 0x3fL"         >> $@
567         echo "FILEOS 0x4L"                 >> $@
568         echo "FILETYPE $(VERSION_FT)"      >> $@
569         echo "FILESUBTYPE 0x0L"            >> $@
570         echo "BEGIN"                       >> $@
571         echo " BLOCK \"StringFileInfo\""   >> $@
572         echo " BEGIN"                      >> $@
573         echo "  BLOCK \"040904B0\""        >> $@
574         echo "  BEGIN"                     >> $@
575         echo "   VALUE \"CompanyName\", \"$(VERSION_RES_NAME)\\0\"" >> $@
576         echo "   VALUE \"FileVersion\", \"$(VERSION_RES)\\0\"" >> $@
577         echo "   VALUE \"ProductVersion\", \"$(VERSION_RES)\\0\"" >> $@
578         echo "   VALUE \"FileDescription\", \"$(VERSION_DESC)\\0\"" >> $@
579         echo "  END" >> $@
580         echo " END" >> $@
581         echo " BLOCK \"VarFileInfo\""  >> $@
582         echo " BEGIN" >> $@
583         echo "  VALUE \"Translation\", 0x0409, 1200" >> $@
584         echo " END" >> $@
585         echo "END" >> $@
586
587 #----------------------------------------
588 #       Script programs
589
590 ifneq "$(SCRIPT_PROG)" ""
591
592 # To produce a fully functional script, you may
593 # have to add some configuration variables at the top of 
594 # the script, i.e., the compiler driver needs to know
595 # the path to various utils in the build tree for instance.
596 #
597 # To have the build rule for the script automatically do this
598 # for you, set the variable SCRIPT_SUBST_VARS to the list of
599 # variables you need to put in.
600
601 #
602 # SCRIPT_SUBST creates a string of echo commands that
603 # will when evaluated append the (perl)variable name and its value 
604 # to the target it is used for, i.e.,
605 #
606 #    A=foo
607 #    B=bar
608 #    SCRIPT_SUBST_VARS = A B
609 #    SCRIPT_SUBST=echo "$""A=\"foo\";" >> $@; echo "$""B=\"bar\";" >> $@
610 #
611 #    so if you have a rule like the following
612 #    
613 #     foo:
614 #         @(RM) $@
615 #         @(TOUCH) $@
616 #         @eval $(SCRIPT_SUBST)
617 #
618 #    `make foo' would create a file `foo' containing the following
619 #
620 #    % cat foo
621 #    $A=foo;
622 #    $B=bar;
623 #    %
624 #
625 # ToDo: make this work for shell scripts (drop the initial $).
626 #
627 ifeq "$(INTERP)" "$(SHELL)"
628 SCRIPT_SUBST=$(foreach val,$(SCRIPT_SUBST_VARS),"echo \"$(val)=\\\"$($(val))\\\";\" >> $@;")
629 else
630 SCRIPT_SUBST=$(foreach val,$(SCRIPT_SUBST_VARS),"echo \"$$\"\"$(val)=\\\"$($(val))\\\";\" >> $@;")
631 endif
632
633 all :: $(SCRIPT_PROG)
634
635 $(SCRIPT_PROG) : $(SCRIPT_OBJS)
636         $(RM) $@
637         @echo Creating $@...
638 ifeq "$(INTERP)" "perl"
639         echo "#! "$(PERL) > $@
640 else
641 ifneq "$(INTERP)" ""
642         @echo "#!"$(INTERP) > $@
643 else
644         @touch $@
645 endif
646 endif
647 ifneq "$(SCRIPT_PREFIX_FILES)" ""
648         @cat $(SCRIPT_PREFIX_FILES) >> $@
649 endif
650 ifneq "$(SCRIPT_SUBST)" ""
651         @eval $(SCRIPT_SUBST) 
652 endif
653         @cat $(SCRIPT_OBJS) >> $@
654         @chmod a+x $@
655         @echo Done.
656 endif
657
658 # ---------------------------------------------------------------------------
659 # Symbolic links
660
661 # links to programs: we sometimes install a program as
662 # <name>-<version> with a link from <name> to the real program.
663
664 ifneq "$(LINK)" ""
665
666 all :: $(LINK)
667
668 CLEAN_FILES += $(LINK)
669
670 ifeq "$(LINK_TARGET)" ""
671 ifneq "$(SCRIPT_PROG)" ""
672 LINK_TARGET = $(SCRIPT_PROG)
673 else
674 ifneq "$(HS_PROG)" ""
675 LINK_TARGET = $(HS_PROG)
676 else
677 ifneq "$(C_PROG)" ""
678 LINK_TARGET = $(C_PROG)
679 else
680 LINK_TARGET = dunno
681 endif
682 endif
683 endif
684 endif
685
686 #
687 # Don't want to overwrite $(LINK)s that aren't symbolic
688 # links. Testing for symbolic links is problematic to do in
689 # a portable fashion using a /bin/sh test, so we simply rely
690 # on perl.
691 #
692 $(LINK) : $(LINK_TARGET)
693         @if ( $(PERL) -e '$$fn="$(LINK)"; exit ((! -f $$fn || -l $$fn) ? 0 : 1);' ); then \
694            echo "Creating a symbolic link from $(LINK_TARGET) to $(LINK)"; \
695            $(RM) $(LINK); \
696            $(LN_S) $(LINK_TARGET) $(LINK); \
697          else \
698            echo "Creating a symbolic link from $(LINK_TARGET) to $(LINK) failed: \`$(LINK)' already exists"; \
699            echo "Perhaps remove \`$(LINK)' manually?"; \
700            exit 1; \
701          fi;
702
703
704 #
705 # install links to script drivers.
706 #
707 install ::
708         @$(INSTALL_DIR) $(bindir)
709         @if ( $(PERL) -e '$$fn="$(bindir)/$(LINK)"; exit ((! -f $$fn || -l $$fn) ? 0 : 1);' ); then \
710            echo "Creating a symbol link from $(LINK_TARGET) to $(LINK) in $(bindir)"; \
711            $(RM) $(bindir)/$(LINK); \
712            $(LN_S) $(LINK_TARGET) $(bindir)/$(LINK); \
713          else \
714            echo "Creating a symbol link from $(LINK_TARGET) to $(LINK) in $(bindir) failed: \`$(bindir)/$(LINK)' already exists"; \
715            echo "Perhaps remove \`$(bindir)/$(LINK)' manually?"; \
716            exit 1; \
717          fi;
718
719 endif # LINK 
720
721
722 ###########################################
723 #
724 #       Targets: install install-strip uninstall
725 #
726 ###########################################
727
728 # For each of these variables that is defined, you
729 # get one install rule
730 #
731 #       INSTALL_PROGS        executable programs in $(bindir)
732 #       INSTALL_SCRIPTS      executable scripts in $(bindir)
733 #       INSTALL_LIBS         platform-dependent libraries in $(libdir) (ranlib'ed)
734 #       INSTALL_LIB_SCRIPTS  platform-dependent scripts   in $(libdir)
735 #       INSTALL_LIBEXECS     platform-dependent execs in $(libdir)
736 #       INSTALL_DATAS        platform-independent files in $(datadir)
737 #
738 # If the installation directory variable is undefined, the install rule simply
739 # emits a suitable error message.
740 #
741 # Remember, too, that the installation directory variables ($(bindir) and
742 # friends can be overridden from their original settings in mk/config.mk.in
743 # || mk/build.mk
744 #
745 .PHONY: install installdirs install-strip install-dirs uninstall install-docs show-install
746
747 show-install :
748         @echo "bindir = $(bindir)"
749         @echo "libdir = $(libdir)"
750         @echo "libexecdir = $(libexecdir)  # by default, same as libdir"
751         @echo "datadir = $(datadir)  # unused for ghc project"
752
753 #
754 # Sometimes useful to separate out the creation of install directories 
755 # from the installation itself.
756 #
757 install-dirs ::
758         @$(INSTALL_DIR) $(bindir)
759         @$(INSTALL_DIR) $(libdir)
760         @$(INSTALL_DIR) $(libexecdir)
761         @$(INSTALL_DIR) $(datadir)
762
763 # Better do this first...
764 # but we won't for the moment, do it on-demand from
765 # within the various install targets instead.
766 #install:: install-dirs
767
768 # Install libraries automatically
769 ifneq "$(LIBRARY)" ""
770 INSTALL_LIBS  += $(LIBRARY)
771 ifeq "$(DLLized)" "YES"
772 INSTALL_PROGS += $(DLL_NAME)
773 INSTALL_LIBS += $(patsubst %.a,%_imp.a, $(LIBRARY))
774 endif
775 INSTALL_DATAS += $(HS_IFACES)
776 endif
777
778 ifneq "$(INSTALL_PROGS)" ""
779
780 #
781 # Here's an interesting one - when using the win32 version
782 # of install (provided via the cygwin toolkit), we have to
783 # supply the .exe suffix, *if* there's no other suffix.
784 #
785 # The rule below does this by ferreting out the suffix of each
786 # entry in the INSTALL_PROGS list. If there's no suffix, use
787 # $(exeext).
788
789 # This is bit of a pain to express since GNU make doesn't have
790 # something like $(if ...), but possible using $(subst ...)
791 # [Aside: I added support for $(if ...) to my local copy of GNU
792 # make at one stage, perhaps I should propagate the patch to
793 # the GNU make maintainers...] 
794 #
795 INSTALL_PROGS := $(foreach p, $(INSTALL_PROGS), $(addsuffix $(subst _,,$(subst __,$(exeext),_$(suffix $(p))_)), $(basename $(p))))
796
797 install:: $(INSTALL_PROGS)
798         @$(INSTALL_DIR) $(bindir)
799         @for i in $(INSTALL_PROGS); do \
800                     echo $(INSTALL_PROGRAM) $(INSTALL_BIN_OPTS) $$i $(bindir); \
801                     $(INSTALL_PROGRAM) $(INSTALL_BIN_OPTS) $$i $(bindir) ;  \
802         done
803 endif
804
805 #
806 # Just like INSTALL_PROGS, but prefix with install sites bin/lib/data and
807 # install without stripping.
808 #
809 ifneq "$(INSTALL_SCRIPTS)" ""
810 install:: $(INSTALL_SCRIPTS)
811         @$(INSTALL_DIR) $(bindir)
812         for i in $(INSTALL_SCRIPTS); do \
813                 $(INSTALL_SCRIPT) $(INSTALL_OPTS) $$i $(bindir); \
814         done
815 endif
816
817 ifneq "$(INSTALL_LIB_SCRIPTS)" ""
818 install:: $(INSTALL_LIB_SCRIPTS)
819         @$(INSTALL_DIR) $(libdir)
820         for i in $(INSTALL_LIB_SCRIPTS); do \
821                 $(INSTALL_SCRIPT) $(INSTALL_OPTS) $$i $(libdir); \
822         done
823 endif
824
825 ifneq "$(INSTALL_LIBEXEC_SCRIPTS)" ""
826 install:: $(INSTALL_LIBEXEC_SCRIPTS)
827         @$(INSTALL_DIR) $(libexecdir)
828         for i in $(INSTALL_LIBEXEC_SCRIPTS); do \
829                 $(INSTALL_SCRIPT) $(INSTALL_OPTS) $$i $(libexecdir); \
830         done
831 endif
832
833 ifneq "$(INSTALL_LIBS)" ""
834 install:: $(INSTALL_LIBS)
835         @$(INSTALL_DIR) $(libdir)
836         for i in $(INSTALL_LIBS); do \
837                 case $$i in \
838                   *.a) \
839                     $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(libdir); \
840                     $(RANLIB) $(libdir)/`basename $$i` ;; \
841                   *.dll) \
842                     $(INSTALL_DATA) -s $(INSTALL_OPTS) $$i $(libdir) ;; \
843                   *) \
844                     $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(libdir); \
845                 esac; \
846         done
847 endif
848
849 ifneq "$(INSTALL_LIBEXECS)" ""
850 #
851 # See above comment next to defn of INSTALL_PROGS for what
852 # the purpose of this one-liner is.
853
854 INSTALL_LIBEXECS := $(foreach p, $(INSTALL_LIBEXECS), $(addsuffix $(subst _,,$(subst __,$(exeext),_$(suffix $(p))_)), $(basename $(p))))
855
856 install:: $(INSTALL_LIBEXECS)
857         @$(INSTALL_DIR) $(libexecdir)
858         -for i in $(INSTALL_LIBEXECS); do \
859                 $(INSTALL_PROGRAM) $(INSTALL_BIN_OPTS) $$i $(libexecdir); \
860         done
861 endif
862
863 ifneq "$(INSTALL_DATAS)" ""
864 install:: $(INSTALL_DATAS)
865         @$(INSTALL_DIR) $(datadir)
866         for i in $(INSTALL_DATAS); do \
867                 $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(datadir); \
868         done
869 endif
870
871 ifneq "$(INSTALL_INCLUDES)" ""
872 install:: $(INSTALL_INCLUDES)
873         @$(INSTALL_DIR) $(includedir)
874         for i in $(INSTALL_INCLUDES); do \
875                 $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(includedir); \
876         done
877 endif
878
879 #
880 # Use with care..
881 #
882 uninstall:: 
883         @for i in $(INSTALL_PROGS) "" ; do                      \
884           if test "$$i"; then                                   \
885                 echo rm -f $(bindir)/`basename $$i`;            \
886                 rm -f $(bindir)/`basename $$i`;                 \
887           fi;                                                   \
888         done
889         @for i in $(INSTALL_LIBS) ""; do                        \
890           if test "$$i"; then                                   \
891                 echo rm -f $(libdir)/`basename $$i`;            \
892                 rm -f $(libdir)/`basename $$i`;                 \
893           fi;                                                   \
894         done
895         @for i in $(INSTALL_LIBEXECS) ""; do                    \
896           if test "$$i"; then                                   \
897                 echo rm -f $(libexecdir)/`basename $$i`;        \
898                 rm -f $(libexecdir)/`basename $$i`;             \
899           fi;                                                   \
900         done
901         @for i in $(INSTALL_DATAS) ""; do                       \
902           if test "$$i"; then                                   \
903                 echo rm -f $(datadir)/`basename $$i`;           \
904                 rm -f $(datadir)/`basename $$i`;                \
905           fi;                                                   \
906         done
907
908 #
909 # install-strip is from the GNU Makefile standard.
910 #
911 ifneq "$(way)" ""
912 install-strip::
913         @$(MAKE) EXTRA_INSTALL_OPTS='-s' install                                        
914 endif
915
916 ###########################################
917 #
918 #       Targets: check tags show
919 #
920 ###########################################
921
922 #------------------------------------------------------------
923 #                       Check
924
925 .PHONY: check
926
927 check:: $(TESTS)
928         @for i in $(filter-out %.lhs .hs, $(TESTS)) ''; do      \
929           if (test -f "$$i"); then              \
930             echo Running: `basename $$i` ;      \
931             cd test; `basename $$i` ;           \
932           fi;                                   \
933         done;
934
935 #------------------------------------------------------------
936 #                       Tags
937
938 .PHONY: TAGS tags
939
940 tags TAGS:: $(TAGS_HS_SRCS) $(TAGS_C_SRCS)
941         @$(RM) TAGS
942         @touch TAGS
943 ifneq "$(TAGS_HS_SRCS)" ""
944         $(HSTAGS) $(HSTAGS_OPTS) -- $(TAGS_HS_SRCS)
945 endif
946 ifneq "$(TAGS_C_SRCS)" ""
947         etags -a $(TAGS_C_SRCS)
948 endif
949         @( DEREFFED=`ls -l Makefile | sed -e 's/.*-> \(.*\)/\1/g'` && $(RM) `dirname $$DEREFFED`/TAGS && $(CP) TAGS `dirname $$DEREFFED` ) 2>/dev/null || echo TAGS file generated, perhaps copy over to source tree?
950
951 #------------------------------------------------------------
952 #                       Makefile debugging
953 # to see the effective value used for a Makefile variable, do
954 #  make show VALUE=MY_VALUE
955 #
956
957 show:
958         @echo '$(VALUE)=$($(VALUE))'
959
960 #--------------------------------------------------------------------------
961 # SGML Documentation
962 #
963 .PHONY: dvi ps html pdf rtf
964
965 ifneq "$(SGML_DOC)" ""
966
967 # multi-file SGML document: main document name is specified in $(SGML_DOC),
968 # sub-documents (.sgml files) listed in $(SGML_SRCS).
969
970 ifeq "$(SGML_SRCS)" ""
971 SGML_SRCS = $(wildcard *.sgml)
972 endif
973
974 SGML_TEX  = $(addsuffix .tex,$(SGML_DOC))
975 SGML_DVI  = $(addsuffix .dvi,$(SGML_DOC))
976 SGML_PS   = $(addsuffix .ps,$(SGML_DOC))
977 SGML_PDF  = $(addsuffix .pdf,$(SGML_DOC))
978 SGML_RTF  = $(addsuffix .rtf,$(SGML_DOC))
979 SGML_HTML = $(addsuffix .html,$(SGML_DOC))
980 # HTML output goes in a subdirectory on its own.
981 SGML_TEXT = $(addsuffix .txt,$(SGML_DOC))
982
983 $(SGML_DVI) $(SGML_PS) $(SGML_HTML) $(SGML_TEXT) $(SGML_PDF) :: $(SGML_SRCS)
984
985 dvi  :: $(SGML_DVI)
986 ps   :: $(SGML_PS)
987 pdf  :: $(SGML_PDF)
988 rtf  :: $(SGML_RTF)
989 html :: $(SGML_HTML)
990 txt  :: $(SGML_TEXT)
991
992 CLEAN_FILES += $(SGML_TEXT) $(SGML_TEX) $(SGML_PS) $(SGML_DVI) $(SGML_PDF) $(SGML_RTF) $(SGML_HTML) $(SGML_DOC)-*.html
993 # can't use $(SGML_SRCS) here, it was maybe used elsewhere
994
995 extraclean ::
996         $(RM) -rf DBTOHTML_OUTPUT_*
997         $(RM) -rf *.junk/
998         $(RM) -rf $(SGML_DOC)
999 endif
1000
1001 ###########################################
1002 #
1003 #       Targets: clean
1004 #
1005 ###########################################
1006
1007 # we have to be careful about recursion here; since all the clean
1008 # targets are recursive, we don't want to make eg. distclean depend on
1009 # clean because that would result in far too many recursive calls.
1010
1011 .PHONY: mostlyclean clean distclean maintainer-clean
1012
1013 mostlyclean::
1014         rm -f $(MOSTLY_CLEAN_FILES)
1015
1016 # extraclean is used for adding actions to the clean target.
1017 extraclean::
1018
1019 clean:: extraclean
1020         rm -f $(MOSTLY_CLEAN_FILES) $(CLEAN_FILES)
1021
1022 distclean:: extraclean
1023         rm -f $(MOSTLY_CLEAN_FILES) $(CLEAN_FILES) $(DIST_CLEAN_FILES)
1024
1025 maintainer-clean:: extraclean
1026         @echo 'This command is intended for maintainers to use; it'
1027         @echo 'deletes files that may need special tools to rebuild.'
1028         rm -f $(MOSTLY_CLEAN_FILES) $(CLEAN_FILES) $(DIST_CLEAN_FILES) $(MAINTAINER_CLEAN_FILES)
1029
1030 #################################################################################
1031 #
1032 #                       Way management
1033 #
1034 #################################################################################
1035
1036 # Here is the ingenious jiggery pokery that allows you to build multiple versions
1037 # of a program in a single build tree.
1038 #
1039 # The ways setup requires the following variables to be set:
1040 #
1041 # Expects:      $(WAYS)                 the possible "way" strings to one of 
1042 #                                       which $(way) will be set
1043
1044
1045 # So how does $(way) ever get set to anything?  Answer, we recursively
1046 # invoke make, setting $(way) on the command line.
1047 # When do we do this recursion?  Answer: whenever the programmer
1048 # asks make to make a target that involves a way suffix.
1049 # We must remember *not* to recurse again; but that's easy: we
1050 # just see if $(way) is set:
1051
1052 ifeq "$(way)" ""
1053
1054 # If $(WAYS) = p mc, then WAY_TARGETS expands to
1055 #       %.p_lhs %.p_hs %.p_o ... %.mc_lhs %.p_hs ...
1056 # and OTHER_WAY_TARGETS to
1057 #       %_p.a %_p %_mc.a %_mc
1058 # where the suffixes are from $(SUFFIXES)
1059 #
1060 # We have to treat libraries and "other" targets differently, 
1061 # because their names are of the form
1062 #       libHS_p.a and Foo_p
1063 # whereas everything else has names of the form
1064 #       Foo.p_o
1065
1066 FPTOOLS_SUFFIXES := o hi hc
1067
1068 WAY_TARGETS     = $(foreach way,$(WAYS),$(foreach suffix, $(FPTOOLS_SUFFIXES), %.$(way)_$(suffix)))
1069 LIB_WAY_TARGETS = $(foreach way,$(WAYS),%_$(way).a %_$(way))
1070
1071 # $@ will be something like Foo.p_o
1072 # $(suffix $@)     returns .p_o
1073 # $(subst .,.p_o)  returns p_o
1074 # $(subst _,.,p_o) returns p.o   (clever)
1075 # $(basename p.o)  returns p
1076
1077 $(WAY_TARGETS) :
1078         $(MAKE) way=$(basename $(subst _,.,$(subst .,,$(suffix $@)))) $@
1079
1080 # $(@F) will be something like libHS_p.a, or Foo_p
1081 # $(basename $(@F)) will be libHS_p, or Foo_p
1082 # The sed script extracts the "p" part.
1083
1084 $(LIB_WAY_TARGETS) :
1085         $(MAKE) $(MFLAGS) $@ way=$(subst .,,$(suffix $(subst _,.,$(basename $@))))
1086
1087 endif   # if way
1088
1089 ifneq "$(WAYS)" ""
1090 ifeq "$(way)" ""
1091
1092 # NB: the targets exclude 
1093 #       boot runtests
1094 # since these are way-independent
1095 all docs TAGS clean distclean mostlyclean maintainer-clean install ::
1096         @echo "------------------------------------------------------------------------"
1097         @echo "===fptools== Recursively making \`$@' for ways: $(WAYS) ..."
1098         @echo "PWD = $(shell pwd)"
1099         @echo "------------------------------------------------------------------------"
1100 # Don't rely on -e working, instead we check exit return codes from sub-makes.
1101         @case '${MFLAGS}' in *-[ik]*) x_on_err=0;; *-r*[ik]*) x_on_err=0;; *) x_on_err=1;; esac; \
1102         for i in $(WAYS) ; do \
1103           echo "------------------------------------------------------------------------"; \
1104           echo "==fptools== $(MAKE) way=$$i $@;"; \
1105           echo "PWD = $(shell pwd)"; \
1106           echo "------------------------------------------------------------------------"; \
1107           $(MAKE) way=$$i --no-print-directory $(MFLAGS) $@ ; \
1108           if [ $$? -eq 0 ] ; then true; else exit $$x_on_err; fi; \
1109         done
1110         @echo "------------------------------------------------------------------------"
1111         @echo "===fptools== Finished recursively making \`$@' for ways: $(WAYS) ..."
1112         @echo "PWD = $(shell pwd)"
1113         @echo "------------------------------------------------------------------------"
1114
1115 endif
1116 endif