Actually build things the dynamic way
[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
64 $1_$2_WANT_INPLACE_WRAPPER = NO
65 endif
66
67 ifeq "$$(Windows)" "YES"
68 $1_$2_WANT_INSTALLED_WRAPPER = NO
69 else ifneq "$$($1_$2_INSTALL)" "YES"
70 $1_$2_WANT_INSTALLED_WRAPPER = NO
71 else ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
72 $1_$2_WANT_INSTALLED_WRAPPER = YES
73 else
74 $1_$2_WANT_INSTALLED_WRAPPER = NO
75 endif
76
77 $(call package-config,$1,$2,$3)
78
79 $1_$2_depfile_base = $1/$2/build/.depend
80
81 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
82 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
83 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
84 else
85 $1_$2_INPLACE =
86 endif
87 else
88 # Where do we install the inplace version?
89 ifeq "$$($1_$2_WANT_INPLACE_WRAPPER)" "YES"
90 $1_$2_INPLACE = $$(INPLACE_LIB)/bin/$$($1_$2_PROG)
91 else
92 ifeq "$$($1_$2_TOPDIR)" "YES"
93 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
94 else
95 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
96 endif
97 endif
98 endif
99
100 ifeq "$$($1_$2_USES_CABAL)" "YES"
101 $(call build-package-data,$1,$2,$3)
102 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
103 ifeq "$3" "0"
104 include $1/$2/package-data.mk
105 else ifeq "$(phase)" "final"
106 include $1/$2/package-data.mk
107 endif
108 endif
109 endif
110
111 $(call all-target,$1,all_$1_$2)
112 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
113
114 # INPLACE_BIN might be empty if we're distcleaning
115 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
116 ifeq "$$($1_$2_INSTALL_INPLACE)" "YES"
117 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
118 "$$(CP)" -p $$< $$@
119 endif
120 endif
121
122 $(call shell-wrapper,$1,$2)
123
124 ifeq "$$($1_$2_PROGRAM_WAY)" ""
125 ifeq "$3" "0"
126 $1_$2_PROGRAM_WAY = v
127 else ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
128 $1_$2_PROGRAM_WAY = dyn
129 else
130 $1_$2_PROGRAM_WAY = v
131 endif
132 endif
133
134 $1_$2_WAYS = $$($1_$2_PROGRAM_WAY)
135
136 $(call hs-sources,$1,$2)
137 $(call c-sources,$1,$2)
138
139 # --- IMPLICIT RULES
140
141 $(call distdir-way-opts,$1,$2,$$($1_$2_PROGRAM_WAY),$3)
142
143 ifeq "$3" "0"
144 # For stage 0, we use GHC to compile C sources so that we don't have to
145 # worry about where the RTS header files are
146 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
147 else
148 ifeq "$$($1_$2_UseGhcForCC)" "YES"
149 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),YES)
150 else
151 $(call c-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY),NO)
152 endif
153 endif
154
155 $(call hs-suffix-rules,$1,$2,$$($1_$2_PROGRAM_WAY))
156 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
157 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,$$($1_$2_PROGRAM_WAY),$$(dir))))
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 ifeq "$$(BootingFromHc)" "YES"
164 $1_$2_LINK_WITH_GCC = YES
165 endif
166
167 ifeq "$$($1_$2_$$($1_$2_PROGRAM_WAY)_HS_OBJS)" ""
168 # We don't want to link the GHC RTS into C-only programs. There's no
169 # point, and it confuses the test that all GHC-compiled programs
170 # were compiled with the right GHC.
171 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
172 endif
173
174 ifneq "$3" "0"
175 ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
176 $1_$2_GHC_LD_OPTS = \
177 -fno-use-rpaths \
178 $$(addprefix -optl-Wl$$(comma)-rpath -optl-Wl$$(comma),$$($1_$2_RPATHS))
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_DEP_NAMES),\
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 $$$$(libraries/$$(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_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 else
198 $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 $$$$@)/.
199 $$(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))
200 endif
201
202 # Note [lib-depends] if this program is built with stage1 or greater, we
203 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
204 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
205 # been included, this introduces an ordering dependency.
206 ifneq "$$(CLEANING)" "YES"
207 ifneq "$3" "0"
208 ifneq "$$($1_$2_HS_SRCS)" ""
209 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
210 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
211 endif
212 endif
213 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
214 endif
215 endif
216 endif
217
218 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
219 $(call all-target,$1_$2,$$($1_$2_INPLACE))
220 endif
221 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
222
223 ifeq "$$($1_$2_INSTALL)" "YES"
224 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
225 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
226 else ifeq "$$($1_$2_TOPDIR)" "YES"
227 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
228 else
229 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
230 endif
231 endif
232
233 $(call dependencies,$1,$2,$3)
234
235 endef