Moved compiler detection inside Environment.
This commit is contained in:
parent
6502708b08
commit
016b9093d7
|
@ -67,7 +67,7 @@ class Builder():
|
||||||
raise interpreter.InvalidCode('Builder file is empty.')
|
raise interpreter.InvalidCode('Builder file is empty.')
|
||||||
assert(isinstance(code, str))
|
assert(isinstance(code, str))
|
||||||
env = environment.Environment(self.source_dir, self.build_dir)
|
env = environment.Environment(self.source_dir, self.build_dir)
|
||||||
intr = interpreter.Interpreter(code, env.get_scratch_dir())
|
intr = interpreter.Interpreter(code, env)
|
||||||
g = shellgenerator.ShellGenerator(intr, env)
|
g = shellgenerator.ShellGenerator(intr, env)
|
||||||
g.generate()
|
g.generate()
|
||||||
|
|
||||||
|
|
|
@ -20,16 +20,6 @@ class EnvironmentException(Exception):
|
||||||
def __init(self, *args, **kwargs):
|
def __init(self, *args, **kwargs):
|
||||||
Exception.__init__(self, *args, **kwargs)
|
Exception.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
def detect_c_compiler(execmd):
|
|
||||||
exelist = execmd.split()
|
|
||||||
p = subprocess.Popen(exelist + ['--version'], stdout=subprocess.PIPE)
|
|
||||||
out = p.communicate()[0]
|
|
||||||
out = out.decode()
|
|
||||||
if (out.startswith('cc ') or out.startswith('gcc')) and \
|
|
||||||
'Free Software Foundation' in out:
|
|
||||||
return GnuCCompiler(exelist)
|
|
||||||
raise EnvironmentException('Unknown compiler "' + execmd + '"')
|
|
||||||
|
|
||||||
class CCompiler():
|
class CCompiler():
|
||||||
def __init__(self, exelist):
|
def __init__(self, exelist):
|
||||||
if type(exelist) == type(''):
|
if type(exelist) == type(''):
|
||||||
|
@ -91,19 +81,6 @@ class GnuCCompiler(CCompiler):
|
||||||
def shell_quote(cmdlist):
|
def shell_quote(cmdlist):
|
||||||
return ["'" + x + "'" for x in cmdlist]
|
return ["'" + x + "'" for x in cmdlist]
|
||||||
|
|
||||||
def test_cmd_line_building():
|
|
||||||
src_file = 'file.c'
|
|
||||||
dst_file = 'file.o'
|
|
||||||
gnuc = detect_c_compiler('/usr/bin/cc')
|
|
||||||
cmds = gnuc.get_exelist()
|
|
||||||
cmds += gnuc.get_std_warn_flags()
|
|
||||||
cmds += gnuc.get_compile_only_flags()
|
|
||||||
cmds.append(src_file)
|
|
||||||
cmds += gnuc.get_output_flags()
|
|
||||||
cmds.append(dst_file)
|
|
||||||
cmd_line = ' '.join(shell_quote(cmds))
|
|
||||||
print(cmd_line)
|
|
||||||
|
|
||||||
class Environment():
|
class Environment():
|
||||||
def __init__(self, source_dir, build_dir):
|
def __init__(self, source_dir, build_dir):
|
||||||
self.source_dir = source_dir
|
self.source_dir = source_dir
|
||||||
|
@ -121,16 +98,26 @@ class Environment():
|
||||||
self.static_lib_prefix = 'lib'
|
self.static_lib_prefix = 'lib'
|
||||||
self.object_suffix = 'o'
|
self.object_suffix = 'o'
|
||||||
|
|
||||||
def get_c_compiler(self):
|
def get_c_compiler_exelist(self):
|
||||||
evar = 'CC'
|
evar = 'CC'
|
||||||
if evar in os.environ:
|
if evar in os.environ:
|
||||||
return os.environ[evar].split()
|
return os.environ[evar].split()
|
||||||
return self.default_c
|
return self.default_c
|
||||||
|
|
||||||
|
def detect_c_compiler(self):
|
||||||
|
exelist = self.get_c_compiler_exelist()
|
||||||
|
p = subprocess.Popen(exelist + ['--version'], stdout=subprocess.PIPE)
|
||||||
|
out = p.communicate()[0]
|
||||||
|
out = out.decode()
|
||||||
|
if (out.startswith('cc ') or out.startswith('gcc')) and \
|
||||||
|
'Free Software Foundation' in out:
|
||||||
|
return GnuCCompiler(exelist)
|
||||||
|
raise EnvironmentException('Unknown compiler "' + execmd + '"')
|
||||||
|
|
||||||
def get_scratch_dir(self):
|
def get_scratch_dir(self):
|
||||||
return self.scratch_dir
|
return self.scratch_dir
|
||||||
|
|
||||||
def get_cxx_compiler(self):
|
def get_cxx_compiler_exelist(self):
|
||||||
evar = 'CXX'
|
evar = 'CXX'
|
||||||
if evar in os.environ:
|
if evar in os.environ:
|
||||||
return os.environ[evar].split()
|
return os.environ[evar].split()
|
||||||
|
|
|
@ -66,14 +66,14 @@ class Executable(BuildTarget):
|
||||||
|
|
||||||
class Interpreter():
|
class Interpreter():
|
||||||
|
|
||||||
def __init__(self, code, scratch_dir):
|
def __init__(self, code, environment):
|
||||||
self.ast = parser.build_ast(code)
|
self.ast = parser.build_ast(code)
|
||||||
self.sanity_check_ast()
|
self.sanity_check_ast()
|
||||||
self.project = None
|
self.project = None
|
||||||
self.compilers = []
|
self.compilers = []
|
||||||
self.targets = {}
|
self.targets = {}
|
||||||
self.variables = {}
|
self.variables = {}
|
||||||
self.scratch_dir = scratch_dir
|
self.environment = environment
|
||||||
|
|
||||||
def get_project(self):
|
def get_project(self):
|
||||||
return self.project
|
return self.project
|
||||||
|
@ -141,8 +141,8 @@ class Interpreter():
|
||||||
raise InvalidCode('Function language() can only be called once (line %d).' % node.lineno())
|
raise InvalidCode('Function language() can only be called once (line %d).' % node.lineno())
|
||||||
for lang in args:
|
for lang in args:
|
||||||
if lang.lower() == 'c':
|
if lang.lower() == 'c':
|
||||||
comp = environment.detect_c_compiler('gcc')
|
comp = self.environment.detect_c_compiler()
|
||||||
comp.sanity_check(self.scratch_dir)
|
comp.sanity_check(self.environment.get_scratch_dir())
|
||||||
self.compilers.append(comp)
|
self.compilers.append(comp)
|
||||||
else:
|
else:
|
||||||
raise InvalidCode('Tried to use unknown language "%s".' % lang)
|
raise InvalidCode('Tried to use unknown language "%s".' % lang)
|
||||||
|
@ -238,5 +238,5 @@ if __name__ == '__main__':
|
||||||
dep = find_dep('gtk+-3.0')
|
dep = find_dep('gtk+-3.0')
|
||||||
prog.add_dep(dep)
|
prog.add_dep(dep)
|
||||||
"""
|
"""
|
||||||
i = Interpreter(code, '.')
|
i = Interpreter(code, environment.Environment('.', 'work area'))
|
||||||
i.run()
|
i.run()
|
||||||
|
|
|
@ -98,6 +98,6 @@ if __name__ == '__main__':
|
||||||
import interpreter, environment
|
import interpreter, environment
|
||||||
os.chdir(os.path.split(__file__)[0])
|
os.chdir(os.path.split(__file__)[0])
|
||||||
envir = environment.Environment('.', 'work area')
|
envir = environment.Environment('.', 'work area')
|
||||||
intpr = interpreter.Interpreter(code, envir.get_scratch_dir())
|
intpr = interpreter.Interpreter(code, envir)
|
||||||
g = ShellGenerator(intpr, envir)
|
g = ShellGenerator(intpr, envir)
|
||||||
g.generate()
|
g.generate()
|
||||||
|
|
Loading…
Reference in New Issue