Can generate compile commands.

pull/15/head
Jussi Pakkanen 12 years ago
parent e006116936
commit a7b991bfd4
  1. 6
      environment.py
  2. 9
      interpreter.py
  3. 45
      shellgenerator.py

@ -49,7 +49,7 @@ class CCompiler():
return ['-o']
def can_compile(self, filename):
suffix = filename.split['.'][-1]
suffix = filename.split('.')[-1]
if suffix == 'c' or suffix == 'h':
return True
return False
@ -114,6 +114,7 @@ class Environment():
self.shared_lib_prefix = 'lib'
self.static_lib_suffix = 'a'
self.static_lib_prefix = 'lib'
self.object_suffix = 'o'
def get_c_compiler(self):
evar = 'CC'
@ -147,6 +148,9 @@ class Environment():
def get_static_lib_suffix(self):
return self.static_lib_suffix
def get_object_suffix(self):
return self.object_suffix
if __name__ == '__main__':
test_cmd_line_building()

@ -35,6 +35,12 @@ class Executable(InterpreterObject):
def __init__(self, name, sources):
self.name = name
self.sources = sources
def get_basename(self):
return self.name
def get_sources(self):
return self.sources
class Interpreter():
@ -46,6 +52,9 @@ class Interpreter():
self.compilers = []
self.executables = {}
def get_executables(self):
return self.executables
def sanity_check_ast(self):
if not isinstance(self.ast, nodes.CodeBlock):
raise InvalidCode('AST is of invalid type. Possibly a bug in the parser.')

@ -14,15 +14,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import interpreter
from environment import Environment
import interpreter, environment
import os, stat
class ShellGenerator():
def __init__(self, code, source_dir, build_dir):
self.code = code
self.environment = Environment(source_dir, build_dir)
self.environment = environment.Environment(source_dir, build_dir)
self.interpreter = interpreter.Interpreter(code)
self.build_filename = 'compile.sh'
@ -31,11 +30,49 @@ class ShellGenerator():
outfilename = os.path.join(self.environment.get_build_dir(), self.build_filename)
outfile = open(outfilename, 'w')
outfile.write('#!/bin/sh\n')
outfile.write('echo This is the output\n')
self.generate_commands(outfile)
outfile.close()
os.chmod(outfilename, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |\
stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
def generate_single_compile(self, outfile, src):
compiler = None
for i in self.interpreter.compilers:
if i.can_compile(src):
compiler = i
break
if compiler is None:
raise RuntimeError('No specified compiler can handle file ' + src)
abs_src = os.path.join(self.environment.get_source_dir(), src)
abs_obj = os.path.join(self.environment.get_build_dir(), src)
abs_obj += '.' + self.environment.get_object_suffix()
commands = compiler.get_exelist()
commands += compiler.get_std_warn_flags()
commands += compiler.get_compile_only_flags()
commands.append(abs_src)
commands += compiler.get_output_flags()
commands.append(abs_obj)
quoted = environment.shell_quote(commands) + ['\n']
outfile.write(' '.join(quoted))
return abs_obj
def generate_exe_link(self, outfile, outname, obj_list):
outfile.write('Linking %s with files %s.\n' % (outname, ' '.join(obj_list)))
def generate_commands(self, outfile):
for i in self.interpreter.get_executables().items():
name = i[0]
e = i[1]
print('Generating executable', name)
outname = os.path.join(self.environment.get_build_dir(), e.get_basename())
suffix = self.environment.get_exe_suffix()
if suffix != '':
outname = outname + '.' + suffix
obj_list = []
for src in e.get_sources():
obj_list.append(self.generate_single_compile(outfile, src))
self.generate_exe_link(outfile, outname, obj_list)
if __name__ == '__main__':
code = """
project('simple generator')

Loading…
Cancel
Save