From ce40381734023a3f265bf5e0c4cc7703422df682 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Sun, 2 Jun 2019 02:05:01 +0000 Subject: [PATCH] [CMake] Use libtool for runtimes when building for Apple platform LLVM CMake build already uses libtool instead of ar when building for Apple platform and we should be using the same when building runtimes. To do so, this change extracts the logic for finding libtool into a separate file and then uses it from both the LLVM build as well as the LLVM runtimes build. Differential Revision: https://reviews.llvm.org/D62769 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362313 91177308-0d34-0410-b5e6-96231b3b80d8 --- CMakeLists.txt | 60 +++------------------------------- cmake/modules/UseLibtool.cmake | 50 ++++++++++++++++++++++++++++ runtimes/CMakeLists.txt | 5 +++ 3 files changed, 60 insertions(+), 55 deletions(-) create mode 100644 cmake/modules/UseLibtool.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 895f9ab7189..a9addfc5a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,61 +49,6 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type (default Debug)" FORCE) endif() -# This should only apply if you are both on an Apple host, and targeting Apple. -if(CMAKE_HOST_APPLE AND APPLE) - # if CMAKE_LIBTOOL is not set, try and find it with xcrun or find_program - if(NOT CMAKE_LIBTOOL) - if(NOT CMAKE_XCRUN) - find_program(CMAKE_XCRUN NAMES xcrun) - endif() - if(CMAKE_XCRUN) - execute_process(COMMAND ${CMAKE_XCRUN} -find libtool - OUTPUT_VARIABLE CMAKE_LIBTOOL - OUTPUT_STRIP_TRAILING_WHITESPACE) - endif() - - if(NOT CMAKE_LIBTOOL OR NOT EXISTS CMAKE_LIBTOOL) - find_program(CMAKE_LIBTOOL NAMES libtool) - endif() - endif() - - get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES) - if(CMAKE_LIBTOOL) - set(CMAKE_LIBTOOL ${CMAKE_LIBTOOL} CACHE PATH "libtool executable") - message(STATUS "Found libtool - ${CMAKE_LIBTOOL}") - - execute_process(COMMAND ${CMAKE_LIBTOOL} -V - OUTPUT_VARIABLE LIBTOOL_V_OUTPUT - OUTPUT_STRIP_TRAILING_WHITESPACE) - if("${LIBTOOL_V_OUTPUT}" MATCHES ".*cctools-([0-9.]+).*") - string(REGEX REPLACE ".*cctools-([0-9.]+).*" "\\1" LIBTOOL_VERSION - ${LIBTOOL_V_OUTPUT}) - if(NOT LIBTOOL_VERSION VERSION_LESS "862") - set(LIBTOOL_NO_WARNING_FLAG "-no_warning_for_no_symbols") - endif() - endif() - - foreach(lang ${languages}) - set(CMAKE_${lang}_CREATE_STATIC_LIBRARY - "\"${CMAKE_LIBTOOL}\" -static ${LIBTOOL_NO_WARNING_FLAG} -o \ - ") - endforeach() - endif() - - # If DYLD_LIBRARY_PATH is set we need to set it on archiver commands - if(DYLD_LIBRARY_PATH) - set(dyld_envar "DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}") - foreach(lang ${languages}) - foreach(cmd ${CMAKE_${lang}_CREATE_STATIC_LIBRARY}) - list(APPEND CMAKE_${lang}_CREATE_STATIC_LIBRARY_NEW - "${dyld_envar} ${cmd}") - endforeach() - set(CMAKE_${lang}_CREATE_STATIC_LIBRARY - ${CMAKE_${lang}_CREATE_STATIC_LIBRARY_NEW}) - endforeach() - endif() -endif() - # Side-by-side subprojects layout: automatically set the # LLVM_EXTERNAL_${project}_SOURCE_DIR using LLVM_ALL_PROJECTS # This allows an easy way of setting up a build directory for llvm and another @@ -648,6 +593,11 @@ if (LLVM_BUILD_STATIC) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") endif() +# Use libtool instead of ar if you are both on an Apple host, and targeting Apple. +if(CMAKE_HOST_APPLE AND APPLE) + include(UseLibtool) +endif() + # Override the default target with an environment variable named by LLVM_TARGET_TRIPLE_ENV. set(LLVM_TARGET_TRIPLE_ENV CACHE STRING "The name of environment variable to override default target. Disabled by blank.") mark_as_advanced(LLVM_TARGET_TRIPLE_ENV) diff --git a/cmake/modules/UseLibtool.cmake b/cmake/modules/UseLibtool.cmake new file mode 100644 index 00000000000..38d197d4846 --- /dev/null +++ b/cmake/modules/UseLibtool.cmake @@ -0,0 +1,50 @@ +# if CMAKE_LIBTOOL is not set, try and find it with xcrun or find_program +if(NOT CMAKE_LIBTOOL) + if(NOT CMAKE_XCRUN) + find_program(CMAKE_XCRUN NAMES xcrun) + endif() + if(CMAKE_XCRUN) + execute_process(COMMAND ${CMAKE_XCRUN} -find libtool + OUTPUT_VARIABLE CMAKE_LIBTOOL + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + + if(NOT CMAKE_LIBTOOL OR NOT EXISTS CMAKE_LIBTOOL) + find_program(CMAKE_LIBTOOL NAMES libtool) + endif() +endif() + +get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES) +if(CMAKE_LIBTOOL) + set(CMAKE_LIBTOOL ${CMAKE_LIBTOOL} CACHE PATH "libtool executable") + message(STATUS "Found libtool - ${CMAKE_LIBTOOL}") + + execute_process(COMMAND ${CMAKE_LIBTOOL} -V + OUTPUT_VARIABLE LIBTOOL_V_OUTPUT + OUTPUT_STRIP_TRAILING_WHITESPACE) + if("${LIBTOOL_V_OUTPUT}" MATCHES ".*cctools-([0-9.]+).*") + string(REGEX REPLACE ".*cctools-([0-9.]+).*" "\\1" LIBTOOL_VERSION + ${LIBTOOL_V_OUTPUT}) + if(NOT LIBTOOL_VERSION VERSION_LESS "862") + set(LIBTOOL_NO_WARNING_FLAG "-no_warning_for_no_symbols") + endif() + endif() + + foreach(lang ${languages}) + set(CMAKE_${lang}_CREATE_STATIC_LIBRARY + "\"${CMAKE_LIBTOOL}\" -static ${LIBTOOL_NO_WARNING_FLAG} -o ") + endforeach() +endif() + +# If DYLD_LIBRARY_PATH is set we need to set it on archiver commands +if(DYLD_LIBRARY_PATH) + set(dyld_envar "DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}") + foreach(lang ${languages}) + foreach(cmd ${CMAKE_${lang}_CREATE_STATIC_LIBRARY}) + list(APPEND CMAKE_${lang}_CREATE_STATIC_LIBRARY_NEW + "${dyld_envar} ${cmd}") + endforeach() + set(CMAKE_${lang}_CREATE_STATIC_LIBRARY + ${CMAKE_${lang}_CREATE_STATIC_LIBRARY_NEW}) + endforeach() +endif() diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt index 6ac92cc6703..218c5a82833 100644 --- a/runtimes/CMakeLists.txt +++ b/runtimes/CMakeLists.txt @@ -117,6 +117,11 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) # Remove the -nostdlib++ option we've added earlier. string(REPLACE "-nostdlib++" "" CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + # Use libtool instead of ar if you are both on an Apple host, and targeting Apple. + if(CMAKE_HOST_APPLE AND APPLE) + include(UseLibtool) + endif() + # This can be used to detect whether we're in the runtimes build. set(RUNTIMES_BUILD ON) -- 2.40.0