Home | History | Annotate | Download | only in Modules
      1 macro(find_llvm_parts)
      2 # Rely on llvm-config.
      3   set(CONFIG_OUTPUT)
      4   if(NOT LLVM_CONFIG_PATH)
      5     find_program(LLVM_CONFIG_PATH "llvm-config")
      6   endif()
      7   if(DEFINED LLVM_PATH)
      8     set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
      9     set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree")
     10     set(LLVM_MAIN_SRC_DIR ${LLVM_PATH})
     11     set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules")
     12   elseif(LLVM_CONFIG_PATH)
     13     message(STATUS "Found LLVM_CONFIG_PATH as ${LLVM_CONFIG_PATH}")
     14     set(LIBCXX_USING_INSTALLED_LLVM 1)
     15     set(CONFIG_COMMAND ${LLVM_CONFIG_PATH}
     16       "--includedir"
     17       "--prefix"
     18       "--src-root")
     19     execute_process(
     20       COMMAND ${CONFIG_COMMAND}
     21       RESULT_VARIABLE HAD_ERROR
     22       OUTPUT_VARIABLE CONFIG_OUTPUT
     23     )
     24     if(NOT HAD_ERROR)
     25       string(REGEX REPLACE
     26         "[ \t]*[\r\n]+[ \t]*" ";"
     27         CONFIG_OUTPUT ${CONFIG_OUTPUT})
     28     else()
     29       string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
     30       message(STATUS "${CONFIG_COMMAND_STR}")
     31       message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
     32     endif()
     33 
     34     list(GET CONFIG_OUTPUT 0 INCLUDE_DIR)
     35     list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
     36     list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR)
     37 
     38     set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
     39     set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
     40     set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
     41 
     42     # --cmakedir is supported since llvm r291218 (4.0 release)
     43     execute_process(
     44       COMMAND ${LLVM_CONFIG_PATH} --cmakedir
     45       RESULT_VARIABLE HAD_ERROR
     46       OUTPUT_VARIABLE CONFIG_OUTPUT
     47       ERROR_QUIET)
     48     if(NOT HAD_ERROR)
     49       string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH)
     50     else()
     51       set(LLVM_CMAKE_PATH
     52           "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
     53     endif()
     54   else()
     55     set(LLVM_FOUND OFF)
     56     message(WARNING "UNSUPPORTED LIBCXX CONFIGURATION DETECTED: "
     57                     "llvm-config not found and LLVM_PATH not defined.\n"
     58                     "Reconfigure with -DLLVM_CONFIG_PATH=path/to/llvm-config "
     59                     "or -DLLVM_PATH=path/to/llvm-source-root.")
     60     return()
     61   endif()
     62 
     63   if (EXISTS "${LLVM_CMAKE_PATH}")
     64     list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
     65   elseif (EXISTS "${LLVM_MAIN_SRC_DIR}/cmake/modules")
     66     list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
     67   else()
     68     set(LLVM_FOUND OFF)
     69     message(WARNING "Neither ${LLVM_CMAKE_PATH} nor ${LLVM_MAIN_SRC_DIR}/cmake/modules found")
     70     return()
     71   endif()
     72 
     73   set(LLVM_FOUND ON)
     74 endmacro(find_llvm_parts)
     75 
     76 macro(configure_out_of_tree_llvm)
     77   message(STATUS "Configuring for standalone build.")
     78   set(LIBCXX_STANDALONE_BUILD 1)
     79 
     80   find_llvm_parts()
     81 
     82   # Add LLVM Functions --------------------------------------------------------
     83   if (LLVM_FOUND AND LIBCXX_USING_INSTALLED_LLVM)
     84     include(LLVMConfig) # For TARGET_TRIPLE
     85   else()
     86     if (WIN32)
     87       set(LLVM_ON_UNIX 0)
     88       set(LLVM_ON_WIN32 1)
     89     else()
     90       set(LLVM_ON_UNIX 1)
     91       set(LLVM_ON_WIN32 0)
     92     endif()
     93   endif()
     94   if (LLVM_FOUND)
     95     include(AddLLVM OPTIONAL)
     96   endif()
     97 
     98   # LLVM Options --------------------------------------------------------------
     99   if (NOT DEFINED LLVM_INCLUDE_TESTS)
    100     set(LLVM_INCLUDE_TESTS ${LLVM_FOUND})
    101   endif()
    102   if (NOT DEFINED LLVM_INCLUDE_DOCS)
    103     set(LLVM_INCLUDE_DOCS ${LLVM_FOUND})
    104   endif()
    105   if (NOT DEFINED LLVM_ENABLE_SPHINX)
    106     set(LLVM_ENABLE_SPHINX OFF)
    107   endif()
    108 
    109   # In a standalone build, we don't have llvm to automatically generate the
    110   # llvm-lit script for us.  So we need to provide an explicit directory that
    111   # the configurator should write the script into.
    112   set(LLVM_LIT_OUTPUT_DIR "${libcxx_BINARY_DIR}/bin")
    113 
    114   if (LLVM_INCLUDE_TESTS)
    115     # Required LIT Configuration ------------------------------------------------
    116     # Define the default arguments to use with 'lit', and an option for the user
    117     # to override.
    118     set(LLVM_EXTERNAL_LIT "${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py")
    119     set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
    120     if (MSVC OR XCODE)
    121       set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
    122     endif()
    123     set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
    124   endif()
    125 
    126   # Required doc configuration
    127   if (LLVM_ENABLE_SPHINX)
    128     find_package(Sphinx REQUIRED)
    129   endif()
    130 
    131   if (LLVM_ON_UNIX AND NOT APPLE)
    132     set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
    133   else()
    134     set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
    135   endif()
    136 endmacro(configure_out_of_tree_llvm)
    137 
    138 configure_out_of_tree_llvm()
    139