gpt4 book ai didi

c++ - 参数化自定义CMake工具链

转载 作者:行者123 更新时间:2023-12-02 10:00:26 25 4
gpt4 key购买 nike

我正在编写用于交叉编译的CMake工具链文件。
我有几个工具链,它们之间的差别很小。我想为cmake创建一个描述所有工具链的文件,并让用户从命令行指定它们:cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake -DTOOLCHAIN_NAME=<name>但是,似乎TOOLCHAIN_NAME有时变为空字符串。
这是工具链文件的示例:

set(tools /opt/toolchains/Custom/toolchains/toolchain)
set(sdk /opt/toolchains/Custom/platforms/)

message(STATUS "toolchain_name ${TOOLCHAIN_NAME}")
if(NOT TOOLCHAIN_NAME)
message(SEND_ERROR "Please specify toolchain name in -DTOOLCHAIN_NAME parameter")
endif()

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

# further processing
然后我叫CMake:
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain.cmake -DTOOLCHAIN_NAME=proc-os-gnueabi-gcc_6_3

并获得以下输出错误:
-- toolchain name proc-os-gnueabi-gcc_6_3
-- toolchain name proc-os-gnueabi-gcc_6_3
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is GNU 6.3.1
-- Check for working C compiler: /opt/toolchains/.../bin/arm-os-gnueabi-gcc
CMake Error at /home/user/project/cmake/toolchain.cmake:59 (message):
Please specify toolchain name in -DTOOLCHAIN_NAME parameter

Call Stack (most recent call first):
/home/user/project/build/CMakeFiles/3.15.4/CMakeSystem.cmake:6 (include)
/home/user/project/build/CMakeFiles/CMakeTmp/CMakeLists.txt:2 (project)


CMake Error at /usr/local/share/cmake-3.15/Modules/CMakeTestCCompiler.cmake:44 (try_compile):
Failed to configure test project build system.
Call Stack (most recent call first):
CMakeLists.txt:15 (project)


-- Configuring incomplete, errors occurred!
See also "/home/user/project/build/CMakeFiles/CMakeOutput.log".
See also "/home/user/project/build/CMakeFiles/CMakeError.log".
CMakeOutput.log和CMakeError.log包含消息,表示编译器未找到某些库。
如果打开生成的文件 /home/user/project/build/CMakeFiles/3.15.4/CMakeSystem.cmake,可以看到以下内容
set(CMAKE_HOST_SYSTEM "Linux-4.2.0-27-generic")
set(CMAKE_HOST_SYSTEM_NAME "Linux")
set(CMAKE_HOST_SYSTEM_VERSION "4.2.0-27-generic")
set(CMAKE_HOST_SYSTEM_PROCESSOR "x86_64")

include("/home/user/project/cmake/toolchain.cmake")

set(CMAKE_SYSTEM "Generic")
set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_SYSTEM_VERSION "")
set(CMAKE_SYSTEM_PROCESSOR "ARM")

set(CMAKE_CROSSCOMPILING "TRUE")

set(CMAKE_SYSTEM_LOADED 1)
根据CMake手册,其 -D参数用于指定缓存条目。
从上面的输出中可以得出结论,CMake至少处理了所提供的工具链文件3次。在前两次中,有适当的缓存条目,而在第三次运行中,它们不存在。
因此,如何避免工具链文件的代码重复?
UPD。阅读 this question之后,我尝试了CMake命令行参数的几种排列:在工具链之后但在选项之前指定源路径,在选项之后指定源路径,显式指定源路径并使用 -S-B选项构建路径。没有任何帮助。

最佳答案

I conclude from the output above that supplied toolchain file is processed by CMake at least 3 times. First two times there were proper cache entries, and at the third run they were absent.


是的,这是非常好的观察。从技术上讲,只有在 project()调用时才保证第一次工具链调用可以看到CACHE条目(使用 -Dcmake参数或在 set()调用之前使用 project()命令创建)。
在不同工具链调用之间传递CACHE条目的一种方法是将它们存储在 环境变量中:
下面的示例使用 MY_TOOLCHAIN_NAME环境变量作为 TOOLCHAIN_NAME CMake变量的存储。
if(DEFINED ENV{MY_TOOLCHAIN_NAME})
# Environment variable is set.
if (TOOLCHAIN_NAME)
# CMake variable is set too.
# It is up to your which one to use.
# Uncomment line below for prefer environment variable to CMake one.
# set(TOOLCHAIN_NAME $ENV{MY_TOOLCHAIN_NAME})
else ()
# CMake variable is not set. Use environment one.
set(TOOLCHAIN_NAME $ENV{MY_TOOLCHAIN_NAME})
endif()
else()
# Environment variable is not set.
if (TOOLCHAIN_NAME)
# But CMake variable is set.
# Store it into the environment and use it.
set(ENV{MY_TOOLCHAIN_NAME} ${TOOLCHAIN_NAME})
else()
# Neither environment nor CMake variable is set.
message(SEND_ERROR "Please specify toolchain name in -DTOOLCHAIN_NAME parameter")
endif()
endif()
如果您有多个“调整”变量,则可以为上述所有步骤编写一个宏,并为所需的每个变量应用(调用)此宏。

关于c++ - 参数化自定义CMake工具链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62795924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com