如何在CMake中更改VS工具集

时间:2020-04-29 19:31:05

标签: c++ build cmake

我正在使用CMake构建项目。 我正在使用“ NMake Makefiles”作为生成器(VS生成器在使用更复杂的命令并将stdout重定向到文件时存在一些问题。)

我有静态库,必须使用VC2015,VC2017和VC2019工具集以不同的方式构建静态库。 整个项目是使用VC2019工具集构建的。

我有两个库,第一个是静态库,第二个是共享库。共享库被注入到静态库中,然后在运行时提取。注入需要配置library.h文件(静态库的一部分)。

现在,我需要以某种方式切换工具集,但是我找不到在CMake中执行此操作的方法。 我试图使用不同的二进制目录运行cmake,并使用不同的工具集仅构建一个目标,但是该目标取决于共享库,该库只能构建一次(使用VC2019)。

有没有一种方法可以只为一个目标切换工具集?

我的CMake代码如下:

add_library(static STATIC "") # this library has to be build using different toolsets (VC2015, VC2017, VC2019)
add_library(shared SHARED "") # this has to be build only once (using VC2019)

add_custom_target(library.h DEPENDS shared) # part of static library
configure_file(library.h.in library.h) # here I inject size & md5 sum of shared

add_dependency(static shared library.h)
target_sources(static PRIVATE library.h library.c)

# these two libraries should be build using VC2015 & VC2017 respectively
add_library(static_vc2017 STATIC "")
add_library(static_vc2015 STATIC "")
add_dependency(static_vc2017 shared library.h)
add_dependency(static_vc2015 shared library.h)

# injecting shared into static (same for static_vc2015 & static_vc2017)
add_custom_command(TARGET static POST_BUILD
  COMMAND
     $<TARGET_NAME:fileinjector> $<TARGET_NAME:static> $<TARGET_NAME:shared>
  DEPENDS
    fileinjector
)

CMake甚至有可能吗?我当时以为可以在CMake命令中将路径作为变量传递给共享库,然后依赖于构建它还是直接使用它,但是我正在寻找一个更好(更干净)的解决方案。

0 个答案:

没有答案
相关问题