[CMake] Refactor add_llvm_tool_symlink for reuse

The old implementation of add_llvm_tool_symlink could fail in odd ways when building out of tree. This version solves that problem by not using the LLVM_* variables, and instead reaeding the target's properties.

llvm-svn: 288632
This commit is contained in:
Chris Bieneman 2016-12-05 03:28:03 +00:00
parent ca17841fc4
commit 89e08ed0c1
1 changed files with 28 additions and 17 deletions

View File

@ -1288,43 +1288,54 @@ function(llvm_install_symlink name dest)
endif()
endfunction()
function(add_llvm_tool_symlink name dest)
cmake_parse_arguments(ARG "ALWAYS_GENERATE" "" "" ${ARGN})
if(UNIX)
set(LLVM_LINK_OR_COPY create_symlink)
set(dest_binary "${dest}${CMAKE_EXECUTABLE_SUFFIX}")
function(add_llvm_tool_symlink link_name target)
cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN})
if(NOT ARG_OUTPUT_DIR)
get_target_property(target_type ${target} TYPE)
if(${target_type} STREQUAL "STATIC_LIBRARY")
get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY)
elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY")
get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY)
else()
set(LLVM_LINK_OR_COPY copy)
set(dest_binary "${LLVM_RUNTIME_OUTPUT_INTDIR}/${dest}${CMAKE_EXECUTABLE_SUFFIX}")
get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY)
endif()
endif()
set(output_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/${name}${CMAKE_EXECUTABLE_SUFFIX}")
if(UNIX)
set(LLVM_LINK_OR_COPY create_symlink)
set(dest_binary "$<TARGET_FILE_NAME:${target}>")
else()
set(LLVM_LINK_OR_COPY copy)
set(dest_binary "$<TARGET_FILE:${target}>")
endif()
set(target_name ${name})
if(TARGET ${name})
set(target_name ${name}-link)
set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}")
set(target_name ${link_name})
if(TARGET ${link_name})
set(target_name ${link_name}-link)
endif()
if(ARG_ALWAYS_GENERATE)
set_property(DIRECTORY APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary})
add_custom_command(TARGET ${dest} POST_BUILD
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}")
else()
add_custom_command(OUTPUT ${output_path}
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}"
DEPENDS ${dest})
add_custom_target(${target_name} ALL DEPENDS ${output_path})
DEPENDS ${target})
add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path})
set_target_properties(${target_name} PROPERTIES FOLDER Tools)
# Make sure both the link and target are toolchain tools
if (${name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${dest} IN_LIST LLVM_TOOLCHAIN_TOOLS)
if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS)
set(TOOL_IS_TOOLCHAIN ON)
endif()
if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS)
llvm_install_symlink(${name} ${dest})
llvm_install_symlink(${link_name} ${target})
endif()
endif()
endfunction()