3

I'm trying to generate a header with a custom command. The header should be updated on each rebuild, so that the source file which includes it would also be rebuilt. (Actual command is a script, but here is a simplified version.)

Here's my project:

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
set(test_SOURCES test.c)
include_directories("${CMAKE_BINARY_DIR}")

set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h")
message("VERSION_H_PATH: ${VERSION_H_PATH}")
add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND "touch" "${VERSION_H_PATH}")
#add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)

test.c

#include <version.h>

int main()
{
    return 0;
}

Now the problem is that the CMakeList.txt, as presented above, doesn't result in version.h being created at all. Only after I switch from add_custom_target to add_custom_command does it do. But then, if I change the file somehow, next make doesn't rebuild the project.

Looks like CMake doesn't recognize that test.c depends on version.h, although it does explicitly #include it. What am I doing wrong here?

4 Answers 4

7
+50

Change:

add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND "touch" "${VERSION_H_PATH}")
#add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)

Into:

add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND ${CMAKE_COMMAND} -E touch "${VERSION_H_PATH}") #More reliable touch, use cmake itself to touch the file
add_custom_target(generate_version_h DEPENDS "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog generate_version_h)

See:

  1. CMake command line tool mode.
  2. add_custom_target:

DEPENDS:

Reference files and outputs of custom commands created with add_custom_command() command calls in the same directory (CMakeLists.txt file). They will be brought up to date when the target is built.

  1. add_dependencies:

Make a top-level depend on other top-level targets to ensure that they build before does. A top-level target is one created by one of the add_executable(), add_library(), or add_custom_target() commands (but not targets generated by CMake like install).

By the way, I do not know your specific case, but you might consider using configure_file to generate your header.

1
  • So you get the bounty and upvote, but I accept my own answer because it describes what exactly I had to do in my own case to fix the (stupidly formulated) problem. Thank you for pointing to my mistake.
    – Ruslan
    Dec 16, 2016 at 10:45
1

The problem seems to be that the name test used for the executable target is a name reserved by CMake. See policy CMP0037. Using a different name for the executable seems to work as expected with the custom target:

add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}")
add_executable(testexe ${test_SOURCES})
add_dependencies(testexe GENERATE)
6
  • Doesn't seem to work normally: your variant rebuilds test.c.o unconditionally. Also if I change target name in my version, the symptoms remain, so this doesn't seem to be the reason. Thus I'll edit the OP to avoid using the reserved target name to prevent further confusion.
    – Ruslan
    Dec 9, 2016 at 19:01
  • @Ruslan I do not understand what you are saying: in fact, building test.c.o unconditionally (always) is what you are asking. You are asking that version.h is touched every time you build, and therefore files including it must be rebuilt. Can you please clarify?
    – Antonio
    Dec 16, 2016 at 9:55
  • @Antonio I feel stupid now. Now as I test it, it works even with the code in the OP when I switch to (commented out) add_custom_target. I indeed wanted the command to be run on every build, but I shouldn't have simplified the command that much — it should only sometimes change the header. And that didn't work anyway until I removed all from build directory and re-ran cmake. Don't even know what to do now with the question...
    – Ruslan
    Dec 16, 2016 at 10:27
  • @Ruslan So it seems this was the correct answer after all, and you should accept it (maybe roll back your question). You can reward also other answers that work. BTW, which cmake version are you using?
    – Antonio
    Dec 16, 2016 at 10:32
  • @Antonio it wasn't: changing target name doesn't result in anything apart from change in name of binary. My cmake version is 2.8.12.2 (default on Kubuntu 14.04).
    – Ruslan
    Dec 16, 2016 at 10:41
0

Every custom command needs a driver (something that will depend on its output). In your case, the best driver would be the test executable, which means you should add the generated file into your executable's sources:

add_executable(test ${test_SOURCES} ${VERSION_H_PATH})
3
  • It still doesn't rebuild test.c.o if I touch version.h from command line or remove version.h after building and try make again.
    – Ruslan
    Dec 1, 2016 at 15:40
  • @Ruslan Strange. Is it actually including the correct version.h file? That's a rather common name, is it perhaps picking up an unrelated one? Dec 1, 2016 at 15:48
  • Yes. I've done echo asdfdsaf > version.h; rm CMakeFiles/test.dir/test.c.o; rm test; make and got the expected compilation failure. But not without removing the binaries.
    – Ruslan
    Dec 1, 2016 at 15:52
0

OK, actually the question was badly formulated from the beginning. I shouldn't have simplified the generating command as much as I did. But the problems I had seem to have been solved by removing content of build directory and re-running cmake. For some reason some changes to CMakeLists.txt are not easy for cmake to understand. So here's the CMakeLists.txt which does work, and with not too much simplified generator command:

cmake_minimum_required(VERSION 2.8)
set(test_SOURCES test.c)
include_directories("${CMAKE_BINARY_DIR}")

set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h")
message("VERSION_H_PATH: ${VERSION_H_PATH}")
add_custom_target(GENERATE COMMAND "bash" "-c" "[ -e ${VERSION_H_PATH} ] \\|\\| touch ${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)

This appears to work even when using the reserved test target instead of myprog.

1
  • This solved my issue. I spent hours thinking I was doing it wrong and searched through countless threads. Creating a new build folder, CMake detected the dependencies of the generated target on the header files included by the generated file.
    – Raekye
    Jan 13, 2021 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.