-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathlink.bzl
373 lines (329 loc) · 12.4 KB
/
link.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""Actions for linking object code produced by compilation"""
load(":private/packages.bzl", "expose_packages", "pkg_info_to_compile_flags")
load(":private/pkg_id.bzl", "pkg_id")
load(":private/cc_libraries.bzl", "create_link_config")
def merge_parameter_files(hs, file1, file2):
"""Merge two GHC parameter files into one.
Args:
hs: Haskell context.
file1: The first parameter file.
file2: The second parameter file.
Returns:
File: A new parameter file containing the parameters of both input files.
The file name is based on the file names of the input files. The file
is located next to the first input file.
"""
params_file = hs.actions.declare_file(
file1.basename + ".and." + file2.basename,
sibling = file1,
)
hs.actions.run_shell(
inputs = [file1, file2],
outputs = [params_file],
command = """
cat {file1} {file2} > {out}
""".format(
file1 = file1.path,
file2 = file2.path,
out = params_file.path,
),
)
return params_file
def _create_objects_dir_manifest(hs, posix, objects_dir, dynamic, with_profiling):
suffix = ".dynamic.manifest" if dynamic else ".static.manifest"
objects_dir_manifest = hs.actions.declare_file(
objects_dir.basename + suffix,
sibling = objects_dir,
)
if with_profiling:
ext = "p_o"
elif dynamic:
ext = "dyn_o"
else:
ext = "o"
hs.actions.run_shell(
inputs = [objects_dir],
outputs = [objects_dir_manifest],
# Note: The output of `find` is not stable. The order of the
# lines in the output depend on the filesystem. By using
# `sort`, we force the output to be stable. This is mandatory
# for efficient caching. See
# https://github.com/tweag/rules_haskell/issues/1126.
command = """
"{find}" {dir} -name '*.{ext}' | "{sort}" > {out}
""".format(
find = posix.commands["find"],
sort = posix.commands["sort"],
dir = objects_dir.path,
ext = ext,
out = objects_dir_manifest.path,
),
)
return objects_dir_manifest
def link_binary(
hs,
cc,
posix,
dep_info,
extra_srcs,
compiler_flags,
objects_dir,
dynamic,
with_profiling,
version):
"""Link Haskell binary from static object files.
Returns:
File: produced executable
"""
exe_name = hs.name + (".exe" if hs.toolchain.is_windows else "")
executable = hs.actions.declare_file(exe_name)
args = hs.actions.args()
args.add_all(["-optl" + f for f in cc.linker_flags])
if with_profiling:
args.add("-prof")
args.add_all(hs.toolchain.compiler_flags)
args.add_all(compiler_flags)
# By default, GHC will produce mostly-static binaries, i.e. in which all
# Haskell code is statically linked and foreign libraries and system
# dependencies are dynamically linked. If linkstatic is false, i.e. the user
# has requested fully dynamic linking, we must therefore add flags to make
# sure that GHC dynamically links Haskell code too. The one exception to
# this is when we are compiling for profiling, which currently does not play
# nicely with dynamic linking.
if dynamic:
if with_profiling:
print("WARNING: dynamic linking and profiling don't mix. Omitting -dynamic.\nSee https://ghc.haskell.org/trac/ghc/ticket/15394")
else:
args.add_all(["-pie", "-dynamic"])
elif not hs.toolchain.is_darwin and not hs.toolchain.is_windows:
# See Note [No PIE when linking]
args.add("-optl-no-pie")
# When compiling with `-threaded`, GHC needs to link against
# the pthread library when linking against static archives (.a).
# We assume it’s not a problem to pass it for other cases,
# so we just default to passing it.
args.add("-optl-pthread")
if hs.features.fully_static_link:
# Create a fully statically linked binary.
args.add("-optl-static")
args.add_all(["-o", executable.path])
(pkg_info_inputs, pkg_info_args) = pkg_info_to_compile_flags(
hs,
pkg_info = expose_packages(
package_ids = hs.package_ids,
package_databases = dep_info.package_databases,
version = version,
),
prefix = "link-",
)
args.add_all(pkg_info_args)
(cache_file, static_libs, dynamic_libs) = create_link_config(
hs = hs,
posix = posix,
cc_libraries_info = cc.cc_libraries_info,
libraries_to_link = cc.transitive_libraries,
dynamic = dynamic,
binary = executable,
args = args,
)
# XXX: Suppress a warning that Clang prints due to GHC automatically passing
# "-pie" or "-no-pie" to the C compiler.
# This is linked to https://ghc.haskell.org/trac/ghc/ticket/15319
args.add_all([
"-optc-Wno-unused-command-line-argument",
"-optl-Wno-unused-command-line-argument",
])
objects_dir_manifest = _create_objects_dir_manifest(
hs,
posix,
objects_dir,
dynamic = dynamic,
with_profiling = with_profiling,
)
if hs.toolchain.is_darwin:
args.add("-optl-Wl,-headerpad_max_install_names")
# Nixpkgs commit 3513034208a introduces -liconv in NIX_LDFLAGS on
# Darwin. We don't currently handle NIX_LDFLAGS in any special
# way, so a hack is to simply do what NIX_LDFLAGS is telling us we
# should do always when using a toolchain from Nixpkgs.
# TODO remove this gross hack.
args.add("-liconv")
hs.toolchain.actions.run_ghc(
hs,
cc,
inputs = depset(transitive = [
depset(extra_srcs),
dep_info.package_databases,
dep_info.hs_libraries,
depset([cache_file, objects_dir]),
pkg_info_inputs,
depset(static_libs + dynamic_libs),
]),
outputs = [executable],
mnemonic = "HaskellLinkBinary",
arguments = args,
params_file = objects_dir_manifest,
)
return (executable, dynamic_libs)
# Note [No PIE while linking]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# GHC links object files with the `ld` flag `-r` and compiles the `main`
# function with non-position independent code when generating an executable.
# On some more recent Linux distributions (e.g. Ubuntu 18.04) `gcc` defaults
# to linking with `-pie` which is incompatible with both behaviors described
# above.
#
# Combining `-pie` and `ld -r` causes errors of the form:
#
# /usr/bin/ld: -r and -pie may not be used together
#
# Compiling executables with `-pie` leads to errors of the form:
#
# /usr/bin/ld.gold: error: /tmp/ghc3_0/ghc_2.o: requires dynamic R_X86_64_32 reloc against 'ZCMain_main_closure' which may overflow at runtime; recompile with -fPIC
# /usr/bin/ld.gold: error: ../../../../../external/rules_haskell_ghc_linux_amd64/lib/base-4.13.0.0/libHSbase-4.13.0.0.a(Base.o): requires unsupported dynamic reloc 11; recompile with -fPIC
#
# GHC determines whether the C compiler supports the `-no-pie` flag during
# `./configure` and stores this information in its `settings` file. Depending
# on this setting GHC would then automatically set `-no-pie`. However,
# rules_haskell uses GHC's `-pgmc` flag to point GHC to the CC toolchain's C
# compiler. This disables the flags configured in the `setting` file. Instead,
# we have to pass `-no-pie` explicitly when linking binaries.
#
# In GHC < 8.10, we must always pass -no-pie. In newer compilers, we
# must take care to only pass -no-pie when compiling libraries.
#
# Ideally, we would determine whether the CC toolchain's C compiler supports
# `-no-pie` before setting it. Unfortunately, this is complicated by the fact
# that Bazel does not support dynamic dependencies in build actions and that
# repository rules don't have access to toolchains, yet.
#
# If necessary this flag could be made user configurable at the level of the
# GHC toolchain or a wrapper around the GHC compiler could determine if
# the C compiler supports `-no-pie`.
#
# Further information:
#
# - GHC Note [No PIE while linking] https://gitlab.haskell.org/ghc/ghc/-/blob/d0bab2e3419e49cdbb1201d4650572b57f33420c/compiler/main/DynFlags.hs#L5551-5557
# - https://gitlab.haskell.org/ghc/ghc/-/issues/12759
# - https://gitlab.haskell.org/ghc/ghc/-/issues/15319
def _so_extension(hs):
"""Returns the extension for shared libraries.
Args:
hs: Haskell rule context.
Returns:
string of extension.
"""
return "dylib" if hs.toolchain.is_darwin else "so"
def link_library_static(hs, cc, posix, dep_info, objects_dir, my_pkg_id, with_profiling):
"""Link a static library for the package using given object files.
Returns:
File: Produced static library.
"""
static_library = hs.actions.declare_file(
"lib{0}.a".format(pkg_id.library_name(hs, my_pkg_id, prof_suffix = with_profiling)),
)
objects_dir_manifest = _create_objects_dir_manifest(
hs,
posix,
objects_dir,
dynamic = False,
with_profiling = with_profiling,
)
args = hs.actions.args()
inputs = [objects_dir, objects_dir_manifest] + cc.files
if hs.toolchain.is_darwin:
# On Darwin, ar doesn't support params files.
args.add_all([
static_library,
objects_dir_manifest.path,
])
# TODO Get ar location from the CC toolchain. This is
# complicated by the fact that the CC toolchain does not
# always use ar, and libtool has an entirely different CLI.
# See https://github.com/bazelbuild/bazel/issues/5127
hs.actions.run_shell(
inputs = inputs,
outputs = [static_library],
mnemonic = "HaskellLinkStaticLibrary",
command = "{ar} qc $1 $(< $2)".format(ar = cc.tools.ar),
arguments = [args],
# Use the default macosx toolchain
env = {"SDKROOT": "macosx"},
)
else:
args.add_all([
"qc",
static_library,
"@" + objects_dir_manifest.path,
])
hs.actions.run(
inputs = inputs,
outputs = [static_library],
mnemonic = "HaskellLinkStaticLibrary",
executable = cc.tools.ar,
arguments = [args],
)
return static_library
def link_library_dynamic(hs, cc, posix, dep_info, extra_srcs, objects_dir, my_pkg_id, compiler_flags):
"""Link a dynamic library for the package using given object files.
Returns:
File: Produced dynamic library.
"""
dynamic_library = hs.actions.declare_file(
"lib{0}-ghc{1}.{2}".format(
pkg_id.library_name(hs, my_pkg_id),
hs.toolchain.version,
_so_extension(hs),
),
)
args = hs.actions.args()
args.add_all(["-optl" + f for f in cc.linker_flags])
args.add_all(["-shared", "-dynamic"])
args.add_all(hs.toolchain.compiler_flags)
args.add_all(compiler_flags)
(pkg_info_inputs, pkg_info_args) = pkg_info_to_compile_flags(
hs,
pkg_info = expose_packages(
package_ids = hs.package_ids,
package_databases = dep_info.package_databases,
version = my_pkg_id.version if my_pkg_id else None,
),
prefix = "link-",
)
args.add_all(pkg_info_args)
(cache_file, static_libs, dynamic_libs) = create_link_config(
hs = hs,
posix = posix,
cc_libraries_info = cc.cc_libraries_info,
libraries_to_link = cc.transitive_libraries,
dynamic = True,
pic = True,
binary = dynamic_library,
args = args,
)
args.add_all(["-o", dynamic_library.path])
# Profiling not supported for dynamic libraries.
objects_dir_manifest = _create_objects_dir_manifest(
hs,
posix,
objects_dir,
dynamic = True,
with_profiling = False,
)
hs.toolchain.actions.run_ghc(
hs,
cc,
inputs = depset([cache_file, objects_dir], transitive = [
extra_srcs,
dep_info.package_databases,
dep_info.hs_libraries,
pkg_info_inputs,
depset(static_libs + dynamic_libs),
]),
outputs = [dynamic_library],
mnemonic = "HaskellLinkDynamicLibrary",
arguments = args,
params_file = objects_dir_manifest,
)
return dynamic_library