11

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)
  set(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.

Hassan Syed
  • 20,075
  • 11
  • 87
  • 171

2 Answers2

11

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)

uelordi
  • 2,189
  • 3
  • 21
  • 36
11

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.

tibur
  • 11,531
  • 2
  • 37
  • 39
  • yep that did the trick,thanks. Do you have any link to a part of documentation that covers the rules and semantics of variables / properties /functions and macros ? I generally use the man page docummentation online -- but that generally omits such details. – Hassan Syed Jul 13 '11 at 12:56