Skip to content

Commit 497cac6

Browse files
author
Chris Bieneman
committed
[CMake] Add support for Swift buildtrees including CMake packages
This patch generates SwiftExports.cmake and SwiftConfig.cmake in the build tree to be compatible with CMake's `find_package` function for importing targets between CMake build trees. This will allow LLDB to consume Swift's targets and infer their transitive dependencies.
1 parent 13d6b18 commit 497cac6

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ endif()
459459

460460
set(SWIFT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
461461
set(SWIFT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
462+
set(SWIFT_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
463+
set(SWIFT_MAIN_INCLUDE_DIR "${SWIFT_SOURCE_DIR}/include")
464+
set(SWIFT_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
462465

463466
set(SWIFT_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin")
464467
set(SWIFT_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib")
@@ -479,8 +482,8 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
479482

480483
# We'll need this once we have generated headers
481484
include_directories(BEFORE
482-
${CMAKE_CURRENT_BINARY_DIR}/include
483-
${CMAKE_CURRENT_SOURCE_DIR}/include
485+
${SWIFT_MAIN_INCLUDE_DIR}
486+
${SWIFT_INCLUDE_DIR}
484487
)
485488

486489
# A convenience pattern to match Darwin platforms. Example:
@@ -954,6 +957,8 @@ if(SWIFT_INCLUDE_DOCS)
954957
add_subdirectory(docs)
955958
endif()
956959

960+
add_subdirectory(cmake/modules)
961+
957962
swift_install_in_component(license
958963
FILES "LICENSE.txt"
959964
DESTINATION "share/swift")

cmake/modules/AddSwift.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,13 @@ function(add_swift_library name)
12281228
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
12291229
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
12301230
RUNTIME DESTINATION bin)
1231+
swift_is_installing_component(dev is_installing)
1232+
1233+
if(NOT is_installing)
1234+
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name})
1235+
else()
1236+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
1237+
endif()
12311238
endfunction()
12321239

12331240
# Add a new Swift library.
@@ -2079,5 +2086,14 @@ function(add_swift_host_tool executable)
20792086
swift_install_in_component(${ADDSWIFTHOSTTOOL_SWIFT_COMPONENT}
20802087
TARGETS ${executable}
20812088
RUNTIME DESTINATION bin)
2089+
2090+
swift_is_installing_component(${ADDSWIFTHOSTTOOL_SWIFT_COMPONENT}
2091+
is_installing)
2092+
2093+
if(NOT is_installing)
2094+
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${executable})
2095+
else()
2096+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${executable})
2097+
endif()
20822098
endif()
20832099
endfunction()

cmake/modules/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
set(SWIFT_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/swift)
2+
set(swift_cmake_builddir "${SWIFT_BINARY_DIR}/${SWIFT_INSTALL_PACKAGE_DIR}")
3+
4+
# Generate build-tree exports list only
5+
set(SWIFT_EXPORTS_FILE ${swift_cmake_builddir}/SwiftExports.cmake)
6+
get_property(SWIFT_EXPORTS GLOBAL PROPERTY SWIFT_EXPORTS)
7+
get_property(SWIFT_BUILDTREE_EXPORTS GLOBAL PROPERTY SWIFT_BUILDTREE_EXPORTS)
8+
9+
set(SWIFT_CONFIG_EXPORTS ${SWIFT_EXPORTS} ${SWIFT_BUILDTREE_EXPORTS})
10+
export(TARGETS ${SWIFT_CONFIG_EXPORTS} FILE ${SWIFT_EXPORTS_FILE})
11+
12+
13+
set(SWIFT_INCLUDE_DIRS ${SWIFT_INCLUDE_DIR} ${SWIFT_MAIN_INCLUDE_DIR})
14+
set(SWIFT_LIBRARY_DIRS ${SWIFT_LIBRARY_DIR})
15+
16+
configure_file(
17+
SwiftConfig.cmake.in
18+
${swift_cmake_builddir}/SwiftConfig.cmake
19+
@ONLY)

cmake/modules/SwiftConfig.cmake.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file provides information and services to the final user.
2+
3+
@SWIFT_CONFIG_CODE@
4+
5+
set(SWIFT_VERSION @SWIFT_VERSION@)
6+
set(SWIFT_MAIN_SRC_DIR @SWIFT_MAIN_SRC_DIR@)
7+
8+
set(SWIFT_INCLUDE_DIRS "@SWIFT_INCLUDE_DIR@")
9+
set(SWIFT_LIBRARY_DIRS "@SWIFT_CONFIG_LIBRARY_DIRS@")
10+
11+
# These variables are duplicated, but they must match the LLVM variables of the
12+
# same name. The variables ending in "S" could some day become lists, and are
13+
# preserved for convention and compatibility.
14+
set(SWIFT_INCLUDE_DIR "@SWIFT_INCLUDE_DIR@")
15+
set(SWIFT_LIBRARY_DIR "@SWIFT_CONFIG_LIBRARY_DIRS@")
16+
17+
set(SWIFT_CMAKE_DIR "@SWIFT_CMAKE_DIR@")
18+
set(SWIFT_BINARY_DIR "@SWIFT_BINARY_DIR@")
19+
20+
if(NOT TARGET swift)
21+
set(SWIFT_EXPORTED_TARGETS "@SWIFT_CONFIG_EXPORTS@")
22+
include("@SWIFT_EXPORTS_FILE@")
23+
endif()

0 commit comments

Comments
 (0)