Skip to content

Commit ca08b5f

Browse files
Merge pull request #401 from compnerd/split-sdk-overlay
Split Swift SDK Overlay
2 parents 9acbab3 + 3526fab commit ca08b5f

File tree

3 files changed

+196
-90
lines changed

3 files changed

+196
-90
lines changed

cmake/modules/SwiftSupport.cmake

Lines changed: 142 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,74 @@
11

22
include(CMakeParseArguments)
33

4-
function(add_swift_library library)
5-
set(options)
4+
function(add_swift_target target)
5+
set(options LIBRARY;SHARED;STATIC)
66
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
7-
set(multiple_value_options SOURCES;SWIFT_FLAGS;CFLAGS;DEPENDS)
7+
set(multiple_value_options CFLAGS;DEPENDS;LINK_FLAGS;SOURCES;SWIFT_FLAGS)
88

9-
cmake_parse_arguments(ASL "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN})
9+
cmake_parse_arguments(AST "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN})
1010

1111
set(flags ${CMAKE_SWIFT_FLAGS})
12+
set(link_flags)
1213

13-
list(APPEND flags -emit-library)
14-
15-
if(ASL_TARGET)
16-
list(APPEND FLAGS -target;${ASL_TARGET})
17-
endif()
18-
if(ASL_MODULE_NAME)
19-
list(APPEND flags -module-name;${ASL_MODULE_NAME})
14+
if(AST_TARGET)
15+
list(APPEND flags -target;${AST_TARGET})
2016
endif()
21-
if(ASL_MODULE_LINK_NAME)
22-
list(APPEND flags -module-link-name;${ASL_MODULE_LINK_NAME})
17+
if(AST_MODULE_NAME)
18+
list(APPEND flags -module-name;${AST_MODULE_NAME})
19+
else()
20+
list(APPEND flags -module-name;${target})
2321
endif()
24-
if(ASL_MODULE_PATH)
25-
list(APPEND flags -emit-module-path;${ASL_MODULE_PATH})
22+
if(AST_MODULE_LINK_NAME)
23+
list(APPEND flags -module-link-name;${AST_MODULE_LINK_NAME})
2624
endif()
27-
if(ASL_MODULE_CACHE_PATH)
28-
list(APPEND flags -module-cache-path;${ASL_MODULE_CACHE_PATH})
25+
if(AST_MODULE_CACHE_PATH)
26+
list(APPEND flags -module-cache-path;${AST_MODULE_CACHE_PATH})
2927
endif()
30-
if(ASL_SWIFT_FLAGS)
31-
foreach(flag ${ASL_SWIFT_FLAGS})
28+
if(AST_SWIFT_FLAGS)
29+
foreach(flag ${AST_SWIFT_FLAGS})
3230
list(APPEND flags ${flag})
3331
endforeach()
3432
endif()
35-
if(ASL_CFLAGS)
36-
foreach(flag ${ASL_CFLAGS})
33+
if(AST_CFLAGS)
34+
foreach(flag ${AST_CFLAGS})
3735
list(APPEND flags -Xcc;${flag})
3836
endforeach()
3937
endif()
40-
41-
# FIXME: We shouldn't /have/ to build things in a single process.
42-
# <rdar://problem/15972329>
43-
list(APPEND flags -force-single-frontend-invocation)
38+
if(AST_LINK_FLAGS)
39+
foreach(flag ${AST_LINK_FLAGS})
40+
list(APPEND link_flags ${flag})
41+
endforeach()
42+
endif()
43+
if(AST_LIBRARY)
44+
if(AST_STATIC AND AST_SHARED)
45+
message(SEND_ERROR "add_swift_target asked to create library as STATIC and SHARED")
46+
elseif(AST_STATIC OR NOT BUILD_SHARED_LIBS)
47+
set(library_kind STATIC)
48+
elseif(AST_SHARED OR BUILD_SHARED_LIBS)
49+
set(library_kind SHARED)
50+
endif()
51+
else()
52+
if(AST_STATIC OR AST_SHARED)
53+
message(SEND_ERROR "add_swift_target asked to create executable as STATIC or SHARED")
54+
endif()
55+
endif()
56+
if(NOT AST_OUTPUT)
57+
if(AST_LIBRARY)
58+
if(AST_SHARED OR BUILD_SHARED_LIBS)
59+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
60+
else()
61+
# NOTE(compnerd) this is a hack for the computation of the
62+
# basename/dirname below for the static path.
63+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target})
64+
endif()
65+
else()
66+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
67+
endif()
68+
endif()
4469

