[llvm] 695c7d6 - [llvm] Added support for stand-alone cmake object libraries.

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 12:01:53 PDT 2020


Author: Mircea Trofin
Date: 2020-06-15T12:01:43-07:00
New Revision: 695c7d6313d74dc02222f6497d4c4985d67f433f

URL: https://github.com/llvm/llvm-project/commit/695c7d6313d74dc02222f6497d4c4985d67f433f
DIFF: https://github.com/llvm/llvm-project/commit/695c7d6313d74dc02222f6497d4c4985d67f433f.diff

LOG: [llvm] Added support for stand-alone cmake object libraries.

Summary:
Currently, add_llvm_library would create an OBJECT library alongside
of a STATIC / SHARED library, but losing the link interface (its
elements would become dependencies instead). To support scenarios
where linking an object library also brings in its usage
requirements, this patch adds support for 'stand-alone' OBJECT
libraries - i.e. without an accompanying SHARED/STATIC library, and
maintaining the link interface defined by the user.

The support is via a new option, OBJECT_ONLY, to avoid breaking changes
- since just specifying "OBJECT" would currently imply also STATIC or
SHARED, depending on BUILD_SHARED_LIBS.

This is useful for cases where, for example, we want to build a part
of a component separately. Using a STATIC target would incur the risk
that symbols not referenced in the consumer would be dropped (which may
be undesirable).

The current application is the ML part of Analysis. It should be part
of the Analysis component, so it may reference other analyses; and (in
upcoming changes) it has dependencies on optional libraries.

Reviewers: karies, davidxl

Subscribers: mgorny, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D81447

Added: 
    

Modified: 
    llvm/cmake/modules/AddLLVM.cmake
    llvm/lib/Analysis/CMakeLists.txt
    llvm/lib/Analysis/LLVMBuild.txt
    llvm/lib/Analysis/ML/CMakeLists.txt
    llvm/lib/Passes/LLVMBuild.txt
    llvm/unittests/Analysis/ML/CMakeLists.txt

Removed: 
    llvm/lib/Analysis/ML/LLVMBuild.txt


################################################################################
diff  --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index e25c72f3cae3..959e0daf718d 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -390,8 +390,13 @@ endfunction(set_windows_version_resource_properties)
 #   SHARED;STATIC
 #     STATIC by default w/o BUILD_SHARED_LIBS.
 #     SHARED by default w/  BUILD_SHARED_LIBS.
+#   OBJECT_ONLY
+#     builds an OBJECT target, irrespective of BUILD_SHARED_LIBS. 
+#     Cannot be mixed with SHARED, STATIC, or OBJECT.
 #   OBJECT
 #     Also create an OBJECT library target. Default if STATIC && SHARED.
+#     The OBJECT target will be named obj.${name} and will have an empty link
+#     interface.
 #   MODULE
 #     Target ${name} might not be created on unsupported platforms.
 #     Check with "if(TARGET ${name})".
@@ -421,7 +426,7 @@ endfunction(set_windows_version_resource_properties)
 #   )
 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;OBJECT_ONLY;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
     "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
     "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
     ${ARGN})
@@ -436,6 +441,10 @@ function(llvm_add_library name)
     llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
   endif()
 
+  if ((ARG_STATIC OR ARG_SHARED OR ARG_OBJECT) AND ARG_OBJECT_ONLY)
+    message(ERROR "OBJECT_ONLY should appear alone, without STATIC|SHARED|OBJECT")
+  endif()
+
   if(ARG_MODULE)
     if(ARG_SHARED OR ARG_STATIC)
       message(WARNING "MODULE with SHARED|STATIC doesn't make sense.")
@@ -452,12 +461,12 @@ function(llvm_add_library name)
     if(BUILD_SHARED_LIBS AND NOT ARG_STATIC)
       set(ARG_SHARED TRUE)
     endif()
-    if(NOT ARG_SHARED)
+    if(NOT ARG_SHARED AND NOT ARG_OBJECT_ONLY)
       set(ARG_STATIC TRUE)
     endif()
   endif()
 
-  # Generate objlib
+  # Generate the extra objlib
   if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT)
     # Generate an obj library for both targets.
     set(obj_name "obj.${name}")
@@ -523,8 +532,10 @@ function(llvm_add_library name)
   elseif(ARG_SHARED)
     add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
     add_library(${name} SHARED ${ALL_FILES})
-  else()
+  elseif(ARG_STATIC)
     add_library(${name} STATIC ${ALL_FILES})
+  else()
+    add_library(${name} OBJECT ${ALL_FILES})
   endif()
 
   if(ARG_COMPONENT_LIB)
@@ -635,11 +646,11 @@ function(llvm_add_library name)
     get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
   endif()
 
-  if(ARG_STATIC)
-    set(libtype PUBLIC)
-  else()
+  if(ARG_SHARED)
     # We can use PRIVATE since SO knows its dependent libs.
     set(libtype PRIVATE)
