Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize CMake and add conan support for dependencies #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions CMake/BoostTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function(boost_test_discover_tests TARGET)
cmake_parse_arguments(
BOOST_TEST
""
"WORKING_DIRECTORY;DISCOVERY_TIMEOUT"
"EXTRA_ARGS"
${ARGN}
)
if (NOT BOOST_TEST_WORKING_DIRECTORY)
set(BOOST_TEST_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
endif()
if (NOT BOOST_TEST_DISCOVERY_TIMEOUT)
set(BOOST_TEST_DISCOVERY_TIMEOUT 10)
endif()
get_property(
has_counter
TARGET ${TARGET}
PROPERTY BOOST_TEST_DISCOVERED_TEST_COUNTER
SET
)
if(has_counter)
get_property(
counter
TARGET ${TARGET}
PROPERTY BOOST_TEST_DISCOVERED_TEST_COUNTER
)
math(EXPR counter "${counter} + 1")
else()
set(counter 1)
endif()
set_property(
TARGET ${TARGET}
PROPERTY BOOST_TEST_DISCOVERED_TEST_COUNTER
${counter}
)

# Define rule to generate test list for aforementioned test executable
set(ctest_file_base "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_${counter}")
set(ctest_include_file "${ctest_file_base}_include.cmake")
set(ctest_tests_file "${ctest_file_base}_tests.cmake")
add_custom_command(
TARGET ${TARGET} POST_BUILD
COMMAND "${CMAKE_COMMAND}"
-D "TEST_TARGET=${TARGET}"
-D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
-D "TEST_WORKING_DIR=${BOOST_TEST_WORKING_DIRECTORY}"
-D "TEST_DISCOVERY_TIMEOUT=${BOOST_TEST_DISCOVERY_TIMEOUT}"
-D "CTEST_FILE=${ctest_tests_file}"
-P "${_BOOST_TEST_DISCOVER_TEST_SCRIPTS}"
VERBATIM
)

file(WRITE "${ctest_include_file}"
"if(EXISTS \"${ctest_tests_file}\")\n"
" include(\"${ctest_tests_file}\")\n"
"else()\n"
" add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)\n"
"endif()\n"
)

# Add discovered tests to directory TEST_INCLUDE_FILES
set_property(DIRECTORY
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
)

endfunction()

set(_BOOST_TEST_DISCOVER_TEST_SCRIPTS
${CMAKE_CURRENT_LIST_DIR}/BoostTestDiscoverTests.cmake
)
56 changes: 56 additions & 0 deletions CMake/BoostTestDiscoverTests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
set(script)

function(add_command NAME)
set(_args "")
foreach (_arg ${ARGN})
set(_args "${_args} ${_arg}")
endforeach()
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
endfunction()

# List all available tests from executable
execute_process(
COMMAND "${TEST_EXECUTABLE}" -- --list_tests
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
TIMEOUT "${TEST_DISCOVERY_TIMEOUT}"
OUTPUT_VARIABLE _boost_test_output
RESULT_VARIABLE _boost_test_result
)

if (NOT ${_boost_test_result} EQUAL 0)
message(FATAL_ERROR
"Error listing test content from executable ${TEST_EXECUTABLE}.\n"
"Result: ${_boost_test_result}\n"
"Output:\n"
" ${_boost_test_output}"
)
endif()

string(REPLACE "\n" ";" _boost_test_output "${_boost_test_output}")

# Parse output
foreach(line ${_boost_test_output})
set(test ${line})
set(pretty_test ${test})
string(REGEX REPLACE "^MANUAL_" "" pretty_test "${pretty_test}")
add_command(add_test
"\"${pretty_test}\""
"${TEST_EXECUTABLE}"
"\"--run_test=${line}\""
)
if (line MATCHES "^MANUAL_")
add_command(set_tests_properties
"${pretty_test}"
PROPERTIES DISABLED TRUE
)
endif()
add_command(set_tests_properties
"\"${pretty_test}\""
PROPERTIES
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
LABELS "${TEST_TARGET}"
)
endforeach()

# Write CTest script
file(WRITE "${CTEST_FILE}" "${script}")
24 changes: 22 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ set(DISRUPTOR_VERSION "${DISRUPTOR_VERSION_MAJOR}.${DISRUPTOR_VERSION_MINOR}.${D

enable_testing()

option(DISRUPTOR_BUILD_TESTS "Enable the build of tests and samples." OFF)
option(DISRUPTOR_BUILD_STATIC "Enable the build of the disruptor static library." ON)
option(DISRUPTOR_BUILD_SHARED "Enable the build of the disruptor static library." OFF)
option(DISRUPTOR_CONAN "Try to use conan to fetch dependencies." ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)

if (DISRUPTOR_CONAN)
if (EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
set(WITH_CONAN "conan")
message(STATUS "Using conan for dependencies.")
endif()
endif()

# enable gcc specific stuff
if(CMAKE_COMPILER_IS_GNUCXX)
set(COMPILER_FLAGS "-std=c++14")
Expand Down Expand Up @@ -46,8 +62,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILER_FLAGS} ${WARNING_FLAGS}")
add_subdirectory(Disruptor)

if(DISRUPTOR_BUILD_TESTS)
add_subdirectory(googletest-release-1.8.0)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
message(STATUS
"Building tests and samples."
)
if (NOT WITH_CONAN)
add_subdirectory(googletest-release-1.8.0)
endif()

add_subdirectory(Disruptor.PerfTests)
add_subdirectory(Disruptor.TestTools)
Expand Down
20 changes: 7 additions & 13 deletions Disruptor.PerfTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
project(Disruptor.PerfTests)
cmake_minimum_required(VERSION 2.6)


find_package(Boost COMPONENTS system thread date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
endif()

include_directories("..")


set(DisruptorPerfTests_sources

main.cpp
Expand Down Expand Up @@ -52,9 +42,13 @@ set(DisruptorPerfTests_sources
ValueMutationEventHandler.cpp
)

add_definitions(-DBOOST_TEST_DYN_LINK)

add_executable(Disruptor.PerfTests ${DisruptorPerfTests_sources})
target_link_libraries(Disruptor.PerfTests DisruptorStatic Disruptor.TestTools ${Boost_LIBRARIES} pthread)

target_link_libraries(Disruptor.PerfTests
PRIVATE
Disruptor
Disruptor.TestTools
pthread
)

add_custom_target(performance_test ${CMAKE_CURRENT_BINARY_DIR}/Disruptor.PerfTests)
47 changes: 47 additions & 0 deletions Disruptor.TestTools/BoostTestOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <boost/test/tree/traverse.hpp>
#include <boost/test/tree/decorator.hpp>

namespace Disruptor
{
namespace Tests
{

static const boost::unit_test::label Manual("MANUAL");

struct BoostTestsListerVisitor : boost::unit_test::test_tree_visitor
{
void visit(const boost::unit_test::test_case& test)
{
static const auto& master = boost::unit_test::framework::master_test_suite();
static const auto& masterName = master.p_name.get();

auto fullName = test.full_name();
auto pos = fullName.find(masterName);
if (pos != std::string::npos)
fullName.erase(pos, masterName.length());

if (test.has_label("MANUAL"))
std::cout << "MANUAL_";
std::cout << fullName << std::endl;
}
};

inline void addAdditionalCommandLineOptions()
{
int argc = boost::unit_test::framework::master_test_suite().argc;
for (int i = 0; i < argc; i++)
{
std::string argument(boost::unit_test::framework::master_test_suite().argv[i]);
if (argument == "--list_tests")
{
BoostTestsListerVisitor visitor;
boost::unit_test::traverse_test_tree(boost::unit_test::framework::master_test_suite(), visitor);
exit(0);
}
}
}

}
}
15 changes: 5 additions & 10 deletions Disruptor.TestTools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
project(Disruptor.TestTools)
cmake_minimum_required(VERSION 2.6)


find_package(Boost COMPONENTS system)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
endif()

include_directories("..")


set(DisruptorTestTools_sources

CountdownEvent.cpp
Expand All @@ -22,3 +12,8 @@ set(DisruptorTestTools_sources
)

add_library(Disruptor.TestTools STATIC ${DisruptorTestTools_sources})

target_link_libraries(Disruptor.TestTools
PUBLIC
Disruptor
)
27 changes: 16 additions & 11 deletions Disruptor.Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
project(Disruptor.Tests)
cmake_minimum_required(VERSION 2.6)

find_package(Boost COMPONENTS system chrono thread unit_test_framework)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
endif()
include (BoostTest)

ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK)
if (WITH_CONAN)
set(GMock_LIBRARY CONAN_PKG::gtest)
set(Boost_Libraries CONAN_PKG::boost)
else()
set(GMock_LIBRARY gtest gmock)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
set(Boost_Libraries Boost::unit_test_framwork)
endif()

set(DisruptorTests_sources

Expand Down Expand Up @@ -57,10 +60,12 @@ set(DisruptorTests_sources
YieldingWaitStrategyTests.cpp
)

include_directories("..")

add_executable(Disruptor.Tests ${DisruptorTests_sources})
target_link_libraries(Disruptor.Tests DisruptorStatic Disruptor.TestTools ${Boost_LIBRARIES} gmock)

add_test(cmake_Disruptor.Tests ${CMAKE_CURRENT_BINARY_DIR}/Disruptor.Tests --result_code=no --report_level=no)
boost_test_discover_tests(Disruptor.Tests)

target_link_libraries(Disruptor.Tests
PRIVATE
Disruptor.TestTools
${GMock_LIBRARY}
${Boost_LIBRARIES}
)
6 changes: 5 additions & 1 deletion Disruptor.Tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <gmock/gmock.h>

#include "Disruptor.TestTools/BoostTestOptions.h"

#if _MSC_VER
# pragma warning (disable: 4231) // nonstandard extension used : 'extern' before template explicit instantiation
#endif
Expand All @@ -21,6 +23,9 @@ struct GlobalFixture

testing::GTEST_FLAG(throw_on_failure) = true;
testing::InitGoogleMock(&boost::unit_test::framework::master_test_suite().argc, boost::unit_test::framework::master_test_suite().argv);

Disruptor::Tests::addAdditionalCommandLineOptions();

}

~GlobalFixture()
Expand All @@ -29,4 +34,3 @@ struct GlobalFixture
};

BOOST_GLOBAL_FIXTURE(GlobalFixture);

Loading