Fix CLANG_ENABLE_STATIC_ANALYZER=OFF building all analyzer source

Since https://reviews.llvm.org/D87118, the StaticAnalyzer directory is
added unconditionally. In theory this should not cause the static analyzer
sources to be built unless they are referenced by another target. However,
the clang-cpp target (defined in clang/tools/clang-shlib) uses the
CLANG_STATIC_LIBS global property to determine which libraries need to
be included. To solve this issue, this patch avoids adding libraries to
that property if EXCLUDE_FROM_ALL is set.

In case something like this comes up again: `cmake --graphviz=targets.dot`
is quite useful to see why a target is included as part of `ninja all`.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D109611
This commit is contained in:
Alex Richardson 2021-09-20 12:32:56 +01:00
parent 7fc12b822c
commit 6d7b3d6b3a
3 changed files with 28 additions and 3 deletions

View File

@ -100,7 +100,12 @@ macro(add_clang_library name)
# The Xcode generator doesn't handle object libraries correctly.
list(APPEND LIBTYPE OBJECT)
endif()
set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
if (NOT EXCLUDE_FROM_ALL)
# Only include libraries that don't have EXCLUDE_FROM_ALL set. This
# ensure that the clang static analyzer libraries are not compiled
# as part of clang-shlib if CLANG_ENABLE_STATIC_ANALYZER=OFF.
set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
endif()
endif()
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
@ -110,8 +115,11 @@ macro(add_clang_library name)
endif()
foreach(lib ${libs})
if(TARGET ${lib})
if(TARGET ${lib})
target_link_libraries(${lib} INTERFACE ${LLVM_COMMON_LIBS})
if (EXCLUDE_FROM_ALL)
continue()
endif()
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
get_target_export_arg(${name} Clang export_to_clangtargets UMBRELLA clang-libraries)

View File

@ -1,3 +1,10 @@
# These directories can significantly impact build time, only build
# them if anything depends on the clangStaticAnalyzer* libraries.
if(NOT CLANG_ENABLE_STATIC_ANALYZER)
set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON)
set(EXCLUDE_FROM_ALL ON)
endif()
add_subdirectory(Core)
add_subdirectory(Checkers)
add_subdirectory(Frontend)

View File

@ -429,10 +429,13 @@ endfunction(set_windows_version_resource_properties)
# This is used to specify that this is a component library of
# LLVM which means that the source resides in llvm/lib/ and it is a
# candidate for inclusion into libLLVM.so.
# EXCLUDE_FROM_ALL
# Do not build this library as part of the default target, only
# if explicitly requested or when linked against.
# )
function(llvm_add_library name)
cmake_parse_arguments(ARG
"MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
"MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB;EXCLUDE_FROM_ALL"
"OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
${ARGN})
@ -535,6 +538,9 @@ function(llvm_add_library name)
# FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY.
set(ARG_STATIC)
if(ARG_EXCLUDE_FROM_ALL OR EXCLUDE_FROM_ALL)
set_target_properties(${name_static} PROPERTIES EXCLUDE_FROM_ALL ON)
endif()
endif()
if(ARG_MODULE)
@ -546,6 +552,10 @@ function(llvm_add_library name)
add_library(${name} STATIC ${ALL_FILES})
endif()
if(ARG_EXCLUDE_FROM_ALL OR EXCLUDE_FROM_ALL)
set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL ON)
endif()
if(ARG_COMPONENT_LIB)
set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE)
set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name})