Skip to content

Commit

Permalink
fix(ldgen): enable default name SORT in linker fragment
Browse files Browse the repository at this point in the history
Currently, the `SORT` flag mandates the inclusion of at least the
`sort_by_first` argument in the grammar, despite the documentation[1]
indicating that `SORT` can be utilized without any arguments, defaulting
to sorting input sections by name. Fix this by modifying the grammar
to allow a default `SORT` and update a test accordingly.

[1] https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/
    linker-script-generation.html

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
  • Loading branch information
fhrbata committed Oct 10, 2024
1 parent b7d0eeb commit d414eac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
43 changes: 32 additions & 11 deletions tools/ldgen/ldgen/fragments.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
#
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
#

from typing import Any, Dict, List, Optional, Set, Tuple, Union

from pyparsing import (Combine, Forward, Group, IndentedBlock, Keyword, LineEnd, Literal, OneOrMore, Opt,
ParseFatalException, SkipTo, Suppress, Word, ZeroOrMore, alphanums, alphas, delimited_list,
nums, rest_of_line)
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Tuple
from typing import Union

from pyparsing import alphanums
from pyparsing import alphas
from pyparsing import Combine
from pyparsing import delimited_list
from pyparsing import Forward
from pyparsing import Group
from pyparsing import IndentedBlock
from pyparsing import Keyword
from pyparsing import LineEnd
from pyparsing import Literal
from pyparsing import nums
from pyparsing import OneOrMore
from pyparsing import Opt
from pyparsing import ParseFatalException
from pyparsing import rest_of_line
from pyparsing import SkipTo
from pyparsing import Suppress
from pyparsing import Word
from pyparsing import ZeroOrMore


class Empty:
Expand Down Expand Up @@ -227,11 +248,11 @@ class Sort(EntryFlag):
_keywords = Keyword('name') | Keyword('alignment') | Keyword('init_priority')
SORT = (Keyword('SORT').suppress()
+ Suppress('(')
+ _keywords.set_results_name('first')
+ Opt(Suppress(',') + _keywords.set_results_name('second'))
+ Opt(_keywords.set_results_name('first')
+ Opt(Suppress(',') + _keywords.set_results_name('second')))
+ Suppress(')'))

def __init__(self, first: str, second: Optional[str] = None):
def __init__(self, first: Optional[str] = None, second: Optional[str] = None):
self.first = first
self.second = second

Expand All @@ -244,7 +265,7 @@ def __eq__(self, other):

@staticmethod
def parse(toks):
return Sort(toks.first, toks.second or None)
return Sort(toks.first or None, toks.second or None)


class Flag:
Expand Down
4 changes: 3 additions & 1 deletion tools/ldgen/test/test_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ def test_sort_flag(self):
archive: libmain.a
entries:
obj1 (default);
text->flash_text SORT(),
text->flash_text SORT(name),
rodata->flash_rodata SORT(alignment),
data->dram0_data SORT(init_priority),
Expand All @@ -717,7 +718,8 @@ def test_sort_flag(self):
fragment_file = parse_fragment_file(test_fragment, self.sdkconfig)
fragment = fragment_file.fragments[0]

expected = [Flag('text', 'flash_text', [Sort('name')]),
expected = [Flag('text', 'flash_text', [Sort()]),
Flag('text', 'flash_text', [Sort('name')]),
Flag('rodata', 'flash_rodata', [Sort('alignment')]),
Flag('data', 'dram0_data', [Sort('init_priority')]),
Flag('bss', 'dram0_bss', [Sort('name', 'alignment')]),
Expand Down

0 comments on commit d414eac

Please sign in to comment.