Skip to content

Commit db2315a

Browse files
rnkpetrhosek
andauthored
[clang] Merge gtest binaries into AllClangUnitTests (#134196)
This reduces the size of the clang/unittests build directory by 64% and my overall build dir size by 5%. Static linking is the real driving factor here, but even if the default build configuration used shared libraries, I don't see why we should be building so many unit test binaries. To make the project more approachable for new contributors, I'm attempting to make the build a bit less resource-intensive. Build directory size is a common complaint, and this is low-hanging fruit. I've noticed that incremental builds leave behind the old, stale gtest binaries, and lit will keep running them. This mostly doesn't matter unless they use shared libraries, which will eventually stop working after successive builds. You can clean up the old test binaries with this command in the build directory: $ find tools/clang/unittests/ -iname '*Tests' -type f | xargs rm ... or you can simply clean the build directory in a more holistic way. --------- Co-authored-by: Petr Hosek <[email protected]>
1 parent 6695976 commit db2315a

File tree

7 files changed

+66
-17
lines changed

7 files changed

+66
-17
lines changed

clang/lib/CodeGen/BackendUtil.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ getCodeModel(const CodeGenOptions &CodeGenOpts) {
304304
.Case("kernel", llvm::CodeModel::Kernel)
305305
.Case("medium", llvm::CodeModel::Medium)
306306
.Case("large", llvm::CodeModel::Large)
307-
.Case("default", ~1u)
307+
.Cases("default", "", ~1u)
308308
.Default(~0u);
309309
assert(CodeModel != ~0u && "invalid code model!");
310310
if (CodeModel == ~1u)
@@ -617,7 +617,8 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
617617
return;
618618
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
619619
Options, RM, CM, OptLevel));
620-
TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
620+
if (TM)
621+
TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
621622
}
622623

623624
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,

clang/unittests/CMakeLists.txt

+52-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ if(CLANG_BUILT_STANDALONE)
1515
endif()
1616
endif()
1717

18-
# add_clang_unittest(test_name file1.cpp file2.cpp)
18+
# add_distinct_clang_unittest(test_name file1.cpp file2.cpp)
1919
#
2020
# Will compile the list of files together and link against the clang
2121
# Produces a binary named 'basename(test_name)'.
22-
function(add_clang_unittest test_name)
22+
function(add_distinct_clang_unittest test_name)
2323
cmake_parse_arguments(ARG
2424
""
2525
""
@@ -47,6 +47,34 @@ function(add_clang_unittest test_name)
4747
target_link_libraries(${test_name} PRIVATE ${ARG_LINK_LIBS})
4848
endfunction()
4949

50+
set(doc_opts BRIEF_DOCS "<internal setting>" FULL_DOCS "<internal settings>")
51+
define_property(GLOBAL PROPERTY CLANG_UNITTEST_SRCS ${doc_opts})
52+
define_property(GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS ${doc_opts})
53+
define_property(GLOBAL PROPERTY CLANG_UNITTEST_CLANG_LIBS ${doc_opts})
54+
define_property(GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS ${doc_opts})
55+
56+
# add_clang_unittest(test_name file1.cpp file2.cpp)
57+
#
58+
# Adds unittests to the combined AllClangUnitTests binary. The unittest binary
59+
# is defined after adding all unittest subdirectories.
60+
function(add_clang_unittest test_name)
61+
cmake_parse_arguments(ARG
62+
""
63+
""
64+
"CLANG_LIBS;LINK_LIBS;LLVM_COMPONENTS"
65+
${ARGN})
66+
67+
file(RELATIVE_PATH src_prefix "${CMAKE_CURRENT_FUNCTION_LIST_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
68+
set(srcs_prefixed)
69+
foreach(src ${ARG_UNPARSED_ARGUMENTS})
70+
set(srcs_prefixed ${srcs_prefixed} "${src_prefix}/${src}")
71+
endforeach()
72+
set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_SRCS ${srcs_prefixed})
73+
set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_CLANG_LIBS ${ARG_CLANG_LIBS})
74+
set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_LINK_LIBS ${ARG_LINK_LIBS})
75+
set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS ${ARG_LLVM_COMPONENTS})
76+
endfunction()
77+
5078
add_subdirectory(Basic)
5179
add_subdirectory(Lex)
5280
add_subdirectory(Parse)
@@ -77,3 +105,25 @@ add_subdirectory(Index)
77105
add_subdirectory(InstallAPI)
78106
add_subdirectory(Serialization)
79107
add_subdirectory(Support)
108+
109+
110+
# If we're doing a single merged clang unit test binary, add that target after
111+
# all the previous subdirectories have been processed.
112+
get_property(SRCS GLOBAL PROPERTY CLANG_UNITTEST_SRCS)
113+
get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_UNITTEST_CLANG_LIBS)
114+
get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
115+
get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
116+
add_distinct_clang_unittest(AllClangUnitTests
117+
${SRCS}
118+
CLANG_LIBS
119+
${CLANG_LIBS}
120+
LINK_LIBS
121+
${LINK_LIBS}
122+
LLVM_COMPONENTS
123+
${LLVM_COMPONENTS}
124+
)
125+
126+
# The Tooling library has some internal headers. Make those work. If we like
127+
# the merged clang unit test binary, we can update the include paths and make
128+
# this the default.
129+
include_directories(Tooling)

