[CMake] Refactor and cleanup generating and installing symlinks to tools.
[oota-llvm.git] / cmake / modules / AddLLVM.cmake
index 924d672a22e0b4304c8c46fd4db314541a1926c2..1a314cb77ffa7c19ba3e3fc9aca20f545fb3706e 100644 (file)
@@ -643,6 +643,8 @@ endfunction()
 if(NOT LLVM_TOOLCHAIN_TOOLS)
   set (LLVM_TOOLCHAIN_TOOLS
     llvm-ar
+    llvm-ranlib
+    llvm-lib
     llvm-objdump
     )
 endif()
@@ -1020,3 +1022,49 @@ function(add_lit_testsuites project directory)
     endforeach()
   endif()
 endfunction()
+
+function(add_llvm_tool_symlink name target)
+  if(UNIX)
+    set(LLVM_LINK_OR_COPY create_symlink)
+    set(target_binary "${target}${CMAKE_EXECUTABLE_SUFFIX}")
+  else()
+    set(LLVM_LINK_OR_COPY copy)
+    set(target_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}")
+  endif()
+
+  set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
+
+  add_custom_command(OUTPUT ${output_path}
+                     COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${target_binary}" "${output_path}"
+                     DEPENDS ${target})
+
+  add_custom_target(${name} ALL DEPENDS ${output_path})
+  set_target_properties(${name} PROPERTIES FOLDER Tools)
+
+  # MAke sure the parent tool is a toolchain tool, otherwise exclude this tool
+  list(FIND LLVM_TOOLCHAIN_TOOLS ${target} LLVM_IS_${target}_TOOLCHAIN_TOOL)
+  if (NOT LLVM_IS_${target}_TOOLCHAIN_TOOL GREATER -1)
+    set(LLVM_IS_${name}_TOOLCHAIN_TOOL ${LLVM_IS_${target}_TOOLCHAIN_TOOL})
+  else()
+    list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
+  endif()
+
+  # LLVM_IS_${name}_TOOLCHAIN_TOOL will only be greater than -1 if both this
+  # tool and its parent tool are in LLVM_TOOLCHAIN_TOOLS
+  if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
+    if( LLVM_BUILD_TOOLS )
+      install(SCRIPT ${CMAKE_SOURCE_DIR}/cmake/modules/install_symlink.cmake
+              CODE "install_symlink(${name} ${target})"
+              COMPONENT ${name})
+
+      if (NOT CMAKE_CONFIGURATION_TYPES)
+        add_custom_target(install-${name}
+                          DEPENDS ${name}
+                          COMMAND "${CMAKE_COMMAND}"
+                                  -DCMAKE_INSTALL_COMPONENT=${name}
+                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
+      endif()
+    endif()
+  endif()
+endfunction()
+