CMake变量管理让我疯狂......
我想调用一个函数,该函数调用另一个函数来修改一个最终将成为要写入的文件内容的变量。
看起来像这样:
function( QMAKE_ADD_LINE_TO_VAR var line )
set( ${var} "${${var}}\n${line}\n" PARENT_SCOPE )
endfunction()
function( QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR var relline debugline )
QMAKE_ADD_LINE_TO_VAR( ${var} "release:${relline} (two functions call)" )
QMAKE_ADD_LINE_TO_VAR( ${var} "debug:${debugline} (two functions call)" )
endfunction()
QMAKE_ADD_LINE_TO_VAR(FILE_CONTENT "#Generated by CMake scripts!")
QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR( FILE_CONTENT "foo" "bar" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "release:foo (single function call)" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "debug:bar (single function call)" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "EOF" )
file(WRITE myFile.txt ${FILE_CONTENT} )
这会生成myFile.txt:
#Generated by CMake scripts!
release:foo (single function call)
debug:bar (single function call)
EOF
我期待的时候:
#Generated by CMake scripts!
release:foo (two functions call)
debug:bar (two functions call)
release:foo (single function call)
debug:bar (single function call)
EOF
最有可能的问题是(set ... PARENT_SCOPE)
将变量设置在QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR
中,而不是在其父范围内...
我尝试将functions
替换为macros
,然后我得到了正确的输出,但引入了一个带有特殊字符管理的错误(How to escape accolade characters in CMake)。所以这对我来说不是一个可以接受的解决方案。
我尝试使用缓存变量,如下所示:
function( QMAKE_ADD_LINE_TO_VAR var line )
set( ${var} "${${var}}\n${line}\n" CACHE INTERNAL "" FORCE )
endfunction()
function( QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR var relline debugline )
QMAKE_ADD_LINE_TO_VAR( ${var} "release:${relline} (two functions call)" )
QMAKE_ADD_LINE_TO_VAR( ${var} "debug:${debugline} (two functions call)" )
endfunction()
set( FILE_CONTENT "" CACHE INTERNAL "" FORCE )
QMAKE_ADD_LINE_TO_VAR(FILE_CONTENT "#Generated by CMake scripts!")
QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR( FILE_CONTENT "foo" "bar" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "release:foo (single function call)" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "debug:bar (single function call)" )
QMAKE_ADD_LINE_TO_VAR( FILE_CONTENT "EOF" )
file(WRITE myFile.txt ${FILE_CONTENT} )
这很有效,但是,第二次调用CMake时,由于CMakeCache.txt
内容无效,我收到很多错误。
任何想法,无论是使用全新的解决方案,还是以更好的方式使函数或缓存变量工作的方法都会受到欢迎。
答案 0 :(得分:1)
在PARENT_SCOPE
中添加另一个QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR
作业,即:
function( QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR var relline debugline )
QMAKE_ADD_LINE_TO_VAR( ${var} "release:${relline} (two functions call)" )
QMAKE_ADD_LINE_TO_VAR( ${var} "debug:${debugline} (two functions call)" )
set( ${var} "${${var}}" PARENT_SCOPE )
endfunction()
答案 1 :(得分:0)
我找到的最好的替代方法是实际删除变量并直接从函数中写入文件(效率较低,但真正关心...):
function( QMAKE_ADD_LINE_TO_VAR file line )
file( APPEND ${file} "${line}\n" )
endfunction()
function( QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR file relline debugline )
QMAKE_ADD_LINE_TO_VAR( ${file} "release:${relline} (two functions call)" )
QMAKE_ADD_LINE_TO_VAR( ${file} "debug:${debugline} (two functions call)" )
endfunction()
file( WRITE myFile.txt "" )
QMAKE_ADD_LINE_TO_VAR(myFile.txt "#Generated by CMake scripts!")
QMAKE_ADD_RELEASE_AND_DEBUG_LINES_TO_VAR( myFile.txt "foo" "bar" )
QMAKE_ADD_LINE_TO_VAR( myFile.txt "release:foo (single function call)" )
QMAKE_ADD_LINE_TO_VAR( myFile.txt "debug:bar (single function call)" )
QMAKE_ADD_LINE_TO_VAR( myFile.txt "EOF" )