Rename hs-suffix-rules to hs-suffix-way-rules
[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-way-rules,$1,$2,$$($1_$2_PROGRAM_WAY))
158
159 $(call c-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
160 $(call hs-objs,$1,$2,$$($1_$2_PROGRAM_WAY))
161
162 $1_$2_LINK_WITH_GCC = NO
163
164 ifeq "$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS)" ""
165 # We don't want to link the GHC RTS into C-only programs. There's no
166 # point, and it confuses the test that all GHC-compiled programs
167 # were compiled with the right GHC.
168 $1_$2_$$($1_$2_PROGRAM_WAY)_GHC_LD_OPTS += -no-auto-link-packages -no-hs-main
169 endif
170
171 # XXX
172 # ifneq "$3" "0"
173 # ifeq "$$(TargetOS_CPP)" "linux"
174 # $1_$2_dyn_GHC_LD_OPTS += \
175 # -fno-use-rpaths \
176 # $$(foreach d,$$($1_$2_TRANSITIVE_DEPS),-optl-Wl$$(comma)-rpath -optl-Wl$$(comma)'$$$$ORIGIN/../$$d')
177 # else ifeq "$$(TargetOS_CPP)" "darwin"
178 # $1_$2_dyn_GHC_LD_OPTS += -optl-Wl,-headerpad_max_install_names
179 # endif
180 # endif
181
182 ifneq "$$(BINDIST)" "YES"
183 # The quadrupled $'s here are because the _<way>_LIB variables aren't
184 # necessarily set when this part of the makefile is read
185 $1/$2/build/tmp/$$($1_$2_PROG) : \
186 $$(foreach dep,$$($1_$2_DEPS),\
187 $$(if $$(filter ghc%,$$(dep)),\
188 $(if $(filter 0,$3),$$(compiler_stage1_PROGRAM_DEP_LIB),\
189 $(if $(filter 1,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
190 $(if $(filter 2,$3),$$(compiler_stage2_PROGRAM_DEP_LIB),\
191 $$(error Bad build stage)))),\
192 $$$$($$(dep)_dist-$(if $(filter 0,$3),boot,install)_PROGRAM_DEP_LIB)))
193
194 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
195 $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 $$$$@)/.
196 $$(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))
197 ifeq "$$(TargetOS_CPP)" "darwin"
198 ifneq "$3" "0"
199 ifeq "$$($1_$2_PROGRAM_WAY)" "dyn"
200 # Use relative paths for all the libraries
201 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)) $$@
202 # Use relative paths for the RTS. Rather than try to work out which RTS
203 # way is being linked, we just change it for all ways
204 install_name_tool $$(foreach w,$$(rts_WAYS), -change $$(TOP)/$$(rts_$$w_LIB) @loader_path/../rts-$$(rts_VERSION)/$$(rts_$$w_LIB_NAME)) $$@
205 endif
206 endif
207 endif
208 else
209 $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 $$$$@)/.
210 $$(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))
211 endif
212
213 # Note [lib-depends] if this program is built with stage1 or greater, we
214 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
215 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
216 # been included, this introduces an ordering dependency.
217 ifneq "$$(CLEANING)" "YES"
218 ifneq "$3" "0"
219 ifneq "$$($1_$2_HS_SRCS)" ""
220 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
221 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
222 endif
223 endif
224 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
225 endif
226 endif
227 endif
228
229 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
230 $(call all-target,$1_$2,$$($1_$2_INPLACE))
231 endif
232 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
233
234 ifeq "$$($1_$2_INSTALL)" "YES"
235 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
236 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
237 else ifeq "$$($1_$2_TOPDIR)" "YES"
238 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
239 else
240 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
241 endif
242 endif
243
244 $(call dependencies,$1,$2,$3)
245
246 # The Main module of a program implicitly depends on GHC.TopHandler
247 # so we need to add a dependency for that. As we don't know which
248 # module contains Main, we just make all modules in the program
249 # depend on it.
250 ifneq "$3" "0"
251 $$(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))))
252 endif
253
254 endef