title: CMake generate C header via shell script
body: <p>I have a shell script that takes a JSON file in and outputs a <code>.h</code> file which one of my targets depends on. It would appear that CMake's <code>add_custom_command</code> is what I need to accomplish this, but I can't get the header file to generate. I have tried just about every combination I could think of using the information from <a href="https://stackoverflow.com/questions/4225005/how-to-generate-a-header-in-source-with-cmake">this post</a> and <a href="https://stackoverflow.com/questions/9827208/run-a-shell-command-ctags-in-cmake-and-make">this post</a>. </p>
  
  <p>Below is the simplest I could create to reproduce the issues I am encountering.</p>
  
  <p>My project structure is as follows:</p>
  
  <pre>
  .
  ├── CMakeLists.txt
  ├── main.c
  └── res
      ├── generate.sh
      └── input.json
  </pre>
  
  <h3>CMakeLists.txt</h3>
  
  <hr>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  project(test)
  
  set(TEST_DATA_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.h)
  add_custom_command(
      OUTPUT ${TEST_DATA_OUTPUT}
      COMMAND res/generate.sh h res/input.json ${TEST_DATA_OUTPUT}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      COMMENT "Generates the header file containing the JSON data."
  )
  
  # add the binary tree to the search path for include files so we
  # will fine the generated files
  include_directories(${CMAKE_CURRENT_BINARY_DIR})
  
  set(SRCS main.c)
  add_executable(test ${SRCS})
  </code></pre>
  
  <h3>main.c</h3>
  
  <hr>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include "test_data.h"
  
  int main(int argc, char** argv)
  {
      printf("%s\n", TEST_DATA);
      return 0;
  }
  </code></pre>
  
  <h3>res/generate.sh</h3>
  
  <hr>
  
  <pre><code>#!/bin/sh
  #
  # Converts the JSON to a C header file to be used as a resource file.
  
  print_usage()
  {
  cat &lt;&lt; EOF
  USAGE:
      $0 h INPUT
  
  DESCRIPTION:
      Outputs JSON data to another format.
  
  EOF
  }
  
  to_h()
  {
  cat &lt;&lt; EOF
  #ifndef TEST_DATA_H
  #define TEST_DATA_H
  
  static const char* TEST_DATA =
  "$(cat "$1" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/"\n"/g')";
  
  #endif // TEST_DATA_H
  EOF
  }
  
  case "$1" in
  h)
      if [ $# -eq 3 ] ; then
          to_h "$2" &gt; "$3"
      elif [ $# -eq 2 ] ; then
          to_h "$2"
      else
          echo "no input file specified" 1&gt;&amp;2
      fi
      ;;
  *)
      print_usage
      ;;
  esac
  
  exit 0
  </code></pre>
  
  <h3>res/input.json</h3>
  
  <hr>
  
  <pre><code>{
      "1": {
          "attr1": "value1",
          "attr2": "value2"
      },
      "2": {
          "attr1": "value1",
          "attr2": "value2"
      }
  }
  </code></pre>