1# Copyright 2022 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15load("//pw_build:binary_tools.bzl", "pw_elf_to_bin", "pw_elf_to_dump") 16load("//pw_build:pigweed.bzl", "pw_linker_script") 17 18cc_library( 19 name = "header_test", 20 hdrs = ["header_test.h"], 21 includes = ["."], 22) 23 24pw_linker_script( 25 name = "linker_script_test", 26 defines = [ 27 "PW_BOOT_FLASH_BEGIN=0x08000200", 28 "PW_BOOT_FLASH_SIZE=1024K", 29 "PW_BOOT_HEAP_SIZE=112K", 30 "PW_BOOT_MIN_STACK_SIZE=1K", 31 "PW_BOOT_RAM_BEGIN=0x20000000", 32 "PW_BOOT_RAM_SIZE=192K", 33 "PW_BOOT_VECTOR_TABLE_BEGIN=0x08000000", 34 "PW_BOOT_VECTOR_TABLE_SIZE=1M", 35 ], 36 linker_script = "linker_script.ld", 37 deps = [ 38 ":header_test", 39 "//pw_build:must_place", 40 ], 41) 42 43# Use cc_binary to build the test to avoid duplicating the linker script in the 44# command line via implicit deps in pw_cc_binary. 45cc_binary( 46 name = "test_linker_script", 47 srcs = ["test.cc"], 48 copts = ["-Wno-unused-variable"], 49 target_compatible_with = select({ 50 # This test and its siblings will not link with asan: 51 # ld.lld: error: section '.text' will not fit in region 'VECTOR_TABLE': overflowed by 319296 bytes 52 # ld.lld: error: section '.init' will not fit in region 'VECTOR_TABLE': overflowed by 319319 bytes 53 # ld.lld: error: section '.fini' will not fit in region 'VECTOR_TABLE': overflowed by 319329 bytes 54 # ld.lld: error: section '.plt' will not fit in region 'VECTOR_TABLE': overflowed by 320096 bytes 55 # ld.lld: error: section '.bss' will not fit in region 'RAM': overflowed by 10135216 bytes 56 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 57 # Only compatible with platforms that support linker scripts. 58 "@platforms//os:macos": ["@platforms//:incompatible"], 59 "@platforms//os:windows": ["@platforms//:incompatible"], 60 "//conditions:default": [], 61 }), 62 deps = [":linker_script_test"], 63) 64 65# Use cc_library to depend on the linker script, and then use cc_binary to build 66# the test, verifying that linker scripts can be included via transitive deps. 67cc_library( 68 name = "lib_linker_script", 69 deps = [":linker_script_test"], 70) 71 72cc_binary( 73 name = "test_transitive_linker_script", 74 srcs = ["test.cc"], 75 copts = ["-Wno-unused-variable"], 76 # Only compatible with platforms that support linker scripts. 77 target_compatible_with = select({ 78 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 79 "@platforms//os:macos": ["@platforms//:incompatible"], 80 "@platforms//os:windows": ["@platforms//:incompatible"], 81 "//conditions:default": [], 82 }), 83 deps = [":lib_linker_script"], 84) 85 86# Verify that the linker script can also be specified directly. 87cc_binary( 88 name = "test_direct_linker_script", 89 srcs = ["test.cc"], 90 additional_linker_inputs = [":linker_script_test"], 91 copts = ["-Wno-unused-variable"], 92 linkopts = ["-T $(location :linker_script_test)"], 93 # Only compatible with platforms that support linker scripts. 94 target_compatible_with = select({ 95 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 96 "@platforms//os:macos": ["@platforms//:incompatible"], 97 "@platforms//os:windows": ["@platforms//:incompatible"], 98 "//conditions:default": [], 99 }), 100) 101 102pw_elf_to_bin( 103 name = "test_bin", 104 bin_out = "test.bin", 105 elf_input = ":test_linker_script", 106) 107 108pw_elf_to_dump( 109 name = "test_dump", 110 dump_out = "test.dump", 111 elf_input = ":test_linker_script", 112) 113