Skusku
Skusku

Reputation: 588

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

Upvotes: 0

Views: 309

Answers (1)

Martin Kopecký
Martin Kopecký

Reputation: 1010

I would propose to have common implementation in the root your source directory while having dedicated architecture specifics in arch directory. Then you can select appropirate architecture dependent code in root CMakeLists.txt using

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

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)

Upvotes: 1

Related Questions