[Distutils-sig] Compilers module

John Skaller skaller@maxtal.com.au
Sat, 05 Dec 1998 00:45:45 +1000


Well, for those who haven't looked at interscript, here is the
compilers module for comment. These modules work with gcc on my
box. The idea is to _require_ the client implement these
modules (providing some common implementation such as this one).

That way, there is a standard _interface_ for compiling
external tools and python modules, which can be used
by author build software in a platform independent way.

------------ compilers.py ---------------------------------------
#line 13 "compilers.ipk"
import os
import sys
import string
import interscript.compilers.cconfig


class python_module:
  def __init__(self,**kwds):
    self.config = interscript.compilers.cconfig.config()
    self.config.append_dict(kwds)

  def configure(self,**kwds):
    self.config.append_dict(kwds)

  def compile(self,filename, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    base = string.join(string.split(filename,'.')[:-1],'.')
    obj = base+'.o'
    cc ='gcc -g -O2 -fpic -fPIC -pedantic '
    inc = '-I' + sys.prefix + '/include/python1.5 '
    if sys.prefix != sys.exec_prefix:
      inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
    cstr = str(config)+' '
    arg = cc + cstr +  inc + '-c '+filename + ' -o '+ obj
    print 'system',repr(arg)
    os.system(arg)
    return obj

  def link(self,modname, filenames, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    dll = modname +'.so'
    cc ='gcc -shared -Xlinker -export-dynamic '
    cstr = str(config) + ' '
    lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
    files = string.join(filenames) + ' '
    arg = cc + cstr + lib + files + '-o ' + dll

    print 'system',repr(arg)
    os.system(arg)
    return dll

class application:
  def __init__(self,**kwds):
    self.config = interscript.compilers.cconfig.config()
    self.config.append_dict(kwds)

  def configure(self,**kwds):
    self.config.append_dict(kwds)

  def compile(self,filename, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    base = string.join(string.split(filename,'.')[:-1],'.')
    obj = base+'.o'
    cc ='gcc -g -O2 -fpic -fPIC -pedantic '
    inc = '-I' + sys.prefix + '/include/python1.5 '
    if sys.prefix != sys.exec_prefix:
      inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
    cstr = str(config)+' '
    arg = cc + cstr +  inc + '-c '+filename + ' -o '+ obj
    print 'system',repr(arg)
    os.system(arg)
    return obj

  def link(self,appname, filenames, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    cc ='gcc '
    cstr = str(config) + ' '
    lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
    files = string.join(filenames) + ' '
    arg = cc + cstr + lib + files + '-o ' + appname

    print 'system',repr(arg)
    os.system(arg)
    return appname
------------ cconfig.py ---------
#line 188 "compilers.ipk"
import os
import sys
import string

class config:
  def __init__(self,**kwds):
    self.libdirs = []
    self.incdirs = []
    self.libs = []
    self.macros = {}
    self.switches = {}
    self.extra = ''
    self.append_dict(kwds)

  def copy(self):
    c = config()
    c.libdirs = self.libdirs[:]
    c.incdirs = self.incdirs[:]
    c.libs = self.libs[:]
    c.macros = self.macros.copy()
    c.switches = self.switches.copy()
    c.extra = self.extra
    return c

  def append_kwds(self,**kwds):
    self.append_dict(kwds)

  def append_dict(self,kwds):
    if kwds.has_key('libdirs'):
      self.libdirs[-1:-1]=kwds['libdirs']
    if kwds.has_key('incdirs'):
      self.incdirs[-1:-1]=kwds['incdirs']
    if kwds.has_key('libs'):
      self.libs[-1:-1]=kwds['libs']
    if kwds.has_key('extra'):
      self.extra = self.extra + ' ' + kwds['extra']
    if kwds.has_key('macros'):
      macros = kwds['macros']
      for macro in macros:
        self.macros[macro] = macros[macro]
    if kwds.has_key('switches'):
      switches = kwds['switches']
      for switch in switches:
        self.switches[switch] = switches[switch]

  def __str__(self):
    s = self.extra
    for x in self.libdirs: s = s + ' -L' + x
    for x in self.incdirs : s = s + ' -I' + x
    for x in self.libs: s = s + ' -l' + x
    for x in self.macros.keys():
      s = s + ' -D' + x
      if self.macros[x]: s = s + '=' + self.macros[x]
    for x in self.switches.keys():
      s = s + ' -' + x + self.switches[x]
    return s
-------------------------------------------------------
John Skaller    email: skaller@maxtal.com.au
		http://www.maxtal.com.au/~skaller
		phone: 61-2-96600850
		snail: 10/1 Toxteth Rd, Glebe NSW 2037, Australia