Reputation: 20485
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.
Upvotes: 11
Views: 7851
Reputation: 2209
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:
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)
Upvotes: 11
Reputation: 11636
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.
Upvotes: 11