CMake:以二进制格式存储内部版本/系统信息 [英] CMake: Store build/system information in binary

查看:86
本文介绍了CMake:以二进制格式存储内部版本/系统信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何存储基本信息,例如系统名称( Linux ),使用的编译器 gcc )或体系结构( x86_64 ),放在我包含在二进制文件中的文件中?

How do I store basic information, like system name (Linux), used compiler gcc) or architecture (x86_64), in a file that I include into my binary?

用于编写这样的 .in 文件已经存在,除了相关的CMake变量全部为空,例如 CMAKE_SYSTEM CMAKE_SYSTEM_PROCESSOR

All the code for writing such a .in file is already there, except that the relevant CMake variables are all empty, e.g. CMAKE_SYSTEM or CMAKE_SYSTEM_PROCESSOR:

set(NEWLINE "\\\\n")
file(WRITE ${CMAKE_BINARY_DIR}/update_buildflags.cmake "
file(WRITE \${DST} \"#define BUILDFLAGS \\\" 
     ARCH=\${CMAKE_SYSTEM}${NEWLINE} 
     COMP=\${CMAKE_SYSTEM_PROCESSOR}${NEWLINE} 
     BUILD=\${CMAKE_BUILD_TYPE}${NEWLINE}\\\"\n\")")    
add_custom_target(update_buildflags COMMAND ${CMAKE_COMMAND}
     -DDST=${PROJECT_SOURCE_DIR}/src/buildflags.c
     -P ${CMAKE_BINARY_DIR}/update_buildflags.cmake)

此自定义然后将target作为主要二进制文件的依赖项添加,结果文件 src / buildflags.c 看起来像这样:

This custom target is then added as dependency for the main binary and the resulting file src/buildflags.c looks like this:

#define BUILDFLAGS " ARCH=\n COMP=\n BUILD=\n"

为什么使用的CMake变量为空,尽管使用 message()打印它们显示了co

Why are the used CMake variables empty although printing them with message() shows the correct information?

CMAKE_SYSTEM: Linux-4.4.0-92-generic


推荐答案

您可以使用模板文件使其更具可读性。

You can use an template file to make it more readable.


  • Pro:无需在cmake中写入整个文件

  • Pro:易于编辑

  • :附加文件

示例src / autocode / buildflags.h.in:

Example src/autocode/buildflags.h.in:

#ifndef BUILD_FLAGS_H
#def BUILD_FLAGS_H  
#define ARCH ${CMAKE_SYSTEM}
#define COMP ${CMAKE_SYSTEM_PROCESSOR}
#define BUILD ${CMAKE_BUILD_TYPE} 
// Add more of your stuff here
#define APP_BUILD_WITH_CMAKE_VERSION "${CMAKE_VERSION}"
#define APP_BUILD_ON_PLATFORM "${CMAKE_SYSTEM}"
#endif // BUILD_FLAGS_H

在您的CMakeLists.txt

In your CMakeLists.txt

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/autocode/buildflags.h.in 
               ${CMAKE_BINARY_DIR}/buildflags.h)
include_directories(${CMAKE_BINARY_DIR})

在您的代码中

#include "buildflags.h"

这篇关于CMake:以二进制格式存储内部版本/系统信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