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