Skip to content

Commit b9e6adc

Browse files
committed
[cmake] Refactor out unittest configuration logic from unittests/CMakeLists.txt => cmake/modules/AddSwiftUnittests.cmake.
rdar://24717107
1 parent 6f139d7 commit b9e6adc

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
include(AddSwift)
3+
4+
add_custom_target(SwiftUnitTests)
5+
6+
set_target_properties(SwiftUnitTests PROPERTIES FOLDER "Tests")
7+
8+
function(add_swift_unittest test_dirname)
9+
# *NOTE* Even though "add_unittest" does not have llvm in its name, it is a
10+
# function defined by AddLLVM.cmake.
11+
add_unittest(SwiftUnitTests ${test_dirname} ${ARGN})
12+
13+
if(SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
14+
# Replace target references with full paths, so that we use LLVM's
15+
# build configuration rather than Swift's.
16+
get_target_property(libnames ${test_dirname} LINK_LIBRARIES)
17+
18+
set(new_libnames)
19+
foreach(dep ${libnames})
20+
if("${dep}" MATCHES "^(LLVM|Clang|gtest)")
21+
list(APPEND new_libnames "${LLVM_LIBRARY_OUTPUT_INTDIR}/lib${dep}.a")
22+
else()
23+
list(APPEND new_libnames "${dep}")
24+
endif()
25+
endforeach()
26+
27+
set_property(TARGET ${test_dirname} PROPERTY LINK_LIBRARIES ${new_libnames})
28+
swift_common_llvm_config(${test_dirname} support)
29+
endif()
30+
31+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
32+
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
33+
LINK_FLAGS " -Xlinker -rpath -Xlinker ${SWIFT_LIBRARY_OUTPUT_INTDIR}/swift/macosx")
34+
35+
if(SWIFT_ANALYZE_CODE_COVERAGE)
36+
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
37+
LINK_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
38+
endif()
39+
elseif(${SWIFT_ENABLE_GOLD_LINKER})
40+
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
41+
LINK_FLAGS " -fuse-ld=gold")
42+
endif()
43+
endfunction()
44+
45+

unittests/CMakeLists.txt

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
add_custom_target(SwiftUnitTests)
21

3-
set_target_properties(SwiftUnitTests PROPERTIES FOLDER "Tests")
4-
5-
function(add_swift_unittest test_dirname)
6-
add_unittest(SwiftUnitTests ${test_dirname} ${ARGN})
7-
8-
if(SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
9-
# Replace target references with full paths, so that we use LLVM's
10-
# build configuration rather than Swift's.
11-
get_target_property(libnames ${test_dirname} LINK_LIBRARIES)
12-
13-
set(new_libnames)
14-
foreach(dep ${libnames})
15-
if("${dep}" MATCHES "^(LLVM|Clang|gtest)")
16-
list(APPEND new_libnames "${LLVM_LIBRARY_OUTPUT_INTDIR}/lib${dep}.a")
17-
else()
18-
list(APPEND new_libnames "${dep}")
19-
endif()
20-
endforeach()
21-
22-
set_property(TARGET ${test_dirname} PROPERTY LINK_LIBRARIES ${new_libnames})
23-
swift_common_llvm_config(${test_dirname} support)
24-
endif()
25-
26-
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
27-
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
28-
LINK_FLAGS " -Xlinker -rpath -Xlinker ${SWIFT_LIBRARY_OUTPUT_INTDIR}/swift/macosx")
29-
30-
if(SWIFT_ANALYZE_CODE_COVERAGE)
31-
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
32-
LINK_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
33-
endif()
34-
elseif(${SWIFT_ENABLE_GOLD_LINKER})
35-
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
36-
LINK_FLAGS " -fuse-ld=gold")
37-
endif()
38-
endfunction()
2+
include(AddSwiftUnittests)
393

404
if(SWIFT_BUILD_TOOLS)
415
# We can't link C++ unit tests unless we build the tools.

0 commit comments

Comments
 (0)