Skip to content

Commit e549d37

Browse files
committed
[cmake] Update SwiftSupport.cmake to match Foundations's
Also, pass the explicit target to the Swift library creation, so one can cross compile.
1 parent b5ecf30 commit e549d37

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ add_swift_library(XCTest
8888
Sources/XCTest/Public/Asynchronous/XCTWaiter.swift
8989
Sources/XCTest/Public/Asynchronous/XCTestCase+Asynchronous.swift
9090
Sources/XCTest/Public/Asynchronous/XCTestExpectation.swift
91+
TARGET
92+
${CMAKE_C_COMPILER_TARGET}
9193
SWIFT_FLAGS
9294
$<$<NOT:$<CONFIG:Debug>>:-O>
9395

cmake/modules/SwiftSupport.cmake

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11

22
include(CMakeParseArguments)
3+
include(ProcessorCount)
4+
5+
# Creates an output file map give a target and its list of sources at
6+
# output_path
7+
#
8+
# Usage:
9+
# create_output_file_map(target sources output_path)
10+
function(create_output_file_map target sources output_path)
11+
set(output_list)
12+
set(output_file_map "{\n")
13+
foreach(source ${sources})
14+
get_filename_component(name ${source} NAME)
15+
16+
set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
17+
set(deps ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.d)
18+
set(swiftdeps ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdeps)
19+
set(dia ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.dia)
20+
set(output_entry "\"${source}\": {\n\
21+
\"object\": \"${obj}\",\n\
22+
\"dependencies\": \"${deps}\",\n\
23+
\"swift-dependencies\": \"${swiftdeps}\",\n\
24+
\"diagnostics\": \"${dia}\"\n\
25+
},\n")
26+
string(APPEND output_file_map ${output_entry})
27+
endforeach()
28+
set(master_deps ${CMAKE_CURRENT_BINARY_DIR}/${target}.swiftdeps)
29+
string(APPEND output_file_map "\"\": {\n\
30+
\"swift-dependencies\": \"${master_deps}\"\n\
31+
}\n")
32+
string(APPEND output_file_map "}\n")
33+
file(WRITE ${output_path} ${output_file_map})
34+
endfunction()
335

436
function(add_swift_target target)
537
set(options LIBRARY;SHARED;STATIC)
@@ -9,8 +41,13 @@ function(add_swift_target target)
941
cmake_parse_arguments(AST "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN})
1042

1143
set(compile_flags ${CMAKE_SWIFT_FLAGS})
12-
set(link_flags)
44+
set(link_flags ${CMAKE_SWIFT_LINK_FLAGS})
1345

46+
list(APPEND compile_flags -incremental)
47+
ProcessorCount(CPU_COUNT)
48+
if(NOT CPU_COUNT EQUAL 0)
49+
list(APPEND compile_flags -j;${CPU_COUNT})
50+
endif()
1451
if(AST_TARGET)
1552
list(APPEND compile_flags -target;${AST_TARGET})
1653
list(APPEND link_flags -target;${AST_TARGET})
@@ -26,6 +63,11 @@ function(add_swift_target target)
2663
if(AST_MODULE_CACHE_PATH)
2764
list(APPEND compile_flags -module-cache-path;${AST_MODULE_CACHE_PATH})
2865
endif()
66+
if(AST_MODULE_PATH)
67+
get_filename_component(module_location ${AST_MODULE_PATH} PATH)
68+
file(MAKE_DIRECTORY "${module_location}")
69+
list(APPEND compile_flags "-emit-module-path;${AST_MODULE_PATH}")
70+
endif()
2971
if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
3072
list(APPEND compile_flags -g)
3173
endif()
@@ -75,82 +117,45 @@ function(add_swift_target target)
75117
endif()
76118

77119
set(sources)
120+
set(rsp_text)
78121
foreach(source ${AST_SOURCES})
79122
get_filename_component(location ${source} PATH)
80123
if(IS_ABSOLUTE ${location})
81124
list(APPEND sources ${source})
125+
string(APPEND rsp_text "${source} ")
82126
else()
83127
list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${source})
128+
string(APPEND rsp_text "${CMAKE_CURRENT_SOURCE_DIR}/${source} ")
84129
endif()
85130
endforeach()
86131

87-
set(objs)
88-
set(mods)
89-
set(docs)
90-
set(i 0)
91-
foreach(source ${sources})
92-
get_filename_component(name ${source} NAME)
93-
94-
set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
95-
set(mod ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftmodule)
96-
set(doc ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdoc)
97-
98-
set(all_sources ${sources})
99-
list(INSERT all_sources ${i} -primary-file)
100-
101-
add_custom_command(OUTPUT
102-
${obj}
103-
${mod}
104-
${doc}
105-
DEPENDS
106-
${source}
107-
${AST_DEPENDS}
108-
COMMAND
109-
${CMAKE_SWIFT_COMPILER} -frontend ${compile_flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources})
110-
111-
list(APPEND objs ${obj})
112-
list(APPEND mods ${mod})
113-
list(APPEND docs ${doc})
132+
set(output_map_path ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/output-file-map.json)
133+
create_output_file_map(${target} "${sources}" ${output_map_path})
134+
list(APPEND compile_flags -output-file-map;${output_map_path})
114135

115-
math(EXPR i "${i}+1")
116-
endforeach()
136+
string(LENGTH sources source_length)
137+
set(rsp_file ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}.rsp)
138+
file(WRITE ${rsp_file} ${rsp_text})
117139

