Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a header in source with cmake?

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

like image 223
amso Avatar asked Nov 19 '10 12:11

amso


People also ask

How do I add a header in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

How do I build from source CMake?

In order to build CMake from a source tree on Windows, you must first install the latest binary version of CMake because it is used for building the source tree. Once the binary is installed, run it on CMake as you would any other project.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is Cmake_source_dir?

The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .


1 Answers

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})
like image 66
tibur Avatar answered Oct 11 '22 09:10

tibur