Do dependency analysis when kind-checking type declarations
[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 $(call all-target,$1,all_$1_$2)
52
53 ifeq "$$($1_USES_CABAL)" "YES"
54 $1_$2_USES_CABAL = YES
55 endif
56
57 ifeq "$$($1_$2_USES_CABAL)" "YES"
58 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
59 include $1/$2/package-data.mk
60 endif
61 endif
62
63 $(call package-config,$1,$2,$3)
64
65 ifeq "$$($1_$2_USES_CABAL)$$($1_$2_VERSION)" "YES"
66 $1_$2_DISABLE = YES
67 endif
68
69 ifeq "$$($1_$2_DISABLE)" "YES"
70
71 ifeq "$$(DEBUG)" "YES"
72 $$(warning $1/$2 disabled)
73 endif
74
75 # The following code to build the package all depends on settings
76 # obtained from package-data.mk.  If we don't have package-data.mk
77 # yet, then don't try to do anything else with this package.  Make will
78 # try to build package-data.mk, then restart itself and we'll be in business.
79
80 $(call all-target,$1_$2,$1/$2/package-data.mk)
81
82 # We have a rule for package-data.mk only when the package is
83 # disabled, because we want the build to fail if we haven't run phase 0.
84 ifneq "$$(BINDIST)" "YES"
85 $(call build-package-data,$1,$2,$3)
86 endif
87
88 else
89
90 ifneq "$$(BINDIST)" "YES"
91 $1_$2_WAYS = v
92
93 $(call hs-sources,$1,$2)
94 $(call c-sources,$1,$2)
95
96 # --- DEPENDENCIES
97
98 $1_$2_depfile_base = $1/$2/build/.depend
99
100 $(call build-dependencies,$1,$2,$3)
101
102 # --- IMPLICIT RULES
103
104 # Just the 'v' way for programs
105 $(call distdir-way-opts,$1,$2,v,$3)
106
107 ifeq "$3" "0"
108 # For stage 0, we use GHC to compile C sources so that we don't have to
109 # worry about where the RTS header files are
110 $(call c-suffix-rules,$1,$2,v,YES)
111 else
112 $(call c-suffix-rules,$1,$2,v,NO)
113 endif
114
115 $(call hs-suffix-rules,$1,$2,v)
116 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
117   $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
118
119 $(call c-objs,$1,$2,v)
120 $(call hs-objs,$1,$2,v)
121
122 $1_$2_LINK_WITH_GCC = NO
123 ifeq "$$(BootingFromHc)" "YES"
124 $1_$2_LINK_WITH_GCC = YES
125 endif
126
127 ifeq "$$($1_$2_v_HS_OBJS)" ""
128 # We don't want to link the GHC RTS into C-only programs. There's no
129 # point, and it confuses the test that all GHC-compiled programs
130 # were compiled with the right GHC.
131 $1_$2_GHC_LD_OPTS = -no-auto-link-packages -no-hs-main
132 endif
133
134 ifeq "$$($1_$2_LINK_WITH_GCC)" "NO"
135 $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 $$$$@)/.
136         "$$($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))
137 else
138 $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 $$$$@)/.
139         "$$(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))
140 endif
141
142 # Note [lib-depends] if this program is built with stage1 or greater, we
143 # need to depend on the libraries too.  NB. since $(ALL_STAGE1_LIBS) and
144 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
145 # been included, this introduces an ordering dependency.
146 ifneq "$$(CLEANING)" "YES"
147 ifneq "$3" "0"
148 ifneq "$$($1_$2_HS_SRCS)" ""
149 ifeq "$$(strip $$(ALL_STAGE1_LIBS))" ""
150 $$(error ordering failure in $1 ($2): ALL_STAGE1_LIBS is empty)
151 endif
152 endif
153 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
154 endif
155 endif
156 endif
157
158 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
159 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
160 else
161 # Where do we install the inplace version?
162 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
163 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
164 else
165 ifeq "$$($1_$2_TOPDIR)" "YES"
166 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
167 else
168 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
169 endif
170 endif
171
172 $(call all-target,$1_$2,$$($1_$2_INPLACE))
173 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
174
175 # INPLACE_BIN might be empty if we're distcleaning
176 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
177 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
178         "$$(CP)" -p $$< $$@
179         touch $$@
180 endif
181
182 # touch is necessary; cp doesn't update the file time.
183 endif
184
185 $(call shell-wrapper,$1,$2)
186
187 ifeq "$$($1_$2_INSTALL)" "YES"
188 ifeq "$$($1_$2_TOPDIR)" "YES"
189 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
190 else
191 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
192 endif
193 endif
194
195 endif # package-data.mk exists
196
197 endef