4570
set(sources)
46-
foreach(source ${ASL_SOURCES})
71+
foreach(source ${AST_SOURCES})
4772
get_filename_component(location ${source} PATH)
4873
if(IS_ABSOLUTE ${location})
4974
list(APPEND sources ${source})
@@ -52,25 +77,97 @@ function(add_swift_library library)
5277
endif()
5378
endforeach()
5479

55-
get_filename_component(module_directory ${ASL_MODULE_PATH} DIRECTORY)
56-
57-
add_custom_command(OUTPUT
58-
${ASL_OUTPUT}
59-
${ASL_MODULE_PATH}
60-
${module_directory}/${ASL_MODULE_NAME}.swiftdoc
61-
DEPENDS
62-
${ASL_SOURCES}
63-
${CMAKE_SWIFT_COMPILER}
64-
${ASL_DEPENDS}
65-
COMMAND
66-
${CMAKE_COMMAND} -E make_directory ${module_directory}
67-
COMMAND
68-
${CMAKE_SWIFT_COMPILER} ${flags} -c ${sources} -o ${ASL_OUTPUT})
69-
add_custom_target(${library}
70-
DEPENDS
71-
${ASL_OUTPUT}
72-
${ASL_MODULE_PATH}
73-
${module_directory}/${ASL_MODULE_NAME}.swiftdoc)
80+
set(objs)
81+
set(mods)
82+
set(docs)
83+
set(i 0)
84+
foreach(source ${sources})
85+
get_filename_component(name ${source} NAME)
86+
87+
set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
88+
set(mod ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftmodule)
89+
set(doc ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdoc)
90+
91+
set(all_sources ${sources})
92+
list(INSERT all_sources ${i} -primary-file)
93+
94+
add_custom_command(OUTPUT
95+
${obj}
96+
${mod}
97+
${doc}
98+
DEPENDS
99+
${source}
100+
${AST_DEPENDS}
101+
COMMAND
102+
${CMAKE_SWIFT_COMPILER} -frontend ${flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources})
103+
104+
list(APPEND objs ${obj})
105+
list(APPEND mods ${mod})
106+
list(APPEND docs ${doc})
107+
108+
math(EXPR i "${i}+1")
109+
endforeach()
110+
111+
if(AST_LIBRARY)
112+
get_filename_component(module_directory ${AST_MODULE_PATH} DIRECTORY)
113+
114+
set(module ${AST_MODULE_PATH})
115+
set(documentation ${module_directory}/${AST_MODULE_NAME}.swiftdoc)
116+
117+
add_custom_command(OUTPUT
118+
${module}
119+
${documentation}
120+
DEPENDS
121+
${mods}
122+
${docs}
123+
${AST_DEPENDS}
124+
COMMAND
125+
${CMAKE_SWIFT_COMPILER} -frontend ${flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation})
126+
endif()
127+
128+
if(AST_LIBRARY)
129+
set(emit_library -emit-library)
130+
endif()
131+
if(library_kind STREQUAL SHARED)
132+
add_custom_command(OUTPUT
133+
${AST_OUTPUT}
134+
DEPENDS
135+
${objs}
136+
${AST_DEPENDS}
137+
COMMAND
138+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs}
139+
COMMAND
140+
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
141+
add_custom_target(${target}
142+
ALL
143+
DEPENDS
144+
${AST_OUTPUT}
145+
${module}
146+
${documentation})
147+
else()
148+
add_library(${target}-static STATIC ${objs})
149+
get_filename_component(ast_output_bn ${AST_OUTPUT} NAME)
150+
get_filename_component(ast_output_dn ${AST_OUTPUT} DIRECTORY)
151+
set_target_properties(${target}-static
152+
PROPERTIES
153+
LINKER_LANGUAGE C
154+
OUTPUT_DIRECTORY ${ast_output_dn}
155+
OUTPUT_NAME ${ast_output_bn})
156+
add_custom_target(${target}
157+
ALL
158+
DEPENDS
159+
${target}-static
160+
${module}
161+
${documentation})
162+
endif()
163+
endfunction()
164+
165+
function(add_swift_library library)
166+
add_swift_target(${library} LIBRARY ${ARGN})
167+
endfunction()
168+
169+
function(add_swift_executable executable)
170+
add_swift_target(${executable} ${ARGN})
74171
endfunction()
75172

