Skip to content

Commit 542dc42

Browse files
authored
Fix Ninja support (#24584)
Fixes #24393 by using subprocess.list2cmdline instead of shlex.join. Unfortunately, subprocess.list2cmdline is undocumented but apparently has been available for well over a decade (eg see https://stackoverflow.com/questions/12130163/how-to-get-resulting-subprocess-command-string) and, judging by discussions like https://discuss.python.org/t/why-is-subprocess-list2cmdline-not-documented/25044 is unlikely to be ever removed since it's been long used in the wild. It does exactly what we want - quote & join a command by Windows rules.
1 parent 8cd28a2 commit 542dc42

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

tools/system_libs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import shutil
1414
import textwrap
1515
import shlex
16+
import subprocess
1617
from enum import IntEnum, auto
1718
from glob import iglob
1819
from typing import List, Optional
@@ -183,13 +184,15 @@ def create_ninja_file(input_files, filename, libname, cflags, asflags=None, cust
183184
if asflags is None:
184185
asflags = []
185186

187+
join_cmd = shlex.join if os.name == 'posix' else subprocess.list2cmdline
188+
186189
out = f'''\
187190
# Automatically generated by tools/system_libs.py. DO NOT EDIT
188191
189192
ninja_required_version = 1.5
190193
191-
ASFLAGS = {shlex.join(asflags)}
192-
CFLAGS = {shlex.join(cflags)}
194+
ASFLAGS = {join_cmd(asflags)}
195+
CFLAGS = {join_cmd(cflags)}
193196
EMCC = {shared.EMCC}
194197
EMXX = {shared.EMXX}
195198
EMAR = {shared.EMAR}
@@ -267,7 +270,7 @@ def create_ninja_file(input_files, filename, libname, cflags, asflags=None, cust
267270
if customize_build_flags:
268271
custom_flags = customize_build_flags(flags, src)
269272
if custom_flags != flags:
270-
out += f' CFLAGS = {shlex.join(custom_flags)}'
273+
out += f' CFLAGS = {join_cmd(custom_flags)}'
271274
out += '\n'
272275

273276
objects = sorted(objects, key=objectfile_sort_key)

0 commit comments

Comments
 (0)