Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,7 @@ if(NOT MBED_IS_NATIVE_BUILD)
# Make sure that things linking mbed-core-flags can also get the target-specific include dirs and flags.
mbed_extract_flags(${MBED_TARGET_CMAKE_NAME}-flags ${MBED_TARGET_CMAKE_NAME})
target_link_libraries(mbed-core-flags INTERFACE ${MBED_TARGET_CMAKE_NAME}-flags)

# If the user specified default link libraries in their mbed_app.json, link those to mbed-os
target_link_libraries(${MBED_OS_CORE_LIB_NAME} INTERFACE ${MBED_DEFAULT_LINK_LIBRARIES})
endif()
10 changes: 10 additions & 0 deletions tools/python/mbed_platformio/build_mbed_ce.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
extract_flags,
extract_includes,
extract_link_args,
extract_link_libraries,
find_included_files,
)
from mbed_platformio.pio_variants import PIO_VARIANT_TO_MBED_TARGET
Expand Down Expand Up @@ -281,6 +282,7 @@ def get_app_defines(app_config: dict) -> list[tuple[str, str]]:
project_defines = get_app_defines(app_target_json)
project_flags = extract_flags(app_target_json)
app_includes = extract_includes(app_target_json)
app_link_libraries = extract_link_libraries(app_target_json)

## Linker flags --------------------------------------------------------------------------------------------------------

Expand All @@ -290,6 +292,14 @@ def get_app_defines(app_config: dict) -> list[tuple[str, str]]:
link_args = ["-Wl,--whole-archive", '"' + str(mbed_ce_lib_path) + '"', "-Wl,--no-whole-archive"]
_ = env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", str(mbed_ce_lib_path))

# Now link optional libraries. Note that according to normal link order rules, we'd link the optional libraries
# first since they reference libmbed-os.a. However, mbed-os can also reference the optional libraries
# (e.g. usb for USB serial support), and all of libmbed-os.a is already being considered for linking thanks
# to --whole-archive. So we actually want to link these second.
for lib in app_link_libraries:
link_args.append(str(lib))
_ = env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", str(lib))

# Get other linker flags from Mbed. We want these to appear after the application objects and Mbed libraries
# because they contain the C/C++ library link flags.
link_args.extend(extract_link_args(app_target_json))
Expand Down
13 changes: 13 additions & 0 deletions tools/python/mbed_platformio/cmake_to_scons_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ def extract_link_args(target_json: dict) -> list[str]:
result.extend(args)

return result


def extract_link_libraries(target_json: dict) -> list[pathlib.Path]:
"""
Extract the link libraries from a CMake target and return a list of libraries to link, in order.

Note that this is currently set up to handle only static libraries.
"""
return [
pathlib.Path("$BUILD_DIR") / (fragment["fragment"])
for fragment in target_json.get("link", {}).get("commandFragments", [])
if fragment["role"] == "libraries"
]
1 change: 1 addition & 0 deletions tools/python/mbed_platformio/pio_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
"nucleo_h743zi": "NUCLEO_H743ZI2",
"genericSTM32F103RB": "NUCLEO_F103RB",
"disco_h747xi": "DISCO_H747I",
"rpipico": "RASPBERRY_PI_PICO",
}
11 changes: 11 additions & 0 deletions tools/python/mbed_tools/build/_internal/config/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ class MbedAppJSON(BaseJSONConfig):
results may not be what you expect.
"""

link_libraries: list[str] = Field(default_factory=list)
"""
Default link libraries to link along with the base ``mbed-os`` (or ``mbed-baremetal``) library to your application.

This should be a set to a list of strings containing CMake library names built in to Mbed to link, such as
"mbed-netsocket" or "mbed-storage".

This is simply a shortcut for linking libraries in CMake, if you don't want to or can't
(e.g. because you are using PlatformIO so you don't have direct access to CMake scripts).
"""

@model_validator(mode="after")
def verify_target_memory_bank_config_used_where_appropriate(self) -> Self:
for overrides in self.target_overrides.values():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(MBED_C_LIB "{{c_lib}}")
set(MBED_PRINTF_LIB "{{printf_lib}}")
set(MBED_OUTPUT_EXT "{{OUTPUT_EXT}}")
set(MBED_GREENTEA_TEST_RESET_TIMEOUT "{{forced_reset_timeout}}")
set(MBED_DEFAULT_LINK_LIBRARIES {% for lib in link_libraries %} {{lib}} {%- endfor %})

# JSON files used to generate this config. If any of these change, the Python config generation
# scripts must be rerun.
Expand Down