76173
# Returns the current achitecture name in a variable

src/CMakeLists.txt

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,60 @@ target_sources(dispatch
7272
PRIVATE
7373
block.cpp)
7474
if(HAVE_OBJC)
75+
# TODO(compnerd) split DispatchStubs.cc into a separate component for the ObjC
76+
# registration and a separate component for the swift compiler's emission of a
77+
# call to the ObjC autorelease elision entry point.
7578
target_sources(dispatch
7679
PRIVATE
7780
data.m
78-
object.m)
81+
object.m
82+
swift/DispatchStubs.cc)
7983
endif()
8084
if(ENABLE_SWIFT)
8185
set(swift_optimization_flags)
8286
if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
8387
set(swift_optimization_flags -O)
8488
endif()
89+
90+
# NOTE(compnerd) Today regardless of whether or not ObjC interop is enabled,
91+
# swift will use an autoreleased return value convention for certain CF
92+
# functions (including some that are used/related to dispatch). This means
93+
# that the swift compiler in callers to such functions will call the function,
94+
# and then pass the result of the function to
95+
# objc_retainAutoreleasedReturnValue. In a context where we have ObjC interop
96+
# disabled, we do not have access to the objc runtime so an implementation of
97+
# objc_retainAutoreleasedReturnValue is not available. To work around this, we
98+
# provide a shim for objc_retainAutoreleasedReturnValue in DispatchStubs.cc
99+
# that just calls retain on the object. Once we fix the swift compiler to
100+
# switch to a different model for handling these arguments with objc-interop
101+
# disabled these shims can be eliminated.
102+
add_library(DispatchStubs
103+
STATIC
104+
swift/DispatchStubs.cc)
105+
target_include_directories(DispatchStubs
106+
PRIVATE
107+
${PROJECT_SOURCE_DIR})
108+
set_target_properties(DispatchStubs
109+
PROPERTIES
110+
POSITION_INDEPENDENT_CODE YES)
111+
85112
add_swift_library(swiftDispatch
113+
CFLAGS
114+
-fblocks
115+
-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
116+
DEPENDS
117+
${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
118+
DispatchStubs
119+
LINK_FLAGS
120+
-lDispatchStubs
121+
-L $<TARGET_LINKER_FILE_DIR:dispatch>
122+
-ldispatch
86123
MODULE_NAME
87124
Dispatch
88125
MODULE_LINK_NAME
89-
dispatch
126+
swiftDispatch
90127
MODULE_PATH
91128
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
92-
OUTPUT
93-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
94129
SOURCES
95130
swift/Block.swift
96131
swift/Data.swift
@@ -101,32 +136,12 @@ if(ENABLE_SWIFT)
101136
swift/Source.swift
102137
swift/Time.swift
103138
swift/Wrapper.swift
104-
TARGET
105-
${CMAKE_C_COMPILER_TARGET}
106-
CFLAGS
107-
-fblocks
108-
-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
109139
SWIFT_FLAGS
110140
-I ${PROJECT_SOURCE_DIR}
111141
-I/usr/include
112142
${swift_optimization_flags}
113-
DEPENDS
114-
${PROJECT_SOURCE_DIR}/dispatch/module.modulemap)
115-
116-
get_filename_component(swift_toolchain ${CMAKE_SWIFT_COMPILER} DIRECTORY)
117-
get_filename_component(swift_toolchain ${swift_toolchain} DIRECTORY)
118-
set(swift_runtime_libdir ${swift_toolchain}/lib/${swift_dir}/${swift_os}/${swift_arch})
119-
120-
target_sources(dispatch
121-
PRIVATE
122-
swift/DispatchStubs.cc
123-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
124-
${swift_runtime_libdir}/swiftrt.o)
125-
if(CMAKE_BUILD_TYPE MATCHES Debug)
126-
target_link_libraries(dispatch
127-
PRIVATE
128-
swiftSwiftOnoneSupport)
129-
endif()
143+
TARGET
144+
${CMAKE_C_COMPILER_TARGET})
130145
endif()
131146
if(ENABLE_DTRACE)
132147
dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d
@@ -231,8 +246,6 @@ add_custom_command(TARGET dispatch POST_BUILD
231246
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:dispatch> .libs
232247
COMMENT "Copying libdispatch to .libs")
233248

