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

   ## Summary
   
   This comprehensive pull request implements a major feature for NuttX CMake 
build system: **ELF application build mode** with full support for:
   
   1. **KERNEL mode applications** - Apps loaded into kernel memory space
   2. **Loadable mode applications** (modules, `-m` flag) - Dynamically 
loadable ELF modules
   3. **DYNLIB mode applications** - Dynamic library support for 
position-independent executables
   
   ### Key Achievement
   Successfully refactored the CMake build system to support multiple 
application building models, enabling flexible application deployment 
strategies while maintaining backward compatibility.
   
   ---
   
   ## Solution Overview
   
   ### 1. Host Tools Directory Separation
   Renamed the host tools output directory from `CMAKE_BINARY_DIR/bin` to 
`CMAKE_BINARY_DIR/bin_host`:
   - **Purpose**: Reserve `bin` directory exclusively for cross-compiled 
application ELF files
   - **Benefit**: Clear distinction between:
     - `bin_host/` - Native build host tools (gencromfs, mksyscall, mkconfig, 
etc.)
     - `bin/` - Target platform application binaries
     - `bin_debug/` - Unstripped debug versions of applications
   
   ### 2. ELF Compilation & Linking Framework
   
   #### New Architecture-Specific Configuration
   Added `arch/*/src/cmake/elf.cmake` files for each architecture (ARM, ARM64, 
x86_64, SIM) with architecture-specific ELF compile and link options.
   
   #### Global ELF Target System
   Implemented `nuttx_global` custom target to centralize ELF-specific 
properties via generator expressions:
   - `NUTTX_ELF_APP_COMPILE_OPTIONS/NUTTX_ELF_APP_LINK_OPTIONS` - For 
KERNEL/LOADABLE apps
   - `NUTTX_MOD_APP_COMPILE_OPTIONS/NUTTX_MOD_APP_LINK_OPTIONS` - For 
modular/DYNLIB apps
   - `NUTTX_ELF_LINK_LIBRARIES/NUTTX_ELF_LINK_EXTRA_LIBRARIES` - Library 
linkage tracking
   
   ### 3. Application Build Mode Classification
   
   Three application build modes are now supported:
   
   **KERNEL Mode** (CONFIG_BUILD_KERNEL): Apps directly linked into kernel with 
all system libraries
   **LOADABLE Mode** (MODULE=m): Position-independent modules loaded at runtime 
via binfmt_elf
   **DYNLIB Mode** (DYNLIB=y): Dynamic library support with minimal system 
dependencies
   
   ### 4. Application Startup Object (crt0)
   
   Added `STARTUP_OBJS` auxiliary library in each architecture's common 
directory:
   ```cmake
   nuttx_add_aux_library(STARTUP_OBJS crt0.c)
   ```
   
   **Key features**:
   - Compiled with ELF-specific compile options from `nuttx_global`
   - Included in KERNEL and LOADABLE mode builds
   - Excluded from DYNLIB mode builds
   - Ensures consistent initialization across application types
   
   ### 5. CMake Extensions for ELF Configuration
   
   New functions in `cmake/nuttx_extensions.cmake`:
   - `nuttx_elf_compile_options()`, `nuttx_elf_compile_options_ifdef()` - ELF 
compile options
   - `nuttx_elf_link_options()`, `nuttx_elf_link_options_ifdef()` - ELF link 
options
   - `nuttx_mod_compile_options()`, `nuttx_mod_link_options()` - Modular/DYNLIB 
options
   - `nuttx_build_host_target()` - Build host tools target
   - `nuttx_add_elf_link_options()` - Global ELF link options
   
   ### 6. Application Link Generation
   
   Enhanced `cmake/nuttx_add_application.cmake` with sophisticated ELF linking:
   - Automatic build mode detection (KERNEL/LOADABLE/DYNLIB)
   - Mode-specific startup object and library linkage
   - Debug symbol preservation (`bin_debug/`) and release binary stripping 
