Move some make variables around
[ghc-hetmet.git] / rules / build-prog.mk
1 # -----------------------------------------------------------------------------
2 #
3 # (c) 2009 The University of Glasgow
4 #
5 # This file is part of the GHC build system.
6 #
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
10 #
11 # -----------------------------------------------------------------------------
12
13
14 # Build a program.  Invoke like this:
15 #
16 # utils/genapply_MODULES = Main
17 # utils/genapply_HC_OPTS = -package Cabal
18 # utils/genapply_dist_PROG = genapply
19 #
20 # $(eval $(call build-prog,utils/genapply,dist-install,1))
21
22 define build-prog
23 $(call trace, build-prog($1,$2,$3))
24 # $1 = dir
25 # $2 = distdir
26 # $3 = GHC stage to use (0 == bootstrapping compiler)
27
28 ifneq "$$(CLEANING)" "YES"
29 ifeq "$$($1_$2_PROG)" ""
30 $$(error $1_$2_PROG is not set)
31 endif
32 endif
33
34 ifeq "$$(findstring $3,0 1 2)" ""
35 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
36 endif
37
38 $(call clean-target,$1,$2,$1/$2)
39
40 ifneq "$$($1_$2_NOT_NEEDED)" "YES"
41 $$(eval $$(call build-prog-helper,$1,$2,$3))
42 endif
43 endef
44
45
46 define build-prog-helper
47 # $1 = dir
48 # $2 = distdir
49 # $3 = GHC stage to use (0 == bootstrapping compiler)
50
51 ifeq "$$($1_USES_CABAL)" "YES"
52 $1_$2_USES_CABAL = YES
53 endif
54
55 $(call package-config,$1,$2,$3)
56
57 $1_$2_depfile_base = $1/$2/build/.depend
58
59 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
60 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
61 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
62 else
63 $1_$2_INPLACE =
64 endif
65 else
66 # Where do we install the inplace version?
67 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
68 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
69 else
70 ifeq "$$($1_$2_TOPDIR)" "YES"
71 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
72 else
73 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
74 endif
75 endif
76 endif
77
78 ########################################
79 ifeq "$$($1_$2_CONFIGURE_PHASE)" ""
80 $$(error No configure phase for $1_$2)
81 else ifeq "$$($1_$2_CONFIGURE_PHASE)" "$$(phase)"
82
83 ifeq "$$(DEBUG)" "YES"
84 $$(warning $1/$2 configure phase)
85 endif
86
87 ifneq "$$(BINDIST)" "YES"
88 $(call build-package-data,$1,$2,$3)
89 endif
90
91 ifeq "$$($1_$2_USES_CABAL)" "YES"
92 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
93 include $1/$2/package-data.mk
94 endif
95 endif
96
97 # INPLACE_BIN might be empty if we're distcleaning
98 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
99 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
100 $$($1_$2_INPLACE) :
101         $$(error $1_$2 is configuring, but trying to build $$($1_$2_INPLACE)")
102 endif
103 endif
104
105 else ifeq "$$(phase_$$($1_$2_CONFIGURE_PHASE)_or_later)" "YES"
106
107 ifeq "$$(DEBUG)" "YES"
108 $$(warning $1/$2 build phase)
109 endif
110
111 ifeq "$$($1_$2_USES_CABAL)" "YES"
112 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
113 include $1/$2/package-data.mk
114 ifeq "$$($1_$2_VERSION)" ""
115 $$(error No version for $1_$2 found)
116 endif
117 endif
118 endif
119
120 $(call all-target,$1,all_$1_$2)
121 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
122
123 # INPLACE_BIN might be empty if we're distcleaning
124 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
125 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
126 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
127         "$$(CP)" -p $$< $$@
128         touch $$@
129 endif
130 endif
131
132 else
133
134 ifeq "$$(DEBUG)" "YES"
135 $$(warning $1/$2 disabled phase)
136 endif
137
138 # INPLACE_BIN might be empty if we're distcleaning
139 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
140 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
141 $$($1_$2_INPLACE) :
142         $$(error $1_$2 is disabled, but trying to build $$($1_$2_INPLACE)")
143 endif
144 endif
145
146 endif
147 ########################################
148
149 $(call shell-wrapper,$1,$2)
150
151 ifeq "$$(phase_$$($1_$2_CONFIGURE_PHASE)_done)" "YES"
152
153 ifneq "$$(BINDIST)" "YES"
154 $1_$2_WAYS = v
155
156 $(call hs-sources,$1,$2)
157 $(call c-sources,$1,$2)
158
159 # --- DEPENDENCIES
160
161 $(call build-dependencies,$1,$2,$3)
162
163 # --- IMPLICIT RULES
164
165 # Just the 'v' way for programs
166 $(call distdir-way-opts,$1,$2,v,$3)
167
168 ifeq "$3" "0"
169 # For stage 0, we use GHC to compile C sources so that we don't have to
170 # worry about where the RTS header files are
171 $(call c-suffix-rules,$1,$2,v,YES)
172 else
173 $(call c-suffix-rules,$1,$2,v,NO)
174 endif
175
176 $(call hs-suffix-rules,$1,$2,v)
177 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
178   $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
179
180 $(call c-objs,$1,$2,v)
181 $(call hs-objs,$1,$2,v)
182
183 $1_$2_LINK_WITH_GCC = NO
184 ifeq "$$(BootingFromHc)" "YES"
185 $1_$2_LINK_WITH_GCC = YES
186 endif
187
188 ifeq "$$($1_$2_v_HS_OBJS)" ""
189 # We don't want to link the GHC RTS into C-only programs. There's no
190 # point, and it confuses the test that all GHC-compiled programs
191 # were compiled with the right GHC.
192 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
193 endif
194
195 # The quadrupled $'s here are because the _v_LIB variables aren't
196 # necessarily set when this part of the makefile is read
197 $1/$2/build/tmp/$$($1_$2_PROG) : \
198     $$(foreach dep,$$($1_$2_DEP_NAMES),\
199         $$(if $$(filter ghc,$$(dep)),\
200             $(if $(filter 0,$3),$$(compiler_stage1_v_LIB),\
201             $(if $(filter 1,$3),$$(compiler_stage2_v_LIB),\
202             $(if $(filter 2,$3),$$(compiler_stage2_v_LIB),\
203             $$(error Bad build stage)))),\
204         $$$$(libraries/$$(dep)_dist-$(if $(filter 0,$3),boot,install)_v_LIB)))
205
206 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
207 $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 $$$$@)/.
208         "$$($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))
209 else
210 $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 $$$$@)/.
211         "$$(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))
212 endif
213
214 # Note [lib-depends] if this program is built with stage1 or greater, we
215 # need to depend on the libraries too.  NB. since $(ALL_STAGE1_LIBS) and
216 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
217 # been included, this introduces an ordering dependency.
218 ifneq "$$(CLEANING)" "YES"
219 ifneq "$3" "0"
220 ifneq "$$($1_$2_HS_SRCS)" ""
221 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
222 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
223 endif
224 endif
225 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
226 endif
227 endif
228
229 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
230 $(call all-target,$1_$2,$$($1_$2_INPLACE))
231 endif
232 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
233
234 # touch is necessary; cp doesn't update the file time.
235 endif
236
237 ifeq "$$($1_$2_INSTALL)" "YES"
238 ifeq "$$($1_$2_TOPDIR)" "YES"
239 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
240 else
241 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
242 endif
243 endif
244
245 endif # package-data.mk exists
246
247 endef