Build system improvements
[ghc-hetmet.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 # $1 = dir
25 # $2 = distdir
26 # $3 = GHC stage to use (0 == bootstrapping compiler)
27
28 ifneq "$$(CLEANING)" "YES"
29 ifeq "$$($1_$2_PROG)" ""
30 $$(error $1_$2_PROG is not set)
31 endif
32 endif
33
34 ifeq "$$(findstring $3,0 1 2)" ""
35 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
36 endif
37
38 $(call clean-target,$1,$2,$1/$2)
39
40 ifneq "$$($1_$2_NOT_NEEDED)" "YES"
41 $$(eval $$(call build-prog-helper,$1,$2,$3))
42 endif
43 endef
44
45
46 define build-prog-helper
47 # $1 = dir
48 # $2 = distdir
49 # $3 = GHC stage to use (0 == bootstrapping compiler)
50
51 ifeq "$$($1_USES_CABAL)" "YES"
52 $1_$2_USES_CABAL = YES
53 endif
54
55 $(call package-config,$1,$2,$3)
56
57 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
58 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
59 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
60 else
61 $1_$2_INPLACE =
62 endif
63 else
64 # Where do we install the inplace version?
65 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
66 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
67 else
68 ifeq "$$($1_$2_TOPDIR)" "YES"
69 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
70 else
71 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
72 endif
73 endif
74 endif
75
76 ########################################
77 ifeq "$$($1_$2_CONFIGURE_PHASE)" ""
78 $$(error No configure phase for $1_$2)
79 else ifeq "$$($1_$2_CONFIGURE_PHASE)" "$$(phase)"
80
81 ifeq "$$(DEBUG)" "YES"
82 $$(warning $1/$2 configure phase)
83 endif
84
85 ifneq "$$(BINDIST)" "YES"
86 $(call build-package-data,$1,$2,$3)
87 endif
88
89 ifeq "$$($1_$2_USES_CABAL)" "YES"
90 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
91 include $1/$2/package-data.mk
92 endif
93 endif
94
95 # INPLACE_BIN might be empty if we're distcleaning
96 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
97 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
98 $$($1_$2_INPLACE) :
99         $$(error $1_$2 is configuring, but trying to build $$($1_$2_INPLACE)")
100 endif
101 endif
102
103 else ifeq "$$(phase_$$($1_$2_CONFIGURE_PHASE)_or_later)" "YES"
104
105 ifeq "$$(DEBUG)" "YES"
106 $$(warning $1/$2 build phase)
107 endif
108
109 ifeq "$$($1_$2_USES_CABAL)" "YES"
110 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
111 include $1/$2/package-data.mk
112 ifeq "$$($1_$2_VERSION)" ""
113 $$(error No version for $1_$2 found)
114 endif
115 endif
116 endif
117
118 $(call all-target,$1,all_$1_$2)
119 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
120
121 # INPLACE_BIN might be empty if we're distcleaning
122 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
123 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
124 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
125         "$$(CP)" -p $$< $$@
126         touch $$@
127 endif
128 endif
129
130 else
131
132 ifeq "$$(DEBUG)" "YES"
133 $$(warning $1/$2 disabled phase)
134 endif
135
136 # INPLACE_BIN might be empty if we're distcleaning
137 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
138 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
139 $$($1_$2_INPLACE) :
140         $$(error $1_$2 is disabled, but trying to build $$($1_$2_INPLACE)")
141 endif
142 endif
143
144 endif
145 ########################################
146
147 $(call shell-wrapper,$1,$2)
148
149 ifeq "$$(phase_$$($1_$2_CONFIGURE_PHASE)_done)" "YES"
150
151 ifneq "$$(BINDIST)" "YES"
152 $1_$2_WAYS = v
153
154 $(call hs-sources,$1,$2)
155 $(call c-sources,$1,$2)
156
157 # --- DEPENDENCIES
158
159 $1_$2_depfile_base = $1/$2/build/.depend
160
161 $(call build-dependencies,$1,$2,$3)
162
163 # --- IMPLICIT RULES
164
165 # Just the 'v' way for programs
166 $(call distdir-way-opts,$1,$2,v,$3)
167
168 ifeq "$3" "0"
169 # For stage 0, we use GHC to compile C sources so that we don't have to
170 # worry about where the RTS header files are
171 $(call c-suffix-rules,$1,$2,v,YES)
172 else
173 $(call c-suffix-rules,$1,$2,v,NO)
174 endif
175
176 $(call hs-suffix-rules,$1,$2,v)
177 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
178   $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
179
180 $(call c-objs,$1,$2,v)
181 $(call hs-objs,$1,$2,v)
182
183 $1_$2_LINK_WITH_GCC = NO
184 ifeq "$$(BootingFromHc)" "YES"
185 $1_$2_LINK_WITH_GCC = YES
186 endif
187
188 ifeq "$$($1_$2_v_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_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
193 endif
194
195 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
196 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
197         "$$($1_$2_HC)" -o $$@ $$($1_$2_v_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_GHC_LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
198 else
199 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) | $$$$(dir $$$$@)/.
200         "$$(CC)" -o $$@ $$($1_$2_v_ALL_CC_OPTS) $$(LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS) $$($1_$2_v_EXTRA_CC_OPTS) $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES))
201 endif
202
203 # Note [lib-depends] if this program is built with stage1 or greater, we
204 # need to depend on the libraries too.  NB. since $(ALL_STAGE1_LIBS) and
205 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
206 # been included, this introduces an ordering dependency.
207 ifneq "$$(CLEANING)" "YES"
208 ifneq "$3" "0"
209 ifneq "$$($1_$2_HS_SRCS)" ""
210 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
211 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
212 endif
213 endif
214 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
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 # touch is necessary; cp doesn't update the file time.
224 endif
225
226 ifeq "$$($1_$2_INSTALL)" "YES"
227 ifeq "$$($1_$2_TOPDIR)" "YES"
228 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
229 else
230 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
231 endif
232 endif
233
234 endif # package-data.mk exists
235
236 endef