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