[PATCH] D156179: [CMake] Support per-target compiler and linker flags

Vincent Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 24 16:30:03 PDT 2023


thevinster created this revision.
Herald added a subscriber: ekilmer.
Herald added a project: All.
thevinster retitled this revision from "[CMake] Support per-binary custom compiler/linker flags" to "[CMake] Support per-target compiler and linker flags".
thevinster edited the summary of this revision.
thevinster added reviewers: phosek, smeenai.
thevinster published this revision for review.
thevinster added a comment.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Would love to get some feedback whether there's interest in this or if there's a better way to solve the build speed issue.


`CMAKE_{C/CXX}_FLAGS` affects all targets in LLVM. This is 
undesirable in situations where we do not want to apply
every optimization flag to every target for faster build speeds.
For example, we can selectively apply `-flto=thin` flags to
binaries that we care about optimizing and keep other tools
in the distribution on the non-optimized version. The pattern
follows a similar approach to how multi-distributions are created.
The example of the thinLTO above can be done by doing the
following:

  set(LLVM_CUSTOM_BINARIES clang lld CACHE STRING "List of targets to apply custom flags")
  set(LLVM_clang_COMPILER_FLAGS -flto=thin CACHE STRING "Custom compiler flags to clang")
  set(LLVM_lld_COMPILER_FLAGS -flto=thin CACHE STRING "Custom compiler flags to lld")

There's probably other applications where this could be used 
(e.g. avoid optimizing host tools for build speed improvement etc.). 
I've generalized this so that users can apply their desired flags to
targets that are generated by `llvm_add_library` and `add_llvm_executable`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156179

Files:
  llvm/cmake/modules/AddLLVM.cmake


Index: llvm/cmake/modules/AddLLVM.cmake
===================================================================
--- llvm/cmake/modules/AddLLVM.cmake
+++ llvm/cmake/modules/AddLLVM.cmake
@@ -726,6 +726,39 @@
     endforeach()
   endif()
 
+  if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+    if (NOT LLVM_${name}_COMPILER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
+      message(FATAL_ERROR "At least one of compiler or linker flags must be provided when specifying LLVM_CUSTOM_BINARIES")
+    else()
+      if (LLVM_${name}_COMPILER_FLAGS)
+        message(STATUS "Applying ${LLVM_${name}_COMPILER_FLAGS} to ${name}")
+        target_compile_options(${name} PRIVATE ${LLVM_${name}_COMPILER_FLAGS})
+        # For binaries that link with static libraries, recursively apply the flag to those targets.
+        get_target_property(target_libraries ${name} LINK_LIBRARIES)
+        foreach(lib ${target_libraries})
+          if(TARGET ${lib})
+            message(STATUS "Applying ${LLVM_${name}_COMPILER_FLAGS} to ${lib}")
+            target_compile_options(${lib} PRIVATE ${LLVM_${name}_COMPILER_FLAGS})
+          else()
+            message(STATUS "Target ${lib} does not exist in the project. Skipping...")
+          endif()
+        endforeach()
+      endif()
+      if (ARG_SHARED)
+        if (LLVM_${name}_SHARED_LINKER_FLAGS)
+          message(STATUS "Applying ${LLVM_${name}_SHARED_LINKER_FLAGS} to ${name}")
+          target_link_options(${name} PRIVATE ${LLVM_${name}_SHARED_LINKER_FLAGS})
+        endif()
+      endif()
+      if (ARG_MODULE)
+        if (LLVM_${name}_MODULE_LINKER_FLAGS)
+          message(STATUS "Applying ${LLVM_${name}_MODULE_LINKER_FLAGS} to ${name}")
+          target_link_options(${name} PRIVATE ${LLVM_${name}_MODULE_LINKER_FLAGS})
+        endif()
+      endif()
+    endif()
+  endif()
+
   if(ARG_SHARED OR ARG_MODULE)
     llvm_externalize_debuginfo(${name})
     llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
@@ -1019,6 +1052,31 @@
     endforeach()
   endif( LLVM_COMMON_DEPENDS )
 
+  if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+    if (NOT LLVM_${name}_COMPILER_FLAGS AND NOT LLVM_${name}_EXE_LINKER_FLAGS)
+      message(FATAL_ERROR "At least one of compiler or linker flags must be provided when specifying LLVM_CUSTOM_BINARIES")
+    else()
+      if (LLVM_${name}_COMPILER_FLAGS)
+        message(STATUS "Applying ${LLVM_${name}_COMPILER_FLAGS} to ${name}")
+        target_compile_options(${name} PRIVATE ${LLVM_${name}_COMPILER_FLAGS})
+        # For binaries that link with static libraries, recursively apply the flag to those targets.
+        get_target_property(target_libraries ${name} LINK_LIBRARIES)
+        foreach(lib ${target_libraries})
+          if(TARGET ${lib})
+            message(STATUS "Applying ${LLVM_${name}_COMPILER_FLAGS} to ${lib}")
+            target_compile_options(${lib} PRIVATE ${LLVM_${name}_COMPILER_FLAGS})
+          else()
+            message(STATUS "Target ${lib} does not exist in the project. Skipping...")
+          endif()
+        endforeach()
+      endif()
+      if (LLVM_${name}_EXE_LINKER_FLAGS)
+        message(STATUS "Applying ${LLVM_${name}_EXE_LINKER_FLAGS} to ${name}")
+        target_link_options(${name} PRIVATE ${LLVM_${name}_EXE_LINKER_FLAGS})
+      endif()
+    endif()
+  endif()
+
   if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
     llvm_externalize_debuginfo(${name})
   endif()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156179.543733.patch
Type: text/x-patch
Size: 3482 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230724/437177f9/attachment.bin>


More information about the llvm-commits mailing list