drumaddict
8/3/2017 - 10:11 PM

Import a shared library with CMake

Import a shared library with CMake

#Findsimple2d.cmake


# SIMPLE2D_FOUND
# SIMPLE2D_INCLUDE_DIR
# SIMPLE2D_LIB

# /usr/local/include/simple2d.h
# /usr/local/lib/libsimple2d.a
# /usr/local/bin/simple2d


find_path(SIMPLE2D_INCLUDE_DIR simple2d.h
    /usr/local/include
    /usr/local/include/simple2d
    /usr/include
    DOC "The directory where simple2d.h resides.")


find_library(SIMPLE2D_LIB
  NAMES libsimple2d.a
  PATHS
  /usr/local/lib
  /usr/local/lib/simple2d
  /usr/lib
  DOC "The simple2d library")


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SIMPLE2D REQUIRED_VARS SIMPLE2D_INCLUDE_DIR SIMPLE2D_LIB)

mark_as_advanced(SIMPLE2D_INCLUDE_DIRS)

#-------------------------------------------------------------------------------------------


include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
ExternalProject_Add(simple_2d_download
  GIT_REPOSITORY https://github.com/simple2d/simple2d.git
  PREFIX ${PROJECT_BINARY_DIR}/_3rdParty/simple_2d
  CONFIGURE_COMMAND ""
  BUILD_COMMAND cd ${PROJECT_BINARY_DIR}/_3rdParty/simple_2d/src/simple_2d && make
  # INSTALL_COMMAND make install
  INSTALL_COMMAND  ""

)

# ExternalProject_Get_Property(simple_2d install_dir)


# calls Findsimple2d.cmake
find_package(simple2d REQUIRED)
if (NOT SIMPLE2D_FOUND)
    message(FATAL_ERROR "Package simple2d not found")
  endif (NOT SIMPLE2D_FOUND)

add_library(simple2dd SHARED IMPORTED GLOBAL)
set_target_properties(simple2dd PROPERTIES IMPORTED_LOCATION ${SIMPLE2D_LIB})


target_include_directories(${target}
    PRIVATE
    ${DEFAULT_INCLUDE_DIRECTORIES}
    ${PROJECT_BINARY_DIR}/source/include
    PUBLIC
    ${SIMPLE2D_INCLUDE_DIR}
)

target_link_libraries(${target}
    PUBLIC
    simple2dd
    PRIVATE
    ${DEFAULT_LIBRARIES}
    ${META_PROJECT_NAME}::maze
    ${META_PROJECT_NAME}::baselib
)