7

In my project I have a "schema" file and utility that I wrote to generate a header file. I'm using cmake and out of source build to build the application.

Currently I have to regenerate the header file by hand then build the application.

Then I came up with this CMakeLists.txt statements, but they generate the header in the build directory instead of in the source directory.

configure_file( generator.pl generator COPYONLY )
configure_file( schema.txt.in schema.txt COPYONLY )
add_custom_command(
    OUTPUT generated.h
    COMMAND ./generator schema.txt generated.h
    DEPENDS mib_schema.txt.in generator.pl
    COMMENT "Regenerating header file..."
)

Is it possible to generate the header in the source directory?

edit (to reflect the answer):

The file can be reached directly by fully qualifying its path with either

${CMAKE_CURRENT_SOURCE_DIR}

or:

${CMAKE_CURRENT_BINARY_DIR}

So, to generate the header in my source dir, the previous excerpt from CMakeLists.txt becomes:

add_custom_command(
    OUTPUT generated.h
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generator.pl ${CMAKE_CURRENT_SOURCE_DIR}/schema.txt.in ${CMAKE_CURRENT_SOURCE_DIR}/generated.h
    DEPENDS mib_schema.txt.in generator.pl
    COMMENT "Regenerating header file..."
)

which is actually simpler. Thanks

--to

amso
  • 514
  • 1
  • 6
  • 14
  • This is actually a good idea. If I can save the generated file in the source directory it gets included in the source package which is exactly what I want (because that way users of my library do not need to generate the file as long as they don't modify the generator and/or the source data file.) This means more people can compile my library! – Alexis Wilke Feb 20 '13 at 05:39

1 Answers1

8

I think that generated header are well placed in the binary directory, since you might want to create to build directories with the same source and different configurations resulting in different header generated.

You might want to include the build directory to your project:

include_directories(${CMAKE_CURRENT_BINARY_DIR})
tibur
  • 11,531
  • 2
  • 37
  • 39
  • If CMake has trouble finding your file, then fully qualify them with `${CMAKE_CURRENT_BINARY_DIR}`. If you are sure that you want to create your files in the source directory, you can even use `${CMAKE_CURRENT_SOURCE_DIR}` in your `configure` and `add_custom_command` commands. – tibur Nov 19 '10 at 12:50
  • I agree, the best place for generated files is in the build directory. This header is convenient in the source directory because it gives useful code completion while coding. It is the same header for all build configurations, so having it in the source directory is not harmfull at all. NB: I'm having problems editing comments, probably because I'm on a large network behing a unique IP. Sorry for the confusion. – amso Nov 19 '10 at 13:56