[clang] 6d7b3d6 - Fix CLANG_ENABLE_STATIC_ANALYZER=OFF building all analyzer source

Alex Richardson via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 20 04:56:20 PDT 2021


Author: Alex Richardson
Date: 2021-09-20T12:55:56+01:00
New Revision: 6d7b3d6b3a8dbd62650b6c3dae1fe904a8ae9048

URL: https://github.com/llvm/llvm-project/commit/6d7b3d6b3a8dbd62650b6c3dae1fe904a8ae9048
DIFF: https://github.com/llvm/llvm-project/commit/6d7b3d6b3a8dbd62650b6c3dae1fe904a8ae9048.diff

LOG: 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

Added: 
    

Modified: 
    clang/cmake/modules/AddClang.cmake
    clang/lib/StaticAnalyzer/CMakeLists.txt
    llvm/cmake/modules/AddLLVM.cmake

Removed: 
    


################################################################################
diff  --git a/clang/cmake/modules/AddClang.cmake b/clang/cmake/modules/AddClang.cmake
index 5752f4277444..c2d46b743b85 100644
--- a/clang/cmake/modules/AddClang.cmake
+++ b/clang/cmake/modules/AddClang.cmake
@@ -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)

diff  --git a/clang/lib/StaticAnalyzer/CMakeLists.txt b/clang/lib/StaticAnalyzer/CMakeLists.txt
index 3d1509254f52..a610252e1de7 100644
--- a/clang/lib/StaticAnalyzer/CMakeLists.txt
+++ b/clang/lib/StaticAnalyzer/CMakeLists.txt
@@ -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)

diff  --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index c5a9e0209f13..dca1c2af45bb 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -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})


        


More information about the cfe-commits mailing list