1
-
2
1
import glob
3
2
import os
3
+ import shutil
4
4
import subprocess
5
5
import sys
6
6
7
- def cleanup (out ):
8
- ret = ''
9
- for s in out .decode ('utf-8' ).split ('\n ' ):
10
- if len (s ) > 1 and s [0 ] == '#' :
7
+
8
+ def cleanup (out : str ) -> str :
9
+ parts = []
10
+ for line in out .splitlines ():
11
+ if len (line ) > 1 and line [0 ] == '#' :
11
12
continue
12
- s = "" .join (s .split ())
13
- ret = ret + s
14
- return ret
13
+ parts .append ("" .join (line .split ()))
14
+ return "" .join (parts )
15
+
16
+
17
+ # Check for required compilers and exit if any are missing
18
+ CLANG_EXE = shutil .which ('clang' )
19
+ if not CLANG_EXE :
20
+ sys .exit ('Failed to run tests: clang compiler not found' )
21
+
22
+ GCC_EXE = shutil .which ('gcc' )
23
+ if not GCC_EXE :
24
+ sys .exit ('Failed to run tests: gcc compiler not found' )
25
+
26
+ SIMPLECPP_EXE = './simplecpp'
27
+
15
28
16
29
commands = []
17
30
@@ -78,6 +91,21 @@ def cleanup(out):
78
91
'pr57580.c' ,
79
92
]
80
93
94
+
95
+ def run (compiler_executable : str , compiler_args : list [str ]) -> tuple [int , str , str ]:
96
+ """Execute a compiler command and capture its exit code, stdout, and stderr."""
97
+ compiler_cmd = [compiler_executable ]
98
+ compiler_cmd .extend (compiler_args )
99
+
100
+ with subprocess .Popen (compiler_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , encoding = "utf-8" ) as process :
101
+ stdout , stderr = process .communicate ()
102
+ exit_code = process .returncode
103
+
104
+ output = cleanup (stdout )
105
+ error = (stderr or "" ).strip ()
106
+ return (exit_code , output , error )
107
+
108
+
81
109
numberOfSkipped = 0
82
110
numberOfFailed = 0
83
111
numberOfFixed = 0
@@ -89,26 +117,12 @@ def cleanup(out):
89
117
numberOfSkipped = numberOfSkipped + 1
90
118
continue
91
119
92
- clang_cmd = ['clang' ]
93
- clang_cmd .extend (cmd .split (' ' ))
94
- p = subprocess .Popen (clang_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
95
- comm = p .communicate ()
96
- clang_output = cleanup (comm [0 ])
120
+ _ , clang_output , _ = run (CLANG_EXE , cmd .split (' ' ))
97
121
98
- gcc_cmd = ['gcc' ]
99
- gcc_cmd .extend (cmd .split (' ' ))
100
- p = subprocess .Popen (gcc_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
101
- comm = p .communicate ()
102
- gcc_output = cleanup (comm [0 ])
122
+ _ , gcc_output , _ = run (GCC_EXE , cmd .split (' ' ))
103
123
104
- simplecpp_cmd = ['./simplecpp' ]
105
124
# -E is not supported and we bail out on unknown options
106
- simplecpp_cmd .extend (cmd .replace ('-E ' , '' , 1 ).split (' ' ))
107
- p = subprocess .Popen (simplecpp_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
108
- comm = p .communicate ()
109
- simplecpp_ec = p .returncode
110
- simplecpp_output = cleanup (comm [0 ])
111
- simplecpp_err = comm [0 ].decode ('utf-8' ).strip ()
125
+ simplecpp_ec , simplecpp_output , simplecpp_err = run (SIMPLECPP_EXE , cmd .replace ('-E ' , '' , 1 ).split (' ' ))
112
126
113
127
if simplecpp_output != clang_output and simplecpp_output != gcc_output :
114
128
filename = cmd [cmd .rfind ('/' )+ 1 :]
0 commit comments