[clang] Only build clang-tblgen if it is actually needed

It's possible to build clang with an existing clang-tblgen (common when
cross-compiling, for instance) by setting CLANG_TABLEGEN_EXE.  If this
is the case there's no need to build it, as it won't be used.

Upstream-Status: Submitted [https://github.com/llvm/llvm-project/pull/161952]
Signed-off-by: Ross Burton <ross.burton@arm.com>

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index e4cb1a359620..b650b3b986f4 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -479,7 +479,9 @@ option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
 # While HLSL support is experimental this should stay hidden.
 mark_as_advanced(CLANG_ENABLE_HLSL)
 
-add_subdirectory(utils/TableGen)
+if (NOT DEFINED CLANG_TABLEGEN_EXE OR CLANG_INCLUDE_TESTS)
+  add_subdirectory(utils/TableGen)
+endif()
 
 # Export CLANG_TABLEGEN_EXE for use by flang docs.
 set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
