Skip to content

Scope build_src_flags to project source compilation only#5407

Open
huyhoang171106 wants to merge 1 commit intoplatformio:developfrom
huyhoang171106:fix/scope-build-src-flags-to-project-source-
Open

Scope build_src_flags to project source compilation only#5407
huyhoang171106 wants to merge 1 commit intoplatformio:developfrom
huyhoang171106:fix/scope-build-src-flags-to-project-source-

Conversation

@huyhoang171106
Copy link
Copy Markdown

Summary

The current build flow applies build_src_flags too early (on a shared/base construction environment), which leaks those flags into library compilation (lib_deps). This breaks third-party libraries when users set strict flags like -Wpedantic in build_src_flags. The fix is to keep build_src_flags isolated and apply them only when compiling files from $PROJECT_SRC_DIR (and not to library builders, framework packages, or other dependency compilation units).

Files changed

  • platformio/builder/tools/piobuild.py (modified)

Testing

  • Not run in this environment.

Closes #5245

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 26, 2026 14:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent user build_src_flags from leaking into third-party library (lib_deps) compilation by scoping those flags to compilation of sources under $PROJECT_SRC_DIR only.

Changes:

  • Clone the project lib builder environment into a dedicated src_env for building project sources.
  • Apply project-source-only flags on src_env before compiling $PROJECT_SRC_DIR.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if "test" not in env["BUILD_TYPE"] or env.GetProjectOption("test_build_src"):
plb.env.BuildSources(
src_env = plb.env.Clone()
src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS"))
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_src_flags is stored in the build env as SRC_BUILD_FLAGS (see platformio/project/options.py), but this code reads BUILD_SRC_FLAGS, so the option won’t be applied to project sources (and builds that rely on -D… in build_src_flags will break). Use SRC_BUILD_FLAGS here.

Suggested change
src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS"))
src_env.ProcessFlags(src_env.get("SRC_BUILD_FLAGS"))

Copilot uses AI. Check for mistakes.
Comment on lines +179 to +180
src_env = plb.env.Clone()
src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS"))
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is changed to use SRC_BUILD_FLAGS, note that ProjectAsLibBuilder already processes SRC_BUILD_FLAGS into plb.env during initialization (via ProjectAsLibBuilder.build_flags + LibBuilderBase.process_extra_options). Processing the same flags again on src_env would duplicate them. To truly scope build_src_flags to project source compilation, SRC_BUILD_FLAGS should be removed/avoided from plb.env and applied only on the dedicated env used to compile $PROJECT_SRC_DIR.

Suggested change
src_env = plb.env.Clone()
src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS"))
# Use a dedicated environment for project sources to avoid
# re-processing source build flags that may already be applied in plb.env
src_env = env.Clone()
build_src_flags = plb.env.get("BUILD_SRC_FLAGS") or env.get("BUILD_SRC_FLAGS")
if build_src_flags:
src_env.ProcessFlags(build_src_flags)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: build_src_flags has an impact on lib_deps

2 participants