118140
if(AST_LIBRARY)
119-
get_filename_component(module_directory ${AST_MODULE_PATH} DIRECTORY)
120-
121-
set(module ${AST_MODULE_PATH})
122-
set(documentation ${module_directory}/${AST_MODULE_NAME}.swiftdoc)
123-
124-
add_custom_command(OUTPUT
125-
${module}
126-
${documentation}
127-
DEPENDS
128-
${mods}
129-
${docs}
130-
${AST_DEPENDS}
131-
COMMAND
132-
${CMAKE_SWIFT_COMPILER} -frontend ${compile_flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation})
133-
endif()
134-
135-
if(AST_LIBRARY)
136-
set(emit_library -emit-library)
141+
set(emit_library -emit-library)
137142
endif()
138143
if(NOT AST_LIBRARY OR library_kind STREQUAL SHARED)
139144
add_custom_command(OUTPUT
140145
${AST_OUTPUT}
141146
DEPENDS
142-
${objs}
147+
${sources}
143148
${AST_DEPENDS}
144149
COMMAND
145-
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
150+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${compile_flags} ${link_flags} -o ${AST_OUTPUT} @${rsp_file})
146151
add_custom_target(${target}
147152
ALL
148153
DEPENDS
149154
${AST_OUTPUT}
150155
${module}
151156
${documentation})
152157
else()
153-
add_library(${target}-static STATIC ${objs})
158+
add_library(${target}-static STATIC ${sources})
154159
if(AST_DEPENDS)
155160
add_dependencies(${target}-static ${AST_DEPENDS})
156161
endif()
@@ -177,6 +182,11 @@ function(add_swift_target target)
177182
endif()
178183

179184
if(AST_RESOURCES)
185+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
186+
set(resources_dir ${target}.resources)
187+
else()
188+
set(resources_dir Resources)
189+
endif()
180190
add_custom_command(TARGET
181191
${target}
182192
POST_BUILD
@@ -185,13 +195,22 @@ function(add_swift_target target)
185195
COMMAND
186196
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${target}
187197
COMMAND
188-
${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${target}/Resources)
198+
${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${target}/${resources_dir})
189199
foreach(resource ${AST_RESOURCES})
190-
add_custom_command(TARGET
191-
${target}
192-
POST_BUILD
193-
COMMAND
194-
${CMAKE_COMMAND} -E copy ${resource} ${CMAKE_CURRENT_BINARY_DIR}/${target}/Resources/)
200+
if(IS_DIRECTORY ${resource})
201+
get_filename_component(resource_basename ${resource} NAME)
202+
add_custom_command(TARGET
203+
${target}
204+
POST_BUILD
205+
COMMAND
206+
${CMAKE_COMMAND} -E copy_directory ${resource} ${CMAKE_CURRENT_BINARY_DIR}/${target}/${resources_dir}/${resource_basename})
207+
else()
208+
add_custom_command(TARGET
209+
${target}
210+
POST_BUILD
211+
COMMAND
212+
${CMAKE_COMMAND} -E copy ${resource} ${CMAKE_CURRENT_BINARY_DIR}/${target}/${resources_dir}/)
213+
endif()
195214
endforeach()
196215
else()
197216
add_custom_command(TARGET
@@ -219,7 +238,7 @@ function(add_swift_executable executable)
219238
add_swift_target(${executable} ${ARGN})
220239
endfunction()
221240

222-
# Returns the current achitecture name in a variable
241+
# Returns the current architecture name in a variable
223242
#
224243
# Usage:
225244
# get_swift_host_arch(result_var_name)
@@ -241,6 +260,8 @@ function(get_swift_host_arch result_var_name)
241260
set("${result_var_name}" "armv6" PARENT_SCOPE)
242261
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
243262
set("${result_var_name}" "armv7" PARENT_SCOPE)
263+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
264+
set("${result_var_name}" "armv7" PARENT_SCOPE)
244265
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
245266
set("${result_var_name}" "x86_64" PARENT_SCOPE)
246267
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")

0 commit comments

Comments
 (0)