+  else()
+    set(libtype PUBLIC)
   endif()
 
   target_link_libraries(${name} ${libtype}
@@ -675,6 +686,27 @@ function(llvm_add_library name)
   endif()
 endfunction()
 
+# Add a target which is logically and deployment-wise part of another one
+# (owner), but - perhaps because it has optional build dependencies - may be
+# built separately.
+# The owner consumes it via target_link_libraries (or equivalent syntax).
+#
+# PUBLIC creates an OBJECT library, so linking it in the owner translates to 
+# linking the object files in this target as if they were built by the owner.
+#
+# PRIVATE creates a STATIC library, so linking it would drop objects that are
+# not referenced.
+macro (add_llvm_internal_library name)
+  cmake_parse_arguments(ARG "PUBLIC;PRIVATE" "" "" ${ARGN})
+  if (ARG_PUBLIC)
+    add_llvm_library(${name} OBJECT_ONLY DISABLE_LLVM_LINK_LLVM_DYLIB 
+      ${ARG_UNPARSED_ARGUMENTS})
+  else()
+    add_llvm_library(${name} STATIC DISABLE_LLVM_LINK_LLVM_DYLIB 
+      ${ARG_UNPARSED_ARGUMENTS})
+  endif()
+endmacro()
+
 function(add_llvm_install_targets target)
   cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN})
   if(ARG_COMPONENT)

diff  --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt
index faf0a3186fd6..cd9a68820860 100644
--- a/llvm/lib/Analysis/CMakeLists.txt
+++ b/llvm/lib/Analysis/CMakeLists.txt
@@ -108,4 +108,7 @@ add_llvm_component_library(LLVMAnalysis
 
   DEPENDS
   intrinsics_gen
+
+  LINK_LIBS
+  PRIVATE LLVMMLPolicies
   )

diff  --git a/llvm/lib/Analysis/LLVMBuild.txt b/llvm/lib/Analysis/LLVMBuild.txt
index ef52c41da8a6..d73b55f037fa 100644
--- a/llvm/lib/Analysis/LLVMBuild.txt
+++ b/llvm/lib/Analysis/LLVMBuild.txt
@@ -14,9 +14,6 @@
 ;
 ;===------------------------------------------------------------------------===;
 
-[common]
-subdirectories = ML
-
 [component_0]
 type = Library
 name = Analysis

diff  --git a/llvm/lib/Analysis/ML/CMakeLists.txt b/llvm/lib/Analysis/ML/CMakeLists.txt
index 28a5f98b793b..cb49954acc7c 100644
--- a/llvm/lib/Analysis/ML/CMakeLists.txt
+++ b/llvm/lib/Analysis/ML/CMakeLists.txt
@@ -1,4 +1,6 @@
-add_llvm_component_library(LLVMMLPolicies
+set(BUILD_SHARED_LIBS OFF)
+
+add_llvm_internal_library(LLVMMLPolicies PUBLIC
   InlineFeaturesAnalysis.cpp
 
   DEPENDS

diff  --git a/llvm/lib/Analysis/ML/LLVMBuild.txt b/llvm/lib/Analysis/ML/LLVMBuild.txt
deleted file mode 100644
index a0bb919bb411..000000000000
--- a/llvm/lib/Analysis/ML/LLVMBuild.txt
+++ /dev/null
@@ -1,21 +0,0 @@
-;===- ./lib/Analysis/ML/LLVMBuild.txt --------------------------*- Conf -*--===;
-;
-; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-; See https://llvm.org/LICENSE.txt for license information.
-; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-;
-;===------------------------------------------------------------------------===;
-;
-; This is an LLVMBuild description file for the components in this subdirectory.
-;
-; For more information on the LLVMBuild system, please see:
-;
-;   http://llvm.org/docs/LLVMBuild.html
-;
-;===------------------------------------------------------------------------===;
-
-[component_0]
-type = Library
-name = MLPolicies
-parent = Analysis
-required_libraries = Core Support

diff  --git a/llvm/lib/Passes/LLVMBuild.txt b/llvm/lib/Passes/LLVMBuild.txt
index 14586b640849..438fc5c7c2d4 100644
--- a/llvm/lib/Passes/LLVMBuild.txt
+++ b/llvm/lib/Passes/LLVMBuild.txt
@@ -18,4 +18,4 @@
 type = Library
 name = Passes
 parent = Libraries
-required_libraries = AggressiveInstCombine Analysis MLPolicies CodeGen Core Coroutines IPO InstCombine Scalar Support Target TransformUtils Vectorize Instrumentation
+required_libraries = AggressiveInstCombine Analysis CodeGen Core Coroutines IPO InstCombine Scalar Support Target TransformUtils Vectorize Instrumentation

diff  --git a/llvm/unittests/Analysis/ML/CMakeLists.txt b/llvm/unittests/Analysis/ML/CMakeLists.txt
index 8d1c90312ad0..2cd6dbe4e94e 100644
--- a/llvm/unittests/Analysis/ML/CMakeLists.txt
+++ b/llvm/unittests/Analysis/ML/CMakeLists.txt
@@ -2,7 +2,6 @@ set(LLVM_LINK_COMPONENTS
   Analysis  
   AsmParser
   Core
-  MLPolicies
   Support
   TransformUtils
   )


        


More information about the llvm-commits mailing list