Merge branch 'master' of darcs.haskell.org:/srv/darcs//ghc
[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 # Link a dynamic library
60 # On windows we have to supply the extra libs this one links to when building it.
61 ifeq "$$(HostOS_CPP)" "mingw32"
62 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
63 ifneq "$$($1_$2_$3_LIB0)" ""
64 $$(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),$$@)
65 else
66 $$(call build-dll,$1,$2,$3,,$$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$@)
67 endif
68
69 ifneq "$$($1_$2_$3_LIB0)" ""
70 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
71 $$($1_$2_$3_LIB0) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
72 $$(call build-dll,$1,$2,$3,,$$($1_$2_dll0_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),$$($1_$2_$3_LIB0))
73 endif
74
75 else
76
77 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
78 $$(call cmd,$1_$2_HC) $$($1_$2_$3_ALL_HC_OPTS) $$($1_$2_$3_GHC_LD_OPTS) $$($1_$2_$3_ALL_OBJS) \
79 -shared -dynamic -dynload deploy \
80 $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
81 -no-auto-link-packages \
82 -o $$@
83 endif
84 else
85 # Build the ordinary .a library
86 $$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS)
87 $$(call removeFiles,$$@ $$@.contents)
88 ifeq "$$($1_$2_SplitObjs)" "YES"
89 $$(FIND) $$(patsubst %.$$($3_osuf),%_$$($3_osuf)_split,$$($1_$2_$3_HS_OBJS)) -name '*.$$($3_osuf)' -print >> $$@.contents
90 echo $$($1_$2_$3_NON_HS_OBJS) >> $$@.contents
91 else
92 echo $$($1_$2_$3_ALL_OBJS) >> $$@.contents
93 endif
94 ifeq "$$($1_$2_ArSupportsAtFile)" "YES"
95 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ @$$@.contents
96 else
97 "$$(XARGS)" $$(XARGS_OPTS) "$$($1_$2_AR)" $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ < $$@.contents
98 endif
99 $$(call removeFiles,$$@.contents)
100
101 ifeq "$$(HostOS_CPP)" "mingw32"
102 ifneq "$$($1_$2_$3_LIB0)" ""
103 $$($1_$2_$3_LIB) : $$($1_$2_$3_LIB0)
104 $$($1_$2_$3_LIB0) :
105 $$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@
106 endif
107 endif
108
109 endif
110
111 $(call all-target,$1_$2,all_$1_$2_$3)
112 $(call all-target,$1_$2_$3,$$($1_$2_$3_LIB))
113
114 # Don't put bootstrapping packages in the bindist
115 ifneq "$4" "0"
116 BINDIST_HI += $$($1_$2_$3_HI)
117 BINDIST_LIBS += $$($1_$2_$3_LIB)
118 BINDIST_LIBS += $$($1_$2_$3_LIB0)
119 endif
120
121 # Build the GHCi library
122 ifeq "$$(DYNAMIC_GHC_PROGRAMS)" "YES"
123 $1_$2_GHCI_LIB = $$($1_$2_dyn_LIB)
124 else
125 ifeq "$3" "v"
126 $1_$2_GHCI_LIB = $1/$2/build/HS$$($1_PACKAGE)-$$($1_$2_VERSION).$$($3_osuf)
127 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
128 # Don't put bootstrapping packages in the bindist
129 ifneq "$4" "0"
130 BINDIST_LIBS += $$($1_$2_GHCI_LIB)
131 endif
132 endif
133 $$($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)
134 $$(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)
135
136 ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
137 # Don't bother making ghci libs for bootstrapping packages
138 ifneq "$4" "0"
139 $(call all-target,$1_$2,$$($1_$2_GHCI_LIB))
140 endif
141 endif
142 endif
143 endif
144
145 $(call profEnd, build-package-way($1,$2,$3))
146 endef
147
148 # $1 = dir
149 # $2 = distdir
150 # $3 = way
151 # $4 = extra flags
152 # $5 = object files to link
153 # $6 = output filename
154 define build-dll
155 $(call cmd,$1_$2_HC) $($1_$2_$3_ALL_HC_OPTS) $($1_$2_$3_GHC_LD_OPTS) $4 $5 \
156 -shared -dynamic -dynload deploy \
157 $(addprefix -l,$($1_$2_EXTRA_LIBRARIES)) \
158 -no-auto-link-packages \
159 -o $6
160 # Now check that the DLL doesn't have too many symbols. See trac #5987.
161 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"
162 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
163 endef
164