简体   繁体   English

具有 CMake 的驱动程序的多个交叉编译目标

[英]Multiple cross-compilation targets for drivers with CMake

I am developing a driver for a hardware module and I want to make it as portable as possible while using CMake.我正在开发一个硬件模块的驱动程序,我希望在使用 CMake 时使其尽可能便携。 Are there best practices to achieve this?是否有最佳实践来实现这一目标?

There is common code that should be used in all platforms and platform-dependent code.有应该在所有平台和平台相关代码中使用的通用代码。 What directory structure fits this use case?什么样的目录结构适合这个用例? How do I structure the CMakeLists.txt files?如何构建 CMakeLists.txt 文件?

I would propose to have common implementation in the root your source directory while having dedicated architecture specifics in arch directory.我建议在源目录的根目录中使用通用实现,同时在 arch 目录中使用专用的架构细节。 Then you can select appropirate architecture dependent code in root CMakeLists.txt using然后,您可以使用 select 在根CMakeLists.txt中使用适当的体系结构相关代码

add_subdirectory( ${PROJECT_SOURCE_DIR}/arch/${CMAKE_TARGET_PROCESSOR} )

Samle project structure:示例项目结构:

source/arch/arm/platform_dependent_code.cpp
source/arch/arm/CMakeLists.txt
source/arch/x86/platform_dependent_code.cpp
source/arch/x86/CMakeLists.txt
source/common_code.cpp
source/CMakeLists.txt <-- to contain add_subdirectory( ${PROJECT_SOURCE_DIR}/arch/${CMAKE_TARGET_PROCESSOR} )

Once CMAKE_TARGET_PROCESSOR CMake variable is set to arm , then only arm related code is built aside of common one, or once x86 is set, x86 and common code is used... the add_subdirectory(...) command does the magic for you Once CMAKE_TARGET_PROCESSOR CMake variable is set to arm , then only arm related code is built aside of common one, or once x86 is set, x86 and common code is used... the add_subdirectory(...) command does the magic for you

I am not sure how you handle the toolchains in multiple cross compilation but I would propose to use CMake toolchain files where CMAKE_TARGET_PROCESSOR shall be set... (see https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/CrossCompiling )我不确定您如何在多个交叉编译中处理工具链,但我建议使用 CMake 工具链文件,其中CMAKE_TARGET_PROCESSOR应设置...(请参阅https://gitlab.kitware.com/cmake/community/-/wikis/ doc/cmake/交叉编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM