6
6
# Usage:
7
7
# handle_swift_sources(sourcesvar externalvar)
8
8
function (handle_swift_sources
9
- dependency_target_out_var_name sourcesvar externalvar name )
9
+ dependency_target_out_var_name
10
+ dependency_module_target_out_var_name
11
+ sourcesvar externalvar name )
10
12
cmake_parse_arguments (SWIFTSOURCES
11
13
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY"
12
14
"SDK;ARCHITECTURE;INSTALL_IN_COMPONENT"
@@ -38,6 +40,7 @@ function(handle_swift_sources
38
40
39
41
# Clear the result variable.
40
42
set ("${dependency_target_out_var_name} " "" PARENT_SCOPE )
43
+ set ("${dependency_module_target_out_var_name} " "" PARENT_SCOPE )
41
44
42
45
set (result )
43
46
set (swift_sources )
@@ -78,6 +81,7 @@ function(handle_swift_sources
78
81
79
82
_compile_swift_files (
80
83
dependency_target
84
+ module_dependency_target
81
85
OUTPUT ${swift_obj}
82
86
SOURCES ${swift_sources}
83
87
DEPENDS ${SWIFTSOURCES_DEPENDS}
@@ -93,6 +97,7 @@ function(handle_swift_sources
93
97
${STATIC_arg}
94
98
INSTALL_IN_COMPONENT "${SWIFTSOURCES_INSTALL_IN_COMPONENT} " )
95
99
set ("${dependency_target_out_var_name} " "${dependency_target} " PARENT_SCOPE )
100
+ set ("${dependency_module_target_out_var_name} " "${module_dependency_target} " PARENT_SCOPE )
96
101
list (APPEND result ${swift_obj} )
97
102
endif ()
98
103
@@ -134,7 +139,8 @@ endfunction()
134
139
# [IS_STDLIB] # Install produced files.
135
140
# [EMIT_SIB] # Emit the file as a sib file instead of a .o
136
141
# )
137
- function (_compile_swift_files dependency_target_out_var_name )
142
+ function (_compile_swift_files
143
+ dependency_target_out_var_name dependency_module_target_out_var_name )
138
144
cmake_parse_arguments (SWIFTFILE
139
145
"IS_MAIN;IS_STDLIB;IS_STDLIB_CORE;IS_SDK_OVERLAY;EMIT_SIB"
140
146
"OUTPUT;MODULE_NAME;INSTALL_IN_COMPONENT"
@@ -274,6 +280,7 @@ function(_compile_swift_files dependency_target_out_var_name)
274
280
set (module_file )
275
281
set (module_doc_file )
276
282
283
+ set (module_command )
277
284
if (NOT SWIFTFILE_IS_MAIN )
278
285
# Determine the directory where the module file should be placed.
279
286
if (SWIFTFILE_MODULE_DIR )
@@ -284,16 +291,15 @@ function(_compile_swift_files dependency_target_out_var_name)
284
291
message (FATAL_ERROR "Don't know where to put the module files" )
285
292
endif ()
286
293
294
+ list (APPEND swift_flags "-parse-as-library" )
287
295
if (NOT SWIFTFILE_EMIT_SIB )
288
296
# Right now sib files seem to not be output when we emit a module. So
289
297
# don't emit it.
290
298
set (module_file "${module_dir} /${SWIFTFILE_MODULE_NAME} .swiftmodule" )
291
299
set (module_doc_file "${module_dir} /${SWIFTFILE_MODULE_NAME} .swiftdoc" )
292
- list (APPEND swift_flags
293
- "-parse-as-library"
294
- "-emit-module" "-emit-module-path" "${module_file} " )
295
- else ()
296
- list (APPEND swift_flags "-parse-as-library" )
300
+ list (APPEND module_command
301
+ "-emit-module"
302
+ "-o" "${module_file} " )
297
303
endif ()
298
304
299
305
list (APPEND command_create_dirs
@@ -366,9 +372,9 @@ function(_compile_swift_files dependency_target_out_var_name)
366
372
set (swift_compiler_tool "${SWIFT_SOURCE_DIR} /utils/check-incremental" "${swift_compiler_tool} " )
367
373
endif ()
368
374
369
- set (outputs
370
- ${SWIFTFILE_OUTPUT} " ${module_file} " " ${module_doc_file} "
371
- ${apinote_files} )
375
+ set (standard_outputs ${SWIFTFILE_OUTPUT} )
376
+ set ( apinotes_outputs ${apinote_files} )
377
+ set ( module_outputs " ${module_file} " " ${module_doc_file} " )
372
378
373
379
if (XCODE )
374
380
# HACK: work around an issue with CMake Xcode generator and the Swift
@@ -383,29 +389,74 @@ function(_compile_swift_files dependency_target_out_var_name)
383
389
#
384
390
# To work around this issue we touch the output files so that their mtime
385
391
# always gets updated.
386
- set (command_touch_outputs
387
- COMMAND "${CMAKE_COMMAND} " -E touch ${outputs} )
392
+ set (command_touch_standard_outputs
393
+ COMMAND "${CMAKE_COMMAND} " -E touch ${standard_outputs} )
394
+ set (command_touch_apinotes_outputs
395
+ COMMAND "${CMAKE_COMMAND} " -E touch ${apinotes_outputs} )
396
+ set (command_touch_module_outputs
397
+ COMMAND "${CMAKE_COMMAND} " -E touch ${module_outputs} )
388
398
endif ()
389
399
400
+ # First generate the obj dirs
390
401
add_custom_command_target (
391
- dependency_target
402
+ obj_dirs_dependency_target
392
403
${command_create_dirs}
393
- # Create API notes before compiling, because this will affect the APIs
394
- # the overlay sees.
395
- ${command_create_apinotes}
404
+ COMMAND ""
405
+ OUTPUT ${obj_dirs}
406
+ COMMENT "Generating obj dirs for ${first_output} " )
407
+
408
+ # Generate the api notes if we need them.
409
+ if (apinotes_outputs )
410
+ add_custom_command_target (
411
+ api_notes_dependency_target
412
+ # Create API notes before compiling, because this will affect the APIs
413
+ # the overlay sees.
414
+ ${command_create_apinotes}
415
+ ${command_touch_apinotes_outputs}
416
+ COMMAND ""
417
+ OUTPUT ${apinotes_outputs}
418
+ DEPENDS
419
+ ${swift_compiler_tool_dep}
420
+ ${depends_create_apinotes}
421
+ ${obj_dirs_dependency_target}
422
+ COMMENT "Generating API notes ${first_output} " )
423
+ endif ()
424
+
425
+ # Then we can compile both the object files and the swiftmodule files
426
+ # in parallel in this target for the object file, and ...
427
+ add_custom_command_target (
428
+ dependency_target
396
429
COMMAND
397
430
"${line_directive_tool} " "${source_files} " --
398
431
"${swift_compiler_tool} " "${main_command} " ${swift_flags}
399
432
${output_option} "${source_files} "
400
- ${command_touch_outputs }
401
- OUTPUT ${outputs }
433
+ ${command_touch_standard_outputs }
434
+ OUTPUT ${standard_outputs }
402
435
DEPENDS
403
436
${swift_compiler_tool_dep}
404
437
${source_files} ${SWIFTFILE_DEPENDS}
405
- ${swift_ide_test_dependency} ${depends_create_apinotes}
438
+ ${swift_ide_test_dependency} ${api_notes_dependency_target}
439
+ ${obj_dirs_dependency_target}
406
440
COMMENT "Compiling ${first_output} " )
407
441
set ("${dependency_target_out_var_name} " "${dependency_target} " PARENT_SCOPE )
408
442
443
+ # This is the target to generate the .swiftmodule and .swiftdoc
444
+ add_custom_command_target (
445
+ module_dependency_target
446
+ COMMAND
447
+ "${line_directive_tool} " "${source_files} " --
448
+ "${swift_compiler_tool} " "${module_command} " ${swift_flags}
449
+ "${source_files} "
450
+ ${command_touch_module_outputs}
451
+ OUTPUT ${module_outputs}
452
+ DEPENDS
453
+ ${swift_compiler_tool_dep}
454
+ ${source_files} ${SWIFTFILE_DEPENDS}
455
+ ${swift_ide_test_dependency} ${api_notes_dependency_target}
456
+ ${obj_dirs_dependency_target}
457
+ COMMENT "Generating ${module_file} " )
458
+ set ("${dependency_module_target_out_var_name} " "${module_dependency_target} " PARENT_SCOPE )
459
+
409
460
# Make sure the build system knows the file is a generated object file.
410
461
set_source_files_properties (${SWIFTFILE_OUTPUT}
411
462
PROPERTIES
0 commit comments