diff --git a/chc/cmdline/ParseManager.py b/chc/cmdline/ParseManager.py index 8472c50..5a0fc71 100644 --- a/chc/cmdline/ParseManager.py +++ b/chc/cmdline/ParseManager.py @@ -33,6 +33,8 @@ import shlex import shutil +from pathlib import Path + import xml.etree.ElementTree as ET from typing import Any, Dict, List, Optional, Tuple @@ -77,6 +79,7 @@ def __init__( verbose: bool = True, keepUnused: bool = False, tgtplatform: str = "-m64", + preprocessed_path: str = "", ) -> None: """Initialize paths to code, results, and parser executable. @@ -109,6 +112,7 @@ def __init__( + ". Target platform is set to -m64" ) self._tgtplatform = "-m64" + self._preprocessed_path = preprocessed_path self.config = Config() @property @@ -261,22 +265,36 @@ def preprocess_file_with_cc( if mac: cmd = cmd[:1] + macoptions + cmd[1:] cmd = cmd[:1] + moreoptions + cmd[1:] + + real_ifile = None + if self._preprocessed_path: + path = Path(self._preprocessed_path) / ifilename + path = path.resolve() + if path.is_file(): + real_ifile = path + chklogger.logger.info("Changing directory to %s", self.projectpath) os.chdir(self.projectpath) - if self.verbose: - chklogger.logger.info("Preprocess file: " + str(cmd)) - p = subprocess.call( - cmd, cwd=self.projectpath, stderr=subprocess.STDOUT) - if p != 0: - chklogger.logger.warning("Result of preprocessing: " + str(p)) + if real_ifile: + try: + shutil.copy(real_ifile, ifilename) + except shutil.SameFileError: + pass else: - chklogger.logger.info("Preprocess file: " + str(cmd)) - subprocess.call( - cmd, - cwd=self.projectpath, - stdout=open(os.devnull, "w"), - stderr=subprocess.STDOUT, - ) + if self.verbose: + chklogger.logger.info("Preprocess file: " + str(cmd)) + p = subprocess.call( + cmd, cwd=self.projectpath, stderr=subprocess.STDOUT) + if p != 0: + chklogger.logger.warning("Result of preprocessing: " + str(p)) + else: + chklogger.logger.info("Preprocess file: " + str(cmd)) + subprocess.call( + cmd, + cwd=self.projectpath, + stdout=open(os.devnull, "w"), + stderr=subprocess.STDOUT, + ) if copyfiles: tgtcfilename = os.path.join(self.savedsourcepath, cfilename) tgtifilename = os.path.join(self.savedsourcepath, ifilename) @@ -367,37 +385,50 @@ def preprocess( ecommand.append("-fno-asm") self.set_platform(ecommand) - # issue modified command to produce i files - if self.verbose: - print("\nIssue command: " + str(ecommand) + "\n") - resultcode = subprocess.call( - ecommand, - cwd=ccommand["directory"], - stderr=subprocess.STDOUT - ) - print("result: " + str(resultcode)) + real_ifile = None + if self._preprocessed_path: + path = Path(self._preprocessed_path) / ifilename + path = path.resolve() + if path.is_file(): + real_ifile = path + + if real_ifile: + try: + shutil.copy(real_ifile, ifilename) + except shutil.SameFileError: + pass else: - subprocess.call( - ecommand, - cwd=ccommand["directory"], - stdout=open(os.devnull, "w"), - stderr=subprocess.STDOUT, - ) - - # issue original command - if self.verbose: - print("\nIssue original command: " + str(command) + "\n") - resultcode = subprocess.call( - command, cwd=ccommand["directory"], stderr=subprocess.STDOUT - ) - print("result: " + str(resultcode)) - else: - subprocess.call( - command, - cwd=ccommand["directory"], - stdout=open(os.devnull, "w"), - stderr=subprocess.STDOUT, - ) + # issue modified command to produce i files + if self.verbose: + print("\nIssue command: " + str(ecommand) + "\n") + resultcode = subprocess.call( + ecommand, + cwd=ccommand["directory"], + stderr=subprocess.STDOUT + ) + print("result: " + str(resultcode)) + else: + subprocess.call( + ecommand, + cwd=ccommand["directory"], + stdout=open(os.devnull, "w"), + stderr=subprocess.STDOUT, + ) + + # issue original command + if self.verbose: + print("\nIssue original command: " + str(command) + "\n") + resultcode = subprocess.call( + command, cwd=ccommand["directory"], stderr=subprocess.STDOUT + ) + print("result: " + str(resultcode)) + else: + subprocess.call( + command, + cwd=ccommand["directory"], + stdout=open(os.devnull, "w"), + stderr=subprocess.STDOUT, + ) if copyfiles: tgtcfilename = os.path.join( diff --git a/chc/cmdline/c_file/cfileutil.py b/chc/cmdline/c_file/cfileutil.py index 0075824..52231ba 100644 --- a/chc/cmdline/c_file/cfileutil.py +++ b/chc/cmdline/c_file/cfileutil.py @@ -124,6 +124,7 @@ def cfile_parse_file(args: argparse.Namespace) -> NoReturn: loglevel: str = args.loglevel logfilename: Optional[str] = args.logfilename logfilemode: str = args.logfilemode + preprocessed_path: str = args.preprocessed_path try: UF.check_parser() @@ -159,7 +160,8 @@ def cfile_parse_file(args: argparse.Namespace) -> NoReturn: chklogger.logger.info("Target path: %s", targetpath) parsemanager = ParseManager( - projectpath, projectname, targetpath, keep_system_includes=keep_system_includes) + projectpath, projectname, targetpath, keep_system_includes=keep_system_includes, + preprocessed_path=preprocessed_path) parsemanager.remove_semantics() parsemanager.initialize_paths() diff --git a/chc/cmdline/c_project/cprojectutil.py b/chc/cmdline/c_project/cprojectutil.py index b268c25..df08da2 100644 --- a/chc/cmdline/c_project/cprojectutil.py +++ b/chc/cmdline/c_project/cprojectutil.py @@ -121,6 +121,7 @@ def cproject_parse_project(args: argparse.Namespace) -> NoReturn: loglevel: str = args.loglevel logfilename: Optional[str] = args.logfilename logfilemode: str = args.logfilemode + preprocessed_path: str = args.preprocessed_path try: UF.check_parser() @@ -168,7 +169,8 @@ def cproject_parse_project(args: argparse.Namespace) -> NoReturn: projectpath, projectname, targetpath, - keep_system_includes=keep_system_includes) + keep_system_includes=keep_system_includes, + preprocessed_path=preprocessed_path) parsemanager.remove_semantics() parsemanager.initialize_paths() exitcode = parsemanager.parse_with_ccommands(compilecommands, copyfiles=True) diff --git a/chc/cmdline/chkc b/chc/cmdline/chkc index 5848e0c..7024a5c 100755 --- a/chc/cmdline/chkc +++ b/chc/cmdline/chkc @@ -663,6 +663,9 @@ def parse() -> argparse.Namespace: "--keep-system-includes", action="store_true", help="do not filter out functions from files with absolute file names") + cfileparse.add_argument( + "--preprocessed-path", + help="directory that contains preprocessed source files") cfileparse.add_argument( "--loglevel", "-log", choices=UL.LogLevel.options(), @@ -1491,6 +1494,9 @@ def parse() -> argparse.Namespace: "--keep-system-includes", action="store_true", help="don't filter out functions from files with absolute filenames") + cprojectparse.add_argument( + "--preprocessed-path", + help="directory that contains preprocessed source files") cprojectparse.add_argument( "--loglevel", "-log", choices=UL.LogLevel.options(),