|
1 | 1 | import os
|
| 2 | +import subprocess |
2 | 3 | from hashlib import sha1
|
3 | 4 |
|
4 | 5 |
|
@@ -200,25 +201,29 @@ def compile(self, dimension, density, float_precision, operator):
|
200 | 201 | if os.path.exists(object_path):
|
201 | 202 | print("Shared object already compiled in:", object_path)
|
202 | 203 | else:
|
203 |
| - cmd = ( |
204 |
| - self.cc |
205 |
| - + " " |
206 |
| - + program_path |
207 |
| - + " " |
208 |
| - + self.cflags |
209 |
| - + " {}".format(float_precision) |
210 |
| - + language_c |
211 |
| - + " -o " |
212 |
| - + object_path |
213 |
| - ) |
214 |
| - |
215 |
| - print("Compilation command:", cmd) |
| 204 | + # create arguments list for `subprocess.run`: pay attention to not |
| 205 | + # providing empty arguments, which the compiler will try to |
| 206 | + # interpret as source filenames and consequenty fail; moreover |
| 207 | + # split the compilation flags string to separate arguments to |
| 208 | + # ensure proper parsing |
| 209 | + args = [self.cc, program_path] |
| 210 | + args += [flag.strip() |
| 211 | + for flag in self.cflags.split(' ') |
| 212 | + if flag.strip() != ''] |
| 213 | + if float_precision.strip() != '': |
| 214 | + args.append("{}".format(float_precision).strip()) |
| 215 | + if language_c.strip() != '': |
| 216 | + args.append(language_c.strip()) |
| 217 | + args += ["-o", object_path] |
| 218 | + |
| 219 | + print("Compilation command:", ' '.join(args)) |
216 | 220 |
|
217 | 221 | # create a dir to save the compiled shared object
|
218 | 222 | os.makedirs(object_dir, exist_ok=True)
|
219 | 223 |
|
220 | 224 | # execute the command
|
221 |
| - if os.system(cmd) != 0: |
| 225 | + result = subprocess.run(args) |
| 226 | + if result.returncode != 0: |
222 | 227 | raise Exception("Compilation failed")
|
223 | 228 |
|
224 | 229 | return object_path
|
0 commit comments