1

I have a project structure as follows. A.dll depends on B.exe, B.exe depends on custom target C. The custom target C generates some files, which B.exe packages in an archive as a post build step on target B.
When I first build A.dll, since it is the first time B.exe gets built and as a post build step myArchieve.a gets built. From then on if I build the A.dll, B.exe doesn't get rebuilt, since it's an executable and it exists because of a previous build. The problems because of this are :
1. I always have a stale executable
2. If I make some changes to C and trigger a build of A.dll, cmake just rebuilds C and A. It doesn't rebuild B.exe and as a result it missed the archieve step and my archieve never gets updated.

Is there a solution to this problem ? I have read this link already and doesn't help much.

Community
  • 1
  • 1
voidMainReturn
  • 3,339
  • 6
  • 38
  • 66

2 Answers2

0

There is a conceptual problem with your setup: The packaging should not be a post-build step.

Instead you should use add_custom_command for the packaging and have that command DEPENDS on both the target that builds B.exe and the output files from your custom target C.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • This won't work because add_custom_command's documentation clearly states that the custom command is given as a rule to target which uses its output files as source file. My output i.e. "myArchieve.a " isn't being used as a source by any target. Hence this custom command won't be added on any target. – voidMainReturn Jan 22 '15 at 19:44
0

I am quite newbie with CMake and had a similar issue with exec not being rebuilt. I share my solution hopefully this will be helpful for future readers.

Note: The following solution was tested using VS 2017 with the new built in CMake support, so no solution is generated. Only the folder is loaded where the top-level CMakeLists.txt is found.

The folder structure would be:

.
└── A
    ├── B
    │   └── main.cpp
    ├── A.cpp
    └── A.h

You could create a sub folder for B:

# Content of "B/CMakeLists.txt"
project(B)

add_executable( BExec main.cpp)
add_custom_command(TARGET BExec 
                   PRE_BUILD
                   COMMAND cmake -E cmake_autogen ${PROJECT_BINARY_DIR}/CMakeFiles/BExec_autogen.dir Debug)

And then you could do the following in A:

# Content of CMakeLists.txt for A
project(A)
add_subdirectory(B)

add_library(A A.cpp A.h)
add_dependencies(A BExec)

Every time you change the code in main.cpp, A.cpp or A.h, it will rebuild the library and the exec as well. Maybe not the most elegant, but working solution.

Peter Devenyi
  • 177
  • 2
  • 15