blob: 9e63dd7ed74b860206a5c40c64f6a9fd22cb528d [file] [log] [blame]
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
def zephyr_cc_library(*, name, skip_deps = [], **kwargs):
"""
A macro that creates a cc_library with Zephyr-specific dependencies and options.
Args:
name: The name of the library
skip_deps: Dependencies to skip that are usuaully added
**kwargs: Keyword arguments to pass to the cc_library rule.
"""
deps = kwargs.pop("deps", [])
kwargs["alwayslink"] = 1
for dep in [
"@zephyr//:autoconf_library",
"@zephyr//:dts_cc_library",
"@zephyr//:syscall_list",
"@zephyr//:zephyr",
"@zephyr//:soc",
]:
# TODO revisit this as per
# http://pwrev.dev/235635/comment/c36d1453_5d9b7b3b/
if dep in deps:
continue
should_skip_dep = False
for skip_dep in skip_deps:
if Label(dep) == Label(skip_dep):
should_skip_dep = True
break
if should_skip_dep:
continue
deps.append(dep)
additional_compiler_inputs = kwargs.pop("additional_compiler_inputs", []) + [
"@zephyr//:autoconf_file",
]
copts = kwargs.pop("copts", []) + [
"-include generated/zephyr/autoconf.h",
"-include $(execpath @zephyr//:autoconf_file)",
]
native.cc_library(
name = name,
deps = deps,
copts = copts,
additional_compiler_inputs = additional_compiler_inputs,
**kwargs
)
def zephyr_cc_binary(*, name, deps = [], **kwargs):
"""
A macro that creates a cc_binary with Zephyr-specific dependencies and options.
Args:
name: The name of the final binary
deps: The dependencies for the app
**kwargs: Keyword arguments to pass to the cc_binary rule.
"""
# TODO convert to symbolic macro (https://bazel.build/extending/macros)
name_pre0 = name + "._pre0.elf"
target_pre0 = ":" + name_pre0
# TODO revisit this, some of these might need to be a part of the toolchain
linkopts = kwargs.pop("linkopts", []) + [
"-nostdlib",
"-lgcc",
"-mfp16-format=ieee",
"-mtp=soft",
"-fuse-ld=bfd",
"-Wl,--build-id=none",
"-Wl,--sort-common=descending",
"-Wl,--sort-section=alignment",
"-Wl,-u,_OffsetAbsSyms",
"-Wl,-u,_ConfigAbsSyms",
"-static",
"-Wl,-X",
"-Wl,-N",
"-Wl,--orphan-handling=warn",
"-Wl,-no-pie",
"-DPICOLIBC_LONG_LONG_PRINTF_SCANF",
]
native.cc_binary(
name = name_pre0,
deps = deps + [
":linkerscripts/linker_zephyr_pre0.ld",
],
linkopts = linkopts + [
"-T $(execpath :linkerscripts/linker_zephyr_pre0.ld)",
],
**kwargs
)
# This should only run if CONFIG_GEN_ISR_TABLES=y
# TODO isr_table.c and the other outs should be scoped to the name
native.genrule(
name = name + "_isr_tables",
srcs = [target_pre0],
outs = ["isr_table.c", "isr_tables_vt.ld", "isr_tables_swi.ld"],
cmd = """
./$(execpath @zephyr//scripts/build:gen_isr_tables) \
--output-source $(location isr_table.c) \
--linker-output-files $(location isr_tables_vt.ld) $(location isr_tables_swi.ld) \
--kernel $(SRCS) \
--intlist-section .intList \
--intlist-section intList \
--sw-isr-table \
--vector-table
""",
tools = ["@zephyr//scripts/build:gen_isr_tables"],
)
zephyr_cc_library(
name = name + "._isr_table",
srcs = [":isr_table.c"],
deps = [":{}_isr_tables".format(name)],
)
native.cc_binary(
name = name,
deps = deps + [
":linkerscripts/linker.ld",
":{}._isr_table".format(name),
],
linkopts = linkopts + [
"-T $(execpath linkerscripts/linker.ld)",
"-Wl,--print-memory-usage",
],
**kwargs
)
# zephyr_final_binary(
# name = name,
# lds = ":linkerscripts/linker.ld",
# pre0_elf = target_pre0,
# )