| # Copyright lowRISC contributors. |
| # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| # SPDX-License-Identifier: Apache-2.0 |
| |
| """Rules for declaring linker scripts and linker script fragments.""" |
| |
| def _ld_library_impl(ctx): |
| files = [] + ctx.files.includes |
| user_link_flags = [] |
| |
| # Disable non-volatile scratch region and counters if building for english |
| # breakfast. This should appear before the linker script. |
| if "-DOT_IS_ENGLISH_BREAKFAST_REDUCED_SUPPORT_FOR_INTERNAL_USE_ONLY_" in ctx.fragments.cpp.copts: |
| user_link_flags += [ |
| "-Wl,--defsym=no_ottf_nv_scratch=1", |
| "-Wl,--defsym=no_ottf_nv_counter=1", |
| ] |
| |
| if ctx.file.script: |
| files += ctx.files.script |
| user_link_flags += [ |
| "-Wl,-T,{}".format(ctx.file.script.path), |
| ] |
| |
| return [ |
| cc_common.merge_cc_infos( |
| direct_cc_infos = [CcInfo( |
| linking_context = cc_common.create_linking_context( |
| linker_inputs = depset([cc_common.create_linker_input( |
| owner = ctx.label, |
| additional_inputs = depset(files), |
| user_link_flags = depset(user_link_flags), |
| )]), |
| ), |
| )], |
| cc_infos = [dep[CcInfo] for dep in ctx.attr.deps], |
| ), |
| ] |
| |
| ld_library = rule( |
| implementation = _ld_library_impl, |
| fragments = ["cpp"], |
| doc = """ |
| A linker script library. Linker script libraries consist of a collection of |
| "includes" (the linker equivalent of a header) and an optional script. Linker |
| script libraries can depend on other libraries to access the includes they |
| publish; cc_binaries can depend on an ld_library with a script to specify it |
| as the linker script for that binary. |
| |
| At most one ld_library in a cc_binary's dependencies may have a script. |
| """, |
| attrs = { |
| "script": attr.label(allow_single_file = True), |
| "includes": attr.label_list( |
| default = [], |
| allow_files = True, |
| ), |
| "deps": attr.label_list( |
| default = [], |
| providers = [CcInfo], |
| ), |
| }, |
| ) |