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 for i in */*hi-boot*; do \
97 $(LN_S) -f ../../$$i stage$(stage)/$$i; \
127 SRC_HC_OPTS += $(patsubst %, -i$(odir)/%, $(ALL_DIRS))
129 HS_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(HS_SRCS))))
130 C_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(C_SRCS))))
132 CLEAN_FILES += $(odir)/*/*.hi
135 mostlyclean clean distclean maintainer-clean ::
140 # -----------------------------------------------------------------------------
143 # Note: there have been reports of people running up against the ARG_MAX limit
144 # when linking ghc with all its constituent object files. The likely source of
145 # the problem is that the environment is a bit too big, so a workaround could
146 # be to do `env PATH=$(PATH) make ghc' to minimise the environment. (or the
147 # equivalent of `env' if it doesn't exist locally).
150 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
151 HS_PROG=$(odir)/ghc$(_way)-$(ProjectVersion)
153 HS_PROG=$(odir)/ghc$(_way)
156 HS_PROG=$(odir)/ghc-$(ProjectVersion)
159 # -----------------------------------------------------------------------------
160 # Create compiler configuration
162 # The 'echo' commands simply spit the values of various make variables
163 # into Config.hs, whence they can be compiled and used by GHC itself
165 CONFIG_HS = main/Config.hs
168 $(CONFIG_HS) : $(FPTOOLS_TOP)/mk/config.mk Makefile
169 @$(RM) -f $(CONFIG_HS)
170 @echo -n "Creating $(CONFIG_HS) ... "
171 @echo "module Config where" >>$(CONFIG_HS)
172 @echo "cProjectName = \"$(ProjectName)\"" >> $(CONFIG_HS)
173 @echo "cProjectVersion = \"$(ProjectVersion)\"" >> $(CONFIG_HS)
174 @echo "cProjectVersionInt = \"$(ProjectVersionInt)\"" >> $(CONFIG_HS)
175 @echo "cProjectPatchLevel = \"$(ProjectPatchLevel)\"" >> $(CONFIG_HS)
176 @echo "cBooterVersion = \"$(GhcVersion)\"" >> $(CONFIG_HS)
177 @echo "cHscIfaceFileVersion = \"$(HscIfaceFileVersion)\"" >> $(CONFIG_HS)
178 @echo "cHOSTPLATFORM = \"$(HOSTPLATFORM)\"" >> $(CONFIG_HS)
179 @echo "cTARGETPLATFORM = \"$(TARGETPLATFORM)\"" >> $(CONFIG_HS)
180 @echo "cGhcWithNativeCodeGen = \"$(GhcWithNativeCodeGen)\"" >> $(CONFIG_HS)
181 @echo "cGhcUnregisterised = \"$(GhcUnregisterised)\"" >> $(CONFIG_HS)
182 @echo "cLeadingUnderscore = \"$(LeadingUnderscore)\"" >> $(CONFIG_HS)
183 @echo "cRAWCPP_FLAGS = \"$(RAWCPP_FLAGS)\"" >> $(CONFIG_HS)
184 @echo "cGCC = \"$(WhatGccIsCalled)\"" >> $(CONFIG_HS)
185 @echo "cMKDLL = \"$(BLD_DLL)\"" >> $(CONFIG_HS)
186 @echo "cPROJECT_DIR = \"$(PROJECT_DIR)\"" >> $(CONFIG_HS)
187 @echo "cGHC_DRIVER_DIR_REL = \"$(GHC_DRIVER_DIR_REL)\"" >> $(CONFIG_HS)
188 @echo "cGHC_TOUCHY_PGM = \"$(GHC_TOUCHY_PGM)\"" >> $(CONFIG_HS)
189 @echo "cGHC_TOUCHY_DIR_REL = \"$(GHC_TOUCHY_DIR_REL)\"" >> $(CONFIG_HS)
190 @echo "cGHC_UNLIT_PGM = \"$(GHC_UNLIT_PGM)\"" >> $(CONFIG_HS)
191 @echo "cGHC_UNLIT_DIR_REL = \"$(GHC_UNLIT_DIR_REL)\"" >> $(CONFIG_HS)
192 @echo "cGHC_MANGLER_PGM = \"$(GHC_MANGLER_PGM)\"" >> $(CONFIG_HS)
193 @echo "cGHC_MANGLER_DIR_REL = \"$(GHC_MANGLER_DIR_REL)\"" >> $(CONFIG_HS)
194 @echo "cGHC_SPLIT_PGM = \"$(GHC_SPLIT_PGM)\"" >> $(CONFIG_HS)
195 @echo "cGHC_SPLIT_DIR_REL = \"$(GHC_SPLIT_DIR_REL)\"" >> $(CONFIG_HS)
196 @echo "cGHC_SYSMAN_PGM = \"$(GHC_SYSMAN)\"" >> $(CONFIG_HS)
197 @echo "cGHC_SYSMAN_DIR_REL = \"$(GHC_SYSMAN_DIR)\"" >> $(CONFIG_HS)
198 @echo "cGHC_CP = \"$(GHC_CP)\"" >> $(CONFIG_HS)
199 @echo "cGHC_PERL = \"$(GHC_PERL)\"" >> $(CONFIG_HS)
200 ifeq ($(GhcWithIlx),YES)
201 @echo "cILX2IL = \"$(ILX2IL)\"" >> $(CONFIG_HS)
202 @echo "cILASM = \"$(ILASM)\"" >> $(CONFIG_HS)
204 @echo "cEnableWin32DLLs = \"$(EnableWin32DLLs)\"" >> $(CONFIG_HS)
205 @echo "cCONTEXT_DIFF = \"$(CONTEXT_DIFF)\"" >> $(CONFIG_HS)
206 @echo "cHaveLibGmp = \"$(HaveLibGmp)\"" >> $(CONFIG_HS)
207 @echo "cUSER_WAY_NAMES = \"$(USER_WAY_NAMES)\"" >> $(CONFIG_HS)
208 @echo "cUSER_WAY_OPTS = \"$(USER_WAY_OPTS)\"" >> $(CONFIG_HS)
209 @echo "cDEFAULT_TMPDIR = \"$(DEFAULT_TMPDIR)\"" >> $(CONFIG_HS)
212 CLEAN_FILES += $(CONFIG_HS)
214 # -----------------------------------------------------------------------------
217 # First figure out ALL_DIRS, the source sub-directories
220 utils basicTypes types hsSyn prelude rename typecheck deSugar coreSyn \
221 specialise simplCore stranal stgSyn simplStg codeGen absCSyn main \
222 profiling parser usageSP cprAnalysis compMan ndpFlatten
224 # Make sure we include Config.hs even if it doesn't exist yet...
225 ALL_SRCS += $(CONFIG_HS)
227 # HsGeneric.hs is not used just now
228 EXCLUDED_SRCS += hsSyn/HsGeneric.hs
230 ifeq ($(GhcWithNativeCodeGen),YES)
231 ALL_DIRS += nativeGen
233 SRC_HC_OPTS += -DOMIT_NATIVE_CODEGEN
236 ifeq ($(GhcWithIlx),YES)
241 ifeq ($(GhcWithJavaGen),YES)
243 SRC_HC_OPTS += -DJAVA
246 ifeq "$(BootingFromHc)" "YES"
247 # HC files are always from a self-booted compiler
249 compiling_with_4xx=NO
251 ifneq "$(findstring $(stage), 2 3)" ""
253 compiling_with_4xx = NO
255 bootstrapped = $(shell if (test $(GhcCanonVersion) -ge $(ProjectVersionInt) -a $(GhcPatchLevel) -ge $(ProjectPatchLevel)); then echo YES; else echo NO; fi)
256 compiling_with_4xx = $(shell if (test $(GhcCanonVersion) -lt 500); then echo YES; else echo NO; fi)
260 # Only include GHCi if we're bootstrapping with at least version 411
261 ifeq "$(GhcWithInterpreter) $(bootstrapped)" "YES YES"
262 # Yes, include the interepreter, readline, and Template Haskell extensions
263 SRC_HC_OPTS += -DGHCI -package haskell-src
264 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
265 SRC_HC_OPTS += -package unix -package readline
269 # No interpreter, so exclude Template Haskell modules
270 EXCLUDED_SRCS += deSugar/DsMeta.hs typecheck/TcSplice.lhs hsSyn/Convert.lhs
273 # There are some C files to include in HS_PROG, so add these to HS_OBJS
276 # -----------------------------------------------
279 # Big Fudge to get around inherent problem that Makefile setup
280 # has got with 'mkdependC'.
282 SRC_MKDEPENDC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt) -I$(GHC_INCLUDE_DIR)
284 # -----------------------------------------------------------------------------
285 # Haskell compilations
288 -cpp -fglasgow-exts -Rghc-timing \
289 -I. -IcodeGen -InativeGen -Iparser
291 # Omitted: -I$(GHC_INCLUDE_DIR)
292 # We should have -I$(GHC_INCLUDE_DIR) in SRC_HC_OPTS,
293 # to avoid the use of an explicit path in GHC source files
294 # (include "../includes/config.h"
295 # But alas GHC 4.08 (and others for all I know) uses this very
296 # same include path when compiling the .hc files it generates.
297 # Disaster! Then the hc file sees the GHC 5.02 (or whatever)
298 # include files. For the moment we've reverted to using
299 # an explicit path in the .hs sources
301 # For the benefit of <5.00 compilers, do include GHC_INCLUDE_DIR
302 # when generating dependencies. (=> it gets passed onto mkdependHS,
304 SRC_MKDEPENDHS_OPTS += -I$(GHC_INCLUDE_DIR)
306 # When bootstrapped, we don't make use of *any* packages
307 # (except possibly readline if GHCi is enabled, see above)
308 ifneq "$(bootstrapped)" "YES"
309 ifneq "$(mingw32_HOST_OS)" "1"
310 SRC_HC_OPTS += -package concurrent -package posix -package util
312 SRC_HC_OPTS += -package concurrent -package util
316 SRC_CC_OPTS += -Iparser -I. -O
317 SRC_HC_OPTS += -recomp $(GhcHcOpts)
320 ifeq "$(BootingFromHc)" "YES"
321 SRC_CC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt)
324 # Special flags for particular modules
325 # The standard suffix rule for compiling a Haskell file
326 # adds these flags to the command line
328 # not sure if this is required --SDM
329 main/Main_HC_OPTS = -fvia-C
331 prelude/PrimOp_HC_OPTS = -no-recomp -H80m
333 # because the NCG can't handle the 64-bit math in here
334 prelude/PrelRules_HC_OPTS = -fvia-C
336 main/ParsePkgConf_HC_OPTS += -fno-warn-incomplete-patterns
337 # Use -fvia-C since the NCG can't handle the narrow16Int# (and intToInt16#?)
338 # primops on all platforms.
339 parser/Parser_HC_OPTS += -Onot -fno-warn-incomplete-patterns -fvia-C
341 # The latest GHC version doesn't have a -K option yet, and it doesn't
342 # seem to be necessary anymore for the modules below.
343 ifeq "$(compiling_with_4xx)" "YES"
344 parser/Parser_HC_OPTS += -K2m
347 ifeq "$(HOSTPLATFORM)" "hppa1.1-hp-hpux9"
348 rename/RnMonad_HC_OPTS = -O2 -O2-for-C
351 utils/StringBuffer_HC_OPTS = -fvia-C -fno-prune-tydecls
352 utils/Digraph_HC_OPTS = -fglasgow-exts
354 ifeq "$(bootstrapped)" "YES"
355 utils/Binary_HC_OPTS = -funbox-strict-fields
358 # 4.08.2's NCG can't cope with Binary
359 ifeq "$(compiling_with_4xx)" "YES"
360 utils/Binary_HC_OPTS += -fvia-C
363 # ByteCodeItbls uses primops that the NCG doesn't support yet.
364 ghci/ByteCodeItbls_HC_OPTS = -fvia-C
365 ghci/ByteCodeLink_HC_OPTS = -fvia-C -monly-3-regs
367 # CSE interacts badly with top-level IORefs (reportedly in DriverState and
368 # DriverMkDepend), causing some of them to be commoned up. We have a fix for
369 # this in 5.00+, but earlier versions of the compiler will need CSE turned off.
370 # To be on the safe side, we disable CSE in *all* modules with top-level IORefs.
371 compMan/CompManager_HC_OPTS = -fno-cse
372 ghci/InteractiveUI_HC_OPTS = -fno-cse
373 main/CmdLineOpts_HC_OPTS = -fno-cse
374 main/DriverFlags_HC_OPTS = -fno-cse
375 main/DriverMkDepend_HC_OPTS = -fno-cse
376 main/DriverPipeline_HC_OPTS = -fno-cse
377 main/DriverState_HC_OPTS = -fno-cse
378 main/DriverUtil_HC_OPTS = -fno-cse
379 main/Finder_HC_OPTS = -fno-cse
380 main/SysTools_HC_OPTS = -fno-cse
382 # The #include is vital for the via-C route, else the C
383 # compiler doesn't realise that the stcall foreign imports are indeed
384 # stdcall, and doesn't generate the Foo@8 name for them
385 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
386 main/SysTools_HC_OPTS += '-\#include <windows.h>' '-\#include <process.h>'
389 # ghc_strlen percolates through so many modules that it is easier to get its
390 # prototype via a global option instead of a myriad of per-file OPTIONS
391 SRC_HC_OPTS += '-\#include "hschooks.h"'
393 # ----------------------------------------------------------------------------
394 # Generate supporting stuff for prelude/PrimOp.lhs
395 # from prelude/primops.txt
397 GENPOC=$(TOP)/utils/genprimopcode/genprimopcode
399 PRIMOP_BITS=primop-data-decl.hs-incl \
401 primop-list.hs-incl \
402 primop-has-side-effects.hs-incl \
403 primop-out-of-line.hs-incl \
404 primop-commutable.hs-incl \
405 primop-needs-wrapper.hs-incl \
406 primop-can-fail.hs-incl \
407 primop-strictness.hs-incl \
408 primop-usage.hs-incl \
409 primop-primop-info.hs-incl
411 CLEAN_FILES += prelude/primops.txt
412 CLEAN_FILES += $(PRIMOP_BITS)
414 SRC_CPP_OPTS += -I$(GHC_INCLUDE_DIR) -traditional
415 SRC_CPP_OPTS += ${GhcCppOpts}
417 ifneq "$(BootingFromHc)" "YES"
418 prelude/PrimOp.lhs prelude/PrimOp.o: $(PRIMOP_BITS)
421 ifneq "$(BootingFromHc)" "YES"
422 depend :: $(PRIMOP_BITS)
425 primop-data-decl.hs-incl: prelude/primops.txt
426 $(GENPOC) --data-decl < $< > $@
427 primop-tag.hs-incl: prelude/primops.txt
428 $(GENPOC) --primop-tag < $< > $@
429 primop-list.hs-incl: prelude/primops.txt
430 $(GENPOC) --primop-list < $< > $@
431 primop-has-side-effects.hs-incl: prelude/primops.txt
432 $(GENPOC) --has-side-effects < $< > $@
433 primop-out-of-line.hs-incl: prelude/primops.txt
434 $(GENPOC) --out-of-line < $< > $@
435 primop-commutable.hs-incl: prelude/primops.txt
436 $(GENPOC) --commutable < $< > $@
437 primop-needs-wrapper.hs-incl: prelude/primops.txt
438 $(GENPOC) --needs-wrapper < $< > $@
439 primop-can-fail.hs-incl: prelude/primops.txt
440 $(GENPOC) --can-fail < $< > $@
441 primop-strictness.hs-incl: prelude/primops.txt
442 $(GENPOC) --strictness < $< > $@
443 primop-usage.hs-incl: prelude/primops.txt
444 $(GENPOC) --usage < $< > $@
445 primop-primop-info.hs-incl: prelude/primops.txt
446 $(GENPOC) --primop-primop-info < $< > $@
450 # ----------------------------------------------------------------------------
453 SRC_HAPPY_OPTS += +RTS -K2m -H16m -RTS $(GHC_HAPPY_OPTS)
455 #-----------------------------------------------------------------------------
458 SRC_LD_OPTS += -no-link-chk
460 # -----------------------------------------------------------------------------
461 # create ghc-inplace, a convenient way to run ghc from the build tree...
463 all :: $(odir)/ghc-inplace ghc-inplace
465 $(odir)/ghc-inplace : $(HS_PROG)
467 echo '#!/bin/sh' >>$@
468 echo exec $(FPTOOLS_TOP_ABS)/ghc/compiler/$(HS_PROG) '-B$(subst \,\\,$(FPTOOLS_TOP_ABS_PLATFORM))' '"$$@"' >>$@
471 ghc-inplace : stage1/ghc-inplace
474 CLEAN_FILES += $(odir)/ghc-inplace ghc-inplace
476 #-----------------------------------------------------------------------------
479 # We don't want ghc treated as an ordinary executable,
480 # but put it together with the libraries.
481 # Also don't want any interface files installed
483 DESTDIR = $(INSTALL_LIBRARY_DIR_GHC)
485 ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
486 INSTALL_LIBEXECS += $(HS_PROG)
488 INSTALL_PROGS += $(HS_PROG)
491 # ----------------------------------------------------------------------------
494 # rename/Rename_HC_OPTS += -auto-all
495 # rename/RnEnv_HC_OPTS += -auto-all
496 # rename/RnHiFiles_HC_OPTS += -auto-all
497 # rename/RnIfaces_HC_OPTS += -auto-all
498 # rename/RnSource_HC_OPTS += -auto-all
499 # rename/RnBinds_HC_OPTS += -auto-all
500 # rename/RnExpr_HC_OPTS += -auto-all
501 # rename/RnHsSyn_HC_OPTS += -auto-all
502 # rename/RnNames_HC_OPTS += -auto-all
503 # rename/RnTypes_HC_OPTS += -auto-all
505 #-----------------------------------------------------------------------------
508 MAINTAINER_CLEAN_FILES += parser/Parser.info main/ParsePkgConf.info
510 #-----------------------------------------------------------------------------
511 # Include target-rule boilerplate
513 # Don't use the default MKDEPENDHS stuff... we'll do our own, below
517 include $(TOP)/mk/target.mk
519 # -----------------------------------------------------------------------------
522 MKDEPENDHS_HC_OPTS = $(patsubst -i$(odir)/%, -i%, $(HC_OPTS))
526 # Must do this *after* including target.mk, because $(HS_SRCS) isn't set yet.
527 depend :: $(HS_SRCS) $(C_SRCS)
528 $(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)
529 $(MKDEPENDC) -f .depend-BASE $(MKDEPENDC_OPTS) $(foreach way,$(WAYS),-s $(way)) -- $(CC_OPTS) -- $(C_SRCS)
530 $(PERL) -pe 's@^(\S*\.o)@stage$(stage)/$$1@g; s@(\S*\.hi)@stage$(stage)/$$1@g' <.depend-BASE >.depend-$(stage)
532 -include .depend-$(stage)