Skip to content

Commit 953257c

Browse files
authored
Merge pull request #1959 from jmittert/ResponsibleCompilation
Use Response Files in CMake
2 parents d1ae699 + 5bbcfea commit 953257c

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

Diff for: cmake/modules/SwiftSupport.cmake

+50-50
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11

22
include(CMakeParseArguments)
33

4+
# Creates an output file map give a target and its list of sources at
5+
# output_path
6+
#
7+
# Usage:
8+
# create_output_file_map(target sources output_path)
9+
function(create_output_file_map target sources output_path)
10+
set(output_list)
11+
set(output_file_map "{\n")
12+
foreach(source ${sources})
13+
get_filename_component(name ${source} NAME)
14+
15+
set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
16+
set(deps ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.d)
17+
set(swiftdeps ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdeps)
18+
set(dia ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.dia)
19+
set(output_entry "\"${source}\": {\n\
20+
\"object\": \"${obj}\",\n\
21+
\"dependencies\": \"${deps}\",\n\
22+
\"swift-dependencies\": \"${swiftdeps}\",\n\
23+
\"diagnostics\": \"${dia}\"\n\
24+
},\n")
25+
string(APPEND output_file_map ${output_entry})
26+
endforeach()
27+
set(master_deps ${CMAKE_CURRENT_BINARY_DIR}/${target}.swiftdeps)
28+
string(APPEND output_file_map "\"\": {\n\
29+
\"swift-dependencies\": \"${master_deps}\"\n\
30+
}\n")
31+
string(APPEND output_file_map "}\n")
32+
file(WRITE ${output_path} ${output_file_map})
33+
endfunction()
34+
435
function(add_swift_target target)
536
set(options LIBRARY;SHARED;STATIC)
637
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
@@ -11,6 +42,7 @@ function(add_swift_target target)
1142
set(compile_flags ${CMAKE_SWIFT_FLAGS})
1243
set(link_flags)
1344

45+
list(APPEND compile_flags -incremental)
1446
if(AST_TARGET)
1547
list(APPEND compile_flags -target;${AST_TARGET})
1648
list(APPEND link_flags -target;${AST_TARGET})
@@ -26,6 +58,11 @@ function(add_swift_target target)
2658
if(AST_MODULE_CACHE_PATH)
2759
list(APPEND compile_flags -module-cache-path;${AST_MODULE_CACHE_PATH})
2860
endif()
61+
if(AST_MODULE_PATH)
62+
get_filename_component(module_location ${AST_MODULE_PATH} PATH)
63+
file(MAKE_DIRECTORY "${module_location}")
64+
list(APPEND compile_flags "-emit-module-path;${AST_MODULE_PATH}")
65+
endif()
2966
if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
3067
list(APPEND compile_flags -g)
3168
endif()
@@ -75,82 +112,45 @@ function(add_swift_target target)
75112
endif()
76113

77114
set(sources)
115+
set(rsp_text)
78116
foreach(source ${AST_SOURCES})
79117
get_filename_component(location ${source} PATH)
80118
if(IS_ABSOLUTE ${location})
81119
list(APPEND sources ${source})
120+
string(APPEND rsp_text "${source} ")
82121
else()
83122
list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${source})
123+
string(APPEND rsp_text "${CMAKE_CURRENT_SOURCE_DIR}/${source} ")
84124
endif()
85125
endforeach()
86126

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})
114-
115-
math(EXPR i "${i}+1")
116-
endforeach()
117-
118-
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)
127+
set(output_map_path ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/output-file-map.json)
128+
create_output_file_map(${target} "${sources}" ${output_map_path})
129+
list(APPEND compile_flags -output-file-map;${output_map_path})
123130

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()
131+
string(LENGTH sources source_length)
132+
set(rsp_file ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}.rsp)
133+
file(WRITE ${rsp_file} ${rsp_text})
134134

135135
if(AST_LIBRARY)
136-
set(emit_library -emit-library)
136+
set(emit_library -emit-library)
137137
endif()
138138
if(NOT AST_LIBRARY OR library_kind STREQUAL SHARED)
139139
add_custom_command(OUTPUT
140140
${AST_OUTPUT}
141141
DEPENDS
142-
${objs}
142+
${sources}
143143
${AST_DEPENDS}
144144
COMMAND
145-
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
145+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${compile_flags} ${link_flags} -o ${AST_OUTPUT} @${rsp_file})
146146
add_custom_target(${target}
147147
ALL
148148
DEPENDS
149149
${AST_OUTPUT}
150150
${module}
151151
${documentation})
152152
else()
153-
add_library(${target}-static STATIC ${objs})
153+
add_library(${target}-static STATIC ${sources})
154154
if(AST_DEPENDS)
155155
add_dependencies(${target}-static ${AST_DEPENDS})
156156
endif()

0 commit comments

Comments
 (0)