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

Viewed 7088

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.

2 Answers

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)

Related