Added support for C++.

pull/15/head
Jussi Pakkanen 13 years ago
parent a1efb27a8e
commit e01200a4ab
  1. 60
      environment.py
  2. 4
      interpreter.py
  3. 3
      test cases/2 cxx/builder.txt
  4. 5
      test cases/2 cxx/trivial.cc

@ -63,12 +63,38 @@ class CCompiler():
pe = subprocess.Popen(binary_name)
pe.wait()
if pe.returncode != 0:
raise RuntimeError('Executables created by compiler %s are not runnable.' % self.name_string())
raise RuntimeError('Executables created by C compiler %s are not runnable.' % self.name_string())
class CXXCompiler(CCompiler):
def __init__(self, exelist):
CCompiler.__init__(self, exelist)
def can_compile(self, filename):
suffix = filename.split('.')[-1]
if suffix == 'cc' or suffix == 'cpp' or suffix == 'cxx' or \
suffix == 'hh' or suffix == 'hpp' or suffix == 'hxx':
return True
return False
def sanity_check(self, work_dir):
source_name = os.path.join(work_dir, 'sanitycheckcxx.cc')
binary_name = os.path.join(work_dir, 'sanitycheckcxx')
ofile = open(source_name, 'w')
ofile.write('class breakCCompiler;int main(int argc, char **argv) { return 0; }\n')
ofile.close()
pc = subprocess.Popen(self.exelist + [source_name, '-o', binary_name])
pc.wait()
if pc.returncode != 0:
raise RuntimeError('Compiler %s can not compile programs.' % self.name_string())
pe = subprocess.Popen(binary_name)
pe.wait()
if pe.returncode != 0:
raise RuntimeError('Executables created by C++ compiler %s are not runnable.' % self.name_string())
class GnuCCompiler(CCompiler):
std_warn_flags = ['-Wall', '-Winvalid-pch']
std_opt_flags = ['-O2']
def __init__(self, exelist):
CCompiler.__init__(self, exelist)
@ -78,6 +104,20 @@ class GnuCCompiler(CCompiler):
def get_std_opt_flags(self):
return GnuCCompiler.std_opt_flags
class GnuCXXCompiler(CXXCompiler):
std_warn_flags = ['-Wall', '-Winvalid-pch']
std_opt_flags = ['-O2']
def __init__(self, exelist):
CXXCompiler.__init__(self, exelist)
def get_std_warn_flags(self):
return GnuCXXCompiler.std_warn_flags
def get_std_opt_flags(self):
return GnuCXXCompiler.std_opt_flags
class Environment():
def __init__(self, source_dir, build_dir):
self.source_dir = source_dir
@ -100,7 +140,7 @@ class Environment():
if evar in os.environ:
return os.environ[evar].split()
return self.default_c
def detect_c_compiler(self):
exelist = self.get_c_compiler_exelist()
p = subprocess.Popen(exelist + ['--version'], stdout=subprocess.PIPE)
@ -109,11 +149,21 @@ class Environment():
if (out.startswith('cc ') or out.startswith('gcc')) and \
'Free Software Foundation' in out:
return GnuCCompiler(exelist)
raise EnvironmentException('Unknown compiler "' + execmd + '"')
raise EnvironmentException('Unknown compiler "' + ' '.join(exelist) + '"')
def get_scratch_dir(self):
return self.scratch_dir
def detect_cxx_compiler(self):
exelist = self.get_cxx_compiler_exelist()
p = subprocess.Popen(exelist + ['--version'], stdout=subprocess.PIPE)
out = p.communicate()[0]
out = out.decode()
if (out.startswith('c++ ') or out.startswith('g++')) and \
'Free Software Foundation' in out:
return GnuCXXCompiler(exelist)
raise EnvironmentException('Unknown compiler "' + ' '.join(exelist) + '"')
def get_cxx_compiler_exelist(self):
evar = 'CXX'
if evar in os.environ:

@ -144,6 +144,10 @@ class Interpreter():
comp = self.environment.detect_c_compiler()
comp.sanity_check(self.environment.get_scratch_dir())
self.compilers.append(comp)
elif lang.lower() == 'c++':
comp = self.environment.detect_cxx_compiler()
comp.sanity_check(self.environment.get_scratch_dir())
self.compilers.append(comp)
else:
raise InvalidCode('Tried to use unknown language "%s".' % lang)

@ -0,0 +1,3 @@
project('trivial test')
language('c++')
exe = executable('trivialprog', 'trivial.cc')

@ -0,0 +1,5 @@
#include<iostream>
int main(int argc, char **argv) {
return 0;
}
Loading…
Cancel
Save