Skip to content

Commit c8e62be

Browse files
authored
Replace os.system with subprocess.run (HPCSys-Lab#65)
* Replace `os.system` with `subprocess.run` Besides being recommended by the Python documentation of `os.system()` itself (https://docs.python.org/3/library/os.html#os.system), the previous implementation with `os.system()` has the issue of not handling paths with spaces, since the arguments like `program_path` and `object_path` were concatenated as strings and then incorrectly parsed by GCC in the presence of whitespace. With `subprocess.run()`, we provide a list of arguments and the paths are handled correctly. A small commented piece of code has been introduced to build up an arguments list correctly. * Fix handling of compiler flags with whitespace at the ends This is relative to the previous commit, and fixes the code in some instances where the `args` list ended up with empty arguments or arguments with trailing spaces. * Fix flake8 lints
1 parent 2c93f3a commit c8e62be

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

simwave/kernel/backend/compiler.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import subprocess
23
from hashlib import sha1
34

45

@@ -200,25 +201,29 @@ def compile(self, dimension, density, float_precision, operator):
200201
if os.path.exists(object_path):
201202
print("Shared object already compiled in:", object_path)
202203
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))
216220

217221
# create a dir to save the compiled shared object
218222
os.makedirs(object_dir, exist_ok=True)
219223

220224
# execute the command
221-
if os.system(cmd) != 0:
225+
result = subprocess.run(args)
226+
if result.returncode != 0:
222227
raise Exception("Compilation failed")
223228

224229
return object_path

0 commit comments

Comments
 (0)