Refactor gcc.c, pulling out the reusable code
[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 # $1 = dir
24 # $2 = distdir
25 # $3 = GHC stage to use (0 == bootstrapping compiler)
26
27 ifneq "$$(CLEANING)" "YES"
28 ifeq "$$($1_$2_PROG)" ""
29 $$(error $1_$2_PROG is not set)
30 endif
31 endif
32
33 ifeq "$$(findstring $3,0 1 2)" ""
34 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
35 endif
36
37 $(call all-target,$1,all_$1_$2)
38
39 $(call clean-target,$1,$2,$1/$2)
40
41 $(call package-config,$1,$2,$3)
42
43 ifeq "$$($1_USES_CABAL)" "YES"
44 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
45 include $1/$2/package-data.mk
46 endif
47 endif
48
49 ifeq "$$($1_USES_CABAL)$$($1_$2_VERSION)" "YES"
50 $1_$2_DISABLE = YES
51 endif
52
53 ifeq "$$($1_$2_DISABLE)" "YES"
54
55 ifeq "$$(DEBUG)" "YES"
56 $$(warning $1/$2 disabled)
57 endif
58
59 # The following code to build the package all depends on settings
60 # obtained from package-data.mk.  If we don't have package-data.mk
61 # yet, then don't try to do anything else with this package.  Make will
62 # try to build package-data.mk, then restart itself and we'll be in business.
63
64 $(call all-target,$1_$2,$1/$2/package-data.mk)
65
66 # We have a rule for package-data.mk only when the package is
67 # disabled, because we want the build to fail if we haven't run phase 0.
68 ifneq "$$(BINDIST)" "YES"
69 $(call build-package-data,$1,$2,$3)
70 endif
71
72 else
73
74 ifneq "$$(BINDIST)" "YES"
75 $1_$2_WAYS = v
76
77 $(call hs-sources,$1,$2)
78 $(call c-sources,$1,$2)
79
80 # --- DEPENDENCIES
81
82 $1_$2_depfile_base = $1/$2/build/.depend
83
84 $(call build-dependencies,$1,$2)
85
86 # --- IMPLICIT RULES
87
88 # Just the 'v' way for programs
89 $(call distdir-way-opts,$1,$2,v,$3)
90
91 $(call c-suffix-rules,$1,$2,v,YES)
92
93 $(call hs-suffix-rules,$1,$2,v)
94 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
95   $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
96
97 $(call c-objs,$1,$2,v)
98 $(call hs-objs,$1,$2,v)
99
100 ifeq "$$(BootingFromHc)" "NO"
101 $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 $$$$@)/.
102         "$$($1_$2_HC)" -o $$@ $$($1_$2_v_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_v_C_OBJS) $$($1_$2_v_S_OBJS) $$($1_$2_OTHER_OBJS)
103 else
104 $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 $$$$@)/.
105         "$$(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)
106 endif
107
108 # Note [lib-depends] if this program is built with stage1 or greater, we
109 # need to depend on the libraries too.  NB. since $(ALL_STAGE1_LIBS) and
110 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
111 # been included, this introduces an ordering dependency.
112 ifneq "$3" "0"
113 ifeq "$$(ALL_STAGE1_LIBS)" ""
114 $$(error ordering failure in $1: $$(ALL_STAGE1_LIBS) is empty)
115 endif
116 $1/$2/build/tmp/$$($1_$2_PROG) : $$(ALL_STAGE1_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
117 endif
118 endif
119
120 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
121 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
122 else
123 # Where do we install the inplace version?
124 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
125 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
126 else
127 ifeq "$$($1_$2_TOPDIR)" "YES"
128 $1_$2_INPLACE = $$(INPLACE_TOPDIR)/$$($1_$2_PROG)
129 else
130 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
131 endif
132 endif
133
134 $(call all-target,$1_$2,$$($1_$2_INPLACE))
135 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
136
137 # INPLACE_BIN might be empty if we're distcleaning
138 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
139 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) | $$$$(dir $$$$@)/.
140         "$$(CP)" -p $$< $$@
141         touch $$@
142 endif
143
144 # touch is necessary; cp doesn't update the file time.
145 endif
146
147 $(call shell-wrapper,$1,$2)
148
149 ifeq "$$($1_$2_INSTALL)" "YES"
150 ifeq "$$($1_$2_TOPDIR)" "YES"
151 INSTALL_TOPDIRS += $1/$2/build/tmp/$$($1_$2_PROG)
152 else
153 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
154 endif
155 endif
156
157 endif # package-data.mk exists
158
159 endef