Merge branch 'master' of darcs.haskell.org:/srv/darcs//ghc
[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_PROG = 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_PROG)" ""
31 $$(error $1_$2_PROG is not set)
32 endif
33 endif
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 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 define build-prog-helper
49 # $1 = dir
50 # $2 = distdir
51 # $3 = GHC stage to use (0 == bootstrapping compiler)
52
53 ifeq "$$($1_USES_CABAL)" "YES"
54 $1_$2_USES_CABAL = YES
55 endif
56
57 ifeq "$$(Windows)" "YES"
58 $1_$2_WANT_INPLACE_WRAPPER = NO
59 else ifneq "$$($1_$2_INSTALL_INPLACE)" "YES"
60 $1_$2_WANT_INPLACE_WRAPPER = NO
61 else ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
62 $1_$2_WANT_INPLACE_WRAPPER = YES
63 else ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
64 $1_$2_WANT_INPLACE_WRAPPER = YES
65 else
66 $1_$2_WANT_INPLACE_WRAPPER = NO
67 endif
68
69 ifeq "$$(Windows)" "YES"
70 $1_$2_WANT_INSTALLED_WRAPPER = NO
71 else ifneq "$$($1_$2_INSTALL)" "YES"
72 $1_$2_WANT_INSTALLED_WRAPPER = NO
73 else ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
74 $1_$2_WANT_INSTALLED_WRAPPER = YES
75 else
76 $1_$2_WANT_INSTALLED_WRAPPER = NO
77 endif
78
79 $(call package-config,$1,$2,$3)
80
81 $1_$2_depfile_base = $1/$2/build/.depend
82
83 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
84 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
85 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
86 else
87 $1_$2_INPLACE =
88 endif
89 else
90 # Where do we install the inplace version?
91 ifeq "$$($1_$2_WANT_INPLACE_WRAPPER)" "YES"
92 $1_$2_INPLACE = $$(INPLACE_LIB)/bin/$$($1_$2_PROG)
93 else
94 ifeq "$$($1_$2_TOPDIR)" "YES"
95 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
96 else
97 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
98 endif
99 endif
100 endif
101
102 ifeq "$$($1_$2_USES_CABAL)" "YES"
103 $(call build-package-data,$1,$2,$3)
104 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
105 ifeq "$3" "0"
106 include $1/$2/package-data.mk
107 else ifeq "$(phase)" "final"
108 include $1/$2/package-data.mk
109 endif
110 endif
111 endif
112
113 $(call all-target,$1,all_$1_$2)
114 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
115
116 # INPLACE_BIN might be empty if we're distcleaning
117 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
118 ifeq "$$($1_$2_INSTALL_INPLACE)" "YES"
119 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
120 "$$(CP)" -p $$< $$@
121 endif
122 endif
123
124 $(call shell-wrapper,$1,$2)
125
126 ifeq "$$($1_$2_PROGRAM_WAY)" ""
127 ifeq "$3" "0"
128 $1_$2_PROGRAM_WAY = v
129 else ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
130 $1_$2_PROGRAM_WAY = dyn
131 else
132 $1_$2_PROGRAM_WAY = v
133 endif
134 endif
135
136 $1_$2_WAYS = $$($1_$2_PROGRAM_WAY)
137
138 $(call hs-sources,$1,$2)
139 $(call c-sources,$1,$2)
140
141 # --- IMPLICIT RULES
142
143 $(call distdir-way-opts,$1,$2,$$($1_$2_PROGRAM_WAY),$3)
144
145 ifeq "$3" "0"
146 # For stage 0, we use GHC to compile C sources so that we don't have to
147 # worry about where the RTS header files are
148 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
149 else
150 ifeq "$$($1_$2_UseGhcForCC)" "YES"
151 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
152 else
153 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),NO)
154 endif
155 endif
156
157 $(call hs-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY))
158 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
159 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,$$($1_$2_PROGRAM_WAY),$$(dir))))
160
161 $(call c-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
162 $(call hs-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
163
164 $1_$2_LINK_WITH_GCC = NO
165 ifeq "$$(BootingFromHc)" "YES"
166 $1_$2_LINK_WITH_GCC = YES
167 endif
168
169 ifeq "$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS)" ""
170 # We don't want to link the GHC RTS into C-only programs. There's no
171 # point, and it confuses the test that all GHC-compiled programs
172 # were compiled with the right GHC.
173 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
174 endif
175
176 ifneq "$3" "0"
177 ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
178 ifeq "$$(TargetOS_CPP)" "linux"
179 $1_$2_GHC_LD_OPTS = \
180 -fno-use-rpaths \
181 $$(foreach d,$$($1_$2_TRANSITIVE_DEPS),-optl-Wl$$(comma)-rpath -optl-Wl$$(comma)'$$$$ORIGIN/../$$d')
182 else ifeq "$$(TargetOS_CPP)" "darwin"
183 $1_$2_GHC_LD_OPTS = -optl-Wl,-headerpad_max_install_names
184 endif
185 endif
186 endif
187
188 ifneq "$$(BINDIST)" "YES"
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) : \
192 $$(foreach dep,$$($1_$2_DEP_NAMES),\
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 $$$$(libraries/$$(dep)_dist-$(if $(filter 0,$3),boot,install)_PROGRAM_DEP_LIB)))
199
200 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
201 $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 $$$$@)/.
202 $$(call cmd,$1_$2_HC) -o $$@ $$($1_$2_$$($1_$2_PROGRAM_WAY)_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_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))
203 ifeq "$$(TargetOS_CPP)" "darwin"
204 ifneq "$3" "0"
205 ifeq "$$($1_$2_PROGRAM_WAY)" "dyn"
206 # Use relative paths for all the libraries
207 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)) $$@
208 # Use relative paths for the RTS. Rather than try to work out which RTS
209 # way is being linked, we just change it for all ways
210 install_name_tool $$(foreach w,$$(rts_WAYS), -change $$(TOP)/$$(rts_$$w_LIB) @loader_path/../rts-$$(rts_VERSION)/$$(rts_$$w_LIB_NAME)) $$@
211 endif
212 endif
213 endif
214 else
215 $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 $$$$@)/.
216 $$(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))
217 endif
218
219 # Note [lib-depends] if this program is built with stage1 or greater, we
220 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
221 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
222 # been included, this introduces an ordering dependency.
223 ifneq "$$(CLEANING)" "YES"
224 ifneq "$3" "0"
225 ifneq "$$($1_$2_HS_SRCS)" ""
226 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
227 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
228 endif
229 endif
230 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
231 endif
232 endif
233 endif
234
235 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
236 $(call all-target,$1_$2,$$($1_$2_INPLACE))
237 endif
238 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
239
240 ifeq "$$($1_$2_INSTALL)" "YES"
241 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
242 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
243 else ifeq "$$($1_$2_TOPDIR)" "YES"
244 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
245 else
246 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
247 endif
248 endif
249
250 $(call dependencies,$1,$2,$3)
251
252 endef