为什么变量不能在CMake的脚本中访问? [英] Why variables are not accessed inside script in CMake?

查看:114
本文介绍了为什么变量不能在CMake的脚本中访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 install_copy_dlls.cmake 的脚本,该脚本被调用以从顶级cmake文件执行,如下所示。


$ b

此外,我有一个名为的变量。



INSTALL(SCRIPTinstall_copy_dlls.cmake USE_OSG_STATIC ,如果我使用静态编译的OpenSceneGraph设置为ON,如果我使用动态编译的OpenSceneGraph设置为OFF。



我需要在install_copy_dlls.cmake脚本中使用这个变量。



因此,这里是install_copy_dlls.cmake文件应该是什么样子。



复制其他必需的dll ...



if(NOT USE_OSG_STATIC)// if dynamic OSG



复制osg dlls



在这里,我尝试使用message打印USE_OSG_STATIC变量它不打印任何东西。



任何人都可以解释为什么我无法在脚本文件中使用变量?

解决方案

 任何人都可以解释为什么我不能在脚本文件中使用变量? 

install(SCRIPT ...) cmake -P 这样工作。所以没有变量从父脚本转移
到孩子(直到你明确定义一个):

  cat run.cmake 
if(A)
message(A:$ {A})
else()
message(A is empty)
endif()
> cmake -P run.cmake
A is empty
> cmake -DA = 15 -P run.cmake
A:15

使用 CMakeLists.txt

 > cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)
set(A 43)
execute_process(COMMAND $ {CMAKE_COMMAND} -P run.cmake)
> cmake -H。 -B_builds
A为空

转到子进程:

 > cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)
set(A 43)
execute_process(COMMAND $ {CMAKE_COMMAND} -DA = $ {A} -P run.cmake)
> cmake -H。 -B_builds
A:43



解决方案#1
$ b

使用 install(CODE ...)命令可以为定义变量run.cmake script:

 > cat CMakeLists.txt 
install(
CODE
execute_process(
COMMAND
$ {CMAKE_COMMAND}
-DA = $ {A}
-P
$ {CMAKE_CURRENT_LIST_DIR} /run.cmake


> cmake -H。 -B_builds -DA = 554
> cmake --build _builds --target install
安装项目...
- 安装配置:
A:554
pre>

解决方案2(配置)



您可以使用configure_file 命令:

 > cat run.cmake.in 
set(A @ A @)

if(A)
message(A:$ {A})
else )
消息(A is empty)
endif()
> cat CMakeLists.txt
set(custom_script $ {PROJECT_BINARY_DIR} /custom_install_scripts/run.cmake)
configure_file(run.cmake.in $ {custom_script} @ONLY)
install(SCRIPT $ {custom_script })
> cmake -H。 -B_builds -DA = 42
> cmake --build _builds --target install
安装项目...
- 安装配置:
A:42
pre>

I have a script called "install_copy_dlls.cmake", which is called to execute from top level cmake file as shown below.

INSTALL(SCRIPT "install_copy_dlls.cmake")

And, I have a variable named "USE_OSG_STATIC" which is set to ON if I use Statically compiled OpenSceneGraph and set of OFF if I use Dynamically compiled OpenSceneGraph.

I need to use this variable inside install_copy_dlls.cmake script.

so, here is how install_copy_dlls.cmake file should look like.

copy other required dlls...

if(NOT USE_OSG_STATIC) //if dynamic OSG

copy osg dlls

here, I try to use "message" to print USE_OSG_STATIC variable and it doesn't print anything.

Can anyone explain me why I can not use variables in Script file?

解决方案

Can anyone explain me why I can not use variables in Script file?

install(SCRIPT ...) command works like cmake -P. So there is no variables forwarded from parent script to child (until you explicitly define one):

> cat run.cmake 
if(A)
  message("A: ${A}")
else()
  message("A is empty")
endif()
> cmake -P run.cmake 
A is empty
> cmake -DA=15 -P run.cmake 
A: 15

Using CMakeLists.txt:

> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(A 43)
execute_process(COMMAND ${CMAKE_COMMAND} -P run.cmake)
> cmake -H. -B_builds
A is empty

Forward to child process:

> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(A 43)
execute_process(COMMAND ${CMAKE_COMMAND} -DA=${A} -P run.cmake)
> cmake -H. -B_builds 
A: 43

Solution #1 (forwarding)

Using install(CODE ...) command you can define variable for run.cmake script:

> cat CMakeLists.txt
install(
    CODE
    "execute_process(
        COMMAND
        ${CMAKE_COMMAND}
        -DA=${A}
        -P
        ${CMAKE_CURRENT_LIST_DIR}/run.cmake
    )"
)
> cmake -H. -B_builds -DA=554
> cmake --build _builds --target install
Install the project...
-- Install configuration: ""
A: 554

Solution #2 (configuring)

You can configure install script using configure_file command:

> cat run.cmake.in 
set(A @A@)

if(A)
  message("A: ${A}")
else()
  message("A is empty")
endif()
> cat CMakeLists.txt    
set(custom_script ${PROJECT_BINARY_DIR}/custom_install_scripts/run.cmake)
configure_file(run.cmake.in ${custom_script} @ONLY)
install(SCRIPT ${custom_script})
> cmake -H. -B_builds -DA=42
> cmake --build _builds --target install
Install the project...
-- Install configuration: ""
A: 42

这篇关于为什么变量不能在CMake的脚本中访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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