Skip to content

Conversation

@dcpleung
Copy link
Member

@dcpleung dcpleung commented Apr 4, 2023

Cache the property GNULD_LINKER_IS_BFD between cmake invocations. It is observed that, in repeated builds (2nd time and later), this property becomes true even for non-bfd compatible linker. So cache it to avoid any surprises.

Fixes #56501

marc-hb
marc-hb previously approved these changes Apr 4, 2023
Copy link
Contributor

@marc-hb marc-hb left a comment

Choose a reason for hiding this comment

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

I confirm a (small variation of) this fixes #56501. Thanks for the very prompt fix!

I would still like some clarification about https://docs.zephyrproject.org/latest/develop/west/build-flash-debug.html#one-time-cmake-arguments : is it "safe" or not? I mean as long as the same CMake arguments are used every time. Just slower sometimes?

Nit: instead of duplicating them exactly, I would make the doc strings slightly different and unique so they can be used as "breadcrumbs".

nashif
nashif previously approved these changes Apr 4, 2023
Copy link
Contributor

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

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

minor adjustments requested, but generally looks good.

Copy link
Contributor

Choose a reason for hiding this comment

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

The lookup at those locations has already set the cache variable:
https://github.com/zephyrproject-rtos/zephyr/blob/bbc4f11fb3a35d84568c53340a6a934777f71461/cmake/modules/FindGnuLd.cmake#L46
https://github.com/zephyrproject-rtos/zephyr/blob/bbc4f11fb3a35d84568c53340a6a934777f71461/cmake/modules/FindGnuLd.cmake#L49

so only chance that GNULD_LINKER_IS_BFD has already been set, but the corresponding GNULD_LINKER_IS_BFD is undefined is when a user has specifically pointed to a ld through the use of: -DGNULD_LINKER_IS_BFD=<forced-linker-to-use>.

In such a case we cannot really know if linker is bfd compatible or not, as we have no proper detection mechanism.

So in the scenario described we must rely on user to provide us the correct setting, so I propose issue a warning in such case.

Something like:

if(EXISTS "${GNULD_LINKER}")
  if(NOT DEFINED GNULD_LINKER_IS_BFD)
    message(WARNING "GNULD_LINKER specified directly in cache, but GNULD_LINKER_IS_BFD is not defined."
                    "Assuming GNULD_LINKER_IS_BFD as OFF, please set GNULD_LINKER_IS_BFD to correct value in cache to silence this warning"
    )
    set(GNULD_LINKER_IS_BFD OFF)
  endif()
endif()

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the warning and also to return early if variable is already set.

Copy link
Contributor

Choose a reason for hiding this comment

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

please add FORCE.

If this code ever executes a second time, then it means that someone purposely has cleared the GNULD_LINKER setting, and therefor we should ensure to update the corresponding GNULD_LINKER_IS_BFD to the new lookup we perform.

Suggested change
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Is linker ld.bfd compatible")
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Is linker ld.bfd compatible" FORCE)

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the code to return early if GNULD_LINKER_IS_BFD is already set. So no need to FORCE a change.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dcpleung your "return early" is currently when GNULD_LINKER exists. @tejlmand describes a case where someone deleted it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added back.

Copy link
Contributor

@marc-hb marc-hb Apr 6, 2023

Choose a reason for hiding this comment

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

My rule of thumb for FORCE is to add it first and then think later
https://stackoverflow.com/questions/39588836/why-do-i-need-force-to-override-a-cmake-option

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
set(GNULD_LINKER_IS_BFD OFF CACHE BOOL "Is linker ld.bfd compatible")
set(GNULD_LINKER_IS_BFD OFF CACHE BOOL "Is linker ld.bfd compatible" FORCE)

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the code to return early if GNULD_LINKER_IS_BFD is already set. So no need to FORCE a change.

tmleman
tmleman previously approved these changes Apr 5, 2023
@marc-hb
Copy link
Contributor

marc-hb commented Apr 5, 2023

Thanks again @tejlmand for your time and critical/"bus factor" expertise. Since you have a very good understanding of these CMake issues and while you're here, could you please answer this earlier, more generic and related question of mine:

I would still like some clarification about https://docs.zephyrproject.org/latest/develop/west/build-flash-debug.html#one-time-cmake-arguments : is it "safe" or not? I mean as long as the same CMake arguments are used every time. Just slower sometimes?

@mbolivar-nordic I think you mentioned once some past discussions on this topic, were they on Github, Slack or Discord?

As usual I'm volunteering for a minor doc fix if needed but I need to know what it should be :-)

I'm among the few people who know the secrets to make the doc build usable (#55708)

@dcpleung dcpleung dismissed stale reviews from tmleman, nashif, and marc-hb via 8f5d46b April 5, 2023 17:46
@dcpleung dcpleung force-pushed the toolchain/cache_bfd_flag branch from bbc4f11 to 8f5d46b Compare April 5, 2023 17:46
@nashif nashif requested a review from tejlmand April 5, 2023 20:19
nashif
nashif previously approved these changes Apr 5, 2023
Copy link
Contributor

@marc-hb marc-hb left a comment

Choose a reason for hiding this comment

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

Tested again and fixes the issue again.

Nit: instead of duplicating them exactly, I would make the doc strings slightly different and unique so they can be used as "breadcrumbs".

Ping? Even more relevant now that the logic is becoming more complex.

