Skip to content

Commit

Permalink
Refactor processing of IDF linker fragments
Browse files Browse the repository at this point in the history
The helper function extracts fragments directly from Ninja build script
  • Loading branch information
valeros committed May 10, 2024
1 parent 09056b0 commit 35b29a1
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions builder/frameworks/espidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import sys
import shutil
import os
import re
import platform as sys_platform

import click
Expand Down Expand Up @@ -494,7 +495,7 @@ def _scan_components_from_framework():
return components or _scan_components_from_framework()


def extract_linker_script_fragments(framework_components_dir, sdk_config):
def extract_linker_script_fragments_backup(framework_components_dir, sdk_config):
# Hardware-specific components are excluded from search and added manually below
project_components = load_component_paths(
framework_components_dir, ignored_component_prefixes=("esp32", "riscv")
Expand Down Expand Up @@ -542,6 +543,52 @@ def extract_linker_script_fragments(framework_components_dir, sdk_config):
return result


def extract_linker_script_fragments(
ninja_buildfile, framework_components_dir, sdk_config
):
def _normalize_fragment_path(base_dir, fragment_path):
if not os.path.isabs(fragment_path):
fragment_path = os.path.abspath(
os.path.join(base_dir, fragment_path)
)
if not os.path.isfile(fragment_path):
print("Warning! The `%s` fragment is not found!" % fragment_path)

return fragment_path

assert os.path.isfile(
ninja_buildfile
), "Cannot extract linker fragments! Ninja build file is missing!"

result = []
with open(ninja_buildfile, encoding="utf8") as fp:
for line in fp.readlines():
if "sections.ld: CUSTOM_COMMAND" not in line:
continue
for fragment_match in re.finditer(r"(\S+\.lf\b)+", line):
result.append(_normalize_fragment_path(
BUILD_DIR, fragment_match.group(0).replace("$:", ":")
))

break

# Fall back option if the new algorithm didn't work
if not result:
result = extract_linker_script_fragments_backup(
framework_components_dir, sdk_config
)

if board.get("build.esp-idf.extra_lf_files", ""):
for fragment_path in board.get(
"build.esp-idf.extra_lf_files"
).splitlines():
if not fragment_path.strip():
continue
result.append(_normalize_fragment_path(PROJECT_DIR, fragment_path))

return result


def create_custom_libraries_list(ldgen_libraries_file, ignore_targets):
if not os.path.isfile(ldgen_libraries_file):
sys.stderr.write("Error: Couldn't find the list of framework libraries\n")
Expand Down Expand Up @@ -570,11 +617,13 @@ def create_custom_libraries_list(ldgen_libraries_file, ignore_targets):
def generate_project_ld_script(sdk_config, ignore_targets=None):
ignore_targets = ignore_targets or []
linker_script_fragments = extract_linker_script_fragments(
os.path.join(FRAMEWORK_DIR, "components"), sdk_config
os.path.join(BUILD_DIR, "build.ninja"),
os.path.join(FRAMEWORK_DIR, "components"),
sdk_config
)

# Create a new file to avoid automatically generated library entry as files from
# this library are built internally by PlatformIO
# Create a new file to avoid automatically generated library entry as files
# from this library are built internally by PlatformIO
libraries_list = create_custom_libraries_list(
os.path.join(BUILD_DIR, "ldgen_libraries"), ignore_targets
)
Expand Down

0 comments on commit 35b29a1

Please sign in to comment.