1 # -----------------------------------------------------------------------------
3 # (c) 2009 The University of Glasgow
5 # This file is part of the GHC build system.
7 # To understand how the build system works and how to modify it, see
8 # http://hackage.haskell.org/trac/ghc/wiki/Building/Architecture
9 # http://hackage.haskell.org/trac/ghc/wiki/Building/Modifying
11 # -----------------------------------------------------------------------------
14 # Build a program. Invoke like this:
16 # utils/genapply_MODULES = Main
17 # utils/genapply_HC_OPTS = -package Cabal
18 # utils/genapply_dist_PROG = genapply
20 # $(eval $(call build-prog,utils/genapply,dist-install,1))
23 $(call trace, build-prog($1,$2,$3))
24 $(call profStart, build-prog($1,$2,$3))
27 # $3 = GHC stage to use (0 == bootstrapping compiler)
29 ifneq "$$(CLEANING)" "YES"
30 ifeq "$$($1_$2_PROG)" ""
31 $$(error $1_$2_PROG is not set)
35 ifeq "$$(findstring $3,0 1 2)" ""
36 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
39 $(call clean-target,$1,$2,$1/$2)
41 ifneq "$$($1_$2_NOT_NEEDED)" "YES"
42 $$(eval $$(call build-prog-helper,$1,$2,$3))
44 $(call profEnd, build-prog($1,$2,$3))
48 define build-prog-helper
51 # $3 = GHC stage to use (0 == bootstrapping compiler)
53 ifeq "$$($1_USES_CABAL)" "YES"
54 $1_$2_USES_CABAL = YES
57 $(call package-config,$1,$2,$3)
59 $1_$2_depfile_base = $1/$2/build/.depend
61 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
62 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
63 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
68 # Where do we install the inplace version?
69 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
70 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
72 ifeq "$$($1_$2_TOPDIR)" "YES"
73 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
75 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
80 ifeq "$$($1_$2_USES_CABAL)" "YES"
81 $(call build-package-data,$1,$2,$3)
82 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
84 include $1/$2/package-data.mk
85 else ifeq "$(phase)" ""
86 include $1/$2/package-data.mk
91 $(call all-target,$1,all_$1_$2)
92 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
94 # INPLACE_BIN might be empty if we're distcleaning
95 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
96 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
97 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
103 $(call shell-wrapper,$1,$2)
107 $(call hs-sources,$1,$2)
108 $(call c-sources,$1,$2)
112 # Just the 'v' way for programs
113 $(call distdir-way-opts,$1,$2,v,$3)
116 # For stage 0, we use GHC to compile C sources so that we don't have to
117 # worry about where the RTS header files are
118 $(call c-suffix-rules,$1,$2,v,YES)
120 $(call c-suffix-rules,$1,$2,v,NO)
123 $(call hs-suffix-rules,$1,$2,v)
124 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
125 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
127 $(call c-objs,$1,$2,v)
128 $(call hs-objs,$1,$2,v)
130 $1_$2_LINK_WITH_GCC = NO
131 ifeq "$$(BootingFromHc)" "YES"
132 $1_$2_LINK_WITH_GCC = YES
135 ifeq "$$($1_$2_v_HS_OBJS)" ""
136 # We don't want to link the GHC RTS into C-only programs. There's no
137 # point, and it confuses the test that all GHC-compiled programs
138 # were compiled with the right GHC.
139 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
142 ifneq "$$(BINDIST)" "YES"
143 # The quadrupled $'s here are because the _v_LIB variables aren't
144 # necessarily set when this part of the makefile is read
145 $1/$2/build/tmp/$$($1_$2_PROG) : \
146 $$(foreach dep,$$($1_$2_DEP_NAMES),\
147 $$(if $$(filter ghc,$$(dep)),\
148 $(if $(filter 0,$3),$$(compiler_stage1_v_LIB),\
149 $(if $(filter 1,$3),$$(compiler_stage2_v_LIB),\
150 $(if $(filter 2,$3),$$(compiler_stage2_v_LIB),\
151 $$(error Bad build stage)))),\
152 $$$$(libraries/$$(dep)_dist-$(if $(filter 0,$3),boot,install)_v_LIB)))
154 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
155 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
156 "$$($1_$2_HC)" -o $$@ $$($1_$2_v_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_GHC_LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
158 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
159 "$$(CC)" -o $$@ $$($1_$2_v_ALL_CC_OPTS) $$(LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) $$($1_$2_v_EXTRA_CC_OPTS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
162 # Note [lib-depends] if this program is built with stage1 or greater, we
163 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
164 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
165 # been included, this introduces an ordering dependency.
166 ifneq "$$(CLEANING)" "YES"
168 ifneq "$$($1_$2_HS_SRCS)" ""
169 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
170 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
173 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
178 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
179 $(call all-target,$1_$2,$$($1_$2_INPLACE))
181 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
183 ifeq "$$($1_$2_INSTALL)" "YES"
184 ifeq "$$($1_$2_TOPDIR)" "YES"
185 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
187 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
192 # We always have the dependency rules available, as we need to know
193 # how to build hsc2hs's dependency file in phase 0
194 $(call build-dependencies,$1,$2,$3)
196 # From phase 1 we actually include the dependency files for the
197 # bootstrapping stuff
199 $(call include-dependencies,$1,$2,$3)
200 else ifeq "$(phase)" ""
201 # In the final phase, we also include the dependency files for
203 $(call include-dependencies,$1,$2,$3)