Skip to content

Commit 5e486f1

Browse files
committed
cmake: update SwiftSupport from XCTest
Update the swift support CMake rules from XCTest. This adds support for additional features like partial module compilation which improves incremental builds. It allows for executable and library builds.
1 parent 31f4c3f commit 5e486f1

File tree

1 file changed

+97
-39
lines changed

1 file changed

+97
-39
lines changed

cmake/modules/SwiftSupport.cmake

Lines changed: 97 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11

22
include(CMakeParseArguments)
33

4-
function(add_swift_library library)
5-
set(options)
4+
function(add_swift_target target)
5+
set(options LIBRARY)
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})
14+
if(AST_TARGET)
15+
list(APPEND flags -target;${AST_TARGET})
1716
endif()
18-
if(ASL_MODULE_NAME)
19-
list(APPEND flags -module-name;${ASL_MODULE_NAME})
20-
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(NOT AST_OUTPUT)
44+
if(AST_LIBRARY)
45+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
46+
else()
47+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
48+
endif()
49+
endif()
4450

4551
set(sources)
46-
foreach(source ${ASL_SOURCES})
52+
foreach(source ${AST_SOURCES})
4753
get_filename_component(location ${source} PATH)
4854
if(IS_ABSOLUTE ${location})
4955
list(APPEND sources ${source})
@@ -52,25 +58,77 @@ function(add_swift_library library)
5258
endif()
5359
endforeach()
5460

55-
get_filename_component(module_directory ${ASL_MODULE_PATH} DIRECTORY)
61+
set(objs)
62+
set(mods)
63+
set(docs)
64+
set(i 0)
65+
foreach(source ${sources})
66+
get_filename_component(name ${source} NAME)
67+
68+
set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
69+
set(mod ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftmodule)
70+
set(doc ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdoc)
71+
72+
set(all_sources ${sources})
73+
list(INSERT all_sources ${i} -primary-file)
74+
75+
add_custom_command(OUTPUT
76+
${obj}
77+
${mod}
78+
${doc}
79+
DEPENDS
80+
${source}
81+
COMMAND
82+
${CMAKE_SWIFT_COMPILER} -frontend ${flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources})
83+
84+
list(APPEND objs ${obj})
85+
list(APPEND mods ${mod})
86+
list(APPEND docs ${doc})
87+
88+
math(EXPR i "${i}+1")
89+
endforeach()
90+
91+
if(AST_LIBRARY)
92+
get_filename_component(module_directory ${AST_MODULE_PATH} DIRECTORY)
93+
94+
set(module ${AST_MODULE_PATH})
95+
set(documentation ${module_directory}/${AST_MODULE_NAME}.swiftdoc)
96+
97+
add_custom_command(OUTPUT
98+
${module}
99+
${documentation}
100+
DEPENDS
101+
${mods}
102+
${docs}
103+
COMMAND
104+
${CMAKE_SWIFT_COMPILER} -frontend ${flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation})
105+
endif()
56106

107+
if(AST_LIBRARY)
108+
set(emit_library -emit-library)
109+
endif()
57110
add_custom_command(OUTPUT
58-
${ASL_OUTPUT}
59-
${ASL_MODULE_PATH}
60-
${module_directory}/${ASL_MODULE_NAME}.swiftdoc
111+
${AST_OUTPUT}
61112
DEPENDS
62-
${ASL_SOURCES}
63-
${CMAKE_SWIFT_COMPILER}
64-
${ASL_DEPENDS}
113+
${objs}
65114
COMMAND
66-
${CMAKE_COMMAND} -E make_directory ${module_directory}
115+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs}
67116
COMMAND
68-
${CMAKE_SWIFT_COMPILER} ${flags} -c ${sources} -o ${ASL_OUTPUT})
69-
add_custom_target(${library}
117+
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
118+
add_custom_target(${target}
119+
ALL
70120
DEPENDS
71-
${ASL_OUTPUT}
72-
${ASL_MODULE_PATH}
73-
${module_directory}/${ASL_MODULE_NAME}.swiftdoc)
121+
${AST_OUTPUT}
122+
${module}
123+
${documentation})
124+
endfunction()
125+
126+
function(add_swift_library library)
127+
add_swift_target(${library} LIBRARY ${ARGN})
128+
endfunction()
129+
130+
function(add_swift_executable executable)
131+
add_swift_target(${executable} ${ARGN})
74132
endfunction()
75133

76134
# Returns the current achitecture name in a variable

0 commit comments

Comments
 (0)