1 # -----------------------------------------------------------------------------
2 # Main ghc/compiler Makefile
6 # all builds stage1 compiler
8 # boot stage=N generate build dirs and dependencies for stage N.
9 # NB. Must be done before 'make stageN'.
10 # NB. Cannot 'make boot stage=2' until stage1 has
11 # been built (similarly for stage3).
13 # stage1 (or stage=1) builds stage1 compiler
14 # stage2 (or stage=2) builds stage2 compiler
15 # stage3 (or stage=3) builds stage3 compiler
20 # Use GHC for compiling C bits (NB. must be before boilerplate include)
22 ifneq "$(BootingFromHc)" "YES"
26 include $(TOP)/mk/boilerplate.mk
28 #-----------------------------------------------------------------------------
29 # Counting source code lines
31 USER_SRCS = $(filter-out $(DERIVED_SRCS),$(SRCS))
33 ./count_lines $(USER_SRCS)
35 #-----------------------------------------------------------------------------
36 # Building ghc different ways (default is just `normal' sequential)
38 WAYS=$(GhcCompilerWays)
40 # -----------------------------------------------------------------------------
43 # The stage1/stage2/stage3 business is quite delicate. Here's how it works:
45 # - the variable $(stage) holds the current stage number. To build a
46 # particular stage, you say 'make stage=N' where N is 1, 2, or 3.
49 # - for stage N, object files and .hi files are placed inside
50 # the directory stageN, in subdirectories as per the sources.
52 # - .hi-boot files are *linked* into the stageN tree, because in GHC 5.05+
53 # the .hi-boot file must reside in the same place as the .hi file.
55 # - we use explicit -o and -ohi options to direct the output from C &
56 # Haskell compilations.
58 # - we generate a different .depend file for each build. They need to be
59 # different, because each stage might include different files: stage1
60 # might not include GHCi, for example. For each stage, a normal .depend
61 # file is generated, and then post-processed to add the correct stageN/
62 # prefix to each object and .hi filename. The resulting .depend file
63 # is named .depend-$(stage). See the end of this Makefile for details.
65 # - normal implicit rules don't work any more, because they're of the form
69 # whereas we really need
73 # so suffix.mk now defines the appropriate suffix rules when
74 # $(odir) is set to a non-empty value. Here we set $(odir) to
75 # stage1, stage2, or stage3.
77 # There are other plausible designs that might work, but each has different
80 # - using -odir and -hidir: GHC <= 4.08 doesn't support -hidir, and
81 # anyway -odir puts all the objects in one directory (strips off the
82 # subdirectory part), which eventually forces us to use VPATH to find
83 # the sources. I have a really bad feeling about VPATH.
85 # - invoke make in the stageN subdirectory. This probably requires VPATH
88 # - create a link tree. The problem with requiring link trees is that
89 # Windows doesn't support symbolic links.
92 $(MKDIRHIER) stage$(stage)
93 for i in $(ALL_DIRS); do \
94 $(MKDIRHIER) stage$(stage)/$$i; \
96 # On Windows, we can't use symbolic links for the -hi-boot files
97 # because GHC itself is a Mingw program and does not understand
98 # symbolic links. So we have to copy the files instead of link them.
99 # That means that if you modify a .hi-boot file in Windows, you
100 # have to to say 'make boot' again.
102 # PS: 'ln -s foo baz' takes 'foo' relative to the path to 'baz'
103 # whereas 'cp foo baz' treats the two paths independently.
104 # Hence the "../.." in the ln command line
105 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
106 for i in */*hi-boot*; do \
109 for i in */*hi-boot*; do \
110 $(LN_S) -f ../../$$i stage$(stage)/$$i; \
141 SRC_HC_OPTS += $(patsubst %, -i$(odir)/%, $(ALL_DIRS))
143 HS_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(HS_SRCS))))
144 C_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(C_SRCS))))
146 CLEAN_FILES += $(odir)/*/*.hi
149 mostlyclean clean distclean maintainer-clean ::
154 # -----------------------------------------------------------------------------
157 # Note: there have been reports of people running up against the ARG_MAX limit
158 # when linking ghc with all its constituent object files. The likely source of
159 # the problem is that the environment is a bit too big, so a workaround could
160 # be to do `env PATH=$(PATH) make ghc' to minimise the environment. (or the
161 # equivalent of `env' if it doesn't exist locally).
164 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
165 HS_PROG=$(odir)/ghc$(_way)-$(ProjectVersion)
167 HS_PROG=$(odir)/ghc$(_way)
170 HS_PROG=$(odir)/ghc-$(ProjectVersion)
173 # -----------------------------------------------------------------------------
174 # Create compiler configuration
176 # The 'echo' commands simply spit the values of various make variables
177 # into Config.hs, whence they can be compiled and used by GHC itself
179 CONFIG_HS = main/Config.hs
182 $(CONFIG_HS) : $(FPTOOLS_TOP)/mk/config.mk Makefile
183 @$(RM) -f $(CONFIG_HS)
184 @echo -n "Creating $(CONFIG_HS) ... "
185 @echo "module Config where" >>$(CONFIG_HS)
186 @echo "cProjectName = \"$(ProjectName)\"" >> $(CONFIG_HS)
187 @echo "cProjectVersion = \"$(ProjectVersion)\"" >> $(CONFIG_HS)
188 @echo "cProjectVersionInt = \"$(ProjectVersionInt)\"" >> $(CONFIG_HS)
189 @echo "cProjectPatchLevel = \"$(ProjectPatchLevel)\"" >> $(CONFIG_HS)
190 @echo "cBooterVersion = \"$(GhcVersion)\"" >> $(CONFIG_HS)
191 @echo "cHscIfaceFileVersion = \"$(HscIfaceFileVersion)\"" >> $(CONFIG_HS)
192 @echo "cHOSTPLATFORM = \"$(HOSTPLATFORM)\"" >> $(CONFIG_HS)
193 @echo "cTARGETPLATFORM = \"$(TARGETPLATFORM)\"" >> $(CONFIG_HS)
194 @echo "cGhcWithNativeCodeGen = \"$(GhcWithNativeCodeGen)\"" >> $(CONFIG_HS)
195 @echo "cGhcUnregisterised = \"$(GhcUnregisterised)\"" >> $(CONFIG_HS)
196 @echo "cLeadingUnderscore = \"$(LeadingUnderscore)\"" >> $(CONFIG_HS)
197 @echo "cRAWCPP_FLAGS = \"$(RAWCPP_FLAGS)\"" >> $(CONFIG_HS)
198 @echo "cGCC = \"$(WhatGccIsCalled)\"" >> $(CONFIG_HS)
199 @echo "cMKDLL = \"$(BLD_DLL)\"" >> $(CONFIG_HS)
200 @echo "cPROJECT_DIR = \"$(PROJECT_DIR)\"" >> $(CONFIG_HS)
201 @echo "cGHC_DRIVER_DIR_REL = \"$(GHC_DRIVER_DIR_REL)\"" >> $(CONFIG_HS)
202 @echo "cGHC_TOUCHY_PGM = \"$(GHC_TOUCHY_PGM)\"" >> $(CONFIG_HS)
203 @echo "cGHC_TOUCHY_DIR_REL = \"$(GHC_TOUCHY_DIR_REL)\"" >> $(CONFIG_HS)
204 @echo "cGHC_UNLIT_PGM = \"$(GHC_UNLIT_PGM)\"" >> $(CONFIG_HS)
205 @echo "cGHC_UNLIT_DIR_REL = \"$(GHC_UNLIT_DIR_REL)\"" >> $(CONFIG_HS)
206 @echo "cGHC_MANGLER_PGM = \"$(GHC_MANGLER_PGM)\"" >> $(CONFIG_HS)
207 @echo "cGHC_MANGLER_DIR_REL = \"$(GHC_MANGLER_DIR_REL)\"" >> $(CONFIG_HS)
208 @echo "cGHC_SPLIT_PGM = \"$(GHC_SPLIT_PGM)\"" >> $(CONFIG_HS)
209 @echo "cGHC_SPLIT_DIR_REL = \"$(GHC_SPLIT_DIR_REL)\"" >> $(CONFIG_HS)
210 @echo "cGHC_SYSMAN_PGM = \"$(GHC_SYSMAN)\"" >> $(CONFIG_HS)
211 @echo "cGHC_SYSMAN_DIR_REL = \"$(GHC_SYSMAN_DIR)\"" >> $(CONFIG_HS)
212 @echo "cGHC_CP = \"$(GHC_CP)\"" >> $(CONFIG_HS)
213 @echo "cGHC_PERL = \"$(GHC_PERL)\"" >> $(CONFIG_HS)
214 ifeq ($(GhcWithIlx),YES)
215 @echo "cILX2IL = \"$(ILX2IL)\"" >> $(CONFIG_HS)
216 @echo "cILASM = \"$(ILASM)\"" >> $(CONFIG_HS)
218 @echo "cEnableWin32DLLs = \"$(EnableWin32DLLs)\"" >> $(CONFIG_HS)
219 @echo "cCONTEXT_DIFF = \"$(CONTEXT_DIFF)\"" >> $(CONFIG_HS)
220 @echo "cHaveLibGmp = \"$(HaveLibGmp)\"" >> $(CONFIG_HS)
221 @echo "cUSER_WAY_NAMES = \"$(USER_WAY_NAMES)\"" >> $(CONFIG_HS)
222 @echo "cUSER_WAY_OPTS = \"$(USER_WAY_OPTS)\"" >> $(CONFIG_HS)
223 @echo "cDEFAULT_TMPDIR = \"$(DEFAULT_TMPDIR)\"" >> $(CONFIG_HS)
226 CLEAN_FILES += $(CONFIG_HS)
228 # -----------------------------------------------------------------------------
231 # First figure out ALL_DIRS, the source sub-directories
234 utils basicTypes types hsSyn prelude rename typecheck deSugar coreSyn \
235 specialise simplCore stranal stgSyn simplStg codeGen absCSyn main \
236 profiling parser usageSP cprAnalysis compMan ndpFlatten
238 # Make sure we include Config.hs even if it doesn't exist yet...
239 ALL_SRCS += $(CONFIG_HS)
241 # HsGeneric.hs is not used just now
242 EXCLUDED_SRCS += hsSyn/HsGeneric.hs
244 ifeq ($(GhcWithNativeCodeGen),YES)
245 ALL_DIRS += nativeGen
247 SRC_HC_OPTS += -DOMIT_NATIVE_CODEGEN
250 ifeq ($(GhcWithIlx),YES)
255 ifeq ($(GhcWithJavaGen),YES)
257 SRC_HC_OPTS += -DJAVA
260 ifeq "$(BootingFromHc)" "YES"
261 # HC files are always from a self-booted compiler
263 compiling_with_4xx=NO
265 ifneq "$(findstring $(stage), 2 3)" ""
267 compiling_with_4xx = NO
269 bootstrapped = $(shell if (test $(GhcCanonVersion) -ge $(ProjectVersionInt) -a $(GhcPatchLevel) -ge $(ProjectPatchLevel)); then echo YES; else echo NO; fi)
270 compiling_with_4xx = $(shell if (test $(GhcCanonVersion) -lt 500); then echo YES; else echo NO; fi)
274 # Only include GHCi if we're bootstrapping with at least version 411
275 ifeq "$(GhcWithInterpreter) $(bootstrapped)" "YES YES"
276 # Yes, include the interepreter, readline, and Template Haskell extensions
277 SRC_HC_OPTS += -DGHCI -package haskell-src
278 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
279 SRC_HC_OPTS += -package unix -package readline
283 # No interpreter, so exclude Template Haskell modules
284 EXCLUDED_SRCS += deSugar/DsMeta.hs typecheck/TcSplice.lhs hsSyn/Convert.lhs
287 # There are some C files to include in HS_PROG, so add these to HS_OBJS
290 # -----------------------------------------------
293 # Big Fudge to get around inherent problem that Makefile setup
294 # has got with 'mkdependC'.
296 SRC_MKDEPENDC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt) -I$(GHC_INCLUDE_DIR)
298 # -----------------------------------------------------------------------------
299 # Haskell compilations
302 -cpp -fglasgow-exts -Rghc-timing \
303 -I. -IcodeGen -InativeGen -Iparser
305 # Omitted: -I$(GHC_INCLUDE_DIR)
306 # We should have -I$(GHC_INCLUDE_DIR) in SRC_HC_OPTS,
307 # to avoid the use of an explicit path in GHC source files
308 # (include "../includes/config.h"
309 # But alas GHC 4.08 (and others for all I know) uses this very
310 # same include path when compiling the .hc files it generates.
311 # Disaster! Then the hc file sees the GHC 5.02 (or whatever)
312 # include files. For the moment we've reverted to using
313 # an explicit path in the .hs sources
315 # For the benefit of <5.00 compilers, do include GHC_INCLUDE_DIR
316 # when generating dependencies. (=> it gets passed onto mkdependHS,
318 SRC_MKDEPENDHS_OPTS += -I$(GHC_INCLUDE_DIR)
320 # When bootstrapped, we don't make use of *any* packages
321 # (except possibly readline if GHCi is enabled, see above)
322 ifneq "$(bootstrapped)" "YES"
323 ifneq "$(mingw32_HOST_OS)" "1"
324 SRC_HC_OPTS += -package concurrent -package posix -package util
326 SRC_HC_OPTS += -package concurrent -package util
330 SRC_CC_OPTS += -Iparser -I. -O
331 SRC_HC_OPTS += -recomp $(GhcHcOpts)
334 ifeq "$(BootingFromHc)" "YES"
335 SRC_CC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt)
338 # Special flags for particular modules
339 # The standard suffix rule for compiling a Haskell file
340 # adds these flags to the command line
342 # not sure if this is required --SDM
343 main/Main_HC_OPTS = -fvia-C
345 prelude/PrimOp_HC_OPTS = -no-recomp -H80m
347 # because the NCG can't handle the 64-bit math in here
348 prelude/PrelRules_HC_OPTS = -fvia-C
350 main/ParsePkgConf_HC_OPTS += -fno-warn-incomplete-patterns
351 # Use -fvia-C since the NCG can't handle the narrow16Int# (and intToInt16#?)
352 # primops on all platforms.
353 parser/Parser_HC_OPTS += -Onot -fno-warn-incomplete-patterns -fvia-C
355 # The latest GHC version doesn't have a -K option yet, and it doesn't
356 # seem to be necessary anymore for the modules below.
357 ifeq "$(compiling_with_4xx)" "YES"
358 parser/Parser_HC_OPTS += -K2m
361 ifeq "$(HOSTPLATFORM)" "hppa1.1-hp-hpux9"
362 rename/RnMonad_HC_OPTS = -O2 -O2-for-C
365 utils/StringBuffer_HC_OPTS = -fvia-C -fno-prune-tydecls
366 utils/Digraph_HC_OPTS = -fglasgow-exts
368 ifeq "$(bootstrapped)" "YES"
369 utils/Binary_HC_OPTS = -funbox-strict-fields
372 # 4.08.2's NCG can't cope with Binary
373 ifeq "$(compiling_with_4xx)" "YES"
374 utils/Binary_HC_OPTS += -fvia-C
377 # ByteCodeItbls uses primops that the NCG doesn't support yet.
378 ghci/ByteCodeItbls_HC_OPTS = -fvia-C
379 ghci/ByteCodeLink_HC_OPTS = -fvia-C -monly-3-regs
381 # CSE interacts badly with top-level IORefs (reportedly in DriverState and
382 # DriverMkDepend), causing some of them to be commoned up. We have a fix for
383 # this in 5.00+, but earlier versions of the compiler will need CSE turned off.
384 # To be on the safe side, we disable CSE in *all* modules with top-level IORefs.
385 compMan/CompManager_HC_OPTS = -fno-cse
386 ghci/InteractiveUI_HC_OPTS = -fno-cse
387 main/CmdLineOpts_HC_OPTS = -fno-cse
388 main/DriverFlags_HC_OPTS = -fno-cse
389 main/DriverMkDepend_HC_OPTS = -fno-cse
390 main/DriverPipeline_HC_OPTS = -fno-cse
391 main/DriverState_HC_OPTS = -fno-cse
392 main/DriverUtil_HC_OPTS = -fno-cse
393 main/Finder_HC_OPTS = -fno-cse
394 main/SysTools_HC_OPTS = -fno-cse
396 # The #include is vital for the via-C route, else the C
397 # compiler doesn't realise that the stcall foreign imports are indeed
398 # stdcall, and doesn't generate the Foo@8 name for them
399 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
400 main/SysTools_HC_OPTS += '-\#include <windows.h>' '-\#include <process.h>'
403 # ghc_strlen percolates through so many modules that it is easier to get its
404 # prototype via a global option instead of a myriad of per-file OPTIONS
405 SRC_HC_OPTS += '-\#include "hschooks.h"'
407 # ----------------------------------------------------------------------------
408 # Generate supporting stuff for prelude/PrimOp.lhs
409 # from prelude/primops.txt
411 GENPOC=$(TOP)/utils/genprimopcode/genprimopcode
413 PRIMOP_BITS=primop-data-decl.hs-incl \
415 primop-list.hs-incl \
416 primop-has-side-effects.hs-incl \
417 primop-out-of-line.hs-incl \
418 primop-commutable.hs-incl \
419 primop-needs-wrapper.hs-incl \
420 primop-can-fail.hs-incl \
421 primop-strictness.hs-incl \
422 primop-usage.hs-incl \
423 primop-primop-info.hs-incl
425 CLEAN_FILES += prelude/primops.txt
426 CLEAN_FILES += $(PRIMOP_BITS)
428 SRC_CPP_OPTS += -I$(GHC_INCLUDE_DIR) -traditional
429 SRC_CPP_OPTS += ${GhcCppOpts}
431 ifneq "$(BootingFromHc)" "YES"
432 prelude/PrimOp.lhs prelude/PrimOp.o: $(PRIMOP_BITS)
435 ifneq "$(BootingFromHc)" "YES"
436 depend :: $(PRIMOP_BITS)
439 primop-data-decl.hs-incl: prelude/primops.txt
440 $(GENPOC) --data-decl < $< > $@
441 primop-tag.hs-incl: prelude/primops.txt
442 $(GENPOC) --primop-tag < $< > $@
443 primop-list.hs-incl: prelude/primops.txt
444 $(GENPOC) --primop-list < $< > $@
445 primop-has-side-effects.hs-incl: prelude/primops.txt
446 $(GENPOC) --has-side-effects < $< > $@
447 primop-out-of-line.hs-incl: prelude/primops.txt
448 $(GENPOC) --out-of-line < $< > $@
449 primop-commutable.hs-incl: prelude/primops.txt
450 $(GENPOC) --commutable < $< > $@
451 primop-needs-wrapper.hs-incl: prelude/primops.txt
452 $(GENPOC) --needs-wrapper < $< > $@
453 primop-can-fail.hs-incl: prelude/primops.txt
454 $(GENPOC) --can-fail < $< > $@
455 primop-strictness.hs-incl: prelude/primops.txt
456 $(GENPOC) --strictness < $< > $@
457 primop-usage.hs-incl: prelude/primops.txt
458 $(GENPOC) --usage < $< > $@
459 primop-primop-info.hs-incl: prelude/primops.txt
460 $(GENPOC) --primop-primop-info < $< > $@
464 # ----------------------------------------------------------------------------
467 SRC_HAPPY_OPTS += +RTS -K2m -H16m -RTS $(GHC_HAPPY_OPTS)
469 #-----------------------------------------------------------------------------
472 SRC_LD_OPTS += -no-link-chk
474 # -----------------------------------------------------------------------------
475 # create ghc-inplace, a convenient way to run ghc from the build tree...
477 all :: $(odir)/ghc-inplace ghc-inplace
479 $(odir)/ghc-inplace : $(HS_PROG)
481 echo '#!/bin/sh' >>$@
482 echo exec $(FPTOOLS_TOP_ABS)/ghc/compiler/$(HS_PROG) '-B$(subst \,\\,$(FPTOOLS_TOP_ABS_PLATFORM))' '"$$@"' >>$@
485 ghc-inplace : stage1/ghc-inplace
488 CLEAN_FILES += $(odir)/ghc-inplace ghc-inplace
490 #-----------------------------------------------------------------------------
493 # We don't want ghc treated as an ordinary executable,
494 # but put it together with the libraries.
495 # Also don't want any interface files installed
497 DESTDIR = $(INSTALL_LIBRARY_DIR_GHC)
499 ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
500 INSTALL_LIBEXECS += $(HS_PROG)
502 INSTALL_PROGS += $(HS_PROG)
505 # ----------------------------------------------------------------------------
508 # rename/Rename_HC_OPTS += -auto-all
509 # rename/RnEnv_HC_OPTS += -auto-all
510 # rename/RnHiFiles_HC_OPTS += -auto-all
511 # rename/RnIfaces_HC_OPTS += -auto-all
512 # rename/RnSource_HC_OPTS += -auto-all
513 # rename/RnBinds_HC_OPTS += -auto-all
514 # rename/RnExpr_HC_OPTS += -auto-all
515 # rename/RnHsSyn_HC_OPTS += -auto-all
516 # rename/RnNames_HC_OPTS += -auto-all
517 # rename/RnTypes_HC_OPTS += -auto-all
519 #-----------------------------------------------------------------------------
522 MAINTAINER_CLEAN_FILES += parser/Parser.info main/ParsePkgConf.info
524 #-----------------------------------------------------------------------------
525 # Include target-rule boilerplate
527 # Don't use the default MKDEPENDHS stuff... we'll do our own, below
531 include $(TOP)/mk/target.mk
533 # -----------------------------------------------------------------------------
536 MKDEPENDHS_HC_OPTS = $(patsubst -i$(odir)/%, -i%, $(HC_OPTS))
540 # Must do this *after* including target.mk, because $(HS_SRCS) isn't set yet.
541 depend :: $(HS_SRCS) $(C_SRCS)
542 $(MKDEPENDHS) -M -optdep-f -optdep.depend-BASE $(foreach way,$(WAYS),-optdep-s -optdep$(way)) $(foreach obj,$(MKDEPENDHS_OBJ_SUFFICES),-osuf $(obj)) $(MKDEPENDHS_OPTS) $(filter-out -split-objs, $(MKDEPENDHS_HC_OPTS)) $(HS_SRCS)
543 $(MKDEPENDC) -f .depend-BASE $(MKDEPENDC_OPTS) $(foreach way,$(WAYS),-s $(way)) -- $(CC_OPTS) -- $(C_SRCS)
544 $(PERL) -pe 'binmode(stdin); binmode(stdout); s@^(\S*\.o)@stage$(stage)/$$1@g; s@(\S*\.hi)@stage$(stage)/$$1@g' <.depend-BASE >.depend-$(stage)
545 # The binmode stuff tells perl not to add stupid ^M's to the output
547 -include .depend-$(stage)