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