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#      https://gitlab.haskell.org/ghc/ghc/wikis/building/architecture
9#      https://gitlab.haskell.org/ghc/ghc/wikis/building/modifying
10#
11# -----------------------------------------------------------------------------
12
13
14define 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,$4)
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)$(subst .,%,$$($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# Note [inconsistent distdirs]
31#
32# hack: the DEPS_LIBS mechanism assumes that the distdirs for packages
33# that depend on each other are the same, but that is not the case for
34# ghc where we use stage1/stage2 rather than dist/dist-install.
35# Really we should use a consistent scheme for distdirs, but in the
36# meantime we work around it by defining ghc-<ver>_dist-install_way_LIB:
37#
38# A similar hack is applied to the PROGRAM_DEP_LIB mechanism in
39# rules/build-package.mk.
40ifeq "$$($1_PACKAGE) $2" "ghc stage2"
41$$($1_$2_COMPONENT_ID)_dist-install_$3_LIB = $$($1_$2_$3_LIB)
42endif
43
44# All the .a/.so library file dependencies for this library.
45#
46# The $(subst stage2,dist-install,..) is needed due to Note
47# [inconsistent distdirs].
48#
49# NB: Use DEP_COMPONENT_IDS for the /directory/ parameter.
50$1_$2_$3_DEPS_LIBS=$$(foreach dep,$$($1_$2_DEP_COMPONENT_IDS),$$($$(dep)_$(subst stage2,dist-install,$2)_$3_LIB))
51
52$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)
53$1_$2_$3_ALL_OBJS = $$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS)
54
55ifeq "$3" "dyn"
56
57# Link a dynamic library
58# On windows we have to supply the extra libs this one links to when building it.
59ifeq "$$(TargetOS_CPP)" "mingw32"
60$$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
61	$$(call build-dll,$1,$2,$3,-L$1/$2/build,,$$($1_$2_$3_HS_OBJS) $$($1_$2_$3_NON_HS_OBJS),"$$@","NO","$$($1_PACKAGE)","$$($1_$2_VERSION)")
62
63else # ifneq "$$(TargetOS_CPP)" "mingw32"
64$$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS) $$(ALL_RTS_LIBS) $$($1_$2_$3_DEPS_LIBS)
65	$$(call cmd,$1_$2_HC) $$($1_$2_$3_ALL_HC_OPTS) $$($1_$2_$3_GHC_LD_OPTS) $$($1_$2_$3_ALL_OBJS) \
66         -shared -dynamic -dynload deploy \
67	 $$(addprefix -l,$$($1_$2_EXTRA_LIBRARIES)) $$(addprefix -L,$$($1_$2_EXTRA_LIBDIRS)) \
68         -no-auto-link-packages \
69         -o $$@
70endif # "$$(TargetOS_CPP)" "mingw32"
71
72else # ifneq "$3" "dyn"
73
74# Build the ordinary .a library
75$$($1_$2_$3_LIB) : $$($1_$2_$3_ALL_OBJS)
76	$$(call removeFiles,$$@ $$@.contents)
77	echo $$($1_$2_$3_ALL_OBJS) >> $$@.contents
78ifeq "$$($1_$2_ArSupportsAtFile)" "YES"
79	$$(call cmd,$1_$2_AR) $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ @$$@.contents
80else
81	"$$(XARGS)" $$(XARGS_OPTS) "$$($1_$2_AR)" $$($1_$2_AR_OPTS) $$($1_$2_EXTRA_AR_ARGS) $$@ < $$@.contents
82endif
83	$$(call removeFiles,$$@.contents)
84
85endif # "$3" "dyn"
86
87$(call all-target,$1_$2,all_$1_$2_$3)
88$(call all-target,$1_$2_$3,$$($1_$2_$3_LIB))
89
90# Don't put bootstrapping packages in the bindist
91ifneq "$4" "0"
92BINDIST_HI += $$($1_$2_$3_HI)
93BINDIST_LIBS += $$($1_$2_$3_LIB)
94# Need to put the split libs and import libraries here
95endif
96
97ifeq "$$($1_$2_SplitSections)" "YES"
98ifeq "$(LdIsGNULd)" "YES"
99ifeq "$$(HostOS_CPP)" "mingw32"
100$1_$2_LD_SCRIPT_CMD =
101$1_$2_LD_SCRIPT = driver/utils/merge_sections_pe.ld
102else
103$1_$2_LD_SCRIPT_CMD = -T
104$1_$2_LD_SCRIPT = driver/utils/merge_sections.ld
105endif
106endif
107endif
108
109# Build the GHCi library
110# See Note [Merging object files for GHCi] in GHC.Driver.Pipeline.
111ifneq "$(filter $3, v p)" ""
112$1_$2_$3_GHCI_LIB = $1/$2/build/HS$$($1_$2_COMPONENT_ID).$$($3_osuf)
113ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
114# Don't put bootstrapping packages in the bindist
115ifneq "$4" "0"
116BINDIST_LIBS += $$($1_$2_$3_GHCI_LIB)
117endif
118endif
119$$($1_$2_$3_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)
120	$$(call cmd,MERGE_OBJS_STAGE$4) $(MERGE_OBJS_STAGE$4_FLAGS) $$(if $$($1_$2_LD_SCRIPT),$$($1_$2_LD_SCRIPT_CMD) $$($1_$2_LD_SCRIPT)) -o $$@ $$(EXTRA_LD_LINKER_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)
121ifeq "$$($1_$2_BUILD_GHCI_LIB)" "YES"
122# Don't bother making ghci libs for bootstrapping packages
123ifneq "$4" "0"
124$(call all-target,$1_$2,$$($1_$2_$3_GHCI_LIB))
125endif
126endif # "$$($1_$2_BUILD_GHCI_LIB)" "YES"
127endif # "$(filter $3, v p)" ""
128
129$(call profEnd, build-package-way($1,$2,$3))
130endef # build-package-way
131
132define build-dll
133# Call out to the shell script to decide how to build the util dll.
134# 1  = dir
135# 2  = distdir
136# 3  = way
137# 4  = extra flags
138# 5  = extra libraries to link
139# 6  = object files to link
140# 7  = output filename
141# 8  = link command
142# 9  = create delay load import lib
143# 10 = SxS Name
144# 11 = SxS Version
145$(gen-dll_INPLACE) link "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$(call cmd,$1_$2_HC) $(subst -no-hs-main,,$($1_$2_$3_ALL_HC_OPTS) $($1_$2_$3_GHC_LD_OPTS)) \
146           -shared -dynamic -dynload deploy \
147           $(addprefix -l,$($1_$2_EXTRA_LIBRARIES)) \
148           -no-auto-link-packages" "$8" \
149           "$9" "${10}"
150endef
151
152