6.23.2. compiler package
This package
Start python section to interscript/compilers/c.py[1
/1
]
1: #line 123 "compilers.ipk"
2: import os
3: import sys
4: import string
5: import interscript.compilers.cconfig
6:
7: class python_module:
8: def __init__(self,**kwds):
9: self.config = interscript.compilers.cconfig.config()
10: self.config.append_dict(kwds)
11:
12: def configure(self,**kwds):
13: self.config.append_dict(kwds)
14:
15: def compile(self,filename, **kwds):
16: config = self.config.copy()
17: config.append_dict(kwds)
18:
19: base = string.join(string.split(filename,'.')[:-1],'.')
20: obj = base+'.o'
21: cc = 'gcc -g -O2 -fpic -fPIC -pedantic '
22: inc = '-I' + sys.prefix + '/include/python1.5 '
23: if sys.prefix != sys.exec_prefix:
24: inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
25: cstr = str(config)+' '
26: arg = cc + cstr + inc + '-c '+filename + ' -o '+ obj
27: print 'system',repr(arg)
28: os.system(arg)
29: return obj
30:
31: def link(self,modname, filenames, **kwds):
32: config = self.config.copy()
33: config.append_dict(kwds)
34:
35: dll = modname +'.so'
36: cc ='gcc -shared -Xlinker -export-dynamic '
37: cstr = str(config) + ' '
38: lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
39: files = string.join(filenames) + ' '
40: arg = cc + cstr + lib + files + '-o ' + dll
41:
42: print 'system',repr(arg)
43: os.system(arg)
44: return dll
45:
46: class application:
47: def __init__(self,**kwds):
48: self.config = interscript.compilers.cconfig.config()
49: self.config.append_dict(kwds)
50:
51: def configure(self,**kwds):
52: self.config.append_dict(kwds)
53:
54: def compile(self,filename, **kwds):
55: config = self.config.copy()
56: config.append_dict(kwds)
57:
58: base = string.join(string.split(filename,'.')[:-1],'.')
59: obj = base+'.o'
60: cc ='gcc -g -O2 -fpic -fPIC -pedantic '
61: inc = '-I' + sys.prefix + '/include/python1.5 '
62: if sys.prefix != sys.exec_prefix:
63: inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
64: cstr = str(config)+' '
65: arg = cc + cstr + inc + '-c '+filename + ' -o '+ obj
66: print 'system',repr(arg)
67: os.system(arg)
68: return obj
69:
70: def link(self,appname, filenames, **kwds):
71: config = self.config.copy()
72: config.append_dict(kwds)
73:
74: cc ='gcc '
75: cstr = str(config) + ' '
76: lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
77: files = string.join(filenames) + ' '
78: arg = cc + cstr + lib + files + '-o ' + appname
79:
80: print 'system',repr(arg)
81: os.system(arg)
82: return appname
83:
Start python section to interscript/compilers/cpp.py[1
/1
]
1: #line 206 "compilers.ipk"
2: import os
3: import sys
4: import string
5: import interscript.compilers.cconfig
6:
7: class python_module:
8: def __init__(self,**kwds):
9: self.config = compilers.cconfig.config()
10: self.config.append_dict(kwds)
11:
12: def configure(self,**kwds):
13: self.config.append_dict(kwds)
14:
15: def compile(self,filename, **kwds):
16: config = self.config.copy()
17: config.append_dict(kwds)
18:
19: base = string.join(string.split(filename,'.')[:-1],'.')
20: obj = base+'.o'
21: cc ='g++ -g -O2 -fhandle-exceptions -fpic -fPIC -pedantic '
22: inc = '-I' + sys.prefix + '/include/python1.5 '
23: if sys.prefix != sys.exec_prefix:
24: inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
25: cstr = str(config)+' '
26: arg = cc + cstr + inc + '-c '+filename + ' -o '+ obj
27: print 'system',repr(arg)
28: result = os.system(arg)
29: if result != 0:
30: raise 'Compiler Error'
31: return obj
32:
33: def link(self,modname, filenames, **kwds):
34: config = self.config.copy()
35: config.append_dict(kwds)
36:
37: cc ='g++ -shared -Xlinker -export-dynamic '
38: cstr = str(config) + ' '
39: lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
40: dll = modname +'.so'
41: files = string.join(filenames) + ' '
42: arg = cc + cstr + lib + files + '-o '+dll
43:
44: print 'system',repr(arg)
45: result = os.system(arg)
46: if result != 0:
47: raise 'Linker Error'
48: return dll
49:
50: class application:
51: def __init__(self,**kwds):
52: self.config = interscript.compilers.cconfig.config()
53: self.config.append_dict(kwds)
54:
55: def configure(self,**kwds):
56: self.config.append_dict(kwds)
57:
58: def compile(self,filename, **kwds):
59: config = self.config.copy()
60: config.append_dict(kwds)
61:
62: base = string.join(string.split(filename,'.')[:-1],'.')
63: obj = base+'.o'
64: cc ='g++ -g -O2 -fhandle-exceptions -fpic -fPIC -pedantic '
65: inc = '-I' + sys.prefix + '/include/python1.5 '
66: if sys.prefix != sys.exec_prefix:
67: inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
68: cstr = str(config)+' '
69: arg = cc + cstr + inc + '-c '+filename + ' -o '+ obj
70: print 'system',repr(arg)
71: result = os.system(arg)
72: if result != 0:
73: raise 'Compiler Error'
74: return obj
75:
76: def link(self,appname, filenames, **kwds):
77: config = self.config.copy()
78: config.append_dict(kwds)
79:
80: cc ='g++ '
81: cstr = str(config) + ' '
82: lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
83: files = string.join(filenames) + ' '
84: arg = cc + cstr + lib + files + '-o '+appname
85:
86: print 'system',repr(arg)
87: result = os.system(arg)
88: if result != 0:
89: raise 'Linker Error'
90: return appname
91:
Start python section to interscript/compilers/cconfig.py[1
/1
]
1: #line 297 "compilers.ipk"
2: import os
3: import sys
4: import string
5:
6: class config:
7: def __init__(self,**kwds):
8: self.libdirs = []
9: self.incdirs = []
10: self.libs = []
11: self.macros = {}
12: self.switches = {}
13: self.extra = ''
14: self.append_dict(kwds)
15:
16: def copy(self):
17: c = config()
18: c.libdirs = self.libdirs[:]
19: c.incdirs = self.incdirs[:]
20: c.libs = self.libs[:]
21: c.macros = self.macros.copy()
22: c.switches = self.switches.copy()
23: c.extra = self.extra
24: return c
25:
26: def append_kwds(self,**kwds):
27: self.append_dict(kwds)
28:
29: def append_dict(self,kwds):
30: if kwds.has_key('libdirs'):
31: self.libdirs[-1:-1]=kwds['libdirs']
32: if kwds.has_key('incdirs'):
33: self.incdirs[-1:-1]=kwds['incdirs']
34: if kwds.has_key('libs'):
35: self.libs[-1:-1]=kwds['libs']
36: if kwds.has_key('extra'):
37: self.extra = self.extra + ' ' + kwds['extra']
38: if kwds.has_key('macros'):
39: macros = kwds['macros']
40: for macro in macros:
41: self.macros[macro] = macros[macro]
42: if kwds.has_key('switches'):
43: switches = kwds['switches']
44: for switch in switches:
45: self.switches[switch] = switches[switch]
46:
47: def __str__(self):
48: s = self.extra
49: for x in self.libdirs: s = s + ' -L' + x
50: for x in self.incdirs : s = s + ' -I' + x
51: for x in self.libs: s = s + ' -l' + x
52: for x in self.macros.keys():
53: s = s + ' -D' + x
54: if self.macros[x]: s = s + '=' + self.macros[x]
55: for x in self.switches.keys():
56: s = s + ' -' + x + self.switches[x]
57: return s
58:
Start python section to interscript/compilers/__init__.py[1
/1
]
1: #line 355 "compilers.ipk"
2:
Start python section to interscript/tests/test_compilers.py[1
/1
]
1: #line 357 "compilers.ipk"
2: import os
3: import sys
4: sys.path = ['']+sys.path
5: import interscript.compilers.c
6: cc = interscript.compilers.c.application()
7: obj = cc.compile('interscript/tests/example.c')
8: print 'Object file',obj,'generated'
9: exe = cc.link('interscript/tests/example.exe',[obj])
10: print 'Executable',exe,'generated'
11:
12: os.system('interscript/tests/example.exe')
13: print 'Executable executed'
14:
Start C section to interscript/tests/example.c[1
/1
]
1: #line 371 "compilers.ipk"
2: #include <stdio.h>
3: void main() {
4: printf("Hello World from compiler test\n");
5: }
6:
6.23.2.1. Test