Merge remote branch 'origin/patch-4404'
[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 $(call package-config,$1,$2,$3)
58
59 $1_$2_depfile_base = $1/$2/build/.depend
60
61 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
62 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
63 $1_$2_INPLACE = $$(error $1_$2 should not be installed inplace, but INPLACE var evaluated)
64 else
65 $1_$2_INPLACE =
66 endif
67 else
68 # Where do we install the inplace version?
69 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
70 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
71 else
72 ifeq "$$($1_$2_TOPDIR)" "YES"
73 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
74 else
75 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
76 endif
77 endif
78 endif
79
80 ifeq "$$($1_$2_USES_CABAL)" "YES"
81 $(call build-package-data,$1,$2,$3)
82 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
83 ifeq "$3" "0"
84 include $1/$2/package-data.mk
85 else ifeq "$(phase)" "final"
86 include $1/$2/package-data.mk
87 endif
88 endif
89 endif
90
91 $(call all-target,$1,all_$1_$2)
92 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
93
94 # INPLACE_BIN might be empty if we're distcleaning
95 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
96 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
97 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
98 "$$(CP)" -p $$< $$@
99 touch $$@
100 endif
101 endif
102
103 $(call shell-wrapper,$1,$2)
104
105 $1_$2_WAYS = v
106
107 $(call hs-sources,$1,$2)
108 $(call c-sources,$1,$2)
109
110 # --- IMPLICIT RULES
111
112 # Just the 'v' way for programs
113 $(call distdir-way-opts,$1,$2,v,$3)
114
115 ifeq "$3" "0"
116 # For stage 0, we use GHC to compile C sources so that we don't have to
117 # worry about where the RTS header files are
118 $(call c-suffix-rules,$1,$2,v,YES)
119 else
120 $(call c-suffix-rules,$1,$2,v,NO)
121 endif
122
123 $(call hs-suffix-rules,$1,$2,v)
124 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
125 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
126
127 $(call c-objs,$1,$2,v)
128 $(call hs-objs,$1,$2,v)
129
130 $1_$2_LINK_WITH_GCC = NO
131 ifeq "$$(BootingFromHc)" "YES"
132 $1_$2_LINK_WITH_GCC = YES
133 endif
134
135 ifeq "$$($1_$2_v_HS_OBJS)" ""
136 # We don't want to link the GHC RTS into C-only programs. There's no
137 # point, and it confuses the test that all GHC-compiled programs
138 # were compiled with the right GHC.
139 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
140 endif
141
142 ifneq "$$(BINDIST)" "YES"
143 # The quadrupled $'s here are because the _v_LIB variables aren't
144 # necessarily set when this part of the makefile is read
145 $1/$2/build/tmp/$$($1_$2_PROG) : \
146 $$(foreach dep,$$($1_$2_DEP_NAMES),\
147 $$(if $$(filter ghc,$$(dep)),\
148 $(if $(filter 0,$3),$$(compiler_stage1_v_LIB),\
149 $(if $(filter 1,$3),$$(compiler_stage2_v_LIB),\
150 $(if $(filter 2,$3),$$(compiler_stage2_v_LIB),\
151 $$(error Bad build stage)))),\
152 $$$$(libraries/$$(dep)_dist-$(if $(filter 0,$3),boot,install)_v_LIB)))
153
154 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
155 $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 $$$$@)/.
156 "$$($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))
157 else
158 $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 $$$$@)/.
159 "$$(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))
160 endif
161
162 # Note [lib-depends] if this program is built with stage1 or greater, we
163 # need to depend on the libraries too. NB. since $(ALL_STAGE1_LIBS) and
164 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
165 # been included, this introduces an ordering dependency.
166 ifneq "$$(CLEANING)" "YES"
167 ifneq "$3" "0"
168 ifneq "$$($1_$2_HS_SRCS)" ""
169 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
170 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
171 endif
172 endif
173 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
174 endif
175 endif
176 endif
177
178 ifneq "$$($1_$2_INSTALL_INPLACE)" "NO"
179 $(call all-target,$1_$2,$$($1_$2_INPLACE))
180 endif
181 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
182
183 ifeq "$$($1_$2_INSTALL)" "YES"
184 ifeq "$$($1_$2_TOPDIR)" "YES"
185 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
186 else
187 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
188 endif
189 endif
190
191 # --- DEPENDENCIES
192 # We always have the dependency rules available, as we need to know
193 # how to build hsc2hs's dependency file in phase 0
194 $(call build-dependencies,$1,$2,$3)
195 ifneq "$(phase)" "0"
196 # From phase 1 we actually include the dependency files for the
197 # bootstrapping stuff
198 ifeq "$3" "0"
199 $(call include-dependencies,$1,$2,$3)
200 else ifeq "$(phase)" "final"
201 # In the final phase, we also include the dependency files for
202 # everything else
203 $(call include-dependencies,$1,$2,$3)
204 endif
205 endif
206
207 endef