tags:

views:

902

answers:

2

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?

+2  A: 

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.

mloskot
That totally fixed it. Thank you very much. You saved me a lot of frustration.
boiler96
I'm glad and welcome!
mloskot
A: 

Spot On!

add_dependencies allowed me to create the final dependency 'link' between add_custom_target created targets, and add_custom_target generates the targets needed to get add_custom_command to create real content in the makefile, i.e., My add_custom_command wouldn't generate output to the makefiles unless the OUTPUT's from the add_custom_command were specified as dependencies in the add_custom_target command.

Then to get the add_custom_command's to execute in the correct order, I had to use the target names defined in the add_custom_target's as dependencies in the add_dependencies. NOTE: The OUTPUT's specified in the add_custom_command and add_custom_target commands had to be specified in separate quoted paths, i.e.:

add_custom_command(OUTPUT "out1 out2 out3" ...)  # WRONG!
add_custom_target(CUSTOMTARG1 "out1 out2 out3")  # WRONG!

add_custom_command(OUTPUT "out1" "out2" "out3" ...)  # RIGHT!
add_custom_target(CUSTOMTARG1 "out1" "out2" "out3")  # RIGHT!

If I didn't have them in separate quoted paths, the makefile built the outputs every time regardless of whether they existed or not!

mdeazley