56 lines
1.8 KiB
CMake
56 lines
1.8 KiB
CMake
function(init_shared_library)
|
|
include(${CMAKE_SOURCE_DIR}/cmake/ExportToParent.cmake)
|
|
set(source_dir)
|
|
if (${ARGC} EQUAL 0)
|
|
set(source_dir ${PROJECT_NAME})
|
|
elseif(${ARGC} GREATER 1)
|
|
message(FATAL_ERROR "init_shared_library received invalid arguments. Usage: init_shared_library(<OPTIONAL:source_dir>)")
|
|
else()
|
|
set(source_dir ${ARGV})
|
|
endif()
|
|
|
|
# Glob cpp sources
|
|
file(
|
|
GLOB_RECURSE ${PROJECT_NAME}_SOURCES
|
|
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
|
${source_dir}/*.cpp
|
|
)
|
|
# Note: cpp files inside 'tests' dir(s) are considered tests.
|
|
# Remove tests from main sources, and add them to test sources
|
|
foreach(item ${${PROJECT_NAME}_SOURCES})
|
|
if(${item} MATCHES "tests/*.cpp")
|
|
list(REMOVE_ITEM ${PROJECT_NAME}_SOURCES ${item})
|
|
list(APPEND ${PROJECT_NAME}_TEST_SOURCES ${item})
|
|
endif()
|
|
endforeach()
|
|
|
|
export_to_parent(
|
|
${PROJECT_NAME}_SOURCES
|
|
${PROJECT_NAME}_TEST_SOURCES
|
|
)
|
|
|
|
# Add main library
|
|
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES})
|
|
target_include_directories(
|
|
${PROJECT_NAME}
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
)
|
|
target_link_directories(
|
|
${PROJECT_NAME}
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>
|
|
)
|
|
|
|
# Add tests executable (if there are any)
|
|
if (${PROJECT_NAME}_TEST_SOURCES)
|
|
add_executable(${PROJECT_NAME}-tests ${${PROJECT_NAME}_TEST_SOURCES})
|
|
target_link_libraries(${PROJECT_NAME}-tests PRIVATE ${PROJECT_NAME})
|
|
# Catch2
|
|
find_package(Catch2 CONFIG REQUIRED COMPONENTS Catch2WithMain)
|
|
target_link_libraries(${PROJECT_NAME}-tests PRIVATE Catch2::Catch2WithMain)
|
|
endif()
|
|
endfunction(init_shared_library)
|