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): |
| 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 Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 13 | "-Wl,-T,{}".format(ctx.file.script.path), |
Miguel Young de la Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 14 | ] |
| 15 | |
| 16 | return [ |
Miguel Young de la Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 17 | 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 Sota | d164026 | 2022-03-31 14:34:33 -0400 | [diff] [blame] | 29 | ] |
| 30 | |
| 31 | ld_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 Sota | 98cfa22 | 2022-04-06 15:31:06 -0400 | [diff] [blame] | 53 | ) |