def executePreprocessor(self): source = self.getCodeFileNameIfExists() # Copy the command parameters newParams = self.parameters.copy() outputFile = self.getOutputFileIfExists() if outputFile: self.preprocessedFile = outputFile + '.ii' for i in range(len(newParams)): p = self.parameters[i] if p == '-o' or p == '--output-file': newParams[i + 1] = self.preprocessedFile break else: self.preprocessedFile = source + '.ii' newParams.append('-o') newParams.append(self.preprocessedFile) new_cmd = [COMPILER_NAME, '-E'] + newParams try: if verbose(): prGreen(' '.join(new_cmd)) cmdOutput = subprocess.run(' '.join(new_cmd), shell=True, check=True) except Exception as e: message = 'Could not execute pre-processor' if verbose(): prRed(e) logMessage(str(e)) logMessage(message) raise RuntimeError(message) from e return True
def compileInstrumentedFile(self): source = self.getCodeFileNameIfExists() # Copy original command new_cmd = [COMPILER_NAME, '-include', FPCHECKER_RUNTIME ] + self.parameters # Replace file by instrumented file for i in range(len(new_cmd)): p = new_cmd[i] if p == source: new_cmd[i] = self.instrumentedFile break # Change output file if not self.outputFile: fileName, ext = os.path.splitext(source) newOutputFile = fileName + '.o' new_cmd = new_cmd + ['-o', newOutputFile] # Compile try: if verbose(): prGreen('Compiling: ' + ' '.join(new_cmd)) cmdOutput = subprocess.run(' '.join(new_cmd), shell=True, check=True) except Exception as e: if verbose(): prRed(e) logMessage(str(e)) message = 'Could not compile instrumented file' logMessage(message) raise CompileException(message) from e
def getCodeFileNameIfExists(self): global CPP_EXTENSION fileName = None for t in self.parameters: for ext in CPP_EXTENSION: if t.endswith(ext): fileName = t if not fileName: message = 'Could not find source file in command' logMessage(message) raise CommandException(message) return fileName
raise CompileException(message) from e if __name__ == '__main__': cmd = Command(sys.argv) if 'FPC_INSTRUMENT' not in os.environ: cmd.executeOriginalCommand() exit() # Link command if cmd.isLinkCommand(): cmd.executeOriginalCommand() else: # Compilation command try: cmd.executePreprocessor() cmd.instrumentSource() cmd.compileInstrumentedFile() logMessage('Instrumented: ' + cmd.instrumentedFile) except Exception as e: # Fall back to original command if verbose(): logMessage(str(e)) prRed(e) if not isinstance(e, EmptyFileException): logMessage('Failed: ' + ' '.join(sys.argv)) else: if verbose(): logMessage('Failed: ' + ' '.join(sys.argv)) cmd.executeOriginalCommand()