Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Imported libraries as OUTPUT or BYPRODUCTS of another custom command or target

I want to extract a static library from a ZIP-file and link against it.

Having the following setting:

add_library(COMSDK_LIB STATIC IMPORTED GLOBAL)
set_property(TARGET COMSDK_LIB PROPERTY IMPORTED_LOCATION "/tmp/lib/libRTSClientSdk.a")

And the imported library being used in another CMakeLists.txt:

target_link_libraries(mylib COMSDK_LIB)

Would it be possible that the imported library is generated by another add_custom_command or add_custom_target?

I tried the following, but it did NOT work:

add_custom_command(
    OUTPUT "/tmp/lib/libRTSClientSdk.a"
    COMMAND unzip -x client_sdk.zip -o /tmp
    DEPENDS client_sdk.zip
)

The given error message was:

$ ninja
ninja: error: '/tmp/lib/libRTSClientSdk.a', needed by 'mylib.dll', missing and no known rule to make it
like image 493
dubbaluga Avatar asked Oct 20 '25 14:10

dubbaluga


1 Answers

The problem is that Your custom command is being executed during make step, and it's expecting extracted dependency earlier --- during cmake execution. So, basically, You need to get that static library from SDK before You're using it in CMake rules or during linkage itself.

Two of possible solutions are listed in CMake mailing list.

Solution #1: (executed in CMake side)

Re-phrasing one of the code snippets, downloading and unzipping is being done during CMake execution:

set(CLIENTSDK_ZIP "cliendsdk.zip")
set(CLIENTSDK_URL "http://example.com/${CLIENTSDK_ZIP}")
set(CLIENTSDK_LIB "libRTSClientSdk.a")
set(CLIENTSDK_OUTPUT_DIR "/tmp/sdk/dir")

# Basically just downloading zip file:
message(STATUS "Downloading ${CLIENTSDK_URL}")
execute_process(COMMAND wget ${CLIENTSDK_URL}
  WORKING_DIRECTORY ${CLIENTSDK_OUTPUT_DIR}
  RESULT_VARIABLE errno
  ERROR_VARIABLE err)

if (NOT ${errno} EQUAL 0)
  message(ERROR "Failed downloading ${CLIENTSDK_URL}. Code: ${err}")
endif()

# Extracting downloaded zip file:
message(STATUS "Extracting ${CLIENTSDK_ZIP})
execute_process(COMMAND unzip ${CLIENTSDK_ZIP}
  WORKING_DIRECTORY ${CLIENTSDK_OUTPUT_DIR}
  RESULT_VARIABLE errno
  ERROR_VARIABLE err)

if (NOT ${errno} EQUAL 0)
  message(ERROR "Failed extracting ${CLIENTSDK_ZIP}. Code: ${err}")
endif()

# Importing into CMake scope one library from extracted zip:
add_library(specific_sdk_lib STATIC IMPORTED)
set_target_properties(specific_sdk_lib
  PROPERTIES IMPORTED_LOCATION ${CLIENTSDK_OUTPUT_DIR}/lib/${CLIENTSDK_LIB})

# Adding rule for linking to specific static library from extracted zip:
target_link_libraries(mylib specific_sdk_lib)

Solution #2 (executed in make side):

# We must include ExternalProject CMake module first!
include("ExternalProject")

set(CLIENTSDK_URL "http://example.com/clientsdk.zip")
set(CLIENTSDK_LIB "libRTSClientSdk.a")
set(CLIENTSDK_PREFIX "3rd_party")
set(CLIENTSDK_EXTRACTED_DIR
    "${CMAKE_CURRENT_BINARY_DIR}/${CLIENTSDK_PREFIX}/src/DownloadClientSDK/")

ExternalProject_Add("DownloadClientSDK"
    PREFIX ${CLIENTSDK_PREFIX}
    URL "${CLIENTSDK_URL}"
    # Suppress ExternalProject configure/build/install targets:
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND "")

add_library(COMSDK_LIB STATIC IMPORTED)
set_target_properties(COMSDK_LIB PROPERTIES IMPORTED_LOCATION
    ${CLIENTSDK_EXTRACTED_DIR}/${CLIENTSDK_LIB})

# Require all that download/unzip mumbojumbo only for COMSDK_LIB:
add_dependencies(COMSDK_LIB DownloadClientSDK)

target_link_libraries(my_library COMSDK_LIB)

Basically, CMake and it's ExternalProject module takes care (generates proper make targets) of recognizing archive format, unzipping it, and if needed - configuring, building and installing.

like image 89
Kamiccolo Avatar answered Oct 22 '25 05:10

Kamiccolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!