Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 1 | # 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 | |
| 7 | def _ld_library_impl(ctx): |
Alphan Ulusoy | a5fed72 | 2022-09-14 09:03:44 -0400 | [diff] [blame] | 8 | files = [] + ctx.files.includes |
Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 9 | user_link_flags = [] |
Alphan Ulusoy | 88f78ad | 2022-09-14 08:29:41 -0400 | [diff] [blame] | 10 | |
| 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 Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 19 | if ctx.file.script: |
| 20 | files += ctx.files.script |
| 21 | user_link_flags += [ |
Miguel Young de la Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 22 | "-Wl,-T,{}".format(ctx.file.script.path), |
Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 23 | ] |
| 24 | |
| 25 | return [ |
Miguel Young de la Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 26 | 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 Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 38 | ] |
| 39 | |
| 40 | ld_library = rule( |
| 41 | implementation = _ld_library_impl, |
Alphan Ulusoy | 88f78ad | 2022-09-14 08:29:41 -0400 | [diff] [blame] | 42 | fragments = ["cpp"], |
Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 43 | doc = """ |
| 44 | A linker script library. Linker script libraries consist of a collection of |
Alphan Ulusoy | a5fed72 | 2022-09-14 09:03:44 -0400 | [diff] [blame] | 45 | "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 Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 48 | 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 Ulusoy | a5fed72 | 2022-09-14 09:03:44 -0400 | [diff] [blame] | 54 | "includes": attr.label_list( |
Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 55 | default = [], |
| 56 | allow_files = True, |
| 57 | ), |
| 58 | "deps": attr.label_list( |
| 59 | default = [], |
| 60 | providers = [CcInfo], |
| 61 | ), |
| 62 | }, |
Miguel Young de la Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 63 | ) |