I\'m trying to get a file produced by an add_custom_command in one directory to be a dependency of an add_custom_command in another directory.
In the first directory (li
The cmake documentation says the following about the DEPENDS parameter:
The DEPENDS option specifies files on which the command depends. If any dependency is an OUTPUT of another custom command in the same directory (CMakeLists.txt file) CMake automatically brings the other custom command into the target in which this command is built. If DEPENDS specifies any target (created by an ADD_* command) a target-level dependency is created to make sure the target is built before any target using this custom command.
Therefore I think you will have to define a target using add_custom_target and depend on this.
The documentation for add_custom_target says:
Dependencies listed with the DEPENDS argument may reference files and outputs of custom commands created with add_custom_command() in the same directory (CMakeLists.txt file).
So you will have to use add_custom_command and add_custom_target as follows:
In the first directory generating the bc file you do
add_custom_command(OUTPUT libcore.bc ... ) # just as in your question
add_custom_target (LibCoreBC DEPENDS libcore.bc)
In the second directory you do
add_custom_command (OUT ${OBJ_FILE} DEPENDS LibCoreBC ....)
try to add following command into second directory:
set_source_files_properties(${PROJECT_BINARY_DIR}/lib/core/libcore.bc PROPERTIES GENERATED TRUE)
I solved my problem with this command. a relating link: https://cmake.org/cmake/help/latest/prop_sf/GENERATED.html
This is a non-answer but a clarification to one of your answers above.
According to the cmake documents, a custom target created by
add_custom_target
is always considered out of date and is always built.
IMO, the cmake documents should say instead:
A custom target created by add_custom_target is always considered out of date and is always built, but only when requested.
That means that if all of your targets are marked as EXCLUDE_FROM_ALL
, and you have add_custom_target
commands that create new targets, and you type make
from the command line with no targets specified, the targets added with add_custom_target
are not built. But if you spell them out on the make
command line explicitly, then they are built. Also, there is the ALL
keyword that you can specify to the add_custom_target
to force those to be built as a part of the all rule, which I believe means when make
is executed without arguments.
I don't think add_custom_target
will work for what I want. According to the cmake documents, a custom target created by add_custom_target is always considered out of date and is always built.
The problem is that I am trying to take the output from one add_custom_command
, and feed it into the input of another add_custom_command
in a different directory. I only want this to happen if the original source file is out of date - if I used add_custom_target
, then the output would always be rebuilt even if the source file had not changed. Given that there are hundreds of these source files, this would make the build very slow.
Here's what I am trying to do: I have a program which generates a .bc file (LLVM bitcode) given a source file. There are a lot of these source files, which create a lot of .bc files.
A second program transforms all of the the .bc files into a single .obj (ELF object) file. So the transformation steps look like this:
source file -> .bc (via add_custom_command)
.bc -> .obj (via add_custom_command)
.obj -> .exe (via add_executable)
The original source code files are in different directories because they are libraries of classes - I don't want to have to put all the code for every class library in the same directory.