Scope build_src_flags to project source compilation only#5407
Scope build_src_flags to project source compilation only#5407huyhoang171106 wants to merge 1 commit intoplatformio:developfrom
build_src_flags to project source compilation only#5407Conversation
Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
There was a problem hiding this comment.
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_envfor building project sources. - Apply project-source-only flags on
src_envbefore 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")) |
There was a problem hiding this comment.
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.
| src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS")) | |
| src_env.ProcessFlags(src_env.get("SRC_BUILD_FLAGS")) |
| src_env = plb.env.Clone() | ||
| src_env.ProcessFlags(src_env.get("BUILD_SRC_FLAGS")) |
There was a problem hiding this comment.
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.
| 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) |
Summary
The current build flow applies
build_src_flagstoo 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-Wpedanticinbuild_src_flags. The fix is to keepbuild_src_flagsisolated 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
Closes #5245