Build system: fix `make -j1` (#10973)
[ghc.git] / rules / build-package-way.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://ghc.haskell.org/trac/ghc/wiki/Building/Architecture
9 # http://ghc.haskell.org/trac/ghc/wiki/Building/Modifying
10 #
11 # -----------------------------------------------------------------------------
12
13
14 define build-package-way # $1 = dir, $2 = distdir, $3 = way, $4 = stage
15 $(call trace, build-package-way($1,$2,$3))
16 $(call profStart, build-package-way($1,$2,$3))
17
18 $(call distdir-way-opts,$1,$2,$3,$4)
19 $(call hs-suffix-way-rules,$1,$2,$3)
20
21 $(call hs-objs,$1,$2,$3)
22
23 # The .a/.so library file, indexed by two different sets of vars:
24 # the first is indexed by the dir, distdir and way
25 # the second is indexed by the package id, distdir and way
26 $1_$2_$3_LIB_FILE = libHS$$($1_$2_COMPONENT_ID)$$($3_libsuf)
27 $1_$2_$3_LIB = $1/$2/build/$$($1_$2_$3_LIB_FILE)
28 $$($1_$2_COMPONENT_ID)_$2_$3_LIB = $$($1_$2_$3_LIB)
29
30 ifeq "$$(HostOS_CPP)" "mingw32"
31 ifneq "$$($1_$2_dll0_HS_OBJS)" ""
32 $1_$2_$3_LIB0_ROOT = HS$$($1_$2_COMPONENT_ID)-0$$($3_libsuf)
33 $1_$2_$3_LIB0_NAME = lib$$($1_$2_$3_LIB0_ROOT)
34 $1_$2_$3_LIB0 = $1/$2/build/$$($1_$2_$3_LIB0_NAME)
35 endif
36 endif
37
38 # Note [inconsistent distdirs]
39 #
40 # hack: the DEPS_LIBS mechanism assumes that the distdirs for packages
41 # that depend on each other are the same, but that is not the case for
42 # ghc where we use stage1/stage2 rather than dist/dist-install.
43 # Really we should use a consistent scheme for distdirs, but in the
44 # meantime we work around it by defining ghc-<ver>_dist-install_way_LIB:
45 #
46 # A similar hack is applied to the PROGRAM_DEP_LIB mechanism in
47 # rules/build-package.mk.
48 ifeq "$$($1_PACKAGE) $2" "ghc stage2"
49 $$($1_$2_COMPONENT_ID)_dist-install_$3_LIB = $$($1_$2_$3_LIB)
50 endif
51
52 # All the .a/.so library file dependencies for this library.
53 #
54 # The $(subst stage2,dist-install,..) is needed due to Note
55 # [inconsistent distdirs].
56 #
57 # NB: Use DEP_COMPONENT_IDS for the /directory/ parameter.
58 $1_$2_$3_DEPS_LIBS=$$(foreach dep,$$($1_$2_DEP_COMPONENT_IDS),$$($$(dep)_$(subst stage2,dist-install,$2)_$3_LIB))
59
60 $1_$2_$3_NON_HS_OBJS = $$($1_$2_$3_CMM_OBJS) $$($1_$2_$3_C_OBJS) $$($1_$2_$3_S_OBJS) $$($1_$2_EXTRA_OBJS)
61 $1_$2_$3_ALL_OBJS = $$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS)
62
63 ifeq "$3" "dyn"
64
65 ifneq "$$($1_$2_dll0_MODULES)" ""
66 $$($1_$2_$3_LIB) : $1/$2/dll-split.stamp
67 ifneq "$$($1_$2_$3_LIB0)" ""
68 $$($1_$2_$3_LIB0) : $1/$2/dll-split.stamp
69 endif
70 endif
71
72 $1/$2/dll-split.stamp: $$($1_$2_depfile_haskell) $$$$(dll-split_INPLACE)
73 $$(dll-split_INPLACE) $$< "$$($1_$2_dll0_START_MODULE)" "$$($1_$2_dll0_MODULES)"
74 touch $$@
75
76 # Link a dynamic library
77 # On windows we have to supply the extra libs this one links to when building it.
78 ifeq "$$(HostOS_CPP)" "mingw32"
79 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
80 ifneq "$$($1_$2_$3_LIB0)" ""
81 $$(call build-dll,$1,$2,$3,-L$1/$2/build -l$$($1_$2_$3_LIB0_ROOT),$$(filter-out $$($1_$2_dll0_HS_OBJS),$$($1_$2_$3_HS_OBJS)) $$($1_$2_$3_NON_HS_OBJS),$$@)
82 else
83 $$(call build-dll,$1,$2,$3,,$$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$@)
84 endif
85
86 ifneq "$$($1_$2_$3_LIB0)" ""
87 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
88 $$($1_$2_$3_LIB0) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
89 $$(call build-dll,$1,$2,$3,,$$($1_$2_dll0_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$($1_$2_$3_LIB0))
90 endif
91
92 else
93
94 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
95 $$(call cmd,$1_$2_HC) $$($1_$2_$3_ALL_HC_OPTS) $$($1_$2_$3_GHC_LD_OPTS) $$($1_$2_$3_ALL_OBJS) \
96 -shared -dynamic -dynload deploy \
97 $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
98 -no-auto-link-packages \
99 -o $$@
100 endif
101 else
102 # Build the ordinary .a library
103 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS)
104 $$(call removeFiles,$$@ $$@.contents)
105 ifeq "$$($1_$2_SplitObjs)" "YES"
106 $$(FIND) $$(patsubst %.$$($3_osuf),%_$$($3_osuf)_split,$$($1_$2_$3_HS_OBJS)) -name '*.$$($3_osuf)' -print >> $$@.contents
107 echo $$($1_$2_$3_NON_HS_OBJS) >> $$@.contents
108 else
109 echo $$($1_$2_$3_ALL_OBJS) >> $$@.contents
110 endif
111 ifeq "$$($1_$2_ArSupportsAtFile)" "YES"
112 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ @$$@.contents
113 else
114 "$$(XARGS)" $$(XARGS_OPTS) "$$($1_$2_AR)" $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ < $$@.contents
115 endif
116 $$(call removeFiles,$$@.contents)
117
118 ifeq "$$(HostOS_CPP)" "mingw32"
119 ifneq "$$($1_$2_$3_LIB0)" ""
120 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
121 $$($1_$2_$3_LIB0) :
122 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@
123 endif
124 endif
125
126 endif
127
128 $(call all-target,$1_$2,all_$1_$2_$3)
129 $(call all-target,$1_$2_$3,$$($1_$2_$3_LIB))
130
131 # Don't put bootstrapping packages in the bindist
132 ifneq "$4" "0"
133 BINDIST_HI += $$($1_$2_$3_HI)
134 BINDIST_LIBS += $$($1_$2_$3_LIB)
135 BINDIST_LIBS += $$($1_$2_$3_LIB0)
136 endif
137
138 # Build the GHCi library
139 ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
140 $1_$2_GHCI_LIB = $$($1_$2_dyn_LIB)
141 else
142 ifeq "$3" "v"
143 $1_$2_GHCI_LIB = $1/$2/build/HS$$($1_$2_COMPONENT_ID).$$($3_osuf)
144 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
145 # Don't put bootstrapping packages in the bindist
146 ifneq "$4" "0"
147 BINDIST_LIBS += $$($1_$2_GHCI_LIB)
148 endif
149 endif
150 $$($1_$2_GHCI_LIB) : $$($1_$2_$3_HS_OBJS) $$($1_$2_$3_CMM_OBJS) $$($1_$2_$3_C_OBJS) $$($1_$2_$3_S_OBJS) $$($1_$2_EXTRA_OBJS)
151 $$(call cmd,LD) $$(CONF_LD_LINKER_OPTS_STAGE$4) -r -o $$@ $$(EXTRA_LD_OPTS) $$($1_$2_$3_HS_OBJS) $$($1_$2_$3_CMM_OBJS) $$($1_$2_$3_C_OBJS) $$($1_$2_$3_S_OBJS) $$($1_$2_EXTRA_OBJS)
152
153 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
154 # Don't bother making ghci libs for bootstrapping packages
155 ifneq "$4" "0"
156 $(call all-target,$1_$2,$$($1_$2_GHCI_LIB))
157 endif
158 endif
159 endif
160 endif
161
162 $(call profEnd, build-package-way($1,$2,$3))
163 endef
164
165 # $1 = dir
166 # $2 = distdir
167 # $3 = way
168 # $4 = extra flags
169 # $5 = object files to link
170 # $6 = output filename
171 define build-dll
172 $(call cmd,$1_$2_HC) $($1_$2_$3_ALL_HC_OPTS) $($1_$2_$3_GHC_LD_OPTS) $4 $5 \
173 -shared -dynamic -dynload deploy \
174 $(addprefix -l,$($1_$2_EXTRA_LIBRARIES)) \
175 -no-auto-link-packages \
176 -o $6
177 # Now check that the DLL doesn't have too many symbols. See trac #5987.
178 SYMBOLS=`$(OBJDUMP) -p $6 | sed -n "1,/^.Ordinal\/Name Pointer/ D; p; /^$$/ q" | tail -n +2 | wc -l`; echo "Number of symbols in $6: $$SYMBOLS"
179 case `$(OBJDUMP) -p $6 | sed -n "1,/^.Ordinal\/Name Pointer/ D; p; /^$$/ q" | grep "\[ *0\]" | wc -l` in 1) echo DLL $6 OK;; 0) echo No symbols in DLL $6; exit 1;; [0-9]*) echo Too many symbols in DLL $6; $(OBJDUMP) -p $6 | sed -n "1,/^.Ordinal\/Name Pointer/ D; p; /^$$/ q" | tail; exit 1;; *) echo bad DLL $6; exit 1;; esac
180 endef
181