title: cmake, lost in the concept of global variables (and PARENT_SCOPE or add_subdirectory alternatives)
body: <p>I have a cmake project in which I have some modules and I'm using Find-*.cmake for including the shared modules in the application.
  For not taking in account every module that I add, I have defined a kind of global <code>LIB</code> variables tor the linker:</p>
  
  <pre><code># inside a Find-*.cmake or in the CMakeLists.txt of the modules:
  set(LIB ${LIB} ...)
  </code></pre>
  
  <p>so in one of the final applications that uses some modules I can just do:</p>
  
  <pre><code>target_link_libraries(${APP_NAME} ${LIB})
  </code></pre>
  
  <p>Then, I'd like to have the compiled modules in the <code>/project_path/modules/foo/build</code> so that if a module is really big to compile it can be compiled once for all the application that use it. The way I'm achieving this is to load the CMakeLists.txt of the module from the Find-*.cmake in this way:</p>
  
  <pre><code># Inside FindFoo.cmake, for loading /project_path/modules/foo/CMakeLists.txt
  # and compile it in /project_path/modules/foo/build
  add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME} 
                   ${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME}/build
  )
  include_directories(${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME}/include)
  </code></pre>
  
  <p>But it happened sometimes that some module require another modules so that the <code>add_subdirectory</code> creates new scopes and can correctly load <code>LIB</code> but cannot write it (when I use <code>set</code> it is in the deeper scope and not changes the upper scope). For bypass this I have to add <strong>PARENT_SCOPE</strong> in the <code>set</code>).. So I have tried to add it in some module that I think could be nested and hidden in some dependencies but compiling all the application I suddenly faced with:</p>
  
  <pre><code>CMake Warning (dev) at /path_to_repo/cmake/FindFooX.cmake:6 (set):
    Cannot set "LIB": current scope has no parent.
  Call Stack (most recent call first):
    CMakeLists.txt:14 (find_package)
  This warning is for project developers.  Use -Wno-dev to suppress it.
  </code></pre>
  
  <p>I am afraid that this can change from app to app in respect to which module I need or in respect of the dependecies tree in the modules itself so I'm looking for a cleaner solution.</p>