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