234-
get_swift_host_arch(SWIFT_HOST_ARCH)
235-
236249
install(TARGETS
237250
dispatch
238251
DESTINATION
@@ -242,6 +255,18 @@ if(ENABLE_SWIFT)
242255
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
243256
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
244257
DESTINATION
245-
"${INSTALL_TARGET_DIR}/${SWIFT_HOST_ARCH}")
258+
${INSTALL_TARGET_DIR}/${swift_arch})
259+
260+
if(BUILD_SHARED_LIBS)
261+
set(library_kind SHARED)
262+
else()
263+
set(library_kind STATIC)
264+
endif()
265+
set(swiftDispatch_OUTPUT_FILE
266+
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_${library_kind}_LIBRARY_PREFIX}swiftDispatch${CMAKE_${library_kind}_LIBRARY_SUFFIX})
267+
install(FILES
268+
${swiftDispatch_OUTPUT_FILE}
269+
DESTINATION
270+
${INSTALL_TARGET_DIR})
246271
endif()
247272

tests/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ if(BSD_OVERLAY_FOUND)
5656
PRIVATE
5757
${BSD_OVERLAY_LDFLAGS})
5858
endif()
59-
if(ENABLE_SWIFT)
60-
target_link_libraries(bsdtestharness
61-
PRIVATE
62-
swiftCore-${swift_os}-${swift_arch}
63-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
64-
endif()
6559

6660
function(add_unit_test name)
6761
set(options DISABLED_TEST;NO_BSD_OVERLAY)
@@ -83,10 +77,6 @@ function(add_unit_test name)
8377
# For testing in swift.org CI system; make deadlines lenient by default
8478
# to reduce probability of test failures due to machine load.
8579
target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1)
86-
target_link_libraries(${name}
87-
PRIVATE
88-
swiftCore-${swift_os}-${swift_arch}
89-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
9080
endif()
9181
target_include_directories(${name}
9282
SYSTEM BEFORE PRIVATE
@@ -114,12 +104,6 @@ function(add_unit_test name)
114104
PRIVATE
115105
${BSD_OVERLAY_LDFLAGS})
116106
endif()
117-
if(ENABLE_SWIFT)
118-
target_link_libraries(${name}
119-
PRIVATE
120-
swiftCore-${swift_os}-${swift_arch}
121-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
122-
endif()
123107
target_link_libraries(${name} PRIVATE bsdtests)
124108
add_test(NAME ${name}
125109
COMMAND bsdtestharness $<TARGET_FILE:${name}>)

0 commit comments

Comments
 (0)