@dcpleung
Copy link
Member Author

dcpleung commented Apr 5, 2023

Nit: instead of duplicating them exactly, I would make the doc strings slightly different and unique to they can be used as "breadcrumbs".

Ping? Even more relevant now that the logic is becoming more complex.

Forgot to change those while juggling other changes.

nashif
nashif previously approved these changes Apr 5, 2023
@dcpleung dcpleung force-pushed the toolchain/cache_bfd_flag branch 2 times, most recently from ca84c55 to e444f68 Compare April 6, 2023 16:04
marc-hb
marc-hb previously approved these changes Apr 6, 2023
nashif
nashif previously approved these changes Apr 6, 2023
@marc-hb
Copy link
Contributor

marc-hb commented Apr 7, 2023

Copy link
Contributor

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

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

I believe the return should also cover the case where the GNULD_LINKER_IS_BFD as there seems to be no reason to start detecting / manipulating GNULD_LINKER` setting when already set.

And while at it, please align the CMakeCache description text.

Comment on lines 29 to 30
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you would want to move the return down, so that if ever GNULD_LINKER is defined then the look-up of ld linker is aborted.

Other comment will be made to follow up this comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the early return should happen here.

If for any reason GNULD_LINKER has been pointed out manually by a user, then we should not try to detect GNULD_LINKER_IS_BFD based on current compiler, hence I believe we should move the return here:

Suggested change
endif()
endif()
return()

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe try to make the CMake cache comment more aligned. (see also following comments)

Proposal:

Suggested change
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Compiler says linker is ld.bfd compatible" FORCE)
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Linker BFD compatibility (compiler reported)" FORCE)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Found ld.bfd compatible linker" FORCE)
set(GNULD_LINKER_IS_BFD ON CACHE BOOL "Linker BFD compatibility (inferred from binary)" FORCE)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
set(GNULD_LINKER_IS_BFD OFF CACHE BOOL "ld.bfd compatible linker not found" FORCE)
set(GNULD_LINKER_IS_BFD OFF CACHE BOOL "Linker BFD compatibility (inferred from binary)" FORCE)

@tejlmand
Copy link
Contributor

I would still like some clarification about https://docs.zephyrproject.org/latest/develop/west/build-flash-debug.html#one-time-cmake-arguments : is it "safe" or not? I mean as long as the same CMake arguments are used every time. Just slower sometimes?

been out for Easter 🐰 , but happy to give an answer.
From CMake perspective, then it should always be valid to set a public CMake cache variable during a new CMake run.
Internal CMake cache variables should not be set by users because as the name implies, they are internal.

When it comes to west, then west will detect that -- is used and forcefully invoke a CMake re-run even if such re-run is not required.
This is perfectly valid but it will of course waste resources if a CMake re-run was not required.
For example a CMake re-run is not needed when the "new" CMake cache value is identical to the existing value.

Some settings can be passed multiple times only if the value is identical to the existing value in the cache, for example BOARD.
So doing this is allowed:

$ cmake -DBOARD=nrf52840dk_nrf52840 -Bbuild samples/hello_world
$ cmake -DBOARD=nrf52840dk_nrf52840 build
$ cmake -DBOARD=nrf52840dk_nrf52840 build

when the passed board is identical to existing value.

But changing the board is prohibited, so if you try to do:

$ cmake -DBOARD=nrf52840dk_nrf52811 build
...
CMake Warning at /projects/github/ncs/zephyr/cmake/modules/extensions.cmake:2617 (message):
  The build directory must be cleaned pristinely when changing board,

  Current value="nrf52840dk_nrf52840", Ignored value="nrf52840dk_nrf52811"

the old value will be used.

That is not a CMake limitation, but a Zephyr limitation because some settings are so fundamental that we cannot allow them to be changed without a pristine build.

@marc-hb marc-hb added the platform: Intel ADSP Intel Audio platforms label Apr 11, 2023
Cache the property GNULD_LINKER_IS_BFD between cmake invocations.
It is observed that, in repeated builds (2nd time and later),
this property becomes true even for non-bfd compatible linker.
So cache it to avoid any surprises.

Fixes zephyrproject-rtos#56501

Signed-off-by: Daniel Leung <[email protected]>
@dcpleung dcpleung dismissed stale reviews from nashif and marc-hb via 0521d54 April 11, 2023 21:06
@dcpleung dcpleung force-pushed the toolchain/cache_bfd_flag branch from e444f68 to 0521d54 Compare April 11, 2023 21:06
@dcpleung
Copy link
Member Author

Updated according to comments.

@tejlmand
Copy link
Contributor

@dcpleung thanks for the patience 👍

@marc-hb marc-hb added bug The issue is a bug, or the PR is fixing a bug priority: high High impact/importance bug labels Apr 12, 2023
@nashif
Copy link
Member

nashif commented Apr 12, 2023

image

Please do not mark PRs with issue labels, that does not help anyone.

@nashif nashif merged commit 2a76637 into zephyrproject-rtos:main Apr 12, 2023
@nashif nashif removed bug The issue is a bug, or the PR is fixing a bug priority: high High impact/importance bug labels Apr 12, 2023
@dcpleung dcpleung deleted the toolchain/cache_bfd_flag branch April 12, 2023 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Since GNULD_LINKER_IS_BFD, incremental SOF build fails with a very confusing "-fuse-ld=bfd: unknown flag"

6 participants