Fix the dynmaic library paths in the libs, as well as in the programs
[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://hackage.haskell.org/trac/ghc/wiki/Building/Architecture
9 # http://hackage.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_NAME = libHS$$($1_PACKAGE)-$$($1_$2_VERSION)$$($3_libsuf)
27 $1_$2_$3_LIB = $1/$2/build/$$($1_$2_$3_LIB_NAME)
28 $$($1_PACKAGE)-$$($1_$2_VERSION)_$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_PACKAGE)-$$($1_$2_VERSION)-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 # hack: the DEPS_LIBS mechanism assumes that the distdirs for packages
40 # that depend on each other are the same, but that is not the case for
41 # ghc where we use stage1/stage2 rather than dist/dist-install.
42 # Really we should use a consistent scheme for distdirs, but in the
43 # meantime we work around it by defining ghc-<ver>_dist-install_way_LIB:
44 ifeq "$$($1_PACKAGE) $2" "ghc stage2"
45 $$($1_PACKAGE)-$$($1_$2_VERSION)_dist-install_$3_LIB = $$($1_$2_$3_LIB)
46 endif
47
48 # All the .a/.so library file dependencies for this library.
49 #
50 # The $(subst stage2,dist-install,..) is needed due to Note
51 # [inconsistent distdirs].
52 $1_$2_$3_DEPS_LIBS=$$(foreach dep,$$($1_$2_DEPS),$$($$(dep)_$(subst stage2,dist-install,$2)_$3_LIB))
53
54 $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)
55 $1_$2_$3_ALL_OBJS = $$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS)
56
57 ifeq "$3" "dyn"
58
59 ifneq "$$($1_$2_dll0_MODULES)" ""
60 $$($1_$2_$3_LIB) : $1/$2/dll-split.stamp
61 ifneq "$$($1_$2_$3_LIB0)" ""
62 $$($1_$2_$3_LIB0) : $1/$2/dll-split.stamp
63 endif
64 endif
65
66 $1/$2/dll-split.stamp: $$($1_$2_depfile_haskell) $$$$(dll-split_INPLACE)
67 $$(dll-split_INPLACE) $$< "$$($1_$2_dll0_START_MODULE)" "$$($1_$2_dll0_MODULES)"
68 touch $$@
69
70 # Link a dynamic library
71 # On windows we have to supply the extra libs this one links to when building it.
72 ifeq "$$(HostOS_CPP)" "mingw32"
73 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
74 ifneq "$$($1_$2_$3_LIB0)" ""
75 $$(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),$$@)
76 else
77 $$(call build-dll,$1,$2,$3,,$$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$@)
78 endif
79
80 ifneq "$$($1_$2_$3_LIB0)" ""
81 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
82 $$($1_$2_$3_LIB0) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
83 $$(call build-dll,$1,$2,$3,,$$($1_$2_dll0_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$($1_$2_$3_LIB0))
84 endif
85
86 else
87
88 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
89 $$(call cmd,$1_$2_HC) $$($1_$2_$3_ALL_HC_OPTS) $$($1_$2_$3_GHC_LD_OPTS) $$($1_$2_$3_ALL_OBJS) \
90 -shared -dynamic -dynload deploy \
91 $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
92 -no-auto-link-packages \
93 -o $$@
94 $(call relative-dynlib-references,$1,$2,$4)
95 endif
96 else
97 # Build the ordinary .a library
98 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS)
99 $$(call removeFiles,$$@ $$@.contents)
100 ifeq "$$($1_$2_SplitObjs)" "YES"
101 $$(FIND) $$(patsubst %.$$($3_osuf),%_$$($3_osuf)_split,$$($1_$2_$3_HS_OBJS)) -name '*.$$($3_osuf)' -print >> $$@.contents
102 echo $$($1_$2_$3_NON_HS_OBJS) >> $$@.contents
103 else
104 echo $$($1_$2_$3_ALL_OBJS) >> $$@.contents
105 endif
106 ifeq "$$($1_$2_ArSupportsAtFile)" "YES"
107 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ @$$@.contents
108 else
109 "$$(XARGS)" $$(XARGS_OPTS) "$$($1_$2_AR)" $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ < $$@.contents
110 endif
111 $$(call removeFiles,$$@.contents)
112
113 ifeq "$$(HostOS_CPP)" "mingw32"
114 ifneq "$$($1_$2_$3_LIB0)" ""
115 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
116 $$($1_$2_$3_LIB0) :
117 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@
118 endif
119 endif
120
121 endif
122
123 $(call all-target,$1_$2,all_$1_$2_$3)
124 $(call all-target,$1_$2_$3,$$($1_$2_$3_LIB))
125
126 # Don't put bootstrapping packages in the bindist
127 ifneq "$4" "0"
128 BINDIST_HI += $$($1_$2_$3_HI)
129 BINDIST_LIBS += $$($1_$2_$3_LIB)
130 BINDIST_LIBS += $$($1_$2_$3_LIB0)
131 endif
132
133 # Build the GHCi library
134 ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
135 $1_$2_GHCI_LIB = $$($1_$2_dyn_LIB)
136 else
137 ifeq "$3" "v"
138 $1_$2_GHCI_LIB = $1/$2/build/HS$$($1_PACKAGE)-$$($1_$2_VERSION).$$($3_osuf)
139 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
140 # Don't put bootstrapping packages in the bindist
141 ifneq "$4" "0"
142 BINDIST_LIBS += $$($1_$2_GHCI_LIB)
143 endif
144 endif
145 $$($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)
146 $$(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)
147
148 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
149 # Don't bother making ghci libs for bootstrapping packages
150 ifneq "$4" "0"
151 $(call all-target,$1_$2,$$($1_$2_GHCI_LIB))
152 endif
153 endif
154 endif
155 endif
156
157 $(call profEnd, build-package-way($1,$2,$3))
158 endef
159
160 # $1 = dir
161 # $2 = distdir
162 # $3 = way
163 # $4 = extra flags
164 # $5 = object files to link
165 # $6 = output filename
166 define build-dll
167 $(call cmd,$1_$2_HC) $($1_$2_$3_ALL_HC_OPTS) $($1_$2_$3_GHC_LD_OPTS) $4 $5 \
168 -shared -dynamic -dynload deploy \
169 $(addprefix -l,$($1_$2_EXTRA_LIBRARIES)) \
170 -no-auto-link-packages \
171 -o $6
172 # Now check that the DLL doesn't have too many symbols. See trac #5987.
173 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"
174 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
175 endef
176