Summary
During a cross-compile of compiler-rt for RISC-V, assembly files failed due to missing -march=rv64gc in compiler flags. The CMake configuration explicitly set CMAKE_C_FLAGS and CMAKE_CXX Desarrollo_FLAGS but didn’t propagate to .S assembly files, causing FPU instruction failures.
Root Cause
- CMake’s language-specific flag handling:
CMAKE_C_FLAGS/CMAKE_CXX_FLAGSapply to C/C++ sources only, not assembly (.Sfiles).- Assembly compilation requires
CMAKE_ASM_FLAGSfor architecture/target flags.
- Implicit behavior:
- Build systems like CMake often separate compiler drivers per language.
- When
clangcompiles assembly files, it defaults to the host’s target.
Why This Happens in Real Systems
- Heterogeneous codebases: Projects mixing C, C++, and assembly necessitate per-language flags.
- Toolchain abstraction: CMake abstracts toolchain details, requiring explicit configuration for each language.
- Legacy assumptions: Engineers assume
CMAKE_C_FLAGSpropagates universally, causing oversights.
Real-World Impact
Failure to build compiler-rt halts downstream toolchain workflows:
- Bootstrap failures: Inability to build critical runtime libraries like
libclang_rt. - Dead toolchains: RISC-V targets lacking FPU instructions become unusable.
- Debug paralysis: Non-obvious errors manifest at instruction-level diagnostics.
How Senior Engineers Fix It
- Explicitly set
CMAKE_ASM_FLAGS:set(CMAKE_ASM_FLAGS "-target riscv64-unknown-linux-gnu -march=rv64gc") - Unify flags via toolchain files:
set(CMAKE_COMMON_FLAGS "-target riscv64-unknown-linux-gnu -march=rv64gc") set(CMAKE_C_FLAGS "${CMAKE_COMMON_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_COMMON_FLAGS}") set(CMAKE_ASM_FLAGS "${CMAKE_COMMON_FLAGS}") - Override assembly extensions in CMake:
enable_language(ASM)
Why Juniors Miss It
- Mental model mismatch: Assuming
CMAKE_C_FLAGSaffects all compilation phases. - Lack of granularity awareness: Not distinguishing compilation drivers for C vs. assembly.
- Debugging shortcuts: Dependency on verbose builds (
make VERBOSE=1) for diagnosis instead of inspecting CMake internals. - Undocumented edge cases: Toolchain behaviors for niche architectures like RISC-V are rarely taught.
(腻code omitted intentionally:No executable example required beyond CMake snippets)