[libc-commits] [libc] 3d15515 - [libc] Use add_library in add_entrypoint_library instead of invoking ar.

Siva Chandra via libc-commits libc-commits at lists.llvm.org
Fri Mar 19 21:27:42 PDT 2021


Author: Siva Chandra
Date: 2021-03-20T04:25:51Z
New Revision: 3d155157bf621effd51e3f62050d488572a11501

URL: https://github.com/llvm/llvm-project/commit/3d155157bf621effd51e3f62050d488572a11501
DIFF: https://github.com/llvm/llvm-project/commit/3d155157bf621effd51e3f62050d488572a11501.diff

LOG: [libc] Use add_library in add_entrypoint_library instead of invoking ar.

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCLibraryRules.cmake

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCLibraryRules.cmake b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
index 21a99a0dd0a9..bdc361a719de 100644
--- a/libc/cmake/modules/LLVMLibCLibraryRules.cmake
+++ b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
@@ -1,70 +1,42 @@
-# This is a helper function and not a build rule. It is to be used by the
-# the "add_entrypoint_library" rule to generate the full list of object files
-# recursively produced by "add_object_library" targets upstream in the
-# dependency tree. This function traverses up through the
-# "add_entrypoint_object" targets but does not collect the object files
-# produced by them.
-# Usage:
-#   get_object_files_for_test(<result var> <target0> [<target1> ...])
-#
-#   targetN is either an "add_entrypoint_target" target or an
-#   "add_object_library" target.
-function(get_object_files_for_entrypoint_library result)
-  set(object_files "")
-  foreach(dep IN LISTS ARGN)
-    get_target_property(dep_type ${dep} "TARGET_TYPE")
-    if (NOT dep_type)
-      continue()
-    endif()
-
-    if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
-      get_target_property(dep_object_files ${dep} "OBJECT_FILES")
-      if(dep_object_files)
-        list(APPEND object_files ${dep_object_files})
-      endif()
-    endif()
-
-    get_target_property(indirect_deps ${dep} "DEPS")
-    get_object_files_for_entrypoint_library(indirect_objfiles ${indirect_deps})
-    list(APPEND object_files ${indirect_objfiles})
-  endforeach(dep)
-  list(REMOVE_DUPLICATES object_files)
-  set(${result} ${object_files} PARENT_SCOPE)
-endfunction()
-
-# This is a helper function and not a build rule. Given an entrypoint object
-# target, it returns the object file produced by this target in |result|.
-# If the given entrypoint target is an alias, then it traverses up to the
-# aliasee to get the object file.
-function(get_entrypoint_object_file entrypoint_target result)
-  get_target_property(target_type ${entrypoint_target} "TARGET_TYPE")
-  if(NOT (${target_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
-    message(FATAL_ERROR
-            "Expected an target added using `add_entrypoint_object` rule.")
+function(collect_object_file_deps target result)
+  set(all_deps "")
+  get_target_property(target_type ${target} "TARGET_TYPE")
+  if(NOT target_type)
+    return()
   endif()
 
-  get_target_property(objfile ${entrypoint_target} "OBJECT_FILE")
-  if(objfile)
-    set(${result} ${objfile} PARENT_SCOPE)
+  if(${target_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
+    list(APPEND all_deps ${target})
+    get_target_property(deps ${target} "DEPS")
+    foreach(dep IN LISTS deps)
+      collect_object_file_deps(${dep} dep_targets)
+      list(APPEND all_deps ${dep_targets})
+    endforeach(dep)
+    set(${result} ${all_deps} PARENT_SCOPE)
     return()
   endif()
 
-  # If the entrypoint is an alias, fetch the object file from the aliasee.
-  get_target_property(is_alias ${entrypoint_target} "IS_ALIAS")
-  if(is_alias)
-    get_target_property(aliasee ${entrypoint_target} "DEPS")
-    if(NOT aliasee)
-      message(FATAL_ERROR
-              "Entrypoint alias ${entrypoint_target} does not have an aliasee.")
+  if(${target_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
+    set(entrypoint_target ${target})
+    get_target_property(is_alias ${entrypoint_target} "IS_ALIAS")
+    if(is_alias)
+      get_target_property(aliasee ${entrypoint_target} "DEPS")
+      if(NOT aliasee)
+        message(FATAL_ERROR
+                "Entrypoint alias ${entrypoint_target} does not have an aliasee.")
+      endif()
+      set(entrypoint_target ${aliasee})
     endif()
-    get_entrypoint_object_file(${aliasee} objfile)
-    set(${result} ${objfile} PARENT_SCOPE)
+    list(APPEND all_deps ${entrypoint_target})
+    get_target_property(deps ${target} "DEPS")
+    foreach(dep IN LISTS deps)
+      collect_object_file_deps(${dep} dep_targets)
+      list(APPEND all_deps ${dep_targets})
+    endforeach(dep)
+    set(${result} ${all_deps} PARENT_SCOPE)
     return()
   endif()
-
-  message(FATAL_ERROR
-          "Entrypoint ${entrypoint_target} does not produce an object file.")
-endfunction(get_entrypoint_object_file)
+endfunction(collect_object_file_deps)
 
 # A rule to build a library from a collection of entrypoint objects.
 # Usage:
@@ -89,28 +61,30 @@ function(add_entrypoint_library target_name)
   endif()
 
   get_fq_deps_list(fq_deps_list ${ENTRYPOINT_LIBRARY_DEPENDS})
-  get_object_files_for_entrypoint_library(obj_list ${fq_deps_list})
+  set(all_deps "")
   foreach(dep IN LISTS fq_deps_list)
     get_target_property(dep_type ${dep} "TARGET_TYPE")
     if(NOT (${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
       message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_collection' is "
                           "not an 'add_entrypoint_object' target.")
     endif()
-    get_entrypoint_object_file(${dep} objfile)
-    list(APPEND obj_list ${objfile})
+    collect_object_file_deps(${dep} recursive_deps)
+    list(APPEND all_deps ${recursive_deps})
   endforeach(dep)
-  list(REMOVE_DUPLICATES obj_list)
-
-  set(library_file "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${target_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
-  add_custom_command(
-    OUTPUT ${library_file}
-    COMMAND ${CMAKE_AR} -r ${library_file} ${obj_list}
-    DEPENDS ${obj_list}
+  list(REMOVE_DUPLICATES all_deps)
+  set(objects "")
+  foreach(dep IN LISTS all_deps)
+    list(APPEND objects $<TARGET_OBJECTS:${dep}>)
+  endforeach(dep)
+  add_library(
+    ${target_name}
+    STATIC
+    ${objects}
   )
-  add_custom_target(
+  set_target_properties(
     ${target_name}
-    ALL
-    DEPENDS ${library_file}
+    PROPERTIES
+      ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
   )
 endfunction(add_entrypoint_library)
 


        


More information about the libc-commits mailing list