Skip to content

cmake/sitl: use CheckLinkerFlag instead of GCC version check for --no-warn-rwx-segments#11383

Open
sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
sensei-hacker:fix-sitl-cmake-rwx-linker-flag
Open

cmake/sitl: use CheckLinkerFlag instead of GCC version check for --no-warn-rwx-segments#11383
sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
sensei-hacker:fix-sitl-cmake-rwx-linker-flag

Conversation

@sensei-hacker
Copy link
Member

Summary

Replace the fragile GCC version check for --no-warn-rwx-segments in cmake/sitl.cmake with a proper CheckLinkerFlag capability probe.

Changes

  • Remove CMAKE_COMPILER_IS_GNUCC + VERSION_LESS 12.0 heuristic
  • Add include(CheckLinkerFlag) and check_linker_flag() probe
  • The probe directly tests whether the linker accepts the flag, rather than guessing from the compiler version

check_linker_flag() has been available since CMake 3.18; SITL already requires CMake 3.22+, so there is no minimum version concern.

Testing

  • Built SITL successfully after the change
  • On this system the probe correctly detected that --no-warn-rwx-segments is not supported (ld < 2.39) and skipped the flag — build completed cleanly with no linker warnings or errors
  • On systems with ld ≥ 2.39 the probe will pass and the flag will be applied as before

…-warn-rwx-segments

Replace the fragile CMAKE_COMPILER_IS_GNUCC + version threshold heuristic
with a proper check_linker_flag() probe (available since CMake 3.18; SITL
already requires 3.22+). The probe directly tests whether the linker
accepts the flag rather than guessing based on compiler version.
@qodo-code-review
Copy link
Contributor

Review Summary by Qodo

Replace GCC version check with CheckLinkerFlag probe

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Replace GCC version heuristic with proper linker capability probe
• Use CMake's CheckLinkerFlag module for robust flag detection
• Directly test linker support instead of guessing from compiler version
Diagram
flowchart LR
  A["GCC version check<br/>CMAKE_COMPILER_IS_GNUCC"] -->|replaced| B["CheckLinkerFlag probe<br/>check_linker_flag()"]
  B -->|tests linker support| C["LINKER_SUPPORTS_NO_RWX_WARNING"]
  C -->|if true| D["Apply --no-warn-rwx-segments flag"]
Loading

Grey Divider

File Changes

1. cmake/sitl.cmake ✨ Enhancement +3/-1

Replace version check with linker capability probe

• Removed fragile GCC version check (CMAKE_COMPILER_IS_GNUCC + VERSION_LESS 12.0)
• Added include(CheckLinkerFlag) module
• Replaced version heuristic with check_linker_flag() probe for --no-warn-rwx-segments
• Flag is now applied only when linker actually supports it

cmake/sitl.cmake


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Mar 1, 2026

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Breaks older CMake configs🐞 Bug ⛯ Reliability
Description
cmake/sitl.cmake now unconditionally calls include(CheckLinkerFlag)/check_linker_flag(), but
the repository still allows CMake 3.13+. Configuring with CMake < 3.18 will fail while parsing
sitl.cmake, even when SITL is not enabled, because include(sitl) is unconditional.
Code

cmake/sitl.cmake[R67-68]

+    include(CheckLinkerFlag)
+    check_linker_flag(C "-Wl,--no-warn-rwx-segments" LINKER_SUPPORTS_NO_RWX_WARNING)
Evidence
The repo declares a minimum CMake version as low as 3.13 and always includes sitl.cmake, so any
newer CMake-module dependency used at file scope becomes a hard configure-time requirement for all
builds. The PR description itself states check_linker_flag() is only available since CMake 3.18,
which conflicts with the repo’s stated minimum.

