@@ -3,28 +3,37 @@ include(SwiftXcodeSupport)
33
44include (CheckCXXCompilerFlag)
55
6- macro (swift_common_standalone_build_config_llvm product)
6+ macro (swift_common_standalone_build_config_llvm product is_cross_compiling )
77 option (LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON )
88
9+ precondition_translate_flag(${product} _PATH_TO_LLVM_SOURCE PATH_TO_LLVM_SOURCE)
10+ precondition_translate_flag(${product} _PATH_TO_LLVM_BUILD PATH_TO_LLVM_BUILD)
11+
12+ file (TO_CMAKE_PATH "${PATH_TO_LLVM_BUILD} " PATH_TO_LLVM_BUILD)
13+
14+ set (SWIFT_LLVM_CMAKE_PATHS
15+ "${PATH_TO_LLVM_BUILD} /share/llvm/cmake"
16+ "${PATH_TO_LLVM_BUILD} /lib/cmake/llvm" )
17+
18+ # Add all LLVM CMake paths to our cmake module path.
19+ foreach (path ${SWIFT_LLVM_CMAKE_PATHS} )
20+ list (APPEND CMAKE_MODULE_PATH ${path} )
21+ endforeach ()
22+
923 # If we already have a cached value for LLVM_ENABLE_ASSERTIONS, save the value.
10- if (DEFINED LLVM_ENABLE_ASSERTIONS)
24+ if (DEFINED LLVM_ENABLE_ASSERTIONS)
1125 set (LLVM_ENABLE_ASSERTIONS_saved "${LLVM_ENABLE_ASSERTIONS} " )
1226 endif ()
1327
1428 # Then we import LLVMConfig. This is going to override whatever cached value
1529 # we have for LLVM_ENABLE_ASSERTIONS.
16- find_package (LLVM CONFIG REQUIRED NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
17- list (APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR} " )
18-
19- set (LLVM_MAIN_SRC_DIR "${LLVM_BUILD_MAIN_SRC_DIR} "
20- CACHE PATH "Path to LLVM source tree" )
21- set (LLVM_MAIN_INCLUDE_DIR "${LLVM_BUILD_MAIN_INCLUDE_DIR} "
22- CACHE PATH "Path to llvm/include" )
30+ find_package (LLVM REQUIRED CONFIG
31+ HINTS "${PATH_TO_LLVM_BUILD} " NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
2332
2433 # If we did not have a cached value for LLVM_ENABLE_ASSERTIONS, set
2534 # LLVM_ENABLE_ASSERTIONS_saved to be the ENABLE_ASSERTIONS value from LLVM so
2635 # we follow LLVMConfig.cmake's value by default if nothing is provided.
27- if (NOT DEFINED LLVM_ENABLE_ASSERTIONS_saved)
36+ if (NOT DEFINED LLVM_ENABLE_ASSERTIONS_saved)
2837 set (LLVM_ENABLE_ASSERTIONS_saved "${LLVM_ENABLE_ASSERTIONS} " )
2938 endif ()
3039
@@ -40,10 +49,11 @@ macro(swift_common_standalone_build_config_llvm product)
4049
4150 precondition(LLVM_TOOLS_BINARY_DIR )
4251 escape_path_for_xcode("${LLVM_BUILD_TYPE} " "${LLVM_TOOLS_BINARY_DIR} " LLVM_TOOLS_BINARY_DIR )
43-
4452 precondition_translate_flag(LLVM_BUILD_LIBRARY_DIR LLVM_LIBRARY_DIR)
4553 escape_path_for_xcode("${LLVM_BUILD_TYPE} " "${LLVM_LIBRARY_DIR} " LLVM_LIBRARY_DIR)
46-
54+ precondition_translate_flag(LLVM_BUILD_MAIN_INCLUDE_DIR LLVM_MAIN_INCLUDE_DIR)
55+ precondition_translate_flag(LLVM_BUILD_BINARY_DIR LLVM_BINARY_DIR )
56+ precondition_translate_flag(LLVM_BUILD_MAIN_SRC_DIR LLVM_MAIN_SRC_DIR)
4757 precondition(LLVM_LIBRARY_DIRS)
4858 escape_path_for_xcode("${LLVM_BUILD_TYPE} " "${LLVM_LIBRARY_DIRS} " LLVM_LIBRARY_DIRS)
4959
@@ -54,41 +64,18 @@ macro(swift_common_standalone_build_config_llvm product)
5464 set (LLVM_BINARY_OUTPUT_INTDIR "${LLVM_TOOLS_BINARY_DIR} " )
5565 set (LLVM_LIBRARY_OUTPUT_INTDIR "${LLVM_LIBRARY_DIR} " )
5666
57- if (XCODE)
67+ if (XCODE)
5868 fix_imported_targets_for_xcode("${LLVM_EXPORTED_TARGETS} " )
5969 endif ()
6070
61- if (NOT CMAKE_CROSSCOMPILING )
71+ if (NOT ${is_cross_compiling} )
6272 set (${product} _NATIVE_LLVM_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR} " )
6373 endif ()
6474
65- if (CMAKE_CROSSCOMPILING )
66- set (LLVM_NATIVE_BUILD "${LLVM_BINARY_DIR} /NATIVE" )
67- if (NOT EXISTS "${LLVM_NATIVE_BUILD} " )
68- message (FATAL_ERROR
69- "Attempting to cross-compile swift standalone but no native LLVM build
70- found. Please cross-compile LLVM as well." )
71- endif ()
72-
73- if (CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
74- set (HOST_EXECUTABLE_SUFFIX ".exe" )
75- endif ()
76-
77- if (NOT CMAKE_CONFIGURATION_TYPES )
78- set (LLVM_TABLEGEN_EXE
79- "${LLVM_NATIVE_BUILD_DIR} /bin/llvm-tblgen${HOST_EXECUTABLE_SUFFIX} " )
80- else ()
81- # NOTE: LLVM NATIVE build is always built Release, as is specified in
82- # CrossCompile.cmake
83- set (LLVM_TABLEGEN_EXE
84- "${LLVM_NATIVE_BUILD_DIR} /Release/bin/llvm-tblgen${HOST_EXECUTABLE_SUFFIX} " )
85- endif ()
86- else ()
87- find_program (LLVM_TABLEGEN_EXE "llvm-tblgen" HINTS ${LLVM_TOOLS_BINARY_DIR}
88- NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
89- if (LLVM_TABLEGEN_EXE STREQUAL "LLVM_TABLEGEN_EXE-NOTFOUND" )
90- message (FATAL_ERROR "Failed to find tablegen in ${LLVM_TOOLS_BINARY_DIR} " )
91- endif ()
75+ find_program (LLVM_TABLEGEN_EXE "llvm-tblgen" "${${product} _NATIVE_LLVM_TOOLS_PATH}"
76+ NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
77+ if ("${LLVM_TABLEGEN_EXE} " STREQUAL "LLVM_TABLEGEN_EXE-NOTFOUND" )
78+ message (FATAL_ERROR "Failed to find tablegen in ${${product} _NATIVE_LLVM_TOOLS_PATH}" )
9279 endif ()
9380
9481 include (AddLLVM)
@@ -119,7 +106,7 @@ macro(swift_common_standalone_build_config_llvm product)
119106 "${PACKAGE_VERSION_MAJOR} .${PACKAGE_VERSION_MINOR} " CACHE STRING
120107 "Version number that will be placed into the libclang library , in the form XX.YY" )
121108
122- foreach (INCLUDE_DIR ${LLVM_INCLUDE_DIRS} )
109+ foreach (INCLUDE_DIR ${LLVM_INCLUDE_DIRS} )
123110 escape_path_for_xcode("${LLVM_BUILD_TYPE} " "${INCLUDE_DIR} " INCLUDE_DIR)
124111 include_directories (${INCLUDE_DIR} )
125112 endforeach ()
@@ -147,18 +134,52 @@ macro(swift_common_standalone_build_config_llvm product)
147134 endif ()
148135endmacro ()
149136
150- macro (swift_common_standalone_build_config_clang product)
151- find_package (Clang CONFIG REQUIRED NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
137+ macro (swift_common_standalone_build_config_clang product is_cross_compiling)
138+ set (${product} _PATH_TO_CLANG_SOURCE "${PATH_TO_LLVM_SOURCE} /tools/clang"
139+ CACHE PATH "Path to Clang source code." )
140+ set (${product} _PATH_TO_CLANG_BUILD "${PATH_TO_LLVM_BUILD} " CACHE PATH
141+ "Path to the directory where Clang was built or installed." )
142+
143+ set (PATH_TO_CLANG_SOURCE "${${product} _PATH_TO_CLANG_SOURCE}" )
144+ set (PATH_TO_CLANG_BUILD "${${product} _PATH_TO_CLANG_BUILD}" )
145+
146+ file (TO_CMAKE_PATH "${PATH_TO_CLANG_SOURCE} " PATH_TO_CLANG_SOURCE)
147+ file (TO_CMAKE_PATH "${PATH_TO_CLANG_BUILD} " PATH_TO_CLANG_BUILD)
148+
149+ # Add all Clang CMake paths to our cmake module path.
150+ set (SWIFT_CLANG_CMAKE_PATHS
151+ "${PATH_TO_CLANG_BUILD} /share/clang/cmake"
152+ "${PATH_TO_CLANG_BUILD} /lib/cmake/clang" )
153+ foreach (path ${SWIFT_CLANG_CMAKE_PATHS} )
154+ list (APPEND CMAKE_MODULE_PATH ${path} )
155+ endforeach ()
156+
157+ # Then include Clang.
158+ find_package (Clang REQUIRED CONFIG
159+ HINTS "${PATH_TO_CLANG_BUILD} " NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH )
152160
153- if (NOT CMAKE_CROSSCOMPILING )
161+ if (NOT EXISTS "${PATH_TO_CLANG_SOURCE} /include/clang/AST/Decl.h" )
162+ message (FATAL_ERROR "Please set ${product} _PATH_TO_CLANG_SOURCE to the root directory of Clang's source code." )
163+ endif ()
164+ get_filename_component (CLANG_MAIN_SRC_DIR "${PATH_TO_CLANG_SOURCE} " ABSOLUTE )
165+
166+ if (NOT EXISTS "${PATH_TO_CLANG_BUILD} /tools/clang/include/clang/Basic/Version.inc" )
167+ message (FATAL_ERROR "Please set ${product} _PATH_TO_CLANG_BUILD to a directory containing a Clang build." )
168+ endif ()
169+ set (CLANG_BUILD_INCLUDE_DIR "${PATH_TO_CLANG_BUILD} /tools/clang/include" )
170+
171+ if (NOT ${is_cross_compiling} )
154172 set (${product} _NATIVE_CLANG_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR} " )
155173 endif ()
156174
157- if (XCODE)
175+ set (CLANG_MAIN_INCLUDE_DIR "${CLANG_MAIN_SRC_DIR} /include" )
176+
177+ if (XCODE)
158178 fix_imported_targets_for_xcode("${CLANG_EXPORTED_TARGETS} " )
159179 endif ()
160180
161- include_directories (${CLANG_INCLUDE_DIRS} )
181+ include_directories ("${CLANG_BUILD_INCLUDE_DIR} "
182+ "${CLANG_MAIN_INCLUDE_DIR} " )
162183endmacro ()
163184
164185macro (swift_common_standalone_build_config_cmark product)
@@ -197,9 +218,9 @@ endmacro()
197218#
198219# is_cross_compiling
199220# Whether this is cross-compiling host tools.
200- macro (swift_common_standalone_build_config product)
201- swift_common_standalone_build_config_llvm(${product} )
202- swift_common_standalone_build_config_clang(${product} )
221+ macro (swift_common_standalone_build_config product is_cross_compiling )
222+ swift_common_standalone_build_config_llvm(${product} ${is_cross_compiling} )
223+ swift_common_standalone_build_config_clang(${product} ${is_cross_compiling} )
203224 swift_common_standalone_build_config_cmark(${product} )
204225
205226 # Enable groups for IDE generators (Xcode and MSVC).
0 commit comments