11"""Macro for creating binary and hex files using objcopy."""
22
3+ load ("@rules_cc//cc:cc_binary.bzl" , "cc_binary" )
4+
35def binary_out (name , src , visibility = None , ** kwargs ):
46 """
57 Runs objcopy to convert a source file (e.g., an ELF) into a raw binary.
@@ -40,4 +42,63 @@ def hex_out(name, src, visibility = None, **kwargs):
4042 tools = ["@arm_none_eabi//:objcopy" ],
4143 visibility = visibility ,
4244 ** kwargs
43- )
45+ )
46+
47+ MCU_FLAGS = [
48+ "-mcpu=cortex-m4" ,
49+ "-mthumb" ,
50+ "-mfpu=fpv4-sp-d16" ,
51+ "-mfloat-abi=hard" ,
52+ ]
53+
54+ def firmware_project (name , linker_script , startup_script , defines = [], extra_srcs = [], ** kwargs ):
55+ cc_binary (
56+ name = name ,
57+ srcs = native .glob ([
58+ "Core/Src/**/*.c" ,
59+ "Core/Inc/**/*.h" ,
60+ ], allow_empty = True )
61+ + [
62+ # Include the HAL for compilation
63+ "//drivers/stm32g4:hal_srcs" ,
64+ ] + extra_srcs ,
65+
66+ includes = [
67+ "Core/Inc" ,
68+ ],
69+
70+ deps = [
71+ "//drivers/stm32g4:stm32_headers" ,
72+ ],
73+
74+ # Linker options, also includes the reset handler startup file
75+ linkopts = MCU_FLAGS + [
76+ "-Wl,-Map=output.map" ,
77+ "-Wl,--gc-sections" ,
78+ "-Wl,--no-warn-rwx-segments" ,
79+ "-T $(location " + linker_script + ")" ,
80+ "$(location " + startup_script + ")" ,
81+ ],
82+
83+ defines = defines ,
84+
85+ additional_linker_inputs = [
86+ linker_script ,
87+ startup_script ,
88+ ],
89+
90+ target_compatible_with = [
91+ "@platforms//cpu:arm" ,
92+ "@platforms//os:none" ,
93+ ],
94+
95+ copts = MCU_FLAGS + [
96+ "-mthumb-interwork" ,
97+ "-ffunction-sections" ,
98+ "-fdata-sections" ,
99+ ],
100+
101+ visibility = ["//visibility:public" ],
102+
103+ ** kwargs
104+ )
0 commit comments