quetzalfir
quetzalfir

Reputation: 568

Fetch and build libpng-android from CMake

I'm developing a C++ library for Android, and I have as dependency the libpng. I'm using the modified library for Android https://github.com/julienr/libpng-android.git, which has a build.sh file in order to compile for the different architectures. Or it can be compiled using ndk-build.

I'm Fetching the library from CMake using FetchContent, this effectively download the sources files, but it doesn't build properly. I'm setting the BUILD_COMMAND but it doesn't work.

FetchContent_Declare( png
        GIT_REPOSITORY      https://github.com/julienr/libpng-android.git
        GIT_TAG             master
        UPDATE_DISCONNECTED TRUE
        STEP_TARGETS        update
        BUILD_COMMAND       "${NDK_PATH}/ndk-build.cmd NDK_PROJECT_PATH=${png_SOURCE_DIR}"

FetchContent_GetProperties(png)
if (NOT png_POPULATED)
    FetchContent_Populate(png)
    add_subdirectory("${png_SOURCE_DIR}" "${png_BINARY_DIR}" EXCLUDE_FROM_ALL)
endif()

It seems that the BUILD_COMMAND do nothing.

So, How can I fetch and build the libpng-android from CMake, and then import that result as an imported library?

Upvotes: 1

Views: 738

Answers (2)

quetzalfir
quetzalfir

Reputation: 568

Thanks to @Tsyvarev and @Mizux I can follow the correct way to do it.

FetchContent_Declare( libpng
        GIT_REPOSITORY      https://github.com/julienr/libpng-android.git
        GIT_TAG             master
        UPDATE_DISCONNECTED TRUE
        STEP_TARGETS        update)

FetchContent_GetProperties(libpng)
if (NOT libpng_POPULATED)
    FetchContent_Populate(libpng)
    add_subdirectory("${libpng_SOURCE_DIR}" ${libpng_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

exec_program("${CMAKE_ANDROID_NDK}/ndk-build.cmd"
        ${libpng_BINARY_DIR}
        ARGS NDK_PROJECT_PATH=${libpng_SOURCE_DIR} APP_ABI=${ANDROID_ABI})

and then linking the libraries

target_link_libraries( project
        PRIVATE
            ${libpng_SOURCE_DIR}/obj/local/${ANDROID_ABI}/libpng.a
        )

Upvotes: 2

Mizux
Mizux

Reputation: 9301

To complete the Tsyvarev comment with references, according to the doc...

The following options are explicitly prohibited (they are disabled by the FetchContent_Populate() command):

  • CONFIGURE_COMMAND
  • BUILD_COMMAND
  • INSTALL_COMMAND
  • TEST_COMMAND

ref: https://cmake.org/cmake/help/latest/module/FetchContent.html#command:fetchcontent_getproperties (just the paragraph above)

Upvotes: 2

Related Questions