blob: 229ad8d4709011f9f6ce2dc38417f6e5b5d0266f [file] [log] [blame]
Miguel Young de la Sotad1640262022-03-31 14:34:33 -04001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
5"""Rules for declaring linker scripts and linker script fragments."""
6
7def _ld_library_impl(ctx):
8 files = [] + ctx.files.fragments
9 user_link_flags = []
10 if ctx.file.script:
11 files += ctx.files.script
12 user_link_flags += [
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040013 "-Wl,-T,{}".format(ctx.file.script.path),
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040014 ]
15
16 return [
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040017 cc_common.merge_cc_infos(
18 direct_cc_infos = [CcInfo(
19 linking_context = cc_common.create_linking_context(
20 linker_inputs = depset([cc_common.create_linker_input(
21 owner = ctx.label,
22 additional_inputs = depset(files),
23 user_link_flags = depset(user_link_flags),
24 )]),
25 ),
26 )],
27 cc_infos = [dep[CcInfo] for dep in ctx.attr.deps],
28 ),
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040029 ]
30
31ld_library = rule(
32 implementation = _ld_library_impl,
33 doc = """
34 A linker script library. Linker script libraries consist of a collection of
35 "fragments" (the linker equivalent of a header) and an optional script. Linker
36 script libraries can depend on other libraries to access the fragments they
37 public; cc_binaries can depend on an ld_library with a script to specify it
38 as the linker script for that binary.
39
40 At most one ld_library in a cc_binary's dependencies may have a script.
41 """,
42 attrs = {
43 "script": attr.label(allow_single_file = True),
44 "fragments": attr.label_list(
45 default = [],
46 allow_files = True,
47 ),
48 "deps": attr.label_list(
49 default = [],
50 providers = [CcInfo],
51 ),
52 },
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040053)