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