@@ -110,6 +110,27 @@ def get_lldb_python_interpreter(lldb_build_root):
110
110
return None
111
111
return python_path
112
112
113
+ if not platform .system () == 'Windows' :
114
+ # Python 3.3 has shlex.quote, while previous Python have pipes.quote
115
+ if sys .version_info [0 :2 ] >= (3 , 2 ):
116
+ shell_quote = shlex .quote
117
+ else :
118
+ shell_quote = pipes .quote
119
+ else :
120
+ # In Windows neither pipe.quote nor shlex.quote works.
121
+ def shell_quote (s ):
122
+ # Quote the argument if it is empty, or contains a quote or a space.
123
+ if len (s ) == 0 or re .search (r'["\s]' , s ):
124
+ s = '"' + s .replace ('"' , r'\"' ) + '"'
125
+ return s
126
+
127
+ def escape_for_substitute_captures (s ):
128
+ # SubstituteCaptures strings are used as replacement patterns for regular
129
+ # expressions. In them escapes like \1, \2 are used as references, but that
130
+ # means that simple \ will try to be interpreted, so we need to escape them
131
+ # with \\.
132
+ return s .replace ("\\ " , "\\ \\ " )
133
+
113
134
###
114
135
115
136
# Check that the object root is known.
@@ -380,7 +401,7 @@ config.sil_test_options = os.environ.get('SIL_TEST_OPTIONS', '')
380
401
381
402
config .clang_module_cache_path = make_path (config .swift_test_results_dir , "clang-module-cache" )
382
403
shutil .rmtree (config .clang_module_cache_path , ignore_errors = True )
383
- mcp_opt = "-module-cache-path %r " % config .clang_module_cache_path
404
+ mcp_opt = "-module-cache-path %s " % shell_quote ( config .clang_module_cache_path )
384
405
clang_mcp_opt = "-fmodules-cache-path=%r" % config .clang_module_cache_path
385
406
lit_config .note ("Using Clang module cache: " + config .clang_module_cache_path )
386
407
lit_config .note ("Using test results dir: " + config .swift_test_results_dir )
@@ -396,7 +417,7 @@ config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) )
396
417
config .substitutions .append ( ('%llvm_src_root' , config .llvm_src_root ) )
397
418
config .substitutions .append ( ('%swift_obj_root' , config .swift_obj_root ) )
398
419
config .substitutions .append ( ('%swift_src_root' , config .swift_src_root ) )
399
- config .substitutions .append ( ('%{python}' , pipes . quote (sys .executable )) )
420
+ config .substitutions .append ( ('%{python}' , shell_quote (sys .executable )) )
400
421
config .substitutions .append ( ('%{python.unquoted}' , sys .executable ) )
401
422
config .substitutions .append ( ('%mcp_opt' , mcp_opt ) )
402
423
config .substitutions .append ( ('%swift_driver_plain' , "%r" % config .swift ) )
@@ -1096,7 +1117,8 @@ elif run_os in ['windows-msvc']:
1096
1117
subst_target_swift_frontend_mock_sdk_after = ''
1097
1118
1098
1119
config .target_build_swift_dylib = \
1099
- SubstituteCaptures ("%s -parse-as-library -emit-library -o \\ 1" % (config .target_build_swift ))
1120
+ SubstituteCaptures (r"%s -parse-as-library -emit-library -o \1" % (
1121
+ escape_for_substitute_captures (config .target_build_swift )))
1100
1122
config .target_add_rpath = r''
1101
1123
1102
1124
config .target_clang = \
@@ -1280,7 +1302,7 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
1280
1302
toolchain_directory = make_path (
1281
1303
config .android_ndk_path , "toolchains" , toolchain_directory_name ,
1282
1304
"prebuilt" , prebuilt_directory )
1283
- tools_directory = pipes . quote (make_path (
1305
+ tools_directory = shell_quote (make_path (
1284
1306
toolchain_directory , ndk_platform_triple , "bin" ))
1285
1307
lit_config .note ("Testing Android " + config .variant_triple )
1286
1308
config .target_object_format = "elf"
@@ -1290,20 +1312,20 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
1290
1312
config .target_swift_autolink_extract = inferSwiftBinary ("swift-autolink-extract" )
1291
1313
config .target_sdk_name = "android"
1292
1314
android_link_paths_opt = "-L {} -L {} -L {}" .format (
1293
- pipes . quote (make_path (
1315
+ shell_quote (make_path (
1294
1316
config .android_ndk_path , "sources" , "cxx-stl" , "llvm-libc++" ,
1295
1317
"libs" , ndk_platform_tuple )),
1296
- pipes . quote (make_path (
1318
+ shell_quote (make_path (
1297
1319
toolchain_directory , "lib" , "gcc" , ndk_platform_triple ,
1298
1320
"{}.x" .format (config .android_ndk_gcc_version ))),
1299
- pipes . quote (make_path (
1321
+ shell_quote (make_path (
1300
1322
toolchain_directory , ndk_platform_triple , "lib" )))
1301
1323
# Since NDK r14 the headers are unified under $NDK_PATH/sysroot, so the -sdk
1302
1324
# switch is not enough. Additionally we have to include both the unified
1303
1325
# sysroot, and the architecture sysroot.
1304
- unified_android_include_path = pipes . quote (make_path (
1326
+ unified_android_include_path = shell_quote (make_path (
1305
1327
config .android_ndk_path , "sysroot" , "usr" , "include" ))
1306
- architecture_android_include_path = pipes . quote (make_path (
1328
+ architecture_android_include_path = shell_quote (make_path (
1307
1329
config .android_ndk_path , "sysroot" , "usr" , "include" ,
1308
1330
ndk_platform_triple ))
1309
1331
android_include_paths_opt = "-I {} -I {}" .format (
@@ -1749,21 +1771,27 @@ if not kIsWindows:
1749
1771
1750
1772
if not getattr (config , 'target_run_simple_swift' , None ):
1751
1773
config .target_run_simple_swift_parameterized = SubstituteCaptures (
1752
- "%%empty-directory(%%t) && "
1753
- "%s %s %%s \\ 1 -o %%t/a.out -module-name main && "
1754
- "%s %%t/a.out && "
1755
- "%s %%t/a.out"
1756
- % (config .target_build_swift , mcp_opt , config .target_codesign , config .target_run )
1774
+ r"%%empty-directory(%%t) && "
1775
+ r"%s %s %%s \1 -o %%t/a.out -module-name main && "
1776
+ r"%s %%t/a.out && "
1777
+ r"%s %%t/a.out"
1778
+ % (escape_for_substitute_captures (config .target_build_swift ),
1779
+ escape_for_substitute_captures (mcp_opt ),
1780
+ escape_for_substitute_captures (config .target_codesign ),
1781
+ escape_for_substitute_captures (config .target_run ))
1757
1782
)
1758
1783
config .target_run_simple_swiftgyb_parameterized = SubstituteCaptures (
1759
- "%%empty-directory(%%t) && "
1760
- "%%gyb %%s -o %%t/main.swift && "
1761
- "%%line-directive %%t/main.swift -- "
1762
- "%s %s %%t/main.swift \\ 1 -o %%t/a.out -module-name main && "
1763
- "%s %%t/a.out && "
1764
- "%%line-directive %%t/main.swift -- "
1765
- "%s %%t/a.out"
1766
- % (config .target_build_swift , mcp_opt , config .target_codesign , config .target_run )
1784
+ r"%%empty-directory(%%t) && "
1785
+ r"%%gyb %%s -o %%t/main.swift && "
1786
+ r"%%line-directive %%t/main.swift -- "
1787
+ r"%s %s %%t/main.swift \1 -o %%t/a.out -module-name main && "
1788
+ r"%s %%t/a.out && "
1789
+ r"%%line-directive %%t/main.swift -- "
1790
+ r"%s %%t/a.out"
1791
+ % (escape_for_substitute_captures (config .target_build_swift ),
1792
+ escape_for_substitute_captures (mcp_opt ),
1793
+ escape_for_substitute_captures (config .target_codesign ),
1794
+ escape_for_substitute_captures (config .target_run ))
1767
1795
)
1768
1796
1769
1797
config .target_run_simple_swift = (
@@ -1804,7 +1832,7 @@ config.target_resilience_test = (
1804
1832
'%s %s --target-build-swift "%s" --target-run "%s" --t %%t --S %%S '
1805
1833
'--s %%s --lib-prefix "%s" --lib-suffix "%s" --target-codesign "%s" '
1806
1834
'--additional-compile-flags "%s" --triple "%s"'
1807
- % (pipes . quote (sys .executable ), config .rth , config .target_build_swift ,
1835
+ % (shell_quote (sys .executable ), config .rth , config .target_build_swift ,
1808
1836
config .target_run , config .target_shared_library_prefix ,
1809
1837
config .target_shared_library_suffix , config .target_codesign ,
1810
1838
rth_flags , config .variant_triple ))
@@ -1833,8 +1861,9 @@ config.substitutions.append(('%target-swift-emit-ir\(mock-sdk:([^)]+)\)',
1833
1861
SubstituteCaptures (r'%target-swift-frontend(mock-sdk:\1) -emit-ir -verify-syntax-tree' )))
1834
1862
config .substitutions .append (('%target-swift-emit-ir' , '%target-swift-frontend -emit-ir -verify-syntax-tree' ))
1835
1863
config .substitutions .append (('%target-swift-frontend\(mock-sdk:([^)]+)\)' ,
1836
- SubstituteCaptures (r'%s \1 %s' % (subst_target_swift_frontend_mock_sdk ,
1837
- subst_target_swift_frontend_mock_sdk_after ))))
1864
+ SubstituteCaptures (r'%s \1 %s' % (
1865
+ escape_for_substitute_captures (subst_target_swift_frontend_mock_sdk ),
1866
+ escape_for_substitute_captures (subst_target_swift_frontend_mock_sdk_after )))))
1838
1867
config .substitutions .append (('%target-swift-frontend' , config .target_swift_frontend ))
1839
1868
1840
1869
@@ -1872,22 +1901,23 @@ else:
1872
1901
SubstituteCaptures (r'ln \1 \2 || cp \1 \2' )))
1873
1902
1874
1903
config .substitutions .append (('%utils' , config .swift_utils ))
1875
- config .substitutions .append (('%line-directive' , '%s %s' % (pipes . quote (sys .executable ), config .line_directive )))
1876
- config .substitutions .append (('%gyb' , '%s %s' % (pipes . quote (sys .executable ), config .gyb )))
1904
+ config .substitutions .append (('%line-directive' , '%s %s' % (shell_quote (sys .executable ), config .line_directive )))
1905
+ config .substitutions .append (('%gyb' , '%s %s' % (shell_quote (sys .executable ), config .gyb )))
1877
1906
config .substitutions .append (('%round-trip-syntax-test' ,
1878
- '%s %s' % (pipes . quote (sys .executable ),
1907
+ '%s %s' % (shell_quote (sys .executable ),
1879
1908
config .round_trip_syntax_test )))
1880
- config .substitutions .append (('%rth' , '%s %s' % (pipes . quote (sys .executable ), config .rth )))
1909
+ config .substitutions .append (('%rth' , '%s %s' % (shell_quote (sys .executable ), config .rth )))
1881
1910
config .substitutions .append (('%scale-test' ,
1882
1911
'{} {} --swiftc-binary={} --tmpdir=%t' .format (
1883
- pipes . quote (sys .executable ), config .scale_test ,
1912
+ shell_quote (sys .executable ), config .scale_test ,
1884
1913
config .swiftc )))
1885
1914
config .substitutions .append (('%empty-directory\(([^)]+)\)' ,
1886
1915
SubstituteCaptures (r'rm -rf "\1" && mkdir -p "\1"' )))
1887
1916
1888
1917
config .substitutions .append (('%target-sil-opt\(mock-sdk:([^)]+)\)' ,
1889
- SubstituteCaptures (r'%s \1 %s' % (subst_target_sil_opt_mock_sdk ,
1890
- subst_target_sil_opt_mock_sdk_after ))))
1918
+ SubstituteCaptures (r'%s \1 %s' % (
1919
+ escape_for_substitute_captures (subst_target_sil_opt_mock_sdk ),
1920
+ escape_for_substitute_captures (subst_target_sil_opt_mock_sdk_after )))))
1891
1921
# NOTE: This needs to be appended after the mock-sdk expansion to ensure that we
1892
1922
# first expand the mock-sdk when lit is processing.
1893
1923
config .substitutions .append (('%target-sil-opt' , config .target_sil_opt ))
@@ -1897,9 +1927,10 @@ config.substitutions.append(('%target-sil-llvm-gen', config.target_sil_llvm_gen)
1897
1927
config .substitutions .append (('%target-sil-nm' , config .target_sil_nm ))
1898
1928
1899
1929
config .substitutions .append (('%target-swift-ide-test\(mock-sdk:([^)]+)\)' ,
1900
- SubstituteCaptures (r'%s \1 %s -swift-version %s' % (subst_target_swift_ide_test_mock_sdk ,
1901
- subst_target_swift_ide_test_mock_sdk_after ,
1902
- swift_version ))))
1930
+ SubstituteCaptures (r'%s \1 %s -swift-version %s' % (
1931
+ escape_for_substitute_captures (subst_target_swift_ide_test_mock_sdk ),
1932
+ escape_for_substitute_captures (subst_target_swift_ide_test_mock_sdk_after ),
1933
+ escape_for_substitute_captures (swift_version )))))
1903
1934
config .substitutions .append (('%target-swift-ide-test' , "%s -swift-version %s" % (config .target_swift_ide_test , swift_version )))
1904
1935
1905
1936
config .substitutions .append (('%target-swift-symbolgraph-extract' , config .target_swift_symbolgraph_extract ))
@@ -1944,8 +1975,9 @@ config.substitutions.append(('%target-object-format', config.target_object_forma
1944
1975
config .substitutions .append (('%{target-shared-library-prefix}' , config .target_shared_library_prefix ))
1945
1976
config .substitutions .append (('%{target-shared-library-suffix}' , config .target_shared_library_suffix ))
1946
1977
config .substitutions .insert (0 , ('%target-library-name\(([^)]+)\)' ,
1947
- SubstituteCaptures (r'%s\1%s' % (config .target_shared_library_prefix ,
1948
- config .target_shared_library_suffix ))))
1978
+ SubstituteCaptures (r'%s\1%s' % (
1979
+ escape_for_substitute_captures (config .target_shared_library_prefix ),
1980
+ escape_for_substitute_captures (config .target_shared_library_suffix )))))
1949
1981
config .substitutions .append (('%target-rpath\(([^)]+)\)' ,
1950
1982
SubstituteCaptures (config .target_add_rpath )))
1951
1983
@@ -1958,14 +1990,20 @@ if hasattr(config, 'otool_classic'):
1958
1990
config .substitutions .append (('%otool-classic' , config .otool_classic ))
1959
1991
1960
1992
config .substitutions .append (('%FileCheck' ,
1961
- '%s %r --sanitize BUILD_DIR=%r --sanitize SOURCE_DIR=%r --use-filecheck %r %s' % (
1962
- pipes .quote (sys .executable ),
1963
- config .PathSanitizingFileCheck ,
1964
- swift_obj_root ,
1965
- config .swift_src_root ,
1966
- config .filecheck ,
1993
+ '%s %s --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --use-filecheck %s %s' % (
1994
+ shell_quote (sys .executable ),
1995
+ shell_quote (config .PathSanitizingFileCheck ),
1996
+ # LLVM Lit performs realpath with the config path, so all paths are relative
1997
+ # to the real path. swift_obj_root and swift_src_root come from CMake, which
1998
+ # might not do real path. Because we have to match what Lit uses against what
1999
+ # we provide we use realpath here. Because PathSanitizingFileCheck only
2000
+ # understands sanitize patterns with forward slashes, and realpath normalizes
2001
+ # the slashes, we have to replace them back to forward slashes.
2002
+ shell_quote (os .path .realpath (swift_obj_root ).replace ("\\ " , "/" )),
2003
+ shell_quote (os .path .realpath (config .swift_src_root ).replace ("\\ " , "/" )),
2004
+ shell_quote (config .filecheck ),
1967
2005
'--enable-windows-compatibility' if kIsWindows else '' )))
1968
- config .substitutions .append (('%raw-FileCheck' , pipes . quote (config .filecheck )))
2006
+ config .substitutions .append (('%raw-FileCheck' , shell_quote (config .filecheck )))
1969
2007
config .substitutions .append (('%import-libdispatch' , getattr (config , 'import_libdispatch' , '' )))
1970
2008
1971
2009
if config .lldb_build_root != "" :
0 commit comments