title: How do I iterate over all CMake targets programmatically?
body: <p>Is there a way to get all targets of a CMake project from within the top level <code>CMakeLists.txt</code>, i.e. iterate over the targets programmatically?</p>
  
  <p>The reason I want to do this is to apply some XCode specific settings to every target . . </p>
  
  <pre><code>if (CMAKE_GENERATOR MATCHES "Xcode")
      include(sanitize_xcode)
      sanitize_xcode(myTarget)
  endif()
  </code></pre>
  
  <p>FYI - the sanitization module looks like this . . </p>
  
  <pre><code>macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
      set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
  endmacro (set_xcode_property)
  
  macro (sanitize_xcode TARGET)
      set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=Debug] "YES")
      set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=MinSizeRel] "NO")
      set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=RelWithDebInfo] "YES")
      set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=Release] "NO")
  
      set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=Debug] "NO")
      set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=MinSizeRel] "YES")
      set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=RelWithDebInfo] "NO")
      set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=Release] "YES")
  
      set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=Debug] "0")
      set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=MinSizeRel] "s")
      set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=RelWithDebInfo] "3")
      set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=Release] "3")
  
      set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=Debug] "7.0")
      set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=MinSizeRel] "7.0")
      set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=RelWithDebInfo] "7.0")
      set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=Release] "7.0")
  endmacro (sanitize_xcode)
  </code></pre>