xref: /aosp_15_r20/external/pigweed/pw_build/bazel_internal/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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:pw_linker_script.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    # Only compatible with platforms that support linker scripts.
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    features = ["-pic"],
57    target_compatible_with = select({
58        "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"],
59        "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"],
60        "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"],
61        "@platforms//os:linux": [],
62        "//conditions:default": ["@platforms//:incompatible"],
63    }),
64    deps = [":linker_script_test"],
65)
66
67# Use cc_library to depend on the linker script, and then use cc_binary to build
68# the test, verifying that linker scripts can be included via transitive deps.
69cc_library(
70    name = "lib_linker_script",
71    deps = [":linker_script_test"],
72)
73
74cc_binary(
75    name = "test_transitive_linker_script",
76    srcs = ["test.cc"],
77    copts = ["-Wno-unused-variable"],
78    features = ["-pic"],
79    # Only compatible with platforms that support linker scripts.
80    target_compatible_with = select({
81        "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"],
82        "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"],
83        "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"],
84        "@platforms//os:linux": [],
85        "//conditions:default": ["@platforms//:incompatible"],
86    }),
87    deps = [":lib_linker_script"],
88)
89
90# Verify that the linker script can also be specified directly.
91cc_binary(
92    name = "test_direct_linker_script",
93    srcs = ["test.cc"],
94    additional_linker_inputs = [":linker_script_test"],
95    copts = ["-Wno-unused-variable"],
96    features = ["-pic"],
97    linkopts = ["-T $(location :linker_script_test)"],
98    # Only compatible with platforms that support linker scripts.
99    target_compatible_with = select({
100        "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"],
101        "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"],
102        "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"],
103        "@platforms//os:linux": [],
104        "//conditions:default": ["@platforms//:incompatible"],
105    }),
106)
107
108pw_elf_to_bin(
109    name = "test_bin",
110    bin_out = "test.bin",
111    elf_input = ":test_linker_script",
112)
113
114pw_elf_to_dump(
115    name = "test_dump",
116    dump_out = "test.dump",
117    elf_input = ":test_linker_script",
118)
119