diff mbox series

[v2] classes/cmake: unset LDFLAGS in toolchain-native.bbclass

Message ID 20250926143339.3542389-1-ross.burton@arm.com
State Under Review
Headers show
Series [v2] classes/cmake: unset LDFLAGS in toolchain-native.bbclass | expand

Commit Message

Ross Burton Sept. 26, 2025, 2:33 p.m. UTC
If a recipe is using toolchain-native.cmake to build native portion in a
non-native build, the target LDFLAGS from the environment will leak into
the native build.

This was noticed as building a SDK with clang means that LDFLAGS contains
a --dynamic-loader argument, so native binaries were trying to use the
target loader.

There are several variables that are set from LDFLAGS[1] so instead of
setting them all, we can simply unset the environment variable in the
toolchain.

[1] https://cmake.org/cmake/help/latest/envvar/LDFLAGS.html

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 meta/classes-recipe/cmake.bbclass | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Khem Raj Sept. 26, 2025, 3:31 p.m. UTC | #1
Thanks for adding more context. LGTM

On Fri, Sep 26, 2025 at 7:33 AM Ross Burton via lists.openembedded.org
<ross.burton=arm.com@lists.openembedded.org> wrote:
>
> If a recipe is using toolchain-native.cmake to build native portion in a
> non-native build, the target LDFLAGS from the environment will leak into
> the native build.
>
> This was noticed as building a SDK with clang means that LDFLAGS contains
> a --dynamic-loader argument, so native binaries were trying to use the
> target loader.
>
> There are several variables that are set from LDFLAGS[1] so instead of
> setting them all, we can simply unset the environment variable in the
> toolchain.
>
> [1] https://cmake.org/cmake/help/latest/envvar/LDFLAGS.html
>
> Signed-off-by: Ross Burton <ross.burton@arm.com>
> ---
>  meta/classes-recipe/cmake.bbclass | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/meta/classes-recipe/cmake.bbclass b/meta/classes-recipe/cmake.bbclass
> index b8cd622c2df..4f59966521e 100644
> --- a/meta/classes-recipe/cmake.bbclass
> +++ b/meta/classes-recipe/cmake.bbclass
> @@ -212,6 +212,15 @@ set( CMAKE_LIBRARY_PATH ${STAGING_BASE_LIBDIR_NATIVE} ${STAGING_LIBDIR_NATIVE})
>  list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${STAGING_INCDIR_NATIVE})
>  list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${STAGING_INCDIR_NATIVE})
>
> +# The assignmens above override CFLAGS and CXXFLAGS from the environment but
> +# not LDFLAGS, which ends up in CMAKE_EXE_LINKER_FLAGS. This then means our
> +# native builds use target flags, and can fail.
> +#
> +# As there are a number of variables that are set from LDFLAGS,
> +# clear it at source.
> +#
> +# https://cmake.org/cmake/help/latest/envvar/LDFLAGS.html
> +unset(ENV{LDFLAGS})
>  EOF
>  }
>
> --
> 2.43.0
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#224082): https://lists.openembedded.org/g/openembedded-core/message/224082
> Mute This Topic: https://lists.openembedded.org/mt/115449745/1997914
> Group Owner: openembedded-core+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [raj.khem@gmail.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
diff mbox series

Patch

diff --git a/meta/classes-recipe/cmake.bbclass b/meta/classes-recipe/cmake.bbclass
index b8cd622c2df..4f59966521e 100644
--- a/meta/classes-recipe/cmake.bbclass
+++ b/meta/classes-recipe/cmake.bbclass
@@ -212,6 +212,15 @@  set( CMAKE_LIBRARY_PATH ${STAGING_BASE_LIBDIR_NATIVE} ${STAGING_LIBDIR_NATIVE})
 list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${STAGING_INCDIR_NATIVE})
 list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${STAGING_INCDIR_NATIVE})
 
+# The assignmens above override CFLAGS and CXXFLAGS from the environment but
+# not LDFLAGS, which ends up in CMAKE_EXE_LINKER_FLAGS. This then means our
+# native builds use target flags, and can fail.
+#
+# As there are a number of variables that are set from LDFLAGS,
+# clear it at source.
+#
+# https://cmake.org/cmake/help/latest/envvar/LDFLAGS.html
+unset(ENV{LDFLAGS})
 EOF
 }