Skip to content

Commit a0480e5

Browse files
authored
Merge pull request #3299 from gottesmm/fix_xcode_builds
Fix xcode builds
2 parents 1625d97 + c2645d0 commit a0480e5

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,6 @@ if(NOT EXISTS "${CLANG_MAIN_INCLUDE_DIR}/clang/AST/Decl.h")
384384
message(FATAL_ERROR "Clang is missing from llvm/tools subdirectory.")
385385
endif()
386386

387-
# This could be computed using ${CMAKE_CFG_INTDIR} if we want to link Swift
388-
# against a matching LLVM build configuration. However, we usually want to be
389-
# flexible and allow linking a debug Swift against optimized LLVM.
390-
set(LLVM_RUNTIME_OUTPUT_INTDIR "${LLVM_BINARY_DIR}")
391-
set(LLVM_LIBRARY_OUTPUT_INTDIR "${LLVM_LIBRARY_DIR}")
392-
393387
set(SWIFT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
394388
set(SWIFT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
395389

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,57 @@ else()
1212
set(cmake_3_2_USES_TERMINAL USES_TERMINAL)
1313
endif()
1414

15+
function(get_effective_platform_for_triple triple output)
16+
string(FIND "${triple}" "macos" IS_MACOS)
17+
if (IS_MACOS)
18+
set(${output} "" PARENT_SCOPE)
19+
return()
20+
endif()
21+
message(FATAL_ERROR "Not supported")
22+
endfunction()
23+
24+
# Eliminate $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) from a path.
25+
#
26+
# We do not support compiling llvm with an Xcode setting beyond the one that was
27+
# used with build-script. This allows us to remove those paths. Right now,
28+
# nothing here is tested for cross compiling with Xcode, but it is in principal
29+
# possible.
30+
function(escape_llvm_path_for_xcode path outvar)
31+
# First check if we are using Xcode. If not, return early.
32+
if (NOT XCODE)
33+
set(${outvar} "${path}" PARENT_SCOPE)
34+
return()
35+
endif()
36+
37+
get_effective_platform_for_triple("${SWIFT_HOST_TRIPLE}" SWIFT_EFFECTIVE_PLATFORM_NAME)
38+
string(REPLACE "$(CONFIGURATION)" "${LLVM_BUILD_TYPE}" path "${path}")
39+
string(REPLACE "$(EFFECTIVE_PLATFORM_NAME)" "${SWIFT_EFFECTIVE_PLATFORM_NAME}" path "${path}")
40+
set(${outvar} "${path}" PARENT_SCOPE)
41+
endfunction()
42+
43+
# What this function does is go through each of the passed in imported targets
44+
# from LLVM/Clang and changes them to use the appropriate fully expanded paths.
45+
#
46+
# TODO: This function needs a better name.
47+
function(fix_imported_target_locations_for_xcode targets)
48+
foreach(t ${targets})
49+
if (NOT TARGET ${t})
50+
message(FATAL_ERROR "${t} is not a target?!")
51+
endif()
52+
53+
get_target_property(TARGET_TYPE ${t} TYPE)
54+
55+
# We only want to do this for static libraries for now.
56+
if (NOT ${TARGET_TYPE} STREQUAL "STATIC_LIBRARY")
57+
continue()
58+
endif()
59+
60+
set_target_properties(${t} PROPERTIES
61+
IMPORTED_LOCATION_DEBUG "${LLVM_LIBRARY_OUTPUT_INTDIR}/lib${t}.a"
62+
IMPORTED_LOCATION_RELEASE "${LLVM_LIBRARY_OUTPUT_INTDIR}/lib${t}.a")
63+
endforeach()
64+
endfunction()
65+
1566
macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
1667
option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
1768

@@ -34,7 +85,8 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
3485

3586
# Then we import LLVMConfig. This is going to override whatever cached value
3687
# we have for LLVM_ENABLE_ASSERTIONS.
37-
include(LLVMConfig)
88+
find_package(LLVM REQUIRED CONFIG
89+
HINTS "${PATH_TO_LLVM_BUILD}" NO_DEFAULT_PATH)
3890

3991
# If we did not have a cached value for LLVM_ENABLE_ASSERTIONS, set
4092
# LLVM_ENABLE_ASSERTIONS_saved to be the ENABLE_ASSERTIONS value from LLVM so
@@ -54,20 +106,33 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
54106
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
55107

56108
precondition(LLVM_TOOLS_BINARY_DIR)
109+
escape_llvm_path_for_xcode("${LLVM_TOOLS_BINARY_DIR}" LLVM_TOOLS_BINARY_DIR)
57110
precondition_translate_flag(LLVM_BUILD_LIBRARY_DIR LLVM_LIBRARY_DIR)
111+
escape_llvm_path_for_xcode("${LLVM_LIBRARY_DIR}" LLVM_LIBRARY_DIR)
58112
precondition_translate_flag(LLVM_BUILD_MAIN_INCLUDE_DIR LLVM_MAIN_INCLUDE_DIR)
59113
precondition_translate_flag(LLVM_BUILD_BINARY_DIR LLVM_BINARY_DIR)
60114
precondition_translate_flag(LLVM_BUILD_MAIN_SRC_DIR LLVM_MAIN_SRC_DIR)
115+
precondition(LLVM_LIBRARY_DIRS)
116+
escape_llvm_path_for_xcode("${LLVM_LIBRARY_DIRS}" LLVM_LIBRARY_DIRS)
61117

62-
if(${is_cross_compiling})
63-
find_program(SWIFT_TABLEGEN_EXE "llvm-tblgen" "${${product}_NATIVE_LLVM_TOOLS_PATH}"
64-
NO_DEFAULT_PATH)
65-
if ("${SWIFT_TABLEGEN_EXE}" STREQUAL "SWIFT_TABLEGEN_EXE-NOTFOUND")
66-
message(FATAL_ERROR "Failed to find tablegen in ${${product}_NATIVE_LLVM_TOOLS_PATH}")
67-
endif()
68-
else()
69-
set(SWIFT_TABLEGEN_EXE llvm-tblgen)
70-
set(${product}_NATIVE_LLVM_TOOLS_PATH "${PATH_TO_LLVM_TOOLS_BINARY_DIR}")
118+
# This could be computed using ${CMAKE_CFG_INTDIR} if we want to link Swift
119+
# against a matching LLVM build configuration. However, we usually want to be
120+
# flexible and allow linking a debug Swift against optimized LLVM.
121+
set(LLVM_RUNTIME_OUTPUT_INTDIR "${LLVM_BINARY_DIR}")
122+
set(LLVM_LIBRARY_OUTPUT_INTDIR "${LLVM_LIBRARY_DIR}")
123+
124+
if (XCODE)
125+
fix_imported_target_locations_for_xcode("${LLVM_EXPORTED_TARGETS}")
126+
endif()
127+
128+
if(NOT ${is_cross_compiling})
129+
set(${product}_NATIVE_LLVM_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR}")
130+
endif()
131+
132+
find_program(SWIFT_TABLEGEN_EXE "llvm-tblgen" "${${product}_NATIVE_LLVM_TOOLS_PATH}"
133+
NO_DEFAULT_PATH)
134+
if ("${SWIFT_TABLEGEN_EXE}" STREQUAL "SWIFT_TABLEGEN_EXE-NOTFOUND")
135+
message(FATAL_ERROR "Failed to find tablegen in ${${product}_NATIVE_LLVM_TOOLS_PATH}")
71136
endif()
72137

73138
include(AddLLVM)
@@ -85,6 +150,7 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
85150
"Version number that will be placed into the libclang library , in the form XX.YY")
86151

87152
foreach (INCLUDE_DIR ${LLVM_INCLUDE_DIRS})
153+
escape_llvm_path_for_xcode("${INCLUDE_DIR}" INCLUDE_DIR)
88154
include_directories(${INCLUDE_DIR})
89155
endforeach ()
90156

@@ -128,10 +194,9 @@ macro(swift_common_standalone_build_config_clang product is_cross_compiling)
128194
list(APPEND CMAKE_MODULE_PATH ${path})
129195
endforeach()
130196

131-
# Then include ClangTargets.cmake. If Clang adds a ClangConfig.cmake, this is
132-
# where it will be included. By including ClangTargets.cmake, we at least get
133-
# the right dependency ordering for clang libraries.
134-
include(ClangTargets)
197+
# Then include Clang.
198+
find_package(Clang REQUIRED CONFIG
199+
HINTS "${PATH_TO_CLANG_BUILD}" NO_DEFAULT_PATH)
135200

136201
if(NOT EXISTS "${PATH_TO_CLANG_SOURCE}/include/clang/AST/Decl.h")
137202
message(FATAL_ERROR "Please set ${product}_PATH_TO_CLANG_SOURCE to the root directory of Clang's source code.")
@@ -144,11 +209,15 @@ macro(swift_common_standalone_build_config_clang product is_cross_compiling)
144209
set(CLANG_BUILD_INCLUDE_DIR "${PATH_TO_CLANG_BUILD}/tools/clang/include")
145210

146211
if (NOT ${is_cross_compiling})
147-
set(${product}_NATIVE_CLANG_TOOLS_PATH "${PATH_TO_LLVM_TOOLS_BINARY_DIR}")
212+
set(${product}_NATIVE_CLANG_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR}")
148213
endif()
149214

150215
set(CLANG_MAIN_INCLUDE_DIR "${CLANG_MAIN_SRC_DIR}/include")
151216

217+
if (XCODE)
218+
fix_imported_target_locations_for_xcode("${CLANG_EXPORTED_TARGETS}")
219+
endif()
220+
152221
include_directories("${CLANG_BUILD_INCLUDE_DIR}"
153222
"${CLANG_MAIN_INCLUDE_DIR}")
154223
endmacro()

0 commit comments

Comments
 (0)