Omit unnecessary linker flags
[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 # ifneq "$$(HostOS_CPP)" "mingw32"
93 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
94 $$(call cmd,$1_$2_HC) $$($1_$2_$3_ALL_HC_OPTS) $$($1_$2_$3_GHC_LD_OPTS) $$($1_$2_$3_ALL_OBJS) \
95 -shared -dynamic -dynload deploy \
96 $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
97 -no-auto-link-packages \
98 -o $$@
99 endif # "$$(HostOS_CPP)" "mingw32"
100
101 else # ifneq "$3" "dyn"
102
103 # Build the ordinary .a library
104 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS)
105 $$(call removeFiles,$$@ $$@.contents)
106 ifeq "$$($1_$2_SplitObjs)" "YES"
107 $$(FIND) $$(patsubst %.$$($3_osuf),%_$$($3_osuf)_split,$$($1_$2_$3_HS_OBJS)) -name '*.$$($3_osuf)' -print >> $$@.contents
108 echo $$($1_$2_$3_NON_HS_OBJS) >> $$@.contents
109 else
110 echo $$($1_$2_$3_ALL_OBJS) >> $$@.contents
111 endif
112 ifeq "$$($1_$2_ArSupportsAtFile)" "YES"
113 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ @$$@.contents
114 else
115 "$$(XARGS)" $$(XARGS_OPTS) "$$($1_$2_AR)" $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ < $$@.contents
116 endif
117 $$(call removeFiles,$$@.contents)
118
119 ifeq "$$(HostOS_CPP)" "mingw32"
120 ifneq "$$($1_$2_$3_LIB0)" ""
121 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
122 $$($1_$2_$3_LIB0) :
123 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@
124 endif
125 endif
126
127 endif # "$3" "dyn"
128
129 $(call all-target,$1_$2,all_$1_$2_$3)
130 $(call all-target,$1_$2_$3,$$($1_$2_$3_LIB))
131
132 # Don't put bootstrapping packages in the bindist
133 ifneq "$4" "0"
134 BINDIST_HI += $$($1_$2_$3_HI)
135 BINDIST_LIBS += $$($1_$2_$3_LIB)
136 BINDIST_LIBS += $$($1_$2_$3_LIB0)
137 endif
138
139 ifeq "$$($1_$2_SplitSections)" "YES"
140 ifeq "$(LdIsGNULd)" "YES"
141 $1_$2_LD_SCRIPT = driver/utils/merge_sections.ld
142 endif
143 endif
144
145 # Build the GHCi library
146 ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
147 $1_$2_GHCI_LIB = $$($1_$2_dyn_LIB)
148 else
149 ifeq "$3" "v"
150 $1_$2_GHCI_LIB = $1/$2/build/HS$$($1_$2_COMPONENT_ID).$$($3_osuf)
151 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
152 # Don't put bootstrapping packages in the bindist
153 ifneq "$4" "0"
154 BINDIST_LIBS += $$($1_$2_GHCI_LIB)
155 endif
156 endif
157 $$($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) $$($1_$2_LD_SCRIPT)
158 $$(call cmd,LD) $$(CONF_LD_LINKER_OPTS_STAGE$4) -r $$(if $$($1_$2_LD_SCRIPT),-T $$($1_$2_LD_SCRIPT)) -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)
159
160 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
161 # Don't bother making ghci libs for bootstrapping packages
162 ifneq "$4" "0"
163 $(call all-target,$1_$2,$$($1_$2_GHCI_LIB))
164 endif
165 endif # "$$($1_$2_BUILD_GHCI_LIB)" "YES"
166 endif # "$3" "v"
167 endif # "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
168
169 $(call profEnd, build-package-way($1,$2,$3))
170 endef # build-package-way
171
172 # $1 = dir
173 # $2 = distdir
174 # $3 = way
175 # $4 = extra flags
176 # $5 = object files to link
177 # $6 = output filename
178 define build-dll
179 $(call cmd,$1_$2_HC) $($1_$2_$3_ALL_HC_OPTS) $($1_$2_$3_GHC_LD_OPTS) $4 $5 \
180 -shared -dynamic -dynload deploy \
181 $(addprefix -l,$($1_$2_EXTRA_LIBRARIES)) \
182 -no-auto-link-packages \
183 -o $6
184 # Now check that the DLL doesn't have too many symbols. See trac #5987.
185 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"
186 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
187 endef
188