code_relocation: applied ruff code formatting to gen_relocate_app

Ruff is the Zephyr projects supported Python formatting tool. This patch
applies auto-formatting gen_relocate_app.py in preparation for coming
tidy-ups and improvements.

With the Ruff auto-formatter applied, error E501 can be removed from
.ruff-excludes.toml exclusion rules.

gen_relocate_app.py has also been removed from the format exclude list.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
This commit is contained in:
Joel Holdsworth 2025-05-21 09:36:57 -07:00 committed by Benjamin Cabé
commit 66e35624af
2 changed files with 117 additions and 66 deletions

View file

@ -235,7 +235,6 @@
"./scripts/build/gen_relocate_app.py" = [ "./scripts/build/gen_relocate_app.py" = [
"B028", # https://docs.astral.sh/ruff/rules/no-explicit-stacklevel "B028", # https://docs.astral.sh/ruff/rules/no-explicit-stacklevel
"E101", # https://docs.astral.sh/ruff/rules/mixed-spaces-and-tabs "E101", # https://docs.astral.sh/ruff/rules/mixed-spaces-and-tabs
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if "SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
"SIM401", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get "SIM401", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get
@ -1476,7 +1475,6 @@ exclude = [
"./scripts/build/gen_kobject_list.py", "./scripts/build/gen_kobject_list.py",
"./scripts/build/gen_kobject_placeholders.py", "./scripts/build/gen_kobject_placeholders.py",
"./scripts/build/gen_offset_header.py", "./scripts/build/gen_offset_header.py",
"./scripts/build/gen_relocate_app.py",
"./scripts/build/gen_strerror_table.py", "./scripts/build/gen_strerror_table.py",
"./scripts/build/gen_strsignal_table.py", "./scripts/build/gen_strsignal_table.py",
"./scripts/build/gen_symtab.py", "./scripts/build/gen_symtab.py",

View file

@ -41,7 +41,6 @@ Multiple regions can be appended together like SRAM2_DATA_BSS
this will place data and bss inside SRAM2. this will place data and bss inside SRAM2.
""" """
import sys import sys
import argparse import argparse
import glob import glob
@ -251,9 +250,7 @@ def find_sections(filename: str, symbol_filter: str) -> 'dict[SectionKind, list[
if section_kind is None: if section_kind is None:
continue continue
out[section_kind].append( out[section_kind].append(OutputSection(obj_file_path.name, section.name))
OutputSection(obj_file_path.name, section.name)
)
# Common variables will be placed in the .bss section # Common variables will be placed in the .bss section
# only after linking in the final executable. This "if" finds # only after linking in the final executable. This "if" finds
@ -261,19 +258,22 @@ def find_sections(filename: str, symbol_filter: str) -> 'dict[SectionKind, list[
# The solution to which is simply assigning a 0 to # The solution to which is simply assigning a 0 to
# bss variable and it will go to the required place. # bss variable and it will go to the required place.
if isinstance(section, SymbolTableSection): if isinstance(section, SymbolTableSection):
def is_common_symbol(s): def is_common_symbol(s):
return s.entry["st_shndx"] == "SHN_COMMON" return s.entry["st_shndx"] == "SHN_COMMON"
for symbol in filter(is_common_symbol, section.iter_symbols()): for symbol in filter(is_common_symbol, section.iter_symbols()):
warnings.warn("Common variable found. Move "+ warnings.warn(
symbol.name + " to bss by assigning it to 0/NULL") "Common variable found. Move "
+ symbol.name
+ " to bss by assigning it to 0/NULL"
)
return out return out
def assign_to_correct_mem_region( def assign_to_correct_mem_region(
memory_region: str, memory_region: str, full_list_of_sections: 'dict[SectionKind, list[OutputSection]]'
full_list_of_sections: 'dict[SectionKind, list[OutputSection]]'
) -> 'dict[MemoryRegion, dict[SectionKind, list[OutputSection]]]': ) -> 'dict[MemoryRegion, dict[SectionKind, list[OutputSection]]]':
""" """
Generate a mapping of memory region to collection of output sections to be Generate a mapping of memory region to collection of output sections to be
@ -292,8 +292,7 @@ def assign_to_correct_mem_region(
for used_kind in use_section_kinds: for used_kind in use_section_kinds:
# Pass through section kinds that go into this memory region # Pass through section kinds that go into this memory region
output_sections[used_kind] = [ output_sections[used_kind] = [
section._replace(keep=keep_sections) section._replace(keep=keep_sections) for section in full_list_of_sections[used_kind]
for section in full_list_of_sections[used_kind]
] ]
return {MemoryRegion(memory_region): output_sections} return {MemoryRegion(memory_region): output_sections}
@ -328,10 +327,12 @@ def print_linker_sections(list_sections: 'list[OutputSection]'):
out = '' out = ''
for section in sorted(list_sections): for section in sorted(list_sections):
template = PRINT_TEMPLATE if section.keep else PRINT_TEMPLATE_NOKEEP template = PRINT_TEMPLATE if section.keep else PRINT_TEMPLATE_NOKEEP
out += template.format(obj_file_name=section.obj_file_name, out += template.format(
section_name=section.section_name) obj_file_name=section.obj_file_name, section_name=section.section_name
)
return out return out
def add_phdr(memory_type, phdrs): def add_phdr(memory_type, phdrs):
return f'{memory_type} {phdrs[memory_type] if memory_type in phdrs else ""}' return f'{memory_type} {phdrs[memory_type] if memory_type in phdrs else ""}'
@ -342,14 +343,16 @@ def string_create_helper(
full_list_of_sections: 'dict[SectionKind, list[OutputSection]]', full_list_of_sections: 'dict[SectionKind, list[OutputSection]]',
load_address_in_flash, load_address_in_flash,
is_copy, is_copy,
phdrs phdrs,
): ):
linker_string = '' linker_string = ''
if load_address_in_flash: if load_address_in_flash:
if is_copy: if is_copy:
load_address_string = LOAD_ADDRESS_LOCATION_FLASH.format(add_phdr(memory_type, phdrs)) load_address_string = LOAD_ADDRESS_LOCATION_FLASH.format(add_phdr(memory_type, phdrs))
else: else:
load_address_string = LOAD_ADDRESS_LOCATION_FLASH_NOCOPY.format(add_phdr(memory_type, phdrs)) load_address_string = LOAD_ADDRESS_LOCATION_FLASH_NOCOPY.format(
add_phdr(memory_type, phdrs)
)
else: else:
load_address_string = LOAD_ADDRESS_LOCATION_BSS.format(add_phdr(memory_type, phdrs)) load_address_string = LOAD_ADDRESS_LOCATION_BSS.format(add_phdr(memory_type, phdrs))
if full_list_of_sections[kind]: if full_list_of_sections[kind]:
@ -364,50 +367,87 @@ def string_create_helper(
if memory_type in mpu_align: if memory_type in mpu_align:
align_size = mpu_align[memory_type] align_size = mpu_align[memory_type]
linker_string += LINKER_SECTION_SEQ_MPU.format(memory_type.lower(), kind.value, memory_type.upper(), linker_string += LINKER_SECTION_SEQ_MPU.format(
kind, tmp, load_address_string, align_size) memory_type.lower(),
kind.value,
memory_type.upper(),
kind,
tmp,
load_address_string,
align_size,
)
else: else:
if region_is_default_ram(memory_type) and kind in (SectionKind.TEXT, SectionKind.LITERAL): if region_is_default_ram(memory_type) and kind in (
SectionKind.TEXT,
SectionKind.LITERAL,
):
align_size = 0 align_size = 0
linker_string += LINKER_SECTION_SEQ_MPU.format(memory_type.lower(), kind.value, memory_type.upper(), linker_string += LINKER_SECTION_SEQ_MPU.format(
kind, tmp, load_address_string, align_size) memory_type.lower(),
kind.value,
memory_type.upper(),
kind,
tmp,
load_address_string,
align_size,
)
else: else:
linker_string += LINKER_SECTION_SEQ.format(memory_type.lower(), kind.value, memory_type.upper(), linker_string += LINKER_SECTION_SEQ.format(
kind, tmp, load_address_string) memory_type.lower(),
kind.value,
memory_type.upper(),
kind,
tmp,
load_address_string,
)
if load_address_in_flash: if load_address_in_flash:
linker_string += SECTION_LOAD_MEMORY_SEQ.format(memory_type.lower(), kind.value, memory_type.upper(), linker_string += SECTION_LOAD_MEMORY_SEQ.format(
kind) memory_type.lower(), kind.value, memory_type.upper(), kind
)
return linker_string return linker_string
def generate_linker_script(linker_file, sram_data_linker_file, sram_bss_linker_file, def generate_linker_script(
complete_list_of_sections, phdrs): linker_file, sram_data_linker_file, sram_bss_linker_file, complete_list_of_sections, phdrs
):
gen_string = '' gen_string = ''
gen_string_sram_data = '' gen_string_sram_data = ''
gen_string_sram_bss = '' gen_string_sram_bss = ''
for memory_type, full_list_of_sections in \ for memory_type, full_list_of_sections in sorted(complete_list_of_sections.items()):
sorted(complete_list_of_sections.items()):
is_copy = bool("|COPY" in memory_type) is_copy = bool("|COPY" in memory_type)
memory_type = memory_type.split("|", 1)[0] memory_type = memory_type.split("|", 1)[0]
if region_is_default_ram(memory_type) and is_copy: if region_is_default_ram(memory_type) and is_copy:
gen_string += MPU_RO_REGION_START.format(memory_type.lower(), memory_type.upper()) gen_string += MPU_RO_REGION_START.format(memory_type.lower(), memory_type.upper())
gen_string += string_create_helper(SectionKind.LITERAL, memory_type, full_list_of_sections, 1, is_copy, phdrs) gen_string += string_create_helper(
gen_string += string_create_helper(SectionKind.TEXT, memory_type, full_list_of_sections, 1, is_copy, phdrs) SectionKind.LITERAL, memory_type, full_list_of_sections, 1, is_copy, phdrs
gen_string += string_create_helper(SectionKind.RODATA, memory_type, full_list_of_sections, 1, is_copy, phdrs) )
gen_string += string_create_helper(
SectionKind.TEXT, memory_type, full_list_of_sections, 1, is_copy, phdrs
)
gen_string += string_create_helper(
SectionKind.RODATA, memory_type, full_list_of_sections, 1, is_copy, phdrs
)
if region_is_default_ram(memory_type) and is_copy: if region_is_default_ram(memory_type) and is_copy:
gen_string += MPU_RO_REGION_END.format(memory_type.lower()) gen_string += MPU_RO_REGION_END.format(memory_type.lower())
if region_is_default_ram(memory_type): if region_is_default_ram(memory_type):
gen_string_sram_data += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs) gen_string_sram_data += string_create_helper(
gen_string_sram_bss += string_create_helper(SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs) SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs
)
gen_string_sram_bss += string_create_helper(
SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs
)
else: else:
gen_string += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs) gen_string += string_create_helper(
gen_string += string_create_helper(SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs) SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs
)
gen_string += string_create_helper(
SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs
)
# finally writing to the linker file # finally writing to the linker file
with open(linker_file, "w") as file_desc: with open(linker_file, "w") as file_desc:
@ -432,17 +472,20 @@ def generate_memcpy_code(memory_type, full_list_of_sections, code_generation):
if kind in generate_sections and full_list_of_sections[kind]: if kind in generate_sections and full_list_of_sections[kind]:
code_generation["copy_code"] += MEMCPY_TEMPLATE.format(memory_type.lower(), kind.value) code_generation["copy_code"] += MEMCPY_TEMPLATE.format(memory_type.lower(), kind.value)
code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format( code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format(
memory_type.lower(), kind.value) memory_type.lower(), kind.value
)
# BSS sections in main memory are automatically zeroed; others need to have # BSS sections in main memory are automatically zeroed; others need to have
# zeroing code generated. # zeroing code generated.
if (SectionKind.BSS in generate_sections if (
SectionKind.BSS in generate_sections
and full_list_of_sections[SectionKind.BSS] and full_list_of_sections[SectionKind.BSS]
and not region_is_default_ram(memory_type) and not region_is_default_ram(memory_type)
): ):
code_generation["zero_code"] += MEMSET_TEMPLATE.format(memory_type.lower()) code_generation["zero_code"] += MEMSET_TEMPLATE.format(memory_type.lower())
code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format( code_generation["extern"] += EXTERN_LINKER_VAR_DECLARATION.format(
memory_type.lower(), SectionKind.BSS.value) memory_type.lower(), SectionKind.BSS.value
)
return code_generation return code_generation
@ -472,27 +515,37 @@ def parse_args():
global args global args
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=__doc__, description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) formatter_class=argparse.RawDescriptionHelpFormatter,
parser.add_argument("-d", "--directory", required=True, allow_abbrev=False,
help="obj file's directory") )
parser.add_argument("-i", "--input_rel_dict", required=True, type=argparse.FileType('r'), parser.add_argument("-d", "--directory", required=True, help="obj file's directory")
help="input file with dict src:memory type(sram2 or ccm or aon etc)") parser.add_argument(
"-i",
"--input_rel_dict",
required=True,
type=argparse.FileType('r'),
help="input file with dict src:memory type(sram2 or ccm or aon etc)",
)
parser.add_argument("-o", "--output", required=False, help="Output ld file") parser.add_argument("-o", "--output", required=False, help="Output ld file")
parser.add_argument("-s", "--output_sram_data", required=False, parser.add_argument("-s", "--output_sram_data", required=False, help="Output sram data ld file")
help="Output sram data ld file") parser.add_argument("-b", "--output_sram_bss", required=False, help="Output sram bss ld file")
parser.add_argument("-b", "--output_sram_bss", required=False, parser.add_argument(
help="Output sram bss ld file") "-c", "--output_code", required=False, help="Output relocation code header file"
parser.add_argument("-c", "--output_code", required=False, )
help="Output relocation code header file") parser.add_argument(
parser.add_argument("-R", "--default_ram_region", default='SRAM', "-R",
help="Name of default RAM memory region for system") "--default_ram_region",
parser.add_argument("-v", "--verbose", action="count", default=0, default='SRAM',
help="Verbose Output") help="Name of default RAM memory region for system",
)
parser.add_argument("-v", "--verbose", action="count", default=0, help="Verbose Output")
args = parser.parse_args() args = parser.parse_args()
def gen_all_obj_files(searchpath): def gen_all_obj_files(searchpath):
return list(Path(searchpath).rglob('*.o')) + list(Path(searchpath).rglob('*.obj')) return list(Path(searchpath).rglob('*.o')) + list(Path(searchpath).rglob('*.obj'))
# return the absolute path for the object file. # return the absolute path for the object file.
def get_obj_filename(all_obj_files, filename): def get_obj_filename(all_obj_files, filename):
# get the object file name which is almost always pended with .obj # get the object file name which is almost always pended with .obj
@ -503,6 +556,7 @@ def get_obj_filename(all_obj_files, filename):
if filename.split("/")[-2] in obj_file.parent.name: if filename.split("/")[-2] in obj_file.parent.name:
return str(obj_file) return str(obj_file)
# Extracts all possible components for the input string: # Extracts all possible components for the input string:
# <mem_region>[\ :program_header]:<flag_1>[;<flag_2>...]:<file_1>[;<file_2>...][,filter] # <mem_region>[\ :program_header]:<flag_1>[;<flag_2>...]:<file_1>[;<file_2>...][,filter]
# Returns a 5-tuple with them: (mem_region, program_header, flags, files, filter) # Returns a 5-tuple with them: (mem_region, program_header, flags, files, filter)
@ -520,7 +574,6 @@ def parse_input_string(line):
flag_list, rest = rest.split(':', 1) flag_list, rest = rest.split(':', 1)
flag_list = flag_list.split(';') flag_list = flag_list.split(';')
# Split file list by semicolons, in part to support generator expressions # Split file list by semicolons, in part to support generator expressions
file_list, symbol_filter = rest.split(',', 1) file_list, symbol_filter = rest.split(',', 1)
file_list = file_list.split(';') file_list = file_list.split(';')
@ -555,7 +608,7 @@ def create_dict_wrt_mem():
for file_glob in file_list: for file_glob in file_list:
glob_results = glob.glob(file_glob) glob_results = glob.glob(file_glob)
if not glob_results: if not glob_results:
warnings.warn("File: "+file_glob+" Not found") warnings.warn("File: " + file_glob + " Not found")
continue continue
elif len(glob_results) > 1: elif len(glob_results) > 1:
warnings.warn("Regex in file lists is deprecated, please use file(GLOB) instead") warnings.warn("Regex in file lists is deprecated, please use file(GLOB) instead")
@ -590,8 +643,9 @@ def main():
sram_data_linker_file = args.output_sram_data sram_data_linker_file = args.output_sram_data
sram_bss_linker_file = args.output_sram_bss sram_bss_linker_file = args.output_sram_bss
rel_dict, phdrs = create_dict_wrt_mem() rel_dict, phdrs = create_dict_wrt_mem()
complete_list_of_sections: 'dict[MemoryRegion, dict[SectionKind, list[OutputSection]]]' \ complete_list_of_sections: 'dict[MemoryRegion, dict[SectionKind, list[OutputSection]]]' = (
= defaultdict(lambda: defaultdict(list)) defaultdict(lambda: defaultdict(list))
)
# Create/or truncate file contents if it already exists # Create/or truncate file contents if it already exists
# raw = open(linker_file, "w") # raw = open(linker_file, "w")
@ -613,20 +667,19 @@ def main():
# cleanup and attach the sections to the memory type after cleanup. # cleanup and attach the sections to the memory type after cleanup.
sections_by_category = assign_to_correct_mem_region(memory_type, full_list_of_sections) sections_by_category = assign_to_correct_mem_region(memory_type, full_list_of_sections)
for (region, section_category_map) in sections_by_category.items(): for region, section_category_map in sections_by_category.items():
for (category, sections) in section_category_map.items(): for category, sections in section_category_map.items():
complete_list_of_sections[region][category].extend(sections) complete_list_of_sections[region][category].extend(sections)
generate_linker_script(linker_file, sram_data_linker_file, generate_linker_script(
sram_bss_linker_file, complete_list_of_sections, phdrs) linker_file, sram_data_linker_file, sram_bss_linker_file, complete_list_of_sections, phdrs
)
code_generation = {"copy_code": '', "zero_code": '', "extern": ''} code_generation = {"copy_code": '', "zero_code": '', "extern": ''}
for mem_type, list_of_sections in sorted(complete_list_of_sections.items()): for mem_type, list_of_sections in sorted(complete_list_of_sections.items()):
if "|COPY" in mem_type: if "|COPY" in mem_type:
mem_type = mem_type.split("|", 1)[0] mem_type = mem_type.split("|", 1)[0]
code_generation = generate_memcpy_code(mem_type, code_generation = generate_memcpy_code(mem_type, list_of_sections, code_generation)
list_of_sections, code_generation)
dump_header_file(args.output_code, code_generation) dump_header_file(args.output_code, code_generation)