Skip to content

Commit 091f373

Browse files
authored
Split out chimera library and setup unittest (#233)
1 parent 12f35f2 commit 091f373

File tree

95 files changed

+36018
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+36018
-98
lines changed

.ci/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [ "$OS_NAME" = "linux" ] && [ $(lsb_release -sc) = "bionic" ]; then
1818
make check-format
1919
fi
2020

21-
make -j4 binding_tests
21+
make -j4 tests binding_tests
2222

2323
if [ $BUILD_NAME = TRUSTY_GCC_DEBUG ]; then
2424
make chimera_coverage

CMakeLists.txt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ include_directories("${PROJECT_SOURCE_DIR}/include")
7474
# Set headers and sources
7575
set(${PROJECT_NAME}_HEADERS
7676
include/chimera/binding.h
77+
include/chimera/chimera.h
7778
include/chimera/configuration.h
7879
include/chimera/consumer.h
7980
include/chimera/frontend_action.h
@@ -103,30 +104,29 @@ llvm_map_components_to_libnames(llvm_libs
103104
# Build the external tools: Mustache and Cling.
104105
add_subdirectory(external)
105106

106-
# Build the main chimera executable.
107-
add_executable("${PROJECT_NAME}"
107+
# Build the chimera library.
108+
add_library(libchimera
108109
${${PROJECT_NAME}_HEADERS} # not required, but helpful for some IDEs
109110
${${PROJECT_NAME}_SOURCES}
110111
)
111-
112-
target_compile_options("${PROJECT_NAME}"
113-
PUBLIC "-std=c++11"
114-
)
115-
116-
target_link_libraries("${PROJECT_NAME}"
117-
${YAMLCPP_LIBRARIES}
118-
${CLANG_LIBS}
119-
${llvm_libs}
112+
target_compile_options(libchimera PUBLIC "-std=c++11")
113+
target_link_libraries(libchimera
114+
PRIVATE
115+
${YAMLCPP_LIBRARIES}
116+
${CLANG_LIBS}
117+
${llvm_libs}
120118
)
119+
target_link_libraries(libchimera PRIVATE mstch cling_utils)
120+
target_link_libraries(libchimera PRIVATE chimera_bindings)
121+
clang_format_add_sources(${${PROJECT_NAME}_HEADERS} ${${PROJECT_NAME}_SOURCES})
121122

122-
target_link_libraries("${PROJECT_NAME}"
123-
mstch
124-
cling_utils
125-
)
123+
# Build the main chimera executable.
124+
add_executable(chimera src/chimera_main.cpp)
125+
target_link_libraries(chimera PUBLIC libchimera)
126+
clang_format_add_sources(src/chimera_main.cpp)
126127

127128
# Generate built-in template bindings for supported languages.
128129
add_subdirectory(bindings)
129-
target_link_libraries(chimera chimera_bindings)
130130

131131
# Add main sources for code formatting
132132
clang_format_add_sources(
@@ -169,5 +169,5 @@ add_subdirectory(test EXCLUDE_FROM_ALL)
169169
## CODE FORMATTING ##
170170
#####################
171171

172-
# Define 'format' target that runs ClangFormat
172+
# Define 'format' and 'check-format' targets
173173
clang_format_add_targets()

include/chimera/chimera.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __CHIMERA_H__
2+
#define __CHIMERA_H__
3+
4+
namespace chimera
5+
{
6+
7+
int run(int argc, const char **argv);
8+
9+
} // namespace chimera
10+
11+
#endif // __CHIMERA_H__

src/chimera.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Chimera - a tool to convert c++ headers into Boost.Python bindings.
33
*/
4+
#include "chimera/chimera.h"
45
#include "chimera/configuration.h"
56
#include "chimera/frontend_action.h"
67

@@ -18,6 +19,9 @@ using namespace clang;
1819
using namespace clang::tooling;
1920
using namespace llvm;
2021

22+
namespace chimera
23+
{
24+
2125
// Apply a custom category to all command-line options so that they are the
2226
// only ones displayed.
2327
static cl::OptionCategory ChimeraCategory("Chimera options");
@@ -72,7 +76,7 @@ static cl::extrahelp MoreHelp(
7276
"Chimera is a tool to convert C++ headers into Boost.Python bindings.\n"
7377
"\n");
7478

75-
int main(int argc, const char **argv)
79+
int run(int argc, const char **argv)
7680
{
7781
// Create parser that handles clang options.
7882
CommonOptionsParser OptionsParser(argc, argv, ChimeraCategory);
@@ -129,3 +133,5 @@ int main(int argc, const char **argv)
129133
// Run the instantiated tool on the Chimera frontend.
130134
return Tool.run(newFrontendActionFactory<chimera::FrontendAction>().get());
131135
}
136+
137+
} // namespace chimera

src/chimera_main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Chimera - a tool to convert c++ headers into Boost.Python bindings.
3+
*/
4+
#include "chimera/chimera.h"
5+
6+
int main(int argc, const char **argv)
7+
{
8+
chimera::run(argc, argv);
9+
}

test/CMakeLists.txt

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,82 @@
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+
)
812

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)
1425
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
2329
)
2430

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
2742

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
4760
)
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)
5563
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()
6064

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})
6866

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()
7768

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

Comments
 (0)