Fix installation of ghc-split (#8760)
[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. Invoke like this:
15 #
16 # utils/genapply_MODULES = Main
17 # utils/genapply_HC_OPTS = -package Cabal
18 # utils/genapply_dist_PROGNAME = 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 $(call profStart, build-prog($1,$2,$3))
25 # $1 = dir
26 # $2 = distdir
27 # $3 = GHC stage to use (0 == bootstrapping compiler)
28
29 ifneq "$$(CLEANING)" "YES"
30 ifeq "$$($1_$2_PROGNAME)" ""
31 $$(error $1_$2_PROGNAME is not set)
32 endif
33 ifneq "$$($1_$2_PROG)" ""
34 $$(error $1_$2_PROG is set)
35 endif
36 $1_$2_PROG = $$($1_$2_PROGNAME)$$(exeext$3)
37 endif
38
39 ifeq "$$(findstring $3,0 1 2)" ""
40 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
41 endif
42
43 $(call clean-target,$1,$2,$1/$2)
44
45 $$(eval $$(call build-prog-vars,$1,$2,$3))
46
47 ifneq "$$($1_$2_NOT_NEEDED)" "YES"
48 $$(eval $$(call build-prog-helper,$1,$2,$3))
49 endif
50 $(call profEnd, build-prog($1,$2,$3))
51 endef
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 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
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 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
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 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 # The quadrupled $'s here are because the _<way>_LIB variables aren't
189 # necessarily set when this part of the makefile is read
190 $1/$2/build/tmp/$$($1_$2_PROG) $1/$2/build/tmp/$$($1_$2_PROG).dll : \
191 $$(foreach dep,$$($1_$2_DEPS),\
192 $$(if $$(filter ghc%,$$(dep)),\
193 $(if $(filter 0,$3),$$(compiler_stage1_PROGRAM_DEP_LIB),\
194 $(if $(filter 1,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
195 $(if $(filter 2,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
196 $$(error Bad build stage)))),\
197 $$$$($$(dep)_dist-$(if $(filter 0,$3),boot,install)_PROGRAM_DEP_LIB)))
198
199 $1_$2_PROG_NEEDS_C_WRAPPER = NO
200 $1_$2_PROG_INPLACE = $$($1_$2_PROG)
201 ifeq "$$(Windows_Host) $$($1_$2_PROGRAM_WAY)" "YES dyn"
202 ifneq "$$($1_$2_HS_SRCS)" ""
203 $1_$2_PROG_NEEDS_C_WRAPPER = YES
204 $1_$2_PROG_INPLACE = inplace-$$($1_$2_PROG)
205 endif
206 endif
207
208 ifeq "$$($1_$2_PROG_NEEDS_C_WRAPPER)" "YES"
209
210 $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)))
211 ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts"
212 $1_$2_RTS_OPTS = RtsOptsAll
213 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=all"
214 $1_$2_RTS_OPTS = RtsOptsAll
215 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=some"
216 $1_$2_RTS_OPTS = RtsOptsSafeOnly
217 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-rtsopts=none"
218 $1_$2_RTS_OPTS = RtsOptsNone
219 else ifeq "$$($1_$2_RTS_OPTS_FLAG)" "-no-rtsopts"
220 $1_$2_RTS_OPTS = RtsOptsNone
221 else
222 $1_$2_RTS_OPTS = RtsOptsSafeOnly
223 endif
224
225 $1/$2/build/tmp/$$($1_$2_PROG)-inplace-wrapper.c: driver/utils/dynwrapper.c | $$$$(dir $$$$@)/.
226 $$(call removeFiles,$$@)
227 echo '#include <Windows.h>' >> $$@
228 echo '#include "Rts.h"' >> $$@
229 echo 'LPTSTR path_dirs[] = {' >> $$@
230 $$(foreach d,$$($1_$2_DEP_LIB_REL_DIRS),$$(call make-command,echo ' TEXT("/../../$$d")$$(comma)' >> $$@))
231 echo ' TEXT("/../../$1/$2/build/tmp/"),' >> $$@
232 echo ' NULL};' >> $$@
233 echo 'LPTSTR progDll = TEXT("../../$1/$2/build/tmp/$$($1_$2_PROG).dll");' >> $$@
234 echo 'LPTSTR rtsDll = TEXT("$$($$(WINDOWS_DYN_PROG_RTS))");' >> $$@
235 echo 'int rtsOpts = $$($1_$2_RTS_OPTS);' >> $$@
236 cat driver/utils/dynwrapper.c >> $$@
237
238 $1/$2/build/tmp/$$($1_$2_PROG)-wrapper.c: driver/utils/dynwrapper.c | $$$$(dir $$$$@)/.
239 $$(call removeFiles,$$@)
240 echo '#include <Windows.h>' >> $$@
241 echo '#include "Rts.h"' >> $$@
242 echo 'LPTSTR path_dirs[] = {' >> $$@
243 $$(foreach p,$$($1_$2_TRANSITIVE_DEPS),$$(call make-command,echo ' TEXT("/../lib/$$p")$$(comma)' >> $$@))
244 echo ' TEXT("/../lib/"),' >> $$@
245 echo ' NULL};' >> $$@
246 echo 'LPTSTR progDll = TEXT("../lib/$$($1_$2_PROG).dll");' >> $$@
247 echo 'LPTSTR rtsDll = TEXT("$$($$(WINDOWS_DYN_PROG_RTS))");' >> $$@
248 echo 'int rtsOpts = $$($1_$2_RTS_OPTS);' >> $$@
249 cat driver/utils/dynwrapper.c >> $$@
250
251 $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 $$$$@)/.
252 $$(call cmd,$1_$2_HC) -no-hs-main -no-auto-link-packages -optc-g -optc-O0 -Iincludes $$< -o $$@
253
254 $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 $$$$@)/.
255 $$(call cmd,$1_$2_HC) -no-hs-main -no-auto-link-packages -optc-g -optc-O0 -Iincludes $$< -o $$@
256
257 $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 $$$$@)/.
258 $$(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),$$@)
259 else
260 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
261 $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 $$$$@)/.
262 $$(call cmd,$1_$2_HC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS) $$(LD_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))
263
264 else
265 $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 $$$$@)/.
266 $$(call cmd,$1_$2_CC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_CC_OPTS) $$(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))
267 endif
268 endif
269
270 # Note [lib-depends] if this program is built with stage1 or greater, we
271 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
272 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
273 # been included, this introduces an ordering dependency.
274 ifneq "$$(CLEANING)" "YES"
275 ifneq "$3" "0"
276 ifneq "$$($1_$2_HS_SRCS)" ""
277 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
278 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
279 endif
280 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
281 endif
282 endif
283 endif
284
285 # INPLACE_BIN might be empty if we're distcleaning
286 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
287 ifeq "$$($1_$2_INSTALL_INPLACE)" "YES"
288 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG_INPLACE) | $$$$(dir $$$$@)/.
289 "$$(CP)" -p $$< $$@
290 endif
291 endif
292
293 endif
294
295 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
296 $(call all-target,$1_$2,$$($1_$2_INPLACE))
297 endif
298 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
299
300 ifeq "$$($1_$2_INSTALL)" "YES"
301 ifeq "$$($1_$2_PROG_NEEDS_C_WRAPPER)" "YES"
302 INSTALL_LIBS += $1/$2/build/tmp/$$($1_$2_PROG).dll
303 endif
304 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
305 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
306 else ifeq "$$($1_$2_TOPDIR)" "YES"
307 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
308 else
309 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
310 endif
311 endif
312
313 $(call dependencies,$1,$2,$3)
314
315 # The Main module of a program implicitly depends on GHC.TopHandler
316 # so we need to add a dependency for that. As we don't know which
317 # module contains Main, we just make all modules in the program
318 # depend on it.
319 ifneq "$3" "0"
320 $$(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))))
321 endif
322
323 endef