cmake findboost not working.

274 views
Skip to first unread message

Andrea Fiorito

unread,
Dec 13, 2023, 6:59:46 PM12/13/23
to android-ndk
Hello,
here's another one trying to use boost with android (and possibly writing a portable cmakelists file).

The problem:
find_path appends the HINTS to an ndk path instead of using them as absolute path:

minimal reproducible example:
add on a cmakelists.txt the following:

set(CMAKE_FIND_DEBUG_MODE TRUE)
find_path(ABC_VAR
    NAMES         a_random_existing_file.txt
    HINTS         the_abs_path_of_the_file
    )
output on console similar to:

    find_path called with the following settings:
 
      VAR: Boost_INCLUDE_DIR
      NAMES: "boost/config.hpp"
      Documentation: Path to a file.
      Framework
        Only Search Frameworks: 0
        Search Frameworks Last: 0
        Search Frameworks First: 1
      AppBundle
        Only Search AppBundle: 0
        Search AppBundle Last: 0
        Search AppBundle First: 1
      CMAKE_FIND_USE_CMAKE_PATH: 0
      CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 0
      CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
      CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 0
 
    find_path considered the following locations:
.....
/Users/bloom/Library/Android/sdk/ndk/25.1.8937393/the_abs_path_of_the_file/a_random_existing_file.txt

How do I get rid of "/Users/bloom/Library/Android/sdk/ndk/25.1.8937393/" part?

as a recap:
I cloned boost4android and compiled it (https://github.com/moritz-wundke/Boost-for-Android)
I set the variables:
set(Boost_DEBUG ON)
    set(BOOST_ROOT ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI})
    set(BOOST_INCLUDEDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/include/boost-1_82)
    set(BOOST_LIBRARYDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/lib)
    set(Boost_LIB_PREFIX lib)

set(Boost_ADDITIONAL_VERSIONS "1.82.0" "1.82")
    set(Boost_NO_SYSTEM_PATHS TRUE)
    set(Boost_COMPILER "-clang-darwin")
    set(Boost_USE_STATIC_LIBS        ON)
    set(Boost_USE_MULTITHREADED      ON)
    set(Boost_USE_STATIC_RUNTIME    OFF)
    find_package( Boost 1.82.0 COMPONENTS serialization )

and I'm stuck at that find_path not found.
---------------------------------------------------------------------------------------
Also set(Boost_DEBUG ON) doesn't print anything out of the box because in findBoost.cmake the instruction that prints contains an extra word STATUS.
    message(STATUS "[ ${file}:${line} ] ${text}")
which sucks.

Who's gonna help this poor dev here? :D

Andrea Fiorito

unread,
Dec 14, 2023, 10:56:51 AM12/14/23
to android-ndk
Found a workaround, but probably a fix is needed somewhere to make it work out of the box.

My workaround was the line: set(CMAKE_SYSROOT "")

the full section looks like:

set(CMAKE_FIND_DEBUG_MODE TRUE)
set(Boost_DEBUG 0)
set(CMAKE_SYSROOT_OLD ${CMAKE_SYSROOT})
set(CMAKE_SYSROOT "")

set(BOOST_ROOT ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI})
message("LOOKING FOR BOOST ROOT IN " ${BOOST_ROOT})

set(BOOST_INCLUDEDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/include/boost-1_82)
set(BOOST_LIBRARYDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/lib)
set(Boost_LIB_PREFIX lib)

set(Boost_ADDITIONAL_VERSIONS "1.82.0" "1.82")
set(Boost_NO_SYSTEM_PATHS TRUE)
set(Boost_COMPILER "-clang-darwin")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package( Boost 1.82.0 COMPONENTS serialization )

if(Boost_FOUND)
message("Boost_FOUND is True")
include_directories(${Boost_INCLUDE_DIRS})
else()
message(FATAL_ERROR "Boost_FOUND is False")
endif()
set(CMAKE_SYSROOT ${CMAKE_SYSROOT_OLD})
........
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

funny thing is that for the openssl library (that suffered the same problem) I discovered first this workaround: set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
but also the line set(CMAKE_SYSROOT "") works.

the full section (with the first discovered workaround) looks like:
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
    message("LOOKING FOR OPEN_SSL LIBRARY IN " ${OPENSSL_LIB_DIR}/${CMAKE_ANDROID_ARCH_ABI})
    find_library(lib_crypto NAMES crypto
            HINTS ${OPENSSL_LIB_DIR}/${CMAKE_ANDROID_ARCH_ABI})

I'm not sure what is the impact to set the variable to empty in general, so i'm resetting to the old value just in case right after.


Andrea Fiorito

unread,
Dec 14, 2023, 10:56:52 AM12/14/23
to android-ndk
As update,
I was able to find a workaround with the line: set(CMAKE_SYSROOT "")

the full snippet of CMakelist looks like: (some options may be not necessary)

    set(CMAKE_FIND_DEBUG_MODE FALSE)

    set(Boost_DEBUG 0)
    set(CMAKE_SYSROOT_OLD ${CMAKE_SYSROOT})
    set(CMAKE_SYSROOT "")
    set(BOOST_ROOT ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI})
    message("LOOKING FOR BOOST ROOT IN " ${BOOST_ROOT})

    set(BOOST_INCLUDEDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/include/boost-1_82)
    set(BOOST_LIBRARYDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/lib)
    set(Boost_LIB_PREFIX lib)

    set(Boost_ADDITIONAL_VERSIONS "1.82.0" "1.82")
    set(Boost_NO_SYSTEM_PATHS TRUE)
    set(Boost_COMPILER "-clang-darwin")
    set(Boost_USE_STATIC_LIBS        ON)
    set(Boost_USE_MULTITHREADED      ON)
    set(Boost_USE_STATIC_RUNTIME    OFF)
    find_package( Boost 1.82.0 COMPONENTS serialization )

    if(Boost_FOUND)
        message("Boost_FOUND is True")
        include_directories(${Boost_INCLUDE_DIRS})
    else()
        message(FATAL_ERROR "Boost_FOUND is False")
    endif()
    set(CMAKE_SYSROOT ${CMAKE_SYSROOT_OLD})


Also, I had the same problem with find_library and openSSL (compiled for android obviously), the same solution works but the first one I found was using the line: set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)

full snippet looks like:


    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
    message("LOOKING FOR OPEN_SSL LIBRARY IN " ${OPENSSL_LIB_DIR}/${CMAKE_ANDROID_ARCH_ABI})
    find_library(lib_crypto NAMES crypto
            HINTS ${OPENSSL_LIB_DIR}/${CMAKE_ANDROID_ARCH_ABI})


A review of the settings for those variables, CMAKE_FIND_ROOT_PATH, CMAKE_SYSROOT may be needed imo to have findboost working out of the box.
On Wednesday, December 13, 2023 at 11:59:46 PM UTC Andrea Fiorito wrote:
Reply all
Reply to author
Forward
0 new messages