Skip to content

Commit 466ab59

Browse files
authored
Merge pull request #6464 from hughbe/line-directive-windows
Fix line-directive tool to work around Windows command line max limit
2 parents efef86d + 3b4187f commit 466ab59

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,21 @@ function(_compile_swift_files
451451

452452
# Then we can compile both the object files and the swiftmodule files
453453
# in parallel in this target for the object file, and ...
454+
455+
# Windows doesn't support long command line paths, of 8191 chars or over.
456+
# We need to work around this by avoiding long command line arguments. This can be
457+
# achieved by writing the list of file paths to a file, then reading that list
458+
# in the Python script.
459+
string(RANDOM file_name)
460+
set(file_path "${CMAKE_CURRENT_BINARY_DIR}/${file_name}.txt")
461+
file(WRITE "${file_path}" "${source_files}")
462+
454463
add_custom_command_target(
455464
dependency_target
456465
COMMAND
457-
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "${source_files}" --
466+
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
458467
"${swift_compiler_tool}" "${main_command}" ${swift_flags}
459-
${output_option} ${embed_bitcode_option} "${source_files}"
468+
${output_option} ${embed_bitcode_option} "@${file_path}"
460469
${command_touch_standard_outputs}
461470
OUTPUT ${standard_outputs}
462471
DEPENDS
@@ -485,9 +494,9 @@ function(_compile_swift_files
485494
add_custom_command_target(
486495
module_dependency_target
487496
COMMAND
488-
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "${source_files}" --
497+
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
489498
"${swift_compiler_tool}" "-emit-module" "-o" "${module_file}" ${swift_flags}
490-
"${source_files}"
499+
"@${file_path}"
491500
${command_touch_module_outputs}
492501
OUTPUT ${module_outputs}
493502
DEPENDS
@@ -502,9 +511,9 @@ function(_compile_swift_files
502511
add_custom_command_target(
503512
sib_dependency_target
504513
COMMAND
505-
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "${source_files}" --
514+
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
506515
"${swift_compiler_tool}" "-emit-sib" "-o" "${sib_file}" ${swift_flags} -Onone
507-
"${source_files}"
516+
"@${file_path}"
508517
${command_touch_sib_outputs}
509518
OUTPUT ${sib_outputs}
510519
DEPENDS
@@ -518,9 +527,9 @@ function(_compile_swift_files
518527
add_custom_command_target(
519528
sibopt_dependency_target
520529
COMMAND
521-
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "${source_files}" --
530+
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
522531
"${swift_compiler_tool}" "-emit-sib" "-o" "${sibopt_file}" ${swift_flags} -O
523-
"${source_files}"
532+
"@${file_path}"
524533
${command_touch_sibopt_outputs}
525534
OUTPUT ${sibopt_outputs}
526535
DEPENDS
@@ -535,9 +544,9 @@ function(_compile_swift_files
535544
add_custom_command_target(
536545
sibgen_dependency_target
537546
COMMAND
538-
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "${source_files}" --
547+
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
539548
"${swift_compiler_tool}" "-emit-sibgen" "-o" "${sibgen_file}" ${swift_flags}
540-
"${source_files}"
549+
"@${file_path}"
541550
${command_touch_sibgen_outputs}
542551
OUTPUT ${sibgen_outputs}
543552
DEPENDS

utils/line-directive

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ def map_line_from_source_file(source_filename, source_line_num, target_filename)
154154
return result + 1
155155
raise RuntimeError("line not found")
156156

157+
def read_response_file(file_path):
158+
with open(file_path, 'r') as files:
159+
return filter(None, files.read().split(';'))
160+
161+
def expand_response_files(files):
162+
expanded_files = []
163+
for file_path in files:
164+
# Read a list of files from a response file.
165+
if file_path[0] == '@':
166+
expanded_files.extend(read_response_file(file_path[1:]))
167+
else:
168+
expanded_files.append(file_path)
169+
170+
return expanded_files
171+
157172
def run():
158173
"""Simulate a couple of gyb-generated files
159174
@@ -243,7 +258,7 @@ def run():
243258
print(map_line_from_source_file(source_file, source_line, target_file))
244259
else:
245260
dashes = sys.argv.index('--')
246-
sources = sys.argv[1:dashes]
261+
sources = expand_response_files(sys.argv[1:dashes])
247262

248263
# The first argument of command_args is the process to open.
249264
# subprocess.Popen doesn't normalize arguments. This means that trying
@@ -252,7 +267,7 @@ def run():
252267
# to the Win32 CreateProcess API. Unix systems handle non-normalized
253268
# paths, so don't have this problem.
254269
# Arguments passed to the process are normalized by the process.
255-
command_args = sys.argv[dashes + 1:]
270+
command_args = expand_response_files(sys.argv[dashes + 1:])
256271
command_args[0] = os.path.normpath(command_args[0])
257272

258273
command = subprocess.Popen(

0 commit comments

Comments
 (0)