title: Add files in sub folders to target
body: <p>I'd like to add output files in subdirectory to a build target.
  I wrote in <code>CMakeLists.txt</code> like below:</p>
  
  <pre><code>file(GLOB srcfiles "src/*.txt")
  add_custom_target(subtask ALL)
  set(dest_dir ${PROJECT_SOURCE_DIR}/sub/)
  foreach(srcfile ${srcfiles})
    string(REGEX REPLACE "^.*/(.*).txt$" filename ${srcfile})
    add_custom_command(OUTPUT ${dest_dir}/${filename}.txt
      COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${dest_dir}
      COMMAND ${PROJECT_SOURCE_DIR}/process.sh ARGS ${srcfile}
      MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/process.sh)
      add_dependencies(subtask ${dest_dir}/${filename}.txt)
  endforeach(srcfile)
  </code></pre>
  
  <p>And executed:</p>
  
  <pre><code>mkdir build &amp;&amp; cmake .. &amp;&amp; make
  </code></pre>
  
  <p>But <code>sub/*.txt</code> are not created after build.</p>
  
  <p>How should I do to build all commands on build?</p>
  
  <p><strong>updated (2017/2/4)</strong></p>
  
  <p>I solved the issue: use <code>add_custom_command</code> for each target and then declare <code>add_custom_target</code> that depends on all targets of <code>add_custom_command</code>.</p>
  
  <pre><code>set(TARGET_FILES "")
  file(GLOB SRC_FILES "src/*.txt")
  foreach(SRC_FILE ${SRC_FILES})
    string(REGEX REPLACE "^.*/(.*).txt$" "\\1-foo.txt" TARGET_FILE ${SRC_FILE})
    add_custom_command(
      OUTPUT ${PROJECT_SOURCE_DIR}/sub/${TARGET_FILE}
      COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${PROJECT_SOURCE_DIR}/sub
      COMMAND ${PROJECT_SOURCE_DIR}/process.sh ARGS ${SRC_FILE}
      MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/process.sh)
    list(APPEND TARGET_FILES "${PROJECT_SOURCE_DIR}/sub/${TARGET_FILE}")
  endforeach()
  add_custom_target(foo_txt ALL DEPENDS ${TARGET_FILES})
  </code></pre>