0

I have a custom target & custom command in my CMakeLists.txt to build a .jar of the wrapper for the c library which is the main focus of the project. I am using install(FILES ...) (because even if I specify ARCHIVE with install(TARGETS ...) cmake complains that the target is not a library or executable). The custom target is never built because nothing depends on it except the install(FILES ...). How can I tell cmake about the dependency so the target gets built?

Here is the relevant part of the CMakeLists.txt. In this version I've add ALL to the custom target to force the build but I would prefer this target only be built when the ALL_BUILD or package targets are being built.

add_custom_command(
    OUTPUT
        ${CMAKE_SOURCE_DIR}/interface/java_binding/target/libktx-${PROJECT_VERSION}-source.jar
        ${CMAKE_SOURCE_DIR}/interface/java_binding/target/libktx-${PROJECT_VERSION}.jar
    COMMAND
        ${MAVEN_EXECUTABLE} -Drevision=${PROJECT_VERSION} -Dmaven.test.skip=true package
    DEPENDS
        ktx-jni
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/interface/java_binding
)

add_custom_target( ktx-jar ALL
    DEPENDS
        ${CMAKE_SOURCE_DIR}/interface/java_binding/target/libktx-${PROJECT_VERSION}-source.jar
        ${CMAKE_SOURCE_DIR}/interface/java_binding/target/libktx-${PROJECT_VERSION}.jar
    WORKING_DIRECTORY
        ${CMAKE_SOURCE_DIR}/interface/java_binding
    COMMENT
        "Java wrapper target"
)

install(FILES
    ${CMAKE_SOURCE_DIR}/interface/java_binding/target/libktx-${PROJECT_VERSION}.jar
    TYPE LIB
    COMPONENT jni
)
msc
  • 1,549
  • 2
  • 12
  • 19
  • Installation step depends only on `all` target. You can add no other dependency. See e.g. [this question](https://stackoverflow.com/questions/40037426/cmake-install-target-dependencies) ot [that one](https://stackoverflow.com/questions/17164731/installing-only-one-target-and-its-dependencies-out-of-a-complex-project-with). – Tsyvarev Apr 01 '22 at 14:49
  • I think the documentation for `ALL` on add_custom_target is misleading. The description makes it sound like adding this causes the target to always be built. In fact it seems to be making the target a dependency of the ALL_BUILD and therefore `package` targets, which is exactly what I want. – msc Apr 01 '22 at 23:01

0 Answers0