This new structure provides several benefits:
* Allows us to install multiple CMake projects in the same destination
dir (without include path conflicts).
- This makes shipping the trading algo sdk a trivial excercise. Simply
copy the lib and include dirs from the install dest. That's it!
* Puts us in a position to break up the monolithic libspark into smaller
modules, which are easier to manage.
96 lines
2.9 KiB
CMake
96 lines
2.9 KiB
CMake
include_guard(DIRECTORY)
|
|
include(${CMAKE_SOURCE_DIR}/cmake/ExportToParent.cmake)
|
|
include(${CMAKE_SOURCE_DIR}/cmake/InstallBasic.cmake)
|
|
include(${CMAKE_SOURCE_DIR}/cmake/SetFirstDefined.cmake)
|
|
include(${CMAKE_SOURCE_DIR}/cmake/safe_dep_glob.cmake)
|
|
|
|
function(init_library)
|
|
set(_options SHARED STATIC)
|
|
set(_one_value_args NAME SOURCE_DIR INCLUDE_DIR)
|
|
set(_multi_value_args)
|
|
cmake_parse_arguments(_ARGS "${_options}" "${_one_value_args}" "${_multi_value_args}" ${ARGN})
|
|
if (_ARGS_SHARED AND _ARGS_STATIC)
|
|
message(FATAL_ERROR "Invalid args passed to init_library: cannot specify STATIC and SHARED")
|
|
endif()
|
|
set_first_defined(_name _ARGS_NAME PROJECT_NAME)
|
|
set_first_defined(_source_dir _ARGS_SOURCE_DIR _name)
|
|
set_first_defined(_include_dir _ARGS_INCLUDE_DIR _source_dir)
|
|
|
|
# Cpp sources
|
|
file(
|
|
GLOB_RECURSE ${_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 ${${_name}_SOURCES})
|
|
if(${item} MATCHES "tests/.*\.cpp$")
|
|
list(APPEND ${_name}_TEST_SOURCES ${item})
|
|
endif()
|
|
endforeach()
|
|
foreach(item ${${_name}_TEST_SOURCES})
|
|
list(REMOVE_ITEM ${_name}_SOURCES ${item})
|
|
endforeach()
|
|
|
|
export_to_parent(
|
|
${_name}_SOURCES
|
|
${_name}_TEST_SOURCES
|
|
)
|
|
|
|
# Add main lib
|
|
if (_ARGS_STATIC)
|
|
add_library(${_name} STATIC ${${_name}_SOURCES})
|
|
elseif(_ARGS_SHARED)
|
|
add_library(${_name} SHARED ${${_name}_SOURCES})
|
|
else()
|
|
add_library(${_name} ${${_name}_SOURCES})
|
|
endif()
|
|
target_include_directories(
|
|
${_name}
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
)
|
|
target_link_directories(
|
|
${_name}
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>
|
|
)
|
|
# Install main lib
|
|
install_basic(
|
|
NAME ${_name}
|
|
INCLUDE_DIR ${_include_dir}
|
|
)
|
|
|
|
# Add tests executable (if there are any)
|
|
if (${_name}_TEST_SOURCES)
|
|
add_executable(${_name}-tests ${${_name}_TEST_SOURCES})
|
|
|
|
get_target_property(_lib_type ${_name} TYPE)
|
|
if (_lib_type STREQUAL "SHARED_LIBRARY")
|
|
# Set RPATH so the tests exe knows where to find the shared lib.
|
|
set_target_properties(
|
|
${_name}-tests
|
|
PROPERTIES
|
|
BUILD_RPATH "$ORIGIN"
|
|
INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
|
|
)
|
|
endif()
|
|
|
|
# Link main lib
|
|
target_link_directories(
|
|
${_name}-tests
|
|
PRIVATE
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>
|
|
)
|
|
target_link_libraries(${_name}-tests PRIVATE ${_name})
|
|
|
|
# Catch2
|
|
find_package(Catch2 CONFIG REQUIRED COMPONENTS Catch2WithMain)
|
|
target_link_libraries(${_name}-tests PRIVATE Catch2::Catch2WithMain)
|
|
endif()
|
|
endfunction()
|