blob: 0e106b3ea25efde39ddda6e107b533cbf7b0c77a [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):
Alphan Ulusoya5fed722022-09-14 09:03:44 -04008 files = [] + ctx.files.includes
Miguel Young de la Sotad1640262022-03-31 14:34:33 -04009 user_link_flags = []
Alphan Ulusoy88f78ad2022-09-14 08:29:41 -040010
11 # Disable non-volatile scratch region and counters if building for english
12 # breakfast. This should appear before the linker script.
13 if "-DOT_IS_ENGLISH_BREAKFAST_REDUCED_SUPPORT_FOR_INTERNAL_USE_ONLY_" in ctx.fragments.cpp.copts:
14 user_link_flags += [
15 "-Wl,--defsym=no_ottf_nv_scratch=1",
16 "-Wl,--defsym=no_ottf_nv_counter=1",
17 ]
18
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040019 if ctx.file.script:
20 files += ctx.files.script
21 user_link_flags += [
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040022 "-Wl,-T,{}".format(ctx.file.script.path),
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040023 ]
24
25 return [
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040026 cc_common.merge_cc_infos(
27 direct_cc_infos = [CcInfo(
28 linking_context = cc_common.create_linking_context(
29 linker_inputs = depset([cc_common.create_linker_input(
30 owner = ctx.label,
31 additional_inputs = depset(files),
32 user_link_flags = depset(user_link_flags),
33 )]),
34 ),
35 )],
36 cc_infos = [dep[CcInfo] for dep in ctx.attr.deps],
37 ),
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040038 ]
39
40ld_library = rule(
41 implementation = _ld_library_impl,
Alphan Ulusoy88f78ad2022-09-14 08:29:41 -040042 fragments = ["cpp"],
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040043 doc = """
44 A linker script library. Linker script libraries consist of a collection of
Alphan Ulusoya5fed722022-09-14 09:03:44 -040045 "includes" (the linker equivalent of a header) and an optional script. Linker
46 script libraries can depend on other libraries to access the includes they
47 publish; cc_binaries can depend on an ld_library with a script to specify it
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040048 as the linker script for that binary.
49
50 At most one ld_library in a cc_binary's dependencies may have a script.
51 """,
52 attrs = {
53 "script": attr.label(allow_single_file = True),
Alphan Ulusoya5fed722022-09-14 09:03:44 -040054 "includes": attr.label_list(
Miguel Young de la Sotad1640262022-03-31 14:34:33 -040055 default = [],
56 allow_files = True,
57 ),
58 "deps": attr.label_list(
59 default = [],
60 providers = [CcInfo],
61 ),
62 },
Miguel Young de la Sota98cfa222022-04-06 15:31:06 -040063)