Merge win:/cygdrive/c/ghc/git/dt
[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://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_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 # INPLACE_BIN might be empty if we're distcleaning
136 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
137 ifeq "$$($1_$2_INSTALL_INPLACE)" "YES"
138 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
139 "$$(CP)" -p $$< $$@
140 endif
141 endif
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 # The quadrupled $'s here are because the _<way>_LIB variables aren't
197 # necessarily set when this part of the makefile is read
198 $1/$2/build/tmp/$$($1_$2_PROG) $1/$2/build/tmp/$$($1_$2_PROG).dll : \
199 $$(foreach dep,$$($1_$2_DEPS),\
200 $$(if $$(filter ghc%,$$(dep)),\
201 $(if $(filter 0,$3),$$(compiler_stage1_PROGRAM_DEP_LIB),\
202 $(if $(filter 1,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
203 $(if $(filter 2,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
204 $$(error Bad build stage)))),\
205 $$$$($$(dep)_dist-$(if $(filter 0,$3),boot,install)_PROGRAM_DEP_LIB)))
206
207 $1_$2_PROG_NEEDS_C_WRAPPER = NO
208 ifeq "$$(Windows_Host) $$($1_$2_PROGRAM_WAY)" "YES dyn"
209 ifneq "$$($1_$2_HS_SRCS)" ""
210 $1_$2_PROG_NEEDS_C_WRAPPER = YES
211 endif
212 endif
213
214 ifeq "$$($1_$2_PROG_NEEDS_C_WRAPPER)" "YES"
215
216 $1/$2/build/tmp/$$($1_$2_PROG)-inplace-wrapper.c: driver/utils/dynwrapper.c | $$$$(dir $$$$@)/.
217 $$(call removeFiles,$$@)
218 echo '#include <Windows.h>' >> $$@
219 echo 'LPTSTR path_dirs[] = {' >> $$@
220 $$(foreach d,$$($1_$2_DEP_LIB_REL_DIRS),$$(call make-command,echo ' TEXT("$$d")$$(comma)' >> $$@))
221 echo ' TEXT("$1/$2/build/tmp/"),' >> $$@
222 echo ' NULL};' >> $$@
223 echo 'LPTSTR progDll = TEXT("../../$1/$2/build/tmp/$$($1_$2_PROG).dll");' >> $$@
224 echo 'LPTSTR rtsDll = TEXT("$$($$(WINDOWS_DYN_PROG_RTS))");' >> $$@
225 cat driver/utils/dynwrapper.c >> $$@
226
227 $1/$2/build/tmp/$$($1_$2_PROG) : $1/$2/build/tmp/$$($1_$2_PROG)-inplace-wrapper.c $1/$2/build/tmp/$$($1_$2_PROG).dll | $$$$(dir $$$$@)/.
228 $$(call cmd,$1_$2_HC) -no-hs-main -optc-g -optc-O0 $$< -o $$@
229
230 $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 $$$$@)/.
231 $$(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),$$@)
232 else
233 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
234 $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 $$$$@)/.
235 $$(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))
236 ifeq "$$(TargetOS_CPP)" "darwin"
237 ifneq "$3" "0"
238 ifeq "$$($1_$2_PROGRAM_WAY)" "dyn"
239 # Use relative paths for all the libraries
240 ifneq "$$($1_$2_TRANSITIVE_DEP_NAMES)" ""
241 install_name_tool $$(foreach d,$$($1_$2_TRANSITIVE_DEP_NAMES), -change $$(TOP)/$$($$($$d_INSTALL_INFO)_dyn_LIB) @loader_path/../$$d-$$($$($$d_INSTALL_INFO)_VERSION)/$$($$($$d_INSTALL_INFO)_dyn_LIB_NAME)) $$@
242 endif
243 # Use relative paths for the RTS. Rather than try to work out which RTS
244 # way is being linked, we just change it for all ways
245 install_name_tool $$(foreach w,$$(rts_WAYS), -change $$(TOP)/$$(rts_$$w_LIB) @loader_path/../rts-$$(rts_VERSION)/$$(rts_$$w_LIB_NAME)) $$@
246 endif
247 endif
248 endif
249 else
250 $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 $$$$@)/.
251 $$(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))
252 endif
253 endif
254
255 # Note [lib-depends] if this program is built with stage1 or greater, we
256 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
257 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
258 # been included, this introduces an ordering dependency.
259 ifneq "$$(CLEANING)" "YES"
260 ifneq "$3" "0"
261 ifneq "$$($1_$2_HS_SRCS)" ""
262 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
263 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
264 endif
265 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
266 endif
267 endif
268 endif
269 endif
270
271 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
272 $(call all-target,$1_$2,$$($1_$2_INPLACE))
273 endif
274 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
275
276 ifeq "$$($1_$2_INSTALL)" "YES"
277 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
278 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
279 else ifeq "$$($1_$2_TOPDIR)" "YES"
280 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
281 else
282 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
283 endif
284 endif
285
286 $(call dependencies,$1,$2,$3)
287
288 # The Main module of a program implicitly depends on GHC.TopHandler
289 # so we need to add a dependency for that. As we don't know which
290 # module contains Main, we just make all modules in the program
291 # depend on it.
292 ifneq "$3" "0"
293 $$(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))))
294 endif
295
296 endef