CMake参数如何转发到ExternalProject [英] How can CMake arguments be forwarded to ExternalProject

查看:175
本文介绍了CMake参数如何转发到ExternalProject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(大部分)我已经成功地为googletest设置了ExternalProject_Add。但是,我注意到诸如我选择的C ++编译器,构建类型等之类的信息不会自动转发到ExternalProject。

I have (mostly) successfully set up ExternalProject_Add for googletest. However, I noticed that things like my choice of C++ compiler, build type, etc. are not automatically forwarded to the ExternalProject.

我可以通过添加来轻松添加任何给定标志如下所示,将其发送给CMAKE_ARGS:

I can easily add any given flag by adding it to CMAKE_ARGS in the call to ExternalProject_Add like so:

CMAKE_ARGS -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}

但是,这要求我枚举应该转发给googletests的CMake调用的所有可能参数,并且该列表很漂亮巨大。我还需要为我想要的所有其他ExternalProject_Add创建相同的列表。

However, this requires that I enumerate all of the possible arguments that should be forwarded to googletests's CMake invocation, and that list is pretty enormous. I would also need to create that same list for every other ExternalProject_Add I wanted. That seems fragile and error prone.

有没有办法告诉CMake转发用户提供的配置?换句话说,如果我以以下方式调用CMake:

Is there a way to tell CMake to "forward" the configuration that the user provided along? In other words, if I invoked CMake as:

cmake <path-to-project> -DCMAKE_C_COMPILER=/usr/bin/clang -DSOME_RANDOM_FLAG=stuff

然后我想打电话给ExternalProject_Add为 SOME_RANDOM_FLAG 提供相同的编译器选择和值,而无需则需要明确列出这些名称。我不确定简单地传递CMake的ARGV是否可行,因为说

Then I would like my call to ExternalProject_Add to provide the same compiler selection and value for SOME_RANDOM_FLAG, without needing to explicitly list those names. I'm not sure that simply passing CMake's ARGV along would work, since saying

CC=/usr/bin/clang cmake <path-to-project>

也很理想。

对如何完成此操作有任何想法吗?

Any thoughts on how to accomplish this?

推荐答案

经过长时间的努力,终于找到答案了(Don回答了Hinton)在CMake邮件列表中。 Fraser的解决方案非常接近,但是仍然可以传递一些项目特定的参数,这些参数可能导致某些不可预测的行为。

After long hours of trying to figure this out, it was finally answered (by Don Hinton) in the CMake mailing list. Fraser's solution is very close, but can still pass some project specific arguments that can cause some unpredictable behavior.

以下内容可以正常使用。希望这将节省人们尝试解决此问题的时间:

The following works properly. Hopefully this will save people some time trying to figure this out:

cmake_minimum_required(VERSION 3.1)

# MUST be done before call to 'project'
get_cmake_property(vars CACHE_VARIABLES)
foreach(var ${vars})
  get_property(currentHelpString CACHE "${var}" PROPERTY HELPSTRING)
    if("${currentHelpString}" MATCHES "No help, variable specified on the command line." OR "${currentHelpString}" STREQUAL "")
        # message("${var} = [${${var}}]  --  ${currentHelpString}") # uncomment to see the variables being processed
        list(APPEND CL_ARGS "-D${var}=${${var}}")
    endif()
endforeach()

project(SuperBuild)

include(ExternalProject)

ExternalProject_Add(ext_proj
  ...

  CMAKE_ARGS ${CL_ARGS}
)

链接到邮件列表线程: https://cmake.org /pipermail/cmake/2018-January/067002.html

Link to mailing list thread: https://cmake.org/pipermail/cmake/2018-January/067002.html

这篇关于CMake参数如何转发到ExternalProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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