Add typed holes support in Template Haskell.
[ghc.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://ghc.haskell.org/trac/ghc/wiki/Building/Architecture
9 # http://ghc.haskell.org/trac/ghc/wiki/Building/Modifying
10 #
11 # -----------------------------------------------------------------------------
12
13
14 # Build a program.
15 #
16 # $(eval $(call build-prog,utils/genapply,dist-install,1))
17
18 define build-prog
19 $(call trace, build-prog($1,$2,$3))
20 $(call profStart, build-prog($1,$2,$3))
21 # $1 = dir
22 # $2 = distdir
23 # $3 = GHC stage to use (0 == bootstrapping compiler)
24
25 ifeq "$$($1_$2_PROGNAME)" ""
26 $$(error $1_$2_PROGNAME is not set)
27 endif
28 ifneq "$$($1_$2_PROG)" ""
29 $$(error $1_$2_PROG is set)
30 endif
31 $1_$2_PROG = $$($1_$2_PROGNAME)$$(exeext$3)
32
33 ifeq "$$(findstring $3,0 1 2)" ""
34 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
35 endif
36
37 $(call clean-target,$1,$2,$1/$2)
38
39 $$(eval $$(call build-prog-vars,$1,$2,$3))
40
41 ifneq "$$($1_$2_NOT_NEEDED)" "YES"
42 $$(eval $$(call build-prog-helper,$1,$2,$3))
43 endif
44 $(call profEnd, build-prog($1,$2,$3))
45 endef
46
47
48
49
50
51
52 define build-prog-vars
53 # $1 = dir
54 # $2 = distdir
55 # $3 = GHC stage to use (0 == bootstrapping compiler)
56
57 ifeq "$$($1_USES_CABAL)" "YES"
58 $1_$2_USES_CABAL = YES
59 endif
60
61 ifeq "$$(Windows_Host)" "YES"
62 $1_$2_WANT_INPLACE_WRAPPER = NO
63 else ifneq "$$($1_$2_INSTALL_INPLACE)" "YES"
64 $1_$2_WANT_INPLACE_WRAPPER = NO
65 else ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
66 $1_$2_WANT_INPLACE_WRAPPER = YES
67 else ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
68 $1_$2_WANT_INPLACE_WRAPPER = YES
69 else
70 $1_$2_WANT_INPLACE_WRAPPER = NO
71 endif
72
73 ifeq "$$(Windows_Host)" "YES"
74 $1_$2_WANT_INSTALLED_WRAPPER = NO
75 else ifneq "$$($1_$2_INSTALL)" "YES"
76 $1_$2_WANT_INSTALLED_WRAPPER = NO
77 else ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
78 $1_$2_WANT_INSTALLED_WRAPPER = YES
79 else
80 $1_$2_WANT_INSTALLED_WRAPPER = NO
81 endif
82
83 $1_$2_depfile_base = $1/$2/build/.depend
84
85 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
86 ifneq "$$(CLEANING)" "YES"
87 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
88 else
89 $1_$2_INPLACE =
90 endif
91 else
92 ifneq "$$($$($1_$2_PROGNAME)_INPLACE)" ""
93 $$(error $$($1_$2_PROGNAME)_INPLACE defined twice)
94 endif
95 ifeq "$$($1_$2_TOPDIR)" "YES"
96 $$($1_$2_PROGNAME)_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
97 else
98 $$($1_$2_PROGNAME)_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
99 endif
100 # Where do we install the inplace version?
101 ifeq "$$($1_$2_WANT_INPLACE_WRAPPER)" "YES"
102 $1_$2_INPLACE = $$(INPLACE_LIB)/bin/$$($1_$2_PROG)
103 else
104 $1_$2_INPLACE = $$($$($1_$2_PROGNAME)_INPLACE)
105 endif
106 endif
107
108 endef
109
110
111
112
113
114 define build-prog-helper
115 # $1 = dir
116 # $2 = distdir
117 # $3 = GHC stage to use (0 == bootstrapping compiler)
118
119 $(call package-config,$1,$2,$3)
120
121 ifeq "$$($1_$2_USES_CABAL)" "YES"
122 $(call build-package-data,$1,$2,$3)
123 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
124 ifeq "$3" "0"
125 include $1/$2/package-data.mk
126 else ifeq "$(phase)" "final"
127 include $1/$2/package-data.mk
128 endif
129 endif
130 endif
131
132 $(call all-target,$1,all_$1_$2)
133 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
134
135 $(call shell-wrapper,$1,$2)
136
137 ifeq "$$($1_$2_PROGRAM_WAY)" ""
138 ifeq "$3" "0"
139 $1_$2_PROGRAM_WAY = v
140 else ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
141 $1_$2_PROGRAM_WAY = dyn
142 else
143 $1_$2_PROGRAM_WAY = v
144 endif
145 endif
146
147 $1_$2_WAYS = $$($1_$2_PROGRAM_WAY)
148
149 $1_$2_DYNAMIC_TOO = NO
150
151 $(call hs-sources,$1,$2)
152 $(call c-sources,$1,$2)
153
154 # --- IMPLICIT RULES
155
156 $(call distdir-opts,$1,$2,$3)
157 $(call distdir-way-opts,$1,$2,$$($1_$2_PROGRAM_WAY),$3)
158
159 ifeq "$3" "0"
160 # For stage 0, we use GHC to compile C sources so that we don't have to
161 # worry about where the RTS header files are
162 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
163 else
164 ifeq "$$($1_$2_UseGhcForCC)" "YES"
165 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
166 else
167 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),NO)
168 endif
169 endif
170
171 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
172 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,$$(dir))))
173 $(call hs-suffix-way-rules,$1,$2,$$($1_$2_PROGRAM_WAY))
174
175 $(call c-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
176 $(call hs-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
177
178 $1_$2_LINK_WITH_GCC = NO
179
180 ifeq "$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS)" ""
181 # We don't want to link the GHC RTS into C-only programs. There's no
182 # point, and it confuses the test that all GHC-compiled programs
183 # were compiled with the right GHC.
184 $1_$2_$$($1_$2_PROGRAM_WAY)_GHC_LD_OPTS += -no-auto-link-packages -no-hs-main
185 endif
186
187 ifneq "$$(BINDIST)" "YES"
188
189 # The quadrupled $'s here are because the _<way>_LIB variables aren't
190 # necessarily set when this part of the makefile is read
191 $1/$2/build/tmp/$$($1_$2_PROG) $1/$2/build/tmp/$$($1_$2_PROG).dll : \
192 $$(foreach dep,$$($1_$2_DEP_COMPONENT_IDS),\
193 $$(if $$(filter ghc%,$$(dep)),\
194 $(if $(filter 0,$3),$$(compiler_stage1_PROGRAM_DEP_LIB),\
195 $(if $(filter 1,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
196 $(if $(filter 2,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
197 $$(error Bad build stage)))),\
198 $$$$($$(dep)_dist-$(if $(filter 0,$3),boot,install)_PROGRAM_DEP_LIB)))
199
200 $1_$2_PROG_NEEDS_C_WRAPPER = NO
201 $1_$2_PROG_INPLACE = $$($1_$2_PROG)
202 ifeq "$$(Windows_Host) $$($1_$2_PROGRAM_WAY)" "YES dyn"
203 ifneq "$$($1_$2_HS_SRCS)" ""
204 $1_$2_PROG_NEEDS_C_WRAPPER = YES
205 $1_$2_PROG_INPLACE = inplace-$$($1_$2_PROG)
206 endif
207 endif
208
209 ifeq "$$($1_$2_PROG_NEEDS_C_WRAPPER)" "YES"
210
211 $1_$2_RTS_OPTS_FLAG = $$(lastword $$(filter -rtsopts -rtsopts=all -rtsopts=some -rtsopts=none -no-rtsopts,$$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS)))
212 ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts"
213 $1_$2_RTS_OPTS = RtsOptsAll
214 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=all"
215 $1_$2_RTS_OPTS = RtsOptsAll
216 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=some"
217 $1_$2_RTS_OPTS = RtsOptsSafeOnly
218 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=none"
219 $1_$2_RTS_OPTS = RtsOptsNone
220 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-no-rtsopts"
221 $1_$2_RTS_OPTS = RtsOptsNone
222 else
223 $1_$2_RTS_OPTS = RtsOptsSafeOnly
224 endif
225
226 $1/$2/build/tmp/$$($1_$2_PROG)-inplace-wrapper.c: driver/utils/dynwrapper.c | $$$$(dir $$$$@)/.
227 $$(call removeFiles,$$@)
228 echo '#include <Windows.h>' >> $$@
229 echo '#include "Rts.h"' >> $$@
230 echo 'LPTSTR path_dirs[] = {' >> $$@
231 $$(foreach d,$$($1_$2_DEP_LIB_REL_DIRS),$$(call make-command,echo ' TEXT("/../../$$d")$$(comma)' >> $$@))
232 echo ' TEXT("/../../$1/$2/build/tmp/"),' >> $$@
233 echo ' NULL};' >> $$@
234 echo 'LPTSTR progDll = TEXT("../../$1/$2/build/tmp/$$($1_$2_PROG).dll");' >> $$@
235 echo 'LPTSTR rtsDll = TEXT("$$($$(WINDOWS_DYN_PROG_RTS))");' >> $$@
236 echo 'int rtsOpts = $$($1_$2_RTS_OPTS);' >> $$@
237 cat driver/utils/dynwrapper.c >> $$@
238
239 $1/$2/build/tmp/$$($1_$2_PROG)-wrapper.c: driver/utils/dynwrapper.c | $$$$(dir $$$$@)/.
240 $$(call removeFiles,$$@)
241 echo '#include <Windows.h>' >> $$@
242 echo '#include "Rts.h"' >> $$@
243 echo 'LPTSTR path_dirs[] = {' >> $$@
244 $$(foreach p,$$($1_$2_TRANSITIVE_DEP_COMPONENT_IDS),$$(call make-command,echo ' TEXT("/../lib/$$p")$$(comma)' >> $$@))
245 echo ' TEXT("/../lib/"),' >> $$@
246 echo ' NULL};' >> $$@
247 echo 'LPTSTR progDll = TEXT("../lib/$$($1_$2_PROG).dll");' >> $$@
248 echo 'LPTSTR rtsDll = TEXT("$$($$(WINDOWS_DYN_PROG_RTS))");' >> $$@
249 echo 'int rtsOpts = $$($1_$2_RTS_OPTS);' >> $$@
250 cat driver/utils/dynwrapper.c >> $$@
251
252 $1/$2/build/tmp/$$($1_$2_PROG_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG)-inplace-wrapper.c $1/$2/build/tmp/$$($1_$2_PROG).dll | $$$$(dir $$$$@)/.
253 $$(call cmd,$1_$2_HC) -no-hs-main -no-auto-link-packages -optc-g -optc-O0 -Iincludes $$< -o $$@
254
255 $1/$2/build/tmp/$$($1_$2_PROG) : $1/$2/build/tmp/$$($1_$2_PROG)-wrapper.c $1/$2/build/tmp/$$($1_$2_PROG).dll | $$$$(dir $$$$@)/.
256 $$(call cmd,$1_$2_HC) -no-hs-main -no-auto-link-packages -optc-g -optc-O0 -Iincludes $$< -o $$@
257
258 $1/$2/build/tmp/$$($1_$2_PROG).dll : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
259 $$(call build-dll,$1,$2,$$($1_$2_PROGRAM_WAY),,$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS),$$@)
260 else # $1_$2_PROG_NEEDS_C_WRAPPER=NO
261 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
262 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
263 $$(call cmd,$1_$2_HC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_GHC_LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
264
265 else
266 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
267 $$(call cmd,$1_$2_CC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_CC_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_LD_OPTS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_C_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_S_OBJS) $$($1_$2_OTHER_OBJS) $$($1_$2_$$($1_$2_PROGRAM_WAY)_EXTRA_CC_OPTS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
268 endif
269 endif # $1_$2_PROG_NEEDS_C_WRAPPER
270
271 # Note [lib-depends] if this program is built with stage1 or greater, we
272 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
273 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
274 # been included, this introduces an ordering dependency.
275 ifneq "$$(CLEANING)" "YES"
276 ifneq "$3" "0"
277 ifneq "$$($1_$2_HS_SRCS)" ""
278 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
279 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
280 endif
281 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
282 endif
283 endif
284 endif
285
286 ifeq "$$($1_$2_INSTALL_INPLACE)" "YES"
287 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG_INPLACE) | $$$$(dir $$$$@)/.
288 $$(INSTALL) -m 755 $$< $$@
289 endif
290
291 endif # BINDIST=YES
292
293 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
294 $(call all-target,$1_$2,$$($1_$2_INPLACE))
295 endif
296 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
297
298 ifeq "$$($1_$2_INSTALL)" "YES"
299 ifeq "$$($1_$2_PROG_NEEDS_C_WRAPPER)" "YES"
300 INSTALL_LIBS += $1/$2/build/tmp/$$($1_$2_PROG).dll
301 endif
302 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
303 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
304 else ifeq "$$($1_$2_TOPDIR)" "YES"
305 INSTALL_TOPDIR_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
306 else
307 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
308 endif
309 endif
310
311 $(call dependencies,$1,$2,$3)
312
313 # The Main module of a program implicitly depends on GHC.TopHandler
314 # so we need to add a dependency for that. As we don't know which
315 # module contains Main, we just make all modules in the program
316 # depend on it.
317 ifneq "$3" "0"
318 $$(foreach o,$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS),$$(eval $$(call add-dependency,$$o,libraries/base/dist-install/build/GHC/TopHandler.$$($$($1_$2_PROGRAM_WAY)_osuf))))
319 endif
320
321 endef