CMake add_custom_command/_target in different directories for cross-compilation
cmake
Solution
I presume you need to use add_dependencies to define dependencies between targets defined using `add_custom_target`. The `DEPENDS` option is used to specify file-level dependencies in custom rules but not target-level dependencies.
For example, command generating `.obj` file as output uses DEPENDS to specify it depends on `.cpp` file. Also, a target can be dependant on a single file, not only on other targets. So, for file-level dependendies, use DEPENDS option, but add_dependencies for target-level.
Problem
In CMake, I'm trying to build a project that includes libraries for multiple different processor architectures, compiled by different toolchains. For each architecture, static libraries are built in multiple subdirectories, and then collected into a shared library in the root directory. So far, I haven't been able to figure this out. The closest I've come so far is to use `add_custom_command` and `add_custom_target` in the subdirectories to produce the .a files and then try to collect them into a .so in the root directory using `add_custom_command` and `add_custom_target` again. But if I make the .so dependency point to the list of custom command outputs, the makefile says "No rule to make ..." because the custom command outputs are not exported out of the subdirectory. So I tried making the .so dependency point to the list of custom targets in the subdirectory, and it complains that it can't find a file with that name. To put it in code form, here is what I have in a subdirectory: ``` add_custom_command(OUTPUT subout.a COMMAND ${MYAR} ...) add_custom_target(subout_target DEPENDS subout.a) ``` In the root directory, I tried this: ``` add_custom_command(OUTPUT my.so COMMAND ${MYLD} ... DEPENDS sub/subout.a) add_custom_target(dll ALL DEPENDS my.so) ``` And I get "No rule to make target 'sub/subout.a', needed by 'my.so'. Stop." Which makes sense because `add_custom_command` targets are not exported out of a directory. And I tried this: ``` add_custom_command(OUTPUT my.so COMMAND ${MYLD} ... DEPENDS subout_target) add_custom_target(dll ALL DEPENDS my.so) ``` This will actually cause the sub/subout.a file to be generated, but then it dies, saying "No rule to make target 'subout_target'..." I think what I really want is to be able to mimic the `add_library` behavior for multiple different architectures. But apparently, CMake doesn't want me to do that... :) Any ideas?