[CMake] how to add_library with an already compiled lib in a zip file ?

Andreas Pakulat apaku at gmx.de
Wed Nov 28 08:35:55 EST 2012


Hi,

On Wed, Nov 28, 2012 at 1:41 PM, Martin Koller <martin.koller at etm.at> wrote:
> Hi,
>
> I'm struggling with the following requirement:
> I have a lib stored in a zip file (the lib is not built via cmake, e.g. delivered from external company).
>
> I want a target to link against this lib and want to extract the lib from the zip file only if
> this specific target is being built, and only if it is not already unzipped.
>
> What I tried in the libs directory:
>
> set(TARGET xxx)
>
> add_library(${TARGET} STATIC IMPORTED GLOBAL)
>
> set_target_properties(${TARGET} PROPERTIES IMPORTED_LOCATION
>                       ${CMAKE_CURRENT_BINARY_DIR}/xxx.a)
>
> set(ZIPFILE xxx.zip)
>
> add_custom_target(${TARGET}_lib
>                   COMMAND unzip -o ${CMAKE_CURRENT_SOURCE_DIR}/${ZIPFILE}
>                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${ZIPFILE}
>                   )
> add_dependencies(${TARGET} ${TARGET}_lib)
>
>
> Then I can use this target in e.g.
> target_link_libraries(myTarget xxx)
>
> The above however unzips the lib every time, even if it is already existing.
> Is it possible to avoid that ?

This should be doable by splitting the custom target into a custom
command and a custom target. For the custom command you then need to
list the output files generated by the unzip and thus it'll only be
executed if the .zip is newer than the output files or an output file
is missing. The custom target is then used to provide a dummy target
(use echo or something like that as command) and depends on the output
files from the custom command and used with add_dependencies. That way
the custom-target will always be out-of-date and hence "built" but it
only depends on the output of the custom-command which in turn knows
when to re-run the command and thus only does this if necessary.

Another option would be to do this in a cmake script file manually and
run that via cmake -P as command.

Andreas


More information about the CMake mailing list