(`bin/`)
   - Proper library linking:
     - KERNEL: Links all system + user libraries
     - LOADABLE: Links only extra libraries  
     - DYNLIB: Minimal library linking
   
   ### 7. Library Integration for ELF
   
   Updated library functions in `cmake/nuttx_add_library.cmake`:
   - All libraries depend on `apps_context` and feed into `apps_post` for 
correct build ordering
   - System and extra libraries automatically registered for ELF linking via 
`nuttx_global` target properties
   
   ### 8. Directory Structure Changes
   
   #### Output directories created:
   - `${CMAKE_BINARY_DIR}/bin/` - Stripped application ELF files (final)
   - `${CMAKE_BINARY_DIR}/bin_debug/` - Debug application ELF files (unstripped)
   - `${CMAKE_BINARY_DIR}/bin_host/` - Host tools binaries (native compilation)
   
   ---
   
   ## Implementation Details
   
   ### 13 Commits Summary
   
   1. **`44485d64ad4`** - Implements KERNEL mode in CMake build (foundation)
   2. **`1cbf0759a76`** - Rename host tools binary dir to host_bin
   3. **`6a022f0e9be`** - Use apps_post to identify library completion stage
   4. **`8083dda5310`** - Fix KERNEL mode ELF target duplication
   5. **`ba89f48da0c`** - Define ELF option and enable apps module build
   6. **`8bc4bf5afdc`** - Separate ELF output bin and bin_debug
   7. **`c0dac891468`** - Remove unused section in kernel ELF link
   8. **`f5b7645de76`** - Add x86_64 architecture loadable mod support
   9. **`25ffca9e36a`** - Fix sim loadable ELF build support
   10. **`3384f0fdc18`** - Support DYNLIB build mode
   11. **`760db1b6b1c`** - Unify CMake ELF link rule across architectures
   12. **`960b9d69f31`** - Fix ELF compile/link error with LTO enabled
   13. **`6a75931b8ac`** - Ensure ELF link depends on startup objects
   
   ---
   
   ## Feature Details
   
   ### 1. Multi-Mode Application Building
   
   **KERNEL Mode**: Apps with ELF visibility+hidden, architecture-specific 
flags, all system libraries, crt0 included
   **LOADABLE Mode**: PIC support, limited libraries, crt0 included
   **DYNLIB Mode**: PIC support, minimal libraries, NO crt0
   
   ### 2. Startup Object Management
   
   The `STARTUP_OBJS` auxiliary library:
   - Compiled with ELF-specific flags
   - Included in KERNEL and LOADABLE modes
   - Excluded from DYNLIB mode
   - Applied across all architectures
   
   ### 3. Automatic Library Discovery & Linking
   
   ELF applications automatically link with:
   - NUTTX_ELF_LINK_LIBRARIES: System/user libraries (KERNEL mode)
   - NUTTX_ELF_LINK_EXTRA_LIBRARIES: Extra libraries (all modes)
   
   ---
   
   ## Testing
   
   ### Build Mode Verification
   - Build apps with CONFIG_BUILD_KERNEL, MODULE=m, and DYNLIB=y flags
   - Verify correct compile/link flags applied for each mode
   - Verify directory structure: `bin_host/`, `bin_debug/`, `bin/`
   
   ### Multi-Architecture Testing
   - Test on ARM, ARM64, x86_64, and SIM architectures
   - Verify architecture-specific ELF options applied
   
   ### Application Loading
   - Test KERNEL mode at boot
   - Test dynamic loading of LOADABLE modules
   - Test DYNLIB execution
   
   ---
   ## Conclusion
   
   This implementation establishes NuttX CMake's capability to support multiple 
application deployment strategies through ELF building. The feature provides 
foundation for:
   
   - ✅ Kernel-integrated applications (CONFIG_BUILD_KERNEL)
   - ✅ Loadable modules (MODULE=m)
   - ✅ Dynamic libraries (DYNLIB=y)
   
   With clean separation, proper dependency management, and full backward 
compatibility.
   
   ---
   
   


-- 
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