Search code examples
c++cmakecompilationbuild-system

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


Solution

  • 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.