是否有一个通用的方法禁用在cmake可执行目标 [英] Is there a generic way for disabling executable targets in cmake

查看:288
本文介绍了是否有一个通用的方法禁用在cmake可执行目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我们的CMake构建系统,我构建了一些库和一些可执行文件。



现在的问题是我有一个VS2010和一个VS2008工具链,但我只需要VS2008工具链VS2008库。输出可执行文件是两个工具链的win32目标,所以我只需要通过VS2010工具链一次构建可执行文件,而VS2008工具链应该只跳过可执行文件,并只构建所需的库(这会节省构建时间)。



CMake基本脚本和整体设置也可能会在未来交付给客户,所以如果在CMake中有一种方法来禁用某些目标,像所有可执行文件一般的方式。否则我必须写多个大IF(BUILD_EXECUTABLES)... ENDIF()构造我的可执行文件设置在我的CMakeLists.txt,没有CMake时给我错误,当我忘记他们。



构建通过一些批处理文件触发。理想情况下,我想通过-D选项传递一个变量到cmake(例如-D BUILD_EXECUTABLES = false)



我试图包装ADD_EXECUTABLE宏,但不是工作,因为我有像
TARGET_LINK_LIBRARIES调用,然后抱怨不存在的目标。



我也可以设置输出目录到一些垃圾文件夹,可以删除,但是(如已经提到的)不会节省构建时间(我们有一个相当大的项目)



任何想法如何以干净和通用的方式完成? p>

解决方案

CMake目标有两个属性,控制目标是否默认构建。第一个是 EXCLUDE_FROM_ALL 。它指示目标是否从默认构建目标中排除。对于Makefile生成器,键入 make 将不会触发其 EXCLUDE_FROM_ALL 属性设置为1的目标的构建。 p>

另一个是 EXCLUDE_FROM_DEFAULT_BUILD ,仅适用于Visual Studio生成器。如果设置为1,当调用Build Solution菜单命令时,目标不会成为默认构建的一部分。



您可以设置两者的值可执行目标的属性取决于选项 BUILD_EXECUTABLES

  if BUILD_EXECUTABLES)
set_target_properties(exe1 exe2 PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
endif()


With our CMake build system I build some libraries and some executables. The build products are all output to a specific folder.

Now the problem is I have a VS2010 and a VS2008 toolchain but I only need the VS2008 toolchain for VS2008 libraries. The output executables are win32 target for both toolchains so I only need to build the executables once via the VS2010 toolchain while the VS2008 toolchain should just skip the executables and build only the desired libraries (which saves build time).

The CMake base scripts and overall setup may also be delivered to customers in the future so it would be very nice if there is a way in CMake to disable certain targets, like all executables in a generic way. Otherwise i have to write many big IF( BUILD_EXECUTABLES ) ... ENDIF() constructs around my executables setup in my CMakeLists.txt, without CMake giving me errors when I forget them.

The build is triggered via some batch files. Ideally i want to pass a variable to cmake via the -D option (e.g. "-D BUILD_EXECUTABLES=false")

I tried to wrap the ADD_EXECUTABLE macros but that does not work since I have calls like TARGET_LINK_LIBRARIES which then complain about the nonexistent target.

I could also set the output directory to some garbage folder which could be deleted afterwards, but that (as already mentioned) would not save build time (We have a quite huge project)

Any ideas how to accomplish that in clean and generic way?

解决方案

CMake targets have two properties which control if a target is built by default. The first one is EXCLUDE_FROM_ALL. It indicates if the target is excluded from the default build target. For Makefile generators, typing make will not trigger a build of a target whose EXCLUDE_FROM_ALL property is set to 1.

The other one is EXCLUDE_FROM_DEFAULT_BUILD and only applies to Visual Studio generators. If it is set to 1, the target will not be part of the default build when you invoke the "Build Solution" menu command.

You could set the values of both properties for executable targets depending on the option BUILD_EXECUTABLES:

if (NOT BUILD_EXECUTABLES)
   set_target_properties(exe1 exe2 PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
endif()

这篇关于是否有一个通用的方法禁用在cmake可执行目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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