Capture error message when building C files

Add a new exception `CompileError` to differtiate that the exception raised
during the compilation.

Signed-off-by: Gabor Mezei <[email protected]>
This commit is contained in:
Gabor Mezei
2024-09-19 18:39:13 +02:00
parent ac4c133733
commit 20b9357d37
+16 -1
View File
@@ -11,6 +11,15 @@ import subprocess
import sys
import tempfile
class CompileError(Exception):
"""Excetion to represent an error during the compilation."""
def __init__(self, message):
"""Save the error massage"""
super().__init__()
self.message = message
def remove_file_if_exists(filename):
"""Remove the specified file, ignoring errors."""
if not filename:
@@ -107,7 +116,13 @@ def compile_c_file(c_filename, exe_filename, include_dirs):
else:
cmd += ['-o' + exe_filename]
subprocess.check_call(cmd + [c_filename])
try:
subprocess.check_output(cmd + [c_filename],
stderr=subprocess.PIPE,
universal_newlines=True)
except subprocess.CalledProcessError as e:
raise CompileError(e.stderr) from e
def get_c_expression_values(
cast_to, printf_format,