[CMake] Question about generate a source file during the build

Sébastien Barthélémy barthelemy at crans.org
Sun Feb 3 15:37:28 EST 2013


On Sun, Feb 3, 2013 at 2:12 PM, Olaf Peter <ope-devel at gmx.de> wrote:
> Hi Sébastien,
>
>
>>  > ---8<---
>>  > project(coolcoding)
>>  >
>>  > # generate coolcoding's static lexer
>>  > set(COOLCODING_BAR_HPP
>> ${PROJECT_BINARY_DIR}/include/foo/io/coolcoding/coolcoding_bar.hpp)
>>  >
>>  > add_executable(generate_coolcoding_bar generate_coolcoding_bar.cpp)
>>  >
>>  > add_custom_command(
>>  >    OUTPUT  ${COOLCODING_BAR_HPP}
>>  >    COMMAND generate_coolcoding_bar ${COOLCODING_BAR_HPP}
>>  >    )
>>
>> You might want to add
>>
>> DEPENDS generate_coolcoding_bar
>>
>> to this command.
>>
>
> IMO the DEPENDS is only useful if you have an input file dependency, which
> isn't here the case.

Not only.
The line I suggested adds a dependency on the generator itself: if you
change the generator source file, it will get rebuilt, and thanks to
this DEPENDS rule, cmake will generate the files again.

> file(MAKE_DIRECTORY ${directory})
>
> did help, but is verbose since I use set(DIR ...) and set(HPP ${DIR}/...).
> It would be nice if cmake would know that there is a directory to create....

I know no better way than creating a small wrapper function to do
that, or letting you generator create the directory itself.

Cheers,


More information about the CMake mailing list