|
1 | | -include(CTest) |
2 | | -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") |
3 | | - |
4 | | -set(CMAKE_INCLUDE_CURRENT_DIR ON) |
5 | | -set(chimera_EXECUTABLE $<TARGET_FILE:chimera>) |
6 | | -include(chimeraFunctions) |
7 | | -include(chimeraTest) |
| 1 | +#=============================================================================== |
| 2 | +# Emulator |
| 3 | +#=============================================================================== |
| 4 | +add_library(emulator emulator.h emulator.cpp) |
| 5 | +target_link_libraries(emulator PUBLIC libchimera) |
| 6 | +clang_format_add_sources(emulator.h emulator.cpp) |
| 7 | +target_compile_definitions(emulator |
| 8 | + PUBLIC |
| 9 | + EXAMPLES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/examples/" |
| 10 | + BUILD_PATH="${CMAKE_BINARY_DIR}" |
| 11 | +) |
8 | 12 |
|
9 | | -# Set python version to be used to build bindings for. |
10 | | -if(NOT CHIMERA_TEST_PYTHON_VERSION) |
11 | | - set(CHIMERA_TEST_PYTHON_VERSION 3.4 CACHE STRING |
12 | | - "Choose the target Python version (e.g., 3.4, 2.7)" FORCE |
13 | | - ) |
| 13 | +#=============================================================================== |
| 14 | +# GoogleTest setup |
| 15 | +add_library(gtest STATIC gtest/src/gtest-all.cc) |
| 16 | +add_library(gtest_main STATIC gtest/src/gtest_main.cc) |
| 17 | +target_include_directories(gtest |
| 18 | + PUBLIC |
| 19 | + "${CMAKE_CURRENT_SOURCE_DIR}/gtest" |
| 20 | + "${CMAKE_CURRENT_SOURCE_DIR}/gtest/include" |
| 21 | +) |
| 22 | +target_link_libraries(gtest_main gtest) |
| 23 | +if(NOT WIN32) |
| 24 | + target_link_libraries(gtest pthread) |
14 | 25 | endif() |
15 | | - |
16 | | -# Find PythonInterp |
17 | | -find_package(PythonInterp ${CHIMERA_TEST_PYTHON_VERSION} REQUIRED) |
18 | | -execute_process(COMMAND ${PYTHON_EXECUTABLE} -c |
19 | | - "from distutils.sysconfig import get_python_lib;\ |
20 | | - print(get_python_lib(plat_specific=True, prefix=''))" |
21 | | - OUTPUT_VARIABLE PYTHON_SITE_PACKAGES |
22 | | - OUTPUT_STRIP_TRAILING_WHITESPACE |
| 26 | +set_target_properties( |
| 27 | + gtest PROPERTIES |
| 28 | + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib |
23 | 29 | ) |
24 | 30 |
|
25 | | -# Find PythonLibs version that is the same with the version of PythonInterp. |
26 | | -find_package(PythonLibs ${CHIMERA_TEST_PYTHON_VERSION} REQUIRED) |
| 31 | +#=============================================================================== |
| 32 | +# This function uses following global properties: |
| 33 | +# - CHIMERA_UNITTESTS |
| 34 | +# - CHIMERA_CPP_TESTS |
| 35 | +# |
| 36 | +# Usage: |
| 37 | +# chimera_add_test(test_UnitTestA) # assumed source is test_UnitTestA.cpp |
| 38 | +# chimera_add_test(test_UnitTestB test_SourceB1.cpp) |
| 39 | +# chimera_add_test(test_UnitTestA test_SourceC1.cpp test_SourceC2.cpp) |
| 40 | +#=============================================================================== |
| 41 | +function(chimera_add_test target_name) # ARGN for source files |
27 | 42 |
|
28 | | -# Find boost with python components. The name of python component varies |
29 | | -# depending on the platform, boost version, and python version. |
30 | | -# TODO(JS): Check if thread component is really neccessary |
31 | | -if(APPLE) |
32 | | - find_package(Boost QUIET |
33 | | - COMPONENTS |
34 | | - python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR} thread |
35 | | - ) |
36 | | - set(CHIMERA_TEST_Boost_PYTHON_LIBRARIES |
37 | | - ${Boost_PYTHON${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}_LIBRARIES} |
38 | | - ) |
39 | | -else() # LINUX assumed |
40 | | - if(${PYTHON_VERSION_MAJOR} EQUAL 3) |
41 | | - find_package(Boost QUIET |
42 | | - COMPONENTS |
43 | | - python-py${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR} thread |
44 | | - ) |
45 | | - set(CHIMERA_TEST_Boost_PYTHON_LIBRARIES |
46 | | - ${Boost_PYTHON-PY${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}_LIBRARIES} |
| 43 | + set_property(GLOBAL APPEND PROPERTY CHIMERA_CPP_TESTS ${target_name}) |
| 44 | + |
| 45 | + if(${ARGC} GREATER 2) |
| 46 | + set(sources ${ARGN}) |
| 47 | + else() |
| 48 | + set(sources "${target_name}.cpp") |
| 49 | + endif() |
| 50 | + |
| 51 | + add_executable(${target_name} ${sources}) |
| 52 | + add_test(${target_name} ${target_name}) |
| 53 | + |
| 54 | + if(MSVC) |
| 55 | + target_link_libraries(${target_name} |
| 56 | + libchimera |
| 57 | + emulator |
| 58 | + optimized gtest debug gtestd |
| 59 | + optimized gtest_main debug gtest_maind |
47 | 60 | ) |
48 | | - if(NOT Boost_FOUND) |
49 | | - find_package(Boost QUIET COMPONENTS python3 thread) |
50 | | - set(CHIMERA_TEST_Boost_PYTHON_LIBRARIES ${Boost_PYTHON3_LIBRARIES}) |
51 | | - endif() |
52 | | - else() # Python 2 assumed |
53 | | - find_package(Boost QUIET COMPONENTS python thread) |
54 | | - set(CHIMERA_TEST_Boost_PYTHON_LIBRARIES ${Boost_PYTHON_LIBRARIES}) |
| 61 | + else() |
| 62 | + target_link_libraries(${target_name} libchimera emulator gtest gtest_main) |
55 | 63 | endif() |
56 | | -endif() |
57 | | -if(NOT CHIMERA_TEST_Boost_PYTHON_LIBRARIES) |
58 | | - message(WARNING "Boost.Python is not found. Omitting from unit tests.") |
59 | | -endif() |
60 | 64 |
|
61 | | -# Find pybind11 |
62 | | -# Needs to set PYBIND11_PYTHON_VERSION before finding pybind11 |
63 | | -set(PYBIND11_PYTHON_VERSION ${PYTHON_VERSION_STRING}) |
64 | | -find_package(pybind11 2.2.0 QUIET) |
65 | | -if(NOT pybind11_FOUND) |
66 | | - message(WARNING "pybind11 (>=2.2.0) is not found. Omitting from unit tests.") |
67 | | -endif() |
| 65 | + clang_format_add_sources(${sources}) |
68 | 66 |
|
69 | | -# Add unit tests from subdirectories (roughly in order of complexity). |
70 | | -add_subdirectory(01_function) |
71 | | -add_subdirectory(02_class) |
72 | | -add_subdirectory(03_smart_pointers) |
73 | | -add_subdirectory(04_enumeration) |
74 | | -add_subdirectory(05_variable) |
75 | | -add_subdirectory(99_dart_example) |
76 | | -add_subdirectory(regression) |
| 67 | +endfunction() |
77 | 68 |
|
78 | | -# Add custom target `binding_tests` to build all the tests as a single target |
79 | | -get_property(chimera_pybind11_binding_tests GLOBAL |
80 | | - PROPERTY CHIMERA_PYBIND11_BINDING_TESTS |
81 | | -) |
82 | | -get_property(chimera_boost_python_binding_tests GLOBAL |
83 | | - PROPERTY CHIMERA_BOOST_PYTHON_BINDING_TESTS |
84 | | -) |
85 | | -add_custom_target(binding_tests |
86 | | - DEPENDS |
87 | | - ${chimera_pybind11_binding_tests} |
88 | | - ${chimera_boost_python_binding_tests} |
89 | | -) |
| 69 | +#=============================================================================== |
| 70 | +# Add tests |
| 71 | +#=============================================================================== |
| 72 | +chimera_add_test(test_empty) |
| 73 | +chimera_add_test(test_emulator) |
| 74 | + |
| 75 | +# Add custom target to build all the tests as a single target |
| 76 | +get_property(chimera_cpp_tests GLOBAL PROPERTY CHIMERA_CPP_TESTS) |
| 77 | +add_custom_target(tests DEPENDS ${chimera_cpp_tests}) |
| 78 | + |
| 79 | +#=============================================================================== |
| 80 | +# Add binding tests |
| 81 | +#=============================================================================== |
| 82 | +add_subdirectory(examples) |
0 commit comments