CMakeLists.txt[1-1]
CMakeLists.txt[92-95]
cmake/sitl.cmake[61-69]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`cmake/sitl.cmake` uses `include(CheckLinkerFlag)` / `check_linker_flag()`, but the repo’s declared minimum CMake version is 3.13. This can make configuration fail on older CMake versions because `sitl.cmake` is included unconditionally.
### Issue Context
Top-level `CMakeLists.txt` uses `cmake_minimum_required(VERSION 3.13...3.18)` and always `include(sitl)`, so anything executed at file scope in `cmake/sitl.cmake` must be compatible with the minimum supported CMake.
### Fix Focus Areas
- CMakeLists.txt[1-1]
- CMakeLists.txt[92-95]
- cmake/sitl.cmake[61-71]
### Suggested fix approaches
1) **Preferred (if acceptable):** bump minimum CMake to **&amp;gt;= 3.18**.
2) **Otherwise:** guard usage:
 - Check `if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)` (and/or `if(COMMAND check_linker_flag)` after `include(CheckLinkerFlag OPTIONAL)`) before calling it.
 - On older CMake, skip adding the flag or use a fallback heuristic.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. Probe runs on all builds🐞 Bug ➹ Performance
Description
The new check_linker_flag() probe runs at configure time whenever sitl.cmake is included, even
for non-SITL builds where target_sitl() immediately returns. This adds unnecessary configure-time
work and, under the cross toolchain, try_compile is set to produce only static libraries, which is
a poor fit for validating linker options.
Code

cmake/sitl.cmake[R67-71]

+    include(CheckLinkerFlag)
+    check_linker_flag(C "-Wl,--no-warn-rwx-segments" LINKER_SUPPORTS_NO_RWX_WARNING)
+    if(LINKER_SUPPORTS_NO_RWX_WARNING)
       set(SITL_LINK_OPTIONS ${SITL_LINK_OPTIONS} "-Wl,--no-warn-rwx-segments")
   endif()
Evidence
sitl.cmake is included unconditionally by the top-level build. The linker-flag probe is executed
at file scope, but actual SITL targets are only created when TOOLCHAIN is host. For non-SITL
builds, the default toolchain is arm-none-eabi, which sets CMAKE_TRY_COMPILE_TARGET_TYPE to
STATIC_LIBRARY, making configure-time link-flag probing less appropriate than simply running it
only when building SITL.

CMakeLists.txt[13-23]
CMakeLists.txt[92-95]
cmake/sitl.cmake[61-71]
cmake/sitl.cmake[81-88]
cmake/arm-none-eabi.cmake[8-13]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`check_linker_flag()` currently runs at file scope in `cmake/sitl.cmake`, so it executes during configuration for all builds (including non-SITL embedded builds) even though SITL targets aren’t generated unless `TOOLCHAIN` is `host`.
### Issue Context
- `CMakeLists.txt` unconditionally `include(sitl)`.
- `target_sitl()` returns early when not building with the host toolchain.
- The arm-none-eabi toolchain sets `CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY`, which is not ideal for link-option probing.
### Fix Focus Areas
- CMakeLists.txt[92-95]
- cmake/sitl.cmake[61-71]
- cmake/sitl.cmake[81-88]
### Suggested fix
Move or guard the probe so it only runs when building SITL with the host toolchain, e.g.:
- Wrap the probe with `if(SITL)` and/or `if(TOOLCHAIN STREQUAL &amp;quot;host&amp;quot;)`, **or**
- Relocate the `include(CheckLinkerFlag)` + `check_linker_flag(...)` block into `function(target_sitl ...)` after the `if(NOT host STREQUAL TOOLCHAIN) return()` guard.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

check_linker_flag() requires CMake 3.18. Use include(CheckLinkerFlag OPTIONAL)
and if(COMMAND check_linker_flag) to probe for the module's availability rather
than hardcoding a version number. On CMake < 3.18 the include silently does
nothing, the guard skips the call, and LINKER_SUPPORTS_NO_RWX_WARNING is left
unset — the flag is simply not added, which is safe because linkers old enough
to ship with CMake < 3.18 do not produce the RWX warning anyway.
@github-actions
Copy link

github-actions bot commented Mar 3, 2026

Test firmware build ready — commit 36bb13d

Download firmware for PR #11383

223 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

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.

1 participant