aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReid Kleckner <rnk@google.com>2025-04-29 06:32:03 -0700
committerGitHub <noreply@github.com>2025-04-29 06:32:03 -0700
commitdb2315afa8db1153e3b85d452cd14d5a1b957350 (patch)
treef951cb6fbeffa29b2b160992f11ff32672cd92c5
parent6695976d16d02d72afc249bb5b46d267c1eee458 (diff)
downloadllvm-db2315afa8db1153e3b85d452cd14d5a1b957350.zip
llvm-db2315afa8db1153e3b85d452cd14d5a1b957350.tar.gz
llvm-db2315afa8db1153e3b85d452cd14d5a1b957350.tar.bz2
[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 <phosek@google.com>
-rw-r--r--clang/lib/CodeGen/BackendUtil.cpp5
-rw-r--r--clang/unittests/CMakeLists.txt54
-rw-r--r--clang/unittests/Driver/ModuleCacheTest.cpp2
-rw-r--r--clang/unittests/Frontend/OutputStreamTest.cpp5
-rw-r--r--clang/unittests/Interpreter/CMakeLists.txt2
-rw-r--r--clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt2
-rw-r--r--clang/unittests/Parse/CMakeLists.txt13
7 files changed, 66 insertions, 17 deletions
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index f7eb853..2271f01 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -304,7 +304,7 @@ getCodeModel(const CodeGenOptions &CodeGenOpts) {
.Case("kernel", llvm::CodeModel::Kernel)
.Case("medium", llvm::CodeModel::Medium)
.Case("large", llvm::CodeModel::Large)
- .Case("default", ~1u)
+ .Cases("default", "", ~1u)
.Default(~0u);
assert(CodeModel != ~0u && "invalid code model!");
if (CodeModel == ~1u)
@@ -617,7 +617,8 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
return;
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
Options, RM, CM, OptLevel));
- TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
+ if (TM)
+ TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
}
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt
index f3823ba..b4114d4 100644
--- a/clang/unittests/CMakeLists.txt
+++ b/clang/unittests/CMakeLists.txt
@@ -15,11 +15,11 @@ if(CLANG_BUILT_STANDALONE)
endif()
endif()
-# add_clang_unittest(test_name file1.cpp file2.cpp)
+# add_distinct_clang_unittest(test_name file1.cpp file2.cpp)
#
# Will compile the list of files together and link against the clang
# Produces a binary named 'basename(test_name)'.
-function(add_clang_unittest test_name)
+function(add_distinct_clang_unittest test_name)
cmake_parse_arguments(ARG
""
""
@@ -47,6 +47,34 @@ function(add_clang_unittest test_name)
target_link_libraries(${test_name} PRIVATE ${ARG_LINK_LIBS})
endfunction()
+set(doc_opts BRIEF_DOCS "<internal setting>" FULL_DOCS "<internal settings>")
+define_property(GLOBAL PROPERTY CLANG_UNITTEST_SRCS ${doc_opts})
+define_property(GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS ${doc_opts})
+define_property(GLOBAL PROPERTY CLANG_UNITTEST_CLANG_LIBS ${doc_opts})
+define_property(GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS ${doc_opts})
+
+# add_clang_unittest(test_name file1.cpp file2.cpp)
+#
+# Adds unittests to the combined AllClangUnitTests binary. The unittest binary
+# is defined after adding all unittest subdirectories.
+function(add_clang_unittest test_name)
+ cmake_parse_arguments(ARG
+ ""
+ ""
+ "CLANG_LIBS;LINK_LIBS;LLVM_COMPONENTS"
+ ${ARGN})
+
+ file(RELATIVE_PATH src_prefix "${CMAKE_CURRENT_FUNCTION_LIST_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
+ set(srcs_prefixed)
+ foreach(src ${ARG_UNPARSED_ARGUMENTS})
+ set(srcs_prefixed ${srcs_prefixed} "${src_prefix}/${src}")
+ endforeach()
+ set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_SRCS ${srcs_prefixed})
+ set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_CLANG_LIBS ${ARG_CLANG_LIBS})
+ set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_LINK_LIBS ${ARG_LINK_LIBS})
+ set_property(GLOBAL APPEND PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS ${ARG_LLVM_COMPONENTS})
+endfunction()
+
add_subdirectory(Basic)
add_subdirectory(Lex)
add_subdirectory(Parse)
@@ -77,3 +105,25 @@ add_subdirectory(Index)
add_subdirectory(InstallAPI)
add_subdirectory(Serialization)
add_subdirectory(Support)
+
+
+# If we're doing a single merged clang unit test binary, add that target after
+# all the previous subdirectories have been processed.
+get_property(SRCS GLOBAL PROPERTY CLANG_UNITTEST_SRCS)
+get_property(CLANG_LIBS GLOBAL PROPERTY CLANG_UNITTEST_CLANG_LIBS)
+get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
+get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
+add_distinct_clang_unittest(AllClangUnitTests
+ ${SRCS}
+ CLANG_LIBS
+ ${CLANG_LIBS}
+ LINK_LIBS
+ ${LINK_LIBS}
+ LLVM_COMPONENTS
+ ${LLVM_COMPONENTS}
+)
+
+# The Tooling library has some internal headers. Make those work. If we like
+# the merged clang unit test binary, we can update the include paths and make
+# this the default.
+include_directories(Tooling)
diff --git a/clang/unittests/Driver/ModuleCacheTest.cpp b/clang/unittests/Driver/ModuleCacheTest.cpp
index 4874441..7aa5047 100644
--- a/clang/unittests/Driver/ModuleCacheTest.cpp
+++ b/clang/unittests/Driver/ModuleCacheTest.cpp
@@ -17,7 +17,7 @@ using namespace clang::driver;
namespace {
-TEST(ModuleCacheTest, GetTargetAndMode) {
+TEST(DriverModuleCacheTest, GetTargetAndMode) {
SmallString<128> Buf;
Driver::getDefaultModuleCachePath(Buf);
StringRef Path = Buf;
diff --git a/clang/unittests/Frontend/OutputStreamTest.cpp b/clang/unittests/Frontend/OutputStreamTest.cpp
index fa5d726..27d2d7f 100644
--- a/clang/unittests/Frontend/OutputStreamTest.cpp
+++ b/clang/unittests/Frontend/OutputStreamTest.cpp
@@ -8,11 +8,11 @@
#include "clang/Basic/LangStandard.h"
#include "clang/CodeGen/BackendUtil.h"
-#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/FrontendTool/Utils.h"
#include "clang/Lex/PreprocessorOptions.h"
+#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "gtest/gtest.h"
@@ -23,6 +23,7 @@ using namespace clang::frontend;
namespace {
TEST(FrontendOutputTests, TestOutputStream) {
+ llvm::InitializeAllTargetMCs();
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("").release());
@@ -47,6 +48,7 @@ TEST(FrontendOutputTests, TestOutputStream) {
}
TEST(FrontendOutputTests, TestVerboseOutputStreamShared) {
+ llvm::InitializeAllTargetMCs();
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("invalid").release());
@@ -77,6 +79,7 @@ TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) {
std::string VerboseBuffer;
bool Success;
{
+ llvm::InitializeAllTargetMCs();
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("invalid").release());
diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt
index 9df1a4b..1dda902 100644
--- a/clang/unittests/Interpreter/CMakeLists.txt
+++ b/clang/unittests/Interpreter/CMakeLists.txt
@@ -1,4 +1,4 @@
-add_clang_unittest(ClangReplInterpreterTests
+add_distinct_clang_unittest(ClangReplInterpreterTests
IncrementalCompilerBuilderTest.cpp
IncrementalProcessingTest.cpp
InterpreterTest.cpp
diff --git a/clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt b/clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt
index eb366a8..dfd94d8 100644
--- a/clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt
+++ b/clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt
@@ -3,7 +3,7 @@
set(LLVM_REQUIRES_EH ON)
set(LLVM_REQUIRES_RTTI ON)
-add_clang_unittest(ClangReplInterpreterExceptionTests
+add_distinct_clang_unittest(ClangReplInterpreterExceptionTests
InterpreterExceptionTest.cpp
EXPORT_SYMBOLS
diff --git a/clang/unittests/Parse/CMakeLists.txt b/clang/unittests/Parse/CMakeLists.txt
index 2a31be6..6859efe 100644
--- a/clang/unittests/Parse/CMakeLists.txt
+++ b/clang/unittests/Parse/CMakeLists.txt
@@ -1,20 +1,15 @@
-set(LLVM_LINK_COMPONENTS
- Support
- )
add_clang_unittest(ParseTests
ParseHLSLRootSignatureTest.cpp
- )
-clang_target_link_libraries(ParseTests
- PRIVATE
+ CLANG_LIBS
clangAST
clangBasic
clangLex
clangParse
clangSema
- )
-target_link_libraries(ParseTests
- PRIVATE
+ LINK_LIBS
LLVMTestingAnnotations
LLVMTestingSupport
clangTesting
+ LLVM_COMPONENTS
+ Support
)