Skip to content
Snippets Groups Projects
FindBoost.cmake 4.51 KiB
# This file is NOT an original FindBoost.cmake module provided by KitWare!
#
# It's a simplified version whose only purpose is to be used in a software
# library lib4neuro and it does NOT provide a full funcionality of the original,
# as it only works with the system-layout-named libraries (e.g. libboost_system.so).

# Optional user-defined variables
# (can be passed directly to CMake or exported as environmental variables
#
# * BOOST_LIBRARYDIR - The path to the folder containing Boost libraries
# * BOOST_INCLUDEDIR - The path to the folder containing Boost header files

# "External" used variables
#
# * DEPENDENCIES_LINK_TYPE - TODO

# "Output" variables set after running this script
# * Boost_FOUND - TODO
# * Boost_INCLUDE_DIRS - TODO
# * Boost_LIBRARY_DIRS - TODO
# * Boost_LIBRARIES - TODO

# Module usage
# TODO

message("FindBoost starting...")

# Check if needed Boost components were specified
if(NOT Boost_FIND_COMPONENTS)
    message(FATAL_ERROR "No Boost components were specified! Please, set them correctly with flag COMPONENTS (see Module Usage section in this script).")
else()
    message("Required Boost components: ${Boost_FIND_COMPONENTS}")
endif()

# Look for a standard boost header file.
set(Boost_INCLUDE_DIRS "Boost_INCLUDE_DIRS-NOTFOUND")
find_path(
    Boost_INCLUDE_DIRS

    NAMES
        config.hpp

    HINTS
        ${BOOST_INCLUDEDIR}
        $ENV{BOOST_INCLUDEDIR}
        ${CMAKE_CURRENT_LIST_DIR}/external_dependencies/boost


    PATHS
        /usr/include
        /home/boost

    PATH_SUFFIXES
        boost
        include
)

if(NOT Boost_INCLUDE_DIRS)
    message(FATAL_ERROR "Boost include directory was not found! Please, set variable BOOST_INCLUDEDIR to the correct path.")
endif()

# Create a list of requested Boost libraries with "system" names
if(NOT DEPENDENCIES_LINK_TYPE)
    message(FATAL_ERROR "Variable DEPENDENCIES_LINK_TYPE is not set! Set it to 'static' or 'shared'.")
endif()

set(LIB_SUFFIX "a")  # suffix for Linux static libraries
if("${DEPENDENCIES_LINK_TYPE}" STREQUAL "shared" AND WIN32)
    set(LIB_SUFFIX "dll")
elseif("${DEPENDENCIES_LINK_TYPE}" STREQUAL "static" AND WIN32)
    set(LIB_SUFFIX "lib")
elseif("${DEPENDENCIES_LINK_TYPE}" STREQUAL "shared")
    set(LIB_SUFFIX "so")
endif()

set(LIB_PREFIX "lib")
if(WIN32)
    set(LIB_PREFIX "")
endif()

set(REQUESTED_BOOST_LIBS "")
foreach(COMPONENT ${Boost_FIND_COMPONENTS})
    list(APPEND REQUESTED_BOOST_LIBS "${LIB_PREFIX}boost_${COMPONENT}.${LIB_SUFFIX}")
endforeach()

# Look for libraries specified by COMPONENTS flag
set(Boost_LIBRARY_DIRS "Boost_LIBRARY_DIRS-NOTFOUND")
find_path(
    Boost_LIBRARY_DIRS
    
    NAMES
        ${REQUESTED_BOOST_LIBS}

    HINTS
        ${BOOST_LIBRARYDIR}
        $ENV{BOOST_LIBRARYDIR}
        ${CMAKE_CURRENT_LIST_DIR}/external_dependencies/boost

    PATHS
        /usr/lib/boost
        /usr/lib/x86_64-linux-gnu
        
    PATH_SUFFIXES
        stage/lib
        lib
)

# Construct list of libraries' names and make them
# targets, so they may be linked
set(Boost_LIBRARIES "")
foreach(LIBNAME ${REQUESTED_BOOST_LIBS})
    message("Looking for ${LIBNAME}...")

    set(${LIBNAME} "${LIBNAME}-NOTFOUND")
    find_library(
        ${LIBNAME}

        NAMES
        ${LIBNAME}

        PATHS
            ${Boost_LIBRARY_DIRS}

        PATH_SUFFIXES
        stage/lib
        lib

        NO_DEFAULT_PATH
    )

    # Check, if the Boost component was found
    if("${LIBNAME}" STREQUAL "${LIBNAME}-NOTFOUND")
        message(FATAL_ERROR "Boost library ${LIBNAME} was NOT found!
                             Please, set variable BOOST_LIBRARYDIR to the correct path and check the library names
                             format in your Boost installation.")

    message("${LIBNAME} ${${LIBNAME}}")

    # Make the target of the same name as the library
    #add_library(${LIBNAME}_tar STATIC IMPORTED)
    get_filename_component(TMP ${${LIBNAME}} ABSOLUTE)
    #set_property(TARGET ${LIBNAME}_tar PROPERTY IMPORTED_LOCATION ${TMP})

    list(APPEND Boost_LIBRARIES ${TMP})
endforeach()


if(NOT Boost_LIBRARY_DIRS)
    message(FATAL_ERROR "Boost library directory was not found! Please, set variable BOOST_LIBRARYDIR to the correct path.")
endif()

# Set Boost_FOUND
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
    Boost
    
    FAIL_MESSAGE 
        "Boost was NOT found!"
    
    REQUIRED_VARS
        Boost_INCLUDE_DIRS
        Boost_LIBRARY_DIRS
)

message("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
message("Boost_LIBRARIES: ${Boost_LIBRARIES}")