clang/unittests/Driver/ModuleCacheTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace clang::driver;
1717

1818
namespace {
1919

20-
TEST(ModuleCacheTest, GetTargetAndMode) {
20+
TEST(DriverModuleCacheTest, GetTargetAndMode) {
2121
SmallString<128> Buf;
2222
Driver::getDefaultModuleCachePath(Buf);
2323
StringRef Path = Buf;

clang/unittests/Frontend/OutputStreamTest.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
#include "clang/Basic/LangStandard.h"
1010
#include "clang/CodeGen/BackendUtil.h"
11-
#include "clang/CodeGen/CodeGenAction.h"
1211
#include "clang/Frontend/CompilerInstance.h"
1312
#include "clang/Frontend/TextDiagnosticPrinter.h"
1413
#include "clang/FrontendTool/Utils.h"
1514
#include "clang/Lex/PreprocessorOptions.h"
15+
#include "llvm/Support/TargetSelect.h"
1616
#include "llvm/Support/VirtualFileSystem.h"
1717
#include "gtest/gtest.h"
1818

@@ -23,6 +23,7 @@ using namespace clang::frontend;
2323
namespace {
2424

2525
TEST(FrontendOutputTests, TestOutputStream) {
26+
llvm::InitializeAllTargetMCs();
2627
auto Invocation = std::make_shared<CompilerInvocation>();
2728
Invocation->getPreprocessorOpts().addRemappedFile(
2829
"test.cc", MemoryBuffer::getMemBuffer("").release());
@@ -47,6 +48,7 @@ TEST(FrontendOutputTests, TestOutputStream) {
4748
}
4849

4950
TEST(FrontendOutputTests, TestVerboseOutputStreamShared) {
51+
llvm::InitializeAllTargetMCs();
5052
auto Invocation = std::make_shared<CompilerInvocation>();
5153
Invocation->getPreprocessorOpts().addRemappedFile(
5254
"test.cc", MemoryBuffer::getMemBuffer("invalid").release());
@@ -77,6 +79,7 @@ TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) {
7779
std::string VerboseBuffer;
7880
bool Success;
7981
{
82+
llvm::InitializeAllTargetMCs();
8083
auto Invocation = std::make_shared<CompilerInvocation>();
8184
Invocation->getPreprocessorOpts().addRemappedFile(
8285
"test.cc", MemoryBuffer::getMemBuffer("invalid").release());

clang/unittests/Interpreter/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_clang_unittest(ClangReplInterpreterTests
1+
add_distinct_clang_unittest(ClangReplInterpreterTests
22
IncrementalCompilerBuilderTest.cpp
33
IncrementalProcessingTest.cpp
44
InterpreterTest.cpp

clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set(LLVM_REQUIRES_EH ON)
44
set(LLVM_REQUIRES_RTTI ON)
55

6-
add_clang_unittest(ClangReplInterpreterExceptionTests
6+
add_distinct_clang_unittest(ClangReplInterpreterExceptionTests
77
InterpreterExceptionTest.cpp
88
EXPORT_SYMBOLS
99

clang/unittests/Parse/CMakeLists.txt

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
set(LLVM_LINK_COMPONENTS
2-
Support
3-
)
41
add_clang_unittest(ParseTests
52
ParseHLSLRootSignatureTest.cpp
6-
)
7-
clang_target_link_libraries(ParseTests
8-
PRIVATE
3+
CLANG_LIBS
94
clangAST
105
clangBasic
116
clangLex
127
clangParse
138
clangSema
14-
)
15-
target_link_libraries(ParseTests
16-
PRIVATE
9+
LINK_LIBS
1710
LLVMTestingAnnotations
1811
LLVMTestingSupport
1912
clangTesting
13+
LLVM_COMPONENTS
14+
Support
2015
)

0 commit comments

Comments
 (0)