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