title: CMake: header-only library with generated files
body: <p>I have a library that needs to carry some constant data injected from the content of non-source files (in this case, OpenGL shader code). To achieve this, I'm using <code>add_custom_command()</code> to generate include files that I can then <code>#include</code> into my code to initialize const static variables.</p>
  
  <p>This works perfectly with regular libraries (static or shared), but now I'd like to make my library header-only. The ability of C++ to let static methods return static data without running the risk of having that data duplicated in each translation unit ("magic statics") makes this possible.</p>
  
  <p>The problem however is that CMake seems to assume that an INTERFACE library (which is the CMake feature that I'm using to create header-only libraries) does not need building - which, in this case, is wrong.</p>
  
  <p>(I realize that there is no actual obligation for my library to be header-only. In this particular case, the reason I want this is that I would like the library, which is doing OpenGL, to remain independent of any specific binding library [such as <code>GLEW</code> or <code>GLee</code> or the newcomer <code>glbinding</code>]. By keeping my library header-only, I can leave that choice to the user - all he needs to do is <code>#include</code> the header of the binding library before mine.)</p>
  
  <p>Does anyone see a way to have CMake trigger the header-generating custom commands, at the latest when the consumer project is being built?</p>
  
  <p>EDIT: I just realized that I could have the "best of both worlds" as it were by keeping my library static but still keeping all my code <em>except</em> for the constant data in the header files. That way, there would still be no need to choose a specific OpenGL binding library.
  However, there are still advantages to having a library be header-only - simplicity of use for one - so I'm leaving my question open.</p>
  
  <p>EDIT #2: Here is the relevant part of my <code>CMakeLists.txt</code> file (I only stripped the library dependencies - all header-only - from the end):</p>
  
  <pre><code>set(SHADER_FILES "src/vertex.glsl" "src/fragment.glsl")
  
  add_library(libGPCGUIGLRenderer INTERFACE)
  target_sources(libGPCGUIGLRenderer INTERFACE ${SHADER_FILES})
  
  target_include_directories(libGPCGUIGLRenderer BEFORE
    INTERFACE
      $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include&gt;
      $&lt;INSTALL_INTERFACE:include&gt;
  )
  
  # Embed shader files
  
  source_group("Shader files" FILES ${SHADER_FILES})
  
  set(GENERATED "${CMAKE_CURRENT_BINARY_DIR}/generated")
  target_include_directories(libGPCGUIGLRenderer INTERFACE ${GENERATED})
  
  # Find the GPC Bin2C utility
  find_package(GPCBin2C REQUIRED)
  
  # Add a custom target and a dependency for each shader file    
  foreach(shader ${SHADER_FILES})
    get_filename_component(name "${shader}" NAME)
    set(shader_header "${GENERATED}/${name}.h")
    add_custom_command(
      OUTPUT ${shader_header}
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${shader}
      COMMAND GPCBin2C --input=${CMAKE_CURRENT_SOURCE_DIR}/${shader} --output=${shader_header}
    )
    target_sources(libGPCGUIGLRenderer INTERFACE ${shader_header})
  endforeach()
  </code></pre>