Files
chinook/chinook/compiler/_gnuc.py
T

213 lines
5.4 KiB
Python

from pathlib import Path
from ..models import Optimization
from ..models import Result
from ..shell import execute
from ._driver import CompilerDriver
from ._options import CompilerOptions
class GnucDriver(CompilerDriver):
__OPTLEVEL_MAPPING = {
Optimization.NONE: "-O0",
Optimization.LOW: "-O1",
Optimization.HIGH: "-O2",
Optimization.DEBUG: "-Og",
Optimization.SMALL: "-Os",
Optimization.SPEED: "-O3",
Optimization.TINY: "-Oz",
Optimization.FAST: "-OFast"
}
def compile_program(
self,
options: CompilerOptions,
dstpath: Path
) -> Result:
assert options is not None, "Compiler options are required"
assert len(options.objfiles) != 0, "Object files are required"
compiler = self.tooling.cc
cppext = [".cpp", ".cxx", ".cc"]
for obj in options.objfiles:
if obj.suffixes[0] in cppext:
compiler = self.tooling.cxx
break
args = [f"{compiler} -o {str(dstpath)}"]
args.extend(sorted(
[str(v) for v in options.objfiles],
key=lambda f: f.endswith(".so")
))
args.extend(options.flags)
if len(options.archives) > 0:
args.extend([f"-L{str(directory)}" for directory in options.libdirs])
args.extend([f"-l{str(archive)}" for archive in options.archives])
cmdstr = ' '.join(args)
# self.logger.info(cmdstr)
result = Result(
success = False,
action = "Linked",
target = str(dstpath),
command = cmdstr,
retcode = -1
)
def handle_success(retcode: int, cmdstr: str, stdout: str) -> None:
result.success = True
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stdout)
def handle_failure(retcode: int, cmdstr: str, stderr: str) -> None:
result.success = False
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stderr)
execute(cmdstr, handle_success, handle_failure)
return result
def compile_archive(
self,
options: CompilerOptions,
dstpath: Path
) -> Result:
args = [self.tooling.ar, f"rcs {str(dstpath)}"]
for obj in options.objfiles:
args.append(str(obj))
cmdstr = ' '.join(args)
result = Result(
success = False,
action = "Linked",
target = str(dstpath),
command = cmdstr,
retcode = -1
)
def handle_success(retcode: int, cmdstr: str, stdout: str) -> None:
result.success = True
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stdout)
def handle_failure(retcode: int, cmdstr: str, stderr: str) -> None:
result.success = False
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stderr)
execute(cmdstr, handle_success, handle_failure)
return result
def compile_library(
self,
options: CompilerOptions,
dstpath: Path
) -> Result:
assert options is not None, "Compiler options are required"
assert len(options.objfiles) != 0, "Object files are required"
compiler = self.tooling.cc
cppext = [".cpp", ".cxx", ".cc"]
for obj in options.objfiles:
if obj.suffixes[0] in cppext:
compiler = self.tooling.cxx
break
args = [f"{compiler} -shared -o {str(dstpath)}"]
args.extend(sorted(
[str(v) for v in options.objfiles],
key=lambda f: f.endswith(".so")
))
args.extend(options.flags)
if len(options.archives) > 0:
args.extend([f"-L{str(directory)}" for directory in options.libdirs])
args.extend([f"-l{str(archive)}" for archive in options.archives])
cmdstr = ' '.join(args)
# self.logger.info(cmdstr)
result = Result(
success = False,
action = "Linked",
target = str(dstpath),
command = cmdstr,
retcode = -1
)
def handle_success(retcode: int, cmdstr: str, stdout: str) -> None:
result.success = True
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stdout)
def handle_failure(retcode: int, cmdstr: str, stderr: str) -> None:
result.success = False
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stderr)
execute(cmdstr, handle_success, handle_failure)
return result
def compile_object(
self,
options: CompilerOptions,
srcpath: Path,
dstpath: Path
) -> Result:
assert options is not None, "Compiler options are required"
compiler = self.tooling.cc
standard = options.stdc
optlevel = self.__OPTLEVEL_MAPPING.get(options.optlevel)
cppext = [".cpp", ".cxx", ".cc"]
if srcpath.suffix in cppext:
compiler = self.tooling.cxx
standard = options.stdcpp
args = [
f"{compiler}",
f"-c {str(srcpath)}",
f"-o {str(dstpath)}"
]
if options.pic:
args.append("-fPIC")
args.append(f"-std={standard}")
args.append(optlevel)
args.extend(options.flags)
for directory in options.incdirs: args.append(f"-I{str(directory)}")
for directory in options.libdirs: args.append(f"-L{str(directory)}")
for archive in options.archives: args.append(f"-l{archive}")
for define in options.defines: args.append(f"-D{define}")
cmdstr = ' '.join(args)
# self.logger.info(cmdstr)
result = Result(
success = False,
action = "Compiled",
target = str(dstpath),
command = cmdstr,
retcode = -1
)
def handle_success(retcode: int, cmdstr: str, stdout: str) -> None:
result.success = True
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stdout)
def handle_failure(retcode: int, cmdstr: str, stderr: str) -> None:
result.success = False
result.command = cmdstr
result.retcode = retcode
result.diagnostics.append(stderr)
execute(cmdstr, handle_success, handle_failure)
return result