Skip to content

Commit a5d42f8

Browse files
committed
Factor the logic for setting up a GoogleTest unit test executable into
a helper function in CMake. This will allow us to share all of this logic with Clang, and eventually CompilerRT. llvm-svn: 158896
1 parent 73af5c6 commit a5d42f8

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,53 @@ macro(add_llvm_external_project name)
147147
endif()
148148
endif()
149149
endmacro(add_llvm_external_project)
150+
151+
# Generic support for adding a unittest.
152+
function(add_unittest test_suite test_dirname)
153+
string(REGEX MATCH "([^/]+)$" test_name ${test_dirname})
154+
if (CMAKE_BUILD_TYPE)
155+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
156+
${CMAKE_CURRENT_BINARY_DIR}/${test_dirname}/${CMAKE_BUILD_TYPE})
157+
else()
158+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
159+
${CMAKE_CURRENT_BINARY_DIR}/${test_dirname})
160+
endif()
161+
if( NOT LLVM_BUILD_TESTS )
162+
set(EXCLUDE_FROM_ALL ON)
163+
endif()
164+
165+
add_llvm_executable(${test_name} ${ARGN})
166+
target_link_libraries(${test_name}
167+
gtest
168+
gtest_main
169+
LLVMSupport # gtest needs it for raw_ostream.
170+
)
171+
172+
add_dependencies(${test_suite} ${test_name})
173+
get_target_property(test_suite_folder ${test_suite} FOLDER)
174+
if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
175+
set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
176+
endif ()
177+
178+
# Visual Studio 2012 only supports up to 8 template parameters in
179+
# std::tr1::tuple by default, but gtest requires 10
180+
if (MSVC AND MSVC_VERSION EQUAL 1700)
181+
set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
182+
endif ()
183+
184+
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
185+
set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
186+
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
187+
set_property(TARGET ${test_name} APPEND_STRING PROPERTY COMPILE_FLAGS " -fno-rtti")
188+
elseif (MSVC)
189+
set_property(TARGET ${test_name} APPEND_STRING PROPERTY COMPILE_FLAGS " /GR-")
190+
endif ()
191+
192+
if (NOT LLVM_ENABLE_THREADS)
193+
set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
194+
endif ()
195+
196+
if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
197+
set_property(TARGET ${test_name} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-variadic-macros")
198+
endif ()
199+
endfunction()

llvm/unittests/CMakeLists.txt

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,9 @@
1-
function(add_llvm_unittest test_dirname)
2-
string(REGEX MATCH "([^/]+)$" test_name ${test_dirname})
3-
if (CMAKE_BUILD_TYPE)
4-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
5-
${LLVM_BINARY_DIR}/unittests/${test_dirname}/${CMAKE_BUILD_TYPE})
6-
else()
7-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
8-
${LLVM_BINARY_DIR}/unittests/${test_dirname})
9-
endif()
10-
if( NOT LLVM_BUILD_TESTS )
11-
set(EXCLUDE_FROM_ALL ON)
12-
endif()
13-
14-
add_llvm_executable(${test_name} ${ARGN})
15-
target_link_libraries(${test_name}
16-
gtest
17-
gtest_main
18-
LLVMSupport # gtest needs it for raw_ostream.
19-
)
20-
21-
add_dependencies(UnitTests ${test_name})
22-
set_target_properties(${test_name} PROPERTIES FOLDER "Tests")
23-
endfunction()
24-
25-
# Visual Studio 2012 only supports up to 8 template parameters in
26-
# std::tr1::tuple by default, but gtest requires 10
27-
if(MSVC AND MSVC_VERSION EQUAL 1700)
28-
add_definitions(-D_VARIADIC_MAX=10)
29-
endif ()
30-
311
add_custom_target(UnitTests)
322
set_target_properties(UnitTests PROPERTIES FOLDER "Tests")
333

34-
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
35-
add_definitions(-DGTEST_HAS_RTTI=0)
36-
if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
37-
llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti")
38-
elseif( MSVC )
39-
llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-")
40-
endif()
41-
42-
if (NOT LLVM_ENABLE_THREADS)
43-
add_definitions(-DGTEST_HAS_PTHREAD=0)
44-
endif()
45-
46-
if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
47-
add_definitions("-Wno-variadic-macros")
48-
endif()
4+
function(add_llvm_unittest test_dirname)
5+
add_unittest(UnitTests ${test_dirname} ${ARGN})
6+
endfunction()
497

508
set(LLVM_LINK_COMPONENTS
519
jit

0 commit comments

Comments
 (0)