xuxin930 opened a new pull request, #17868:
URL: https://github.com/apache/nuttx/pull/17868

   ## Summary
   
   **Fix incorrect include directory order in CMake application configuration**
   
   This is a bugfix for include path handling in NuttX CMake build system. The 
commits address a critical issue where private (application-provided) include 
directories were not taking precedence over public library includes, contrary 
to expected behavior and user intent.
   
   ### Problem
   
   Previously, the CMake code processed include directories in a loop without 
the `BEFORE` keyword:
   ```cmake
   foreach(inc ${INCLUDE_DIRECTORIES})
     target_include_directories(${TARGET} PRIVATE ${inc})
   endforeach()
   ```
   
   This caused include directories to be appended to the end of the search 
path, allowing library headers to shadow user-provided headers. The correct 
behavior is: **private includes should always take precedence over public 
includes**.
   
   ### Solution
   
   The fix consolidates the loop into a single call with the `BEFORE` keyword:
   ```cmake
   target_include_directories(${TARGET} BEFORE PRIVATE ${INCLUDE_DIRECTORIES})
   ```
   
   This ensures:
   1. **Correct precedence**: Private includes (first on path) > public library 
includes
   2. **Preserves user order**: The exact order users specified in 
`INCLUDE_DIRECTORIES` is maintained
   3. **CMake best practice**: Uses single target call instead of loop for 
better performance and clarity
   
   ### Root Cause
   
   The bug was introduced because the loop-based approach didn't account for 
the fact that without `BEFORE`, CMake appends paths instead of prepending them. 
This meant that library include paths (added via `PUBLIC` properties) would be 
searched before application-local includes, breaking header precedence.
   
   ### Testing Impact
   
   - Applications that rely on local header overrides now work correctly
   - No breaking changes for properly-configured applications
   - Fixes mysterious header shadowing issues where wrong versions of headers 
were being included
   
   ## Impact
   
   - **Build correctness**: Critical fix ensuring correct header precedence 
rules
   - **Application compatibility**: No breaking changes; fixes non-standard 
behavior
   - **Developer experience**: Removes confusing header shadowing bugs where 
developers couldn't override library headers locally
   - **CMake conformance**: Aligns with standard CMake include path semantics


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to