开发者

CMAKE: Creating and building a list within a function -- with directory, or global scope

I am not entirely familiar with the scoping rules of cmake. I need to buildup a list of various files whilst doing RPC code-generation for an IDL.

function(generate_rpc file_name)
  se开发者_开发问答t(PROTO_FILES ${PROTO_FILES} ${file_name})
endfunction(generate_rpc)

generate_rpc(BasicProtocol.proto)
generate_rpc(dummy.proto)

message(STATUS "PROTO FILES: ${PROTO_FILES}")

The list is empty each time. I need append-able list that can be built from within a function.


Using a macro instead of a function seems to do it:

macro(generate_rpc file_name)
  set(PROTO_FILES ${PROTO_FILES} ${file_name})
endmacro(generate_rpc)

EDIT: According to http://www.cmake.org/cmake/help/syntax.html (should be in the man page, IMO):

CMake functions create a local scope for variables, and macros use the global scope.


Although Macros are defined and called in the same manner as functions, there are some differences between them, for example in SCOPE and when its execution.

SCOPE:

  • Macro: has global scope.
  • Function: has a local scope whether you don't specify.

EXECUTION: it works like C++ or C

  • Macro: the variable names are replaced to strings before configuring.

  • Function: the variable names are replaced during execution.

In conclusion, add the PARENT_SCOPE flag in set command

set(PROTO_FILES ${PROTO_FILES} ${file_name} PARENT_SCOPE)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