GHC new build system megapatch
[ghc.git] / rules / build-prog.mk
1
2 # Build a program with the stage-1 compiler. Invoke like this:
3 #
4 # utils/genapply_MODULES = Main
5 # utils/genapply_HC_OPTS = -package Cabal
6 # utils/genapply_dist_PROG = genapply
7 #
8 # $(eval $(call build-prog,utils/genapply,dist-install))
9
10 define build-prog
11 # $1 = dir
12 # $2 = distdir
13 # $3 = GHC stage to use (0 == bootstrapping compiler)
14
15 ifeq "$$($1_$2_PROG)" ""
16 $$(error $1_$2_PROG is not set)
17 endif
18
19 ifeq "$$(findstring $3,0 1 2)" ""
20 $$(error $1/$2: stage argument to build-prog should be 0, 1, or 2)
21 endif
22
23 $(call all-target,$1,all_$1_$2)
24
25 $(call clean-target,$1,$2,$1/$2)
26
27 $(call package-config,$1,$2,$3)
28
29 ifeq "$$($1_USES_CABAL)" "YES"
30 ifneq "$$(NO_INCLUDE_PKGDATA)" "YES"
31 include $1/$2/package-data.mk
32 endif
33 endif
34
35 ifeq "$$($1_USES_CABAL)$$($1_$2_VERSION)" "YES"
36 $1_$2_DISABLE = YES
37 endif
38
39 ifeq "$$($1_$2_DISABLE)" "YES"
40
41 ifeq "$$(DEBUG)" "YES"
42 $$(warning $1/$2 disabled)
43 endif
44
45 # The following code to build the package all depends on settings
46 # obtained from package-data.mk. If we don't have package-data.mk
47 # yet, then don't try to do anything else with this package. Make will
48 # try to build package-data.mk, then restart itself and we'll be in business.
49
50 $(call all-target,$1_$2,$1/$2/package-data.mk)
51
52 # We have a rule for package-data.mk only when the package is
53 # disabled, because we want the build to fail if we haven't run phase 0.
54 ifneq "$(BINDIST)" "YES"
55 $(call build-package-data,$1,$2)
56 endif
57
58 else
59
60 ifneq "$(BINDIST)" "YES"
61 $(call hs-sources,$1,$2)
62 $(call c-sources,$1,$2)
63
64 # --- DEPENDENCIES
65
66 $1_$2_depfile = $1/$2/build/.depend
67
68 $(call build-dependencies,$1,$2)
69
70 # --- IMPLICIT RULES
71
72 $(call distdir-opts,$1,$2,$3)
73
74 # Just the 'v' way for programs
75 $(call distdir-way-opts,$1,$2,v)
76
77 $(call c-suffix-rules,$1,$2,v,YES)
78
79 $(call hs-suffix-rules,$1,$2,v)
80 $$(foreach dir,$$($1_$2_HS_SRC_DIRS),\
81 $$(eval $$(call hs-suffix-rules-srcdir,$1,$2,v,$$(dir))))
82
83 $(call c-objs,$1,$2)
84 $(call hs-objs,$1,$2,v)
85
86 $1/$2/build/tmp/$$($1_$2_PROG) : $$($1_$2_v_HS_OBJS) $$($1_$2_C_OBJS) $$($1_$2_S_OBJS) $$($1_$2_OTHER_OBJS)
87 $$(MKDIRHIER) $$(dir $$@)
88 $$($1_$2_HC) -o $$@ $$($1_$2_v_ALL_HC_OPTS) $$(LD_OPTS) $$($1_$2_v_HS_OBJS) $$($1_$2_C_OBJS) $$($1_$2_S_OBJS) $$($1_$2_OTHER_OBJS)
89
90 # Note [lib-depends] if this program is built with stage1 or greater, we
91 # need to depend on the libraries too. NB. since $(ALL_LIBS) and
92 # $(ALL_RTS_LIBS) are not defined until after libraries/*/ghc.mk have
93 # been included, this introduces an ordering dependency.
94 ifneq "$3" "0"
95 ifeq "$$(ALL_LIBS)" ""
96 $$(error ordering failure in $1: $$(ALL_LIBS) is empty)
97 endif
98 $1/$2/build/tmp/$($1_$2_PROG) : $$(ALL_LIBS) $$(ALL_RTS_LIBS) $$(OTHER_LIBS)
99 endif
100 endif
101
102 ifeq "$$($1_$2_INSTALL_INPLACE)" "NO"
103 $(call all-target,$1_$2,$1/$2/build/tmp/$$($1_$2_PROG))
104 else
105 # Where do we install the inplace version?
106 ifeq "$$($1_$2_SHELL_WRAPPER) $$(Windows)" "YES NO"
107 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
108 else
109 ifeq "$$($1_$2_LIBEXEC)" "YES"
110 $1_$2_INPLACE = $$(INPLACE_LIB)/$$($1_$2_PROG)
111 else
112 $1_$2_INPLACE = $$(INPLACE_BIN)/$$($1_$2_PROG)
113 endif
114 endif
115
116 $(call all-target,$1_$2,$$($1_$2_INPLACE))
117 $(call clean-target,$1,$2_inplace,$$($1_$2_INPLACE))
118
119 $$($1_$2_INPLACE) : $1/$2/build/tmp/$$($1_$2_PROG) $$(MKDIRHIER)
120 $$(MKDIRHIER) $$(dir $$@)
121 $$(CP) -p $$< $$@
122 touch $$@
123
124 # touch is necessary; cp doesn't update the file time.
125 endif
126
127 $(call shell-wrapper,$1,$2)
128
129 ifeq "$$($1_$2_INSTALL)" "YES"
130 ifeq "$$($1_$2_LIBEXEC)" "YES"
131 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
132 else
133 INSTALL_BINS += $1/$2/build/tmp/$$($1_$2_PROG)
134 endif
135 endif
136
137 endif # package-data.mk exists
138
139 endef